Fixed up key reset message processing (previous test wasn't actually running the code, as I forgot to make the recips pep users in generation) ENGINE-398
authorKrista Bennett <krista@pep-project.org>
Tue, 11 Sep 2018 13:06:56 +0200
branchENGINE-398
changeset 29405c60780cf963
parent 2939 eb176e970094
child 2942 c929f7eba32a
Fixed up key reset message processing (previous test wasn't actually running the code, as I forgot to make the recips pep users in generation)
src/message_api.c
src/pEpEngine.h
test/include/KeyResetMessageTests.h
test/src/engine_tests/KeyResetMessageTests.cc
     1.1 --- a/src/message_api.c	Mon Sep 10 15:29:43 2018 +0200
     1.2 +++ b/src/message_api.c	Tue Sep 11 13:06:56 2018 +0200
     1.3 @@ -1550,13 +1550,13 @@
     1.4  PEP_STATUS create_standalone_key_reset_message(PEP_SESSION session,
     1.5                                                 message** dst, 
     1.6                                                 pEp_identity* recip,
     1.7 -                                               const char* revoke_fpr,
     1.8 +                                               const char* old_fpr,
     1.9                                                 const char* new_fpr) {
    1.10                                                     
    1.11      if (!dst || !recip->user_id || !recip->address)
    1.12          return PEP_ILLEGAL_VALUE;
    1.13  
    1.14 -    if (!revoke_fpr || !new_fpr)
    1.15 +    if (!old_fpr || !new_fpr)
    1.16          return PEP_ILLEGAL_VALUE;
    1.17          
    1.18      *dst = NULL;
    1.19 @@ -1576,23 +1576,16 @@
    1.20      const char* oldtag = "OLD: ";
    1.21      const char* newtag = "\nNEW: ";
    1.22      const size_t taglens = 11;
    1.23 -    size_t full_len = taglens + strlen(revoke_fpr) + strlen(new_fpr) + 2; // \n and \0
    1.24 +    size_t full_len = taglens + strlen(old_fpr) + strlen(new_fpr) + 2; // \n and \0
    1.25      char* longmsg = calloc(full_len, 1);
    1.26      strlcpy(longmsg, oldtag, full_len);
    1.27 -    strlcat(longmsg, revoke_fpr, full_len);
    1.28 +    strlcat(longmsg, old_fpr, full_len);
    1.29      strlcat(longmsg, newtag, full_len);
    1.30      strlcat(longmsg, new_fpr, full_len);
    1.31      strlcat(longmsg, "\n", full_len);
    1.32      reset_message->longmsg = longmsg; 
    1.33      reset_message->shortmsg = strdup("Key reset");    
    1.34      
    1.35 -    status = _attach_key(session, revoke_fpr, reset_message);
    1.36 -    if (status != PEP_STATUS_OK)
    1.37 -        goto pep_free;
    1.38 -    status = _attach_key(session, new_fpr, reset_message);
    1.39 -    if (status != PEP_STATUS_OK)
    1.40 -        goto pep_free;
    1.41 -    
    1.42      message* output_msg = NULL;
    1.43      
    1.44      status = encrypt_message(session, reset_message, NULL,
    1.45 @@ -1602,21 +1595,20 @@
    1.46      if (status == PEP_STATUS_OK)
    1.47          *dst = output_msg;
    1.48          
    1.49 -pep_free:
    1.50      free_message(reset_message);
    1.51      return status;
    1.52  }
    1.53  
    1.54  
    1.55  PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
    1.56 -                                     const char* revoke_fpr, 
    1.57 +                                     const char* old_fpr, 
    1.58                                       const char* new_fpr) {
    1.59 -    assert(revoke_fpr);
    1.60 +    assert(old_fpr);
    1.61      assert(new_fpr);
    1.62      assert(session);
    1.63      assert(session->messageToSend || session->sync_session->messageToSend);
    1.64      
    1.65 -    if (!session || !revoke_fpr || !new_fpr)
    1.66 +    if (!session || !old_fpr || !new_fpr)
    1.67          return PEP_ILLEGAL_VALUE;
    1.68  
    1.69      messageToSend_t send_cb = send_cb = session->messageToSend;
    1.70 @@ -1656,7 +1648,7 @@
    1.71              
    1.72          // Check if they've already been told - this shouldn't be the case, but...
    1.73          bool contacted = false;
    1.74 -        status = has_key_reset_been_sent(session, user_id, revoke_fpr, &contacted);
    1.75 +        status = has_key_reset_been_sent(session, user_id, old_fpr, &contacted);
    1.76          if (status != PEP_STATUS_OK)
    1.77              goto pep_free;
    1.78      
    1.79 @@ -1669,7 +1661,7 @@
    1.80          status = create_standalone_key_reset_message(session,
    1.81                                                       &reset_msg,
    1.82                                                       curr_id,
    1.83 -                                                     revoke_fpr,
    1.84 +                                                     old_fpr,
    1.85                                                       new_fpr);
    1.86  
    1.87          if (status != PEP_STATUS_OK) {
    1.88 @@ -1686,7 +1678,7 @@
    1.89          }
    1.90              
    1.91          // Put into notified DB
    1.92 -        status = set_reset_contact_notified(session, revoke_fpr, user_id);
    1.93 +        status = set_reset_contact_notified(session, old_fpr, user_id);
    1.94          if (status != PEP_STATUS_OK)
    1.95              goto pep_free;            
    1.96      }
    1.97 @@ -1823,8 +1815,8 @@
    1.98      identity_list * _il;
    1.99  
   1.100      if (enc_format != PEP_enc_none && (_il = src->bcc) && _il->ident)
   1.101 +    // BCC limited support:
   1.102      {
   1.103 -        // BCC limited support:
   1.104          //     - App splits mails with BCC in multiple mails.
   1.105          //     - Each email is encrypted separately
   1.106  
   1.107 @@ -3297,52 +3289,44 @@
   1.108  
   1.109      if (!session || !reset_msg)
   1.110          return PEP_ILLEGAL_VALUE;
   1.111 +
   1.112 +    pEp_identity* sender_id = reset_msg->from;
   1.113                  
   1.114 -    if (!reset_msg->from || !reset_msg->from->user_id)
   1.115 +    if (!sender_id)
   1.116          return PEP_MALFORMED_KEY_RESET_MSG;
   1.117          
   1.118 -    if (is_me(session, reset_msg->from)) // hrm...
   1.119 +    PEP_STATUS status = update_identity(session, sender_id);
   1.120 +    if (!sender_id->user_id)
   1.121 +        return PEP_UNKNOWN_ERROR;
   1.122 +        
   1.123 +    // FIXME: We'll have to change this for lists!!!    
   1.124 +    if (is_me(session, sender_id)) // hrm...
   1.125          return PEP_ILLEGAL_VALUE;
   1.126          
   1.127      if (!reset_msg->longmsg || strncmp(reset_msg->longmsg, "OLD: ", 5) != 0) 
   1.128          return PEP_MALFORMED_KEY_RESET_MSG;
   1.129  
   1.130 -    PEP_STATUS status = PEP_STATUS_OK;
   1.131 -    char* revoke_fpr = NULL;
   1.132 +    status = PEP_STATUS_OK;
   1.133 +    char* old_fpr = NULL;
   1.134      char* new_fpr = NULL;
   1.135      
   1.136      stringlist_t* keylist = NULL;
   1.137 -    pEp_identity* temp_ident = NULL;
   1.138 +    pEp_identity* temp_ident = identity_dup(sender_id);
   1.139 +    if (!temp_ident) {
   1.140 +        status = PEP_OUT_OF_MEMORY;
   1.141 +        goto pep_free;
   1.142 +    }        
   1.143              
   1.144      char* rest = NULL;
   1.145 -    char* p = strtok_r(reset_msg->longmsg, "\n", &rest);
   1.146 +    char* p = strtok_r(reset_msg->longmsg, "\r\n", &rest);
   1.147      if (!EMPTYSTR(p + 5))
   1.148 -        revoke_fpr = strdup(p + 5);
   1.149 +        old_fpr = strdup(p + 5);
   1.150      else {
   1.151          status = PEP_MALFORMED_KEY_RESET_MSG;
   1.152          goto pep_free;
   1.153      }
   1.154 -    
   1.155 -    // 1. See if this fpr is even associated with this user_id
   1.156 -    pEp_identity* sender_id = reset_msg->from;
   1.157 -    bool user_has_fpr = false;
   1.158 -
   1.159 -    temp_ident = identity_dup(sender_id);
   1.160 -    free(temp_ident->fpr);
   1.161 -    temp_ident->fpr = strdup(revoke_fpr);
   1.162 -    
   1.163 -    status = exists_trust_entry(session, temp_ident, &user_has_fpr);
   1.164 -    
   1.165 -    if (status != PEP_STATUS_OK)
   1.166 -        goto pep_free;
   1.167 -        
   1.168 -    if (!user_has_fpr) {   
   1.169 -        status = PEP_KEY_NOT_FOUND;
   1.170 -        goto pep_free;
   1.171 -    }
   1.172 -        
   1.173 -    // Ok, we can go on. This was a first check, in any event.    
   1.174 -    p = strtok_r(NULL, "\n", &rest); 
   1.175 +            
   1.176 +    p = strtok_r(NULL, "\r\n", &rest); 
   1.177      if (strncmp(p, "NEW: ", 5) != 0  || EMPTYSTR(p + 5)) {
   1.178          status = PEP_MALFORMED_KEY_RESET_MSG;
   1.179          goto pep_free;
   1.180 @@ -3350,27 +3334,8 @@
   1.181  
   1.182      new_fpr = strdup(p + 5);
   1.183          
   1.184 -    // We do NOT want to import private keys - we're trying to make sure nobody
   1.185 -    // tricks us into using one here.
   1.186 -    identity_list* private_il = NULL;
   1.187 -    
   1.188 -    bool imported_keys = import_attached_keys(session, reset_msg, &private_il);
   1.189 -
   1.190 -    if (!imported_keys) {
   1.191 -        status = PEP_KEY_NOT_RESET; // ??
   1.192 -        goto pep_free;
   1.193 -    }
   1.194 -        
   1.195 -    if (private_il) {
   1.196 -        // This is clearly not a real key reset message. We NEVER distribute
   1.197 -        // private keys this way, so indicate foul play and abort.
   1.198 -        free(private_il);
   1.199 -        status = PEP_MALFORMED_KEY_RESET_MSG;
   1.200 -        goto pep_free;
   1.201 -    }
   1.202 -
   1.203      // Reset the original key
   1.204 -    status = key_reset(session, revoke_fpr, temp_ident);
   1.205 +    status = key_reset(session, old_fpr, temp_ident);
   1.206      if (status != PEP_STATUS_OK)
   1.207          goto pep_free;
   1.208          
   1.209 @@ -3386,15 +3351,14 @@
   1.210      // alright, we've checked as best we can. Let's set that baby.
   1.211      sender_id->fpr = new_fpr;
   1.212      
   1.213 +    // This only sets as the default, does NOT TRUST IN ANY WAY
   1.214      sender_id->comm_type = sender_id->comm_type & (~PEP_ct_confirmed);
   1.215      status = set_identity(session, sender_id);
   1.216      
   1.217 -    if (status == PEP_STATUS_OK)
   1.218 -        status = PEP_KEY_RESET_SUCCESSFUL;
   1.219 -    
   1.220 +    sender_id->fpr = NULL; // ownership for free
   1.221  pep_free:    
   1.222      free_stringlist(keylist);    
   1.223 -    free(revoke_fpr);
   1.224 +    free(old_fpr);
   1.225      free(new_fpr);
   1.226      free_identity(temp_ident);
   1.227      return status;
     2.1 --- a/src/pEpEngine.h	Mon Sep 10 15:29:43 2018 +0200
     2.2 +++ b/src/pEpEngine.h	Tue Sep 11 13:06:56 2018 +0200
     2.3 @@ -52,9 +52,8 @@
     2.4      PEP_CANNOT_EXPORT_KEY                           = 0x0204,
     2.5      PEP_CANNOT_EDIT_KEY                             = 0x0205,
     2.6      PEP_KEY_UNSUITABLE                              = 0x0206,
     2.7 -    PEP_KEY_RESET_SUCCESSFUL                        = 0x0210,
     2.8 -    PEP_MALFORMED_KEY_RESET_MSG                     = 0x0211,
     2.9 -    PEP_KEY_NOT_RESET                               = 0x0212,
    2.10 +    PEP_MALFORMED_KEY_RESET_MSG                     = 0x0210,
    2.11 +    PEP_KEY_NOT_RESET                               = 0x0211,
    2.12      
    2.13      PEP_CANNOT_FIND_IDENTITY                        = 0x0301,
    2.14      PEP_CANNOT_SET_PERSON                           = 0x0381,
     3.1 --- a/test/include/KeyResetMessageTests.h	Mon Sep 10 15:29:43 2018 +0200
     3.2 +++ b/test/include/KeyResetMessageTests.h	Tue Sep 11 13:06:56 2018 +0200
     3.3 @@ -22,7 +22,7 @@
     3.4          static constexpr const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
     3.5          static constexpr const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
     3.6          
     3.7 -        static constexpr const char* alice_receive_reset_fpr = "E7281DAE65DDD847F186B6988819915AA104FB62";
     3.8 +        static constexpr const char* alice_receive_reset_fpr = "6A349E4F68801E39145CD4C5712616A385412538";
     3.9  
    3.10          static const string alice_user_id;
    3.11          static const string bob_user_id;    
    3.12 @@ -37,7 +37,8 @@
    3.13      private:
    3.14          void check_key_reset_message();        
    3.15          void check_reset_key_and_notify();
    3.16 -        void check_receive_revoked();
    3.17 +        void check_non_reset_receive_revoked();
    3.18 +        void check_reset_receive_revoked();
    3.19          void check_receive_key_reset_private();
    3.20          void check_receive_key_reset_wrong_signer();
    3.21          void check_receive_key_reset_unsigned();
     4.1 --- a/test/src/engine_tests/KeyResetMessageTests.cc	Mon Sep 10 15:29:43 2018 +0200
     4.2 +++ b/test/src/engine_tests/KeyResetMessageTests.cc	Tue Sep 11 13:06:56 2018 +0200
     4.3 @@ -32,8 +32,10 @@
     4.4                                                                        static_cast<Func>(&KeyResetMessageTests::check_key_reset_message)));
     4.5      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_key_and_notify"),
     4.6                                                                        static_cast<Func>(&KeyResetMessageTests::check_reset_key_and_notify)));
     4.7 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_revoked"),
     4.8 -                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_revoked)));
     4.9 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_non_reset_receive_revoked"),
    4.10 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_non_reset_receive_revoked)));
    4.11 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_receive_revoked"),
    4.12 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_reset_receive_revoked)));
    4.13      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_private"),
    4.14                                                                        static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_private)));
    4.15      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_wrong_signer"),
    4.16 @@ -140,12 +142,23 @@
    4.17      identity_list* send_idents = 
    4.18          new_identity_list(
    4.19              new_identity("pep.test.bob@pep-project.org", 
    4.20 -                         NULL, "BobId", "Bob's Burgers"));
    4.21 +                         NULL, bob_user_id.c_str(), "Bob's Burgers"));
    4.22                           
    4.23      identity_list_add(send_idents, new_identity("pep-test-carol@pep-project.org", NULL, NULL, NULL));    
    4.24      identity_list_add(send_idents, new_identity("pep-test-dave@pep-project.org", NULL, NULL, NULL)); 
    4.25      identity_list_add(send_idents, new_identity("pep-test-erin@pep-project.org", NULL, NULL, NULL)); 
    4.26      identity_list_add(send_idents, new_identity("pep.test.fenris@thisstilldoesntwork.lu", NULL, NULL, NULL)); 
    4.27 +
    4.28 +    identity_list* curr_ident;
    4.29 +    
    4.30 +    for (curr_ident = send_idents; curr_ident && curr_ident->ident; curr_ident = curr_ident->next) {
    4.31 +        status = update_identity(session, curr_ident->ident);
    4.32 +        if (strcmp(curr_ident->ident->user_id, bob_user_id.c_str()) == 0)
    4.33 +            continue;
    4.34 +        
    4.35 +        status = set_as_pep_user(session, curr_ident->ident);
    4.36 +        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    4.37 +    }
    4.38      
    4.39      cout << "Creating outgoing message to update DB" << endl;
    4.40      message* outgoing_msg = new_message(PEP_dir_outgoing);
    4.41 @@ -219,32 +232,17 @@
    4.42          hashmap[jt->first] = true;   
    4.43  
    4.44          // Uncomment to regenerate received message - remember to update
    4.45 -        // alice_receive_reset_fpr
    4.46 -        //
    4.47 -        // if (it == m_queue.begin()) {
    4.48 +        // alice_receive_reset_fpr        
    4.49 +        // if (strcmp(curr_sent_msg->to->ident->user_id, bob_user_id.c_str()) == 0) {
    4.50          //     char* bob_msg = NULL;
    4.51          //     mime_encode_message(curr_sent_msg, false, &bob_msg);
    4.52          //     cout << bob_msg;
    4.53          // }
    4.54 -            
    4.55 -        message* decrypted_msg = NULL;
    4.56 -        stringlist_t* keylist = NULL;
    4.57 -        PEP_rating rating;
    4.58 -        PEP_decrypt_flags_t flags;
    4.59 -        
    4.60 -        status = decrypt_message(session, curr_sent_msg, 
    4.61 -                                 &decrypted_msg, &keylist, 
    4.62 -                                 &rating, &flags);
    4.63 -                                 
    4.64 -        TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
    4.65 -        TEST_ASSERT(keylist);
    4.66 -        if (keylist) {
    4.67 -            TEST_ASSERT(keylist->value);
    4.68 -            if (keylist->value)
    4.69 -                TEST_ASSERT_MSG(strcmp(keylist->value, new_fpr.c_str()) == 0,
    4.70 -                                keylist->value);
    4.71 -        }
    4.72 -        free_message(curr_sent_msg); // DO NOT USE AFTER THIS
    4.73 +        // else if (strcmp(curr_sent_msg->to->ident->user_id, fenris_user_id.c_str()) == 0) {
    4.74 +        //     char* fenris_msg = NULL;
    4.75 +        //     mime_encode_message(curr_sent_msg, false, &fenris_msg);
    4.76 +        //     cout << fenris_msg;
    4.77 +        // }
    4.78      }
    4.79      
    4.80      // MESSAGE LIST NOW INVALID.
    4.81 @@ -259,7 +257,7 @@
    4.82      TEST_ASSERT(hashmap[fenris_user_id] == true);
    4.83  }
    4.84  
    4.85 -void KeyResetMessageTests::check_receive_revoked() {
    4.86 +void KeyResetMessageTests::check_non_reset_receive_revoked() {
    4.87      receive_setup();
    4.88      pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
    4.89                                              alice_user_id.c_str(), NULL);
    4.90 @@ -290,10 +288,63 @@
    4.91      
    4.92      keylist = NULL;
    4.93  
    4.94 -    free(keylist);
    4.95 +    free(keylist);    
    4.96 +}
    4.97 +
    4.98 +void KeyResetMessageTests::check_reset_receive_revoked() {
    4.99 +    PEP_STATUS status = set_up_ident_from_scratch(session,
   4.100 +                "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
   4.101 +                "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(), 
   4.102 +                "Fenris Leto Hawke", NULL, false
   4.103 +            );
   4.104 +    assert(status == PEP_STATUS_OK);
   4.105 +    status = set_up_ident_from_scratch(session,
   4.106 +                "test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc",
   4.107 +                "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(), 
   4.108 +                "Fenris Leto Hawke", NULL, false
   4.109 +            );
   4.110 +    assert(status == PEP_STATUS_OK);
   4.111      
   4.112 +    status = set_up_ident_from_scratch(session,
   4.113 +                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
   4.114 +                "pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), "Alice is tired of Bob",
   4.115 +                NULL, false
   4.116 +            );
   4.117 +    assert(status == PEP_STATUS_OK);    
   4.118 +    
   4.119 +    pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
   4.120 +                                            alice_user_id.c_str(), NULL);
   4.121 +                                            
   4.122 +    status = update_identity(session, alice_ident);
   4.123 +    TEST_ASSERT(status == PEP_STATUS_OK);
   4.124 +    TEST_ASSERT(strcmp(alice_fpr, alice_ident->fpr) == 0);
   4.125 +    
   4.126 +    string received_mail = slurp("test_files/398_reset_from_alice_to_fenris.eml");
   4.127 +    char* decrypted_msg = NULL;
   4.128 +    char* modified_src = NULL;
   4.129 +    stringlist_t* keylist = NULL;
   4.130 +    PEP_rating rating;
   4.131 +    PEP_decrypt_flags_t flags;
   4.132 +    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   4.133 +                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   4.134 +                                  
   4.135 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   4.136 +    TEST_ASSERT(keylist);
   4.137 +    if (keylist) // there's a test option to continue when asserts fail, so...
   4.138 +        TEST_ASSERT_MSG(strcmp(keylist->value, alice_receive_reset_fpr) == 0,
   4.139 +                        keylist->value);
   4.140 +    
   4.141 +    status = update_identity(session, alice_ident);
   4.142 +    TEST_ASSERT(alice_ident->fpr);
   4.143 +    TEST_ASSERT_MSG(strcmp(alice_receive_reset_fpr, alice_ident->fpr) == 0,
   4.144 +                    alice_ident->fpr);
   4.145 +    
   4.146 +    keylist = NULL;
   4.147 +
   4.148 +    free(keylist);    
   4.149  }
   4.150  
   4.151 +
   4.152  void KeyResetMessageTests::check_receive_key_reset_private() {
   4.153      TEST_ASSERT(true);
   4.154  }