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