vb@1517
|
1 |
// This file is under GNU General Public License 3.0
|
vb@1517
|
2 |
// see LICENSE.txt
|
vb@1517
|
3 |
|
roker@748
|
4 |
#include <iostream>
|
roker@748
|
5 |
#include <fstream>
|
roker@751
|
6 |
#include <stdexcept>
|
roker@748
|
7 |
#include <string>
|
roker@751
|
8 |
#include <vector>
|
roker@748
|
9 |
|
roker@748
|
10 |
#include <assert.h>
|
roker@748
|
11 |
#include <string.h>
|
roker@748
|
12 |
|
roker@748
|
13 |
#include "../src/pEpEngine.h"
|
roker@748
|
14 |
#include "../src/keymanagement.h"
|
roker@748
|
15 |
|
roker@751
|
16 |
|
roker@748
|
17 |
using namespace std;
|
roker@748
|
18 |
|
roker@753
|
19 |
typedef std::string Buffer;
|
roker@751
|
20 |
|
roker@751
|
21 |
// no C++11, yet? So do our own implementation:
|
roker@751
|
22 |
namespace{
|
roker@751
|
23 |
std::string to_string(unsigned long u)
|
roker@751
|
24 |
{
|
roker@751
|
25 |
char buf[32];
|
roker@751
|
26 |
snprintf(buf,31, "%lu", u);
|
roker@751
|
27 |
return buf;
|
roker@751
|
28 |
}
|
roker@1710
|
29 |
|
roker@1710
|
30 |
std::string status(PEP_STATUS status)
|
roker@1710
|
31 |
{
|
roker@1710
|
32 |
char buf[32] = {0};
|
roker@1710
|
33 |
if(status==0)
|
roker@1710
|
34 |
{
|
roker@1710
|
35 |
return "PEP_STATUS_OK";
|
roker@1710
|
36 |
}else{
|
roker@1710
|
37 |
if(status>0)
|
roker@1710
|
38 |
{
|
roker@1710
|
39 |
snprintf(buf,31, "%u (0x%x)", status, status);
|
roker@1710
|
40 |
}else{
|
roker@1710
|
41 |
snprintf(buf,31, "%d", status);
|
roker@1710
|
42 |
}
|
roker@1710
|
43 |
}
|
roker@1710
|
44 |
return buf;
|
roker@1710
|
45 |
}
|
roker@1710
|
46 |
|
roker@1710
|
47 |
} // end of anonymous namespace
|
roker@751
|
48 |
|
roker@751
|
49 |
|
roker@751
|
50 |
Buffer ReadFileIntoMem(const char *fname){
|
roker@750
|
51 |
cout << "opening " << fname << " for reading\n";
|
roker@750
|
52 |
ifstream txtFile (fname, ifstream::binary);
|
roker@750
|
53 |
assert(txtFile.is_open());
|
roker@751
|
54 |
if (!txtFile)
|
roker@751
|
55 |
{
|
roker@751
|
56 |
throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
|
roker@748
|
57 |
}
|
roker@748
|
58 |
|
roker@751
|
59 |
Buffer buffer;
|
roker@751
|
60 |
|
roker@751
|
61 |
// get length of file:
|
roker@751
|
62 |
txtFile.seekg (0, txtFile.end);
|
roker@751
|
63 |
const size_t length = txtFile.tellg();
|
roker@751
|
64 |
txtFile.seekg (0, txtFile.beg);
|
roker@753
|
65 |
buffer.resize(length);
|
roker@751
|
66 |
|
roker@751
|
67 |
cout << "Reading " << length << " characters... ";
|
roker@753
|
68 |
txtFile.read (&buffer[0], length);
|
roker@751
|
69 |
|
roker@751
|
70 |
if (!txtFile)
|
roker@751
|
71 |
{
|
roker@751
|
72 |
throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
|
roker@751
|
73 |
}
|
roker@751
|
74 |
|
roker@751
|
75 |
cout << "all characters read successfully." << std::endl;
|
roker@751
|
76 |
return buffer;
|
roker@748
|
77 |
}
|
roker@748
|
78 |
|
roker@750
|
79 |
|
roker@748
|
80 |
int main(int argc, char* argv[])
|
roker@748
|
81 |
{
|
roker@750
|
82 |
PEP_SESSION session;
|
roker@748
|
83 |
|
roker@750
|
84 |
cout << "calling init()\n";
|
roker@750
|
85 |
PEP_STATUS init_result = init(&session);
|
roker@750
|
86 |
|
roker@1710
|
87 |
cout << "returning from init() with result == " << status(init_result) << endl;
|
roker@750
|
88 |
assert(init_result == PEP_STATUS_OK);
|
roker@748
|
89 |
|
roker@748
|
90 |
PEP_SESSION second_session;
|
roker@748
|
91 |
cout << "second session test\n";
|
roker@748
|
92 |
PEP_STATUS second_init_result = init(&second_session);
|
roker@1710
|
93 |
cout << "returning from second init() with result == " << status(second_init_result) << endl;
|
roker@748
|
94 |
assert(second_init_result == PEP_STATUS_OK);
|
roker@748
|
95 |
assert(second_session);
|
roker@748
|
96 |
cout << "dropping second session\n";
|
roker@750
|
97 |
release(second_session);
|
roker@748
|
98 |
|
roker@750
|
99 |
cout << "logging test\n";
|
krista@1141
|
100 |
log_event(session, "log test", "pEp Engine Test", "This is a logging test sample.", "please ignore this line");
|
roker@748
|
101 |
|
roker@748
|
102 |
// Our test user :
|
roker@748
|
103 |
// pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
|
krista@1141
|
104 |
// 6FF00E97 -- won't work as search term with NetPGP
|
krista@1141
|
105 |
// A9411D176FF00E97 -- won't work as search term with NetPGP
|
krista@1141
|
106 |
// 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 -- full key fingerprint
|
roker@748
|
107 |
//
|
roker@748
|
108 |
// Other peers :
|
roker@748
|
109 |
// pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
|
krista@1141
|
110 |
// C9C2EE39 -- won't work as search term with NetPGP
|
krista@1141
|
111 |
// 59BFF488C9C2EE39 -- won't work as search term with NetPGP
|
krista@1141
|
112 |
// BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 -- full key fingerprint
|
roker@748
|
113 |
//
|
roker@748
|
114 |
// pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
|
krista@1141
|
115 |
// 70DCF575 -- won't work as search term with NetPGP
|
krista@1141
|
116 |
// 135CD6D170DCF575 -- won't work as search term with NetPGP
|
krista@1141
|
117 |
// AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 -- full key fingerprint
|
roker@748
|
118 |
|
roker@748
|
119 |
const char *kflist[] = {
|
roker@748
|
120 |
"0x6FF00E97.asc",
|
roker@748
|
121 |
"0xC9C2EE39.asc",
|
roker@748
|
122 |
"0x70DCF575.asc",
|
roker@748
|
123 |
NULL
|
roker@748
|
124 |
};
|
roker@750
|
125 |
|
roker@748
|
126 |
const char** kf = kflist;
|
roker@748
|
127 |
while(*kf){
|
roker@751
|
128 |
const Buffer k_user_buffer = ReadFileIntoMem(*kf);
|
roker@748
|
129 |
cout << "import_key(" << *kf << ")\n";
|
roker@752
|
130 |
PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
|
roker@748
|
131 |
assert(import_status == PEP_STATUS_OK);
|
roker@748
|
132 |
cout << "successfully imported key\n";
|
roker@748
|
133 |
kf++;
|
roker@748
|
134 |
}
|
roker@748
|
135 |
|
roker@752
|
136 |
const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
|
roker@751
|
137 |
cout << "\n" << cipher_buffer.data();
|
roker@748
|
138 |
|
roker@750
|
139 |
char *buf_text = NULL;
|
roker@750
|
140 |
size_t buf_size = 0;
|
roker@750
|
141 |
stringlist_t *keylist;
|
roker@748
|
142 |
|
roker@748
|
143 |
cout << "calling decrypt_and_verify()\n";
|
krista@1397
|
144 |
PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), NULL, 0, &buf_text, &buf_size, &keylist);
|
roker@748
|
145 |
|
roker@1710
|
146 |
cout << "returning from decrypt_and_verify() with result == " << status(decrypt_result) << endl;
|
roker@748
|
147 |
assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
|
roker@748
|
148 |
assert(buf_text);
|
roker@748
|
149 |
assert(keylist);
|
roker@748
|
150 |
|
roker@748
|
151 |
for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
|
roker@748
|
152 |
assert(_keylist->value);
|
roker@1710
|
153 |
cout << "signed with " << _keylist->value << endl;
|
roker@748
|
154 |
}
|
roker@748
|
155 |
|
roker@748
|
156 |
free_stringlist(keylist);
|
roker@748
|
157 |
buf_text[buf_size] = 0;
|
roker@752
|
158 |
const string plain(buf_text);
|
roker@748
|
159 |
pEp_free(buf_text);
|
roker@748
|
160 |
cout << "\n" << plain;
|
roker@748
|
161 |
|
roker@752
|
162 |
const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
|
roker@752
|
163 |
const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
|
roker@748
|
164 |
|
roker@748
|
165 |
cout << "\ncalling verify_text()\n";
|
roker@752
|
166 |
PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
|
roker@1710
|
167 |
cout << "returning from verify_text() with result == " << status(verify_result) << endl;
|
roker@748
|
168 |
assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
|
roker@748
|
169 |
assert(keylist->value);
|
roker@1710
|
170 |
cout << "signed with " << keylist->value << endl;
|
roker@748
|
171 |
free_stringlist(keylist);
|
roker@748
|
172 |
|
roker@752
|
173 |
const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
|
roker@748
|
174 |
|
roker@748
|
175 |
cout << "\ncalling verify_text()\n";
|
roker@752
|
176 |
verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
|
roker@748
|
177 |
assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
|
roker@748
|
178 |
free_stringlist(keylist);
|
roker@748
|
179 |
|
krista@1141
|
180 |
keylist = new_stringlist("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
|
krista@1141
|
181 |
stringlist_add(keylist, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39");
|
krista@1141
|
182 |
stringlist_add(keylist, "AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575");
|
roker@748
|
183 |
|
roker@750
|
184 |
buf_text = NULL;
|
roker@750
|
185 |
buf_size = 0;
|
roker@748
|
186 |
|
roker@748
|
187 |
cout << "\ncalling encrypt_and_sign()\n";
|
roker@748
|
188 |
PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
|
roker@1710
|
189 |
cout << "returning from encrypt_and_sign() with result == " << status(encrypt_result) << endl;
|
roker@748
|
190 |
assert(encrypt_result == PEP_STATUS_OK);
|
roker@748
|
191 |
free_stringlist(keylist);
|
roker@748
|
192 |
|
roker@752
|
193 |
buf_text[buf_size] = '\0';
|
roker@752
|
194 |
const string cipher2(buf_text);
|
roker@748
|
195 |
cout << "\n" << cipher2;
|
roker@748
|
196 |
pEp_free(buf_text);
|
roker@748
|
197 |
|
roker@750
|
198 |
cout << "\nfinding English trustword for 2342...\n";
|
roker@752
|
199 |
char * word = NULL;
|
roker@750
|
200 |
size_t wsize;
|
roker@750
|
201 |
trustword(session, 2342, "en", &word, &wsize);
|
roker@750
|
202 |
assert(word);
|
roker@1710
|
203 |
cout << "the English trustword for 2342 is " << word << endl;
|
Edouard@791
|
204 |
pEp_free(word);
|
Edouard@791
|
205 |
cout << "\nfinding French trustword for 2342...\n";
|
Edouard@791
|
206 |
trustword(session, 2342, "fr", &word, &wsize);
|
Edouard@791
|
207 |
assert(word);
|
roker@1710
|
208 |
cout << "the French trustword for 2342 is " << word << endl;
|
roker@748
|
209 |
pEp_free(word);
|
roker@748
|
210 |
|
roker@752
|
211 |
const string fingerprint = "4942 2235 FC99 585B 891C 6653 0C7B 109B FA72 61F7";
|
roker@752
|
212 |
char * words = NULL;
|
roker@748
|
213 |
|
roker@748
|
214 |
cout << "\nfinding German trustwords for " << fingerprint << "...\n";
|
roker@748
|
215 |
trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
|
roker@748
|
216 |
assert(words);
|
roker@1710
|
217 |
cout << words << endl;
|
roker@748
|
218 |
pEp_free(words);
|
roker@748
|
219 |
|
roker@751
|
220 |
pEp_identity* identity = new_identity(
|
roker@748
|
221 |
"leon.schumacher@digitalekho.com",
|
roker@748
|
222 |
"8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
|
roker@748
|
223 |
"23",
|
roker@748
|
224 |
"Leon Schumacher"
|
roker@748
|
225 |
);
|
roker@750
|
226 |
identity->comm_type = PEP_ct_pEp;
|
roker@748
|
227 |
|
roker@750
|
228 |
cout << "\nsetting identity...\n";
|
roker@750
|
229 |
PEP_STATUS pep_set_result = set_identity(session, identity);
|
roker@750
|
230 |
assert(pep_set_result == PEP_STATUS_OK);
|
roker@748
|
231 |
free_identity(identity);
|
roker@750
|
232 |
get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
|
roker@750
|
233 |
assert(identity);
|
roker@1710
|
234 |
cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << endl;
|
roker@748
|
235 |
|
roker@748
|
236 |
PEP_STATUS get_trust_result = get_trust(session, identity);
|
roker@748
|
237 |
assert(get_trust_result == PEP_STATUS_OK);
|
roker@1710
|
238 |
cout << "trust of " << identity->user_id << " is " << identity->comm_type << endl;
|
roker@748
|
239 |
|
roker@748
|
240 |
free_identity(identity);
|
roker@748
|
241 |
|
roker@748
|
242 |
cout << "\ngenerating key for testuser\n";
|
roker@748
|
243 |
identity = new_identity(
|
roker@748
|
244 |
"testuser@pibit.ch",
|
roker@748
|
245 |
NULL,
|
roker@748
|
246 |
"423",
|
roker@748
|
247 |
"Alfred E. Neuman"
|
roker@748
|
248 |
);
|
roker@750
|
249 |
|
roker@748
|
250 |
assert(identity);
|
roker@748
|
251 |
PEP_STATUS generate_status = generate_keypair(session, identity);
|
roker@1710
|
252 |
cout << "generate_keypair() exits with " << status(generate_status) << endl;
|
roker@748
|
253 |
assert(generate_status == PEP_STATUS_OK);
|
roker@1710
|
254 |
cout << "generated key is " << identity->fpr << endl;
|
roker@748
|
255 |
|
roker@752
|
256 |
const string key(identity->fpr);
|
roker@748
|
257 |
free_identity(identity);
|
roker@748
|
258 |
|
roker@752
|
259 |
char *key_data = NULL;
|
roker@752
|
260 |
size_t size = 0;
|
roker@748
|
261 |
|
roker@748
|
262 |
cout << "export_key()\n\n";
|
roker@748
|
263 |
PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
|
roker@1710
|
264 |
cout << "export_key() exits with " << status(export_status) << endl;
|
roker@748
|
265 |
assert(export_status == PEP_STATUS_OK);
|
roker@748
|
266 |
cout << key_data << "\n\n";
|
roker@748
|
267 |
|
roker@1710
|
268 |
cout << "deleting key pair " << key.c_str() << endl;
|
roker@748
|
269 |
PEP_STATUS delete_status = delete_keypair(session, key.c_str());
|
roker@1710
|
270 |
cout << "delete_keypair() exits with " << status(delete_status) << endl;
|
roker@748
|
271 |
assert(delete_status == PEP_STATUS_OK);
|
roker@748
|
272 |
|
roker@748
|
273 |
cout << "import_key()\n";
|
roker@748
|
274 |
PEP_STATUS import_status = import_key(session, key_data, size, NULL);
|
roker@748
|
275 |
assert(import_status == PEP_STATUS_OK);
|
roker@748
|
276 |
cout << "successfully imported key\n";
|
roker@748
|
277 |
|
roker@748
|
278 |
pEp_free(key_data);
|
roker@752
|
279 |
key_data=NULL;
|
roker@748
|
280 |
|
roker@748
|
281 |
cout << "deleting key " << key.c_str() << " again\n";
|
roker@748
|
282 |
delete_status = delete_keypair(session, key.c_str());
|
roker@1710
|
283 |
cout << "delete_keypair() exits with " << status(delete_status) << endl;
|
roker@748
|
284 |
assert(delete_status == PEP_STATUS_OK);
|
roker@748
|
285 |
|
roker@748
|
286 |
cout << "finding key for pep.test.john@pep-project.org\n";
|
roker@748
|
287 |
PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
|
roker@1710
|
288 |
cout << "find_keys() exits with " << status(find_keys_status) << endl;
|
roker@748
|
289 |
assert(find_keys_status == PEP_STATUS_OK);
|
roker@748
|
290 |
assert(keylist);
|
roker@1710
|
291 |
cout << "found: " << keylist->value << endl;
|
roker@748
|
292 |
assert(keylist->next == NULL);
|
roker@748
|
293 |
free_stringlist(keylist);
|
roker@748
|
294 |
|
roker@748
|
295 |
cout << "searching for vb@ulm.ccc.de on keyserver\n";
|
roker@748
|
296 |
PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
|
roker@1710
|
297 |
cout << "recv_key() exits with " << status(recv_key_status) << endl;
|
roker@748
|
298 |
assert(recv_key_status == PEP_STATUS_OK);
|
roker@748
|
299 |
|
roker@748
|
300 |
cout << "sending vb@ulm.ccc.de to keyserver\n";
|
roker@748
|
301 |
PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
|
roker@1710
|
302 |
cout << "send_key() exits with " << status(send_key_status) << endl;
|
roker@748
|
303 |
assert(send_key_status == PEP_STATUS_OK);
|
roker@748
|
304 |
|
roker@748
|
305 |
PEP_comm_type tcomm_type;
|
krista@1142
|
306 |
PEP_STATUS tstatus = get_key_rating(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &tcomm_type);
|
roker@1710
|
307 |
cout << "get_key_rating() exits with " << tstatus << endl;
|
roker@748
|
308 |
assert(tstatus == PEP_STATUS_OK);
|
roker@748
|
309 |
assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
|
roker@748
|
310 |
|
roker@750
|
311 |
cout << "\ncalling release()\n";
|
krista@1011
|
312 |
cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;
|
krista@1011
|
313 |
|
roker@750
|
314 |
release(session);
|
roker@750
|
315 |
return 0;
|
roker@748
|
316 |
}
|