-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCrescendoTutorial.cpp
More file actions
589 lines (520 loc) · 15.8 KB
/
Copy pathCrescendoTutorial.cpp
File metadata and controls
589 lines (520 loc) · 15.8 KB
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
#include <Windows.h>
#include <bcrypt.h>
#include <ncrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include "cardmod.h"
typedef DWORD(WINAPI *PACQUIRECONTEXT)(PCARD_DATA, DWORD);
HANDLE hHeap = NULL;
void CheckResult(const char* functionName, LONG lRet)
{
if (lRet != SCARD_S_SUCCESS)
{
fprintf(stderr, "%s error 0x%08lX\n", functionName, (unsigned long)lRet);
exit(EXIT_FAILURE);
}
}
LPVOID WINAPI CspAlloc(SIZE_T Size)
{
return HeapAlloc(hHeap, 0, Size);
}
LPVOID WINAPI CspReAlloc(LPVOID Address, SIZE_T Size)
{
return HeapReAlloc(hHeap, 0, Address, Size);
}
void WINAPI CspFree(LPVOID Address)
{
if (Address)
{
HeapFree(hHeap, 0, Address);
}
}
const char* PinIdToLabel(DWORD pinId)
{
switch (pinId)
{
case ROLE_USER:
return "ROLE_USER";
case ROLE_ADMIN:
return "ROLE_ADMIN";
default:
break;
}
static char labels[MAX_PINS][8];
if (pinId < MAX_PINS)
{
sprintf_s(labels[pinId], sizeof(labels[pinId]), "%lu", (unsigned long)pinId);
return labels[pinId];
}
return "Unknown";
}
const char* SecretTypeToString(SECRET_TYPE secretType)
{
switch (secretType)
{
case AlphaNumericPinType:
return "AlphaNumericPinType";
case ExternalPinType:
return "ExternalPinType";
case ChallengeResponsePinType:
return "ChallengeResponsePinType";
case EmptyPinType:
return "EmptyPinType";
default:
return "UnknownSecretType";
}
}
const char* SecretPurposeToString(SECRET_PURPOSE secretPurpose)
{
switch (secretPurpose)
{
case AuthenticationPin:
return "AuthenticationPin";
case DigitalSignaturePin:
return "DigitalSignaturePin";
case EncryptionPin:
return "EncryptionPin";
case NonRepudiationPin:
return "NonRepudiationPin";
case AdministratorPin:
return "AdministratorPin";
case PrimaryCardPin:
return "PrimaryCardPin";
case UnblockOnlyPin:
return "UnblockOnlyPin";
default:
return "UnknownSecretPurpose";
}
}
void FormatPinSet(PIN_SET pinSet, char* output, size_t outputSize)
{
bool first = true;
if (!output || outputSize == 0)
{
return;
}
output[0] = '\0';
for (DWORD i = 0; i < MAX_PINS; i++)
{
if (!IS_PIN_SET(pinSet, i))
{
continue;
}
if (!first)
{
strcat_s(output, outputSize, ",");
}
strcat_s(output, outputSize, PinIdToLabel(i));
first = false;
}
if (first)
{
strcpy_s(output, outputSize, "None");
}
}
void PrintPinInfo(int pinId, const PIN_INFO* pinInfo)
{
char changePermission[128] = { 0 };
char unblockPermission[128] = { 0 };
static bool headerPrinted = false;
if (!pinInfo)
{
return;
}
FormatPinSet(pinInfo->dwChangePermission, changePermission, sizeof(changePermission));
FormatPinSet(pinInfo->dwUnblockPermission, unblockPermission, sizeof(unblockPermission));
if (!headerPrinted)
{
printf("\n%-12s %-28s %-24s %-32s %-32s\n",
"PIN",
"Secret Type",
"Secret Purpose",
"Change Permission",
"Unblock Permission");
printf("%-12s %-28s %-24s %-32s %-32s\n",
"------------",
"----------------------------",
"------------------------",
"--------------------------------",
"--------------------------------");
headerPrinted = true;
}
printf("%-12s %-28s %-24s %-32s %-32s\n",
PinIdToLabel((DWORD)pinId),
SecretTypeToString(pinInfo->PinType),
SecretPurposeToString(pinInfo->PinPurpose),
changePermission,
unblockPermission);
}
LONG SymmetricChallengeResponse(
LPBYTE pbData,
DWORD cbData,
LPCWSTR pszAlgorithm,
const BYTE* pbKey,
DWORD cbKey,
LPCWSTR pszChainingMode,
const BYTE* pbIv,
DWORD cbIv);
LONG DesResponse(LPBYTE pbData, DWORD cbData)
{
const BYTE desKey[24] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
return SymmetricChallengeResponse(
pbData,
cbData,
BCRYPT_3DES_ALGORITHM,
desKey,
(DWORD)sizeof(desKey),
BCRYPT_CHAIN_MODE_ECB,
NULL,
0);
}
LONG SymmetricChallengeResponse(
LPBYTE pbData,
DWORD cbData,
LPCWSTR pszAlgorithm,
const BYTE* pbKey,
DWORD cbKey,
LPCWSTR pszChainingMode,
const BYTE* pbIv,
DWORD cbIv)
{
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_KEY_HANDLE hKey = NULL;
PBYTE pbKeyObject = NULL;
PBYTE pbLocData = NULL;
PBYTE pbIvLocal = NULL;
DWORD cbKeyObject = 0;
DWORD cbResult = 0;
NTSTATUS status = 0;
DWORD cbChainingMode = 0;
if (!pbData || cbData == 0 || !pszAlgorithm || !pbKey || cbKey == 0 || !pszChainingMode)
{
return ERROR_INVALID_PARAMETER;
}
if ((pbIv && cbIv == 0) || (!pbIv && cbIv != 0))
{
return ERROR_INVALID_PARAMETER;
}
pbLocData = (PBYTE)malloc(cbData);
if (!pbLocData)
{
return ERROR_OUTOFMEMORY;
}
memcpy(pbLocData, pbData, cbData);
if (pbIv && cbIv)
{
pbIvLocal = (PBYTE)malloc(cbIv);
if (!pbIvLocal)
{
status = ERROR_OUTOFMEMORY;
goto Cleanup;
}
memcpy(pbIvLocal, pbIv, cbIv);
}
status = BCryptOpenAlgorithmProvider(&hAlg, pszAlgorithm, NULL, 0);
if (status < 0)
{
goto Cleanup;
}
cbChainingMode = ((DWORD)lstrlenW(pszChainingMode) + 1) * sizeof(WCHAR);
status = BCryptSetProperty(
hAlg,
BCRYPT_CHAINING_MODE,
(PBYTE)pszChainingMode,
cbChainingMode,
0);
if (status < 0)
{
goto Cleanup;
}
status = BCryptGetProperty(
hAlg,
BCRYPT_OBJECT_LENGTH,
(PBYTE)&cbKeyObject,
sizeof(cbKeyObject),
&cbResult,
0);
if (status < 0)
{
goto Cleanup;
}
pbKeyObject = (PBYTE)malloc(cbKeyObject);
if (!pbKeyObject)
{
status = ERROR_OUTOFMEMORY;
goto Cleanup;
}
status = BCryptGenerateSymmetricKey(
hAlg,
&hKey,
pbKeyObject,
cbKeyObject,
(PBYTE)pbKey,
cbKey,
0);
if (status < 0)
{
goto Cleanup;
}
status = BCryptEncrypt(
hKey,
pbLocData,
cbData,
NULL,
pbIvLocal,
cbIv,
pbLocData,
cbData,
&cbResult,
0);
if (status < 0)
{
goto Cleanup;
}
if (cbResult != cbData)
{
status = ERROR_INVALID_DATA;
goto Cleanup;
}
memcpy(pbData, pbLocData, cbData);
Cleanup:
if (hKey)
{
BCryptDestroyKey(hKey);
}
if (pbKeyObject)
{
free(pbKeyObject);
}
if (hAlg)
{
BCryptCloseAlgorithmProvider(hAlg, 0);
}
if (pbIvLocal)
{
free(pbIvLocal);
}
if (pbLocData)
{
free(pbLocData);
}
return status;
}
LONG AesResponse(LPBYTE pbData, DWORD cbData)
{
const BYTE aesKey[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
BYTE iv[16] = { 0 };
return SymmetricChallengeResponse(
pbData,
cbData,
BCRYPT_AES_ALGORITHM,
aesKey,
(DWORD)sizeof(aesKey),
BCRYPT_CHAIN_MODE_CBC,
iv,
(DWORD)sizeof(iv));
}
int main()
{
NCRYPT_PROV_HANDLE hProv;
NCRYPT_KEY_HANDLE hKey;
SECURITY_STATUS lRet;
LPTSTR szPin = L"00000000";
LPBYTE pbRawKey;
DWORD cbRawKey;
LPBYTE pbSerial = NULL;
DWORD cbSerial = 0;
DWORD dwKeyLen = 2048;
SCARDCONTEXT hContext = 0;
BYTE pbAtr[36];
DWORD cbAtr = 36;
LPTSTR szCards = NULL;
DWORD cchCards = 0;
LPTSTR szProvider = NULL;
DWORD cchProvider = 0;
HMODULE hDriver = NULL;
PACQUIRECONTEXT pCardAcquireContext = NULL;
CARD_DATA cardData;
LPBYTE ppbChallenge;
DWORD cbChallenge;
DWORD cbRemaining = 0;
/*
* For PIN management functions, we go through the minidriver
*/
hHeap = HeapCreate(0, 0x00010000, 0x0010000);
lRet = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hContext);
CheckResult("SCardEstablishContext", lRet);
LPOPENCARDNAME_EXW ocn = new OPENCARDNAME_EXW();
ocn->dwFlags = SC_DLG_MINIMAL_UI;
ocn->hSCardContext = hContext;
ocn->dwShareMode = SCARD_SHARE_SHARED;
ocn->dwPreferredProtocols = SCARD_PROTOCOL_Tx;
ocn->lpstrRdr = (LPWSTR)malloc(sizeof(wchar_t) * 255);
ocn->nMaxRdr = 255;
ocn->lpstrCard = (LPWSTR)malloc(sizeof(wchar_t) * 255);
ocn->nMaxCard = 255;
ocn->dwStructSize = sizeof(OPENCARDNAME_EXW);
lRet = SCardUIDlgSelectCard(ocn);
CheckResult("SCardUIDlgSelectCard", lRet);
lRet = SCardGetAttrib(ocn->hCardHandle, SCARD_ATTR_ATR_STRING, pbAtr, &cbAtr);
CheckResult("SCardGetAttrib", lRet);
lRet = SCardListCards(hContext, pbAtr, NULL, 0, NULL, &cchCards);
CheckResult("SCardListCards", lRet);
szCards = (LPTSTR)CspAlloc(cchCards * sizeof(TCHAR));
lRet = SCardListCards(hContext, pbAtr, NULL, 0, szCards, &cchCards);
CheckResult("SCardListCards", lRet);
lRet = SCardGetCardTypeProviderName(hContext, szCards, 0x80000001, NULL, &cchProvider);
CheckResult("SCardGetCardTypeProviderName", lRet);
szProvider = (LPTSTR)CspAlloc(cchProvider * sizeof(TCHAR));
lRet = SCardGetCardTypeProviderName(hContext, szCards, 0x80000001, szProvider, &cchProvider);
CheckResult("SCardGetCardTypeProviderName", lRet);
hDriver = LoadLibrary(szProvider);
pCardAcquireContext = (PACQUIRECONTEXT)GetProcAddress(hDriver, "CardAcquireContext");
memset(&cardData, 0, sizeof(CARD_DATA));
cardData.dwVersion = CARD_DATA_CURRENT_VERSION;
cardData.pbAtr = pbAtr;
cardData.cbAtr = cbAtr;
cardData.pwszCardName = szCards;
// Memory management functions
cardData.pfnCspAlloc = (PFN_CSP_ALLOC)CspAlloc;
cardData.pfnCspReAlloc = (PFN_CSP_REALLOC)CspReAlloc;
cardData.pfnCspFree = (PFN_CSP_FREE)CspFree;
cardData.hSCardCtx = hContext;
cardData.hScard = ocn->hCardHandle;
lRet = (pCardAcquireContext)(&cardData, 0);
CheckResult("CardAcquireContext", lRet);
// Verify User PIN
BYTE pbPin[] = { 0x34, 0x30, 0x34, 0x31, 0x34, 0x32, 0x34, 0x33 };
DWORD cbPin = 8;
lRet = (cardData.pfnCardAuthenticatePin)(&cardData, wszCARD_USER_USER, pbPin, cbPin, &cbRemaining);
CheckResult("CardAuthenticatePin", lRet);
// Change User PIN
BYTE pbNewPin[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
DWORD cbNewPin = 8;
lRet = (cardData.pfnCardChangeAuthenticatorEx)(&cardData, PIN_CHANGE_FLAG_CHANGEPIN, ROLE_USER, pbPin, cbPin, ROLE_USER, pbNewPin, cbNewPin, 0, &cbRemaining);
//lRet = (cardData.pfnCardChangeAuthenticator)(&cardData, wszCARD_USER_USER, pbPin, cbPin, pbNewPin, cbNewPin, 0, PIN_CHANGE_FLAG_CHANGEPIN, &cbRemaining);
CheckResult("CardChangeAuthenticatorEx", lRet);
BYTE pbNewKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
DWORD cbNewKey = 16;
lRet = (cardData.pfnCardChangeAuthenticatorEx)(&cardData, PIN_CHANGE_FLAG_UNBLOCK, ROLE_USER, pbNewPin, cbNewPin, ROLE_ADMIN, pbNewKey, cbNewKey, 0, &cbRemaining);
CheckResult("CardChangeAuthenticatorEx", lRet);
// Challenge / response authentication
lRet = (cardData.pfnCardGetChallenge)(&cardData, &ppbChallenge, &cbChallenge);
CheckResult("CardGetChallenge", lRet);
if (cbChallenge == 8) {
lRet = DesResponse(ppbChallenge, cbChallenge);
CheckResult("DesResponse", lRet);
}
else {
lRet = AesResponse(ppbChallenge, cbChallenge);
CheckResult("AesResponse", lRet);
}
lRet = (cardData.pfnCardAuthenticateChallenge)(&cardData, ppbChallenge, cbChallenge, &cbRemaining);
CheckResult("CardAuthenticateChallenge", lRet);
(cardData.pfnCspFree)(ppbChallenge);
// Change administration key
lRet = (cardData.pfnCardGetChallenge)(&cardData, &ppbChallenge, &cbChallenge);
CheckResult("CardGetChallenge", lRet);
if (cbChallenge == 8) {
lRet = DesResponse(ppbChallenge, cbChallenge);
CheckResult("DesResponse", lRet);
cbNewKey = 16;
}
else {
lRet = AesResponse(ppbChallenge, cbChallenge);
CheckResult("AesResponse", lRet);
cbNewKey = 16;
}
lRet = (cardData.pfnCardChangeAuthenticator)(&cardData, wszCARD_USER_ADMIN, ppbChallenge, cbChallenge, pbNewKey, cbNewKey, 0, CARD_AUTHENTICATE_PIN_CHALLENGE_RESPONSE, &cbRemaining);
CheckResult("CardChangeAuthenticator", lRet);
(cardData.pfnCspFree)(ppbChallenge);
// Unblock User PIN
lRet = (cardData.pfnCardGetChallenge)(&cardData, &ppbChallenge, &cbChallenge);
CheckResult("CardGetChallenge", lRet);
if (cbChallenge == 8) {
lRet = DesResponse(ppbChallenge, cbChallenge);
CheckResult("DesResponse", lRet);
}
else {
lRet = AesResponse(ppbChallenge, cbChallenge);
CheckResult("AesResponse", lRet);
}
lRet = (cardData.pfnCardChangeAuthenticatorEx)(&cardData, PIN_CHANGE_FLAG_UNBLOCK, ROLE_ADMIN, ppbChallenge, cbChallenge, ROLE_USER, pbNewPin, cbNewPin, 0, &cbRemaining);
CheckResult("CardChangeAuthenticatorEx", lRet);
(cardData.pfnCspFree)(ppbChallenge);
// Retrieve PIN properties
PIN_SET pinSet;
DWORD dwDataLen = 0;
lRet = (cardData.pfnCardGetProperty)(&cardData, CP_CARD_LIST_PINS, (LPBYTE)&pinSet, sizeof(pinSet), &dwDataLen, 0);
CheckResult("CardGetProperty (PIN_SET)", lRet);
for (int i = 0; i < 8; i++) {
if (IS_PIN_SET(pinSet, i)) {
PIN_INFO pinInfo;
pinInfo.dwVersion = PIN_INFO_CURRENT_VERSION;
dwDataLen = sizeof(pinInfo);
lRet = (cardData.pfnCardGetProperty)(&cardData, CP_CARD_PIN_INFO, (LPBYTE)&pinInfo, sizeof(pinInfo), &dwDataLen, i);
CheckResult("CardGetProperty (PIN_INFO)", lRet);
PrintPinInfo(i, &pinInfo);
}
}
lRet = (cardData.pfnCardDeleteContext)(&cardData);
CheckResult("CardDeleteContext", lRet);
lRet = SCardDisconnect(ocn->hCardHandle, SCARD_LEAVE_CARD);
CheckResult("SCardDisconnect", lRet);
FreeLibrary(hDriver);
CspFree(szProvider);
CspFree(szCards);
HeapDestroy(hHeap);
/*
* For cryptographic key management and use, we go through CNG
*/
lRet = NCryptOpenStorageProvider(&hProv, MS_SMART_CARD_KEY_STORAGE_PROVIDER, 0);
CheckResult("NCryptOpenStorageProvider", lRet);
// Retrieve serial number for the card
lRet = NCryptGetProperty(hProv, NCRYPT_SMARTCARD_GUID_PROPERTY, NULL, 0, &cbSerial, 0);
CheckResult("NCryptGetProperty", lRet);
pbSerial = (LPBYTE)malloc(cbSerial);
lRet = NCryptGetProperty(hProv, NCRYPT_SMARTCARD_GUID_PROPERTY, pbSerial, cbSerial, &cbSerial, 0);
CheckResult("NCryptGetProperty", lRet);
// Use the serial number and don't forget to free memory!
free(pbSerial);
lRet = NCryptCreatePersistedKey(hProv, &hKey, BCRYPT_RSA_ALGORITHM, L"Crescendo 4000 Key", AT_SIGNATURE, 0);
CheckResult("NCryptCreatePersistedKey", lRet);
// DWORDs are 4 bytes long
lRet = NCryptSetProperty(hKey, NCRYPT_LENGTH_PROPERTY, (LPBYTE)&dwKeyLen, 4, 0);
CheckResult("NCryptSetProperty", lRet);
// We can pass the pin and prevent the pin dialog to be displayed
lRet = NCryptSetProperty(hKey, NCRYPT_PIN_PROPERTY, (LPBYTE)szPin, 8, 0);
CheckResult("NCryptSetProperty", lRet);
lRet = NCryptFinalizeKey(hKey, NCRYPT_SILENT_FLAG);
CheckResult("NCryptFinalizeKey", lRet);
lRet = NCryptExportKey(hKey, 0, BCRYPT_RSAPUBLIC_BLOB, 0, NULL, 0, &cbRawKey, 0);
CheckResult("NCryptExportKey", lRet);
pbRawKey = (LPBYTE)malloc(cbRawKey);
lRet = NCryptExportKey(hKey, 0, BCRYPT_RSAPUBLIC_BLOB, 0, pbRawKey, cbRawKey, &cbRawKey, 0);
CheckResult("NCryptExportKey", lRet);
// pbRawKey contains now the value of the public key that can be used to request a certificate
// lRet = NCryptSetProperty(hKey, NCRYPT_CERTIFICATE_PROPERTY, pbCert, cbCert, 0);
free(pbRawKey);
lRet = NCryptFreeObject(hKey);
CheckResult("NCryptFreeObject", lRet);
// Now delete the key. We get a new handle to show how it would happen in a different session
lRet = NCryptOpenKey(hProv, &hKey, L"Crescendo 4000 Key", AT_SIGNATURE, 0);
CheckResult("NCryptOpenKey", lRet);
lRet = NCryptSetProperty(hKey, NCRYPT_PIN_PROPERTY, (LPBYTE)szPin, 8, 0);
CheckResult("NCryptSetProperty", lRet);
lRet = NCryptDeleteKey(hKey, 0); // After this, no need to free the key handle
CheckResult("NCryptDeleteKey", lRet);
lRet = NCryptFreeObject(hProv);
CheckResult("NCryptFreeObject", lRet);
return EXIT_SUCCESS;
}