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 54c21f043Sizick * Common Development and Distribution License (the "License"). 64c21f043Sizick * 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 /* 22*d288ba74SAnthony Scarpino * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <security/cryptoki.h> 277c478bd9Sstevel@tonic-gate #include "softGlobal.h" 287c478bd9Sstevel@tonic-gate #include "softSession.h" 297c478bd9Sstevel@tonic-gate #include "softKeys.h" 307c478bd9Sstevel@tonic-gate #include "softOps.h" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate CK_RV 347c478bd9Sstevel@tonic-gate C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 357c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey) 367c478bd9Sstevel@tonic-gate { 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate CK_RV rv; 397c478bd9Sstevel@tonic-gate soft_session_t *session_p; 407c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 437c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* Obtain the session pointer. */ 467c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 477c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 487c478bd9Sstevel@tonic-gate return (rv); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if ((pMechanism == NULL) || (phKey == NULL)) { 517c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 527c478bd9Sstevel@tonic-gate goto clean_exit; 537c478bd9Sstevel@tonic-gate } 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate if ((pTemplate == NULL) && (ulCount != 0)) { 567c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 577c478bd9Sstevel@tonic-gate goto clean_exit; 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate rv = soft_genkey(session_p, pMechanism, pTemplate, 617c478bd9Sstevel@tonic-gate ulCount, phKey); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate clean_exit: 647c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 657c478bd9Sstevel@tonic-gate return (rv); 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate CK_RV 717c478bd9Sstevel@tonic-gate C_GenerateKeyPair(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 727c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, 737c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, 747c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate CK_RV rv; 787c478bd9Sstevel@tonic-gate soft_session_t *session_p; 797c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 827c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* Obtain the session pointer. */ 857c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 867c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 877c478bd9Sstevel@tonic-gate return (rv); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if ((pMechanism == NULL) || (phPublicKey == NULL) || 907c478bd9Sstevel@tonic-gate (phPrivateKey == NULL)) { 917c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 927c478bd9Sstevel@tonic-gate goto clean_exit; 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if ((pPublicKeyTemplate == NULL) || 967c478bd9Sstevel@tonic-gate (ulPublicKeyAttributeCount == 0)) { 977c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 987c478bd9Sstevel@tonic-gate goto clean_exit; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if ((pPrivateKeyTemplate == NULL) && 1027c478bd9Sstevel@tonic-gate (ulPrivateKeyAttributeCount != 0)) { 1037c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 1047c478bd9Sstevel@tonic-gate goto clean_exit; 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate rv = soft_genkey_pair(session_p, pMechanism, pPublicKeyTemplate, 1087c478bd9Sstevel@tonic-gate ulPublicKeyAttributeCount, pPrivateKeyTemplate, 1097c478bd9Sstevel@tonic-gate ulPrivateKeyAttributeCount, phPublicKey, phPrivateKey); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate clean_exit: 1127c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 1137c478bd9Sstevel@tonic-gate return (rv); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate CK_RV 1177c478bd9Sstevel@tonic-gate C_WrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 1187c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey, 1197c478bd9Sstevel@tonic-gate CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate CK_RV rv; 1227c478bd9Sstevel@tonic-gate soft_session_t *session_p; 1237c478bd9Sstevel@tonic-gate soft_object_t *wrappingkey_p; 1247c478bd9Sstevel@tonic-gate soft_object_t *hkey_p; 1257c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 1287c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* Obtain the session pointer. */ 1317c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 1327c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 1337c478bd9Sstevel@tonic-gate return (rv); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate if (pMechanism == NULL) { 1367c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 1377c478bd9Sstevel@tonic-gate goto clean_exit; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (pulWrappedKeyLen == NULL) { 1417c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 1427c478bd9Sstevel@tonic-gate goto clean_exit; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* Obtain the wrapping key object pointer. */ 1467c478bd9Sstevel@tonic-gate HANDLE2OBJECT(hWrappingKey, wrappingkey_p, rv); 1477c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 1487c478bd9Sstevel@tonic-gate rv = CKR_WRAPPING_KEY_HANDLE_INVALID; 1497c478bd9Sstevel@tonic-gate goto clean_exit; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* Obtain the to-be-wrapped key object pointer. */ 1537c478bd9Sstevel@tonic-gate HANDLE2OBJECT(hKey, hkey_p, rv); 1547c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 1557c478bd9Sstevel@tonic-gate goto clean_exit1; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* Check if given wrapping key may be used for wrapping. */ 1587c478bd9Sstevel@tonic-gate if (!(wrappingkey_p->bool_attr_mask & WRAP_BOOL_ON)) { 1597c478bd9Sstevel@tonic-gate rv = CKR_WRAPPING_KEY_TYPE_INCONSISTENT; 1607c478bd9Sstevel@tonic-gate goto clean_exit2; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* Check if given wrapping key may be used for encryption. */ 1647c478bd9Sstevel@tonic-gate if (!(wrappingkey_p->bool_attr_mask & ENCRYPT_BOOL_ON)) { 165*d288ba74SAnthony Scarpino rv = CKR_KEY_FUNCTION_NOT_PERMITTED; 1667c478bd9Sstevel@tonic-gate goto clean_exit2; 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Check to see if key to be wrapped is extractable. 1717c478bd9Sstevel@tonic-gate * Note: this should always be true for softtoken keys. 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate if (!(hkey_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) { 1747c478bd9Sstevel@tonic-gate rv = CKR_KEY_UNEXTRACTABLE; 1757c478bd9Sstevel@tonic-gate goto clean_exit2; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex); 1797c478bd9Sstevel@tonic-gate lock_held = B_TRUE; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * Wrapping key objects requires calling encrypt operations. 1837c478bd9Sstevel@tonic-gate * Check to see if encrypt operation is already active. 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate if (session_p->encrypt.flags & CRYPTO_OPERATION_ACTIVE) { 1867c478bd9Sstevel@tonic-gate /* free the memory to avoid memory leak */ 1877c478bd9Sstevel@tonic-gate soft_crypt_cleanup(session_p, B_TRUE, lock_held); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* This active flag will remain ON while wrapping the key. */ 1917c478bd9Sstevel@tonic-gate session_p->encrypt.flags = CRYPTO_OPERATION_ACTIVE; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex); 1947c478bd9Sstevel@tonic-gate lock_held = B_FALSE; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate rv = soft_wrapkey(session_p, pMechanism, wrappingkey_p, 1977c478bd9Sstevel@tonic-gate hkey_p, pWrappedKey, pulWrappedKeyLen); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex); 2004c21f043Sizick 2017c478bd9Sstevel@tonic-gate lock_held = B_TRUE; 2024c21f043Sizick session_p->encrypt.flags = 0; 2034c21f043Sizick 2044c21f043Sizick if ((rv == CKR_OK && pWrappedKey == NULL) || 2054c21f043Sizick rv == CKR_BUFFER_TOO_SMALL) 2064c21f043Sizick soft_crypt_cleanup(session_p, B_TRUE, lock_held); 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate clean_exit2: 2097c478bd9Sstevel@tonic-gate OBJ_REFRELE(hkey_p); 2107c478bd9Sstevel@tonic-gate clean_exit1: 2117c478bd9Sstevel@tonic-gate OBJ_REFRELE(wrappingkey_p); 2127c478bd9Sstevel@tonic-gate clean_exit: 2137c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 2147c478bd9Sstevel@tonic-gate return (rv); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate CK_RV 2187c478bd9Sstevel@tonic-gate C_UnwrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 2197c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey, 2207c478bd9Sstevel@tonic-gate CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate, 2217c478bd9Sstevel@tonic-gate CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate CK_RV rv; 2247c478bd9Sstevel@tonic-gate soft_session_t *session_p; 2257c478bd9Sstevel@tonic-gate soft_object_t *unwrappingkey_p; 2267c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 2297c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* Obtain the session pointer. */ 2327c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 2337c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 2347c478bd9Sstevel@tonic-gate return (rv); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (pMechanism == NULL) { 2377c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 2387c478bd9Sstevel@tonic-gate goto clean_exit; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate if ((pTemplate == NULL) || (ulAttributeCount == 0)) { 2427c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 2437c478bd9Sstevel@tonic-gate goto clean_exit; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate if ((pWrappedKey == NULL) || (ulWrappedKeyLen == 0)) { 2477c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 2487c478bd9Sstevel@tonic-gate goto clean_exit; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (phKey == NULL) { 2527c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 2537c478bd9Sstevel@tonic-gate goto clean_exit; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* Obtain the unwrapping key object pointer. */ 2577c478bd9Sstevel@tonic-gate HANDLE2OBJECT(hUnwrappingKey, unwrappingkey_p, rv); 2587c478bd9Sstevel@tonic-gate if (rv != CKR_OK) { 2597c478bd9Sstevel@tonic-gate rv = CKR_UNWRAPPING_KEY_HANDLE_INVALID; 2607c478bd9Sstevel@tonic-gate goto clean_exit; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate /* Check if given unwrapping key may be used for unwrapping. */ 2647c478bd9Sstevel@tonic-gate if (!(unwrappingkey_p->bool_attr_mask & UNWRAP_BOOL_ON)) { 2657c478bd9Sstevel@tonic-gate rv = CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT; 2667c478bd9Sstevel@tonic-gate goto clean_exit1; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* Check if given unwrapping key may be used to decrypt. */ 2707c478bd9Sstevel@tonic-gate if (!(unwrappingkey_p->bool_attr_mask & DECRYPT_BOOL_ON)) { 271*d288ba74SAnthony Scarpino rv = CKR_KEY_FUNCTION_NOT_PERMITTED; 2727c478bd9Sstevel@tonic-gate goto clean_exit1; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex); 2767c478bd9Sstevel@tonic-gate lock_held = B_TRUE; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * Unwrapping key objects requires calling decrypt operations. 2807c478bd9Sstevel@tonic-gate * Check to see if decrypt operation is already active. 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate if (session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE) { 2837c478bd9Sstevel@tonic-gate /* free the memory to avoid memory leak */ 2847c478bd9Sstevel@tonic-gate soft_crypt_cleanup(session_p, B_FALSE, lock_held); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * This active flag will remain ON until application 2897c478bd9Sstevel@tonic-gate * is done unwrapping the key. 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate session_p->decrypt.flags = CRYPTO_OPERATION_ACTIVE; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex); 2947c478bd9Sstevel@tonic-gate lock_held = B_FALSE; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate rv = soft_unwrapkey(session_p, pMechanism, unwrappingkey_p, 2977c478bd9Sstevel@tonic-gate pWrappedKey, ulWrappedKeyLen, pTemplate, ulAttributeCount, 2987c478bd9Sstevel@tonic-gate phKey); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex); 3014c21f043Sizick 3024c21f043Sizick if ((rv == CKR_OK && pWrappedKey == NULL) || 3034c21f043Sizick rv == CKR_BUFFER_TOO_SMALL) 3044c21f043Sizick soft_crypt_cleanup(session_p, B_TRUE, lock_held); 3054c21f043Sizick 3067c478bd9Sstevel@tonic-gate session_p->decrypt.flags = 0; 3077c478bd9Sstevel@tonic-gate lock_held = B_TRUE; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate clean_exit1: 3107c478bd9Sstevel@tonic-gate OBJ_REFRELE(unwrappingkey_p); 3117c478bd9Sstevel@tonic-gate clean_exit: 3127c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 3137c478bd9Sstevel@tonic-gate return (rv); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate CK_RV 3187c478bd9Sstevel@tonic-gate C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 3197c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, 3207c478bd9Sstevel@tonic-gate CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate CK_RV rv; 3247c478bd9Sstevel@tonic-gate soft_session_t *session_p; 3257c478bd9Sstevel@tonic-gate soft_object_t *basekey_p; 3267c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 3297c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate /* Obtain the session pointer. */ 3327c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 3337c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 3347c478bd9Sstevel@tonic-gate return (rv); 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate if (pMechanism == NULL) { 3377c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 3387c478bd9Sstevel@tonic-gate goto clean_exit; 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate if (((pTemplate != NULL) && (ulAttributeCount == 0)) || 3427c478bd9Sstevel@tonic-gate ((pTemplate == NULL) && (ulAttributeCount != 0))) { 3437c478bd9Sstevel@tonic-gate rv = CKR_ARGUMENTS_BAD; 3447c478bd9Sstevel@tonic-gate goto clean_exit; 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* Obtain the private key object pointer. */ 3487c478bd9Sstevel@tonic-gate HANDLE2OBJECT(hBaseKey, basekey_p, rv); 3497c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 3507c478bd9Sstevel@tonic-gate goto clean_exit; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /* Check to see if key object allows for derivation. */ 3537c478bd9Sstevel@tonic-gate if (!(basekey_p->bool_attr_mask & DERIVE_BOOL_ON)) { 354*d288ba74SAnthony Scarpino rv = CKR_KEY_FUNCTION_NOT_PERMITTED; 3557c478bd9Sstevel@tonic-gate goto clean_exit1; 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate rv = soft_derivekey(session_p, pMechanism, basekey_p, 3597c478bd9Sstevel@tonic-gate pTemplate, ulAttributeCount, phKey); 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate clean_exit1: 3627c478bd9Sstevel@tonic-gate OBJ_REFRELE(basekey_p); 3637c478bd9Sstevel@tonic-gate clean_exit: 3647c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 3657c478bd9Sstevel@tonic-gate return (rv); 3667c478bd9Sstevel@tonic-gate } 367