test/external_revoke_test.cc
author Krista Bennett <krista@pep-project.org>
Mon, 19 Jun 2017 09:01:06 +0200
branchENGINE-9
changeset 1871 51337eb65533
parent 1843 d56e2234b665
child 1945 9503c962f746
permissions -rw-r--r--
ENGINE-9: reverting to default after memory mess.
     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 <time.h>
     7 #include "platform.h"
     8 #include <iostream>
     9 #include <fstream>
    10 #include <assert.h>
    11 #include "mime.h"
    12 #include "message_api.h"
    13 #include "test_util.h"
    14 
    15 using namespace std;
    16 
    17 int main() {
    18     cout << "\n*** external_revoke_test.cc ***\n\n";
    19 
    20     PEP_SESSION session;
    21     
    22     cout << "calling init()\n";
    23     PEP_STATUS status = init(&session);   
    24     assert(status == PEP_STATUS_OK);
    25     assert(session);
    26     cout << "init() completed.\n";
    27 
    28 #ifndef NETPGP
    29     char* fprs[2];
    30 
    31     const string fenris_pub_key = slurp("test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc");
    32     const string fenris_priv_key = slurp("test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc");
    33 
    34     assert(fenris_pub_key.length() != 0);
    35     assert(fenris_priv_key.length() != 0);
    36     
    37     PEP_STATUS statuspub = import_key(session, fenris_pub_key.c_str(), fenris_pub_key.length(), NULL);
    38     PEP_STATUS statuspriv = import_key(session, fenris_priv_key.c_str(), fenris_priv_key.length(), NULL);
    39     assert(statuspub == PEP_STATUS_OK);
    40     assert(statuspriv == PEP_STATUS_OK);
    41 
    42     // Create sender ID
    43     
    44     pEp_identity * me = new_identity("pep.test.fenris@thisstilldoesntwork.lu", "0969FA229DF21C832A64A04711B1B9804F3D2900", PEP_OWN_USERID, "Fenris Hawke");
    45     status = myself(session, me);
    46     
    47     // Create key
    48 
    49     cout << "Creating new id for : ";
    50     char *uniqname = strdup("AAAAtestuser@testdomain.org");
    51     srandom(time(NULL));
    52     for(int i=0; i < 4;i++)
    53         uniqname[i] += random() & 0xf;
    54     
    55     cout << uniqname << "\n";
    56     pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
    57 
    58     status = generate_keypair(session, recip1);
    59     
    60     cout << "Generated fingerprint ";
    61     cout << recip1->fpr << "\n";
    62 
    63     fprs[0] = strdup(recip1->fpr);
    64     
    65     cout << endl << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl;
    66     cout << "Trust and revoke single key, ensure trust changes, then generate new key and ensure rating is correct." << endl;
    67     cout << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl << endl;
    68     
    69     cout << endl << "---------------------------------------------------------" << endl;
    70     cout << "1a. Encrypt message for trusted partner." << endl;
    71     cout << "---------------------------------------------------------" << endl << endl;
    72 
    73     cout << "Trusting personal key for " << uniqname << endl;
    74     // Trust it
    75     recip1->me = false;
    76     status = update_identity(session, recip1);
    77     status = trust_personal_key(session, recip1);
    78     status = update_identity(session, recip1);
    79     
    80     // TODO: Check trust?
    81     cout << "Done! Trusted personal key with fpr " << recip1->fpr << " for " << uniqname << endl;
    82 
    83     const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
    84 
    85     
    86     // encrypt something to the key
    87     cout << "Creating message…\n";
    88     identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
    89     message* outgoing_msg = new_message(PEP_dir_outgoing);
    90     assert(outgoing_msg);
    91     outgoing_msg->from = identity_dup(me);
    92     outgoing_msg->to = to_list;
    93     outgoing_msg->shortmsg = strdup("Greetings, humans!");
    94     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    95     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    96     cout << "Message created.\n";
    97 
    98     message* encrypted_outgoing_msg = NULL;
    99 
   100     cout << "Encrypting message to " << uniqname << "…\n";    
   101     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   102     cout << "Encrypted message with status " << tl_status_string(status) << endl;
   103     // check status
   104     assert(status == PEP_STATUS_OK);
   105     assert(encrypted_outgoing_msg);
   106 
   107     cout << "Checking message recipient comm_type from message." << endl;
   108     // check comm_type
   109     cout << "comm_type: " << tl_ct_string(encrypted_outgoing_msg->to->ident->comm_type) << endl;
   110     assert(encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP);
   111     
   112     status = get_trust(session, recip1);
   113     
   114     cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   115     assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
   116 
   117     // decrypt message
   118     free_message(outgoing_msg);
   119     outgoing_msg = NULL;
   120 
   121     stringlist_t* keylist = nullptr;
   122     PEP_rating rating;
   123     PEP_decrypt_flags_t flags;
   124 
   125     cout << endl << "---------------------------------------------------------" << endl;
   126     cout << "1b. Decrypt message that was encrypted for trusted partner." << endl;
   127     cout << "---------------------------------------------------------" << endl << endl;
   128 
   129     cout << "Decrypting message." << endl;
   130     status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_msg, &keylist, &rating, &flags);
   131     cout << "Decrypted message with status " << tl_status_string(status) << endl;
   132     assert(status == PEP_STATUS_OK);
   133     assert(rating == PEP_rating_trusted);
   134 
   135     // check rating
   136     cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   137     assert(rating == PEP_rating_trusted); // FIXME: trusted and anonymised?
   138     
   139     // check comm_type
   140     status = get_trust(session, recip1);
   141 
   142     cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
   143     assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
   144 
   145     cout << endl << "---------------------------------------------------------" << endl;
   146     cout << "2a. Revoke key for (currently) trusted partner." << endl;
   147     cout << "---------------------------------------------------------" << endl << endl;
   148     // externally revoke key
   149     // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
   150 
   151     cout << "Revoking key." << endl;
   152     status = get_identity(session, uniqname, r1_userid, &recip1);    
   153     status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
   154     cout << "Status of revocation call for " << recip1->fpr << " is "<< tl_status_string(status) << endl;
   155 
   156     // free messages
   157     free_message(outgoing_msg);
   158     free_message(encrypted_outgoing_msg);
   159     outgoing_msg = NULL;
   160     encrypted_outgoing_msg = NULL;
   161     
   162     // encrypt something to the key
   163     cout << "creating message…\n";
   164     to_list = new_identity_list(identity_dup(recip1)); // to bob
   165     outgoing_msg = new_message(PEP_dir_outgoing);
   166     assert(outgoing_msg);
   167     outgoing_msg->from = identity_dup(me);
   168     outgoing_msg->to = to_list;
   169     outgoing_msg->shortmsg = strdup("Greetings, humans!");
   170     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   171     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   172     cout << "message created.\n";
   173 
   174     encrypted_outgoing_msg = NULL;
   175     message* decrypted_msg = NULL;
   176 
   177     cout << endl << "---------------------------------------------------------" << endl;
   178     cout << "2b. Encrypt message for recip whose key has been externally revoked in the keyring, not the app." << endl;
   179     cout << "---------------------------------------------------------" << endl << endl;
   180 
   181     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   182     cout << "Encryption returns with status " << tl_status_string(status) << endl;
   183 
   184     PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   185 
   186     cout << endl << "---------------------------------------------------------" << endl;
   187     cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
   188     cout << "---------------------------------------------------------" << endl << endl;
   189 
   190     // check comm_type
   191     cout << "comm_type: " << tl_ct_string(ct) << endl;
   192     assert(ct == PEP_ct_key_revoked);
   193     
   194     status = get_trust(session, recip1);
   195 
   196     cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   197     assert(recip1->comm_type == PEP_ct_key_revoked);
   198 
   199     cout << endl << "---------------------------------------------------------" << endl;
   200     cout << "2d. Try to decrypt message that was encrypted for revoked key guy." << endl;
   201     cout << "---------------------------------------------------------" << endl << endl;
   202     // decrypt message
   203 //    free_message(outgoing_msg);
   204 //    outgoing_msg = NULL;
   205     // FIXME: Make this make more sense
   206     status = decrypt_message(session, outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
   207     cout << "Decryption returns with status " << tl_status_string(status) << endl;
   208     assert(status == PEP_UNENCRYPTED);
   209     
   210     // check rating
   211     cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   212     assert(rating == PEP_rating_unencrypted);
   213 
   214     ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   215 
   216     cout << "comm_type: " << tl_ct_string(ct) << endl;
   217     assert(ct == PEP_ct_key_revoked);
   218     
   219     status = get_trust(session, recip1);
   220     
   221     cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   222     assert(recip1->comm_type == PEP_ct_key_revoked);
   223 
   224     free_message(encrypted_outgoing_msg);
   225     free_message(decrypted_msg);
   226     free_message(outgoing_msg);
   227     outgoing_msg = NULL;
   228     decrypted_msg = NULL;
   229     encrypted_outgoing_msg = NULL;
   230 
   231     cout << endl << "---------------------------------------------------------" << endl;
   232     cout << "3a. Generate new key, but don't explicitly trust it." << endl;
   233     cout << "---------------------------------------------------------" << endl << endl;
   234 
   235     // now: generate new key
   236     free(recip1->fpr);
   237     recip1->fpr = NULL;
   238     status = generate_keypair(session, recip1);
   239     
   240     cout << "Generated fingerprint \n";
   241     cout << recip1->fpr << "\n";
   242     fprs[1] = strdup(recip1->fpr);
   243 
   244     // try again
   245     cout << endl << "---------------------------------------------------------" << endl;
   246     cout << "3b. Try to send something to the email address of our revoked friend, make sure a new key is used to encrypt." << endl;
   247     cout << "---------------------------------------------------------" << endl << endl;
   248     
   249     // encrypt something to the key
   250     cout << "Creating message…\n";
   251     to_list = new_identity_list(identity_dup(recip1)); // to bob
   252     outgoing_msg = new_message(PEP_dir_outgoing);
   253     assert(outgoing_msg);
   254     outgoing_msg->from = identity_dup(me);
   255     outgoing_msg->to = to_list;
   256     outgoing_msg->shortmsg = strdup("Greetings, humans!");
   257     outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   258     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   259     cout << "Message created.\n";
   260 
   261     status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   262 
   263     ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   264 
   265     // CHECK STATUS???
   266     cout << "Encryption returns with status " << tl_status_string(status) << endl;
   267 
   268     // check comm_type
   269     cout << "comm_type: " << tl_ct_string(ct) << endl;
   270     assert(ct == PEP_ct_OpenPGP_unconfirmed);
   271     
   272     status = get_trust(session, recip1);
   273 
   274     cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   275     assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
   276 
   277     // decrypt message
   278 //    free_message(outgoing_msg);
   279 //    outgoing_msg = NULL;
   280 
   281     cout << endl << "---------------------------------------------------------" << endl;
   282     cout << "3c. Decrypt... that... message!" << endl;
   283     cout << "---------------------------------------------------------" << endl << endl;
   284 
   285 
   286     status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
   287     cout << "Decryption returns with status " << tl_status_string(status) << endl;
   288     assert(status == PEP_STATUS_OK);
   289 
   290     // check rating
   291     cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   292     assert(rating == PEP_rating_reliable);
   293 
   294     ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   295 
   296     cout << "comm_type: " << tl_ct_string(ct) << endl;
   297     assert(ct == PEP_ct_OpenPGP_unconfirmed);
   298     
   299     status = get_trust(session, recip1);
   300     
   301     cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   302     assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
   303 
   304     free_message(encrypted_outgoing_msg);
   305     free_message(decrypted_msg);
   306     free_message(outgoing_msg);
   307     outgoing_msg = NULL;
   308     decrypted_msg = NULL;
   309     encrypted_outgoing_msg = NULL;
   310 
   311     free_identity(me);
   312     free_identity(recip1);
   313     free(uniqname);
   314     
   315     delete_keypair(session, fprs[0]);    
   316     delete_keypair(session, fprs[1]);
   317     
   318     free(fprs[0]);
   319     free(fprs[1]);
   320     
   321 #else
   322     cout << "Sorry, test is not defined for NETPGP at this time." << endl;
   323     
   324 #endif
   325     
   326     release(session);
   327 
   328     return 0;
   329 }