summaryrefslogtreecommitdiff
path: root/usr/src/lib/pkcs11/libpkcs11/common/pkcs11SUNWExtensions.c
blob: 882ca9f8bdb6f55d17798dd0386ce96909e0cc4d (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Solaris specific functions to reduce the initialization
 * overhead of using PKCS #11
 */

#include <stdlib.h>
#include <sys/types.h>
#include <security/cryptoki.h>
#include <assert.h>
#include <cryptoutil.h>
#include <pkcs11Global.h>

static CK_OBJECT_CLASS objclass = CKO_SECRET_KEY;
static CK_BBOOL falsevalue = FALSE;
static CK_BBOOL truevalue = TRUE;

#define	NUM_SECRETKEY_ATTRS	12

typedef struct _ATTRTYPE_MECHINFO_MAPPING {
	CK_ATTRIBUTE_TYPE attr;
	CK_FLAGS	flag;
} ATTRTYPE_MECHINFO_MAPPING;

/* possible attribute types for creating key */
ATTRTYPE_MECHINFO_MAPPING mapping[] = {
	{CKA_ENCRYPT, CKF_ENCRYPT},
	{CKA_DECRYPT, CKF_DECRYPT},
	{CKA_SIGN, CKF_SIGN},
	{CKA_VERIFY, CKF_VERIFY},
	{CKA_WRAP, CKF_WRAP},
	{CKA_UNWRAP, CKF_UNWRAP}
};


/*
 * List of mechanisms that only supports asymmetric key operations
 * in PKCS #11 V2.11
 */
CK_MECHANISM_TYPE asymmetric_mechs[] = {
	CKM_RSA_PKCS_KEY_PAIR_GEN, CKM_RSA_PKCS, CKM_RSA_9796, CKM_RSA_X_509,
	CKM_RSA_PKCS_OAEP, CKM_RSA_X9_31_KEY_PAIR_GEN, CKM_RSA_X9_31,
	CKM_RSA_PKCS_PSS, CKM_DSA_KEY_PAIR_GEN, CKM_DSA, CKM_DSA_SHA1,
	CKM_DSA_PARAMETER_GEN, CKM_ECDSA_KEY_PAIR_GEN, CKM_EC_KEY_PAIR_GEN,
	CKM_ECDSA, CKM_ECDSA_SHA1, CKM_ECDH1_DERIVE,
	CKM_ECDH1_COFACTOR_DERIVE, CKM_ECMQV_DERIVE
};


typedef struct _KEY_TYPE_SIZE_MAPPING {
	CK_KEY_TYPE type;
	CK_ULONG len;
} KEY_TYPE_SIZE_MAPPING;

/*
 * List of secret key types that have fixed sizes and their sizes.
 * These key types do not allow CKA_VALUE_LEN for key generation.
 * The sizes are in bytes.
 *
 * Discrete-sized keys, such as AES and Twofish, and variable sized
 * keys, such as Blowfish, are not in this list.
 */
KEY_TYPE_SIZE_MAPPING fixed_size_secrets[] = {
	{CKK_DES, 8}, {CKK_DES2, 16}, {CKK_DES3, 24}, {CKK_IDEA, 16},
	{CKK_CDMF, 8}, {CKK_SKIPJACK, 12}, {CKK_BATON, 40}, {CKK_JUNIPER, 40}
};

/*
 * match_mech is an example of many possible criteria functions.
 * It matches the given mech type (in args) with the slot's mech info.
 * If no match is found, pkcs11_GetCriteriaSession is asked to return
 * CKR_MECHANISM_INVALID.
 */
boolean_t
match_mech(CK_SLOT_ID slot_id, void *args, CK_RV *rv)
{
	CK_MECHANISM_INFO mech_info;
	CK_MECHANISM_TYPE mech = (CK_MECHANISM_TYPE)args;

	*rv = CKR_MECHANISM_INVALID;
	return (C_GetMechanismInfo(slot_id, mech, &mech_info) == CKR_OK);
}

/*
 * pkcs11_GetCriteriaSession will initialize the framework and do all
 * the necessary work of calling C_GetSlotList(), C_GetMechanismInfo()
 * C_OpenSession() to create a session that meets all the criteria in
 * the given function pointer.
 *
 * The criteria function must return a boolean value of true or false.
 * The arguments to the function are the current slot id, an opaque
 * args value that is passed through to the function, and the error
 * value pkcs11_GetCriteriaSession should return if no slot id meets the
 * criteria.
 *
 * If the function is called multiple times, it will return a new session
 * without reinitializing the framework.
 */
CK_RV
pkcs11_GetCriteriaSession(
    boolean_t (*criteria)(CK_SLOT_ID slot_id, void *args, CK_RV *rv),
    void *args, CK_SESSION_HANDLE_PTR hSession)
{
	CK_RV rv;
	CK_ULONG slotcount;
	CK_SLOT_ID_PTR slot_list;
	CK_SLOT_ID slot_id;
	CK_ULONG i;

	if (hSession == NULL || criteria == NULL) {
		return (CKR_ARGUMENTS_BAD);
	}

	/* initialize PKCS #11 */
	if (!pkcs11_initialized) {
		rv = C_Initialize(NULL);
		if ((rv != CKR_OK) &&
		    (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
			return (rv);
		}
	}

	/* get slot count */
	rv = C_GetSlotList(0, NULL, &slotcount);
	if (rv != CKR_OK) {
		return (rv);
	}

	if (slotcount == 0) {
		return (CKR_FUNCTION_FAILED);
	}


	/* allocate memory for slot list */
	slot_list = malloc(slotcount * sizeof (CK_SLOT_ID));
	if (slot_list == NULL) {
		return (CKR_HOST_MEMORY);
	}

	if ((rv = C_GetSlotList(0, slot_list, &slotcount)) != CKR_OK) {
		free(slot_list);
		return (rv);
	}

	/* find slot with matching criteria */
	for (i = 0; i < slotcount; i++) {
		slot_id = slot_list[i];
		if ((*criteria)(slot_id, args, &rv)) {
			break;
		}
	}

	if (i == slotcount) {
		/* no matching slot found */
		free(slot_list);
		return (rv);	/* this rv is from the criteria function */
	}

	rv = C_OpenSession(slot_id, CKF_SERIAL_SESSION, NULL,
	    NULL, hSession);

	free(slot_list);
	return (rv);
}

/*
 * SUNW_C_GetMechSession will initialize the framework and do all
 * of the neccessary work of calling C_GetSlotList(), C_GetMechanismInfo()
 * C_OpenSession() to create a session capable of providing the requested
 * mechanism.
 *
 * If the function is called multiple times, it will return a new session
 * without reinitializing the framework.
 */
CK_RV
SUNW_C_GetMechSession(CK_MECHANISM_TYPE mech, CK_SESSION_HANDLE_PTR hSession)
{
	/*
	 * All the code in this function can be replaced with one line:
	 *
	 * return (pkcs11_GetCriteriaSession(match_mech, (void *)mech,
	 *	hSession));
	 *
	 */
	CK_RV rv;
	CK_ULONG slotcount;
	CK_SLOT_ID_PTR slot_list;
	CK_SLOT_ID slot_id;
	CK_MECHANISM_INFO mech_info;
	CK_ULONG i;

	if (hSession == NULL) {
		return (CKR_ARGUMENTS_BAD);
	}

	/* initialize PKCS #11 */
	if (!pkcs11_initialized) {
		rv = C_Initialize(NULL);
		if ((rv != CKR_OK) &&
		    (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
			return (rv);
		}
	}

	/* get slot count */
	rv = C_GetSlotList(0, NULL, &slotcount);
	if (rv != CKR_OK) {
		return (rv);
	}

	if (slotcount == 0) {
		return (CKR_FUNCTION_FAILED);
	}


	/* allocate memory for slot list */
	slot_list = malloc(slotcount * sizeof (CK_SLOT_ID));
	if (slot_list == NULL) {
		return (CKR_HOST_MEMORY);
	}

	if ((rv = C_GetSlotList(0, slot_list, &slotcount)) != CKR_OK) {
		free(slot_list);
		return (rv);
	}

	/* find slot with matching mechanism */
	for (i = 0; i < slotcount; i++) {
		slot_id = slot_list[i];
		if (C_GetMechanismInfo(slot_id, mech, &mech_info) == CKR_OK) {
			/* found mechanism */
			break;
		}
	}

	if (i == slotcount) {
		/* no matching mechanism found */
		free(slot_list);
		return (CKR_MECHANISM_INVALID);
	}

	rv = C_OpenSession(slot_id, CKF_SERIAL_SESSION, NULL,
	    NULL, hSession);

	free(slot_list);
	return (rv);
}

/*
 * SUNW_C_KeyToObject creates a secret key object for the given
 * mechanism from the rawkey data.
 */
CK_RV
SUNW_C_KeyToObject(CK_SESSION_HANDLE hSession, CK_MECHANISM_TYPE mech,
    const void *rawkey, size_t rawkey_len, CK_OBJECT_HANDLE_PTR obj)
{

	CK_RV rv;
	CK_SESSION_INFO session_info;
	CK_SLOT_ID slot_id;
	CK_MECHANISM_INFO mech_info;
	CK_ULONG i, j;
	CK_KEY_TYPE keytype;
	CK_ULONG num_asym_mechs, num_mapping;

	/* template for creating generic secret key object */
	CK_ATTRIBUTE template[NUM_SECRETKEY_ATTRS];

	if ((hSession == CK_INVALID_HANDLE) || (obj == NULL) ||
	    (rawkey == NULL) || (rawkey_len == 0)) {
		return (CKR_ARGUMENTS_BAD);
	}

	/*
	 * Check to make sure mechanism type is not for asymmetric key
	 * only operations.  This function is only applicable to
	 * generating secret key.
	 */
	num_asym_mechs = sizeof (asymmetric_mechs) / sizeof (CK_MECHANISM_TYPE);
	for (i = 0; i < num_asym_mechs; i++) {
		if (mech == asymmetric_mechs[i]) {
			return (CKR_MECHANISM_INVALID);
		}
	}

	rv = C_GetSessionInfo(hSession, &session_info);
	if (rv != CKR_OK) {
		return (rv);
	}

	slot_id = session_info.slotID;

	i = 0;
	template[i].type = CKA_CLASS;
	template[i].pValue = &objclass;
	template[i].ulValueLen = sizeof (objclass);
	i++;

	/* get the key type for this mechanism */
	if ((rv = pkcs11_mech2keytype(mech, &keytype)) != CKR_OK) {
		return (rv);
	}

	assert(i < NUM_SECRETKEY_ATTRS);
	template[i].type = CKA_KEY_TYPE;
	template[i].pValue = &keytype;
	template[i].ulValueLen = sizeof (keytype);
	i++;

	rv = C_GetMechanismInfo(slot_id, mech, &mech_info);
	if (rv != CKR_OK) {
		return (rv);
	}

	/* set the attribute type flag on object based on mechanism */
	num_mapping = sizeof (mapping) / sizeof (ATTRTYPE_MECHINFO_MAPPING);
	for (j = 0; j < num_mapping; j++) {
		assert(i < NUM_SECRETKEY_ATTRS);
		template[i].type = mapping[j].attr;
		template[i].ulValueLen = sizeof (falsevalue);
		if (mech_info.flags & ((mapping[j]).flag)) {
			template[i].pValue = &truevalue;
		} else {
			template[i].pValue = &falsevalue;
		}
		i++;
	}

	assert(i < NUM_SECRETKEY_ATTRS);
	template[i].type = CKA_TOKEN;
	template[i].pValue = &falsevalue;
	template[i].ulValueLen = sizeof (falsevalue);
	i++;

	assert(i < NUM_SECRETKEY_ATTRS);
	template[i].type = CKA_VALUE;
	template[i].pValue = (CK_VOID_PTR)rawkey;
	template[i].ulValueLen = (CK_ULONG)rawkey_len;
	i++;

	rv = C_CreateObject(hSession, template, i, obj);
	return (rv);
}


/*
 * pkcs11_PasswdToPBKD2Object will create a secret key from the given string
 * (e.g. passphrase) using PKCS#5 Password-Based Key Derivation Function 2
 * (PBKD2).
 *
 * Session must be open.  Salt and iterations use defaults.
 */
CK_RV
pkcs11_PasswdToPBKD2Object(CK_SESSION_HANDLE hSession, char *passphrase,
    size_t passphrase_len, void *salt, size_t salt_len, CK_ULONG iterations,
    CK_KEY_TYPE key_type, CK_ULONG key_len, CK_FLAGS key_flags,
    CK_OBJECT_HANDLE_PTR obj)
{
	CK_RV rv;
	CK_PKCS5_PBKD2_PARAMS params;
	CK_MECHANISM mechanism;
	CK_KEY_TYPE asym_key_type;
	CK_ULONG i, j, num_asym_mechs, num_fixed_secs, num_mapping;
	CK_ATTRIBUTE template[NUM_SECRETKEY_ATTRS];

	if (hSession == CK_INVALID_HANDLE || obj == NULL ||
	    passphrase == NULL || passphrase_len == 0 ||
	    iterations == 0UL) {
		return (CKR_ARGUMENTS_BAD);
	}

	/*
	 * Check to make sure key type is not asymmetric.  This function
	 * is only applicable to generating secret key.
	 */
	num_asym_mechs = sizeof (asymmetric_mechs) / sizeof (CK_MECHANISM_TYPE);
	for (i = 0; i < num_asym_mechs; i++) {
		rv = pkcs11_mech2keytype(asymmetric_mechs[i], &asym_key_type);
		assert(rv == CKR_OK);
		if (key_type == asym_key_type) {
			return (CKR_KEY_TYPE_INCONSISTENT);
		}
	}

	/*
	 * Key length must either be 0 or the correct size for PBKD of
	 * fixed-size secret keys.  However, underlying key generation
	 * cannot have CKA_VALUE_LEN set for the key length attribute.
	 */
	num_fixed_secs =
	    sizeof (fixed_size_secrets) / sizeof (KEY_TYPE_SIZE_MAPPING);
	for (i = 0; i < num_fixed_secs; i++) {
		if (key_type == fixed_size_secrets[i].type) {
			if (key_len == fixed_size_secrets[i].len) {
				key_len = 0;
			}
			if (key_len == 0) {
				break;
			}
			return (CKR_KEY_SIZE_RANGE);
		}
	}

	if (salt == NULL || salt_len == 0) {
		params.saltSource = 0;
		params.pSaltSourceData = NULL;
		params.ulSaltSourceDataLen = 0;
	} else {
		params.saltSource = CKZ_SALT_SPECIFIED;
		params.pSaltSourceData = salt;
		params.ulSaltSourceDataLen = salt_len;
	}
	params.iterations = iterations;
	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
	params.pPrfData = NULL;
	params.ulPrfDataLen = 0;
	params.pPassword = (CK_UTF8CHAR_PTR)passphrase;
	params.ulPasswordLen = (CK_ULONG_PTR)&passphrase_len;
	/*
	 * PKCS#11 spec error, ulPasswordLen should have been pulPasswordLen,
	 * or its type should have been CK_ULONG instead of CK_ULONG_PTR,
	 * but it's legacy now
	 */

	mechanism.mechanism = CKM_PKCS5_PBKD2;
	mechanism.pParameter = &params;
	mechanism.ulParameterLen = sizeof (params);

	i = 0;
	template[i].type = CKA_CLASS;
	template[i].pValue = &objclass;
	template[i].ulValueLen = sizeof (objclass);
	i++;

	assert(i < NUM_SECRETKEY_ATTRS);
	template[i].type = CKA_KEY_TYPE;
	template[i].pValue = &key_type;
	template[i].ulValueLen = sizeof (key_type);
	i++;

	assert(i < NUM_SECRETKEY_ATTRS);
	template[i].type = CKA_TOKEN;
	template[i].pValue = &falsevalue;
	template[i].ulValueLen = sizeof (falsevalue);
	i++;

	if (key_len != 0) {
		assert(i < NUM_SECRETKEY_ATTRS);
		template[i].type = CKA_VALUE_LEN;
		template[i].pValue = &key_len;
		template[i].ulValueLen = sizeof (key_len);
		i++;
	}

	/*
	 * C_GenerateKey may not implicitly set capability attributes,
	 * e.g. CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP, ...
	 */
	num_mapping = sizeof (mapping) / sizeof (ATTRTYPE_MECHINFO_MAPPING);
	for (j = 0; j < num_mapping; j++) {
		assert(i < NUM_SECRETKEY_ATTRS);
		template[i].type = mapping[j].attr;
		template[i].pValue = (key_flags & ((mapping[j]).flag)) ?
		    &truevalue : &falsevalue;
		template[i].ulValueLen = sizeof (falsevalue);
		i++;
	}

	rv = C_GenerateKey(hSession, &mechanism, template, i, obj);
	return (rv);
}

/*
 * pkcs11_ObjectToKey gets the rawkey data from a secret key object.
 * The caller is responsible to free the allocated rawkey data.
 *
 * Optionally the object can be destroyed after the value is retrieved.
 * As an example, after using pkcs11_PasswdToPBKD2Object() to create a
 * secret key object from a passphrase, an app may call pkcs11_ObjectToKey
 * to get the rawkey data.  The intermediate object may no longer be needed
 * and should be destroyed.
 */
CK_RV
pkcs11_ObjectToKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE obj,
    void **rawkey, size_t *rawkey_len, boolean_t destroy_obj)
{
	CK_RV rv;
	CK_ATTRIBUTE template;

	if (hSession == CK_INVALID_HANDLE)
		return (CKR_SESSION_HANDLE_INVALID);
	if (obj == 0)
		return (CKR_OBJECT_HANDLE_INVALID);
	if (rawkey == NULL || rawkey_len == NULL)
		return (CKR_ARGUMENTS_BAD);

	template.type = CKA_VALUE;
	template.pValue = NULL;
	template.ulValueLen = 0;

	/* First get the size of the rawkey */
	rv = C_GetAttributeValue(hSession, obj, &template, 1);
	if (rv != CKR_OK) {
		return (rv);
	}

	template.pValue = malloc(template.ulValueLen);
	if (template.pValue == NULL) {
		return (CKR_HOST_MEMORY);
	}

	/* Then get the rawkey data */
	rv = C_GetAttributeValue(hSession, obj, &template, 1);
	if (rv != CKR_OK) {
		free(template.pValue);
		return (rv);
	}

	if (destroy_obj) {
		/*
		 * Could have asserted rv == CKR_OK, making threaded
		 * apps that share objects see stars.  Here mercy is ok.
		 */
		(void) C_DestroyObject(hSession, obj);
	}

	*rawkey = template.pValue;
	*rawkey_len = template.ulValueLen;

	return (CKR_OK);
}

/*
 * pkcs11_PasswdToKey will create PKCS#5 PBKD2 rawkey data from the
 * given passphrase.  The caller is responsible to free the allocated
 * rawkey data.
 */
CK_RV
pkcs11_PasswdToKey(CK_SESSION_HANDLE hSession, char *passphrase,
    size_t passphrase_len, void *salt, size_t salt_len, CK_KEY_TYPE key_type,
    CK_ULONG key_len, void **rawkey, size_t *rawkey_len)
{
	CK_RV rv;
	CK_OBJECT_HANDLE obj;

	rv = pkcs11_PasswdToPBKD2Object(hSession, passphrase, passphrase_len,
	    salt, salt_len, CK_PKCS5_PBKD2_ITERATIONS, key_type, key_len, 0,
	    &obj);
	if (rv != CKR_OK)
		return (rv);
	rv = pkcs11_ObjectToKey(hSession, obj, rawkey, rawkey_len, B_TRUE);
	return (rv);
}