ENGINE-755: now works and is tested, though if we want to do this right we should modify the rest of the test as well... !!!!!!!!REQUIRES LIBETPAN UPDATE!!!!!!!! sync Release_2.0.6
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 29 May 2020 14:39:48 +0200
branchsync
changeset 4721ebb62ba262dd
parent 4720 de3a945ae12b
child 4722 7ff4e9775af1
ENGINE-755: now works and is tested, though if we want to do this right we should modify the rest of the test as well... !!!!!!!!REQUIRES LIBETPAN UPDATE!!!!!!!!
src/etpan_mime.c
src/message_api.c
test/src/EncryptForIdentityTest.cc
     1.1 --- a/src/etpan_mime.c	Fri May 29 14:57:14 2020 +0200
     1.2 +++ b/src/etpan_mime.c	Fri May 29 14:39:48 2020 +0200
     1.3 @@ -569,10 +569,25 @@
     1.4      if (_name == NULL)
     1.5          goto enomem;
     1.6  
     1.7 -    _address = strdup(address);
     1.8 -    if (_address == NULL)
     1.9 -        goto enomem;
    1.10 -
    1.11 +    char* at = strstr(address, "@");
    1.12 +    if (!at) {
    1.13 +        // Presumed URI
    1.14 +        int added_char_len = 6; // " " @URI 
    1.15 +        int new_addr_len = strlen(address) + added_char_len + 1;
    1.16 +        _address = calloc(new_addr_len, 1);
    1.17 +        if (_address == NULL)
    1.18 +            goto enomem;
    1.19 +        
    1.20 +        _address[0] = '"';
    1.21 +        strlcat(_address, address, new_addr_len);
    1.22 +        strlcat(_address, "\"@URI", new_addr_len);
    1.23 +    }
    1.24 +    else {
    1.25 +        _address = strdup(address);
    1.26 +        if (_address == NULL)
    1.27 +            goto enomem;
    1.28 +    }
    1.29 +            
    1.30      mb = mailimf_mailbox_new(_name, _address);
    1.31      assert(mb);
    1.32      if (mb == NULL)
    1.33 @@ -1990,6 +2005,7 @@
    1.34  static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
    1.35  {
    1.36      char *username = NULL;
    1.37 +    char *address = NULL;
    1.38  
    1.39      assert(mb);
    1.40      assert(mb->mb_addr_spec);
    1.41 @@ -2005,14 +2021,30 @@
    1.42              goto enomem;
    1.43      }
    1.44  
    1.45 -    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
    1.46 +    const char* raw_addr = mb->mb_addr_spec;
    1.47 +    if (raw_addr && raw_addr[0] == '"') {
    1.48 +        int addr_len = strlen(raw_addr);
    1.49 +        if (addr_len >= 6) { // ""@URI
    1.50 +            const char* endcheck = strstr(raw_addr + 1, "\"@URI");
    1.51 +            if (endcheck && *(endcheck + 5) == '\0') {
    1.52 +                int actual_size = addr_len - 6;
    1.53 +                address = calloc(actual_size + 1, 1);
    1.54 +                if (!address)
    1.55 +                    goto enomem;
    1.56 +                strlcpy(address, raw_addr + 1, actual_size + 1);    
    1.57 +            }
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    pEp_identity *ident = new_identity(address ? address : raw_addr, NULL, NULL, username);
    1.62      if (ident == NULL)
    1.63          goto enomem;
    1.64      free(username);
    1.65 -
    1.66 +    free(address);
    1.67      return ident;
    1.68  
    1.69  enomem:
    1.70 +    free(address);
    1.71      free(username);
    1.72      return NULL;
    1.73  }
     2.1 --- a/src/message_api.c	Fri May 29 14:57:14 2020 +0200
     2.2 +++ b/src/message_api.c	Fri May 29 14:39:48 2020 +0200
     2.3 @@ -3203,8 +3203,11 @@
     2.4  
     2.5      PEP_STATUS status = PEP_STATUS_OK;
     2.6      
     2.7 -    if (strcasecmp(src->from->address, inner_message->from->address) == 0)
     2.8 +    if (src->from && inner_message->from && 
     2.9 +           src->from->address && inner_message->from->address && 
    2.10 +           strcasecmp(src->from->address, inner_message->from->address) == 0) {
    2.11          status = reconcile_identity(src->from, inner_message->from);
    2.12 +    }    
    2.13      
    2.14      if (status == PEP_STATUS_OK && inner_message->to)
    2.15          status = reconcile_identity_lists(src->to, inner_message->to);
     3.1 --- a/test/src/EncryptForIdentityTest.cc	Fri May 29 14:57:14 2020 +0200
     3.2 +++ b/test/src/EncryptForIdentityTest.cc	Fri May 29 14:39:48 2020 +0200
     3.3 @@ -353,3 +353,268 @@
     3.4      }
     3.5      output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
     3.6  }
     3.7 +
     3.8 +TEST_F(EncryptForIdentityTest, check_encrypt_for_identity_with_URI) {
     3.9 +
    3.10 +    // message_api test code
    3.11 +
    3.12 +    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    3.13 +    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    3.14 +    const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
    3.15 +    const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
    3.16 +
    3.17 +    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    3.18 +    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    3.19 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    3.20 +    ASSERT_EQ(statuspriv, PEP_TEST_KEY_IMPORT_SUCCESS);
    3.21 +
    3.22 +    statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
    3.23 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    3.24 +    statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
    3.25 +    ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
    3.26 +
    3.27 +    const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    3.28 +    const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
    3.29 +    const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
    3.30 +    const char* nobody_fpr = "1111111111111111111111111111111111111111";
    3.31 +
    3.32 +    output_stream << "creating message…\n";
    3.33 +    pEp_identity* alice = new_identity("payto://BIC/SYSTEMA", alice_fpr, PEP_OWN_USERID, "Alice Test");
    3.34 +    pEp_identity* bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
    3.35 +
    3.36 +    alice->me = true;
    3.37 +
    3.38 +    PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
    3.39 +    ASSERT_EQ(mystatus, PEP_STATUS_OK);
    3.40 +
    3.41 +    identity_list* to_list = new_identity_list(bob); // to bob
    3.42 +    message* outgoing_message = new_message(PEP_dir_outgoing);
    3.43 +    ASSERT_NE(outgoing_message, nullptr);
    3.44 +    outgoing_message->from = alice;
    3.45 +    outgoing_message->to = to_list;
    3.46 +    outgoing_message->shortmsg = strdup("Greetings, humans!");
    3.47 +    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    3.48 +    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    3.49 +    output_stream << "message created.\n";
    3.50 +
    3.51 +    char* encoded_text = nullptr;
    3.52 +    PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text);
    3.53 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.54 +    ASSERT_NE(encoded_text, nullptr);
    3.55 +
    3.56 +    output_stream << "decrypted:\n\n";
    3.57 +    output_stream << encoded_text << "\n";
    3.58 +
    3.59 +    free(encoded_text);
    3.60 +
    3.61 +    message* encrypted_msg = nullptr;
    3.62 +    output_stream << "calling encrypt_message_for_identity()\n";
    3.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);
    3.64 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
    3.65 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.66 +    ASSERT_NE(encrypted_msg, nullptr);
    3.67 +    output_stream << "message encrypted.\n";
    3.68 +
    3.69 +    status = mime_encode_message(encrypted_msg, false, &encoded_text);
    3.70 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.71 +    ASSERT_NE(encoded_text, nullptr);
    3.72 +
    3.73 +    output_stream << "encrypted:\n\n";
    3.74 +    output_stream << encoded_text << "\n";
    3.75 +
    3.76 +    message* decoded_msg = nullptr;
    3.77 +    status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg);
    3.78 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.79 +    const string string3 = encoded_text;
    3.80 +
    3.81 +    unlink("tmp/msg_encrypt_for_self.asc");
    3.82 +    ofstream outFile3("tmp/msg_encrypt_for_self.asc");
    3.83 +    outFile3.write(string3.c_str(), string3.size());
    3.84 +    outFile3.close();
    3.85 +
    3.86 +    message* decrypted_msg = nullptr;
    3.87 +    stringlist_t* keylist_used = nullptr;
    3.88 +
    3.89 +    PEP_rating rating;
    3.90 +    PEP_decrypt_flags_t flags;
    3.91 +
    3.92 +    flags = 0;
    3.93 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
    3.94 +    ASSERT_NE(decrypted_msg, nullptr);
    3.95 +    ASSERT_NE(keylist_used, nullptr);
    3.96 +    ASSERT_NE(rating, 0);
    3.97 +    ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
    3.98 +    PEP_comm_type ct = encrypted_msg->from->comm_type;
    3.99 +    ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   3.100 +
   3.101 +    output_stream << "keys used:\n";
   3.102 +
   3.103 +    int i = 0;
   3.104 +
   3.105 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   3.106 +    {
   3.107 +        if (i == 0) {
   3.108 +            ASSERT_STRCASEEQ("",kl4->value);
   3.109 +        }
   3.110 +        else {
   3.111 +            output_stream << "\t " << kl4->value << endl;
   3.112 +            ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   3.113 +            output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   3.114 +        }
   3.115 +        ASSERT_LT(i , 2);
   3.116 +    }
   3.117 +    output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   3.118 +
   3.119 +    output_stream << "freeing messages…\n";
   3.120 +    free_message(encrypted_msg);
   3.121 +    free_message(decrypted_msg);
   3.122 +    free_stringlist (keylist_used);
   3.123 +    output_stream << "done.\n";
   3.124 +
   3.125 +    output_stream << "Now encrypt for self with extra keys." << endl;
   3.126 +    stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
   3.127 +    stringlist_add(extra_keys, bella_fpr);
   3.128 +    encrypted_msg = NULL;
   3.129 +    decrypted_msg = NULL;
   3.130 +    keylist_used = NULL;
   3.131 +
   3.132 +    output_stream << "calling encrypt_message_for_identity()\n";
   3.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);
   3.134 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   3.135 +    ASSERT_EQ(status, PEP_STATUS_OK);
   3.136 +    ASSERT_NE(encrypted_msg, nullptr);
   3.137 +    output_stream << "message encrypted.\n";
   3.138 +
   3.139 +    flags = 0;
   3.140 +    status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   3.141 +    ASSERT_NE(decrypted_msg, nullptr);
   3.142 +    ASSERT_NE(keylist_used, nullptr);
   3.143 +    ASSERT_NE(rating, 0);
   3.144 +    ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   3.145 +    ct = encrypted_msg->from->comm_type;
   3.146 +    ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   3.147 +
   3.148 +    output_stream << "keys used:\n";
   3.149 +
   3.150 +    for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
   3.151 +        bool found = false;
   3.152 +        output_stream << "Encrypted for: ";
   3.153 +        for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
   3.154 +            if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
   3.155 +                output_stream << "\t " << kl4->value;
   3.156 +                found = true;
   3.157 +                break;
   3.158 +            }
   3.159 +        }
   3.160 +        output_stream << endl;
   3.161 +        ASSERT_TRUE(found);
   3.162 +    }
   3.163 +    output_stream << "Encrypted for all the extra keys!" << endl;
   3.164 +
   3.165 +    bool found = false;
   3.166 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   3.167 +    {
   3.168 +        if (strcasecmp(alice_fpr, kl4->value) == 0) {
   3.169 +            found = true;
   3.170 +            output_stream << "Encrypted also for Alice! Yay!" << endl;
   3.171 +            break;
   3.172 +        }
   3.173 +    }
   3.174 +    ASSERT_TRUE(found);
   3.175 +
   3.176 +    free_message(encrypted_msg);
   3.177 +    encrypted_msg = NULL;
   3.178 +    free_message(decrypted_msg);
   3.179 +    decrypted_msg = NULL;
   3.180 +    free_stringlist(keylist_used);
   3.181 +    keylist_used = NULL;
   3.182 +
   3.183 +    output_stream << "Now add a bad fpr." << endl;
   3.184 +
   3.185 +    stringlist_add(extra_keys, nobody_fpr);
   3.186 +
   3.187 +    output_stream << "calling encrypt_message_for_identity()\n";
   3.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);
   3.189 +    output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   3.190 +    ASSERT_NE(status, PEP_STATUS_OK);
   3.191 +
   3.192 +    free_message(outgoing_message);
   3.193 +    outgoing_message = NULL;
   3.194 +    free_message(encrypted_msg);
   3.195 +    encrypted_msg = NULL;
   3.196 +    free_message(decrypted_msg);
   3.197 +    decrypted_msg = NULL;
   3.198 +    free_stringlist(keylist_used);
   3.199 +    keylist_used = NULL;
   3.200 +
   3.201 +
   3.202 +    output_stream << "*** Now testing MIME_encrypt_for_self ***" << endl;
   3.203 +
   3.204 +    alice = new_identity("payto://BIC/SYSTEMA", NULL, PEP_OWN_USERID, "Alice Test");
   3.205 +    bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
   3.206 +
   3.207 +    output_stream << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
   3.208 +
   3.209 +    const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   3.210 +
   3.211 +    output_stream << "Text read:" << endl;
   3.212 +    output_stream << mimetext.c_str() << endl;
   3.213 +    char* encrypted_mimetext = nullptr;
   3.214 +
   3.215 +    output_stream << "Calling MIME_encrypt_message_for_self" << endl;
   3.216 +    status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
   3.217 +                                           mimetext.size(),
   3.218 +                                           NULL,
   3.219 +                                           &encrypted_mimetext,
   3.220 +                                           PEP_enc_PGP_MIME,
   3.221 +                                           PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   3.222 +
   3.223 +    output_stream << "Encrypted message:" << endl;
   3.224 +    output_stream << encrypted_mimetext << endl;
   3.225 +
   3.226 +    output_stream << "Calling MIME_decrypt_message" << endl;
   3.227 +
   3.228 +    char* decrypted_mimetext = nullptr;
   3.229 +    free_stringlist(keylist_used);
   3.230 +    keylist_used = nullptr;
   3.231 +    PEP_decrypt_flags_t mimeflags;
   3.232 +    PEP_rating mimerating;
   3.233 +    char* modified_src = NULL;
   3.234 +
   3.235 +    mimeflags = 0;
   3.236 +    status = MIME_decrypt_message(session,
   3.237 +                                  encrypted_mimetext,
   3.238 +                                  strlen(encrypted_mimetext),
   3.239 +                                  &decrypted_mimetext,
   3.240 +                                  &keylist_used,
   3.241 +                                  &mimerating,
   3.242 +                                  &mimeflags,
   3.243 +				  &modified_src);
   3.244 +
   3.245 +    ASSERT_NE(decrypted_mimetext, nullptr);
   3.246 +    ASSERT_NE(keylist_used, nullptr);
   3.247 +    ASSERT_NE(mimerating, 0);
   3.248 +
   3.249 +    ASSERT_TRUE(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
   3.250 +
   3.251 +    output_stream << "Decrypted message:" << endl;
   3.252 +    output_stream << decrypted_mimetext << endl;
   3.253 +
   3.254 +    output_stream << "keys used:\n";
   3.255 +
   3.256 +    i = 0;
   3.257 +
   3.258 +    for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   3.259 +    {
   3.260 +        if (i == 0) {
   3.261 +            ASSERT_STRCASEEQ("",kl4->value);
   3.262 +        }
   3.263 +        else {
   3.264 +            output_stream << "\t " << kl4->value << endl;
   3.265 +            ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   3.266 +            output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   3.267 +        }
   3.268 +        ASSERT_LT(i , 2);
   3.269 +    }
   3.270 +    output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   3.271 +}