ENGINE-275: Ok, mime encoding nightmare SEEMS to be fixed. We'll see. ENGINE-275
authorKrista Bennett <krista@pep-project.org>
Thu, 12 Oct 2017 17:45:48 +0200
branchENGINE-275
changeset 21559d16b7e422cc
parent 2153 d0182c01f0eb
child 2156 0df79faabcc7
ENGINE-275: Ok, mime encoding nightmare SEEMS to be fixed. We'll see.
src/message_api.c
src/mime.c
src/mime.h
test/mime_test.cc
     1.1 --- a/src/message_api.c	Wed Oct 11 15:46:04 2017 +0200
     1.2 +++ b/src/message_api.c	Thu Oct 12 17:45:48 2017 +0200
     1.3 @@ -20,6 +20,108 @@
     1.4  #define _MAX(A, B) ((B) > (A) ? (B) : (A))
     1.5  #endif
     1.6  
     1.7 +// static const char* search_opt_fields(message* src, char* key) {
     1.8 +//     assert(src);
     1.9 +//     assert(key);
    1.10 +//     if (src && key) {
    1.11 +//         stringpair_list_t opt_fields = src->opt_fields;
    1.12 +//         while (opt_fields) {
    1.13 +//             char* currkey = opt_fields->value->key;
    1.14 +//             if (strcmp(currkey, key) == 0) {
    1.15 +//                 return opt_fields->value->value;
    1.16 +//             }
    1.17 +//             opt_fields = opt_fields->next;
    1.18 +//         }
    1.19 +//     }
    1.20 +//     return NULL;
    1.21 +// }
    1.22 +// 
    1.23 +// static const char* get_message_version_string(message* src) {
    1.24 +//     const char* version_key = "X-pEp-Version";
    1.25 +//     return(search_opt_fields(src, version_key));
    1.26 +// }
    1.27 +
    1.28 +
    1.29 +static char * keylist_to_string(const stringlist_t *keylist)
    1.30 +{
    1.31 +    if (keylist) {
    1.32 +        size_t size = stringlist_length(keylist);
    1.33 +
    1.34 +        const stringlist_t *_kl;
    1.35 +        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
    1.36 +            size += strlen(_kl->value);
    1.37 +        }
    1.38 +
    1.39 +        char *result = calloc(1, size);
    1.40 +        if (result == NULL)
    1.41 +            return NULL;
    1.42 +
    1.43 +        char *_r = result;
    1.44 +        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
    1.45 +            _r = stpcpy(_r, _kl->value);
    1.46 +            if (_kl->next && _kl->next->value)
    1.47 +                _r = stpcpy(_r, ",");
    1.48 +        }
    1.49 +
    1.50 +        return result;
    1.51 +    }
    1.52 +    else {
    1.53 +        return NULL;
    1.54 +    }
    1.55 +}
    1.56 +
    1.57 +static const char * rating_to_string(PEP_rating rating)
    1.58 +{
    1.59 +    switch (rating) {
    1.60 +    case PEP_rating_cannot_decrypt:
    1.61 +        return "cannot_decrypt";
    1.62 +    case PEP_rating_have_no_key:
    1.63 +        return "have_no_key";
    1.64 +    case PEP_rating_unencrypted:
    1.65 +        return "unencrypted";
    1.66 +    case PEP_rating_unencrypted_for_some:
    1.67 +        return "unencrypted_for_some";
    1.68 +    case PEP_rating_unreliable:
    1.69 +        return "unreliable";
    1.70 +    case PEP_rating_reliable:
    1.71 +        return "reliable";
    1.72 +    case PEP_rating_trusted:
    1.73 +        return "trusted";
    1.74 +    case PEP_rating_trusted_and_anonymized:
    1.75 +        return "trusted_and_anonymized";
    1.76 +    case PEP_rating_fully_anonymous:
    1.77 +        return "fully_anonymous";
    1.78 +    case PEP_rating_mistrust:
    1.79 +        return "mistrust";
    1.80 +    case PEP_rating_b0rken:
    1.81 +        return "b0rken";
    1.82 +    case PEP_rating_under_attack:
    1.83 +        return "under_attack";
    1.84 +    default:
    1.85 +        return "undefined";
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +static void decorate_message(
    1.90 +    message *msg,
    1.91 +    PEP_rating rating,
    1.92 +    stringlist_t *keylist
    1.93 +    )
    1.94 +{
    1.95 +    assert(msg);
    1.96 +
    1.97 +    add_opt_field(msg, "X-pEp-Version", PEP_VERSION);
    1.98 +
    1.99 +    if (rating != PEP_rating_undefined)
   1.100 +        add_opt_field(msg, "X-EncStatus", rating_to_string(rating));
   1.101 +
   1.102 +    if (keylist) {
   1.103 +        char *_keylist = keylist_to_string(keylist);
   1.104 +        add_opt_field(msg, "X-KeyList", _keylist);
   1.105 +        free(_keylist);
   1.106 +    }
   1.107 +}
   1.108 +
   1.109  static char* _get_resource_ptr_noown(char* uri) {
   1.110      char* uri_delim = strstr(uri, "://");
   1.111      if (!uri_delim)
   1.112 @@ -440,7 +542,9 @@
   1.113      message* attachment) {
   1.114      
   1.115      message* _envelope = envelope;
   1.116 -    
   1.117 +
   1.118 +    add_opt_field(attachment, "X-pEp-Version", PEP_VERSION);
   1.119 +
   1.120      if (!_envelope) {
   1.121          _envelope = extract_minimal_envelope(attachment, PEP_dir_outgoing);
   1.122          attachment->longmsg = encapsulate_message_wrap_info("INNER", attachment->longmsg);
   1.123 @@ -451,6 +555,7 @@
   1.124      }
   1.125      char* message_text = NULL;
   1.126      /* Turn message into a MIME-blob */
   1.127 +    //attachment->enc_format = PEP_enc_none;
   1.128      PEP_STATUS status = mime_encode_message(attachment, false, &message_text);
   1.129      
   1.130      if (status != PEP_STATUS_OK) {
   1.131 @@ -492,7 +597,8 @@
   1.132      assert(_src);
   1.133      if (_src == NULL)
   1.134          goto enomem;
   1.135 -    _src->longmsg = ptext;
   1.136 +//    _src->longmsg = ptext;
   1.137 +    _src->longmsg = src->longmsg;
   1.138      _src->longmsg_formatted = src->longmsg_formatted;
   1.139      _src->attachments = src->attachments;
   1.140      _src->enc_format = PEP_enc_none;
   1.141 @@ -553,85 +659,6 @@
   1.142      return status;
   1.143  }
   1.144  
   1.145 -static char * keylist_to_string(const stringlist_t *keylist)
   1.146 -{
   1.147 -    if (keylist) {
   1.148 -        size_t size = stringlist_length(keylist);
   1.149 -
   1.150 -        const stringlist_t *_kl;
   1.151 -        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
   1.152 -            size += strlen(_kl->value);
   1.153 -        }
   1.154 -
   1.155 -        char *result = calloc(1, size);
   1.156 -        if (result == NULL)
   1.157 -            return NULL;
   1.158 -
   1.159 -        char *_r = result;
   1.160 -        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
   1.161 -            _r = stpcpy(_r, _kl->value);
   1.162 -            if (_kl->next && _kl->next->value)
   1.163 -                _r = stpcpy(_r, ",");
   1.164 -        }
   1.165 -
   1.166 -        return result;
   1.167 -    }
   1.168 -    else {
   1.169 -        return NULL;
   1.170 -    }
   1.171 -}
   1.172 -
   1.173 -static const char * rating_to_string(PEP_rating rating)
   1.174 -{
   1.175 -    switch (rating) {
   1.176 -    case PEP_rating_cannot_decrypt:
   1.177 -        return "cannot_decrypt";
   1.178 -    case PEP_rating_have_no_key:
   1.179 -        return "have_no_key";
   1.180 -    case PEP_rating_unencrypted:
   1.181 -        return "unencrypted";
   1.182 -    case PEP_rating_unencrypted_for_some:
   1.183 -        return "unencrypted_for_some";
   1.184 -    case PEP_rating_unreliable:
   1.185 -        return "unreliable";
   1.186 -    case PEP_rating_reliable:
   1.187 -        return "reliable";
   1.188 -    case PEP_rating_trusted:
   1.189 -        return "trusted";
   1.190 -    case PEP_rating_trusted_and_anonymized:
   1.191 -        return "trusted_and_anonymized";
   1.192 -    case PEP_rating_fully_anonymous:
   1.193 -        return "fully_anonymous";
   1.194 -    case PEP_rating_mistrust:
   1.195 -        return "mistrust";
   1.196 -    case PEP_rating_b0rken:
   1.197 -        return "b0rken";
   1.198 -    case PEP_rating_under_attack:
   1.199 -        return "under_attack";
   1.200 -    default:
   1.201 -        return "undefined";
   1.202 -    }
   1.203 -}
   1.204 -
   1.205 -static void decorate_message(
   1.206 -    message *msg,
   1.207 -    PEP_rating rating,
   1.208 -    stringlist_t *keylist
   1.209 -    )
   1.210 -{
   1.211 -    assert(msg);
   1.212 -
   1.213 -    add_opt_field(msg, "X-pEp-Version", PEP_VERSION);
   1.214 -
   1.215 -    if (rating != PEP_rating_undefined)
   1.216 -        add_opt_field(msg, "X-EncStatus", rating_to_string(rating));
   1.217 -
   1.218 -    if (keylist) {
   1.219 -        char *_keylist = keylist_to_string(keylist);
   1.220 -        add_opt_field(msg, "X-KeyList", _keylist);
   1.221 -        free(_keylist);
   1.222 -    }
   1.223 -}
   1.224  
   1.225  static PEP_rating _rating(PEP_comm_type ct, PEP_rating rating)
   1.226  {
   1.227 @@ -1664,25 +1691,37 @@
   1.228  
   1.229  static PEP_STATUS unencapsulate_hidden_fields(message* src, message* msg,
   1.230                                                char** msg_wrap_info) {
   1.231 +    if (!src)
   1.232 +        return PEP_ILLEGAL_VALUE;
   1.233      unsigned char pepstr[] = PEP_SUBJ_STRING;
   1.234      PEP_STATUS status = PEP_STATUS_OK;
   1.235 +
   1.236 +    bool change_source_in_place = (msg ? false : true);
   1.237 +    
   1.238 +    if (change_source_in_place)
   1.239 +        msg = src;
   1.240 +        
   1.241 +//    const char* version_string = get_message_version_string(src);
   1.242      
   1.243      switch (src->enc_format) {
   1.244          case PEP_enc_PGP_MIME:
   1.245          case PEP_enc_pieces:
   1.246          case PEP_enc_PGP_MIME_Outlook1:
   1.247 -        
   1.248 -            status = copy_fields(msg, src);
   1.249 +//        case PEP_enc_none: // FIXME - this is wrong
   1.250 +
   1.251 +            if (!change_source_in_place)
   1.252 +                status = copy_fields(msg, src);
   1.253 +                
   1.254              if (status != PEP_STATUS_OK)
   1.255                  return status;
   1.256 -
   1.257 +                
   1.258              // FIXME: This is a mess. Talk with VB about how far we go to identify
   1.259              if (is_a_pEpmessage(src) || (src->shortmsg == NULL || strcmp(src->shortmsg, "pEp") == 0 ||
   1.260                  _unsigned_signed_strcmp(pepstr, src->shortmsg, PEP_SUBJ_BYTELEN) == 0))
   1.261              {
   1.262                  char * shortmsg = NULL;
   1.263                  char * longmsg = NULL;
   1.264 -                
   1.265 +        
   1.266                  if (msg->longmsg) {
   1.267                      int r = separate_short_and_long(msg->longmsg, 
   1.268                                                      &shortmsg, 
   1.269 @@ -1695,13 +1734,14 @@
   1.270  
   1.271                  // We only use the shortmsg in version 1.0 messages; if it occurs where we
   1.272                  // didn't replace the subject, we ignore this all
   1.273 -                if (!(*msg_wrap_info)) {
   1.274 +                if (!(*msg_wrap_info || change_source_in_place)) {
   1.275                      if (!shortmsg || 
   1.276                          (src->shortmsg != NULL && strcmp(src->shortmsg, "pEp") != 0 &&
   1.277                           _unsigned_signed_strcmp(pepstr, src->shortmsg, PEP_SUBJ_BYTELEN) != 0)) {
   1.278                               
   1.279                          if (shortmsg != NULL)
   1.280                              free(shortmsg);                        
   1.281 +                            
   1.282                          if (src->shortmsg == NULL) {
   1.283                              shortmsg = strdup("");
   1.284                          }
   1.285 @@ -1710,23 +1750,25 @@
   1.286                              // src->shortmsg already?
   1.287                              // if so, we need to change the logic so
   1.288                              // that in this case, we don't free msg->shortmsg
   1.289 -                            // and do this strdup, etc.
   1.290 -                            shortmsg = strdup(src->shortmsg); 
   1.291 +                            // and do this strdup, etc
   1.292 +                            shortmsg = strdup(src->shortmsg);
   1.293                          }        
   1.294                      }
   1.295 +                    free(msg->shortmsg);
   1.296 +                    msg->shortmsg = shortmsg;
   1.297                  }
   1.298                  
   1.299 -                free(msg->shortmsg);
   1.300                  free(msg->longmsg);
   1.301  
   1.302 -                msg->shortmsg = shortmsg;
   1.303                  msg->longmsg = longmsg;
   1.304              }
   1.305              else {
   1.306 -                msg->shortmsg = strdup(src->shortmsg);
   1.307 -                assert(msg->shortmsg);
   1.308 -                if (msg->shortmsg == NULL)
   1.309 -                    return PEP_OUT_OF_MEMORY;
   1.310 +                if (!change_source_in_place) {
   1.311 +                    msg->shortmsg = strdup(src->shortmsg);
   1.312 +                    assert(msg->shortmsg);
   1.313 +                    if (msg->shortmsg == NULL)
   1.314 +                        return PEP_OUT_OF_MEMORY;
   1.315 +                }
   1.316              }
   1.317              break;
   1.318          default:
   1.319 @@ -2121,8 +2163,9 @@
   1.320                                      // and those are the only ones with such info.
   1.321                                      // Since we capture the information, this is ok.
   1.322                                      wrap_info = NULL;
   1.323 +                                    inner_message->enc_format = PEP_enc_PGP_MIME;
   1.324                                      // FIXME
   1.325 -                                    status = unencapsulate_hidden_fields(src, msg, &wrap_info);
   1.326 +                                    status = unencapsulate_hidden_fields(inner_message, NULL, &wrap_info);
   1.327                                      if (wrap_info) {
   1.328                                          // useless check, but just in case we screw up?
   1.329                                          if (strcmp(wrap_info, "INNER") == 0) {
   1.330 @@ -2136,6 +2179,7 @@
   1.331                                              GOTO(pep_error);
   1.332                                          }
   1.333                                      }
   1.334 +                                    inner_message->enc_format = PEP_enc_none;
   1.335                                  }
   1.336                                  else { // forwarded message, leave it alone
   1.337                                      free_message(inner_message);
   1.338 @@ -2751,6 +2795,7 @@
   1.339          GOTO(pep_error);
   1.340      }
   1.341  
   1.342 +    dec_msg->enc_format = PEP_enc_none; // is this the right thing to do? FIXME
   1.343      status = mime_encode_message(dec_msg, false, mime_plaintext);
   1.344  
   1.345      if (status == PEP_STATUS_OK)
     2.1 --- a/src/mime.c	Wed Oct 11 15:46:04 2017 +0200
     2.2 +++ b/src/mime.c	Thu Oct 12 17:45:48 2017 +0200
     2.3 @@ -819,12 +819,14 @@
     2.4  	struct mailmime_parameter * param;
     2.5      int r;
     2.6      PEP_STATUS status;
     2.7 +    //char *subject;
     2.8      char *plaintext;
     2.9      size_t plaintext_size;
    2.10  
    2.11      assert(msg->attachments && msg->attachments->next &&
    2.12              msg->attachments->next->value);
    2.13  
    2.14 +    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp"; // not used, yet.
    2.15      plaintext = msg->attachments->next->value;
    2.16      plaintext_size = msg->attachments->next->size;
    2.17  
     3.1 --- a/src/mime.h	Wed Oct 11 15:46:04 2017 +0200
     3.2 +++ b/src/mime.h	Thu Oct 12 17:45:48 2017 +0200
     3.3 @@ -40,6 +40,10 @@
     3.4  //      the resulttext will go to the ownership of the caller
     3.5  //      the message will remain in the ownership of the caller
     3.6  //      omit_fields is true for payload of PGP/MIME messages
     3.7 +//
     3.8 +//      also: note that the encryption type will be used to determine what
     3.9 +//      gets encoded from the message struct, so if using this on an 
    3.10 +//      already-encrypted message, set the enc_format of the msg to PEP_enc_none.
    3.11  
    3.12  DYNAMIC_API PEP_STATUS mime_encode_message(
    3.13          const message * msg,
     4.1 --- a/test/mime_test.cc	Wed Oct 11 15:46:04 2017 +0200
     4.2 +++ b/test/mime_test.cc	Thu Oct 12 17:45:48 2017 +0200
     4.3 @@ -102,4 +102,3 @@
     4.4      release(session);
     4.5      return 0;
     4.6  }
     4.7 -