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