Added a DB commit to key_mistrusted to be sure we are recording the mistrust with an identity ENGINE-654
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 16 Oct 2019 16:34:46 +0200
branchENGINE-654
changeset 411760d786f077a8
parent 4110 e064aa840c9c
child 4118 b0995cc07d02
Added a DB commit to key_mistrusted to be sure we are recording the mistrust with an identity
src/key_reset.c
src/keymanagement.c
test/src/KeyResetMessageTest.cc
     1.1 --- a/src/key_reset.c	Fri Oct 04 16:43:45 2019 +0200
     1.2 +++ b/src/key_reset.c	Wed Oct 16 16:34:46 2019 +0200
     1.3 @@ -451,7 +451,7 @@
     1.4                  // There's no identity default. Try resetting user default
     1.5                  status = get_user_default_key(session, tmp_ident->user_id, &fpr_copy);
     1.6              }            
     1.7 -            
     1.8 +                        
     1.9              if (!fpr_copy || status != PEP_STATUS_OK) // No default to free. We're done here.
    1.10                  goto pEp_free;            
    1.11          }
     2.1 --- a/src/keymanagement.c	Fri Oct 04 16:43:45 2019 +0200
     2.2 +++ b/src/keymanagement.c	Wed Oct 16 16:34:46 2019 +0200
     2.3 @@ -1343,6 +1343,29 @@
     2.4  
     2.5          if (!revoked)
     2.6              revoke_key(session, ident->fpr, NULL);
     2.7 +    }
     2.8 +    else {
     2.9 +        if (ident->fpr) {
    2.10 +            // Make sure there was a default in the DB for this identity;
    2.11 +            // if not, set one, even though we're going to mistrust this. Otherwise,
    2.12 +            // cannot reset.
    2.13 +            char* fpr_cache = ident->fpr;
    2.14 +            ident->fpr = NULL;
    2.15 +            update_identity(session, ident);
    2.16 +            if (!ident->fpr) {
    2.17 +                ident->fpr = fpr_cache;
    2.18 +                // set defaults
    2.19 +                status = set_identity(session, ident);
    2.20 +                if (status != PEP_STATUS_OK)
    2.21 +                    return status; // Will this make trouble?
    2.22 +            }
    2.23 +            else {
    2.24 +                // set it back...
    2.25 +                char* switch_fpr = ident->fpr;
    2.26 +                ident->fpr = fpr_cache;
    2.27 +                free(switch_fpr);
    2.28 +            }
    2.29 +        }
    2.30      }            
    2.31              
    2.32      // double-check to be sure key is even in the DB
     3.1 --- a/test/src/KeyResetMessageTest.cc	Fri Oct 04 16:43:45 2019 +0200
     3.2 +++ b/test/src/KeyResetMessageTest.cc	Wed Oct 16 16:34:46 2019 +0200
     3.3 @@ -6,6 +6,8 @@
     3.4  #include <vector>
     3.5  #include <unordered_map>
     3.6  #include <assert.h>
     3.7 +#include <iostream>
     3.8 +#include <fstream>
     3.9  
    3.10  #include "pEpEngine.h"
    3.11  #include "pEp_internal.h"
    3.12 @@ -1149,7 +1151,7 @@
    3.13      ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
    3.14  
    3.15      free_stringlist(keylist);
    3.16 -    keylist = NULL;
    3.17 +        keylist = NULL;
    3.18  
    3.19      alex_id->fpr = pubkey2;
    3.20      status = get_trust(session, alex_id);
    3.21 @@ -1179,3 +1181,99 @@
    3.22      free_identity(alex_id);
    3.23  
    3.24  }
    3.25 +
    3.26 +// TEST_F(KeyResetMessageTest, check_reset_mistrust_next_msg_have_mailed) {
    3.27 +// 
    3.28 +// }
    3.29 +
    3.30 +TEST_F(KeyResetMessageTest, not_a_test) {
    3.31 +    pEp_identity* bob = NULL;
    3.32 +    PEP_STATUS status = set_up_preset(session, BOB,
    3.33 +                                      true, true, true, true, true, &bob);
    3.34 +                                                                
    3.35 +    const char* carol_fpr = "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42";                                                                
    3.36 +    slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");    
    3.37 +    slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
    3.38 +                                      
    3.39 +    pEp_identity* carol = new_identity("pep-test-carol@pep-project.org", carol_fpr, carol_user_id.c_str(), "Christmas Carol");
    3.40 +    status = update_identity(session, carol);
    3.41 +    
    3.42 +    message* bob_msg = new_message(PEP_dir_outgoing);
    3.43 +    bob_msg->from = identity_dup(bob);
    3.44 +    bob_msg->to = new_identity_list(carol);
    3.45 +    bob_msg->shortmsg = strdup("Engine bugs suck\n");
    3.46 +    bob_msg->longmsg = strdup("Everything is the engine's fault.\n");
    3.47 +    
    3.48 +    char* enc_msg_str = NULL;
    3.49 +    message* enc_msg = NULL;
    3.50 +    
    3.51 +    status = encrypt_message(session, bob_msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    3.52 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.53 +    status = mime_encode_message(enc_msg, false, &enc_msg_str);
    3.54 +    
    3.55 +    ofstream myfile;
    3.56 +    myfile.open("test_mails/ENGINE-654_bob_mail.eml");
    3.57 +    myfile << enc_msg_str;
    3.58 +    myfile.close();      
    3.59 +}
    3.60 +
    3.61 +
    3.62 +TEST_F(KeyResetMessageTest, check_reset_mistrust_next_msg_have_not_mailed) {
    3.63 +    pEp_identity* carol = NULL;
    3.64 +    PEP_STATUS status = set_up_preset(session, CAROL,
    3.65 +                                      true, true, true, true, true, &carol);
    3.66 +                                      
    3.67 +    status = myself(session, carol);
    3.68 +    ASSERT_STREQ(carol->fpr, "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42");                                  
    3.69 +
    3.70 +    slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");                                      
    3.71 +    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", bob_fpr, NULL, "Bob's Burgers");
    3.72 +    status = update_identity(session, bob);
    3.73 +    
    3.74 +    cout << bob->fpr << endl;
    3.75 +    
    3.76 +    status = key_mistrusted(session, bob);
    3.77 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.78 +    status = update_identity(session, bob);
    3.79 +    ASSERT_EQ(bob->fpr, nullptr);
    3.80 +    
    3.81 +    string mail_from_bob = slurp("test_mails/ENGINE-654_bob_mail.eml");
    3.82 +    
    3.83 +    // Ok, so let's see if the thing is mistrusted
    3.84 +    message* bob_enc_msg = NULL;
    3.85 +    
    3.86 +    mime_decode_message(mail_from_bob.c_str(), mail_from_bob.size(), &bob_enc_msg);
    3.87 +    
    3.88 +    message* bob_dec_msg = NULL;
    3.89 +    stringlist_t* keylist = NULL;
    3.90 +    PEP_rating rating;
    3.91 +    PEP_decrypt_flags_t flags = 0;
    3.92 +    
    3.93 +    status = decrypt_message(session, bob_enc_msg, &bob_dec_msg, &keylist, &rating, &flags);
    3.94 +    ASSERT_EQ(status, PEP_STATUS_OK);
    3.95 +    ASSERT_EQ(rating, PEP_rating_mistrust);
    3.96 +    
    3.97 +    free_message(bob_enc_msg);
    3.98 +    free_message(bob_dec_msg);
    3.99 +    
   3.100 +    status = key_reset_identity(session, bob, NULL);
   3.101 +    ASSERT_EQ(status, PEP_STATUS_OK);
   3.102 +    status = identity_rating(session, bob, &rating);
   3.103 +    status = update_identity(session, bob);
   3.104 +    status = identity_rating(session, bob, &rating);
   3.105 +    ASSERT_EQ(rating, PEP_rating_have_no_key);
   3.106 +    //update_identity(session, bob);
   3.107 +            //    ASSERT_EQ(bob->fpr, nullptr);
   3.108 +
   3.109 +    mime_decode_message(mail_from_bob.c_str(), mail_from_bob.size(), &bob_enc_msg);
   3.110 +    
   3.111 +    bob_dec_msg = NULL;
   3.112 +    free_stringlist(keylist);
   3.113 +    keylist = NULL;
   3.114 +    flags = 0;
   3.115 +    
   3.116 +    status = decrypt_message(session, bob_enc_msg, &bob_dec_msg, &keylist, &rating, &flags);
   3.117 +    ASSERT_EQ(status, PEP_STATUS_OK);
   3.118 +    ASSERT_EQ(rating, PEP_rating_reliable);
   3.119 +    
   3.120 +}