merged in default import_key_2.1
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 02 Jun 2020 07:55:53 +0200
branchimport_key_2.1
changeset 4726c3a9837c15e6
parent 4718 a6c15bc68c47
parent 4725 93fc1da88f49
child 4728 feacfba6a106
merged in default
src/message_api.c
src/pEpEngine.h
test/src/EncryptForIdentityTest.cc
     1.1 --- a/.hgtags	Thu May 28 20:17:46 2020 +0200
     1.2 +++ b/.hgtags	Tue Jun 02 07:55:53 2020 +0200
     1.3 @@ -36,3 +36,4 @@
     1.4  c173428cd2c31fcc8867dc1ac785a6637c001210 Restarting default from postrelease
     1.5  17e8531e1103d93fd8237525c9ecdc7bc4e329e1 Release_2.0.2
     1.6  e236798013a6453b5162376fdacacd30c1ad15ea Release_2.1.0-RC0
     1.7 +b4d7c37ac8086c6c2b2ff2eb04cd6c203bdcd368 Release_2.1.0-RC1
     2.1 --- a/src/etpan_mime.c	Thu May 28 20:17:46 2020 +0200
     2.2 +++ b/src/etpan_mime.c	Tue Jun 02 07:55:53 2020 +0200
     2.3 @@ -566,10 +566,25 @@
     2.4      if (_name == NULL)
     2.5          goto enomem;
     2.6  
     2.7 -    _address = strdup(address);
     2.8 -    if (_address == NULL)
     2.9 -        goto enomem;
    2.10 -
    2.11 +    char* at = strstr(address, "@");
    2.12 +    if (!at) {
    2.13 +        // Presumed URI
    2.14 +        int added_char_len = 6; // " " @URI 
    2.15 +        int new_addr_len = strlen(address) + added_char_len + 1;
    2.16 +        _address = calloc(new_addr_len, 1);
    2.17 +        if (_address == NULL)
    2.18 +            goto enomem;
    2.19 +        
    2.20 +        _address[0] = '"';
    2.21 +        strlcat(_address, address, new_addr_len);
    2.22 +        strlcat(_address, "\"@URI", new_addr_len);
    2.23 +    }
    2.24 +    else {
    2.25 +        _address = strdup(address);
    2.26 +        if (_address == NULL)
    2.27 +            goto enomem;
    2.28 +    }
    2.29 +            
    2.30      mb = mailimf_mailbox_new(_name, _address);
    2.31      assert(mb);
    2.32      if (mb == NULL)
    2.33 @@ -2071,6 +2086,7 @@
    2.34  static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
    2.35  {
    2.36      char *username = NULL;
    2.37 +    char *address = NULL;
    2.38  
    2.39      assert(mb);
    2.40      assert(mb->mb_addr_spec);
    2.41 @@ -2086,14 +2102,30 @@
    2.42              goto enomem;
    2.43      }
    2.44  
    2.45 -    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
    2.46 +    const char* raw_addr = mb->mb_addr_spec;
    2.47 +    if (raw_addr && raw_addr[0] == '"') {
    2.48 +        int addr_len = strlen(raw_addr);
    2.49 +        if (addr_len >= 6) { // ""@URI
    2.50 +            const char* endcheck = strstr(raw_addr + 1, "\"@URI");
    2.51 +            if (endcheck && *(endcheck + 5) == '\0') {
    2.52 +                int actual_size = addr_len - 6;
    2.53 +                address = calloc(actual_size + 1, 1);
    2.54 +                if (!address)
    2.55 +                    goto enomem;
    2.56 +                strlcpy(address, raw_addr + 1, actual_size + 1);    
    2.57 +            }
    2.58 +        }
    2.59 +    }
    2.60 +
    2.61 +    pEp_identity *ident = new_identity(address ? address : raw_addr, NULL, NULL, username);
    2.62      if (ident == NULL)
    2.63          goto enomem;
    2.64      free(username);
    2.65 -
    2.66 +    free(address);
    2.67      return ident;
    2.68  
    2.69  enomem:
    2.70 +    free(address);
    2.71      free(username);
    2.72      return NULL;
    2.73  }
     3.1 --- a/src/message_api.c	Thu May 28 20:17:46 2020 +0200
     3.2 +++ b/src/message_api.c	Tue Jun 02 07:55:53 2020 +0200
     3.3 @@ -3305,8 +3305,11 @@
     3.4  
     3.5      PEP_STATUS status = PEP_STATUS_OK;
     3.6      
     3.7 -    if (strcasecmp(src->from->address, inner_message->from->address) == 0)
     3.8 +    if (src->from && inner_message->from && 
     3.9 +           src->from->address && inner_message->from->address && 
    3.10 +           strcasecmp(src->from->address, inner_message->from->address) == 0) {
    3.11          status = reconcile_identity(src->from, inner_message->from);
    3.12 +    }    
    3.13      
    3.14      if (status == PEP_STATUS_OK && inner_message->to)
    3.15          status = reconcile_identity_lists(src->to, inner_message->to);
     4.1 --- a/src/pEpEngine.h	Thu May 28 20:17:46 2020 +0200
     4.2 +++ b/src/pEpEngine.h	Tue Jun 02 07:55:53 2020 +0200
     4.3 @@ -27,7 +27,7 @@
     4.4  #define PEP_ENGINE_VERSION_MAJOR 2
     4.5  #define PEP_ENGINE_VERSION_MINOR 1
     4.6  #define PEP_ENGINE_VERSION_PATCH 0
     4.7 -#define PEP_ENGINE_VERSION_RC    1
     4.8 +#define PEP_ENGINE_VERSION_RC    2
     4.9  
    4.10  
    4.11  #define PEP_OWN_USERID "pEp_own_userId"
     5.1 --- a/test/src/EncryptForIdentityTest.cc	Thu May 28 20:17:46 2020 +0200
     5.2 +++ b/test/src/EncryptForIdentityTest.cc	Tue Jun 02 07:55:53 2020 +0200
     5.3 @@ -353,3 +353,268 @@
     5.4      }
     5.5      output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
     5.6  }
     5.7 +
     5.8 +TEST_F(EncryptForIdentityTest, check_encrypt_for_identity_with_URI) {
     5.9 +
    5.10 +    // message_api test code
    5.11 +
    5.12 +    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    5.13 +    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    5.14 +    const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
    5.15 +    const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
    5.16 +
    5.17 +    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    5.18 +    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    5.19 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    5.20 +    ASSERT_EQ(statuspriv, PEP_TEST_KEY_IMPORT_SUCCESS);
    5.21 +
    5.22 +    statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
    5.23 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    5.24 +    statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
    5.25 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    5.26 +
    5.27 +    const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    5.28 +    const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
    5.29 +    const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
    5.30 +    const char* nobody_fpr = "1111111111111111111111111111111111111111";
    5.31 +
    5.32 +    output_stream << "creating message…\n";
    5.33 +    pEp_identity* alice = new_identity("payto://BIC/SYSTEMA", alice_fpr, PEP_OWN_USERID, "Alice Test");
    5.34 +    pEp_identity* bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
    5.35 +
    5.36 +    alice->me = true;
    5.37 +
    5.38 +    PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
    5.39 +    ASSERT_EQ(mystatus, PEP_STATUS_OK);
    5.40 +
    5.41 +    identity_list* to_list = new_identity_list(bob); // to bob
    5.42 +    message* outgoing_message = new_message(PEP_dir_outgoing);
    5.43 +    ASSERT_NE(outgoing_message, nullptr);
    5.44 +    outgoing_message->from = alice;
    5.45 +    outgoing_message->to = to_list;
    5.46 +    outgoing_message->shortmsg = strdup("Greetings, humans!");
    5.47 +    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    5.48 +    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    5.49 +    output_stream << "message created.\n";
    5.50 +
    5.51 +    char* encoded_text = nullptr;
    5.52 +    PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text, false);
    5.53 +    ASSERT_EQ(status, PEP_STATUS_OK);
    5.54 +    ASSERT_NE(encoded_text, nullptr);
    5.55 +
    5.56 +    output_stream << "decrypted:\n\n";
    5.57 +    output_stream << encoded_text << "\n";
    5.58 +
    5.59 +    free(encoded_text);
    5.60 +
    5.61 +    message* encrypted_msg = nullptr;
    5.62 +    output_stream << "calling encrypt_message_for_identity()\n";
    5.63 +    status = encrypt_message_for_self(session, alice, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
    5.64 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
    5.65 +    ASSERT_EQ(status, PEP_STATUS_OK);
    5.66 +    ASSERT_NE(encrypted_msg, nullptr);
    5.67 +    output_stream << "message encrypted.\n";
    5.68 +
    5.69 +    status = mime_encode_message(encrypted_msg, false, &encoded_text, false);
    5.70 +    ASSERT_EQ(status, PEP_STATUS_OK);
    5.71 +    ASSERT_NE(encoded_text, nullptr);
    5.72 +
    5.73 +    output_stream << "encrypted:\n\n";
    5.74 +    output_stream << encoded_text << "\n";
    5.75 +
    5.76 +    message* decoded_msg = nullptr;
    5.77 +    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg, NULL);
    5.78 +    ASSERT_EQ(status, PEP_STATUS_OK);
    5.79 +    const string string3 = encoded_text;
    5.80 +
    5.81 +    unlink("tmp/msg_encrypt_for_self.asc");
    5.82 +    ofstream outFile3("tmp/msg_encrypt_for_self.asc");
    5.83 +    outFile3.write(string3.c_str(), string3.size());
    5.84 +    outFile3.close();
    5.85 +
    5.86 +    message* decrypted_msg = nullptr;
    5.87 +    stringlist_t* keylist_used = nullptr;
    5.88 +
    5.89 +    PEP_rating rating;
    5.90 +    PEP_decrypt_flags_t flags;
    5.91 +
    5.92 +    flags = 0;
    5.93 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
    5.94 +    ASSERT_NE(decrypted_msg, nullptr);
    5.95 +    ASSERT_NE(keylist_used, nullptr);
    5.96 +    ASSERT_NE(rating, 0);
    5.97 +    ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
    5.98 +    PEP_comm_type ct = encrypted_msg->from->comm_type;
    5.99 +    ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   5.100 +
   5.101 +    output_stream << "keys used:\n";
   5.102 +
   5.103 +    int i = 0;
   5.104 +
   5.105 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   5.106 +    {
   5.107 +        if (i == 0) {
   5.108 +            ASSERT_STRCASEEQ("",kl4->value);
   5.109 +        }
   5.110 +        else {
   5.111 +            output_stream << "\t " << kl4->value << endl;
   5.112 +            ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   5.113 +            output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   5.114 +        }
   5.115 +        ASSERT_LT(i , 2);
   5.116 +    }
   5.117 +    output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   5.118 +
   5.119 +    output_stream << "freeing messages…\n";
   5.120 +    free_message(encrypted_msg);
   5.121 +    free_message(decrypted_msg);
   5.122 +    free_stringlist (keylist_used);
   5.123 +    output_stream << "done.\n";
   5.124 +
   5.125 +    output_stream << "Now encrypt for self with extra keys." << endl;
   5.126 +    stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
   5.127 +    stringlist_add(extra_keys, bella_fpr);
   5.128 +    encrypted_msg = NULL;
   5.129 +    decrypted_msg = NULL;
   5.130 +    keylist_used = NULL;
   5.131 +
   5.132 +    output_stream << "calling encrypt_message_for_identity()\n";
   5.133 +    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);
   5.134 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   5.135 +    ASSERT_EQ(status, PEP_STATUS_OK);
   5.136 +    ASSERT_NE(encrypted_msg, nullptr);
   5.137 +    output_stream << "message encrypted.\n";
   5.138 +
   5.139 +    flags = 0;
   5.140 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   5.141 +    ASSERT_NE(decrypted_msg, nullptr);
   5.142 +    ASSERT_NE(keylist_used, nullptr);
   5.143 +    ASSERT_NE(rating, 0);
   5.144 +    ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   5.145 +    ct = encrypted_msg->from->comm_type;
   5.146 +    ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   5.147 +
   5.148 +    output_stream << "keys used:\n";
   5.149 +
   5.150 +    for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
   5.151 +        bool found = false;
   5.152 +        output_stream << "Encrypted for: ";
   5.153 +        for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
   5.154 +            if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
   5.155 +                output_stream << "\t " << kl4->value;
   5.156 +                found = true;
   5.157 +                break;
   5.158 +            }
   5.159 +        }
   5.160 +        output_stream << endl;
   5.161 +        ASSERT_TRUE(found);
   5.162 +    }
   5.163 +    output_stream << "Encrypted for all the extra keys!" << endl;
   5.164 +
   5.165 +    bool found = false;
   5.166 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   5.167 +    {
   5.168 +        if (strcasecmp(alice_fpr, kl4->value) == 0) {
   5.169 +            found = true;
   5.170 +            output_stream << "Encrypted also for Alice! Yay!" << endl;
   5.171 +            break;
   5.172 +        }
   5.173 +    }
   5.174 +    ASSERT_TRUE(found);
   5.175 +
   5.176 +    free_message(encrypted_msg);
   5.177 +    encrypted_msg = NULL;
   5.178 +    free_message(decrypted_msg);
   5.179 +    decrypted_msg = NULL;
   5.180 +    free_stringlist(keylist_used);
   5.181 +    keylist_used = NULL;
   5.182 +
   5.183 +    output_stream << "Now add a bad fpr." << endl;
   5.184 +
   5.185 +    stringlist_add(extra_keys, nobody_fpr);
   5.186 +
   5.187 +    output_stream << "calling encrypt_message_for_identity()\n";
   5.188 +    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);
   5.189 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   5.190 +    ASSERT_NE(status, PEP_STATUS_OK);
   5.191 +
   5.192 +    free_message(outgoing_message);
   5.193 +    outgoing_message = NULL;
   5.194 +    free_message(encrypted_msg);
   5.195 +    encrypted_msg = NULL;
   5.196 +    free_message(decrypted_msg);
   5.197 +    decrypted_msg = NULL;
   5.198 +    free_stringlist(keylist_used);
   5.199 +    keylist_used = NULL;
   5.200 +
   5.201 +
   5.202 +    output_stream << "*** Now testing MIME_encrypt_for_self ***" << endl;
   5.203 +
   5.204 +    alice = new_identity("payto://BIC/SYSTEMA", NULL, PEP_OWN_USERID, "Alice Test");
   5.205 +    bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
   5.206 +
   5.207 +    output_stream << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
   5.208 +
   5.209 +    const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   5.210 +
   5.211 +    output_stream << "Text read:" << endl;
   5.212 +    output_stream << mimetext.c_str() << endl;
   5.213 +    char* encrypted_mimetext = nullptr;
   5.214 +
   5.215 +    output_stream << "Calling MIME_encrypt_message_for_self" << endl;
   5.216 +    status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
   5.217 +                                           mimetext.size(),
   5.218 +                                           NULL,
   5.219 +                                           &encrypted_mimetext,
   5.220 +                                           PEP_enc_PGP_MIME,
   5.221 +                                           PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   5.222 +
   5.223 +    output_stream << "Encrypted message:" << endl;
   5.224 +    output_stream << encrypted_mimetext << endl;
   5.225 +
   5.226 +    output_stream << "Calling MIME_decrypt_message" << endl;
   5.227 +
   5.228 +    char* decrypted_mimetext = nullptr;
   5.229 +    free_stringlist(keylist_used);
   5.230 +    keylist_used = nullptr;
   5.231 +    PEP_decrypt_flags_t mimeflags;
   5.232 +    PEP_rating mimerating;
   5.233 +    char* modified_src = NULL;
   5.234 +
   5.235 +    mimeflags = 0;
   5.236 +    status = MIME_decrypt_message(session,
   5.237 +                                  encrypted_mimetext,
   5.238 +                                  strlen(encrypted_mimetext),
   5.239 +                                  &decrypted_mimetext,
   5.240 +                                  &keylist_used,
   5.241 +                                  &mimerating,
   5.242 +                                  &mimeflags,
   5.243 +				  &modified_src);
   5.244 +
   5.245 +    ASSERT_NE(decrypted_mimetext, nullptr);
   5.246 +    ASSERT_NE(keylist_used, nullptr);
   5.247 +    ASSERT_NE(mimerating, 0);
   5.248 +
   5.249 +    ASSERT_TRUE(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
   5.250 +
   5.251 +    output_stream << "Decrypted message:" << endl;
   5.252 +    output_stream << decrypted_mimetext << endl;
   5.253 +
   5.254 +    output_stream << "keys used:\n";
   5.255 +
   5.256 +    i = 0;
   5.257 +
   5.258 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   5.259 +    {
   5.260 +        if (i == 0) {
   5.261 +            ASSERT_STRCASEEQ("",kl4->value);
   5.262 +        }
   5.263 +        else {
   5.264 +            output_stream << "\t " << kl4->value << endl;
   5.265 +            ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   5.266 +            output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   5.267 +        }
   5.268 +        ASSERT_LT(i , 2);
   5.269 +    }
   5.270 +    output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   5.271 +}