ENGINE-650 sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 02 Oct 2019 13:07:45 +0200
branchsync
changeset 41045c9862277d1f
parent 4102 b2e687335205
child 4105 982548344703
child 4106 3ca70862e37a
ENGINE-650
src/message_api.c
test/src/ReencryptPlusExtraKeysTest.cc
     1.1 --- a/src/message_api.c	Tue Oct 01 08:40:29 2019 +0200
     1.2 +++ b/src/message_api.c	Wed Oct 02 13:07:45 2019 +0200
     1.3 @@ -3943,12 +3943,22 @@
     1.4      bool reenc_signer_key_is_own_key = false; // only matters for reencrypted messages 
     1.5      
     1.6      // 4. Reencrypt if necessary
     1.7 +    bool has_extra_keys = _have_extrakeys(extra);
     1.8 +    if (reencrypt && session->unencrypted_subject && !has_extra_keys) {
     1.9 +        if (src->shortmsg && msg->shortmsg) {
    1.10 +            if (strcmp(src->shortmsg, msg->shortmsg) == 0)
    1.11 +                reencrypt = false;
    1.12 +        }
    1.13 +        else if (src->shortmsg == NULL && msg->shortmsg == NULL)
    1.14 +            reencrypt = false;
    1.15 +    }    
    1.16 +
    1.17      if (reencrypt) {
    1.18          if (decrypt_status == PEP_DECRYPTED || decrypt_status == PEP_DECRYPTED_AND_VERIFIED) {
    1.19              const char* sfpr = NULL;
    1.20 -            if (_have_extrakeys(extra))
    1.21 +            if (has_extra_keys)
    1.22                  sfpr = _keylist->value;
    1.23 -             
    1.24 +
    1.25              if (sfpr && decrypt_status == PEP_DECRYPTED_AND_VERIFIED) {
    1.26                  own_key_is_listed(session, sfpr, &reenc_signer_key_is_own_key);
    1.27                  
    1.28 @@ -3988,7 +3998,7 @@
    1.29                          decrypt_status = PEP_CANNOT_REENCRYPT;
    1.30                  }
    1.31              }            
    1.32 -            else if (!_have_extrakeys(extra) && session->unencrypted_subject) {
    1.33 +            else if (!has_extra_keys && session->unencrypted_subject) {
    1.34                  free(src->shortmsg);
    1.35                  src->shortmsg = strdup(msg->shortmsg);
    1.36                  assert(src->shortmsg);
     2.1 --- a/test/src/ReencryptPlusExtraKeysTest.cc	Tue Oct 01 08:40:29 2019 +0200
     2.2 +++ b/test/src/ReencryptPlusExtraKeysTest.cc	Wed Oct 02 13:07:45 2019 +0200
     2.3 @@ -131,8 +131,71 @@
     2.4      status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
     2.5      ASSERT_NE(src_msg, nullptr);
     2.6      ASSERT_STREQ(src_msg->attachments->next->value, checker->attachments->next->value);
     2.7 +    config_unencrypted_subject(session, false);
     2.8 +
     2.9  }
    2.10  
    2.11 +TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_check_efficient) {
    2.12 +    config_unencrypted_subject(session, true);
    2.13 +    pEp_identity* carol = NULL;
    2.14 +
    2.15 +    PEP_STATUS status = set_up_preset(session, CAROL,
    2.16 +                                      true, true, true, true, true, &carol);
    2.17 +
    2.18 +    ASSERT_EQ(status , PEP_STATUS_OK);
    2.19 +    ASSERT_NE(carol, nullptr);
    2.20 +
    2.21 +    string mailfile = slurp("test_mails/From_M2_1.eml");
    2.22 +
    2.23 +    char* decrypted_text = nullptr;
    2.24 +
    2.25 +    // In: extra keys; Out: keys that were used to encrypt this.
    2.26 +    stringlist_t* keys = NULL;
    2.27 +    PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
    2.28 +    PEP_rating rating;
    2.29 +
    2.30 +    flags = PEP_decrypt_flag_untrusted_server;
    2.31 +    char* modified_src = NULL;
    2.32 +
    2.33 +    status = MIME_decrypt_message(session,
    2.34 +                                  mailfile.c_str(),
    2.35 +                                  mailfile.size(),
    2.36 +                                  &decrypted_text,
    2.37 +                                  &keys,
    2.38 +                                  &rating,
    2.39 +                                  &flags,
    2.40 +                                  &modified_src);
    2.41 +                                       
    2.42 +    ASSERT_NE(decrypted_text , nullptr);
    2.43 +    ASSERT_NE(modified_src , nullptr);
    2.44 +    message* checker = NULL;
    2.45 +    status = mime_decode_message(modified_src, strlen(modified_src), &checker);
    2.46 +    ASSERT_NE(checker, nullptr);
    2.47 +    ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
    2.48 +    cout << modified_src << endl;
    2.49 +    message* src_msg = NULL;
    2.50 +    status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
    2.51 +    ASSERT_NE(src_msg, nullptr);
    2.52 +    ASSERT_STREQ(src_msg->attachments->next->value, checker->attachments->next->value);
    2.53 +    
    2.54 +    message* dec_msg = NULL;
    2.55 +    flags = PEP_decrypt_flag_untrusted_server;
    2.56 +    free_stringlist(keys);
    2.57 +    keys = NULL; // remember, this is no extra_keys in this test
    2.58 +
    2.59 +    status = decrypt_message(session, checker, &dec_msg, &keys, &rating, &flags);
    2.60 +    ASSERT_EQ(status , PEP_STATUS_OK);
    2.61 +    ASSERT_NE(dec_msg , nullptr);
    2.62 +    ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
    2.63 +    ASSERT_NE(checker, nullptr);
    2.64 +    ASSERT_NE(dec_msg->_sender_fpr, nullptr);
    2.65 +    ASSERT_NE(keys, nullptr);
    2.66 +    ASSERT_STREQ(dec_msg->_sender_fpr, keys->value); // should be the same, since not reencrypted
    2.67 +    
    2.68 +    config_unencrypted_subject(session, false);    
    2.69 +}
    2.70 +
    2.71 +
    2.72  TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys) {
    2.73      config_unencrypted_subject(session, true);
    2.74      
    2.75 @@ -145,7 +208,6 @@
    2.76      stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
    2.77      stringlist_add(keys, fpr_pub_extra_key_1);
    2.78      
    2.79 -    config_unencrypted_subject(session, true);
    2.80      pEp_identity* carol = NULL;
    2.81  
    2.82      PEP_STATUS status = set_up_preset(session, CAROL,
    2.83 @@ -180,7 +242,6 @@
    2.84      status = mime_decode_message(modified_src, strlen(modified_src), &checker);
    2.85      ASSERT_NE(checker, nullptr);
    2.86      ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
    2.87 -    config_unencrypted_subject(session, false);
    2.88      cout << modified_src << endl;
    2.89      message* src_msg = NULL;
    2.90      status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
    2.91 @@ -221,6 +282,76 @@
    2.92      config_unencrypted_subject(session, false);      
    2.93  }
    2.94  
    2.95 +TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_unencrypted_subj_extra_keys_efficient) {
    2.96 +    config_unencrypted_subject(session, true);
    2.97 +    
    2.98 +    const char* fpr_pub_extra_key_0 = "33BB6C92EBFB6F29641C75B5B79D916C828AA789";
    2.99 +    const char* fpr_pub_extra_key_1 = "3DB93A746785FDD6110798AB3B193A9E8B026AEC";
   2.100 +
   2.101 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_0-0x828AA789_pub.asc"));
   2.102 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/reencrypt_extra_keys_1-0x8B026AEC_pub.asc"));
   2.103 +    
   2.104 +    stringlist_t* keys = new_stringlist(fpr_pub_extra_key_0);
   2.105 +    stringlist_add(keys, fpr_pub_extra_key_1);
   2.106 +    
   2.107 +    pEp_identity* carol = NULL;
   2.108 +
   2.109 +    PEP_STATUS status = set_up_preset(session, CAROL,
   2.110 +                                      true, true, true, true, true, &carol);
   2.111 +
   2.112 +    ASSERT_EQ(status, PEP_STATUS_OK);
   2.113 +    ASSERT_NE(carol, nullptr);
   2.114 +
   2.115 +    string mailfile = slurp("test_mails/From_M2_1.eml");
   2.116 +
   2.117 +    char* decrypted_text = nullptr;
   2.118 +
   2.119 +    // In: extra keys; Out: keys that were used to encrypt this.
   2.120 +    PEP_decrypt_flags_t flags = PEP_decrypt_flag_untrusted_server;
   2.121 +    PEP_rating rating;
   2.122 +
   2.123 +    flags = PEP_decrypt_flag_untrusted_server;
   2.124 +    char* modified_src = NULL;
   2.125 +
   2.126 +    status = MIME_decrypt_message(session,
   2.127 +                                  mailfile.c_str(),
   2.128 +                                  mailfile.size(),
   2.129 +                                  &decrypted_text,
   2.130 +                                  &keys,
   2.131 +                                  &rating,
   2.132 +                                  &flags,
   2.133 +                                  &modified_src);
   2.134 +                                       
   2.135 +    ASSERT_NE(decrypted_text , nullptr);
   2.136 +    ASSERT_NE(modified_src , nullptr);
   2.137 +    message* checker = NULL;
   2.138 +    status = mime_decode_message(modified_src, strlen(modified_src), &checker);
   2.139 +    ASSERT_NE(checker, nullptr);
   2.140 +    ASSERT_STREQ(checker->shortmsg, "Boom shaka laka");
   2.141 +    cout << modified_src << endl;
   2.142 +    message* src_msg = NULL;
   2.143 +    status = mime_decode_message(mailfile.c_str(), mailfile.size(), &src_msg);
   2.144 +    ASSERT_NE(src_msg, nullptr);
   2.145 +    ASSERT_STRNE(src_msg->attachments->next->value, checker->attachments->next->value);
   2.146 +
   2.147 +    free_stringlist(keys);
   2.148 +    keys = new_stringlist(fpr_pub_extra_key_0);
   2.149 +    stringlist_add(keys, fpr_pub_extra_key_1);
   2.150 +    flags = PEP_decrypt_flag_untrusted_server;
   2.151 +    message* decryptomatic = NULL;
   2.152 +    status = decrypt_message(session, checker, &decryptomatic, &keys, &rating, &flags);
   2.153 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.154 +    ASSERT_NE(decryptomatic, nullptr);
   2.155 +    ASSERT_EQ(flags & PEP_decrypt_flag_src_modified, 0);
   2.156 +    ASSERT_NE(checker, nullptr);
   2.157 +    ASSERT_NE(decryptomatic->_sender_fpr, nullptr);
   2.158 +    ASSERT_NE(keys, nullptr);
   2.159 +    ASSERT_STRNE(decryptomatic->_sender_fpr, keys->value); // should be the same, since not reencrypted
   2.160 +    
   2.161 +    config_unencrypted_subject(session, false);    
   2.162 +}
   2.163 +
   2.164 +
   2.165  TEST_F(ReencryptPlusExtraKeysTest, check_reencrypt_plus_extra_keys) {
   2.166      PEP_STATUS status = PEP_STATUS_OK;
   2.167