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