test/src/engine_tests/DecryptAttachPrivateKeyUntrustedTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 2669 1b103e8a95f7
child 3276 c0b3430f1f1d
permissions -rw-r--r--
ENGINE-448: made key removal much less aggressive. NetPGP will still be a problem, but versions built against gpg will now only remove actual keys
krista@2600
     1
// This file is under GNU General Public License 3.0
krista@2600
     2
// see LICENSE.txt
krista@2600
     3
krista@2600
     4
#include <stdlib.h>
krista@2654
     5
#include <string>
krista@2654
     6
#include <cstring>
krista@2654
     7
#include <cpptest.h>
krista@2654
     8
krista@2600
     9
#include "mime.h"
krista@2600
    10
#include "message_api.h"
krista@2600
    11
#include "keymanagement.h"
krista@2600
    12
#include "test_util.h"
krista@2600
    13
krista@2654
    14
#include "pEpEngine.h"
krista@2654
    15
krista@2654
    16
#include "EngineTestSessionSuite.h"
krista@2654
    17
#include "DecryptAttachPrivateKeyUntrustedTests.h"
krista@2654
    18
krista@2600
    19
using namespace std;
krista@2600
    20
krista@2654
    21
DecryptAttachPrivateKeyUntrustedTests::DecryptAttachPrivateKeyUntrustedTests(string suitename, string test_home_dir) :
krista@2654
    22
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
krista@2654
    23
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DecryptAttachPrivateKeyUntrustedTests::check_decrypt_attach_private_key_untrusted"),
krista@2654
    24
                                                                      static_cast<Func>(&DecryptAttachPrivateKeyUntrustedTests::check_decrypt_attach_private_key_untrusted)));
krista@2654
    25
}
krista@2600
    26
krista@2654
    27
void DecryptAttachPrivateKeyUntrustedTests::check_decrypt_attach_private_key_untrusted() {
krista@2600
    28
krista@2600
    29
    const char* own_uid = PEP_OWN_USERID;
krista@2600
    30
krista@2600
    31
    cout << "Importing keys..." << endl;
krista@2600
    32
    
krista@2600
    33
    string input_key;
krista@2600
    34
    const char* main_addr = "priv-key-import-test-main@darthmama.cool";
krista@2600
    35
    pEp_identity* main_me = NULL;
krista@2600
    36
    const char* fpr_main_me = "13A9F97964A2B52520CAA40E51BCA783C065A213";    
krista@2600
    37
    pEp_identity* same_addr_same_uid = NULL;
krista@2600
    38
    const char* fpr_same_addr_same_uid = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
krista@2600
    39
        
krista@2600
    40
    PEP_STATUS status = PEP_STATUS_OK;
krista@2600
    41
krista@2600
    42
    // key for main own user
krista@2600
    43
    // 
krista@2600
    44
    // 13A9F97964A2B52520CAA40E51BCA783C065A213    
krista@2600
    45
    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0xC065A213_pub.asc");
krista@2600
    46
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    47
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), "status == PEP_STATUS_OK");
krista@2600
    48
krista@2600
    49
    input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0xC065A213_priv.asc");
krista@2600
    50
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    51
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), "status == PEP_STATUS_OK");
krista@2600
    52
krista@2603
    53
    // ensure there's no private key - doesn't work in automated tests, sadly. Uncommon when running script manually.
krista@2600
    54
    bool has_priv = false;
krista@2603
    55
    // status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
krista@2603
    56
    // if (status == PEP_STATUS_OK && has_priv) {
krista@2603
    57
    //     cout << "SORRY, have to delete keys here to run test correctly..." << endl;
krista@2603
    58
    //     status = delete_keypair(session, fpr_same_addr_same_uid);
krista@2603
    59
    //     if (status == PEP_STATUS_OK)
krista@2603
    60
    //         cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
krista@2603
    61
    // }
krista@2600
    62
        
krista@2600
    63
    // key with same address and user_id
krista@2600
    64
    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
krista@2600
    65
    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
krista@2600
    66
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    67
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), "status == PEP_STATUS_OK");
krista@2600
    68
krista@2600
    69
    
krista@2600
    70
    cout << "Setting up own identity with default key " << fpr_main_me << endl;
krista@2600
    71
    // Own identity with default key etc
krista@2600
    72
    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
krista@2600
    73
    status = set_own_key(session, main_me, fpr_main_me);
krista@2669
    74
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2600
    75
krista@2669
    76
    TEST_ASSERT_MSG((strcmp(main_me->fpr, fpr_main_me) == 0), "strcmp(main_me->fpr, fpr_main_me) == 0");
krista@2600
    77
    cout << "Done!" << endl << endl;
krista@2600
    78
    
krista@2600
    79
    cout << "Setting up sender identities and resetting key trust." << endl;
krista@2601
    80
    cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
krista@2600
    81
    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
krista@2669
    82
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), "status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY");
krista@2669
    83
    TEST_ASSERT_MSG(((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed), "(same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed");
krista@2600
    84
krista@2600
    85
    status = key_reset_trust(session, same_addr_same_uid);
krista@2600
    86
    
krista@2600
    87
    cout << "Done!" << endl << endl;
krista@2600
    88
krista@2600
    89
    cout << "Reading in message..." << endl;
krista@2600
    90
    
krista@2600
    91
    string encoded_text = slurp("test_mails/priv_key_attach.eml");
krista@2600
    92
krista@2600
    93
    cout << "Starting tests..." << endl;
krista@2600
    94
    // Case 1:
krista@2600
    95
    // Same address, same user_id, untrusted
krista@2601
    96
    cout << "Same address, same user_id, untrusted" << endl;
krista@2600
    97
    char* decrypted_text = NULL;
krista@2600
    98
    stringlist_t* keylist_used = NULL;
krista@2600
    99
    PEP_rating rating;
krista@2600
   100
    PEP_decrypt_flags_t flags;
krista@2623
   101
    char* modified_src = NULL;
krista@2600
   102
    
krista@2600
   103
    status = get_trust(session, same_addr_same_uid);
krista@2600
   104
    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
krista@2600
   105
    
krista@2669
   106
    TEST_ASSERT_MSG(((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed), "(same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed");
krista@2600
   107
    
krista@2615
   108
    flags = 0;
krista@2600
   109
    status = MIME_decrypt_message(session, encoded_text.c_str(), 
krista@2600
   110
                                  encoded_text.size(), &decrypted_text, 
krista@2623
   111
                                  &keylist_used, &rating, &flags,
krista@2623
   112
				  &modified_src);
krista@2600
   113
krista@2600
   114
    status = get_trust(session, same_addr_same_uid);
krista@2669
   115
    TEST_ASSERT_MSG((same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed), "same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed");
krista@2600
   116
krista@2600
   117
    cout << "Case 1 Status: " << tl_status_string(status) << endl; 
krista@2600
   118
    cout << "Private key is not trusted for " << same_addr_same_uid->fpr << ", as desired, as the public key was not trusted." << endl;
krista@2600
   119
    cout << "PASS!" << endl;
krista@2600
   120
krista@2600
   121
    // Case 2:
krista@2600
   122
    cout << decrypted_text << endl;
krista@2600
   123
    
krista@2600
   124
    status = key_reset_trust(session, main_me);      
krista@2600
   125
    status = key_reset_trust(session, same_addr_same_uid);      
krista@2600
   126
}