17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5034448feSmcpowers  * Common Development and Distribution License (the "License").
6034448feSmcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227b79d846SDina K Nimeh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24cfcec266SJason King  *
25cfcec266SJason King  * Copyright 2020 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <pthread.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
3423c57df7Smcpowers #include <sys/crypto/common.h>
357c478bd9Sstevel@tonic-gate #include <aes_impl.h>
36f66d273dSizick #include <blowfish_impl.h>
377c478bd9Sstevel@tonic-gate #include <des_impl.h>
387c478bd9Sstevel@tonic-gate #include <arcfour.h>
397b79d846SDina K Nimeh #include <cryptoutil.h>
407c478bd9Sstevel@tonic-gate #include "softGlobal.h"
417c478bd9Sstevel@tonic-gate #include "softSession.h"
427c478bd9Sstevel@tonic-gate #include "softObject.h"
437c478bd9Sstevel@tonic-gate #include "softDSA.h"
447c478bd9Sstevel@tonic-gate #include "softRSA.h"
457c478bd9Sstevel@tonic-gate #include "softDH.h"
46f9fbec18Smcpowers #include "softEC.h"
477c478bd9Sstevel@tonic-gate #include "softMAC.h"
487c478bd9Sstevel@tonic-gate #include "softOps.h"
497c478bd9Sstevel@tonic-gate #include "softKeys.h"
507c478bd9Sstevel@tonic-gate #include "softKeystore.h"
517c478bd9Sstevel@tonic-gate #include "softSSL.h"
527c478bd9Sstevel@tonic-gate #include "softASN1.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	local_min(a, b)	((a) < (b) ? (a) : (b))
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static CK_RV
587c478bd9Sstevel@tonic-gate soft_pkcs12_pbe(soft_session_t *, CK_MECHANISM_PTR, soft_object_t *);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Create a temporary key object struct by filling up its template attributes.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate CK_RV
soft_gen_keyobject(CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,soft_object_t ** objp,soft_session_t * sp,CK_OBJECT_CLASS class,CK_KEY_TYPE key_type,CK_ULONG keylen,CK_ULONG mode,boolean_t internal)647c478bd9Sstevel@tonic-gate soft_gen_keyobject(CK_ATTRIBUTE_PTR pTemplate,  CK_ULONG ulCount,
65cfcec266SJason King     soft_object_t **objp, soft_session_t *sp,
667c478bd9Sstevel@tonic-gate     CK_OBJECT_CLASS class, CK_KEY_TYPE key_type, CK_ULONG keylen, CK_ULONG mode,
677c478bd9Sstevel@tonic-gate     boolean_t internal)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	CK_RV rv;
707c478bd9Sstevel@tonic-gate 	soft_object_t *new_objp = NULL;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	new_objp = calloc(1, sizeof (soft_object_t));
737c478bd9Sstevel@tonic-gate 	if (new_objp == NULL) {
747c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	new_objp->extra_attrlistp = NULL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * Validate attribute template and fill in the attributes
817c478bd9Sstevel@tonic-gate 	 * in the soft_object_t.
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	rv = soft_build_key(pTemplate, ulCount, new_objp, class, key_type,
847c478bd9Sstevel@tonic-gate 	    keylen, mode);
857c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
867c478bd9Sstevel@tonic-gate 		goto fail_cleanup1;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * If generating a key is an internal request (i.e. not a C_XXX
917c478bd9Sstevel@tonic-gate 	 * API request), then skip the following checks.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	if (!internal) {
947c478bd9Sstevel@tonic-gate 		rv = soft_pin_expired_check(new_objp);
957c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
967c478bd9Sstevel@tonic-gate 			goto fail_cleanup2;
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		rv = soft_object_write_access_check(sp, new_objp);
1007c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
1017c478bd9Sstevel@tonic-gate 			goto fail_cleanup2;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Initialize the rest of stuffs in soft_object_t. */
1067c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&new_objp->object_mutex, NULL);
1077c478bd9Sstevel@tonic-gate 	new_objp->magic_marker = SOFTTOKEN_OBJECT_MAGIC;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* Write the new token object to the keystore */
1107c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(new_objp)) {
1117c478bd9Sstevel@tonic-gate 		new_objp->version = 1;
112cfcec266SJason King 		new_objp->session_handle = CK_INVALID_HANDLE;
1137c478bd9Sstevel@tonic-gate 		soft_add_token_object_to_slot(new_objp);
114cfcec266SJason King 
115cfcec266SJason King 		set_objecthandle(new_objp);
116cfcec266SJason King 		*objp = new_objp;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		return (CKR_OK);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
121cfcec266SJason King 	new_objp->session_handle = sp->handle;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	/* Add the new object to the session's object list. */
1247c478bd9Sstevel@tonic-gate 	soft_add_object_to_session(new_objp, sp);
1257c478bd9Sstevel@tonic-gate 
126cfcec266SJason King 	set_objecthandle(new_objp);
127cfcec266SJason King 	*objp = new_objp;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate fail_cleanup2:
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * When any error occurs after soft_build_key(), we will need to
1347c478bd9Sstevel@tonic-gate 	 * clean up the memory allocated by the soft_build_key().
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	soft_cleanup_object(new_objp);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate fail_cleanup1:
1397c478bd9Sstevel@tonic-gate 	if (new_objp) {
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		 * The storage allocated inside of this object should have
1427c478bd9Sstevel@tonic-gate 		 * been cleaned up by the soft_build_key() if it failed.
1437c478bd9Sstevel@tonic-gate 		 * Therefore, we can safely free the object.
1447c478bd9Sstevel@tonic-gate 		 */
1457c478bd9Sstevel@tonic-gate 		free(new_objp);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (rv);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate CK_RV
soft_genkey(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,CK_OBJECT_HANDLE_PTR phKey)1527c478bd9Sstevel@tonic-gate soft_genkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
1537c478bd9Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1577c478bd9Sstevel@tonic-gate 	soft_object_t *secret_key;
1587c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE key_type;
1597c478bd9Sstevel@tonic-gate 	CK_ULONG keylen = 0;
1607c478bd9Sstevel@tonic-gate 	CK_ULONG i;
1617c478bd9Sstevel@tonic-gate 	int des_strength = 0;
1627c478bd9Sstevel@tonic-gate 	int retry = 0;
1637c478bd9Sstevel@tonic-gate 	int keyfound = 0;
1647c478bd9Sstevel@tonic-gate 	boolean_t is_ssl_mech = B_FALSE;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
1677c478bd9Sstevel@tonic-gate 	case CKM_DES_KEY_GEN:
1687c478bd9Sstevel@tonic-gate 		key_type = CKK_DES;
1697c478bd9Sstevel@tonic-gate 		break;
1707c478bd9Sstevel@tonic-gate 
171436935a1SVladimir Kotal 	case CKM_DES2_KEY_GEN:
172436935a1SVladimir Kotal 		key_type = CKK_DES2;
173436935a1SVladimir Kotal 		break;
174436935a1SVladimir Kotal 
1757c478bd9Sstevel@tonic-gate 	case CKM_DES3_KEY_GEN:
1767c478bd9Sstevel@tonic-gate 		key_type = CKK_DES3;
1777c478bd9Sstevel@tonic-gate 		break;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	case CKM_AES_KEY_GEN:
1807c478bd9Sstevel@tonic-gate 		key_type = CKK_AES;
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 
183f66d273dSizick 	case CKM_BLOWFISH_KEY_GEN:
184f66d273dSizick 		key_type = CKK_BLOWFISH;
185f66d273dSizick 		break;
186f66d273dSizick 
1877c478bd9Sstevel@tonic-gate 	case CKM_RC4_KEY_GEN:
1887c478bd9Sstevel@tonic-gate 		key_type = CKK_RC4;
1897c478bd9Sstevel@tonic-gate 		break;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	case CKM_SSL3_PRE_MASTER_KEY_GEN:
1927c478bd9Sstevel@tonic-gate 	case CKM_TLS_PRE_MASTER_KEY_GEN:
1937c478bd9Sstevel@tonic-gate 		if (pMechanism->pParameter == NULL ||
1947c478bd9Sstevel@tonic-gate 		    pMechanism->ulParameterLen != sizeof (CK_VERSION))
1957c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCOMPLETE);
1967c478bd9Sstevel@tonic-gate 		is_ssl_mech = B_TRUE;
1977c478bd9Sstevel@tonic-gate 		key_type = CKK_GENERIC_SECRET;
1987c478bd9Sstevel@tonic-gate 		keylen = 48;
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	case CKM_PKCS5_PBKD2:
2027c478bd9Sstevel@tonic-gate 		keyfound = 0;
2037c478bd9Sstevel@tonic-gate 		for (i = 0; i < ulCount && !keyfound; i++) {
2047c478bd9Sstevel@tonic-gate 			if (pTemplate[i].type == CKA_KEY_TYPE &&
2057c478bd9Sstevel@tonic-gate 			    pTemplate[i].pValue != NULL) {
2067c478bd9Sstevel@tonic-gate 				key_type = *((CK_KEY_TYPE*)pTemplate[i].pValue);
2077c478bd9Sstevel@tonic-gate 				keyfound = 1;
2087c478bd9Sstevel@tonic-gate 			}
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		if (!keyfound)
2117c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCOMPLETE);
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * Make sure that parameters were given for this
2147c478bd9Sstevel@tonic-gate 		 * mechanism.
2157c478bd9Sstevel@tonic-gate 		 */
2167c478bd9Sstevel@tonic-gate 		if (pMechanism->pParameter == NULL ||
2177c478bd9Sstevel@tonic-gate 		    pMechanism->ulParameterLen !=
2187c478bd9Sstevel@tonic-gate 		    sizeof (CK_PKCS5_PBKD2_PARAMS))
2197c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCOMPLETE);
2207c478bd9Sstevel@tonic-gate 		break;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	case CKM_PBE_SHA1_RC4_128:
2237c478bd9Sstevel@tonic-gate 		keyfound = 0;
2247c478bd9Sstevel@tonic-gate 		for (i = 0; i < ulCount; i++) {
2257c478bd9Sstevel@tonic-gate 			if (pTemplate[i].type == CKA_KEY_TYPE &&
2267c478bd9Sstevel@tonic-gate 			    pTemplate[i].pValue != NULL) {
2277c478bd9Sstevel@tonic-gate 				key_type = *((CK_KEY_TYPE*)pTemplate[i].pValue);
2287c478bd9Sstevel@tonic-gate 				keyfound = 1;
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 			if (pTemplate[i].type == CKA_VALUE_LEN &&
2317c478bd9Sstevel@tonic-gate 			    pTemplate[i].pValue != NULL) {
2327c478bd9Sstevel@tonic-gate 				keylen = *((CK_ULONG*)pTemplate[i].pValue);
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		/* If a keytype was specified, it had better be CKK_RC4 */
2367c478bd9Sstevel@tonic-gate 		if (keyfound && key_type != CKK_RC4)
2377c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCONSISTENT);
2387c478bd9Sstevel@tonic-gate 		else if (!keyfound)
2397c478bd9Sstevel@tonic-gate 			key_type = CKK_RC4;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		/* If key length was specified, it better be 16 bytes */
2427c478bd9Sstevel@tonic-gate 		if (keylen != 0 && keylen != 16)
2437c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCONSISTENT);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * Make sure that parameters were given for this
2477c478bd9Sstevel@tonic-gate 		 * mechanism.
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		if (pMechanism->pParameter == NULL ||
2507c478bd9Sstevel@tonic-gate 		    pMechanism->ulParameterLen !=
2517c478bd9Sstevel@tonic-gate 		    sizeof (CK_PBE_PARAMS))
2527c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCOMPLETE);
2537c478bd9Sstevel@tonic-gate 		break;
2547c478bd9Sstevel@tonic-gate 	default:
2557c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* Create a new object for secret key. */
259cfcec266SJason King 	rv = soft_gen_keyobject(pTemplate, ulCount, &secret_key, session_p,
2607c478bd9Sstevel@tonic-gate 	    CKO_SECRET_KEY, key_type, keylen, SOFT_GEN_KEY, B_FALSE);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
2637c478bd9Sstevel@tonic-gate 		return (rv);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
2677c478bd9Sstevel@tonic-gate 	case CKM_DES_KEY_GEN:
2687c478bd9Sstevel@tonic-gate 		/*
2697c478bd9Sstevel@tonic-gate 		 * Set up key value len since it is not a required
2707c478bd9Sstevel@tonic-gate 		 * attribute for C_GenerateKey.
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
2737c478bd9Sstevel@tonic-gate 		des_strength = DES;
2747c478bd9Sstevel@tonic-gate 		break;
2757c478bd9Sstevel@tonic-gate 
276436935a1SVladimir Kotal 	case CKM_DES2_KEY_GEN:
277436935a1SVladimir Kotal 		/*
278436935a1SVladimir Kotal 		 * Set up key value len since it is not a required
279436935a1SVladimir Kotal 		 * attribute for C_GenerateKey.
280436935a1SVladimir Kotal 		 */
281436935a1SVladimir Kotal 		keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES2_KEYSIZE;
282436935a1SVladimir Kotal 		des_strength = DES2;
283436935a1SVladimir Kotal 		break;
284436935a1SVladimir Kotal 
2857c478bd9Sstevel@tonic-gate 	case CKM_DES3_KEY_GEN:
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * Set up key value len since it is not a required
2887c478bd9Sstevel@tonic-gate 		 * attribute for C_GenerateKey.
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES3_KEYSIZE;
2917c478bd9Sstevel@tonic-gate 		des_strength = DES3;
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	case CKM_SSL3_PRE_MASTER_KEY_GEN:
2957c478bd9Sstevel@tonic-gate 	case CKM_TLS_PRE_MASTER_KEY_GEN:
2967c478bd9Sstevel@tonic-gate 		secret_key->bool_attr_mask |= DERIVE_BOOL_ON;
2977c478bd9Sstevel@tonic-gate 	/* FALLTHRU */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	case CKM_AES_KEY_GEN:
300f66d273dSizick 	case CKM_BLOWFISH_KEY_GEN:
3017c478bd9Sstevel@tonic-gate 	case CKM_PBE_SHA1_RC4_128:
3027c478bd9Sstevel@tonic-gate 	case CKM_RC4_KEY_GEN:
3037c478bd9Sstevel@tonic-gate 		keylen = OBJ_SEC_VALUE_LEN(secret_key);
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	case CKM_PKCS5_PBKD2:
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * PKCS#11 does not allow one to specify key
3097c478bd9Sstevel@tonic-gate 		 * sizes for DES and 3DES, so we must set it here
3107c478bd9Sstevel@tonic-gate 		 * when using PBKD2 algorithms.
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		if (key_type == CKK_DES) {
3137c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
3147c478bd9Sstevel@tonic-gate 			des_strength = DES;
3157c478bd9Sstevel@tonic-gate 		} else if (key_type == CKK_DES3) {
3167c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(secret_key) = DES3_KEYSIZE;
3177c478bd9Sstevel@tonic-gate 			des_strength = DES3;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		keylen = OBJ_SEC_VALUE_LEN(secret_key);
3217c478bd9Sstevel@tonic-gate 		break;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if ((OBJ_SEC_VALUE(secret_key) = malloc(keylen)) == NULL) {
3257c478bd9Sstevel@tonic-gate 		if (IS_TOKEN_OBJECT(secret_key))
3267c478bd9Sstevel@tonic-gate 			soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
3277c478bd9Sstevel@tonic-gate 		else
3281f49a79aSZdenek Kotala 			soft_delete_object(session_p, secret_key,
3291f49a79aSZdenek Kotala 			    B_FALSE, B_FALSE);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
3347c478bd9Sstevel@tonic-gate 	case CKM_PBE_SHA1_RC4_128:
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * Use the PBE algorithm described in PKCS#11 section
3377c478bd9Sstevel@tonic-gate 		 * 12.33 to derive the key.
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		rv = soft_pkcs12_pbe(session_p, pMechanism, secret_key);
3407c478bd9Sstevel@tonic-gate 		break;
3417c478bd9Sstevel@tonic-gate 	case CKM_PKCS5_PBKD2:
3427c478bd9Sstevel@tonic-gate 		/* Generate keys using PKCS#5 PBKD2 algorithm */
3437c478bd9Sstevel@tonic-gate 		rv = soft_generate_pkcs5_pbkdf2_key(session_p, pMechanism,
3447c478bd9Sstevel@tonic-gate 		    secret_key);
3457c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK && des_strength > 0) {
3467c478bd9Sstevel@tonic-gate 			/* Perform weak key checking for DES and DES3. */
3477c478bd9Sstevel@tonic-gate 			if (des_keycheck(OBJ_SEC_VALUE(secret_key),
3487c478bd9Sstevel@tonic-gate 			    des_strength, OBJ_SEC_VALUE(secret_key)) ==
3497c478bd9Sstevel@tonic-gate 			    B_FALSE) {
3507c478bd9Sstevel@tonic-gate 				/* We got a weak secret key. */
3517c478bd9Sstevel@tonic-gate 				rv = CKR_FUNCTION_FAILED;
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		break;
3557c478bd9Sstevel@tonic-gate 	default:
3567c478bd9Sstevel@tonic-gate 		do {
3577c478bd9Sstevel@tonic-gate 			/* If this fails, bail out */
3587b79d846SDina K Nimeh 			rv = CKR_OK;
3597b79d846SDina K Nimeh 			if (pkcs11_get_urandom(
3607b79d846SDina K Nimeh 			    OBJ_SEC_VALUE(secret_key), keylen) < 0) {
3617b79d846SDina K Nimeh 				rv = CKR_DEVICE_ERROR;
3627c478bd9Sstevel@tonic-gate 				break;
3637b79d846SDina K Nimeh 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			/* Perform weak key checking for DES and DES3. */
3667c478bd9Sstevel@tonic-gate 			if (des_strength > 0) {
3677c478bd9Sstevel@tonic-gate 				rv = CKR_OK;
3687c478bd9Sstevel@tonic-gate 				if (des_keycheck(OBJ_SEC_VALUE(secret_key),
3697c478bd9Sstevel@tonic-gate 				    des_strength, OBJ_SEC_VALUE(secret_key)) ==
3707c478bd9Sstevel@tonic-gate 				    B_FALSE) {
3717c478bd9Sstevel@tonic-gate 					/* We got a weak key, retry! */
3727c478bd9Sstevel@tonic-gate 					retry++;
3737c478bd9Sstevel@tonic-gate 					rv = CKR_FUNCTION_FAILED;
3747c478bd9Sstevel@tonic-gate 				}
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			/*
3777c478bd9Sstevel@tonic-gate 			 * Copy over the SSL client version For SSL mechs
3787c478bd9Sstevel@tonic-gate 			 * The first two bytes of the key is the version
3797c478bd9Sstevel@tonic-gate 			 */
3807c478bd9Sstevel@tonic-gate 			if (is_ssl_mech)
3817c478bd9Sstevel@tonic-gate 				bcopy(pMechanism->pParameter,
3827c478bd9Sstevel@tonic-gate 				    OBJ_SEC_VALUE(secret_key),
3837c478bd9Sstevel@tonic-gate 				    sizeof (CK_VERSION));
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		} while (rv != CKR_OK && retry < KEYGEN_RETRY);
3867c478bd9Sstevel@tonic-gate 		if (retry == KEYGEN_RETRY)
3877c478bd9Sstevel@tonic-gate 			rv = CKR_FUNCTION_FAILED;
3887c478bd9Sstevel@tonic-gate 		break;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3927c478bd9Sstevel@tonic-gate 		if (IS_TOKEN_OBJECT(secret_key))
3937c478bd9Sstevel@tonic-gate 			soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
3947c478bd9Sstevel@tonic-gate 		else
3951f49a79aSZdenek Kotala 			soft_delete_object(session_p, secret_key,
3961f49a79aSZdenek Kotala 			    B_FALSE, B_FALSE);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(secret_key)) {
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * All the info has been filled, so we can write to
4017c478bd9Sstevel@tonic-gate 		 * keystore now.
4027c478bd9Sstevel@tonic-gate 		 */
4037c478bd9Sstevel@tonic-gate 		rv = soft_put_object_to_keystore(secret_key);
4047c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
4057c478bd9Sstevel@tonic-gate 			soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
408cfcec266SJason King 	*phKey = secret_key->handle;
4097c478bd9Sstevel@tonic-gate 	return (rv);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate CK_RV
soft_genkey_pair(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pPublicKeyTemplate,CK_ULONG ulPublicAttrCount,CK_ATTRIBUTE_PTR pPrivateKeyTemplate,CK_ULONG ulPrivateAttrCount,CK_OBJECT_HANDLE_PTR phPublicKey,CK_OBJECT_HANDLE_PTR phPrivateKey)4137c478bd9Sstevel@tonic-gate soft_genkey_pair(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
4147c478bd9Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicAttrCount,
4157c478bd9Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateAttrCount,
4167c478bd9Sstevel@tonic-gate     CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	CK_RV rv;
4207c478bd9Sstevel@tonic-gate 	soft_object_t *public_key, *private_key;
4217c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE key_type;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS_KEY_PAIR_GEN:
4267c478bd9Sstevel@tonic-gate 		key_type = CKK_RSA;
4277c478bd9Sstevel@tonic-gate 		break;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	case CKM_DSA_KEY_PAIR_GEN:
4307c478bd9Sstevel@tonic-gate 		key_type = CKK_DSA;
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	case CKM_DH_PKCS_KEY_PAIR_GEN:
4347c478bd9Sstevel@tonic-gate 		key_type = CKK_DH;
4357c478bd9Sstevel@tonic-gate 		break;
4367c478bd9Sstevel@tonic-gate 
437034448feSmcpowers 	case CKM_EC_KEY_PAIR_GEN:
438034448feSmcpowers 		key_type = CKK_EC;
439f9fbec18Smcpowers 		break;
440034448feSmcpowers 
4417c478bd9Sstevel@tonic-gate 	default:
4427c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/* Create a new object for public key. */
4467c478bd9Sstevel@tonic-gate 	rv = soft_gen_keyobject(pPublicKeyTemplate, ulPublicAttrCount,
447cfcec266SJason King 	    &public_key, session_p, CKO_PUBLIC_KEY, key_type, 0,
4487c478bd9Sstevel@tonic-gate 	    SOFT_GEN_KEY, B_FALSE);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
4517c478bd9Sstevel@tonic-gate 		return (rv);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/* Create a new object for private key. */
4557c478bd9Sstevel@tonic-gate 	rv = soft_gen_keyobject(pPrivateKeyTemplate, ulPrivateAttrCount,
456cfcec266SJason King 	    &private_key, session_p, CKO_PRIVATE_KEY, key_type, 0,
4577c478bd9Sstevel@tonic-gate 	    SOFT_GEN_KEY, B_FALSE);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Both public key and private key must be successful.
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		if (IS_TOKEN_OBJECT(public_key))
4647c478bd9Sstevel@tonic-gate 			soft_delete_token_object(public_key, B_FALSE, B_FALSE);
4657c478bd9Sstevel@tonic-gate 		else
4661f49a79aSZdenek Kotala 			soft_delete_object(session_p, public_key,
4671f49a79aSZdenek Kotala 			    B_FALSE, B_FALSE);
4687c478bd9Sstevel@tonic-gate 		return (rv);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * At this point, both public key and private key objects
4737c478bd9Sstevel@tonic-gate 	 * are settled with the application specified attributes.
4747c478bd9Sstevel@tonic-gate 	 * We are ready to generate the rest of key attributes based
4757c478bd9Sstevel@tonic-gate 	 * on the existing attributes.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	switch (key_type) {
4797c478bd9Sstevel@tonic-gate 	case CKK_RSA:
4807c478bd9Sstevel@tonic-gate 		rv = soft_rsa_genkey_pair(public_key, private_key);
4817c478bd9Sstevel@tonic-gate 		break;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	case CKK_DSA:
4847c478bd9Sstevel@tonic-gate 		rv = soft_dsa_genkey_pair(public_key, private_key);
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	case CKK_DH:
4887c478bd9Sstevel@tonic-gate 		rv = soft_dh_genkey_pair(public_key, private_key);
4897c478bd9Sstevel@tonic-gate 		private_key->bool_attr_mask |= DERIVE_BOOL_ON;
4907c478bd9Sstevel@tonic-gate 		break;
491f9fbec18Smcpowers 	case CKK_EC:
492f9fbec18Smcpowers 		rv = soft_ec_genkey_pair(public_key, private_key);
493f9fbec18Smcpowers 		private_key->bool_attr_mask |= DERIVE_BOOL_ON;
494f9fbec18Smcpowers 		break;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
4987c478bd9Sstevel@tonic-gate 		if (IS_TOKEN_OBJECT(public_key)) {
4997c478bd9Sstevel@tonic-gate 			soft_delete_token_object(public_key, B_FALSE, B_FALSE);
5007c478bd9Sstevel@tonic-gate 			soft_delete_token_object(private_key, B_FALSE, B_FALSE);
5017c478bd9Sstevel@tonic-gate 		} else {
5021f49a79aSZdenek Kotala 			soft_delete_object(session_p, public_key,
5031f49a79aSZdenek Kotala 			    B_FALSE, B_FALSE);
5041f49a79aSZdenek Kotala 			soft_delete_object(session_p, private_key,
5051f49a79aSZdenek Kotala 			    B_FALSE, B_FALSE);
5067c478bd9Sstevel@tonic-gate 		}
507b5a2d845SHai-May Chao 		return (rv);
508b5a2d845SHai-May Chao 	}
509b5a2d845SHai-May Chao 
5107c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(public_key)) {
5117c478bd9Sstevel@tonic-gate 		/*
5127c478bd9Sstevel@tonic-gate 		 * All the info has been filled, so we can write to
5137c478bd9Sstevel@tonic-gate 		 * keystore now.
5147c478bd9Sstevel@tonic-gate 		 */
5157c478bd9Sstevel@tonic-gate 		rv = soft_put_object_to_keystore(public_key);
5167c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
5177c478bd9Sstevel@tonic-gate 			soft_delete_token_object(public_key, B_FALSE, B_FALSE);
5187c478bd9Sstevel@tonic-gate 			soft_delete_token_object(private_key, B_FALSE, B_FALSE);
519b5a2d845SHai-May Chao 			return (rv);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(private_key)) {
5247c478bd9Sstevel@tonic-gate 		rv = soft_put_object_to_keystore(private_key);
5257c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
5267c478bd9Sstevel@tonic-gate 			/*
5277c478bd9Sstevel@tonic-gate 			 * We also need to delete the public token object
5287c478bd9Sstevel@tonic-gate 			 * from keystore.
5297c478bd9Sstevel@tonic-gate 			 */
5307c478bd9Sstevel@tonic-gate 			soft_delete_token_object(public_key, B_TRUE, B_FALSE);
5317c478bd9Sstevel@tonic-gate 			soft_delete_token_object(private_key, B_FALSE, B_FALSE);
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
535cfcec266SJason King 	*phPublicKey = public_key->handle;
536cfcec266SJason King 	*phPrivateKey = private_key->handle;
537cfcec266SJason King 
5387c478bd9Sstevel@tonic-gate 	return (rv);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate CK_RV
soft_key_derive_check_length(soft_object_t * secret_key,CK_ULONG max_keylen)5437c478bd9Sstevel@tonic-gate soft_key_derive_check_length(soft_object_t *secret_key, CK_ULONG max_keylen)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	switch (secret_key->key_type) {
5477c478bd9Sstevel@tonic-gate 	case CKK_GENERIC_SECRET:
5487c478bd9Sstevel@tonic-gate 		if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5497c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(secret_key) = max_keylen;
5507c478bd9Sstevel@tonic-gate 			return (CKR_OK);
5517c478bd9Sstevel@tonic-gate 		} else if (OBJ_SEC_VALUE_LEN(secret_key) > max_keylen) {
5527c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 	case CKK_RC4:
5567c478bd9Sstevel@tonic-gate 	case CKK_AES:
557f66d273dSizick 	case CKK_BLOWFISH:
5587c478bd9Sstevel@tonic-gate 		if ((OBJ_SEC_VALUE_LEN(secret_key) == 0) ||
5597c478bd9Sstevel@tonic-gate 		    (OBJ_SEC_VALUE_LEN(secret_key) > max_keylen)) {
5607c478bd9Sstevel@tonic-gate 			/* RC4 and AES has variable key length */
5617c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		break;
5647c478bd9Sstevel@tonic-gate 	case CKK_DES:
5657c478bd9Sstevel@tonic-gate 		if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5667c478bd9Sstevel@tonic-gate 			/* DES has a well-defined length */
5677c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
5687c478bd9Sstevel@tonic-gate 			return (CKR_OK);
5697c478bd9Sstevel@tonic-gate 		} else if (OBJ_SEC_VALUE_LEN(secret_key) != DES_KEYSIZE) {
5707c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
5717c478bd9Sstevel@tonic-gate 		}
5727c478bd9Sstevel@tonic-gate 		break;
5737c478bd9Sstevel@tonic-gate 	case CKK_DES2:
5747c478bd9Sstevel@tonic-gate 		if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5757c478bd9Sstevel@tonic-gate 			/* DES2 has a well-defined length */
5767c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(secret_key) = DES2_KEYSIZE;
5777c478bd9Sstevel@tonic-gate 			return (CKR_OK);
5787c478bd9Sstevel@tonic-gate 		} else if (OBJ_SEC_VALUE_LEN(secret_key) != DES2_KEYSIZE) {
5797c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 		break;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	default:
5847c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	return (CKR_OK);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate  * PKCS#11 (12.33) says that v = 512 bits (64 bytes) for SHA1
5927c478bd9Sstevel@tonic-gate  * PBE methods.
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate #define	PKCS12_BUFFER_SIZE 64
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * PKCS#12 defines 3 different ID bytes to be used for
5977c478bd9Sstevel@tonic-gate  * deriving keys for different operations.
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate #define	PBE_ID_ENCRYPT	1
6007c478bd9Sstevel@tonic-gate #define	PBE_ID_IV	2
6017c478bd9Sstevel@tonic-gate #define	PBE_ID_MAC	3
6027c478bd9Sstevel@tonic-gate #define	PBE_CEIL(a, b)	(((a)/(b)) + (((a)%(b)) > 0))
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static CK_RV
soft_pkcs12_pbe(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * derived_key)6057c478bd9Sstevel@tonic-gate soft_pkcs12_pbe(soft_session_t *session_p,
606a8793c76SJason King     CK_MECHANISM_PTR pMechanism, soft_object_t *derived_key)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
6097c478bd9Sstevel@tonic-gate 	CK_PBE_PARAMS *params = pMechanism->pParameter;
6107c478bd9Sstevel@tonic-gate 	CK_ULONG c, i, j, k;
6117c478bd9Sstevel@tonic-gate 	CK_ULONG hashSize;
6127c478bd9Sstevel@tonic-gate 	CK_ULONG buffSize;
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * Terse variable names are used to make following
6157c478bd9Sstevel@tonic-gate 	 * the PKCS#12 spec easier.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	CK_BYTE *A = NULL;
6187c478bd9Sstevel@tonic-gate 	CK_BYTE *Ai = NULL;
6197c478bd9Sstevel@tonic-gate 	CK_BYTE *B = NULL;
6207c478bd9Sstevel@tonic-gate 	CK_BYTE *D = NULL;
6217c478bd9Sstevel@tonic-gate 	CK_BYTE *I = NULL, *S, *P;
6227c478bd9Sstevel@tonic-gate 	CK_BYTE *keybuf = NULL;
6237c478bd9Sstevel@tonic-gate 	CK_ULONG Alen, Ilen, Slen, Plen, AiLen, Blen, Dlen;
6247c478bd9Sstevel@tonic-gate 	CK_ULONG keysize = OBJ_SEC_VALUE_LEN(derived_key);
6257c478bd9Sstevel@tonic-gate 	CK_MECHANISM digest_mech;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/* U = hash function output bits */
6287c478bd9Sstevel@tonic-gate 	if (pMechanism->mechanism == CKM_PBE_SHA1_RC4_128) {
6297c478bd9Sstevel@tonic-gate 		hashSize = SHA1_HASH_SIZE;
6307c478bd9Sstevel@tonic-gate 		buffSize = PKCS12_BUFFER_SIZE;
6317c478bd9Sstevel@tonic-gate 		digest_mech.mechanism = CKM_SHA_1;
6327c478bd9Sstevel@tonic-gate 		digest_mech.pParameter = NULL;
6337c478bd9Sstevel@tonic-gate 		digest_mech.ulParameterLen = 0;
6347c478bd9Sstevel@tonic-gate 	} else {
6357c478bd9Sstevel@tonic-gate 		/* we only support 1 PBE mech for now */
6367c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	keybuf = OBJ_SEC_VALUE(derived_key);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	Blen = Dlen = buffSize;
6417c478bd9Sstevel@tonic-gate 	D = (CK_BYTE *)malloc(Dlen);
6427c478bd9Sstevel@tonic-gate 	if (D == NULL) {
6437c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
6447c478bd9Sstevel@tonic-gate 		goto cleanup;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	B = (CK_BYTE *)malloc(Blen);
6487c478bd9Sstevel@tonic-gate 	if (B == NULL) {
6497c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
6507c478bd9Sstevel@tonic-gate 		goto cleanup;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * Initialize some values and create some buffers
6557c478bd9Sstevel@tonic-gate 	 * that we need later.
6567c478bd9Sstevel@tonic-gate 	 *
6577c478bd9Sstevel@tonic-gate 	 * Slen = buffSize * CEIL(SaltLength/buffSize)
6587c478bd9Sstevel@tonic-gate 	 */
6597c478bd9Sstevel@tonic-gate 	Slen = buffSize * PBE_CEIL(params->ulSaltLen, buffSize);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * Plen = buffSize * CEIL(PasswordLength/buffSize)
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	Plen = buffSize * PBE_CEIL(params->ulPasswordLen, buffSize);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * From step 4: I = S + P, so: Ilen = Slen + Plen
6687c478bd9Sstevel@tonic-gate 	 */
6697c478bd9Sstevel@tonic-gate 	Ilen = Slen + Plen;
6707c478bd9Sstevel@tonic-gate 	I = (CK_BYTE *)malloc(Ilen);
6717c478bd9Sstevel@tonic-gate 	if (I == NULL) {
6727c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
6737c478bd9Sstevel@tonic-gate 		goto cleanup;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	S = I;
6777c478bd9Sstevel@tonic-gate 	P = I + Slen;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * Step 1.
6817c478bd9Sstevel@tonic-gate 	 * We are only interested in deriving keys for encrypt/decrypt
6827c478bd9Sstevel@tonic-gate 	 * for now, so construct the "D"iversifier accordingly.
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	(void) memset(D, PBE_ID_ENCRYPT, Dlen);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * Step 2.
6887c478bd9Sstevel@tonic-gate 	 * Concatenate copies of the salt together to make S.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	for (i = 0; i < Slen; i += params->ulSaltLen) {
6917c478bd9Sstevel@tonic-gate 		(void) memcpy(S+i, params->pSalt,
6927c478bd9Sstevel@tonic-gate 		    ((Slen - i) > params->ulSaltLen ?
6937c478bd9Sstevel@tonic-gate 		    params->ulSaltLen : (Slen - i)));
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/*
6977c478bd9Sstevel@tonic-gate 	 * Step 3.
6987c478bd9Sstevel@tonic-gate 	 * Concatenate copies of the password together to make
6997c478bd9Sstevel@tonic-gate 	 * a string P.
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	for (i = 0; i < Plen; i += params->ulPasswordLen) {
7027c478bd9Sstevel@tonic-gate 		(void) memcpy(P+i, params->pPassword,
7037c478bd9Sstevel@tonic-gate 		    ((Plen - i) > params->ulPasswordLen ?
7047c478bd9Sstevel@tonic-gate 		    params->ulPasswordLen : (Plen - i)));
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Step 4.
7097c478bd9Sstevel@tonic-gate 	 * I = S+P - this is now done because S and P are
7107c478bd9Sstevel@tonic-gate 	 * pointers into I.
7117c478bd9Sstevel@tonic-gate 	 *
7127c478bd9Sstevel@tonic-gate 	 * Step 5.
7137c478bd9Sstevel@tonic-gate 	 * c= CEIL[n/u]
7147c478bd9Sstevel@tonic-gate 	 * where n = pseudorandom bits of output desired.
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	c = PBE_CEIL(keysize, hashSize);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * Step 6.
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	Alen = c * hashSize;
7227c478bd9Sstevel@tonic-gate 	A = (CK_BYTE *)malloc(Alen);
7237c478bd9Sstevel@tonic-gate 	if (A == NULL) {
7247c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
7257c478bd9Sstevel@tonic-gate 		goto cleanup;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 	AiLen = hashSize;
7287c478bd9Sstevel@tonic-gate 	Ai = (CK_BYTE *)malloc(AiLen);
7297c478bd9Sstevel@tonic-gate 	if (Ai == NULL) {
7307c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
7317c478bd9Sstevel@tonic-gate 		goto cleanup;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Step 6a.
7367c478bd9Sstevel@tonic-gate 	 * Ai = Hr(D+I)
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	for (i = 0; i < c; i++) {
7397c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
7427c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&session_p->session_mutex);
7437c478bd9Sstevel@tonic-gate 			rv = CKR_OPERATION_ACTIVE;
7447c478bd9Sstevel@tonic-gate 			goto cleanup;
7457c478bd9Sstevel@tonic-gate 		}
7467c478bd9Sstevel@tonic-gate 		session_p->sign.flags |= CRYPTO_OPERATION_ACTIVE;
7477c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		for (j = 0; j < params->ulIteration; j++) {
7507c478bd9Sstevel@tonic-gate 			rv = soft_digest_init(session_p, &digest_mech);
7517c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
7527c478bd9Sstevel@tonic-gate 				goto digest_done;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 			if (j == 0) {
755f9fbec18Smcpowers 				rv = soft_digest_update(session_p, D, Dlen);
7567c478bd9Sstevel@tonic-gate 				if (rv != CKR_OK)
7577c478bd9Sstevel@tonic-gate 					goto digest_done;
7587c478bd9Sstevel@tonic-gate 
759f9fbec18Smcpowers 				rv = soft_digest_update(session_p, I, Ilen);
7607c478bd9Sstevel@tonic-gate 			} else {
761f9fbec18Smcpowers 				rv = soft_digest_update(session_p, Ai, AiLen);
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
7647c478bd9Sstevel@tonic-gate 				goto digest_done;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 			rv = soft_digest_final(session_p, Ai, &AiLen);
7677c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
7687c478bd9Sstevel@tonic-gate 				goto digest_done;
7697c478bd9Sstevel@tonic-gate 		}
7707c478bd9Sstevel@tonic-gate digest_done:
7717c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
7727c478bd9Sstevel@tonic-gate 		session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
7737c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
7767c478bd9Sstevel@tonic-gate 			goto cleanup;
7777c478bd9Sstevel@tonic-gate 		/*
7787c478bd9Sstevel@tonic-gate 		 * Step 6b.
7797c478bd9Sstevel@tonic-gate 		 * Concatenate Ai to make B
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		for (j = 0; j < Blen; j += hashSize) {
7827c478bd9Sstevel@tonic-gate 			(void) memcpy(B+j, Ai, ((Blen - j > hashSize) ?
7837c478bd9Sstevel@tonic-gate 			    hashSize : Blen - j));
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * Step 6c.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		k = Ilen / Blen;
7907c478bd9Sstevel@tonic-gate 		for (j = 0; j < k; j++) {
7917c478bd9Sstevel@tonic-gate 			uchar_t idx;
7927c478bd9Sstevel@tonic-gate 			CK_ULONG m, q = 1, cbit = 0;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 			for (m = Blen - 1; m >= (CK_ULONG)0; m--, q = 0) {
7957c478bd9Sstevel@tonic-gate 				idx = m + j*Blen;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 				q += (CK_ULONG)I[idx] + (CK_ULONG)B[m];
7987c478bd9Sstevel@tonic-gate 				q += cbit;
7997c478bd9Sstevel@tonic-gate 				I[idx] = (CK_BYTE)(q & 0xff);
8007c478bd9Sstevel@tonic-gate 				cbit = (q > 0xff);
8017c478bd9Sstevel@tonic-gate 			}
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		/*
8057c478bd9Sstevel@tonic-gate 		 * Step 7.
8067c478bd9Sstevel@tonic-gate 		 *  A += Ai
8077c478bd9Sstevel@tonic-gate 		 */
8087c478bd9Sstevel@tonic-gate 		(void) memcpy(A + i*hashSize, Ai, AiLen);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * Step 8.
8137c478bd9Sstevel@tonic-gate 	 * The final output of this process is the A buffer
8147c478bd9Sstevel@tonic-gate 	 */
8157c478bd9Sstevel@tonic-gate 	(void) memcpy(keybuf, A, keysize);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate cleanup:
818a8793c76SJason King 	freezero(A, Alen);
819a8793c76SJason King 	freezero(Ai, AiLen);
820a8793c76SJason King 	freezero(B, Blen);
821a8793c76SJason King 	freezero(D, Dlen);
822a8793c76SJason King 	freezero(I, Ilen);
8237c478bd9Sstevel@tonic-gate 	return (rv);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate CK_RV
soft_derivekey(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * basekey_p,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)8277c478bd9Sstevel@tonic-gate soft_derivekey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
8287c478bd9Sstevel@tonic-gate     soft_object_t *basekey_p, CK_ATTRIBUTE_PTR pTemplate,
8297c478bd9Sstevel@tonic-gate     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
8337c478bd9Sstevel@tonic-gate 	soft_object_t *secret_key;
8347c478bd9Sstevel@tonic-gate 	CK_MECHANISM digest_mech;
835f66d273dSizick 	CK_BYTE hash[SHA512_DIGEST_LENGTH]; /* space enough for all mechs */
836f66d273dSizick 	CK_ULONG hash_len = SHA512_DIGEST_LENGTH;
8377c478bd9Sstevel@tonic-gate 	CK_ULONG secret_key_len;
8387c478bd9Sstevel@tonic-gate 	CK_ULONG hash_size;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
8417c478bd9Sstevel@tonic-gate 	case CKM_DH_PKCS_DERIVE:
842*6cb54de2SJason King 		if (phKey == NULL_PTR)
843*6cb54de2SJason King 			return (CKR_ARGUMENTS_BAD);
844*6cb54de2SJason King 
8457c478bd9Sstevel@tonic-gate 		/*
8467c478bd9Sstevel@tonic-gate 		 * Create a new object for secret key. The key type should
8477c478bd9Sstevel@tonic-gate 		 * be provided in the template.
8487c478bd9Sstevel@tonic-gate 		 */
8497c478bd9Sstevel@tonic-gate 		rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
850cfcec266SJason King 		    &secret_key, session_p, CKO_SECRET_KEY, (CK_KEY_TYPE)~0UL,
851cfcec266SJason King 		    0, SOFT_DERIVE_KEY_DH, B_FALSE);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
8547c478bd9Sstevel@tonic-gate 			return (rv);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 
857034448feSmcpowers 		rv = soft_dh_key_derive(basekey_p, secret_key,
858034448feSmcpowers 		    (CK_BYTE *)pMechanism->pParameter,
859034448feSmcpowers 		    pMechanism->ulParameterLen);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
8627c478bd9Sstevel@tonic-gate 			if (IS_TOKEN_OBJECT(secret_key))
8637c478bd9Sstevel@tonic-gate 				soft_delete_token_object(secret_key, B_FALSE,
8647c478bd9Sstevel@tonic-gate 				    B_FALSE);
8657c478bd9Sstevel@tonic-gate 			else
8667c478bd9Sstevel@tonic-gate 				soft_delete_object(session_p, secret_key,
8671f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
8687c478bd9Sstevel@tonic-gate 			return (rv);
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		break;
8727c478bd9Sstevel@tonic-gate 
873f9fbec18Smcpowers 	case CKM_ECDH1_DERIVE:
874*6cb54de2SJason King 		if (phKey == NULL_PTR)
875*6cb54de2SJason King 			return (CKR_ARGUMENTS_BAD);
876*6cb54de2SJason King 
877f9fbec18Smcpowers 		/*
878f9fbec18Smcpowers 		 * Create a new object for secret key. The key type should
879f9fbec18Smcpowers 		 * be provided in the template.
880f9fbec18Smcpowers 		 */
881f9fbec18Smcpowers 		rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
882cfcec266SJason King 		    &secret_key, session_p, CKO_SECRET_KEY, (CK_KEY_TYPE)~0UL,
883cfcec266SJason King 		    0, SOFT_DERIVE_KEY_DH, B_FALSE);
884f9fbec18Smcpowers 
885f9fbec18Smcpowers 		if (rv != CKR_OK) {
886f9fbec18Smcpowers 			return (rv);
887f9fbec18Smcpowers 		}
888f9fbec18Smcpowers 
889f9fbec18Smcpowers 		rv = soft_ec_key_derive(basekey_p, secret_key,
890f9fbec18Smcpowers 		    (CK_BYTE *)pMechanism->pParameter,
891f9fbec18Smcpowers 		    pMechanism->ulParameterLen);
892f9fbec18Smcpowers 
893f9fbec18Smcpowers 		if (rv != CKR_OK) {
894f9fbec18Smcpowers 			if (IS_TOKEN_OBJECT(secret_key))
895f9fbec18Smcpowers 				soft_delete_token_object(secret_key, B_FALSE,
896f9fbec18Smcpowers 				    B_FALSE);
897f9fbec18Smcpowers 			else
898f9fbec18Smcpowers 				soft_delete_object(session_p, secret_key,
8991f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
900f9fbec18Smcpowers 			return (rv);
901f9fbec18Smcpowers 		}
902f9fbec18Smcpowers 
903f9fbec18Smcpowers 		break;
904f9fbec18Smcpowers 
9057c478bd9Sstevel@tonic-gate 	case CKM_SHA1_KEY_DERIVATION:
9067c478bd9Sstevel@tonic-gate 		hash_size = SHA1_HASH_SIZE;
9077c478bd9Sstevel@tonic-gate 		digest_mech.mechanism = CKM_SHA_1;
9087c478bd9Sstevel@tonic-gate 		goto common;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	case CKM_MD5_KEY_DERIVATION:
9117c478bd9Sstevel@tonic-gate 		hash_size = MD5_HASH_SIZE;
9127c478bd9Sstevel@tonic-gate 		digest_mech.mechanism = CKM_MD5;
913f66d273dSizick 		goto common;
914f66d273dSizick 
915f66d273dSizick 	case CKM_SHA256_KEY_DERIVATION:
916f66d273dSizick 		hash_size = SHA256_DIGEST_LENGTH;
917f66d273dSizick 		digest_mech.mechanism = CKM_SHA256;
918f66d273dSizick 		goto common;
919f66d273dSizick 
920f66d273dSizick 	case CKM_SHA384_KEY_DERIVATION:
921f66d273dSizick 		hash_size = SHA384_DIGEST_LENGTH;
922f66d273dSizick 		digest_mech.mechanism = CKM_SHA384;
923f66d273dSizick 		goto common;
924f66d273dSizick 
925f66d273dSizick 	case CKM_SHA512_KEY_DERIVATION:
926f66d273dSizick 		hash_size = SHA512_DIGEST_LENGTH;
927f66d273dSizick 		digest_mech.mechanism = CKM_SHA512;
928f66d273dSizick 		goto common;
929f66d273dSizick 
93005204290SJason King 	case CKM_SHA512_224_KEY_DERIVATION:
93105204290SJason King 		hash_size = SHA512_224_DIGEST_LENGTH;
93205204290SJason King 		digest_mech.mechanism = CKM_SHA512_224;
93305204290SJason King 		goto common;
93405204290SJason King 
93505204290SJason King 	case CKM_SHA512_256_KEY_DERIVATION:
93605204290SJason King 		hash_size = SHA512_256_DIGEST_LENGTH;
93705204290SJason King 		digest_mech.mechanism = CKM_SHA512_256;
93805204290SJason King 		goto common;
93905204290SJason King 
9407c478bd9Sstevel@tonic-gate common:
941*6cb54de2SJason King 		if (phKey == NULL_PTR)
942*6cb54de2SJason King 			return (CKR_ARGUMENTS_BAD);
943*6cb54de2SJason King 
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * Create a new object for secret key. The key type is optional
9467c478bd9Sstevel@tonic-gate 		 * to be provided in the template. If it is not specified in
9477c478bd9Sstevel@tonic-gate 		 * the template, the default is CKK_GENERIC_SECRET.
9487c478bd9Sstevel@tonic-gate 		 */
9497c478bd9Sstevel@tonic-gate 		rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
950cfcec266SJason King 		    &secret_key, session_p, CKO_SECRET_KEY,
9517c478bd9Sstevel@tonic-gate 		    (CK_KEY_TYPE)CKK_GENERIC_SECRET, 0,
9527c478bd9Sstevel@tonic-gate 		    SOFT_DERIVE_KEY_OTHER, B_FALSE);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
9557c478bd9Sstevel@tonic-gate 			return (rv);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		/* Validate the key type and key length */
9597c478bd9Sstevel@tonic-gate 		rv = soft_key_derive_check_length(secret_key, hash_size);
9607c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
9617c478bd9Sstevel@tonic-gate 			if (IS_TOKEN_OBJECT(secret_key))
9627c478bd9Sstevel@tonic-gate 				soft_delete_token_object(secret_key, B_FALSE,
9637c478bd9Sstevel@tonic-gate 				    B_FALSE);
9647c478bd9Sstevel@tonic-gate 			else
9657c478bd9Sstevel@tonic-gate 				soft_delete_object(session_p, secret_key,
9661f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
9677c478bd9Sstevel@tonic-gate 			return (rv);
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		/*
9717c478bd9Sstevel@tonic-gate 		 * Derive the secret key by digesting the value of another
9727c478bd9Sstevel@tonic-gate 		 * secret key (base key) with SHA-1 or MD5.
9737c478bd9Sstevel@tonic-gate 		 */
9747c478bd9Sstevel@tonic-gate 		rv = soft_digest_init_internal(session_p, &digest_mech);
9757c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
9767c478bd9Sstevel@tonic-gate 			if (IS_TOKEN_OBJECT(secret_key))
9777c478bd9Sstevel@tonic-gate 				soft_delete_token_object(secret_key, B_FALSE,
9787c478bd9Sstevel@tonic-gate 				    B_FALSE);
9797c478bd9Sstevel@tonic-gate 			else
9807c478bd9Sstevel@tonic-gate 				soft_delete_object(session_p, secret_key,
9811f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
9827c478bd9Sstevel@tonic-gate 			return (rv);
9837c478bd9Sstevel@tonic-gate 		}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		rv = soft_digest(session_p, OBJ_SEC_VALUE(basekey_p),
9867c478bd9Sstevel@tonic-gate 		    OBJ_SEC_VALUE_LEN(basekey_p), hash, &hash_len);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
9897c478bd9Sstevel@tonic-gate 		/* soft_digest_common() has freed the digest context */
9907c478bd9Sstevel@tonic-gate 		session_p->digest.flags = 0;
9917c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
9947c478bd9Sstevel@tonic-gate 			if (IS_TOKEN_OBJECT(secret_key))
9957c478bd9Sstevel@tonic-gate 				soft_delete_token_object(secret_key, B_FALSE,
9967c478bd9Sstevel@tonic-gate 				    B_FALSE);
9977c478bd9Sstevel@tonic-gate 			else
9987c478bd9Sstevel@tonic-gate 				soft_delete_object(session_p, secret_key,
9991f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
10007c478bd9Sstevel@tonic-gate 			return (rv);
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		secret_key_len = OBJ_SEC_VALUE_LEN(secret_key);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		if ((OBJ_SEC_VALUE(secret_key) = malloc(secret_key_len)) ==
10067c478bd9Sstevel@tonic-gate 		    NULL) {
10077c478bd9Sstevel@tonic-gate 			if (IS_TOKEN_OBJECT(secret_key))
10087c478bd9Sstevel@tonic-gate 				soft_delete_token_object(secret_key, B_FALSE,
10097c478bd9Sstevel@tonic-gate 				    B_FALSE);
10107c478bd9Sstevel@tonic-gate 			else
10117c478bd9Sstevel@tonic-gate 				soft_delete_object(session_p, secret_key,
10121f49a79aSZdenek Kotala 				    B_FALSE, B_FALSE);
10137c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 		/*
10177c478bd9Sstevel@tonic-gate 		 * The key produced by this mechanism will be of the
10187c478bd9Sstevel@tonic-gate 		 * specified type and length.
10197c478bd9Sstevel@tonic-gate 		 * The truncation removes extra bytes from the leading
10207c478bd9Sstevel@tonic-gate 		 * of the digested key value.
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 		(void) memcpy(OBJ_SEC_VALUE(secret_key),
10237c478bd9Sstevel@tonic-gate 		    (hash + hash_len - secret_key_len),
10247c478bd9Sstevel@tonic-gate 		    secret_key_len);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		break;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * The key sensitivity and extractability rules for the generated
10307c478bd9Sstevel@tonic-gate 	 * keys will be enforced inside soft_ssl_master_key_derive() and
10317c478bd9Sstevel@tonic-gate 	 * soft_ssl_key_and_mac_derive()
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MASTER_KEY_DERIVE:
10347c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MASTER_KEY_DERIVE_DH:
10357c478bd9Sstevel@tonic-gate 	case CKM_TLS_MASTER_KEY_DERIVE:
10367c478bd9Sstevel@tonic-gate 	case CKM_TLS_MASTER_KEY_DERIVE_DH:
10377c478bd9Sstevel@tonic-gate 		if (phKey == NULL_PTR)
10387c478bd9Sstevel@tonic-gate 			return (CKR_ARGUMENTS_BAD);
10397c478bd9Sstevel@tonic-gate 		return (soft_ssl_master_key_derive(session_p, pMechanism,
10407c478bd9Sstevel@tonic-gate 		    basekey_p, pTemplate, ulAttributeCount, phKey));
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	case CKM_SSL3_KEY_AND_MAC_DERIVE:
10437c478bd9Sstevel@tonic-gate 	case CKM_TLS_KEY_AND_MAC_DERIVE:
1044*6cb54de2SJason King 		/* These mechanisms do not use phKey */
10457c478bd9Sstevel@tonic-gate 		return (soft_ssl_key_and_mac_derive(session_p, pMechanism,
10467c478bd9Sstevel@tonic-gate 		    basekey_p, pTemplate, ulAttributeCount));
10477c478bd9Sstevel@tonic-gate 
104860722cc8Sizick 	case CKM_TLS_PRF:
1049*6cb54de2SJason King 		/* This mechanism does not use phKey */
105060722cc8Sizick 		if (pMechanism->pParameter == NULL ||
105160722cc8Sizick 		    pMechanism->ulParameterLen != sizeof (CK_TLS_PRF_PARAMS) ||
105260722cc8Sizick 		    phKey != NULL)
105360722cc8Sizick 			return (CKR_ARGUMENTS_BAD);
105460722cc8Sizick 
105560722cc8Sizick 		if (pTemplate != NULL)
105660722cc8Sizick 			return (CKR_TEMPLATE_INCONSISTENT);
105760722cc8Sizick 
105860722cc8Sizick 		return (derive_tls_prf(
1059f9fbec18Smcpowers 		    (CK_TLS_PRF_PARAMS_PTR)pMechanism->pParameter, basekey_p));
106060722cc8Sizick 
10617c478bd9Sstevel@tonic-gate 	default:
10627c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	soft_derive_enforce_flags(basekey_p, secret_key);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(secret_key)) {
10687c478bd9Sstevel@tonic-gate 		/*
10697c478bd9Sstevel@tonic-gate 		 * All the info has been filled, so we can write to
10707c478bd9Sstevel@tonic-gate 		 * keystore now.
10717c478bd9Sstevel@tonic-gate 		 */
10727c478bd9Sstevel@tonic-gate 		rv = soft_put_object_to_keystore(secret_key);
10737c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
10747c478bd9Sstevel@tonic-gate 			soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
1077*6cb54de2SJason King 	/*
1078*6cb54de2SJason King 	 * Some mechanisms don't use phKey either because they create
1079*6cb54de2SJason King 	 * multiple key objects and instead populate a structure passed in
1080*6cb54de2SJason King 	 * as a field in their pParameter parameter with the resulting key
1081*6cb54de2SJason King 	 * objects (e.g. CKM_TLS_KEY_AND_MAC_DERIVE) or they instead write
1082*6cb54de2SJason King 	 * their result to an output buffer passed in their pParameter
1083*6cb54de2SJason King 	 * parameter (e.g. CKM_TLS_PRF). All such mechanisms return prior
1084*6cb54de2SJason King 	 * to reaching here. The remaining mechanisms (which do use phKey)
1085*6cb54de2SJason King 	 * should have already validated phKey is not NULL prior to doing
1086*6cb54de2SJason King 	 * their key derivation.
1087*6cb54de2SJason King 	 */
1088*6cb54de2SJason King 	*phKey = secret_key->handle;
1089*6cb54de2SJason King 
10907c478bd9Sstevel@tonic-gate 	return (rv);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * Perform key derivation rules on key's sensitivity and extractability.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate void
soft_derive_enforce_flags(soft_object_t * basekey,soft_object_t * newkey)10987c478bd9Sstevel@tonic-gate soft_derive_enforce_flags(soft_object_t *basekey, soft_object_t *newkey)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	boolean_t new_sensitive = B_FALSE;
11027c478bd9Sstevel@tonic-gate 	boolean_t new_extractable = B_FALSE;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/*
11057c478bd9Sstevel@tonic-gate 	 * The sensitive and extractable bits have been set when
11067c478bd9Sstevel@tonic-gate 	 * the newkey was built.
11077c478bd9Sstevel@tonic-gate 	 */
11087c478bd9Sstevel@tonic-gate 	if (newkey->bool_attr_mask & SENSITIVE_BOOL_ON) {
11097c478bd9Sstevel@tonic-gate 		new_sensitive = B_TRUE;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	if (newkey->bool_attr_mask & EXTRACTABLE_BOOL_ON) {
11137c478bd9Sstevel@tonic-gate 		new_extractable = B_TRUE;
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/* Derive the CKA_ALWAYS_SENSITIVE flag */
11177c478bd9Sstevel@tonic-gate 	if (!basekey->bool_attr_mask & ALWAYS_SENSITIVE_BOOL_ON) {
11187c478bd9Sstevel@tonic-gate 		/*
11197c478bd9Sstevel@tonic-gate 		 * If the base key has its CKA_ALWAYS_SENSITIVE set to
11207c478bd9Sstevel@tonic-gate 		 * FALSE, then the derived key will as well.
11217c478bd9Sstevel@tonic-gate 		 */
11227c478bd9Sstevel@tonic-gate 		newkey->bool_attr_mask &= ~ALWAYS_SENSITIVE_BOOL_ON;
11237c478bd9Sstevel@tonic-gate 	} else {
11247c478bd9Sstevel@tonic-gate 		/*
11257c478bd9Sstevel@tonic-gate 		 * If the base key has its CKA_ALWAYS_SENSITIVE set to TRUE,
11267c478bd9Sstevel@tonic-gate 		 * then the derived key has the CKA_ALWAYS_SENSITIVE set to
11277c478bd9Sstevel@tonic-gate 		 * the same value as its CKA_SENSITIVE;
11287c478bd9Sstevel@tonic-gate 		 */
11297c478bd9Sstevel@tonic-gate 		if (new_sensitive) {
11307c478bd9Sstevel@tonic-gate 			newkey->bool_attr_mask |= ALWAYS_SENSITIVE_BOOL_ON;
11317c478bd9Sstevel@tonic-gate 		} else {
11327c478bd9Sstevel@tonic-gate 			newkey->bool_attr_mask &= ~ALWAYS_SENSITIVE_BOOL_ON;
11337c478bd9Sstevel@tonic-gate 		}
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/* Derive the CKA_NEVER_EXTRACTABLE flag */
11377c478bd9Sstevel@tonic-gate 	if (!basekey->bool_attr_mask & NEVER_EXTRACTABLE_BOOL_ON) {
11387c478bd9Sstevel@tonic-gate 		/*
11397c478bd9Sstevel@tonic-gate 		 * If the base key has its CKA_NEVER_EXTRACTABLE set to
11407c478bd9Sstevel@tonic-gate 		 * FALSE, then the derived key will as well.
11417c478bd9Sstevel@tonic-gate 		 */
11427c478bd9Sstevel@tonic-gate 		newkey->bool_attr_mask &= ~NEVER_EXTRACTABLE_BOOL_ON;
11437c478bd9Sstevel@tonic-gate 	} else {
11447c478bd9Sstevel@tonic-gate 		/*
11457c478bd9Sstevel@tonic-gate 		 * If the base key has its CKA_NEVER_EXTRACTABLE set to TRUE,
11467c478bd9Sstevel@tonic-gate 		 * then the derived key has the CKA_NEVER_EXTRACTABLE set to
11477c478bd9Sstevel@tonic-gate 		 * the opposite value from its CKA_EXTRACTABLE;
11487c478bd9Sstevel@tonic-gate 		 */
11497c478bd9Sstevel@tonic-gate 		if (new_extractable) {
11507c478bd9Sstevel@tonic-gate 			newkey->bool_attr_mask &= ~NEVER_EXTRACTABLE_BOOL_ON;
11517c478bd9Sstevel@tonic-gate 		} else {
11527c478bd9Sstevel@tonic-gate 			newkey->bool_attr_mask |= NEVER_EXTRACTABLE_BOOL_ON;
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/* Set the CKA_LOCAL flag to false */
11577c478bd9Sstevel@tonic-gate 	newkey->bool_attr_mask &= ~LOCAL_BOOL_ON;
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * do_prf
11637c478bd9Sstevel@tonic-gate  *
11647c478bd9Sstevel@tonic-gate  * This routine implements Step 3. of the PBKDF2 function
11657c478bd9Sstevel@tonic-gate  * defined in PKCS#5 for generating derived keys from a
11667c478bd9Sstevel@tonic-gate  * password.
11677c478bd9Sstevel@tonic-gate  *
11687c478bd9Sstevel@tonic-gate  * Currently, PRF is always SHA_1_HMAC.
11697c478bd9Sstevel@tonic-gate  */
11707c478bd9Sstevel@tonic-gate static CK_RV
do_prf(soft_session_t * session_p,CK_PKCS5_PBKD2_PARAMS_PTR params,soft_object_t * hmac_key,CK_BYTE * newsalt,CK_ULONG saltlen,CK_BYTE * blockdata,CK_ULONG blocklen)1171a8793c76SJason King do_prf(soft_session_t *session_p, CK_PKCS5_PBKD2_PARAMS_PTR params,
1172a8793c76SJason King     soft_object_t *hmac_key, CK_BYTE *newsalt, CK_ULONG saltlen,
11737c478bd9Sstevel@tonic-gate     CK_BYTE *blockdata, CK_ULONG blocklen)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
11767c478bd9Sstevel@tonic-gate 	CK_MECHANISM digest_mech = {CKM_SHA_1_HMAC, NULL, 0};
11777c478bd9Sstevel@tonic-gate 	CK_BYTE buffer[2][SHA1_HASH_SIZE];
11787c478bd9Sstevel@tonic-gate 	CK_ULONG hmac_outlen = SHA1_HASH_SIZE;
11797c478bd9Sstevel@tonic-gate 	CK_ULONG inlen;
11807c478bd9Sstevel@tonic-gate 	CK_BYTE *input, *output;
11817c478bd9Sstevel@tonic-gate 	CK_ULONG i, j;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	input = newsalt;
11847c478bd9Sstevel@tonic-gate 	inlen = saltlen;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	output = buffer[1];
11877c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
11907c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
11917c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_ACTIVE);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	session_p->sign.flags |= CRYPTO_OPERATION_ACTIVE;
11947c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	for (i = 0; i < params->iterations; i++) {
11977c478bd9Sstevel@tonic-gate 		/*
11987c478bd9Sstevel@tonic-gate 		 * The key doesn't change, its always the
11997c478bd9Sstevel@tonic-gate 		 * password iniitally given.
12007c478bd9Sstevel@tonic-gate 		 */
12017c478bd9Sstevel@tonic-gate 		rv = soft_sign_init(session_p, &digest_mech, hmac_key);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
12047c478bd9Sstevel@tonic-gate 			goto cleanup;
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		/* Call PRF function (SHA1_HMAC for now). */
1208f9fbec18Smcpowers 		rv = soft_sign(session_p, input, inlen, output, &hmac_outlen);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
12117c478bd9Sstevel@tonic-gate 			goto cleanup;
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 		/*
12147c478bd9Sstevel@tonic-gate 		 * The first time, initialize the output buffer
12157c478bd9Sstevel@tonic-gate 		 * with the HMAC signature.
12167c478bd9Sstevel@tonic-gate 		 */
12177c478bd9Sstevel@tonic-gate 		if (i == 0) {
12187c478bd9Sstevel@tonic-gate 			(void) memcpy(blockdata, output,
12197c478bd9Sstevel@tonic-gate 			    local_min(blocklen, hmac_outlen));
12207c478bd9Sstevel@tonic-gate 		} else {
12217c478bd9Sstevel@tonic-gate 			/*
12227c478bd9Sstevel@tonic-gate 			 * XOR the existing data with output from PRF.
12237c478bd9Sstevel@tonic-gate 			 *
12247c478bd9Sstevel@tonic-gate 			 * Only XOR up to the length of the blockdata,
12257c478bd9Sstevel@tonic-gate 			 * it may be less than a full hmac buffer when
12267c478bd9Sstevel@tonic-gate 			 * the final block is being computed.
12277c478bd9Sstevel@tonic-gate 			 */
12287c478bd9Sstevel@tonic-gate 			for (j = 0; j < hmac_outlen && j < blocklen; j++)
12297c478bd9Sstevel@tonic-gate 				blockdata[j] ^= output[j];
12307c478bd9Sstevel@tonic-gate 		}
12317c478bd9Sstevel@tonic-gate 		/* Output from previous PRF is input for next round */
12327c478bd9Sstevel@tonic-gate 		input = output;
12337c478bd9Sstevel@tonic-gate 		inlen = hmac_outlen;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 		/*
12367c478bd9Sstevel@tonic-gate 		 * Switch buffers to avoid overuse of memcpy.
12377c478bd9Sstevel@tonic-gate 		 * Initially we used buffer[1], so after the end of
12387c478bd9Sstevel@tonic-gate 		 * the first iteration (i==0), we switch to buffer[0]
12397c478bd9Sstevel@tonic-gate 		 * and continue swapping with each iteration.
12407c478bd9Sstevel@tonic-gate 		 */
12417c478bd9Sstevel@tonic-gate 		output = buffer[i%2];
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate cleanup:
12447c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
12457c478bd9Sstevel@tonic-gate 	session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
12467c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	return (rv);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate static CK_RV
soft_create_hmac_key(soft_session_t * session_p,CK_BYTE * passwd,CK_ULONG passwd_len,soft_object_t ** keyp)12527c478bd9Sstevel@tonic-gate soft_create_hmac_key(soft_session_t *session_p,  CK_BYTE *passwd,
12534c60ecf7SJason King     CK_ULONG passwd_len, soft_object_t **keyp)
12547c478bd9Sstevel@tonic-gate {
12557c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
12567c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS keyclass = CKO_SECRET_KEY;
12577c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE keytype = CKK_GENERIC_SECRET;
12587c478bd9Sstevel@tonic-gate 	CK_BBOOL True = TRUE;
12597c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE keytemplate[4];
1260cfcec266SJason King 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * We must initialize each template member individually
12637c478bd9Sstevel@tonic-gate 	 * because at the time of initial coding for ON10, the
12647c478bd9Sstevel@tonic-gate 	 * compiler was using the "-xc99=%none" option
12657c478bd9Sstevel@tonic-gate 	 * which prevents us from being able to declare the whole
12667c478bd9Sstevel@tonic-gate 	 * template in place as usual.
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	keytemplate[0].type = CKA_CLASS;
12697c478bd9Sstevel@tonic-gate 	keytemplate[0].pValue = &keyclass;
12707c478bd9Sstevel@tonic-gate 	keytemplate[0].ulValueLen =  sizeof (keyclass);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	keytemplate[1].type = CKA_KEY_TYPE;
12737c478bd9Sstevel@tonic-gate 	keytemplate[1].pValue = &keytype;
12747c478bd9Sstevel@tonic-gate 	keytemplate[1].ulValueLen =  sizeof (keytype);
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	keytemplate[2].type = CKA_SIGN;
12777c478bd9Sstevel@tonic-gate 	keytemplate[2].pValue = &True;
12787c478bd9Sstevel@tonic-gate 	keytemplate[2].ulValueLen =  sizeof (True);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	keytemplate[3].type = CKA_VALUE;
12817c478bd9Sstevel@tonic-gate 	keytemplate[3].pValue = passwd;
12827c478bd9Sstevel@tonic-gate 	keytemplate[3].ulValueLen = passwd_len;
12837c478bd9Sstevel@tonic-gate 	/*
12847c478bd9Sstevel@tonic-gate 	 * Create a generic key object to be used for HMAC operations.
12857c478bd9Sstevel@tonic-gate 	 * The "value" for this key is the password from the
12867c478bd9Sstevel@tonic-gate 	 * mechanism parameter structure.
12877c478bd9Sstevel@tonic-gate 	 */
12887c478bd9Sstevel@tonic-gate 	rv = soft_gen_keyobject(keytemplate,
12894c60ecf7SJason King 	    sizeof (keytemplate)/sizeof (CK_ATTRIBUTE), keyp, session_p,
1290f9fbec18Smcpowers 	    CKO_SECRET_KEY, (CK_KEY_TYPE)CKK_GENERIC_SECRET, 0,
12917c478bd9Sstevel@tonic-gate 	    SOFT_CREATE_OBJ, B_TRUE);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	return (rv);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate CK_RV
soft_generate_pkcs5_pbkdf2_key(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * secret_key)12977c478bd9Sstevel@tonic-gate soft_generate_pkcs5_pbkdf2_key(soft_session_t *session_p,
1298a8793c76SJason King     CK_MECHANISM_PTR pMechanism, soft_object_t *secret_key)
12997c478bd9Sstevel@tonic-gate {
13007c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
13017c478bd9Sstevel@tonic-gate 	CK_PKCS5_PBKD2_PARAMS	*params =
13027c478bd9Sstevel@tonic-gate 	    (CK_PKCS5_PBKD2_PARAMS *)pMechanism->pParameter;
13037c478bd9Sstevel@tonic-gate 	CK_ULONG hLen = SHA1_HASH_SIZE;
13047c478bd9Sstevel@tonic-gate 	CK_ULONG dkLen, i;
13057c478bd9Sstevel@tonic-gate 	CK_ULONG blocks, remainder;
13067c478bd9Sstevel@tonic-gate 	soft_object_t *hmac_key = NULL;
13077c478bd9Sstevel@tonic-gate 	CK_BYTE *salt = NULL;
13087c478bd9Sstevel@tonic-gate 	CK_BYTE *keydata = NULL;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	params = (CK_PKCS5_PBKD2_PARAMS_PTR) pMechanism->pParameter;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	if (params->prf != CKP_PKCS5_PBKD2_HMAC_SHA1)
13137c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_PARAM_INVALID);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if (params->pPrfData != NULL || params->ulPrfDataLen != 0)
13167c478bd9Sstevel@tonic-gate 		return (CKR_DATA_INVALID);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (params->saltSource != CKZ_SALT_SPECIFIED ||
13197c478bd9Sstevel@tonic-gate 	    params->iterations == 0)
13207c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_PARAM_INVALID);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Create a key object to use for HMAC operations.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	rv = soft_create_hmac_key(session_p, params->pPassword,
13264c60ecf7SJason King 	    *params->ulPasswordLen, &hmac_key);
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
13297c478bd9Sstevel@tonic-gate 		return (rv);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/* Step 1. */
13327c478bd9Sstevel@tonic-gate 	dkLen = OBJ_SEC_VALUE_LEN(secret_key);  /* length of desired key */
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	if (dkLen > ((((u_longlong_t)1)<<32)-1)*hLen) {
13351f49a79aSZdenek Kotala 		(void) soft_delete_object(session_p, hmac_key, B_FALSE,
13361f49a79aSZdenek Kotala 		    B_FALSE);
13377c478bd9Sstevel@tonic-gate 		return (CKR_KEY_SIZE_RANGE);
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	/* Step 2. */
13417c478bd9Sstevel@tonic-gate 	blocks = dkLen / hLen;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	/* crude "Ceiling" function to adjust the number of blocks to use */
13447c478bd9Sstevel@tonic-gate 	if (blocks * hLen != dkLen)
13457c478bd9Sstevel@tonic-gate 		blocks++;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	remainder = dkLen - ((blocks - 1) * hLen);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/* Step 3 */
13507c478bd9Sstevel@tonic-gate 	salt = (CK_BYTE *)malloc(params->ulSaltSourceDataLen + 4);
13517c478bd9Sstevel@tonic-gate 	if (salt == NULL) {
13521f49a79aSZdenek Kotala 		(void) soft_delete_object(session_p, hmac_key, B_FALSE,
13531f49a79aSZdenek Kotala 		    B_FALSE);
13547c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 	/*
13577c478bd9Sstevel@tonic-gate 	 * Nothing in PKCS#5 says you cannot pass an empty
13587c478bd9Sstevel@tonic-gate 	 * salt, so we will allow for this and not return error
13597c478bd9Sstevel@tonic-gate 	 * if the salt is not specified.
13607c478bd9Sstevel@tonic-gate 	 */
1361f9fbec18Smcpowers 	if (params->pSaltSourceData != NULL && params->ulSaltSourceDataLen > 0)
13627c478bd9Sstevel@tonic-gate 		(void) memcpy(salt, params->pSaltSourceData,
13637c478bd9Sstevel@tonic-gate 		    params->ulSaltSourceDataLen);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Get pointer to the data section of the key,
13677c478bd9Sstevel@tonic-gate 	 * this will be used below as output from the
13687c478bd9Sstevel@tonic-gate 	 * PRF iteration/concatenations so that when the
13697c478bd9Sstevel@tonic-gate 	 * blocks are all iterated, the secret_key will
13707c478bd9Sstevel@tonic-gate 	 * have the resulting derived key value.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	keydata = (CK_BYTE *)OBJ_SEC_VALUE(secret_key);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/* Step 4. */
13757c478bd9Sstevel@tonic-gate 	for (i = 0; i < blocks && (rv == CKR_OK); i++) {
13767c478bd9Sstevel@tonic-gate 		CK_BYTE *s;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 		s = salt + params->ulSaltSourceDataLen;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		/*
13817c478bd9Sstevel@tonic-gate 		 * Append the block index to the salt as input
13827c478bd9Sstevel@tonic-gate 		 * to the PRF.  Block index should start at 1
13837c478bd9Sstevel@tonic-gate 		 * not 0.
13847c478bd9Sstevel@tonic-gate 		 */
13857c478bd9Sstevel@tonic-gate 		*s++ = ((i+1) >> 24) & 0xff;
13867c478bd9Sstevel@tonic-gate 		*s++ = ((i+1) >> 16) & 0xff;
13877c478bd9Sstevel@tonic-gate 		*s++ = ((i+1) >> 8) & 0xff;
13887c478bd9Sstevel@tonic-gate 		*s   = ((i+1)) & 0xff;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 		/*
13917c478bd9Sstevel@tonic-gate 		 * Adjust the key pointer so we always append the
13927c478bd9Sstevel@tonic-gate 		 * PRF output to the current key.
13937c478bd9Sstevel@tonic-gate 		 */
13947c478bd9Sstevel@tonic-gate 		rv = do_prf(session_p, params, hmac_key,
1395f9fbec18Smcpowers 		    salt, params->ulSaltSourceDataLen + 4, keydata,
13967c478bd9Sstevel@tonic-gate 		    ((i + 1) == blocks ? remainder : hLen));
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 		keydata += hLen;
13997c478bd9Sstevel@tonic-gate 	}
14001f49a79aSZdenek Kotala 	(void) soft_delete_object(session_p, hmac_key, B_FALSE, B_FALSE);
1401a8793c76SJason King 	freezero(salt, params->ulSaltSourceDataLen);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	return (rv);
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate CK_RV
soft_wrapkey(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * wrappingKey_p,soft_object_t * hkey_p,CK_BYTE_PTR pWrappedKey,CK_ULONG_PTR pulWrappedKeyLen)14077c478bd9Sstevel@tonic-gate soft_wrapkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
14087c478bd9Sstevel@tonic-gate     soft_object_t *wrappingKey_p, soft_object_t *hkey_p,
14097c478bd9Sstevel@tonic-gate     CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
14107c478bd9Sstevel@tonic-gate {
14117c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
14127c478bd9Sstevel@tonic-gate 	CK_ULONG	plain_len = 0;
14137c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	plain_data = NULL;
14147c478bd9Sstevel@tonic-gate 	CK_ULONG	padded_len = 0;
14157c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	padded_data = NULL;
14167c478bd9Sstevel@tonic-gate 	CK_ULONG	wkey_blksz = 1;		/* so modulo will work right */
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/* Check if the mechanism is supported. */
14197c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
14207c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC_PAD:
14217c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC_PAD:
14227c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC_PAD:
14237c478bd9Sstevel@tonic-gate 		/*
14247c478bd9Sstevel@tonic-gate 		 * Secret key mechs with padding can be used to wrap secret
14257c478bd9Sstevel@tonic-gate 		 * keys and private keys only.  See PKCS#11, * sec 11.14,
14267c478bd9Sstevel@tonic-gate 		 * C_WrapKey and secs 12.* for each mechanism's wrapping/
14277c478bd9Sstevel@tonic-gate 		 * unwrapping constraints.
14287c478bd9Sstevel@tonic-gate 		 */
14297c478bd9Sstevel@tonic-gate 		if (hkey_p->class != CKO_SECRET_KEY && hkey_p->class !=
14307c478bd9Sstevel@tonic-gate 		    CKO_PRIVATE_KEY)
14317c478bd9Sstevel@tonic-gate 			return (CKR_MECHANISM_INVALID);
14327c478bd9Sstevel@tonic-gate 		break;
14337c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
14347c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
14357c478bd9Sstevel@tonic-gate 	case CKM_DES_ECB:
14367c478bd9Sstevel@tonic-gate 	case CKM_DES3_ECB:
14377c478bd9Sstevel@tonic-gate 	case CKM_AES_ECB:
14387c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC:
14397c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC:
14407c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC:
144123c57df7Smcpowers 	case CKM_AES_CTR:
1442f66d273dSizick 	case CKM_BLOWFISH_CBC:
14437c478bd9Sstevel@tonic-gate 		/*
14447c478bd9Sstevel@tonic-gate 		 * Unpadded secret key mechs and private key mechs are only
14457c478bd9Sstevel@tonic-gate 		 * defined for wrapping secret keys.  See PKCS#11 refs above.
14467c478bd9Sstevel@tonic-gate 		 */
14477c478bd9Sstevel@tonic-gate 		if (hkey_p->class != CKO_SECRET_KEY)
14487c478bd9Sstevel@tonic-gate 			return (CKR_MECHANISM_INVALID);
14497c478bd9Sstevel@tonic-gate 		break;
14507c478bd9Sstevel@tonic-gate 	default:
14517c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (hkey_p->class == CKO_SECRET_KEY) {
14557c478bd9Sstevel@tonic-gate 		plain_data = OBJ_SEC_VALUE(hkey_p);
14567c478bd9Sstevel@tonic-gate 		plain_len = OBJ_SEC_VALUE_LEN(hkey_p);
14577c478bd9Sstevel@tonic-gate 	} else {
14587c478bd9Sstevel@tonic-gate 		/*
14597c478bd9Sstevel@tonic-gate 		 * BER-encode the object to be wrapped:  call first with
14607c478bd9Sstevel@tonic-gate 		 * plain_data = NULL to get the size needed, allocate that
14617c478bd9Sstevel@tonic-gate 		 * much space, call again to fill space with actual data.
14627c478bd9Sstevel@tonic-gate 		 */
14637c478bd9Sstevel@tonic-gate 		rv = soft_object_to_asn1(hkey_p, NULL, &plain_len);
14647c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
14657c478bd9Sstevel@tonic-gate 			return (rv);
14667c478bd9Sstevel@tonic-gate 		if ((plain_data = malloc(plain_len)) == NULL)
14677c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
14687c478bd9Sstevel@tonic-gate 		(void) memset(plain_data, 0x0, plain_len);
14697c478bd9Sstevel@tonic-gate 		rv = soft_object_to_asn1(hkey_p, plain_data, &plain_len);
14707c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
14717c478bd9Sstevel@tonic-gate 			goto cleanup_wrap;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	/*
14757c478bd9Sstevel@tonic-gate 	 * For unpadded ECB and CBC mechanisms, the object needs to be
14767c478bd9Sstevel@tonic-gate 	 * padded to the wrapping key's blocksize prior to the encryption.
14777c478bd9Sstevel@tonic-gate 	 */
14787c478bd9Sstevel@tonic-gate 	padded_len = plain_len;
14797c478bd9Sstevel@tonic-gate 	padded_data = plain_data;
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
14827c478bd9Sstevel@tonic-gate 	case CKM_DES_ECB:
14837c478bd9Sstevel@tonic-gate 	case CKM_DES3_ECB:
14847c478bd9Sstevel@tonic-gate 	case CKM_AES_ECB:
14857c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC:
14867c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC:
14877c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC:
1488f66d273dSizick 	case CKM_BLOWFISH_CBC:
14897c478bd9Sstevel@tonic-gate 		/* Find the block size of the wrapping key. */
14907c478bd9Sstevel@tonic-gate 		if (wrappingKey_p->class == CKO_SECRET_KEY) {
14917c478bd9Sstevel@tonic-gate 			switch (wrappingKey_p->key_type) {
14927c478bd9Sstevel@tonic-gate 			case CKK_DES:
14937c478bd9Sstevel@tonic-gate 			case CKK_DES2:
14947c478bd9Sstevel@tonic-gate 			case CKK_DES3:
14957c478bd9Sstevel@tonic-gate 				wkey_blksz = DES_BLOCK_LEN;
14967c478bd9Sstevel@tonic-gate 				break;
14977c478bd9Sstevel@tonic-gate 			case CKK_AES:
14987c478bd9Sstevel@tonic-gate 				wkey_blksz = AES_BLOCK_LEN;
14997c478bd9Sstevel@tonic-gate 				break;
1500f66d273dSizick 			case CKK_BLOWFISH:
1501f66d273dSizick 				wkey_blksz = BLOWFISH_BLOCK_LEN;
1502f66d273dSizick 				break;
15037c478bd9Sstevel@tonic-gate 			default:
15047c478bd9Sstevel@tonic-gate 				break;
15057c478bd9Sstevel@tonic-gate 			}
15067c478bd9Sstevel@tonic-gate 		} else {
15077c478bd9Sstevel@tonic-gate 			rv = CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
15087c478bd9Sstevel@tonic-gate 			goto cleanup_wrap;
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		/* Extend the plain text data to block size boundary.  */
15127c478bd9Sstevel@tonic-gate 		if ((padded_len % wkey_blksz) != 0) {
15137c478bd9Sstevel@tonic-gate 			padded_len += (wkey_blksz - (plain_len % wkey_blksz));
15147c478bd9Sstevel@tonic-gate 			if ((padded_data = malloc(padded_len)) == NULL) {
15157c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
15167c478bd9Sstevel@tonic-gate 				goto cleanup_wrap;
15177c478bd9Sstevel@tonic-gate 			}
15187c478bd9Sstevel@tonic-gate 			(void) memset(padded_data, 0x0, padded_len);
15197c478bd9Sstevel@tonic-gate 			(void) memcpy(padded_data, plain_data, plain_len);
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 		break;
15227c478bd9Sstevel@tonic-gate 	default:
15237c478bd9Sstevel@tonic-gate 		break;
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	rv = soft_encrypt_init(session_p, pMechanism, wrappingKey_p);
15277c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
15287c478bd9Sstevel@tonic-gate 		goto cleanup_wrap;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	rv = soft_encrypt(session_p, padded_data, padded_len,
15317c478bd9Sstevel@tonic-gate 	    pWrappedKey, pulWrappedKeyLen);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate cleanup_wrap:
15347c478bd9Sstevel@tonic-gate 	if (padded_data != NULL && padded_len != plain_len) {
15357c478bd9Sstevel@tonic-gate 		/* Clear buffer before returning to memory pool. */
1536a8793c76SJason King 		freezero(padded_data, padded_len);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	if ((hkey_p->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
15407c478bd9Sstevel@tonic-gate 		/* Clear buffer before returning to memory pool. */
1541a8793c76SJason King 		freezero(plain_data, plain_len);
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	return (rv);
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate /*
15487c478bd9Sstevel@tonic-gate  * Quick check for whether unwrapped key length is appropriate for key type
15497c478bd9Sstevel@tonic-gate  * and whether it needs to be truncated (in case the wrapping function had
15507c478bd9Sstevel@tonic-gate  * to pad the key prior to wrapping).
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate static CK_RV
soft_unwrap_secret_len_check(CK_KEY_TYPE keytype,CK_MECHANISM_TYPE mechtype,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount)15537c478bd9Sstevel@tonic-gate soft_unwrap_secret_len_check(CK_KEY_TYPE keytype, CK_MECHANISM_TYPE mechtype,
15547c478bd9Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	CK_ULONG	i;
15577c478bd9Sstevel@tonic-gate 	boolean_t	isValueLen = B_FALSE;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/*
15607c478bd9Sstevel@tonic-gate 	 * Based on the key type and the mech used to unwrap, need to
15617c478bd9Sstevel@tonic-gate 	 * determine if CKA_VALUE_LEN should or should not be specified.
15627c478bd9Sstevel@tonic-gate 	 * PKCS#11 v2.11 restricts CKA_VALUE_LEN from being specified
15637c478bd9Sstevel@tonic-gate 	 * for C_UnwrapKey for all mechs and key types, but v2.20 loosens
15647c478bd9Sstevel@tonic-gate 	 * that restriction, perhaps because it makes it impossible to
15657c478bd9Sstevel@tonic-gate 	 * determine the original length of unwrapped variable-length secret
15667c478bd9Sstevel@tonic-gate 	 * keys, such as RC4, AES, and GENERIC_SECRET.  These variable-length
15677c478bd9Sstevel@tonic-gate 	 * secret keys would have been padded with trailing null-bytes so
15687c478bd9Sstevel@tonic-gate 	 * that they could be successfully wrapped with *_ECB and *_CBC
15697c478bd9Sstevel@tonic-gate 	 * mechanisms.  Hence for unwrapping with these mechs, CKA_VALUE_LEN
15707c478bd9Sstevel@tonic-gate 	 * must be specified.  For unwrapping with other mechs, such as
15717c478bd9Sstevel@tonic-gate 	 * *_CBC_PAD, the CKA_VALUE_LEN is not needed.
15727c478bd9Sstevel@tonic-gate 	 */
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	/* Find out if template has CKA_VALUE_LEN. */
15757c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttributeCount; i++) {
15767c478bd9Sstevel@tonic-gate 		if (pTemplate[i].type == CKA_VALUE_LEN &&
15777c478bd9Sstevel@tonic-gate 		    pTemplate[i].pValue != NULL) {
15787c478bd9Sstevel@tonic-gate 			isValueLen = B_TRUE;
15797c478bd9Sstevel@tonic-gate 			break;
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	/* Does its presence  conflict with the mech type and key type? */
15847c478bd9Sstevel@tonic-gate 	switch (mechtype) {
15857c478bd9Sstevel@tonic-gate 	case CKM_DES_ECB:
15867c478bd9Sstevel@tonic-gate 	case CKM_DES3_ECB:
15877c478bd9Sstevel@tonic-gate 	case CKM_AES_ECB:
15887c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC:
15897c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC:
15907c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC:
1591f66d273dSizick 	case CKM_BLOWFISH_CBC:
15927c478bd9Sstevel@tonic-gate 		/*
15937c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN must be specified
15947c478bd9Sstevel@tonic-gate 		 * if keytype is CKK_RC4, CKK_AES and CKK_GENERIC_SECRET
15957c478bd9Sstevel@tonic-gate 		 * and must not be specified otherwise
15967c478bd9Sstevel@tonic-gate 		 */
15977c478bd9Sstevel@tonic-gate 		switch (keytype) {
15987c478bd9Sstevel@tonic-gate 		case CKK_DES:
15997c478bd9Sstevel@tonic-gate 		case CKK_DES2:
16007c478bd9Sstevel@tonic-gate 		case CKK_DES3:
16017c478bd9Sstevel@tonic-gate 			if (isValueLen)
16027c478bd9Sstevel@tonic-gate 				return (CKR_TEMPLATE_INCONSISTENT);
16037c478bd9Sstevel@tonic-gate 			break;
16047c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
16057c478bd9Sstevel@tonic-gate 		case CKK_RC4:
16067c478bd9Sstevel@tonic-gate 		case CKK_AES:
1607f66d273dSizick 		case CKK_BLOWFISH:
16087c478bd9Sstevel@tonic-gate 			if (!isValueLen)
16097c478bd9Sstevel@tonic-gate 				return (CKR_TEMPLATE_INCOMPLETE);
16107c478bd9Sstevel@tonic-gate 			break;
16117c478bd9Sstevel@tonic-gate 		default:
16127c478bd9Sstevel@tonic-gate 			return (CKR_FUNCTION_NOT_SUPPORTED);
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate 		break;
16157c478bd9Sstevel@tonic-gate 	default:
16167c478bd9Sstevel@tonic-gate 		/* CKA_VALUE_LEN must not be specified */
16177c478bd9Sstevel@tonic-gate 		if (isValueLen)
16187c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCONSISTENT);
16197c478bd9Sstevel@tonic-gate 		break;
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	return (CKR_OK);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate CK_RV
soft_unwrapkey(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * unwrappingkey_p,CK_BYTE_PTR pWrappedKey,CK_ULONG ulWrappedKeyLen,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)16267c478bd9Sstevel@tonic-gate soft_unwrapkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
1627a8793c76SJason King     soft_object_t *unwrappingkey_p, CK_BYTE_PTR pWrappedKey,
1628a8793c76SJason King     CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
1629a8793c76SJason King     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
16307c478bd9Sstevel@tonic-gate {
16317c478bd9Sstevel@tonic-gate 	CK_RV			rv = CKR_OK;
16327c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS		new_obj_class = ~0UL;
16337c478bd9Sstevel@tonic-gate 	int			i = 0;
16347c478bd9Sstevel@tonic-gate 	soft_object_t		*new_objp = NULL;
16357c478bd9Sstevel@tonic-gate 	boolean_t		persistent = B_FALSE;
16367c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR		plain_data = NULL;
16377c478bd9Sstevel@tonic-gate 	CK_ULONG		plain_len = 0;
16387c478bd9Sstevel@tonic-gate 	secret_key_obj_t	*sck = NULL;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/* Scan the attribute template for the object class. */
16417c478bd9Sstevel@tonic-gate 	if (pTemplate != NULL && ulAttributeCount != 0) {
16427c478bd9Sstevel@tonic-gate 		for (i = 0; i < ulAttributeCount; i++) {
16437c478bd9Sstevel@tonic-gate 			if (pTemplate[i].type == CKA_CLASS) {
16447c478bd9Sstevel@tonic-gate 				new_obj_class =
16457c478bd9Sstevel@tonic-gate 				    *((CK_OBJECT_CLASS *)pTemplate[i].pValue);
16467c478bd9Sstevel@tonic-gate 				break;
16477c478bd9Sstevel@tonic-gate 			}
16487c478bd9Sstevel@tonic-gate 		}
16497c478bd9Sstevel@tonic-gate 		if (new_obj_class == ~0UL)
16507c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCOMPLETE);
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/*
16547c478bd9Sstevel@tonic-gate 	 * Check if the mechanism is supported, and now that the new
16557c478bd9Sstevel@tonic-gate 	 * object's class is known, the mechanism selected should be
16567c478bd9Sstevel@tonic-gate 	 * capable of doing the unwrap.
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
16597c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
16607c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
16617c478bd9Sstevel@tonic-gate 	case CKM_DES_ECB:
16627c478bd9Sstevel@tonic-gate 	case CKM_DES3_ECB:
16637c478bd9Sstevel@tonic-gate 	case CKM_AES_ECB:
16647c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC:
16657c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC:
16667c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC:
1667f66d273dSizick 	case CKM_BLOWFISH_CBC:
16687c478bd9Sstevel@tonic-gate 		if (new_obj_class != CKO_SECRET_KEY)
16697c478bd9Sstevel@tonic-gate 			return (CKR_MECHANISM_INVALID);
16707c478bd9Sstevel@tonic-gate 		break;
16717c478bd9Sstevel@tonic-gate 	case CKM_DES_CBC_PAD:
16727c478bd9Sstevel@tonic-gate 	case CKM_DES3_CBC_PAD:
16737c478bd9Sstevel@tonic-gate 	case CKM_AES_CBC_PAD:
16747c478bd9Sstevel@tonic-gate 		if (new_obj_class != CKO_SECRET_KEY && new_obj_class !=
16757c478bd9Sstevel@tonic-gate 		    CKO_PRIVATE_KEY)
16767c478bd9Sstevel@tonic-gate 			return (CKR_MECHANISM_INVALID);
16777c478bd9Sstevel@tonic-gate 		break;
16787c478bd9Sstevel@tonic-gate 	default:
16797c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	/* Create a new object based on the attribute template. */
16837c478bd9Sstevel@tonic-gate 	rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
1684cfcec266SJason King 	    &new_objp, session_p, (CK_OBJECT_CLASS)~0UL,
16857c478bd9Sstevel@tonic-gate 	    (CK_KEY_TYPE)~0UL, 0, SOFT_UNWRAP_KEY, B_FALSE);
16867c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
16877c478bd9Sstevel@tonic-gate 		return (rv);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/*
16907c478bd9Sstevel@tonic-gate 	 * New key will have CKA_ALWAYS_SENSITIVE and CKA_NEVER_EXTRACTABLE
16917c478bd9Sstevel@tonic-gate 	 * both set to FALSE.  CKA_EXTRACTABLE will be set _by_default_ to
16927c478bd9Sstevel@tonic-gate 	 * true -- leaving the possibility that it may be set FALSE by the
16937c478bd9Sstevel@tonic-gate 	 * supplied attribute template.  If the precise template cannot be
16947c478bd9Sstevel@tonic-gate 	 * supported, unwrap fails.  PKCS#11 spec, Sec. 11.14, C_UnwrapKey.
16957c478bd9Sstevel@tonic-gate 	 *
16967c478bd9Sstevel@tonic-gate 	 * Therefore, check the new object's NEVER_EXTRACTABLE_BOOL_ON and
16977c478bd9Sstevel@tonic-gate 	 * ALWAYS_SENSITVE_BOOL_ON; if they are TRUE, the template must
16987c478bd9Sstevel@tonic-gate 	 * have supplied them and therefore we cannot honor the unwrap.
16997c478bd9Sstevel@tonic-gate 	 */
17007c478bd9Sstevel@tonic-gate 	if ((new_objp->bool_attr_mask & NEVER_EXTRACTABLE_BOOL_ON) ||
17017c478bd9Sstevel@tonic-gate 	    (new_objp->bool_attr_mask & ALWAYS_SENSITIVE_BOOL_ON)) {
17027c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
17037c478bd9Sstevel@tonic-gate 		goto cleanup_unwrap;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	rv = soft_decrypt_init(session_p, pMechanism, unwrappingkey_p);
17077c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
17087c478bd9Sstevel@tonic-gate 		goto cleanup_unwrap;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	/* First get the length of the plain data */
17117c478bd9Sstevel@tonic-gate 	rv = soft_decrypt(session_p, pWrappedKey, ulWrappedKeyLen, NULL,
17127c478bd9Sstevel@tonic-gate 	    &plain_len);
17137c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
17147c478bd9Sstevel@tonic-gate 		goto cleanup_unwrap;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/* Allocate space for the unwrapped data */
17177c478bd9Sstevel@tonic-gate 	if ((plain_data = malloc(plain_len)) == NULL) {
17187c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
17197c478bd9Sstevel@tonic-gate 		goto cleanup_unwrap;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 	(void) memset(plain_data, 0x0, plain_len);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/* Perform actual decryption into the allocated space. */
17247c478bd9Sstevel@tonic-gate 	rv = soft_decrypt(session_p, pWrappedKey, ulWrappedKeyLen, plain_data,
17257c478bd9Sstevel@tonic-gate 	    &plain_len);
17267c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
17277c478bd9Sstevel@tonic-gate 		goto cleanup_unwrap;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	if (new_objp->class == CKO_SECRET_KEY) {
17307c478bd9Sstevel@tonic-gate 		/*
17317c478bd9Sstevel@tonic-gate 		 * Since no ASN.1 encoding is done for secret keys, check for
17327c478bd9Sstevel@tonic-gate 		 * appropriateness and copy decrypted buffer to the key object.
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		/* Check keytype and mechtype don't conflict with valuelen */
17367c478bd9Sstevel@tonic-gate 		rv = soft_unwrap_secret_len_check(new_objp->key_type,
17377c478bd9Sstevel@tonic-gate 		    pMechanism->mechanism, pTemplate, ulAttributeCount);
17387c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
17397c478bd9Sstevel@tonic-gate 			goto cleanup_unwrap;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		/*
17427c478bd9Sstevel@tonic-gate 		 * Allocate the secret key structure if not already there;
17437c478bd9Sstevel@tonic-gate 		 * it will exist for variable length keys since CKA_VALUE_LEN
17447c478bd9Sstevel@tonic-gate 		 * is specified and saved, but not for fixed length keys.
17457c478bd9Sstevel@tonic-gate 		 */
17467c478bd9Sstevel@tonic-gate 		if (OBJ_SEC(new_objp) == NULL) {
17477c478bd9Sstevel@tonic-gate 			if ((sck = calloc(1, sizeof (secret_key_obj_t))) ==
17487c478bd9Sstevel@tonic-gate 			    NULL) {
17497c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
17507c478bd9Sstevel@tonic-gate 				goto cleanup_unwrap;
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 			OBJ_SEC(new_objp) = sck;
17537c478bd9Sstevel@tonic-gate 		}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		switch (new_objp->key_type) {
17567c478bd9Sstevel@tonic-gate 		/* Fixed length secret keys don't have CKA_VALUE_LEN */
17577c478bd9Sstevel@tonic-gate 		case CKK_DES:
17587c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(new_objp) = DES_KEYSIZE;
17597c478bd9Sstevel@tonic-gate 			break;
17607c478bd9Sstevel@tonic-gate 		case CKK_DES2:
17617c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(new_objp) = DES2_KEYSIZE;
17627c478bd9Sstevel@tonic-gate 			break;
17637c478bd9Sstevel@tonic-gate 		case CKK_DES3:
17647c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(new_objp) = DES3_KEYSIZE;
17657c478bd9Sstevel@tonic-gate 			break;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		/*
17687c478bd9Sstevel@tonic-gate 		 * Variable length secret keys.  CKA_VALUE_LEN must be
17697c478bd9Sstevel@tonic-gate 		 * provided by the template when mech is *_ECB or *_CBC, and
17707c478bd9Sstevel@tonic-gate 		 * should already have been set during soft_gen_keyobject().
17717c478bd9Sstevel@tonic-gate 		 * Otherwise we don't need CKA_VALUE_LEN.
17727c478bd9Sstevel@tonic-gate 		 */
17737c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
17747c478bd9Sstevel@tonic-gate 		case CKK_RC4:
17757c478bd9Sstevel@tonic-gate 		case CKK_AES:
1776f66d273dSizick 		case CKK_BLOWFISH:
17777c478bd9Sstevel@tonic-gate 			break;
17787c478bd9Sstevel@tonic-gate 		default:
17797c478bd9Sstevel@tonic-gate 			rv = CKR_WRAPPED_KEY_INVALID;
17807c478bd9Sstevel@tonic-gate 			goto cleanup_unwrap;
17817c478bd9Sstevel@tonic-gate 		};
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 		if (OBJ_SEC_VALUE_LEN(new_objp) == 0) {
17847c478bd9Sstevel@tonic-gate 			/* No CKA_VALUE_LEN set so set it now and save data */
17857c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE_LEN(new_objp) = plain_len;
17867c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE(new_objp) = plain_data;
17877c478bd9Sstevel@tonic-gate 		} else if (OBJ_SEC_VALUE_LEN(new_objp) == plain_len) {
17887c478bd9Sstevel@tonic-gate 			/* No need to truncate, just save the data */
17897c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE(new_objp) = plain_data;
17907c478bd9Sstevel@tonic-gate 		} else if (OBJ_SEC_VALUE_LEN(new_objp) > plain_len) {
17917c478bd9Sstevel@tonic-gate 			/* Length can't be bigger than what was decrypted */
17927c478bd9Sstevel@tonic-gate 			rv = CKR_WRAPPED_KEY_LEN_RANGE;
17937c478bd9Sstevel@tonic-gate 			goto cleanup_unwrap;
17947c478bd9Sstevel@tonic-gate 		} else {	/* betw 0 and plain_len, hence padded */
17957c478bd9Sstevel@tonic-gate 			/* Truncate the data before saving. */
17967c478bd9Sstevel@tonic-gate 			OBJ_SEC_VALUE(new_objp) = realloc(plain_data,
17977c478bd9Sstevel@tonic-gate 			    OBJ_SEC_VALUE_LEN(new_objp));
17987c478bd9Sstevel@tonic-gate 			if (OBJ_SEC_VALUE(new_objp) == NULL) {
17997c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
18007c478bd9Sstevel@tonic-gate 				goto cleanup_unwrap;
18017c478bd9Sstevel@tonic-gate 			}
18027c478bd9Sstevel@tonic-gate 		}
18037c478bd9Sstevel@tonic-gate 	} else {
18047c478bd9Sstevel@tonic-gate 		/* BER-decode the object to be unwrapped. */
18057c478bd9Sstevel@tonic-gate 		rv = soft_asn1_to_object(new_objp, plain_data, plain_len);
18067c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
18077c478bd9Sstevel@tonic-gate 			goto cleanup_unwrap;
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/* If it needs to be persistent, write it to the keystore */
18117c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(new_objp)) {
18127c478bd9Sstevel@tonic-gate 		persistent = B_TRUE;
18137c478bd9Sstevel@tonic-gate 		rv = soft_put_object_to_keystore(new_objp);
18147c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
18157c478bd9Sstevel@tonic-gate 			goto cleanup_unwrap;
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if (new_objp->class != CKO_SECRET_KEY) {
18197c478bd9Sstevel@tonic-gate 		/* Clear buffer before returning to memory pool. */
1820a8793c76SJason King 		freezero(plain_data, plain_len);
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	*phKey = (CK_OBJECT_HANDLE)new_objp;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	return (CKR_OK);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate cleanup_unwrap:
18287c478bd9Sstevel@tonic-gate 	/* The decrypted private key buffer must be freed explicitly. */
18297c478bd9Sstevel@tonic-gate 	if ((new_objp->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
18307c478bd9Sstevel@tonic-gate 		/* Clear buffer before returning to memory pool. */
1831a8793c76SJason King 		freezero(plain_data, plain_len);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/* sck and new_objp are indirectly free()d inside these functions */
18357c478bd9Sstevel@tonic-gate 	if (IS_TOKEN_OBJECT(new_objp))
18367c478bd9Sstevel@tonic-gate 		soft_delete_token_object(new_objp, persistent, B_FALSE);
18377c478bd9Sstevel@tonic-gate 	else
18381f49a79aSZdenek Kotala 		soft_delete_object(session_p, new_objp, B_FALSE, B_FALSE);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	return (rv);
18417c478bd9Sstevel@tonic-gate }
1842