-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
621 lines (479 loc) · 18.1 KB
/
main.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
The MIT License (MIT)
Copyright (c) 2014 - Commonwealth of Australia (Represented by the Department of Defence)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include <ios>
#include <fstream>
#include <iomanip>
#include <map>
#include "PCSC.h"
#include "PKCS11Manager.h"
#include "PKCS11Object.h"
#include "Options.h"
#include "Utility.h"
#include "Log.h"
/*
* Application Variables
*/
// The PKCS11 Object
PKCS11Manager * m_PKCS11 = NULL;
// Holds a list of GlobalPlatform CPLC ICC Serial Numbers associated with the token in each slot
map<CK_ULONG, string> m_SlotSerials;
// Global - The number of iterations performed so far
int _iterations = 0;
// Global - A flag set by the Control Handler to indicate the application should cancel after the next processing cycle.
bool _shutdown = false;
// Options instance
Options _options;
/*
* Function Prototypes
*/
// Initialises the PKCS11 library and enumerates the available slots/tokens
void Startup(vector<PKCS11Slot> * slots);
// Process a single iteration of the transaction simulation against all available tokens
void Process(vector<PKCS11Slot> * slots);
// Reads the GlobalPlatform CPLC information via the PC/SC interface
string Process_FindSerial(PKCS11Slot * slot);
// Gets the Key object handle identifier for the PUBLIC key
CK_OBJECT_HANDLE Process_FindPublicKey(PKCS11Slot * slot);
// Gets the Key object handle identifier for the PRIVATE key
CK_OBJECT_HANDLE Process_FindPrivateKey(PKCS11Slot * slot);
// Release all application resources
void Shutdown();
// Handler for the atexit() call
void Shutdown_AtExit();
// Shows application version information on the command-line
void DisplayVersion();
// Shows application usage information on the command-line
void DisplayUsage();
// Writes to the token log file
void AppendJournal( string serial, char * operation, bool outcome, char * data, int len);
static BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType);
/*
* Global application definitions
*/
#define APP_NAME "PKCS11 Load Test Tool"
#define APP_VERSION "V1.0.1"
int _tmain(int argc, _TCHAR* argv[])
{
/*
* APPLICATION MAIN
*/
// Handle CTRL-C + CTRL-BRK
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
// Set the locale for string conversion
setlocale(LC_CTYPE, "");
// Set the logging level
Log::setLevel(LOG_INFO);
// Define the at-exit function
atexit(Shutdown_AtExit);
// Process command-line arguments
if (!_options.Parse(argc, argv)) {
DisplayUsage();
return (EXIT_FAILURE);
}
// Validate Arguments
// MANDATORY - PKCS11 Libary
if (_options.PKCS11Library.empty()) {
Log::error("A PKCS11 library must be supplied using the -L argument.\n");
exit(EXIT_FAILURE);
}
// MANDATORY - PIN
if (_options.PIN.empty()) {
Log::error("A valid, numeric PIN must be supplied using the -P argument.\n");
exit(EXIT_FAILURE);
}
// MANDATORY - KEY ID
if (0 == _options.KeyIdLength) {
Log::error("A valid, hexidecimal key identifier must supplied using the -K argument.\n");
exit(EXIT_FAILURE);
}
// Holds the list of detected PKCS11 Slots
vector<PKCS11Slot> slots;
// Call Startup
Startup(&slots);
// Loop
_iterations = 0;
while (_iterations < _options.MaxIterations) {
// Increment the iteration count
_iterations++;
// Call Process
Log::info ("ITERATION %d of %d:\n", _iterations, _options.MaxIterations);
try
{
Process(&slots);
}
catch (...) {
Log::error("Unhandled exception during processing ...\n");
}
// Wait for the next round
if (_shutdown) {
Log::info("Skipping further load test iterations\n");
break;
}
if (_iterations < _options.MaxIterations) {
Log::info ("Iteration complete, waiting for %dms.\n", _options.Interval);
Sleep(_options.Interval);
}
}
Log::info("LOAD TEST COMPLETE\n");
// Call Shutdown
Shutdown();
}
void Startup(vector<PKCS11Slot> * slots)
{
// Create the PKCS11 manager object
try {
m_PKCS11 = PKCS11Manager::Create(_options.PKCS11Library.c_str());
}
catch (...) {
Log::error("Unable to connect to the PKCS11 library, aborting ...\n");
exit(EXIT_FAILURE);
}
// Print PKCS11 Module Info
// TODO:
// Enumerate all slots WITH tokens
try {
m_PKCS11->QuerySlots(true, slots);
}
catch (...) {
Log::error("Unable to list the available slots, aborting ...\n");
exit(EXIT_FAILURE);
}
// If there are not slots with a token, shut down
if (slots->size() == 0) {
Log::error("You must have at least one slot available with a token present.\n");
exit(EXIT_FAILURE);
}
// Print slot info
for(vector<PKCS11Slot>::iterator slot = slots->begin();
slot != slots->end();
++slot)
{
PKCS11Token token;
slot->QueryToken(&token);
// Print slot / token information
Log::info("FOUND SLOT ID = %u, DESCRIPTION = '%s', TOKEN = '%s' \n", slot->id, slot->description.c_str(), token.label.c_str());
try {
slot->OpenSession(false);
string serial = Process_FindSerial(&(*slot));
try {
string csn = PCSC::QueryCPLC(slot->description);
m_SlotSerials[slot->id] = csn;
Log::info("Matched serial %s to CPLC ICC CSN %u. Using CSN\n", serial.c_str(), csn);
}
catch (...) {
Log::error("Unable to retrieve CPLC, using certificate serial number %s\n", serial.c_str());
m_SlotSerials[slot->id] = serial;
}
} catch ( ... ) {
Log::error("Unable to retrieve serial number for slot %u\n", slot->id);
slot->CloseSession();
exit(EXIT_FAILURE);
}
slot->CloseSession();
}
}
string Process_FindSerial(PKCS11Slot * slot) {
try {
// Create the search template
CK_OBJECT_CLASS classValue = CKO_CERTIFICATE;
CK_ATTRIBUTE attributes[] = {
{ CKA_CLASS, &classValue, sizeof(CK_OBJECT_CLASS) }
,{ CKA_ID, &_options.KeyId, _options.KeyIdLength }
};
// Search
vector<CK_OBJECT_HANDLE> handles;
slot->QueryObjects(&handles, attributes, 2);
if (handles.size() == 0) {
// Try again with only the CKO_CERTIFICATE
slot->QueryObjects(&handles, attributes, 1);
if (handles.size() == 0) {
throw "Certificate was not found, please check Key Id.";
}
else {
Log::warn("Certificate with supplied Key Identifier not found, defaulting to first available.\n");
slot->QueryObject(handles[0], attributes, 2);
Log::warn("Found first certificate using Key Identifier %s\n", Utility::ArraytoHexString(_options.KeyId, _options.KeyIdLength).c_str());
}
}
// Get the data for the certificate
// NOTE: Currently assuming it is X.509!
PKCS11X509CertificateObject * cert = dynamic_cast<PKCS11X509CertificateObject*>(PKCS11Object::Create(slot, handles[0]));
string result = cert->getSerialString();
delete cert;
return result;
}
catch (...) {
throw "Exception thrown whilst trying to query the private key handle.";
}
}
CK_OBJECT_HANDLE Process_FindPrivateKey(PKCS11Slot * slot) {
try {
// Create the search template
CK_OBJECT_CLASS classValue = CKO_PRIVATE_KEY;
CK_ATTRIBUTE attributes[] = {
{ CKA_CLASS, &classValue, sizeof(CK_OBJECT_CLASS) }
,{ CKA_ID, &_options.KeyId, _options.KeyIdLength }
};
// Search
vector<CK_OBJECT_HANDLE> handles;
slot->QueryObjects(&handles, attributes, 2);
if (handles.size() == 0) {
throw "Private key was not found";
} else {
// Return the first one
return handles[0];
}
}
catch (...) {
throw "Exception thrown whilst trying to query the private key handle.";
}
}
CK_OBJECT_HANDLE Process_FindPublicKey(PKCS11Slot * slot) {
try {
// Create the search template
CK_OBJECT_CLASS classValue = CKO_PUBLIC_KEY;
CK_ATTRIBUTE attributes[] = {
{ CKA_CLASS, &classValue, sizeof(CK_OBJECT_CLASS) },
{ CKA_ID, &_options.KeyId, _options.KeyIdLength }
};
// Search
vector<CK_OBJECT_HANDLE> handles;
slot->QueryObjects(&handles, attributes, 2);
if (handles.size() == 0) {
throw "Public key was not found";
} else {
// Return the first one
return handles[0];
}
}
catch (...) {
throw "Exception thrown whilst trying to query the private key handle.";
}
}
void Process(vector<PKCS11Slot> * slots) {
// Loop through each slot with a token
for(vector<PKCS11Slot>::iterator slot = slots->begin();
slot != slots->end();
++slot)
{
// Retrieve the serial number associated with this slot
string serial = m_SlotSerials[slot->id];
Log::info("SERIAL: %s\n", serial.c_str());
// Open Session
slot->OpenSession(false);
CK_OBJECT_HANDLE privateKey, publicKey;
int dataLength = 128;
char * data = new char[dataLength];
int cipherTextLength = 256;
char * cipherText = new char[cipherTextLength];
int digestLength = 20;
char * digest = new char[digestLength];
int signatureLength = 512;
char * signature = new char[signatureLength];
// Login
try {
Log::info(" - Login ...");
string pin(_options.PIN.begin(), _options.PIN.end());
slot->Login(&pin);
AppendJournal(serial, "LOGIN", true, NULL, 0);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "LOGIN", false, NULL, 0);
slot->CloseSession();
continue;
}
// Find Private Key [x]
try {
Log::info(" - Find Private key ...");
privateKey = Process_FindPrivateKey((&(*slot)));
AppendJournal(serial, "FIND_KEY_PRIVATE", true, NULL, 0);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "FIND_KEY_PRIVATE", false, NULL, 0);
slot->CloseSession();
continue;
}
// Find Public Key [x]
try {
Log::info(" - Find Public key ...");
publicKey = Process_FindPublicKey((&(*slot)));
AppendJournal(serial, "FIND_KEY_PUBLIC", true, NULL, 0);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "FIND_KEY_PUBLIC", false, NULL, 0);
slot->CloseSession();
continue;
}
// Generate Random Data
try {
Log::info(" - Generate Random Data (%d bytes) ...", dataLength);
slot->GenerateRandom(data, dataLength);
AppendJournal(serial, "RANDOM", true, data, dataLength);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "RANDOM", false, NULL, 0);
slot->CloseSession();
continue;
}
// Encrypt (with Public Key)
try {
Log::info(" - Encrypt (%d bytes) ...", dataLength);
slot->EncryptData(publicKey, data, dataLength, cipherText, &cipherTextLength);
AppendJournal(serial, "ENCRYPT", true, cipherText, cipherTextLength);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "ENCRYPT", false, NULL, 0);
slot->CloseSession();
continue;
}
// Digest
try {
Log::info(" - Digest (%d bytes) ...", dataLength);
slot->GenerateDigest(cipherText, cipherTextLength, digest, &digestLength);
AppendJournal(serial, "DIGEST", true, NULL, 0);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "DIGEST", false, NULL, 0);
slot->CloseSession();
continue;
}
// Sign
try {
Log::info(" - Sign (%d bytes) ...", digestLength);
slot->GenerateSignature(privateKey, digest, digestLength, signature, &signatureLength);
AppendJournal(serial, "SIGN", true, signature, signatureLength);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "SIGN", false, NULL, 0);
slot->CloseSession();
continue;
}
// Verify
try {
Log::info(" - Verify Signature (%d bytes) ...", signatureLength);
slot->VerifySignature(publicKey, digest, digestLength, signature, signatureLength);
AppendJournal(serial, "VERIFY", true, NULL, NULL);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "VERIFY", false, NULL, 0);
slot->CloseSession();
continue;
}
// Decrypt (with Private Key)
try {
Log::info(" - Decrypt (%d bytes) ...", cipherTextLength);
slot->DecryptData(privateKey, cipherText, cipherTextLength, data, &dataLength);
AppendJournal(serial, "DECRYPT", true, data, dataLength);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "DECRYPT", false, NULL, 0);
slot->CloseSession();
continue;
}
// Logout
try {
Log::info(" - Logout ...", cipherTextLength);
slot->Logout();
AppendJournal(serial, "LOGOUT", true, NULL, 0);
Log::info("Success\n");
} catch (...) {
Log::info("Failed\n");
AppendJournal(serial, "LOGOUT", false, NULL, 0);
}
// Close Session
slot->CloseSession();
}
}
void Shutdown() {
Log::debug("Shutdown: Complete\n");
}
void Shutdown_AtExit() {
if (m_PKCS11 != NULL) {
delete m_PKCS11;
}
Log::debug("Shutdown_AtExit: Complete\n");
}
void DisplayVersion()
{
Log::info("%s - %s\n", APP_NAME, APP_VERSION);
}
void DisplayUsage()
{
DisplayVersion();
cout << "Usage: " << _options.EXEName << " <-L library_path> <-P pin> [-C count] [-I interval] [-HD]" << endl << endl;
cout << " L : Sets the library path" << endl;
cout << " P : Sets the USER pin used for the PKCS#11 Login" << endl;
cout << " C : Sets the maximum iteration count (defaults to 9999999)" << endl;
cout << " I : Set the load testing interval in milliseconds (defaults to 1000)" << endl;
cout << " H : Show this usage description and exits" << endl;
cout << " D : Enabled debugging output" << endl << endl;
}
void AppendJournal( string serial, char * operation, bool outcome, char * data, int len ) {
// Generate the file path
string path = serial + ".log";
// Check if the file exists
ofstream o(path, ios_base::app | ios_base::out);
// Get the date/time
o << Utility::CurrentDateTime() << ",";
o << serial << "," << _iterations << "," << string(operation) << ",";
if (outcome) {
o << "SUCCESS";
} else {
o << "FAIL";
}
if (len != 0) {
o << "," << Utility::ArraytoHexString(data, len);
}
o << endl;
o.close();
}
static BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_C_EVENT: // Ctrl+C
Log::info("\nCTRL-C - Will shut down after this operation completes\n");
_shutdown = true;
return TRUE;
case CTRL_BREAK_EVENT: // Ctrl+Break
Log::info("\nCTRL-BRK - Will shut down after this operation completes\n");
_shutdown = true;
return TRUE;
case CTRL_CLOSE_EVENT: // Closing the console window
break;
}
// Return TRUE if handled this message, further handler functions won't be called.
// Return FALSE to pass this message to further handlers until default handler calls ExitProcess().
return FALSE;
}