test/external_revoke_test.cc
author Krista Bennett <krista@pep-project.org>
Thu, 19 Apr 2018 16:03:44 +0200
branchENGINE-423
changeset 2615 5cd3625e0d91
parent 2477 45e8ed722fec
permissions -rw-r--r--
ENGINE-423: normal tests pass. Now to test reencrypt functionality.
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@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@2461
    73
    recip1->me = false;
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@2615
   128
    flags = 0;
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@2477
   152
    status = update_identity(session, 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@1871
   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@2477
   181
krista@1812
   182
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1812
   183
    cout << "Encryption returns with status " << tl_status_string(status) << endl;
krista@2477
   184
    assert (status == PEP_UNENCRYPTED);
krista@2461
   185
    assert (encrypted_outgoing_msg == NULL);
krista@2461
   186
    status = update_identity(session, recip1);
krista@2477
   187
    assert(recip1->comm_type = PEP_ct_key_not_found);
krista@1812
   188
krista@1812
   189
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   190
    cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
krista@1812
   191
    cout << "---------------------------------------------------------" << endl << endl;
krista@1811
   192
krista@2461
   193
    assert(recip1->fpr == NULL);
krista@2461
   194
    recip1->fpr = fprs[0];
krista@1808
   195
    status = get_trust(session, recip1);
krista@2461
   196
    recip1->fpr = NULL;
krista@1808
   197
krista@1812
   198
    cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
krista@2477
   199
    assert(recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked);
krista@1808
   200
krista@1812
   201
    free_message(decrypted_msg);
krista@1812
   202
    free_message(outgoing_msg);
krista@1812
   203
    outgoing_msg = NULL;
krista@1812
   204
    decrypted_msg = NULL;
krista@1812
   205
krista@1812
   206
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   207
    cout << "3a. Generate new key, but don't explicitly trust it." << endl;
krista@1812
   208
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   209
krista@1812
   210
    // now: generate new key
krista@1812
   211
    free(recip1->fpr);
krista@1812
   212
    recip1->fpr = NULL;
krista@1808
   213
    status = generate_keypair(session, recip1);
krista@1808
   214
    
krista@1812
   215
    cout << "Generated fingerprint \n";
krista@1808
   216
    cout << recip1->fpr << "\n";
krista@1812
   217
    fprs[1] = strdup(recip1->fpr);
krista@1808
   218
krista@1812
   219
    // try again
krista@1812
   220
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   221
    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
   222
    cout << "---------------------------------------------------------" << endl << endl;
krista@1808
   223
    
krista@1812
   224
    // encrypt something to the key
krista@1812
   225
    cout << "Creating message…\n";
krista@2147
   226
    
krista@2147
   227
    // cout << "First, update identity though!\n";
krista@2147
   228
    // status = update_identity(session, recip1);
krista@1812
   229
    to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1812
   230
    outgoing_msg = new_message(PEP_dir_outgoing);
krista@1812
   231
    assert(outgoing_msg);
krista@1812
   232
    outgoing_msg->from = identity_dup(me);
krista@1812
   233
    outgoing_msg->to = to_list;
krista@1812
   234
    outgoing_msg->shortmsg = strdup("Greetings, humans!");
krista@1812
   235
    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1871
   236
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1812
   237
    cout << "Message created.\n";
krista@1808
   238
krista@1812
   239
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@2461
   240
    PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@2147
   241
    
krista@1812
   242
krista@1812
   243
    // CHECK STATUS???
krista@1812
   244
    cout << "Encryption returns with status " << tl_status_string(status) << endl;
krista@1812
   245
krista@1812
   246
    // check comm_type
krista@1812
   247
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@1812
   248
    assert(ct == PEP_ct_OpenPGP_unconfirmed);
krista@1812
   249
    
krista@1812
   250
    status = get_trust(session, recip1);
krista@1812
   251
krista@2461
   252
    cout << "Recip's trust DB comm_type (should be unknown, as we're using a keyring-only key, not in DB) = " << hex << tl_ct_string(recip1->comm_type) << endl;
krista@2461
   253
    assert(recip1->comm_type != PEP_ct_OpenPGP_unconfirmed);
krista@1812
   254
krista@1812
   255
    // decrypt message
krista@1812
   256
//    free_message(outgoing_msg);
krista@1812
   257
//    outgoing_msg = NULL;
krista@1812
   258
krista@1812
   259
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   260
    cout << "3c. Decrypt... that... message!" << endl;
krista@1812
   261
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   262
krista@1812
   263
krista@2615
   264
    flags = 0;
krista@1812
   265
    status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
krista@1812
   266
    cout << "Decryption returns with status " << tl_status_string(status) << endl;
krista@1812
   267
    assert(status == PEP_STATUS_OK);
krista@2147
   268
    assert(decrypted_msg);
krista@2147
   269
    
krista@1808
   270
    // check rating
krista@1812
   271
    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
krista@1812
   272
    assert(rating == PEP_rating_reliable);
krista@1808
   273
krista@2147
   274
    status = update_identity(session, decrypted_msg->to->ident);
krista@1812
   275
    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@1808
   276
krista@1812
   277
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@1812
   278
    assert(ct == PEP_ct_OpenPGP_unconfirmed);
krista@1808
   279
    
krista@1812
   280
    status = get_trust(session, recip1);
krista@1812
   281
    
krista@2461
   282
    cout << "Recip's trust DB comm_type (should be unknown - there's nothing in the DB) = " << hex << tl_ct_string(recip1->comm_type) << endl;
krista@2461
   283
    assert(recip1->comm_type == PEP_ct_unknown);
krista@1808
   284
krista@1812
   285
    free_message(encrypted_outgoing_msg);
krista@1812
   286
    free_message(decrypted_msg);
krista@1812
   287
    free_message(outgoing_msg);
krista@1812
   288
    outgoing_msg = NULL;
krista@1812
   289
    decrypted_msg = NULL;
krista@1812
   290
    encrypted_outgoing_msg = NULL;
krista@1808
   291
krista@1812
   292
    free_identity(me);
krista@1812
   293
    free_identity(recip1);
krista@1812
   294
    free(uniqname);
krista@1808
   295
    
krista@1812
   296
    delete_keypair(session, fprs[0]);    
krista@1812
   297
    delete_keypair(session, fprs[1]);
krista@1808
   298
    
krista@1812
   299
    free(fprs[0]);
krista@1812
   300
    free(fprs[1]);
krista@1808
   301
    
krista@1808
   302
#else
krista@1808
   303
    cout << "Sorry, test is not defined for NETPGP at this time." << endl;
krista@1808
   304
    
krista@1808
   305
#endif
krista@1808
   306
    
krista@1808
   307
    release(session);
krista@1808
   308
krista@1808
   309
    return 0;
krista@1808
   310
}