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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef CKCAPI_H
#include "ckcapi.h"
#endif /* CKCAPI_H */
/*
* ckcapi/cfind.c
*
* This file implements the NSSCKMDFindObjects object for the
* "capi" cryptoki module.
*/
struct ckcapiFOStr {
NSSArena *arena;
CK_ULONG n;
CK_ULONG i;
ckcapiInternalObject **objs;
};
static void
ckcapi_mdFindObjects_Final(
NSSCKMDFindObjects *mdFindObjects,
NSSCKFWFindObjects *fwFindObjects,
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance)
{
struct ckcapiFOStr *fo = (struct ckcapiFOStr *)mdFindObjects->etc;
NSSArena *arena = fo->arena;
PRUint32 i;
/* walk down an free the unused 'objs' */
for (i = fo->i; i < fo->n; i++) {
nss_ckcapi_DestroyInternalObject(fo->objs[i]);
}
nss_ZFreeIf(fo->objs);
nss_ZFreeIf(fo);
nss_ZFreeIf(mdFindObjects);
if ((NSSArena *)NULL != arena) {
NSSArena_Destroy(arena);
}
return;
}
static NSSCKMDObject *
ckcapi_mdFindObjects_Next(
NSSCKMDFindObjects *mdFindObjects,
NSSCKFWFindObjects *fwFindObjects,
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance,
NSSArena *arena,
CK_RV *pError)
{
struct ckcapiFOStr *fo = (struct ckcapiFOStr *)mdFindObjects->etc;
ckcapiInternalObject *io;
if (fo->i == fo->n) {
*pError = CKR_OK;
return (NSSCKMDObject *)NULL;
}
io = fo->objs[fo->i];
fo->i++;
return nss_ckcapi_CreateMDObject(arena, io, pError);
}
static CK_BBOOL
ckcapi_attrmatch(
CK_ATTRIBUTE_PTR a,
ckcapiInternalObject *o)
{
PRBool prb;
const NSSItem *b;
b = nss_ckcapi_FetchAttribute(o, a->type);
if (b == NULL) {
return CK_FALSE;
}
if (a->ulValueLen != b->size) {
/* match a decoded serial number */
if ((a->type == CKA_SERIAL_NUMBER) && (a->ulValueLen < b->size)) {
unsigned int len;
unsigned char *data;
data = nss_ckcapi_DERUnwrap(b->data, b->size, &len, NULL);
if ((len == a->ulValueLen) &&
nsslibc_memequal(a->pValue, data, len, (PRStatus *)NULL)) {
return CK_TRUE;
}
}
return CK_FALSE;
}
prb = nsslibc_memequal(a->pValue, b->data, b->size, (PRStatus *)NULL);
if (PR_TRUE == prb) {
return CK_TRUE;
} else {
return CK_FALSE;
}
}
static CK_BBOOL
ckcapi_match(
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
ckcapiInternalObject *o)
{
CK_ULONG i;
for (i = 0; i < ulAttributeCount; i++) {
if (CK_FALSE == ckcapi_attrmatch(&pTemplate[i], o)) {
return CK_FALSE;
}
}
/* Every attribute passed */
return CK_TRUE;
}
#define CKAPI_ITEM_CHUNK 20
#define PUT_Object(obj, err) \
{ \
if (count >= size) { \
*listp = *listp ? nss_ZREALLOCARRAY(*listp, ckcapiInternalObject *, \
(size + \
CKAPI_ITEM_CHUNK)) \
: nss_ZNEWARRAY(NULL, ckcapiInternalObject *, \
(size + \
CKAPI_ITEM_CHUNK)); \
if ((ckcapiInternalObject **)NULL == *listp) { \
err = CKR_HOST_MEMORY; \
goto loser; \
} \
size += CKAPI_ITEM_CHUNK; \
} \
(*listp)[count] = (obj); \
count++; \
}
/*
* pass parameters back through the callback.
*/
typedef struct BareCollectParamsStr {
CK_OBJECT_CLASS objClass;
CK_ATTRIBUTE_PTR pTemplate;
CK_ULONG ulAttributeCount;
ckcapiInternalObject ***listp;
PRUint32 size;
PRUint32 count;
} BareCollectParams;
/* collect_bare's callback. Called for each object that
* supposedly has a PROVINDER_INFO property */
static BOOL WINAPI
doBareCollect(
const CRYPT_HASH_BLOB *msKeyID,
DWORD flags,
void *reserved,
void *args,
DWORD cProp,
DWORD *propID,
void **propData,
DWORD *propSize)
{
BareCollectParams *bcp = (BareCollectParams *)args;
PRUint32 size = bcp->size;
PRUint32 count = bcp->count;
ckcapiInternalObject ***listp = bcp->listp;
ckcapiInternalObject *io = NULL;
DWORD i;
CRYPT_KEY_PROV_INFO *keyProvInfo = NULL;
void *idData;
CK_RV error;
/* make sure there is a Key Provider Info property */
for (i = 0; i < cProp; i++) {
if (CERT_KEY_PROV_INFO_PROP_ID == propID[i]) {
keyProvInfo = (CRYPT_KEY_PROV_INFO *)propData[i];
break;
}
}
if ((CRYPT_KEY_PROV_INFO *)NULL == keyProvInfo) {
return 1;
}
/* copy the key ID */
idData = nss_ZNEWARRAY(NULL, char, msKeyID->cbData);
if ((void *)NULL == idData) {
goto loser;
}
nsslibc_memcpy(idData, msKeyID->pbData, msKeyID->cbData);
/* build a bare internal object */
io = nss_ZNEW(NULL, ckcapiInternalObject);
if ((ckcapiInternalObject *)NULL == io) {
goto loser;
}
io->type = ckcapiBareKey;
io->objClass = bcp->objClass;
io->u.key.provInfo = *keyProvInfo;
io->u.key.provInfo.pwszContainerName =
nss_ckcapi_WideDup(keyProvInfo->pwszContainerName);
io->u.key.provInfo.pwszProvName =
nss_ckcapi_WideDup(keyProvInfo->pwszProvName);
io->u.key.provName = nss_ckcapi_WideToUTF8(keyProvInfo->pwszProvName);
io->u.key.containerName =
nss_ckcapi_WideToUTF8(keyProvInfo->pwszContainerName);
io->u.key.hProv = 0;
io->idData = idData;
io->id.data = idData;
io->id.size = msKeyID->cbData;
idData = NULL;
/* see if it matches */
if (CK_FALSE == ckcapi_match(bcp->pTemplate, bcp->ulAttributeCount, io)) {
goto loser;
}
PUT_Object(io, error);
bcp->size = size;
bcp->count = count;
return 1;
loser:
if (io) {
nss_ckcapi_DestroyInternalObject(io);
}
nss_ZFreeIf(idData);
return 1;
}
/*
* collect the bare keys running around
*/
static PRUint32
collect_bare(
CK_OBJECT_CLASS objClass,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
ckcapiInternalObject ***listp,
PRUint32 *sizep,
PRUint32 count,
CK_RV *pError)
{
BOOL rc;
BareCollectParams bareCollectParams;
bareCollectParams.objClass = objClass;
bareCollectParams.pTemplate = pTemplate;
bareCollectParams.ulAttributeCount = ulAttributeCount;
bareCollectParams.listp = listp;
bareCollectParams.size = *sizep;
bareCollectParams.count = count;
rc = CryptEnumKeyIdentifierProperties(NULL, CERT_KEY_PROV_INFO_PROP_ID, 0,
NULL, NULL, &bareCollectParams, doBareCollect);
*sizep = bareCollectParams.size;
return bareCollectParams.count;
}
/* find all the certs that represent the appropriate object (cert, priv key, or
* pub key) in the cert store.
*/
static PRUint32
collect_class(
CK_OBJECT_CLASS objClass,
LPCSTR storeStr,
PRBool hasID,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
ckcapiInternalObject ***listp,
PRUint32 *sizep,
PRUint32 count,
CK_RV *pError)
{
PRUint32 size = *sizep;
ckcapiInternalObject *next = NULL;
HCERTSTORE hStore;
PCCERT_CONTEXT certContext = NULL;
PRBool isKey =
(objClass == CKO_PUBLIC_KEY) | (objClass == CKO_PRIVATE_KEY);
hStore = CertOpenSystemStore((HCRYPTPROV)NULL, storeStr);
if (NULL == hStore) {
return count; /* none found does not imply an error */
}
/* FUTURE: use CertFindCertificateInStore to filter better -- so we don't
* have to enumerate all the certificates */
while ((PCERT_CONTEXT)NULL !=
(certContext = CertEnumCertificatesInStore(hStore, certContext))) {
/* first filter out non user certs if we are looking for keys */
if (isKey) {
/* make sure there is a Key Provider Info property */
CRYPT_KEY_PROV_INFO *keyProvInfo;
DWORD size = 0;
BOOL rv;
rv = CertGetCertificateContextProperty(certContext,
CERT_KEY_PROV_INFO_PROP_ID, NULL, &size);
if (!rv) {
int reason = GetLastError();
/* we only care if it exists, we don't really need to fetch it yet */
if (reason == CRYPT_E_NOT_FOUND) {
continue;
}
}
/* filter out the non-microsoft providers */
keyProvInfo = (CRYPT_KEY_PROV_INFO *)nss_ZAlloc(NULL, size);
if (keyProvInfo) {
rv = CertGetCertificateContextProperty(certContext,
CERT_KEY_PROV_INFO_PROP_ID, keyProvInfo, &size);
if (rv) {
char *provName =
nss_ckcapi_WideToUTF8(keyProvInfo->pwszProvName);
nss_ZFreeIf(keyProvInfo);
if (provName &&
(strncmp(provName, "Microsoft", sizeof("Microsoft") -
1) != 0)) {
continue;
}
} else {
int reason =
GetLastError();
/* we only care if it exists, we don't really need to fetch it yet */
nss_ZFreeIf(keyProvInfo);
if (reason ==
CRYPT_E_NOT_FOUND) {
continue;
}
}
}
}
if ((ckcapiInternalObject *)NULL == next) {
next = nss_ZNEW(NULL, ckcapiInternalObject);
if ((ckcapiInternalObject *)NULL == next) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
}
next->type = ckcapiCert;
next->objClass = objClass;
next->u.cert.certContext = certContext;
next->u.cert.hasID = hasID;
next->u.cert.certStore = storeStr;
if (CK_TRUE == ckcapi_match(pTemplate, ulAttributeCount, next)) {
/* clear cached values that may be dependent on our old certContext */
memset(&next->u.cert, 0, sizeof(next->u.cert));
/* get a 'permanent' context */
next->u.cert.certContext = CertDuplicateCertificateContext(certContext);
next->objClass = objClass;
next->u.cert.certContext = certContext;
next->u.cert.hasID = hasID;
next->u.cert.certStore = storeStr;
PUT_Object(next, *pError);
next = NULL; /* need to allocate a new one now */
} else {
/* don't cache the values we just loaded */
memset(&next->u.cert, 0, sizeof(next->u.cert));
}
}
loser:
CertCloseStore(hStore, 0);
nss_ZFreeIf(next);
*sizep = size;
return count;
}
NSS_IMPLEMENT PRUint32
nss_ckcapi_collect_all_certs(
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
ckcapiInternalObject ***listp,
PRUint32 *sizep,
PRUint32 count,
CK_RV *pError)
{
count = collect_class(CKO_CERTIFICATE, "My", PR_TRUE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
/*count = collect_class(CKO_CERTIFICATE, "AddressBook", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError); */
count = collect_class(CKO_CERTIFICATE, "CA", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
count = collect_class(CKO_CERTIFICATE, "Root", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
count = collect_class(CKO_CERTIFICATE, "Trust", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
count = collect_class(CKO_CERTIFICATE, "TrustedPeople", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
count = collect_class(CKO_CERTIFICATE, "AuthRoot", PR_FALSE, pTemplate,
ulAttributeCount, listp, sizep, count, pError);
return count;
}
CK_OBJECT_CLASS
ckcapi_GetObjectClass(CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount)
{
CK_ULONG i;
for (i = 0; i < ulAttributeCount; i++) {
if (pTemplate[i].type == CKA_CLASS) {
return *(CK_OBJECT_CLASS *)pTemplate[i].pValue;
}
}
/* need to return a value that says 'fetch them all' */
return CK_INVALID_HANDLE;
}
static PRUint32
collect_objects(
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
ckcapiInternalObject ***listp,
CK_RV *pError)
{
PRUint32 i;
PRUint32 count = 0;
PRUint32 size = 0;
CK_OBJECT_CLASS objClass;
/*
* first handle the static build in objects (if any)
*/
for (i = 0; i < nss_ckcapi_nObjects; i++) {
ckcapiInternalObject *o = (ckcapiInternalObject *)&nss_ckcapi_data[i];
if (CK_TRUE == ckcapi_match(pTemplate, ulAttributeCount, o)) {
PUT_Object(o, *pError);
}
}
/*
* now handle the various object types
*/
objClass = ckcapi_GetObjectClass(pTemplate, ulAttributeCount);
*pError = CKR_OK;
switch (objClass) {
case CKO_CERTIFICATE:
count = nss_ckcapi_collect_all_certs(pTemplate, ulAttributeCount, listp,
&size, count, pError);
break;
case CKO_PUBLIC_KEY:
count = collect_class(objClass, "My", PR_TRUE, pTemplate,
ulAttributeCount, listp, &size, count, pError);
count = collect_bare(objClass, pTemplate, ulAttributeCount, listp,
&size, count, pError);
break;
case CKO_PRIVATE_KEY:
count = collect_class(objClass, "My", PR_TRUE, pTemplate,
ulAttributeCount, listp, &size, count, pError);
count = collect_bare(objClass, pTemplate, ulAttributeCount, listp,
&size, count, pError);
break;
/* all of them */
case CK_INVALID_HANDLE:
count = nss_ckcapi_collect_all_certs(pTemplate, ulAttributeCount, listp,
&size, count, pError);
count = collect_class(CKO_PUBLIC_KEY, "My", PR_TRUE, pTemplate,
ulAttributeCount, listp, &size, count, pError);
count = collect_bare(CKO_PUBLIC_KEY, pTemplate, ulAttributeCount, listp,
&size, count, pError);
count = collect_class(CKO_PRIVATE_KEY, "My", PR_TRUE, pTemplate,
ulAttributeCount, listp, &size, count, pError);
count = collect_bare(CKO_PRIVATE_KEY, pTemplate, ulAttributeCount, listp,
&size, count, pError);
break;
default:
goto done; /* no other object types we understand in this module */
}
if (CKR_OK != *pError) {
goto loser;
}
done:
return count;
loser:
nss_ZFreeIf(*listp);
return 0;
}
NSS_IMPLEMENT NSSCKMDFindObjects *
nss_ckcapi_FindObjectsInit(
NSSCKFWSession *fwSession,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
CK_RV *pError)
{
/* This could be made more efficient. I'm rather rushed. */
NSSArena *arena;
NSSCKMDFindObjects *rv = (NSSCKMDFindObjects *)NULL;
struct ckcapiFOStr *fo = (struct ckcapiFOStr *)NULL;
ckcapiInternalObject **temp = (ckcapiInternalObject **)NULL;
arena = NSSArena_Create();
if ((NSSArena *)NULL == arena) {
goto loser;
}
rv = nss_ZNEW(arena, NSSCKMDFindObjects);
if ((NSSCKMDFindObjects *)NULL == rv) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
fo = nss_ZNEW(arena, struct ckcapiFOStr);
if ((struct ckcapiFOStr *)NULL == fo) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
fo->arena = arena;
/* fo->n and fo->i are already zero */
rv->etc = (void *)fo;
rv->Final = ckcapi_mdFindObjects_Final;
rv->Next = ckcapi_mdFindObjects_Next;
rv->null = (void *)NULL;
fo->n = collect_objects(pTemplate, ulAttributeCount, &temp, pError);
if (*pError != CKR_OK) {
goto loser;
}
fo->objs = nss_ZNEWARRAY(arena, ckcapiInternalObject *, fo->n);
if ((ckcapiInternalObject **)NULL == fo->objs) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
(void)nsslibc_memcpy(fo->objs, temp, sizeof(ckcapiInternalObject *) * fo->n);
nss_ZFreeIf(temp);
temp = (ckcapiInternalObject **)NULL;
return rv;
loser:
nss_ZFreeIf(temp);
nss_ZFreeIf(fo);
nss_ZFreeIf(rv);
if ((NSSArena *)NULL != arena) {
NSSArena_Destroy(arena);
}
return (NSSCKMDFindObjects *)NULL;
}
|