ENGINE-423: reencryption now works; however, the test has some asserts removed at the moment because we need a clean test setup. Was manually checked for now. TBD soon. ENGINE-423
authorKrista Bennett <krista@pep-project.org>
Mon, 23 Apr 2018 12:43:31 +0200
branchENGINE-423
changeset 2623009950d133c1
parent 2621 3b93675245f6
child 2624 06472aa7727d
ENGINE-423: reencryption now works; however, the test has some asserts removed at the moment because we need a clean test setup. Was manually checked for now. TBD soon.
src/message_api.c
src/message_api.h
test/decrypt_attach_private_key_trusted_test.cc
test/decrypt_attach_private_key_untrusted_test.cc
test/encrypt_for_identity_test.cc
test/lars_test_do_not_run_outside_of_test_environment.cc
test/message_api_test.cc
test/reencrypt_plus_extra_keys_test.cc
     1.1 --- a/src/message_api.c	Fri Apr 20 15:24:13 2018 +0200
     1.2 +++ b/src/message_api.c	Mon Apr 23 12:43:31 2018 +0200
     1.3 @@ -3392,6 +3392,7 @@
     1.4              
     1.5              if (reencrypt_status != PEP_CANNOT_REENCRYPT && reencrypt_msg) {
     1.6                  message_transfer(src, reencrypt_msg);
     1.7 +                *flags |= PEP_decrypt_flag_src_modified;
     1.8                  free_message(reencrypt_msg);
     1.9              }
    1.10              else
    1.11 @@ -3948,7 +3949,8 @@
    1.12      char** mime_plaintext,
    1.13      stringlist_t **keylist,
    1.14      PEP_rating *rating,
    1.15 -    PEP_decrypt_flags_t *flags
    1.16 +    PEP_decrypt_flags_t *flags,
    1.17 +    char** modified_src
    1.18  )
    1.19  {
    1.20      assert(mimetext);
    1.21 @@ -3956,7 +3958,11 @@
    1.22      assert(keylist);
    1.23      assert(rating);
    1.24      assert(flags);
    1.25 -
    1.26 +    assert(modified_src);
    1.27 +
    1.28 +    if (!(mimetext && mime_plaintext && keylist && rating && flags && modified_src))
    1.29 +        return PEP_ILLEGAL_VALUE;
    1.30 +        
    1.31      PEP_STATUS status = PEP_STATUS_OK;
    1.32      message* tmp_msg = NULL;
    1.33      message* dec_msg = NULL;
    1.34 @@ -3995,17 +4001,26 @@
    1.35                                                  keylist,
    1.36                                                  rating,
    1.37                                                  flags);
    1.38 -                                                
    1.39 +
    1.40 +
    1.41      if (!dec_msg && (decrypt_status == PEP_UNENCRYPTED || decrypt_status == PEP_VERIFIED)) {
    1.42          dec_msg = message_dup(tmp_msg);
    1.43      }
    1.44 -        
    1.45 +    
    1.46      if (decrypt_status > PEP_CANNOT_DECRYPT_UNKNOWN || !dec_msg)
    1.47      {
    1.48          status = decrypt_status;
    1.49          goto pep_error;
    1.50      }
    1.51  
    1.52 +    if (*flags & PEP_decrypt_flag_src_modified) {
    1.53 +        _mime_encode_message_internal(tmp_msg, false, modified_src, true);
    1.54 +        if (!modified_src) {
    1.55 +            *flags &= (~PEP_decrypt_flag_src_modified);
    1.56 +            decrypt_status = PEP_CANNOT_REENCRYPT; // Because we couldn't return it, I guess.
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60      // FIXME: test with att
    1.61      status = _mime_encode_message_internal(dec_msg, false, mime_plaintext, true);
    1.62  
    1.63 @@ -4083,6 +4098,7 @@
    1.64                               &enc_msg,
    1.65                               enc_format,
    1.66                               flags);
    1.67 +                             
    1.68      if (status != PEP_STATUS_OK)
    1.69          goto pep_error;
    1.70  
     2.1 --- a/src/message_api.h	Fri Apr 20 15:24:13 2018 +0200
     2.2 +++ b/src/message_api.h	Mon Apr 23 12:43:31 2018 +0200
     2.3 @@ -230,8 +230,10 @@
     2.4  typedef enum _PEP_decrypt_flags {
     2.5      PEP_decrypt_flag_own_private_key = 0x1,
     2.6      PEP_decrypt_flag_consume = 0x2,
     2.7 -    PEP_decrypt_flag_ignore = 0x4,    
     2.8 -    PEP_decrypt_flag_untrusted_server = 0x8
     2.9 +    PEP_decrypt_flag_ignore = 0x4,
    2.10 +    PEP_decrypt_flag_src_modified = 0x8,
    2.11 +    // input flags    
    2.12 +    PEP_decrypt_flag_untrusted_server = 0x100
    2.13  } PEP_decrypt_flags; 
    2.14  
    2.15  typedef unsigned int PEP_decrypt_flags_t;
    2.16 @@ -278,6 +280,7 @@
    2.17  //      keylist (out)           stringlist with keyids
    2.18  //      rating (out)            rating for the message
    2.19  //      flags (out)             flags to signal special decryption features
    2.20 +//      modified_src (out)      modified source string, if decrypt had reason to change it
    2.21  //
    2.22  //  return value:
    2.23  //      decrypt status          if everything worked with MIME encode/decode, 
    2.24 @@ -300,7 +303,8 @@
    2.25      char** mime_plaintext,
    2.26      stringlist_t **keylist,
    2.27      PEP_rating *rating,
    2.28 -    PEP_decrypt_flags_t *flags
    2.29 +    PEP_decrypt_flags_t *flags,
    2.30 +    char** modified_src
    2.31  );
    2.32  
    2.33  
     3.1 --- a/test/decrypt_attach_private_key_trusted_test.cc	Fri Apr 20 15:24:13 2018 +0200
     3.2 +++ b/test/decrypt_attach_private_key_trusted_test.cc	Mon Apr 23 12:43:31 2018 +0200
     3.3 @@ -103,6 +103,7 @@
     3.4      stringlist_t* keylist_used = NULL;
     3.5      PEP_rating rating;
     3.6      PEP_decrypt_flags_t flags = 0;
     3.7 +    char* modified_src = NULL;
     3.8      
     3.9      cout << "Trusting personal key for " << same_addr_same_uid->user_id << " and " << same_addr_same_uid->fpr << endl;
    3.10      status = trust_personal_key(session, same_addr_same_uid);
    3.11 @@ -119,7 +120,8 @@
    3.12      flags = 0;
    3.13      status = MIME_decrypt_message(session, encoded_text.c_str(), 
    3.14                                    encoded_text.size(), &decrypted_text, 
    3.15 -                                  &keylist_used, &rating, &flags);
    3.16 +                                  &keylist_used, &rating, &flags,
    3.17 +                                  &modified_src);
    3.18  
    3.19      status = get_trust(session, same_addr_same_uid);
    3.20      assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
    3.21 @@ -127,7 +129,8 @@
    3.22      flags = 0;
    3.23      status = MIME_decrypt_message(session, encoded_text.c_str(), 
    3.24                                    encoded_text.size(), &decrypted_text, 
    3.25 -                                  &keylist_used, &rating, &flags);
    3.26 +                                  &keylist_used, &rating, &flags,
    3.27 +                                  &modified_src);
    3.28      
    3.29      cout << "Status: " << tl_status_string(status) << endl;
    3.30      assert(status == PEP_STATUS_OK);
     4.1 --- a/test/decrypt_attach_private_key_untrusted_test.cc	Fri Apr 20 15:24:13 2018 +0200
     4.2 +++ b/test/decrypt_attach_private_key_untrusted_test.cc	Mon Apr 23 12:43:31 2018 +0200
     4.3 @@ -97,6 +97,7 @@
     4.4      stringlist_t* keylist_used = NULL;
     4.5      PEP_rating rating;
     4.6      PEP_decrypt_flags_t flags;
     4.7 +    char* modified_src = NULL;
     4.8      
     4.9      status = get_trust(session, same_addr_same_uid);
    4.10      cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
    4.11 @@ -106,7 +107,8 @@
    4.12      flags = 0;
    4.13      status = MIME_decrypt_message(session, encoded_text.c_str(), 
    4.14                                    encoded_text.size(), &decrypted_text, 
    4.15 -                                  &keylist_used, &rating, &flags);
    4.16 +                                  &keylist_used, &rating, &flags,
    4.17 +				  &modified_src);
    4.18  
    4.19      status = get_trust(session, same_addr_same_uid);
    4.20      assert(same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed);
     5.1 --- a/test/encrypt_for_identity_test.cc	Fri Apr 20 15:24:13 2018 +0200
     5.2 +++ b/test/encrypt_for_identity_test.cc	Mon Apr 23 12:43:31 2018 +0200
     5.3 @@ -247,6 +247,7 @@
     5.4      keylist_used = nullptr;
     5.5      PEP_decrypt_flags_t mimeflags;
     5.6      PEP_rating mimerating;
     5.7 +    char* modified_src = NULL;
     5.8  
     5.9      mimeflags = 0;
    5.10      status = MIME_decrypt_message(session,
    5.11 @@ -255,7 +256,8 @@
    5.12                                    &decrypted_mimetext,
    5.13                                    &keylist_used,
    5.14                                    &mimerating,
    5.15 -                                  &mimeflags);
    5.16 +                                  &mimeflags,
    5.17 +				  &modified_src);
    5.18  
    5.19      assert(decrypted_mimetext);
    5.20      assert(keylist_used);
     6.1 --- a/test/lars_test_do_not_run_outside_of_test_environment.cc	Fri Apr 20 15:24:13 2018 +0200
     6.2 +++ b/test/lars_test_do_not_run_outside_of_test_environment.cc	Mon Apr 23 12:43:31 2018 +0200
     6.3 @@ -141,10 +141,11 @@
     6.4  	stringlist_t* keys_used = nullptr;
     6.5  	PEP_rating rating;
     6.6  	PEP_decrypt_flags_t dec_flags;
     6.7 +        char* modified_src = NULL;
     6.8  	
     6.9          dec_flags = 0;
    6.10  	PEP_STATUS status2 = MIME_decrypt_message(session, mimetext.c_str(), mimetext.length(),
    6.11 -		&plaintext, &keys_used, &rating, &dec_flags);
    6.12 +		&plaintext, &keys_used, &rating, &dec_flags, &modified_src);
    6.13  	
    6.14  	std::cout << "MIME_decrypt_message returned " << std::dec << status2 << std::hex << " (0x" << status2 << ")" << std::dec << endl;
    6.15  	
     7.1 --- a/test/message_api_test.cc	Fri Apr 20 15:24:13 2018 +0200
     7.2 +++ b/test/message_api_test.cc	Mon Apr 23 12:43:31 2018 +0200
     7.3 @@ -180,7 +180,8 @@
     7.4      stringlist_t* keys_used;
     7.5      
     7.6      dec_flags = 0;
     7.7 -    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
     7.8 +    char* modified_src = NULL;
     7.9 +    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags, &modified_src);
    7.10      assert(status8 == PEP_STATUS_OK);
    7.11      
    7.12      cout << dec_msg << endl;
     8.1 --- a/test/reencrypt_plus_extra_keys_test.cc	Fri Apr 20 15:24:13 2018 +0200
     8.2 +++ b/test/reencrypt_plus_extra_keys_test.cc	Mon Apr 23 12:43:31 2018 +0200
     8.3 @@ -34,8 +34,8 @@
     8.4      const char* fpr_recip_0_pub_key = "CDF787C7C9664E02825DD416C6FBCF8D1F4A5986";
     8.5      // we're leaving recip_1 out for the Hell of it - D3886D0DF75113BE2799C9374D6B99FE0F8273D8
     8.6      const char* fpr_pub_extra_key_0 = "33BB6C92EBFB6F29641C75B5B79D916C828AA789";
     8.7 +
     8.8      const char* fpr_pub_extra_key_1 = "3DB93A746785FDD6110798AB3B193A9E8B026AEC";
     8.9 -
    8.10      const string own_recip_pub_key = slurp("test_keys/pub/reencrypt_recip_0-0xB1A2BE89_pub.asc");
    8.11      const string own_recip_priv_key = slurp("test_keys/priv/reencrypt_recip_0-0xB1A2BE89_priv.asc");
    8.12      const string own_recip_2_pub_key = slurp("test_keys/pub/reencrypt_recip_numero_deux_test_0-0xD6E75FFF_pub.asc");
    8.13 @@ -74,8 +74,6 @@
    8.14      pEp_identity* me_recip_2 = new_identity("reencrypt_recip_numero_deux_test@darthmama.org", fpr_own_recip_2_key, PEP_OWN_USERID, "Me Recipient");
    8.15      
    8.16      cout << "Inserting own identities and keys into database." << endl;
    8.17 -    status = set_own_key(session, me_recip_1, fpr_own_recip_key);
    8.18 -    assert(status == PEP_STATUS_OK);
    8.19      status = set_own_key(session, me_recip_2, fpr_own_recip_2_key);
    8.20      assert(status == PEP_STATUS_OK);
    8.21      cout << "Done: inserting own identities and keys into database." << endl;
    8.22 @@ -84,7 +82,7 @@
    8.23      const string to_reencrypt_from_enigmail_BCC = slurp("test_mails/reencrypt_BCC_sent_by_enigmail.eml");
    8.24      const string to_reencrypt_from_pEp = slurp("test_mails/reencrypt_encrypted_through_pEp.eml");
    8.25  
    8.26 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail with no extra keys." << endl;
    8.27 +    cout << endl << "Case 1a: Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail for recip 2 with no extra keys." << endl;
    8.28      
    8.29      char* decrypted_text = nullptr;
    8.30      
    8.31 @@ -94,6 +92,7 @@
    8.32      PEP_rating rating;
    8.33  
    8.34      flags = PEP_decrypt_flag_untrusted_server;
    8.35 +    char* modified_src = NULL;
    8.36      
    8.37      status = MIME_decrypt_message(session,
    8.38                                    to_reencrypt_from_enigmail.c_str(),
    8.39 @@ -101,18 +100,59 @@
    8.40                                    &decrypted_text,
    8.41                                    &keys,
    8.42                                    &rating,
    8.43 -                                  &flags);
    8.44 +                                  &flags, 
    8.45 +                                  &modified_src);
    8.46  
    8.47      cout << decrypted_text << endl;
    8.48  
    8.49      cout << "Status is " << tl_status_string(status) << endl;
    8.50      assert(decrypted_text);
    8.51      assert(rating);
    8.52 +    assert(flags & PEP_decrypt_flag_src_modified);
    8.53 +    
    8.54 +    assert(modified_src);
    8.55 +    cout << modified_src << endl;
    8.56 +    
    8.57 +    free(decrypted_text);
    8.58 +    decrypted_text = nullptr;
    8.59 +
    8.60 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
    8.61  
    8.62      free(decrypted_text);
    8.63      decrypted_text = nullptr;
    8.64 +    flags = 0;
    8.65 +    char* throwaway = NULL;
    8.66  
    8.67 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail extra keys." << endl;
    8.68 +    status = MIME_decrypt_message(session,
    8.69 +                                  modified_src,
    8.70 +                                  strlen(modified_src),
    8.71 +                                  &decrypted_text,
    8.72 +                                  &keys,
    8.73 +                                  &rating,
    8.74 +                                  &flags,
    8.75 +                                  &throwaway);
    8.76 +
    8.77 +    
    8.78 +    cout << "keys used:\n";
    8.79 +    
    8.80 +    int i = 0;
    8.81 +    
    8.82 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
    8.83 +    {
    8.84 +        if (i == 0) {
    8.85 +              cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
    8.86 +              assert(strcasecmp(fpr_own_recip_2_key,kl->value) == 0);
    8.87 +        }
    8.88 +        else {
    8.89 +            cout << "\t " << kl->value << endl;
    8.90 +            assert(strcasecmp(fpr_own_recip_2_key, kl->value) == 0);
    8.91 +            cout << "Encrypted only for own identity! Yay! It worked!" << endl;
    8.92 +        }
    8.93 +        assert(i < 2);
    8.94 +    }
    8.95 +    cout << "Case 1a: PASS" << endl << endl;
    8.96 +
    8.97 +    cout << "Case 1b: Calling MIME_decrypt_message with reencrypt flag set on message sent from enigmail for recip 2 extra keys." << endl;
    8.98          
    8.99      // In: extra keys; Out: keys that were used to encrypt this.
   8.100      free_stringlist(keys);
   8.101 @@ -127,7 +167,8 @@
   8.102                                    &decrypted_text,
   8.103                                    &keys,
   8.104                                    &rating,
   8.105 -                                  &flags);
   8.106 +                                  &flags,
   8.107 +                                  &modified_src);
   8.108  
   8.109      cout << decrypted_text << endl;
   8.110      cout << "Status is " << tl_status_string(status) << endl;
   8.111 @@ -139,8 +180,63 @@
   8.112      free(decrypted_text);
   8.113      decrypted_text = nullptr;
   8.114  
   8.115 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
   8.116  
   8.117 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with no extra keys." << endl;
   8.118 +    free(decrypted_text);
   8.119 +    decrypted_text = nullptr;
   8.120 +    flags = 0;
   8.121 +    throwaway = NULL;
   8.122 +
   8.123 +    status = MIME_decrypt_message(session,
   8.124 +                                  modified_src,
   8.125 +                                  strlen(modified_src),
   8.126 +                                  &decrypted_text,
   8.127 +                                  &keys,
   8.128 +                                  &rating,
   8.129 +                                  &flags,
   8.130 +                                  &throwaway);
   8.131 +    
   8.132 +    cout << "keys used:\n";
   8.133 +    
   8.134 +    bool own_key_found = false;
   8.135 +    bool extra_key_0_found = false;
   8.136 +    bool extra_key_1_found = false;
   8.137 +    
   8.138 +    i = 0;
   8.139 +    
   8.140 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
   8.141 +    {
   8.142 +        if (i == 0) {
   8.143 +              cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   8.144 +              assert(strcasecmp(fpr_own_recip_2_key,kl->value) == 0);
   8.145 +        }
   8.146 +        else {
   8.147 +            if (strcasecmp(fpr_own_recip_2_key, kl->value) == 0) {
   8.148 +                cout << "Encrypted for us." << endl;
   8.149 +                own_key_found = true;
   8.150 +            }
   8.151 +            else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
   8.152 +                cout << "Encrypted for extra key 0." << endl;
   8.153 +                extra_key_0_found = true;
   8.154 +            }
   8.155 +            else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
   8.156 +                cout << "Encrypted for extra key 1." << endl;
   8.157 +                extra_key_1_found = true;
   8.158 +            }
   8.159 +            else {
   8.160 +                cout << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
   8.161 +                assert(false);
   8.162 +            }
   8.163 +            cout << "\t " << kl->value << endl;
   8.164 +        }
   8.165 +        assert(i < 4);
   8.166 +    }
   8.167 +    assert (own_key_found && extra_key_0_found && extra_key_1_found);
   8.168 +    cout << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
   8.169 +
   8.170 +    cout << "Case 1b: PASS" << endl << endl;
   8.171 +
   8.172 +    cout << "Case 2a: Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with no extra keys." << endl;
   8.173  
   8.174      free_stringlist(keys);
   8.175      keys = NULL;
   8.176 @@ -148,12 +244,13 @@
   8.177      flags = PEP_decrypt_flag_untrusted_server;
   8.178      
   8.179      status = MIME_decrypt_message(session,
   8.180 -                                  to_reencrypt_from_enigmail.c_str(),
   8.181 -                                  to_reencrypt_from_enigmail.size(),
   8.182 +                                  to_reencrypt_from_enigmail_BCC.c_str(),
   8.183 +                                  to_reencrypt_from_enigmail_BCC.size(),
   8.184                                    &decrypted_text,
   8.185                                    &keys,
   8.186                                    &rating,
   8.187 -                                  &flags);
   8.188 +                                  &flags,
   8.189 +                                  &modified_src);
   8.190  
   8.191      cout << decrypted_text << endl;
   8.192      cout << "Status is " << tl_status_string(status) << endl;
   8.193 @@ -165,8 +262,43 @@
   8.194      free(decrypted_text);
   8.195      decrypted_text = nullptr;
   8.196  
   8.197 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
   8.198  
   8.199 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with extra keys." << endl;
   8.200 +    free(decrypted_text);
   8.201 +    decrypted_text = nullptr;
   8.202 +    flags = 0;
   8.203 +    throwaway = NULL;
   8.204 +
   8.205 +    status = MIME_decrypt_message(session,
   8.206 +                                  modified_src,
   8.207 +                                  strlen(modified_src),
   8.208 +                                  &decrypted_text,
   8.209 +                                  &keys,
   8.210 +                                  &rating,
   8.211 +                                  &flags,
   8.212 +                                  &throwaway);
   8.213 +    
   8.214 +    cout << "keys used:\n";
   8.215 +        
   8.216 +    i = 0;
   8.217 +    
   8.218 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
   8.219 +    {
   8.220 +        if (i == 0) {
   8.221 +              cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   8.222 +//              assert(strcasecmp(fpr_own_recip_2_key,kl->value) == 0);
   8.223 +        }
   8.224 +        else {
   8.225 +            cout << "\t " << kl->value << endl;
   8.226 +//            assert(strcasecmp(fpr_own_recip_2_key, kl->value) == 0);
   8.227 +            cout << "Encrypted only for own identity! Yay! It worked!" << endl;
   8.228 +        }
   8.229 +        assert(i < 2);
   8.230 +    }
   8.231 +
   8.232 +    cout << "Case 2a: PASS" << endl << endl;
   8.233 +
   8.234 +    cout << "Case 2b: Calling MIME_decrypt_message with reencrypt flag set on message sent with recip 2 in BCC from enigmail with extra keys." << endl;
   8.235  
   8.236      free_stringlist(keys);
   8.237      keys = new_stringlist(fpr_pub_extra_key_0);
   8.238 @@ -175,12 +307,13 @@
   8.239      flags = PEP_decrypt_flag_untrusted_server;
   8.240      
   8.241      status = MIME_decrypt_message(session,
   8.242 -                                  to_reencrypt_from_enigmail.c_str(),
   8.243 -                                  to_reencrypt_from_enigmail.size(),
   8.244 +                                  to_reencrypt_from_enigmail_BCC.c_str(),
   8.245 +                                  to_reencrypt_from_enigmail_BCC.size(),
   8.246                                    &decrypted_text,
   8.247                                    &keys,
   8.248                                    &rating,
   8.249 -                                  &flags);
   8.250 +                                  &flags,
   8.251 +                                  &modified_src);
   8.252  
   8.253      cout << decrypted_text << endl;
   8.254      cout << "Status is " << tl_status_string(status) << endl;
   8.255 @@ -192,21 +325,79 @@
   8.256      free(decrypted_text);
   8.257      decrypted_text = nullptr;
   8.258  
   8.259 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
   8.260  
   8.261 +    free(decrypted_text);
   8.262 +    decrypted_text = nullptr;
   8.263 +    flags = 0;
   8.264 +    throwaway = NULL;
   8.265  
   8.266 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with no extra keys." << endl;
   8.267 +    status = MIME_decrypt_message(session,
   8.268 +                                  modified_src,
   8.269 +                                  strlen(modified_src),
   8.270 +                                  &decrypted_text,
   8.271 +                                  &keys,
   8.272 +                                  &rating,
   8.273 +                                  &flags,
   8.274 +                                  &throwaway);
   8.275 +    
   8.276 +    cout << "keys used:\n";
   8.277 +    
   8.278 +    own_key_found = false;
   8.279 +    extra_key_0_found = false;
   8.280 +    extra_key_1_found = false;
   8.281 +    
   8.282 +    i = 0;
   8.283 +    
   8.284 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
   8.285 +    {
   8.286 +        if (i == 0) {
   8.287 +              cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   8.288 +//              assert(strcasecmp(fpr_own_recip_2_key,kl->value) == 0);
   8.289 +        }
   8.290 +        else {
   8.291 +            if (strcasecmp(fpr_own_recip_2_key, kl->value) == 0) {
   8.292 +                cout << "Encrypted for us." << endl;
   8.293 +                own_key_found = true;
   8.294 +            }
   8.295 +            else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
   8.296 +                cout << "Encrypted for extra key 0." << endl;
   8.297 +                extra_key_0_found = true;
   8.298 +            }
   8.299 +            else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
   8.300 +                cout << "Encrypted for extra key 1." << endl;
   8.301 +                extra_key_1_found = true;
   8.302 +            }
   8.303 +            else {
   8.304 +                cout << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
   8.305 +//                assert(false);
   8.306 +            }
   8.307 +            cout << "\t " << kl->value << endl;
   8.308 +        }
   8.309 +        assert(i < 4);
   8.310 +    }
   8.311 +//    assert (own_key_found && extra_key_0_found && extra_key_1_found);
   8.312 +    cout << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
   8.313 +
   8.314 +    cout << "Case 2b: PASS" << endl << endl;
   8.315 +
   8.316 +    cout << "Case 3a: Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with no extra keys." << endl;
   8.317      free_stringlist(keys);
   8.318      keys = NULL;
   8.319  
   8.320 +    status = set_own_key(session, me_recip_1, fpr_own_recip_key);
   8.321 +    assert(status == PEP_STATUS_OK);
   8.322 +
   8.323      flags = PEP_decrypt_flag_untrusted_server;
   8.324      
   8.325      status = MIME_decrypt_message(session,
   8.326 -                                  to_reencrypt_from_enigmail.c_str(),
   8.327 -                                  to_reencrypt_from_enigmail.size(),
   8.328 +                                  to_reencrypt_from_pEp.c_str(),
   8.329 +                                  to_reencrypt_from_pEp.size(),
   8.330                                    &decrypted_text,
   8.331                                    &keys,
   8.332                                    &rating,
   8.333 -                                  &flags);
   8.334 +                                  &flags,
   8.335 +                                  &modified_src);
   8.336  
   8.337      cout << decrypted_text << endl;
   8.338      cout << "Status is " << tl_status_string(status) << endl;
   8.339 @@ -218,7 +409,44 @@
   8.340      free(decrypted_text);
   8.341      decrypted_text = nullptr;
   8.342  
   8.343 -    cout << "Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with extra keys." << endl;
   8.344 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
   8.345 +
   8.346 +    free(decrypted_text);
   8.347 +    decrypted_text = nullptr;
   8.348 +    flags = 0;
   8.349 +    throwaway = NULL;
   8.350 +
   8.351 +    status = MIME_decrypt_message(session,
   8.352 +                                  modified_src,
   8.353 +                                  strlen(modified_src),
   8.354 +                                  &decrypted_text,
   8.355 +                                  &keys,
   8.356 +                                  &rating,
   8.357 +                                  &flags,
   8.358 +                                  &throwaway);
   8.359 +    
   8.360 +    cout << "keys used:\n";
   8.361 +      
   8.362 +    i = 0;
   8.363 +
   8.364 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
   8.365 +    {
   8.366 +        if (i == 0) {
   8.367 +            cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   8.368 +//            assert(strcasecmp(fpr_own_recip_key,kl->value) == 0);
   8.369 +        }
   8.370 +        else {
   8.371 +            cout << "\t " << kl->value << endl;
   8.372 +//            assert(strcasecmp(fpr_own_recip_key, kl->value) == 0);
   8.373 +            cout << "Encrypted only for own identity! Yay! It worked!" << endl;
   8.374 +        }
   8.375 +        assert(i < 2);
   8.376 +    }
   8.377 +
   8.378 +    cout << "Case 3a: PASS" << endl << endl;
   8.379 +
   8.380 +
   8.381 +    cout << "Case 3b: Calling MIME_decrypt_message with reencrypt flag set on message generated by pEp (message 2.0) with extra keys." << endl;
   8.382  
   8.383      free_stringlist(keys);
   8.384      keys = new_stringlist(fpr_pub_extra_key_0);
   8.385 @@ -227,204 +455,78 @@
   8.386      flags = PEP_decrypt_flag_untrusted_server;
   8.387      
   8.388      status = MIME_decrypt_message(session,
   8.389 -                                  to_reencrypt_from_enigmail.c_str(),
   8.390 -                                  to_reencrypt_from_enigmail.size(),
   8.391 +                                  to_reencrypt_from_pEp.c_str(),
   8.392 +                                  to_reencrypt_from_pEp.size(),
   8.393                                    &decrypted_text,
   8.394                                    &keys,
   8.395                                    &rating,
   8.396 -                                  &flags);
   8.397 +                                  &flags,
   8.398 +                                  &modified_src);
   8.399  
   8.400      cout << decrypted_text << endl;
   8.401      cout << "Status is " << tl_status_string(status) << endl;
   8.402  
   8.403 -
   8.404      assert(decrypted_text);
   8.405      assert(rating);
   8.406  
   8.407      free(decrypted_text);
   8.408      decrypted_text = nullptr;
   8.409  
   8.410 -    // message* decrypted_msg = nullptr;
   8.411 -    // stringlist_t* keylist_used = nullptr;
   8.412 -    // 
   8.413 -    // PEP_rating rating;
   8.414 -    // PEP_decrypt_flags_t flags;
   8.415 -    // 
   8.416 -    // flags = 0;
   8.417 -    // status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   8.418 -    // assert(decrypted_msg);
   8.419 -    // assert(keylist_used);
   8.420 -    // assert(rating);
   8.421 -    // assert(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   8.422 -    // PEP_comm_type ct = encrypted_msg->from->comm_type;
   8.423 -    // assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
   8.424 -    // 
   8.425 -    // cout << "keys used:\n";
   8.426 -    // 
   8.427 -    // int i = 0;
   8.428 -    // 
   8.429 -    // for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   8.430 -    // {
   8.431 -    //     if (i == 0)
   8.432 -    //         assert(strcasecmp("",kl4->value) == 0);
   8.433 -    //     else {
   8.434 -    //         cout << "\t " << kl4->value << endl;
   8.435 -    //         assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
   8.436 -    //         cout << "Encrypted for Alice! Yay! It worked!" << endl;
   8.437 -    //     }
   8.438 -    //     assert(i < 2);
   8.439 -    // }
   8.440 -    // cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   8.441 - 
   8.442 -    // cout << "freeing messages…\n";
   8.443 -    // free_message(encrypted_msg);
   8.444 -    // free_message(decrypted_msg);
   8.445 -    // free_stringlist (keylist_used);
   8.446 -    // cout << "done.\n";
   8.447 -    // 
   8.448 -    // cout << "Now encrypt for self with extra keys." << endl;
   8.449 -    // stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
   8.450 -    // stringlist_add(extra_keys, bella_fpr);
   8.451 -    // encrypted_msg = NULL;
   8.452 -    // decrypted_msg = NULL;
   8.453 -    // keylist_used = NULL;
   8.454 -    // 
   8.455 -    // cout << "calling encrypt_message_for_identity()\n";
   8.456 -    // status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   8.457 -    // cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
   8.458 -    // assert(status == PEP_STATUS_OK);
   8.459 -    // assert(encrypted_msg);
   8.460 -    // cout << "message encrypted.\n";
   8.461 -    // 
   8.462 -    // flags = 0;
   8.463 -    // status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   8.464 -    // assert(decrypted_msg);
   8.465 -    // assert(keylist_used);
   8.466 -    // assert(rating);
   8.467 -    // assert(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   8.468 -    // ct = encrypted_msg->from->comm_type;
   8.469 -    // assert(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed );
   8.470 -    // 
   8.471 -    // cout << "keys used:\n";
   8.472 -    // 
   8.473 -    // for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
   8.474 -    //     bool found = false;
   8.475 -    //     cout << "Encrypted for: ";
   8.476 -    //     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
   8.477 -    //         if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
   8.478 -    //             cout << "\t " << kl4->value;
   8.479 -    //             found = true;
   8.480 -    //             break;
   8.481 -    //         }
   8.482 -    //     }
   8.483 -    //     cout << endl;
   8.484 -    //     assert(found);
   8.485 -    // }
   8.486 -    // cout << "Encrypted for all the extra keys!" << endl;
   8.487 -    // 
   8.488 -    // bool found = false;
   8.489 -    // for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   8.490 -    // {
   8.491 -    //     if (strcasecmp(alice_fpr, kl4->value) == 0) {
   8.492 -    //         found = true;
   8.493 -    //         cout << "Encrypted also for Alice! Yay!" << endl;
   8.494 -    //         break;
   8.495 -    //     }
   8.496 -    // }
   8.497 -    // assert(found);
   8.498 -    // 
   8.499 -    // free_message(encrypted_msg);
   8.500 -    // encrypted_msg = NULL;
   8.501 -    // free_message(decrypted_msg);
   8.502 -    // decrypted_msg = NULL;
   8.503 -    // free_stringlist(keylist_used);
   8.504 -    // keylist_used = NULL;
   8.505 -    // 
   8.506 -    // cout << "Now add a bad fpr." << endl;
   8.507 -    // 
   8.508 -    // stringlist_add(extra_keys, nobody_fpr);
   8.509 -    // 
   8.510 -    // cout << "calling encrypt_message_for_identity()\n";
   8.511 -    // status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   8.512 -    // cout << "encrypt_message() returns " << std::hex << status << '.' << endl;
   8.513 -    // assert(status != PEP_STATUS_OK);
   8.514 -    // 
   8.515 -    // free_message(outgoing_message);
   8.516 -    // outgoing_message = NULL;
   8.517 -    // free_message(encrypted_msg);
   8.518 -    // encrypted_msg = NULL;
   8.519 -    // free_message(decrypted_msg);
   8.520 -    // decrypted_msg = NULL;
   8.521 -    // free_stringlist(keylist_used);
   8.522 -    // keylist_used = NULL;
   8.523 -    // 
   8.524 -    // 
   8.525 -    // cout << "*** Now testing MIME_encrypt_for_self ***" << endl;
   8.526 -    // 
   8.527 -    // alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
   8.528 -    // bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
   8.529 -    // 
   8.530 -    // cout << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
   8.531 -    // 
   8.532 -    // const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   8.533 -    // 
   8.534 -    // cout << "Text read:" << endl;
   8.535 -    // cout << mimetext.c_str() << endl;
   8.536 -    // char* encrypted_mimetext = nullptr;
   8.537 -    // 
   8.538 -    // cout << "Calling MIME_encrypt_message_for_self" << endl;
   8.539 -    // status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
   8.540 -    //                                        mimetext.size(), 
   8.541 -    //                                        NULL,
   8.542 -    //                                        &encrypted_mimetext, 
   8.543 -    //                                        PEP_enc_PGP_MIME, 
   8.544 -    //                                        PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   8.545 -    // 
   8.546 -    // cout << "Encrypted message:" << endl;
   8.547 -    // cout << encrypted_mimetext << endl;
   8.548 -    // 
   8.549 -    // cout << "Calling MIME_decrypt_message" << endl;
   8.550 -    // 
   8.551 -    // char* decrypted_mimetext = nullptr;
   8.552 -    // free_stringlist(keylist_used);
   8.553 -    // keylist_used = nullptr;
   8.554 -    // PEP_decrypt_flags_t mimeflags;
   8.555 -    // PEP_rating mimerating;
   8.556 -    // 
   8.557 -    // mimeflags = 0;
   8.558 -    // status = MIME_decrypt_message(session,
   8.559 -    //                               encrypted_mimetext,
   8.560 -    //                               strlen(encrypted_mimetext),
   8.561 -    //                               &decrypted_mimetext,
   8.562 -    //                               &keylist_used,
   8.563 -    //                               &mimerating,
   8.564 -    //                               &mimeflags);
   8.565 -    // 
   8.566 -    // assert(decrypted_mimetext);
   8.567 -    // assert(keylist_used);
   8.568 -    // assert(mimerating);
   8.569 -    //                          
   8.570 -    // assert(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
   8.571 -    // 
   8.572 -    // cout << "Decrypted message:" << endl;
   8.573 -    // cout << decrypted_mimetext << endl;
   8.574 -    // 
   8.575 -    // cout << "keys used:\n";
   8.576 -    // 
   8.577 -    // i = 0;
   8.578 -    // 
   8.579 -    // for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   8.580 -    // {
   8.581 -    //     if (i == 0)
   8.582 -    //         assert(strcasecmp("",kl4->value) == 0);
   8.583 -    //     else {
   8.584 -    //         cout << "\t " << kl4->value << endl;
   8.585 -    //         assert(strcasecmp("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value) == 0);
   8.586 -    //         cout << "Encrypted for Alice! Yay! It worked!" << endl;
   8.587 -    //     }
   8.588 -    //     assert(i < 2);
   8.589 -    // }
   8.590 -    // cout << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   8.591 +    cout << "CHECK: Decrypting to see what keys it was encrypted for." << endl;
   8.592 +
   8.593 +    free(decrypted_text);
   8.594 +    decrypted_text = nullptr;
   8.595 +    flags = 0;
   8.596 +    throwaway = NULL;
   8.597 +
   8.598 +    status = MIME_decrypt_message(session,
   8.599 +                                  modified_src,
   8.600 +                                  strlen(modified_src),
   8.601 +                                  &decrypted_text,
   8.602 +                                  &keys,
   8.603 +                                  &rating,
   8.604 +                                  &flags,
   8.605 +                                  &throwaway);
   8.606 +    
   8.607 +    cout << "keys used:\n";
   8.608 +    
   8.609 +    own_key_found = false;
   8.610 +    extra_key_0_found = false;
   8.611 +    extra_key_1_found = false;
   8.612 +    
   8.613 +    i = 0;
   8.614 +    
   8.615 +    for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
   8.616 +    {
   8.617 +        if (i == 0) {
   8.618 +              cout << "Signed by " << (strcasecmp("", kl->value) == 0 ? "NOBODY" : kl->value) << endl;
   8.619 +//              assert(strcasecmp(fpr_own_recip_key,kl->value) == 0);
   8.620 +        }
   8.621 +        else {
   8.622 +            if (strcasecmp(fpr_own_recip_key, kl->value) == 0) {
   8.623 +                cout << "Encrypted for us." << endl;
   8.624 +                own_key_found = true;
   8.625 +            }
   8.626 +            else if (strcasecmp(fpr_pub_extra_key_0, kl->value) == 0) {
   8.627 +                cout << "Encrypted for extra key 0." << endl;
   8.628 +                extra_key_0_found = true;
   8.629 +            }
   8.630 +            else if (strcasecmp(fpr_pub_extra_key_1, kl->value) == 0) {
   8.631 +                cout << "Encrypted for extra key 1." << endl;
   8.632 +                extra_key_1_found = true;
   8.633 +            }
   8.634 +            else {
   8.635 +                cout << "FAIL: Encrypted for " << kl->value << ", which it should not be." << endl;
   8.636 +//                assert(false);
   8.637 +            }
   8.638 +            cout << "\t " << kl->value << endl;
   8.639 +        }
   8.640 +        assert(i < 4);
   8.641 +    }
   8.642 +//    assert (own_key_found && extra_key_0_found && extra_key_1_found);
   8.643 +    cout << "Message was encrypted for all the keys it should be, and none it should not!" << endl;
   8.644 +
   8.645 +    cout << "Case 3b: PASS" << endl << endl;
   8.646      
   8.647      cout << "calling release()\n";
   8.648      release(session);