xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softSlotToken.c (revision bde334a8dbd66dfa70ce4d7fc9dcad6e1ae45fe4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2018, Joyent, Inc.
25  */
26 
27 #include <strings.h>
28 #include <cryptoutil.h>
29 #include <security/cryptoki.h>
30 #include <sys/crypto/common.h>
31 #include <arcfour.h>
32 #include "softGlobal.h"
33 #include "softSession.h"
34 #include <aes_impl.h>
35 #include <blowfish_impl.h>
36 #include <des_impl.h>
37 #include <ecc_impl.h>
38 #include "softDH.h"
39 #include "softObject.h"
40 #include "softKeystore.h"
41 #include "softKeystoreUtil.h"
42 
43 
44 static CK_MECHANISM_TYPE soft_mechanisms[] = {
45 	CKM_DES_CBC,
46 	CKM_DES_CBC_PAD,
47 	CKM_DES_ECB,
48 	CKM_DES_KEY_GEN,
49 	CKM_DES_MAC_GENERAL,
50 	CKM_DES_MAC,
51 	CKM_DES3_CBC,
52 	CKM_DES3_CBC_PAD,
53 	CKM_DES3_ECB,
54 	CKM_DES2_KEY_GEN,
55 	CKM_DES3_KEY_GEN,
56 	CKM_AES_CBC,
57 	CKM_AES_CBC_PAD,
58 	CKM_AES_CTR,
59 	CKM_AES_CMAC_GENERAL,
60 	CKM_AES_CMAC,
61 	CKM_AES_ECB,
62 	CKM_AES_KEY_GEN,
63 	CKM_AES_GCM,
64 	CKM_AES_CCM,
65 	CKM_BLOWFISH_CBC,
66 	CKM_BLOWFISH_KEY_GEN,
67 	CKM_SHA_1,
68 	CKM_SHA_1_HMAC,
69 	CKM_SHA_1_HMAC_GENERAL,
70 	CKM_SHA256,
71 	CKM_SHA256_HMAC,
72 	CKM_SHA256_HMAC_GENERAL,
73 	CKM_SHA384,
74 	CKM_SHA384_HMAC,
75 	CKM_SHA384_HMAC_GENERAL,
76 	CKM_SHA512,
77 	CKM_SHA512_HMAC,
78 	CKM_SHA512_HMAC_GENERAL,
79 	CKM_SSL3_SHA1_MAC,
80 	CKM_MD5,
81 	CKM_MD5_HMAC,
82 	CKM_MD5_HMAC_GENERAL,
83 	CKM_SSL3_MD5_MAC,
84 	CKM_RC4,
85 	CKM_RC4_KEY_GEN,
86 	CKM_DSA,
87 	CKM_DSA_SHA1,
88 	CKM_DSA_KEY_PAIR_GEN,
89 	CKM_RSA_PKCS,
90 	CKM_RSA_PKCS_KEY_PAIR_GEN,
91 	CKM_RSA_X_509,
92 	CKM_MD5_RSA_PKCS,
93 	CKM_SHA1_RSA_PKCS,
94 	CKM_SHA256_RSA_PKCS,
95 	CKM_SHA384_RSA_PKCS,
96 	CKM_SHA512_RSA_PKCS,
97 	CKM_DH_PKCS_KEY_PAIR_GEN,
98 	CKM_DH_PKCS_DERIVE,
99 	CKM_MD5_KEY_DERIVATION,
100 	CKM_SHA1_KEY_DERIVATION,
101 	CKM_SHA256_KEY_DERIVATION,
102 	CKM_SHA384_KEY_DERIVATION,
103 	CKM_SHA512_KEY_DERIVATION,
104 	CKM_PBE_SHA1_RC4_128,
105 	CKM_PKCS5_PBKD2,
106 	CKM_SSL3_PRE_MASTER_KEY_GEN,
107 	CKM_TLS_PRE_MASTER_KEY_GEN,
108 	CKM_SSL3_MASTER_KEY_DERIVE,
109 	CKM_TLS_MASTER_KEY_DERIVE,
110 	CKM_SSL3_MASTER_KEY_DERIVE_DH,
111 	CKM_TLS_MASTER_KEY_DERIVE_DH,
112 	CKM_SSL3_KEY_AND_MAC_DERIVE,
113 	CKM_TLS_KEY_AND_MAC_DERIVE,
114 	CKM_TLS_PRF,
115 	CKM_EC_KEY_PAIR_GEN,
116 	CKM_ECDSA,
117 	CKM_ECDSA_SHA1,
118 	CKM_ECDH1_DERIVE
119 };
120 
121 /*
122  * This is the table of CK_MECHANISM_INFO structs for the supported mechanisms.
123  * The index for this table is the same as the one above for the same
124  * mechanism.
125  * The minimum and maximum sizes of the key for the mechanism can be measured
126  * in bits or in bytes (i.e. mechanism-dependent). This table specifies the
127  * supported range of key sizes in bytes; unless noted as in bits.
128  */
129 static CK_MECHANISM_INFO soft_mechanism_info[] = {
130 	{DES_MINBYTES, DES_MAXBYTES,
131 		CKF_ENCRYPT|CKF_DECRYPT|
132 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES_CBC */
133 	{DES_MINBYTES, DES_MAXBYTES,
134 		CKF_ENCRYPT|CKF_DECRYPT|
135 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES_CBC_PAD */
136 	{DES_MINBYTES, DES_MAXBYTES,
137 		CKF_ENCRYPT|CKF_DECRYPT|
138 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES_ECB */
139 	{DES_MINBYTES, DES_MAXBYTES,
140 		CKF_GENERATE},			/* CKM_DES_KEY_GEN */
141 	{DES_MINBYTES, DES_MAXBYTES,
142 		CKF_SIGN|CKF_VERIFY},		/* CKM_DES_MAC_GENERAL */
143 	{DES_MINBYTES, DES_MAXBYTES,
144 		CKF_SIGN|CKF_VERIFY},		/* CKM_DES_MAC */
145 	{DES3_MINBYTES, DES3_MAXBYTES,
146 		CKF_ENCRYPT|CKF_DECRYPT|
147 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES3_CBC */
148 	{DES3_MINBYTES, DES3_MAXBYTES,
149 		CKF_ENCRYPT|CKF_DECRYPT|
150 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES3_CBC_PAD */
151 	{DES3_MINBYTES, DES3_MAXBYTES,
152 		CKF_ENCRYPT|CKF_DECRYPT|
153 		CKF_WRAP|CKF_UNWRAP},		/* CKM_DES3_ECB */
154 	{DES2_MAXBYTES, DES2_MAXBYTES,
155 		CKF_GENERATE},			/* CKM_DES2_KEY_GEN */
156 	{DES3_MAXBYTES, DES3_MAXBYTES,		/* CKK_DES3 only */
157 		CKF_GENERATE},			/* CKM_DES3_KEY_GEN */
158 	{AES_MINBYTES, AES_MAXBYTES,
159 		CKF_ENCRYPT|CKF_DECRYPT|
160 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_CBC */
161 	{AES_MINBYTES, AES_MAXBYTES,
162 		CKF_ENCRYPT|CKF_DECRYPT|
163 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_CBC_PAD */
164 	{AES_MINBYTES, AES_MAXBYTES,
165 		CKF_ENCRYPT|CKF_DECRYPT|
166 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_CTR */
167 	{AES_MINBYTES, AES_MAXBYTES,
168 		CKF_SIGN|CKF_VERIFY},		/* CKM_AES_CMAC_GENERAL */
169 	{AES_MINBYTES, AES_MAXBYTES,
170 		CKF_SIGN|CKF_VERIFY},		/* CKM_AES_CMAC */
171 	{AES_MINBYTES, AES_MAXBYTES,
172 		CKF_ENCRYPT|CKF_DECRYPT|
173 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_ECB */
174 	{AES_MINBYTES, AES_MAXBYTES,
175 		CKF_GENERATE},			/* CKM_AES_KEY_GEN */
176 	{AES_MINBYTES, AES_MAXBYTES,
177 		CKF_ENCRYPT|CKF_DECRYPT|
178 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_GCM */
179 	{AES_MINBYTES, AES_MAXBYTES,
180 		CKF_ENCRYPT|CKF_DECRYPT|
181 		CKF_WRAP|CKF_UNWRAP},		/* CKM_AES_CCM */
182 	{BLOWFISH_MINBYTES, BLOWFISH_MAXBYTES,
183 		CKF_ENCRYPT|CKF_DECRYPT|
184 		CKF_WRAP|CKF_UNWRAP},		/* CKM_BLOWFISH_ECB */
185 	{BLOWFISH_MINBYTES, BLOWFISH_MAXBYTES,
186 		CKF_GENERATE},			/* CKM_BLOWFISH_KEY_GEN */
187 	{0, 0, CKF_DIGEST},			/* CKM_SHA_1 */
188 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA_1_HMAC */
189 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA_1_HMAC_GENERAL */
190 	{0, 0, CKF_DIGEST},			/* CKM_SHA256 */
191 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA256_HMAC */
192 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA256_HMAC_GENERAL */
193 	{0, 0, CKF_DIGEST},			/* CKM_SHA384 */
194 	{1, 128, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA384_HMAC */
195 	{1, 128, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA384_HMAC_GENERAL */
196 	{0, 0, CKF_DIGEST},			/* CKM_SHA512 */
197 	{1, 128, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA512_HMAC */
198 	{1, 128, CKF_SIGN|CKF_VERIFY},		/* CKM_SHA512_HMAC_GENERAL */
199 	{1, 512, CKF_SIGN|CKF_VERIFY},		/* CKM_SSL3_SHA1_MAC */
200 	{0, 0, CKF_DIGEST},			/* CKM_MD5 */
201 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_MD5_HMAC */
202 	{1, 64, CKF_SIGN|CKF_VERIFY},		/* CKM_MD5_HMAC_GENERAL */
203 	{1, 512, CKF_SIGN|CKF_VERIFY},		/* CKM_SSL3_MD5_MAC */
204 	{8, ARCFOUR_MAX_KEY_BITS, CKF_ENCRYPT|CKF_DECRYPT}, /* CKM_RC4; */
205 							    /* in bits  */
206 	{8, ARCFOUR_MAX_KEY_BITS, CKF_GENERATE }, /* CKM_RC4_KEY_GEN; in bits */
207 	{512, 1024, CKF_SIGN|CKF_VERIFY},	/* CKM_DSA; in bits */
208 	{512, 1024, CKF_SIGN|CKF_VERIFY},	/* CKM_DSA_SHA1; in bits */
209 	{512, 1024, CKF_GENERATE_KEY_PAIR},	/* CKM_DSA_KEY_PAIR_GEN; */
210 						/* in bits */
211 	{256, 4096, CKF_ENCRYPT|CKF_DECRYPT|
212 		CKF_SIGN|CKF_SIGN_RECOVER|
213 		CKF_WRAP|CKF_UNWRAP|
214 		CKF_VERIFY|CKF_VERIFY_RECOVER},	/* CKM_RSA_PKCS; in bits */
215 	{256, 4096, CKF_GENERATE_KEY_PAIR},	/* CKM_RSA_PKCS_KEY_PAIR_GEN; */
216 						/* in bits */
217 	{256, 4096, CKF_ENCRYPT|CKF_DECRYPT|
218 		CKF_SIGN|CKF_SIGN_RECOVER|
219 		CKF_WRAP|CKF_UNWRAP|
220 		CKF_VERIFY|CKF_VERIFY_RECOVER},	/* CKM_RSA_X_509 in bits */
221 	{256, 4096, CKF_SIGN|CKF_VERIFY},	/* CKM_MD5_RSA_PKCS in bits */
222 	{256, 4096, CKF_SIGN|CKF_VERIFY},	/* CKM_SHA1_RSA_PKCS in bits */
223 	{256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA256_RSA_PKCS in bits */
224 	{256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA384_RSA_PKCS in bits */
225 	{256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA512_RSA_PKCS in bits */
226 	{DH_MIN_KEY_LEN, DH_MAX_KEY_LEN, CKF_GENERATE_KEY_PAIR},
227 						/* CKM_DH_PKCS_KEY_PAIR_GEN */
228 						/* in bits */
229 	{DH_MIN_KEY_LEN, DH_MAX_KEY_LEN, CKF_DERIVE},
230 						/* CKM_DH_PKCS_DERIVE; */
231 						/* in bits */
232 	{1, 16, CKF_DERIVE},			/* CKM_MD5_KEY_DERIVATION */
233 	{1, 20, CKF_DERIVE},			/* CKM_SHA1_KEY_DERIVATION */
234 	{1, 32, CKF_DERIVE},			/* CKM_SHA256_KEY_DERIVATION */
235 	{1, 48, CKF_DERIVE},			/* CKM_SHA384_KEY_DERIVATION */
236 	{1, 64, CKF_DERIVE},			/* CKM_SHA512_KEY_DERIVATION */
237 	{0, 0, CKF_GENERATE},			/* CKM_PBE_SHA1_RC4_128 */
238 	{0, 0, CKF_GENERATE},			/* CKM_PKCS5_PBKD2 */
239 	{48, 48, CKF_GENERATE},		/* CKM_SSL3_PRE_MASTER_KEY_GEN */
240 	{48, 48, CKF_GENERATE},		/* CKM_TLS_PRE_MASTER_KEY_GEN */
241 	{48, 48, CKF_DERIVE},		/* CKM_SSL3_MASTER_KEY_DERIVE */
242 	{48, 48, CKF_DERIVE},		/* CKM_TLS_MASTER_KEY_DERIVE */
243 	{48, 48, CKF_DERIVE},		/* CKM_SSL3_MASTER_KEY_DERIVE_DH */
244 	{48, 48, CKF_DERIVE},		/* CKM_TLS_MASTER_KEY_DERIVE_DH */
245 	{0, 0, CKF_DERIVE},		/* CKM_SSL3_KEY_AND_MAC_DERIVE */
246 	{0, 0, CKF_DERIVE},		/* CKM_TLS_KEY_AND_MAC_DERIVE */
247 	{0, 0, CKF_DERIVE},		/* CKM_TLS_PRF */
248 	{EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_GENERATE_KEY_PAIR},
249 	{EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_SIGN|CKF_VERIFY},
250 	{EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_SIGN|CKF_VERIFY},
251 	{EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_DERIVE}
252 };
253 
254 /*
255  * Slot ID for softtoken is always 1. tokenPresent is ignored.
256  * Also, only one slot is used.
257  */
258 /*ARGSUSED*/
259 CK_RV
260 C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
261     CK_ULONG_PTR pulCount)
262 {
263 
264 	CK_RV rv;
265 
266 	if (!softtoken_initialized)
267 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
268 
269 	if (pulCount == NULL) {
270 		return (CKR_ARGUMENTS_BAD);
271 	}
272 
273 	if (pSlotList == NULL) {
274 		/*
275 		 * Application only wants to know the number of slots.
276 		 */
277 		*pulCount = 1;
278 		return (CKR_OK);
279 	}
280 
281 	if ((*pulCount < 1) && (pSlotList != NULL)) {
282 		rv = CKR_BUFFER_TOO_SMALL;
283 	} else {
284 		pSlotList[0] = SOFTTOKEN_SLOTID;
285 		rv = CKR_OK;
286 	}
287 
288 	*pulCount = 1;
289 	return (rv);
290 }
291 
292 
293 CK_RV
294 C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
295 {
296 
297 	if (!softtoken_initialized)
298 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
299 
300 	if (pInfo == NULL)
301 		return (CKR_ARGUMENTS_BAD);
302 
303 	/* Make sure the slot ID is valid */
304 	if (slotID != SOFTTOKEN_SLOTID)
305 		return (CKR_SLOT_ID_INVALID);
306 
307 	/* Provide information about the slot in the provided buffer */
308 	(void) strncpy((char *)pInfo->slotDescription, SOFT_SLOT_DESCRIPTION,
309 	    64);
310 	(void) strncpy((char *)pInfo->manufacturerID, SOFT_MANUFACTURER_ID, 32);
311 	pInfo->flags = CKF_TOKEN_PRESENT;
312 	pInfo->hardwareVersion.major = HARDWARE_VERSION_MAJOR;
313 	pInfo->hardwareVersion.minor = HARDWARE_VERSION_MINOR;
314 	pInfo->firmwareVersion.major = FIRMWARE_VERSION_MAJOR;
315 	pInfo->firmwareVersion.minor = FIRMWARE_VERSION_MINOR;
316 
317 	return (CKR_OK);
318 }
319 
320 CK_RV
321 C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
322 {
323 	boolean_t pin_initialized = B_FALSE;
324 	char	*ks_cryptpin = NULL;
325 
326 	if (!softtoken_initialized)
327 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
328 
329 	/* Make sure the slot ID is valid */
330 	if (slotID != SOFTTOKEN_SLOTID)
331 		return (CKR_SLOT_ID_INVALID);
332 
333 	if (pInfo == NULL)
334 		return (CKR_ARGUMENTS_BAD);
335 
336 	/*
337 	 * It is intentional that we don't forward the error code
338 	 * returned from soft_keystore_pin_initialized() to the caller
339 	 */
340 	pInfo->flags = SOFT_TOKEN_FLAGS;
341 	if (soft_slot.keystore_load_status == KEYSTORE_UNAVAILABLE) {
342 		pInfo->flags |= CKF_WRITE_PROTECTED;
343 	} else {
344 		if ((soft_keystore_pin_initialized(&pin_initialized,
345 		    &ks_cryptpin, B_FALSE) == CKR_OK) && !pin_initialized)
346 			pInfo->flags |= CKF_USER_PIN_TO_BE_CHANGED;
347 	}
348 
349 	if (ks_cryptpin != NULL) {
350 		size_t cplen = strlen(ks_cryptpin) + 1;
351 
352 		freezero(ks_cryptpin, cplen);
353 	}
354 
355 	/* Provide information about a token in the provided buffer */
356 	(void) strncpy((char *)pInfo->label, SOFT_TOKEN_LABEL, 32);
357 	(void) strncpy((char *)pInfo->manufacturerID, SOFT_MANUFACTURER_ID, 32);
358 	(void) strncpy((char *)pInfo->model, TOKEN_MODEL, 16);
359 	(void) strncpy((char *)pInfo->serialNumber, SOFT_TOKEN_SERIAL, 16);
360 
361 	pInfo->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
362 	pInfo->ulSessionCount = soft_session_cnt;
363 	pInfo->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
364 	pInfo->ulRwSessionCount = soft_session_rw_cnt;
365 	pInfo->ulMaxPinLen = MAX_PIN_LEN;
366 	pInfo->ulMinPinLen = MIN_PIN_LEN;
367 	pInfo->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
368 	pInfo->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
369 	pInfo->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
370 	pInfo->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
371 	pInfo->hardwareVersion.major = HARDWARE_VERSION_MAJOR;
372 	pInfo->hardwareVersion.minor = HARDWARE_VERSION_MINOR;
373 	pInfo->firmwareVersion.major = FIRMWARE_VERSION_MAJOR;
374 	pInfo->firmwareVersion.minor = FIRMWARE_VERSION_MINOR;
375 	(void) memset(pInfo->utcTime, ' ', 16);
376 
377 	return (CKR_OK);
378 }
379 
380 /*ARGSUSED*/
381 CK_RV
382 C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved)
383 {
384 	if (!softtoken_initialized)
385 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
386 
387 	/*
388 	 * This is currently not implemented, however we could cause this
389 	 * to wait for the token files to appear if soft_token_present is
390 	 * false.
391 	 * However there is currently no polite and portable way to do that
392 	 * because we might not even be able to get to an fd to the
393 	 * parent directory, so instead we don't support any slot events.
394 	 */
395 	return (CKR_FUNCTION_NOT_SUPPORTED);
396 }
397 
398 
399 CK_RV
400 C_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
401     CK_ULONG_PTR pulCount)
402 {
403 
404 	ulong_t i;
405 	ulong_t mechnum;
406 
407 	if (!softtoken_initialized)
408 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
409 
410 	if (slotID != SOFTTOKEN_SLOTID)
411 		return (CKR_SLOT_ID_INVALID);
412 
413 	mechnum = sizeof (soft_mechanisms) / sizeof (CK_MECHANISM_TYPE);
414 
415 	if (pMechanismList == NULL) {
416 		/*
417 		 * Application only wants to know the number of
418 		 * supported mechanism types.
419 		 */
420 		*pulCount = mechnum;
421 		return (CKR_OK);
422 	}
423 
424 	if (*pulCount < mechnum) {
425 		*pulCount = mechnum;
426 		return (CKR_BUFFER_TOO_SMALL);
427 	}
428 
429 	for (i = 0; i < mechnum; i++) {
430 		pMechanismList[i] = soft_mechanisms[i];
431 	}
432 
433 	*pulCount = mechnum;
434 
435 	return (CKR_OK);
436 }
437 
438 
439 CK_RV
440 C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
441     CK_MECHANISM_INFO_PTR pInfo)
442 {
443 
444 	ulong_t i;
445 	ulong_t mechnum;
446 
447 	if (!softtoken_initialized)
448 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
449 
450 	if (slotID != SOFTTOKEN_SLOTID)
451 		return (CKR_SLOT_ID_INVALID);
452 
453 	if (pInfo == NULL) {
454 		return (CKR_ARGUMENTS_BAD);
455 	}
456 
457 	mechnum = sizeof (soft_mechanisms) / sizeof (CK_MECHANISM_TYPE);
458 	for (i = 0; i < mechnum; i++) {
459 		if (soft_mechanisms[i] == type)
460 			break;
461 	}
462 
463 	if (i == mechnum)
464 		/* unsupported mechanism */
465 		return (CKR_MECHANISM_INVALID);
466 
467 	pInfo->ulMinKeySize = soft_mechanism_info[i].ulMinKeySize;
468 	pInfo->ulMaxKeySize = soft_mechanism_info[i].ulMaxKeySize;
469 	pInfo->flags = soft_mechanism_info[i].flags;
470 
471 	return (CKR_OK);
472 }
473 
474 
475 /*ARGSUSED*/
476 CK_RV
477 C_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
478     CK_UTF8CHAR_PTR pLabel)
479 {
480 	if (!softtoken_initialized)
481 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
482 
483 	if (create_keystore() != 0)
484 		return (CKR_FUNCTION_FAILED);
485 
486 	return (CKR_OK);
487 }
488 
489 /*ARGSUSED*/
490 CK_RV
491 C_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
492 {
493 	if (!softtoken_initialized)
494 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
495 
496 	return (CKR_FUNCTION_NOT_SUPPORTED);
497 }
498 
499 
500 CK_RV
501 C_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
502     CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen)
503 {
504 
505 	soft_session_t *session_p;
506 	CK_RV rv;
507 	boolean_t lock_held = B_FALSE;
508 
509 	if (!softtoken_initialized)
510 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
511 
512 	/*
513 	 * Obtain the session pointer. Also, increment the session
514 	 * reference count.
515 	 */
516 	rv = handle2session(hSession, &session_p);
517 	if (rv != CKR_OK)
518 		return (rv);
519 
520 	if (!soft_keystore_status(KEYSTORE_LOAD)) {
521 		SES_REFRELE(session_p, lock_held);
522 		return (CKR_DEVICE_REMOVED);
523 	}
524 
525 	if ((ulOldPinLen < MIN_PIN_LEN) || (ulOldPinLen > MAX_PIN_LEN) ||
526 	    (ulNewPinLen < MIN_PIN_LEN) ||(ulNewPinLen > MAX_PIN_LEN)) {
527 		SES_REFRELE(session_p, lock_held);
528 		return (CKR_PIN_LEN_RANGE);
529 	}
530 
531 	if ((pOldPin == NULL_PTR) || (pNewPin == NULL_PTR)) {
532 		/*
533 		 * We don't support CKF_PROTECTED_AUTHENTICATION_PATH
534 		 */
535 		SES_REFRELE(session_p, lock_held);
536 		return (CKR_ARGUMENTS_BAD);
537 	}
538 
539 	/* check the state of the session */
540 	if ((session_p->state != CKS_RW_PUBLIC_SESSION) &&
541 	    (session_p->state != CKS_RW_USER_FUNCTIONS)) {
542 		SES_REFRELE(session_p, lock_held);
543 		return (CKR_SESSION_READ_ONLY);
544 	}
545 
546 	rv = soft_setpin(pOldPin, ulOldPinLen, pNewPin, ulNewPinLen);
547 
548 	SES_REFRELE(session_p, lock_held);
549 	return (rv);
550 }
551