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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
/*
* Copyright (C) 2007 Stef Walter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "ckcapi.h"
#include "ckcapi-cert.h"
#include "ckcapi-object.h"
#include "ckcapi-session.h"
#include "ckcapi-token.h"
#include <memory.h>
#ifndef CERT_FIND_KEY_IDENTIFIER
#define CERT_FIND_KEY_IDENTIFIER 983040
#endif
#ifndef CERT_KEY_IDENTIFIER_PROP_ID
#define CERT_KEY_IDENTIFIER_PROP_ID 20
#endif
typedef struct _CertObject
{
CkCapiObject obj;
/* Together these can uniquely identify a certificate */
CRYPT_INTEGER_BLOB serial;
CERT_NAME_BLOB issuer;
/*
* This must stay together. It comprises a unique
* key, together with the data that runs off the end.
*/
int otype;
BYTE cert_data[1];
}
CertObject;
typedef struct _CertObjectData
{
CkCapiObjectData base;
PCCERT_CONTEXT cert;
}
CertObjectData;
static CK_RV
cert_bool_attribute(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr)
{
CertObjectData* cdata = (CertObjectData*)objdata;
CK_BBOOL val;
ASSERT(cdata);
switch(attr->type)
{
/*
* Resides on the token
* - Always true for CAPI objects.
*/
case CKA_TOKEN:
val = CK_TRUE;
break;
/*
* Private vs. Public object.
* - Always false for certificates.
*/
case CKA_PRIVATE:
val = CK_FALSE;
break;
/*
* If object can be modified.
* - Currently always false. In the future with additional
* functionality this may change.
*/
case CKA_MODIFIABLE:
val = CK_FALSE;
break;
/*
* Whether the certificate can be trusted for the application
* in which it was created.
* - Use CertGetCertificateChain to build up a chain
* for this certificate, and then look in the CERT_CHAIN_CONTEXT
* TrustStatus field.
*/
case CKA_TRUSTED:
/* TODO: Implement */
default:
return CKR_ATTRIBUTE_TYPE_INVALID;
};
return ckcapi_return_data(attr, &val, sizeof(CK_BBOOL));
}
static CK_RV
cert_ulong_attribute(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr)
{
CertObjectData* cdata = (CertObjectData*)objdata;
CK_ULONG val;
ASSERT(objdata);
switch(attr->type)
{
/*
* Object class.
* - Always CKO_CERTIFICATE for certificates.
*/
case CKA_CLASS:
val = CKO_CERTIFICATE;
break;
/*
* Type of certificate.
* - Always X509.
*/
case CKA_CERTIFICATE_TYPE:
val = CKC_X_509;
break;
/*
* Whether a CA, user certificate, other.
* - Get certificate szOID_ENHANCED_KEY_USAGE
* extension or CERT_CTL_PROP_ID and look into CTL_USAGE structure.
*/
case CKA_CERTIFICATE_CATEGORY:
/* TODO: Implement */
default:
return CKR_ATTRIBUTE_TYPE_INVALID;
};
return ckcapi_return_data(attr, &val, sizeof(CK_ULONG));
}
static CK_RV
cert_bytes_attribute(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr)
{
CertObjectData* cdata = (CertObjectData*)objdata;
PCCERT_CONTEXT cert = cdata->cert;
ASSERT(sizeof(CK_ULONG) == sizeof(DWORD));
ASSERT(cdata);
return ckcapi_cert_certificate_get_bytes(cdata->cert, attr);
}
static CK_RV
cert_date_attribute(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attr)
{
switch(attr->type)
{
/*
* Start date for the certificate.
* - TODO: Work out where to get this.
* pCertInfo->NotBefore;
*/
case CKA_START_DATE:
break;
/*
* End date for the certificate.
* - TODO: Work out where to get this.
* pCertInfo->NotBefore;
*/
case CKA_END_DATE:
break;
};
return CKR_ATTRIBUTE_TYPE_INVALID;
}
static void
cert_data_release(void* data)
{
CertObjectData* cdata = (CertObjectData*)data;
ASSERT(cdata && cdata->cert);
CertFreeCertificateContext(cdata->cert);
free(cdata);
}
static const CkCapiObjectDataVtable cert_objdata_vtable = {
cert_bool_attribute,
cert_ulong_attribute,
cert_bytes_attribute,
cert_date_attribute,
cert_data_release,
};
static CkCapiObjectData*
cert_alloc_data(CkCapiSession* sess, CkCapiObject* obj, PCCERT_CONTEXT cert)
{
CertObjectData* cdata;
cdata = (CertObjectData*)calloc(sizeof(CertObjectData), 1);
if(!cdata)
return NULL;
cdata->cert = cert;
cdata->base.object = obj->id;
cdata->base.data_funcs = &cert_objdata_vtable;
return &(cdata->base);
}
static CK_RV
cert_load_data(CkCapiSession* sess, CkCapiObject* obj, CkCapiObjectData** objdata)
{
CertObject* cobj = (CertObject*)obj;
CERT_INFO info;
PCCERT_CONTEXT cert;
ASSERT(cobj);
ASSERT(objdata);
ASSERT(cobj->issuer.pbData);
ASSERT(cobj->issuer.cbData);
ASSERT(cobj->serial.pbData);
ASSERT(cobj->serial.cbData);
/* No store should mean no objects were loaded */
ASSERT(sess->store);
/* Setup our search */
memset(&info, 0, sizeof(info));
memcpy(&info.SerialNumber, &cobj->serial, sizeof(info.SerialNumber));
memcpy(&info.Issuer, &cobj->issuer, sizeof(info.Issuer));
cert = CertGetSubjectCertificateFromStore(sess->store, CKCAPI_ENCODINGS, &info);
if(!cert)
{
DWORD err = GetLastError();
/* TODO: Is this right for a deleted certificate? */
ASSERT(err != E_INVALIDARG);
if(err == CRYPT_E_NOT_FOUND)
return CKR_OBJECT_HANDLE_INVALID;
else
return ckcapi_winerr_to_ckr(GetLastError());
}
*objdata = cert_alloc_data(sess, obj, cert);
if(!(*objdata))
{
CertFreeCertificateContext(cert);
return CKR_HOST_MEMORY;
}
return CKR_OK;
}
static void
cert_object_release(void* data)
{
CertObject* cobj = (CertObject*)data;
ASSERT(cobj);
free(cobj);
}
static const CkCapiObjectVtable cert_object_vtable = {
cert_load_data,
cert_object_release,
};
CK_RV
ckcapi_cert_certificate_get_bytes(PCCERT_CONTEXT cert, CK_ATTRIBUTE_PTR attr)
{
DWORD err;
ASSERT(cert);
ASSERT(attr);
switch(attr->type)
{
/*
* Description of the object.
* - We use CAPI's CERT_FRIENDLY_NAME_PROP_ID property,
* converted into UTF8.
* - Yes this is slow, but this is not really a property
* that's searched on or retrieved intensively.
*/
case CKA_LABEL:
{
WCHAR* utf16 = NULL;
DWORD size;
if(!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &size))
{
err = GetLastError();
if(err == CRYPT_E_NOT_FOUND)
utf16 = L"Unnamed Certificate";
else
return ckcapi_winerr_to_ckr(err);
}
if(!utf16)
{
utf16 = _alloca(size);
if(!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, utf16, &size))
return ckcapi_winerr_to_ckr(GetLastError());
}
return ckcapi_return_string(attr, utf16);
}
break;
/*
* A byte array unique to this certificate. The CKA_ID of
* matching certificates and private keys should match.
* Should match the key identifier in an X.509v3 certificate.
*
* We use CAPI's CERT_KEY_IDENTIFIER_PROP_ID property directly.
*/
case CKA_ID:
if(!CertGetCertificateContextProperty(cert, CERT_KEY_IDENTIFIER_PROP_ID,
attr->pValue, (DWORD*)&attr->ulValueLen))
{
err = GetLastError();
if(err == CRYPT_E_NOT_FOUND)
return CKR_ATTRIBUTE_TYPE_INVALID;
return ckcapi_winerr_to_ckr(err);
}
return CKR_OK;
/*
* DER-encoding of the certificate subject name.
*
* We use CAPI's CERT_CONTEXT pCertInfo->Subject field
* directly.
*/
case CKA_SUBJECT:
return ckcapi_return_data(attr, cert->pCertInfo->Subject.pbData,
cert->pCertInfo->Subject.cbData);
/*
* DER-encoding of the certificate issuer name.
*
* We use CAPI's CERT_CONTEXT pCertInfo->Issuer field
* directly.
*/
case CKA_ISSUER:
return ckcapi_return_data(attr, cert->pCertInfo->Issuer.pbData,
cert->pCertInfo->Issuer.cbData);
/*
* DER-encoding of the certificate serial number.
*
* TODO:
* BOOL rc = CryptEncodeObject(X509_ASN_ENCODING,
* X509_MULTI_BYTE_INTEGER,
* &certContext->pCertInfo->SerialNumber,
* co->derSerial,
* &size);
*/
case CKA_SERIAL_NUMBER:
if(!CryptEncodeObject(X509_ASN_ENCODING, X509_MULTI_BYTE_INTEGER,
&cert->pCertInfo->SerialNumber,
attr->pValue, (DWORD*)&attr->ulValueLen))
{
err = GetLastError();
if(err == ERROR_FILE_NOT_FOUND)
return CKR_GENERAL_ERROR;
return ckcapi_winerr_to_ckr(err);
}
return CKR_OK;
/*
* BER-encoding of the full certificate.
*
* We use CAPI's CERT_CONTEXT pbCertEncoded field directly.
*/
case CKA_VALUE:
return ckcapi_return_data(attr, cert->pbCertEncoded,
cert->cbCertEncoded);
/*
* If CKA_VALUE not specified, this is where the full
* certificate can be found.
*
* We don't support this. All our certificates are present
* in full.
*/
case CKA_URL:
return CKR_ATTRIBUTE_TYPE_INVALID;
/*
* Checksum
* - TODO: Work out what to do here
*/
case CKA_CHECK_VALUE:
return CKR_ATTRIBUTE_TYPE_INVALID;
/*
* TODO: Should we support these?
*/
case CKA_HASH_OF_SUBJECT_PUBLIC_KEY:
case CKA_HASH_OF_ISSUER_PUBLIC_KEY:
return CKR_ATTRIBUTE_TYPE_INVALID;
/* Not supported */
default:
return CKR_ATTRIBUTE_TYPE_INVALID;
};
}
PCCERT_CONTEXT
ckcapi_cert_object_data_get_certificate(CkCapiObjectData* objdata)
{
CertObjectData* cdata;
ASSERT(objdata);
ASSERT(objdata->data_funcs == &cert_objdata_vtable);
cdata = (CertObjectData*)objdata;
return cdata->cert;
}
static CK_RV
register_cert_object(CkCapiSession* sess, PCCERT_CONTEXT cert, CkCapiObject** obj)
{
CertObject* cobj;
CK_RV ret;
size_t len;
/* We save the Issuer and SerialNumber for identification later */
len = cert->pCertInfo->SerialNumber.cbData +
cert->pCertInfo->Issuer.cbData;
/* Add one in case null termination is needed */
len++;
cobj = calloc(sizeof(CertObject) + len, 1);
if(!cobj)
return CKR_HOST_MEMORY;
cobj->otype = OBJECT_CERT;
cobj->obj.id = 0;
cobj->obj.unique_key = UNIQUE_KEY_AT(cobj, otype);
cobj->obj.unique_len = UNIQUE_KEY_VAR_LEN(cobj, otype, cert_data, len);
cobj->obj.obj_funcs = &cert_object_vtable;
/* Copy Issuer data in */
cobj->issuer.cbData = cert->pCertInfo->Issuer.cbData;
cobj->issuer.pbData = cobj->cert_data;
memcpy(cobj->issuer.pbData, cert->pCertInfo->Issuer.pbData,
cobj->issuer.cbData);
/* Copy Serial Number data in */
cobj->serial.cbData = cert->pCertInfo->SerialNumber.cbData;
cobj->serial.pbData = cobj->cert_data + cobj->issuer.cbData;
memcpy(cobj->serial.pbData, cert->pCertInfo->SerialNumber.pbData,
cobj->serial.cbData);
ret = ckcapi_token_register_object(sess->slot, &(cobj->obj));
if(ret != CKR_OK)
{
free(cobj);
return ret;
}
ASSERT(cobj->obj.id != 0);
*obj = &cobj->obj;
return CKR_OK;
}
static CK_RV
find_in_store(CkCapiSession* sess, DWORD find_type, const void *find_criteria,
CK_ATTRIBUTE_PTR match, CK_ULONG count, CkCapiArray* arr)
{
PCCERT_CONTEXT cert = NULL;
CkCapiObject* obj;
CkCapiObjectData* objdata;
CertObjectData cdata;
DWORD err;
CK_RV ret = CKR_OK;
/* No store, no objects */
if(!sess->store)
return CKR_OK;
for(;;)
{
cert = CertFindCertificateInStore(sess->store, CKCAPI_ENCODINGS, 0,
find_type, find_criteria, cert);
if(cert == NULL)
{
err = GetLastError();
/* Certificate not found, we don't care */
if(err == CRYPT_E_NOT_FOUND)
return CKR_OK;
else
return ckcapi_winerr_to_ckr(err);
}
/* Match the certificate */
cdata.cert = cert;
cdata.base.object = 0;
cdata.base.data_funcs = &cert_objdata_vtable;
if(ckcapi_object_data_match(&cdata.base, match, count))
{
ret = register_cert_object(sess, cert, &obj);
if(ret == CKR_OK)
{
ASSERT(obj);
/* Store away the object data for performance reasons */
objdata = cert_alloc_data(sess, obj, cert);
if(objdata)
{
ckcapi_session_take_object_data(sess, obj, objdata);
/* For continuing the enumeration */
cert = CertDuplicateCertificateContext(cert);
}
ckcapi_array_append(arr, obj->id);
}
}
}
if(ret != CKR_OK && cert)
CertFreeCertificateContext(cert);
return ret;
}
CK_RV
ckcapi_cert_find(CkCapiSession* sess, CK_OBJECT_CLASS cls, CK_ATTRIBUTE_PTR match,
CK_ULONG count, CkCapiArray* arr)
{
CRYPT_INTEGER_BLOB* serial = NULL;
CK_RV ret;
CK_ULONG i;
DWORD size;
CERT_INFO find_info; /* For searching by issuer and serial */
CRYPT_HASH_BLOB find_key; /* For searching by ID */
/* We only have certificates here */
if(cls != CKO_CERTIFICATE && cls != CKO_ANY)
return CKR_OK;
/* Only work with slots that have certificates */
if(!(ckcapi_token_get_flags (sess->slot) & CKCAPI_SLOT_CERTS))
return CKR_OK;
/*
* There are some better searches we can do rather than
* listing everything.
*
* CKA_ISSUER + CKA_SERIAL_NUMBER
* See if we have a issuer and serial number for a
* specific certificate to find.
*
* CKA_ID
* Search by key identifier
*
* TODO: could search by hash (use CertFindCertificateInStore
* with CERT_FIND_HASH or CERT_FIND_SHA1_HASH or CERT_FIND_MD5_HASH)
*
* TODO: could search by issuer (use CertFindCertificateInStore
* with CERT_FIND_ISSUER_NAME)
*
* TODO: could search by subject (use CertFindCertificateInStore
* with CERT_FIND_SUBJECT_NAME)
*
* TODO: could search by CKA_VALUE (use CertFindCertificateInStore
* with CERT_FIND_EXISTING)
*/
memset(&find_info, 0, sizeof(find_info));
memset(&find_key, 0, sizeof(find_key));
for(i = 0; i < count; ++i)
{
if(!match[i].pValue || !match[i].ulValueLen)
continue;
if(match[i].type == CKA_ISSUER)
{
find_info.Issuer.cbData = match[i].ulValueLen;
find_info.Issuer.pbData = match[i].pValue;
}
else if(match[i].type == CKA_SERIAL_NUMBER && !serial)
{
if(!CryptDecodeObject(CKCAPI_ENCODINGS, X509_MULTI_BYTE_INTEGER,
match[i].pValue, match[i].ulValueLen, 0, NULL, &size))
{
continue;
}
serial = calloc(1, size);
if(!CryptDecodeObject(CKCAPI_ENCODINGS, X509_MULTI_BYTE_INTEGER,
match[i].pValue, match[i].ulValueLen, 0, serial, &size))
continue;
ASSERT(serial->cbData);
ASSERT(serial->pbData);
find_info.SerialNumber.cbData = serial->cbData;
find_info.SerialNumber.pbData = serial->pbData;
}
else if(match[i].type == CKA_ID)
{
find_key.cbData = match[i].ulValueLen;
find_key.pbData = match[i].pValue;
}
}
/* Match a specific certificate */
if(find_info.SerialNumber.cbData && find_info.Issuer.cbData)
{
ret = find_in_store(sess, CERT_FIND_SUBJECT_CERT, &find_info,
match, count, arr);
}
/* Find all certificates with key identifier */
else if(find_key.cbData)
{
ret = find_in_store(sess, CERT_FIND_KEY_IDENTIFIER, &find_key,
match, count, arr);
}
/* Match any ol certificate */
else
{
ret = find_in_store(sess, CERT_FIND_ANY, NULL,
match, count, arr);
}
if(serial)
free(serial);
return ret;
}
|