summaryrefslogtreecommitdiff
path: root/ckcapi-object.c
blob: 531ad2f300318c974eee38ec2eb5442f805469b2 (plain)
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

#include "cryptoki-capi.h"

#include <wtypes.h>
#include <wincrypt.h>

enum 
{
	DATA_UNKNOWN = 0,
	DATA_BOOL,
	DATA_ULONG,
	DATA_DATE,
	DATA_BYTES
};


int
attribute_data_type(CK_ATTRIBUTE_TYPE type)
{
	switch(type)
	{
	// CK_ULONG attribute types
	case CKA_CLASS:
	case CKA_CERTIFICATE_TYPE:
	case CKA_CERTIFICATE_CATEGORY:
	case CKA_KEY_TYPE:
	case CKA_MODULUS_BITS:
	case CKA_PRIME_BITS:
	case CKA_SUBPRIME_BITS:
	case CKA_SUB_PRIME_BITS:
	case CKA_VALUE_BITS:
	case CKA_VALUE_LEN:
	case CKA_KEY_GEN_MECHANISM:
	case CKA_HW_FEATURE_TYPE:
	case CKA_PIXEL_X:
	case CKA_PIXEL_Y:
	case CKA_RESOLUTION:
	case CKA_CHAR_ROWS:
	case CKA_CHAR_COLUMNS:
	case CKA_BITS_PER_PIXEL:
	case CKA_MECHANISM_TYPE:
	case CKA_JAVA_MIDP_SECURITY_DOMAIN:
		return DATA_ULONG:

	// CK_BBOOL attribute types
	case CKA_TOKEN:
	case CKA_PRIVATE:
	case CKA_MODIFIABLE:
	case CKA_TRUSTED:
	case CKA_SENSITIVE:
	case CKA_DECRYPT:
	case CKA_SIGN:
	case CKA_SIGN_RECOVER:
	case CKA_UNWRAP:
	case CKA_EXTRACTABLE:
	case CKA_NEVER_EXTRACTABLE:
	case CKA_ALWAYS_SENSITIVE:
	case CKA_WRAP_WITH_TRUSTED:
	case CKA_ALWAYS_AUTHENTICATE
	case CKA_ENCRYPT:
	case CKA_WRAP:
	case CKA_VERIFY:
	case CKA_VERIFY_RECOVER:
	case CKA_DERIVE:
	case CKA_LOCAL:
	case CKA_RESET_ON_INIT:
	case CKA_HAS_RESET:
	case CKA_COLOR:
		return DATA_BOOL;

	// Raw or string data
	case CKA_LABEL:
	case CKA_APPLICATION:
	case CKA_VALUE:
	case CKA_OBJECT_ID:
	case CKA_CHECK_VALUE:
	case CKA_ISSUER:
	case CKA_SERIAL_NUMBER:
	case CKA_SUBJECT:
	case CKA_ID:
	case CKA_URL:
	case CKA_HASH_OF_SUBJECT_PUBLIC_KEY:
	case CKA_HASH_OF_ISSUER_PUBLIC_KEY:
	case CKA_AC_ISSUER:
	case CKA_OWNER:
	case CKA_ATTR_TYPES:
	case CKA_MODULUS:
	case CKA_PUBLIC_EXPONENT:
	case CKA_PRIVATE_EXPONENT:
	case CKA_PRIME_1:
	case CKA_PRIME_2:
	case CKA_EXPONENT_1:
	case CKA_EXPONENT_2:
	case CKA_COEFFICIENT:
	case CKA_PRIME:
	case CKA_SUBPRIME:
	case CKA_BASE:
	case CKA_ECDSA_PARAMS:
	case CKA_EC_PARAMS:
	case CKA_EC_POINT:
	case CKA_CHAR_SETS:
	case CKA_ENCODING_METHODS:
	case CKA_MIME_TYPES:
	case CKA_REQUIRED_CMS_ATTRIBUTES:
	case CKA_DEFAULT_CMS_ATTRIBUTES:
	case CKA_SUPPORTED_CMS_ATTRIBUTES:
		return DATA_BYTES;

	// CK_DATE data
	case CKA_START_DATE:
	case CKA_END_DATE:
		return DATA_DATE;

	// Arrays are nasty
	case CKA_WRAP_TEMPLATE:
	case CKA_ALLOWED_MECHANISMS:
	case CKA_UNWRAP_TEMPLATE:
	default:
		return DATA_UNKNOWN;
	};
}

CK_RV
attribute_fill(CK_ATTRIBUTE_PTR attr, CK_ATTRIBUTE_TYPE type, 
			   CK_VOID_PTR* val, CK_ULONG len)
{
	ASSERT(attr);
	ASSERT(val);
	ASSERT(len);

	attr->type = type;

	if(attr->pValue && attr->ulValueLen < len)
	{
		attr->ulValueLen = len;
		return CKR_BUFFER_TOO_SMALL;
	}

	else if(!attr->pValue)
	{
		attr->ulValueLen = len;
		return CKR_OK;
	}

	else
	{
		memcpy(attr->pValue, &val, len);
		attr->ulValueLen = len;
		return CKR_OK;
	}
}

CK_RV
attribute_match(CK_ATTRIBUTE_PTR attr, CK_ATTRIBUTE_TYPE type,
				CK_VOID_PTR* val, CK_ULONG len)
{
	ASSERT(attr);
	ASSERT(val);
	ASSERT(len);

	if(attr->pValue && attr->ulValueLen == len && 
	   memcmp(attr->pValue, &val, len);
		   return CKR_OK;
	return CKR_CANCEL;
}

/* --------------------------------------------------------------------------
 * CERTIFICATES
 */

static CK_RV
cert_bool_attribute(CK_ATTRIBUTE_TYPE type, PCCERT_CONTEXT cert,
				    CK_ATTRIBUTE_PTR attr, CK_BBOOL fill)
{
	CK_BBOOL val;
	switch(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 CK_CANCEL;
	};

	if(fill)
		return attribute_fill(attr, type, &val, sizeof(CK_BBOOL));
	else
		return attribute_match(attr, type, &val, sizeof(CK_BBOOL));
}

static CK_RV
cert_ulong_attribute(CK_ATTRIBUTE_TYPE type, PCCERT_CONTEXT cert,
				     CK_ATTRIBUTE_PTR attr, CK_BBOOL fill)
{
	CK_ULONG val;
	switch(type)
	{

	/*
	 * Object class.
	 * - Always CKO_CERTIFICATE for certificates.
	 */
	case CKA_OBJECT_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 CK_CANCEL;
	};

	if(fill)
		return attribute_fill(attr, type, &val, sizeof(CK_ULONG));
	else
		return attribute_match(attr, type, &val, sizeof(CK_ULONG));
}

static CK_RV
cert_bytes_attribute(CK_ATTRIBUTE_TYPE type, PCCERT_CONTEXT cert,
				     CK_ATTRIBUTE_PTR attr, CK_BBOOL fill)
{
	switch(type)
	{

	/*
	 * Description of the object.
	 * - We use CAPI's CERT_FRIENDLY_NAME_PROP_ID property, 
	 *   converted into UTF8.
	 */
	case CKA_LABEL:
		xxxxx
		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:
		xxxxx
		break;

	/*
	 * DER-encoding of the certificate subject name.
	 * 
	 * We use CAPI's CERT_CONTEXT pCertInfo->Subject field
	 * directly. 
	 */
	case CKA_SUBJECT:
		xxxxx
		break;

	/*
	 * DER-encoding of the certificate issuer name.
	 * 
	 * We use CAPI's CERT_CONTEXT pCertInfo->Issuer field
	 * directly.
	 */
	case CKA_ISSUER:
		xxxxx
		break;

	/*
	 * 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:
		xxxxx
		break;

	/*
	 * BER-encoding of the full certificate.
	 *
	 * We use CAPI's CERT_CONTEXT pbCertEncoded field directly.
	 */
	case CKA_VALUE:
		xxxxx
		break;

	/*
	 * 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:
		break;

	/*
	 * Checksum
	 * - TODO: Work out what to do here
	 */
	case CKA_CHECKSUM:
		break;

	/*
	 * TODO: Should we support these?
	 */
	CKA_HASH_OF_SUBJECT_PUBLIC_KEY
	CKA_HASH_OF_ISSUER_PUBLIC_KEY
		break;

	/* Not supported */

	default:
		break
	};
};

static xxx
cert_date_attribute()
{
	switch(type)
	{

	/*
	 * Start date for the certificate.
	 * - TODO: Work out where to get this.
	 */
		pCertInfo->NotBefore;
	case CKA_START_DATE:
		xxxx;
		break;

	/*
	 * End date for the certificate.
	 * - TODO: Work out where to get this.
	 */
		pCertInfo->NotAfter;
	case CKA_END_DATE:
		xxxx;
		break;
	};
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

static CK_RV
cert_ulong_attribute(CK_ATTRIBUTE_TYPE type, PCCERT_CONTEXT cert,
				     CK_ATTRIBUTE_PTR attr)
{
	CK_ATTRIBUTE_PTR ret;
	CK_BBOOL val;

	switch(type) 
	{
	case CKA_TOKEN:
		val = CK_TRUE;
		break;
	case CKA_PRIVATE:
	case CKA_MODIFIABLE:
		val = CK_FALSE;
		break;
	};

	/* If attr is initialized, just do a match */
	if(attr.type == type)
	{
		if(attr->pValue && attr->ulValueLen == sizeof(CK_BBOOL) && 
		   memcmp(attr->pValue, &val, sizeof(CK_BBOOL));
		   return CKR_OK;
		return CKR_CANCEL;
	}

	/* Otherwise fill in the value */
	else
	{
		return prep_attribute(attr, type, &val, sizeof(CK_BBOOL);
	}
}

static CK_ATTRIBUTE_PTR
cert_load_attribute(CK_ATTRIBUTE_TYPE type, PCCERT_CONTEXT cert)
{
	CK_BBOOL vval = CK_FALSE;
	CK_ULONG uval = 0;
	CK_VOID_PTR val;
	CK_ULONG n_val;

	switch(type)
	{
	case CKA_CLASS:
		uval = CKO_CERTIFICATE;
		break;
	case CKA_TOKEN;
	
	}
}

static CK_BBOOL
cert_match_attr(CK_ATTRIBUTE_PTR match, PCCERT_CONTEXT cert)
{
	CK_ATTRIBUTE_PTR attr;
	CK_BBOOL ret;

	attr = cert_load_attribute(match->type, cert);
	if(!attr)
		return CK_FALSE;

	ret = (attr->ulValueLen == match->ulValueLen && 
		   memcmp(attr->pValue, match->pValue, attr->ulValueLen) == 0);

	free_attribute(attr);
	return ret;
}

static CK_BBOOL
cert_match(CK_ATTRIBUTE_PTR matches, CK_ULONG count, 
		   PCCERT_CONTEXT cert)
{
	CK_ULONG i;

	for(i = 0; i < count; ++i)
	{
		if(!cert_match_attr(&match[i], cert))
			return CK_FALSE;
	}

	return CK_TRUE;
}

static CK_RV
gather_store_certs(const char* store_name, Array* arr, 
				   CK_ATTRIBUTE_PTR match, CK_ULONG count)
{
	PCCERT_CONTEXT cert = NULL;
	CK_OBJECT_HANDLE obj;
	HCERTSTORE store;
	DWORD err;
	CK_RV ret = CKR_OK;

	store = CertOpenSystemStore((HCRYPTPROV)NULL, store_name);
	if(store == NULL)
	{
		err = GetLastError();

		/* Store not found, we don't care */
		if(err == ERROR_FILE_NOT_FOUND)
			return CKR_OK;

		else
			return ckcapi_winerr_to_ckr(err);
	}

	/* Match each certificate */
	while((cert = CertEnumCertificatesInStore(store, cert)) != NULL)
	{
		if(match_cert(match, count, cert))
		{
			obj = register_cert(store, cert);
			if(!obj)
			{
				ret = CKR_HOST_MEMORY;
				break;
			}

			ckcapi_util_array_append(arr, obj);
		}
	}

	ASSERT(store);
	CertCloseStore(store, 0);

	return ret;
}

/* ----------------------------------------------------------------------------
 * FIND
 */

BOOL
get_ulong_attribute(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE_PTR templ, 
					CK_ULONG count, CK_ULONG* val)
{
	CK_ULONG i;

	ASSERT(val);
	ASSERT(!count || templ);

	for(i = 0; i < count; ++i)
	{
		if(templ[i].type == type)
		{
			*val = *((CK_ULONG*)templ[i].pValue);
			return TRUE;
		}
	}

	return FALSE;
}




CK_RV
gather_objects(Array* arr, CK_ATTRIBUTE_PTR match, CK_ULONG count)
{
	CK_OBJECT_CLASS ocls = CK_INVALID_HANDLE;
	CK_RV ret = CKR_OK;

	get_ulong_attribute(CKA_CLASS, match, count, &ocls);
	switch(ocls)
	{
	/* all different classes */
	case CK_INVALID_HANDLE:
	case CKO_CERTIFICATE:
		ret = gather_certificates(arr, match, count); 
		break;
	case CKO_PUBLIC_KEY:
	case CKO_PRIVATE_KEY:
	default:
		break;
	};

	return ret;
}

void 
cleanup_find_operation(Session* sess)
{
	ASSERT(sess->operation_type == OPERATION_FIND);
	if(sess->operation_data)
		ckcapi_util_array_free((Array*)sess->operation_data, TRUE);
	sess->operation_type = OPERATION_NONE;
	sess->operation_data = NULL;
	sess->operation_cancel = NULL;
}

CK_RV
ckcapi_object_find_init(Session* sess, CK_ATTRIBUTE_PTR match, 
						CK_ULONG count)
{
	Array* arr;
	CK_RV ret;

	ASSERT(sess);
	ASSERT(!count || match);

	if(sess->operation_type != OPERATION_NONE)
		return CKR_OPERATION_ACTIVE;

	arr = ckcapi_util_array_new(0, 1, sizeof(CK_OBJECT_HANDLE));
	if(!arr)
		return CKR_HOST_MEMORY;

	ret = gather_objects(arr, match, count);
	if(ret != CKR_OK) 
	{
		ckcapi_util_array_free(arr, TRUE);
		return ret;
	}

	sess->operation_type = OPERATION_FIND;
	sess->operation_data = arr;
	sess->operation_cancel = cleanup_find_operation;

	return CKR_OK;
}

CK_RV
ckcapi_object_find(Session* sess, CK_OBJECT_HANDLE_PTR objects, 
				   CK_ULONG max_object_count, CK_ULONG_PTR object_count)
{
	Array* arr;
	size_t i;

	ASSERT(sess);
	ASSERT(object_count);
	ASSERT(!max_object_count || objects);

	if(sess->operation_type != OPERATION_FIND)
		return CKR_OPERATION_NOT_INITIALIZED;

	if(!max_object_count)
	{
		*object_count = 0;
		return CKR_OK;
	}

	arr = (Array*)sess->operation_data;
	*object_count = (max_object_count > arr->len ? arr->len : max_object_count);
	for(i = 0; i < *object_count; ++i)
		objects[i] = ckcapi_util_array_index(arr, CK_OBJECT_HANDLE, i);
	ckcapi_util_array_remove_range(arr, 0, *object_count);

	return CKR_OK;
}

CK_RV
ckcapi_objects_find_final(Session* sess)
{
	ASSERT(sess);

	if(sess->operation_type != OPERATION_FIND)
		return CKR_OPERATION_NOT_INITIALIZED;

	cleanup_find_operation(sess);
	return CKR_OK;
}