forked from noloader/cryptopp-pem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpem_test.cxx
463 lines (393 loc) · 14.7 KB
/
pem_test.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// pem_test.cxx - PEM testing routines.
// Written and placed in the public domain by Jeffrey Walton
#include <string>
#include <iostream>
#include <cstdlib>
#include "cryptlib.h"
#include "integer.h"
#include "eccrypto.h"
#include "osrng.h"
#include "files.h"
#include "rsa.h"
#include "dsa.h"
#include "pem.h"
int main(int argc, char* argv[])
{
using namespace CryptoPP;
try
{
RSA::PublicKey k1;
RSA::PrivateKey k2, k3;
DSA::PublicKey k4;
DSA::PrivateKey k5, k6;
DL_GroupParameters_EC<ECP> p7;
DL_PublicKey_EC<ECP> k8;
DL_PrivateKey_EC<ECP> k9, k10;
// Read from OpenSSL generated key
{
std::cout << "Load RSA public key" << std::endl;
FileSource fs1("rsa-pub.pem", true);
PEM_Load(fs1, k1);
std::cout << " - OK" << std::endl;
std::cout << "Load RSA private key" << std::endl;
FileSource fs2("rsa-priv.pem", true);
PEM_Load(fs2, k2);
std::cout << " - OK" << std::endl;
std::cout << "Load encrypted RSA private key" << std::endl;
FileSource fs3("rsa-enc-priv.pem", true);
PEM_Load(fs3, k3, "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
std::cout << "Load DSA public key" << std::endl;
FileSource fs4("dsa-pub.pem", true);
PEM_Load(fs4, k4);
std::cout << " - OK" << std::endl;
std::cout << "Load DSA private key" << std::endl;
FileSource fs5("dsa-priv.pem", true);
PEM_Load(fs5, k5);
std::cout << " - OK" << std::endl;
std::cout << "Load encrypted DSA private key" << std::endl;
FileSource fs6("dsa-enc-priv.pem", true);
PEM_Load(fs6, k6, "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
std::cout << "Load ECP parameters" << std::endl;
FileSource fs7("ec-params.pem", true);
PEM_Load(fs7, p7);
std::cout << " - OK" << std::endl;
std::cout << "Load ECP public key" << std::endl;
FileSource fs8("ec-pub.pem", true);
PEM_Load(fs8, k8);
std::cout << " - OK" << std::endl;
std::cout << "Load ECP private key" << std::endl;
FileSource fs9("ec-priv.pem", true);
PEM_Load(fs9, k9);
std::cout << " - OK" << std::endl;
std::cout << "Load encrypted ECP private key" << std::endl;
FileSource fs10("ec-enc-priv.pem", true);
PEM_Load(fs10, k10, "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
}
// Write for OpenSSL to verify
{
AutoSeededRandomPool prng;
std::cout << "Save RSA public key" << std::endl;
FileSink fs1("rsa-pub.cryptopp.pem");
PEM_Save(fs1, k1);
std::cout << " - OK" << std::endl;
std::cout << "Save RSA private key" << std::endl;
FileSink fs2("rsa-priv.cryptopp.pem");
PEM_Save(fs2, k2);
std::cout << " - OK" << std::endl;
std::cout << "Save encrypted RSA private key" << std::endl;
FileSink fs3("rsa-enc-priv.cryptopp.pem");
PEM_Save(fs3, k3, prng, "AES-128-CBC", "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
std::cout << "Save DSA public key" << std::endl;
FileSink fs4("dsa-pub.cryptopp.pem");
PEM_Save(fs4, k4);
std::cout << " - OK" << std::endl;
std::cout << "Save DSA private key" << std::endl;
FileSink fs5("dsa-priv.cryptopp.pem");
PEM_Save(fs5, k5);
std::cout << " - OK" << std::endl;
std::cout << "Save encrypted DSA private key" << std::endl;
FileSink fs6("dsa-enc-priv.cryptopp.pem");
PEM_Save(fs6, k6, prng, "AES-128-CBC", "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
std::cout << "Save ECP parameters" << std::endl;
FileSink fs7("ec-params.cryptopp.pem", true);
PEM_Save(fs7, p7);
std::cout << " - OK" << std::endl;
std::cout << "Save ECP public key" << std::endl;
FileSink fs8("ec-pub.cryptopp.pem", true);
PEM_Save(fs8, k8);
std::cout << " - OK" << std::endl;
std::cout << "Save ECP private key" << std::endl;
FileSink fs9("ec-priv.cryptopp.pem", true);
PEM_Save(fs9, k9);
std::cout << " - OK" << std::endl;
std::cout << "Save encrypted ECP private key" << std::endl;
FileSink fs10("ec-enc-priv.cryptopp.pem", true);
PEM_Save(fs10, k10, prng, "AES-128-CBC", "abcdefghijklmnopqrstuvwxyz", 26);
std::cout << " - OK" << std::endl;
}
}
catch(const Exception& ex)
{
std::cout << "Caught exception: " << ex.what() << std::endl;
std::exit(1);
}
// Load malformed, missing final CRLF, should be OK
try
{
RSA::PublicKey k1;
std::cout << "Load malformed key 1" << std::endl;
FileSource fs1("rsa-trunc-1.pem", true);
PEM_Load(fs1, k1);
std::cout << " - OK" << std::endl;
}
catch(const Exception& ex)
{
std::cout << " - Failed" << std::endl;
std::exit(1);
}
// Load malformed, tampered encapsulation boundary
try
{
RSA::PublicKey k2;
std::cout << "Load malformed key 2" << std::endl;
FileSource fs2("rsa-trunc-2.pem", true);
PEM_Load(fs2, k2);
std::cout << " - Failed" << std::endl;
std::exit(1);
}
catch(const Exception& ex)
{
std::cout << " - OK" << std::endl;
}
// Load malformed, missing final CRLF, another key concat'd, should be OK
try
{
RSA::PublicKey k3;
std::cout << "Load malformed key 3" << std::endl;
FileSource fs3("rsa-concat.pem", true);
PEM_Load(fs3, k3);
std::cout << " - OK" << std::endl;
}
catch(const Exception& ex)
{
std::cout << " - Failed" << std::endl;
std::exit(1);
}
// Load malformed, only -----BEGIN RSA KEY-----
try
{
RSA::PublicKey k4;
std::cout << "Load malformed key 4" << std::endl;
FileSource fs4("rsa-short.pem", true);
PEM_Load(fs4, k4);
std::cout << " - Failed" << std::endl;
std::exit(1);
}
catch(const Exception& ex)
{
std::cout << " - OK" << std::endl;
}
// Load malformed, EOL is CR, should be OK
try
{
RSA::PublicKey k5;
std::cout << "Load malformed key 5" << std::endl;
FileSource fs5("rsa-eol-cr.pem", true);
PEM_Load(fs5, k5);
std::cout << " - OK" << std::endl;
}
catch(const Exception& ex)
{
std::cout << " - Failed" << std::endl;
std::exit(1);
}
// Load malformed, EOL is LF, should be OK
try
{
RSA::PublicKey k6;
std::cout << "Load malformed key 6" << std::endl;
FileSource fs6("rsa-eol-lf.pem", true);
PEM_Load(fs6, k6);
std::cout << " - OK" << std::endl;
}
catch(const Exception& ex)
{
std::cout << " - Failed" << std::endl;
std::exit(1);
}
// Load malformed, no EOL, should fail due
// to no EOL on encapsulation boundaries
try
{
RSA::PublicKey k7;
std::cout << "Load malformed key 7" << std::endl;
FileSource fs7("rsa-eol-none.pem", true);
PEM_Load(fs7, k7);
std::cout << " - Failed" << std::endl;
std::exit(1);
}
catch(const Exception& ex)
{
std::cout << " - OK" << std::endl;
}
// Load malformed, -----BEGIN FOO----- and -----END BAR-----
try
{
RSA::PublicKey k8;
std::cout << "Load malformed key 8" << std::endl;
FileSource fs8("foobar.pem", true);
PEM_Load(fs8, k8);
std::cout << " - Failed" << std::endl;
std::exit(1);
}
catch(const Exception& ex)
{
std::cout << " - OK" << std::endl;
}
// Read the Let's Encrypt cert for www.cryptopp.com
{
try {
std::cout << "\nLoad Let's Encrypt cert for www.cryptopp.com" << std::endl;
X509Certificate cert;
FileSource fs("www-cryptopp-com.cert.pem", true);
PEM_Load(fs, cert);
std::cout << " - OK" << std::endl;
std::cout << "\nDumping certificate:" << std::endl;
std::cout << cert << std::endl;
std::cout << "\nDumping identities:" << std::endl;
std::cout << cert.GetSubjectIdentities() << std::endl;
}
catch(const Exception& ex) {
std::cout << "Caught exception: " << ex.what() << std::endl;
std::exit(1);
}
}
// Read the OpenSSL generated self-signed end-entity cert for example.com
{
try {
std::cout << "\nLoad X.509 example-com.cert.pem certificate" << std::endl;
X509Certificate cert;
FileSource fs("example-com.cert.pem", true);
PEM_Load(fs, cert);
std::cout << " - OK" << std::endl;
std::cout << "\nDumping certificate:" << std::endl;
std::cout << cert << std::endl;
std::cout << "\nDumping identities:" << std::endl;
std::cout << cert.GetSubjectIdentities() << std::endl;
}
catch(const Exception& ex) {
std::cout << "Caught exception: " << ex.what() << std::endl;
std::exit(1);
}
}
// Malformed.
try
{
X509Certificate cert;
std::cout << "\nLoad malformed X.509 certificate" << std::endl;
const std::string pem =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIE+TCCA+GgAwIBAgIURIR"
"-----END CERTIFICATE-----\r\n";
ArraySource source(pem, true);
PEM_Load(source, cert);
std::cout << " - Failed" << std::endl;
std::exit(1);
}
catch(const Exception& ex)
{
std::cout << " - OK" << std::endl;
}
// Test cacert.pem from Mozilla.
// There should be ~130 to ~150 certs in it.
try
{
std::cout << "\nLoad root certificates from Mozilla" << std::endl;
FileSource fs("./cacert.pem", true);
ByteQueue t;
size_t count=0;
while (PEM_NextObject(fs, t))
{
count++; // 1-based
X509Certificate cert;
std::ostringstream oss;
try {
PEM_Load(t, cert);
oss << cert << std::endl;
}
catch (const Exception&) {
std::cerr << "Failed to parse certificate " << count << std::endl;
std::cerr << "\nWriting certificate badcert.der" << std::endl;
FileSink x("badcert.der");
cert.WriteCertificateBytes(x);
std::cerr << "\nDumping certificate" << std::endl;
std::cerr << oss.str() << std::endl;
throw;
}
AutoSeededRandomPool prng;
if (cert.Validate(prng, 2) == false)
{
std::ostringstream message;
message << "Failed to validate public key for " << cert.GetSubjectDistinguishedName();
std::cerr << message.str() << std::endl;;
std::cerr << "\nWriting certificate badcert.der" << std::endl;
FileSink x("badcert.der");
cert.WriteCertificateBytes(x);
std::cerr << "\nDumping certificate" << std::endl;
std::cerr << oss.str() << std::endl;
throw Exception(Exception::OTHER_ERROR, message.str());
}
}
if (count >= 120) {
std::cout << " - OK (" << count << " certificates)" << std::endl;
}
else {
std::cout << " - Failed (died at certificate " << count << ")" << std::endl;
std::exit(1);
}
}
catch(const Exception& ex)
{
std::cout << " - Exception: " << ex.what() << std::endl;
std::exit(1);
}
// Test roots.pem from Google.
// There should be ~35 to ~40 certs in it.
try
{
std::cout << "\nLoad root certificates from Google" << std::endl;
FileSource fs("./roots.pem", true);
ByteQueue t;
size_t count=0;
while (PEM_NextObject(fs, t))
{
count++; // 1-based
X509Certificate cert;
std::ostringstream oss;
try {
PEM_Load(t, cert);
oss << cert << std::endl;
}
catch (const Exception&) {
std::cerr << "Failed to parse certificate " << count << std::endl;
std::cerr << "\nWriting certificate badcert.der" << std::endl;
FileSink x("badcert.der");
cert.WriteCertificateBytes(x);
std::cerr << "\nDumping certificate" << std::endl;
std::cerr << oss.str() << std::endl;
throw;
}
AutoSeededRandomPool prng;
if (cert.Validate(prng, 2) == false)
{
std::ostringstream message;
message << "Failed to validate public key for " << cert.GetSubjectDistinguishedName();
std::cerr << message.str() << std::endl;;
std::cerr << "\nWriting certificate badcert.der" << std::endl;
FileSink x("badcert.der");
cert.WriteCertificateBytes(x);
std::cerr << "\nDumping certificate" << std::endl;
std::cerr << oss.str() << std::endl;
throw Exception(Exception::OTHER_ERROR, message.str());
}
}
if (count >= 30) {
std::cout << " - OK (" << count << " certificates)" << std::endl;
}
else {
std::cout << " - Failed (died at certificate " << count << ")" << std::endl;
std::exit(1);
}
}
catch(const Exception& ex)
{
std::cout << " - Exception: " << ex.what() << std::endl;
std::exit(1);
}
return 0;
}