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