Almost working, but extra carriage return. I think it's probably because the content headers are NULL in the signed part when constructing the final message; etpan probably puts in a CFLF no matter what, which we'll have to patch. ENGINE-633
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 23 Sep 2019 02:41:34 +0200
branchENGINE-633
changeset 40802f2d566db998
parent 4079 da3b0627a59c
child 4084 44135988583a
Almost working, but extra carriage return. I think it's probably because the content headers are NULL in the signed part when constructing the final message; etpan probably puts in a CFLF no matter what, which we'll have to patch.
Makefile.conf
src/aux_mime_msg.c
src/etpan_mime.c
src/message_api.c
src/mime.c
src/mime.h
src/pEpEngine.h
test/src/MessageTwoPointOhTest.cc
test/src/SignOnlyTest.cc
test/src/test_util.cc
     1.1 --- a/Makefile.conf	Sun Sep 22 21:41:35 2019 +0200
     1.2 +++ b/Makefile.conf	Mon Sep 23 02:41:34 2019 +0200
     1.3 @@ -113,8 +113,7 @@
     1.4      endif
     1.5  else ifeq ($(BUILD_FOR),Darwin)
     1.6      ifdef WARN
     1.7 -        # FIXME Remove 'no-extended-offsetof' after ENGINE-236 is closed.
     1.8 -        CFLAGS+= -Wall -pedantic -Wno-extended-offsetof
     1.9 +        CFLAGS+= -Wall -pedantic
    1.10      else
    1.11          CFLAGS+= -w
    1.12      endif
     2.1 --- a/src/aux_mime_msg.c	Sun Sep 22 21:41:35 2019 +0200
     2.2 +++ b/src/aux_mime_msg.c	Mon Sep 23 02:41:34 2019 +0200
     2.3 @@ -120,7 +120,7 @@
     2.4      }
     2.5  
     2.6      if (*flags & PEP_decrypt_flag_src_modified) {
     2.7 -        _mime_encode_message_internal(tmp_msg, false, modified_src, true, false);
     2.8 +        _mime_encode_message_internal(tmp_msg, false, modified_src, true, false, false);
     2.9          if (!modified_src) {
    2.10              *flags &= (~PEP_decrypt_flag_src_modified);
    2.11              decrypt_status = PEP_CANNOT_REENCRYPT; // Because we couldn't return it, I guess.
    2.12 @@ -128,7 +128,7 @@
    2.13      }
    2.14  
    2.15      // FIXME: test with att
    2.16 -    status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true, false);
    2.17 +    status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true, false, false);
    2.18  
    2.19      if (status == PEP_STATUS_OK)
    2.20      {
    2.21 @@ -214,7 +214,7 @@
    2.22          goto pEp_error;
    2.23      }
    2.24  
    2.25 -    status = _mime_encode_message_internal(enc_msg, false, mime_ciphertext, false, false);
    2.26 +    status = _mime_encode_message_internal(enc_msg, false, mime_ciphertext, false, false, false);
    2.27  
    2.28  pEp_error:
    2.29      free_message(tmp_msg);
     3.1 --- a/src/etpan_mime.c	Sun Sep 22 21:41:35 2019 +0200
     3.2 +++ b/src/etpan_mime.c	Mon Sep 23 02:41:34 2019 +0200
     3.3 @@ -183,6 +183,28 @@
     3.4      return NULL;
     3.5  }
     3.6  
     3.7 +struct mailmime * get_signed_text_part(const char* body_text)
     3.8 +{
     3.9 +    struct mailmime* mime = mailmime_new(MAILMIME_SINGLE,
    3.10 +                                         NULL, 0, 
    3.11 +                                         NULL, NULL, NULL,
    3.12 +                                         NULL, NULL, NULL, NULL, NULL);
    3.13 +
    3.14 +    int encoding = MAILMIME_MECHANISM_7BIT; // ????
    3.15 +    struct mailmime_data * data = mailmime_data_new(MAILMIME_DATA_TEXT, encoding, 0, 
    3.16 +                                                    body_text, strlen(body_text), NULL);
    3.17 +    if (data == NULL)
    3.18 +        goto pEp_error; 
    3.19 +
    3.20 +    mime->mm_data.mm_single = data;
    3.21 +    return mime;   
    3.22 +     
    3.23 +pEp_error:
    3.24 +    mailmime_free(mime);
    3.25 +    return NULL;
    3.26 +        
    3.27 +}
    3.28 +
    3.29  struct mailmime * get_pgp_encrypted_part(void)
    3.30  {
    3.31      struct mailmime * mime = NULL;
    3.32 @@ -1919,16 +1941,28 @@
    3.33          goto enomem;
    3.34  
    3.35      if (msg->attachments && msg->attachments->value && msg->attachments->mime_type) {
    3.36 -        if (strcmp(msg->attachments->mime_type, "multipart/signed")) {
    3.37 +        if (strcmp(msg->attachments->mime_type, "multipart/signed") == 0) {
    3.38 +            param = mailmime_param_new_with_data("protocol", strdup("application/pgp-signature"));
    3.39 +            clist_append(mime->mm_content_type->ct_parameters, param);                
    3.40              param = mailmime_param_new_with_data("micalg", strdup(msg->attachments->value));
    3.41              clist_append(mime->mm_content_type->ct_parameters, param);    
    3.42          }
    3.43      }
    3.44 -
    3.45 -    r = mailmime_set_body_text(mime, msg->longmsg, strlen(msg->longmsg));
    3.46 -    if (r != 0)
    3.47 +    submime = get_signed_text_part(msg->longmsg);
    3.48 +    assert(submime);
    3.49 +    if (submime == NULL)
    3.50          goto enomem;
    3.51 -            
    3.52 +
    3.53 +    r = mailmime_smart_add_part(mime, submime);
    3.54 +    assert(r == MAILIMF_NO_ERROR);
    3.55 +    if (r == MAILIMF_ERROR_MEMORY) {
    3.56 +        goto enomem;
    3.57 +    }
    3.58 +    else {
    3.59 +        // mailmime_smart_add_part() takes ownership of submime
    3.60 +        submime = NULL;
    3.61 +    }
    3.62 +                
    3.63      pEp_rid_list_t* resource = new_rid_node(PEP_RID_FILENAME, "signature.asc");
    3.64      submime = get_text_part(resource, "application/pgp-signature", msg->attachments->next->value,
    3.65              msg->attachments->next->size, MAILMIME_MECHANISM_7BIT);
    3.66 @@ -1969,7 +2003,8 @@
    3.67          bool omit_fields,
    3.68          char **mimetext,
    3.69          bool transport_encode,
    3.70 -        bool set_attachment_forward_comment
    3.71 +        bool set_attachment_forward_comment,
    3.72 +        bool omit_version
    3.73      )
    3.74  {
    3.75      PEP_STATUS status = PEP_STATUS_OK;
    3.76 @@ -2023,6 +2058,13 @@
    3.77      if (msg_mime == NULL)
    3.78          goto enomem;
    3.79  
    3.80 +    if (omit_version) {
    3.81 +        mailmime_fields_free(msg_mime->mm_mime_fields);
    3.82 +        msg_mime->mm_mime_fields = mailmime_fields_new_empty();
    3.83 +        if (msg_mime->mm_mime_fields == NULL)
    3.84 +            goto enomem;
    3.85 +    }
    3.86 +    
    3.87      r = mailmime_add_part(msg_mime, mime);
    3.88      if (r) {
    3.89          mailmime_free(mime);
     4.1 --- a/src/message_api.c	Sun Sep 22 21:41:35 2019 +0200
     4.2 +++ b/src/message_api.c	Mon Sep 23 02:41:34 2019 +0200
     4.3 @@ -916,7 +916,7 @@
     4.4                );
     4.5              
     4.6      /* Turn message into a MIME-blob */
     4.7 -    status = _mime_encode_message_internal(attachment, false, &message_text, true, false);
     4.8 +    status = _mime_encode_message_internal(attachment, false, &message_text, true, false, false);
     4.9          
    4.10      if (status != PEP_STATUS_OK)
    4.11          goto enomem;
    4.12 @@ -1025,7 +1025,7 @@
    4.13      // These vars are here to be clear, and because I don't know how this may change in the near future.
    4.14      bool wrapped = (wrap_type != PEP_message_unwrapped);
    4.15      bool mime_encode = !wrapped;
    4.16 -    status = _mime_encode_message_internal(_src, true, &mimetext, mime_encode, wrapped);
    4.17 +    status = _mime_encode_message_internal(_src, true, &mimetext, mime_encode, wrapped, false);
    4.18      assert(status == PEP_STATUS_OK);
    4.19      if (status != PEP_STATUS_OK)
    4.20          goto pEp_error;
    4.21 @@ -1111,7 +1111,7 @@
    4.22      _src->enc_format = PEP_enc_none;
    4.23      
    4.24      // These vars are here to be clear, and because I don't know how this may change in the near future.
    4.25 -    status = _mime_encode_message_internal(_src, true, &mimetext, true, true);
    4.26 +    status = _mime_encode_message_internal(_src, true, &mimetext, true, false, true);
    4.27      assert(status == PEP_STATUS_OK);
    4.28      if (status != PEP_STATUS_OK)
    4.29          goto pEp_error;
    4.30 @@ -2003,7 +2003,8 @@
    4.31      if (max_version_major == 1)
    4.32          force_v_1 = true;
    4.33          
    4.34 -    if (enc_format == PEP_enc_none || !dest_keys_found ||
    4.35 +    if (enc_format == PEP_enc_none || enc_format == PEP_enc_sign_only ||
    4.36 +        !dest_keys_found || 
    4.37          stringlist_length(keys)  == 0 ||
    4.38          _rating(max_comm_type) < PEP_rating_reliable)
    4.39      {
    4.40 @@ -2014,6 +2015,21 @@
    4.41              added_key_to_real_src = true;
    4.42          }
    4.43          decorate_message(src, PEP_rating_undefined, NULL, true, true);
    4.44 +        
    4.45 +        // Now sign it
    4.46 +        if (!(flags & PEP_encrypt_flag_force_unsigned) 
    4.47 +            && (enc_format != PEP_enc_none) && !EMPTYSTR(src->from->fpr)) {
    4.48 +            msg = clone_to_empty_message(_src);                
    4.49 +            status = sign_PGP_MIME(session, _src, src->from->fpr, msg);
    4.50 +            if (status == PEP_STATUS_OK) {                
    4.51 +                msg->enc_format = PEP_enc_sign_only;
    4.52 +                *dst = msg;
    4.53 +            }
    4.54 +            else {
    4.55 +                status = PEP_CANNOT_SIGN;
    4.56 +                goto pEp_error;
    4.57 +            }    
    4.58 +        }
    4.59          return PEP_UNENCRYPTED;
    4.60      }
    4.61      else {
     5.1 --- a/src/mime.c	Sun Sep 22 21:41:35 2019 +0200
     5.2 +++ b/src/mime.c	Mon Sep 23 02:41:34 2019 +0200
     5.3 @@ -39,5 +39,5 @@
     5.4          char **mimetext
     5.5      )
     5.6  {
     5.7 -    return _mime_encode_message_internal(msg, omit_fields, mimetext, true, false);
     5.8 +    return _mime_encode_message_internal(msg, omit_fields, mimetext, true, false, false);
     5.9  }
     6.1 --- a/src/mime.h	Sun Sep 22 21:41:35 2019 +0200
     6.2 +++ b/src/mime.h	Mon Sep 23 02:41:34 2019 +0200
     6.3 @@ -92,7 +92,8 @@
     6.4          bool omit_fields,
     6.5          char **mimetext,
     6.6          bool transport_encode,
     6.7 -        bool set_attachment_forward_comment        
     6.8 +        bool set_attachment_forward_comment,
     6.9 +        bool omit_version       
    6.10      );
    6.11  
    6.12  
     7.1 --- a/src/pEpEngine.h	Sun Sep 22 21:41:35 2019 +0200
     7.2 +++ b/src/pEpEngine.h	Mon Sep 23 02:41:34 2019 +0200
     7.3 @@ -84,6 +84,7 @@
     7.4      PEP_VERIFY_NO_KEY                               = 0x0407,
     7.5      PEP_VERIFIED_AND_TRUSTED                        = 0x0408,
     7.6      PEP_CANNOT_REENCRYPT                            = 0x0409,
     7.7 +    PEP_CANNOT_SIGN                                 = 0x040a,
     7.8      PEP_CANNOT_DECRYPT_UNKNOWN                      = 0x04ff,
     7.9  
    7.10      PEP_TRUSTWORD_NOT_FOUND                         = 0x0501,
     8.1 --- a/test/src/MessageTwoPointOhTest.cc	Sun Sep 22 21:41:35 2019 +0200
     8.2 +++ b/test/src/MessageTwoPointOhTest.cc	Mon Sep 23 02:41:34 2019 +0200
     8.3 @@ -217,7 +217,7 @@
     8.4      }
     8.5  
     8.6      decrypted_msg->enc_format = PEP_enc_none;
     8.7 -    status = _mime_encode_message_internal(decrypted_msg, false, &encoded_text, false, false);
     8.8 +    status = _mime_encode_message_internal(decrypted_msg, false, &encoded_text, false, false, false);
     8.9      ASSERT_EQ(status , PEP_STATUS_OK);
    8.10      ASSERT_NE(encoded_text, nullptr);
    8.11      output_stream << "Decrypted message: " << endl;
     9.1 --- a/test/src/SignOnlyTest.cc	Sun Sep 22 21:41:35 2019 +0200
     9.2 +++ b/test/src/SignOnlyTest.cc	Mon Sep 23 02:41:34 2019 +0200
     9.3 @@ -7,6 +7,7 @@
     9.4  #include <fstream>
     9.5  
     9.6  #include "pEpEngine.h"
     9.7 +#include "mime.h"
     9.8  
     9.9  #include "test_util.h"
    9.10  #include "TestConstants.h"
    9.11 @@ -119,3 +120,36 @@
    9.12      // FIXME: free stuff
    9.13  
    9.14  }
    9.15 +TEST_F(SignOnlyTest, check_signed_message) {
    9.16 +    const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    9.17 +    PEP_STATUS status = read_file_and_import_key(session,
    9.18 +                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    9.19 +    assert(status == PEP_KEY_IMPORTED);
    9.20 +    pEp_identity* alice = NULL;
    9.21 +    status = set_up_ident_from_scratch(session,
    9.22 +                "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
    9.23 +                "pep.test.alice@pep-project.org", alice_fpr,
    9.24 +                "ALICE", "Alice in Wonderland", &alice, true);
    9.25 +    assert(status == PEP_STATUS_OK);            
    9.26 +    status = set_identity(session, alice);    
    9.27 +    assert(status == PEP_STATUS_OK);            
    9.28 +    status = set_own_key(session, alice, alice_fpr);
    9.29 +    
    9.30 +    message* msg = new_message(PEP_dir_outgoing);    
    9.31 +    msg->from = identity_dup(alice);
    9.32 +    msg->to = new_identity_list(identity_dup(alice));
    9.33 +    msg->shortmsg = strdup("Sign this, baby");
    9.34 +    msg->longmsg = strdup("Nah really, sign this.");
    9.35 +    msg->enc_format = PEP_enc_sign_only;
    9.36 +    
    9.37 +    message* enc_msg = NULL;
    9.38 +    
    9.39 +    status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_sign_only, 0);
    9.40 +    
    9.41 +    char* outmsg = NULL;
    9.42 +    
    9.43 +    status = mime_encode_message(enc_msg, false, &outmsg);
    9.44 +    
    9.45 +    cout << outmsg << endl; 
    9.46 +    
    9.47 +}
    10.1 --- a/test/src/test_util.cc	Sun Sep 22 21:41:35 2019 +0200
    10.2 +++ b/test/src/test_util.cc	Mon Sep 23 02:41:34 2019 +0200
    10.3 @@ -565,7 +565,7 @@
    10.4      }
    10.5  
    10.6      if (*flags & PEP_decrypt_flag_src_modified) {
    10.7 -        _mime_encode_message_internal(tmp_msg, false, modified_src, true, false);
    10.8 +        _mime_encode_message_internal(tmp_msg, false, modified_src, true, false, false);
    10.9          if (!modified_src) {
   10.10              *flags &= (~PEP_decrypt_flag_src_modified);
   10.11              decrypt_status = PEP_CANNOT_REENCRYPT; // Because we couldn't return it, I guess.
   10.12 @@ -573,7 +573,7 @@
   10.13      }
   10.14  
   10.15      // FIXME: test with att
   10.16 -    status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true, false);
   10.17 +    status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true, false, false);
   10.18  
   10.19      if (status == PEP_STATUS_OK)
   10.20      {
   10.21 @@ -658,7 +658,7 @@
   10.22          goto pEp_error;
   10.23      }
   10.24  
   10.25 -    status = _mime_encode_message_internal(enc_msg, false, mime_ciphertext, false, false);
   10.26 +    status = _mime_encode_message_internal(enc_msg, false, mime_ciphertext, false, false, false);
   10.27  
   10.28  pEp_error:
   10.29      free_message(tmp_msg);
   10.30 @@ -950,6 +950,9 @@
   10.31      if (trust && status == PEP_STATUS_OK) {
   10.32          if (!retval->me)
   10.33              status = update_identity(session, retval);
   10.34 +        else
   10.35 +            status = myself(session, retval);
   10.36 +            
   10.37          if (retval->comm_type >= PEP_ct_strong_but_unconfirmed) {
   10.38              retval->comm_type = (PEP_comm_type)(retval->comm_type | PEP_ct_confirmed);
   10.39              status = set_trust(session, retval);