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