src/message_api.c
branchimport_own_key
changeset 734 bd9a3eb1d707
parent 729 6e93d01cf378
parent 731 6f7c688e0fd4
child 736 496905895282
     1.1 --- a/src/message_api.c	Thu Jun 09 16:51:44 2016 +0200
     1.2 +++ b/src/message_api.c	Sun Jun 12 20:20:45 2016 +0200
     1.3 @@ -830,7 +830,17 @@
     1.4      }
     1.5  }
     1.6  
     1.7 -bool _is_pgp_key(bloblist_t *bl)
     1.8 +static void free_bl_entry(bloblist_t *bl)
     1.9 +{
    1.10 +    if (bl) {
    1.11 +        free(bl->value);
    1.12 +        free(bl->mime_type);
    1.13 +        free(bl->filename);
    1.14 +        free(bl);
    1.15 +    }
    1.16 +}
    1.17 +
    1.18 +static bool is_key(const bloblist_t *bl)
    1.19  {
    1.20      return (// workaround for Apple Mail bugs
    1.21              (is_mime_type(bl, "application/x-apple-msg-attachment") &&
    1.22 @@ -849,7 +859,31 @@
    1.23             );
    1.24  }
    1.25  
    1.26 -void import_attached_keys(
    1.27 +static void remove_attached_keys(message *msg)
    1.28 +{
    1.29 +    if (msg) {
    1.30 +        bloblist_t *last = NULL;
    1.31 +        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
    1.32 +            bloblist_t *next = bl->next;
    1.33 +
    1.34 +            if (is_key(bl)) {
    1.35 +                if (last) {
    1.36 +                    last->next = next;
    1.37 +                }
    1.38 +                else {
    1.39 +                    msg->attachments = next;
    1.40 +                }
    1.41 +                free_bl_entry(bl);
    1.42 +            }
    1.43 +            else {
    1.44 +                last = bl;
    1.45 +            }
    1.46 +            bl = next;
    1.47 +        }
    1.48 +    }
    1.49 +}
    1.50 +
    1.51 +bool import_attached_keys(
    1.52          PEP_SESSION session, 
    1.53          const message *msg,
    1.54          identity_list **private_idents
    1.55 @@ -858,17 +892,22 @@
    1.56      assert(session);
    1.57      assert(msg);
    1.58  
    1.59 +    bool remove = false;
    1.60 +
    1.61      bloblist_t *bl;
    1.62      for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
    1.63          assert(bl && bl->value && bl->size);
    1.64  
    1.65 -        if (_is_pgp_key(bl))
    1.66 +        if (is_key(bl)) 
    1.67          {
    1.68              import_key(session, bl->value, bl->size, private_idents);
    1.69 +            remove = true;
    1.70          }
    1.71      }
    1.72 +    return remove;
    1.73  }
    1.74  
    1.75 +
    1.76  PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
    1.77  {
    1.78      char *keydata;
    1.79 @@ -1163,8 +1202,8 @@
    1.80      if (!(session && src && dst && keylist && color))
    1.81          return PEP_ILLEGAL_VALUE;
    1.82  
    1.83 -    // Private key in an unencrypted mail... srsly ? -> NULL
    1.84 -    import_attached_keys(session, src, NULL);
    1.85 +    // Private key in unencrypted mail are ignored -> NULL
    1.86 +    bool imported_keys = import_attached_keys(session, src, NULL);
    1.87  
    1.88      // Update src->from in case we just imported a key
    1.89      // we would need to check signature
    1.90 @@ -1180,6 +1219,8 @@
    1.91      switch (src->enc_format) {
    1.92          case PEP_enc_none:
    1.93              *color = PEP_rating_unencrypted;
    1.94 +            if (imported_keys)
    1.95 +                remove_attached_keys(src);
    1.96              return PEP_UNENCRYPTED;
    1.97  
    1.98          case PEP_enc_PGP_MIME:
    1.99 @@ -1337,10 +1378,9 @@
   1.100                  NOT_IMPLEMENTED
   1.101          }
   1.102         
   1.103 -        // Only check for private key imported if
   1.104 -        // in decrypted message attachement
   1.105 +        // check for private key in decrypted message attachement while inporting
   1.106          identity_list *private_il = NULL;
   1.107 -        import_attached_keys(session, msg, &private_il);
   1.108 +        imported_keys = import_attached_keys(session, msg, &private_il);
   1.109          if (private_il && 
   1.110              identity_list_length(private_il) == 1 &&
   1.111              private_il->ident->address)
   1.112 @@ -1371,7 +1411,7 @@
   1.113              status = cryptotech[crypto].decrypt_and_verify(session, ctext,
   1.114                  csize, &re_ptext, &re_psize, &_keylist);
   1.115              
   1.116 -            if(re_ptext)
   1.117 +            if (re_ptext)
   1.118                  free(re_ptext);
   1.119              
   1.120              if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
   1.121 @@ -1447,6 +1487,8 @@
   1.122  
   1.123      if (msg){
   1.124          decorate_message(msg, *color, _keylist);
   1.125 +        if (imported_keys)
   1.126 +            remove_attached_keys(msg);
   1.127      }
   1.128  
   1.129      *dst = msg;