test/external_revoke_test.cc
author Krista Bennett <krista@pep-project.org>
Mon, 09 Oct 2017 16:34:08 +0200
branchENGINE-214
changeset 2147 22394e4b5047
parent 1945 9503c962f746
child 2288 8aee10631677
child 2461 85c8e5aad3bc
permissions -rw-r--r--
ENGINE-214: it woooooorks!
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@1841
    31
    const string fenris_pub_key = slurp("test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc");
krista@1841
    32
    const string fenris_priv_key = slurp("test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc");
krista@1817
    33
krista@1841
    34
    assert(fenris_pub_key.length() != 0);
krista@1841
    35
    assert(fenris_priv_key.length() != 0);
krista@1841
    36
    
krista@1818
    37
    PEP_STATUS statuspub = import_key(session, fenris_pub_key.c_str(), fenris_pub_key.length(), NULL);
krista@1818
    38
    PEP_STATUS statuspriv = import_key(session, fenris_priv_key.c_str(), fenris_priv_key.length(), NULL);
krista@1817
    39
    assert(statuspub == PEP_STATUS_OK);
krista@1817
    40
    assert(statuspriv == PEP_STATUS_OK);
krista@1817
    41
krista@1808
    42
    // Create sender ID
krista@1808
    43
    
krista@1841
    44
    pEp_identity * me = new_identity("pep.test.fenris@thisstilldoesntwork.lu", "0969FA229DF21C832A64A04711B1B9804F3D2900", PEP_OWN_USERID, "Fenris Hawke");
krista@1841
    45
    status = myself(session, me);
krista@1808
    46
    
krista@1808
    47
    // Create key
krista@1808
    48
krista@1812
    49
    cout << "Creating new id for : ";
krista@1808
    50
    char *uniqname = strdup("AAAAtestuser@testdomain.org");
krista@1808
    51
    srandom(time(NULL));
krista@1808
    52
    for(int i=0; i < 4;i++)
krista@1808
    53
        uniqname[i] += random() & 0xf;
krista@1808
    54
    
krista@1808
    55
    cout << uniqname << "\n";
krista@1808
    56
    pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
krista@1808
    57
krista@1808
    58
    status = generate_keypair(session, recip1);
krista@1808
    59
    
krista@1812
    60
    cout << "Generated fingerprint ";
krista@1808
    61
    cout << recip1->fpr << "\n";
krista@1808
    62
krista@1812
    63
    fprs[0] = strdup(recip1->fpr);
krista@1812
    64
    
krista@1812
    65
    cout << endl << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl;
krista@1812
    66
    cout << "Trust and revoke single key, ensure trust changes, then generate new key and ensure rating is correct." << endl;
krista@1812
    67
    cout << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl << endl;
krista@1812
    68
    
krista@1812
    69
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
    70
    cout << "1a. Encrypt message for trusted partner." << endl;
krista@1812
    71
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
    72
krista@1812
    73
    cout << "Trusting personal key for " << uniqname << endl;
krista@1808
    74
    // Trust it
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@1871
    94
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", 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@1871
   170
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", 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@2147
   250
    
krista@2147
   251
    // cout << "First, update identity though!\n";
krista@2147
   252
    // status = update_identity(session, recip1);
krista@1812
   253
    to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1812
   254
    outgoing_msg = new_message(PEP_dir_outgoing);
krista@1812
   255
    assert(outgoing_msg);
krista@1812
   256
    outgoing_msg->from = identity_dup(me);
krista@1812
   257
    outgoing_msg->to = to_list;
krista@1812
   258
    outgoing_msg->shortmsg = strdup("Greetings, humans!");
krista@1812
   259
    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1871
   260
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1812
   261
    cout << "Message created.\n";
krista@1808
   262
krista@1812
   263
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1812
   264
    ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@2147
   265
    
krista@1812
   266
krista@1812
   267
    // CHECK STATUS???
krista@1812
   268
    cout << "Encryption returns with status " << tl_status_string(status) << endl;
krista@1812
   269
krista@1812
   270
    // check comm_type
krista@1812
   271
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@1812
   272
    assert(ct == PEP_ct_OpenPGP_unconfirmed);
krista@1812
   273
    
krista@1812
   274
    status = get_trust(session, recip1);
krista@1812
   275
krista@1812
   276
    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
krista@1812
   277
    assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@1812
   278
krista@1812
   279
    // decrypt message
krista@1812
   280
//    free_message(outgoing_msg);
krista@1812
   281
//    outgoing_msg = NULL;
krista@1812
   282
krista@1812
   283
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   284
    cout << "3c. Decrypt... that... message!" << endl;
krista@1812
   285
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   286
krista@1812
   287
krista@1812
   288
    status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
krista@1812
   289
    cout << "Decryption returns with status " << tl_status_string(status) << endl;
krista@1812
   290
    assert(status == PEP_STATUS_OK);
krista@2147
   291
    assert(decrypted_msg);
krista@2147
   292
    
krista@1808
   293
    // check rating
krista@1812
   294
    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
krista@1812
   295
    assert(rating == PEP_rating_reliable);
krista@1808
   296
krista@2147
   297
    status = update_identity(session, decrypted_msg->to->ident);
krista@1812
   298
    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@1808
   299
krista@1812
   300
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@1812
   301
    assert(ct == PEP_ct_OpenPGP_unconfirmed);
krista@1808
   302
    
krista@1812
   303
    status = get_trust(session, recip1);
krista@1812
   304
    
krista@1812
   305
    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
krista@1812
   306
    assert(recip1->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@1808
   307
krista@1812
   308
    free_message(encrypted_outgoing_msg);
krista@1812
   309
    free_message(decrypted_msg);
krista@1812
   310
    free_message(outgoing_msg);
krista@1812
   311
    outgoing_msg = NULL;
krista@1812
   312
    decrypted_msg = NULL;
krista@1812
   313
    encrypted_outgoing_msg = NULL;
krista@1808
   314
krista@1812
   315
    free_identity(me);
krista@1812
   316
    free_identity(recip1);
krista@1812
   317
    free(uniqname);
krista@1808
   318
    
krista@1812
   319
    delete_keypair(session, fprs[0]);    
krista@1812
   320
    delete_keypair(session, fprs[1]);
krista@1808
   321
    
krista@1812
   322
    free(fprs[0]);
krista@1812
   323
    free(fprs[1]);
krista@1808
   324
    
krista@1808
   325
#else
krista@1808
   326
    cout << "Sorry, test is not defined for NETPGP at this time." << endl;
krista@1808
   327
    
krista@1808
   328
#endif
krista@1808
   329
    
krista@1808
   330
    release(session);
krista@1808
   331
krista@1808
   332
    return 0;
krista@1808
   333
}