merged
authorEdouard Tisserant
Tue, 05 May 2015 17:27:57 +0200
changeset 24543b4cf6fe548
parent 244 36abc78b7de5
parent 242 6736f061c8f8
child 247 12e852387299
child 252 d0aa4a732456
merged
     1.1 --- a/src/message_api.c	Tue May 05 17:23:38 2015 +0200
     1.2 +++ b/src/message_api.c	Tue May 05 17:27:57 2015 +0200
     1.3 @@ -52,6 +52,7 @@
     1.4  
     1.5  void import_attached_keys(PEP_SESSION session, const message *msg)
     1.6  {
     1.7 +    assert(session);
     1.8      assert(msg);
     1.9  
    1.10      bloblist_t *bl;
    1.11 @@ -76,6 +77,34 @@
    1.12      }
    1.13  }
    1.14  
    1.15 +void attach_own_key(PEP_SESSION session, message *msg)
    1.16 +{
    1.17 +    char *keydata;
    1.18 +    size_t size;
    1.19 +    bloblist_t *bl;
    1.20 +
    1.21 +    assert(session);
    1.22 +    assert(msg);
    1.23 +
    1.24 +    if (msg->dir == PEP_dir_incoming)
    1.25 +        return;
    1.26 +
    1.27 +    assert(msg->from && msg->from->fpr);
    1.28 +    if (msg->from == NULL || msg->from->fpr == NULL)
    1.29 +        return;
    1.30 +
    1.31 +    PEP_STATUS status = export_key(session, msg->from->fpr, &keydata, &size);
    1.32 +    assert(status == PEP_STATUS_OK);
    1.33 +    if (status != PEP_STATUS_OK)
    1.34 +        return;
    1.35 +    assert(size);
    1.36 +
    1.37 +    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
    1.38 +            "pEp_key.asc");
    1.39 +    if (bl)
    1.40 +        msg->attachments = bl;
    1.41 +}
    1.42 +
    1.43  static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
    1.44  {
    1.45      char * ptext;
    1.46 @@ -329,26 +358,28 @@
    1.47          else {
    1.48              // decrypt and re-encrypt again
    1.49              message * _dst = NULL;
    1.50 +            stringlist_t *_keylist = NULL;
    1.51              PEP_MIME_format mime = (enc_format == PEP_enc_PEP) ? PEP_MIME :
    1.52                      PEP_MIME_fields_omitted;
    1.53  
    1.54 -            status = decrypt_message(session, src, mime, &_dst);
    1.55 +            status = decrypt_message(session, src, mime, &_dst, &_keylist);
    1.56              if (status != PEP_STATUS_OK)
    1.57                  goto pep_error;
    1.58 +            free_stringlist(_keylist);
    1.59  
    1.60              src = _dst;
    1.61              free_src = true;
    1.62          }
    1.63      }
    1.64  
    1.65 +    status = myself(session, src->from);
    1.66 +    if (status != PEP_STATUS_OK)
    1.67 +        goto pep_error;
    1.68 +
    1.69      msg = clone_to_empty_message(src);
    1.70      if (msg == NULL)
    1.71          goto enomem;
    1.72  
    1.73 -    status = myself(session, src->from);
    1.74 -    if (status != PEP_STATUS_OK)
    1.75 -        goto pep_error;
    1.76 -
    1.77      keys = new_stringlist(src->from->fpr);
    1.78      if (keys == NULL)
    1.79          goto enomem;
    1.80 @@ -545,7 +576,7 @@
    1.81      if (msg->shortmsg == NULL)
    1.82          msg->shortmsg = strdup("pEp");
    1.83  
    1.84 -    import_attached_keys(session, msg);
    1.85 +    attach_own_key(session, msg);
    1.86  
    1.87      *dst = msg;
    1.88      return PEP_STATUS_OK;
    1.89 @@ -619,7 +650,8 @@
    1.90          PEP_SESSION session,
    1.91          message *src,
    1.92          PEP_MIME_format mime,
    1.93 -        message **dst
    1.94 +        message **dst,
    1.95 +        stringlist_t **keylist
    1.96      )
    1.97  {
    1.98      PEP_STATUS status = PEP_STATUS_OK;
    1.99 @@ -628,14 +660,15 @@
   1.100      size_t csize;
   1.101      char *ptext;
   1.102      size_t psize;
   1.103 -    stringlist_t *keylist;
   1.104 +    stringlist_t *_keylist = NULL;
   1.105      bool free_src = false;
   1.106  
   1.107      assert(session);
   1.108      assert(src);
   1.109      assert(dst);
   1.110 +    assert(keylist);
   1.111  
   1.112 -    if (!(session && src && dst))
   1.113 +    if (!(session && src && dst && keylist))
   1.114          return PEP_ILLEGAL_VALUE;
   1.115  
   1.116      *dst = NULL;
   1.117 @@ -672,7 +705,7 @@
   1.118      csize = strlen(src->longmsg);
   1.119  
   1.120      status = decrypt_and_verify(session, ctext, csize, &ptext, &psize,
   1.121 -            &keylist);
   1.122 +            &_keylist);
   1.123      if (ptext == NULL)
   1.124          goto pep_error;
   1.125  
   1.126 @@ -697,14 +730,16 @@
   1.127              bloblist_t *_s;
   1.128              for (_s = src->attachments; _s; _s = _s->next) {
   1.129                  if (is_encrypted_attachment(_s)) {
   1.130 +                    stringlist_t *_keylist = NULL;
   1.131                      ctext = _s->data;
   1.132                      csize = _s->size;
   1.133  
   1.134                      status = decrypt_and_verify(session, ctext, csize, &ptext,
   1.135 -                            &psize, &keylist);
   1.136 +                            &psize, &_keylist);
   1.137                      if (ptext == NULL)
   1.138                          goto pep_error;
   1.139 -                    
   1.140 +                    free_stringlist(_keylist);
   1.141 +
   1.142                      if (is_encrypted_html_attachment(_s)) {
   1.143                          msg->longmsg_formatted = strdup(ptext);
   1.144                          if (msg->longmsg_formatted == NULL)
   1.145 @@ -811,6 +846,8 @@
   1.146      import_attached_keys(session, msg);
   1.147  
   1.148      *dst = msg;
   1.149 +    *keylist = _keylist;
   1.150 +
   1.151      return PEP_STATUS_OK;
   1.152  
   1.153  enomem:
   1.154 @@ -818,6 +855,7 @@
   1.155  
   1.156  pep_error:
   1.157      free_message(msg);
   1.158 +    free_stringlist(_keylist);
   1.159      if (free_src)
   1.160          free_message(src);
   1.161  
   1.162 @@ -846,7 +884,31 @@
   1.163      }
   1.164  }
   1.165  
   1.166 -DYNAMIC_API PEP_STATUS get_message_color(
   1.167 +static PEP_color _rating(PEP_comm_type ct)
   1.168 +{
   1.169 +    if (ct == PEP_ct_unknown)
   1.170 +        return PEP_rating_undefined;
   1.171 +
   1.172 +    else if (ct == PEP_ct_compromized)
   1.173 +        return PEP_rating_under_attack;
   1.174 +
   1.175 +    else if (ct >= PEP_ct_confirmed_enc_anon)
   1.176 +        return PEP_rating_trusted_and_anonymized;
   1.177 +
   1.178 +    else if (ct >= PEP_ct_strong_encryption)
   1.179 +        return PEP_rating_trusted;
   1.180 +
   1.181 +    else if (ct >= PEP_ct_strong_but_unconfirmed && ct < PEP_ct_confirmed)
   1.182 +        return PEP_rating_reliable;
   1.183 +    
   1.184 +    else if (ct == PEP_ct_no_encryption || ct == PEP_ct_no_encrypted_channel)
   1.185 +        return PEP_rating_unencrypted;
   1.186 +
   1.187 +    else
   1.188 +        return PEP_rating_unreliable;
   1.189 +}
   1.190 +
   1.191 +DYNAMIC_API PEP_STATUS message_color(
   1.192          PEP_SESSION session,
   1.193          message *msg,
   1.194          PEP_color *color
   1.195 @@ -864,7 +926,7 @@
   1.196      if (!(session && msg && color))
   1.197          return PEP_ILLEGAL_VALUE;
   1.198  
   1.199 -    *color = PEP_undefined;
   1.200 +    *color = PEP_rating_undefined;
   1.201  
   1.202      assert(msg->from);
   1.203      if (msg->from == NULL)
   1.204 @@ -899,14 +961,6 @@
   1.205                      comm_type_determined = true;
   1.206                  }
   1.207              }
   1.208 -
   1.209 -            for (il = msg->bcc; il != NULL; il = il->next) {
   1.210 -                if (il->ident) {
   1.211 -                    max_comm_type = _get_comm_type(session, max_comm_type,
   1.212 -                            il->ident);
   1.213 -                    comm_type_determined = true;
   1.214 -                }
   1.215 -            }
   1.216              break;
   1.217  
   1.218          default:
   1.219 @@ -914,31 +968,36 @@
   1.220      }
   1.221  
   1.222      if (comm_type_determined == false)
   1.223 -        *color = PEP_undefined;
   1.224 -
   1.225 -    else if (max_comm_type == PEP_ct_compromized)
   1.226 -        *color = PEP_under_attack;
   1.227 -
   1.228 -    else if (max_comm_type >= PEP_ct_confirmed_enc_anon)
   1.229 -        *color = PEP_trusted_and_anonymized;
   1.230 -
   1.231 -    else if (max_comm_type >= PEP_ct_strong_encryption)
   1.232 -        *color = PEP_trusted;
   1.233 -
   1.234 -    else if (max_comm_type >= PEP_ct_strong_but_unconfirmed &&
   1.235 -            max_comm_type < PEP_ct_confirmed)
   1.236 -        *color = PEP_reliable;
   1.237 -    
   1.238 -    else if (max_comm_type == PEP_ct_no_encryption ||
   1.239 -            max_comm_type == PEP_ct_no_encrypted_channel)
   1.240 -        *color = PEP_unencrypted;
   1.241 -
   1.242 -    else if (max_comm_type == PEP_ct_unknown)
   1.243 -        *color = PEP_undefined;
   1.244 -
   1.245 +        *color = PEP_rating_undefined;
   1.246      else
   1.247 -        *color = PEP_unreliable;
   1.248 +        *color = _rating(max_comm_type);
   1.249  
   1.250      return PEP_STATUS_OK;
   1.251  }
   1.252  
   1.253 +DYNAMIC_API PEP_STATUS identity_color(
   1.254 +        PEP_SESSION session,
   1.255 +        pEp_identity *ident,
   1.256 +        PEP_color *color
   1.257 +    )
   1.258 +{
   1.259 +    PEP_STATUS status = PEP_STATUS_OK;
   1.260 +
   1.261 +    assert(session);
   1.262 +    assert(ident);
   1.263 +    assert(color);
   1.264 +
   1.265 +    if (!(session && ident && color))
   1.266 +        return PEP_ILLEGAL_VALUE;
   1.267 +
   1.268 +    if (ident->me)
   1.269 +        status = myself(session, ident);
   1.270 +    else
   1.271 +        status = update_identity(session, ident);
   1.272 +
   1.273 +    if (status == PEP_STATUS_OK)
   1.274 +        *color = _rating(ident->comm_type);
   1.275 +
   1.276 +    return status;
   1.277 +}
   1.278 +
     2.1 --- a/src/message_api.h	Tue May 05 17:23:38 2015 +0200
     2.2 +++ b/src/message_api.h	Tue May 05 17:27:57 2015 +0200
     2.3 @@ -10,6 +10,7 @@
     2.4  
     2.5  
     2.6  void import_attached_keys(PEP_SESSION session, const message *msg);
     2.7 +void attach_own_key(PEP_SESSION session, message *msg);
     2.8  
     2.9  
    2.10  // encrypt_message() - encrypt message in memory
    2.11 @@ -50,6 +51,7 @@
    2.12  //      src (in)            message to decrypt
    2.13  //      mime (in)           MIME encoding wanted
    2.14  //      dst (out)           pointer to new decrypted message or NULL on failure
    2.15 +//      keylist (out)       stringlist with keyids
    2.16  //
    2.17  //  return value:
    2.18  //      error status or PEP_STATUS_OK on success
    2.19 @@ -61,27 +63,29 @@
    2.20          PEP_SESSION session,
    2.21          message *src,
    2.22          PEP_MIME_format mime,
    2.23 -        message **dst
    2.24 +        message **dst,
    2.25 +        stringlist_t **keylist
    2.26      );
    2.27  
    2.28  
    2.29  typedef enum _PEP_color {
    2.30 -    PEP_undefined = 0,
    2.31 -    PEP_unencrypted,
    2.32 -    PEP_unreliable,
    2.33 -    PEP_reliable,
    2.34 -    PEP_yellow = PEP_reliable,
    2.35 -    PEP_trusted,
    2.36 -    PEP_green = PEP_trusted,
    2.37 -    PEP_trusted_and_anonymized,
    2.38 -    PEP_fully_anonymous,   
    2.39 +    PEP_rating_undefined = 0,
    2.40 +    PEP_rating_unencrypted,
    2.41 +    PEP_rating_cannot_decrypt,
    2.42 +    PEP_rating_unreliable,
    2.43 +    PEP_rating_reliable,
    2.44 +    PEP_rating_yellow = PEP_rating_reliable,
    2.45 +    PEP_rating_trusted,
    2.46 +    PEP_rating_green = PEP_rating_trusted,
    2.47 +    PEP_rating_trusted_and_anonymized,
    2.48 +    PEP_rating_fully_anonymous,   
    2.49  
    2.50 -    PEP_under_attack = -1,
    2.51 -    PEP_red = PEP_under_attack,
    2.52 -    PEP_b0rken = -2
    2.53 +    PEP_rating_under_attack = -1,
    2.54 +    PEP_rating_red = PEP_rating_under_attack,
    2.55 +    PEP_rating_b0rken = -2
    2.56  } PEP_color;
    2.57  
    2.58 -// get_message_color() - get color for a message
    2.59 +// message_color() - get color for a message
    2.60  //
    2.61  //  parameters:
    2.62  //      session (in)        session handle
    2.63 @@ -94,12 +98,30 @@
    2.64  //  caveat:
    2.65  //      msg->from must point to a valid pEp_identity
    2.66  
    2.67 -DYNAMIC_API PEP_STATUS get_message_color(
    2.68 +DYNAMIC_API PEP_STATUS message_color(
    2.69          PEP_SESSION session,
    2.70          message *msg,
    2.71          PEP_color *color
    2.72      );
    2.73  
    2.74 +
    2.75 +// identity_color() - get color for a single identity
    2.76 +//
    2.77 +//  parameters:
    2.78 +//      session (in)        session handle
    2.79 +//      ident (in)          identity to get the color for
    2.80 +//      color (out)         color for the identity
    2.81 +//
    2.82 +//  return value:
    2.83 +//      error status or PEP_STATUS_OK on success
    2.84 +
    2.85 +DYNAMIC_API PEP_STATUS identity_color(
    2.86 +        PEP_SESSION session,
    2.87 +        pEp_identity *ident,
    2.88 +        PEP_color *color
    2.89 +    );
    2.90 +
    2.91 +
    2.92  #ifdef __cplusplus
    2.93  }
    2.94  #endif
     3.1 --- a/test/message_api_test.cc	Tue May 05 17:23:38 2015 +0200
     3.2 +++ b/test/message_api_test.cc	Tue May 05 17:27:57 2015 +0200
     3.3 @@ -65,9 +65,19 @@
     3.4      outFile3.close();
     3.5  
     3.6      message *msg4;
     3.7 -    PEP_STATUS status4 = decrypt_message(session, enc_msg2, PEP_MIME_none, &msg4);
     3.8 +    stringlist_t *keylist4;
     3.9 +    PEP_STATUS status4 = decrypt_message(session, enc_msg2, PEP_MIME_none, &msg4, &keylist4);
    3.10      assert(status4 == PEP_STATUS_OK);
    3.11      assert(msg4);
    3.12 +    assert(keylist4);
    3.13 +
    3.14 +    cout << "keys used:";
    3.15 +    stringlist_t *kl4;
    3.16 +    for (kl4 = keylist4; kl4 && kl4->value; kl4 = kl4->next)
    3.17 +        cout << " " << kl4->value;
    3.18 +    cout << "\n\n";
    3.19 +
    3.20 +    free_stringlist(keylist4);
    3.21  
    3.22      cout << "freeing messages…\n";
    3.23      free_message(msg4);