merged in ENGINE-649 sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 30 Sep 2019 16:48:37 +0200
branchsync
changeset 40991d38cc7fcc5b
parent 4096 ef291fbfa5c6
parent 4098 627522e248e6
child 4101 76729f6acb0d
child 4102 b2e687335205
merged in ENGINE-649
     1.1 --- a/src/message_api.c	Fri Sep 27 14:26:09 2019 +0200
     1.2 +++ b/src/message_api.c	Mon Sep 30 16:48:37 2019 +0200
     1.3 @@ -2318,17 +2318,21 @@
     1.4      if (status != PEP_STATUS_OK)
     1.5          goto pEp_error;
     1.6  
     1.7 -    if (msg && msg->shortmsg == NULL) {
     1.8 -        if (session->unencrypted_subject && src->shortmsg)
     1.9 -            msg->shortmsg = strdup(src->shortmsg);
    1.10 -        else
    1.11 +    if (msg) {
    1.12 +        if (!src->shortmsg) {
    1.13 +            free(msg->shortmsg);
    1.14              msg->shortmsg = _pEp_subj_copy();
    1.15 -        assert(msg->shortmsg);
    1.16 -        if (msg->shortmsg == NULL)
    1.17 -            goto enomem;
    1.18 -    }
    1.19 -
    1.20 -    if (msg) {
    1.21 +            assert(msg->shortmsg);
    1.22 +            if (msg->shortmsg == NULL)
    1.23 +                goto enomem;
    1.24 +        }
    1.25 +        else {
    1.26 +            if (session->unencrypted_subject && (flags & PEP_encrypt_reencrypt)) {
    1.27 +                free(msg->shortmsg);
    1.28 +                msg->shortmsg = strdup(src->shortmsg);
    1.29 +            }    
    1.30 +        }
    1.31 +
    1.32          if (_src->id) {
    1.33              msg->id = strdup(_src->id);
    1.34              assert(msg->id);
    1.35 @@ -3935,17 +3939,14 @@
    1.36          }
    1.37      }
    1.38  
    1.39 -    // 4. Set up return values
    1.40 -    *dst = msg;
    1.41 -    *keylist = _keylist;
    1.42  
    1.43      bool reenc_signer_key_is_own_key = false; // only matters for reencrypted messages 
    1.44      
    1.45 -    // 5. Reencrypt if necessary
    1.46 +    // 4. Reencrypt if necessary
    1.47      if (reencrypt) {
    1.48          if (decrypt_status == PEP_DECRYPTED || decrypt_status == PEP_DECRYPTED_AND_VERIFIED) {
    1.49              const char* sfpr = NULL;
    1.50 -            if (_have_extrakeys(*keylist))
    1.51 +            if (_have_extrakeys(extra))
    1.52                  sfpr = _keylist->value;
    1.53               
    1.54              if (sfpr && decrypt_status == PEP_DECRYPTED_AND_VERIFIED) {
    1.55 @@ -3966,7 +3967,7 @@
    1.56                              if (target_id) {
    1.57                                  reencrypt_status = encrypt_message_for_self(session, target_id, msg,
    1.58                                                                              extra, &reencrypt_msg, PEP_enc_PGP_MIME,
    1.59 -                                                                            0);
    1.60 +                                                                            PEP_encrypt_reencrypt);
    1.61                                  if (reencrypt_status != PEP_STATUS_OK)
    1.62                                      reencrypt_status = PEP_CANNOT_REENCRYPT;
    1.63                                  
    1.64 @@ -3987,7 +3988,7 @@
    1.65                          decrypt_status = PEP_CANNOT_REENCRYPT;
    1.66                  }
    1.67              }            
    1.68 -            else if (!_have_extrakeys(*keylist) && session->unencrypted_subject) {
    1.69 +            else if (!_have_extrakeys(extra) && session->unencrypted_subject) {
    1.70                  free(src->shortmsg);
    1.71                  src->shortmsg = strdup(msg->shortmsg);
    1.72                  assert(src->shortmsg);
    1.73 @@ -3998,6 +3999,10 @@
    1.74          }
    1.75      }
    1.76      
    1.77 +    // 5. Set up return values
    1.78 +    *dst = msg;
    1.79 +    *keylist = _keylist;
    1.80 +    
    1.81      // Double-check for message 2.1: (note, we don't do this for already-reencrypted-messages)
    1.82      if (!(reencrypt && reenc_signer_key_is_own_key)) { 
    1.83          if (major_ver > 2 || (major_ver == 2 && minor_ver > 0)) {
     2.1 --- a/src/message_api.h	Fri Sep 27 14:26:09 2019 +0200
     2.2 +++ b/src/message_api.h	Mon Sep 30 16:48:37 2019 +0200
     2.3 @@ -41,8 +41,12 @@
     2.4      // This is mainly used by pEp clients to send private keys to 
     2.5      // their own PGP-only device
     2.6      PEP_encrypt_flag_force_version_1 = 0x10,
     2.7 +        
     2.8 +    PEP_encrypt_flag_key_reset_only = 0x20,
     2.9      
    2.10 -    PEP_encrypt_flag_key_reset_only = 0x20
    2.11 +    // This flag is used to let internal functions know that an encryption 
    2.12 +    // call is being used as part of a reencryption operation
    2.13 +    PEP_encrypt_reencrypt = 0x40
    2.14      
    2.15  } PEP_encrypt_flags; 
    2.16  
     3.1 --- a/test/src/ReencryptPlusExtraKeysTest.cc	Fri Sep 27 14:26:09 2019 +0200
     3.2 +++ b/test/src/ReencryptPlusExtraKeysTest.cc	Mon Sep 30 16:48:37 2019 +0200
     3.3 @@ -88,6 +88,139 @@
     3.4  }  // namespace
     3.5  
     3.6  
     3.7 +TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj) {
     3.8 +    config_unencrypted_subject(session, true);
     3.9 +    pEp_identity* carol = NULL;
    3.10 +
    3.11 +    PEP_STATUS status = set_up_preset(session, CAROL,
    3.12 +                                      true, true, true, true, true, &carol);
    3.13 +
    3.14 +    ASSERT_EQ(status , PEP_STATUS_OK);
    3.15 +    ASSERT_NE(carol, nullptr);
    3.16 +
    3.17 +    string mailfile = slurp("test_mails/From_M2_1.eml");
    3.18 +
    3.19 +    char* decrypted_text = nullptr;
    3.20 +
    3.21 +    // In: extra keys; Out: keys that were used to encrypt this.
    3.22 +    stringlist_t* keys = NULL;
    3.23 +    PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
    3.24 +    PEP_rating rating;
    3.25 +
    3.26 +    flags = PEP_decrypt_flag_untrusted_server;
    3.27 +    char* modified_src = NULL;
    3.28 +
    3.29 +    status = MIME_decrypt_message(session,
    3.30 +                                  mailfile.c_str(),
    3.31 +                                  mailfile.size(),
    3.32 +                                  &decrypted_text,
    3.33 +                                  &keys,
    3.34 +                                  &rating,
    3.35 +                                  &flags,
    3.36 +                                  &modified_src);
    3.37 +                                       
    3.38 +    ASSERT_NE(decrypted_text , nullptr);
    3.39 +    ASSERT_NE(modified_src , nullptr);
    3.40 +    message* checker = NULL;
    3.41 +    status = mime_decode_message(modified_src, strlen(modified_src), &checker);
    3.42 +    ASSERT_NE(checker, nullptr);
    3.43 +    ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
    3.44 +    config_unencrypted_subject(session, false);
    3.45 +    cout << modified_src << endl;
    3.46 +    message* src_msg = NULL;
    3.47 +    status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
    3.48 +    ASSERT_NE(src_msg, nullptr);
    3.49 +    ASSERT_STREQ(src_msg->attachments->next->value, checker->attachments->next->value);
    3.50 +}
    3.51 +
    3.52 +TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys) {
    3.53 +    config_unencrypted_subject(session, true);
    3.54 +    
    3.55 +    const char* fpr_pub_extra_key_0 = "33BB6C92EBFB6F29641C75B5B79D916C828AA789";
    3.56 +    const char* fpr_pub_extra_key_1 = "3DB93A746785FDD6110798AB3B193A9E8B026AEC";
    3.57 +
    3.58 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc"));
    3.59 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc"));
    3.60 +    
    3.61 +    stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
    3.62 +    stringlist_add(keys, fpr_pub_extra_key_1);
    3.63 +    
    3.64 +    config_unencrypted_subject(session, true);
    3.65 +    pEp_identity* carol = NULL;
    3.66 +
    3.67 +    PEP_STATUS status = set_up_preset(session, CAROL,
    3.68 +                                      true, true, true, true, true, &carol);
    3.69 +
    3.70 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.71 +    ASSERT_NE(carol, nullptr);
    3.72 +
    3.73 +    string mailfile = slurp("test_mails/From_M2_1.eml");
    3.74 +
    3.75 +    char* decrypted_text = nullptr;
    3.76 +
    3.77 +    // In: extra keys; Out: keys that were used to encrypt this.
    3.78 +    PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
    3.79 +    PEP_rating rating;
    3.80 +
    3.81 +    flags = PEP_decrypt_flag_untrusted_server;
    3.82 +    char* modified_src = NULL;
    3.83 +
    3.84 +    status = MIME_decrypt_message(session,
    3.85 +                                  mailfile.c_str(),
    3.86 +                                  mailfile.size(),
    3.87 +                                  &decrypted_text,
    3.88 +                                  &keys,
    3.89 +                                  &rating,
    3.90 +                                  &flags,
    3.91 +                                  &modified_src);
    3.92 +                                       
    3.93 +    ASSERT_NE(decrypted_text , nullptr);
    3.94 +    ASSERT_NE(modified_src , nullptr);
    3.95 +    message* checker = NULL;
    3.96 +    status = mime_decode_message(modified_src, strlen(modified_src), &checker);
    3.97 +    ASSERT_NE(checker, nullptr);
    3.98 +    ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
    3.99 +    config_unencrypted_subject(session, false);
   3.100 +    cout << modified_src << endl;
   3.101 +    message* src_msg = NULL;
   3.102 +    status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
   3.103 +    ASSERT_NE(src_msg, nullptr);
   3.104 +    ASSERT_STRNE(src_msg->attachments->next->value, checker->attachments->next->value);
   3.105 +
   3.106 +    flags = 0;
   3.107 +    message* decryptomatic = NULL;
   3.108 +    stringlist_t* extra_keys = NULL;
   3.109 +    status = decrypt_message(session, checker, &decryptomatic, &extra_keys, &rating, &flags);
   3.110 +
   3.111 +    bool own_key_found, extra_key_0_found, extra_key_1_found;
   3.112 +    
   3.113 +    own_key_found = extra_key_0_found = extra_key_1_found = false;
   3.114 +    int i = 0;
   3.115 +
   3.116 +    for (stringlist_t* kl = extra_keys; kl && kl->value; kl = kl->next, i++)
   3.117 +    {
   3.118 +        if (i == 0) {
   3.119 +              output_stream << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   3.120 +              ASSERT_STRCASEEQ(carol->fpr, kl->value);
   3.121 +        }
   3.122 +        else {
   3.123 +            if (strcasecmp(carol->fpr, kl->value) == 0)
   3.124 +                own_key_found = true;
   3.125 +            else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0)
   3.126 +                extra_key_0_found = true;
   3.127 +            else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0)
   3.128 +                extra_key_1_found = true;
   3.129 +            else {
   3.130 +                output_stream << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
   3.131 +                ASSERT_TRUE(false);
   3.132 +            }
   3.133 +        }
   3.134 +        ASSERT_LT(i, 4);
   3.135 +    }
   3.136 +    ASSERT_TRUE(own_key_found && extra_key_0_found && extra_key_1_found);  
   3.137 +    config_unencrypted_subject(session, false);      
   3.138 +}
   3.139 +
   3.140  TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_plus_extra_keys) {
   3.141      PEP_STATUS status = PEP_STATUS_OK;
   3.142