We now clean up after ourselves when we add keys and wrap stuff in the encrypt functions. ENGINE-425
authorKrista Bennett <krista@pep-project.org>
Fri, 20 Apr 2018 15:06:16 +0200
branchENGINE-425
changeset 2617fdbb28063368
parent 2616 13e9725995f2
child 2620 903cbf3f7c09
We now clean up after ourselves when we add keys and wrap stuff in the encrypt functions.
src/message_api.c
     1.1 --- a/src/message_api.c	Fri Apr 20 10:58:08 2018 +0200
     1.2 +++ b/src/message_api.c	Fri Apr 20 15:06:16 2018 +0200
     1.3 @@ -280,8 +280,7 @@
     1.4      return strcmp(bl->filename + (fn_len - fe_len), fe) == 0;
     1.5  }
     1.6  
     1.7 -
     1.8 -static char * encapsulate_message_wrap_info(const char *msg_wrap_info, const char *longmsg)
     1.9 +char * encapsulate_message_wrap_info(const char *msg_wrap_info, const char *longmsg)
    1.10  {
    1.11      assert(msg_wrap_info);
    1.12      
    1.13 @@ -1486,6 +1485,36 @@
    1.14      }
    1.15  }
    1.16  
    1.17 +static void _cleanup_src(message* src, bool remove_attached_key) {
    1.18 +    assert(src);
    1.19 +    
    1.20 +    if (!src)
    1.21 +        return;
    1.22 +        
    1.23 +    char* longmsg = NULL;
    1.24 +    char* shortmsg = NULL;
    1.25 +    char* msg_wrap_info = NULL;
    1.26 +    separate_short_and_long(src->longmsg, &shortmsg, &msg_wrap_info,
    1.27 +                            &longmsg);
    1.28 +    if (longmsg) {                    
    1.29 +        free(src->longmsg);
    1.30 +        free(shortmsg);
    1.31 +        free(msg_wrap_info);
    1.32 +        src->longmsg = longmsg;
    1.33 +    }
    1.34 +    if (remove_attached_key) {
    1.35 +        // End of the attachment list
    1.36 +        if (src->attachments) {
    1.37 +            bloblist_t* tmp = src->attachments;
    1.38 +            while (tmp->next && tmp->next->next) {
    1.39 +                tmp = tmp->next;
    1.40 +            }
    1.41 +            free_bloblist(tmp->next);
    1.42 +            tmp->next = NULL;
    1.43 +        }    
    1.44 +    }                   
    1.45 +}
    1.46 +
    1.47  DYNAMIC_API PEP_STATUS encrypt_message(
    1.48          PEP_SESSION session,
    1.49          message *src,
    1.50 @@ -1549,6 +1578,8 @@
    1.51  
    1.52      identity_list * _il;
    1.53  
    1.54 +    bool added_key_to_real_src = false;
    1.55 +
    1.56      if (enc_format != PEP_enc_none && (_il = src->bcc) && _il->ident)
    1.57      {
    1.58          // BCC limited support:
    1.59 @@ -1714,7 +1745,7 @@
    1.60              }
    1.61          }
    1.62      }
    1.63 -
    1.64 +        
    1.65      if (enc_format == PEP_enc_none || !dest_keys_found ||
    1.66          stringlist_length(keys)  == 0 ||
    1.67          _rating(max_comm_type,
    1.68 @@ -1724,6 +1755,7 @@
    1.69          if ((has_pep_user || !session->passive_mode) && 
    1.70              !(flags & PEP_encrypt_flag_force_no_attached_key)) {
    1.71              attach_own_key(session, src);
    1.72 +            added_key_to_real_src = true;
    1.73          }
    1.74          decorate_message(src, PEP_rating_undefined, NULL, true);
    1.75          return PEP_UNENCRYPTED;
    1.76 @@ -1742,6 +1774,8 @@
    1.77                  if (status == PEP_OUT_OF_MEMORY)
    1.78                      goto enomem;
    1.79              }
    1.80 +            if (!(flags & PEP_encrypt_flag_force_no_attached_key))
    1.81 +                added_key_to_real_src = true;            
    1.82          }
    1.83          if (!(flags & PEP_encrypt_flag_force_no_attached_key))
    1.84              attach_own_key(session, _src);
    1.85 @@ -1756,10 +1790,6 @@
    1.86                  status = encrypt_PGP_MIME(session, _src, keys, msg, flags);
    1.87                  break;
    1.88  
    1.89 -            // case PEP_enc_pieces:
    1.90 -            //     status = encrypt_PGP_in_pieces(session, src, keys, msg, flags);
    1.91 -            //     break;
    1.92 -
    1.93              /* case PEP_enc_PEP:
    1.94                  // TODO: implement
    1.95                  NOT_IMPLEMENTED */
    1.96 @@ -1802,6 +1832,8 @@
    1.97      // I don't think we do.
    1.98      if (_src && _src != src)
    1.99          free_message(_src);
   1.100 +    
   1.101 +    _cleanup_src(src, added_key_to_real_src);
   1.102          
   1.103      return status;
   1.104  
   1.105 @@ -1814,6 +1846,8 @@
   1.106      if (_src && _src != src)
   1.107          free_message(_src);
   1.108  
   1.109 +    _cleanup_src(src, added_key_to_real_src);
   1.110 +
   1.111      return status;
   1.112  }
   1.113  
   1.114 @@ -2067,8 +2101,8 @@
   1.115      }
   1.116  
   1.117      /* KG: did we ever do this??? */
   1.118 -    if (!(flags & PEP_encrypt_flag_force_no_attached_key))
   1.119 -        _attach_key(session, target_fpr, src);
   1.120 +    // if (!(flags & PEP_encrypt_flag_force_no_attached_key))
   1.121 +    //     _attach_key(session, target_fpr, src);
   1.122  
   1.123      _src = wrap_message_as_attachment(NULL, src, session->unencrypted_subject);
   1.124      if (!_src)
   1.125 @@ -2082,6 +2116,8 @@
   1.126          case PEP_enc_PGP_MIME:
   1.127          case PEP_enc_PEP: // BUG: should be implemented extra
   1.128              status = encrypt_PGP_MIME(session, _src, keys, msg, flags);
   1.129 +            if (status == PEP_STATUS_OK || (src->longmsg && strstr(src->longmsg, "INNER")))
   1.130 +                _cleanup_src(src, false);
   1.131              break;
   1.132  
   1.133          default: