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