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