src/message_api.c
changeset 731 6f7c688e0fd4
parent 694 7e236f95f1e4
child 734 bd9a3eb1d707
child 745 32d7952cccfa
     1.1 --- a/src/message_api.c	Thu Jun 09 18:50:59 2016 +0200
     1.2 +++ b/src/message_api.c	Fri Jun 10 13:56:46 2016 +0200
     1.3 @@ -830,37 +830,90 @@
     1.4      }
     1.5  }
     1.6  
     1.7 -void import_attached_keys(PEP_SESSION session, const message *msg)
     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 +    bool result = false;
    1.21 +
    1.22 +    // workaround for Apple Mail bugs
    1.23 +    if (is_mime_type(bl, "application/x-apple-msg-attachment")) {
    1.24 +        if (is_fileending(bl, ".asc")) {
    1.25 +            result = true;
    1.26 +        }
    1.27 +    }
    1.28 +    else if (bl->mime_type == NULL ||
    1.29 +                is_mime_type(bl, "application/octet-stream")) {
    1.30 +        if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
    1.31 +                is_fileending(bl, ".key") || is_fileending(bl, ".asc")) {
    1.32 +            result = true;
    1.33 +        }
    1.34 +    }
    1.35 +    else if (is_mime_type(bl, "application/pgp-keys")) {
    1.36 +        result = true;
    1.37 +    }
    1.38 +    else if (is_mime_type(bl, "text/plain")) {
    1.39 +        if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
    1.40 +                is_fileending(bl, ".key") || is_fileending(bl, ".asc")) {
    1.41 +            result = true;
    1.42 +        }
    1.43 +    }
    1.44 +
    1.45 +    return result;
    1.46 +}
    1.47 +
    1.48 +static void remove_attached_keys(message *msg)
    1.49 +{
    1.50 +    if (msg) {
    1.51 +        bloblist_t *last = NULL;
    1.52 +        for (bloblist_t *bl = msg->attachments; bl && bl->value; ) {
    1.53 +            bloblist_t *next = bl->next;
    1.54 +
    1.55 +            if (is_key(bl)) {
    1.56 +                if (last) {
    1.57 +                    last->next = next;
    1.58 +                }
    1.59 +                else {
    1.60 +                    msg->attachments = next;
    1.61 +                }
    1.62 +                free_bl_entry(bl);
    1.63 +            }
    1.64 +            else {
    1.65 +                last = bl;
    1.66 +            }
    1.67 +            bl = next;
    1.68 +        }
    1.69 +    }
    1.70 +}
    1.71 +
    1.72 +bool import_attached_keys(PEP_SESSION session, message *msg)
    1.73  {
    1.74      assert(session);
    1.75      assert(msg);
    1.76  
    1.77 +    bool remove = false;
    1.78 +
    1.79      bloblist_t *bl;
    1.80      for (bl = msg->attachments; bl && bl->value; bl = bl->next) {
    1.81          assert(bl && bl->value && bl->size);
    1.82 -
    1.83 -        // workaround for Apple Mail bugs
    1.84 -        if (is_mime_type(bl, "application/x-apple-msg-attachment")) {
    1.85 -            if (is_fileending(bl, ".asc"))
    1.86 -                import_key(session, bl->value, bl->size);
    1.87 -        }
    1.88 -        else if (bl->mime_type == NULL ||
    1.89 -                    is_mime_type(bl, "application/octet-stream")) {
    1.90 -            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
    1.91 -                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
    1.92 -                import_key(session, bl->value, bl->size);
    1.93 -        }
    1.94 -        else if (is_mime_type(bl, "application/pgp-keys")) {
    1.95 +        if (is_key(bl)) {
    1.96              import_key(session, bl->value, bl->size);
    1.97 -        }
    1.98 -        else if (is_mime_type(bl, "text/plain")) {
    1.99 -            if (is_fileending(bl, ".pgp") || is_fileending(bl, ".gpg") ||
   1.100 -                    is_fileending(bl, ".key") || is_fileending(bl, ".asc"))
   1.101 -                import_key(session, bl->value, bl->size);
   1.102 +            remove = true;
   1.103          }
   1.104      }
   1.105 -    if(msg->from && msg->from->user_id && msg->from->address)
   1.106 +
   1.107 +    if (msg->from && msg->from->user_id && msg->from->address)
   1.108          update_identity(session, msg->from);
   1.109 +
   1.110 +    return remove;
   1.111  }
   1.112  
   1.113  
   1.114 @@ -1157,7 +1210,7 @@
   1.115      if (!(session && src && dst && keylist && color))
   1.116          return PEP_ILLEGAL_VALUE;
   1.117  
   1.118 -    import_attached_keys(session, src);
   1.119 +    bool imported_keys = import_attached_keys(session, src);
   1.120      PEP_cryptotech crypto = determine_encryption_format(src);
   1.121  
   1.122      *dst = NULL;
   1.123 @@ -1167,6 +1220,8 @@
   1.124      switch (src->enc_format) {
   1.125          case PEP_enc_none:
   1.126              *color = PEP_rating_unencrypted;
   1.127 +            if (imported_keys)
   1.128 +                remove_attached_keys(src);
   1.129              return PEP_UNENCRYPTED;
   1.130  
   1.131          case PEP_enc_PGP_MIME:
   1.132 @@ -1322,9 +1377,9 @@
   1.133                  NOT_IMPLEMENTED
   1.134          }
   1.135          
   1.136 -        import_attached_keys(session, msg);
   1.137 +        imported_keys = import_attached_keys(session, msg);
   1.138          
   1.139 -        if(decrypt_status == PEP_DECRYPTED){
   1.140 +        if (decrypt_status == PEP_DECRYPTED) {
   1.141              
   1.142              // In case message did decrypt, but no valid signature could be found
   1.143              // then retry decrypt+verify after importing key.
   1.144 @@ -1339,7 +1394,7 @@
   1.145              status = cryptotech[crypto].decrypt_and_verify(session, ctext,
   1.146                  csize, &re_ptext, &re_psize, &_keylist);
   1.147              
   1.148 -            if(re_ptext)
   1.149 +            if (re_ptext)
   1.150                  free(re_ptext);
   1.151              
   1.152              if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
   1.153 @@ -1385,8 +1440,11 @@
   1.154          }
   1.155      }
   1.156  
   1.157 -    if (msg)
   1.158 +    if (msg) {
   1.159          decorate_message(msg, *color, _keylist);
   1.160 +        if (imported_keys)
   1.161 +            remove_attached_keys(msg);
   1.162 +    }
   1.163  
   1.164      *dst = msg;
   1.165      *keylist = _keylist;