src/message_api.c
changeset 431 f8a10d359cde
parent 428 05d3a6d5a6a9
child 433 c8e149ad9df4
     1.1 --- a/src/message_api.c	Thu Jan 28 11:40:49 2016 +0100
     1.2 +++ b/src/message_api.c	Fri Jan 29 18:04:49 2016 +0100
     1.3 @@ -943,7 +943,7 @@
     1.4          if (msg == NULL)
     1.5              goto enomem;
     1.6  
     1.7 -        attach_own_key(session, msg);
     1.8 +        attach_own_key(session, src);
     1.9  
    1.10          switch (enc_format) {
    1.11          case PEP_enc_PGP_MIME:
    1.12 @@ -1036,64 +1036,22 @@
    1.13          case PEP_enc_PGP_MIME:
    1.14              ctext = src->attachments->next->value;
    1.15              csize = src->attachments->next->size;
    1.16 -
    1.17 -            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
    1.18 -                    csize, &ptext, &psize, &_keylist);
    1.19 -            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
    1.20 -                goto pep_error;
    1.21 -            decrypt_status = status;
    1.22              break;
    1.23  
    1.24          case PEP_enc_pieces:
    1.25              ctext = src->longmsg;
    1.26              csize = strlen(ctext);
    1.27 -
    1.28 -            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
    1.29 -                    csize, &ptext, &psize, &_keylist);
    1.30 -            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
    1.31 -                goto pep_error;
    1.32 -            decrypt_status = status;
    1.33              break;
    1.34  
    1.35          default:
    1.36              NOT_IMPLEMENTED
    1.37      }
    1.38 -
    1.39 -    *color = decrypt_color(status);
    1.40 -
    1.41 -    if (*color != PEP_rating_under_attack) {
    1.42 -        PEP_color kl_color = PEP_rating_undefined;
    1.43 -
    1.44 -        if (_keylist)
    1.45 -            kl_color = keylist_color(session, _keylist);
    1.46 +    status = cryptotech[crypto].decrypt_and_verify(session, ctext,
    1.47 +                                                   csize, &ptext, &psize, &_keylist);
    1.48 +    if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
    1.49 +        goto pep_error;
    1.50  
    1.51 -        if (kl_color == PEP_rating_under_attack) {
    1.52 -            *color = PEP_rating_under_attack;
    1.53 -        }
    1.54 -        else if (*color >= PEP_rating_reliable &&
    1.55 -               kl_color < PEP_rating_reliable) {
    1.56 -            *color = PEP_rating_unreliable;
    1.57 -        }
    1.58 -        else if (*color >= PEP_rating_reliable &&
    1.59 -               kl_color >= PEP_rating_reliable) {
    1.60 -            if (!(src->from && src->from->user_id && src->from->user_id[0])) {
    1.61 -                *color = PEP_rating_unreliable;
    1.62 -            }
    1.63 -            else {
    1.64 -                char *fpr = _keylist->value;
    1.65 -                pEp_identity *_from = new_identity(src->from->address, fpr,
    1.66 -                        src->from->user_id, src->from->username);
    1.67 -                if (_from == NULL)
    1.68 -                    goto enomem;
    1.69 -                status = update_identity(session, _from);
    1.70 -                if (_from->comm_type != PEP_ct_unknown)
    1.71 -                    *color = _rating(_from->comm_type);
    1.72 -                free_identity(_from);
    1.73 -                if (status != PEP_STATUS_OK)
    1.74 -                    goto pep_error;
    1.75 -            }
    1.76 -        }
    1.77 -    }
    1.78 +    decrypt_status = status;
    1.79  
    1.80      if (ptext) {
    1.81          switch (src->enc_format) {
    1.82 @@ -1122,10 +1080,13 @@
    1.83                  for (_s = src->attachments; _s && _s->value; _s = _s->next) {
    1.84                      if (is_encrypted_attachment(_s)) {
    1.85                          stringlist_t *_keylist = NULL;
    1.86 -                        ctext = _s->value;
    1.87 -                        csize = _s->size;
    1.88 +                        char *attctext;
    1.89 +                        size_t attcsize;
    1.90  
    1.91 -                        status = decrypt_and_verify(session, ctext, csize,
    1.92 +                        attctext = _s->value;
    1.93 +                        attcsize = _s->size;
    1.94 +
    1.95 +                        status = decrypt_and_verify(session, attctext, attcsize,
    1.96                                  &ptext, &psize, &_keylist);
    1.97                          free_stringlist(_keylist);
    1.98  
    1.99 @@ -1186,7 +1147,7 @@
   1.100                  // BUG: must implement more
   1.101                  NOT_IMPLEMENTED
   1.102          }
   1.103 -
   1.104 +        
   1.105          switch (src->enc_format) {
   1.106              case PEP_enc_PGP_MIME:
   1.107              case PEP_enc_pieces:
   1.108 @@ -1221,8 +1182,63 @@
   1.109                  // BUG: must implement more
   1.110                  NOT_IMPLEMENTED
   1.111          }
   1.112 +        
   1.113 +        import_attached_keys(session, msg);
   1.114 +        
   1.115 +        if(decrypt_status == PEP_DECRYPTED){
   1.116 +            
   1.117 +            // In case message did decrypt, but no valid signature could be found
   1.118 +            // then retry decrypt+verify after importing key.
   1.119 +            // TODO optimize if import_attached_keys didn't import any key
   1.120 +            
   1.121 +            char *re_ptext = NULL;
   1.122 +            size_t re_psize;
   1.123 +            free_stringlist(_keylist);
   1.124 +            _keylist = NULL;
   1.125  
   1.126 -        import_attached_keys(session, msg);
   1.127 +            status = cryptotech[crypto].decrypt_and_verify(session, ctext,
   1.128 +                csize, &re_ptext, &re_psize, &_keylist);
   1.129 +            if (status > PEP_CANNOT_DECRYPT_UNKNOWN)
   1.130 +                goto pep_error;
   1.131 +            
   1.132 +            decrypt_status = status;
   1.133 +        }
   1.134 +        
   1.135 +        *color = decrypt_color(decrypt_status);
   1.136 +        
   1.137 +        if (*color != PEP_rating_under_attack) {
   1.138 +            PEP_color kl_color = PEP_rating_undefined;
   1.139 +            
   1.140 +            if (_keylist)
   1.141 +                kl_color = keylist_color(session, _keylist);
   1.142 +            
   1.143 +            if (kl_color == PEP_rating_under_attack) {
   1.144 +                *color = PEP_rating_under_attack;
   1.145 +            }
   1.146 +            else if (*color >= PEP_rating_reliable &&
   1.147 +                     kl_color < PEP_rating_reliable) {
   1.148 +                *color = PEP_rating_unreliable;
   1.149 +            }
   1.150 +            else if (*color >= PEP_rating_reliable &&
   1.151 +                     kl_color >= PEP_rating_reliable) {
   1.152 +                if (!(src->from && src->from->user_id && src->from->user_id[0])) {
   1.153 +                    *color = PEP_rating_unreliable;
   1.154 +                }
   1.155 +                else {
   1.156 +                    char *fpr = _keylist->value;
   1.157 +                    pEp_identity *_from = new_identity(src->from->address, fpr,
   1.158 +                                                       src->from->user_id, src->from->username);
   1.159 +                    if (_from == NULL)
   1.160 +                        goto enomem;
   1.161 +                    status = update_identity(session, _from);
   1.162 +                    if (_from->comm_type != PEP_ct_unknown)
   1.163 +                        *color = _rating(_from->comm_type);
   1.164 +                    free_identity(_from);
   1.165 +                    if (status != PEP_STATUS_OK)
   1.166 +                        goto pep_error;
   1.167 +                }
   1.168 +            }
   1.169 +        }
   1.170      }
   1.171  
   1.172      if (msg)