test/external_revoke_test.cc
branchENGINE-209
changeset 1812 925ad845e3ec
parent 1811 04d2a7abcfa2
child 1817 d206064589cf
     1.1 --- a/test/external_revoke_test.cc	Wed May 31 17:47:42 2017 +0200
     1.2 +++ b/test/external_revoke_test.cc	Thu Jun 01 19:52:13 2017 +0200
     1.3 @@ -10,6 +10,7 @@
     1.4  #include <assert.h>
     1.5  #include "mime.h"
     1.6  #include "message_api.h"
     1.7 +#include "test_util.h"
     1.8  
     1.9  using namespace std;
    1.10  
    1.11 @@ -25,7 +26,8 @@
    1.12      cout << "init() completed.\n";
    1.13  
    1.14  #ifndef NETPGP
    1.15 -
    1.16 +    char* fprs[2];
    1.17 +    
    1.18      // Create sender ID
    1.19      
    1.20      pEp_identity * me = new_identity("pep.test.apple@pep-project.org", NULL, PEP_OWN_USERID, "Alice Cooper");
    1.21 @@ -35,7 +37,7 @@
    1.22      
    1.23      // Create key
    1.24  
    1.25 -    cout << "creating new id for : ";
    1.26 +    cout << "Creating new id for : ";
    1.27      char *uniqname = strdup("AAAAtestuser@testdomain.org");
    1.28      srandom(time(NULL));
    1.29      for(int i=0; i < 4;i++)
    1.30 @@ -46,178 +48,267 @@
    1.31  
    1.32      status = generate_keypair(session, recip1);
    1.33      
    1.34 -    cout << "generated fingerprint \n";
    1.35 +    cout << "Generated fingerprint ";
    1.36      cout << recip1->fpr << "\n";
    1.37  
    1.38 +    fprs[0] = strdup(recip1->fpr);
    1.39 +    
    1.40 +    cout << endl << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl;
    1.41 +    cout << "Trust and revoke single key, ensure trust changes, then generate new key and ensure rating is correct." << endl;
    1.42 +    cout << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl << endl;
    1.43 +    
    1.44 +    cout << endl << "---------------------------------------------------------" << endl;
    1.45 +    cout << "1a. Encrypt message for trusted partner." << endl;
    1.46 +    cout << "---------------------------------------------------------" << endl << endl;
    1.47 +
    1.48 +    cout << "Trusting personal key for " << uniqname << endl;
    1.49      // Trust it
    1.50      recip1->me = false;
    1.51      status = update_identity(session, recip1);
    1.52      status = trust_personal_key(session, recip1);
    1.53      status = update_identity(session, recip1);
    1.54 +    
    1.55 +    // TODO: Check trust?
    1.56 +    cout << "Done! Trusted personal key with fpr " << recip1->fpr << " for " << uniqname << endl;
    1.57  
    1.58      const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
    1.59  
    1.60 +    
    1.61      // encrypt something to the key
    1.62 -    cout << "creating message…\n";
    1.63 +    cout << "Creating message…\n";
    1.64      identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
    1.65 -    message* outgoing_message = new_message(PEP_dir_outgoing);
    1.66 -    assert(outgoing_message);
    1.67 -    outgoing_message->from = identity_dup(me);
    1.68 -    outgoing_message->to = to_list;
    1.69 -    outgoing_message->shortmsg = strdup("Greetings, humans!");
    1.70 -    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    1.71 -    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    1.72 -    cout << "message created.\n";
    1.73 +    message* outgoing_msg = new_message(PEP_dir_outgoing);
    1.74 +    assert(outgoing_msg);
    1.75 +    outgoing_msg->from = identity_dup(me);
    1.76 +    outgoing_msg->to = to_list;
    1.77 +    outgoing_msg->shortmsg = strdup("Greetings, humans!");
    1.78 +    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
    1.79 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    1.80 +    cout << "Message created.\n";
    1.81  
    1.82      message* encrypted_outgoing_msg = NULL;
    1.83 -    
    1.84 -    status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
    1.85 -    
    1.86 +
    1.87 +    cout << "Encrypting message to " << uniqname << "…\n";    
    1.88 +    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
    1.89 +    cout << "Encrypted message with status " << tl_status_string(status) << endl;
    1.90      // check status
    1.91      assert(status == PEP_STATUS_OK);
    1.92      assert(encrypted_outgoing_msg);
    1.93  
    1.94 +    cout << "Checking message recipient comm_type from message." << endl;
    1.95      // check comm_type
    1.96 -    cout << "comm_type: " << encrypted_outgoing_msg->to->ident->comm_type << endl;
    1.97 +    cout << "comm_type: " << tl_ct_string(encrypted_outgoing_msg->to->ident->comm_type) << endl;
    1.98      assert(encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP);
    1.99      
   1.100      status = get_trust(session, recip1);
   1.101      
   1.102 -    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
   1.103 +    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   1.104      assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
   1.105  
   1.106      // decrypt message
   1.107 -    free_message(outgoing_message);
   1.108 -    outgoing_message = NULL;
   1.109 +    free_message(outgoing_msg);
   1.110 +    outgoing_msg = NULL;
   1.111  
   1.112      stringlist_t* keylist = nullptr;
   1.113      PEP_rating rating;
   1.114      PEP_decrypt_flags_t flags;
   1.115  
   1.116 -    status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_message, &keylist, &rating, &flags);
   1.117 +    cout << endl << "---------------------------------------------------------" << endl;
   1.118 +    cout << "1b. Decrypt message that was encrypted for trusted partner." << endl;
   1.119 +    cout << "---------------------------------------------------------" << endl << endl;
   1.120 +
   1.121 +    cout << "Decrypting message." << endl;
   1.122 +    status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_msg, &keylist, &rating, &flags);
   1.123 +    cout << "Decrypted message with status " << tl_status_string(status) << endl;
   1.124      assert(status == PEP_STATUS_OK);
   1.125      assert(rating == PEP_rating_trusted);
   1.126  
   1.127      // check rating
   1.128 -    cout << "Rating of decrypted message to trusted recip: " << rating << endl;
   1.129 +    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   1.130      assert(rating == PEP_rating_trusted); // FIXME: trusted and anonymised?
   1.131      
   1.132      // check comm_type
   1.133      status = get_trust(session, recip1);
   1.134  
   1.135 -    cout << "Recip's trust DB comm_type = " << recip1->comm_type << endl;
   1.136 +    cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
   1.137      assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
   1.138  
   1.139 +    cout << endl << "---------------------------------------------------------" << endl;
   1.140 +    cout << "2a. Revoke key for (currently) trusted partner." << endl;
   1.141 +    cout << "---------------------------------------------------------" << endl << endl;
   1.142      // externally revoke key
   1.143      // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
   1.144  
   1.145 -    status = get_identity(session, uniqname, r1_userid, &recip1);
   1.146 -    
   1.147 +    cout << "Revoking key." << endl;
   1.148 +    status = get_identity(session, uniqname, r1_userid, &recip1);    
   1.149      status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
   1.150 -    
   1.151 +    cout << "Status of revocation call for " << recip1->fpr << " is "<< tl_status_string(status) << endl;
   1.152 +
   1.153      // free messages
   1.154 -    free_message(outgoing_message);
   1.155 +    free_message(outgoing_msg);
   1.156      free_message(encrypted_outgoing_msg);
   1.157 -    outgoing_message = NULL;
   1.158 +    outgoing_msg = NULL;
   1.159      encrypted_outgoing_msg = NULL;
   1.160      
   1.161      // encrypt something to the key
   1.162      cout << "creating message…\n";
   1.163      to_list = new_identity_list(identity_dup(recip1)); // to bob
   1.164 -    outgoing_message = new_message(PEP_dir_outgoing);
   1.165 -    assert(outgoing_message);
   1.166 -    outgoing_message->from = identity_dup(me);
   1.167 -    outgoing_message->to = to_list;
   1.168 -    outgoing_message->shortmsg = strdup("Greetings, humans!");
   1.169 -    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   1.170 -    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   1.171 +    outgoing_msg = new_message(PEP_dir_outgoing);
   1.172 +    assert(outgoing_msg);
   1.173 +    outgoing_msg->from = identity_dup(me);
   1.174 +    outgoing_msg->to = to_list;
   1.175 +    outgoing_msg->shortmsg = strdup("Greetings, humans!");
   1.176 +    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   1.177 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   1.178      cout << "message created.\n";
   1.179  
   1.180      encrypted_outgoing_msg = NULL;
   1.181      message* decrypted_msg = NULL;
   1.182  
   1.183 -    status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   1.184 +    cout << endl << "---------------------------------------------------------" << endl;
   1.185 +    cout << "2b. Encrypt message for recip whose key has been externally revoked in the keyring, not the app." << endl;
   1.186 +    cout << "---------------------------------------------------------" << endl << endl;
   1.187  
   1.188 -    PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
   1.189 +    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   1.190 +    cout << "Encryption returns with status " << tl_status_string(status) << endl;
   1.191 +
   1.192 +    PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   1.193 +
   1.194 +    cout << endl << "---------------------------------------------------------" << endl;
   1.195 +    cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
   1.196 +    cout << "---------------------------------------------------------" << endl << endl;
   1.197  
   1.198      // check comm_type
   1.199 -    cout << "comm_type: " << ct << endl;
   1.200 +    cout << "comm_type: " << tl_ct_string(ct) << endl;
   1.201      assert(ct == PEP_ct_key_revoked);
   1.202      
   1.203      status = get_trust(session, recip1);
   1.204  
   1.205 -    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
   1.206 +    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   1.207      assert(recip1->comm_type == PEP_ct_key_revoked);
   1.208  
   1.209 +    cout << endl << "---------------------------------------------------------" << endl;
   1.210 +    cout << "2d. Try to decrypt message that was encrypted for revoked key guy." << endl;
   1.211 +    cout << "---------------------------------------------------------" << endl << endl;
   1.212      // decrypt message
   1.213 -//    free_message(outgoing_message);
   1.214 -//    outgoing_message = NULL;
   1.215 -
   1.216 -    status = decrypt_message(session, outgoing_message, &decrypted_msg, &keylist, &rating, &flags);
   1.217 +//    free_message(outgoing_msg);
   1.218 +//    outgoing_msg = NULL;
   1.219 +    // FIXME: Make this make more sense
   1.220 +    status = decrypt_message(session, outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
   1.221 +    cout << "Decryption returns with status " << tl_status_string(status) << endl;
   1.222      assert(status == PEP_UNENCRYPTED);
   1.223 -
   1.224 +    
   1.225      // check rating
   1.226 -    cout << "Rating of decrypted message to trusted recip: " << rating << endl;
   1.227 +    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   1.228      assert(rating == PEP_rating_unencrypted);
   1.229  
   1.230 -    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
   1.231 +    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   1.232  
   1.233 -    cout << "comm_type: " << ct << endl;
   1.234 +    cout << "comm_type: " << tl_ct_string(ct) << endl;
   1.235      assert(ct == PEP_ct_key_revoked);
   1.236      
   1.237      status = get_trust(session, recip1);
   1.238      
   1.239 -    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
   1.240 +    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   1.241      assert(recip1->comm_type == PEP_ct_key_revoked);
   1.242  
   1.243 -    // generate new key
   1.244 +    free_message(encrypted_outgoing_msg);
   1.245 +    free_message(decrypted_msg);
   1.246 +    free_message(outgoing_msg);
   1.247 +    outgoing_msg = NULL;
   1.248 +    decrypted_msg = NULL;
   1.249 +    encrypted_outgoing_msg = NULL;
   1.250 +
   1.251 +    cout << endl << "---------------------------------------------------------" << endl;
   1.252 +    cout << "3a. Generate new key, but don't explicitly trust it." << endl;
   1.253 +    cout << "---------------------------------------------------------" << endl << endl;
   1.254 +
   1.255 +    // now: generate new key
   1.256 +    free(recip1->fpr);
   1.257 +    recip1->fpr = NULL;
   1.258      status = generate_keypair(session, recip1);
   1.259      
   1.260 -    cout << "generated fingerprint \n";
   1.261 +    cout << "Generated fingerprint \n";
   1.262      cout << recip1->fpr << "\n";
   1.263 +    fprs[1] = strdup(recip1->fpr);
   1.264  
   1.265 -    // PART DEUX
   1.266 +    // try again
   1.267 +    cout << endl << "---------------------------------------------------------" << endl;
   1.268 +    cout << "3b. Try to send something to the email address of our revoked friend, make sure a new key is used to encrypt." << endl;
   1.269 +    cout << "---------------------------------------------------------" << endl << endl;
   1.270      
   1.271 -    // Create key
   1.272 +    // encrypt something to the key
   1.273 +    cout << "Creating message…\n";
   1.274 +    to_list = new_identity_list(identity_dup(recip1)); // to bob
   1.275 +    outgoing_msg = new_message(PEP_dir_outgoing);
   1.276 +    assert(outgoing_msg);
   1.277 +    outgoing_msg->from = identity_dup(me);
   1.278 +    outgoing_msg->to = to_list;
   1.279 +    outgoing_msg->shortmsg = strdup("Greetings, humans!");
   1.280 +    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   1.281 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   1.282 +    cout << "Message created.\n";
   1.283  
   1.284 -    // DO NOT trust it
   1.285 +    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
   1.286  
   1.287 -    // encrypt something to the key
   1.288 +    ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   1.289 +
   1.290 +    // CHECK STATUS???
   1.291 +    cout << "Encryption returns with status " << tl_status_string(status) << endl;
   1.292 +
   1.293 +    // check comm_type
   1.294 +    cout << "comm_type: " << tl_ct_string(ct) << endl;
   1.295 +    assert(ct == PEP_ct_OpenPGP_unconfirmed);
   1.296 +    
   1.297 +    status = get_trust(session, recip1);
   1.298 +
   1.299 +    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   1.300 +    assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
   1.301 +
   1.302 +    // decrypt message
   1.303 +//    free_message(outgoing_msg);
   1.304 +//    outgoing_msg = NULL;
   1.305 +
   1.306 +    cout << endl << "---------------------------------------------------------" << endl;
   1.307 +    cout << "3c. Decrypt... that... message!" << endl;
   1.308 +    cout << "---------------------------------------------------------" << endl << endl;
   1.309 +
   1.310 +
   1.311 +    status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
   1.312 +    cout << "Decryption returns with status " << tl_status_string(status) << endl;
   1.313 +    assert(status == PEP_STATUS_OK);
   1.314  
   1.315      // check rating
   1.316 +    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
   1.317 +    assert(rating == PEP_rating_reliable);
   1.318  
   1.319 -    // check comm_type
   1.320 +    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
   1.321  
   1.322 -    // externally revoke key
   1.323 +    cout << "comm_type: " << tl_ct_string(ct) << endl;
   1.324 +    assert(ct == PEP_ct_OpenPGP_unconfirmed);
   1.325      
   1.326 -    // encrypt something to the key
   1.327 +    status = get_trust(session, recip1);
   1.328 +    
   1.329 +    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
   1.330 +    assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
   1.331  
   1.332 -    // check rating
   1.333 +    free_message(encrypted_outgoing_msg);
   1.334 +    free_message(decrypted_msg);
   1.335 +    free_message(outgoing_msg);
   1.336 +    outgoing_msg = NULL;
   1.337 +    decrypted_msg = NULL;
   1.338 +    encrypted_outgoing_msg = NULL;
   1.339  
   1.340 -    // check comm_type
   1.341 -
   1.342 -
   1.343 -    // PART TROIS
   1.344 +    free_identity(me);
   1.345 +    free_identity(recip1);
   1.346 +    free(uniqname);
   1.347      
   1.348 -    // Create key
   1.349 -
   1.350 -    // DO NOT trust it
   1.351 -
   1.352 -    // encrypt something to the key
   1.353 -
   1.354 -    // check rating
   1.355 -
   1.356 -    // check comm_type
   1.357 -
   1.358 -    // externally revoke key
   1.359 +    delete_keypair(session, fprs[0]);    
   1.360 +    delete_keypair(session, fprs[1]);
   1.361      
   1.362 -    // encrypt something to the key
   1.363 -
   1.364 -    // check rating
   1.365 -
   1.366 -    // check comm_type
   1.367 +    free(fprs[0]);
   1.368 +    free(fprs[1]);
   1.369      
   1.370 -    free(uniqname);
   1.371  #else
   1.372      cout << "Sorry, test is not defined for NETPGP at this time." << endl;
   1.373