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
590e0e8c4Sizick  * Common Development and Distribution License (the "License").
690e0e8c4Sizick  * 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 /*
22d288ba74SAnthony Scarpino  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2433f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
25*a8793c76SJason King  * Copyright (c) 2018, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
3123c57df7Smcpowers #include <sys/crypto/common.h>
327c478bd9Sstevel@tonic-gate #include <arcfour.h>
337c478bd9Sstevel@tonic-gate #include <aes_impl.h>
34f66d273dSizick #include <blowfish_impl.h>
357c478bd9Sstevel@tonic-gate #include <bignum.h>
367c478bd9Sstevel@tonic-gate #include <des_impl.h>
377c478bd9Sstevel@tonic-gate #include <rsa_impl.h>
387c478bd9Sstevel@tonic-gate #include "softGlobal.h"
397c478bd9Sstevel@tonic-gate #include "softObject.h"
407c478bd9Sstevel@tonic-gate #include "softSession.h"
417c478bd9Sstevel@tonic-gate #include "softKeystore.h"
427c478bd9Sstevel@tonic-gate #include "softKeystoreUtil.h"
43f66d273dSizick #include "softCrypt.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * This attribute table is used by the soft_lookup_attr()
487c478bd9Sstevel@tonic-gate  * to validate the attributes.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE attr_map[] = {
517c478bd9Sstevel@tonic-gate 	CKA_PRIVATE,
527c478bd9Sstevel@tonic-gate 	CKA_LABEL,
537c478bd9Sstevel@tonic-gate 	CKA_APPLICATION,
547c478bd9Sstevel@tonic-gate 	CKA_OBJECT_ID,
557c478bd9Sstevel@tonic-gate 	CKA_CERTIFICATE_TYPE,
567c478bd9Sstevel@tonic-gate 	CKA_ISSUER,
577c478bd9Sstevel@tonic-gate 	CKA_SERIAL_NUMBER,
587c478bd9Sstevel@tonic-gate 	CKA_AC_ISSUER,
597c478bd9Sstevel@tonic-gate 	CKA_OWNER,
607c478bd9Sstevel@tonic-gate 	CKA_ATTR_TYPES,
617c478bd9Sstevel@tonic-gate 	CKA_SUBJECT,
627c478bd9Sstevel@tonic-gate 	CKA_ID,
637c478bd9Sstevel@tonic-gate 	CKA_SENSITIVE,
647c478bd9Sstevel@tonic-gate 	CKA_START_DATE,
657c478bd9Sstevel@tonic-gate 	CKA_END_DATE,
667c478bd9Sstevel@tonic-gate 	CKA_MODULUS,
677c478bd9Sstevel@tonic-gate 	CKA_MODULUS_BITS,
687c478bd9Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
697c478bd9Sstevel@tonic-gate 	CKA_PRIVATE_EXPONENT,
707c478bd9Sstevel@tonic-gate 	CKA_PRIME_1,
717c478bd9Sstevel@tonic-gate 	CKA_PRIME_2,
727c478bd9Sstevel@tonic-gate 	CKA_EXPONENT_1,
737c478bd9Sstevel@tonic-gate 	CKA_EXPONENT_2,
747c478bd9Sstevel@tonic-gate 	CKA_COEFFICIENT,
757c478bd9Sstevel@tonic-gate 	CKA_PRIME,
767c478bd9Sstevel@tonic-gate 	CKA_SUBPRIME,
777c478bd9Sstevel@tonic-gate 	CKA_BASE,
787c478bd9Sstevel@tonic-gate 	CKA_EXTRACTABLE,
797c478bd9Sstevel@tonic-gate 	CKA_LOCAL,
807c478bd9Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
817c478bd9Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE,
827c478bd9Sstevel@tonic-gate 	CKA_MODIFIABLE,
837c478bd9Sstevel@tonic-gate 	CKA_ECDSA_PARAMS,
84034448feSmcpowers 	CKA_EC_PARAMS,
857c478bd9Sstevel@tonic-gate 	CKA_EC_POINT,
867c478bd9Sstevel@tonic-gate 	CKA_SECONDARY_AUTH,
877c478bd9Sstevel@tonic-gate 	CKA_AUTH_PIN_FLAGS,
887c478bd9Sstevel@tonic-gate 	CKA_HW_FEATURE_TYPE,
897c478bd9Sstevel@tonic-gate 	CKA_RESET_ON_INIT,
907c478bd9Sstevel@tonic-gate 	CKA_HAS_RESET
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * attributes that exists only in public key objects
957c478bd9Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
967c478bd9Sstevel@tonic-gate  *       other object classes, but they are also listed
977c478bd9Sstevel@tonic-gate  *       because not all object have them.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE PUB_KEY_ATTRS[] =
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	CKA_SUBJECT,
1027c478bd9Sstevel@tonic-gate 	CKA_ENCRYPT,
1037c478bd9Sstevel@tonic-gate 	CKA_WRAP,
1047c478bd9Sstevel@tonic-gate 	CKA_VERIFY,
1057c478bd9Sstevel@tonic-gate 	CKA_VERIFY_RECOVER,
1067c478bd9Sstevel@tonic-gate 	CKA_MODULUS,
1077c478bd9Sstevel@tonic-gate 	CKA_MODULUS_BITS,
1087c478bd9Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
1097c478bd9Sstevel@tonic-gate 	CKA_PRIME,
1107c478bd9Sstevel@tonic-gate 	CKA_SUBPRIME,
1117c478bd9Sstevel@tonic-gate 	CKA_BASE,
1127c478bd9Sstevel@tonic-gate 	CKA_TRUSTED,
1137c478bd9Sstevel@tonic-gate 	CKA_ECDSA_PARAMS,
1147c478bd9Sstevel@tonic-gate 	CKA_EC_PARAMS,
1157c478bd9Sstevel@tonic-gate 	CKA_EC_POINT
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * attributes that exists only in private key objects
1207c478bd9Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1217c478bd9Sstevel@tonic-gate  *       other object classes, but they are also listed
1227c478bd9Sstevel@tonic-gate  *       because not all object have them.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE PRIV_KEY_ATTRS[] =
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	CKA_DECRYPT,
1277c478bd9Sstevel@tonic-gate 	CKA_UNWRAP,
1287c478bd9Sstevel@tonic-gate 	CKA_SIGN,
1297c478bd9Sstevel@tonic-gate 	CKA_SIGN_RECOVER,
1307c478bd9Sstevel@tonic-gate 	CKA_MODULUS,
1317c478bd9Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
1327c478bd9Sstevel@tonic-gate 	CKA_PRIVATE_EXPONENT,
1337c478bd9Sstevel@tonic-gate 	CKA_PRIME,
1347c478bd9Sstevel@tonic-gate 	CKA_SUBPRIME,
1357c478bd9Sstevel@tonic-gate 	CKA_BASE,
1367c478bd9Sstevel@tonic-gate 	CKA_PRIME_1,
1377c478bd9Sstevel@tonic-gate 	CKA_PRIME_2,
1387c478bd9Sstevel@tonic-gate 	CKA_EXPONENT_1,
1397c478bd9Sstevel@tonic-gate 	CKA_EXPONENT_2,
1407c478bd9Sstevel@tonic-gate 	CKA_COEFFICIENT,
1417c478bd9Sstevel@tonic-gate 	CKA_VALUE_BITS,
1427c478bd9Sstevel@tonic-gate 	CKA_SUBJECT,
1437c478bd9Sstevel@tonic-gate 	CKA_SENSITIVE,
1447c478bd9Sstevel@tonic-gate 	CKA_EXTRACTABLE,
1457c478bd9Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
1467c478bd9Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE,
1477c478bd9Sstevel@tonic-gate 	CKA_EC_PARAMS
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * attributes that exists only in secret key objects
1527c478bd9Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1537c478bd9Sstevel@tonic-gate  *       other object classes, but they are also listed
1547c478bd9Sstevel@tonic-gate  *       because not all object have them.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE SECRET_KEY_ATTRS[] =
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	CKA_VALUE_LEN,
1597c478bd9Sstevel@tonic-gate 	CKA_ENCRYPT,
1607c478bd9Sstevel@tonic-gate 	CKA_DECRYPT,
1617c478bd9Sstevel@tonic-gate 	CKA_WRAP,
1627c478bd9Sstevel@tonic-gate 	CKA_UNWRAP,
1637c478bd9Sstevel@tonic-gate 	CKA_SIGN,
1647c478bd9Sstevel@tonic-gate 	CKA_VERIFY,
1657c478bd9Sstevel@tonic-gate 	CKA_SENSITIVE,
1667c478bd9Sstevel@tonic-gate 	CKA_EXTRACTABLE,
1677c478bd9Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
1687c478bd9Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * attributes that exists only in domain parameter objects
1737c478bd9Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1747c478bd9Sstevel@tonic-gate  *       other object classes, but they are also listed
1757c478bd9Sstevel@tonic-gate  *       because not all object have them.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE DOMAIN_ATTRS[] =
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	CKA_PRIME,
1807c478bd9Sstevel@tonic-gate 	CKA_SUBPRIME,
1817c478bd9Sstevel@tonic-gate 	CKA_BASE,
1827c478bd9Sstevel@tonic-gate 	CKA_PRIME_BITS,
1837c478bd9Sstevel@tonic-gate 	CKA_SUBPRIME_BITS,
1847c478bd9Sstevel@tonic-gate 	CKA_SUB_PRIME_BITS
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * attributes that exists only in hardware feature objects
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE HARDWARE_ATTRS[] =
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	CKA_HW_FEATURE_TYPE,
1947c478bd9Sstevel@tonic-gate 	CKA_RESET_ON_INIT,
1957c478bd9Sstevel@tonic-gate 	CKA_HAS_RESET
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * attributes that exists only in certificate objects
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_TYPE CERT_ATTRS[] =
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	CKA_CERTIFICATE_TYPE,
2047c478bd9Sstevel@tonic-gate 	CKA_TRUSTED,
2057c478bd9Sstevel@tonic-gate 	CKA_SUBJECT,
2067c478bd9Sstevel@tonic-gate 	CKA_ID,
2077c478bd9Sstevel@tonic-gate 	CKA_ISSUER,
2087c478bd9Sstevel@tonic-gate 	CKA_AC_ISSUER,
2097c478bd9Sstevel@tonic-gate 	CKA_SERIAL_NUMBER,
2107c478bd9Sstevel@tonic-gate 	CKA_OWNER,
2117c478bd9Sstevel@tonic-gate 	CKA_ATTR_TYPES
2127c478bd9Sstevel@tonic-gate };
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Validate the attribute by using binary search algorithm.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate CK_RV
soft_lookup_attr(CK_ATTRIBUTE_TYPE type)2197c478bd9Sstevel@tonic-gate soft_lookup_attr(CK_ATTRIBUTE_TYPE type)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	size_t lower, middle, upper;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	lower = 0;
2257c478bd9Sstevel@tonic-gate 	upper = (sizeof (attr_map) / sizeof (CK_ATTRIBUTE_TYPE)) - 1;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	while (lower <= upper) {
2287c478bd9Sstevel@tonic-gate 		/* Always starts from middle. */
2297c478bd9Sstevel@tonic-gate 		middle = (lower + upper) / 2;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (type > attr_map[middle]) {
2327c478bd9Sstevel@tonic-gate 			/* Adjust the lower bound to upper half. */
2337c478bd9Sstevel@tonic-gate 			lower = middle + 1;
2347c478bd9Sstevel@tonic-gate 			continue;
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		if (type == attr_map[middle]) {
2387c478bd9Sstevel@tonic-gate 			/* Found it. */
2397c478bd9Sstevel@tonic-gate 			return (CKR_OK);
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if (type < attr_map[middle]) {
2437c478bd9Sstevel@tonic-gate 			/* Adjust the upper bound to lower half. */
2447c478bd9Sstevel@tonic-gate 			upper = middle - 1;
2457c478bd9Sstevel@tonic-gate 			continue;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* Failed to find the matching attribute from the attribute table. */
2507c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * Validate the attribute by using the following search algorithm:
2567c478bd9Sstevel@tonic-gate  *
2577c478bd9Sstevel@tonic-gate  * 1) Search for the most frequently used attributes first.
2587c478bd9Sstevel@tonic-gate  * 2) If not found, search for the usage-purpose attributes - these
2597c478bd9Sstevel@tonic-gate  *    attributes have dense set of values, therefore compiler will
2607c478bd9Sstevel@tonic-gate  *    optimize it with a branch table and branch to the appropriate
2617c478bd9Sstevel@tonic-gate  *    case.
2627c478bd9Sstevel@tonic-gate  * 3) If still not found, use binary search for the rest of the
2637c478bd9Sstevel@tonic-gate  *    attributes in the attr_map[] table.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate CK_RV
soft_validate_attr(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,CK_OBJECT_CLASS * class)2667c478bd9Sstevel@tonic-gate soft_validate_attr(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
267*a8793c76SJason King     CK_OBJECT_CLASS *class)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	CK_ULONG i;
2717c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
2747c478bd9Sstevel@tonic-gate 		/* First tier search */
2757c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
2767c478bd9Sstevel@tonic-gate 		case CKA_CLASS:
2777c478bd9Sstevel@tonic-gate 			*class = *((CK_OBJECT_CLASS*)template[i].pValue);
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case CKA_TOKEN:
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 		case CKA_VALUE_LEN:
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case CKA_VALUE_BITS:
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 		default:
2907c478bd9Sstevel@tonic-gate 			/* Second tier search */
2917c478bd9Sstevel@tonic-gate 			switch (template[i].type) {
2927c478bd9Sstevel@tonic-gate 			case CKA_ENCRYPT:
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			case CKA_DECRYPT:
2957c478bd9Sstevel@tonic-gate 				break;
2967c478bd9Sstevel@tonic-gate 			case CKA_WRAP:
2977c478bd9Sstevel@tonic-gate 				break;
2987c478bd9Sstevel@tonic-gate 			case CKA_UNWRAP:
2997c478bd9Sstevel@tonic-gate 				break;
3007c478bd9Sstevel@tonic-gate 			case CKA_SIGN:
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			case CKA_SIGN_RECOVER:
3037c478bd9Sstevel@tonic-gate 				break;
3047c478bd9Sstevel@tonic-gate 			case CKA_VERIFY:
3057c478bd9Sstevel@tonic-gate 				break;
3067c478bd9Sstevel@tonic-gate 			case CKA_VERIFY_RECOVER:
3077c478bd9Sstevel@tonic-gate 				break;
3087c478bd9Sstevel@tonic-gate 			case CKA_DERIVE:
3097c478bd9Sstevel@tonic-gate 				break;
3107c478bd9Sstevel@tonic-gate 			default:
3117c478bd9Sstevel@tonic-gate 				/* Third tier search */
3127c478bd9Sstevel@tonic-gate 				rv = soft_lookup_attr(template[i].type);
3137c478bd9Sstevel@tonic-gate 				if (rv != CKR_OK)
3147c478bd9Sstevel@tonic-gate 					return (rv);
3157c478bd9Sstevel@tonic-gate 				break;
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	return (rv);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static void
cleanup_cert_attr(cert_attr_t * attr)3247c478bd9Sstevel@tonic-gate cleanup_cert_attr(cert_attr_t *attr)
3257c478bd9Sstevel@tonic-gate {
326*a8793c76SJason King 	if (attr != NULL) {
327*a8793c76SJason King 		freezero(attr->value, attr->length);
3287c478bd9Sstevel@tonic-gate 		attr->value = NULL;
3297c478bd9Sstevel@tonic-gate 		attr->length = 0;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static CK_RV
copy_cert_attr(cert_attr_t * src_attr,cert_attr_t ** dest_attr)3347c478bd9Sstevel@tonic-gate copy_cert_attr(cert_attr_t *src_attr, cert_attr_t **dest_attr)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (src_attr == NULL || dest_attr == NULL)
3397c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (src_attr->value == NULL)
3427c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* free memory if its already allocated */
3457c478bd9Sstevel@tonic-gate 	if (*dest_attr != NULL) {
346*a8793c76SJason King 		cleanup_cert_attr(*dest_attr);
3477c478bd9Sstevel@tonic-gate 	} else {
3487c478bd9Sstevel@tonic-gate 		*dest_attr = malloc(sizeof (cert_attr_t));
3497c478bd9Sstevel@tonic-gate 		if (*dest_attr == NULL)
3507c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	(*dest_attr)->value = NULL;
3547c478bd9Sstevel@tonic-gate 	(*dest_attr)->length = 0;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (src_attr->length) {
3577c478bd9Sstevel@tonic-gate 		(*dest_attr)->value = malloc(src_attr->length);
3587c478bd9Sstevel@tonic-gate 		if ((*dest_attr)->value == NULL) {
3597c478bd9Sstevel@tonic-gate 			free(*dest_attr);
3607c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		(void) memcpy((*dest_attr)->value, src_attr->value,
364f9fbec18Smcpowers 		    src_attr->length);
3657c478bd9Sstevel@tonic-gate 		(*dest_attr)->length = src_attr->length;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (rv);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate void
soft_cleanup_cert_object(soft_object_t * object_p)3727c478bd9Sstevel@tonic-gate soft_cleanup_cert_object(soft_object_t *object_p)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	CK_CERTIFICATE_TYPE certtype = object_p->cert_type;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (object_p->class != CKO_CERTIFICATE ||
3777c478bd9Sstevel@tonic-gate 	    OBJ_CERT(object_p) == NULL)
3787c478bd9Sstevel@tonic-gate 		return;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (certtype == CKC_X_509) {
3817c478bd9Sstevel@tonic-gate 		if (X509_CERT_SUBJECT(object_p) != NULL) {
3827c478bd9Sstevel@tonic-gate 			cleanup_cert_attr(X509_CERT_SUBJECT(object_p));
3837c478bd9Sstevel@tonic-gate 			free(X509_CERT_SUBJECT(object_p));
3847c478bd9Sstevel@tonic-gate 			X509_CERT_SUBJECT(object_p) = NULL;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		if (X509_CERT_VALUE(object_p) != NULL) {
3877c478bd9Sstevel@tonic-gate 			cleanup_cert_attr(X509_CERT_VALUE(object_p));
3887c478bd9Sstevel@tonic-gate 			free(X509_CERT_VALUE(object_p));
3897c478bd9Sstevel@tonic-gate 			X509_CERT_VALUE(object_p) = NULL;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		free(OBJ_CERT(object_p));
3927c478bd9Sstevel@tonic-gate 	} else if (certtype == CKC_X_509_ATTR_CERT) {
3937c478bd9Sstevel@tonic-gate 		if (X509_ATTR_CERT_VALUE(object_p) != NULL) {
3947c478bd9Sstevel@tonic-gate 			cleanup_cert_attr(X509_ATTR_CERT_VALUE(object_p));
3957c478bd9Sstevel@tonic-gate 			free(X509_ATTR_CERT_VALUE(object_p));
3967c478bd9Sstevel@tonic-gate 			X509_ATTR_CERT_VALUE(object_p) = NULL;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		if (X509_ATTR_CERT_OWNER(object_p) != NULL) {
3997c478bd9Sstevel@tonic-gate 			cleanup_cert_attr(X509_ATTR_CERT_OWNER(object_p));
4007c478bd9Sstevel@tonic-gate 			free(X509_ATTR_CERT_OWNER(object_p));
4017c478bd9Sstevel@tonic-gate 			X509_ATTR_CERT_OWNER(object_p) = NULL;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		free(OBJ_CERT(object_p));
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * Clean up and release all the storage in the extra attribute list
4097c478bd9Sstevel@tonic-gate  * of an object.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate void
soft_cleanup_extra_attr(soft_object_t * object_p)4127c478bd9Sstevel@tonic-gate soft_cleanup_extra_attr(soft_object_t *object_p)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
4167c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR tmp;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
4197c478bd9Sstevel@tonic-gate 	while (extra_attr) {
4207c478bd9Sstevel@tonic-gate 		tmp = extra_attr->next;
421*a8793c76SJason King 		if (extra_attr->attr.pValue != NULL) {
4227c478bd9Sstevel@tonic-gate 			/*
4237c478bd9Sstevel@tonic-gate 			 * All extra attributes in the extra attribute
4247c478bd9Sstevel@tonic-gate 			 * list have pValue points to the value of the
4257c478bd9Sstevel@tonic-gate 			 * attribute (with simple byte array type).
4267c478bd9Sstevel@tonic-gate 			 * Free the storage for the value of the attribute.
4277c478bd9Sstevel@tonic-gate 			 */
428*a8793c76SJason King 			freezero(extra_attr->attr.pValue,
429*a8793c76SJason King 			    extra_attr->attr.ulValueLen);
430*a8793c76SJason King 		}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		/* Free the storage for the attribute_info struct. */
4337c478bd9Sstevel@tonic-gate 		free(extra_attr);
4347c478bd9Sstevel@tonic-gate 		extra_attr = tmp;
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	object_p->extra_attrlistp = NULL;
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Create the attribute_info struct to hold the object's attribute,
4437c478bd9Sstevel@tonic-gate  * and add it to the extra attribute list of an object.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate CK_RV
soft_add_extra_attr(CK_ATTRIBUTE_PTR template,soft_object_t * object_p)4467c478bd9Sstevel@tonic-gate soft_add_extra_attr(CK_ATTRIBUTE_PTR template, soft_object_t *object_p)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR attrp;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/* Allocate the storage for the attribute_info struct. */
4527c478bd9Sstevel@tonic-gate 	attrp = calloc(1, sizeof (attribute_info_t));
4537c478bd9Sstevel@tonic-gate 	if (attrp == NULL) {
4547c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* Set up attribute_info struct. */
4587c478bd9Sstevel@tonic-gate 	attrp->attr.type = template->type;
4597c478bd9Sstevel@tonic-gate 	attrp->attr.ulValueLen = template->ulValueLen;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
4627c478bd9Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
4637c478bd9Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
4647c478bd9Sstevel@tonic-gate 		attrp->attr.pValue = malloc(template->ulValueLen);
4657c478bd9Sstevel@tonic-gate 		if (attrp->attr.pValue == NULL) {
4667c478bd9Sstevel@tonic-gate 			free(attrp);
4677c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		(void) memcpy(attrp->attr.pValue, template->pValue,
4717c478bd9Sstevel@tonic-gate 		    template->ulValueLen);
4727c478bd9Sstevel@tonic-gate 	} else {
4737c478bd9Sstevel@tonic-gate 		attrp->attr.pValue = NULL;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/* Insert the new attribute in front of extra attribute list. */
4777c478bd9Sstevel@tonic-gate 	if (object_p->extra_attrlistp == NULL) {
4787c478bd9Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
4797c478bd9Sstevel@tonic-gate 		attrp->next = NULL;
4807c478bd9Sstevel@tonic-gate 	} else {
4817c478bd9Sstevel@tonic-gate 		attrp->next = object_p->extra_attrlistp;
4827c478bd9Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	return (CKR_OK);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate CK_RV
soft_copy_certificate(certificate_obj_t * oldcert,certificate_obj_t ** newcert,CK_CERTIFICATE_TYPE type)4897c478bd9Sstevel@tonic-gate soft_copy_certificate(certificate_obj_t *oldcert, certificate_obj_t **newcert,
490*a8793c76SJason King     CK_CERTIFICATE_TYPE type)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
4937c478bd9Sstevel@tonic-gate 	certificate_obj_t	*cert;
4947c478bd9Sstevel@tonic-gate 	x509_cert_t		x509;
4957c478bd9Sstevel@tonic-gate 	x509_attr_cert_t	x509_attr;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	cert = calloc(1, sizeof (certificate_obj_t));
4987c478bd9Sstevel@tonic-gate 	if (cert == NULL) {
4997c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (type == CKC_X_509) {
5037c478bd9Sstevel@tonic-gate 		x509 = oldcert->cert_type_u.x509;
5047c478bd9Sstevel@tonic-gate 		if (x509.subject)
5057c478bd9Sstevel@tonic-gate 			if ((rv = copy_cert_attr(x509.subject,
506f9fbec18Smcpowers 			    &cert->cert_type_u.x509.subject)))
5077c478bd9Sstevel@tonic-gate 				return (rv);
5087c478bd9Sstevel@tonic-gate 		if (x509.value)
5097c478bd9Sstevel@tonic-gate 			if ((rv = copy_cert_attr(x509.value,
510f9fbec18Smcpowers 			    &cert->cert_type_u.x509.value)))
5117c478bd9Sstevel@tonic-gate 				return (rv);
5127c478bd9Sstevel@tonic-gate 	} else if (type == CKC_X_509_ATTR_CERT) {
5137c478bd9Sstevel@tonic-gate 		x509_attr = oldcert->cert_type_u.x509_attr;
5147c478bd9Sstevel@tonic-gate 		if (x509_attr.owner)
5157c478bd9Sstevel@tonic-gate 			if ((rv = copy_cert_attr(x509_attr.owner,
516f9fbec18Smcpowers 			    &cert->cert_type_u.x509_attr.owner)))
5177c478bd9Sstevel@tonic-gate 				return (rv);
5187c478bd9Sstevel@tonic-gate 		if (x509_attr.value)
5197c478bd9Sstevel@tonic-gate 			if ((rv = copy_cert_attr(x509_attr.value,
520f9fbec18Smcpowers 			    &cert->cert_type_u.x509_attr.value)))
5217c478bd9Sstevel@tonic-gate 				return (rv);
5227c478bd9Sstevel@tonic-gate 	} else {
5237c478bd9Sstevel@tonic-gate 		/* wrong certificate type */
5247c478bd9Sstevel@tonic-gate 		rv = CKR_ATTRIBUTE_TYPE_INVALID;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK)
5277c478bd9Sstevel@tonic-gate 		*newcert = cert;
5287c478bd9Sstevel@tonic-gate 	return (rv);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Copy the attribute_info struct from the old object to a new attribute_info
5337c478bd9Sstevel@tonic-gate  * struct, and add that new struct to the extra attribute list of the new
5347c478bd9Sstevel@tonic-gate  * object.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate CK_RV
soft_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp,soft_object_t * object_p)5377c478bd9Sstevel@tonic-gate soft_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp, soft_object_t *object_p)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR attrp;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* Allocate attribute_info struct. */
5427c478bd9Sstevel@tonic-gate 	attrp = calloc(1, sizeof (attribute_info_t));
5437c478bd9Sstevel@tonic-gate 	if (attrp == NULL) {
5447c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	attrp->attr.type = old_attrp->attr.type;
5487c478bd9Sstevel@tonic-gate 	attrp->attr.ulValueLen = old_attrp->attr.ulValueLen;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if ((old_attrp->attr.pValue != NULL) &&
551f9fbec18Smcpowers 	    (old_attrp->attr.ulValueLen > 0)) {
5527c478bd9Sstevel@tonic-gate 		attrp->attr.pValue = malloc(old_attrp->attr.ulValueLen);
5537c478bd9Sstevel@tonic-gate 		if (attrp->attr.pValue == NULL) {
5547c478bd9Sstevel@tonic-gate 			free(attrp);
5557c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		(void) memcpy(attrp->attr.pValue, old_attrp->attr.pValue,
5597c478bd9Sstevel@tonic-gate 		    old_attrp->attr.ulValueLen);
5607c478bd9Sstevel@tonic-gate 	} else {
5617c478bd9Sstevel@tonic-gate 		attrp->attr.pValue = NULL;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* Insert the new attribute in front of extra attribute list */
5657c478bd9Sstevel@tonic-gate 	if (object_p->extra_attrlistp == NULL) {
5667c478bd9Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
5677c478bd9Sstevel@tonic-gate 		attrp->next = NULL;
5687c478bd9Sstevel@tonic-gate 	} else {
5697c478bd9Sstevel@tonic-gate 		attrp->next = object_p->extra_attrlistp;
5707c478bd9Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	return (CKR_OK);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * Get the attribute triple from the extra attribute list in the object
5797c478bd9Sstevel@tonic-gate  * (if the specified attribute type is found), and copy it to a template.
5807c478bd9Sstevel@tonic-gate  * Note the type of the attribute to be copied is specified by the template,
5817c478bd9Sstevel@tonic-gate  * and the storage is pre-allocated for the atrribute value in the template
5827c478bd9Sstevel@tonic-gate  * for doing the copy.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate CK_RV
get_extra_attr_from_object(soft_object_t * object_p,CK_ATTRIBUTE_PTR template)5857c478bd9Sstevel@tonic-gate get_extra_attr_from_object(soft_object_t *object_p, CK_ATTRIBUTE_PTR template)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
5897c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_TYPE type = template->type;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	while (extra_attr) {
5947c478bd9Sstevel@tonic-gate 		if (type == extra_attr->attr.type) {
5957c478bd9Sstevel@tonic-gate 			/* Found it. */
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		} else {
5987c478bd9Sstevel@tonic-gate 			/* Does not match, try next one. */
5997c478bd9Sstevel@tonic-gate 			extra_attr = extra_attr->next;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (extra_attr == NULL) {
6047c478bd9Sstevel@tonic-gate 		/* A valid but un-initialized attribute. */
6057c478bd9Sstevel@tonic-gate 		template->ulValueLen = 0;
6067c478bd9Sstevel@tonic-gate 		return (CKR_OK);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * We found the attribute in the extra attribute list.
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	if (template->pValue == NULL) {
6137c478bd9Sstevel@tonic-gate 		template->ulValueLen = extra_attr->attr.ulValueLen;
6147c478bd9Sstevel@tonic-gate 		return (CKR_OK);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (template->ulValueLen >= extra_attr->attr.ulValueLen) {
6187c478bd9Sstevel@tonic-gate 		/*
6197c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application is large
6207c478bd9Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
6217c478bd9Sstevel@tonic-gate 		 */
6227c478bd9Sstevel@tonic-gate 		(void) memcpy(template->pValue, extra_attr->attr.pValue,
6237c478bd9Sstevel@tonic-gate 		    extra_attr->attr.ulValueLen);
6247c478bd9Sstevel@tonic-gate 		template->ulValueLen = extra_attr->attr.ulValueLen;
6257c478bd9Sstevel@tonic-gate 		return (CKR_OK);
6267c478bd9Sstevel@tonic-gate 	} else {
6277c478bd9Sstevel@tonic-gate 		/*
6287c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application does
6297c478bd9Sstevel@tonic-gate 		 * not have enough space to hold the value.
6307c478bd9Sstevel@tonic-gate 		 */
6317c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
6327c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Modify the attribute triple in the extra attribute list of the object
6397c478bd9Sstevel@tonic-gate  * if the specified attribute type is found. Otherwise, just add it to
6407c478bd9Sstevel@tonic-gate  * list.
6417c478bd9Sstevel@tonic-gate  */
6427c478bd9Sstevel@tonic-gate CK_RV
set_extra_attr_to_object(soft_object_t * object_p,CK_ATTRIBUTE_TYPE type,CK_ATTRIBUTE_PTR template)6437c478bd9Sstevel@tonic-gate set_extra_attr_to_object(soft_object_t *object_p, CK_ATTRIBUTE_TYPE type,
644*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	while (extra_attr) {
6527c478bd9Sstevel@tonic-gate 		if (type == extra_attr->attr.type) {
6537c478bd9Sstevel@tonic-gate 			/* Found it. */
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		} else {
6567c478bd9Sstevel@tonic-gate 			/* Does not match, try next one. */
6577c478bd9Sstevel@tonic-gate 			extra_attr = extra_attr->next;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (extra_attr == NULL) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * This attribute is a new one, go ahead adding it to
6647c478bd9Sstevel@tonic-gate 		 * the extra attribute list.
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		return (soft_add_extra_attr(template, object_p));
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* We found the attribute in the extra attribute list. */
6707c478bd9Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
6717c478bd9Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
6727c478bd9Sstevel@tonic-gate 		if (template->ulValueLen > extra_attr->attr.ulValueLen) {
6737c478bd9Sstevel@tonic-gate 			/* The old buffer is too small to hold the new value. */
674*a8793c76SJason King 			if (extra_attr->attr.pValue != NULL) {
6757c478bd9Sstevel@tonic-gate 				/* Free storage for the old attribute value. */
676*a8793c76SJason King 				freezero(extra_attr->attr.pValue,
677*a8793c76SJason King 				    extra_attr->attr.ulValueLen);
678*a8793c76SJason King 			}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			/* Allocate storage for the new attribute value. */
6817c478bd9Sstevel@tonic-gate 			extra_attr->attr.pValue = malloc(template->ulValueLen);
6827c478bd9Sstevel@tonic-gate 			if (extra_attr->attr.pValue == NULL) {
6837c478bd9Sstevel@tonic-gate 				return (CKR_HOST_MEMORY);
6847c478bd9Sstevel@tonic-gate 			}
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/* Replace the attribute with new value. */
6887c478bd9Sstevel@tonic-gate 		extra_attr->attr.ulValueLen = template->ulValueLen;
6897c478bd9Sstevel@tonic-gate 		(void) memcpy(extra_attr->attr.pValue, template->pValue,
6907c478bd9Sstevel@tonic-gate 		    template->ulValueLen);
6917c478bd9Sstevel@tonic-gate 	} else {
6927c478bd9Sstevel@tonic-gate 		extra_attr->attr.pValue = NULL;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	return (CKR_OK);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * Copy the big integer attribute value from template to a biginteger_t struct.
7017c478bd9Sstevel@tonic-gate  */
7027c478bd9Sstevel@tonic-gate CK_RV
get_bigint_attr_from_template(biginteger_t * big,CK_ATTRIBUTE_PTR template)7037c478bd9Sstevel@tonic-gate get_bigint_attr_from_template(biginteger_t *big, CK_ATTRIBUTE_PTR template)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
7077c478bd9Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
7087c478bd9Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
7097c478bd9Sstevel@tonic-gate 		big->big_value = malloc(template->ulValueLen);
7107c478bd9Sstevel@tonic-gate 		if (big->big_value == NULL) {
7117c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		(void) memcpy(big->big_value, template->pValue,
7157c478bd9Sstevel@tonic-gate 		    template->ulValueLen);
7167c478bd9Sstevel@tonic-gate 		big->big_value_len = template->ulValueLen;
7177c478bd9Sstevel@tonic-gate 	} else {
7187c478bd9Sstevel@tonic-gate 		big->big_value = NULL;
7197c478bd9Sstevel@tonic-gate 		big->big_value_len = 0;
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	return (CKR_OK);
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Copy the big integer attribute value from a biginteger_t struct in the
7287c478bd9Sstevel@tonic-gate  * object to a template.
7297c478bd9Sstevel@tonic-gate  */
7307c478bd9Sstevel@tonic-gate CK_RV
get_bigint_attr_from_object(biginteger_t * big,CK_ATTRIBUTE_PTR template)7317c478bd9Sstevel@tonic-gate get_bigint_attr_from_object(biginteger_t *big, CK_ATTRIBUTE_PTR template)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (template->pValue == NULL) {
7357c478bd9Sstevel@tonic-gate 		template->ulValueLen = big->big_value_len;
7367c478bd9Sstevel@tonic-gate 		return (CKR_OK);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (big->big_value == NULL) {
7407c478bd9Sstevel@tonic-gate 		template->ulValueLen = 0;
7417c478bd9Sstevel@tonic-gate 		return (CKR_OK);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (template->ulValueLen >= big->big_value_len) {
7457c478bd9Sstevel@tonic-gate 		/*
7467c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application is large
7477c478bd9Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
7487c478bd9Sstevel@tonic-gate 		 */
7497c478bd9Sstevel@tonic-gate 		(void) memcpy(template->pValue, big->big_value,
7507c478bd9Sstevel@tonic-gate 		    big->big_value_len);
7517c478bd9Sstevel@tonic-gate 		template->ulValueLen = big->big_value_len;
7527c478bd9Sstevel@tonic-gate 		return (CKR_OK);
7537c478bd9Sstevel@tonic-gate 	} else {
7547c478bd9Sstevel@tonic-gate 		/*
7557c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application does
7567c478bd9Sstevel@tonic-gate 		 * not have enough space to hold the value.
7577c478bd9Sstevel@tonic-gate 		 */
7587c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
7597c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * Copy the boolean data type attribute value from an object for the
7667c478bd9Sstevel@tonic-gate  * specified attribute to the template.
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate CK_RV
get_bool_attr_from_object(soft_object_t * object_p,CK_ULONG bool_flag,CK_ATTRIBUTE_PTR template)7697c478bd9Sstevel@tonic-gate get_bool_attr_from_object(soft_object_t *object_p, CK_ULONG bool_flag,
770*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (template->pValue == NULL) {
7747c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
7757c478bd9Sstevel@tonic-gate 		return (CKR_OK);
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	if (template->ulValueLen >= sizeof (CK_BBOOL)) {
7797c478bd9Sstevel@tonic-gate 		/*
7807c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application is large
7817c478bd9Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
7827c478bd9Sstevel@tonic-gate 		 */
7837c478bd9Sstevel@tonic-gate 		if (object_p->bool_attr_mask & bool_flag) {
7847c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
7857c478bd9Sstevel@tonic-gate 		} else {
7867c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
7907c478bd9Sstevel@tonic-gate 		return (CKR_OK);
7917c478bd9Sstevel@tonic-gate 	} else {
7927c478bd9Sstevel@tonic-gate 		/*
7937c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application does
7947c478bd9Sstevel@tonic-gate 		 * not have enough space to hold the value.
7957c478bd9Sstevel@tonic-gate 		 */
7967c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
7977c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * Set the boolean data type attribute value in the object.
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate CK_RV
set_bool_attr_to_object(soft_object_t * object_p,CK_ULONG bool_flag,CK_ATTRIBUTE_PTR template)8057c478bd9Sstevel@tonic-gate set_bool_attr_to_object(soft_object_t *object_p, CK_ULONG bool_flag,
806*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (*(CK_BBOOL *)template->pValue)
8107c478bd9Sstevel@tonic-gate 		object_p->bool_attr_mask |= bool_flag;
8117c478bd9Sstevel@tonic-gate 	else
8127c478bd9Sstevel@tonic-gate 		object_p->bool_attr_mask &= ~bool_flag;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	return (CKR_OK);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Copy the CK_ULONG data type attribute value from an object to the
8207c478bd9Sstevel@tonic-gate  * template.
8217c478bd9Sstevel@tonic-gate  */
8227c478bd9Sstevel@tonic-gate CK_RV
get_ulong_attr_from_object(CK_ULONG value,CK_ATTRIBUTE_PTR template)8237c478bd9Sstevel@tonic-gate get_ulong_attr_from_object(CK_ULONG value, CK_ATTRIBUTE_PTR template)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (template->pValue == NULL) {
8277c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_ULONG);
8287c478bd9Sstevel@tonic-gate 		return (CKR_OK);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if (template->ulValueLen >= sizeof (CK_ULONG)) {
8327c478bd9Sstevel@tonic-gate 		/*
8337c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application is large
8347c478bd9Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
8357c478bd9Sstevel@tonic-gate 		 * It is also assumed to be correctly aligned.
8367c478bd9Sstevel@tonic-gate 		 */
8377c478bd9Sstevel@tonic-gate 		*(CK_ULONG_PTR)template->pValue = value;
8387c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_ULONG);
8397c478bd9Sstevel@tonic-gate 		return (CKR_OK);
8407c478bd9Sstevel@tonic-gate 	} else {
8417c478bd9Sstevel@tonic-gate 		/*
8427c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application does
8437c478bd9Sstevel@tonic-gate 		 * not have enough space to hold the value.
8447c478bd9Sstevel@tonic-gate 		 */
8457c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
8467c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate  * Copy the CK_ULONG data type attribute value from a template to the
8537c478bd9Sstevel@tonic-gate  * object.
8547c478bd9Sstevel@tonic-gate  */
8557739299dSAnthony Scarpino static CK_RV
get_ulong_attr_from_template(CK_ULONG * value,CK_ATTRIBUTE_PTR template)8567c478bd9Sstevel@tonic-gate get_ulong_attr_from_template(CK_ULONG *value, CK_ATTRIBUTE_PTR template)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 
8597739299dSAnthony Scarpino 	if (template->ulValueLen < sizeof (CK_ULONG))
8607739299dSAnthony Scarpino 		return (CKR_ATTRIBUTE_VALUE_INVALID);
8617739299dSAnthony Scarpino 
8627c478bd9Sstevel@tonic-gate 	if (template->pValue != NULL) {
8637c478bd9Sstevel@tonic-gate 		*value = *(CK_ULONG_PTR)template->pValue;
8647c478bd9Sstevel@tonic-gate 	} else {
8657c478bd9Sstevel@tonic-gate 		*value = 0;
8667c478bd9Sstevel@tonic-gate 	}
8677739299dSAnthony Scarpino 
8687739299dSAnthony Scarpino 	return (CKR_OK);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /*
8727c478bd9Sstevel@tonic-gate  * Copy the big integer attribute value from source's biginteger_t to
8737c478bd9Sstevel@tonic-gate  * destination's biginteger_t.
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate void
copy_bigint_attr(biginteger_t * src,biginteger_t * dst)8767c478bd9Sstevel@tonic-gate copy_bigint_attr(biginteger_t *src, biginteger_t *dst)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if ((src->big_value != NULL) &&
8807c478bd9Sstevel@tonic-gate 	    (src->big_value_len > 0)) {
8817c478bd9Sstevel@tonic-gate 		/*
8827c478bd9Sstevel@tonic-gate 		 * To do the copy, just have dst's big_value points
8837c478bd9Sstevel@tonic-gate 		 * to src's.
8847c478bd9Sstevel@tonic-gate 		 */
8857c478bd9Sstevel@tonic-gate 		dst->big_value = src->big_value;
8867c478bd9Sstevel@tonic-gate 		dst->big_value_len = src->big_value_len;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		/*
8897c478bd9Sstevel@tonic-gate 		 * After the copy, nullify the src's big_value pointer.
8907c478bd9Sstevel@tonic-gate 		 * It prevents any double freeing the value.
8917c478bd9Sstevel@tonic-gate 		 */
8927c478bd9Sstevel@tonic-gate 		src->big_value = NULL;
8937c478bd9Sstevel@tonic-gate 		src->big_value_len = 0;
8947c478bd9Sstevel@tonic-gate 	} else {
8957c478bd9Sstevel@tonic-gate 		dst->big_value = NULL;
8967c478bd9Sstevel@tonic-gate 		dst->big_value_len = 0;
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate CK_RV
get_string_from_template(CK_ATTRIBUTE_PTR dest,CK_ATTRIBUTE_PTR src)9017c478bd9Sstevel@tonic-gate get_string_from_template(CK_ATTRIBUTE_PTR dest, CK_ATTRIBUTE_PTR src)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	if ((src->pValue != NULL) &&
9047c478bd9Sstevel@tonic-gate 	    (src->ulValueLen > 0)) {
9057c478bd9Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
9067c478bd9Sstevel@tonic-gate 		dest->pValue = malloc(src->ulValueLen);
9077c478bd9Sstevel@tonic-gate 		if (dest->pValue == NULL) {
9087c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		(void) memcpy(dest->pValue, src->pValue,
9127c478bd9Sstevel@tonic-gate 		    src->ulValueLen);
9137c478bd9Sstevel@tonic-gate 		dest->ulValueLen = src->ulValueLen;
9147c478bd9Sstevel@tonic-gate 		dest->type = src->type;
9157c478bd9Sstevel@tonic-gate 	} else {
9167c478bd9Sstevel@tonic-gate 		dest->pValue = NULL;
9177c478bd9Sstevel@tonic-gate 		dest->ulValueLen = 0;
9187c478bd9Sstevel@tonic-gate 		dest->type = src->type;
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	return (CKR_OK);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate CK_RV
get_cert_attr_from_template(cert_attr_t ** dest,CK_ATTRIBUTE_PTR src)9267c478bd9Sstevel@tonic-gate get_cert_attr_from_template(cert_attr_t **dest, CK_ATTRIBUTE_PTR src)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	if (src->pValue != NULL && src->ulValueLen > 0) {
9297c478bd9Sstevel@tonic-gate 		/*
9307c478bd9Sstevel@tonic-gate 		 * If the attribute was already set, clear out the
9317c478bd9Sstevel@tonic-gate 		 * existing value and release the memory.
9327c478bd9Sstevel@tonic-gate 		 */
9337c478bd9Sstevel@tonic-gate 		if (*dest != NULL) {
934*a8793c76SJason King 			cleanup_cert_attr(*dest);
9357c478bd9Sstevel@tonic-gate 		} else {
9367c478bd9Sstevel@tonic-gate 			*dest = malloc(sizeof (cert_attr_t));
9377c478bd9Sstevel@tonic-gate 			if (*dest == NULL) {
9387c478bd9Sstevel@tonic-gate 				return (CKR_HOST_MEMORY);
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 			(void) memset(*dest, 0, sizeof (cert_attr_t));
9417c478bd9Sstevel@tonic-gate 		}
9427c478bd9Sstevel@tonic-gate 		(*dest)->value = malloc(src->ulValueLen);
9437c478bd9Sstevel@tonic-gate 		if ((*dest)->value == NULL) {
9447c478bd9Sstevel@tonic-gate 			free(*dest);
9457c478bd9Sstevel@tonic-gate 			*dest = NULL;
9467c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 		(void) memcpy((*dest)->value, src->pValue, src->ulValueLen);
9497c478bd9Sstevel@tonic-gate 		(*dest)->length = src->ulValueLen;
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	return (CKR_OK);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate  * Copy the certificate attribute information to the template.
9577c478bd9Sstevel@tonic-gate  * If the template attribute is not big enough, set the ulValueLen=-1
9587c478bd9Sstevel@tonic-gate  * and return CKR_BUFFER_TOO_SMALL.
9597c478bd9Sstevel@tonic-gate  */
9607c478bd9Sstevel@tonic-gate static CK_RV
get_cert_attr_from_object(cert_attr_t * src,CK_ATTRIBUTE_PTR template)9617c478bd9Sstevel@tonic-gate get_cert_attr_from_object(cert_attr_t *src, CK_ATTRIBUTE_PTR template)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	if (template->pValue == NULL) {
9647c478bd9Sstevel@tonic-gate 		template->ulValueLen = src->length;
9657c478bd9Sstevel@tonic-gate 		return (CKR_OK);
9667c478bd9Sstevel@tonic-gate 	} else if (template->ulValueLen >= src->length) {
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application is large
9697c478bd9Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		(void) memcpy(template->pValue, src->value, src->length);
9727c478bd9Sstevel@tonic-gate 		template->ulValueLen = src->length;
9737c478bd9Sstevel@tonic-gate 		return (CKR_OK);
9747c478bd9Sstevel@tonic-gate 	} else {
9757c478bd9Sstevel@tonic-gate 		/*
9767c478bd9Sstevel@tonic-gate 		 * The buffer provided by the application does
9777c478bd9Sstevel@tonic-gate 		 * not have enough space to hold the value.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
9807c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate void
string_attr_cleanup(CK_ATTRIBUTE_PTR template)9857c478bd9Sstevel@tonic-gate string_attr_cleanup(CK_ATTRIBUTE_PTR template)
9867c478bd9Sstevel@tonic-gate {
987*a8793c76SJason King 	freezero(template->pValue, template->ulValueLen);
988*a8793c76SJason King 	template->pValue = NULL;
989*a8793c76SJason King 	template->ulValueLen = 0;
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Release the storage allocated for object attribute with big integer
9947c478bd9Sstevel@tonic-gate  * value.
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate void
bigint_attr_cleanup(biginteger_t * big)9977c478bd9Sstevel@tonic-gate bigint_attr_cleanup(biginteger_t *big)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (big == NULL)
10017c478bd9Sstevel@tonic-gate 		return;
10027c478bd9Sstevel@tonic-gate 
1003*a8793c76SJason King 	freezero(big->big_value, big->big_value_len);
1004*a8793c76SJason King 	big->big_value = NULL;
1005*a8793c76SJason King 	big->big_value_len = 0;
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate  * Clean up and release all the storage allocated to hold the big integer
10117c478bd9Sstevel@tonic-gate  * attributes associated with the type (i.e. class) of the object. Also,
10127c478bd9Sstevel@tonic-gate  * release the storage allocated to the type of the object.
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate void
soft_cleanup_object_bigint_attrs(soft_object_t * object_p)10157c478bd9Sstevel@tonic-gate soft_cleanup_object_bigint_attrs(soft_object_t *object_p)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS class = object_p->class;
10197c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	switch (class) {
10237c478bd9Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
10247c478bd9Sstevel@tonic-gate 		if (OBJ_PUB(object_p)) {
10257c478bd9Sstevel@tonic-gate 			switch (keytype) {
10267c478bd9Sstevel@tonic-gate 			case CKK_RSA:
10277c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_RSA_MOD(
10287c478bd9Sstevel@tonic-gate 				    object_p));
10297c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_RSA_PUBEXPO(
10307c478bd9Sstevel@tonic-gate 				    object_p));
10317c478bd9Sstevel@tonic-gate 				break;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 			case CKK_DSA:
10347c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_PRIME(
10357c478bd9Sstevel@tonic-gate 				    object_p));
10367c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_SUBPRIME(
10377c478bd9Sstevel@tonic-gate 				    object_p));
10387c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_BASE(
10397c478bd9Sstevel@tonic-gate 				    object_p));
10407c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_VALUE(
10417c478bd9Sstevel@tonic-gate 				    object_p));
10427c478bd9Sstevel@tonic-gate 				break;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 			case CKK_DH:
10457c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH_PRIME(
10467c478bd9Sstevel@tonic-gate 				    object_p));
10477c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH_BASE(
10487c478bd9Sstevel@tonic-gate 				    object_p));
10497c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH_VALUE(
10507c478bd9Sstevel@tonic-gate 				    object_p));
10517c478bd9Sstevel@tonic-gate 				break;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 			case CKK_X9_42_DH:
10547c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH942_PRIME(
10557c478bd9Sstevel@tonic-gate 				    object_p));
10567c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH942_BASE(
10577c478bd9Sstevel@tonic-gate 				    object_p));
10587c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH942_SUBPRIME(
10597c478bd9Sstevel@tonic-gate 				    object_p));
10607c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DH942_VALUE(
10617c478bd9Sstevel@tonic-gate 				    object_p));
10627c478bd9Sstevel@tonic-gate 				break;
1063034448feSmcpowers 			case CKK_EC:
1064034448feSmcpowers 				bigint_attr_cleanup(OBJ_PUB_EC_POINT(
1065034448feSmcpowers 				    object_p));
1066034448feSmcpowers 				break;
10677c478bd9Sstevel@tonic-gate 			}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 			/* Release Public Key Object struct */
10707c478bd9Sstevel@tonic-gate 			free(OBJ_PUB(object_p));
10717c478bd9Sstevel@tonic-gate 			OBJ_PUB(object_p) = NULL;
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 		break;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
10767c478bd9Sstevel@tonic-gate 		if (OBJ_PRI(object_p)) {
10777c478bd9Sstevel@tonic-gate 			switch (keytype) {
10787c478bd9Sstevel@tonic-gate 			case CKK_RSA:
10797c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_MOD(
10807c478bd9Sstevel@tonic-gate 				    object_p));
10817c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PUBEXPO(
10827c478bd9Sstevel@tonic-gate 				    object_p));
10837c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIEXPO(
10847c478bd9Sstevel@tonic-gate 				    object_p));
10857c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIME1(
10867c478bd9Sstevel@tonic-gate 				    object_p));
10877c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIME2(
10887c478bd9Sstevel@tonic-gate 				    object_p));
10897c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_EXPO1(
10907c478bd9Sstevel@tonic-gate 				    object_p));
10917c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_EXPO2(
10927c478bd9Sstevel@tonic-gate 				    object_p));
10937c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_COEF(
10947c478bd9Sstevel@tonic-gate 				    object_p));
10957c478bd9Sstevel@tonic-gate 				break;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			case CKK_DSA:
10987c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_PRIME(
10997c478bd9Sstevel@tonic-gate 				    object_p));
11007c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_SUBPRIME(
11017c478bd9Sstevel@tonic-gate 				    object_p));
11027c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_BASE(
11037c478bd9Sstevel@tonic-gate 				    object_p));
11047c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_VALUE(
11057c478bd9Sstevel@tonic-gate 				    object_p));
11067c478bd9Sstevel@tonic-gate 				break;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 			case CKK_DH:
11097c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH_PRIME(
11107c478bd9Sstevel@tonic-gate 				    object_p));
11117c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH_BASE(
11127c478bd9Sstevel@tonic-gate 				    object_p));
11137c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH_VALUE(
11147c478bd9Sstevel@tonic-gate 				    object_p));
11157c478bd9Sstevel@tonic-gate 				break;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			case CKK_X9_42_DH:
11187c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH942_PRIME(
11197c478bd9Sstevel@tonic-gate 				    object_p));
11207c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH942_BASE(
11217c478bd9Sstevel@tonic-gate 				    object_p));
11227c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH942_SUBPRIME(
11237c478bd9Sstevel@tonic-gate 				    object_p));
11247c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DH942_VALUE(
11257c478bd9Sstevel@tonic-gate 				    object_p));
11267c478bd9Sstevel@tonic-gate 				break;
1127034448feSmcpowers 
1128034448feSmcpowers 			case CKK_EC:
1129034448feSmcpowers 				bigint_attr_cleanup(OBJ_PRI_EC_VALUE(
1130034448feSmcpowers 				    object_p));
1131034448feSmcpowers 				break;
11327c478bd9Sstevel@tonic-gate 			}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 			/* Release Private Key Object struct. */
11357c478bd9Sstevel@tonic-gate 			free(OBJ_PRI(object_p));
11367c478bd9Sstevel@tonic-gate 			OBJ_PRI(object_p) = NULL;
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 		break;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	case CKO_SECRET_KEY:
11417c478bd9Sstevel@tonic-gate 		if (OBJ_SEC(object_p)) {
11427c478bd9Sstevel@tonic-gate 			/* cleanup key data area */
11437c478bd9Sstevel@tonic-gate 			if (OBJ_SEC_VALUE(object_p) != NULL &&
11447c478bd9Sstevel@tonic-gate 			    OBJ_SEC_VALUE_LEN(object_p) > 0) {
1145*a8793c76SJason King 				freezero(OBJ_SEC_VALUE(object_p),
1146f9fbec18Smcpowers 				    OBJ_SEC_VALUE_LEN(object_p));
11477c478bd9Sstevel@tonic-gate 			}
11487c478bd9Sstevel@tonic-gate 			/* cleanup key schedule data area */
11497c478bd9Sstevel@tonic-gate 			if (OBJ_KEY_SCHED(object_p) != NULL &&
11507c478bd9Sstevel@tonic-gate 			    OBJ_KEY_SCHED_LEN(object_p) > 0) {
1151*a8793c76SJason King 				freezero(OBJ_KEY_SCHED(object_p),
1152f9fbec18Smcpowers 				    OBJ_KEY_SCHED_LEN(object_p));
11537c478bd9Sstevel@tonic-gate 			}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 			/* Release Secret Key Object struct. */
11567c478bd9Sstevel@tonic-gate 			free(OBJ_SEC(object_p));
11577c478bd9Sstevel@tonic-gate 			OBJ_SEC(object_p) = NULL;
11587c478bd9Sstevel@tonic-gate 		}
11597c478bd9Sstevel@tonic-gate 		break;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
11627c478bd9Sstevel@tonic-gate 		if (OBJ_DOM(object_p)) {
11637c478bd9Sstevel@tonic-gate 			switch (keytype) {
11647c478bd9Sstevel@tonic-gate 			case CKK_DSA:
11657c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DSA_PRIME(
11667c478bd9Sstevel@tonic-gate 				    object_p));
11677c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DSA_SUBPRIME(
11687c478bd9Sstevel@tonic-gate 				    object_p));
11697c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DSA_BASE(
11707c478bd9Sstevel@tonic-gate 				    object_p));
11717c478bd9Sstevel@tonic-gate 				break;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 			case CKK_DH:
11747c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DH_PRIME(
11757c478bd9Sstevel@tonic-gate 				    object_p));
11767c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DH_BASE(
11777c478bd9Sstevel@tonic-gate 				    object_p));
11787c478bd9Sstevel@tonic-gate 				break;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 			case CKK_X9_42_DH:
11817c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DH942_PRIME(
11827c478bd9Sstevel@tonic-gate 				    object_p));
11837c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DH942_BASE(
11847c478bd9Sstevel@tonic-gate 				    object_p));
11857c478bd9Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_DOM_DH942_SUBPRIME(
11867c478bd9Sstevel@tonic-gate 				    object_p));
11877c478bd9Sstevel@tonic-gate 				break;
11887c478bd9Sstevel@tonic-gate 			}
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 			/* Release Domain Parameters Object struct. */
11917c478bd9Sstevel@tonic-gate 			free(OBJ_DOM(object_p));
11927c478bd9Sstevel@tonic-gate 			OBJ_DOM(object_p) = NULL;
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 		break;
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate  * Parse the common attributes. Return to caller with appropriate return
12017c478bd9Sstevel@tonic-gate  * value to indicate if the supplied template specifies a valid attribute
12027c478bd9Sstevel@tonic-gate  * with a valid value.
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate CK_RV
soft_parse_common_attrs(CK_ATTRIBUTE_PTR template,uchar_t * object_type)12057c478bd9Sstevel@tonic-gate soft_parse_common_attrs(CK_ATTRIBUTE_PTR template, uchar_t *object_type)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	switch (template->type) {
12117c478bd9Sstevel@tonic-gate 	case CKA_CLASS:
12127c478bd9Sstevel@tonic-gate 		break;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/* default boolean attributes */
12157c478bd9Sstevel@tonic-gate 	case CKA_TOKEN:
12167c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_TRUE) {
121790e0e8c4Sizick 			if (!soft_keystore_status(KEYSTORE_INITIALIZED))
12187c478bd9Sstevel@tonic-gate 				return (CKR_DEVICE_REMOVED);
12197c478bd9Sstevel@tonic-gate 			*object_type |= TOKEN_OBJECT;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE:
12247c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_TRUE) {
12257c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&soft_giant_mutex);
12267c478bd9Sstevel@tonic-gate 			if (!soft_slot.authenticated) {
12277c478bd9Sstevel@tonic-gate 				/*
12287c478bd9Sstevel@tonic-gate 				 * Check if this is the special case when
12297c478bd9Sstevel@tonic-gate 				 * the PIN is never initialized in the keystore.
12307c478bd9Sstevel@tonic-gate 				 * If true, we will let it pass here and let
12317c478bd9Sstevel@tonic-gate 				 * it fail with CKR_PIN_EXPIRED later on.
12327c478bd9Sstevel@tonic-gate 				 */
12337c478bd9Sstevel@tonic-gate 				if (!soft_slot.userpin_change_needed) {
12347c478bd9Sstevel@tonic-gate 					(void) pthread_mutex_unlock(
12357c478bd9Sstevel@tonic-gate 					    &soft_giant_mutex);
12367c478bd9Sstevel@tonic-gate 					return (CKR_USER_NOT_LOGGED_IN);
12377c478bd9Sstevel@tonic-gate 				}
12387c478bd9Sstevel@tonic-gate 			}
12397c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&soft_giant_mutex);
12407c478bd9Sstevel@tonic-gate 			*object_type |= PRIVATE_OBJECT;
12417c478bd9Sstevel@tonic-gate 		}
12427c478bd9Sstevel@tonic-gate 		break;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	case CKA_LABEL:
12457c478bd9Sstevel@tonic-gate 		break;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	default:
12487c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	return (rv);
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate  * Build a Public Key Object.
12577c478bd9Sstevel@tonic-gate  *
12587c478bd9Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
12597c478bd9Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
12607c478bd9Sstevel@tonic-gate  *   with appropriate return value.
12617c478bd9Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
12627c478bd9Sstevel@tonic-gate  *   attributes that have boolean type.
12637c478bd9Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
12647c478bd9Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
12657c478bd9Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
12667c478bd9Sstevel@tonic-gate  * - Allocate storage for the Public Key object.
12677c478bd9Sstevel@tonic-gate  * - Build the Public Key object according to the key type. Allocate
12687c478bd9Sstevel@tonic-gate  *   storage to hold the big integer value for the supplied attributes
12697c478bd9Sstevel@tonic-gate  *   that are required for a certain key type.
12707c478bd9Sstevel@tonic-gate  *
12717c478bd9Sstevel@tonic-gate  */
12727c478bd9Sstevel@tonic-gate CK_RV
soft_build_public_key_object(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,soft_object_t * new_object,CK_ULONG mode,CK_KEY_TYPE key_type)12737c478bd9Sstevel@tonic-gate soft_build_public_key_object(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
1274*a8793c76SJason King     soft_object_t *new_object, CK_ULONG mode, CK_KEY_TYPE key_type)
12757c478bd9Sstevel@tonic-gate {
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	ulong_t		i;
12787c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
12797c478bd9Sstevel@tonic-gate 	uint64_t	attr_mask = PUBLIC_KEY_DEFAULT;
1280*a8793c76SJason King 	CK_RV		rv = CKR_OK;
12817c478bd9Sstevel@tonic-gate 	int		isLabel = 0;
12827c478bd9Sstevel@tonic-gate 	/* Must set flags */
12837c478bd9Sstevel@tonic-gate 	int		isModulus = 0;
12847c478bd9Sstevel@tonic-gate 	int		isPubExpo = 0;
12857c478bd9Sstevel@tonic-gate 	int		isPrime = 0;
12867c478bd9Sstevel@tonic-gate 	int		isSubprime = 0;
12877c478bd9Sstevel@tonic-gate 	int		isBase = 0;
12887c478bd9Sstevel@tonic-gate 	int		isValue = 0;
1289034448feSmcpowers 	int		isECParam = 0;
1290034448feSmcpowers 	int		isECPoint = 0;
12917c478bd9Sstevel@tonic-gate 	/* Must not set flags */
12927c478bd9Sstevel@tonic-gate 	int		isModulusBits = 0;
12937c478bd9Sstevel@tonic-gate 	CK_ULONG	modulus_bits = 0;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	biginteger_t	modulus;
12967c478bd9Sstevel@tonic-gate 	biginteger_t	pubexpo;
1297f9fbec18Smcpowers 	biginteger_t	prime;
1298f9fbec18Smcpowers 	biginteger_t	subprime;
12997c478bd9Sstevel@tonic-gate 	biginteger_t	base;
13007c478bd9Sstevel@tonic-gate 	biginteger_t	value;
1301f9fbec18Smcpowers 	biginteger_t	point;
13027c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
1303f9fbec18Smcpowers 	CK_ATTRIBUTE	param_tmp;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	public_key_obj_t  *pbk;
13067c478bd9Sstevel@tonic-gate 	uchar_t	object_type = 0;
13077c478bd9Sstevel@tonic-gate 
130820d58091SDina K Nimeh 	CK_ATTRIBUTE	defpubexpo = { CKA_PUBLIC_EXPONENT,
130920d58091SDina K Nimeh 	    (CK_BYTE_PTR)DEFAULT_PUB_EXPO, DEFAULT_PUB_EXPO_Len };
131020d58091SDina K Nimeh 
131120d58091SDina K Nimeh 	BIGNUM		n;
131220d58091SDina K Nimeh 
13137c478bd9Sstevel@tonic-gate 	/* prevent bigint_attr_cleanup from freeing invalid attr value */
13147c478bd9Sstevel@tonic-gate 	(void) memset(&modulus, 0x0, sizeof (biginteger_t));
13157c478bd9Sstevel@tonic-gate 	(void) memset(&pubexpo, 0x0, sizeof (biginteger_t));
13167c478bd9Sstevel@tonic-gate 	(void) memset(&prime, 0x0, sizeof (biginteger_t));
13177c478bd9Sstevel@tonic-gate 	(void) memset(&subprime, 0x0, sizeof (biginteger_t));
13187c478bd9Sstevel@tonic-gate 	(void) memset(&base, 0x0, sizeof (biginteger_t));
13197c478bd9Sstevel@tonic-gate 	(void) memset(&value, 0x0, sizeof (biginteger_t));
1320f9fbec18Smcpowers 	(void) memset(&point, 0x0, sizeof (biginteger_t));
13217c478bd9Sstevel@tonic-gate 	string_tmp.pValue = NULL;
1322f9fbec18Smcpowers 	param_tmp.pValue = NULL;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 		/* Public Key Object Attributes */
13277c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		/* common key attributes */
13307c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
13317c478bd9Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
13327c478bd9Sstevel@tonic-gate 			break;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		case CKA_ID:
13357c478bd9Sstevel@tonic-gate 		case CKA_START_DATE:
13367c478bd9Sstevel@tonic-gate 		case CKA_END_DATE:
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		/* common public key attribute */
13397c478bd9Sstevel@tonic-gate 		case CKA_SUBJECT:
13407c478bd9Sstevel@tonic-gate 			/*
13417c478bd9Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
13427c478bd9Sstevel@tonic-gate 			 * value with byte array type, and add it to
13437c478bd9Sstevel@tonic-gate 			 * the extra attribute list of the object.
13447c478bd9Sstevel@tonic-gate 			 */
13457c478bd9Sstevel@tonic-gate 			rv = soft_add_extra_attr(&template[i],
13467c478bd9Sstevel@tonic-gate 			    new_object);
13477c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
13487c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
13497c478bd9Sstevel@tonic-gate 			}
13507c478bd9Sstevel@tonic-gate 			break;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		/*
13537c478bd9Sstevel@tonic-gate 		 * The following key related attribute types must
13547c478bd9Sstevel@tonic-gate 		 * not be specified by C_CreateObject, C_GenerateKey(Pair).
13557c478bd9Sstevel@tonic-gate 		 */
13567c478bd9Sstevel@tonic-gate 		case CKA_LOCAL:
13577c478bd9Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
13587c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
13597c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 		/* Key related boolean attributes */
13627c478bd9Sstevel@tonic-gate 		case CKA_DERIVE:
13637c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13647c478bd9Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
13657c478bd9Sstevel@tonic-gate 			break;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		case CKA_ENCRYPT:
13687c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13697c478bd9Sstevel@tonic-gate 				attr_mask |= ENCRYPT_BOOL_ON;
13707c478bd9Sstevel@tonic-gate 			else
13717c478bd9Sstevel@tonic-gate 				attr_mask &= ~ENCRYPT_BOOL_ON;
13727c478bd9Sstevel@tonic-gate 			break;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 		case CKA_VERIFY:
13757c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13767c478bd9Sstevel@tonic-gate 				attr_mask |= VERIFY_BOOL_ON;
13777c478bd9Sstevel@tonic-gate 			else
13787c478bd9Sstevel@tonic-gate 				attr_mask &= ~VERIFY_BOOL_ON;
13797c478bd9Sstevel@tonic-gate 			break;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 		case CKA_VERIFY_RECOVER:
13827c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13837c478bd9Sstevel@tonic-gate 				attr_mask |= VERIFY_RECOVER_BOOL_ON;
13847c478bd9Sstevel@tonic-gate 			else
13857c478bd9Sstevel@tonic-gate 				attr_mask &= ~VERIFY_RECOVER_BOOL_ON;
13867c478bd9Sstevel@tonic-gate 			break;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 		case CKA_WRAP:
13897c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13907c478bd9Sstevel@tonic-gate 				attr_mask |= WRAP_BOOL_ON;
13917c478bd9Sstevel@tonic-gate 			else
13927c478bd9Sstevel@tonic-gate 				attr_mask &= ~WRAP_BOOL_ON;
13937c478bd9Sstevel@tonic-gate 			break;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		case CKA_TRUSTED:
13967c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
13977c478bd9Sstevel@tonic-gate 				attr_mask |= TRUSTED_BOOL_ON;
13987c478bd9Sstevel@tonic-gate 			break;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		case CKA_MODIFIABLE:
14017c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template[i].pValue) == B_FALSE)
14027c478bd9Sstevel@tonic-gate 				attr_mask |= NOT_MODIFIABLE_BOOL_ON;
14037c478bd9Sstevel@tonic-gate 			break;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		/*
14067c478bd9Sstevel@tonic-gate 		 * The following key related attribute types must
14077c478bd9Sstevel@tonic-gate 		 * be specified according to the key type by
14087c478bd9Sstevel@tonic-gate 		 * C_CreateObject.
14097c478bd9Sstevel@tonic-gate 		 */
14107c478bd9Sstevel@tonic-gate 		case CKA_MODULUS:
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 			isModulus = 1;
14137c478bd9Sstevel@tonic-gate 			/*
14147c478bd9Sstevel@tonic-gate 			 * Copyin big integer attribute from template
14157c478bd9Sstevel@tonic-gate 			 * to a local variable.
14167c478bd9Sstevel@tonic-gate 			 */
14177c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&modulus,
14187c478bd9Sstevel@tonic-gate 			    &template[i]);
14197c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14207c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 			/*
14237c478bd9Sstevel@tonic-gate 			 * Modulus length needs to be between min key length and
14247c478bd9Sstevel@tonic-gate 			 * max key length.
14257c478bd9Sstevel@tonic-gate 			 */
14267c478bd9Sstevel@tonic-gate 			if ((modulus.big_value_len <
14277c478bd9Sstevel@tonic-gate 			    MIN_RSA_KEYLENGTH_IN_BYTES) ||
14287c478bd9Sstevel@tonic-gate 			    (modulus.big_value_len >
14297c478bd9Sstevel@tonic-gate 			    MAX_RSA_KEYLENGTH_IN_BYTES)) {
14307c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
14317c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14327c478bd9Sstevel@tonic-gate 			}
14337c478bd9Sstevel@tonic-gate 			break;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
14367c478bd9Sstevel@tonic-gate 			isPubExpo = 1;
14377c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&pubexpo,
14387c478bd9Sstevel@tonic-gate 			    &template[i]);
14397c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14407c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14417c478bd9Sstevel@tonic-gate 			break;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 		case CKA_PRIME:
14447c478bd9Sstevel@tonic-gate 			isPrime = 1;
14457c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime,
14467c478bd9Sstevel@tonic-gate 			    &template[i]);
14477c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14487c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14497c478bd9Sstevel@tonic-gate 			break;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		case CKA_SUBPRIME:
14527c478bd9Sstevel@tonic-gate 			isSubprime = 1;
14537c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&subprime,
14547c478bd9Sstevel@tonic-gate 			    &template[i]);
14557c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14567c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14577c478bd9Sstevel@tonic-gate 			break;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		case CKA_BASE:
14607c478bd9Sstevel@tonic-gate 			isBase = 1;
14617c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&base,
14627c478bd9Sstevel@tonic-gate 			    &template[i]);
14637c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14647c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14657c478bd9Sstevel@tonic-gate 			break;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
14687c478bd9Sstevel@tonic-gate 			isValue = 1;
14697c478bd9Sstevel@tonic-gate 			if (mode == SOFT_CREATE_OBJ) {
14707c478bd9Sstevel@tonic-gate 				if ((template[i].ulValueLen == 0) ||
14717c478bd9Sstevel@tonic-gate 				    (template[i].pValue == NULL)) {
14727c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
14737c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
1474f9fbec18Smcpowers 				}
14757c478bd9Sstevel@tonic-gate 			}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&value,
14787c478bd9Sstevel@tonic-gate 			    &template[i]);
14797c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14807c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14817c478bd9Sstevel@tonic-gate 			break;
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		case CKA_MODULUS_BITS:
14847c478bd9Sstevel@tonic-gate 			isModulusBits = 1;
14857739299dSAnthony Scarpino 			rv = get_ulong_attr_from_template(&modulus_bits,
14867c478bd9Sstevel@tonic-gate 			    &template[i]);
14877739299dSAnthony Scarpino 			if (rv != CKR_OK)
14887739299dSAnthony Scarpino 				goto fail_cleanup;
14897c478bd9Sstevel@tonic-gate 			break;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
14927c478bd9Sstevel@tonic-gate 			isLabel = 1;
14937c478bd9Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
14947c478bd9Sstevel@tonic-gate 			    &template[i]);
14957c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
14967c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
14977c478bd9Sstevel@tonic-gate 			break;
14987c478bd9Sstevel@tonic-gate 
1499034448feSmcpowers 		case CKA_EC_PARAMS:
1500034448feSmcpowers 			isECParam = 1;
1501f9fbec18Smcpowers 			rv = get_string_from_template(&param_tmp, &template[i]);
1502034448feSmcpowers 			if (rv != CKR_OK)
1503034448feSmcpowers 				goto fail_cleanup;
1504034448feSmcpowers 			break;
1505034448feSmcpowers 
1506034448feSmcpowers 		case CKA_EC_POINT:
1507034448feSmcpowers 			isECPoint = 1;
1508f9fbec18Smcpowers 			rv = get_bigint_attr_from_template(&point,
1509034448feSmcpowers 			    &template[i]);
1510034448feSmcpowers 			if (rv != CKR_OK)
1511034448feSmcpowers 				goto fail_cleanup;
1512034448feSmcpowers 			break;
1513034448feSmcpowers 
15147c478bd9Sstevel@tonic-gate 		default:
15157c478bd9Sstevel@tonic-gate 			rv = soft_parse_common_attrs(&template[i],
15167c478bd9Sstevel@tonic-gate 			    &object_type);
15177c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
15187c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
15197c478bd9Sstevel@tonic-gate 			break;
15207c478bd9Sstevel@tonic-gate 		}
15217c478bd9Sstevel@tonic-gate 	} /* For */
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/* Allocate storage for Public Key Object. */
15247c478bd9Sstevel@tonic-gate 	pbk = calloc(1, sizeof (public_key_obj_t));
15257c478bd9Sstevel@tonic-gate 	if (pbk == NULL) {
15267c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
15277c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	new_object->object_class_u.public_key = pbk;
15317c478bd9Sstevel@tonic-gate 	new_object->class = CKO_PUBLIC_KEY;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if ((mode == SOFT_CREATE_OBJ) && (keytype == (CK_KEY_TYPE)~0UL)) {
15347c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
15357c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if ((mode == SOFT_GEN_KEY) && (keytype == (CK_KEY_TYPE)~0UL)) {
15397c478bd9Sstevel@tonic-gate 		keytype = key_type;
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	if ((mode == SOFT_GEN_KEY) && (keytype != key_type)) {
15437c478bd9Sstevel@tonic-gate 		/*
15447c478bd9Sstevel@tonic-gate 		 * The key type specified in the template does not
15457c478bd9Sstevel@tonic-gate 		 * match the implied key type based on the mechanism.
15467c478bd9Sstevel@tonic-gate 		 */
15477c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
15487c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	new_object->key_type = keytype;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	/* Supported key types of the Public Key Object */
15547c478bd9Sstevel@tonic-gate 	switch (keytype) {
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	case CKK_RSA:
15577c478bd9Sstevel@tonic-gate 		if (mode == SOFT_CREATE_OBJ) {
15587c478bd9Sstevel@tonic-gate 			if (isModulusBits || isPrime || isSubprime ||
15597c478bd9Sstevel@tonic-gate 			    isBase || isValue) {
15607c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
15617c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
15627c478bd9Sstevel@tonic-gate 			}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 			if (isModulus && isPubExpo) {
15657c478bd9Sstevel@tonic-gate 				/*
15665cd3be5eSDina K Nimeh 				 * Derive modulus_bits attribute from modulus.
15675cd3be5eSDina K Nimeh 				 * Save modulus_bits integer value to the
15685cd3be5eSDina K Nimeh 				 * designated place in the public key object.
15695cd3be5eSDina K Nimeh 				 */
15705cd3be5eSDina K Nimeh 				n.malloced = 0;
15715cd3be5eSDina K Nimeh #ifdef  __sparcv9
15725cd3be5eSDina K Nimeh 				if (big_init(&n, (int)CHARLEN2BIGNUMLEN(
15735cd3be5eSDina K Nimeh 				    modulus.big_value_len)) != BIG_OK) {
15745cd3be5eSDina K Nimeh #else   /* !__sparcv9 */
15755cd3be5eSDina K Nimeh 				if (big_init(&n, CHARLEN2BIGNUMLEN(
15765cd3be5eSDina K Nimeh 				    modulus.big_value_len)) != BIG_OK) {
15775cd3be5eSDina K Nimeh #endif  /* __sparcv9 */
15785cd3be5eSDina K Nimeh 					rv = CKR_HOST_MEMORY;
15795cd3be5eSDina K Nimeh 					big_finish(&n);
15805cd3be5eSDina K Nimeh 					goto fail_cleanup;
15815cd3be5eSDina K Nimeh 				}
15825cd3be5eSDina K Nimeh 				bytestring2bignum(&n, modulus.big_value,
15835cd3be5eSDina K Nimeh 				    modulus.big_value_len);
15845cd3be5eSDina K Nimeh 
15855cd3be5eSDina K Nimeh 				modulus_bits = big_bitlength(&n);
15865cd3be5eSDina K Nimeh 				KEY_PUB_RSA_MOD_BITS(pbk) = modulus_bits;
15875cd3be5eSDina K Nimeh 				big_finish(&n);
15885cd3be5eSDina K Nimeh 
15895cd3be5eSDina K Nimeh 				/*
15905cd3be5eSDina K Nimeh 				 * After modulus_bits has been computed,
15915cd3be5eSDina K Nimeh 				 * it is safe to move modulus and pubexpo
15925cd3be5eSDina K Nimeh 				 * big integer attribute value to the
15937c478bd9Sstevel@tonic-gate 				 * designated place in the public key object.
15947c478bd9Sstevel@tonic-gate 				 */
15957c478bd9Sstevel@tonic-gate 				copy_bigint_attr(&modulus,
15967c478bd9Sstevel@tonic-gate 				    KEY_PUB_RSA_MOD(pbk));
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 				copy_bigint_attr(&pubexpo,
15997c478bd9Sstevel@tonic-gate 				    KEY_PUB_RSA_PUBEXPO(pbk));
16007c478bd9Sstevel@tonic-gate 			} else {
16017c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
16027c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16037c478bd9Sstevel@tonic-gate 			}
16047c478bd9Sstevel@tonic-gate 		} else {
160520d58091SDina K Nimeh 			/* mode is SOFT_GEN_KEY */
160620d58091SDina K Nimeh 
16077c478bd9Sstevel@tonic-gate 			if (isModulus || isPrime || isSubprime ||
16087c478bd9Sstevel@tonic-gate 			    isBase || isValue) {
16097c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
16107c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16117c478bd9Sstevel@tonic-gate 			}
16127c478bd9Sstevel@tonic-gate 
161320d58091SDina K Nimeh 
161420d58091SDina K Nimeh 			if (isModulusBits) {
16157c478bd9Sstevel@tonic-gate 				/*
16167c478bd9Sstevel@tonic-gate 				 * Copy big integer attribute value to the
16177c478bd9Sstevel@tonic-gate 				 * designated place in the public key object.
16187c478bd9Sstevel@tonic-gate 				 */
16197c478bd9Sstevel@tonic-gate 				KEY_PUB_RSA_MOD_BITS(pbk) = modulus_bits;
16207c478bd9Sstevel@tonic-gate 			} else {
16217c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
16227c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16237c478bd9Sstevel@tonic-gate 			}
162420d58091SDina K Nimeh 
162520d58091SDina K Nimeh 			/*
162620d58091SDina K Nimeh 			 * Use PKCS#11 default 0x010001 for public exponent
162720d58091SDina K Nimeh 			 * if not not specified in attribute template.
162820d58091SDina K Nimeh 			 */
162920d58091SDina K Nimeh 			if (!isPubExpo) {
163020d58091SDina K Nimeh 				isPubExpo = 1;
163120d58091SDina K Nimeh 				rv = get_bigint_attr_from_template(&pubexpo,
163220d58091SDina K Nimeh 				    &defpubexpo);
163320d58091SDina K Nimeh 				if (rv != CKR_OK)
163420d58091SDina K Nimeh 					goto fail_cleanup;
163520d58091SDina K Nimeh 			}
163620d58091SDina K Nimeh 			/*
163720d58091SDina K Nimeh 			 * Copy big integer attribute value to the
163820d58091SDina K Nimeh 			 * designated place in the public key object.
163920d58091SDina K Nimeh 			 */
164020d58091SDina K Nimeh 			copy_bigint_attr(&pubexpo, KEY_PUB_RSA_PUBEXPO(pbk));
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		break;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	case CKK_DSA:
16467c478bd9Sstevel@tonic-gate 		if (mode == SOFT_CREATE_OBJ) {
16477c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo) {
16487c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
16497c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16507c478bd9Sstevel@tonic-gate 			}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 			if (isPrime && isSubprime && isBase && isValue) {
16537c478bd9Sstevel@tonic-gate 				copy_bigint_attr(&value,
16547c478bd9Sstevel@tonic-gate 				    KEY_PUB_DSA_VALUE(pbk));
16557c478bd9Sstevel@tonic-gate 			} else {
16567c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
16577c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16587c478bd9Sstevel@tonic-gate 			}
16597c478bd9Sstevel@tonic-gate 		} else {
16607c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo ||
16617c478bd9Sstevel@tonic-gate 			    isValue) {
16627c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
16637c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16647c478bd9Sstevel@tonic-gate 			}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 			if (!(isPrime && isSubprime && isBase)) {
16677c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
16687c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16697c478bd9Sstevel@tonic-gate 			}
16707c478bd9Sstevel@tonic-gate 		}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&prime, KEY_PUB_DSA_PRIME(pbk));
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&subprime, KEY_PUB_DSA_SUBPRIME(pbk));
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&base, KEY_PUB_DSA_BASE(pbk));
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		break;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	case CKK_DH:
16817c478bd9Sstevel@tonic-gate 		if (mode == SOFT_CREATE_OBJ) {
16827c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo ||
16837c478bd9Sstevel@tonic-gate 			    isSubprime) {
16847c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
16857c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16867c478bd9Sstevel@tonic-gate 			}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 			if (isPrime && isBase && isValue) {
16897c478bd9Sstevel@tonic-gate 				copy_bigint_attr(&value,
16907c478bd9Sstevel@tonic-gate 				    KEY_PUB_DH_VALUE(pbk));
16917c478bd9Sstevel@tonic-gate 			} else {
16927c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
16937c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
16947c478bd9Sstevel@tonic-gate 			}
16957c478bd9Sstevel@tonic-gate 		} else {
16967c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo ||
16977c478bd9Sstevel@tonic-gate 			    isSubprime || isValue) {
16987c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
16997c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17007c478bd9Sstevel@tonic-gate 			}
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 			if (!(isPrime && isBase)) {
17037c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
17047c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17057c478bd9Sstevel@tonic-gate 			}
17067c478bd9Sstevel@tonic-gate 		}
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&prime, KEY_PUB_DH_PRIME(pbk));
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&base, KEY_PUB_DH_BASE(pbk));
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 		break;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	case CKK_X9_42_DH:
17157c478bd9Sstevel@tonic-gate 		if (mode == SOFT_CREATE_OBJ) {
17167c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo) {
17177c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
17187c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17197c478bd9Sstevel@tonic-gate 			}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 			if (isPrime && isSubprime && isBase && isValue) {
17227c478bd9Sstevel@tonic-gate 				copy_bigint_attr(&value,
17237c478bd9Sstevel@tonic-gate 				    KEY_PUB_DH942_VALUE(pbk));
17247c478bd9Sstevel@tonic-gate 			} else {
17257c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
17267c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17277c478bd9Sstevel@tonic-gate 			}
17287c478bd9Sstevel@tonic-gate 		} else {
17297c478bd9Sstevel@tonic-gate 			if (isModulusBits || isModulus || isPubExpo ||
17307c478bd9Sstevel@tonic-gate 			    isValue) {
17317c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
17327c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17337c478bd9Sstevel@tonic-gate 			}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 			if (!(isPrime && isSubprime && isBase)) {
17367c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
17377c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
17387c478bd9Sstevel@tonic-gate 			}
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&prime, KEY_PUB_DH942_PRIME(pbk));
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&base, KEY_PUB_DH942_BASE(pbk));
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 		copy_bigint_attr(&subprime, KEY_PUB_DH942_SUBPRIME(pbk));
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 		break;
17487c478bd9Sstevel@tonic-gate 
1749034448feSmcpowers 	case CKK_EC:
1750f9fbec18Smcpowers 		if (mode == SOFT_CREATE_OBJ) {
1751f9fbec18Smcpowers 			if (isModulusBits || isModulus || isPubExpo ||
1752f9fbec18Smcpowers 			    isPrime || isSubprime || isBase || isValue) {
1753f9fbec18Smcpowers 				rv = CKR_TEMPLATE_INCONSISTENT;
1754f9fbec18Smcpowers 				goto fail_cleanup;
1755034448feSmcpowers 
1756f9fbec18Smcpowers 			} else if (!isECParam || !isECPoint) {
1757f9fbec18Smcpowers 				rv = CKR_TEMPLATE_INCOMPLETE;
1758f9fbec18Smcpowers 				goto fail_cleanup;
1759f9fbec18Smcpowers 			}
1760f9fbec18Smcpowers 		} else {
1761f9fbec18Smcpowers 			if (isModulusBits || isModulus || isPubExpo ||
1762f9fbec18Smcpowers 			    isPrime || isSubprime || isBase || isValue) {
1763f9fbec18Smcpowers 				rv = CKR_TEMPLATE_INCONSISTENT;
1764f9fbec18Smcpowers 				goto fail_cleanup;
1765f9fbec18Smcpowers 
1766f9fbec18Smcpowers 			} else if (!isECParam) {
1767f9fbec18Smcpowers 				rv = CKR_TEMPLATE_INCOMPLETE;
1768f9fbec18Smcpowers 				goto fail_cleanup;
1769f9fbec18Smcpowers 			}
1770034448feSmcpowers 		}
1771034448feSmcpowers 
1772f9fbec18Smcpowers 		if (isECPoint) {
1773f9fbec18Smcpowers 			copy_bigint_attr(&point, KEY_PUB_EC_POINT(pbk));
1774f9fbec18Smcpowers 		}
1775f9fbec18Smcpowers 		rv = soft_add_extra_attr(&param_tmp, new_object);
1776f9fbec18Smcpowers 		if (rv != CKR_OK)
1777f9fbec18Smcpowers 			goto fail_cleanup;
1778f9fbec18Smcpowers 		string_attr_cleanup(&param_tmp);
1779034448feSmcpowers 		break;
1780034448feSmcpowers 
17817c478bd9Sstevel@tonic-gate 	default:
17827c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
17837c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/* Set up object. */
17877c478bd9Sstevel@tonic-gate 	new_object->object_type = object_type;
17887c478bd9Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
17897c478bd9Sstevel@tonic-gate 	if (isLabel) {
17907c478bd9Sstevel@tonic-gate 		rv = soft_add_extra_attr(&string_tmp, new_object);
17917c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
17927c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
17937c478bd9Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	return (rv);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate fail_cleanup:
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
18017c478bd9Sstevel@tonic-gate 	 */
18027c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&modulus);
18037c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&pubexpo);
18047c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&prime);
18057c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&subprime);
18067c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&base);
18077c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&value);
1808f9fbec18Smcpowers 	bigint_attr_cleanup(&point);
18097c478bd9Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
1810f9fbec18Smcpowers 	string_attr_cleanup(&param_tmp);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	/*
18137c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	soft_cleanup_object(new_object);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	return (rv);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate /*
18227c478bd9Sstevel@tonic-gate  * Build a Private Key Object.
18237c478bd9Sstevel@tonic-gate  *
18247c478bd9Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
18257c478bd9Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
18267c478bd9Sstevel@tonic-gate  *   with appropriate return value.
18277c478bd9Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
18287c478bd9Sstevel@tonic-gate  *   attributes that have boolean type.
18297c478bd9Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
18307c478bd9Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
18317c478bd9Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
18327c478bd9Sstevel@tonic-gate  * - Allocate storage for the Private Key object.
18337c478bd9Sstevel@tonic-gate  * - Build the Private Key object according to the key type. Allocate
18347c478bd9Sstevel@tonic-gate  *   storage to hold the big integer value for the supplied attributes
18357c478bd9Sstevel@tonic-gate  *   that are required for a certain key type.
18367c478bd9Sstevel@tonic-gate  *
18377c478bd9Sstevel@tonic-gate  */
18387c478bd9Sstevel@tonic-gate CK_RV
18397c478bd9Sstevel@tonic-gate soft_build_private_key_object(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
1840*a8793c76SJason King     soft_object_t *new_object, CK_ULONG mode, CK_KEY_TYPE key_type)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate 	ulong_t		i;
18437c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
18447c478bd9Sstevel@tonic-gate 	uint64_t	attr_mask = PRIVATE_KEY_DEFAULT;
1845*a8793c76SJason King 	CK_RV		rv = CKR_OK;
18467c478bd9Sstevel@tonic-gate 	int		isLabel = 0;
1847034448feSmcpowers 	int		isECParam = 0;
18487c478bd9Sstevel@tonic-gate 	/* Must set flags unless mode == SOFT_UNWRAP_KEY */
18497c478bd9Sstevel@tonic-gate 	int		isModulus = 0;
18507c478bd9Sstevel@tonic-gate 	int		isPriExpo = 0;
18517c478bd9Sstevel@tonic-gate 	int		isPrime = 0;
18527c478bd9Sstevel@tonic-gate 	int		isSubprime = 0;
18537c478bd9Sstevel@tonic-gate 	int		isBase = 0;
18547c478bd9Sstevel@tonic-gate 	/* Must set flags if mode == SOFT_GEN_KEY */
18557c478bd9Sstevel@tonic-gate 	int		isValue = 0;
18567c478bd9Sstevel@tonic-gate 	/* Must not set flags */
18577c478bd9Sstevel@tonic-gate 	int		isValueBits = 0;
18587c478bd9Sstevel@tonic-gate 	CK_ULONG	value_bits = 0;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/* Private Key RSA optional */
18617c478bd9Sstevel@tonic-gate 	int		isPubExpo = 0;
18627c478bd9Sstevel@tonic-gate 	int		isPrime1 = 0;
18637c478bd9Sstevel@tonic-gate 	int		isPrime2 = 0;
18647c478bd9Sstevel@tonic-gate 	int		isExpo1 = 0;
18657c478bd9Sstevel@tonic-gate 	int		isExpo2 = 0;
18667c478bd9Sstevel@tonic-gate 	int		isCoef = 0;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	biginteger_t	modulus;
18697c478bd9Sstevel@tonic-gate 	biginteger_t	priexpo;
18707c478bd9Sstevel@tonic-gate 	biginteger_t	prime;
18717c478bd9Sstevel@tonic-gate 	biginteger_t	subprime;
18727c478bd9Sstevel@tonic-gate 	biginteger_t	base;
18737c478bd9Sstevel@tonic-gate 	biginteger_t	value;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	biginteger_t	pubexpo;
18767c478bd9Sstevel@tonic-gate 	biginteger_t	prime1;
18777c478bd9Sstevel@tonic-gate 	biginteger_t	prime2;
18787c478bd9Sstevel@tonic-gate 	biginteger_t	expo1;
18797c478bd9Sstevel@tonic-gate 	biginteger_t	expo2;
18807c478bd9Sstevel@tonic-gate 	biginteger_t	coef;
18817c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
1882f9fbec18Smcpowers 	CK_ATTRIBUTE	param_tmp;
18837c478bd9Sstevel@tonic-gate 	BIGNUM	x, q;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	private_key_obj_t *pvk;
18867c478bd9Sstevel@tonic-gate 	uchar_t	object_type = 0;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/* prevent bigint_attr_cleanup from freeing invalid attr value */
18897c478bd9Sstevel@tonic-gate 	(void) memset(&modulus, 0x0, sizeof (biginteger_t));
18907c478bd9Sstevel@tonic-gate 	(void) memset(&priexpo, 0x0, sizeof (biginteger_t));
18917c478bd9Sstevel@tonic-gate 	(void) memset(&prime, 0x0, sizeof (biginteger_t));
18927c478bd9Sstevel@tonic-gate 	(void) memset(&subprime, 0x0, sizeof (biginteger_t));
18937c478bd9Sstevel@tonic-gate 	(void) memset(&base, 0x0, sizeof (biginteger_t));
18947c478bd9Sstevel@tonic-gate 	(void) memset(&value, 0x0, sizeof (biginteger_t));
18957c478bd9Sstevel@tonic-gate 	(void) memset(&pubexpo, 0x0, sizeof (biginteger_t));
18967c478bd9Sstevel@tonic-gate 	(void) memset(&prime1, 0x0, sizeof (biginteger_t));
18977c478bd9Sstevel@tonic-gate 	(void) memset(&prime2, 0x0, sizeof (biginteger_t));
18987c478bd9Sstevel@tonic-gate 	(void) memset(&expo1, 0x0, sizeof (biginteger_t));
18997c478bd9Sstevel@tonic-gate 	(void) memset(&expo2, 0x0, sizeof (biginteger_t));
19007c478bd9Sstevel@tonic-gate 	(void) memset(&coef, 0x0, sizeof (biginteger_t));
19017c478bd9Sstevel@tonic-gate 	string_tmp.pValue = NULL;
1902f9fbec18Smcpowers 	param_tmp.pValue = NULL;
19037c478bd9Sstevel@tonic-gate 	x.malloced = 0;
19047c478bd9Sstevel@tonic-gate 	q.malloced = 0;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		/* Private Key Object Attributes */
19097c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
19107c478bd9Sstevel@tonic-gate 		/* common key attributes */
19117c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
19127c478bd9Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
19137c478bd9Sstevel@tonic-gate 			break;
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 		case CKA_ID:
19167c478bd9Sstevel@tonic-gate 		case CKA_START_DATE:
19177c478bd9Sstevel@tonic-gate 		case CKA_END_DATE:
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 		/* common private key attribute */
19207c478bd9Sstevel@tonic-gate 		case CKA_SUBJECT:
19217c478bd9Sstevel@tonic-gate 			/*
19227c478bd9Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
19237c478bd9Sstevel@tonic-gate 			 * value with byte array type, and add it to
19247c478bd9Sstevel@tonic-gate 			 * the extra attribute list of the object.
19257c478bd9Sstevel@tonic-gate 			 */
19267c478bd9Sstevel@tonic-gate 			rv = soft_add_extra_attr(&template[i],
19277c478bd9Sstevel@tonic-gate 			    new_object);
19287c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
19297c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
19307c478bd9Sstevel@tonic-gate 			}
19317c478bd9Sstevel@tonic-gate 			break;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 		/*
19347c478bd9Sstevel@tonic-gate 		 * The following key related attribute types must
19357c478bd9Sstevel@tonic-gate 		 * not be specified by C_CreateObject or C_GenerateKey(Pair).
19367c478bd9Sstevel@tonic-gate 		 */
19377c478bd9Sstevel@tonic-gate 		case CKA_LOCAL:
19387c478bd9Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
19397c478bd9Sstevel@tonic-gate 		case CKA_AUTH_PIN_FLAGS:
19407c478bd9Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
19417c478bd9Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
19427c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
19437c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 		/* Key related boolean attributes */
19467c478bd9Sstevel@tonic-gate 		case CKA_DERIVE:
19477c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19487c478bd9Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
19497c478bd9Sstevel@tonic-gate 			break;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		case CKA_SENSITIVE:
19527c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19537c478bd9Sstevel@tonic-gate 				attr_mask |= SENSITIVE_BOOL_ON;
19547c478bd9Sstevel@tonic-gate 			break;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 		case CKA_SECONDARY_AUTH:
19577c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue) {
19587c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
19597c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
19607c478bd9Sstevel@tonic-gate 			}
19617c478bd9Sstevel@tonic-gate 			break;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		case CKA_DECRYPT:
19647c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19657c478bd9Sstevel@tonic-gate 				attr_mask |= DECRYPT_BOOL_ON;
19667c478bd9Sstevel@tonic-gate 			else
19677c478bd9Sstevel@tonic-gate 				attr_mask &= ~DECRYPT_BOOL_ON;
19687c478bd9Sstevel@tonic-gate 			break;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		case CKA_SIGN:
19717c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19727c478bd9Sstevel@tonic-gate 				attr_mask |= SIGN_BOOL_ON;
19737c478bd9Sstevel@tonic-gate 			else
19747c478bd9Sstevel@tonic-gate 				attr_mask &= ~SIGN_BOOL_ON;
19757c478bd9Sstevel@tonic-gate 			break;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 		case CKA_SIGN_RECOVER:
19787c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19797c478bd9Sstevel@tonic-gate 				attr_mask |= SIGN_RECOVER_BOOL_ON;
19807c478bd9Sstevel@tonic-gate 			else
19817c478bd9Sstevel@tonic-gate 				attr_mask &= ~SIGN_RECOVER_BOOL_ON;
19827c478bd9Sstevel@tonic-gate 			break;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 		case CKA_UNWRAP:
19857c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19867c478bd9Sstevel@tonic-gate 				attr_mask |= UNWRAP_BOOL_ON;
19877c478bd9Sstevel@tonic-gate 			else
19887c478bd9Sstevel@tonic-gate 				attr_mask &= ~UNWRAP_BOOL_ON;
19897c478bd9Sstevel@tonic-gate 			break;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
19927c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19937c478bd9Sstevel@tonic-gate 				attr_mask |= EXTRACTABLE_BOOL_ON;
19947c478bd9Sstevel@tonic-gate 			else
19957c478bd9Sstevel@tonic-gate 				attr_mask &= ~EXTRACTABLE_BOOL_ON;
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 		case CKA_MODIFIABLE:
19997c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template[i].pValue) == B_FALSE)
20007c478bd9Sstevel@tonic-gate 				attr_mask |= NOT_MODIFIABLE_BOOL_ON;
20017c478bd9Sstevel@tonic-gate 			break;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 		/*
20047c478bd9Sstevel@tonic-gate 		 * The following key related attribute types must
20057c478bd9Sstevel@tonic-gate 		 * be specified according to the key type by
20067c478bd9Sstevel@tonic-gate 		 * C_CreateObject.
20077c478bd9Sstevel@tonic-gate 		 */
20087c478bd9Sstevel@tonic-gate 		case CKA_MODULUS:
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 			isModulus = 1;
20117c478bd9Sstevel@tonic-gate 			/*
20127c478bd9Sstevel@tonic-gate 			 * Copyin big integer attribute from template
20137c478bd9Sstevel@tonic-gate 			 * to a local variable.
20147c478bd9Sstevel@tonic-gate 			 */
20157c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&modulus,
20167c478bd9Sstevel@tonic-gate 			    &template[i]);
20177c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20187c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 			/*
20217c478bd9Sstevel@tonic-gate 			 * Modulus length needs to be between min key length and
20227c478bd9Sstevel@tonic-gate 			 * max key length.
20237c478bd9Sstevel@tonic-gate 			 */
20247c478bd9Sstevel@tonic-gate 			if ((modulus.big_value_len <
20257c478bd9Sstevel@tonic-gate 			    MIN_RSA_KEYLENGTH_IN_BYTES) ||
20267c478bd9Sstevel@tonic-gate 			    (modulus.big_value_len >
20277c478bd9Sstevel@tonic-gate 			    MAX_RSA_KEYLENGTH_IN_BYTES)) {
20287c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
20297c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20307c478bd9Sstevel@tonic-gate 			}
20317c478bd9Sstevel@tonic-gate 			break;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 			isPubExpo = 1;
20367c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&pubexpo,
20377c478bd9Sstevel@tonic-gate 			    &template[i]);
20387c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20397c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20407c478bd9Sstevel@tonic-gate 			break;
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 			isPriExpo = 1;
20457c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&priexpo,
20467c478bd9Sstevel@tonic-gate 			    &template[i]);
20477c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20487c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20497c478bd9Sstevel@tonic-gate 			break;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 		case CKA_PRIME_1:
20527c478bd9Sstevel@tonic-gate 			isPrime1 = 1;
20537c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime1,
20547c478bd9Sstevel@tonic-gate 			    &template[i]);
20557c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20567c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20577c478bd9Sstevel@tonic-gate 			break;
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 		case CKA_PRIME_2:
20607c478bd9Sstevel@tonic-gate 			isPrime2 = 1;
20617c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime2,
20627c478bd9Sstevel@tonic-gate 			    &template[i]);
20637c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20647c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20657c478bd9Sstevel@tonic-gate 			break;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_1:
20687c478bd9Sstevel@tonic-gate 			isExpo1 = 1;
20697c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&expo1,
20707c478bd9Sstevel@tonic-gate 			    &template[i]);
20717c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20727c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20737c478bd9Sstevel@tonic-gate 			break;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_2:
20767c478bd9Sstevel@tonic-gate 			isExpo2 = 1;
20777c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&expo2,
20787c478bd9Sstevel@tonic-gate 			    &template[i]);
20797c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20807c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20817c478bd9Sstevel@tonic-gate 			break;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 		case CKA_COEFFICIENT:
20847c478bd9Sstevel@tonic-gate 			isCoef = 1;
20857c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&coef,
20867c478bd9Sstevel@tonic-gate 			    &template[i]);
20877c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20887c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20897c478bd9Sstevel@tonic-gate 			break;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		case CKA_PRIME:
20927c478bd9Sstevel@tonic-gate 			isPrime = 1;
20937c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime,
20947c478bd9Sstevel@tonic-gate 			    &template[i]);
20957c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
20967c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
20977c478bd9Sstevel@tonic-gate 			break;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 		case CKA_SUBPRIME:
21007c478bd9Sstevel@tonic-gate 			isSubprime = 1;
21017c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&subprime,
21027c478bd9Sstevel@tonic-gate 			    &template[i]);
21037c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
21047c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
21057c478bd9Sstevel@tonic-gate 			break;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 		case CKA_BASE:
21087c478bd9Sstevel@tonic-gate 			isBase = 1;
21097c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&base,
21107c478bd9Sstevel@tonic-gate 			    &template[i]);
21117c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
21127c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
21137c478bd9Sstevel@tonic-gate 			break;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
21167c478bd9Sstevel@tonic-gate 			isValue = 1;
21177c478bd9Sstevel@tonic-gate 			if (mode == SOFT_CREATE_OBJ) {
21187c478bd9Sstevel@tonic-gate 				if ((template[i].ulValueLen == 0) ||
21197c478bd9Sstevel@tonic-gate 				    (template[i].pValue == NULL)) {
21207c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
21217c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
21227c478bd9Sstevel@tonic-gate 				}
21237c478bd9Sstevel@tonic-gate 			}
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&value,
21267c478bd9Sstevel@tonic-gate 			    &template[i]);
21277c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
21287c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
21297c478bd9Sstevel@tonic-gate 			break;
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		case CKA_VALUE_BITS:
21327c478bd9Sstevel@tonic-gate 			isValueBits = 1;
21337739299dSAnthony Scarpino 			rv = get_ulong_attr_from_template(&value_bits,
21347c478bd9Sstevel@tonic-gate 			    &template[i]);
21357739299dSAnthony Scarpino 			if (rv != CKR_OK)
21367739299dSAnthony Scarpino 				goto fail_cleanup;
21377c478bd9Sstevel@tonic-gate 			break;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
21407c478bd9Sstevel@tonic-gate 			isLabel = 1;
21417c478bd9Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
21427c478bd9Sstevel@tonic-gate 			    &template[i]);
21437c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
21447c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
21457c478bd9Sstevel@tonic-gate 			break;
21467c478bd9Sstevel@tonic-gate 
2147034448feSmcpowers 		case CKA_EC_PARAMS:
2148034448feSmcpowers 			isECParam = 1;
2149f9fbec18Smcpowers 			rv = get_string_from_template(&param_tmp,
2150034448feSmcpowers 			    &template[i]);
2151034448feSmcpowers 			if (rv != CKR_OK)
2152034448feSmcpowers 				goto fail_cleanup;
2153034448feSmcpowers 			break;
2154034448feSmcpowers 
21557c478bd9Sstevel@tonic-gate 		default:
21567c478bd9Sstevel@tonic-gate 			rv = soft_parse_common_attrs(&template[i],
21577c478bd9Sstevel@tonic-gate 			    &object_type);
21587c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
21597c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
21607c478bd9Sstevel@tonic-gate 			break;
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 		}
21637c478bd9Sstevel@tonic-gate 	} /* For */
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	/* Allocate storage for Private Key Object. */
21667c478bd9Sstevel@tonic-gate 	pvk = calloc(1, sizeof (private_key_obj_t));
21677c478bd9Sstevel@tonic-gate 	if (pvk == NULL) {
21687c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
21697c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	new_object->object_class_u.private_key = pvk;
21737c478bd9Sstevel@tonic-gate 	new_object->class = CKO_PRIVATE_KEY;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if ((mode == SOFT_CREATE_OBJ) && (keytype == (CK_KEY_TYPE)~0UL)) {
21767c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
21777c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	if (mode == SOFT_GEN_KEY) {
21817c478bd9Sstevel@tonic-gate 		/*
21827c478bd9Sstevel@tonic-gate 		 * The key type is not specified in the application's
21837c478bd9Sstevel@tonic-gate 		 * template, so we use the implied key type based on
21847c478bd9Sstevel@tonic-gate 		 * the mechanism.
21857c478bd9Sstevel@tonic-gate 		 */
21867c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
21877c478bd9Sstevel@tonic-gate 			keytype = key_type;
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 		/* If still unspecified, template is incomplete */
21917c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
21927c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
21937c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 		/*
21977c478bd9Sstevel@tonic-gate 		 * The key type specified in the template does not
21987c478bd9Sstevel@tonic-gate 		 * match the implied key type based on the mechanism.
21997c478bd9Sstevel@tonic-gate 		 */
22007c478bd9Sstevel@tonic-gate 		if (keytype != key_type) {
22017c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
22027c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
22037c478bd9Sstevel@tonic-gate 		}
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	if (mode == SOFT_UNWRAP_KEY) {
22077c478bd9Sstevel@tonic-gate 		/*
22087c478bd9Sstevel@tonic-gate 		 * Note that, for mode SOFT_UNWRAP_KEY, key type is not
22097c478bd9Sstevel@tonic-gate 		 * implied by the mechanism (key_type), so if it is not
22107c478bd9Sstevel@tonic-gate 		 * specified from the attribute template (keytype), it is
22117c478bd9Sstevel@tonic-gate 		 * incomplete.
22127c478bd9Sstevel@tonic-gate 		 */
22137c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
22147c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
22157c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	new_object->key_type = keytype;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	/* Supported key types of the Private Key Object */
22227c478bd9Sstevel@tonic-gate 	switch (keytype) {
22237c478bd9Sstevel@tonic-gate 	case CKK_RSA:
22247c478bd9Sstevel@tonic-gate 		if (isPrime || isSubprime || isBase || isValue ||
22257c478bd9Sstevel@tonic-gate 		    isValueBits) {
22267c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
22277c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
22287c478bd9Sstevel@tonic-gate 		}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		if (mode == SOFT_GEN_KEY || mode == SOFT_UNWRAP_KEY) {
22317c478bd9Sstevel@tonic-gate 			if (isModulus || isPubExpo || isPriExpo || isPrime1 ||
22327c478bd9Sstevel@tonic-gate 			    isPrime2 || isExpo1 || isExpo2 || isCoef) {
22337c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
22347c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
22357c478bd9Sstevel@tonic-gate 			} else
22367c478bd9Sstevel@tonic-gate 				break;
22377c478bd9Sstevel@tonic-gate 		}
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 		if (isModulus && isPriExpo) {
22407c478bd9Sstevel@tonic-gate 			/*
22417c478bd9Sstevel@tonic-gate 			 * Copy big integer attribute value to the
22427c478bd9Sstevel@tonic-gate 			 * designated place in the Private Key object.
22437c478bd9Sstevel@tonic-gate 			 */
22447c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&modulus, KEY_PRI_RSA_MOD(pvk));
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&priexpo, KEY_PRI_RSA_PRIEXPO(pvk));
22477c478bd9Sstevel@tonic-gate 		} else {
22487c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
22497c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
22507c478bd9Sstevel@tonic-gate 		}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		/* The following attributes are optional. */
22537c478bd9Sstevel@tonic-gate 		if (isPubExpo) {
22547c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&pubexpo, KEY_PRI_RSA_PUBEXPO(pvk));
22557c478bd9Sstevel@tonic-gate 		}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		if (isPrime1) {
22587c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime1, KEY_PRI_RSA_PRIME1(pvk));
22597c478bd9Sstevel@tonic-gate 		}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		if (isPrime2) {
22627c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime2, KEY_PRI_RSA_PRIME2(pvk));
22637c478bd9Sstevel@tonic-gate 		}
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		if (isExpo1) {
22667c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&expo1, KEY_PRI_RSA_EXPO1(pvk));
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 		if (isExpo2) {
22707c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&expo2, KEY_PRI_RSA_EXPO2(pvk));
22717c478bd9Sstevel@tonic-gate 		}
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 		if (isCoef) {
22747c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&coef, KEY_PRI_RSA_COEF(pvk));
22757c478bd9Sstevel@tonic-gate 		}
22767c478bd9Sstevel@tonic-gate 		break;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	case CKK_DSA:
22797c478bd9Sstevel@tonic-gate 		if (isModulus || isPubExpo || isPriExpo || isPrime1 ||
22807c478bd9Sstevel@tonic-gate 		    isPrime2 || isExpo1 || isExpo2 || isCoef ||
22817c478bd9Sstevel@tonic-gate 		    isValueBits) {
22827c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
22837c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
22847c478bd9Sstevel@tonic-gate 		}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 		if (mode == SOFT_GEN_KEY || mode == SOFT_UNWRAP_KEY) {
22877c478bd9Sstevel@tonic-gate 			if (isPrime || isSubprime || isBase || isValue) {
22887c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
22897c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
22907c478bd9Sstevel@tonic-gate 			} else
22917c478bd9Sstevel@tonic-gate 				break;
22927c478bd9Sstevel@tonic-gate 		}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 		if (isPrime && isSubprime && isBase && isValue) {
22957c478bd9Sstevel@tonic-gate 			/*
22967c478bd9Sstevel@tonic-gate 			 * The private value x must be less than subprime q.
2297b60f2a0bSfr 			 * Size for big_init is in BIG_CHUNK_TYPE words.
22987c478bd9Sstevel@tonic-gate 			 */
22997c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
2300b60f2a0bSfr 			if (big_init(&x,
2301b60f2a0bSfr 			    (int)CHARLEN2BIGNUMLEN(value.big_value_len))
2302b60f2a0bSfr 			    != BIG_OK) {
23037c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
2304b60f2a0bSfr 			if (big_init(&x,
2305b60f2a0bSfr 			    CHARLEN2BIGNUMLEN(value.big_value_len))
2306b60f2a0bSfr 			    != BIG_OK) {
2307f9fbec18Smcpowers #endif	/* __sparcv9 */
23087c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
23097c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
23107c478bd9Sstevel@tonic-gate 			}
23117c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
2312b60f2a0bSfr 			if (big_init(&q,
2313b60f2a0bSfr 			    (int)CHARLEN2BIGNUMLEN(subprime.big_value_len))
2314b60f2a0bSfr 			    != BIG_OK) {
23157c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
2316b60f2a0bSfr 			if (big_init(&q,
2317b60f2a0bSfr 			    CHARLEN2BIGNUMLEN(subprime.big_value_len))
2318b60f2a0bSfr 			    != BIG_OK) {
2319f9fbec18Smcpowers #endif	/* __sparcv9 */
23207c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
23217c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
23227c478bd9Sstevel@tonic-gate 			}
23237c478bd9Sstevel@tonic-gate 			bytestring2bignum(&x, value.big_value,
23247c478bd9Sstevel@tonic-gate 			    value.big_value_len);
23257c478bd9Sstevel@tonic-gate 			bytestring2bignum(&q, subprime.big_value,
23267c478bd9Sstevel@tonic-gate 			    subprime.big_value_len);
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 			if (big_cmp_abs(&x, &q) > 0) {
23297c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
23307c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
23317c478bd9Sstevel@tonic-gate 			}
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_PRI_DSA_PRIME(pvk));
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&subprime, KEY_PRI_DSA_SUBPRIME(pvk));
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_PRI_DSA_BASE(pvk));
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&value, KEY_PRI_DSA_VALUE(pvk));
23407c478bd9Sstevel@tonic-gate 		} else {
23417c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
23427c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
23437c478bd9Sstevel@tonic-gate 		}
23447c478bd9Sstevel@tonic-gate 		break;
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	case CKK_DH:
23477c478bd9Sstevel@tonic-gate 		if (isModulus || isPubExpo || isPriExpo || isPrime1 ||
23487c478bd9Sstevel@tonic-gate 		    isPrime2 || isExpo1 || isExpo2 || isCoef ||
23497c478bd9Sstevel@tonic-gate 		    isSubprime) {
23507c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
23517c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 		/* CKA_VALUE_BITS is for key gen but not unwrap */
23557c478bd9Sstevel@tonic-gate 		if (mode == SOFT_GEN_KEY)
23567c478bd9Sstevel@tonic-gate 			KEY_PRI_DH_VAL_BITS(pvk) = (isValueBits) ?
23577c478bd9Sstevel@tonic-gate 			    value_bits : 0;
23587c478bd9Sstevel@tonic-gate 		else if (mode == SOFT_UNWRAP_KEY) {
23597c478bd9Sstevel@tonic-gate 			if (isValueBits) {
23607c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
23617c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
23627c478bd9Sstevel@tonic-gate 			}
23637c478bd9Sstevel@tonic-gate 		}
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 		if (mode == SOFT_GEN_KEY || mode == SOFT_UNWRAP_KEY) {
23667c478bd9Sstevel@tonic-gate 			if (isPrime || isBase || isValue) {
23677c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
23687c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
23697c478bd9Sstevel@tonic-gate 			} else
23707c478bd9Sstevel@tonic-gate 				break;
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 		if (isValueBits) {
23747c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
23757c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
23767c478bd9Sstevel@tonic-gate 		}
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		if (isPrime && isBase && isValue) {
23797c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_PRI_DH_PRIME(pvk));
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_PRI_DH_BASE(pvk));
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&value, KEY_PRI_DH_VALUE(pvk));
23847c478bd9Sstevel@tonic-gate 		} else {
23857c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
23867c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
23877c478bd9Sstevel@tonic-gate 		}
23887c478bd9Sstevel@tonic-gate 		break;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	case CKK_X9_42_DH:
23917c478bd9Sstevel@tonic-gate 		if (isModulus || isPubExpo || isPriExpo || isPrime1 ||
23927c478bd9Sstevel@tonic-gate 		    isPrime2 || isExpo1 || isExpo2 || isCoef ||
23937c478bd9Sstevel@tonic-gate 		    isValueBits) {
23947c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
23957c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
23967c478bd9Sstevel@tonic-gate 		}
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 		if (mode == SOFT_GEN_KEY || mode == SOFT_UNWRAP_KEY) {
23997c478bd9Sstevel@tonic-gate 			if (isPrime || isSubprime || isBase || isValue) {
24007c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
24017c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
24027c478bd9Sstevel@tonic-gate 			} else
24037c478bd9Sstevel@tonic-gate 				break;
24047c478bd9Sstevel@tonic-gate 		}
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		if (isPrime && isSubprime && isBase && isValue) {
24077c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_PRI_DH942_PRIME(pvk));
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_PRI_DH942_BASE(pvk));
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&subprime,
2412f9fbec18Smcpowers 			    KEY_PRI_DH942_SUBPRIME(pvk));
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&value, KEY_PRI_DH942_VALUE(pvk));
24157c478bd9Sstevel@tonic-gate 		} else {
24167c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
24177c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 		break;
24207c478bd9Sstevel@tonic-gate 
2421034448feSmcpowers 	case CKK_EC:
2422034448feSmcpowers 		if (isModulus || isPubExpo || isPrime ||
2423034448feSmcpowers 		    isPrime1 || isPrime2 || isExpo1 || isExpo2 || isCoef ||
2424f9fbec18Smcpowers 		    isValueBits || isBase) {
2425034448feSmcpowers 			rv = CKR_TEMPLATE_INCONSISTENT;
2426034448feSmcpowers 			goto fail_cleanup;
2427034448feSmcpowers 
2428f9fbec18Smcpowers 		} else if (isECParam) {
2429f9fbec18Smcpowers 			rv = soft_add_extra_attr(&param_tmp, new_object);
2430f9fbec18Smcpowers 			if (rv != CKR_OK)
2431f9fbec18Smcpowers 				goto fail_cleanup;
2432f9fbec18Smcpowers 			string_attr_cleanup(&param_tmp);
2433f9fbec18Smcpowers 		}
2434f9fbec18Smcpowers 		if (isValue) {
2435f9fbec18Smcpowers 			copy_bigint_attr(&value, KEY_PRI_EC_VALUE(pvk));
2436034448feSmcpowers 		}
2437034448feSmcpowers 		break;
2438034448feSmcpowers 
24397c478bd9Sstevel@tonic-gate 	default:
24407c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
24417c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	/* Set up object. */
24457c478bd9Sstevel@tonic-gate 	new_object->object_type = object_type;
24467c478bd9Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
24477c478bd9Sstevel@tonic-gate 	if (isLabel) {
24487c478bd9Sstevel@tonic-gate 		rv = soft_add_extra_attr(&string_tmp, new_object);
24497c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
24507c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
24517c478bd9Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
24527c478bd9Sstevel@tonic-gate 	}
24537c478bd9Sstevel@tonic-gate 	big_finish(&x);
24547c478bd9Sstevel@tonic-gate 	big_finish(&q);
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	return (rv);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate fail_cleanup:
24597c478bd9Sstevel@tonic-gate 	/*
24607c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
24617c478bd9Sstevel@tonic-gate 	 */
24627c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&modulus);
24637c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&priexpo);
24647c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&prime);
24657c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&subprime);
24667c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&base);
24677c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&value);
24687c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&pubexpo);
24697c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&prime1);
24707c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&prime2);
24717c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&expo1);
24727c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&expo2);
24737c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&coef);
24747c478bd9Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
2475f9fbec18Smcpowers 	string_attr_cleanup(&param_tmp);
24767c478bd9Sstevel@tonic-gate 	big_finish(&x);
24777c478bd9Sstevel@tonic-gate 	big_finish(&q);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	/*
24807c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
24817c478bd9Sstevel@tonic-gate 	 */
24827c478bd9Sstevel@tonic-gate 	soft_cleanup_object(new_object);
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	return (rv);
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate /*
24897c478bd9Sstevel@tonic-gate  * Build a Secret Key Object.
24907c478bd9Sstevel@tonic-gate  *
24917c478bd9Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
24927c478bd9Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
24937c478bd9Sstevel@tonic-gate  *   with appropriate return value.
24947c478bd9Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
24957c478bd9Sstevel@tonic-gate  *   attributes that have boolean type.
24967c478bd9Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
24977c478bd9Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
24987c478bd9Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
24997c478bd9Sstevel@tonic-gate  * - Allocate storage for the Secret Key object.
25007c478bd9Sstevel@tonic-gate  * - Build the Secret Key object. Allocate storage to hold the big integer
25017c478bd9Sstevel@tonic-gate  *   value for the attribute CKA_VALUE that is required for all the key
25027c478bd9Sstevel@tonic-gate  *   types supported by secret key object.
25037c478bd9Sstevel@tonic-gate  * This function is called internally with mode = SOFT_CREATE_OBJ_INT.
25047c478bd9Sstevel@tonic-gate  *
25057c478bd9Sstevel@tonic-gate  */
25067c478bd9Sstevel@tonic-gate CK_RV
25077c478bd9Sstevel@tonic-gate soft_build_secret_key_object(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
2508*a8793c76SJason King     soft_object_t *new_object, CK_ULONG mode, CK_ULONG key_len,
2509*a8793c76SJason King     CK_KEY_TYPE key_type)
25107c478bd9Sstevel@tonic-gate {
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	ulong_t		i;
25137c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
25147c478bd9Sstevel@tonic-gate 	uint64_t	attr_mask = SECRET_KEY_DEFAULT;
2515*a8793c76SJason King 	CK_RV		rv = CKR_OK;
25167c478bd9Sstevel@tonic-gate 	int		isLabel = 0;
25177c478bd9Sstevel@tonic-gate 	/* Must set flags if mode != SOFT_UNWRAP_KEY, else must not set */
25187c478bd9Sstevel@tonic-gate 	int		isValue = 0;
25197c478bd9Sstevel@tonic-gate 	/* Must not set flags if mode != SOFT_UNWRAP_KEY, else optional */
25207c478bd9Sstevel@tonic-gate 	int		isValueLen = 0;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	secret_key_obj_t  *sck;
25257c478bd9Sstevel@tonic-gate 	uchar_t	object_type = 0;
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	string_tmp.pValue = NULL;
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	/* Allocate storage for Secret Key Object. */
25307c478bd9Sstevel@tonic-gate 	sck = calloc(1, sizeof (secret_key_obj_t));
25317c478bd9Sstevel@tonic-gate 	if (sck == NULL) {
25327c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
25337c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
25347c478bd9Sstevel@tonic-gate 	}
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	new_object->object_class_u.secret_key = sck;
25377c478bd9Sstevel@tonic-gate 	new_object->class = CKO_SECRET_KEY;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 		/* Secret Key Object Attributes */
25427c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 		/* common key attributes */
25457c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
25467c478bd9Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
25477c478bd9Sstevel@tonic-gate 			break;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 		case CKA_ID:
25507c478bd9Sstevel@tonic-gate 		case CKA_START_DATE:
25517c478bd9Sstevel@tonic-gate 		case CKA_END_DATE:
25527c478bd9Sstevel@tonic-gate 			/*
25537c478bd9Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
25547c478bd9Sstevel@tonic-gate 			 * value with byte array type, and add it to
25557c478bd9Sstevel@tonic-gate 			 * the extra attribute list of the object.
25567c478bd9Sstevel@tonic-gate 			 */
25577c478bd9Sstevel@tonic-gate 			rv = soft_add_extra_attr(&template[i],
25587c478bd9Sstevel@tonic-gate 			    new_object);
25597c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
25607c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
25617c478bd9Sstevel@tonic-gate 			}
25627c478bd9Sstevel@tonic-gate 			break;
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 		/*
25657c478bd9Sstevel@tonic-gate 		 * The following key related attribute types must
25667c478bd9Sstevel@tonic-gate 		 * not be specified by C_CreateObject and C_GenerateKey.
25677c478bd9Sstevel@tonic-gate 		 */
25687c478bd9Sstevel@tonic-gate 		case CKA_LOCAL:
25697c478bd9Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
25707c478bd9Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
25717c478bd9Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
25727c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
25737c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 		/* Key related boolean attributes */
25767c478bd9Sstevel@tonic-gate 		case CKA_DERIVE:
25777c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
25787c478bd9Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
25797c478bd9Sstevel@tonic-gate 			break;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 		case CKA_SENSITIVE:
25827c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
25837c478bd9Sstevel@tonic-gate 				attr_mask |= SENSITIVE_BOOL_ON;
25847c478bd9Sstevel@tonic-gate 			break;
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 		case CKA_ENCRYPT:
25877c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
25887c478bd9Sstevel@tonic-gate 				attr_mask |= ENCRYPT_BOOL_ON;
25897c478bd9Sstevel@tonic-gate 			else
25907c478bd9Sstevel@tonic-gate 				attr_mask &= ~ENCRYPT_BOOL_ON;
25917c478bd9Sstevel@tonic-gate 			break;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 		case CKA_DECRYPT:
25947c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
25957c478bd9Sstevel@tonic-gate 				attr_mask |= DECRYPT_BOOL_ON;
25967c478bd9Sstevel@tonic-gate 			else
25977c478bd9Sstevel@tonic-gate 				attr_mask &= ~DECRYPT_BOOL_ON;
25987c478bd9Sstevel@tonic-gate 			break;
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 		case CKA_SIGN:
26017c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
26027c478bd9Sstevel@tonic-gate 				attr_mask |= SIGN_BOOL_ON;
26037c478bd9Sstevel@tonic-gate 			else
26047c478bd9Sstevel@tonic-gate 				attr_mask &= ~SIGN_BOOL_ON;
26057c478bd9Sstevel@tonic-gate 			break;
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 		case CKA_VERIFY:
26087c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
26097c478bd9Sstevel@tonic-gate 				attr_mask |= VERIFY_BOOL_ON;
26107c478bd9Sstevel@tonic-gate 			else
26117c478bd9Sstevel@tonic-gate 				attr_mask &= ~VERIFY_BOOL_ON;
26127c478bd9Sstevel@tonic-gate 			break;
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 		case CKA_WRAP:
26157c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
26167c478bd9Sstevel@tonic-gate 				attr_mask |= WRAP_BOOL_ON;
26177c478bd9Sstevel@tonic-gate 			else
26187c478bd9Sstevel@tonic-gate 				attr_mask &= ~WRAP_BOOL_ON;
26197c478bd9Sstevel@tonic-gate 			break;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 		case CKA_UNWRAP:
26227c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
26237c478bd9Sstevel@tonic-gate 				attr_mask |= UNWRAP_BOOL_ON;
26247c478bd9Sstevel@tonic-gate 			else
26257c478bd9Sstevel@tonic-gate 				attr_mask &= ~UNWRAP_BOOL_ON;
26267c478bd9Sstevel@tonic-gate 			break;
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
26297c478bd9Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
26307c478bd9Sstevel@tonic-gate 				attr_mask |= EXTRACTABLE_BOOL_ON;
26317c478bd9Sstevel@tonic-gate 			else
26327c478bd9Sstevel@tonic-gate 				attr_mask &= ~EXTRACTABLE_BOOL_ON;
26337c478bd9Sstevel@tonic-gate 			break;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 		case CKA_MODIFIABLE:
26367c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template[i].pValue) == B_FALSE)
26377c478bd9Sstevel@tonic-gate 				attr_mask |= NOT_MODIFIABLE_BOOL_ON;
26387c478bd9Sstevel@tonic-gate 			break;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
26417c478bd9Sstevel@tonic-gate 			isValue = 1;
26427c478bd9Sstevel@tonic-gate 			if (mode == SOFT_CREATE_OBJ) {
26437c478bd9Sstevel@tonic-gate 				if ((template[i].ulValueLen == 0) ||
26447c478bd9Sstevel@tonic-gate 				    (template[i].pValue == NULL)) {
26457c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
26467c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
26477c478bd9Sstevel@tonic-gate 				}
26487c478bd9Sstevel@tonic-gate 			}
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 			/*
26517c478bd9Sstevel@tonic-gate 			 * Copyin attribute from template
26527c478bd9Sstevel@tonic-gate 			 * to a local variable.
26537c478bd9Sstevel@tonic-gate 			 */
26547c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template((biginteger_t *)sck,
26557c478bd9Sstevel@tonic-gate 			    &template[i]);
26567c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
26577c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
26587c478bd9Sstevel@tonic-gate 			break;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 		case CKA_VALUE_LEN:
26617c478bd9Sstevel@tonic-gate 			isValueLen = 1;
26627739299dSAnthony Scarpino 			rv = get_ulong_attr_from_template(&sck->sk_value_len,
26637c478bd9Sstevel@tonic-gate 			    &template[i]);
26647739299dSAnthony Scarpino 			if (rv != CKR_OK)
26657739299dSAnthony Scarpino 				goto fail_cleanup;
26667c478bd9Sstevel@tonic-gate 			break;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
26697c478bd9Sstevel@tonic-gate 			isLabel = 1;
26707c478bd9Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
26717c478bd9Sstevel@tonic-gate 			    &template[i]);
26727c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
26737c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
26747c478bd9Sstevel@tonic-gate 			break;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 		default:
26777c478bd9Sstevel@tonic-gate 			rv = soft_parse_common_attrs(&template[i],
26787c478bd9Sstevel@tonic-gate 			    &object_type);
26797c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
26807c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
26817c478bd9Sstevel@tonic-gate 			break;
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 	} /* For */
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 	switch (mode) {
26877c478bd9Sstevel@tonic-gate 	case SOFT_CREATE_OBJ:
26887c478bd9Sstevel@tonic-gate 	case SOFT_CREATE_OBJ_INT:
26897c478bd9Sstevel@tonic-gate 	case SOFT_DERIVE_KEY_DH:
26907c478bd9Sstevel@tonic-gate 		/*
26917c478bd9Sstevel@tonic-gate 		 * The key type must be specified in the application's
26927c478bd9Sstevel@tonic-gate 		 * template. Otherwise, returns error.
26937c478bd9Sstevel@tonic-gate 		 */
26947c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
26957c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
26967c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
26977c478bd9Sstevel@tonic-gate 		}
26987c478bd9Sstevel@tonic-gate 		break;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	case SOFT_GEN_KEY:
27017c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
27027c478bd9Sstevel@tonic-gate 			/*
27037c478bd9Sstevel@tonic-gate 			 * The key type is not specified in the application's
27047c478bd9Sstevel@tonic-gate 			 * template, so we use the implied key type based on
27057c478bd9Sstevel@tonic-gate 			 * the mechanism.
27067c478bd9Sstevel@tonic-gate 			 */
27077c478bd9Sstevel@tonic-gate 			keytype = key_type;
27087c478bd9Sstevel@tonic-gate 		} else {
27097c478bd9Sstevel@tonic-gate 			if (keytype != key_type) {
27107c478bd9Sstevel@tonic-gate 				/*
27117c478bd9Sstevel@tonic-gate 				 * The key type specified in the template
27127c478bd9Sstevel@tonic-gate 				 * does not match the implied key type based
27137c478bd9Sstevel@tonic-gate 				 * on the mechanism.
27147c478bd9Sstevel@tonic-gate 				 */
27157c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
27167c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27177c478bd9Sstevel@tonic-gate 			}
27187c478bd9Sstevel@tonic-gate 		}
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 		/*
27217c478bd9Sstevel@tonic-gate 		 * If a key_len is passed as a parameter, it has to
27227c478bd9Sstevel@tonic-gate 		 * match the one found in the template.
27237c478bd9Sstevel@tonic-gate 		 */
27247c478bd9Sstevel@tonic-gate 		if (key_len > 0) {
27257c478bd9Sstevel@tonic-gate 			if (isValueLen && sck->sk_value_len != key_len) {
27267c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
27277c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27287c478bd9Sstevel@tonic-gate 			}
27297c478bd9Sstevel@tonic-gate 			isValueLen = 1;
27307c478bd9Sstevel@tonic-gate 			sck->sk_value_len = key_len;
27317c478bd9Sstevel@tonic-gate 		}
27327c478bd9Sstevel@tonic-gate 		break;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	case SOFT_UNWRAP_KEY:
27357c478bd9Sstevel@tonic-gate 		/*
27367c478bd9Sstevel@tonic-gate 		 * Note that, for mode SOFT_UNWRAP_KEY, key type is not
27377c478bd9Sstevel@tonic-gate 		 * implied by the mechanism (key_type), so if it is not
27387c478bd9Sstevel@tonic-gate 		 * specified from the attribute template (keytype), it is
27397c478bd9Sstevel@tonic-gate 		 * incomplete.
27407c478bd9Sstevel@tonic-gate 		 */
27417c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
27427c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
27437c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
27447c478bd9Sstevel@tonic-gate 		}
27457c478bd9Sstevel@tonic-gate 		break;
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	case SOFT_DERIVE_KEY_OTHER:
27487c478bd9Sstevel@tonic-gate 		/*
27497c478bd9Sstevel@tonic-gate 		 * For CKM_MD5_KEY_DERIVATION & CKM_SHA1_KEY_DERIVATION, the
27507c478bd9Sstevel@tonic-gate 		 * key type is optional.
27517c478bd9Sstevel@tonic-gate 		 */
27527c478bd9Sstevel@tonic-gate 		if (keytype == (CK_KEY_TYPE)~0UL) {
27537c478bd9Sstevel@tonic-gate 			keytype = key_type;
27547c478bd9Sstevel@tonic-gate 		}
27557c478bd9Sstevel@tonic-gate 		break;
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	switch (mode) {
27597c478bd9Sstevel@tonic-gate 	case SOFT_CREATE_OBJ:
27607c478bd9Sstevel@tonic-gate 	case SOFT_CREATE_OBJ_INT:
27617c478bd9Sstevel@tonic-gate 		switch (keytype) {
27627c478bd9Sstevel@tonic-gate 		case CKK_RC4:
27637c478bd9Sstevel@tonic-gate 			if (!isValue) {
27647c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
27657c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27667c478bd9Sstevel@tonic-gate 			}
27677c478bd9Sstevel@tonic-gate 			if ((sck->sk_value_len < ARCFOUR_MIN_KEY_BYTES) ||
27687c478bd9Sstevel@tonic-gate 			    (sck->sk_value_len > ARCFOUR_MAX_KEY_BYTES)) {
27697c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
27707c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27717c478bd9Sstevel@tonic-gate 			}
27727c478bd9Sstevel@tonic-gate 			break;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
27757c478bd9Sstevel@tonic-gate 			if (!isValue) {
27767c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
27777c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27787c478bd9Sstevel@tonic-gate 			}
27797c478bd9Sstevel@tonic-gate 			break;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 		case CKK_AES:
27827c478bd9Sstevel@tonic-gate 			if (!isValue) {
27837c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
27847c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27857c478bd9Sstevel@tonic-gate 			}
27867c478bd9Sstevel@tonic-gate 			if ((sck->sk_value_len != AES_MIN_KEY_BYTES) &&
27877c478bd9Sstevel@tonic-gate 			    (sck->sk_value_len != AES_192_KEY_BYTES) &&
27887c478bd9Sstevel@tonic-gate 			    (sck->sk_value_len != AES_MAX_KEY_BYTES)) {
27897c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
27907c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
27917c478bd9Sstevel@tonic-gate 			}
27927c478bd9Sstevel@tonic-gate 			break;
27937c478bd9Sstevel@tonic-gate 
2794f66d273dSizick 		case CKK_BLOWFISH:
2795f66d273dSizick 			if (!isValue) {
2796f66d273dSizick 				rv = CKR_TEMPLATE_INCOMPLETE;
2797f66d273dSizick 				goto fail_cleanup;
2798f66d273dSizick 			}
2799f66d273dSizick 			if ((sck->sk_value_len < BLOWFISH_MINBYTES) ||
2800f66d273dSizick 			    (sck->sk_value_len > BLOWFISH_MAXBYTES)) {
2801f66d273dSizick 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
2802f66d273dSizick 				goto fail_cleanup;
2803f66d273dSizick 			}
2804f66d273dSizick 
2805f66d273dSizick 			break;
2806f66d273dSizick 
28077c478bd9Sstevel@tonic-gate 		case CKK_DES:
28087c478bd9Sstevel@tonic-gate 			if (!isValue) {
28097c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
28107c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28117c478bd9Sstevel@tonic-gate 			}
28127c478bd9Sstevel@tonic-gate 			if (sck->sk_value_len != DES_KEYSIZE) {
28137c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
28147c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28157c478bd9Sstevel@tonic-gate 			}
28167c478bd9Sstevel@tonic-gate 			break;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 		case CKK_DES2:
28197c478bd9Sstevel@tonic-gate 			if (!isValue) {
28207c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
28217c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28227c478bd9Sstevel@tonic-gate 			}
28237c478bd9Sstevel@tonic-gate 			if (sck->sk_value_len != DES2_KEYSIZE) {
28247c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
28257c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28267c478bd9Sstevel@tonic-gate 			}
28277c478bd9Sstevel@tonic-gate 			break;
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 		case CKK_DES3:
28307c478bd9Sstevel@tonic-gate 			if (!isValue) {
28317c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
28327c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28337c478bd9Sstevel@tonic-gate 			}
28347c478bd9Sstevel@tonic-gate 			if (sck->sk_value_len != DES3_KEYSIZE) {
28357c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
28367c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28377c478bd9Sstevel@tonic-gate 			}
28387c478bd9Sstevel@tonic-gate 			break;
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		default:
28417c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
28427c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
28437c478bd9Sstevel@tonic-gate 		}
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 		if (isValueLen) {
28467c478bd9Sstevel@tonic-gate 			/*
28477c478bd9Sstevel@tonic-gate 			 * Templates for internal object creation come from
28487c478bd9Sstevel@tonic-gate 			 * applications calls to C_DeriveKey(), for which it
28497c478bd9Sstevel@tonic-gate 			 * is OKey to pass a CKA_VALUE_LEN attribute, as
28507c478bd9Sstevel@tonic-gate 			 * long as it does not conflict with the length of the
28517c478bd9Sstevel@tonic-gate 			 * CKA_VALUE attribute.
28527c478bd9Sstevel@tonic-gate 			 */
28537c478bd9Sstevel@tonic-gate 			if ((mode != SOFT_CREATE_OBJ_INT) ||
28547c478bd9Sstevel@tonic-gate 			    ((key_len > 0) && sck->sk_value_len != key_len)) {
28557c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
28567c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28577c478bd9Sstevel@tonic-gate 			}
28587c478bd9Sstevel@tonic-gate 		}
28597c478bd9Sstevel@tonic-gate 		break;
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	case SOFT_GEN_KEY:
28627c478bd9Sstevel@tonic-gate 		/* CKA_VALUE must not be specified */
28637c478bd9Sstevel@tonic-gate 		if (isValue) {
28647c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
28657c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
28667c478bd9Sstevel@tonic-gate 		}
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 		switch (keytype) {
28697c478bd9Sstevel@tonic-gate 		/*
28707c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN must be specified by C_GenerateKey
28717c478bd9Sstevel@tonic-gate 		 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
28727c478bd9Sstevel@tonic-gate 		 */
28737c478bd9Sstevel@tonic-gate 		case CKK_RC4:
28747c478bd9Sstevel@tonic-gate 			if (!isValueLen) {
2875d288ba74SAnthony Scarpino 				rv = CKR_TEMPLATE_INCOMPLETE;
28767c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28777c478bd9Sstevel@tonic-gate 			}
2878d288ba74SAnthony Scarpino 			;
28797c478bd9Sstevel@tonic-gate 			if ((sck->sk_value_len < ARCFOUR_MIN_KEY_BYTES) ||
2880f9fbec18Smcpowers 			    (sck->sk_value_len > ARCFOUR_MAX_KEY_BYTES)) {
28817c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
28827c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28837c478bd9Sstevel@tonic-gate 			}
28847c478bd9Sstevel@tonic-gate 			break;
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
28877c478bd9Sstevel@tonic-gate 			/* arbitrary key length - no length checking */
28887c478bd9Sstevel@tonic-gate 			if (!isValueLen) {
2889d288ba74SAnthony Scarpino 				rv = CKR_TEMPLATE_INCOMPLETE;
28907c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28917c478bd9Sstevel@tonic-gate 			}
28927c478bd9Sstevel@tonic-gate 			break;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 		case CKK_AES:
28957c478bd9Sstevel@tonic-gate 			if (!isValueLen) {
2896d288ba74SAnthony Scarpino 				rv = CKR_TEMPLATE_INCOMPLETE;
28977c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
28987c478bd9Sstevel@tonic-gate 			}
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 			if ((sck->sk_value_len != AES_MIN_KEY_BYTES) &&
29017c478bd9Sstevel@tonic-gate 			    (sck->sk_value_len != AES_192_KEY_BYTES) &&
29027c478bd9Sstevel@tonic-gate 			    (sck->sk_value_len != AES_MAX_KEY_BYTES)) {
29037c478bd9Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
29047c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
29057c478bd9Sstevel@tonic-gate 			}
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 			break;
29087c478bd9Sstevel@tonic-gate 
2909f66d273dSizick 		case CKK_BLOWFISH:
2910f66d273dSizick 			if (!isValueLen) {
2911d288ba74SAnthony Scarpino 				rv = CKR_TEMPLATE_INCOMPLETE;
2912f66d273dSizick 				goto fail_cleanup;
2913f66d273dSizick 			}
2914f66d273dSizick 			if ((sck->sk_value_len < BLOWFISH_MINBYTES) ||
2915f66d273dSizick 			    (sck->sk_value_len > BLOWFISH_MAXBYTES)) {
2916f66d273dSizick 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
2917f66d273dSizick 				goto fail_cleanup;
2918f66d273dSizick 			}
2919f66d273dSizick 
2920f66d273dSizick 			break;
2921f66d273dSizick 
29227c478bd9Sstevel@tonic-gate 		case CKK_DES:
29237c478bd9Sstevel@tonic-gate 		case CKK_DES2:
29247c478bd9Sstevel@tonic-gate 		case CKK_DES3:
29257c478bd9Sstevel@tonic-gate 			/* CKA_VALUE_LEN attribute does not apply to DES<n> */
29267c478bd9Sstevel@tonic-gate 			if (isValueLen) {
29277c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
29287c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
29297c478bd9Sstevel@tonic-gate 			}
29307c478bd9Sstevel@tonic-gate 			break;
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 		default:
29337c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
29347c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
29357c478bd9Sstevel@tonic-gate 		}
29367c478bd9Sstevel@tonic-gate 		break;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	case SOFT_UNWRAP_KEY:
29397c478bd9Sstevel@tonic-gate 		/*
29407c478bd9Sstevel@tonic-gate 		 * According to v2.11 of PKCS#11 spec, neither CKA_VALUE nor
29417c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN can be be specified; however v2.20 has this
29427c478bd9Sstevel@tonic-gate 		 * restriction removed, perhaps because it makes it hard to
29437c478bd9Sstevel@tonic-gate 		 * determine variable-length key sizes.  This case statement
29447c478bd9Sstevel@tonic-gate 		 * complied with v2.20.
29457c478bd9Sstevel@tonic-gate 		 */
29467c478bd9Sstevel@tonic-gate 		if (isValue) {
29477c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
29487c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
29497c478bd9Sstevel@tonic-gate 		}
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 		switch (keytype) {
29527c478bd9Sstevel@tonic-gate 		/*
29537c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN is optional
29547c478bd9Sstevel@tonic-gate 		 * if key is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET
29557c478bd9Sstevel@tonic-gate 		 * and the unwrapping mech is *_CBC_PAD.
29567c478bd9Sstevel@tonic-gate 		 *
29577c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN is required
29587c478bd9Sstevel@tonic-gate 		 * if key is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET
29597c478bd9Sstevel@tonic-gate 		 * and the unwrapping mech is *_ECB or *_CBC.
29607c478bd9Sstevel@tonic-gate 		 *
29617c478bd9Sstevel@tonic-gate 		 * since mech is not known at this point, CKA_VALUE_LEN is
29627c478bd9Sstevel@tonic-gate 		 * treated as optional and the caller needs to enforce it.
29637c478bd9Sstevel@tonic-gate 		 */
29647c478bd9Sstevel@tonic-gate 		case CKK_RC4:
29657c478bd9Sstevel@tonic-gate 			if (isValueLen) {
29667c478bd9Sstevel@tonic-gate 				if ((sck->sk_value_len <
29677c478bd9Sstevel@tonic-gate 				    ARCFOUR_MIN_KEY_BYTES) ||
29687c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len >
29697c478bd9Sstevel@tonic-gate 				    ARCFOUR_MAX_KEY_BYTES)) {
29707c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
29717c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
29727c478bd9Sstevel@tonic-gate 				}
29737c478bd9Sstevel@tonic-gate 			}
29747c478bd9Sstevel@tonic-gate 			break;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
29777c478bd9Sstevel@tonic-gate 			/* arbitrary key length - no length checking */
29787c478bd9Sstevel@tonic-gate 			break;
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 		case CKK_AES:
29817c478bd9Sstevel@tonic-gate 			if (isValueLen) {
29827c478bd9Sstevel@tonic-gate 				if ((sck->sk_value_len != AES_MIN_KEY_BYTES) &&
29837c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len != AES_192_KEY_BYTES) &&
29847c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len != AES_MAX_KEY_BYTES)) {
29857c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
29867c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
29877c478bd9Sstevel@tonic-gate 				}
29887c478bd9Sstevel@tonic-gate 			}
29897c478bd9Sstevel@tonic-gate 			break;
29907c478bd9Sstevel@tonic-gate 
2991f66d273dSizick 		case CKK_BLOWFISH:
2992f66d273dSizick 			if (isValueLen &&
2993f66d273dSizick 			    ((sck->sk_value_len < BLOWFISH_MINBYTES) ||
2994f9fbec18Smcpowers 			    (sck->sk_value_len > BLOWFISH_MAXBYTES))) {
2995f9fbec18Smcpowers 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
2996f9fbec18Smcpowers 				goto fail_cleanup;
2997f9fbec18Smcpowers 			}
2998f66d273dSizick 			break;
2999f66d273dSizick 
30007c478bd9Sstevel@tonic-gate 		case CKK_DES:
30017c478bd9Sstevel@tonic-gate 		case CKK_DES2:
30027c478bd9Sstevel@tonic-gate 		case CKK_DES3:
30037c478bd9Sstevel@tonic-gate 			/* CKA_VALUE_LEN attribute does not apply to DES<n> */
30047c478bd9Sstevel@tonic-gate 			if (isValueLen) {
30057c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
30067c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
30077c478bd9Sstevel@tonic-gate 			}
30087c478bd9Sstevel@tonic-gate 			break;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 		default:
30117c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
30127c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
30137c478bd9Sstevel@tonic-gate 		}
30147c478bd9Sstevel@tonic-gate 		break;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	case SOFT_DERIVE_KEY_DH:
30177c478bd9Sstevel@tonic-gate 		/* CKA_VALUE must not be specified */
30187c478bd9Sstevel@tonic-gate 		if (isValue) {
30197c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
30207c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
30217c478bd9Sstevel@tonic-gate 		}
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 		switch (keytype) {
30247c478bd9Sstevel@tonic-gate 		/*
30257c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN is optional
30267c478bd9Sstevel@tonic-gate 		 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
30277c478bd9Sstevel@tonic-gate 		 */
30287c478bd9Sstevel@tonic-gate 		case CKK_RC4:
30297c478bd9Sstevel@tonic-gate 			if (isValueLen) {
30307c478bd9Sstevel@tonic-gate 				if ((sck->sk_value_len <
30317c478bd9Sstevel@tonic-gate 				    ARCFOUR_MIN_KEY_BYTES) ||
30327c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len >
30337c478bd9Sstevel@tonic-gate 				    ARCFOUR_MAX_KEY_BYTES)) {
30347c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
30357c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
30367c478bd9Sstevel@tonic-gate 				}
30377c478bd9Sstevel@tonic-gate 			}
30387c478bd9Sstevel@tonic-gate 			break;
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
30417c478bd9Sstevel@tonic-gate 			/* arbitrary key length - no length checking */
30427c478bd9Sstevel@tonic-gate 			break;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 		case CKK_AES:
30457c478bd9Sstevel@tonic-gate 			if (isValueLen) {
30467c478bd9Sstevel@tonic-gate 				if ((sck->sk_value_len != AES_MIN_KEY_BYTES) &&
30477c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len != AES_192_KEY_BYTES) &&
30487c478bd9Sstevel@tonic-gate 				    (sck->sk_value_len != AES_MAX_KEY_BYTES)) {
30497c478bd9Sstevel@tonic-gate 					rv = CKR_ATTRIBUTE_VALUE_INVALID;
30507c478bd9Sstevel@tonic-gate 					goto fail_cleanup;
30517c478bd9Sstevel@tonic-gate 				}
30527c478bd9Sstevel@tonic-gate 			}
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 			break;
30557c478bd9Sstevel@tonic-gate 
3056f66d273dSizick 		case CKK_BLOWFISH:
3057f66d273dSizick 			if (isValueLen &&
3058f66d273dSizick 			    ((sck->sk_value_len < BLOWFISH_MINBYTES) ||
3059f9fbec18Smcpowers 			    (sck->sk_value_len > BLOWFISH_MAXBYTES))) {
3060f9fbec18Smcpowers 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
3061f9fbec18Smcpowers 				goto fail_cleanup;
3062f9fbec18Smcpowers 			}
3063f66d273dSizick 			break;
3064f66d273dSizick 
30657c478bd9Sstevel@tonic-gate 		case CKK_DES:
30667c478bd9Sstevel@tonic-gate 		case CKK_DES2:
30677c478bd9Sstevel@tonic-gate 		case CKK_DES3:
30687c478bd9Sstevel@tonic-gate 			/* CKA_VALUE_LEN attribute does not apply to DES<n> */
30697c478bd9Sstevel@tonic-gate 			if (isValueLen) {
30707c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
30717c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
30727c478bd9Sstevel@tonic-gate 			}
30737c478bd9Sstevel@tonic-gate 			break;
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 		default:
30767c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
30777c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
30787c478bd9Sstevel@tonic-gate 		}
30797c478bd9Sstevel@tonic-gate 		break;
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	case SOFT_DERIVE_KEY_OTHER:
30827c478bd9Sstevel@tonic-gate 		/* CKA_VALUE must not be specified */
30837c478bd9Sstevel@tonic-gate 		if (isValue) {
30847c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
30857c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
30867c478bd9Sstevel@tonic-gate 		}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 		switch (keytype) {
30897c478bd9Sstevel@tonic-gate 		/*
30907c478bd9Sstevel@tonic-gate 		 * CKA_VALUE_LEN is an optional attribute for
30917c478bd9Sstevel@tonic-gate 		 * CKM_SHA1_KEY_DERIVATION and CKM_MD5_KEY_DERIVATION
30927c478bd9Sstevel@tonic-gate 		 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
30937c478bd9Sstevel@tonic-gate 		 */
30947c478bd9Sstevel@tonic-gate 		case CKK_RC4:
30957c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
30967c478bd9Sstevel@tonic-gate 		case CKK_AES:
3097f66d273dSizick 		case CKK_BLOWFISH:
30987c478bd9Sstevel@tonic-gate 			/*
30997c478bd9Sstevel@tonic-gate 			 * No need to check key length value here, it will be
31007c478bd9Sstevel@tonic-gate 			 * validated later in soft_key_derive_check_length().
31017c478bd9Sstevel@tonic-gate 			 */
31027c478bd9Sstevel@tonic-gate 			break;
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 		case CKK_DES:
31057c478bd9Sstevel@tonic-gate 		case CKK_DES2:
31067c478bd9Sstevel@tonic-gate 		case CKK_DES3:
31077c478bd9Sstevel@tonic-gate 			/* CKA_VALUE_LEN attribute does not apply to DES<n> */
31087c478bd9Sstevel@tonic-gate 			if (isValueLen) {
31097c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCONSISTENT;
31107c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
31117c478bd9Sstevel@tonic-gate 			}
31127c478bd9Sstevel@tonic-gate 			break;
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate 		default:
31157c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
31167c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
31177c478bd9Sstevel@tonic-gate 		}
31187c478bd9Sstevel@tonic-gate 		break;
31197c478bd9Sstevel@tonic-gate 	}
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	/* Set up object. */
31227c478bd9Sstevel@tonic-gate 	new_object->key_type = keytype;
31237c478bd9Sstevel@tonic-gate 	new_object->object_type = object_type;
31247c478bd9Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
31257c478bd9Sstevel@tonic-gate 	if (isLabel) {
31267c478bd9Sstevel@tonic-gate 		rv = soft_add_extra_attr(&string_tmp, new_object);
31277c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
31287c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
31297c478bd9Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate 	return (rv);
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate fail_cleanup:
31347c478bd9Sstevel@tonic-gate 	/*
31357c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
31367c478bd9Sstevel@tonic-gate 	 */
31377c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup((biginteger_t *)sck);
31387c478bd9Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	/*
31417c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
31427c478bd9Sstevel@tonic-gate 	 */
31437c478bd9Sstevel@tonic-gate 	soft_cleanup_object(new_object);
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	return (rv);
31467c478bd9Sstevel@tonic-gate }
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate /*
31507c478bd9Sstevel@tonic-gate  * Build a Domain Parameter Object.
31517c478bd9Sstevel@tonic-gate  *
31527c478bd9Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
31537c478bd9Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
31547c478bd9Sstevel@tonic-gate  *   with appropriate return value.
31557c478bd9Sstevel@tonic-gate  * - Allocate storage for the Domain Parameter object.
31567c478bd9Sstevel@tonic-gate  * - Build the Domain Parameter object according to the key type. Allocate
31577c478bd9Sstevel@tonic-gate  *   storage to hold the big integer value for the supplied attributes
31587c478bd9Sstevel@tonic-gate  *   that are required for a certain key type.
31597c478bd9Sstevel@tonic-gate  *
31607c478bd9Sstevel@tonic-gate  */
31617c478bd9Sstevel@tonic-gate CK_RV
31627c478bd9Sstevel@tonic-gate soft_build_domain_parameters_object(CK_ATTRIBUTE_PTR template,
3163*a8793c76SJason King     CK_ULONG ulAttrNum, soft_object_t *new_object)
31647c478bd9Sstevel@tonic-gate {
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	ulong_t		i;
31677c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
3168*a8793c76SJason King 	CK_RV		rv = CKR_OK;
31697c478bd9Sstevel@tonic-gate 	int		isLabel = 0;
31707c478bd9Sstevel@tonic-gate 	/* Must set flags */
31717c478bd9Sstevel@tonic-gate 	int		isPrime = 0;
31727c478bd9Sstevel@tonic-gate 	int		isSubprime = 0;
31737c478bd9Sstevel@tonic-gate 	int		isBase = 0;
31747c478bd9Sstevel@tonic-gate 	/* Must not set flags */
31757c478bd9Sstevel@tonic-gate 	int		isPrimeBits = 0;
31767c478bd9Sstevel@tonic-gate 	int		isSubPrimeBits = 0;
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	biginteger_t	prime;
31797c478bd9Sstevel@tonic-gate 	biginteger_t	subprime;
31807c478bd9Sstevel@tonic-gate 	biginteger_t	base;
31817c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	domain_obj_t	*dom;
31847c478bd9Sstevel@tonic-gate 	uchar_t	object_type = 0;
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	/* prevent bigint_attr_cleanup from freeing invalid attr value */
31877c478bd9Sstevel@tonic-gate 	(void) memset(&prime, 0x0, sizeof (biginteger_t));
31887c478bd9Sstevel@tonic-gate 	(void) memset(&subprime, 0x0, sizeof (biginteger_t));
31897c478bd9Sstevel@tonic-gate 	(void) memset(&base, 0x0, sizeof (biginteger_t));
31907c478bd9Sstevel@tonic-gate 	string_tmp.pValue = NULL;
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 		/* Domain Parameters Object Attributes */
31957c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 		/* common domain parameter attribute */
31987c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
31997c478bd9Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
32007c478bd9Sstevel@tonic-gate 			break;
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 		/*
32037c478bd9Sstevel@tonic-gate 		 * The following common domain parameter attribute
32047c478bd9Sstevel@tonic-gate 		 * must not be specified by C_CreateObject.
32057c478bd9Sstevel@tonic-gate 		 */
32067c478bd9Sstevel@tonic-gate 		case CKA_LOCAL:
32077c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
32087c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 		/*
32117c478bd9Sstevel@tonic-gate 		 * The following domain parameter attributes must be
32127c478bd9Sstevel@tonic-gate 		 * specified according to the key type by
32137c478bd9Sstevel@tonic-gate 		 * C_CreateObject.
32147c478bd9Sstevel@tonic-gate 		 */
32157c478bd9Sstevel@tonic-gate 		case CKA_PRIME:
32167c478bd9Sstevel@tonic-gate 			isPrime = 1;
32177c478bd9Sstevel@tonic-gate 			/*
32187c478bd9Sstevel@tonic-gate 			 * Copyin big integer attribute from template
32197c478bd9Sstevel@tonic-gate 			 * to a local variable.
32207c478bd9Sstevel@tonic-gate 			 */
32217c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime,
32227c478bd9Sstevel@tonic-gate 			    &template[i]);
32237c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
32247c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
32257c478bd9Sstevel@tonic-gate 			break;
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 		case CKA_SUBPRIME:
32287c478bd9Sstevel@tonic-gate 			isSubprime = 1;
32297c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&subprime,
32307c478bd9Sstevel@tonic-gate 			    &template[i]);
32317c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
32327c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
32337c478bd9Sstevel@tonic-gate 			break;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 		case CKA_BASE:
32367c478bd9Sstevel@tonic-gate 			isBase = 1;
32377c478bd9Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&base,
32387c478bd9Sstevel@tonic-gate 			    &template[i]);
32397c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
32407c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
32417c478bd9Sstevel@tonic-gate 			break;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 		case CKA_PRIME_BITS:
32447c478bd9Sstevel@tonic-gate 			isPrimeBits = 1;
32457c478bd9Sstevel@tonic-gate 			break;
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 		case CKA_SUB_PRIME_BITS:
32487c478bd9Sstevel@tonic-gate 			isSubPrimeBits = 1;
32497c478bd9Sstevel@tonic-gate 			break;
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
32527c478bd9Sstevel@tonic-gate 			isLabel = 1;
32537c478bd9Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
32547c478bd9Sstevel@tonic-gate 			    &template[i]);
32557c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
32567c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
32577c478bd9Sstevel@tonic-gate 			break;
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 		default:
32607c478bd9Sstevel@tonic-gate 			rv = soft_parse_common_attrs(&template[i],
32617c478bd9Sstevel@tonic-gate 			    &object_type);
32627c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
32637c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
32647c478bd9Sstevel@tonic-gate 			break;
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 		}
32677c478bd9Sstevel@tonic-gate 	} /* For */
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	/* Allocate storage for Domain Parameters Object. */
32707c478bd9Sstevel@tonic-gate 	dom = calloc(1, sizeof (domain_obj_t));
32717c478bd9Sstevel@tonic-gate 	if (dom == NULL) {
32727c478bd9Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
32737c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
32747c478bd9Sstevel@tonic-gate 	}
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	new_object->object_class_u.domain = dom;
32777c478bd9Sstevel@tonic-gate 	new_object->class = CKO_DOMAIN_PARAMETERS;
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 	if (keytype == (CK_KEY_TYPE)~0UL) {
32807c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
32817c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	new_object->key_type = keytype;
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	/* Supported key types of the Domain Parameters Object */
32877c478bd9Sstevel@tonic-gate 	switch (keytype) {
32887c478bd9Sstevel@tonic-gate 	case CKK_DSA:
32897c478bd9Sstevel@tonic-gate 		if (isPrimeBits || isSubPrimeBits) {
32907c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
32917c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 		if (isPrime && isSubprime && isBase) {
32957c478bd9Sstevel@tonic-gate 			/*
32967c478bd9Sstevel@tonic-gate 			 * Copy big integer attribute value to the
32977c478bd9Sstevel@tonic-gate 			 * designated place in the domain parameter
32987c478bd9Sstevel@tonic-gate 			 * object.
32997c478bd9Sstevel@tonic-gate 			 */
33007c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_DOM_DSA_PRIME(dom));
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&subprime, KEY_DOM_DSA_SUBPRIME(dom));
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_DOM_DSA_BASE(dom));
33057c478bd9Sstevel@tonic-gate 		} else {
33067c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
33077c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 		break;
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	case CKK_DH:
33127c478bd9Sstevel@tonic-gate 		if (isPrimeBits || isSubprime || isSubPrimeBits) {
33137c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
33147c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33157c478bd9Sstevel@tonic-gate 		}
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 		if (isPrime && isBase) {
33187c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_DOM_DH_PRIME(dom));
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_DOM_DH_BASE(dom));
33217c478bd9Sstevel@tonic-gate 		} else {
33227c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
33237c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33247c478bd9Sstevel@tonic-gate 		}
33257c478bd9Sstevel@tonic-gate 		break;
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	case CKK_X9_42_DH:
33287c478bd9Sstevel@tonic-gate 		if (isPrimeBits || isSubPrimeBits) {
33297c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
33307c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33317c478bd9Sstevel@tonic-gate 		}
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 		if (isPrime && isSubprime && isBase) {
33347c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&prime, KEY_DOM_DH942_PRIME(dom));
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&base, KEY_DOM_DH942_BASE(dom));
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 			copy_bigint_attr(&subprime,
3339f9fbec18Smcpowers 			    KEY_DOM_DH942_SUBPRIME(dom));
33407c478bd9Sstevel@tonic-gate 		} else {
33417c478bd9Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
33427c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33437c478bd9Sstevel@tonic-gate 		}
33447c478bd9Sstevel@tonic-gate 		break;
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	default:
33477c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
33487c478bd9Sstevel@tonic-gate 		goto fail_cleanup;
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 
33517c478bd9Sstevel@tonic-gate 	new_object->object_type = object_type;
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	if (isLabel) {
33547c478bd9Sstevel@tonic-gate 		rv = soft_add_extra_attr(&string_tmp, new_object);
33557c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
33567c478bd9Sstevel@tonic-gate 			goto fail_cleanup;
33577c478bd9Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	return (rv);
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate fail_cleanup:
33637c478bd9Sstevel@tonic-gate 	/*
33647c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
33657c478bd9Sstevel@tonic-gate 	 */
33667c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&prime);
33677c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&subprime);
33687c478bd9Sstevel@tonic-gate 	bigint_attr_cleanup(&base);
33697c478bd9Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 	/*
33727c478bd9Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
33737c478bd9Sstevel@tonic-gate 	 */
33747c478bd9Sstevel@tonic-gate 	soft_cleanup_object(new_object);
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 	return (rv);
33777c478bd9Sstevel@tonic-gate }
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate /*
33807c478bd9Sstevel@tonic-gate  * Build a Certificate Object
33817c478bd9Sstevel@tonic-gate  *
33827c478bd9Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
33837c478bd9Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
33847c478bd9Sstevel@tonic-gate  *   with appropriate return value.
33857c478bd9Sstevel@tonic-gate  * - Allocate storage for the Certificate object
33867c478bd9Sstevel@tonic-gate  */
33877c478bd9Sstevel@tonic-gate static CK_RV
33887c478bd9Sstevel@tonic-gate soft_build_certificate_object(CK_ATTRIBUTE_PTR template,
3389*a8793c76SJason King     CK_ULONG ulAttrNum, soft_object_t *new_object,
3390*a8793c76SJason King     CK_CERTIFICATE_TYPE cert_type)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	uint64_t	attr_mask = 0;
3393*a8793c76SJason King 	CK_RV		rv = CKR_OK;
33947c478bd9Sstevel@tonic-gate 	CK_ULONG	i;
33957c478bd9Sstevel@tonic-gate 	int		owner_set = 0;
33967c478bd9Sstevel@tonic-gate 	int		value_set = 0;
33977c478bd9Sstevel@tonic-gate 	int		subject_set = 0;
33987c478bd9Sstevel@tonic-gate 	certificate_obj_t *cert;
33997c478bd9Sstevel@tonic-gate 	/* certificate type defaults to the value given as a parameter */
34007c478bd9Sstevel@tonic-gate 	CK_CERTIFICATE_TYPE certtype = cert_type;
34017c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
34027c478bd9Sstevel@tonic-gate 	int		isLabel = 0;
34037c478bd9Sstevel@tonic-gate 	uchar_t		object_type = 0;
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	/*
34067c478bd9Sstevel@tonic-gate 	 * Look for the certificate type attribute and do some
34077c478bd9Sstevel@tonic-gate 	 * sanity checking before creating the structures.
34087c478bd9Sstevel@tonic-gate 	 */
34097c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
34107c478bd9Sstevel@tonic-gate 		/* Certificate Object Attributes */
34117c478bd9Sstevel@tonic-gate 		switch (template[i].type) {
34127c478bd9Sstevel@tonic-gate 			case CKA_CERTIFICATE_TYPE:
34137c478bd9Sstevel@tonic-gate 				certtype =
34147c478bd9Sstevel@tonic-gate 				    *((CK_CERTIFICATE_TYPE*)template[i].pValue);
34157c478bd9Sstevel@tonic-gate 				break;
34167c478bd9Sstevel@tonic-gate 			case CKA_SUBJECT:
34177c478bd9Sstevel@tonic-gate 				subject_set = 1;
34187c478bd9Sstevel@tonic-gate 				break;
34197c478bd9Sstevel@tonic-gate 			case CKA_OWNER:
34207c478bd9Sstevel@tonic-gate 				owner_set = 1;
34217c478bd9Sstevel@tonic-gate 				break;
34227c478bd9Sstevel@tonic-gate 			case CKA_VALUE:
34237c478bd9Sstevel@tonic-gate 				value_set = 1;
34247c478bd9Sstevel@tonic-gate 				break;
34257c478bd9Sstevel@tonic-gate 		}
34267c478bd9Sstevel@tonic-gate 	}
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	/* The certificate type MUST be specified */
34297c478bd9Sstevel@tonic-gate 	if (certtype != CKC_X_509 && certtype != CKC_X_509_ATTR_CERT)
34307c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	/*
34337c478bd9Sstevel@tonic-gate 	 * For X.509 certs, the CKA_SUBJECT and CKA_VALUE
34347c478bd9Sstevel@tonic-gate 	 * must be present at creation time.
34357c478bd9Sstevel@tonic-gate 	 */
34367c478bd9Sstevel@tonic-gate 	if (certtype == CKC_X_509 &&
34377c478bd9Sstevel@tonic-gate 	    (!subject_set || !value_set))
34387c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	/*
34417c478bd9Sstevel@tonic-gate 	 * For X.509 Attribute certs, the CKA_OWNER and CKA_VALUE
34427c478bd9Sstevel@tonic-gate 	 * must be present at creation time.
34437c478bd9Sstevel@tonic-gate 	 */
34447c478bd9Sstevel@tonic-gate 	if (certtype == CKC_X_509_ATTR_CERT &&
34457c478bd9Sstevel@tonic-gate 	    (!owner_set || !value_set))
34467c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	string_tmp.pValue = NULL;
34497c478bd9Sstevel@tonic-gate 	cert = calloc(1, sizeof (certificate_obj_t));
34507c478bd9Sstevel@tonic-gate 	if (cert == NULL) {
34517c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
34527c478bd9Sstevel@tonic-gate 	}
34537c478bd9Sstevel@tonic-gate 	cert->certificate_type = certtype;
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
34567c478bd9Sstevel@tonic-gate 		/* Certificate Object Attributes */
34577c478bd9Sstevel@tonic-gate 		switch (certtype) {
34587c478bd9Sstevel@tonic-gate 			case CKC_X_509:
34597c478bd9Sstevel@tonic-gate 			switch (template[i].type) {
34607c478bd9Sstevel@tonic-gate 				case CKA_SUBJECT:
34617c478bd9Sstevel@tonic-gate 					rv = get_cert_attr_from_template(
3462f9fbec18Smcpowers 					    &cert->cert_type_u.x509.subject,
3463f9fbec18Smcpowers 					    &template[i]);
34647c478bd9Sstevel@tonic-gate 					break;
34657c478bd9Sstevel@tonic-gate 				case CKA_VALUE:
34667c478bd9Sstevel@tonic-gate 					rv = get_cert_attr_from_template(
3467f9fbec18Smcpowers 					    &cert->cert_type_u.x509.value,
3468f9fbec18Smcpowers 					    &template[i]);
34697c478bd9Sstevel@tonic-gate 					break;
34707c478bd9Sstevel@tonic-gate 				case CKA_LABEL:
34717c478bd9Sstevel@tonic-gate 					isLabel = 1;
34727c478bd9Sstevel@tonic-gate 					rv = get_string_from_template(
3473f9fbec18Smcpowers 					    &string_tmp,
3474f9fbec18Smcpowers 					    &template[i]);
34757c478bd9Sstevel@tonic-gate 					if (rv != CKR_OK)
34767c478bd9Sstevel@tonic-gate 						goto fail_cleanup;
34777c478bd9Sstevel@tonic-gate 					break;
34787c478bd9Sstevel@tonic-gate 				case CKA_ID:
34797c478bd9Sstevel@tonic-gate 				case CKA_ISSUER:
34807c478bd9Sstevel@tonic-gate 				case CKA_SERIAL_NUMBER:
34817c478bd9Sstevel@tonic-gate 					rv = soft_add_extra_attr(&template[i],
3482f9fbec18Smcpowers 					    new_object);
34837c478bd9Sstevel@tonic-gate 					break;
34847c478bd9Sstevel@tonic-gate 				case CKA_MODIFIABLE:
34857c478bd9Sstevel@tonic-gate 					if ((*(CK_BBOOL *)template[i].pValue) ==
34867c478bd9Sstevel@tonic-gate 					    B_FALSE)
34877c478bd9Sstevel@tonic-gate 						attr_mask |=
34887c478bd9Sstevel@tonic-gate 						    NOT_MODIFIABLE_BOOL_ON;
34897c478bd9Sstevel@tonic-gate 					break;
34907c478bd9Sstevel@tonic-gate 				case CKA_CERTIFICATE_TYPE:
34917c478bd9Sstevel@tonic-gate 					break;
34927c478bd9Sstevel@tonic-gate 				default:
3493f9fbec18Smcpowers 					rv = soft_parse_common_attrs(
3494f9fbec18Smcpowers 					    &template[i], &object_type);
3495f9fbec18Smcpowers 					if (rv != CKR_OK)
3496f9fbec18Smcpowers 						goto fail_cleanup;
34977c478bd9Sstevel@tonic-gate 			}
34987c478bd9Sstevel@tonic-gate 			break;
34997c478bd9Sstevel@tonic-gate 			case CKC_X_509_ATTR_CERT:
35007c478bd9Sstevel@tonic-gate 			switch (template[i].type) {
35017c478bd9Sstevel@tonic-gate 				case CKA_OWNER:
35027c478bd9Sstevel@tonic-gate 					rv = get_cert_attr_from_template(
35037c478bd9Sstevel@tonic-gate 					    &cert->cert_type_u.x509_attr.owner,
35047c478bd9Sstevel@tonic-gate 					    &template[i]);
35057c478bd9Sstevel@tonic-gate 					break;
35067c478bd9Sstevel@tonic-gate 				case CKA_VALUE:
35077c478bd9Sstevel@tonic-gate 					rv = get_cert_attr_from_template(
35087c478bd9Sstevel@tonic-gate 					    &cert->cert_type_u.x509_attr.value,
35097c478bd9Sstevel@tonic-gate 					    &template[i]);
35107c478bd9Sstevel@tonic-gate 					break;
35117c478bd9Sstevel@tonic-gate 				case CKA_LABEL:
35127c478bd9Sstevel@tonic-gate 					isLabel = 1;
35137c478bd9Sstevel@tonic-gate 					rv = get_string_from_template(
3514f9fbec18Smcpowers 					    &string_tmp, &template[i]);
35157c478bd9Sstevel@tonic-gate 					if (rv != CKR_OK)
35167c478bd9Sstevel@tonic-gate 						goto fail_cleanup;
35177c478bd9Sstevel@tonic-gate 					break;
35187c478bd9Sstevel@tonic-gate 				case CKA_SERIAL_NUMBER:
35197c478bd9Sstevel@tonic-gate 				case CKA_AC_ISSUER:
35207c478bd9Sstevel@tonic-gate 				case CKA_ATTR_TYPES:
35217c478bd9Sstevel@tonic-gate 					rv = soft_add_extra_attr(&template[i],
3522f9fbec18Smcpowers 					    new_object);
35237c478bd9Sstevel@tonic-gate 					break;
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 				case CKA_MODIFIABLE:
35267c478bd9Sstevel@tonic-gate 					if ((*(CK_BBOOL *)template[i].pValue) ==
35277c478bd9Sstevel@tonic-gate 					    B_FALSE)
35287c478bd9Sstevel@tonic-gate 						attr_mask |=
35297c478bd9Sstevel@tonic-gate 						    NOT_MODIFIABLE_BOOL_ON;
35307c478bd9Sstevel@tonic-gate 					break;
35317c478bd9Sstevel@tonic-gate 				case CKA_CERTIFICATE_TYPE:
35327c478bd9Sstevel@tonic-gate 					break;
35337c478bd9Sstevel@tonic-gate 				default:
3534f9fbec18Smcpowers 					rv = soft_parse_common_attrs(
3535f9fbec18Smcpowers 					    &template[i], &object_type);
3536f9fbec18Smcpowers 					if (rv != CKR_OK)
3537f9fbec18Smcpowers 						goto fail_cleanup;
3538f9fbec18Smcpowers 					break;
35397c478bd9Sstevel@tonic-gate 			}
35407c478bd9Sstevel@tonic-gate 			break;
35417c478bd9Sstevel@tonic-gate 			default:
35427c478bd9Sstevel@tonic-gate 				rv = CKR_TEMPLATE_INCOMPLETE;
35437c478bd9Sstevel@tonic-gate 				break;
35447c478bd9Sstevel@tonic-gate 		}
35457c478bd9Sstevel@tonic-gate 	}
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK) {
35487c478bd9Sstevel@tonic-gate 		new_object->object_class_u.certificate = cert;
35497c478bd9Sstevel@tonic-gate 		new_object->class = CKO_CERTIFICATE;
35507c478bd9Sstevel@tonic-gate 		new_object->object_type = object_type;
35517c478bd9Sstevel@tonic-gate 		new_object->cert_type = certtype;
35527c478bd9Sstevel@tonic-gate 		new_object->bool_attr_mask = attr_mask;
35537c478bd9Sstevel@tonic-gate 		if (isLabel) {
35547c478bd9Sstevel@tonic-gate 			rv = soft_add_extra_attr(&string_tmp, new_object);
35557c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK)
35567c478bd9Sstevel@tonic-gate 				goto fail_cleanup;
35577c478bd9Sstevel@tonic-gate 			string_attr_cleanup(&string_tmp);
35587c478bd9Sstevel@tonic-gate 		}
35597c478bd9Sstevel@tonic-gate 	}
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate fail_cleanup:
35627c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
35637c478bd9Sstevel@tonic-gate 		soft_cleanup_cert_object(new_object);
35647c478bd9Sstevel@tonic-gate 	}
35657c478bd9Sstevel@tonic-gate 	return (rv);
35667c478bd9Sstevel@tonic-gate }
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate /*
35707c478bd9Sstevel@tonic-gate  * Validate the attribute types in the object's template. Then,
35717c478bd9Sstevel@tonic-gate  * call the appropriate build function according to the class of
35727c478bd9Sstevel@tonic-gate  * the object specified in the template.
35737c478bd9Sstevel@tonic-gate  *
35747c478bd9Sstevel@tonic-gate  * Note: The following classes of objects are supported:
35757c478bd9Sstevel@tonic-gate  * - CKO_PUBLIC_KEY
35767c478bd9Sstevel@tonic-gate  * - CKO_PRIVATE_KEY
35777c478bd9Sstevel@tonic-gate  * - CKO_SECRET_KEY
35787c478bd9Sstevel@tonic-gate  * - CKO_DOMAIN_PARAMETERS
35797c478bd9Sstevel@tonic-gate  * - CKO_CERTIFICATE
35807c478bd9Sstevel@tonic-gate  *
35817c478bd9Sstevel@tonic-gate  */
35827c478bd9Sstevel@tonic-gate CK_RV
35837c478bd9Sstevel@tonic-gate soft_build_object(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
3584*a8793c76SJason King     soft_object_t *new_object)
35857c478bd9Sstevel@tonic-gate {
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS class = (CK_OBJECT_CLASS)~0UL;
3588*a8793c76SJason King 	CK_RV		rv = CKR_OK;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	if (template == NULL) {
35917c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
35927c478bd9Sstevel@tonic-gate 	}
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	/* Validate the attribute type in the template. */
35957c478bd9Sstevel@tonic-gate 	rv = soft_validate_attr(template, ulAttrNum, &class);
35967c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
35977c478bd9Sstevel@tonic-gate 		return (rv);
35987c478bd9Sstevel@tonic-gate 	/*
35997c478bd9Sstevel@tonic-gate 	 * CKA_CLASS is a mandatory attribute for C_CreateObject
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 	if (class == (CK_OBJECT_CLASS)~0UL)
36027c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	/*
36057c478bd9Sstevel@tonic-gate 	 * Call the appropriate function based on the supported class
36067c478bd9Sstevel@tonic-gate 	 * of the object.
36077c478bd9Sstevel@tonic-gate 	 */
36087c478bd9Sstevel@tonic-gate 	switch (class) {
36097c478bd9Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
36107c478bd9Sstevel@tonic-gate 		rv = soft_build_public_key_object(template, ulAttrNum,
36117c478bd9Sstevel@tonic-gate 		    new_object, SOFT_CREATE_OBJ, (CK_KEY_TYPE)~0UL);
36127c478bd9Sstevel@tonic-gate 		break;
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
36157c478bd9Sstevel@tonic-gate 		rv = soft_build_private_key_object(template, ulAttrNum,
36167c478bd9Sstevel@tonic-gate 		    new_object, SOFT_CREATE_OBJ, (CK_KEY_TYPE)~0UL);
36177c478bd9Sstevel@tonic-gate 		break;
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	case CKO_SECRET_KEY:
36207c478bd9Sstevel@tonic-gate 		rv = soft_build_secret_key_object(template, ulAttrNum,
36217c478bd9Sstevel@tonic-gate 		    new_object, SOFT_CREATE_OBJ, 0, (CK_KEY_TYPE)~0UL);
36227c478bd9Sstevel@tonic-gate 		break;
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
36257c478bd9Sstevel@tonic-gate 		rv = soft_build_domain_parameters_object(template, ulAttrNum,
36267c478bd9Sstevel@tonic-gate 		    new_object);
36277c478bd9Sstevel@tonic-gate 		break;
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	case CKO_CERTIFICATE:
36307c478bd9Sstevel@tonic-gate 		rv = soft_build_certificate_object(template, ulAttrNum,
3631f9fbec18Smcpowers 		    new_object, (CK_CERTIFICATE_TYPE)~0UL);
36327c478bd9Sstevel@tonic-gate 		break;
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	case CKO_DATA:
36357c478bd9Sstevel@tonic-gate 	case CKO_HW_FEATURE:
36367c478bd9Sstevel@tonic-gate 	case CKO_VENDOR_DEFINED:
36377c478bd9Sstevel@tonic-gate 	default:
36387c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_VALUE_INVALID);
36397c478bd9Sstevel@tonic-gate 	}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	return (rv);
36427c478bd9Sstevel@tonic-gate }
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate /*
36457c478bd9Sstevel@tonic-gate  * Validate the attribute types in the object's template. Then,
36467c478bd9Sstevel@tonic-gate  * call the appropriate build function according to the class of
36477c478bd9Sstevel@tonic-gate  * the object specified in the template.
36487c478bd9Sstevel@tonic-gate  *
36497c478bd9Sstevel@tonic-gate  */
36507c478bd9Sstevel@tonic-gate CK_RV
36517c478bd9Sstevel@tonic-gate soft_build_key(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
3652*a8793c76SJason King     soft_object_t *new_object, CK_OBJECT_CLASS class, CK_KEY_TYPE key_type,
3653*a8793c76SJason King     CK_ULONG key_len, CK_ULONG mode)
36547c478bd9Sstevel@tonic-gate {
36557c478bd9Sstevel@tonic-gate 
3656*a8793c76SJason King 	CK_RV		rv = CKR_OK;
36577c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS temp_class = (CK_OBJECT_CLASS)~0UL;
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate 	/* Validate the attribute type in the template. */
36607c478bd9Sstevel@tonic-gate 	if ((template != NULL) && (ulAttrNum != 0)) {
36617c478bd9Sstevel@tonic-gate 		rv = soft_validate_attr(template, ulAttrNum, &temp_class);
36627c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
36637c478bd9Sstevel@tonic-gate 			return (rv);
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/*
36677c478bd9Sstevel@tonic-gate 	 * If either the class from the parameter list ("class") or
36687c478bd9Sstevel@tonic-gate 	 * the class from the template ("temp_class") is not specified,
36697c478bd9Sstevel@tonic-gate 	 * try to use the other one.
36707c478bd9Sstevel@tonic-gate 	 */
36717c478bd9Sstevel@tonic-gate 	if (temp_class == (CK_OBJECT_CLASS)~0UL) {
36727c478bd9Sstevel@tonic-gate 		temp_class = class;
36737c478bd9Sstevel@tonic-gate 	} else if (class == (CK_OBJECT_CLASS)~0UL) {
36747c478bd9Sstevel@tonic-gate 		class = temp_class;
36757c478bd9Sstevel@tonic-gate 	}
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	/* If object class is still not specified, template is incomplete. */
36787c478bd9Sstevel@tonic-gate 	if (class == (CK_OBJECT_CLASS)~0UL)
36797c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 	/* Class should match if specified in both parameters and template. */
36827c478bd9Sstevel@tonic-gate 	if (class != temp_class)
36837c478bd9Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCONSISTENT);
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	/*
36867c478bd9Sstevel@tonic-gate 	 * Call the appropriate function based on the supported class
36877c478bd9Sstevel@tonic-gate 	 * of the object.
36887c478bd9Sstevel@tonic-gate 	 */
36897c478bd9Sstevel@tonic-gate 	switch (class) {
36907c478bd9Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 		/* Unwrapping public keys is not supported. */
36937c478bd9Sstevel@tonic-gate 		if (mode == SOFT_UNWRAP_KEY) {
36947c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
36957c478bd9Sstevel@tonic-gate 			break;
36967c478bd9Sstevel@tonic-gate 		}
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 		rv = soft_build_public_key_object(template, ulAttrNum,
36997c478bd9Sstevel@tonic-gate 		    new_object, mode, key_type);
37007c478bd9Sstevel@tonic-gate 		break;
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 		rv = soft_build_private_key_object(template, ulAttrNum,
37057c478bd9Sstevel@tonic-gate 		    new_object, mode, key_type);
37067c478bd9Sstevel@tonic-gate 		break;
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	case CKO_SECRET_KEY:
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 		rv = soft_build_secret_key_object(template, ulAttrNum,
37117c478bd9Sstevel@tonic-gate 		    new_object, mode, key_len, key_type);
37127c478bd9Sstevel@tonic-gate 		break;
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 		/* Unwrapping domain parameters is not supported. */
37177c478bd9Sstevel@tonic-gate 		if (mode == SOFT_UNWRAP_KEY) {
37187c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
37197c478bd9Sstevel@tonic-gate 			break;
37207c478bd9Sstevel@tonic-gate 		}
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 		rv = soft_build_domain_parameters_object(template, ulAttrNum,
37237c478bd9Sstevel@tonic-gate 		    new_object);
37247c478bd9Sstevel@tonic-gate 		break;
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	case CKO_DATA:
37277c478bd9Sstevel@tonic-gate 	case CKO_CERTIFICATE:
37287c478bd9Sstevel@tonic-gate 	case CKO_HW_FEATURE:
37297c478bd9Sstevel@tonic-gate 	case CKO_VENDOR_DEFINED:
37307c478bd9Sstevel@tonic-gate 	default:
37317c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_VALUE_INVALID);
37327c478bd9Sstevel@tonic-gate 	}
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	return (rv);
37357c478bd9Sstevel@tonic-gate }
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate /*
37397c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute that is common to all supported
37407c478bd9Sstevel@tonic-gate  * classes (i.e. public key, private key, secret key, domain parameters,
37417c478bd9Sstevel@tonic-gate  * and certificate classes).
37427c478bd9Sstevel@tonic-gate  */
37437c478bd9Sstevel@tonic-gate CK_RV
37447c478bd9Sstevel@tonic-gate soft_get_common_attrs(soft_object_t *object_p, CK_ATTRIBUTE_PTR template,
37457c478bd9Sstevel@tonic-gate     uchar_t object_type)
37467c478bd9Sstevel@tonic-gate {
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	switch (template->type) {
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	case CKA_CLASS:
37537c478bd9Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->class,
37547c478bd9Sstevel@tonic-gate 		    template));
37557c478bd9Sstevel@tonic-gate 
37567c478bd9Sstevel@tonic-gate 	/* default boolean attributes */
37577c478bd9Sstevel@tonic-gate 	case CKA_TOKEN:
37587c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
37597c478bd9Sstevel@tonic-gate 		if (template->pValue == NULL) {
37607c478bd9Sstevel@tonic-gate 			return (CKR_OK);
37617c478bd9Sstevel@tonic-gate 		}
37627c478bd9Sstevel@tonic-gate 		if (object_type & TOKEN_OBJECT)
37637c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
37647c478bd9Sstevel@tonic-gate 		else
37657c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
37667c478bd9Sstevel@tonic-gate 		break;
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE:
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
37717c478bd9Sstevel@tonic-gate 		if (template->pValue == NULL) {
37727c478bd9Sstevel@tonic-gate 			return (CKR_OK);
37737c478bd9Sstevel@tonic-gate 		}
37747c478bd9Sstevel@tonic-gate 		if (object_type & PRIVATE_OBJECT)
37757c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
37767c478bd9Sstevel@tonic-gate 		else
37777c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
37787c478bd9Sstevel@tonic-gate 		break;
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate 	case CKA_MODIFIABLE:
37817c478bd9Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
37827c478bd9Sstevel@tonic-gate 		if (template->pValue == NULL) {
37837c478bd9Sstevel@tonic-gate 			return (CKR_OK);
37847c478bd9Sstevel@tonic-gate 		}
37857c478bd9Sstevel@tonic-gate 		if ((object_p->bool_attr_mask) & NOT_MODIFIABLE_BOOL_ON)
37867c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
37877c478bd9Sstevel@tonic-gate 		else
37887c478bd9Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
37897c478bd9Sstevel@tonic-gate 		break;
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	case CKA_LABEL:
37927c478bd9Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
37937c478bd9Sstevel@tonic-gate 		    template));
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	default:
37967c478bd9Sstevel@tonic-gate 		/*
37977c478bd9Sstevel@tonic-gate 		 * The specified attribute for the object is invalid.
37987c478bd9Sstevel@tonic-gate 		 * (the object does not possess such an attribute.)
37997c478bd9Sstevel@tonic-gate 		 */
38007c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
38017c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
38027c478bd9Sstevel@tonic-gate 	}
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	return (rv);
38057c478bd9Sstevel@tonic-gate }
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate /*
38087c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute that is common to all key objects
38097c478bd9Sstevel@tonic-gate  * (i.e. public key, private key and secret key).
38107c478bd9Sstevel@tonic-gate  */
38117c478bd9Sstevel@tonic-gate CK_RV
38127c478bd9Sstevel@tonic-gate soft_get_common_key_attrs(soft_object_t *object_p, CK_ATTRIBUTE_PTR template)
38137c478bd9Sstevel@tonic-gate {
38147c478bd9Sstevel@tonic-gate 
38157c478bd9Sstevel@tonic-gate 	switch (template->type) {
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 	case CKA_KEY_TYPE:
38187c478bd9Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->key_type,
38197c478bd9Sstevel@tonic-gate 		    template));
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 	case CKA_ID:
38227c478bd9Sstevel@tonic-gate 	case CKA_START_DATE:
38237c478bd9Sstevel@tonic-gate 	case CKA_END_DATE:
38247c478bd9Sstevel@tonic-gate 		/*
38257c478bd9Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
38267c478bd9Sstevel@tonic-gate 		 */
38277c478bd9Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
38287c478bd9Sstevel@tonic-gate 		    template));
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	/* Key related boolean attributes */
38317c478bd9Sstevel@tonic-gate 	case CKA_LOCAL:
38327c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38337c478bd9Sstevel@tonic-gate 		    LOCAL_BOOL_ON, template));
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	case CKA_DERIVE:
38367c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38377c478bd9Sstevel@tonic-gate 		    DERIVE_BOOL_ON, template));
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	case CKA_KEY_GEN_MECHANISM:
38407c478bd9Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->mechanism,
38417c478bd9Sstevel@tonic-gate 		    template));
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 	default:
38447c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
38457c478bd9Sstevel@tonic-gate 	}
38467c478bd9Sstevel@tonic-gate }
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate /*
38497c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute of a Public Key Object.
38507c478bd9Sstevel@tonic-gate  *
38517c478bd9Sstevel@tonic-gate  * Rule: All the attributes in the public key object can be revealed.
38527c478bd9Sstevel@tonic-gate  */
38537c478bd9Sstevel@tonic-gate CK_RV
38547c478bd9Sstevel@tonic-gate soft_get_public_key_attribute(soft_object_t *object_p,
3855*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
38567c478bd9Sstevel@tonic-gate {
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
38597c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	switch (template->type) {
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 	case CKA_SUBJECT:
3864f9fbec18Smcpowers 	case CKA_EC_PARAMS:
38657c478bd9Sstevel@tonic-gate 		/*
38667c478bd9Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
38677c478bd9Sstevel@tonic-gate 		 */
38687c478bd9Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
38697c478bd9Sstevel@tonic-gate 		    template));
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	/* Key related boolean attributes */
38727c478bd9Sstevel@tonic-gate 	case CKA_ENCRYPT:
38737c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38747c478bd9Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 	case CKA_VERIFY:
38777c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38787c478bd9Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 	case CKA_VERIFY_RECOVER:
38817c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38827c478bd9Sstevel@tonic-gate 		    VERIFY_RECOVER_BOOL_ON, template));
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	case CKA_WRAP:
38857c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38867c478bd9Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 	case CKA_TRUSTED:
38897c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
38907c478bd9Sstevel@tonic-gate 		    TRUSTED_BOOL_ON, template));
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
38937c478bd9Sstevel@tonic-gate 		/*
38947c478bd9Sstevel@tonic-gate 		 * This attribute is valid only for RSA public key
38957c478bd9Sstevel@tonic-gate 		 * object.
38967c478bd9Sstevel@tonic-gate 		 */
38977c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
38987c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
38997c478bd9Sstevel@tonic-gate 			    OBJ_PUB_RSA_MOD(object_p), template));
39007c478bd9Sstevel@tonic-gate 		} else {
39017c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39027c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39037c478bd9Sstevel@tonic-gate 		}
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
39067c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
39077c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39087c478bd9Sstevel@tonic-gate 			    OBJ_PUB_RSA_PUBEXPO(object_p), template));
39097c478bd9Sstevel@tonic-gate 		} else {
39107c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39117c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39127c478bd9Sstevel@tonic-gate 		}
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	case CKA_MODULUS_BITS:
39157c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
39167c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
39177c478bd9Sstevel@tonic-gate 			    OBJ_PUB_RSA_MOD_BITS(object_p), template));
39187c478bd9Sstevel@tonic-gate 		} else {
39197c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39207c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39217c478bd9Sstevel@tonic-gate 		}
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
39247c478bd9Sstevel@tonic-gate 		switch (keytype) {
39257c478bd9Sstevel@tonic-gate 		case CKK_DSA:
39267c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39277c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DSA_PRIME(object_p), template));
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 		case CKK_DH:
39307c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39317c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH_PRIME(object_p), template));
39327c478bd9Sstevel@tonic-gate 
39337c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
39347c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39357c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH942_PRIME(object_p), template));
39367c478bd9Sstevel@tonic-gate 
39377c478bd9Sstevel@tonic-gate 		default:
39387c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39397c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39407c478bd9Sstevel@tonic-gate 		}
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
39437c478bd9Sstevel@tonic-gate 		switch (keytype) {
39447c478bd9Sstevel@tonic-gate 		case CKK_DSA:
39457c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39467c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DSA_SUBPRIME(object_p), template));
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
39497c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39507c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH942_SUBPRIME(object_p), template));
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate 		default:
39537c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39547c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39557c478bd9Sstevel@tonic-gate 		}
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate 	case CKA_BASE:
39587c478bd9Sstevel@tonic-gate 		switch (keytype) {
39597c478bd9Sstevel@tonic-gate 		case CKK_DSA:
39607c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39617c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DSA_BASE(object_p), template));
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 		case CKK_DH:
39647c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39657c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH_BASE(object_p), template));
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
39687c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39697c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH942_BASE(object_p), template));
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 		default:
39727c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39737c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39747c478bd9Sstevel@tonic-gate 		}
39757c478bd9Sstevel@tonic-gate 
3976034448feSmcpowers 	case CKA_EC_POINT:
3977f9fbec18Smcpowers 		return (get_bigint_attr_from_object(
3978f9fbec18Smcpowers 		    OBJ_PUB_EC_POINT(object_p), template));
3979034448feSmcpowers 
39807c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
39817c478bd9Sstevel@tonic-gate 		switch (keytype) {
39827c478bd9Sstevel@tonic-gate 		case CKK_DSA:
39837c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39847c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DSA_VALUE(object_p), template));
39857c478bd9Sstevel@tonic-gate 
39867c478bd9Sstevel@tonic-gate 		case CKK_DH:
39877c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39887c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH_VALUE(object_p), template));
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
39917c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
39927c478bd9Sstevel@tonic-gate 			    OBJ_PUB_DH942_VALUE(object_p), template));
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 		default:
39957c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
39967c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
39977c478bd9Sstevel@tonic-gate 		}
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 	default:
40007c478bd9Sstevel@tonic-gate 		/*
40017c478bd9Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
40027c478bd9Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
40037c478bd9Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
40047c478bd9Sstevel@tonic-gate 		 * attribute from the list of common attributes.
40057c478bd9Sstevel@tonic-gate 		 */
40067c478bd9Sstevel@tonic-gate 		rv = soft_get_common_key_attrs(object_p, template);
40077c478bd9Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
40087c478bd9Sstevel@tonic-gate 			rv = soft_get_common_attrs(object_p, template,
40097c478bd9Sstevel@tonic-gate 			    object_p->object_type);
40107c478bd9Sstevel@tonic-gate 		}
40117c478bd9Sstevel@tonic-gate 		break;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	return (rv);
40157c478bd9Sstevel@tonic-gate }
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate /*
40197c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute of a Private Key Object.
40207c478bd9Sstevel@tonic-gate  *
40217c478bd9Sstevel@tonic-gate  * Rule: All the attributes in the private key object can be revealed
40227c478bd9Sstevel@tonic-gate  *       except those marked with footnote number "7" when the object
40237c478bd9Sstevel@tonic-gate  *       has its CKA_SENSITIVE attribute set to TRUE or its
40247c478bd9Sstevel@tonic-gate  *       CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
40257c478bd9Sstevel@tonic-gate  */
40267c478bd9Sstevel@tonic-gate CK_RV
40277c478bd9Sstevel@tonic-gate soft_get_private_key_attribute(soft_object_t *object_p,
4028*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
40297c478bd9Sstevel@tonic-gate {
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
40327c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	/*
40367c478bd9Sstevel@tonic-gate 	 * If the following specified attributes for the private key
40377c478bd9Sstevel@tonic-gate 	 * object cannot be revealed because the object is sensitive
40387c478bd9Sstevel@tonic-gate 	 * or unextractable, then the ulValueLen is set to -1.
40397c478bd9Sstevel@tonic-gate 	 */
40407c478bd9Sstevel@tonic-gate 	if ((object_p->bool_attr_mask & SENSITIVE_BOOL_ON) ||
40417c478bd9Sstevel@tonic-gate 	    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate 		switch (template->type) {
40447c478bd9Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
40457c478bd9Sstevel@tonic-gate 		case CKA_PRIME_1:
40467c478bd9Sstevel@tonic-gate 		case CKA_PRIME_2:
40477c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_1:
40487c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_2:
40497c478bd9Sstevel@tonic-gate 		case CKA_COEFFICIENT:
40507c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
40517c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
40527c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_SENSITIVE);
40537c478bd9Sstevel@tonic-gate 		}
40547c478bd9Sstevel@tonic-gate 	}
40557c478bd9Sstevel@tonic-gate 
40567c478bd9Sstevel@tonic-gate 	switch (template->type) {
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	case CKA_SUBJECT:
4059f9fbec18Smcpowers 	case CKA_EC_PARAMS:
40607c478bd9Sstevel@tonic-gate 		/*
40617c478bd9Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
40627c478bd9Sstevel@tonic-gate 		 */
40637c478bd9Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
40647c478bd9Sstevel@tonic-gate 		    template));
40657c478bd9Sstevel@tonic-gate 
40667c478bd9Sstevel@tonic-gate 	/* Key related boolean attributes */
40677c478bd9Sstevel@tonic-gate 	case CKA_SENSITIVE:
40687c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40697c478bd9Sstevel@tonic-gate 		    SENSITIVE_BOOL_ON, template));
40707c478bd9Sstevel@tonic-gate 
40717c478bd9Sstevel@tonic-gate 	case CKA_SECONDARY_AUTH:
40727c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40737c478bd9Sstevel@tonic-gate 		    SECONDARY_AUTH_BOOL_ON, template));
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 	case CKA_DECRYPT:
40767c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40777c478bd9Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	case CKA_SIGN:
40807c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40817c478bd9Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	case CKA_SIGN_RECOVER:
40847c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40857c478bd9Sstevel@tonic-gate 		    SIGN_RECOVER_BOOL_ON, template));
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	case CKA_UNWRAP:
40887c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40897c478bd9Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
40927c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40937c478bd9Sstevel@tonic-gate 		    EXTRACTABLE_BOOL_ON, template));
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	case CKA_ALWAYS_SENSITIVE:
40967c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
40977c478bd9Sstevel@tonic-gate 		    ALWAYS_SENSITIVE_BOOL_ON, template));
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	case CKA_NEVER_EXTRACTABLE:
41007c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
41017c478bd9Sstevel@tonic-gate 		    NEVER_EXTRACTABLE_BOOL_ON, template));
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
41047c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41057c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41067c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_MOD(object_p), template));
41077c478bd9Sstevel@tonic-gate 		} else {
41087c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41097c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41107c478bd9Sstevel@tonic-gate 			break;
41117c478bd9Sstevel@tonic-gate 		}
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
41147c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41157c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41167c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_PUBEXPO(object_p), template));
41177c478bd9Sstevel@tonic-gate 		} else {
41187c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41197c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41207c478bd9Sstevel@tonic-gate 			break;
41217c478bd9Sstevel@tonic-gate 		}
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE_EXPONENT:
41247c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41257c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41267c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIEXPO(object_p), template));
41277c478bd9Sstevel@tonic-gate 		} else {
41287c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41297c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41307c478bd9Sstevel@tonic-gate 			break;
41317c478bd9Sstevel@tonic-gate 		}
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 	case CKA_PRIME_1:
41347c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41357c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41367c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIME1(object_p), template));
41377c478bd9Sstevel@tonic-gate 		} else {
41387c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41397c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41407c478bd9Sstevel@tonic-gate 			break;
41417c478bd9Sstevel@tonic-gate 		}
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate 	case CKA_PRIME_2:
41447c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41457c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41467c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIME2(object_p), template));
41477c478bd9Sstevel@tonic-gate 		} else {
41487c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41497c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41507c478bd9Sstevel@tonic-gate 			break;
41517c478bd9Sstevel@tonic-gate 		}
41527c478bd9Sstevel@tonic-gate 
41537c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_1:
41547c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41557c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41567c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_EXPO1(object_p), template));
41577c478bd9Sstevel@tonic-gate 		} else {
41587c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41597c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41607c478bd9Sstevel@tonic-gate 			break;
41617c478bd9Sstevel@tonic-gate 		}
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_2:
41647c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41657c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41667c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_EXPO2(object_p), template));
41677c478bd9Sstevel@tonic-gate 		} else {
41687c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41697c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41707c478bd9Sstevel@tonic-gate 			break;
41717c478bd9Sstevel@tonic-gate 		}
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 	case CKA_COEFFICIENT:
41747c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
41757c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41767c478bd9Sstevel@tonic-gate 			    OBJ_PRI_RSA_COEF(object_p), template));
41777c478bd9Sstevel@tonic-gate 		} else {
41787c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41797c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41807c478bd9Sstevel@tonic-gate 			break;
41817c478bd9Sstevel@tonic-gate 		}
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	case CKA_VALUE_BITS:
41847c478bd9Sstevel@tonic-gate 		if (keytype == CKK_DH) {
41857c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
41867c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH_VAL_BITS(object_p), template));
41877c478bd9Sstevel@tonic-gate 		} else {
41887c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
41897c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
41907c478bd9Sstevel@tonic-gate 			break;
41917c478bd9Sstevel@tonic-gate 		}
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
41947c478bd9Sstevel@tonic-gate 		switch (keytype) {
41957c478bd9Sstevel@tonic-gate 		case CKK_DSA:
41967c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
41977c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DSA_PRIME(object_p), template));
41987c478bd9Sstevel@tonic-gate 
41997c478bd9Sstevel@tonic-gate 		case CKK_DH:
42007c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42017c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH_PRIME(object_p), template));
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
42047c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42057c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH942_PRIME(object_p), template));
42067c478bd9Sstevel@tonic-gate 
42077c478bd9Sstevel@tonic-gate 		default:
42087c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
42097c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
42107c478bd9Sstevel@tonic-gate 		}
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
42137c478bd9Sstevel@tonic-gate 		switch (keytype) {
42147c478bd9Sstevel@tonic-gate 		case CKK_DSA:
42157c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42167c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DSA_SUBPRIME(object_p), template));
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
42197c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42207c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH942_SUBPRIME(object_p), template));
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 		default:
42237c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
42247c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
42257c478bd9Sstevel@tonic-gate 		}
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 	case CKA_BASE:
42287c478bd9Sstevel@tonic-gate 		switch (keytype) {
42297c478bd9Sstevel@tonic-gate 		case CKK_DSA:
42307c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42317c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DSA_BASE(object_p), template));
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 		case CKK_DH:
42347c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42357c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH_BASE(object_p), template));
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
42387c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42397c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH942_BASE(object_p), template));
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate 		default:
42427c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
42437c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
42447c478bd9Sstevel@tonic-gate 		}
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
42477c478bd9Sstevel@tonic-gate 		switch (keytype) {
42487c478bd9Sstevel@tonic-gate 		case CKK_DSA:
42497c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42507c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DSA_VALUE(object_p), template));
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate 		case CKK_DH:
42537c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42547c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH_VALUE(object_p), template));
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
42577c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
42587c478bd9Sstevel@tonic-gate 			    OBJ_PRI_DH942_VALUE(object_p), template));
42597c478bd9Sstevel@tonic-gate 
4260034448feSmcpowers 		case CKK_EC:
4261034448feSmcpowers 			return (get_bigint_attr_from_object(
4262034448feSmcpowers 			    OBJ_PRI_EC_VALUE(object_p), template));
4263034448feSmcpowers 
42647c478bd9Sstevel@tonic-gate 		default:
42657c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
42667c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	default:
42707c478bd9Sstevel@tonic-gate 		/*
42717c478bd9Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
42727c478bd9Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
42737c478bd9Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
42747c478bd9Sstevel@tonic-gate 		 * attribute from the list of common attributes.
42757c478bd9Sstevel@tonic-gate 		 */
42767c478bd9Sstevel@tonic-gate 		rv = soft_get_common_key_attrs(object_p, template);
42777c478bd9Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
42787c478bd9Sstevel@tonic-gate 			rv = soft_get_common_attrs(object_p, template,
42797c478bd9Sstevel@tonic-gate 			    object_p->object_type);
42807c478bd9Sstevel@tonic-gate 		}
42817c478bd9Sstevel@tonic-gate 		break;
42827c478bd9Sstevel@tonic-gate 	}
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate 	return (rv);
42857c478bd9Sstevel@tonic-gate }
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate /*
42897c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute of a Secret Key Object.
42907c478bd9Sstevel@tonic-gate  *
42917c478bd9Sstevel@tonic-gate  * Rule: All the attributes in the secret key object can be revealed
42927c478bd9Sstevel@tonic-gate  *       except those marked with footnote number "7" when the object
42937c478bd9Sstevel@tonic-gate  *       has its CKA_SENSITIVE attribute set to TRUE or its
42947c478bd9Sstevel@tonic-gate  *       CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
42957c478bd9Sstevel@tonic-gate  */
42967c478bd9Sstevel@tonic-gate CK_RV
42977c478bd9Sstevel@tonic-gate soft_get_secret_key_attribute(soft_object_t *object_p,
4298*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
42997c478bd9Sstevel@tonic-gate {
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
43027c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	switch (template->type) {
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate 	/* Key related boolean attributes */
43077c478bd9Sstevel@tonic-gate 	case CKA_SENSITIVE:
43087c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43097c478bd9Sstevel@tonic-gate 		    SENSITIVE_BOOL_ON, template));
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	case CKA_ENCRYPT:
43127c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43137c478bd9Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 	case CKA_DECRYPT:
43167c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43177c478bd9Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	case CKA_SIGN:
43207c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43217c478bd9Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 	case CKA_VERIFY:
43247c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43257c478bd9Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 	case CKA_WRAP:
43287c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43297c478bd9Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 	case CKA_UNWRAP:
43327c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43337c478bd9Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
43367c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43377c478bd9Sstevel@tonic-gate 		    EXTRACTABLE_BOOL_ON, template));
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	case CKA_ALWAYS_SENSITIVE:
43407c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43417c478bd9Sstevel@tonic-gate 		    ALWAYS_SENSITIVE_BOOL_ON, template));
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate 	case CKA_NEVER_EXTRACTABLE:
43447c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
43457c478bd9Sstevel@tonic-gate 		    NEVER_EXTRACTABLE_BOOL_ON, template));
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
43487c478bd9Sstevel@tonic-gate 	case CKA_VALUE_LEN:
43497c478bd9Sstevel@tonic-gate 		/*
43507c478bd9Sstevel@tonic-gate 		 * If the specified attribute for the secret key object
43517c478bd9Sstevel@tonic-gate 		 * cannot be revealed because the object is sensitive
43527c478bd9Sstevel@tonic-gate 		 * or unextractable, then the ulValueLen is set to -1.
43537c478bd9Sstevel@tonic-gate 		 */
43547c478bd9Sstevel@tonic-gate 		if ((object_p->bool_attr_mask & SENSITIVE_BOOL_ON) ||
43557c478bd9Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
43567c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
43577c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_SENSITIVE);
43587c478bd9Sstevel@tonic-gate 		}
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate 		switch (keytype) {
43617c478bd9Sstevel@tonic-gate 		case CKK_RC4:
43627c478bd9Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
43637c478bd9Sstevel@tonic-gate 		case CKK_RC5:
43647c478bd9Sstevel@tonic-gate 		case CKK_DES:
43657c478bd9Sstevel@tonic-gate 		case CKK_DES2:
43667c478bd9Sstevel@tonic-gate 		case CKK_DES3:
43677c478bd9Sstevel@tonic-gate 		case CKK_CDMF:
43687c478bd9Sstevel@tonic-gate 		case CKK_AES:
4369f66d273dSizick 		case CKK_BLOWFISH:
43707c478bd9Sstevel@tonic-gate 			if (template->type == CKA_VALUE_LEN) {
43717c478bd9Sstevel@tonic-gate 				return (get_ulong_attr_from_object(
43727c478bd9Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(object_p),
43737c478bd9Sstevel@tonic-gate 				    template));
43747c478bd9Sstevel@tonic-gate 			} else {
43757c478bd9Sstevel@tonic-gate 				return (get_bigint_attr_from_object(
43767c478bd9Sstevel@tonic-gate 				    (biginteger_t *)OBJ_SEC(object_p),
43777c478bd9Sstevel@tonic-gate 				    template));
43787c478bd9Sstevel@tonic-gate 			}
43797c478bd9Sstevel@tonic-gate 		default:
43807c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
43817c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
43827c478bd9Sstevel@tonic-gate 			break;
43837c478bd9Sstevel@tonic-gate 		}
43847c478bd9Sstevel@tonic-gate 		break;
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate 	default:
43877c478bd9Sstevel@tonic-gate 		/*
43887c478bd9Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
43897c478bd9Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
43907c478bd9Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
43917c478bd9Sstevel@tonic-gate 		 * attribute from the list of common attributes.
43927c478bd9Sstevel@tonic-gate 		 */
43937c478bd9Sstevel@tonic-gate 		rv = soft_get_common_key_attrs(object_p, template);
43947c478bd9Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
43957c478bd9Sstevel@tonic-gate 			rv = soft_get_common_attrs(object_p, template,
43967c478bd9Sstevel@tonic-gate 			    object_p->object_type);
43977c478bd9Sstevel@tonic-gate 		}
43987c478bd9Sstevel@tonic-gate 		break;
43997c478bd9Sstevel@tonic-gate 	}
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	return (rv);
44027c478bd9Sstevel@tonic-gate }
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate /*
44067c478bd9Sstevel@tonic-gate  * Get the value of a requested attribute of a Domain Parameters Object.
44077c478bd9Sstevel@tonic-gate  *
44087c478bd9Sstevel@tonic-gate  * Rule: All the attributes in the domain parameters object can be revealed.
44097c478bd9Sstevel@tonic-gate  */
44107c478bd9Sstevel@tonic-gate CK_RV
44117c478bd9Sstevel@tonic-gate soft_get_domain_parameters_attribute(soft_object_t *object_p,
4412*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
44137c478bd9Sstevel@tonic-gate {
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
44167c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 	switch (template->type) {
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	case CKA_KEY_TYPE:
44217c478bd9Sstevel@tonic-gate 		return (get_ulong_attr_from_object(keytype,
44227c478bd9Sstevel@tonic-gate 		    template));
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	case CKA_LOCAL:
44257c478bd9Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
44267c478bd9Sstevel@tonic-gate 		    LOCAL_BOOL_ON, template));
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
44297c478bd9Sstevel@tonic-gate 		switch (keytype) {
44307c478bd9Sstevel@tonic-gate 		case CKK_DSA:
44317c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44327c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DSA_PRIME(object_p), template));
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate 		case CKK_DH:
44357c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44367c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH_PRIME(object_p), template));
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
44397c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44407c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH942_PRIME(object_p), template));
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 		default:
44437c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
44447c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
44457c478bd9Sstevel@tonic-gate 		}
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
44487c478bd9Sstevel@tonic-gate 		switch (keytype) {
44497c478bd9Sstevel@tonic-gate 		case CKK_DSA:
44507c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44517c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DSA_SUBPRIME(object_p), template));
44527c478bd9Sstevel@tonic-gate 
44537c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
44547c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44557c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH942_SUBPRIME(object_p), template));
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 		default:
44587c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
44597c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
44607c478bd9Sstevel@tonic-gate 		}
44617c478bd9Sstevel@tonic-gate 
44627c478bd9Sstevel@tonic-gate 	case CKA_BASE:
44637c478bd9Sstevel@tonic-gate 		switch (keytype) {
44647c478bd9Sstevel@tonic-gate 		case CKK_DSA:
44657c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44667c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DSA_BASE(object_p), template));
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate 		case CKK_DH:
44697c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44707c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH_BASE(object_p), template));
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
44737c478bd9Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
44747c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH942_BASE(object_p), template));
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 		default:
44777c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
44787c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
44797c478bd9Sstevel@tonic-gate 		}
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 	case CKA_PRIME_BITS:
44827c478bd9Sstevel@tonic-gate 		switch (keytype) {
44837c478bd9Sstevel@tonic-gate 		case CKK_DSA:
44847c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
44857c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DSA_PRIME_BITS(object_p), template));
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate 		case CKK_DH:
44887c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
44897c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH_PRIME_BITS(object_p), template));
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
44927c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
44937c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH942_PRIME_BITS(object_p), template));
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 		default:
44967c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
44977c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
44987c478bd9Sstevel@tonic-gate 		}
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	case CKA_SUB_PRIME_BITS:
45017c478bd9Sstevel@tonic-gate 		switch (keytype) {
45027c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
45037c478bd9Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
45047c478bd9Sstevel@tonic-gate 			    OBJ_DOM_DH942_SUBPRIME_BITS(object_p), template));
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 		default:
45077c478bd9Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
45087c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
45097c478bd9Sstevel@tonic-gate 		}
45107c478bd9Sstevel@tonic-gate 
45117c478bd9Sstevel@tonic-gate 	default:
45127c478bd9Sstevel@tonic-gate 		/*
45137c478bd9Sstevel@tonic-gate 		 * Get the value of a common attribute.
45147c478bd9Sstevel@tonic-gate 		 */
45157c478bd9Sstevel@tonic-gate 		rv = soft_get_common_attrs(object_p, template,
45167c478bd9Sstevel@tonic-gate 		    object_p->object_type);
45177c478bd9Sstevel@tonic-gate 		break;
45187c478bd9Sstevel@tonic-gate 	}
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate 	return (rv);
45217c478bd9Sstevel@tonic-gate }
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate /*
45247c478bd9Sstevel@tonic-gate  * Get certificate attributes from an object.
45257c478bd9Sstevel@tonic-gate  * return CKR_ATTRIBUTE_TYPE_INVALID if the requested type
45267c478bd9Sstevel@tonic-gate  * does not exist in the certificate.
45277c478bd9Sstevel@tonic-gate  */
45287c478bd9Sstevel@tonic-gate CK_RV
45297c478bd9Sstevel@tonic-gate soft_get_certificate_attribute(soft_object_t *object_p,
4530*a8793c76SJason King     CK_ATTRIBUTE_PTR template)
45317c478bd9Sstevel@tonic-gate {
45327c478bd9Sstevel@tonic-gate 	CK_CERTIFICATE_TYPE certtype = object_p->cert_type;
45337c478bd9Sstevel@tonic-gate 	cert_attr_t src;
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	switch (template->type) {
45367c478bd9Sstevel@tonic-gate 		case CKA_SUBJECT:
45377c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509) {
45387c478bd9Sstevel@tonic-gate 				return (get_cert_attr_from_object(
4539f9fbec18Smcpowers 				    X509_CERT_SUBJECT(object_p), template));
45407c478bd9Sstevel@tonic-gate 			}
45417c478bd9Sstevel@tonic-gate 			break;
45427c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
45437c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509) {
4544f9fbec18Smcpowers 				return (get_cert_attr_from_object(
4545f9fbec18Smcpowers 				    X509_CERT_VALUE(object_p), template));
45467c478bd9Sstevel@tonic-gate 			} else if (certtype == CKC_X_509_ATTR_CERT) {
4547f9fbec18Smcpowers 				return (get_cert_attr_from_object(
4548f9fbec18Smcpowers 				    X509_ATTR_CERT_VALUE(object_p), template));
45497c478bd9Sstevel@tonic-gate 			}
45507c478bd9Sstevel@tonic-gate 			break;
45517c478bd9Sstevel@tonic-gate 		case CKA_OWNER:
45527c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509_ATTR_CERT) {
45537c478bd9Sstevel@tonic-gate 				return (get_cert_attr_from_object(
4554f9fbec18Smcpowers 				    X509_ATTR_CERT_OWNER(object_p), template));
45557c478bd9Sstevel@tonic-gate 			}
45567c478bd9Sstevel@tonic-gate 			break;
45577c478bd9Sstevel@tonic-gate 		case CKA_CERTIFICATE_TYPE:
45587c478bd9Sstevel@tonic-gate 			src.value = (CK_BYTE *)&certtype;
45597c478bd9Sstevel@tonic-gate 			src.length = sizeof (certtype);
45607c478bd9Sstevel@tonic-gate 			return (get_cert_attr_from_object(&src, template));
45617c478bd9Sstevel@tonic-gate 		case CKA_TRUSTED:
45627c478bd9Sstevel@tonic-gate 			return (get_bool_attr_from_object(object_p,
4563f9fbec18Smcpowers 			    TRUSTED_BOOL_ON, template));
45647c478bd9Sstevel@tonic-gate 		case CKA_ID:
45657c478bd9Sstevel@tonic-gate 		case CKA_ISSUER:
45667c478bd9Sstevel@tonic-gate 		case CKA_SERIAL_NUMBER:
45677c478bd9Sstevel@tonic-gate 		case CKA_AC_ISSUER:
45687c478bd9Sstevel@tonic-gate 		case CKA_ATTR_TYPES:
45697c478bd9Sstevel@tonic-gate 			return (get_extra_attr_from_object(object_p,
4570f66d273dSizick 			    template));
45717c478bd9Sstevel@tonic-gate 		default:
45727c478bd9Sstevel@tonic-gate 			return (soft_get_common_attrs(object_p, template,
4573f9fbec18Smcpowers 			    object_p->object_type));
45747c478bd9Sstevel@tonic-gate 	}
45757c478bd9Sstevel@tonic-gate 
45767c478bd9Sstevel@tonic-gate 	/*
45777c478bd9Sstevel@tonic-gate 	 * If we got this far, then the combination of certificate type
45787c478bd9Sstevel@tonic-gate 	 * and requested attribute is invalid.
45797c478bd9Sstevel@tonic-gate 	 */
45807c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
45817c478bd9Sstevel@tonic-gate }
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate CK_RV
45847c478bd9Sstevel@tonic-gate soft_set_certificate_attribute(soft_object_t *object_p,
4585*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
45867c478bd9Sstevel@tonic-gate {
45877c478bd9Sstevel@tonic-gate 	CK_CERTIFICATE_TYPE certtype = object_p->cert_type;
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 	switch (template->type) {
45907c478bd9Sstevel@tonic-gate 		case CKA_SUBJECT:
45917c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509) {
45927c478bd9Sstevel@tonic-gate 				/* SUBJECT attr cannot be modified. */
45937c478bd9Sstevel@tonic-gate 				return (CKR_ATTRIBUTE_READ_ONLY);
45947c478bd9Sstevel@tonic-gate 			}
45957c478bd9Sstevel@tonic-gate 			break;
45967c478bd9Sstevel@tonic-gate 		case CKA_OWNER:
45977c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509_ATTR_CERT) {
45987c478bd9Sstevel@tonic-gate 				/* OWNER attr cannot be modified. */
45997c478bd9Sstevel@tonic-gate 				return (CKR_ATTRIBUTE_READ_ONLY);
46007c478bd9Sstevel@tonic-gate 			}
46017c478bd9Sstevel@tonic-gate 			break;
46027c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
46037c478bd9Sstevel@tonic-gate 			/* VALUE attr cannot be modified. */
46047c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
46057c478bd9Sstevel@tonic-gate 		case CKA_ID:
46067c478bd9Sstevel@tonic-gate 		case CKA_ISSUER:
46077c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509) {
46087c478bd9Sstevel@tonic-gate 				return (set_extra_attr_to_object(object_p,
4609f9fbec18Smcpowers 				    template->type, template));
46107c478bd9Sstevel@tonic-gate 			}
46117c478bd9Sstevel@tonic-gate 			break;
46127c478bd9Sstevel@tonic-gate 		case CKA_AC_ISSUER:
46137c478bd9Sstevel@tonic-gate 		case CKA_ATTR_TYPES:
46147c478bd9Sstevel@tonic-gate 			if (certtype == CKC_X_509_ATTR_CERT) {
46157c478bd9Sstevel@tonic-gate 				return (set_extra_attr_to_object(object_p,
4616f9fbec18Smcpowers 				    template->type, template));
46177c478bd9Sstevel@tonic-gate 			}
46187c478bd9Sstevel@tonic-gate 			break;
46197c478bd9Sstevel@tonic-gate 		case CKA_SERIAL_NUMBER:
46207c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
46217c478bd9Sstevel@tonic-gate 			return (set_extra_attr_to_object(object_p,
4622f9fbec18Smcpowers 			    template->type, template));
46237c478bd9Sstevel@tonic-gate 		default:
46247c478bd9Sstevel@tonic-gate 			return (soft_set_common_storage_attribute(
46257c478bd9Sstevel@tonic-gate 			    object_p, template, copy));
46267c478bd9Sstevel@tonic-gate 	}
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 	/*
46297c478bd9Sstevel@tonic-gate 	 * If we got this far, then the combination of certificate type
46307c478bd9Sstevel@tonic-gate 	 * and requested attribute is invalid.
46317c478bd9Sstevel@tonic-gate 	 */
46327c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
46337c478bd9Sstevel@tonic-gate }
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate /*
46367c478bd9Sstevel@tonic-gate  * Call the appropriate get attribute function according to the class
46377c478bd9Sstevel@tonic-gate  * of object.
46387c478bd9Sstevel@tonic-gate  *
46397c478bd9Sstevel@tonic-gate  * The caller of this function holds the lock on the object.
46407c478bd9Sstevel@tonic-gate  */
46417c478bd9Sstevel@tonic-gate CK_RV
46427c478bd9Sstevel@tonic-gate soft_get_attribute(soft_object_t *object_p, CK_ATTRIBUTE_PTR template)
46437c478bd9Sstevel@tonic-gate {
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
46467c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS class = object_p->class;
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	switch (class) {
46497c478bd9Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
46507c478bd9Sstevel@tonic-gate 		rv = soft_get_public_key_attribute(object_p, template);
46517c478bd9Sstevel@tonic-gate 		break;
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
46547c478bd9Sstevel@tonic-gate 		rv = soft_get_private_key_attribute(object_p, template);
46557c478bd9Sstevel@tonic-gate 		break;
46567c478bd9Sstevel@tonic-gate 
46577c478bd9Sstevel@tonic-gate 	case CKO_SECRET_KEY:
46587c478bd9Sstevel@tonic-gate 		rv = soft_get_secret_key_attribute(object_p, template);
46597c478bd9Sstevel@tonic-gate 		break;
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
46627c478bd9Sstevel@tonic-gate 		rv = soft_get_domain_parameters_attribute(object_p, template);
46637c478bd9Sstevel@tonic-gate 		break;
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	case CKO_CERTIFICATE:
46667c478bd9Sstevel@tonic-gate 		rv = soft_get_certificate_attribute(object_p, template);
46677c478bd9Sstevel@tonic-gate 		break;
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	default:
46707c478bd9Sstevel@tonic-gate 		/*
46717c478bd9Sstevel@tonic-gate 		 * If the specified attribute for the object is invalid
46727c478bd9Sstevel@tonic-gate 		 * (the object does not possess such as attribute), then
46737c478bd9Sstevel@tonic-gate 		 * the ulValueLen is modified to hold the value -1.
46747c478bd9Sstevel@tonic-gate 		 */
46757c478bd9Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
46767c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
46777c478bd9Sstevel@tonic-gate 	}
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	return (rv);
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate }
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate CK_RV
46847c478bd9Sstevel@tonic-gate soft_set_common_storage_attribute(soft_object_t *object_p,
4685*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
46867c478bd9Sstevel@tonic-gate {
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	switch (template->type) {
46917c478bd9Sstevel@tonic-gate 
46927c478bd9Sstevel@tonic-gate 	case CKA_TOKEN:
46937c478bd9Sstevel@tonic-gate 		if (copy) {
46947c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template->pValue) == B_TRUE) {
469590e0e8c4Sizick 				if (!soft_keystore_status(KEYSTORE_INITIALIZED))
46967c478bd9Sstevel@tonic-gate 					return (CKR_DEVICE_REMOVED);
46977c478bd9Sstevel@tonic-gate 				object_p->object_type |= TOKEN_OBJECT;
46987c478bd9Sstevel@tonic-gate 			}
46997c478bd9Sstevel@tonic-gate 		} else {
47007c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_READ_ONLY;
47017c478bd9Sstevel@tonic-gate 		}
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 		break;
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE:
47067c478bd9Sstevel@tonic-gate 		if (copy) {
47077c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template->pValue) == B_TRUE) {
47087c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_lock(&soft_giant_mutex);
47097c478bd9Sstevel@tonic-gate 				if (!soft_slot.authenticated) {
47107c478bd9Sstevel@tonic-gate 					/*
47117c478bd9Sstevel@tonic-gate 					 * Check if this is the special case
47127c478bd9Sstevel@tonic-gate 					 * when the PIN is never initialized
47137c478bd9Sstevel@tonic-gate 					 * in the keystore. If true, we will
47147c478bd9Sstevel@tonic-gate 					 * let it pass here and let it fail
47157c478bd9Sstevel@tonic-gate 					 * with CKR_PIN_EXPIRED later on.
47167c478bd9Sstevel@tonic-gate 					 */
47177c478bd9Sstevel@tonic-gate 					if (!soft_slot.userpin_change_needed) {
47187c478bd9Sstevel@tonic-gate 						(void) pthread_mutex_unlock(
47197c478bd9Sstevel@tonic-gate 						    &soft_giant_mutex);
47207c478bd9Sstevel@tonic-gate 						return (CKR_USER_NOT_LOGGED_IN);
47217c478bd9Sstevel@tonic-gate 					}
47227c478bd9Sstevel@tonic-gate 				}
47237c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&soft_giant_mutex);
47247c478bd9Sstevel@tonic-gate 				object_p->object_type |= PRIVATE_OBJECT;
47257c478bd9Sstevel@tonic-gate 			}
47267c478bd9Sstevel@tonic-gate 		} else {
47277c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_READ_ONLY;
47287c478bd9Sstevel@tonic-gate 		}
47297c478bd9Sstevel@tonic-gate 		break;
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate 	case CKA_MODIFIABLE:
47327c478bd9Sstevel@tonic-gate 		if (copy) {
47337c478bd9Sstevel@tonic-gate 			if ((*(CK_BBOOL *)template->pValue) == TRUE)
47347c478bd9Sstevel@tonic-gate 				object_p->bool_attr_mask &=
47357c478bd9Sstevel@tonic-gate 				    ~NOT_MODIFIABLE_BOOL_ON;
47367c478bd9Sstevel@tonic-gate 			else
47377c478bd9Sstevel@tonic-gate 				object_p->bool_attr_mask |=
47387c478bd9Sstevel@tonic-gate 				    NOT_MODIFIABLE_BOOL_ON;
47397c478bd9Sstevel@tonic-gate 		} else {
47407c478bd9Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_READ_ONLY;
47417c478bd9Sstevel@tonic-gate 		}
47427c478bd9Sstevel@tonic-gate 		break;
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate 	case CKA_CLASS:
47457c478bd9Sstevel@tonic-gate 		rv = CKR_ATTRIBUTE_READ_ONLY;
47467c478bd9Sstevel@tonic-gate 		break;
47477c478bd9Sstevel@tonic-gate 
47487c478bd9Sstevel@tonic-gate 	default:
47497c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
47507c478bd9Sstevel@tonic-gate 	}
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate 	return (rv);
47537c478bd9Sstevel@tonic-gate }
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate /*
47567c478bd9Sstevel@tonic-gate  * Set the value of an attribute that is common to all key objects
47577c478bd9Sstevel@tonic-gate  * (i.e. public key, private key and secret key).
47587c478bd9Sstevel@tonic-gate  */
47597c478bd9Sstevel@tonic-gate CK_RV
47607c478bd9Sstevel@tonic-gate soft_set_common_key_attribute(soft_object_t *object_p,
4761*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
47627c478bd9Sstevel@tonic-gate {
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	switch (template->type) {
47657c478bd9Sstevel@tonic-gate 
47667c478bd9Sstevel@tonic-gate 	case CKA_LABEL:
47677c478bd9Sstevel@tonic-gate 		/*
47687c478bd9Sstevel@tonic-gate 		 * Only the LABEL can be modified in the common storage
47697c478bd9Sstevel@tonic-gate 		 * object attributes after the object is created.
47707c478bd9Sstevel@tonic-gate 		 */
47717c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
47727c478bd9Sstevel@tonic-gate 		    CKA_LABEL, template));
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 	case CKA_ID:
47757c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
47767c478bd9Sstevel@tonic-gate 		    CKA_ID, template));
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 	case CKA_START_DATE:
47797c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
47807c478bd9Sstevel@tonic-gate 		    CKA_START_DATE, template));
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate 	case CKA_END_DATE:
47837c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
47847c478bd9Sstevel@tonic-gate 		    CKA_END_DATE, template));
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 	case CKA_DERIVE:
47877c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
47887c478bd9Sstevel@tonic-gate 		    DERIVE_BOOL_ON, template));
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 	case CKA_KEY_TYPE:
47917c478bd9Sstevel@tonic-gate 	case CKA_LOCAL:
47927c478bd9Sstevel@tonic-gate 	case CKA_KEY_GEN_MECHANISM:
47937c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_READ_ONLY);
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 	default:
47967c478bd9Sstevel@tonic-gate 		return (soft_set_common_storage_attribute(object_p,
47977c478bd9Sstevel@tonic-gate 		    template, copy));
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	}
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate }
48027c478bd9Sstevel@tonic-gate 
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate /*
48057c478bd9Sstevel@tonic-gate  * Set the value of an attribute of a Public Key Object.
48067c478bd9Sstevel@tonic-gate  *
48077c478bd9Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
48087c478bd9Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
48097c478bd9Sstevel@tonic-gate  */
48107c478bd9Sstevel@tonic-gate CK_RV
48117c478bd9Sstevel@tonic-gate soft_set_public_key_attribute(soft_object_t *object_p,
4812*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
48137c478bd9Sstevel@tonic-gate {
48147c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
48157c478bd9Sstevel@tonic-gate 
48167c478bd9Sstevel@tonic-gate 	switch (template->type) {
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	case CKA_SUBJECT:
48197c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
48207c478bd9Sstevel@tonic-gate 		    CKA_SUBJECT, template));
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate 	case CKA_ENCRYPT:
48237c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
48247c478bd9Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 	case CKA_VERIFY:
48277c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
48287c478bd9Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	case CKA_VERIFY_RECOVER:
48317c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
48327c478bd9Sstevel@tonic-gate 		    VERIFY_RECOVER_BOOL_ON, template));
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 	case CKA_WRAP:
48357c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
48367c478bd9Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
48377c478bd9Sstevel@tonic-gate 
48387c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
48397c478bd9Sstevel@tonic-gate 	case CKA_MODULUS_BITS:
48407c478bd9Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
48417c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA)
48427c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
48437c478bd9Sstevel@tonic-gate 		break;
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
48467c478bd9Sstevel@tonic-gate 		if ((keytype == CKK_DSA) ||
48477c478bd9Sstevel@tonic-gate 		    (keytype == CKK_X9_42_DH))
48487c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
48497c478bd9Sstevel@tonic-gate 		break;
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
48527c478bd9Sstevel@tonic-gate 	case CKA_BASE:
48537c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
48547c478bd9Sstevel@tonic-gate 		if ((keytype == CKK_DSA) ||
48557c478bd9Sstevel@tonic-gate 		    (keytype == CKK_DH) ||
48567c478bd9Sstevel@tonic-gate 		    (keytype == CKK_X9_42_DH))
48577c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
48587c478bd9Sstevel@tonic-gate 		break;
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	default:
48617c478bd9Sstevel@tonic-gate 		/*
48627c478bd9Sstevel@tonic-gate 		 * Set the value of a common key attribute.
48637c478bd9Sstevel@tonic-gate 		 */
48647c478bd9Sstevel@tonic-gate 		return (soft_set_common_key_attribute(object_p,
48657c478bd9Sstevel@tonic-gate 		    template, copy));
48667c478bd9Sstevel@tonic-gate 
48677c478bd9Sstevel@tonic-gate 	}
48687c478bd9Sstevel@tonic-gate 	/*
48697c478bd9Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
48707c478bd9Sstevel@tonic-gate 	 * and requested attribute is invalid.
48717c478bd9Sstevel@tonic-gate 	 */
48727c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
48737c478bd9Sstevel@tonic-gate }
48747c478bd9Sstevel@tonic-gate 
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate /*
48777c478bd9Sstevel@tonic-gate  * Set the value of an attribute of a Private Key Object.
48787c478bd9Sstevel@tonic-gate  *
48797c478bd9Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
48807c478bd9Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
48817c478bd9Sstevel@tonic-gate  */
48827c478bd9Sstevel@tonic-gate CK_RV
48837c478bd9Sstevel@tonic-gate soft_set_private_key_attribute(soft_object_t *object_p,
4884*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
48857c478bd9Sstevel@tonic-gate {
48867c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	switch (template->type) {
48897c478bd9Sstevel@tonic-gate 
48907c478bd9Sstevel@tonic-gate 	case CKA_SUBJECT:
48917c478bd9Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
48927c478bd9Sstevel@tonic-gate 		    CKA_SUBJECT, template));
48937c478bd9Sstevel@tonic-gate 
48947c478bd9Sstevel@tonic-gate 	case CKA_SENSITIVE:
48957c478bd9Sstevel@tonic-gate 		/*
48967c478bd9Sstevel@tonic-gate 		 * Cannot set SENSITIVE to FALSE if it is already ON.
48977c478bd9Sstevel@tonic-gate 		 */
48987c478bd9Sstevel@tonic-gate 		if (((*(CK_BBOOL *)template->pValue) == B_FALSE) &&
48997c478bd9Sstevel@tonic-gate 		    (object_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
49007c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49017c478bd9Sstevel@tonic-gate 		}
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate 		if (*(CK_BBOOL *)template->pValue)
49047c478bd9Sstevel@tonic-gate 			object_p->bool_attr_mask |= SENSITIVE_BOOL_ON;
49057c478bd9Sstevel@tonic-gate 		return (CKR_OK);
49067c478bd9Sstevel@tonic-gate 
49077c478bd9Sstevel@tonic-gate 	case CKA_DECRYPT:
49087c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
49097c478bd9Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
49107c478bd9Sstevel@tonic-gate 
49117c478bd9Sstevel@tonic-gate 	case CKA_SIGN:
49127c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
49137c478bd9Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 	case CKA_SIGN_RECOVER:
49167c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
49177c478bd9Sstevel@tonic-gate 		    SIGN_RECOVER_BOOL_ON, template));
49187c478bd9Sstevel@tonic-gate 
49197c478bd9Sstevel@tonic-gate 	case CKA_UNWRAP:
49207c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
49217c478bd9Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
49247c478bd9Sstevel@tonic-gate 		/*
49257c478bd9Sstevel@tonic-gate 		 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
49267c478bd9Sstevel@tonic-gate 		 */
49277c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) &&
49287c478bd9Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
49297c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49307c478bd9Sstevel@tonic-gate 		}
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_FALSE)
49337c478bd9Sstevel@tonic-gate 			object_p->bool_attr_mask &= ~EXTRACTABLE_BOOL_ON;
49347c478bd9Sstevel@tonic-gate 		return (CKR_OK);
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
49377c478bd9Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
49387c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE_EXPONENT:
49397c478bd9Sstevel@tonic-gate 	case CKA_PRIME_1:
49407c478bd9Sstevel@tonic-gate 	case CKA_PRIME_2:
49417c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_1:
49427c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_2:
49437c478bd9Sstevel@tonic-gate 	case CKA_COEFFICIENT:
49447c478bd9Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
49457c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49467c478bd9Sstevel@tonic-gate 		}
49477c478bd9Sstevel@tonic-gate 		break;
49487c478bd9Sstevel@tonic-gate 
49497c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
49507c478bd9Sstevel@tonic-gate 		if ((keytype == CKK_DSA) ||
49517c478bd9Sstevel@tonic-gate 		    (keytype == CKK_X9_42_DH))
49527c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49537c478bd9Sstevel@tonic-gate 		break;
49547c478bd9Sstevel@tonic-gate 
49557c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
49567c478bd9Sstevel@tonic-gate 	case CKA_BASE:
49577c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
49587c478bd9Sstevel@tonic-gate 		if ((keytype == CKK_DSA) ||
49597c478bd9Sstevel@tonic-gate 		    (keytype == CKK_DH) ||
49607c478bd9Sstevel@tonic-gate 		    (keytype == CKK_X9_42_DH))
49617c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49627c478bd9Sstevel@tonic-gate 		break;
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	case CKA_VALUE_BITS:
49657c478bd9Sstevel@tonic-gate 		if (keytype == CKK_DH)
49667c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
49677c478bd9Sstevel@tonic-gate 		break;
49687c478bd9Sstevel@tonic-gate 
49697c478bd9Sstevel@tonic-gate 	default:
49707c478bd9Sstevel@tonic-gate 		/*
49717c478bd9Sstevel@tonic-gate 		 * Set the value of a common key attribute.
49727c478bd9Sstevel@tonic-gate 		 */
49737c478bd9Sstevel@tonic-gate 		return (soft_set_common_key_attribute(object_p,
49747c478bd9Sstevel@tonic-gate 		    template, copy));
49757c478bd9Sstevel@tonic-gate 	}
49767c478bd9Sstevel@tonic-gate 
49777c478bd9Sstevel@tonic-gate 	/*
49787c478bd9Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
49797c478bd9Sstevel@tonic-gate 	 * and requested attribute is invalid.
49807c478bd9Sstevel@tonic-gate 	 */
49817c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
49827c478bd9Sstevel@tonic-gate }
49837c478bd9Sstevel@tonic-gate 
49847c478bd9Sstevel@tonic-gate /*
49857c478bd9Sstevel@tonic-gate  * Set the value of an attribute of a Secret Key Object.
49867c478bd9Sstevel@tonic-gate  *
49877c478bd9Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
49887c478bd9Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
49897c478bd9Sstevel@tonic-gate  */
49907c478bd9Sstevel@tonic-gate CK_RV
49917c478bd9Sstevel@tonic-gate soft_set_secret_key_attribute(soft_object_t *object_p,
4992*a8793c76SJason King     CK_ATTRIBUTE_PTR template, boolean_t copy)
49937c478bd9Sstevel@tonic-gate {
49947c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
49957c478bd9Sstevel@tonic-gate 
49967c478bd9Sstevel@tonic-gate 	switch (template->type) {
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate 	case CKA_SENSITIVE:
49997c478bd9Sstevel@tonic-gate 		/*
50007c478bd9Sstevel@tonic-gate 		 * Cannot set SENSITIVE to FALSE if it is already ON.
50017c478bd9Sstevel@tonic-gate 		 */
50027c478bd9Sstevel@tonic-gate 		if (((*(CK_BBOOL *)template->pValue) == B_FALSE) &&
50037c478bd9Sstevel@tonic-gate 		    (object_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
50047c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
50057c478bd9Sstevel@tonic-gate 		}
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate 		if (*(CK_BBOOL *)template->pValue)
50087c478bd9Sstevel@tonic-gate 			object_p->bool_attr_mask |= SENSITIVE_BOOL_ON;
50097c478bd9Sstevel@tonic-gate 		return (CKR_OK);
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 	case CKA_ENCRYPT:
50127c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50137c478bd9Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	case CKA_DECRYPT:
50167c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50177c478bd9Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
50187c478bd9Sstevel@tonic-gate 
50197c478bd9Sstevel@tonic-gate 	case CKA_SIGN:
50207c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50217c478bd9Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
50227c478bd9Sstevel@tonic-gate 
50237c478bd9Sstevel@tonic-gate 	case CKA_VERIFY:
50247c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50257c478bd9Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	case CKA_WRAP:
50287c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50297c478bd9Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
50307c478bd9Sstevel@tonic-gate 
50317c478bd9Sstevel@tonic-gate 	case CKA_UNWRAP:
50327c478bd9Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
50337c478bd9Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
50367c478bd9Sstevel@tonic-gate 		/*
50377c478bd9Sstevel@tonic-gate 		 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
50387c478bd9Sstevel@tonic-gate 		 */
50397c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) &&
50407c478bd9Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
50417c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
50427c478bd9Sstevel@tonic-gate 		}
50437c478bd9Sstevel@tonic-gate 
50447c478bd9Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_FALSE)
50457c478bd9Sstevel@tonic-gate 			object_p->bool_attr_mask &= ~EXTRACTABLE_BOOL_ON;
50467c478bd9Sstevel@tonic-gate 		return (CKR_OK);
50477c478bd9Sstevel@tonic-gate 
50487c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
50497c478bd9Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_READ_ONLY);
50507c478bd9Sstevel@tonic-gate 
50517c478bd9Sstevel@tonic-gate 	case CKA_VALUE_LEN:
50527c478bd9Sstevel@tonic-gate 		if ((keytype == CKK_RC4) ||
50537c478bd9Sstevel@tonic-gate 		    (keytype == CKK_GENERIC_SECRET) ||
5054f66d273dSizick 		    (keytype == CKK_AES) ||
5055f66d273dSizick 		    (keytype == CKK_BLOWFISH))
50567c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
50577c478bd9Sstevel@tonic-gate 		break;
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate 	default:
50607c478bd9Sstevel@tonic-gate 		/*
50617c478bd9Sstevel@tonic-gate 		 * Set the value of a common key attribute.
50627c478bd9Sstevel@tonic-gate 		 */
50637c478bd9Sstevel@tonic-gate 		return (soft_set_common_key_attribute(object_p,
50647c478bd9Sstevel@tonic-gate 		    template, copy));
50657c478bd9Sstevel@tonic-gate 
50667c478bd9Sstevel@tonic-gate 	}
50677c478bd9Sstevel@tonic-gate 	/*
50687c478bd9Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
50697c478bd9Sstevel@tonic-gate 	 * and requested attribute is invalid.
50707c478bd9Sstevel@tonic-gate 	 */
50717c478bd9Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
50727c478bd9Sstevel@tonic-gate }
50737c478bd9Sstevel@tonic-gate 
50747c478bd9Sstevel@tonic-gate 
50757c478bd9Sstevel@tonic-gate /*
50767c478bd9Sstevel@tonic-gate  * Call the appropriate set attribute function according to the class
50777c478bd9Sstevel@tonic-gate  * of object.
50787c478bd9Sstevel@tonic-gate  *
50797c478bd9Sstevel@tonic-gate  * The caller of this function does not hold the lock on the original
50807c478bd9Sstevel@tonic-gate  * object, since this function is setting the attribute on the new object
50817c478bd9Sstevel@tonic-gate  * that is being modified.
50827c478bd9Sstevel@tonic-gate  *
50837c478bd9Sstevel@tonic-gate  * Argument copy: TRUE when called by C_CopyObject,
50847c478bd9Sstevel@tonic-gate  *		  FALSE when called by C_SetAttributeValue.
50857c478bd9Sstevel@tonic-gate  */
50867c478bd9Sstevel@tonic-gate CK_RV
50877c478bd9Sstevel@tonic-gate soft_set_attribute(soft_object_t *object_p, CK_ATTRIBUTE_PTR template,
50887c478bd9Sstevel@tonic-gate     boolean_t copy)
50897c478bd9Sstevel@tonic-gate {
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
50927c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS	class = object_p->class;
50937c478bd9Sstevel@tonic-gate 
50947c478bd9Sstevel@tonic-gate 	switch (class) {
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
50977c478bd9Sstevel@tonic-gate 		rv = soft_set_public_key_attribute(object_p, template, copy);
50987c478bd9Sstevel@tonic-gate 		break;
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
51017c478bd9Sstevel@tonic-gate 		rv = soft_set_private_key_attribute(object_p, template, copy);
51027c478bd9Sstevel@tonic-gate 		break;
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 	case CKO_SECRET_KEY:
51057c478bd9Sstevel@tonic-gate 		rv = soft_set_secret_key_attribute(object_p, template, copy);
51067c478bd9Sstevel@tonic-gate 		break;
51077c478bd9Sstevel@tonic-gate 
51087c478bd9Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
51097c478bd9Sstevel@tonic-gate 		switch (template->type) {
51107c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
51117c478bd9Sstevel@tonic-gate 			/*
51127c478bd9Sstevel@tonic-gate 			 * Only the LABEL can be modified in the common
51137c478bd9Sstevel@tonic-gate 			 * storage object attributes after the object is
51147c478bd9Sstevel@tonic-gate 			 * created.
51157c478bd9Sstevel@tonic-gate 			 */
51167c478bd9Sstevel@tonic-gate 			return (set_extra_attr_to_object(object_p,
51177c478bd9Sstevel@tonic-gate 			    CKA_LABEL, template));
51187c478bd9Sstevel@tonic-gate 		default:
51197c478bd9Sstevel@tonic-gate 			return (CKR_TEMPLATE_INCONSISTENT);
51207c478bd9Sstevel@tonic-gate 		}
51217c478bd9Sstevel@tonic-gate 	case CKO_CERTIFICATE:
51227c478bd9Sstevel@tonic-gate 		rv = soft_set_certificate_attribute(object_p, template, copy);
51237c478bd9Sstevel@tonic-gate 		break;
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate 	default:
51267c478bd9Sstevel@tonic-gate 		/*
51277c478bd9Sstevel@tonic-gate 		 * If the template specifies a value of an attribute
51287c478bd9Sstevel@tonic-gate 		 * which is incompatible with other existing attributes
51297c478bd9Sstevel@tonic-gate 		 * of the object, then fails with return code
51307c478bd9Sstevel@tonic-gate 		 * CKR_TEMPLATE_INCONSISTENT.
51317c478bd9Sstevel@tonic-gate 		 */
51327c478bd9Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
51337c478bd9Sstevel@tonic-gate 		break;
51347c478bd9Sstevel@tonic-gate 	}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	return (rv);
51377c478bd9Sstevel@tonic-gate }
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate CK_RV
5140c64d15a5Smcpowers soft_get_public_value(soft_object_t *key, CK_ATTRIBUTE_TYPE type,
51417c478bd9Sstevel@tonic-gate     uchar_t *value, uint32_t *value_len)
51427c478bd9Sstevel@tonic-gate {
51437c478bd9Sstevel@tonic-gate 	uint32_t len = 0;
51447c478bd9Sstevel@tonic-gate 	switch (type) {
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	/* The following attributes belong to RSA */
51477c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
51487c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
51497c478bd9Sstevel@tonic-gate 		len =
51507c478bd9Sstevel@tonic-gate 		    /* LINTED */
51517c478bd9Sstevel@tonic-gate 		    (uint32_t)
51527c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_MOD(key))->big_value_len;
51537c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
51547c478bd9Sstevel@tonic-gate 		len =
51557c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_MOD(key))->big_value_len;
51567c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
51597c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
51607c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
51617c478bd9Sstevel@tonic-gate 		}
51627c478bd9Sstevel@tonic-gate 		*value_len = len;
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
51657c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_MOD(key))->big_value,
51667c478bd9Sstevel@tonic-gate 		    *value_len);
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate 		break;
51697c478bd9Sstevel@tonic-gate 
51707c478bd9Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
51717c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
51727c478bd9Sstevel@tonic-gate 		len =
51737c478bd9Sstevel@tonic-gate 		    /* LINTED */
51747c478bd9Sstevel@tonic-gate 		    (uint32_t)
51757c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_PUBEXPO(key))->big_value_len;
51767c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
51777c478bd9Sstevel@tonic-gate 		len =
51787c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_PUBEXPO(key))->big_value_len;
51797c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
51807c478bd9Sstevel@tonic-gate 
51817c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
51827c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
51837c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
51847c478bd9Sstevel@tonic-gate 		}
51857c478bd9Sstevel@tonic-gate 		*value_len = len;
51867c478bd9Sstevel@tonic-gate 
51877c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
51887c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_RSA_PUBEXPO(key))->big_value,
51897c478bd9Sstevel@tonic-gate 		    *value_len);
51907c478bd9Sstevel@tonic-gate 
51917c478bd9Sstevel@tonic-gate 		break;
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate 	/* The following attributes belong to DSA and DH */
51947c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
51977c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
51987c478bd9Sstevel@tonic-gate 			len =
51997c478bd9Sstevel@tonic-gate 			    /* LINTED */
52007c478bd9Sstevel@tonic-gate 			    (uint32_t)
52017c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_PRIME(key))->
52027c478bd9Sstevel@tonic-gate 			    big_value_len;
52037c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
52047c478bd9Sstevel@tonic-gate 			len =
52057c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_PRIME(key))->
52067c478bd9Sstevel@tonic-gate 			    big_value_len;
52077c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
52087c478bd9Sstevel@tonic-gate 		else
52097c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
52107c478bd9Sstevel@tonic-gate 			len =
52117c478bd9Sstevel@tonic-gate 			    /* LINTED */
52127c478bd9Sstevel@tonic-gate 			    (uint32_t)
52137c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_PRIME(key))->
52147c478bd9Sstevel@tonic-gate 			    big_value_len;
52157c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
52167c478bd9Sstevel@tonic-gate 			len =
52177c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_PRIME(key))->
52187c478bd9Sstevel@tonic-gate 			    big_value_len;
52197c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
52207c478bd9Sstevel@tonic-gate 
52217c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
52227c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
52237c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
52247c478bd9Sstevel@tonic-gate 		}
52257c478bd9Sstevel@tonic-gate 		*value_len = len;
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
52287c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
52297c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_PRIME(key))->big_value,
52307c478bd9Sstevel@tonic-gate 			    *value_len);
52317c478bd9Sstevel@tonic-gate 		else
52327c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
52337c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_PRIME(key))->big_value,
52347c478bd9Sstevel@tonic-gate 			    *value_len);
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate 		break;
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
52397c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
52407c478bd9Sstevel@tonic-gate 		len =
52417c478bd9Sstevel@tonic-gate 		    /* LINTED */
52427c478bd9Sstevel@tonic-gate 		    (uint32_t)
52437c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_DSA_SUBPRIME(key))->big_value_len;
52447c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
52457c478bd9Sstevel@tonic-gate 		len =
52467c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_DSA_SUBPRIME(key))->big_value_len;
52477c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
52507c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
52517c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
52527c478bd9Sstevel@tonic-gate 		}
52537c478bd9Sstevel@tonic-gate 		*value_len = len;
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
52567c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PUB_DSA_SUBPRIME(key))->big_value,
52577c478bd9Sstevel@tonic-gate 		    *value_len);
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 		break;
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 	case CKA_BASE:
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
52647c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
52657c478bd9Sstevel@tonic-gate 			len =
52667c478bd9Sstevel@tonic-gate 			    /* LINTED */
52677c478bd9Sstevel@tonic-gate 			    (uint32_t)
52687c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_BASE(key))->
52697c478bd9Sstevel@tonic-gate 			    big_value_len;
52707c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
52717c478bd9Sstevel@tonic-gate 			len =
52727c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_BASE(key))->
52737c478bd9Sstevel@tonic-gate 			    big_value_len;
52747c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
52757c478bd9Sstevel@tonic-gate 		else
52767c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
52777c478bd9Sstevel@tonic-gate 			len =
52787c478bd9Sstevel@tonic-gate 			    /* LINTED */
52797c478bd9Sstevel@tonic-gate 			    (uint32_t)
52807c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_BASE(key))->
52817c478bd9Sstevel@tonic-gate 			    big_value_len;
52827c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
52837c478bd9Sstevel@tonic-gate 			len =
52847c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_BASE(key))->
52857c478bd9Sstevel@tonic-gate 			    big_value_len;
52867c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
52877c478bd9Sstevel@tonic-gate 
52887c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
52897c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
52907c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
52917c478bd9Sstevel@tonic-gate 		}
52927c478bd9Sstevel@tonic-gate 		*value_len = len;
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
52957c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
52967c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_BASE(key))->big_value,
52977c478bd9Sstevel@tonic-gate 			    *value_len);
52987c478bd9Sstevel@tonic-gate 		else
52997c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
53007c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_BASE(key))->big_value,
53017c478bd9Sstevel@tonic-gate 			    *value_len);
53027c478bd9Sstevel@tonic-gate 		break;
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
53057c478bd9Sstevel@tonic-gate 
53067c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
53077c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
53087c478bd9Sstevel@tonic-gate 			len =
53097c478bd9Sstevel@tonic-gate 			    /* LINTED */
53107c478bd9Sstevel@tonic-gate 			    (uint32_t)
53117c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_VALUE(key))->
53127c478bd9Sstevel@tonic-gate 			    big_value_len;
53137c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
53147c478bd9Sstevel@tonic-gate 			len =
53157c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_VALUE(key))->
53167c478bd9Sstevel@tonic-gate 			    big_value_len;
53177c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
53187c478bd9Sstevel@tonic-gate 		else
53197c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
53207c478bd9Sstevel@tonic-gate 			len =
53217c478bd9Sstevel@tonic-gate 			    /* LINTED */
53227c478bd9Sstevel@tonic-gate 			    (uint32_t)
53237c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_VALUE(key))->
53247c478bd9Sstevel@tonic-gate 			    big_value_len;
53257c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
53267c478bd9Sstevel@tonic-gate 			len =
53277c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_VALUE(key))->
53287c478bd9Sstevel@tonic-gate 			    big_value_len;
53297c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
53327c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
53337c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
53347c478bd9Sstevel@tonic-gate 		}
53357c478bd9Sstevel@tonic-gate 		*value_len = len;
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
53387c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
53397c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DSA_VALUE(key))->big_value,
53407c478bd9Sstevel@tonic-gate 			    *value_len);
53417c478bd9Sstevel@tonic-gate 		else
53427c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
53437c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PUB_DH_VALUE(key))->big_value,
53447c478bd9Sstevel@tonic-gate 			    *value_len);
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate 		break;
53477c478bd9Sstevel@tonic-gate 	}
53487c478bd9Sstevel@tonic-gate 
53497c478bd9Sstevel@tonic-gate 	return (CKR_OK);
53507c478bd9Sstevel@tonic-gate }
53517c478bd9Sstevel@tonic-gate 
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate CK_RV
5354c64d15a5Smcpowers soft_get_private_value(soft_object_t *key, CK_ATTRIBUTE_TYPE type,
53557c478bd9Sstevel@tonic-gate     uchar_t *value, uint32_t *value_len)
53567c478bd9Sstevel@tonic-gate {
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 	uint32_t len = 0;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 	switch (type) {
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	/* The following attributes belong to RSA */
53637c478bd9Sstevel@tonic-gate 	case CKA_MODULUS:
53647c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
53657c478bd9Sstevel@tonic-gate 		len =
53667c478bd9Sstevel@tonic-gate 		    /* LINTED */
53677c478bd9Sstevel@tonic-gate 		    (uint32_t)
53687c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_MOD(key))->big_value_len;
53697c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
53707c478bd9Sstevel@tonic-gate 		len =
53717c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_MOD(key))->big_value_len;
53727c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
53757c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
53767c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
53777c478bd9Sstevel@tonic-gate 		}
53787c478bd9Sstevel@tonic-gate 		*value_len = len;
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
53817c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_MOD(key))->big_value,
53827c478bd9Sstevel@tonic-gate 		    *value_len);
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate 		break;
53857c478bd9Sstevel@tonic-gate 
53867c478bd9Sstevel@tonic-gate 	case CKA_PRIVATE_EXPONENT:
53877c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
53887c478bd9Sstevel@tonic-gate 		len =
53897c478bd9Sstevel@tonic-gate 		    /* LINTED */
53907c478bd9Sstevel@tonic-gate 		    (uint32_t)
53917c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIEXPO(key))->big_value_len;
53927c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
53937c478bd9Sstevel@tonic-gate 		len =
53947c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIEXPO(key))->big_value_len;
53957c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
53967c478bd9Sstevel@tonic-gate 
53977c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
53987c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
53997c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
54007c478bd9Sstevel@tonic-gate 		}
54017c478bd9Sstevel@tonic-gate 		*value_len = len;
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
54047c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIEXPO(key))->big_value,
54057c478bd9Sstevel@tonic-gate 		    *value_len);
54067c478bd9Sstevel@tonic-gate 
54077c478bd9Sstevel@tonic-gate 		break;
54087c478bd9Sstevel@tonic-gate 
54097c478bd9Sstevel@tonic-gate 	case CKA_PRIME_1:
54107c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
54117c478bd9Sstevel@tonic-gate 		len =
54127c478bd9Sstevel@tonic-gate 		    /* LINTED */
54137c478bd9Sstevel@tonic-gate 		    (uint32_t)
54147c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME1(key))->big_value_len;
54157c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
54167c478bd9Sstevel@tonic-gate 		len =
54177c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME1(key))->big_value_len;
54187c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 		if (len > *value_len) {
54217c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
54227c478bd9Sstevel@tonic-gate 		}
54237c478bd9Sstevel@tonic-gate 		*value_len = len;
54247c478bd9Sstevel@tonic-gate 
54257c478bd9Sstevel@tonic-gate 		if (*value_len == 0) {
54267c478bd9Sstevel@tonic-gate 			return (CKR_OK);
54277c478bd9Sstevel@tonic-gate 		}
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
54307c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME1(key))->big_value,
54317c478bd9Sstevel@tonic-gate 		    *value_len);
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate 		break;
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate 	case CKA_PRIME_2:
54367c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
54377c478bd9Sstevel@tonic-gate 		len =
54387c478bd9Sstevel@tonic-gate 		    /* LINTED */
54397c478bd9Sstevel@tonic-gate 		    (uint32_t)
54407c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME2(key))->big_value_len;
54417c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
54427c478bd9Sstevel@tonic-gate 		len =
54437c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME2(key))->big_value_len;
54447c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
54457c478bd9Sstevel@tonic-gate 
54467c478bd9Sstevel@tonic-gate 		if (len > *value_len) {
54477c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
54487c478bd9Sstevel@tonic-gate 		}
54497c478bd9Sstevel@tonic-gate 		*value_len = len;
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 		if (*value_len == 0) {
54527c478bd9Sstevel@tonic-gate 			return (CKR_OK);
54537c478bd9Sstevel@tonic-gate 		}
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
54567c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_PRIME2(key))->big_value,
54577c478bd9Sstevel@tonic-gate 		    *value_len);
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 		break;
54607c478bd9Sstevel@tonic-gate 
54617c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_1:
54627c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
54637c478bd9Sstevel@tonic-gate 		len =
54647c478bd9Sstevel@tonic-gate 		    /* LINTED */
54657c478bd9Sstevel@tonic-gate 		    (uint32_t)
54667c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO1(key))->big_value_len;
54677c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
54687c478bd9Sstevel@tonic-gate 		len =
54697c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO1(key))->big_value_len;
54707c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 		if (len > *value_len) {
54737c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
54747c478bd9Sstevel@tonic-gate 		}
54757c478bd9Sstevel@tonic-gate 		*value_len = len;
54767c478bd9Sstevel@tonic-gate 
54777c478bd9Sstevel@tonic-gate 		if (*value_len == 0) {
54787c478bd9Sstevel@tonic-gate 			return (CKR_OK);
54797c478bd9Sstevel@tonic-gate 		}
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
54827c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO1(key))->big_value,
54837c478bd9Sstevel@tonic-gate 		    *value_len);
54847c478bd9Sstevel@tonic-gate 
54857c478bd9Sstevel@tonic-gate 		break;
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	case CKA_EXPONENT_2:
54887c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
54897c478bd9Sstevel@tonic-gate 		len =
54907c478bd9Sstevel@tonic-gate 		    /* LINTED */
54917c478bd9Sstevel@tonic-gate 		    (uint32_t)
54927c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO2(key))->big_value_len;
54937c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
54947c478bd9Sstevel@tonic-gate 		len =
54957c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO2(key))->big_value_len;
54967c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
54977c478bd9Sstevel@tonic-gate 
54987c478bd9Sstevel@tonic-gate 		if (len > *value_len) {
54997c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
55007c478bd9Sstevel@tonic-gate 		}
55017c478bd9Sstevel@tonic-gate 		*value_len = len;
55027c478bd9Sstevel@tonic-gate 
55037c478bd9Sstevel@tonic-gate 		if (*value_len == 0) {
55047c478bd9Sstevel@tonic-gate 			return (CKR_OK);
55057c478bd9Sstevel@tonic-gate 		}
55067c478bd9Sstevel@tonic-gate 
55077c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
55087c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_EXPO2(key))->big_value,
55097c478bd9Sstevel@tonic-gate 		    *value_len);
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate 		break;
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate 	case CKA_COEFFICIENT:
55147c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
55157c478bd9Sstevel@tonic-gate 		len =
55167c478bd9Sstevel@tonic-gate 		    /* LINTED */
55177c478bd9Sstevel@tonic-gate 		    (uint32_t)
55187c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_COEF(key))->big_value_len;
55197c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
55207c478bd9Sstevel@tonic-gate 		len =
55217c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_COEF(key))->big_value_len;
55227c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
55237c478bd9Sstevel@tonic-gate 
55247c478bd9Sstevel@tonic-gate 		if (len > *value_len) {
55257c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
55267c478bd9Sstevel@tonic-gate 		}
55277c478bd9Sstevel@tonic-gate 		*value_len = len;
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate 		if (*value_len == 0) {
55307c478bd9Sstevel@tonic-gate 			return (CKR_OK);
55317c478bd9Sstevel@tonic-gate 		}
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
55347c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_RSA_COEF(key))->big_value,
55357c478bd9Sstevel@tonic-gate 		    *value_len);
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 		break;
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 	/* The following attributes belong to DSA and DH */
55407c478bd9Sstevel@tonic-gate 	case CKA_PRIME:
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
55437c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
55447c478bd9Sstevel@tonic-gate 			len =
55457c478bd9Sstevel@tonic-gate 			    /* LINTED */
55467c478bd9Sstevel@tonic-gate 			    (uint32_t)
55477c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_PRIME(key))->
55487c478bd9Sstevel@tonic-gate 			    big_value_len;
55497c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
55507c478bd9Sstevel@tonic-gate 			len =
55517c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_PRIME(key))->
55527c478bd9Sstevel@tonic-gate 			    big_value_len;
55537c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
55547c478bd9Sstevel@tonic-gate 		else
55557c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
55567c478bd9Sstevel@tonic-gate 			len =
55577c478bd9Sstevel@tonic-gate 			    /* LINTED */
55587c478bd9Sstevel@tonic-gate 			    (uint32_t)
55597c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_PRIME(key))->
55607c478bd9Sstevel@tonic-gate 			    big_value_len;
55617c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
55627c478bd9Sstevel@tonic-gate 			len =
55637c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_PRIME(key))->
55647c478bd9Sstevel@tonic-gate 			    big_value_len;
55657c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
55687c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
55697c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
55707c478bd9Sstevel@tonic-gate 		}
55717c478bd9Sstevel@tonic-gate 		*value_len = len;
55727c478bd9Sstevel@tonic-gate 
55737c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
55747c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
55757c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_PRIME(key))->big_value,
55767c478bd9Sstevel@tonic-gate 			    *value_len);
55777c478bd9Sstevel@tonic-gate 		else
55787c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
55797c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_PRIME(key))->big_value,
55807c478bd9Sstevel@tonic-gate 			    *value_len);
55817c478bd9Sstevel@tonic-gate 
55827c478bd9Sstevel@tonic-gate 		break;
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 	case CKA_SUBPRIME:
55857c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
55867c478bd9Sstevel@tonic-gate 		len =
55877c478bd9Sstevel@tonic-gate 		    /* LINTED */
55887c478bd9Sstevel@tonic-gate 		    (uint32_t)
55897c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_DSA_SUBPRIME(key))->big_value_len;
55907c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
55917c478bd9Sstevel@tonic-gate 		len =
55927c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_DSA_SUBPRIME(key))->big_value_len;
55937c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
55947c478bd9Sstevel@tonic-gate 
55957c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
55967c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
55977c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
55987c478bd9Sstevel@tonic-gate 		}
55997c478bd9Sstevel@tonic-gate 		*value_len = len;
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 		(void) memcpy(value,
56027c478bd9Sstevel@tonic-gate 		    ((biginteger_t *)OBJ_PRI_DSA_SUBPRIME(key))->big_value,
56037c478bd9Sstevel@tonic-gate 		    *value_len);
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 		break;
56067c478bd9Sstevel@tonic-gate 
56077c478bd9Sstevel@tonic-gate 	case CKA_BASE:
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
56107c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
56117c478bd9Sstevel@tonic-gate 			len =
56127c478bd9Sstevel@tonic-gate 			    /* LINTED */
56137c478bd9Sstevel@tonic-gate 			    (uint32_t)
56147c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_BASE(key))->
56157c478bd9Sstevel@tonic-gate 			    big_value_len;
56167c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
56177c478bd9Sstevel@tonic-gate 			len =
56187c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_BASE(key))->
56197c478bd9Sstevel@tonic-gate 			    big_value_len;
56207c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
56217c478bd9Sstevel@tonic-gate 		else
56227c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
56237c478bd9Sstevel@tonic-gate 			len =
56247c478bd9Sstevel@tonic-gate 			    /* LINTED */
56257c478bd9Sstevel@tonic-gate 			    (uint32_t)
56267c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_BASE(key))->
56277c478bd9Sstevel@tonic-gate 			    big_value_len;
56287c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
56297c478bd9Sstevel@tonic-gate 			len =
56307c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_BASE(key))->
56317c478bd9Sstevel@tonic-gate 			    big_value_len;
56327c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
56337c478bd9Sstevel@tonic-gate 
56347c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
56357c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
56367c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
56377c478bd9Sstevel@tonic-gate 		}
56387c478bd9Sstevel@tonic-gate 		*value_len = len;
56397c478bd9Sstevel@tonic-gate 
56407c478bd9Sstevel@tonic-gate 		if (key->key_type == CKK_DSA)
56417c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
56427c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_BASE(key))->big_value,
56437c478bd9Sstevel@tonic-gate 			    *value_len);
56447c478bd9Sstevel@tonic-gate 		else
56457c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
56467c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_BASE(key))->big_value,
56477c478bd9Sstevel@tonic-gate 			    *value_len);
56487c478bd9Sstevel@tonic-gate 		break;
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 	case CKA_VALUE:
56517c478bd9Sstevel@tonic-gate 
5652c64d15a5Smcpowers 		if (key->key_type == CKK_DSA) {
56537c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
56547c478bd9Sstevel@tonic-gate 			len =
56557c478bd9Sstevel@tonic-gate 			    /* LINTED */
56567c478bd9Sstevel@tonic-gate 			    (uint32_t)
56577c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_VALUE(key))->
56587c478bd9Sstevel@tonic-gate 			    big_value_len;
56597c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
56607c478bd9Sstevel@tonic-gate 			len =
56617c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_VALUE(key))->
56627c478bd9Sstevel@tonic-gate 			    big_value_len;
56637c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
5664c64d15a5Smcpowers 		} else if (key->key_type == CKK_DH) {
56657c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
56667c478bd9Sstevel@tonic-gate 			len =
56677c478bd9Sstevel@tonic-gate 			    /* LINTED */
56687c478bd9Sstevel@tonic-gate 			    (uint32_t)
56697c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_VALUE(key))->
56707c478bd9Sstevel@tonic-gate 			    big_value_len;
56717c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
56727c478bd9Sstevel@tonic-gate 			len =
56737c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_VALUE(key))->
56747c478bd9Sstevel@tonic-gate 			    big_value_len;
56757c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
5676c64d15a5Smcpowers 		} else {
5677c64d15a5Smcpowers #ifdef	__sparcv9
5678c64d15a5Smcpowers 			len =
5679c64d15a5Smcpowers 			    /* LINTED */
5680c64d15a5Smcpowers 			    (uint32_t)
5681c64d15a5Smcpowers 			    ((biginteger_t *)OBJ_PRI_EC_VALUE(key))->
5682c64d15a5Smcpowers 			    big_value_len;
5683c64d15a5Smcpowers #else	/* !__sparcv9 */
5684c64d15a5Smcpowers 			len =
5685c64d15a5Smcpowers 			    ((biginteger_t *)OBJ_PRI_EC_VALUE(key))->
5686c64d15a5Smcpowers 			    big_value_len;
5687c64d15a5Smcpowers #endif	/* __sparcv9 */
5688c64d15a5Smcpowers 		}
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 		/* This attribute MUST BE set */
56917c478bd9Sstevel@tonic-gate 		if (len == 0 || len > *value_len) {
56927c478bd9Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_VALUE_INVALID);
56937c478bd9Sstevel@tonic-gate 		}
56947c478bd9Sstevel@tonic-gate 		*value_len = len;
56957c478bd9Sstevel@tonic-gate 
5696c64d15a5Smcpowers 		if (key->key_type == CKK_DSA) {
56977c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
56987c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DSA_VALUE(key))->big_value,
56997c478bd9Sstevel@tonic-gate 			    *value_len);
5700c64d15a5Smcpowers 		} else if (key->key_type == CKK_DH) {
57017c478bd9Sstevel@tonic-gate 			(void) memcpy(value,
57027c478bd9Sstevel@tonic-gate 			    ((biginteger_t *)OBJ_PRI_DH_VALUE(key))->big_value,
57037c478bd9Sstevel@tonic-gate 			    *value_len);
5704c64d15a5Smcpowers 		} else {
5705c64d15a5Smcpowers 			(void) memcpy(value,
5706c64d15a5Smcpowers 			    ((biginteger_t *)OBJ_PRI_EC_VALUE(key))->big_value,
5707c64d15a5Smcpowers 			    *value_len);
5708c64d15a5Smcpowers 		}
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate 		break;
57117c478bd9Sstevel@tonic-gate 	}
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 	return (CKR_OK);
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate }
57167c478bd9Sstevel@tonic-gate 
57177c478bd9Sstevel@tonic-gate static CK_RV
57187c478bd9Sstevel@tonic-gate copy_bigint(biginteger_t *new_bigint, biginteger_t *old_bigint)
57197c478bd9Sstevel@tonic-gate {
57207c478bd9Sstevel@tonic-gate 	new_bigint->big_value =
57217c478bd9Sstevel@tonic-gate 	    malloc((sizeof (CK_BYTE) * new_bigint->big_value_len));
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 	if (new_bigint->big_value == NULL) {
57247c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
57257c478bd9Sstevel@tonic-gate 	}
57267c478bd9Sstevel@tonic-gate 
57277c478bd9Sstevel@tonic-gate 	(void) memcpy(new_bigint->big_value, old_bigint->big_value,
57287c478bd9Sstevel@tonic-gate 	    (sizeof (CK_BYTE) * new_bigint->big_value_len));
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	return (CKR_OK);
57317c478bd9Sstevel@tonic-gate }
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate static void
57347c478bd9Sstevel@tonic-gate free_public_key_attr(public_key_obj_t *pbk, CK_KEY_TYPE key_type)
57357c478bd9Sstevel@tonic-gate {
57367c478bd9Sstevel@tonic-gate 	if (pbk == NULL) {
57377c478bd9Sstevel@tonic-gate 		return;
57387c478bd9Sstevel@tonic-gate 	}
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate 	switch (key_type) {
57417c478bd9Sstevel@tonic-gate 		case CKK_RSA:
57427c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_RSA_MOD(pbk));
57437c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_RSA_PUBEXPO(pbk));
57447c478bd9Sstevel@tonic-gate 			break;
57457c478bd9Sstevel@tonic-gate 		case CKK_DSA:
57467c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_PRIME(pbk));
57477c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_SUBPRIME(pbk));
57487c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_BASE(pbk));
57497c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_VALUE(pbk));
57507c478bd9Sstevel@tonic-gate 			break;
57517c478bd9Sstevel@tonic-gate 		case CKK_DH:
57527c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH_PRIME(pbk));
57537c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH_BASE(pbk));
57547c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH_VALUE(pbk));
57557c478bd9Sstevel@tonic-gate 			break;
5756f9fbec18Smcpowers 		case CKK_EC:
5757f9fbec18Smcpowers 			bigint_attr_cleanup(KEY_PUB_EC_POINT(pbk));
5758f9fbec18Smcpowers 			break;
57597c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
57607c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH942_PRIME(pbk));
57617c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH942_SUBPRIME(pbk));
57627c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH942_BASE(pbk));
57637c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DH942_VALUE(pbk));
57647c478bd9Sstevel@tonic-gate 			break;
57657c478bd9Sstevel@tonic-gate 		default:
57667c478bd9Sstevel@tonic-gate 			break;
57677c478bd9Sstevel@tonic-gate 	}
57687c478bd9Sstevel@tonic-gate 	free(pbk);
57697c478bd9Sstevel@tonic-gate }
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate CK_RV
57727c478bd9Sstevel@tonic-gate soft_copy_public_key_attr(public_key_obj_t *old_pub_key_obj_p,
57737c478bd9Sstevel@tonic-gate     public_key_obj_t **new_pub_key_obj_p, CK_KEY_TYPE key_type)
57747c478bd9Sstevel@tonic-gate {
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate 	public_key_obj_t *pbk;
57777c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	pbk = calloc(1, sizeof (public_key_obj_t));
57807c478bd9Sstevel@tonic-gate 	if (pbk == NULL) {
57817c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
57827c478bd9Sstevel@tonic-gate 	}
57837c478bd9Sstevel@tonic-gate 
57847c478bd9Sstevel@tonic-gate 	switch (key_type) {
57857c478bd9Sstevel@tonic-gate 		case CKK_RSA:
57867c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_RSA(pbk),
5787f9fbec18Smcpowers 			    KEY_PUB_RSA(old_pub_key_obj_p),
57887c478bd9Sstevel@tonic-gate 			    sizeof (rsa_pub_key_t));
57897c478bd9Sstevel@tonic-gate 			/* copy modulus */
57907c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_RSA_MOD(pbk),
57917c478bd9Sstevel@tonic-gate 			    KEY_PUB_RSA_MOD(old_pub_key_obj_p));
57927c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
57937c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
57947c478bd9Sstevel@tonic-gate 				return (rv);
57957c478bd9Sstevel@tonic-gate 			}
57967c478bd9Sstevel@tonic-gate 			/* copy public exponent */
57977c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_RSA_PUBEXPO(pbk),
57987c478bd9Sstevel@tonic-gate 			    KEY_PUB_RSA_PUBEXPO(old_pub_key_obj_p));
57997c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58007c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58017c478bd9Sstevel@tonic-gate 				return (rv);
58027c478bd9Sstevel@tonic-gate 			}
58037c478bd9Sstevel@tonic-gate 			break;
58047c478bd9Sstevel@tonic-gate 		case CKK_DSA:
58057c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_DSA(pbk),
58067c478bd9Sstevel@tonic-gate 			    KEY_PUB_DSA(old_pub_key_obj_p),
58077c478bd9Sstevel@tonic-gate 			    sizeof (dsa_pub_key_t));
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate 			/* copy prime */
58107c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_PRIME(pbk),
58117c478bd9Sstevel@tonic-gate 			    KEY_PUB_DSA_PRIME(old_pub_key_obj_p));
58127c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58137c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58147c478bd9Sstevel@tonic-gate 				return (rv);
58157c478bd9Sstevel@tonic-gate 			}
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 			/* copy subprime */
58187c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_SUBPRIME(pbk),
58197c478bd9Sstevel@tonic-gate 			    KEY_PUB_DSA_SUBPRIME(old_pub_key_obj_p));
58207c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58217c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58227c478bd9Sstevel@tonic-gate 				return (rv);
58237c478bd9Sstevel@tonic-gate 			}
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate 			/* copy base */
58267c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_BASE(pbk),
58277c478bd9Sstevel@tonic-gate 			    KEY_PUB_DSA_BASE(old_pub_key_obj_p));
58287c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58297c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58307c478bd9Sstevel@tonic-gate 				return (rv);
58317c478bd9Sstevel@tonic-gate 			}
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 			/* copy value */
58347c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_VALUE(pbk),
58357c478bd9Sstevel@tonic-gate 			    KEY_PUB_DSA_VALUE(old_pub_key_obj_p));
58367c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58377c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58387c478bd9Sstevel@tonic-gate 				return (rv);
58397c478bd9Sstevel@tonic-gate 			}
58407c478bd9Sstevel@tonic-gate 			break;
58417c478bd9Sstevel@tonic-gate 		case CKK_DH:
58427c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_DH(pbk),
58437c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH(old_pub_key_obj_p),
58447c478bd9Sstevel@tonic-gate 			    sizeof (dh_pub_key_t));
58457c478bd9Sstevel@tonic-gate 
58467c478bd9Sstevel@tonic-gate 			/* copy prime */
58477c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH_PRIME(pbk),
58487c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH_PRIME(old_pub_key_obj_p));
58497c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58507c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58517c478bd9Sstevel@tonic-gate 				return (rv);
58527c478bd9Sstevel@tonic-gate 			}
58537c478bd9Sstevel@tonic-gate 
58547c478bd9Sstevel@tonic-gate 			/* copy base */
58557c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH_BASE(pbk),
58567c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH_BASE(old_pub_key_obj_p));
58577c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58587c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58597c478bd9Sstevel@tonic-gate 				return (rv);
58607c478bd9Sstevel@tonic-gate 			}
58617c478bd9Sstevel@tonic-gate 
58627c478bd9Sstevel@tonic-gate 			/* copy value */
58637c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH_VALUE(pbk),
58647c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH_VALUE(old_pub_key_obj_p));
58657c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58667c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58677c478bd9Sstevel@tonic-gate 				return (rv);
58687c478bd9Sstevel@tonic-gate 			}
58697c478bd9Sstevel@tonic-gate 			break;
5870f9fbec18Smcpowers 		case CKK_EC:
5871f9fbec18Smcpowers 			(void) memcpy(KEY_PUB_EC(pbk),
5872f9fbec18Smcpowers 			    KEY_PUB_EC(old_pub_key_obj_p),
5873f9fbec18Smcpowers 			    sizeof (ec_pub_key_t));
5874f9fbec18Smcpowers 
5875f9fbec18Smcpowers 			/* copy point */
5876f9fbec18Smcpowers 			rv = copy_bigint(KEY_PUB_EC_POINT(pbk),
5877f9fbec18Smcpowers 			    KEY_PUB_EC_POINT(old_pub_key_obj_p));
5878f9fbec18Smcpowers 			if (rv != CKR_OK) {
5879f9fbec18Smcpowers 				free_public_key_attr(pbk, key_type);
5880f9fbec18Smcpowers 				return (rv);
5881f9fbec18Smcpowers 			}
5882f9fbec18Smcpowers 			break;
58837c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
58847c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_DH942(pbk),
58857c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH942(old_pub_key_obj_p),
58867c478bd9Sstevel@tonic-gate 			    sizeof (dh942_pub_key_t));
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 			/* copy prime */
58897c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH942_PRIME(pbk),
58907c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH942_PRIME(old_pub_key_obj_p));
58917c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
58927c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
58937c478bd9Sstevel@tonic-gate 				return (rv);
58947c478bd9Sstevel@tonic-gate 			}
58957c478bd9Sstevel@tonic-gate 
58967c478bd9Sstevel@tonic-gate 			/* copy subprime */
58977c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH942_SUBPRIME(pbk),
58987c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH942_SUBPRIME(old_pub_key_obj_p));
58997c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
59007c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
59017c478bd9Sstevel@tonic-gate 				return (rv);
59027c478bd9Sstevel@tonic-gate 			}
59037c478bd9Sstevel@tonic-gate 
59047c478bd9Sstevel@tonic-gate 			/* copy base */
59057c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH942_BASE(pbk),
59067c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH942_BASE(old_pub_key_obj_p));
59077c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
59087c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
59097c478bd9Sstevel@tonic-gate 				return (rv);
59107c478bd9Sstevel@tonic-gate 			}
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 			/* copy value */
59137c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DH942_VALUE(pbk),
59147c478bd9Sstevel@tonic-gate 			    KEY_PUB_DH942_VALUE(old_pub_key_obj_p));
59157c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
59167c478bd9Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
59177c478bd9Sstevel@tonic-gate 				return (rv);
59187c478bd9Sstevel@tonic-gate 			}
59197c478bd9Sstevel@tonic-gate 			break;
59207c478bd9Sstevel@tonic-gate 		default:
59217c478bd9Sstevel@tonic-gate 			break;
59227c478bd9Sstevel@tonic-gate 	}
59237c478bd9Sstevel@tonic-gate 	*new_pub_key_obj_p = pbk;
59247c478bd9Sstevel@tonic-gate 	return (rv);
59257c478bd9Sstevel@tonic-gate }
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate static void
59287c478bd9Sstevel@tonic-gate free_private_key_attr(private_key_obj_t *pbk, CK_KEY_TYPE key_type)
59297c478bd9Sstevel@tonic-gate {
59307c478bd9Sstevel@tonic-gate 	if (pbk == NULL) {
59317c478bd9Sstevel@tonic-gate 		return;
59327c478bd9Sstevel@tonic-gate 	}
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 	switch (key_type) {
59357c478bd9Sstevel@tonic-gate 		case CKK_RSA:
59367c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_MOD(pbk));
59377c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(pbk));
59387c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(pbk));
59397c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(pbk));
59407c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(pbk));
59417c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(pbk));
59427c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(pbk));
59437c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_COEF(pbk));
59447c478bd9Sstevel@tonic-gate 			break;
59457c478bd9Sstevel@tonic-gate 		case CKK_DSA:
59467c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_PRIME(pbk));
59477c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(pbk));
59487c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_BASE(pbk));
59497c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_VALUE(pbk));
59507c478bd9Sstevel@tonic-gate 			break;
59517c478bd9Sstevel@tonic-gate 		case CKK_DH:
59527c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH_PRIME(pbk));
59537c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH_BASE(pbk));
59547c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH_VALUE(pbk));
59557c478bd9Sstevel@tonic-gate 			break;
5956f9fbec18Smcpowers 		case CKK_EC:
5957f9fbec18Smcpowers 			bigint_attr_cleanup(KEY_PRI_EC_VALUE(pbk));
5958f9fbec18Smcpowers 			break;
59597c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
59607c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH942_PRIME(pbk));
59617c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH942_SUBPRIME(pbk));
59627c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH942_BASE(pbk));
59637c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DH942_VALUE(pbk));
59647c478bd9Sstevel@tonic-gate 			break;
59657c478bd9Sstevel@tonic-gate 		default:
59667c478bd9Sstevel@tonic-gate 			break;
59677c478bd9Sstevel@tonic-gate 	}
59687c478bd9Sstevel@tonic-gate 	free(pbk);
59697c478bd9Sstevel@tonic-gate }
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate CK_RV
59727c478bd9Sstevel@tonic-gate soft_copy_private_key_attr(private_key_obj_t *old_pri_key_obj_p,
59737c478bd9Sstevel@tonic-gate     private_key_obj_t **new_pri_key_obj_p, CK_KEY_TYPE key_type)
59747c478bd9Sstevel@tonic-gate {
59757c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
59767c478bd9Sstevel@tonic-gate 	private_key_obj_t *pbk;
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 	pbk = calloc(1, sizeof (private_key_obj_t));
59797c478bd9Sstevel@tonic-gate 	if (pbk == NULL) {
59807c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
59817c478bd9Sstevel@tonic-gate 	}
59827c478bd9Sstevel@tonic-gate 
59837c478bd9Sstevel@tonic-gate 	switch (key_type) {
59847c478bd9Sstevel@tonic-gate 		case CKK_RSA:
59857c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_RSA(pbk),
59867c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA(old_pri_key_obj_p),
59877c478bd9Sstevel@tonic-gate 			    sizeof (rsa_pri_key_t));
59887c478bd9Sstevel@tonic-gate 			/* copy modulus */
59897c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_MOD(pbk),
59907c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_MOD(old_pri_key_obj_p));
59917c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
59927c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
59937c478bd9Sstevel@tonic-gate 				return (rv);
59947c478bd9Sstevel@tonic-gate 			}
59957c478bd9Sstevel@tonic-gate 			/* copy public exponent */
59967c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PUBEXPO(pbk),
59977c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_PUBEXPO(old_pri_key_obj_p));
59987c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
59997c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60007c478bd9Sstevel@tonic-gate 				return (rv);
60017c478bd9Sstevel@tonic-gate 			}
60027c478bd9Sstevel@tonic-gate 			/* copy private exponent */
60037c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIEXPO(pbk),
60047c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIEXPO(old_pri_key_obj_p));
60057c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60067c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60077c478bd9Sstevel@tonic-gate 				return (rv);
60087c478bd9Sstevel@tonic-gate 			}
60097c478bd9Sstevel@tonic-gate 			/* copy prime_1 */
60107c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIME1(pbk),
60117c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIME1(old_pri_key_obj_p));
60127c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60137c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60147c478bd9Sstevel@tonic-gate 				return (rv);
60157c478bd9Sstevel@tonic-gate 			}
60167c478bd9Sstevel@tonic-gate 			/* copy prime_2 */
60177c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIME2(pbk),
60187c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIME2(old_pri_key_obj_p));
60197c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60207c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60217c478bd9Sstevel@tonic-gate 				return (rv);
60227c478bd9Sstevel@tonic-gate 			}
60237c478bd9Sstevel@tonic-gate 			/* copy exponent_1 */
60247c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_EXPO1(pbk),
60257c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_EXPO1(old_pri_key_obj_p));
60267c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60277c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60287c478bd9Sstevel@tonic-gate 				return (rv);
60297c478bd9Sstevel@tonic-gate 			}
60307c478bd9Sstevel@tonic-gate 			/* copy exponent_2 */
60317c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_EXPO2(pbk),
60327c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_EXPO2(old_pri_key_obj_p));
60337c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60347c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60357c478bd9Sstevel@tonic-gate 				return (rv);
60367c478bd9Sstevel@tonic-gate 			}
60377c478bd9Sstevel@tonic-gate 			/* copy coefficient */
60387c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_COEF(pbk),
60397c478bd9Sstevel@tonic-gate 			    KEY_PRI_RSA_COEF(old_pri_key_obj_p));
60407c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60417c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60427c478bd9Sstevel@tonic-gate 				return (rv);
60437c478bd9Sstevel@tonic-gate 			}
60447c478bd9Sstevel@tonic-gate 			break;
60457c478bd9Sstevel@tonic-gate 		case CKK_DSA:
60467c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_DSA(pbk),
60477c478bd9Sstevel@tonic-gate 			    KEY_PRI_DSA(old_pri_key_obj_p),
60487c478bd9Sstevel@tonic-gate 			    sizeof (dsa_pri_key_t));
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate 			/* copy prime */
60517c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_PRIME(pbk),
60527c478bd9Sstevel@tonic-gate 			    KEY_PRI_DSA_PRIME(old_pri_key_obj_p));
60537c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60547c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60557c478bd9Sstevel@tonic-gate 				return (rv);
60567c478bd9Sstevel@tonic-gate 			}
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate 			/* copy subprime */
60597c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_SUBPRIME(pbk),
60607c478bd9Sstevel@tonic-gate 			    KEY_PRI_DSA_SUBPRIME(old_pri_key_obj_p));
60617c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60627c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60637c478bd9Sstevel@tonic-gate 				return (rv);
60647c478bd9Sstevel@tonic-gate 			}
60657c478bd9Sstevel@tonic-gate 
60667c478bd9Sstevel@tonic-gate 			/* copy base */
60677c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_BASE(pbk),
60687c478bd9Sstevel@tonic-gate 			    KEY_PRI_DSA_BASE(old_pri_key_obj_p));
60697c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60707c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60717c478bd9Sstevel@tonic-gate 				return (rv);
60727c478bd9Sstevel@tonic-gate 			}
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 			/* copy value */
60757c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_VALUE(pbk),
60767c478bd9Sstevel@tonic-gate 			    KEY_PRI_DSA_VALUE(old_pri_key_obj_p));
60777c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60787c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60797c478bd9Sstevel@tonic-gate 				return (rv);
60807c478bd9Sstevel@tonic-gate 			}
60817c478bd9Sstevel@tonic-gate 			break;
60827c478bd9Sstevel@tonic-gate 		case CKK_DH:
60837c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_DH(pbk),
60847c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH(old_pri_key_obj_p),
60857c478bd9Sstevel@tonic-gate 			    sizeof (dh_pri_key_t));
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 			/* copy prime */
60887c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH_PRIME(pbk),
60897c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH_PRIME(old_pri_key_obj_p));
60907c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60917c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
60927c478bd9Sstevel@tonic-gate 				return (rv);
60937c478bd9Sstevel@tonic-gate 			}
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate 			/* copy base */
60967c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH_BASE(pbk),
60977c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH_BASE(old_pri_key_obj_p));
60987c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
60997c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61007c478bd9Sstevel@tonic-gate 				return (rv);
61017c478bd9Sstevel@tonic-gate 			}
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate 			/* copy value */
61047c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH_VALUE(pbk),
61057c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH_VALUE(old_pri_key_obj_p));
61067c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
61077c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61087c478bd9Sstevel@tonic-gate 				return (rv);
61097c478bd9Sstevel@tonic-gate 			}
61107c478bd9Sstevel@tonic-gate 			break;
6111f9fbec18Smcpowers 		case CKK_EC:
6112f9fbec18Smcpowers 			(void) memcpy(KEY_PRI_EC(pbk),
6113f9fbec18Smcpowers 			    KEY_PRI_EC(old_pri_key_obj_p),
6114f9fbec18Smcpowers 			    sizeof (ec_pri_key_t));
6115f9fbec18Smcpowers 
6116f9fbec18Smcpowers 			/* copy value */
6117f9fbec18Smcpowers 			rv = copy_bigint(KEY_PRI_EC_VALUE(pbk),
6118f9fbec18Smcpowers 			    KEY_PRI_EC_VALUE(old_pri_key_obj_p));
6119f9fbec18Smcpowers 			if (rv != CKR_OK) {
6120f9fbec18Smcpowers 				free_private_key_attr(pbk, key_type);
6121f9fbec18Smcpowers 				return (rv);
6122f9fbec18Smcpowers 			}
6123f9fbec18Smcpowers 			break;
61247c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
61257c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_DH942(pbk),
61267c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH942(old_pri_key_obj_p),
61277c478bd9Sstevel@tonic-gate 			    sizeof (dh942_pri_key_t));
61287c478bd9Sstevel@tonic-gate 
61297c478bd9Sstevel@tonic-gate 			/* copy prime */
61307c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH942_PRIME(pbk),
61317c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH942_PRIME(old_pri_key_obj_p));
61327c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
61337c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61347c478bd9Sstevel@tonic-gate 				return (rv);
61357c478bd9Sstevel@tonic-gate 			}
61367c478bd9Sstevel@tonic-gate 
61377c478bd9Sstevel@tonic-gate 			/* copy subprime */
61387c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH942_SUBPRIME(pbk),
61397c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH942_SUBPRIME(old_pri_key_obj_p));
61407c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
61417c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61427c478bd9Sstevel@tonic-gate 				return (rv);
61437c478bd9Sstevel@tonic-gate 			}
61447c478bd9Sstevel@tonic-gate 
61457c478bd9Sstevel@tonic-gate 			/* copy base */
61467c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH942_BASE(pbk),
61477c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH942_BASE(old_pri_key_obj_p));
61487c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
61497c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61507c478bd9Sstevel@tonic-gate 				return (rv);
61517c478bd9Sstevel@tonic-gate 			}
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 			/* copy value */
61547c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DH942_VALUE(pbk),
61557c478bd9Sstevel@tonic-gate 			    KEY_PRI_DH942_VALUE(old_pri_key_obj_p));
61567c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
61577c478bd9Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
61587c478bd9Sstevel@tonic-gate 				return (rv);
61597c478bd9Sstevel@tonic-gate 			}
61607c478bd9Sstevel@tonic-gate 			break;
61617c478bd9Sstevel@tonic-gate 		default:
61627c478bd9Sstevel@tonic-gate 			break;
61637c478bd9Sstevel@tonic-gate 	}
61647c478bd9Sstevel@tonic-gate 	*new_pri_key_obj_p = pbk;
61657c478bd9Sstevel@tonic-gate 	return (rv);
61667c478bd9Sstevel@tonic-gate }
61677c478bd9Sstevel@tonic-gate 
61687c478bd9Sstevel@tonic-gate static void
61697c478bd9Sstevel@tonic-gate free_domain_attr(domain_obj_t *domain, CK_KEY_TYPE key_type)
61707c478bd9Sstevel@tonic-gate {
61717c478bd9Sstevel@tonic-gate 	if (domain == NULL) {
61727c478bd9Sstevel@tonic-gate 		return;
61737c478bd9Sstevel@tonic-gate 	}
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 	switch (key_type) {
61767c478bd9Sstevel@tonic-gate 		case CKK_DSA:
61777c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DSA_PRIME(domain));
61787c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DSA_SUBPRIME(domain));
61797c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DSA_BASE(domain));
61807c478bd9Sstevel@tonic-gate 			break;
61817c478bd9Sstevel@tonic-gate 		case CKK_DH:
61827c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DH_PRIME(domain));
61837c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DH_BASE(domain));
61847c478bd9Sstevel@tonic-gate 			break;
61857c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
61867c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DH942_PRIME(domain));
61877c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DH942_SUBPRIME(domain));
61887c478bd9Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_DOM_DH942_BASE(domain));
61897c478bd9Sstevel@tonic-gate 			break;
61907c478bd9Sstevel@tonic-gate 		default:
61917c478bd9Sstevel@tonic-gate 			break;
61927c478bd9Sstevel@tonic-gate 	}
61937c478bd9Sstevel@tonic-gate 	free(domain);
61947c478bd9Sstevel@tonic-gate }
61957c478bd9Sstevel@tonic-gate 
61967c478bd9Sstevel@tonic-gate CK_RV
61977c478bd9Sstevel@tonic-gate soft_copy_domain_attr(domain_obj_t *old_domain_obj_p,
61987c478bd9Sstevel@tonic-gate     domain_obj_t **new_domain_obj_p, CK_KEY_TYPE key_type)
61997c478bd9Sstevel@tonic-gate {
62007c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
62017c478bd9Sstevel@tonic-gate 	domain_obj_t *domain;
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 	domain = calloc(1, sizeof (domain_obj_t));
62047c478bd9Sstevel@tonic-gate 	if (domain == NULL) {
62057c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
62067c478bd9Sstevel@tonic-gate 	}
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate 	switch (key_type) {
62097c478bd9Sstevel@tonic-gate 		case CKK_DSA:
62107c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_DOM_DSA(domain),
62117c478bd9Sstevel@tonic-gate 			    KEY_DOM_DSA(old_domain_obj_p),
62127c478bd9Sstevel@tonic-gate 			    sizeof (dsa_dom_key_t));
62137c478bd9Sstevel@tonic-gate 
62147c478bd9Sstevel@tonic-gate 			/* copy prime */
62157c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DSA_PRIME(domain),
62167c478bd9Sstevel@tonic-gate 			    KEY_DOM_DSA_PRIME(old_domain_obj_p));
62177c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62187c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62197c478bd9Sstevel@tonic-gate 				return (rv);
62207c478bd9Sstevel@tonic-gate 			}
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate 			/* copy subprime */
62237c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DSA_SUBPRIME(domain),
62247c478bd9Sstevel@tonic-gate 			    KEY_DOM_DSA_SUBPRIME(old_domain_obj_p));
62257c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62267c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62277c478bd9Sstevel@tonic-gate 				return (rv);
62287c478bd9Sstevel@tonic-gate 			}
62297c478bd9Sstevel@tonic-gate 
62307c478bd9Sstevel@tonic-gate 			/* copy base */
62317c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DSA_BASE(domain),
62327c478bd9Sstevel@tonic-gate 			    KEY_DOM_DSA_BASE(old_domain_obj_p));
62337c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62347c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62357c478bd9Sstevel@tonic-gate 				return (rv);
62367c478bd9Sstevel@tonic-gate 			}
62377c478bd9Sstevel@tonic-gate 
62387c478bd9Sstevel@tonic-gate 			break;
62397c478bd9Sstevel@tonic-gate 		case CKK_DH:
62407c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_DOM_DH(domain),
62417c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH(old_domain_obj_p),
62427c478bd9Sstevel@tonic-gate 			    sizeof (dh_dom_key_t));
62437c478bd9Sstevel@tonic-gate 
62447c478bd9Sstevel@tonic-gate 			/* copy prime */
62457c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DH_PRIME(domain),
62467c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH_PRIME(old_domain_obj_p));
62477c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62487c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62497c478bd9Sstevel@tonic-gate 				return (rv);
62507c478bd9Sstevel@tonic-gate 			}
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 			/* copy base */
62537c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DH_BASE(domain),
62547c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH_BASE(old_domain_obj_p));
62557c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62567c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62577c478bd9Sstevel@tonic-gate 				return (rv);
62587c478bd9Sstevel@tonic-gate 			}
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate 			break;
62617c478bd9Sstevel@tonic-gate 		case CKK_X9_42_DH:
62627c478bd9Sstevel@tonic-gate 			(void) memcpy(KEY_DOM_DH942(domain),
62637c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH942(old_domain_obj_p),
62647c478bd9Sstevel@tonic-gate 			    sizeof (dh942_dom_key_t));
62657c478bd9Sstevel@tonic-gate 
62667c478bd9Sstevel@tonic-gate 			/* copy prime */
62677c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DH942_PRIME(domain),
62687c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH942_PRIME(old_domain_obj_p));
62697c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62707c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62717c478bd9Sstevel@tonic-gate 				return (rv);
62727c478bd9Sstevel@tonic-gate 			}
62737c478bd9Sstevel@tonic-gate 
62747c478bd9Sstevel@tonic-gate 			/* copy subprime */
62757c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DH942_SUBPRIME(domain),
62767c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH942_SUBPRIME(old_domain_obj_p));
62777c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62787c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62797c478bd9Sstevel@tonic-gate 				return (rv);
62807c478bd9Sstevel@tonic-gate 			}
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 			/* copy base */
62837c478bd9Sstevel@tonic-gate 			rv = copy_bigint(KEY_DOM_DH942_BASE(domain),
62847c478bd9Sstevel@tonic-gate 			    KEY_DOM_DH942_BASE(old_domain_obj_p));
62857c478bd9Sstevel@tonic-gate 			if (rv != CKR_OK) {
62867c478bd9Sstevel@tonic-gate 				free_domain_attr(domain, key_type);
62877c478bd9Sstevel@tonic-gate 				return (rv);
62887c478bd9Sstevel@tonic-gate 			}
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 			break;
62917c478bd9Sstevel@tonic-gate 		default:
62927c478bd9Sstevel@tonic-gate 			break;
62937c478bd9Sstevel@tonic-gate 	}
62947c478bd9Sstevel@tonic-gate 	*new_domain_obj_p = domain;
62957c478bd9Sstevel@tonic-gate 	return (rv);
62967c478bd9Sstevel@tonic-gate }
62977c478bd9Sstevel@tonic-gate 
62987c478bd9Sstevel@tonic-gate CK_RV
62997c478bd9Sstevel@tonic-gate soft_copy_secret_key_attr(secret_key_obj_t *old_secret_key_obj_p,
63007c478bd9Sstevel@tonic-gate     secret_key_obj_t **new_secret_key_obj_p)
63017c478bd9Sstevel@tonic-gate {
63027c478bd9Sstevel@tonic-gate 	secret_key_obj_t *sk;
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate 	sk = malloc(sizeof (secret_key_obj_t));
63057c478bd9Sstevel@tonic-gate 	if (sk == NULL) {
63067c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
63077c478bd9Sstevel@tonic-gate 	}
63087c478bd9Sstevel@tonic-gate 	(void) memcpy(sk, old_secret_key_obj_p, sizeof (secret_key_obj_t));
63097c478bd9Sstevel@tonic-gate 
63107c478bd9Sstevel@tonic-gate 	/* copy the secret key value */
6311*a8793c76SJason King 	sk->sk_value = malloc(sk->sk_value_len);
63127c478bd9Sstevel@tonic-gate 	if (sk->sk_value == NULL) {
63137c478bd9Sstevel@tonic-gate 		free(sk);
63147c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
63157c478bd9Sstevel@tonic-gate 	}
63167c478bd9Sstevel@tonic-gate 	(void) memcpy(sk->sk_value, old_secret_key_obj_p->sk_value,
63177c478bd9Sstevel@tonic-gate 	    (sizeof (CK_BYTE) * sk->sk_value_len));
63187c478bd9Sstevel@tonic-gate 
63197c478bd9Sstevel@tonic-gate 	/*
63207c478bd9Sstevel@tonic-gate 	 * Copy the pre-expanded key schedule.
63217c478bd9Sstevel@tonic-gate 	 */
63227c478bd9Sstevel@tonic-gate 	if (old_secret_key_obj_p->key_sched != NULL &&
63237c478bd9Sstevel@tonic-gate 	    old_secret_key_obj_p->keysched_len > 0) {
63247c478bd9Sstevel@tonic-gate 		sk->key_sched = malloc(old_secret_key_obj_p->keysched_len);
63257c478bd9Sstevel@tonic-gate 		if (sk->key_sched == NULL) {
6326*a8793c76SJason King 			freezero(sk->sk_value, sk->sk_value_len);
63277c478bd9Sstevel@tonic-gate 			free(sk);
63287c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
63297c478bd9Sstevel@tonic-gate 		}
63307c478bd9Sstevel@tonic-gate 		sk->keysched_len = old_secret_key_obj_p->keysched_len;
63317c478bd9Sstevel@tonic-gate 		(void) memcpy(sk->key_sched, old_secret_key_obj_p->key_sched,
6332f9fbec18Smcpowers 		    sk->keysched_len);
63337c478bd9Sstevel@tonic-gate 	}
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate 	*new_secret_key_obj_p = sk;
63367c478bd9Sstevel@tonic-gate 
63377c478bd9Sstevel@tonic-gate 	return (CKR_OK);
63387c478bd9Sstevel@tonic-gate }
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate /*
63417c478bd9Sstevel@tonic-gate  * If CKA_CLASS not given, guess CKA_CLASS using
63427c478bd9Sstevel@tonic-gate  * attributes on template .
63437c478bd9Sstevel@tonic-gate  *
63447c478bd9Sstevel@tonic-gate  * Some attributes are specific to an object class.  If one or more
63457c478bd9Sstevel@tonic-gate  * of these attributes are in the template, make a list of classes
63467c478bd9Sstevel@tonic-gate  * that can have these attributes.  This would speed up the search later,
63477c478bd9Sstevel@tonic-gate  * because we can immediately skip an object if the class of that
6348c2e31228SViswanathan Kannappan  * object can not possibly contain one of the attributes.
63497c478bd9Sstevel@tonic-gate  *
63507c478bd9Sstevel@tonic-gate  */
63517c478bd9Sstevel@tonic-gate void
63527c478bd9Sstevel@tonic-gate soft_process_find_attr(CK_OBJECT_CLASS *pclasses,
63537c478bd9Sstevel@tonic-gate     CK_ULONG *num_result_pclasses, CK_ATTRIBUTE_PTR pTemplate,
63547c478bd9Sstevel@tonic-gate     CK_ULONG ulCount)
63557c478bd9Sstevel@tonic-gate {
63567c478bd9Sstevel@tonic-gate 	ulong_t i;
63577c478bd9Sstevel@tonic-gate 	int j;
63587c478bd9Sstevel@tonic-gate 	boolean_t pub_found = B_FALSE,
63597c478bd9Sstevel@tonic-gate 	    priv_found = B_FALSE,
63607c478bd9Sstevel@tonic-gate 	    secret_found = B_FALSE,
63617c478bd9Sstevel@tonic-gate 	    domain_found = B_FALSE,
63627c478bd9Sstevel@tonic-gate 	    hardware_found = B_FALSE,
63637c478bd9Sstevel@tonic-gate 	    cert_found = B_FALSE;
63647c478bd9Sstevel@tonic-gate 	int num_pub_key_attrs, num_priv_key_attrs,
63657c478bd9Sstevel@tonic-gate 	    num_secret_key_attrs, num_domain_attrs,
63667c478bd9Sstevel@tonic-gate 	    num_hardware_attrs, num_cert_attrs;
63677c478bd9Sstevel@tonic-gate 	int num_pclasses = 0;
63687c478bd9Sstevel@tonic-gate 
63697c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulCount; i++) {
63707c478bd9Sstevel@tonic-gate 		if (pTemplate[i].type == CKA_CLASS) {
63717c478bd9Sstevel@tonic-gate 			/*
63727c478bd9Sstevel@tonic-gate 			 * don't need to guess the class, it is specified.
63737c478bd9Sstevel@tonic-gate 			 * Just record the class, and return.
63747c478bd9Sstevel@tonic-gate 			 */
63757c478bd9Sstevel@tonic-gate 			pclasses[0] =
63767c478bd9Sstevel@tonic-gate 			    (*((CK_OBJECT_CLASS *)pTemplate[i].pValue));
63777c478bd9Sstevel@tonic-gate 			*num_result_pclasses = 1;
63787c478bd9Sstevel@tonic-gate 			return;
63797c478bd9Sstevel@tonic-gate 		}
63807c478bd9Sstevel@tonic-gate 	}
63817c478bd9Sstevel@tonic-gate 
63827c478bd9Sstevel@tonic-gate 	num_pub_key_attrs =
63837c478bd9Sstevel@tonic-gate 	    sizeof (PUB_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63847c478bd9Sstevel@tonic-gate 	num_priv_key_attrs =
63857c478bd9Sstevel@tonic-gate 	    sizeof (PRIV_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63867c478bd9Sstevel@tonic-gate 	num_secret_key_attrs =
63877c478bd9Sstevel@tonic-gate 	    sizeof (SECRET_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63887c478bd9Sstevel@tonic-gate 	num_domain_attrs =
63897c478bd9Sstevel@tonic-gate 	    sizeof (DOMAIN_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63907c478bd9Sstevel@tonic-gate 	num_hardware_attrs =
63917c478bd9Sstevel@tonic-gate 	    sizeof (HARDWARE_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63927c478bd9Sstevel@tonic-gate 	num_cert_attrs =
63937c478bd9Sstevel@tonic-gate 	    sizeof (CERT_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
63947c478bd9Sstevel@tonic-gate 
63957c478bd9Sstevel@tonic-gate 	/*
63967c478bd9Sstevel@tonic-gate 	 * Get the list of objects class that might contain
63977c478bd9Sstevel@tonic-gate 	 * some attributes.
63987c478bd9Sstevel@tonic-gate 	 */
63997c478bd9Sstevel@tonic-gate 	for (i = 0; i < ulCount; i++) {
64007c478bd9Sstevel@tonic-gate 		/*
64017c478bd9Sstevel@tonic-gate 		 * only check if this attribute can belong to public key object
64027c478bd9Sstevel@tonic-gate 		 * class if public key object isn't already in the list
64037c478bd9Sstevel@tonic-gate 		 */
64047c478bd9Sstevel@tonic-gate 		if (!pub_found) {
64057c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_pub_key_attrs; j++) {
64067c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == PUB_KEY_ATTRS[j]) {
64077c478bd9Sstevel@tonic-gate 					pub_found = B_TRUE;
64087c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64097c478bd9Sstevel@tonic-gate 					    CKO_PUBLIC_KEY;
64107c478bd9Sstevel@tonic-gate 					break;
64117c478bd9Sstevel@tonic-gate 				}
64127c478bd9Sstevel@tonic-gate 			}
64137c478bd9Sstevel@tonic-gate 		}
64147c478bd9Sstevel@tonic-gate 
64157c478bd9Sstevel@tonic-gate 		if (!priv_found) {
64167c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_priv_key_attrs; j++) {
64177c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == PRIV_KEY_ATTRS[j]) {
64187c478bd9Sstevel@tonic-gate 					priv_found = B_TRUE;
64197c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64207c478bd9Sstevel@tonic-gate 					    CKO_PRIVATE_KEY;
64217c478bd9Sstevel@tonic-gate 					break;
64227c478bd9Sstevel@tonic-gate 				}
64237c478bd9Sstevel@tonic-gate 			}
64247c478bd9Sstevel@tonic-gate 		}
64257c478bd9Sstevel@tonic-gate 
64267c478bd9Sstevel@tonic-gate 		if (!secret_found) {
64277c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_secret_key_attrs; j++) {
64287c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == SECRET_KEY_ATTRS[j]) {
64297c478bd9Sstevel@tonic-gate 					secret_found = B_TRUE;
64307c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64317c478bd9Sstevel@tonic-gate 					    CKO_SECRET_KEY;
64327c478bd9Sstevel@tonic-gate 					break;
64337c478bd9Sstevel@tonic-gate 				}
64347c478bd9Sstevel@tonic-gate 			}
64357c478bd9Sstevel@tonic-gate 		}
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 		if (!domain_found) {
64387c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_domain_attrs; j++) {
64397c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == DOMAIN_ATTRS[j]) {
64407c478bd9Sstevel@tonic-gate 					domain_found = B_TRUE;
64417c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64427c478bd9Sstevel@tonic-gate 					    CKO_DOMAIN_PARAMETERS;
64437c478bd9Sstevel@tonic-gate 					break;
64447c478bd9Sstevel@tonic-gate 				}
64457c478bd9Sstevel@tonic-gate 			}
64467c478bd9Sstevel@tonic-gate 		}
64477c478bd9Sstevel@tonic-gate 
64487c478bd9Sstevel@tonic-gate 		if (!hardware_found) {
64497c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_hardware_attrs; j++) {
64507c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == HARDWARE_ATTRS[j]) {
64517c478bd9Sstevel@tonic-gate 					hardware_found = B_TRUE;
64527c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64537c478bd9Sstevel@tonic-gate 					    CKO_HW_FEATURE;
64547c478bd9Sstevel@tonic-gate 					break;
64557c478bd9Sstevel@tonic-gate 				}
64567c478bd9Sstevel@tonic-gate 			}
64577c478bd9Sstevel@tonic-gate 		}
64587c478bd9Sstevel@tonic-gate 
64597c478bd9Sstevel@tonic-gate 		if (!cert_found) {
64607c478bd9Sstevel@tonic-gate 			for (j = 0; j < num_cert_attrs; j++) {
64617c478bd9Sstevel@tonic-gate 				if (pTemplate[i].type == CERT_ATTRS[j]) {
64627c478bd9Sstevel@tonic-gate 					cert_found = B_TRUE;
64637c478bd9Sstevel@tonic-gate 					pclasses[num_pclasses++] =
64647c478bd9Sstevel@tonic-gate 					    CKO_CERTIFICATE;
64657c478bd9Sstevel@tonic-gate 					break;
64667c478bd9Sstevel@tonic-gate 				}
64677c478bd9Sstevel@tonic-gate 			}
64687c478bd9Sstevel@tonic-gate 		}
64697c478bd9Sstevel@tonic-gate 	}
64707c478bd9Sstevel@tonic-gate 	*num_result_pclasses = num_pclasses;
64717c478bd9Sstevel@tonic-gate }
64727c478bd9Sstevel@tonic-gate 
64737c478bd9Sstevel@tonic-gate boolean_t
64747c478bd9Sstevel@tonic-gate soft_find_match_attrs(soft_object_t *obj, CK_OBJECT_CLASS *pclasses,
64757c478bd9Sstevel@tonic-gate     CK_ULONG num_pclasses, CK_ATTRIBUTE *template, CK_ULONG num_attr)
64767c478bd9Sstevel@tonic-gate {
64777c478bd9Sstevel@tonic-gate 	ulong_t i;
64787c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE *tmpl_attr, *obj_attr;
64797c478bd9Sstevel@tonic-gate 	cert_attr_t *cert_attr;
64807c478bd9Sstevel@tonic-gate 	uint64_t attr_mask;
64817c478bd9Sstevel@tonic-gate 	biginteger_t *bigint;
64827c478bd9Sstevel@tonic-gate 	boolean_t compare_attr, compare_bigint, compare_boolean;
64837c478bd9Sstevel@tonic-gate 	boolean_t compare_cert_val, compare_cert_type;
64847c478bd9Sstevel@tonic-gate 
64857c478bd9Sstevel@tonic-gate 	/*
64867c478bd9Sstevel@tonic-gate 	 * Check if the class of this object match with any
6487c2e31228SViswanathan Kannappan 	 * of object classes that can possibly contain the
64887c478bd9Sstevel@tonic-gate 	 * requested attributes.
64897c478bd9Sstevel@tonic-gate 	 */
64907c478bd9Sstevel@tonic-gate 	if (num_pclasses > 0) {
64917c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_pclasses; i++) {
64927c478bd9Sstevel@tonic-gate 			if (obj->class == pclasses[i]) {
64937c478bd9Sstevel@tonic-gate 				break;
64947c478bd9Sstevel@tonic-gate 			}
64957c478bd9Sstevel@tonic-gate 		}
64967c478bd9Sstevel@tonic-gate 		if (i == num_pclasses) {
64977c478bd9Sstevel@tonic-gate 			/*
6498c2e31228SViswanathan Kannappan 			 * this object can't possibly contain one or
64997c478bd9Sstevel@tonic-gate 			 * more attributes, don't need to check this object
65007c478bd9Sstevel@tonic-gate 			 */
65017c478bd9Sstevel@tonic-gate 			return (B_FALSE);
65027c478bd9Sstevel@tonic-gate 		}
65037c478bd9Sstevel@tonic-gate 	}
65047c478bd9Sstevel@tonic-gate 
65057c478bd9Sstevel@tonic-gate 	/* need to examine everything */
65067c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_attr; i++) {
65077c478bd9Sstevel@tonic-gate 		tmpl_attr = &(template[i]);
65087c478bd9Sstevel@tonic-gate 		compare_attr = B_FALSE;
65097c478bd9Sstevel@tonic-gate 		compare_bigint = B_FALSE;
65107c478bd9Sstevel@tonic-gate 		compare_boolean = B_FALSE;
65117c478bd9Sstevel@tonic-gate 		compare_cert_val = B_FALSE;
65127c478bd9Sstevel@tonic-gate 		compare_cert_type = B_FALSE;
65137c478bd9Sstevel@tonic-gate 		switch (tmpl_attr->type) {
65147c478bd9Sstevel@tonic-gate 		/* First, check the most common attributes */
65157c478bd9Sstevel@tonic-gate 		case CKA_CLASS:
65167c478bd9Sstevel@tonic-gate 			if (*((CK_OBJECT_CLASS *)tmpl_attr->pValue) !=
65177c478bd9Sstevel@tonic-gate 			    obj->class) {
65187c478bd9Sstevel@tonic-gate 				return (B_FALSE);
65197c478bd9Sstevel@tonic-gate 			}
65207c478bd9Sstevel@tonic-gate 			break;
65217c478bd9Sstevel@tonic-gate 		case CKA_KEY_TYPE:
65227c478bd9Sstevel@tonic-gate 			if (*((CK_KEY_TYPE *)tmpl_attr->pValue) !=
65237c478bd9Sstevel@tonic-gate 			    obj->key_type) {
65247c478bd9Sstevel@tonic-gate 				return (B_FALSE);
65257c478bd9Sstevel@tonic-gate 			}
65267c478bd9Sstevel@tonic-gate 			break;
65277c478bd9Sstevel@tonic-gate 		case CKA_ENCRYPT:
65287c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & ENCRYPT_BOOL_ON;
65297c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65307c478bd9Sstevel@tonic-gate 			break;
65317c478bd9Sstevel@tonic-gate 		case CKA_DECRYPT:
65327c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & DECRYPT_BOOL_ON;
65337c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65347c478bd9Sstevel@tonic-gate 			break;
65357c478bd9Sstevel@tonic-gate 		case CKA_WRAP:
65367c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & WRAP_BOOL_ON;
65377c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65387c478bd9Sstevel@tonic-gate 			break;
65397c478bd9Sstevel@tonic-gate 		case CKA_UNWRAP:
65407c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & UNWRAP_BOOL_ON;
65417c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65427c478bd9Sstevel@tonic-gate 			break;
65437c478bd9Sstevel@tonic-gate 		case CKA_SIGN:
65447c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & SIGN_BOOL_ON;
65457c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65467c478bd9Sstevel@tonic-gate 			break;
65477c478bd9Sstevel@tonic-gate 		case CKA_SIGN_RECOVER:
65487c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65497c478bd9Sstevel@tonic-gate 			    SIGN_RECOVER_BOOL_ON;
65507c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65517c478bd9Sstevel@tonic-gate 			break;
65527c478bd9Sstevel@tonic-gate 		case CKA_VERIFY:
65537c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & VERIFY_BOOL_ON;
65547c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65557c478bd9Sstevel@tonic-gate 			break;
65567c478bd9Sstevel@tonic-gate 		case CKA_VERIFY_RECOVER:
65577c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65587c478bd9Sstevel@tonic-gate 			    VERIFY_RECOVER_BOOL_ON;
65597c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65607c478bd9Sstevel@tonic-gate 			break;
65617c478bd9Sstevel@tonic-gate 		case CKA_DERIVE:
65627c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & DERIVE_BOOL_ON;
65637c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65647c478bd9Sstevel@tonic-gate 			break;
65657c478bd9Sstevel@tonic-gate 		case CKA_LOCAL:
65667c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & LOCAL_BOOL_ON;
65677c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65687c478bd9Sstevel@tonic-gate 			break;
65697c478bd9Sstevel@tonic-gate 		case CKA_SENSITIVE:
65707c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & SENSITIVE_BOOL_ON;
65717c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65727c478bd9Sstevel@tonic-gate 			break;
65737c478bd9Sstevel@tonic-gate 		case CKA_SECONDARY_AUTH:
65747c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65757c478bd9Sstevel@tonic-gate 			    SECONDARY_AUTH_BOOL_ON;
65767c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65777c478bd9Sstevel@tonic-gate 			break;
65787c478bd9Sstevel@tonic-gate 		case CKA_TRUSTED:
65797c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & TRUSTED_BOOL_ON;
65807c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65817c478bd9Sstevel@tonic-gate 			break;
65827c478bd9Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
65837c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65847c478bd9Sstevel@tonic-gate 			    EXTRACTABLE_BOOL_ON;
65857c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65867c478bd9Sstevel@tonic-gate 			break;
65877c478bd9Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
65887c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65897c478bd9Sstevel@tonic-gate 			    ALWAYS_SENSITIVE_BOOL_ON;
65907c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65917c478bd9Sstevel@tonic-gate 			break;
65927c478bd9Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
65937c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
65947c478bd9Sstevel@tonic-gate 			    NEVER_EXTRACTABLE_BOOL_ON;
65957c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
65967c478bd9Sstevel@tonic-gate 			break;
65977c478bd9Sstevel@tonic-gate 		case CKA_TOKEN:
65987c478bd9Sstevel@tonic-gate 			attr_mask = (obj->object_type) & TOKEN_OBJECT;
65997c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
66007c478bd9Sstevel@tonic-gate 			break;
66017c478bd9Sstevel@tonic-gate 		case CKA_PRIVATE:
66027c478bd9Sstevel@tonic-gate 			attr_mask = (obj->object_type) & PRIVATE_OBJECT;
66037c478bd9Sstevel@tonic-gate 			compare_boolean = B_TRUE;
66047c478bd9Sstevel@tonic-gate 			break;
66057c478bd9Sstevel@tonic-gate 		case CKA_MODIFIABLE:
66067c478bd9Sstevel@tonic-gate 		{
66077c478bd9Sstevel@tonic-gate 			CK_BBOOL bval;
66087c478bd9Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
66097c478bd9Sstevel@tonic-gate 			    NOT_MODIFIABLE_BOOL_ON;
66107c478bd9Sstevel@tonic-gate 
66117c478bd9Sstevel@tonic-gate 			if (attr_mask) {
66127c478bd9Sstevel@tonic-gate 				bval = FALSE;
66137c478bd9Sstevel@tonic-gate 			} else {
66147c478bd9Sstevel@tonic-gate 				bval = TRUE;
66157c478bd9Sstevel@tonic-gate 			}
66167c478bd9Sstevel@tonic-gate 			if (bval != *((CK_BBOOL *)tmpl_attr->pValue)) {
66177c478bd9Sstevel@tonic-gate 				return (B_FALSE);
66187c478bd9Sstevel@tonic-gate 			}
66197c478bd9Sstevel@tonic-gate 			break;
66207c478bd9Sstevel@tonic-gate 		}
66217c478bd9Sstevel@tonic-gate 		case CKA_OWNER:
66227c478bd9Sstevel@tonic-gate 			/*
66237c478bd9Sstevel@tonic-gate 			 * For X.509 attribute certificate object, get its
66247c478bd9Sstevel@tonic-gate 			 * CKA_OWNER attribute from the x509_attr_cert_t struct.
66257c478bd9Sstevel@tonic-gate 			 */
66267c478bd9Sstevel@tonic-gate 			if ((obj->class == CKO_CERTIFICATE) &&
66277c478bd9Sstevel@tonic-gate 			    (obj->cert_type == CKC_X_509_ATTR_CERT)) {
66287c478bd9Sstevel@tonic-gate 				cert_attr = X509_ATTR_CERT_OWNER(obj);
66297c478bd9Sstevel@tonic-gate 				compare_cert_val = B_TRUE;
66307c478bd9Sstevel@tonic-gate 			}
66317c478bd9Sstevel@tonic-gate 			break;
66327c478bd9Sstevel@tonic-gate 		case CKA_SUBJECT:
66337c478bd9Sstevel@tonic-gate 			/*
66347c478bd9Sstevel@tonic-gate 			 * For X.509 certificate object, get its CKA_SUBJECT
66357c478bd9Sstevel@tonic-gate 			 * attribute from the x509_cert_t struct (not from
66367c478bd9Sstevel@tonic-gate 			 * the extra_attrlistp).
66377c478bd9Sstevel@tonic-gate 			 */
66387c478bd9Sstevel@tonic-gate 			if ((obj->class == CKO_CERTIFICATE) &&
66397c478bd9Sstevel@tonic-gate 			    (obj->cert_type == CKC_X_509)) {
66407c478bd9Sstevel@tonic-gate 				cert_attr = X509_CERT_SUBJECT(obj);
66417c478bd9Sstevel@tonic-gate 				compare_cert_val = B_TRUE;
66427c478bd9Sstevel@tonic-gate 				break;
66437c478bd9Sstevel@tonic-gate 			}
66447c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
66457c478bd9Sstevel@tonic-gate 		case CKA_ID:
66467c478bd9Sstevel@tonic-gate 		case CKA_START_DATE:
66477c478bd9Sstevel@tonic-gate 		case CKA_END_DATE:
66487c478bd9Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
66497c478bd9Sstevel@tonic-gate 		case CKA_LABEL:
66507c478bd9Sstevel@tonic-gate 		case CKA_ISSUER:
66517c478bd9Sstevel@tonic-gate 		case CKA_SERIAL_NUMBER:
66527c478bd9Sstevel@tonic-gate 		case CKA_AC_ISSUER:
66537c478bd9Sstevel@tonic-gate 		case CKA_ATTR_TYPES:
66547c478bd9Sstevel@tonic-gate 			/* find these attributes from extra_attrlistp */
66557c478bd9Sstevel@tonic-gate 			obj_attr = get_extra_attr(tmpl_attr->type, obj);
66567c478bd9Sstevel@tonic-gate 			compare_attr = B_TRUE;
66577c478bd9Sstevel@tonic-gate 			break;
66587c478bd9Sstevel@tonic-gate 		case CKA_CERTIFICATE_TYPE:
66597c478bd9Sstevel@tonic-gate 			compare_cert_type = B_TRUE;
66607c478bd9Sstevel@tonic-gate 			break;
66617c478bd9Sstevel@tonic-gate 		case CKA_VALUE_LEN:
66627c478bd9Sstevel@tonic-gate 			/* only secret key has this attribute */
66637c478bd9Sstevel@tonic-gate 			if (obj->class == CKO_SECRET_KEY) {
66647c478bd9Sstevel@tonic-gate 				if (*((CK_ULONG *)tmpl_attr->pValue) !=
66657c478bd9Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(obj)) {
66667c478bd9Sstevel@tonic-gate 					return (B_FALSE);
66677c478bd9Sstevel@tonic-gate 				}
66687c478bd9Sstevel@tonic-gate 			} else {
66697c478bd9Sstevel@tonic-gate 				return (B_FALSE);
66707c478bd9Sstevel@tonic-gate 			}
66717c478bd9Sstevel@tonic-gate 			break;
66727c478bd9Sstevel@tonic-gate 		case CKA_VALUE:
66737c478bd9Sstevel@tonic-gate 			switch (obj->class) {
66747c478bd9Sstevel@tonic-gate 			case CKO_SECRET_KEY:
66757c478bd9Sstevel@tonic-gate 				/*
66767c478bd9Sstevel@tonic-gate 				 * secret_key_obj_t is the same as
66777c478bd9Sstevel@tonic-gate 				 * biginteger_t
66787c478bd9Sstevel@tonic-gate 				 */
66797c478bd9Sstevel@tonic-gate 				bigint = (biginteger_t *)OBJ_SEC(obj);
66807c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
66817c478bd9Sstevel@tonic-gate 				break;
66827c478bd9Sstevel@tonic-gate 			case CKO_PRIVATE_KEY:
66837c478bd9Sstevel@tonic-gate 				if (obj->key_type == CKK_DSA) {
66847c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_VALUE(obj);
66857c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_DH) {
66867c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH_VALUE(obj);
66877c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_X9_42_DH) {
66887c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH942_VALUE(obj);
66897c478bd9Sstevel@tonic-gate 				} else {
66907c478bd9Sstevel@tonic-gate 					return (B_FALSE);
66917c478bd9Sstevel@tonic-gate 				}
66927c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
66937c478bd9Sstevel@tonic-gate 				break;
66947c478bd9Sstevel@tonic-gate 			case CKO_PUBLIC_KEY:
66957c478bd9Sstevel@tonic-gate 				if (obj->key_type == CKK_DSA) {
66967c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_VALUE(obj);
66977c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_DH) {
66987c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH_VALUE(obj);
66997c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_X9_42_DH) {
67007c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH942_VALUE(obj);
67017c478bd9Sstevel@tonic-gate 				} else {
67027c478bd9Sstevel@tonic-gate 					return (B_FALSE);
67037c478bd9Sstevel@tonic-gate 				}
67047c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67057c478bd9Sstevel@tonic-gate 				break;
67067c478bd9Sstevel@tonic-gate 			case CKO_CERTIFICATE:
67077c478bd9Sstevel@tonic-gate 				if (obj->cert_type == CKC_X_509) {
67087c478bd9Sstevel@tonic-gate 					cert_attr = X509_CERT_VALUE(obj);
67097c478bd9Sstevel@tonic-gate 				} else if (obj->cert_type ==
67107c478bd9Sstevel@tonic-gate 				    CKC_X_509_ATTR_CERT) {
67117c478bd9Sstevel@tonic-gate 					cert_attr = X509_ATTR_CERT_VALUE(obj);
67127c478bd9Sstevel@tonic-gate 				}
67137c478bd9Sstevel@tonic-gate 				compare_cert_val = B_TRUE;
67147c478bd9Sstevel@tonic-gate 				break;
67157c478bd9Sstevel@tonic-gate 			default:
67167c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67177c478bd9Sstevel@tonic-gate 			}
67187c478bd9Sstevel@tonic-gate 			break;
67197c478bd9Sstevel@tonic-gate 		case CKA_MODULUS:
67207c478bd9Sstevel@tonic-gate 			/* only RSA public and private key have this attr */
67217c478bd9Sstevel@tonic-gate 			if (obj->key_type == CKK_RSA) {
67227c478bd9Sstevel@tonic-gate 				if (obj->class == CKO_PUBLIC_KEY) {
67237c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_RSA_MOD(obj);
67247c478bd9Sstevel@tonic-gate 				} else if (obj->class == CKO_PRIVATE_KEY) {
67257c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_RSA_MOD(obj);
67267c478bd9Sstevel@tonic-gate 				} else {
67277c478bd9Sstevel@tonic-gate 					return (B_FALSE);
67287c478bd9Sstevel@tonic-gate 				}
67297c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67307c478bd9Sstevel@tonic-gate 			} else {
67317c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67327c478bd9Sstevel@tonic-gate 			}
67337c478bd9Sstevel@tonic-gate 			break;
67347c478bd9Sstevel@tonic-gate 		case CKA_MODULUS_BITS:
67357c478bd9Sstevel@tonic-gate 			/* only RSA public key has this attribute */
67367c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
67377c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PUBLIC_KEY)) {
67387c478bd9Sstevel@tonic-gate 				CK_ULONG mod_bits = OBJ_PUB_RSA_MOD_BITS(obj);
67397c478bd9Sstevel@tonic-gate 				if (mod_bits !=
67407c478bd9Sstevel@tonic-gate 				    *((CK_ULONG *)tmpl_attr->pValue)) {
67417c478bd9Sstevel@tonic-gate 					return (B_FALSE);
67427c478bd9Sstevel@tonic-gate 				}
67437c478bd9Sstevel@tonic-gate 			} else {
67447c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67457c478bd9Sstevel@tonic-gate 			}
67467c478bd9Sstevel@tonic-gate 			break;
67477c478bd9Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
67487c478bd9Sstevel@tonic-gate 			/* only RSA public and private key have this attr */
67497c478bd9Sstevel@tonic-gate 			if (obj->key_type == CKK_RSA) {
67507c478bd9Sstevel@tonic-gate 				if (obj->class == CKO_PUBLIC_KEY) {
67517c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_RSA_PUBEXPO(obj);
67527c478bd9Sstevel@tonic-gate 				} else if (obj->class == CKO_PRIVATE_KEY) {
67537c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_RSA_PUBEXPO(obj);
67547c478bd9Sstevel@tonic-gate 				} else {
67557c478bd9Sstevel@tonic-gate 					return (B_FALSE);
67567c478bd9Sstevel@tonic-gate 				}
67577c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67587c478bd9Sstevel@tonic-gate 			} else {
67597c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67607c478bd9Sstevel@tonic-gate 			}
67617c478bd9Sstevel@tonic-gate 			break;
67627c478bd9Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
67637c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
67647c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
67657c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
67667c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIEXPO(obj);
67677c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67687c478bd9Sstevel@tonic-gate 			} else {
67697c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67707c478bd9Sstevel@tonic-gate 			}
67717c478bd9Sstevel@tonic-gate 			break;
67727c478bd9Sstevel@tonic-gate 		case CKA_PRIME_1:
67737c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
67747c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
67757c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
67767c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIME1(obj);
67777c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67787c478bd9Sstevel@tonic-gate 			} else {
67797c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67807c478bd9Sstevel@tonic-gate 			}
67817c478bd9Sstevel@tonic-gate 			break;
67827c478bd9Sstevel@tonic-gate 		case CKA_PRIME_2:
67837c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
67847c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
67857c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
67867c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIME2(obj);
67877c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67887c478bd9Sstevel@tonic-gate 			} else {
67897c478bd9Sstevel@tonic-gate 				return (B_FALSE);
67907c478bd9Sstevel@tonic-gate 			}
67917c478bd9Sstevel@tonic-gate 			break;
67927c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_1:
67937c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
67947c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
67957c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
67967c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_EXPO1(obj);
67977c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
67987c478bd9Sstevel@tonic-gate 			} else {
67997c478bd9Sstevel@tonic-gate 				return (B_FALSE);
68007c478bd9Sstevel@tonic-gate 			}
68017c478bd9Sstevel@tonic-gate 			break;
68027c478bd9Sstevel@tonic-gate 		case CKA_EXPONENT_2:
68037c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
68047c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
68057c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
68067c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_EXPO2(obj);
68077c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
68087c478bd9Sstevel@tonic-gate 			} else {
68097c478bd9Sstevel@tonic-gate 				return (B_FALSE);
68107c478bd9Sstevel@tonic-gate 			}
68117c478bd9Sstevel@tonic-gate 			break;
68127c478bd9Sstevel@tonic-gate 		case CKA_COEFFICIENT:
68137c478bd9Sstevel@tonic-gate 			/* only RSA private key has this attribute */
68147c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
68157c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
68167c478bd9Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_COEF(obj);
68177c478bd9Sstevel@tonic-gate 				compare_bigint = B_TRUE;
68187c478bd9Sstevel@tonic-gate 			} else {
68197c478bd9Sstevel@tonic-gate 				return (B_FALSE);
68207c478bd9Sstevel@tonic-gate 			}
68217c478bd9Sstevel@tonic-gate 			break;
68227c478bd9Sstevel@tonic-gate 		case CKA_VALUE_BITS:
68237c478bd9Sstevel@tonic-gate 			/* only Diffie-Hellman private key has this attr */
68247c478bd9Sstevel@tonic-gate 			if ((obj->key_type == CKK_DH) &&
68257c478bd9Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
68267c478bd9Sstevel@tonic-gate 				CK_ULONG val_bits = OBJ_PRI_DH_VAL_BITS(obj);
68277c478bd9Sstevel@tonic-gate 				if (val_bits !=
68287c478bd9Sstevel@tonic-gate 				    *((CK_ULONG *)tmpl_attr->pValue)) {
68297c478bd9Sstevel@tonic-gate 					return (B_FALSE);
68307c478bd9Sstevel@tonic-gate 				}
68317c478bd9Sstevel@tonic-gate 			} else {
68327c478bd9Sstevel@tonic-gate 				return (B_FALSE);
68337c478bd9Sstevel@tonic-gate 			}
68347c478bd9Sstevel@tonic-gate 			break;
68357c478bd9Sstevel@tonic-gate 		case CKA_PRIME:
68367c478bd9Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
68377c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
68387c478bd9Sstevel@tonic-gate 				case CKK_DSA:
68397c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_PRIME(obj);
68407c478bd9Sstevel@tonic-gate 					break;
68417c478bd9Sstevel@tonic-gate 				case CKK_DH:
68427c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH_PRIME(obj);
68437c478bd9Sstevel@tonic-gate 					break;
68447c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
68457c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH942_PRIME(obj);
68467c478bd9Sstevel@tonic-gate 					break;
68477c478bd9Sstevel@tonic-gate 				default:
68487c478bd9Sstevel@tonic-gate 					return (B_FALSE);
68497c478bd9Sstevel@tonic-gate 				}
68507c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
68517c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
68527c478bd9Sstevel@tonic-gate 				case CKK_DSA:
68537c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_PRIME(obj);
68547c478bd9Sstevel@tonic-gate 					break;
68557c478bd9Sstevel@tonic-gate 				case CKK_DH:
68567c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH_PRIME(obj);
68577c478bd9Sstevel@tonic-gate 					break;
68587c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
68597c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH942_PRIME(obj);
68607c478bd9Sstevel@tonic-gate 					break;
68617c478bd9Sstevel@tonic-gate 				default:
68627c478bd9Sstevel@tonic-gate 					return (B_FALSE);
68637c478bd9Sstevel@tonic-gate 				}
68647c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_DOMAIN_PARAMETERS) {
68657c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
68667c478bd9Sstevel@tonic-gate 				case CKK_DSA:
68677c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DSA_PRIME(obj);
68687c478bd9Sstevel@tonic-gate 					break;
68697c478bd9Sstevel@tonic-gate 				case CKK_DH:
68707c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DH_PRIME(obj);
68717c478bd9Sstevel@tonic-gate 					break;
68727c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
68737c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DH942_PRIME(obj);
68747c478bd9Sstevel@tonic-gate 					break;
68757c478bd9Sstevel@tonic-gate 				default:
68767c478bd9Sstevel@tonic-gate 					return (B_FALSE);
68777c478bd9Sstevel@tonic-gate 				}
68787c478bd9Sstevel@tonic-gate 			} else {
68797c478bd9Sstevel@tonic-gate 				return (B_FALSE);
68807c478bd9Sstevel@tonic-gate 			}
68817c478bd9Sstevel@tonic-gate 			compare_bigint = B_TRUE;
68827c478bd9Sstevel@tonic-gate 			break;
68837c478bd9Sstevel@tonic-gate 		case CKA_SUBPRIME:
68847c478bd9Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
68857c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
68867c478bd9Sstevel@tonic-gate 				case CKK_DSA:
68877c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_SUBPRIME(obj);
68887c478bd9Sstevel@tonic-gate 					break;
68897c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
68907c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH942_SUBPRIME(obj);
68917c478bd9Sstevel@tonic-gate 					break;
68927c478bd9Sstevel@tonic-gate 				default:
68937c478bd9Sstevel@tonic-gate 					return (B_FALSE);
68947c478bd9Sstevel@tonic-gate 				}
68957c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
68967c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
68977c478bd9Sstevel@tonic-gate 				case CKK_DSA:
68987c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_SUBPRIME(obj);
68997c478bd9Sstevel@tonic-gate 					break;
69007c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
69017c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH942_SUBPRIME(obj);
69027c478bd9Sstevel@tonic-gate 					break;
69037c478bd9Sstevel@tonic-gate 				default:
69047c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69057c478bd9Sstevel@tonic-gate 				}
69067c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_DOMAIN_PARAMETERS) {
69077c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
69087c478bd9Sstevel@tonic-gate 				case CKK_DSA:
69097c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DSA_SUBPRIME(obj);
69107c478bd9Sstevel@tonic-gate 					break;
69117c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
69127c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DH942_SUBPRIME(obj);
69137c478bd9Sstevel@tonic-gate 					break;
69147c478bd9Sstevel@tonic-gate 				default:
69157c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69167c478bd9Sstevel@tonic-gate 				}
69177c478bd9Sstevel@tonic-gate 			} else {
69187c478bd9Sstevel@tonic-gate 				return (B_FALSE);
69197c478bd9Sstevel@tonic-gate 			}
69207c478bd9Sstevel@tonic-gate 			compare_bigint = B_TRUE;
69217c478bd9Sstevel@tonic-gate 			break;
69227c478bd9Sstevel@tonic-gate 		case CKA_BASE:
69237c478bd9Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
69247c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
69257c478bd9Sstevel@tonic-gate 				case CKK_DSA:
69267c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_BASE(obj);
69277c478bd9Sstevel@tonic-gate 					break;
69287c478bd9Sstevel@tonic-gate 				case CKK_DH:
69297c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH_BASE(obj);
69307c478bd9Sstevel@tonic-gate 					break;
69317c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
69327c478bd9Sstevel@tonic-gate 					bigint = OBJ_PUB_DH942_BASE(obj);
69337c478bd9Sstevel@tonic-gate 					break;
69347c478bd9Sstevel@tonic-gate 				default:
69357c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69367c478bd9Sstevel@tonic-gate 				}
69377c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
69387c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
69397c478bd9Sstevel@tonic-gate 				case CKK_DSA:
69407c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_BASE(obj);
69417c478bd9Sstevel@tonic-gate 					break;
69427c478bd9Sstevel@tonic-gate 				case CKK_DH:
69437c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH_BASE(obj);
69447c478bd9Sstevel@tonic-gate 					break;
69457c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
69467c478bd9Sstevel@tonic-gate 					bigint = OBJ_PRI_DH942_BASE(obj);
69477c478bd9Sstevel@tonic-gate 					break;
69487c478bd9Sstevel@tonic-gate 				default:
69497c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69507c478bd9Sstevel@tonic-gate 				}
69517c478bd9Sstevel@tonic-gate 			} else if (obj->class == CKO_DOMAIN_PARAMETERS) {
69527c478bd9Sstevel@tonic-gate 				switch (obj->key_type) {
69537c478bd9Sstevel@tonic-gate 				case CKK_DSA:
69547c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DSA_BASE(obj);
69557c478bd9Sstevel@tonic-gate 					break;
69567c478bd9Sstevel@tonic-gate 				case CKK_DH:
69577c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DH_BASE(obj);
69587c478bd9Sstevel@tonic-gate 					break;
69597c478bd9Sstevel@tonic-gate 				case CKK_X9_42_DH:
69607c478bd9Sstevel@tonic-gate 					bigint = OBJ_DOM_DH942_BASE(obj);
69617c478bd9Sstevel@tonic-gate 					break;
69627c478bd9Sstevel@tonic-gate 				default:
69637c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69647c478bd9Sstevel@tonic-gate 				}
69657c478bd9Sstevel@tonic-gate 			} else {
69667c478bd9Sstevel@tonic-gate 				return (B_FALSE);
69677c478bd9Sstevel@tonic-gate 			}
69687c478bd9Sstevel@tonic-gate 			compare_bigint = B_TRUE;
69697c478bd9Sstevel@tonic-gate 			break;
69707c478bd9Sstevel@tonic-gate 		case CKA_PRIME_BITS:
69717c478bd9Sstevel@tonic-gate 			if (obj->class == CKO_DOMAIN_PARAMETERS) {
69727c478bd9Sstevel@tonic-gate 				CK_ULONG prime_bits;
69737c478bd9Sstevel@tonic-gate 				if (obj->key_type == CKK_DSA) {
69747c478bd9Sstevel@tonic-gate 					prime_bits =
69757c478bd9Sstevel@tonic-gate 					    OBJ_DOM_DSA_PRIME_BITS(obj);
69767c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_DH) {
69777c478bd9Sstevel@tonic-gate 					prime_bits =
69787c478bd9Sstevel@tonic-gate 					    OBJ_DOM_DH_PRIME_BITS(obj);
69797c478bd9Sstevel@tonic-gate 				} else if (obj->key_type == CKK_X9_42_DH) {
69807c478bd9Sstevel@tonic-gate 					prime_bits =
69817c478bd9Sstevel@tonic-gate 					    OBJ_DOM_DH942_PRIME_BITS(obj);
69827c478bd9Sstevel@tonic-gate 				} else {
69837c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69847c478bd9Sstevel@tonic-gate 				}
69857c478bd9Sstevel@tonic-gate 				if (prime_bits !=
69867c478bd9Sstevel@tonic-gate 				    *((CK_ULONG *)tmpl_attr->pValue)) {
69877c478bd9Sstevel@tonic-gate 					return (B_FALSE);
69887c478bd9Sstevel@tonic-gate 				}
69897c478bd9Sstevel@tonic-gate 			} else {
69907c478bd9Sstevel@tonic-gate 				return (B_FALSE);
69917c478bd9Sstevel@tonic-gate 			}
69927c478bd9Sstevel@tonic-gate 			break;
69937c478bd9Sstevel@tonic-gate 		case CKA_SUBPRIME_BITS:
69947c478bd9Sstevel@tonic-gate 			if ((obj->class == CKO_DOMAIN_PARAMETERS) &&
69957c478bd9Sstevel@tonic-gate 			    (obj->key_type == CKK_X9_42_DH)) {
69967c478bd9Sstevel@tonic-gate 				CK_ULONG subprime_bits =
69977c478bd9Sstevel@tonic-gate 				    OBJ_DOM_DH942_SUBPRIME_BITS(obj);
69987c478bd9Sstevel@tonic-gate 				if (subprime_bits !=
69997c478bd9Sstevel@tonic-gate 				    *((CK_ULONG *)tmpl_attr->pValue)) {
70007c478bd9Sstevel@tonic-gate 					return (B_FALSE);
70017c478bd9Sstevel@tonic-gate 				}
70027c478bd9Sstevel@tonic-gate 			} else {
70037c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70047c478bd9Sstevel@tonic-gate 			}
70057c478bd9Sstevel@tonic-gate 			break;
70067c478bd9Sstevel@tonic-gate 		default:
70077c478bd9Sstevel@tonic-gate 			/*
70087c478bd9Sstevel@tonic-gate 			 * any other attributes are currently not supported.
70097c478bd9Sstevel@tonic-gate 			 * so, it's not possible for them to be in the
70107c478bd9Sstevel@tonic-gate 			 * object
70117c478bd9Sstevel@tonic-gate 			 */
70127c478bd9Sstevel@tonic-gate 			return (B_FALSE);
70137c478bd9Sstevel@tonic-gate 		}
70147c478bd9Sstevel@tonic-gate 		if (compare_boolean) {
70157c478bd9Sstevel@tonic-gate 			CK_BBOOL bval;
70167c478bd9Sstevel@tonic-gate 
70177c478bd9Sstevel@tonic-gate 			if (attr_mask) {
70187c478bd9Sstevel@tonic-gate 				bval = TRUE;
70197c478bd9Sstevel@tonic-gate 			} else {
70207c478bd9Sstevel@tonic-gate 				bval = FALSE;
70217c478bd9Sstevel@tonic-gate 			}
70227c478bd9Sstevel@tonic-gate 			if (bval != *((CK_BBOOL *)tmpl_attr->pValue)) {
70237c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70247c478bd9Sstevel@tonic-gate 			}
70257c478bd9Sstevel@tonic-gate 		} else if (compare_bigint) {
70267c478bd9Sstevel@tonic-gate 			if (bigint == NULL) {
70277c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70287c478bd9Sstevel@tonic-gate 			}
70297c478bd9Sstevel@tonic-gate 			if (tmpl_attr->ulValueLen != bigint->big_value_len) {
70307c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70317c478bd9Sstevel@tonic-gate 			}
70327c478bd9Sstevel@tonic-gate 			if (memcmp(tmpl_attr->pValue, bigint->big_value,
70337c478bd9Sstevel@tonic-gate 			    tmpl_attr->ulValueLen) != 0) {
70347c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70357c478bd9Sstevel@tonic-gate 			}
70367c478bd9Sstevel@tonic-gate 		} else if (compare_attr) {
70377c478bd9Sstevel@tonic-gate 			if (obj_attr == NULL) {
70387c478bd9Sstevel@tonic-gate 				/*
70397c478bd9Sstevel@tonic-gate 				 * The attribute type is valid, and its value
70407c478bd9Sstevel@tonic-gate 				 * has not been initialized in the object. In
70417c478bd9Sstevel@tonic-gate 				 * this case, it only matches the template's
70427c478bd9Sstevel@tonic-gate 				 * attribute if the template's value length
70437c478bd9Sstevel@tonic-gate 				 * is 0.
70447c478bd9Sstevel@tonic-gate 				 */
70457c478bd9Sstevel@tonic-gate 				if (tmpl_attr->ulValueLen != 0)
70467c478bd9Sstevel@tonic-gate 					return (B_FALSE);
70477c478bd9Sstevel@tonic-gate 			} else {
70487c478bd9Sstevel@tonic-gate 				if (tmpl_attr->ulValueLen !=
70497c478bd9Sstevel@tonic-gate 				    obj_attr->ulValueLen) {
70507c478bd9Sstevel@tonic-gate 					return (B_FALSE);
70517c478bd9Sstevel@tonic-gate 				}
70527c478bd9Sstevel@tonic-gate 				if (memcmp(tmpl_attr->pValue, obj_attr->pValue,
70537c478bd9Sstevel@tonic-gate 				    tmpl_attr->ulValueLen) != 0) {
70547c478bd9Sstevel@tonic-gate 					return (B_FALSE);
70557c478bd9Sstevel@tonic-gate 				}
70567c478bd9Sstevel@tonic-gate 			}
70577c478bd9Sstevel@tonic-gate 		} else if (compare_cert_val) {
70587c478bd9Sstevel@tonic-gate 			if (cert_attr == NULL) {
70597c478bd9Sstevel@tonic-gate 				/* specific attribute not found */
70607c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70617c478bd9Sstevel@tonic-gate 			}
70627c478bd9Sstevel@tonic-gate 			if (tmpl_attr->ulValueLen != cert_attr->length) {
70637c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70647c478bd9Sstevel@tonic-gate 			}
70657c478bd9Sstevel@tonic-gate 			if (memcmp(tmpl_attr->pValue, cert_attr->value,
70667c478bd9Sstevel@tonic-gate 			    tmpl_attr->ulValueLen) != 0) {
70677c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70687c478bd9Sstevel@tonic-gate 			}
70697c478bd9Sstevel@tonic-gate 		} else if (compare_cert_type) {
70707c478bd9Sstevel@tonic-gate 			if (memcmp(tmpl_attr->pValue, &(obj->cert_type),
70717c478bd9Sstevel@tonic-gate 			    tmpl_attr->ulValueLen) != 0) {
70727c478bd9Sstevel@tonic-gate 				return (B_FALSE);
70737c478bd9Sstevel@tonic-gate 			}
70747c478bd9Sstevel@tonic-gate 		}
70757c478bd9Sstevel@tonic-gate 	}
70767c478bd9Sstevel@tonic-gate 	return (B_TRUE);
70777c478bd9Sstevel@tonic-gate }
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate CK_ATTRIBUTE_PTR
70807c478bd9Sstevel@tonic-gate get_extra_attr(CK_ATTRIBUTE_TYPE type, soft_object_t *obj)
70817c478bd9Sstevel@tonic-gate {
70827c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR tmp;
70837c478bd9Sstevel@tonic-gate 
70847c478bd9Sstevel@tonic-gate 	tmp = obj->extra_attrlistp;
70857c478bd9Sstevel@tonic-gate 	while (tmp != NULL) {
70867c478bd9Sstevel@tonic-gate 		if (tmp->attr.type == type) {
70877c478bd9Sstevel@tonic-gate 			return (&(tmp->attr));
70887c478bd9Sstevel@tonic-gate 		}
70897c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
70907c478bd9Sstevel@tonic-gate 	}
70917c478bd9Sstevel@tonic-gate 	/* if get there, the specified attribute is not found */
70927c478bd9Sstevel@tonic-gate 	return (NULL);
70937c478bd9Sstevel@tonic-gate }
7094