test/src/engine_tests/EncryptAttachPrivateKeyTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 3222 fcc3d6543681
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@2594
     1
// This file is under GNU General Public License 3.0
krista@2594
     2
// see LICENSE.txt
krista@2594
     3
krista@2594
     4
#include <stdlib.h>
krista@2654
     5
#include <string>
krista@2654
     6
#include <cstring>
krista@2654
     7
#include <cpptest.h>
krista@2654
     8
krista@2654
     9
#include "pEpEngine.h"
krista@2654
    10
krista@2594
    11
#include "mime.h"
krista@2594
    12
#include "message_api.h"
krista@2594
    13
#include "keymanagement.h"
krista@2594
    14
#include "test_util.h"
krista@2594
    15
krista@2654
    16
#include "EngineTestSessionSuite.h"
krista@2654
    17
#include "EncryptAttachPrivateKeyTests.h"
krista@2654
    18
krista@2594
    19
using namespace std;
krista@2594
    20
krista@2654
    21
EncryptAttachPrivateKeyTests::EncryptAttachPrivateKeyTests(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("EncryptAttachPrivateKeyTests::check_encrypt_attach_private_key"),
krista@2654
    24
                                                                      static_cast<Func>(&EncryptAttachPrivateKeyTests::check_encrypt_attach_private_key)));
krista@2654
    25
}
krista@2594
    26
krista@2654
    27
void EncryptAttachPrivateKeyTests::check_encrypt_attach_private_key() {
krista@2594
    28
krista@2594
    29
    const char* own_uid = PEP_OWN_USERID;
krista@2594
    30
    const char* diff_uid_0 = "TASTY_TEST_UID_0";
krista@2594
    31
    const char* diff_uid_1 = "TASTY_TEST_UID_1";
krista@2594
    32
krista@2595
    33
    cout << "Importing keys..." << endl;
krista@2595
    34
    
krista@2594
    35
    string input_key;
krista@2594
    36
    const char* main_addr = "priv-key-import-test-main@darthmama.cool";
krista@2594
    37
    pEp_identity* main_me = NULL;
krista@2594
    38
    const char* fpr_main_me = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
krista@2594
    39
    pEp_identity* same_addr_same_uid = NULL;
krista@2594
    40
    const char* fpr_same_addr_same_uid = "359DD8AC87D1F5E4304D08338D7185F180C8CD87";
krista@2597
    41
    
krista@2594
    42
    pEp_identity* same_addr_diff_uid = NULL;
krista@2594
    43
    const char* fpr_same_addr_diff_uid = "B044B83639E292283A3F6E14C2E64B520B74809C";
krista@2594
    44
krista@2594
    45
    const char* diff_addr_0 = "priv-key-import-test-other_0@darthmama.cool";
krista@2594
    46
    pEp_identity* diff_addr_same_uid = NULL;
krista@2594
    47
    const char* fpr_diff_addr_same_uid = "C52911EBA0D34B0F549594A15A7A363BD11252C9";
krista@2594
    48
    
krista@2594
    49
    const char* diff_addr_1 = "priv-key-import-test-other_1@darthmama.cool";
krista@2594
    50
    pEp_identity* diff_addr_diff_uid = NULL;
krista@2594
    51
    const char* fpr_diff_addr_diff_uid = "567212EFB8A3A76B1D32B9565F45BEA9C785F20A";
krista@2594
    52
    
krista@2594
    53
    PEP_STATUS status = PEP_STATUS_OK;
krista@2594
    54
krista@2594
    55
    // key for main own user
krista@2594
    56
    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
krista@2594
    57
    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
krista@2594
    58
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    59
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2594
    60
krista@2594
    61
    input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0x3276D2E5_priv.asc");
krista@2594
    62
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    63
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2594
    64
        
krista@2594
    65
    // key with same address and user_id (initially untrusted, then trusted)
krista@2594
    66
    // 359DD8AC87D1F5E4304D08338D7185F180C8CD87
krista@2594
    67
    input_key = slurp("test_keys/pub/priv-key-import-test-main_1-0x80C8CD87_pub.asc");
krista@2594
    68
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    69
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2594
    70
krista@2594
    71
    // key with same address and to have different (non-aliased) user_id (initially untrusted, then trusted)
krista@2594
    72
    // B044B83639E292283A3F6E14C2E64B520B74809C
krista@2594
    73
    input_key = slurp("test_keys/pub/priv-key-import-test-main_2-0x0B74809C_pub.asc");
krista@2594
    74
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    75
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2594
    76
krista@2594
    77
    // key with different address to have same user_id (initially untrusted, then trusted)
krista@2594
    78
    // C52911EBA0D34B0F549594A15A7A363BD11252C9
krista@2594
    79
    input_key = slurp("test_keys/pub/priv-key-import-test-other_0-0xD11252C9_pub.asc");
krista@2594
    80
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    81
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2594
    82
        
krista@2594
    83
    // key with different address to have different user_id (initially untrusted, then trusted)
krista@2594
    84
    // 567212EFB8A3A76B1D32B9565F45BEA9C785F20A
krista@2594
    85
    input_key = slurp("test_keys/pub/priv-key-import-test-other_1-0xC785F20A_pub.asc");
krista@2594
    86
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    87
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2595
    88
    cout << "Done!" << endl << endl;
krista@2594
    89
    
krista@2595
    90
    cout << "Setting up own identity with default key " << fpr_main_me << endl;
krista@2594
    91
    // Own identity with default key etc
krista@2594
    92
    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
krista@2594
    93
    status = set_own_key(session, main_me, fpr_main_me);
krista@3217
    94
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2594
    95
krista@2669
    96
    TEST_ASSERT_MSG((strcmp(main_me->fpr, fpr_main_me) == 0), "strcmp(main_me->fpr, fpr_main_me) == 0");
krista@2595
    97
    cout << "Done!" << endl << endl;
krista@2594
    98
    
krista@2595
    99
    cout << "Setting up recipient identities and resetting key trust." << endl;
krista@2595
   100
    cout << "#1: same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
krista@2594
   101
    // Identity with same address and user_id - the fpr here will be ignored in update_identity and friends.
krista@2594
   102
    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
krista@2595
   103
    status = key_reset_trust(session, same_addr_same_uid);
krista@2669
   104
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), "status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY");
krista@2669
   105
    TEST_ASSERT_MSG((strcmp(same_addr_same_uid->fpr, fpr_same_addr_same_uid) == 0), "strcmp(same_addr_same_uid->fpr, fpr_same_addr_same_uid) == 0");
krista@2594
   106
    
krista@2594
   107
    // Identity with same address and different user_id
krista@2595
   108
    cout << "#2: same address, different user_id - address: " << main_addr << ", user_id: " << diff_uid_0 << ", fpr: " << fpr_same_addr_diff_uid << endl;  
krista@2594
   109
    same_addr_diff_uid = new_identity(main_addr, fpr_same_addr_diff_uid, diff_uid_0, "PrivateKey Import Test");
krista@2669
   110
    TEST_ASSERT_MSG((same_addr_diff_uid), "same_addr_diff_uid");
krista@2595
   111
    status = key_reset_trust(session, same_addr_diff_uid);
krista@2669
   112
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), "status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY");
krista@2669
   113
    TEST_ASSERT_MSG((strcmp(same_addr_diff_uid->fpr, fpr_same_addr_diff_uid) == 0), "strcmp(same_addr_diff_uid->fpr, fpr_same_addr_diff_uid) == 0");
krista@2594
   114
    
krista@2594
   115
    // Identity with diff address and same user_id
krista@2595
   116
    cout << "#3: different address, same user_id - address: " << diff_addr_0 << ", user_id: " << own_uid << ", fpr: " << fpr_diff_addr_same_uid << endl;      
krista@2594
   117
    diff_addr_same_uid = new_identity(diff_addr_0, fpr_diff_addr_same_uid, own_uid, "PrivateKey Import Test");
krista@2669
   118
    TEST_ASSERT_MSG((diff_addr_same_uid), "diff_addr_same_uid");
krista@2595
   119
    status = key_reset_trust(session, diff_addr_same_uid);
krista@2669
   120
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), "status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY");
krista@2669
   121
    TEST_ASSERT_MSG((strcmp(diff_addr_same_uid->fpr, fpr_diff_addr_same_uid) == 0), "strcmp(diff_addr_same_uid->fpr, fpr_diff_addr_same_uid) == 0");
krista@2594
   122
krista@2594
   123
    // Identity with different address and different user_id
krista@2595
   124
    cout << "#4: different address, different user_id - address: " << diff_addr_1 << ", user_id: " << diff_uid_1 << ", fpr: " << fpr_diff_addr_diff_uid << endl;      
krista@2594
   125
    diff_addr_diff_uid = new_identity(diff_addr_1, fpr_diff_addr_diff_uid, diff_uid_1, "PrivateKey Import Test");
krista@2669
   126
    TEST_ASSERT_MSG((diff_addr_diff_uid), "diff_addr_diff_uid");
krista@2595
   127
    status = key_reset_trust(session, diff_addr_diff_uid);
krista@2669
   128
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), "status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY");
krista@2669
   129
    TEST_ASSERT_MSG((strcmp(diff_addr_diff_uid->fpr, fpr_diff_addr_diff_uid) == 0), "strcmp(diff_addr_diff_uid->fpr, fpr_diff_addr_diff_uid) == 0");
krista@2595
   130
    cout << "Done!" << endl << endl;
krista@2594
   131
krista@2594
   132
    message* msg_same_addr_same_uid = new_message(PEP_dir_outgoing);
krista@2594
   133
    msg_same_addr_same_uid->from = main_me;
krista@2594
   134
    msg_same_addr_same_uid->shortmsg = strdup("Greetings, humans!");
krista@2594
   135
    msg_same_addr_same_uid->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@2594
   136
    msg_same_addr_same_uid->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@2594
   137
krista@2594
   138
    message* msg_same_addr_diff_uid = message_dup(msg_same_addr_same_uid);
krista@2594
   139
    message* msg_diff_addr_same_uid = message_dup(msg_same_addr_same_uid);       
krista@2594
   140
    message* msg_diff_addr_diff_uid = message_dup(msg_same_addr_same_uid);       
krista@2594
   141
krista@2595
   142
    cout << "Starting tests..." << endl;
krista@2594
   143
    // Case 1:
krista@2594
   144
    // Same address, same user_id, untrusted
krista@2595
   145
    cout << "Case 1: Same address, same user_id, untrusted" << endl;
krista@2669
   146
    TEST_ASSERT_MSG((msg_same_addr_same_uid), "msg_same_addr_same_uid");        
krista@2594
   147
    identity_list* to_list = new_identity_list(same_addr_same_uid);
krista@2595
   148
    msg_same_addr_same_uid->to = to_list;
krista@2594
   149
    message* enc_same_addr_same_uid_untrusted = NULL;
krista@2594
   150
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   151
                                              msg_same_addr_same_uid,
krista@2594
   152
                                              &enc_same_addr_same_uid_untrusted,
krista@2594
   153
                                              fpr_same_addr_same_uid,
krista@2594
   154
                                              PEP_enc_PGP_MIME,
krista@2594
   155
                                              0);
krista@2594
   156
krista@2595
   157
    cout << "Case 1 Status: " << tl_status_string(status) << endl;
krista@2669
   158
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");
krista@2595
   159
    cout << "PASS!" << endl;
krista@2595
   160
    
krista@2594
   161
    // Case 2:
krista@2594
   162
    // Same address, same_user_id, trusted
krista@2595
   163
    cout << "Case 2: Same address, same user_id, trusted" << endl;
krista@3222
   164
    status = trust_own_key(session, same_addr_same_uid);
krista@2630
   165
    cout << "Trust personal key for " << same_addr_same_uid << " gives status " << tl_status_string(status) << " (" << status << ")" << endl;
krista@3217
   166
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2594
   167
    message* enc_same_addr_same_uid_trusted = NULL;
krista@2594
   168
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   169
                                              msg_same_addr_same_uid,
krista@2594
   170
                                              &enc_same_addr_same_uid_trusted,
krista@2594
   171
                                              fpr_same_addr_same_uid,
krista@2594
   172
                                              PEP_enc_PGP_MIME,
krista@2594
   173
                                              0);
krista@2594
   174
krista@2595
   175
    cout << "Case 2 Status: " << tl_status_string(status) << endl;
krista@3217
   176
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2595
   177
    cout << "PASS!" << endl;
krista@2595
   178
krista@2594
   179
    // Case 3:
krista@2594
   180
    // Different address, same user_id, untrusted
krista@2595
   181
    cout << "Case 3: Different address, same user_id, untrusted" << endl;
krista@2669
   182
    TEST_ASSERT_MSG((msg_diff_addr_same_uid), "msg_diff_addr_same_uid");        
krista@2594
   183
    identity_list* to_list_1 = new_identity_list(diff_addr_same_uid);
krista@2595
   184
    msg_diff_addr_same_uid->to = to_list_1;
krista@2594
   185
    message* enc_diff_addr_same_uid_untrusted = NULL;
krista@2594
   186
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   187
                                              msg_diff_addr_same_uid,
krista@2594
   188
                                              &enc_diff_addr_same_uid_untrusted,
krista@2594
   189
                                              fpr_diff_addr_same_uid,
krista@2594
   190
                                              PEP_enc_PGP_MIME,
krista@2594
   191
                                              0);
krista@2595
   192
    
krista@2595
   193
    cout << "Case 3 Status: " << tl_status_string(status) << endl;
krista@2669
   194
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");
krista@2595
   195
    cout << "PASS!" << endl;
krista@2594
   196
krista@2594
   197
    // Case 4:
krista@2594
   198
    // Different address, same user_id, trusted
krista@2595
   199
    cout << "Case 4: Different address, same user_id, trusted" << endl;
krista@3222
   200
    status = trust_own_key(session, diff_addr_same_uid);
krista@3217
   201
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2594
   202
    message* enc_diff_addr_same_uid_trusted = NULL;
krista@2594
   203
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   204
                                              msg_diff_addr_same_uid,
krista@2594
   205
                                              &enc_diff_addr_same_uid_trusted,
krista@2594
   206
                                              fpr_diff_addr_same_uid,
krista@2594
   207
                                              PEP_enc_PGP_MIME,
krista@2594
   208
                                              0);
krista@2595
   209
                                              
krista@2595
   210
    cout << "Case 4 Status: " << tl_status_string(status) << endl;
krista@2669
   211
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");
krista@2595
   212
    cout << "PASS!" << endl;
krista@2594
   213
krista@2594
   214
    // Case 5:
krista@2594
   215
    // Same address, different user_id, untrusted
krista@2595
   216
    cout << "Case 5: Same address, different user_id, untrusted" << endl;    
krista@2669
   217
    TEST_ASSERT_MSG((msg_same_addr_diff_uid), "msg_same_addr_diff_uid");        
krista@2594
   218
    identity_list* to_list_2 = new_identity_list(same_addr_diff_uid);
krista@2595
   219
    msg_same_addr_diff_uid->to = to_list_2;
krista@2594
   220
    message* enc_same_addr_diff_uid_untrusted = NULL;
krista@2594
   221
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   222
                                              msg_same_addr_diff_uid,
krista@2594
   223
                                              &enc_same_addr_diff_uid_untrusted,
krista@2594
   224
                                              fpr_same_addr_diff_uid,
krista@2594
   225
                                              PEP_enc_PGP_MIME,
krista@2594
   226
                                              0);
krista@2594
   227
krista@2595
   228
    cout << "Case 5 Status: " << tl_status_string(status) << endl;
krista@2669
   229
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");    
krista@2595
   230
    cout << "PASS!" << endl;
krista@2595
   231
    
krista@2594
   232
    // Case 6:
krista@2594
   233
    // Same address, different user_id, trusted
krista@2595
   234
    cout << "Case 6: Same address, different user_id, trusted" << endl;        
krista@2594
   235
    status = trust_personal_key(session, same_addr_diff_uid);
krista@3217
   236
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2594
   237
    message* enc_same_addr_diff_uid_trusted = NULL;
krista@2594
   238
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   239
                                              msg_same_addr_diff_uid,
krista@2594
   240
                                              &enc_same_addr_diff_uid_untrusted,
krista@2594
   241
                                              fpr_same_addr_diff_uid,
krista@2594
   242
                                              PEP_enc_PGP_MIME,
krista@2594
   243
                                              0);
krista@2594
   244
krista@2595
   245
    cout << "Case 6 Status: " << tl_status_string(status) << endl;
krista@2669
   246
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");    
krista@2595
   247
    cout << "PASS!" << endl;
krista@2595
   248
krista@2594
   249
    // Case 7:
krista@2594
   250
    // Different address, different user_id, untrusted
krista@2595
   251
    cout << "Case 7: Different address, different user_id, untrusted" << endl;    
krista@2669
   252
    TEST_ASSERT_MSG((msg_diff_addr_diff_uid), "msg_diff_addr_diff_uid");        
krista@2594
   253
    identity_list* to_list_3 = new_identity_list(diff_addr_diff_uid);
krista@2595
   254
    msg_diff_addr_diff_uid->to = to_list_3;
krista@2594
   255
    message* enc_diff_addr_diff_uid_untrusted = NULL;
krista@2594
   256
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   257
                                              msg_diff_addr_diff_uid,
krista@2594
   258
                                              &enc_diff_addr_diff_uid_untrusted,
krista@2594
   259
                                              fpr_diff_addr_diff_uid,
krista@2594
   260
                                              PEP_enc_PGP_MIME,
krista@2594
   261
                                              0);
krista@2594
   262
krista@2595
   263
    cout << "Case 7 Status: " << tl_status_string(status) << endl;
krista@2669
   264
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");
krista@2595
   265
    cout << "PASS!" << endl;
krista@2595
   266
krista@2594
   267
    // Case 8:
krista@2594
   268
    // Different address, different user_id, trusted
krista@2595
   269
    cout << "Case 8: Different address, different user_id, trusted" << endl;    
krista@2594
   270
    status = trust_personal_key(session, diff_addr_diff_uid);
krista@3217
   271
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2594
   272
    message* enc_diff_addr_diff_uid_trusted = NULL;
krista@2594
   273
    status = encrypt_message_and_add_priv_key(session,
krista@2594
   274
                                              msg_diff_addr_diff_uid,
krista@2594
   275
                                              &enc_diff_addr_diff_uid_trusted,
krista@2594
   276
                                              fpr_diff_addr_diff_uid,
krista@2594
   277
                                              PEP_enc_PGP_MIME,
krista@2594
   278
                                              0);
krista@2594
   279
krista@2595
   280
    cout << "Case 8 Status: " << tl_status_string(status) << endl;
krista@2669
   281
    TEST_ASSERT_MSG((status == PEP_ILLEGAL_VALUE), "status == PEP_ILLEGAL_VALUE");
krista@2595
   282
    cout << "PASS!" << endl;
krista@2596
   283
    
krista@2596
   284
    cout << "Correctly encrypted message:" << endl << endl;                
krista@2596
   285
    char* encrypted_msg_text = NULL;
krista@2596
   286
    mime_encode_message(enc_same_addr_same_uid_trusted, false, &encrypted_msg_text);                                    
krista@2596
   287
    cout << encrypted_msg_text << endl << endl;
krista@2596
   288
    
krista@2597
   289
    // FIXME: Free all the damned things
krista@2594
   290
}