ENGINE-420: fixed bug in key_reset_trust when key corresponds to an own identity (this case: public-key-only), plus test expansion and bug fixes on encrypt_message_and_add_private_key ENGINE-420
authorKrista Bennett <krista@pep-project.org>
Fri, 13 Apr 2018 12:04:38 +0200
branchENGINE-420
changeset 2595ca6702d7dcd7
parent 2594 1b45507f4696
child 2596 535f40a7121b
ENGINE-420: fixed bug in key_reset_trust when key corresponds to an own identity (this case: public-key-only), plus test expansion and bug fixes on encrypt_message_and_add_private_key
src/keymanagement.c
src/message_api.c
test/encrypt_attach_private_key_test.cc
     1.1 --- a/src/keymanagement.c	Fri Apr 13 00:45:38 2018 +0200
     1.2 +++ b/src/keymanagement.c	Fri Apr 13 12:04:38 2018 +0200
     1.3 @@ -1289,7 +1289,10 @@
     1.4      if (!tmp_ident)
     1.5          return PEP_OUT_OF_MEMORY;
     1.6      
     1.7 -    status = update_identity(session, tmp_ident);
     1.8 +    if (is_me(session, tmp_ident))
     1.9 +        status = myself(session, tmp_ident);
    1.10 +    else
    1.11 +        status = update_identity(session, tmp_ident);
    1.12      
    1.13      if (status != PEP_STATUS_OK)
    1.14          goto pep_free;
     2.1 --- a/src/message_api.c	Fri Apr 13 00:45:38 2018 +0200
     2.2 +++ b/src/message_api.c	Fri Apr 13 12:04:38 2018 +0200
     2.3 @@ -1825,7 +1825,7 @@
     2.4      assert(src);
     2.5      assert(dst);
     2.6      assert(to_fpr);
     2.7 -    
     2.8 +        
     2.9      if (!session || !src || !dst || !to_fpr)
    2.10          return PEP_ILLEGAL_VALUE;
    2.11          
    2.12 @@ -1844,6 +1844,8 @@
    2.13      if (!strcasecmp(src->from->address, src->to->ident->address) == 0)
    2.14          return PEP_ILLEGAL_VALUE;
    2.15      
    2.16 +    stringlist_t* keys = NULL;
    2.17 +
    2.18      char* own_id = NULL;
    2.19      char* default_id = NULL;
    2.20      
    2.21 @@ -1865,8 +1867,8 @@
    2.22      // Ok, we are at least marginally sure the initial stuff is ok.
    2.23          
    2.24      // Let's get our own, normal identity
    2.25 -    pEp_identity* own_identity = NULL;
    2.26 -    status = get_identity(session, src->to->ident->address, own_id, &own_identity);    
    2.27 +    pEp_identity* own_identity = identity_dup(src->from);
    2.28 +    status = myself(session, own_identity);
    2.29  
    2.30      if (status != PEP_STATUS_OK)
    2.31          goto pep_free;
    2.32 @@ -1877,8 +1879,11 @@
    2.33      
    2.34      status = get_trust(session, own_identity);
    2.35      
    2.36 -    if (status != PEP_STATUS_OK)
    2.37 +    if (status != PEP_STATUS_OK) {
    2.38 +        if (status == PEP_CANNOT_FIND_IDENTITY)
    2.39 +            status = PEP_ILLEGAL_VALUE;
    2.40          goto pep_free;
    2.41 +    }
    2.42          
    2.43      if ((own_identity->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed) {
    2.44          status = PEP_ILLEGAL_VALUE;
    2.45 @@ -1902,12 +1907,14 @@
    2.46      }
    2.47      
    2.48      // Ok, fine... let's encrypt yon blob
    2.49 -    stringlist_t* keys = new_stringlist(to_fpr);
    2.50 +    keys = new_stringlist(own_private_fpr);
    2.51      if (!keys) {
    2.52          status = PEP_OUT_OF_MEMORY;
    2.53          goto pep_free;
    2.54      }
    2.55      
    2.56 +    stringlist_add(keys, to_fpr);
    2.57 +    
    2.58      char* encrypted_key_text = NULL;
    2.59      size_t encrypted_key_size = 0;
    2.60      
    2.61 @@ -1926,7 +1933,14 @@
    2.62      // We will have to delete this before returning, as we allocated it.
    2.63      bloblist_t* created_bl = NULL;
    2.64      bloblist_t* created_predecessor = NULL;
    2.65 -    if (!src->attachments) {
    2.66 +    
    2.67 +    bloblist_t* old_head = NULL;
    2.68 +    
    2.69 +    if (!src->attachments || src->attachments->value == NULL) {
    2.70 +        if (src->attachments->value == NULL) {
    2.71 +            old_head = src->attachments;
    2.72 +            src->attachments = NULL;
    2.73 +        }
    2.74          src->attachments = new_bloblist(encrypted_key_text, encrypted_key_size,
    2.75                                          "application/octet-stream", 
    2.76                                          "file://pEpkey.asc.pgp");
    2.77 @@ -1934,15 +1948,14 @@
    2.78      } 
    2.79      else {
    2.80          bloblist_t* tmp = src->attachments;
    2.81 -        while (src->attachments->next) {
    2.82 -            tmp = src->attachments->next;
    2.83 +        while (tmp && tmp->next) {
    2.84 +            tmp = tmp->next;
    2.85          }
    2.86 -        created_bl = bloblist_add(src->attachments, 
    2.87 +        created_predecessor = tmp;                                    
    2.88 +        created_bl = bloblist_add(tmp, 
    2.89                                    encrypted_key_text, encrypted_key_size,
    2.90                                    "application/octet-stream", 
    2.91                                     "file://pEpkey.asc.pgp");
    2.92 -                                
    2.93 -        created_predecessor = tmp;                                    
    2.94      }
    2.95      
    2.96      if (!created_bl) {
    2.97 @@ -1957,13 +1970,16 @@
    2.98      free_bloblist(created_bl);
    2.99      if (created_predecessor)
   2.100          created_predecessor->next = NULL;
   2.101 -    else
   2.102 -        src->attachments = NULL;    
   2.103 +    else {
   2.104 +        if (old_head)
   2.105 +            src->attachments = old_head;
   2.106 +        else
   2.107 +            src->attachments = NULL;    
   2.108 +    }
   2.109      
   2.110  pep_free:
   2.111      free(own_id);
   2.112      free(default_id);
   2.113 -    free(priv_key_data);
   2.114      free(own_private_fpr);
   2.115      free_identity(own_identity);
   2.116      free_stringlist(keys);
     3.1 --- a/test/encrypt_attach_private_key_test.cc	Fri Apr 13 00:45:38 2018 +0200
     3.2 +++ b/test/encrypt_attach_private_key_test.cc	Fri Apr 13 12:04:38 2018 +0200
     3.3 @@ -29,6 +29,8 @@
     3.4      const char* diff_uid_0 = "TASTY_TEST_UID_0";
     3.5      const char* diff_uid_1 = "TASTY_TEST_UID_1";
     3.6  
     3.7 +    cout << "Importing keys..." << endl;
     3.8 +    
     3.9      string input_key;
    3.10      const char* main_addr = "priv-key-import-test-main@darthmama.cool";
    3.11      pEp_identity* main_me = NULL;
    3.12 @@ -81,29 +83,49 @@
    3.13      input_key = slurp("test_keys/pub/priv-key-import-test-other_1-0xC785F20A_pub.asc");
    3.14      status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    3.15      assert(status == PEP_STATUS_OK);
    3.16 +    cout << "Done!" << endl << endl;
    3.17      
    3.18 +    cout << "Setting up own identity with default key " << fpr_main_me << endl;
    3.19      // Own identity with default key etc
    3.20      main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    3.21      status = set_own_key(session, main_me, fpr_main_me);
    3.22      assert(status == PEP_STATUS_OK);
    3.23  
    3.24      assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    3.25 +    cout << "Done!" << endl << endl;
    3.26      
    3.27 +    cout << "Setting up recipient identities and resetting key trust." << endl;
    3.28 +    cout << "#1: same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    3.29      // Identity with same address and user_id - the fpr here will be ignored in update_identity and friends.
    3.30      same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    3.31 -    // Might be problematic with myself()?
    3.32 +    status = key_reset_trust(session, same_addr_same_uid);
    3.33 +    assert(status == PEP_STATUS_OK);
    3.34 +    assert(strcmp(same_addr_same_uid->fpr, fpr_same_addr_same_uid) == 0);
    3.35      
    3.36      // Identity with same address and different user_id
    3.37 +    cout << "#2: same address, different user_id - address: " << main_addr << ", user_id: " << diff_uid_0 << ", fpr: " << fpr_same_addr_diff_uid << endl;  
    3.38      same_addr_diff_uid = new_identity(main_addr, fpr_same_addr_diff_uid, diff_uid_0, "PrivateKey Import Test");
    3.39      assert(same_addr_diff_uid);
    3.40 +    status = key_reset_trust(session, same_addr_diff_uid);
    3.41 +    assert(status == PEP_STATUS_OK);
    3.42 +    assert(strcmp(same_addr_diff_uid->fpr, fpr_same_addr_diff_uid) == 0);
    3.43      
    3.44      // Identity with diff address and same user_id
    3.45 +    cout << "#3: different address, same user_id - address: " << diff_addr_0 << ", user_id: " << own_uid << ", fpr: " << fpr_diff_addr_same_uid << endl;      
    3.46      diff_addr_same_uid = new_identity(diff_addr_0, fpr_diff_addr_same_uid, own_uid, "PrivateKey Import Test");
    3.47      assert(diff_addr_same_uid);
    3.48 +    status = key_reset_trust(session, diff_addr_same_uid);
    3.49 +    assert(status == PEP_STATUS_OK);
    3.50 +    assert(strcmp(diff_addr_same_uid->fpr, fpr_diff_addr_same_uid) == 0);
    3.51  
    3.52      // Identity with different address and different user_id
    3.53 +    cout << "#4: different address, different user_id - address: " << diff_addr_1 << ", user_id: " << diff_uid_1 << ", fpr: " << fpr_diff_addr_diff_uid << endl;      
    3.54      diff_addr_diff_uid = new_identity(diff_addr_1, fpr_diff_addr_diff_uid, diff_uid_1, "PrivateKey Import Test");
    3.55      assert(diff_addr_diff_uid);
    3.56 +    status = key_reset_trust(session, diff_addr_diff_uid);
    3.57 +    assert(status == PEP_STATUS_OK);
    3.58 +    assert(strcmp(diff_addr_diff_uid->fpr, fpr_diff_addr_diff_uid) == 0);
    3.59 +    cout << "Done!" << endl << endl;
    3.60  
    3.61      message* msg_same_addr_same_uid = new_message(PEP_dir_outgoing);
    3.62      msg_same_addr_same_uid->from = main_me;
    3.63 @@ -115,10 +137,13 @@
    3.64      message* msg_diff_addr_same_uid = message_dup(msg_same_addr_same_uid);       
    3.65      message* msg_diff_addr_diff_uid = message_dup(msg_same_addr_same_uid);       
    3.66  
    3.67 +    cout << "Starting tests..." << endl;
    3.68      // Case 1:
    3.69      // Same address, same user_id, untrusted
    3.70 +    cout << "Case 1: Same address, same user_id, untrusted" << endl;
    3.71      assert(msg_same_addr_same_uid);        
    3.72      identity_list* to_list = new_identity_list(same_addr_same_uid);
    3.73 +    msg_same_addr_same_uid->to = to_list;
    3.74      message* enc_same_addr_same_uid_untrusted = NULL;
    3.75      status = encrypt_message_and_add_priv_key(session,
    3.76                                                msg_same_addr_same_uid,
    3.77 @@ -127,8 +152,13 @@
    3.78                                                PEP_enc_PGP_MIME,
    3.79                                                0);
    3.80  
    3.81 +    cout << "Case 1 Status: " << tl_status_string(status) << endl;
    3.82 +    assert(status == PEP_ILLEGAL_VALUE);
    3.83 +    cout << "PASS!" << endl;
    3.84 +    
    3.85      // Case 2:
    3.86      // Same address, same_user_id, trusted
    3.87 +    cout << "Case 2: Same address, same user_id, trusted" << endl;
    3.88      status = trust_personal_key(session, same_addr_same_uid);
    3.89      assert(status == PEP_STATUS_OK);
    3.90      message* enc_same_addr_same_uid_trusted = NULL;
    3.91 @@ -139,10 +169,16 @@
    3.92                                                PEP_enc_PGP_MIME,
    3.93                                                0);
    3.94  
    3.95 +    cout << "Case 2 Status: " << tl_status_string(status) << endl;
    3.96 +    assert(status == PEP_STATUS_OK);
    3.97 +    cout << "PASS!" << endl;
    3.98 +
    3.99      // Case 3:
   3.100      // Different address, same user_id, untrusted
   3.101 +    cout << "Case 3: Different address, same user_id, untrusted" << endl;
   3.102      assert(msg_diff_addr_same_uid);        
   3.103      identity_list* to_list_1 = new_identity_list(diff_addr_same_uid);
   3.104 +    msg_diff_addr_same_uid->to = to_list_1;
   3.105      message* enc_diff_addr_same_uid_untrusted = NULL;
   3.106      status = encrypt_message_and_add_priv_key(session,
   3.107                                                msg_diff_addr_same_uid,
   3.108 @@ -150,9 +186,14 @@
   3.109                                                fpr_diff_addr_same_uid,
   3.110                                                PEP_enc_PGP_MIME,
   3.111                                                0);
   3.112 +    
   3.113 +    cout << "Case 3 Status: " << tl_status_string(status) << endl;
   3.114 +    assert(status == PEP_ILLEGAL_VALUE);
   3.115 +    cout << "PASS!" << endl;
   3.116  
   3.117      // Case 4:
   3.118      // Different address, same user_id, trusted
   3.119 +    cout << "Case 4: Different address, same user_id, trusted" << endl;
   3.120      status = trust_personal_key(session, diff_addr_same_uid);
   3.121      assert(status == PEP_STATUS_OK);
   3.122      message* enc_diff_addr_same_uid_trusted = NULL;
   3.123 @@ -162,11 +203,17 @@
   3.124                                                fpr_diff_addr_same_uid,
   3.125                                                PEP_enc_PGP_MIME,
   3.126                                                0);
   3.127 +                                              
   3.128 +    cout << "Case 4 Status: " << tl_status_string(status) << endl;
   3.129 +    assert(status == PEP_ILLEGAL_VALUE);
   3.130 +    cout << "PASS!" << endl;
   3.131  
   3.132      // Case 5:
   3.133      // Same address, different user_id, untrusted
   3.134 +    cout << "Case 5: Same address, different user_id, untrusted" << endl;    
   3.135      assert(msg_same_addr_diff_uid);        
   3.136      identity_list* to_list_2 = new_identity_list(same_addr_diff_uid);
   3.137 +    msg_same_addr_diff_uid->to = to_list_2;
   3.138      message* enc_same_addr_diff_uid_untrusted = NULL;
   3.139      status = encrypt_message_and_add_priv_key(session,
   3.140                                                msg_same_addr_diff_uid,
   3.141 @@ -175,8 +222,13 @@
   3.142                                                PEP_enc_PGP_MIME,
   3.143                                                0);
   3.144  
   3.145 +    cout << "Case 5 Status: " << tl_status_string(status) << endl;
   3.146 +    assert(status == PEP_ILLEGAL_VALUE);    
   3.147 +    cout << "PASS!" << endl;
   3.148 +    
   3.149      // Case 6:
   3.150      // Same address, different user_id, trusted
   3.151 +    cout << "Case 6: Same address, different user_id, trusted" << endl;        
   3.152      status = trust_personal_key(session, same_addr_diff_uid);
   3.153      assert(status == PEP_STATUS_OK);
   3.154      message* enc_same_addr_diff_uid_trusted = NULL;
   3.155 @@ -187,10 +239,16 @@
   3.156                                                PEP_enc_PGP_MIME,
   3.157                                                0);
   3.158  
   3.159 +    cout << "Case 6 Status: " << tl_status_string(status) << endl;
   3.160 +    assert(status == PEP_ILLEGAL_VALUE);    
   3.161 +    cout << "PASS!" << endl;
   3.162 +
   3.163      // Case 7:
   3.164      // Different address, different user_id, untrusted
   3.165 +    cout << "Case 7: Different address, different user_id, untrusted" << endl;    
   3.166      assert(msg_diff_addr_diff_uid);        
   3.167      identity_list* to_list_3 = new_identity_list(diff_addr_diff_uid);
   3.168 +    msg_diff_addr_diff_uid->to = to_list_3;
   3.169      message* enc_diff_addr_diff_uid_untrusted = NULL;
   3.170      status = encrypt_message_and_add_priv_key(session,
   3.171                                                msg_diff_addr_diff_uid,
   3.172 @@ -199,8 +257,13 @@
   3.173                                                PEP_enc_PGP_MIME,
   3.174                                                0);
   3.175  
   3.176 +    cout << "Case 7 Status: " << tl_status_string(status) << endl;
   3.177 +    assert(status == PEP_ILLEGAL_VALUE);
   3.178 +    cout << "PASS!" << endl;
   3.179 +
   3.180      // Case 8:
   3.181      // Different address, different user_id, trusted
   3.182 +    cout << "Case 8: Different address, different user_id, trusted" << endl;    
   3.183      status = trust_personal_key(session, diff_addr_diff_uid);
   3.184      assert(status == PEP_STATUS_OK);
   3.185      message* enc_diff_addr_diff_uid_trusted = NULL;
   3.186 @@ -211,6 +274,10 @@
   3.187                                                PEP_enc_PGP_MIME,
   3.188                                                0);
   3.189  
   3.190 +    cout << "Case 8 Status: " << tl_status_string(status) << endl;
   3.191 +    assert(status == PEP_ILLEGAL_VALUE);
   3.192 +    cout << "PASS!" << endl;
   3.193 +                                          
   3.194      release(session);
   3.195      
   3.196      return 0;