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