xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softKeys.c (revision 9341:e32192a0f3a4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52940Sizick  * Common Development and Distribution License (the "License").
62940Sizick  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9341SAnthony.Scarpino@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <security/cryptoki.h>
270Sstevel@tonic-gate #include "softGlobal.h"
280Sstevel@tonic-gate #include "softSession.h"
290Sstevel@tonic-gate #include "softKeys.h"
300Sstevel@tonic-gate #include "softOps.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate CK_RV
C_GenerateKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,CK_OBJECT_HANDLE_PTR phKey)340Sstevel@tonic-gate C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
350Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 	CK_RV		rv;
390Sstevel@tonic-gate 	soft_session_t	*session_p;
400Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	if (!softtoken_initialized)
430Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	/* Obtain the session pointer. */
460Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
470Sstevel@tonic-gate 	if (rv != CKR_OK)
480Sstevel@tonic-gate 		return (rv);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if ((pMechanism == NULL) || (phKey == NULL)) {
510Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
520Sstevel@tonic-gate 		goto clean_exit;
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	if ((pTemplate == NULL) && (ulCount != 0)) {
560Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
570Sstevel@tonic-gate 		goto clean_exit;
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	rv = soft_genkey(session_p, pMechanism, pTemplate,
610Sstevel@tonic-gate 	    ulCount, phKey);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate clean_exit:
640Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
650Sstevel@tonic-gate 	return (rv);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 
700Sstevel@tonic-gate CK_RV
C_GenerateKeyPair(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pPublicKeyTemplate,CK_ULONG ulPublicKeyAttributeCount,CK_ATTRIBUTE_PTR pPrivateKeyTemplate,CK_ULONG ulPrivateKeyAttributeCount,CK_OBJECT_HANDLE_PTR phPublicKey,CK_OBJECT_HANDLE_PTR phPrivateKey)710Sstevel@tonic-gate C_GenerateKeyPair(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
720Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount,
730Sstevel@tonic-gate     CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount,
740Sstevel@tonic-gate     CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	CK_RV		rv;
780Sstevel@tonic-gate 	soft_session_t	*session_p;
790Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (!softtoken_initialized)
820Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/* Obtain the session pointer. */
850Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
860Sstevel@tonic-gate 	if (rv != CKR_OK)
870Sstevel@tonic-gate 		return (rv);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if ((pMechanism == NULL) || (phPublicKey == NULL) ||
900Sstevel@tonic-gate 	    (phPrivateKey == NULL)) {
910Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
920Sstevel@tonic-gate 		goto clean_exit;
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if ((pPublicKeyTemplate == NULL) ||
960Sstevel@tonic-gate 	    (ulPublicKeyAttributeCount == 0)) {
970Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
980Sstevel@tonic-gate 		goto clean_exit;
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if ((pPrivateKeyTemplate == NULL) &&
1020Sstevel@tonic-gate 	    (ulPrivateKeyAttributeCount != 0)) {
1030Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
1040Sstevel@tonic-gate 		goto clean_exit;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	rv = soft_genkey_pair(session_p, pMechanism, pPublicKeyTemplate,
1080Sstevel@tonic-gate 	    ulPublicKeyAttributeCount, pPrivateKeyTemplate,
1090Sstevel@tonic-gate 	    ulPrivateKeyAttributeCount, phPublicKey, phPrivateKey);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate clean_exit:
1120Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
1130Sstevel@tonic-gate 	return (rv);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate CK_RV
C_WrapKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hWrappingKey,CK_OBJECT_HANDLE hKey,CK_BYTE_PTR pWrappedKey,CK_ULONG_PTR pulWrappedKeyLen)1170Sstevel@tonic-gate C_WrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
1180Sstevel@tonic-gate     CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
1190Sstevel@tonic-gate     CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	CK_RV		rv;
1220Sstevel@tonic-gate 	soft_session_t	*session_p;
1230Sstevel@tonic-gate 	soft_object_t	*wrappingkey_p;
1240Sstevel@tonic-gate 	soft_object_t	*hkey_p;
1250Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (!softtoken_initialized)
1280Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/* Obtain the session pointer. */
1310Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
1320Sstevel@tonic-gate 	if (rv != CKR_OK)
1330Sstevel@tonic-gate 		return (rv);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (pMechanism == NULL) {
1360Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
1370Sstevel@tonic-gate 		goto clean_exit;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (pulWrappedKeyLen == NULL) {
1410Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
1420Sstevel@tonic-gate 		goto clean_exit;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/* Obtain the wrapping key object pointer. */
1460Sstevel@tonic-gate 	HANDLE2OBJECT(hWrappingKey, wrappingkey_p, rv);
1470Sstevel@tonic-gate 	if (rv != CKR_OK) {
1480Sstevel@tonic-gate 		rv = CKR_WRAPPING_KEY_HANDLE_INVALID;
1490Sstevel@tonic-gate 		goto clean_exit;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* Obtain the to-be-wrapped key object pointer. */
1530Sstevel@tonic-gate 	HANDLE2OBJECT(hKey, hkey_p, rv);
1540Sstevel@tonic-gate 	if (rv != CKR_OK)
1550Sstevel@tonic-gate 		goto clean_exit1;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* Check if given wrapping key may be used for wrapping. */
1580Sstevel@tonic-gate 	if (!(wrappingkey_p->bool_attr_mask & WRAP_BOOL_ON)) {
1590Sstevel@tonic-gate 		rv = CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
1600Sstevel@tonic-gate 		goto clean_exit2;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/* Check if given wrapping key may be used for encryption. */
1640Sstevel@tonic-gate 	if (!(wrappingkey_p->bool_attr_mask & ENCRYPT_BOOL_ON)) {
165*9341SAnthony.Scarpino@Sun.COM 		rv = CKR_KEY_FUNCTION_NOT_PERMITTED;
1660Sstevel@tonic-gate 		goto clean_exit2;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Check to see if key to be wrapped is extractable.
1710Sstevel@tonic-gate 	 * Note: this should always be true for softtoken keys.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	if (!(hkey_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
1740Sstevel@tonic-gate 		rv = CKR_KEY_UNEXTRACTABLE;
1750Sstevel@tonic-gate 		goto clean_exit2;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
1790Sstevel@tonic-gate 	lock_held = B_TRUE;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Wrapping key objects requires calling encrypt operations.
1830Sstevel@tonic-gate 	 * Check to see if encrypt operation is already active.
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	if (session_p->encrypt.flags & CRYPTO_OPERATION_ACTIVE) {
1860Sstevel@tonic-gate 		/* free the memory to avoid memory leak */
1870Sstevel@tonic-gate 		soft_crypt_cleanup(session_p, B_TRUE, lock_held);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/* This active flag will remain ON while wrapping the key. */
1910Sstevel@tonic-gate 	session_p->encrypt.flags = CRYPTO_OPERATION_ACTIVE;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
1940Sstevel@tonic-gate 	lock_held = B_FALSE;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	rv = soft_wrapkey(session_p, pMechanism, wrappingkey_p,
1970Sstevel@tonic-gate 	    hkey_p, pWrappedKey, pulWrappedKeyLen);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
2002940Sizick 
2012940Sizick 	lock_held = B_TRUE;
2020Sstevel@tonic-gate 	session_p->encrypt.flags = 0;
2032940Sizick 
2042940Sizick 	if ((rv == CKR_OK && pWrappedKey == NULL) ||
2052940Sizick 	    rv == CKR_BUFFER_TOO_SMALL)
2062940Sizick 		soft_crypt_cleanup(session_p, B_TRUE, lock_held);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate clean_exit2:
2090Sstevel@tonic-gate 	OBJ_REFRELE(hkey_p);
2100Sstevel@tonic-gate clean_exit1:
2110Sstevel@tonic-gate 	OBJ_REFRELE(wrappingkey_p);
2120Sstevel@tonic-gate clean_exit:
2130Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
2140Sstevel@tonic-gate 	return (rv);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate CK_RV
C_UnwrapKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hUnwrappingKey,CK_BYTE_PTR pWrappedKey,CK_ULONG ulWrappedKeyLen,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)2180Sstevel@tonic-gate C_UnwrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
2190Sstevel@tonic-gate     CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey,
2200Sstevel@tonic-gate     CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
2210Sstevel@tonic-gate     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	CK_RV		rv;
2240Sstevel@tonic-gate 	soft_session_t	*session_p;
2250Sstevel@tonic-gate 	soft_object_t	*unwrappingkey_p;
2260Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (!softtoken_initialized)
2290Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* Obtain the session pointer. */
2320Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
2330Sstevel@tonic-gate 	if (rv != CKR_OK)
2340Sstevel@tonic-gate 		return (rv);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (pMechanism == NULL) {
2370Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2380Sstevel@tonic-gate 		goto clean_exit;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if ((pTemplate == NULL) || (ulAttributeCount == 0)) {
2420Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2430Sstevel@tonic-gate 		goto clean_exit;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if ((pWrappedKey == NULL) || (ulWrappedKeyLen == 0)) {
2470Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2480Sstevel@tonic-gate 		goto clean_exit;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (phKey == NULL) {
2520Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2530Sstevel@tonic-gate 		goto clean_exit;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/* Obtain the unwrapping key object pointer. */
2570Sstevel@tonic-gate 	HANDLE2OBJECT(hUnwrappingKey, unwrappingkey_p, rv);
2580Sstevel@tonic-gate 	if (rv != CKR_OK) {
2590Sstevel@tonic-gate 		rv = CKR_UNWRAPPING_KEY_HANDLE_INVALID;
2600Sstevel@tonic-gate 		goto clean_exit;
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* Check if given unwrapping key may be used for unwrapping. */
2640Sstevel@tonic-gate 	if (!(unwrappingkey_p->bool_attr_mask & UNWRAP_BOOL_ON)) {
2650Sstevel@tonic-gate 		rv = CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT;
2660Sstevel@tonic-gate 		goto clean_exit1;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/* Check if given unwrapping key may be used to decrypt. */
2700Sstevel@tonic-gate 	if (!(unwrappingkey_p->bool_attr_mask & DECRYPT_BOOL_ON)) {
271*9341SAnthony.Scarpino@Sun.COM 		rv = CKR_KEY_FUNCTION_NOT_PERMITTED;
2720Sstevel@tonic-gate 		goto clean_exit1;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
2760Sstevel@tonic-gate 	lock_held = B_TRUE;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * Unwrapping key objects requires calling decrypt operations.
2800Sstevel@tonic-gate 	 * Check to see if decrypt operation is already active.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	if (session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE) {
2830Sstevel@tonic-gate 		/* free the memory to avoid memory leak */
2840Sstevel@tonic-gate 		soft_crypt_cleanup(session_p, B_FALSE, lock_held);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * This active flag will remain ON until application
2890Sstevel@tonic-gate 	 * is done unwrapping the key.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	session_p->decrypt.flags = CRYPTO_OPERATION_ACTIVE;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
2940Sstevel@tonic-gate 	lock_held = B_FALSE;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	rv = soft_unwrapkey(session_p, pMechanism, unwrappingkey_p,
2970Sstevel@tonic-gate 	    pWrappedKey, ulWrappedKeyLen, pTemplate, ulAttributeCount,
2980Sstevel@tonic-gate 	    phKey);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3012940Sizick 
3022940Sizick 	if ((rv == CKR_OK && pWrappedKey == NULL) ||
3032940Sizick 	    rv == CKR_BUFFER_TOO_SMALL)
3042940Sizick 		soft_crypt_cleanup(session_p, B_TRUE, lock_held);
3052940Sizick 
3060Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
3070Sstevel@tonic-gate 	lock_held = B_TRUE;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate clean_exit1:
3100Sstevel@tonic-gate 	OBJ_REFRELE(unwrappingkey_p);
3110Sstevel@tonic-gate clean_exit:
3120Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
3130Sstevel@tonic-gate 	return (rv);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate CK_RV
C_DeriveKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hBaseKey,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)3180Sstevel@tonic-gate C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
3190Sstevel@tonic-gate     CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
3200Sstevel@tonic-gate     CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	CK_RV		rv;
3240Sstevel@tonic-gate 	soft_session_t	*session_p;
3250Sstevel@tonic-gate 	soft_object_t	*basekey_p;
3260Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (!softtoken_initialized)
3290Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/* Obtain the session pointer. */
3320Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
3330Sstevel@tonic-gate 	if (rv != CKR_OK)
3340Sstevel@tonic-gate 		return (rv);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (pMechanism == NULL) {
3370Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3380Sstevel@tonic-gate 		goto clean_exit;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (((pTemplate != NULL) && (ulAttributeCount == 0)) ||
3420Sstevel@tonic-gate 	    ((pTemplate == NULL) && (ulAttributeCount != 0))) {
3430Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3440Sstevel@tonic-gate 		goto clean_exit;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/* Obtain the private key object pointer. */
3480Sstevel@tonic-gate 	HANDLE2OBJECT(hBaseKey, basekey_p, rv);
3490Sstevel@tonic-gate 	if (rv != CKR_OK)
3500Sstevel@tonic-gate 		goto clean_exit;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/* Check to see if key object allows for derivation. */
3530Sstevel@tonic-gate 	if (!(basekey_p->bool_attr_mask & DERIVE_BOOL_ON)) {
354*9341SAnthony.Scarpino@Sun.COM 		rv = CKR_KEY_FUNCTION_NOT_PERMITTED;
3550Sstevel@tonic-gate 		goto clean_exit1;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	rv = soft_derivekey(session_p, pMechanism, basekey_p,
3590Sstevel@tonic-gate 	    pTemplate, ulAttributeCount, phKey);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate clean_exit1:
3620Sstevel@tonic-gate 	OBJ_REFRELE(basekey_p);
3630Sstevel@tonic-gate clean_exit:
3640Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
3650Sstevel@tonic-gate 	return (rv);
3660Sstevel@tonic-gate }
367