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