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