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.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <pthread.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
3223c57df7Smcpowers #include <sys/crypto/common.h>
337c478bd9Sstevel@tonic-gate #include <aes_impl.h>
34f66d273dSizick #include <blowfish_impl.h>
357c478bd9Sstevel@tonic-gate #include <des_impl.h>
367c478bd9Sstevel@tonic-gate #include <arcfour.h>
377b79d846SDina K Nimeh #include <cryptoutil.h>
387c478bd9Sstevel@tonic-gate #include "softGlobal.h"
397c478bd9Sstevel@tonic-gate #include "softSession.h"
407c478bd9Sstevel@tonic-gate #include "softObject.h"
417c478bd9Sstevel@tonic-gate #include "softDSA.h"
427c478bd9Sstevel@tonic-gate #include "softRSA.h"
437c478bd9Sstevel@tonic-gate #include "softDH.h"
44f9fbec18Smcpowers #include "softEC.h"
457c478bd9Sstevel@tonic-gate #include "softMAC.h"
467c478bd9Sstevel@tonic-gate #include "softOps.h"
477c478bd9Sstevel@tonic-gate #include "softKeys.h"
487c478bd9Sstevel@tonic-gate #include "softKeystore.h"
497c478bd9Sstevel@tonic-gate #include "softSSL.h"
507c478bd9Sstevel@tonic-gate #include "softASN1.h"
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define local_min(a, b) ((a) < (b) ? (a) : (b))
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static CK_RV
567c478bd9Sstevel@tonic-gate soft_pkcs12_pbe(soft_session_t *, CK_MECHANISM_PTR, soft_object_t *);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * Create a temporary key object struct by filling up its template attributes.
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate CK_RV
soft_gen_keyobject(CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,CK_ULONG * objecthandle_p,soft_session_t * sp,CK_OBJECT_CLASS class,CK_KEY_TYPE key_type,CK_ULONG keylen,CK_ULONG mode,boolean_t internal)627c478bd9Sstevel@tonic-gate soft_gen_keyobject(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
637c478bd9Sstevel@tonic-gate CK_ULONG *objecthandle_p, soft_session_t *sp,
647c478bd9Sstevel@tonic-gate CK_OBJECT_CLASS class, CK_KEY_TYPE key_type, CK_ULONG keylen, CK_ULONG mode,
657c478bd9Sstevel@tonic-gate boolean_t internal)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate CK_RV rv;
697c478bd9Sstevel@tonic-gate soft_object_t *new_objp = NULL;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate new_objp = calloc(1, sizeof (soft_object_t));
727c478bd9Sstevel@tonic-gate if (new_objp == NULL) {
737c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate new_objp->extra_attrlistp = NULL;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate * Validate attribute template and fill in the attributes
807c478bd9Sstevel@tonic-gate * in the soft_object_t.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate rv = soft_build_key(pTemplate, ulCount, new_objp, class, key_type,
837c478bd9Sstevel@tonic-gate keylen, mode);
847c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
857c478bd9Sstevel@tonic-gate goto fail_cleanup1;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * If generating a key is an internal request (i.e. not a C_XXX
907c478bd9Sstevel@tonic-gate * API request), then skip the following checks.
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate if (!internal) {
937c478bd9Sstevel@tonic-gate rv = soft_pin_expired_check(new_objp);
947c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
957c478bd9Sstevel@tonic-gate goto fail_cleanup2;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate rv = soft_object_write_access_check(sp, new_objp);
997c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
1007c478bd9Sstevel@tonic-gate goto fail_cleanup2;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /* Initialize the rest of stuffs in soft_object_t. */
1057c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&new_objp->object_mutex, NULL);
1067c478bd9Sstevel@tonic-gate new_objp->magic_marker = SOFTTOKEN_OBJECT_MAGIC;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /* Write the new token object to the keystore */
1097c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(new_objp)) {
1107c478bd9Sstevel@tonic-gate new_objp->version = 1;
1117c478bd9Sstevel@tonic-gate new_objp->session_handle = (CK_SESSION_HANDLE)NULL;
1127c478bd9Sstevel@tonic-gate soft_add_token_object_to_slot(new_objp);
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * Type casting the address of an object struct to
1157c478bd9Sstevel@tonic-gate * an object handle.
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate *objecthandle_p = (CK_ULONG)new_objp;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate return (CKR_OK);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate new_objp->session_handle = (CK_SESSION_HANDLE)sp;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* Add the new object to the session's object list. */
1257c478bd9Sstevel@tonic-gate soft_add_object_to_session(new_objp, sp);
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /* Type casting the address of an object struct to an object handle. */
1287c478bd9Sstevel@tonic-gate *objecthandle_p = (CK_ULONG)new_objp;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate return (CKR_OK);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate fail_cleanup2:
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * When any error occurs after soft_build_key(), we will need to
1357c478bd9Sstevel@tonic-gate * clean up the memory allocated by the soft_build_key().
1367c478bd9Sstevel@tonic-gate */
1377c478bd9Sstevel@tonic-gate soft_cleanup_object(new_objp);
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate fail_cleanup1:
1407c478bd9Sstevel@tonic-gate if (new_objp) {
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * The storage allocated inside of this object should have
1437c478bd9Sstevel@tonic-gate * been cleaned up by the soft_build_key() if it failed.
1447c478bd9Sstevel@tonic-gate * Therefore, we can safely free the object.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate free(new_objp);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate return (rv);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@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)1537c478bd9Sstevel@tonic-gate soft_genkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
1547c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
1587c478bd9Sstevel@tonic-gate soft_object_t *secret_key;
1597c478bd9Sstevel@tonic-gate CK_KEY_TYPE key_type;
1607c478bd9Sstevel@tonic-gate CK_ULONG keylen = 0;
1617c478bd9Sstevel@tonic-gate CK_ULONG i;
1627c478bd9Sstevel@tonic-gate int des_strength = 0;
1637c478bd9Sstevel@tonic-gate int retry = 0;
1647c478bd9Sstevel@tonic-gate int keyfound = 0;
1657c478bd9Sstevel@tonic-gate boolean_t is_ssl_mech = B_FALSE;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
1687c478bd9Sstevel@tonic-gate case CKM_DES_KEY_GEN:
1697c478bd9Sstevel@tonic-gate key_type = CKK_DES;
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate
172436935a1SVladimir Kotal case CKM_DES2_KEY_GEN:
173436935a1SVladimir Kotal key_type = CKK_DES2;
174436935a1SVladimir Kotal break;
175436935a1SVladimir Kotal
1767c478bd9Sstevel@tonic-gate case CKM_DES3_KEY_GEN:
1777c478bd9Sstevel@tonic-gate key_type = CKK_DES3;
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate case CKM_AES_KEY_GEN:
1817c478bd9Sstevel@tonic-gate key_type = CKK_AES;
1827c478bd9Sstevel@tonic-gate break;
1837c478bd9Sstevel@tonic-gate
184f66d273dSizick case CKM_BLOWFISH_KEY_GEN:
185f66d273dSizick key_type = CKK_BLOWFISH;
186f66d273dSizick break;
187f66d273dSizick
1887c478bd9Sstevel@tonic-gate case CKM_RC4_KEY_GEN:
1897c478bd9Sstevel@tonic-gate key_type = CKK_RC4;
1907c478bd9Sstevel@tonic-gate break;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate case CKM_SSL3_PRE_MASTER_KEY_GEN:
1937c478bd9Sstevel@tonic-gate case CKM_TLS_PRE_MASTER_KEY_GEN:
1947c478bd9Sstevel@tonic-gate if (pMechanism->pParameter == NULL ||
1957c478bd9Sstevel@tonic-gate pMechanism->ulParameterLen != sizeof (CK_VERSION))
1967c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
1977c478bd9Sstevel@tonic-gate is_ssl_mech = B_TRUE;
1987c478bd9Sstevel@tonic-gate key_type = CKK_GENERIC_SECRET;
1997c478bd9Sstevel@tonic-gate keylen = 48;
2007c478bd9Sstevel@tonic-gate break;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate case CKM_PKCS5_PBKD2:
2037c478bd9Sstevel@tonic-gate keyfound = 0;
2047c478bd9Sstevel@tonic-gate for (i = 0; i < ulCount && !keyfound; i++) {
2057c478bd9Sstevel@tonic-gate if (pTemplate[i].type == CKA_KEY_TYPE &&
2067c478bd9Sstevel@tonic-gate pTemplate[i].pValue != NULL) {
2077c478bd9Sstevel@tonic-gate key_type = *((CK_KEY_TYPE*)pTemplate[i].pValue);
2087c478bd9Sstevel@tonic-gate keyfound = 1;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate if (!keyfound)
2127c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * Make sure that parameters were given for this
2157c478bd9Sstevel@tonic-gate * mechanism.
2167c478bd9Sstevel@tonic-gate */
2177c478bd9Sstevel@tonic-gate if (pMechanism->pParameter == NULL ||
2187c478bd9Sstevel@tonic-gate pMechanism->ulParameterLen !=
2197c478bd9Sstevel@tonic-gate sizeof (CK_PKCS5_PBKD2_PARAMS))
2207c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
2217c478bd9Sstevel@tonic-gate break;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate case CKM_PBE_SHA1_RC4_128:
2247c478bd9Sstevel@tonic-gate keyfound = 0;
2257c478bd9Sstevel@tonic-gate for (i = 0; i < ulCount; i++) {
2267c478bd9Sstevel@tonic-gate if (pTemplate[i].type == CKA_KEY_TYPE &&
2277c478bd9Sstevel@tonic-gate pTemplate[i].pValue != NULL) {
2287c478bd9Sstevel@tonic-gate key_type = *((CK_KEY_TYPE*)pTemplate[i].pValue);
2297c478bd9Sstevel@tonic-gate keyfound = 1;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate if (pTemplate[i].type == CKA_VALUE_LEN &&
2327c478bd9Sstevel@tonic-gate pTemplate[i].pValue != NULL) {
2337c478bd9Sstevel@tonic-gate keylen = *((CK_ULONG*)pTemplate[i].pValue);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate /* If a keytype was specified, it had better be CKK_RC4 */
2377c478bd9Sstevel@tonic-gate if (keyfound && key_type != CKK_RC4)
2387c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCONSISTENT);
2397c478bd9Sstevel@tonic-gate else if (!keyfound)
2407c478bd9Sstevel@tonic-gate key_type = CKK_RC4;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /* If key length was specified, it better be 16 bytes */
2437c478bd9Sstevel@tonic-gate if (keylen != 0 && keylen != 16)
2447c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCONSISTENT);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate * Make sure that parameters were given for this
2487c478bd9Sstevel@tonic-gate * mechanism.
2497c478bd9Sstevel@tonic-gate */
2507c478bd9Sstevel@tonic-gate if (pMechanism->pParameter == NULL ||
2517c478bd9Sstevel@tonic-gate pMechanism->ulParameterLen !=
2527c478bd9Sstevel@tonic-gate sizeof (CK_PBE_PARAMS))
2537c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
2547c478bd9Sstevel@tonic-gate break;
2557c478bd9Sstevel@tonic-gate default:
2567c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /* Create a new object for secret key. */
2607c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pTemplate, ulCount, phKey, session_p,
2617c478bd9Sstevel@tonic-gate CKO_SECRET_KEY, key_type, keylen, SOFT_GEN_KEY, B_FALSE);
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
2647c478bd9Sstevel@tonic-gate return (rv);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /* Obtain the secret object pointer. */
2687c478bd9Sstevel@tonic-gate secret_key = (soft_object_t *)*phKey;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
2717c478bd9Sstevel@tonic-gate case CKM_DES_KEY_GEN:
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate * Set up key value len since it is not a required
2747c478bd9Sstevel@tonic-gate * attribute for C_GenerateKey.
2757c478bd9Sstevel@tonic-gate */
2767c478bd9Sstevel@tonic-gate keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
2777c478bd9Sstevel@tonic-gate des_strength = DES;
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate
280436935a1SVladimir Kotal case CKM_DES2_KEY_GEN:
281436935a1SVladimir Kotal /*
282436935a1SVladimir Kotal * Set up key value len since it is not a required
283436935a1SVladimir Kotal * attribute for C_GenerateKey.
284436935a1SVladimir Kotal */
285436935a1SVladimir Kotal keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES2_KEYSIZE;
286436935a1SVladimir Kotal des_strength = DES2;
287436935a1SVladimir Kotal break;
288436935a1SVladimir Kotal
2897c478bd9Sstevel@tonic-gate case CKM_DES3_KEY_GEN:
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate * Set up key value len since it is not a required
2927c478bd9Sstevel@tonic-gate * attribute for C_GenerateKey.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate keylen = OBJ_SEC_VALUE_LEN(secret_key) = DES3_KEYSIZE;
2957c478bd9Sstevel@tonic-gate des_strength = DES3;
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate case CKM_SSL3_PRE_MASTER_KEY_GEN:
2997c478bd9Sstevel@tonic-gate case CKM_TLS_PRE_MASTER_KEY_GEN:
3007c478bd9Sstevel@tonic-gate secret_key->bool_attr_mask |= DERIVE_BOOL_ON;
3017c478bd9Sstevel@tonic-gate /* FALLTHRU */
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate case CKM_AES_KEY_GEN:
304f66d273dSizick case CKM_BLOWFISH_KEY_GEN:
3057c478bd9Sstevel@tonic-gate case CKM_PBE_SHA1_RC4_128:
3067c478bd9Sstevel@tonic-gate case CKM_RC4_KEY_GEN:
3077c478bd9Sstevel@tonic-gate keylen = OBJ_SEC_VALUE_LEN(secret_key);
3087c478bd9Sstevel@tonic-gate break;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate case CKM_PKCS5_PBKD2:
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate * PKCS#11 does not allow one to specify key
3137c478bd9Sstevel@tonic-gate * sizes for DES and 3DES, so we must set it here
3147c478bd9Sstevel@tonic-gate * when using PBKD2 algorithms.
3157c478bd9Sstevel@tonic-gate */
3167c478bd9Sstevel@tonic-gate if (key_type == CKK_DES) {
3177c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
3187c478bd9Sstevel@tonic-gate des_strength = DES;
3197c478bd9Sstevel@tonic-gate } else if (key_type == CKK_DES3) {
3207c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(secret_key) = DES3_KEYSIZE;
3217c478bd9Sstevel@tonic-gate des_strength = DES3;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate keylen = OBJ_SEC_VALUE_LEN(secret_key);
3257c478bd9Sstevel@tonic-gate break;
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate if ((OBJ_SEC_VALUE(secret_key) = malloc(keylen)) == NULL) {
3297c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
3307c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
3317c478bd9Sstevel@tonic-gate else
3321f49a79aSZdenek Kotala soft_delete_object(session_p, secret_key,
3331f49a79aSZdenek Kotala B_FALSE, B_FALSE);
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
3387c478bd9Sstevel@tonic-gate case CKM_PBE_SHA1_RC4_128:
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate * Use the PBE algorithm described in PKCS#11 section
3417c478bd9Sstevel@tonic-gate * 12.33 to derive the key.
3427c478bd9Sstevel@tonic-gate */
3437c478bd9Sstevel@tonic-gate rv = soft_pkcs12_pbe(session_p, pMechanism, secret_key);
3447c478bd9Sstevel@tonic-gate break;
3457c478bd9Sstevel@tonic-gate case CKM_PKCS5_PBKD2:
3467c478bd9Sstevel@tonic-gate /* Generate keys using PKCS#5 PBKD2 algorithm */
3477c478bd9Sstevel@tonic-gate rv = soft_generate_pkcs5_pbkdf2_key(session_p, pMechanism,
3487c478bd9Sstevel@tonic-gate secret_key);
3497c478bd9Sstevel@tonic-gate if (rv == CKR_OK && des_strength > 0) {
3507c478bd9Sstevel@tonic-gate /* Perform weak key checking for DES and DES3. */
3517c478bd9Sstevel@tonic-gate if (des_keycheck(OBJ_SEC_VALUE(secret_key),
3527c478bd9Sstevel@tonic-gate des_strength, OBJ_SEC_VALUE(secret_key)) ==
3537c478bd9Sstevel@tonic-gate B_FALSE) {
3547c478bd9Sstevel@tonic-gate /* We got a weak secret key. */
3557c478bd9Sstevel@tonic-gate rv = CKR_FUNCTION_FAILED;
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate break;
3597c478bd9Sstevel@tonic-gate default:
3607c478bd9Sstevel@tonic-gate do {
3617c478bd9Sstevel@tonic-gate /* If this fails, bail out */
3627b79d846SDina K Nimeh rv = CKR_OK;
3637b79d846SDina K Nimeh if (pkcs11_get_urandom(
3647b79d846SDina K Nimeh OBJ_SEC_VALUE(secret_key), keylen) < 0) {
3657b79d846SDina K Nimeh rv = CKR_DEVICE_ERROR;
3667c478bd9Sstevel@tonic-gate break;
3677b79d846SDina K Nimeh }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate /* Perform weak key checking for DES and DES3. */
3707c478bd9Sstevel@tonic-gate if (des_strength > 0) {
3717c478bd9Sstevel@tonic-gate rv = CKR_OK;
3727c478bd9Sstevel@tonic-gate if (des_keycheck(OBJ_SEC_VALUE(secret_key),
3737c478bd9Sstevel@tonic-gate des_strength, OBJ_SEC_VALUE(secret_key)) ==
3747c478bd9Sstevel@tonic-gate B_FALSE) {
3757c478bd9Sstevel@tonic-gate /* We got a weak key, retry! */
3767c478bd9Sstevel@tonic-gate retry++;
3777c478bd9Sstevel@tonic-gate rv = CKR_FUNCTION_FAILED;
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * Copy over the SSL client version For SSL mechs
3827c478bd9Sstevel@tonic-gate * The first two bytes of the key is the version
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate if (is_ssl_mech)
3857c478bd9Sstevel@tonic-gate bcopy(pMechanism->pParameter,
3867c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE(secret_key),
3877c478bd9Sstevel@tonic-gate sizeof (CK_VERSION));
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate } while (rv != CKR_OK && retry < KEYGEN_RETRY);
3907c478bd9Sstevel@tonic-gate if (retry == KEYGEN_RETRY)
3917c478bd9Sstevel@tonic-gate rv = CKR_FUNCTION_FAILED;
3927c478bd9Sstevel@tonic-gate break;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
3967c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
3977c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
3987c478bd9Sstevel@tonic-gate else
3991f49a79aSZdenek Kotala soft_delete_object(session_p, secret_key,
4001f49a79aSZdenek Kotala B_FALSE, B_FALSE);
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key)) {
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate * All the info has been filled, so we can write to
4057c478bd9Sstevel@tonic-gate * keystore now.
4067c478bd9Sstevel@tonic-gate */
4077c478bd9Sstevel@tonic-gate rv = soft_put_object_to_keystore(secret_key);
4087c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
4097c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate return (rv);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@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)4167c478bd9Sstevel@tonic-gate soft_genkey_pair(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
4177c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicAttrCount,
4187c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateAttrCount,
4197c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate CK_RV rv;
4237c478bd9Sstevel@tonic-gate soft_object_t *public_key, *private_key;
4247c478bd9Sstevel@tonic-gate CK_KEY_TYPE key_type;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate case CKM_RSA_PKCS_KEY_PAIR_GEN:
4297c478bd9Sstevel@tonic-gate key_type = CKK_RSA;
4307c478bd9Sstevel@tonic-gate break;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate case CKM_DSA_KEY_PAIR_GEN:
4337c478bd9Sstevel@tonic-gate key_type = CKK_DSA;
4347c478bd9Sstevel@tonic-gate break;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate case CKM_DH_PKCS_KEY_PAIR_GEN:
4377c478bd9Sstevel@tonic-gate key_type = CKK_DH;
4387c478bd9Sstevel@tonic-gate break;
4397c478bd9Sstevel@tonic-gate
440034448feSmcpowers case CKM_EC_KEY_PAIR_GEN:
441034448feSmcpowers key_type = CKK_EC;
442f9fbec18Smcpowers break;
443034448feSmcpowers
4447c478bd9Sstevel@tonic-gate default:
4457c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate /* Create a new object for public key. */
4497c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pPublicKeyTemplate, ulPublicAttrCount,
4507c478bd9Sstevel@tonic-gate phPublicKey, session_p, CKO_PUBLIC_KEY, key_type, 0,
4517c478bd9Sstevel@tonic-gate SOFT_GEN_KEY, B_FALSE);
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
4547c478bd9Sstevel@tonic-gate return (rv);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /* Obtain the public object pointer. */
4587c478bd9Sstevel@tonic-gate public_key = (soft_object_t *)*phPublicKey;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /* Create a new object for private key. */
4617c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pPrivateKeyTemplate, ulPrivateAttrCount,
4627c478bd9Sstevel@tonic-gate phPrivateKey, session_p, CKO_PRIVATE_KEY, key_type, 0,
4637c478bd9Sstevel@tonic-gate SOFT_GEN_KEY, B_FALSE);
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * Both public key and private key must be successful.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(public_key))
4707c478bd9Sstevel@tonic-gate soft_delete_token_object(public_key, B_FALSE, B_FALSE);
4717c478bd9Sstevel@tonic-gate else
4721f49a79aSZdenek Kotala soft_delete_object(session_p, public_key,
4731f49a79aSZdenek Kotala B_FALSE, B_FALSE);
4747c478bd9Sstevel@tonic-gate return (rv);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate /* Obtain the private object pointer. */
4787c478bd9Sstevel@tonic-gate private_key = (soft_object_t *)*phPrivateKey;
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate * At this point, both public key and private key objects
4827c478bd9Sstevel@tonic-gate * are settled with the application specified attributes.
4837c478bd9Sstevel@tonic-gate * We are ready to generate the rest of key attributes based
4847c478bd9Sstevel@tonic-gate * on the existing attributes.
4857c478bd9Sstevel@tonic-gate */
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate switch (key_type) {
4887c478bd9Sstevel@tonic-gate case CKK_RSA:
4897c478bd9Sstevel@tonic-gate rv = soft_rsa_genkey_pair(public_key, private_key);
4907c478bd9Sstevel@tonic-gate break;
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate case CKK_DSA:
4937c478bd9Sstevel@tonic-gate rv = soft_dsa_genkey_pair(public_key, private_key);
4947c478bd9Sstevel@tonic-gate break;
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate case CKK_DH:
4977c478bd9Sstevel@tonic-gate rv = soft_dh_genkey_pair(public_key, private_key);
4987c478bd9Sstevel@tonic-gate private_key->bool_attr_mask |= DERIVE_BOOL_ON;
4997c478bd9Sstevel@tonic-gate break;
500f9fbec18Smcpowers case CKK_EC:
501f9fbec18Smcpowers rv = soft_ec_genkey_pair(public_key, private_key);
502f9fbec18Smcpowers private_key->bool_attr_mask |= DERIVE_BOOL_ON;
503f9fbec18Smcpowers break;
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
5077c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(public_key)) {
5087c478bd9Sstevel@tonic-gate soft_delete_token_object(public_key, B_FALSE, B_FALSE);
5097c478bd9Sstevel@tonic-gate soft_delete_token_object(private_key, B_FALSE, B_FALSE);
5107c478bd9Sstevel@tonic-gate } else {
5111f49a79aSZdenek Kotala soft_delete_object(session_p, public_key,
5121f49a79aSZdenek Kotala B_FALSE, B_FALSE);
5131f49a79aSZdenek Kotala soft_delete_object(session_p, private_key,
5141f49a79aSZdenek Kotala B_FALSE, B_FALSE);
5157c478bd9Sstevel@tonic-gate }
516*b5a2d845SHai-May Chao return (rv);
517*b5a2d845SHai-May Chao }
518*b5a2d845SHai-May Chao
5197c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(public_key)) {
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate * All the info has been filled, so we can write to
5227c478bd9Sstevel@tonic-gate * keystore now.
5237c478bd9Sstevel@tonic-gate */
5247c478bd9Sstevel@tonic-gate rv = soft_put_object_to_keystore(public_key);
5257c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
5267c478bd9Sstevel@tonic-gate soft_delete_token_object(public_key, B_FALSE, B_FALSE);
5277c478bd9Sstevel@tonic-gate soft_delete_token_object(private_key, B_FALSE, B_FALSE);
528*b5a2d845SHai-May Chao return (rv);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(private_key)) {
5337c478bd9Sstevel@tonic-gate rv = soft_put_object_to_keystore(private_key);
5347c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate * We also need to delete the public token object
5377c478bd9Sstevel@tonic-gate * from keystore.
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate soft_delete_token_object(public_key, B_TRUE, B_FALSE);
5407c478bd9Sstevel@tonic-gate soft_delete_token_object(private_key, B_FALSE, B_FALSE);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate return (rv);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate CK_RV
soft_key_derive_check_length(soft_object_t * secret_key,CK_ULONG max_keylen)5497c478bd9Sstevel@tonic-gate soft_key_derive_check_length(soft_object_t *secret_key, CK_ULONG max_keylen)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate switch (secret_key->key_type) {
5537c478bd9Sstevel@tonic-gate case CKK_GENERIC_SECRET:
5547c478bd9Sstevel@tonic-gate if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5557c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(secret_key) = max_keylen;
5567c478bd9Sstevel@tonic-gate return (CKR_OK);
5577c478bd9Sstevel@tonic-gate } else if (OBJ_SEC_VALUE_LEN(secret_key) > max_keylen) {
5587c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate break;
5617c478bd9Sstevel@tonic-gate case CKK_RC4:
5627c478bd9Sstevel@tonic-gate case CKK_AES:
563f66d273dSizick case CKK_BLOWFISH:
5647c478bd9Sstevel@tonic-gate if ((OBJ_SEC_VALUE_LEN(secret_key) == 0) ||
5657c478bd9Sstevel@tonic-gate (OBJ_SEC_VALUE_LEN(secret_key) > max_keylen)) {
5667c478bd9Sstevel@tonic-gate /* RC4 and AES has variable key length */
5677c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate break;
5707c478bd9Sstevel@tonic-gate case CKK_DES:
5717c478bd9Sstevel@tonic-gate if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5727c478bd9Sstevel@tonic-gate /* DES has a well-defined length */
5737c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(secret_key) = DES_KEYSIZE;
5747c478bd9Sstevel@tonic-gate return (CKR_OK);
5757c478bd9Sstevel@tonic-gate } else if (OBJ_SEC_VALUE_LEN(secret_key) != DES_KEYSIZE) {
5767c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate break;
5797c478bd9Sstevel@tonic-gate case CKK_DES2:
5807c478bd9Sstevel@tonic-gate if (OBJ_SEC_VALUE_LEN(secret_key) == 0) {
5817c478bd9Sstevel@tonic-gate /* DES2 has a well-defined length */
5827c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(secret_key) = DES2_KEYSIZE;
5837c478bd9Sstevel@tonic-gate return (CKR_OK);
5847c478bd9Sstevel@tonic-gate } else if (OBJ_SEC_VALUE_LEN(secret_key) != DES2_KEYSIZE) {
5857c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate break;
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate default:
5907c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate return (CKR_OK);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate * PKCS#11 (12.33) says that v = 512 bits (64 bytes) for SHA1
5987c478bd9Sstevel@tonic-gate * PBE methods.
5997c478bd9Sstevel@tonic-gate */
6007c478bd9Sstevel@tonic-gate #define PKCS12_BUFFER_SIZE 64
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate * PKCS#12 defines 3 different ID bytes to be used for
6037c478bd9Sstevel@tonic-gate * deriving keys for different operations.
6047c478bd9Sstevel@tonic-gate */
6057c478bd9Sstevel@tonic-gate #define PBE_ID_ENCRYPT 1
6067c478bd9Sstevel@tonic-gate #define PBE_ID_IV 2
6077c478bd9Sstevel@tonic-gate #define PBE_ID_MAC 3
6087c478bd9Sstevel@tonic-gate #define PBE_CEIL(a, b) (((a)/(b)) + (((a)%(b)) > 0))
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate static CK_RV
soft_pkcs12_pbe(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * derived_key)6117c478bd9Sstevel@tonic-gate soft_pkcs12_pbe(soft_session_t *session_p,
6127c478bd9Sstevel@tonic-gate CK_MECHANISM_PTR pMechanism,
6137c478bd9Sstevel@tonic-gate soft_object_t *derived_key)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
6167c478bd9Sstevel@tonic-gate CK_PBE_PARAMS *params = pMechanism->pParameter;
6177c478bd9Sstevel@tonic-gate CK_ULONG c, i, j, k;
6187c478bd9Sstevel@tonic-gate CK_ULONG hashSize;
6197c478bd9Sstevel@tonic-gate CK_ULONG buffSize;
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate * Terse variable names are used to make following
6227c478bd9Sstevel@tonic-gate * the PKCS#12 spec easier.
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate CK_BYTE *A = NULL;
6257c478bd9Sstevel@tonic-gate CK_BYTE *Ai = NULL;
6267c478bd9Sstevel@tonic-gate CK_BYTE *B = NULL;
6277c478bd9Sstevel@tonic-gate CK_BYTE *D = NULL;
6287c478bd9Sstevel@tonic-gate CK_BYTE *I = NULL, *S, *P;
6297c478bd9Sstevel@tonic-gate CK_BYTE *keybuf = NULL;
6307c478bd9Sstevel@tonic-gate CK_ULONG Alen, Ilen, Slen, Plen, AiLen, Blen, Dlen;
6317c478bd9Sstevel@tonic-gate CK_ULONG keysize = OBJ_SEC_VALUE_LEN(derived_key);
6327c478bd9Sstevel@tonic-gate CK_MECHANISM digest_mech;
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate /* U = hash function output bits */
6357c478bd9Sstevel@tonic-gate if (pMechanism->mechanism == CKM_PBE_SHA1_RC4_128) {
6367c478bd9Sstevel@tonic-gate hashSize = SHA1_HASH_SIZE;
6377c478bd9Sstevel@tonic-gate buffSize = PKCS12_BUFFER_SIZE;
6387c478bd9Sstevel@tonic-gate digest_mech.mechanism = CKM_SHA_1;
6397c478bd9Sstevel@tonic-gate digest_mech.pParameter = NULL;
6407c478bd9Sstevel@tonic-gate digest_mech.ulParameterLen = 0;
6417c478bd9Sstevel@tonic-gate } else {
6427c478bd9Sstevel@tonic-gate /* we only support 1 PBE mech for now */
6437c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate keybuf = OBJ_SEC_VALUE(derived_key);
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate Blen = Dlen = buffSize;
6487c478bd9Sstevel@tonic-gate D = (CK_BYTE *)malloc(Dlen);
6497c478bd9Sstevel@tonic-gate if (D == NULL) {
6507c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
6517c478bd9Sstevel@tonic-gate goto cleanup;
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate B = (CK_BYTE *)malloc(Blen);
6557c478bd9Sstevel@tonic-gate if (B == NULL) {
6567c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
6577c478bd9Sstevel@tonic-gate goto cleanup;
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate * Initialize some values and create some buffers
6627c478bd9Sstevel@tonic-gate * that we need later.
6637c478bd9Sstevel@tonic-gate *
6647c478bd9Sstevel@tonic-gate * Slen = buffSize * CEIL(SaltLength/buffSize)
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate Slen = buffSize * PBE_CEIL(params->ulSaltLen, buffSize);
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate * Plen = buffSize * CEIL(PasswordLength/buffSize)
6707c478bd9Sstevel@tonic-gate */
6717c478bd9Sstevel@tonic-gate Plen = buffSize * PBE_CEIL(params->ulPasswordLen, buffSize);
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate * From step 4: I = S + P, so: Ilen = Slen + Plen
6757c478bd9Sstevel@tonic-gate */
6767c478bd9Sstevel@tonic-gate Ilen = Slen + Plen;
6777c478bd9Sstevel@tonic-gate I = (CK_BYTE *)malloc(Ilen);
6787c478bd9Sstevel@tonic-gate if (I == NULL) {
6797c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
6807c478bd9Sstevel@tonic-gate goto cleanup;
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate S = I;
6847c478bd9Sstevel@tonic-gate P = I + Slen;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate * Step 1.
6887c478bd9Sstevel@tonic-gate * We are only interested in deriving keys for encrypt/decrypt
6897c478bd9Sstevel@tonic-gate * for now, so construct the "D"iversifier accordingly.
6907c478bd9Sstevel@tonic-gate */
6917c478bd9Sstevel@tonic-gate (void) memset(D, PBE_ID_ENCRYPT, Dlen);
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate * Step 2.
6957c478bd9Sstevel@tonic-gate * Concatenate copies of the salt together to make S.
6967c478bd9Sstevel@tonic-gate */
6977c478bd9Sstevel@tonic-gate for (i = 0; i < Slen; i += params->ulSaltLen) {
6987c478bd9Sstevel@tonic-gate (void) memcpy(S+i, params->pSalt,
6997c478bd9Sstevel@tonic-gate ((Slen - i) > params->ulSaltLen ?
7007c478bd9Sstevel@tonic-gate params->ulSaltLen : (Slen - i)));
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate * Step 3.
7057c478bd9Sstevel@tonic-gate * Concatenate copies of the password together to make
7067c478bd9Sstevel@tonic-gate * a string P.
7077c478bd9Sstevel@tonic-gate */
7087c478bd9Sstevel@tonic-gate for (i = 0; i < Plen; i += params->ulPasswordLen) {
7097c478bd9Sstevel@tonic-gate (void) memcpy(P+i, params->pPassword,
7107c478bd9Sstevel@tonic-gate ((Plen - i) > params->ulPasswordLen ?
7117c478bd9Sstevel@tonic-gate params->ulPasswordLen : (Plen - i)));
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate * Step 4.
7167c478bd9Sstevel@tonic-gate * I = S+P - this is now done because S and P are
7177c478bd9Sstevel@tonic-gate * pointers into I.
7187c478bd9Sstevel@tonic-gate *
7197c478bd9Sstevel@tonic-gate * Step 5.
7207c478bd9Sstevel@tonic-gate * c= CEIL[n/u]
7217c478bd9Sstevel@tonic-gate * where n = pseudorandom bits of output desired.
7227c478bd9Sstevel@tonic-gate */
7237c478bd9Sstevel@tonic-gate c = PBE_CEIL(keysize, hashSize);
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate /*
7267c478bd9Sstevel@tonic-gate * Step 6.
7277c478bd9Sstevel@tonic-gate */
7287c478bd9Sstevel@tonic-gate Alen = c * hashSize;
7297c478bd9Sstevel@tonic-gate A = (CK_BYTE *)malloc(Alen);
7307c478bd9Sstevel@tonic-gate if (A == NULL) {
7317c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
7327c478bd9Sstevel@tonic-gate goto cleanup;
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate AiLen = hashSize;
7357c478bd9Sstevel@tonic-gate Ai = (CK_BYTE *)malloc(AiLen);
7367c478bd9Sstevel@tonic-gate if (Ai == NULL) {
7377c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
7387c478bd9Sstevel@tonic-gate goto cleanup;
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate * Step 6a.
7437c478bd9Sstevel@tonic-gate * Ai = Hr(D+I)
7447c478bd9Sstevel@tonic-gate */
7457c478bd9Sstevel@tonic-gate for (i = 0; i < c; i++) {
7467c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
7497c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
7507c478bd9Sstevel@tonic-gate rv = CKR_OPERATION_ACTIVE;
7517c478bd9Sstevel@tonic-gate goto cleanup;
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate session_p->sign.flags |= CRYPTO_OPERATION_ACTIVE;
7547c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate for (j = 0; j < params->ulIteration; j++) {
7577c478bd9Sstevel@tonic-gate rv = soft_digest_init(session_p, &digest_mech);
7587c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
7597c478bd9Sstevel@tonic-gate goto digest_done;
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate if (j == 0) {
762f9fbec18Smcpowers rv = soft_digest_update(session_p, D, Dlen);
7637c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
7647c478bd9Sstevel@tonic-gate goto digest_done;
7657c478bd9Sstevel@tonic-gate
766f9fbec18Smcpowers rv = soft_digest_update(session_p, I, Ilen);
7677c478bd9Sstevel@tonic-gate } else {
768f9fbec18Smcpowers rv = soft_digest_update(session_p, Ai, AiLen);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
7717c478bd9Sstevel@tonic-gate goto digest_done;
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate rv = soft_digest_final(session_p, Ai, &AiLen);
7747c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
7757c478bd9Sstevel@tonic-gate goto digest_done;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate digest_done:
7787c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
7797c478bd9Sstevel@tonic-gate session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
7807c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
7837c478bd9Sstevel@tonic-gate goto cleanup;
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate * Step 6b.
7867c478bd9Sstevel@tonic-gate * Concatenate Ai to make B
7877c478bd9Sstevel@tonic-gate */
7887c478bd9Sstevel@tonic-gate for (j = 0; j < Blen; j += hashSize) {
7897c478bd9Sstevel@tonic-gate (void) memcpy(B+j, Ai, ((Blen - j > hashSize) ?
7907c478bd9Sstevel@tonic-gate hashSize : Blen - j));
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate * Step 6c.
7957c478bd9Sstevel@tonic-gate */
7967c478bd9Sstevel@tonic-gate k = Ilen / Blen;
7977c478bd9Sstevel@tonic-gate for (j = 0; j < k; j++) {
7987c478bd9Sstevel@tonic-gate uchar_t idx;
7997c478bd9Sstevel@tonic-gate CK_ULONG m, q = 1, cbit = 0;
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate for (m = Blen - 1; m >= (CK_ULONG)0; m--, q = 0) {
8027c478bd9Sstevel@tonic-gate idx = m + j*Blen;
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate q += (CK_ULONG)I[idx] + (CK_ULONG)B[m];
8057c478bd9Sstevel@tonic-gate q += cbit;
8067c478bd9Sstevel@tonic-gate I[idx] = (CK_BYTE)(q & 0xff);
8077c478bd9Sstevel@tonic-gate cbit = (q > 0xff);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate * Step 7.
8137c478bd9Sstevel@tonic-gate * A += Ai
8147c478bd9Sstevel@tonic-gate */
8157c478bd9Sstevel@tonic-gate (void) memcpy(A + i*hashSize, Ai, AiLen);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * Step 8.
8207c478bd9Sstevel@tonic-gate * The final output of this process is the A buffer
8217c478bd9Sstevel@tonic-gate */
8227c478bd9Sstevel@tonic-gate (void) memcpy(keybuf, A, keysize);
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate cleanup:
8257c478bd9Sstevel@tonic-gate if (A) {
8267c478bd9Sstevel@tonic-gate bzero(A, Alen);
8277c478bd9Sstevel@tonic-gate free(A);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate if (Ai) {
8307c478bd9Sstevel@tonic-gate bzero(Ai, AiLen);
8317c478bd9Sstevel@tonic-gate free(Ai);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate if (B) {
8347c478bd9Sstevel@tonic-gate bzero(B, Blen);
8357c478bd9Sstevel@tonic-gate free(B);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate if (D) {
8387c478bd9Sstevel@tonic-gate bzero(D, Dlen);
8397c478bd9Sstevel@tonic-gate free(D);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate if (I) {
8427c478bd9Sstevel@tonic-gate bzero(I, Ilen);
8437c478bd9Sstevel@tonic-gate free(I);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate return (rv);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@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)8497c478bd9Sstevel@tonic-gate soft_derivekey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
8507c478bd9Sstevel@tonic-gate soft_object_t *basekey_p, CK_ATTRIBUTE_PTR pTemplate,
8517c478bd9Sstevel@tonic-gate CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
8557c478bd9Sstevel@tonic-gate soft_object_t *secret_key;
8567c478bd9Sstevel@tonic-gate CK_MECHANISM digest_mech;
857f66d273dSizick CK_BYTE hash[SHA512_DIGEST_LENGTH]; /* space enough for all mechs */
858f66d273dSizick CK_ULONG hash_len = SHA512_DIGEST_LENGTH;
8597c478bd9Sstevel@tonic-gate CK_ULONG secret_key_len;
8607c478bd9Sstevel@tonic-gate CK_ULONG hash_size;
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
8637c478bd9Sstevel@tonic-gate case CKM_DH_PKCS_DERIVE:
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate * Create a new object for secret key. The key type should
8667c478bd9Sstevel@tonic-gate * be provided in the template.
8677c478bd9Sstevel@tonic-gate */
8687c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
8697c478bd9Sstevel@tonic-gate phKey, session_p, CKO_SECRET_KEY, (CK_KEY_TYPE)~0UL, 0,
8707c478bd9Sstevel@tonic-gate SOFT_DERIVE_KEY_DH, B_FALSE);
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
8737c478bd9Sstevel@tonic-gate return (rv);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate /* Obtain the secret object pointer. */
8777c478bd9Sstevel@tonic-gate secret_key = (soft_object_t *)*phKey;
8787c478bd9Sstevel@tonic-gate
879034448feSmcpowers rv = soft_dh_key_derive(basekey_p, secret_key,
880034448feSmcpowers (CK_BYTE *)pMechanism->pParameter,
881034448feSmcpowers pMechanism->ulParameterLen);
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
8847c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
8857c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE,
8867c478bd9Sstevel@tonic-gate B_FALSE);
8877c478bd9Sstevel@tonic-gate else
8887c478bd9Sstevel@tonic-gate soft_delete_object(session_p, secret_key,
8891f49a79aSZdenek Kotala B_FALSE, B_FALSE);
8907c478bd9Sstevel@tonic-gate return (rv);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate
8937c478bd9Sstevel@tonic-gate break;
8947c478bd9Sstevel@tonic-gate
895f9fbec18Smcpowers case CKM_ECDH1_DERIVE:
896f9fbec18Smcpowers /*
897f9fbec18Smcpowers * Create a new object for secret key. The key type should
898f9fbec18Smcpowers * be provided in the template.
899f9fbec18Smcpowers */
900f9fbec18Smcpowers rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
901f9fbec18Smcpowers phKey, session_p, CKO_SECRET_KEY, (CK_KEY_TYPE)~0UL, 0,
902f9fbec18Smcpowers SOFT_DERIVE_KEY_DH, B_FALSE);
903f9fbec18Smcpowers
904f9fbec18Smcpowers if (rv != CKR_OK) {
905f9fbec18Smcpowers return (rv);
906f9fbec18Smcpowers }
907f9fbec18Smcpowers
908f9fbec18Smcpowers /* Obtain the secret object pointer. */
909f9fbec18Smcpowers secret_key = (soft_object_t *)*phKey;
910f9fbec18Smcpowers
911f9fbec18Smcpowers rv = soft_ec_key_derive(basekey_p, secret_key,
912f9fbec18Smcpowers (CK_BYTE *)pMechanism->pParameter,
913f9fbec18Smcpowers pMechanism->ulParameterLen);
914f9fbec18Smcpowers
915f9fbec18Smcpowers if (rv != CKR_OK) {
916f9fbec18Smcpowers if (IS_TOKEN_OBJECT(secret_key))
917f9fbec18Smcpowers soft_delete_token_object(secret_key, B_FALSE,
918f9fbec18Smcpowers B_FALSE);
919f9fbec18Smcpowers else
920f9fbec18Smcpowers soft_delete_object(session_p, secret_key,
9211f49a79aSZdenek Kotala B_FALSE, B_FALSE);
922f9fbec18Smcpowers return (rv);
923f9fbec18Smcpowers }
924f9fbec18Smcpowers
925f9fbec18Smcpowers break;
926f9fbec18Smcpowers
9277c478bd9Sstevel@tonic-gate case CKM_SHA1_KEY_DERIVATION:
9287c478bd9Sstevel@tonic-gate hash_size = SHA1_HASH_SIZE;
9297c478bd9Sstevel@tonic-gate digest_mech.mechanism = CKM_SHA_1;
9307c478bd9Sstevel@tonic-gate goto common;
9317c478bd9Sstevel@tonic-gate
9327c478bd9Sstevel@tonic-gate case CKM_MD5_KEY_DERIVATION:
9337c478bd9Sstevel@tonic-gate hash_size = MD5_HASH_SIZE;
9347c478bd9Sstevel@tonic-gate digest_mech.mechanism = CKM_MD5;
935f66d273dSizick goto common;
936f66d273dSizick
937f66d273dSizick case CKM_SHA256_KEY_DERIVATION:
938f66d273dSizick hash_size = SHA256_DIGEST_LENGTH;
939f66d273dSizick digest_mech.mechanism = CKM_SHA256;
940f66d273dSizick goto common;
941f66d273dSizick
942f66d273dSizick case CKM_SHA384_KEY_DERIVATION:
943f66d273dSizick hash_size = SHA384_DIGEST_LENGTH;
944f66d273dSizick digest_mech.mechanism = CKM_SHA384;
945f66d273dSizick goto common;
946f66d273dSizick
947f66d273dSizick case CKM_SHA512_KEY_DERIVATION:
948f66d273dSizick hash_size = SHA512_DIGEST_LENGTH;
949f66d273dSizick digest_mech.mechanism = CKM_SHA512;
950f66d273dSizick goto common;
951f66d273dSizick
9527c478bd9Sstevel@tonic-gate common:
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate * Create a new object for secret key. The key type is optional
9557c478bd9Sstevel@tonic-gate * to be provided in the template. If it is not specified in
9567c478bd9Sstevel@tonic-gate * the template, the default is CKK_GENERIC_SECRET.
9577c478bd9Sstevel@tonic-gate */
9587c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
9597c478bd9Sstevel@tonic-gate phKey, session_p, CKO_SECRET_KEY,
9607c478bd9Sstevel@tonic-gate (CK_KEY_TYPE)CKK_GENERIC_SECRET, 0,
9617c478bd9Sstevel@tonic-gate SOFT_DERIVE_KEY_OTHER, B_FALSE);
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
9647c478bd9Sstevel@tonic-gate return (rv);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate /* Obtain the secret object pointer. */
9687c478bd9Sstevel@tonic-gate secret_key = (soft_object_t *)*phKey;
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate /* Validate the key type and key length */
9717c478bd9Sstevel@tonic-gate rv = soft_key_derive_check_length(secret_key, hash_size);
9727c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
9737c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
9747c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE,
9757c478bd9Sstevel@tonic-gate B_FALSE);
9767c478bd9Sstevel@tonic-gate else
9777c478bd9Sstevel@tonic-gate soft_delete_object(session_p, secret_key,
9781f49a79aSZdenek Kotala B_FALSE, B_FALSE);
9797c478bd9Sstevel@tonic-gate return (rv);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate * Derive the secret key by digesting the value of another
9847c478bd9Sstevel@tonic-gate * secret key (base key) with SHA-1 or MD5.
9857c478bd9Sstevel@tonic-gate */
9867c478bd9Sstevel@tonic-gate rv = soft_digest_init_internal(session_p, &digest_mech);
9877c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
9887c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
9897c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE,
9907c478bd9Sstevel@tonic-gate B_FALSE);
9917c478bd9Sstevel@tonic-gate else
9927c478bd9Sstevel@tonic-gate soft_delete_object(session_p, secret_key,
9931f49a79aSZdenek Kotala B_FALSE, B_FALSE);
9947c478bd9Sstevel@tonic-gate return (rv);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate
9977c478bd9Sstevel@tonic-gate rv = soft_digest(session_p, OBJ_SEC_VALUE(basekey_p),
9987c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(basekey_p), hash, &hash_len);
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
10017c478bd9Sstevel@tonic-gate /* soft_digest_common() has freed the digest context */
10027c478bd9Sstevel@tonic-gate session_p->digest.flags = 0;
10037c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
10067c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
10077c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE,
10087c478bd9Sstevel@tonic-gate B_FALSE);
10097c478bd9Sstevel@tonic-gate else
10107c478bd9Sstevel@tonic-gate soft_delete_object(session_p, secret_key,
10111f49a79aSZdenek Kotala B_FALSE, B_FALSE);
10127c478bd9Sstevel@tonic-gate return (rv);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate secret_key_len = OBJ_SEC_VALUE_LEN(secret_key);
10167c478bd9Sstevel@tonic-gate
10177c478bd9Sstevel@tonic-gate if ((OBJ_SEC_VALUE(secret_key) = malloc(secret_key_len)) ==
10187c478bd9Sstevel@tonic-gate NULL) {
10197c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key))
10207c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE,
10217c478bd9Sstevel@tonic-gate B_FALSE);
10227c478bd9Sstevel@tonic-gate else
10237c478bd9Sstevel@tonic-gate soft_delete_object(session_p, secret_key,
10241f49a79aSZdenek Kotala B_FALSE, B_FALSE);
10257c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate /*
10297c478bd9Sstevel@tonic-gate * The key produced by this mechanism will be of the
10307c478bd9Sstevel@tonic-gate * specified type and length.
10317c478bd9Sstevel@tonic-gate * The truncation removes extra bytes from the leading
10327c478bd9Sstevel@tonic-gate * of the digested key value.
10337c478bd9Sstevel@tonic-gate */
10347c478bd9Sstevel@tonic-gate (void) memcpy(OBJ_SEC_VALUE(secret_key),
10357c478bd9Sstevel@tonic-gate (hash + hash_len - secret_key_len),
10367c478bd9Sstevel@tonic-gate secret_key_len);
10377c478bd9Sstevel@tonic-gate
10387c478bd9Sstevel@tonic-gate break;
10397c478bd9Sstevel@tonic-gate
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate * The key sensitivity and extractability rules for the generated
10427c478bd9Sstevel@tonic-gate * keys will be enforced inside soft_ssl_master_key_derive() and
10437c478bd9Sstevel@tonic-gate * soft_ssl_key_and_mac_derive()
10447c478bd9Sstevel@tonic-gate */
10457c478bd9Sstevel@tonic-gate case CKM_SSL3_MASTER_KEY_DERIVE:
10467c478bd9Sstevel@tonic-gate case CKM_SSL3_MASTER_KEY_DERIVE_DH:
10477c478bd9Sstevel@tonic-gate case CKM_TLS_MASTER_KEY_DERIVE:
10487c478bd9Sstevel@tonic-gate case CKM_TLS_MASTER_KEY_DERIVE_DH:
10497c478bd9Sstevel@tonic-gate if (phKey == NULL_PTR)
10507c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
10517c478bd9Sstevel@tonic-gate return (soft_ssl_master_key_derive(session_p, pMechanism,
10527c478bd9Sstevel@tonic-gate basekey_p, pTemplate, ulAttributeCount, phKey));
10537c478bd9Sstevel@tonic-gate
10547c478bd9Sstevel@tonic-gate case CKM_SSL3_KEY_AND_MAC_DERIVE:
10557c478bd9Sstevel@tonic-gate case CKM_TLS_KEY_AND_MAC_DERIVE:
10567c478bd9Sstevel@tonic-gate return (soft_ssl_key_and_mac_derive(session_p, pMechanism,
10577c478bd9Sstevel@tonic-gate basekey_p, pTemplate, ulAttributeCount));
10587c478bd9Sstevel@tonic-gate
105960722cc8Sizick case CKM_TLS_PRF:
106060722cc8Sizick if (pMechanism->pParameter == NULL ||
106160722cc8Sizick pMechanism->ulParameterLen != sizeof (CK_TLS_PRF_PARAMS) ||
106260722cc8Sizick phKey != NULL)
106360722cc8Sizick return (CKR_ARGUMENTS_BAD);
106460722cc8Sizick
106560722cc8Sizick if (pTemplate != NULL)
106660722cc8Sizick return (CKR_TEMPLATE_INCONSISTENT);
106760722cc8Sizick
106860722cc8Sizick return (derive_tls_prf(
1069f9fbec18Smcpowers (CK_TLS_PRF_PARAMS_PTR)pMechanism->pParameter, basekey_p));
107060722cc8Sizick
10717c478bd9Sstevel@tonic-gate default:
10727c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate soft_derive_enforce_flags(basekey_p, secret_key);
10767c478bd9Sstevel@tonic-gate
10777c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(secret_key)) {
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate * All the info has been filled, so we can write to
10807c478bd9Sstevel@tonic-gate * keystore now.
10817c478bd9Sstevel@tonic-gate */
10827c478bd9Sstevel@tonic-gate rv = soft_put_object_to_keystore(secret_key);
10837c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
10847c478bd9Sstevel@tonic-gate soft_delete_token_object(secret_key, B_FALSE, B_FALSE);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate return (rv);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate * Perform key derivation rules on key's sensitivity and extractability.
10937c478bd9Sstevel@tonic-gate */
10947c478bd9Sstevel@tonic-gate void
soft_derive_enforce_flags(soft_object_t * basekey,soft_object_t * newkey)10957c478bd9Sstevel@tonic-gate soft_derive_enforce_flags(soft_object_t *basekey, soft_object_t *newkey)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate
10987c478bd9Sstevel@tonic-gate boolean_t new_sensitive = B_FALSE;
10997c478bd9Sstevel@tonic-gate boolean_t new_extractable = B_FALSE;
11007c478bd9Sstevel@tonic-gate
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate * The sensitive and extractable bits have been set when
11037c478bd9Sstevel@tonic-gate * the newkey was built.
11047c478bd9Sstevel@tonic-gate */
11057c478bd9Sstevel@tonic-gate if (newkey->bool_attr_mask & SENSITIVE_BOOL_ON) {
11067c478bd9Sstevel@tonic-gate new_sensitive = B_TRUE;
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate if (newkey->bool_attr_mask & EXTRACTABLE_BOOL_ON) {
11107c478bd9Sstevel@tonic-gate new_extractable = B_TRUE;
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate
11137c478bd9Sstevel@tonic-gate /* Derive the CKA_ALWAYS_SENSITIVE flag */
11147c478bd9Sstevel@tonic-gate if (!basekey->bool_attr_mask & ALWAYS_SENSITIVE_BOOL_ON) {
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate * If the base key has its CKA_ALWAYS_SENSITIVE set to
11177c478bd9Sstevel@tonic-gate * FALSE, then the derived key will as well.
11187c478bd9Sstevel@tonic-gate */
11197c478bd9Sstevel@tonic-gate newkey->bool_attr_mask &= ~ALWAYS_SENSITIVE_BOOL_ON;
11207c478bd9Sstevel@tonic-gate } else {
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate * If the base key has its CKA_ALWAYS_SENSITIVE set to TRUE,
11237c478bd9Sstevel@tonic-gate * then the derived key has the CKA_ALWAYS_SENSITIVE set to
11247c478bd9Sstevel@tonic-gate * the same value as its CKA_SENSITIVE;
11257c478bd9Sstevel@tonic-gate */
11267c478bd9Sstevel@tonic-gate if (new_sensitive) {
11277c478bd9Sstevel@tonic-gate newkey->bool_attr_mask |= ALWAYS_SENSITIVE_BOOL_ON;
11287c478bd9Sstevel@tonic-gate } else {
11297c478bd9Sstevel@tonic-gate newkey->bool_attr_mask &= ~ALWAYS_SENSITIVE_BOOL_ON;
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate
11337c478bd9Sstevel@tonic-gate /* Derive the CKA_NEVER_EXTRACTABLE flag */
11347c478bd9Sstevel@tonic-gate if (!basekey->bool_attr_mask & NEVER_EXTRACTABLE_BOOL_ON) {
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate * If the base key has its CKA_NEVER_EXTRACTABLE set to
11377c478bd9Sstevel@tonic-gate * FALSE, then the derived key will as well.
11387c478bd9Sstevel@tonic-gate */
11397c478bd9Sstevel@tonic-gate newkey->bool_attr_mask &= ~NEVER_EXTRACTABLE_BOOL_ON;
11407c478bd9Sstevel@tonic-gate } else {
11417c478bd9Sstevel@tonic-gate /*
11427c478bd9Sstevel@tonic-gate * If the base key has its CKA_NEVER_EXTRACTABLE set to TRUE,
11437c478bd9Sstevel@tonic-gate * then the derived key has the CKA_NEVER_EXTRACTABLE set to
11447c478bd9Sstevel@tonic-gate * the opposite value from its CKA_EXTRACTABLE;
11457c478bd9Sstevel@tonic-gate */
11467c478bd9Sstevel@tonic-gate if (new_extractable) {
11477c478bd9Sstevel@tonic-gate newkey->bool_attr_mask &= ~NEVER_EXTRACTABLE_BOOL_ON;
11487c478bd9Sstevel@tonic-gate } else {
11497c478bd9Sstevel@tonic-gate newkey->bool_attr_mask |= NEVER_EXTRACTABLE_BOOL_ON;
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate
11537c478bd9Sstevel@tonic-gate /* Set the CKA_LOCAL flag to false */
11547c478bd9Sstevel@tonic-gate newkey->bool_attr_mask &= ~LOCAL_BOOL_ON;
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate * do_prf
11607c478bd9Sstevel@tonic-gate *
11617c478bd9Sstevel@tonic-gate * This routine implements Step 3. of the PBKDF2 function
11627c478bd9Sstevel@tonic-gate * defined in PKCS#5 for generating derived keys from a
11637c478bd9Sstevel@tonic-gate * password.
11647c478bd9Sstevel@tonic-gate *
11657c478bd9Sstevel@tonic-gate * Currently, PRF is always SHA_1_HMAC.
11667c478bd9Sstevel@tonic-gate */
11677c478bd9Sstevel@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)11687c478bd9Sstevel@tonic-gate do_prf(soft_session_t *session_p,
11697c478bd9Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS_PTR params,
11707c478bd9Sstevel@tonic-gate soft_object_t *hmac_key,
11717c478bd9Sstevel@tonic-gate CK_BYTE *newsalt, CK_ULONG saltlen,
11727c478bd9Sstevel@tonic-gate CK_BYTE *blockdata, CK_ULONG blocklen)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
11757c478bd9Sstevel@tonic-gate CK_MECHANISM digest_mech = {CKM_SHA_1_HMAC, NULL, 0};
11767c478bd9Sstevel@tonic-gate CK_BYTE buffer[2][SHA1_HASH_SIZE];
11777c478bd9Sstevel@tonic-gate CK_ULONG hmac_outlen = SHA1_HASH_SIZE;
11787c478bd9Sstevel@tonic-gate CK_ULONG inlen;
11797c478bd9Sstevel@tonic-gate CK_BYTE *input, *output;
11807c478bd9Sstevel@tonic-gate CK_ULONG i, j;
11817c478bd9Sstevel@tonic-gate
11827c478bd9Sstevel@tonic-gate input = newsalt;
11837c478bd9Sstevel@tonic-gate inlen = saltlen;
11847c478bd9Sstevel@tonic-gate
11857c478bd9Sstevel@tonic-gate output = buffer[1];
11867c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
11877c478bd9Sstevel@tonic-gate
11887c478bd9Sstevel@tonic-gate if (session_p->sign.flags & CRYPTO_OPERATION_ACTIVE) {
11897c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
11907c478bd9Sstevel@tonic-gate return (CKR_OPERATION_ACTIVE);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate session_p->sign.flags |= CRYPTO_OPERATION_ACTIVE;
11937c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
11947c478bd9Sstevel@tonic-gate
11957c478bd9Sstevel@tonic-gate for (i = 0; i < params->iterations; i++) {
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate * The key doesn't change, its always the
11987c478bd9Sstevel@tonic-gate * password iniitally given.
11997c478bd9Sstevel@tonic-gate */
12007c478bd9Sstevel@tonic-gate rv = soft_sign_init(session_p, &digest_mech, hmac_key);
12017c478bd9Sstevel@tonic-gate
12027c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
12037c478bd9Sstevel@tonic-gate goto cleanup;
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate
12067c478bd9Sstevel@tonic-gate /* Call PRF function (SHA1_HMAC for now). */
1207f9fbec18Smcpowers rv = soft_sign(session_p, input, inlen, output, &hmac_outlen);
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
12107c478bd9Sstevel@tonic-gate goto cleanup;
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate /*
12137c478bd9Sstevel@tonic-gate * The first time, initialize the output buffer
12147c478bd9Sstevel@tonic-gate * with the HMAC signature.
12157c478bd9Sstevel@tonic-gate */
12167c478bd9Sstevel@tonic-gate if (i == 0) {
12177c478bd9Sstevel@tonic-gate (void) memcpy(blockdata, output,
12187c478bd9Sstevel@tonic-gate local_min(blocklen, hmac_outlen));
12197c478bd9Sstevel@tonic-gate } else {
12207c478bd9Sstevel@tonic-gate /*
12217c478bd9Sstevel@tonic-gate * XOR the existing data with output from PRF.
12227c478bd9Sstevel@tonic-gate *
12237c478bd9Sstevel@tonic-gate * Only XOR up to the length of the blockdata,
12247c478bd9Sstevel@tonic-gate * it may be less than a full hmac buffer when
12257c478bd9Sstevel@tonic-gate * the final block is being computed.
12267c478bd9Sstevel@tonic-gate */
12277c478bd9Sstevel@tonic-gate for (j = 0; j < hmac_outlen && j < blocklen; j++)
12287c478bd9Sstevel@tonic-gate blockdata[j] ^= output[j];
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate /* Output from previous PRF is input for next round */
12317c478bd9Sstevel@tonic-gate input = output;
12327c478bd9Sstevel@tonic-gate inlen = hmac_outlen;
12337c478bd9Sstevel@tonic-gate
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate * Switch buffers to avoid overuse of memcpy.
12367c478bd9Sstevel@tonic-gate * Initially we used buffer[1], so after the end of
12377c478bd9Sstevel@tonic-gate * the first iteration (i==0), we switch to buffer[0]
12387c478bd9Sstevel@tonic-gate * and continue swapping with each iteration.
12397c478bd9Sstevel@tonic-gate */
12407c478bd9Sstevel@tonic-gate output = buffer[i%2];
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate cleanup:
12437c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
12447c478bd9Sstevel@tonic-gate session_p->sign.flags &= ~CRYPTO_OPERATION_ACTIVE;
12457c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
12467c478bd9Sstevel@tonic-gate
12477c478bd9Sstevel@tonic-gate return (rv);
12487c478bd9Sstevel@tonic-gate }
12497c478bd9Sstevel@tonic-gate
12507c478bd9Sstevel@tonic-gate static CK_RV
soft_create_hmac_key(soft_session_t * session_p,CK_BYTE * passwd,CK_ULONG passwd_len,CK_OBJECT_HANDLE_PTR phKey)12517c478bd9Sstevel@tonic-gate soft_create_hmac_key(soft_session_t *session_p, CK_BYTE *passwd,
12527c478bd9Sstevel@tonic-gate CK_ULONG passwd_len, CK_OBJECT_HANDLE_PTR phKey)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
12557c478bd9Sstevel@tonic-gate CK_OBJECT_CLASS keyclass = CKO_SECRET_KEY;
12567c478bd9Sstevel@tonic-gate CK_KEY_TYPE keytype = CKK_GENERIC_SECRET;
12577c478bd9Sstevel@tonic-gate CK_BBOOL True = TRUE;
12587c478bd9Sstevel@tonic-gate CK_ATTRIBUTE keytemplate[4];
12597c478bd9Sstevel@tonic-gate /*
12607c478bd9Sstevel@tonic-gate * We must initialize each template member individually
12617c478bd9Sstevel@tonic-gate * because at the time of initial coding for ON10, the
12627c478bd9Sstevel@tonic-gate * compiler was using the "-xc99=%none" option
12637c478bd9Sstevel@tonic-gate * which prevents us from being able to declare the whole
12647c478bd9Sstevel@tonic-gate * template in place as usual.
12657c478bd9Sstevel@tonic-gate */
12667c478bd9Sstevel@tonic-gate keytemplate[0].type = CKA_CLASS;
12677c478bd9Sstevel@tonic-gate keytemplate[0].pValue = &keyclass;
12687c478bd9Sstevel@tonic-gate keytemplate[0].ulValueLen = sizeof (keyclass);
12697c478bd9Sstevel@tonic-gate
12707c478bd9Sstevel@tonic-gate keytemplate[1].type = CKA_KEY_TYPE;
12717c478bd9Sstevel@tonic-gate keytemplate[1].pValue = &keytype;
12727c478bd9Sstevel@tonic-gate keytemplate[1].ulValueLen = sizeof (keytype);
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate keytemplate[2].type = CKA_SIGN;
12757c478bd9Sstevel@tonic-gate keytemplate[2].pValue = &True;
12767c478bd9Sstevel@tonic-gate keytemplate[2].ulValueLen = sizeof (True);
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate keytemplate[3].type = CKA_VALUE;
12797c478bd9Sstevel@tonic-gate keytemplate[3].pValue = passwd;
12807c478bd9Sstevel@tonic-gate keytemplate[3].ulValueLen = passwd_len;
12817c478bd9Sstevel@tonic-gate /*
12827c478bd9Sstevel@tonic-gate * Create a generic key object to be used for HMAC operations.
12837c478bd9Sstevel@tonic-gate * The "value" for this key is the password from the
12847c478bd9Sstevel@tonic-gate * mechanism parameter structure.
12857c478bd9Sstevel@tonic-gate */
12867c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(keytemplate,
1287f9fbec18Smcpowers sizeof (keytemplate)/sizeof (CK_ATTRIBUTE), phKey, session_p,
1288f9fbec18Smcpowers CKO_SECRET_KEY, (CK_KEY_TYPE)CKK_GENERIC_SECRET, 0,
12897c478bd9Sstevel@tonic-gate SOFT_CREATE_OBJ, B_TRUE);
12907c478bd9Sstevel@tonic-gate
12917c478bd9Sstevel@tonic-gate return (rv);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate CK_RV
soft_generate_pkcs5_pbkdf2_key(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * secret_key)12957c478bd9Sstevel@tonic-gate soft_generate_pkcs5_pbkdf2_key(soft_session_t *session_p,
12967c478bd9Sstevel@tonic-gate CK_MECHANISM_PTR pMechanism,
12977c478bd9Sstevel@tonic-gate soft_object_t *secret_key)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
13007c478bd9Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS *params =
13017c478bd9Sstevel@tonic-gate (CK_PKCS5_PBKD2_PARAMS *)pMechanism->pParameter;
13027c478bd9Sstevel@tonic-gate CK_ULONG hLen = SHA1_HASH_SIZE;
13037c478bd9Sstevel@tonic-gate CK_ULONG dkLen, i;
13047c478bd9Sstevel@tonic-gate CK_ULONG blocks, remainder;
13057c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE phKey = 0;
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,
13267c478bd9Sstevel@tonic-gate *params->ulPasswordLen, &phKey);
13277c478bd9Sstevel@tonic-gate
13287c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
13297c478bd9Sstevel@tonic-gate return (rv);
13307c478bd9Sstevel@tonic-gate
13317c478bd9Sstevel@tonic-gate hmac_key = (soft_object_t *)phKey;
13327c478bd9Sstevel@tonic-gate
13337c478bd9Sstevel@tonic-gate /* Step 1. */
13347c478bd9Sstevel@tonic-gate dkLen = OBJ_SEC_VALUE_LEN(secret_key); /* length of desired key */
13357c478bd9Sstevel@tonic-gate
13367c478bd9Sstevel@tonic-gate if (dkLen > ((((u_longlong_t)1)<<32)-1)*hLen) {
13371f49a79aSZdenek Kotala (void) soft_delete_object(session_p, hmac_key, B_FALSE,
13381f49a79aSZdenek Kotala B_FALSE);
13397c478bd9Sstevel@tonic-gate return (CKR_KEY_SIZE_RANGE);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate
13427c478bd9Sstevel@tonic-gate /* Step 2. */
13437c478bd9Sstevel@tonic-gate blocks = dkLen / hLen;
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate /* crude "Ceiling" function to adjust the number of blocks to use */
13467c478bd9Sstevel@tonic-gate if (blocks * hLen != dkLen)
13477c478bd9Sstevel@tonic-gate blocks++;
13487c478bd9Sstevel@tonic-gate
13497c478bd9Sstevel@tonic-gate remainder = dkLen - ((blocks - 1) * hLen);
13507c478bd9Sstevel@tonic-gate
13517c478bd9Sstevel@tonic-gate /* Step 3 */
13527c478bd9Sstevel@tonic-gate salt = (CK_BYTE *)malloc(params->ulSaltSourceDataLen + 4);
13537c478bd9Sstevel@tonic-gate if (salt == NULL) {
13541f49a79aSZdenek Kotala (void) soft_delete_object(session_p, hmac_key, B_FALSE,
13551f49a79aSZdenek Kotala B_FALSE);
13567c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate /*
13597c478bd9Sstevel@tonic-gate * Nothing in PKCS#5 says you cannot pass an empty
13607c478bd9Sstevel@tonic-gate * salt, so we will allow for this and not return error
13617c478bd9Sstevel@tonic-gate * if the salt is not specified.
13627c478bd9Sstevel@tonic-gate */
1363f9fbec18Smcpowers if (params->pSaltSourceData != NULL && params->ulSaltSourceDataLen > 0)
13647c478bd9Sstevel@tonic-gate (void) memcpy(salt, params->pSaltSourceData,
13657c478bd9Sstevel@tonic-gate params->ulSaltSourceDataLen);
13667c478bd9Sstevel@tonic-gate
13677c478bd9Sstevel@tonic-gate /*
13687c478bd9Sstevel@tonic-gate * Get pointer to the data section of the key,
13697c478bd9Sstevel@tonic-gate * this will be used below as output from the
13707c478bd9Sstevel@tonic-gate * PRF iteration/concatenations so that when the
13717c478bd9Sstevel@tonic-gate * blocks are all iterated, the secret_key will
13727c478bd9Sstevel@tonic-gate * have the resulting derived key value.
13737c478bd9Sstevel@tonic-gate */
13747c478bd9Sstevel@tonic-gate keydata = (CK_BYTE *)OBJ_SEC_VALUE(secret_key);
13757c478bd9Sstevel@tonic-gate
13767c478bd9Sstevel@tonic-gate /* Step 4. */
13777c478bd9Sstevel@tonic-gate for (i = 0; i < blocks && (rv == CKR_OK); i++) {
13787c478bd9Sstevel@tonic-gate CK_BYTE *s;
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate s = salt + params->ulSaltSourceDataLen;
13817c478bd9Sstevel@tonic-gate
13827c478bd9Sstevel@tonic-gate /*
13837c478bd9Sstevel@tonic-gate * Append the block index to the salt as input
13847c478bd9Sstevel@tonic-gate * to the PRF. Block index should start at 1
13857c478bd9Sstevel@tonic-gate * not 0.
13867c478bd9Sstevel@tonic-gate */
13877c478bd9Sstevel@tonic-gate *s++ = ((i+1) >> 24) & 0xff;
13887c478bd9Sstevel@tonic-gate *s++ = ((i+1) >> 16) & 0xff;
13897c478bd9Sstevel@tonic-gate *s++ = ((i+1) >> 8) & 0xff;
13907c478bd9Sstevel@tonic-gate *s = ((i+1)) & 0xff;
13917c478bd9Sstevel@tonic-gate
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate * Adjust the key pointer so we always append the
13947c478bd9Sstevel@tonic-gate * PRF output to the current key.
13957c478bd9Sstevel@tonic-gate */
13967c478bd9Sstevel@tonic-gate rv = do_prf(session_p, params, hmac_key,
1397f9fbec18Smcpowers salt, params->ulSaltSourceDataLen + 4, keydata,
13987c478bd9Sstevel@tonic-gate ((i + 1) == blocks ? remainder : hLen));
13997c478bd9Sstevel@tonic-gate
14007c478bd9Sstevel@tonic-gate keydata += hLen;
14017c478bd9Sstevel@tonic-gate }
14021f49a79aSZdenek Kotala (void) soft_delete_object(session_p, hmac_key, B_FALSE, B_FALSE);
14037c478bd9Sstevel@tonic-gate free(salt);
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate return (rv);
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate
14087c478bd9Sstevel@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)14097c478bd9Sstevel@tonic-gate soft_wrapkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
14107c478bd9Sstevel@tonic-gate soft_object_t *wrappingKey_p, soft_object_t *hkey_p,
14117c478bd9Sstevel@tonic-gate CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
14127c478bd9Sstevel@tonic-gate {
14137c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
14147c478bd9Sstevel@tonic-gate CK_ULONG plain_len = 0;
14157c478bd9Sstevel@tonic-gate CK_BYTE_PTR plain_data = NULL;
14167c478bd9Sstevel@tonic-gate CK_ULONG padded_len = 0;
14177c478bd9Sstevel@tonic-gate CK_BYTE_PTR padded_data = NULL;
14187c478bd9Sstevel@tonic-gate CK_ULONG wkey_blksz = 1; /* so modulo will work right */
14197c478bd9Sstevel@tonic-gate
14207c478bd9Sstevel@tonic-gate /* Check if the mechanism is supported. */
14217c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
14227c478bd9Sstevel@tonic-gate case CKM_DES_CBC_PAD:
14237c478bd9Sstevel@tonic-gate case CKM_DES3_CBC_PAD:
14247c478bd9Sstevel@tonic-gate case CKM_AES_CBC_PAD:
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate * Secret key mechs with padding can be used to wrap secret
14277c478bd9Sstevel@tonic-gate * keys and private keys only. See PKCS#11, * sec 11.14,
14287c478bd9Sstevel@tonic-gate * C_WrapKey and secs 12.* for each mechanism's wrapping/
14297c478bd9Sstevel@tonic-gate * unwrapping constraints.
14307c478bd9Sstevel@tonic-gate */
14317c478bd9Sstevel@tonic-gate if (hkey_p->class != CKO_SECRET_KEY && hkey_p->class !=
14327c478bd9Sstevel@tonic-gate CKO_PRIVATE_KEY)
14337c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
14347c478bd9Sstevel@tonic-gate break;
14357c478bd9Sstevel@tonic-gate case CKM_RSA_PKCS:
14367c478bd9Sstevel@tonic-gate case CKM_RSA_X_509:
14377c478bd9Sstevel@tonic-gate case CKM_DES_ECB:
14387c478bd9Sstevel@tonic-gate case CKM_DES3_ECB:
14397c478bd9Sstevel@tonic-gate case CKM_AES_ECB:
14407c478bd9Sstevel@tonic-gate case CKM_DES_CBC:
14417c478bd9Sstevel@tonic-gate case CKM_DES3_CBC:
14427c478bd9Sstevel@tonic-gate case CKM_AES_CBC:
144323c57df7Smcpowers case CKM_AES_CTR:
1444f66d273dSizick case CKM_BLOWFISH_CBC:
14457c478bd9Sstevel@tonic-gate /*
14467c478bd9Sstevel@tonic-gate * Unpadded secret key mechs and private key mechs are only
14477c478bd9Sstevel@tonic-gate * defined for wrapping secret keys. See PKCS#11 refs above.
14487c478bd9Sstevel@tonic-gate */
14497c478bd9Sstevel@tonic-gate if (hkey_p->class != CKO_SECRET_KEY)
14507c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
14517c478bd9Sstevel@tonic-gate break;
14527c478bd9Sstevel@tonic-gate default:
14537c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate
14567c478bd9Sstevel@tonic-gate if (hkey_p->class == CKO_SECRET_KEY) {
14577c478bd9Sstevel@tonic-gate plain_data = OBJ_SEC_VALUE(hkey_p);
14587c478bd9Sstevel@tonic-gate plain_len = OBJ_SEC_VALUE_LEN(hkey_p);
14597c478bd9Sstevel@tonic-gate } else {
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate * BER-encode the object to be wrapped: call first with
14627c478bd9Sstevel@tonic-gate * plain_data = NULL to get the size needed, allocate that
14637c478bd9Sstevel@tonic-gate * much space, call again to fill space with actual data.
14647c478bd9Sstevel@tonic-gate */
14657c478bd9Sstevel@tonic-gate rv = soft_object_to_asn1(hkey_p, NULL, &plain_len);
14667c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
14677c478bd9Sstevel@tonic-gate return (rv);
14687c478bd9Sstevel@tonic-gate if ((plain_data = malloc(plain_len)) == NULL)
14697c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
14707c478bd9Sstevel@tonic-gate (void) memset(plain_data, 0x0, plain_len);
14717c478bd9Sstevel@tonic-gate rv = soft_object_to_asn1(hkey_p, plain_data, &plain_len);
14727c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
14737c478bd9Sstevel@tonic-gate goto cleanup_wrap;
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate
14767c478bd9Sstevel@tonic-gate /*
14777c478bd9Sstevel@tonic-gate * For unpadded ECB and CBC mechanisms, the object needs to be
14787c478bd9Sstevel@tonic-gate * padded to the wrapping key's blocksize prior to the encryption.
14797c478bd9Sstevel@tonic-gate */
14807c478bd9Sstevel@tonic-gate padded_len = plain_len;
14817c478bd9Sstevel@tonic-gate padded_data = plain_data;
14827c478bd9Sstevel@tonic-gate
14837c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
14847c478bd9Sstevel@tonic-gate case CKM_DES_ECB:
14857c478bd9Sstevel@tonic-gate case CKM_DES3_ECB:
14867c478bd9Sstevel@tonic-gate case CKM_AES_ECB:
14877c478bd9Sstevel@tonic-gate case CKM_DES_CBC:
14887c478bd9Sstevel@tonic-gate case CKM_DES3_CBC:
14897c478bd9Sstevel@tonic-gate case CKM_AES_CBC:
1490f66d273dSizick case CKM_BLOWFISH_CBC:
14917c478bd9Sstevel@tonic-gate /* Find the block size of the wrapping key. */
14927c478bd9Sstevel@tonic-gate if (wrappingKey_p->class == CKO_SECRET_KEY) {
14937c478bd9Sstevel@tonic-gate switch (wrappingKey_p->key_type) {
14947c478bd9Sstevel@tonic-gate case CKK_DES:
14957c478bd9Sstevel@tonic-gate case CKK_DES2:
14967c478bd9Sstevel@tonic-gate case CKK_DES3:
14977c478bd9Sstevel@tonic-gate wkey_blksz = DES_BLOCK_LEN;
14987c478bd9Sstevel@tonic-gate break;
14997c478bd9Sstevel@tonic-gate case CKK_AES:
15007c478bd9Sstevel@tonic-gate wkey_blksz = AES_BLOCK_LEN;
15017c478bd9Sstevel@tonic-gate break;
1502f66d273dSizick case CKK_BLOWFISH:
1503f66d273dSizick wkey_blksz = BLOWFISH_BLOCK_LEN;
1504f66d273dSizick break;
15057c478bd9Sstevel@tonic-gate default:
15067c478bd9Sstevel@tonic-gate break;
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate } else {
15097c478bd9Sstevel@tonic-gate rv = CKR_WRAPPING_KEY_TYPE_INCONSISTENT;
15107c478bd9Sstevel@tonic-gate goto cleanup_wrap;
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate
15137c478bd9Sstevel@tonic-gate /* Extend the plain text data to block size boundary. */
15147c478bd9Sstevel@tonic-gate if ((padded_len % wkey_blksz) != 0) {
15157c478bd9Sstevel@tonic-gate padded_len += (wkey_blksz - (plain_len % wkey_blksz));
15167c478bd9Sstevel@tonic-gate if ((padded_data = malloc(padded_len)) == NULL) {
15177c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
15187c478bd9Sstevel@tonic-gate goto cleanup_wrap;
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate (void) memset(padded_data, 0x0, padded_len);
15217c478bd9Sstevel@tonic-gate (void) memcpy(padded_data, plain_data, plain_len);
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate break;
15247c478bd9Sstevel@tonic-gate default:
15257c478bd9Sstevel@tonic-gate break;
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate
15287c478bd9Sstevel@tonic-gate rv = soft_encrypt_init(session_p, pMechanism, wrappingKey_p);
15297c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
15307c478bd9Sstevel@tonic-gate goto cleanup_wrap;
15317c478bd9Sstevel@tonic-gate
15327c478bd9Sstevel@tonic-gate rv = soft_encrypt(session_p, padded_data, padded_len,
15337c478bd9Sstevel@tonic-gate pWrappedKey, pulWrappedKeyLen);
15347c478bd9Sstevel@tonic-gate
15357c478bd9Sstevel@tonic-gate cleanup_wrap:
15367c478bd9Sstevel@tonic-gate if (padded_data != NULL && padded_len != plain_len) {
15377c478bd9Sstevel@tonic-gate /* Clear buffer before returning to memory pool. */
15387c478bd9Sstevel@tonic-gate (void) memset(padded_data, 0x0, padded_len);
15397c478bd9Sstevel@tonic-gate free(padded_data);
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate if ((hkey_p->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
15437c478bd9Sstevel@tonic-gate /* Clear buffer before returning to memory pool. */
15447c478bd9Sstevel@tonic-gate (void) memset(plain_data, 0x0, plain_len);
15457c478bd9Sstevel@tonic-gate free(plain_data);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate
15487c478bd9Sstevel@tonic-gate return (rv);
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate
15517c478bd9Sstevel@tonic-gate /*
15527c478bd9Sstevel@tonic-gate * Quick check for whether unwrapped key length is appropriate for key type
15537c478bd9Sstevel@tonic-gate * and whether it needs to be truncated (in case the wrapping function had
15547c478bd9Sstevel@tonic-gate * to pad the key prior to wrapping).
15557c478bd9Sstevel@tonic-gate */
15567c478bd9Sstevel@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)15577c478bd9Sstevel@tonic-gate soft_unwrap_secret_len_check(CK_KEY_TYPE keytype, CK_MECHANISM_TYPE mechtype,
15587c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount)
15597c478bd9Sstevel@tonic-gate {
15607c478bd9Sstevel@tonic-gate CK_ULONG i;
15617c478bd9Sstevel@tonic-gate boolean_t isValueLen = B_FALSE;
15627c478bd9Sstevel@tonic-gate
15637c478bd9Sstevel@tonic-gate /*
15647c478bd9Sstevel@tonic-gate * Based on the key type and the mech used to unwrap, need to
15657c478bd9Sstevel@tonic-gate * determine if CKA_VALUE_LEN should or should not be specified.
15667c478bd9Sstevel@tonic-gate * PKCS#11 v2.11 restricts CKA_VALUE_LEN from being specified
15677c478bd9Sstevel@tonic-gate * for C_UnwrapKey for all mechs and key types, but v2.20 loosens
15687c478bd9Sstevel@tonic-gate * that restriction, perhaps because it makes it impossible to
15697c478bd9Sstevel@tonic-gate * determine the original length of unwrapped variable-length secret
15707c478bd9Sstevel@tonic-gate * keys, such as RC4, AES, and GENERIC_SECRET. These variable-length
15717c478bd9Sstevel@tonic-gate * secret keys would have been padded with trailing null-bytes so
15727c478bd9Sstevel@tonic-gate * that they could be successfully wrapped with *_ECB and *_CBC
15737c478bd9Sstevel@tonic-gate * mechanisms. Hence for unwrapping with these mechs, CKA_VALUE_LEN
15747c478bd9Sstevel@tonic-gate * must be specified. For unwrapping with other mechs, such as
15757c478bd9Sstevel@tonic-gate * *_CBC_PAD, the CKA_VALUE_LEN is not needed.
15767c478bd9Sstevel@tonic-gate */
15777c478bd9Sstevel@tonic-gate
15787c478bd9Sstevel@tonic-gate /* Find out if template has CKA_VALUE_LEN. */
15797c478bd9Sstevel@tonic-gate for (i = 0; i < ulAttributeCount; i++) {
15807c478bd9Sstevel@tonic-gate if (pTemplate[i].type == CKA_VALUE_LEN &&
15817c478bd9Sstevel@tonic-gate pTemplate[i].pValue != NULL) {
15827c478bd9Sstevel@tonic-gate isValueLen = B_TRUE;
15837c478bd9Sstevel@tonic-gate break;
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate
15877c478bd9Sstevel@tonic-gate /* Does its presence conflict with the mech type and key type? */
15887c478bd9Sstevel@tonic-gate switch (mechtype) {
15897c478bd9Sstevel@tonic-gate case CKM_DES_ECB:
15907c478bd9Sstevel@tonic-gate case CKM_DES3_ECB:
15917c478bd9Sstevel@tonic-gate case CKM_AES_ECB:
15927c478bd9Sstevel@tonic-gate case CKM_DES_CBC:
15937c478bd9Sstevel@tonic-gate case CKM_DES3_CBC:
15947c478bd9Sstevel@tonic-gate case CKM_AES_CBC:
1595f66d273dSizick case CKM_BLOWFISH_CBC:
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate * CKA_VALUE_LEN must be specified
15987c478bd9Sstevel@tonic-gate * if keytype is CKK_RC4, CKK_AES and CKK_GENERIC_SECRET
15997c478bd9Sstevel@tonic-gate * and must not be specified otherwise
16007c478bd9Sstevel@tonic-gate */
16017c478bd9Sstevel@tonic-gate switch (keytype) {
16027c478bd9Sstevel@tonic-gate case CKK_DES:
16037c478bd9Sstevel@tonic-gate case CKK_DES2:
16047c478bd9Sstevel@tonic-gate case CKK_DES3:
16057c478bd9Sstevel@tonic-gate if (isValueLen)
16067c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCONSISTENT);
16077c478bd9Sstevel@tonic-gate break;
16087c478bd9Sstevel@tonic-gate case CKK_GENERIC_SECRET:
16097c478bd9Sstevel@tonic-gate case CKK_RC4:
16107c478bd9Sstevel@tonic-gate case CKK_AES:
1611f66d273dSizick case CKK_BLOWFISH:
16127c478bd9Sstevel@tonic-gate if (!isValueLen)
16137c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
16147c478bd9Sstevel@tonic-gate break;
16157c478bd9Sstevel@tonic-gate default:
16167c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_SUPPORTED);
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate break;
16197c478bd9Sstevel@tonic-gate default:
16207c478bd9Sstevel@tonic-gate /* CKA_VALUE_LEN must not be specified */
16217c478bd9Sstevel@tonic-gate if (isValueLen)
16227c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCONSISTENT);
16237c478bd9Sstevel@tonic-gate break;
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate
16267c478bd9Sstevel@tonic-gate return (CKR_OK);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate
16297c478bd9Sstevel@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)16307c478bd9Sstevel@tonic-gate soft_unwrapkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
16317c478bd9Sstevel@tonic-gate soft_object_t *unwrappingkey_p,
16327c478bd9Sstevel@tonic-gate CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen,
16337c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
16347c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE_PTR phKey)
16357c478bd9Sstevel@tonic-gate {
16367c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
16377c478bd9Sstevel@tonic-gate CK_OBJECT_CLASS new_obj_class = ~0UL;
16387c478bd9Sstevel@tonic-gate int i = 0;
16397c478bd9Sstevel@tonic-gate soft_object_t *new_objp = NULL;
16407c478bd9Sstevel@tonic-gate boolean_t persistent = B_FALSE;
16417c478bd9Sstevel@tonic-gate CK_BYTE_PTR plain_data = NULL;
16427c478bd9Sstevel@tonic-gate CK_ULONG plain_len = 0;
16437c478bd9Sstevel@tonic-gate secret_key_obj_t *sck = NULL;
16447c478bd9Sstevel@tonic-gate
16457c478bd9Sstevel@tonic-gate /* Scan the attribute template for the object class. */
16467c478bd9Sstevel@tonic-gate if (pTemplate != NULL && ulAttributeCount != 0) {
16477c478bd9Sstevel@tonic-gate for (i = 0; i < ulAttributeCount; i++) {
16487c478bd9Sstevel@tonic-gate if (pTemplate[i].type == CKA_CLASS) {
16497c478bd9Sstevel@tonic-gate new_obj_class =
16507c478bd9Sstevel@tonic-gate *((CK_OBJECT_CLASS *)pTemplate[i].pValue);
16517c478bd9Sstevel@tonic-gate break;
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate if (new_obj_class == ~0UL)
16557c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate
16587c478bd9Sstevel@tonic-gate /*
16597c478bd9Sstevel@tonic-gate * Check if the mechanism is supported, and now that the new
16607c478bd9Sstevel@tonic-gate * object's class is known, the mechanism selected should be
16617c478bd9Sstevel@tonic-gate * capable of doing the unwrap.
16627c478bd9Sstevel@tonic-gate */
16637c478bd9Sstevel@tonic-gate switch (pMechanism->mechanism) {
16647c478bd9Sstevel@tonic-gate case CKM_RSA_PKCS:
16657c478bd9Sstevel@tonic-gate case CKM_RSA_X_509:
16667c478bd9Sstevel@tonic-gate case CKM_DES_ECB:
16677c478bd9Sstevel@tonic-gate case CKM_DES3_ECB:
16687c478bd9Sstevel@tonic-gate case CKM_AES_ECB:
16697c478bd9Sstevel@tonic-gate case CKM_DES_CBC:
16707c478bd9Sstevel@tonic-gate case CKM_DES3_CBC:
16717c478bd9Sstevel@tonic-gate case CKM_AES_CBC:
1672f66d273dSizick case CKM_BLOWFISH_CBC:
16737c478bd9Sstevel@tonic-gate if (new_obj_class != CKO_SECRET_KEY)
16747c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
16757c478bd9Sstevel@tonic-gate break;
16767c478bd9Sstevel@tonic-gate case CKM_DES_CBC_PAD:
16777c478bd9Sstevel@tonic-gate case CKM_DES3_CBC_PAD:
16787c478bd9Sstevel@tonic-gate case CKM_AES_CBC_PAD:
16797c478bd9Sstevel@tonic-gate if (new_obj_class != CKO_SECRET_KEY && new_obj_class !=
16807c478bd9Sstevel@tonic-gate CKO_PRIVATE_KEY)
16817c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
16827c478bd9Sstevel@tonic-gate break;
16837c478bd9Sstevel@tonic-gate default:
16847c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID);
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate
16877c478bd9Sstevel@tonic-gate /* Create a new object based on the attribute template. */
16887c478bd9Sstevel@tonic-gate rv = soft_gen_keyobject(pTemplate, ulAttributeCount,
16897c478bd9Sstevel@tonic-gate (CK_ULONG *)&new_objp, session_p, (CK_OBJECT_CLASS)~0UL,
16907c478bd9Sstevel@tonic-gate (CK_KEY_TYPE)~0UL, 0, SOFT_UNWRAP_KEY, B_FALSE);
16917c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
16927c478bd9Sstevel@tonic-gate return (rv);
16937c478bd9Sstevel@tonic-gate
16947c478bd9Sstevel@tonic-gate /*
16957c478bd9Sstevel@tonic-gate * New key will have CKA_ALWAYS_SENSITIVE and CKA_NEVER_EXTRACTABLE
16967c478bd9Sstevel@tonic-gate * both set to FALSE. CKA_EXTRACTABLE will be set _by_default_ to
16977c478bd9Sstevel@tonic-gate * true -- leaving the possibility that it may be set FALSE by the
16987c478bd9Sstevel@tonic-gate * supplied attribute template. If the precise template cannot be
16997c478bd9Sstevel@tonic-gate * supported, unwrap fails. PKCS#11 spec, Sec. 11.14, C_UnwrapKey.
17007c478bd9Sstevel@tonic-gate *
17017c478bd9Sstevel@tonic-gate * Therefore, check the new object's NEVER_EXTRACTABLE_BOOL_ON and
17027c478bd9Sstevel@tonic-gate * ALWAYS_SENSITVE_BOOL_ON; if they are TRUE, the template must
17037c478bd9Sstevel@tonic-gate * have supplied them and therefore we cannot honor the unwrap.
17047c478bd9Sstevel@tonic-gate */
17057c478bd9Sstevel@tonic-gate if ((new_objp->bool_attr_mask & NEVER_EXTRACTABLE_BOOL_ON) ||
17067c478bd9Sstevel@tonic-gate (new_objp->bool_attr_mask & ALWAYS_SENSITIVE_BOOL_ON)) {
17077c478bd9Sstevel@tonic-gate rv = CKR_TEMPLATE_INCONSISTENT;
17087c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate
17117c478bd9Sstevel@tonic-gate rv = soft_decrypt_init(session_p, pMechanism, unwrappingkey_p);
17127c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
17137c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate /* First get the length of the plain data */
17167c478bd9Sstevel@tonic-gate rv = soft_decrypt(session_p, pWrappedKey, ulWrappedKeyLen, NULL,
17177c478bd9Sstevel@tonic-gate &plain_len);
17187c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
17197c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17207c478bd9Sstevel@tonic-gate
17217c478bd9Sstevel@tonic-gate /* Allocate space for the unwrapped data */
17227c478bd9Sstevel@tonic-gate if ((plain_data = malloc(plain_len)) == NULL) {
17237c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
17247c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate (void) memset(plain_data, 0x0, plain_len);
17277c478bd9Sstevel@tonic-gate
17287c478bd9Sstevel@tonic-gate /* Perform actual decryption into the allocated space. */
17297c478bd9Sstevel@tonic-gate rv = soft_decrypt(session_p, pWrappedKey, ulWrappedKeyLen, plain_data,
17307c478bd9Sstevel@tonic-gate &plain_len);
17317c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
17327c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17337c478bd9Sstevel@tonic-gate
17347c478bd9Sstevel@tonic-gate if (new_objp->class == CKO_SECRET_KEY) {
17357c478bd9Sstevel@tonic-gate /*
17367c478bd9Sstevel@tonic-gate * Since no ASN.1 encoding is done for secret keys, check for
17377c478bd9Sstevel@tonic-gate * appropriateness and copy decrypted buffer to the key object.
17387c478bd9Sstevel@tonic-gate */
17397c478bd9Sstevel@tonic-gate
17407c478bd9Sstevel@tonic-gate /* Check keytype and mechtype don't conflict with valuelen */
17417c478bd9Sstevel@tonic-gate rv = soft_unwrap_secret_len_check(new_objp->key_type,
17427c478bd9Sstevel@tonic-gate pMechanism->mechanism, pTemplate, ulAttributeCount);
17437c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
17447c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17457c478bd9Sstevel@tonic-gate
17467c478bd9Sstevel@tonic-gate /*
17477c478bd9Sstevel@tonic-gate * Allocate the secret key structure if not already there;
17487c478bd9Sstevel@tonic-gate * it will exist for variable length keys since CKA_VALUE_LEN
17497c478bd9Sstevel@tonic-gate * is specified and saved, but not for fixed length keys.
17507c478bd9Sstevel@tonic-gate */
17517c478bd9Sstevel@tonic-gate if (OBJ_SEC(new_objp) == NULL) {
17527c478bd9Sstevel@tonic-gate if ((sck = calloc(1, sizeof (secret_key_obj_t))) ==
17537c478bd9Sstevel@tonic-gate NULL) {
17547c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
17557c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate OBJ_SEC(new_objp) = sck;
17587c478bd9Sstevel@tonic-gate }
17597c478bd9Sstevel@tonic-gate
17607c478bd9Sstevel@tonic-gate switch (new_objp->key_type) {
17617c478bd9Sstevel@tonic-gate /* Fixed length secret keys don't have CKA_VALUE_LEN */
17627c478bd9Sstevel@tonic-gate case CKK_DES:
17637c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(new_objp) = DES_KEYSIZE;
17647c478bd9Sstevel@tonic-gate break;
17657c478bd9Sstevel@tonic-gate case CKK_DES2:
17667c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(new_objp) = DES2_KEYSIZE;
17677c478bd9Sstevel@tonic-gate break;
17687c478bd9Sstevel@tonic-gate case CKK_DES3:
17697c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(new_objp) = DES3_KEYSIZE;
17707c478bd9Sstevel@tonic-gate break;
17717c478bd9Sstevel@tonic-gate
17727c478bd9Sstevel@tonic-gate /*
17737c478bd9Sstevel@tonic-gate * Variable length secret keys. CKA_VALUE_LEN must be
17747c478bd9Sstevel@tonic-gate * provided by the template when mech is *_ECB or *_CBC, and
17757c478bd9Sstevel@tonic-gate * should already have been set during soft_gen_keyobject().
17767c478bd9Sstevel@tonic-gate * Otherwise we don't need CKA_VALUE_LEN.
17777c478bd9Sstevel@tonic-gate */
17787c478bd9Sstevel@tonic-gate case CKK_GENERIC_SECRET:
17797c478bd9Sstevel@tonic-gate case CKK_RC4:
17807c478bd9Sstevel@tonic-gate case CKK_AES:
1781f66d273dSizick case CKK_BLOWFISH:
17827c478bd9Sstevel@tonic-gate break;
17837c478bd9Sstevel@tonic-gate default:
17847c478bd9Sstevel@tonic-gate rv = CKR_WRAPPED_KEY_INVALID;
17857c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17867c478bd9Sstevel@tonic-gate };
17877c478bd9Sstevel@tonic-gate
17887c478bd9Sstevel@tonic-gate if (OBJ_SEC_VALUE_LEN(new_objp) == 0) {
17897c478bd9Sstevel@tonic-gate /* No CKA_VALUE_LEN set so set it now and save data */
17907c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(new_objp) = plain_len;
17917c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE(new_objp) = plain_data;
17927c478bd9Sstevel@tonic-gate } else if (OBJ_SEC_VALUE_LEN(new_objp) == plain_len) {
17937c478bd9Sstevel@tonic-gate /* No need to truncate, just save the data */
17947c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE(new_objp) = plain_data;
17957c478bd9Sstevel@tonic-gate } else if (OBJ_SEC_VALUE_LEN(new_objp) > plain_len) {
17967c478bd9Sstevel@tonic-gate /* Length can't be bigger than what was decrypted */
17977c478bd9Sstevel@tonic-gate rv = CKR_WRAPPED_KEY_LEN_RANGE;
17987c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
17997c478bd9Sstevel@tonic-gate } else { /* betw 0 and plain_len, hence padded */
18007c478bd9Sstevel@tonic-gate /* Truncate the data before saving. */
18017c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE(new_objp) = realloc(plain_data,
18027c478bd9Sstevel@tonic-gate OBJ_SEC_VALUE_LEN(new_objp));
18037c478bd9Sstevel@tonic-gate if (OBJ_SEC_VALUE(new_objp) == NULL) {
18047c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
18057c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate }
18087c478bd9Sstevel@tonic-gate } else {
18097c478bd9Sstevel@tonic-gate /* BER-decode the object to be unwrapped. */
18107c478bd9Sstevel@tonic-gate rv = soft_asn1_to_object(new_objp, plain_data, plain_len);
18117c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
18127c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate /* If it needs to be persistent, write it to the keystore */
18167c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(new_objp)) {
18177c478bd9Sstevel@tonic-gate persistent = B_TRUE;
18187c478bd9Sstevel@tonic-gate rv = soft_put_object_to_keystore(new_objp);
18197c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
18207c478bd9Sstevel@tonic-gate goto cleanup_unwrap;
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate
18237c478bd9Sstevel@tonic-gate if (new_objp->class != CKO_SECRET_KEY) {
18247c478bd9Sstevel@tonic-gate /* Clear buffer before returning to memory pool. */
18257c478bd9Sstevel@tonic-gate (void) memset(plain_data, 0x0, plain_len);
18267c478bd9Sstevel@tonic-gate free(plain_data);
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate
18297c478bd9Sstevel@tonic-gate *phKey = (CK_OBJECT_HANDLE)new_objp;
18307c478bd9Sstevel@tonic-gate
18317c478bd9Sstevel@tonic-gate return (CKR_OK);
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate cleanup_unwrap:
18347c478bd9Sstevel@tonic-gate /* The decrypted private key buffer must be freed explicitly. */
18357c478bd9Sstevel@tonic-gate if ((new_objp->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
18367c478bd9Sstevel@tonic-gate /* Clear buffer before returning to memory pool. */
18377c478bd9Sstevel@tonic-gate (void) memset(plain_data, 0x0, plain_len);
18387c478bd9Sstevel@tonic-gate free(plain_data);
18397c478bd9Sstevel@tonic-gate }
18407c478bd9Sstevel@tonic-gate
18417c478bd9Sstevel@tonic-gate /* sck and new_objp are indirectly free()d inside these functions */
18427c478bd9Sstevel@tonic-gate if (IS_TOKEN_OBJECT(new_objp))
18437c478bd9Sstevel@tonic-gate soft_delete_token_object(new_objp, persistent, B_FALSE);
18447c478bd9Sstevel@tonic-gate else
18451f49a79aSZdenek Kotala soft_delete_object(session_p, new_objp, B_FALSE, B_FALSE);
18467c478bd9Sstevel@tonic-gate
18477c478bd9Sstevel@tonic-gate return (rv);
18487c478bd9Sstevel@tonic-gate }
1849