10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54219Smcpowers  * Common Development and Distribution License (the "License").
64219Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
227188Smcpowers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <security/cryptoki.h>
297188Smcpowers #include <sys/crypto/common.h>
300Sstevel@tonic-gate #include <aes_impl.h>
31676Sizick #include <blowfish_impl.h>
320Sstevel@tonic-gate #include <arcfour.h>
330Sstevel@tonic-gate #include <des_impl.h>
340Sstevel@tonic-gate #include "kernelGlobal.h"
350Sstevel@tonic-gate #include "kernelObject.h"
360Sstevel@tonic-gate #include "kernelSession.h"
370Sstevel@tonic-gate #include "kernelSlot.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * This attribute table is used by the kernel_lookup_attr()
420Sstevel@tonic-gate  * to validate the attributes.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate CK_ATTRIBUTE_TYPE attr_map[] = {
450Sstevel@tonic-gate 	CKA_PRIVATE,
460Sstevel@tonic-gate 	CKA_LABEL,
470Sstevel@tonic-gate 	CKA_APPLICATION,
480Sstevel@tonic-gate 	CKA_OBJECT_ID,
490Sstevel@tonic-gate 	CKA_CERTIFICATE_TYPE,
500Sstevel@tonic-gate 	CKA_ISSUER,
510Sstevel@tonic-gate 	CKA_SERIAL_NUMBER,
520Sstevel@tonic-gate 	CKA_AC_ISSUER,
530Sstevel@tonic-gate 	CKA_OWNER,
540Sstevel@tonic-gate 	CKA_ATTR_TYPES,
550Sstevel@tonic-gate 	CKA_SUBJECT,
560Sstevel@tonic-gate 	CKA_ID,
570Sstevel@tonic-gate 	CKA_SENSITIVE,
580Sstevel@tonic-gate 	CKA_START_DATE,
590Sstevel@tonic-gate 	CKA_END_DATE,
600Sstevel@tonic-gate 	CKA_MODULUS,
610Sstevel@tonic-gate 	CKA_MODULUS_BITS,
620Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
630Sstevel@tonic-gate 	CKA_PRIVATE_EXPONENT,
640Sstevel@tonic-gate 	CKA_PRIME_1,
650Sstevel@tonic-gate 	CKA_PRIME_2,
660Sstevel@tonic-gate 	CKA_EXPONENT_1,
670Sstevel@tonic-gate 	CKA_EXPONENT_2,
680Sstevel@tonic-gate 	CKA_COEFFICIENT,
690Sstevel@tonic-gate 	CKA_PRIME,
700Sstevel@tonic-gate 	CKA_SUBPRIME,
710Sstevel@tonic-gate 	CKA_BASE,
720Sstevel@tonic-gate 	CKA_EXTRACTABLE,
730Sstevel@tonic-gate 	CKA_LOCAL,
740Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
750Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE,
760Sstevel@tonic-gate 	CKA_MODIFIABLE,
770Sstevel@tonic-gate 	CKA_ECDSA_PARAMS,
780Sstevel@tonic-gate 	CKA_EC_POINT,
790Sstevel@tonic-gate 	CKA_SECONDARY_AUTH,
800Sstevel@tonic-gate 	CKA_AUTH_PIN_FLAGS,
810Sstevel@tonic-gate 	CKA_HW_FEATURE_TYPE,
820Sstevel@tonic-gate 	CKA_RESET_ON_INIT,
830Sstevel@tonic-gate 	CKA_HAS_RESET
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * attributes that exists only in public key objects
880Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
890Sstevel@tonic-gate  *       other object classes, but they are also listed
900Sstevel@tonic-gate  *       because not all object have them.
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate CK_ATTRIBUTE_TYPE PUB_KEY_ATTRS[] =
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	CKA_SUBJECT,
950Sstevel@tonic-gate 	CKA_ENCRYPT,
960Sstevel@tonic-gate 	CKA_WRAP,
970Sstevel@tonic-gate 	CKA_VERIFY,
980Sstevel@tonic-gate 	CKA_VERIFY_RECOVER,
990Sstevel@tonic-gate 	CKA_MODULUS,
1000Sstevel@tonic-gate 	CKA_MODULUS_BITS,
1010Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
1020Sstevel@tonic-gate 	CKA_PRIME,
1030Sstevel@tonic-gate 	CKA_SUBPRIME,
1040Sstevel@tonic-gate 	CKA_BASE,
1050Sstevel@tonic-gate 	CKA_TRUSTED,
1060Sstevel@tonic-gate 	CKA_ECDSA_PARAMS,
1070Sstevel@tonic-gate 	CKA_EC_PARAMS,
1080Sstevel@tonic-gate 	CKA_EC_POINT
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * attributes that exists only in private key objects
1130Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1140Sstevel@tonic-gate  *       other object classes, but they are also listed
1150Sstevel@tonic-gate  *       because not all object have them.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate CK_ATTRIBUTE_TYPE PRIV_KEY_ATTRS[] =
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	CKA_DECRYPT,
1200Sstevel@tonic-gate 	CKA_UNWRAP,
1210Sstevel@tonic-gate 	CKA_SIGN,
1220Sstevel@tonic-gate 	CKA_SIGN_RECOVER,
1230Sstevel@tonic-gate 	CKA_MODULUS,
1240Sstevel@tonic-gate 	CKA_PUBLIC_EXPONENT,
1250Sstevel@tonic-gate 	CKA_PRIVATE_EXPONENT,
1260Sstevel@tonic-gate 	CKA_PRIME,
1270Sstevel@tonic-gate 	CKA_SUBPRIME,
1280Sstevel@tonic-gate 	CKA_BASE,
1290Sstevel@tonic-gate 	CKA_PRIME_1,
1300Sstevel@tonic-gate 	CKA_PRIME_2,
1310Sstevel@tonic-gate 	CKA_EXPONENT_1,
1320Sstevel@tonic-gate 	CKA_EXPONENT_2,
1330Sstevel@tonic-gate 	CKA_COEFFICIENT,
1340Sstevel@tonic-gate 	CKA_VALUE_BITS,
1350Sstevel@tonic-gate 	CKA_SUBJECT,
1360Sstevel@tonic-gate 	CKA_SENSITIVE,
1370Sstevel@tonic-gate 	CKA_EXTRACTABLE,
1380Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
1390Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE,
1400Sstevel@tonic-gate 	CKA_ECDSA_PARAMS,
1410Sstevel@tonic-gate 	CKA_EC_PARAMS
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * attributes that exists only in secret key objects
1460Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1470Sstevel@tonic-gate  *       other object classes, but they are also listed
1480Sstevel@tonic-gate  *       because not all object have them.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate CK_ATTRIBUTE_TYPE SECRET_KEY_ATTRS[] =
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	CKA_VALUE_LEN,
1530Sstevel@tonic-gate 	CKA_ENCRYPT,
1540Sstevel@tonic-gate 	CKA_DECRYPT,
1550Sstevel@tonic-gate 	CKA_WRAP,
1560Sstevel@tonic-gate 	CKA_UNWRAP,
1570Sstevel@tonic-gate 	CKA_SIGN,
1580Sstevel@tonic-gate 	CKA_VERIFY,
1590Sstevel@tonic-gate 	CKA_SENSITIVE,
1600Sstevel@tonic-gate 	CKA_EXTRACTABLE,
1610Sstevel@tonic-gate 	CKA_NEVER_EXTRACTABLE,
1620Sstevel@tonic-gate 	CKA_ALWAYS_SENSITIVE
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * attributes that exists only in domain parameter objects
1670Sstevel@tonic-gate  * Note: some attributes may also exist in one or two
1680Sstevel@tonic-gate  *       other object classes, but they are also listed
1690Sstevel@tonic-gate  *       because not all object have them.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate CK_ATTRIBUTE_TYPE DOMAIN_ATTRS[] =
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	CKA_PRIME,
1740Sstevel@tonic-gate 	CKA_SUBPRIME,
1750Sstevel@tonic-gate 	CKA_BASE,
1760Sstevel@tonic-gate 	CKA_PRIME_BITS,
1770Sstevel@tonic-gate 	CKA_SUBPRIME_BITS,
1780Sstevel@tonic-gate 	CKA_SUB_PRIME_BITS
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * attributes that exists only in hardware feature objects
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate CK_ATTRIBUTE_TYPE HARDWARE_ATTRS[] =
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	CKA_HW_FEATURE_TYPE,
1870Sstevel@tonic-gate 	CKA_RESET_ON_INIT,
1880Sstevel@tonic-gate 	CKA_HAS_RESET
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * attributes that exists only in certificate objects
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate CK_ATTRIBUTE_TYPE CERT_ATTRS[] =
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	CKA_CERTIFICATE_TYPE,
1970Sstevel@tonic-gate 	CKA_SUBJECT,
1980Sstevel@tonic-gate 	CKA_ID,
1990Sstevel@tonic-gate 	CKA_ISSUER,
2000Sstevel@tonic-gate 	CKA_AC_ISSUER,
2010Sstevel@tonic-gate 	CKA_SERIAL_NUMBER,
2020Sstevel@tonic-gate 	CKA_OWNER,
2030Sstevel@tonic-gate 	CKA_ATTR_TYPES
2040Sstevel@tonic-gate };
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * Validate the attribute by using binary search algorithm.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate CK_RV
kernel_lookup_attr(CK_ATTRIBUTE_TYPE type)2110Sstevel@tonic-gate kernel_lookup_attr(CK_ATTRIBUTE_TYPE type)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	size_t lower, middle, upper;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	lower = 0;
2170Sstevel@tonic-gate 	upper = (sizeof (attr_map) / sizeof (CK_ATTRIBUTE_TYPE)) - 1;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	while (lower <= upper) {
2200Sstevel@tonic-gate 		/* Always starts from middle. */
2210Sstevel@tonic-gate 		middle = (lower + upper) / 2;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		if (type > attr_map[middle]) {
2240Sstevel@tonic-gate 			/* Adjust the lower bound to upper half. */
2250Sstevel@tonic-gate 			lower = middle + 1;
2260Sstevel@tonic-gate 			continue;
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		if (type == attr_map[middle]) {
2300Sstevel@tonic-gate 			/* Found it. */
2310Sstevel@tonic-gate 			return (CKR_OK);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		if (type < attr_map[middle]) {
2350Sstevel@tonic-gate 			/* Adjust the upper bound to lower half. */
2360Sstevel@tonic-gate 			upper = middle - 1;
2370Sstevel@tonic-gate 			continue;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* Failed to find the matching attribute from the attribute table. */
2420Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * Validate the attribute by using the following search algorithm:
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * 1) Search for the most frequently used attributes first.
2500Sstevel@tonic-gate  * 2) If not found, search for the usage-purpose attributes - these
2510Sstevel@tonic-gate  *    attributes have dense set of values, therefore compiler will
2520Sstevel@tonic-gate  *    optimize it with a branch table and branch to the appropriate
2530Sstevel@tonic-gate  *    case.
2540Sstevel@tonic-gate  * 3) If still not found, use binary search for the rest of the
2550Sstevel@tonic-gate  *    attributes in the attr_map[] table.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate CK_RV
kernel_validate_attr(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,CK_OBJECT_CLASS * class)2580Sstevel@tonic-gate kernel_validate_attr(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
2590Sstevel@tonic-gate 	CK_OBJECT_CLASS *class)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	CK_ULONG i;
2630Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
2660Sstevel@tonic-gate 		/* First tier search */
2670Sstevel@tonic-gate 		switch (template[i].type) {
2680Sstevel@tonic-gate 		case CKA_CLASS:
2690Sstevel@tonic-gate 			*class = *((CK_OBJECT_CLASS*)template[i].pValue);
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 		case CKA_TOKEN:
2720Sstevel@tonic-gate 			break;
2730Sstevel@tonic-gate 		case CKA_KEY_TYPE:
2740Sstevel@tonic-gate 			break;
2750Sstevel@tonic-gate 		case CKA_VALUE:
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 		case CKA_VALUE_LEN:
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate 		case CKA_VALUE_BITS:
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 		default:
2820Sstevel@tonic-gate 			/* Second tier search */
2830Sstevel@tonic-gate 			switch (template[i].type) {
2840Sstevel@tonic-gate 			case CKA_ENCRYPT:
2850Sstevel@tonic-gate 				break;
2860Sstevel@tonic-gate 			case CKA_DECRYPT:
2870Sstevel@tonic-gate 				break;
2880Sstevel@tonic-gate 			case CKA_WRAP:
2890Sstevel@tonic-gate 				break;
2900Sstevel@tonic-gate 			case CKA_UNWRAP:
2910Sstevel@tonic-gate 				break;
2920Sstevel@tonic-gate 			case CKA_SIGN:
2930Sstevel@tonic-gate 				break;
2940Sstevel@tonic-gate 			case CKA_SIGN_RECOVER:
2950Sstevel@tonic-gate 				break;
2960Sstevel@tonic-gate 			case CKA_VERIFY:
2970Sstevel@tonic-gate 				break;
2980Sstevel@tonic-gate 			case CKA_VERIFY_RECOVER:
2990Sstevel@tonic-gate 				break;
3000Sstevel@tonic-gate 			case CKA_DERIVE:
3010Sstevel@tonic-gate 				break;
3020Sstevel@tonic-gate 			default:
3030Sstevel@tonic-gate 				/* Third tier search */
3040Sstevel@tonic-gate 				rv = kernel_lookup_attr(template[i].type);
3050Sstevel@tonic-gate 				if (rv != CKR_OK)
3060Sstevel@tonic-gate 					return (rv);
3070Sstevel@tonic-gate 				break;
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	return (rv);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Clean up and release all the storage in the extra attribute list
3180Sstevel@tonic-gate  * of an object.
3190Sstevel@tonic-gate  */
3200Sstevel@tonic-gate void
kernel_cleanup_extra_attr(kernel_object_t * object_p)3210Sstevel@tonic-gate kernel_cleanup_extra_attr(kernel_object_t *object_p)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
3250Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR tmp;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
3280Sstevel@tonic-gate 	while (extra_attr) {
3290Sstevel@tonic-gate 		tmp = extra_attr->next;
3300Sstevel@tonic-gate 		if (extra_attr->attr.pValue)
3310Sstevel@tonic-gate 			/*
3320Sstevel@tonic-gate 			 * All extra attributes in the extra attribute
3330Sstevel@tonic-gate 			 * list have pValue points to the value of the
3340Sstevel@tonic-gate 			 * attribute (with simple byte array type).
3350Sstevel@tonic-gate 			 * Free the storage for the value of the attribute.
3360Sstevel@tonic-gate 			 */
3370Sstevel@tonic-gate 			free(extra_attr->attr.pValue);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		/* Free the storage for the attribute_info struct. */
3400Sstevel@tonic-gate 		free(extra_attr);
3410Sstevel@tonic-gate 		extra_attr = tmp;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	object_p->extra_attrlistp = NULL;
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * Create the attribute_info struct to hold the object's attribute,
3500Sstevel@tonic-gate  * and add it to the extra attribute list of an object.
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate CK_RV
kernel_add_extra_attr(CK_ATTRIBUTE_PTR template,kernel_object_t * object_p)3530Sstevel@tonic-gate kernel_add_extra_attr(CK_ATTRIBUTE_PTR template, kernel_object_t *object_p)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR attrp;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/* Allocate the storage for the attribute_info struct. */
3590Sstevel@tonic-gate 	attrp = calloc(1, sizeof (attribute_info_t));
3600Sstevel@tonic-gate 	if (attrp == NULL) {
3610Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/* Set up attribute_info struct. */
3650Sstevel@tonic-gate 	attrp->attr.type = template->type;
3660Sstevel@tonic-gate 	attrp->attr.ulValueLen = template->ulValueLen;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
3690Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
3700Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
3710Sstevel@tonic-gate 		attrp->attr.pValue = malloc(template->ulValueLen);
3720Sstevel@tonic-gate 		if (attrp->attr.pValue == NULL) {
3730Sstevel@tonic-gate 			free(attrp);
3740Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		(void) memcpy(attrp->attr.pValue, template->pValue,
3780Sstevel@tonic-gate 		    template->ulValueLen);
3790Sstevel@tonic-gate 	} else {
3800Sstevel@tonic-gate 		attrp->attr.pValue = NULL;
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* Insert the new attribute in front of extra attribute list. */
3840Sstevel@tonic-gate 	if (object_p->extra_attrlistp == NULL) {
3850Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
3860Sstevel@tonic-gate 		attrp->next = NULL;
3870Sstevel@tonic-gate 	} else {
3880Sstevel@tonic-gate 		attrp->next = object_p->extra_attrlistp;
3890Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (CKR_OK);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * Copy the attribute_info struct from the old object to a new attribute_info
3980Sstevel@tonic-gate  * struct, and add that new struct to the extra attribute list of the new
3990Sstevel@tonic-gate  * object.
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate CK_RV
kernel_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp,kernel_object_t * object_p)4020Sstevel@tonic-gate kernel_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp,
4030Sstevel@tonic-gate     kernel_object_t *object_p)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR attrp;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/* Allocate attribute_info struct. */
4080Sstevel@tonic-gate 	attrp = calloc(1, sizeof (attribute_info_t));
4090Sstevel@tonic-gate 	if (attrp == NULL) {
4100Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	attrp->attr.type = old_attrp->attr.type;
4140Sstevel@tonic-gate 	attrp->attr.ulValueLen = old_attrp->attr.ulValueLen;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if ((old_attrp->attr.pValue != NULL) &&
4177188Smcpowers 	    (old_attrp->attr.ulValueLen > 0)) {
4180Sstevel@tonic-gate 		attrp->attr.pValue = malloc(old_attrp->attr.ulValueLen);
4190Sstevel@tonic-gate 		if (attrp->attr.pValue == NULL) {
4200Sstevel@tonic-gate 			free(attrp);
4210Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		(void) memcpy(attrp->attr.pValue, old_attrp->attr.pValue,
4250Sstevel@tonic-gate 		    old_attrp->attr.ulValueLen);
4260Sstevel@tonic-gate 	} else {
4270Sstevel@tonic-gate 		attrp->attr.pValue = NULL;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/* Insert the new attribute in front of extra attribute list */
4310Sstevel@tonic-gate 	if (object_p->extra_attrlistp == NULL) {
4320Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
4330Sstevel@tonic-gate 		attrp->next = NULL;
4340Sstevel@tonic-gate 	} else {
4350Sstevel@tonic-gate 		attrp->next = object_p->extra_attrlistp;
4360Sstevel@tonic-gate 		object_p->extra_attrlistp = attrp;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	return (CKR_OK);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Get the attribute triple from the extra attribute list in the object
4450Sstevel@tonic-gate  * (if the specified attribute type is found), and copy it to a template.
4460Sstevel@tonic-gate  * Note the type of the attribute to be copied is specified by the template,
4470Sstevel@tonic-gate  * and the storage is pre-allocated for the atrribute value in the template
4480Sstevel@tonic-gate  * for doing the copy.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate CK_RV
get_extra_attr_from_object(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)4510Sstevel@tonic-gate get_extra_attr_from_object(kernel_object_t *object_p, CK_ATTRIBUTE_PTR template)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
4550Sstevel@tonic-gate 	CK_ATTRIBUTE_TYPE type = template->type;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	while (extra_attr) {
4600Sstevel@tonic-gate 		if (type == extra_attr->attr.type) {
4610Sstevel@tonic-gate 			/* Found it. */
4620Sstevel@tonic-gate 			break;
4630Sstevel@tonic-gate 		} else {
4640Sstevel@tonic-gate 			/* Does not match, try next one. */
4650Sstevel@tonic-gate 			extra_attr = extra_attr->next;
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (extra_attr == NULL) {
4700Sstevel@tonic-gate 		/* A valid but un-initialized attribute. */
4710Sstevel@tonic-gate 		template->ulValueLen = 0;
4720Sstevel@tonic-gate 		return (CKR_OK);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * We found the attribute in the extra attribute list.
4770Sstevel@tonic-gate 	 */
4780Sstevel@tonic-gate 	if (template->pValue == NULL) {
4790Sstevel@tonic-gate 		template->ulValueLen = extra_attr->attr.ulValueLen;
4800Sstevel@tonic-gate 		return (CKR_OK);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if (template->ulValueLen >= extra_attr->attr.ulValueLen) {
4840Sstevel@tonic-gate 		/*
4850Sstevel@tonic-gate 		 * The buffer provided by the application is large
4860Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
4870Sstevel@tonic-gate 		 */
4880Sstevel@tonic-gate 		(void) memcpy(template->pValue, extra_attr->attr.pValue,
4890Sstevel@tonic-gate 		    extra_attr->attr.ulValueLen);
4900Sstevel@tonic-gate 		template->ulValueLen = extra_attr->attr.ulValueLen;
4910Sstevel@tonic-gate 		return (CKR_OK);
4920Sstevel@tonic-gate 	} else {
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * The buffer provided by the application does
4950Sstevel@tonic-gate 		 * not have enough space to hold the value.
4960Sstevel@tonic-gate 		 */
4970Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
4980Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * Modify the attribute triple in the extra attribute list of the object
5050Sstevel@tonic-gate  * if the specified attribute type is found. Otherwise, just add it to
5060Sstevel@tonic-gate  * list.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate CK_RV
set_extra_attr_to_object(kernel_object_t * object_p,CK_ATTRIBUTE_TYPE type,CK_ATTRIBUTE_PTR template)5090Sstevel@tonic-gate set_extra_attr_to_object(kernel_object_t *object_p, CK_ATTRIBUTE_TYPE type,
5100Sstevel@tonic-gate 	CK_ATTRIBUTE_PTR template)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR extra_attr;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	extra_attr = object_p->extra_attrlistp;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	while (extra_attr) {
5180Sstevel@tonic-gate 		if (type == extra_attr->attr.type) {
5190Sstevel@tonic-gate 			/* Found it. */
5200Sstevel@tonic-gate 			break;
5210Sstevel@tonic-gate 		} else {
5220Sstevel@tonic-gate 			/* Does not match, try next one. */
5230Sstevel@tonic-gate 			extra_attr = extra_attr->next;
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (extra_attr == NULL) {
5280Sstevel@tonic-gate 		/*
5290Sstevel@tonic-gate 		 * This attribute is a new one, go ahead adding it to
5300Sstevel@tonic-gate 		 * the extra attribute list.
5310Sstevel@tonic-gate 		 */
5320Sstevel@tonic-gate 		return (kernel_add_extra_attr(template, object_p));
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/* We found the attribute in the extra attribute list. */
5360Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
5370Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
5380Sstevel@tonic-gate 		if (template->ulValueLen > extra_attr->attr.ulValueLen) {
5390Sstevel@tonic-gate 			/* The old buffer is too small to hold the new value. */
5400Sstevel@tonic-gate 			if (extra_attr->attr.pValue != NULL)
5410Sstevel@tonic-gate 				/* Free storage for the old attribute value. */
5420Sstevel@tonic-gate 				free(extra_attr->attr.pValue);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 			/* Allocate storage for the new attribute value. */
5450Sstevel@tonic-gate 			extra_attr->attr.pValue = malloc(template->ulValueLen);
5460Sstevel@tonic-gate 			if (extra_attr->attr.pValue == NULL) {
5470Sstevel@tonic-gate 				return (CKR_HOST_MEMORY);
5480Sstevel@tonic-gate 			}
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/* Replace the attribute with new value. */
5520Sstevel@tonic-gate 		extra_attr->attr.ulValueLen = template->ulValueLen;
5530Sstevel@tonic-gate 		(void) memcpy(extra_attr->attr.pValue, template->pValue,
5540Sstevel@tonic-gate 		    template->ulValueLen);
5550Sstevel@tonic-gate 	} else {
5560Sstevel@tonic-gate 		extra_attr->attr.pValue = NULL;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	return (CKR_OK);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate  * Copy the big integer attribute value from template to a biginteger_t struct.
5650Sstevel@tonic-gate  */
5660Sstevel@tonic-gate CK_RV
get_bigint_attr_from_template(biginteger_t * big,CK_ATTRIBUTE_PTR template)5670Sstevel@tonic-gate get_bigint_attr_from_template(biginteger_t *big, CK_ATTRIBUTE_PTR template)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if ((template->pValue != NULL) &&
5710Sstevel@tonic-gate 	    (template->ulValueLen > 0)) {
5720Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
5730Sstevel@tonic-gate 		big->big_value = malloc(template->ulValueLen);
5740Sstevel@tonic-gate 		if (big->big_value == NULL) {
5750Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		(void) memcpy(big->big_value, template->pValue,
5790Sstevel@tonic-gate 		    template->ulValueLen);
5800Sstevel@tonic-gate 		big->big_value_len = template->ulValueLen;
5810Sstevel@tonic-gate 	} else {
5820Sstevel@tonic-gate 		big->big_value = NULL;
5830Sstevel@tonic-gate 		big->big_value_len = 0;
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	return (CKR_OK);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * Copy the big integer attribute value from a biginteger_t struct in the
5920Sstevel@tonic-gate  * object to a template.
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate CK_RV
get_bigint_attr_from_object(biginteger_t * big,CK_ATTRIBUTE_PTR template)5950Sstevel@tonic-gate get_bigint_attr_from_object(biginteger_t *big, CK_ATTRIBUTE_PTR template)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (template->pValue == NULL) {
5990Sstevel@tonic-gate 		template->ulValueLen = big->big_value_len;
6000Sstevel@tonic-gate 		return (CKR_OK);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (big->big_value == NULL) {
6040Sstevel@tonic-gate 		template->ulValueLen = 0;
6050Sstevel@tonic-gate 		return (CKR_OK);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (template->ulValueLen >= big->big_value_len) {
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * The buffer provided by the application is large
6110Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
6120Sstevel@tonic-gate 		 */
6130Sstevel@tonic-gate 		(void) memcpy(template->pValue, big->big_value,
6140Sstevel@tonic-gate 		    big->big_value_len);
6150Sstevel@tonic-gate 		template->ulValueLen = big->big_value_len;
6160Sstevel@tonic-gate 		return (CKR_OK);
6170Sstevel@tonic-gate 	} else {
6180Sstevel@tonic-gate 		/*
6190Sstevel@tonic-gate 		 * The buffer provided by the application does
6200Sstevel@tonic-gate 		 * not have enough space to hold the value.
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
6230Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate  * Copy the boolean data type attribute value from an object for the
6300Sstevel@tonic-gate  * specified attribute to the template.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate CK_RV
get_bool_attr_from_object(kernel_object_t * object_p,CK_ULONG bool_flag,CK_ATTRIBUTE_PTR template)6330Sstevel@tonic-gate get_bool_attr_from_object(kernel_object_t *object_p, CK_ULONG bool_flag,
6340Sstevel@tonic-gate 	CK_ATTRIBUTE_PTR template)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (template->pValue == NULL) {
6380Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
6390Sstevel@tonic-gate 		return (CKR_OK);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (template->ulValueLen >= sizeof (CK_BBOOL)) {
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * The buffer provided by the application is large
6450Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		if (object_p->bool_attr_mask & bool_flag) {
6480Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
6490Sstevel@tonic-gate 		} else {
6500Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
6540Sstevel@tonic-gate 		return (CKR_OK);
6550Sstevel@tonic-gate 	} else {
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * The buffer provided by the application does
6580Sstevel@tonic-gate 		 * not have enough space to hold the value.
6590Sstevel@tonic-gate 		 */
6600Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
6610Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /*
6660Sstevel@tonic-gate  * Set the boolean data type attribute value in the object.
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate CK_RV
set_bool_attr_to_object(kernel_object_t * object_p,CK_ULONG bool_flag,CK_ATTRIBUTE_PTR template)6690Sstevel@tonic-gate set_bool_attr_to_object(kernel_object_t *object_p, CK_ULONG bool_flag,
6700Sstevel@tonic-gate 	CK_ATTRIBUTE_PTR template)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (*(CK_BBOOL *)template->pValue)
6740Sstevel@tonic-gate 		object_p->bool_attr_mask |= bool_flag;
6750Sstevel@tonic-gate 	else
6760Sstevel@tonic-gate 		object_p->bool_attr_mask &= ~bool_flag;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	return (CKR_OK);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate  * Copy the CK_ULONG data type attribute value from an object to the
6840Sstevel@tonic-gate  * template.
6850Sstevel@tonic-gate  */
6860Sstevel@tonic-gate CK_RV
get_ulong_attr_from_object(CK_ULONG value,CK_ATTRIBUTE_PTR template)6870Sstevel@tonic-gate get_ulong_attr_from_object(CK_ULONG value, CK_ATTRIBUTE_PTR template)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if (template->pValue == NULL) {
6910Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_ULONG);
6920Sstevel@tonic-gate 		return (CKR_OK);
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (template->ulValueLen >= sizeof (CK_ULONG)) {
6960Sstevel@tonic-gate 		/*
6970Sstevel@tonic-gate 		 * The buffer provided by the application is large
6980Sstevel@tonic-gate 		 * enough to hold the value of the attribute.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		*(CK_ULONG_PTR)template->pValue = value;
7010Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_ULONG);
7020Sstevel@tonic-gate 		return (CKR_OK);
7030Sstevel@tonic-gate 	} else {
7040Sstevel@tonic-gate 		/*
7050Sstevel@tonic-gate 		 * The buffer provided by the application does
7060Sstevel@tonic-gate 		 * not have enough space to hold the value.
7070Sstevel@tonic-gate 		 */
7080Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
7090Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate  * Copy the CK_ULONG data type attribute value from a template to the
7160Sstevel@tonic-gate  * object.
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate void
get_ulong_attr_from_template(CK_ULONG * value,CK_ATTRIBUTE_PTR template)7190Sstevel@tonic-gate get_ulong_attr_from_template(CK_ULONG *value, CK_ATTRIBUTE_PTR template)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if (template->pValue != NULL) {
7230Sstevel@tonic-gate 		*value = *(CK_ULONG_PTR)template->pValue;
7240Sstevel@tonic-gate 	} else {
7250Sstevel@tonic-gate 		*value = 0;
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate  * Copy the big integer attribute value from source's biginteger_t to
7320Sstevel@tonic-gate  * destination's biginteger_t.
7330Sstevel@tonic-gate  */
7340Sstevel@tonic-gate void
copy_bigint_attr(biginteger_t * src,biginteger_t * dst)7350Sstevel@tonic-gate copy_bigint_attr(biginteger_t *src, biginteger_t *dst)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	if ((src->big_value != NULL) &&
7390Sstevel@tonic-gate 	    (src->big_value_len > 0)) {
7400Sstevel@tonic-gate 		/*
7410Sstevel@tonic-gate 		 * To do the copy, just have dst's big_value points
7420Sstevel@tonic-gate 		 * to src's.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		dst->big_value = src->big_value;
7450Sstevel@tonic-gate 		dst->big_value_len = src->big_value_len;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * After the copy, nullify the src's big_value pointer.
7490Sstevel@tonic-gate 		 * It prevents any double freeing the value.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		src->big_value = NULL;
7520Sstevel@tonic-gate 		src->big_value_len = 0;
7530Sstevel@tonic-gate 	} else {
7540Sstevel@tonic-gate 		dst->big_value = NULL;
7550Sstevel@tonic-gate 		dst->big_value_len = 0;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate CK_RV
get_string_from_template(CK_ATTRIBUTE_PTR dest,CK_ATTRIBUTE_PTR src)7620Sstevel@tonic-gate get_string_from_template(CK_ATTRIBUTE_PTR dest, CK_ATTRIBUTE_PTR src)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	if ((src->pValue != NULL) &&
7650Sstevel@tonic-gate 	    (src->ulValueLen > 0)) {
7660Sstevel@tonic-gate 		/* Allocate storage for the value of the attribute. */
7670Sstevel@tonic-gate 		dest->pValue = malloc(src->ulValueLen);
7680Sstevel@tonic-gate 		if (dest->pValue == NULL) {
7690Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		(void) memcpy(dest->pValue, src->pValue,
7730Sstevel@tonic-gate 		    src->ulValueLen);
7740Sstevel@tonic-gate 		dest->ulValueLen = src->ulValueLen;
7750Sstevel@tonic-gate 		dest->type = src->type;
7760Sstevel@tonic-gate 	} else {
7770Sstevel@tonic-gate 		dest->pValue = NULL;
7780Sstevel@tonic-gate 		dest->ulValueLen = 0;
7790Sstevel@tonic-gate 		dest->type = src->type;
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	return (CKR_OK);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate void
string_attr_cleanup(CK_ATTRIBUTE_PTR template)7870Sstevel@tonic-gate string_attr_cleanup(CK_ATTRIBUTE_PTR template)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (template->pValue) {
7910Sstevel@tonic-gate 		free(template->pValue);
7920Sstevel@tonic-gate 		template->pValue = NULL;
7930Sstevel@tonic-gate 		template->ulValueLen = 0;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate  * Release the storage allocated for object attribute with big integer
7990Sstevel@tonic-gate  * value.
8000Sstevel@tonic-gate  */
8010Sstevel@tonic-gate void
bigint_attr_cleanup(biginteger_t * big)8020Sstevel@tonic-gate bigint_attr_cleanup(biginteger_t *big)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (big == NULL)
8060Sstevel@tonic-gate 		return;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (big->big_value) {
8090Sstevel@tonic-gate 		(void) memset(big->big_value, 0, big->big_value_len);
8100Sstevel@tonic-gate 		free(big->big_value);
8110Sstevel@tonic-gate 		big->big_value = NULL;
8120Sstevel@tonic-gate 		big->big_value_len = 0;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate /*
8180Sstevel@tonic-gate  * Clean up and release all the storage allocated to hold the big integer
8190Sstevel@tonic-gate  * attributes associated with the type (i.e. class) of the object. Also,
8200Sstevel@tonic-gate  * release the storage allocated to the type of the object.
8210Sstevel@tonic-gate  */
8220Sstevel@tonic-gate void
kernel_cleanup_object_bigint_attrs(kernel_object_t * object_p)8230Sstevel@tonic-gate kernel_cleanup_object_bigint_attrs(kernel_object_t *object_p)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	CK_OBJECT_CLASS class = object_p->class;
8270Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	switch (class) {
8310Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
8320Sstevel@tonic-gate 		if (OBJ_PUB(object_p)) {
8330Sstevel@tonic-gate 			switch (keytype) {
8340Sstevel@tonic-gate 			case CKK_RSA:
8350Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_RSA_MOD(
8360Sstevel@tonic-gate 				    object_p));
8370Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_RSA_PUBEXPO(
8380Sstevel@tonic-gate 				    object_p));
8390Sstevel@tonic-gate 				break;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 			case CKK_DSA:
8420Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_PRIME(
8430Sstevel@tonic-gate 				    object_p));
8440Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_SUBPRIME(
8450Sstevel@tonic-gate 				    object_p));
8460Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_BASE(
8470Sstevel@tonic-gate 				    object_p));
8480Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PUB_DSA_VALUE(
8490Sstevel@tonic-gate 				    object_p));
8500Sstevel@tonic-gate 				break;
8514219Smcpowers 
8524219Smcpowers 			case CKK_DH:
8534219Smcpowers 				bigint_attr_cleanup(OBJ_PUB_DH_PRIME(object_p));
8544219Smcpowers 				bigint_attr_cleanup(OBJ_PUB_DH_BASE(object_p));
8554219Smcpowers 				bigint_attr_cleanup(OBJ_PUB_DH_VALUE(object_p));
8564219Smcpowers 				break;
8574219Smcpowers 
8584219Smcpowers 			case CKK_EC:
8594219Smcpowers 				bigint_attr_cleanup(OBJ_PUB_EC_POINT(object_p));
8604219Smcpowers 				break;
8610Sstevel@tonic-gate 			}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 			/* Release Public Key Object struct */
8640Sstevel@tonic-gate 			free(OBJ_PUB(object_p));
8650Sstevel@tonic-gate 			OBJ_PUB(object_p) = NULL;
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 		break;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
8700Sstevel@tonic-gate 		if (OBJ_PRI(object_p)) {
8710Sstevel@tonic-gate 			switch (keytype) {
8720Sstevel@tonic-gate 			case CKK_RSA:
8730Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_MOD(
8740Sstevel@tonic-gate 				    object_p));
8750Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PUBEXPO(
8760Sstevel@tonic-gate 				    object_p));
8770Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIEXPO(
8780Sstevel@tonic-gate 				    object_p));
8790Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIME1(
8800Sstevel@tonic-gate 				    object_p));
8810Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_PRIME2(
8820Sstevel@tonic-gate 				    object_p));
8830Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_EXPO1(
8840Sstevel@tonic-gate 				    object_p));
8850Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_EXPO2(
8860Sstevel@tonic-gate 				    object_p));
8870Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_RSA_COEF(
8880Sstevel@tonic-gate 				    object_p));
8890Sstevel@tonic-gate 				break;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			case CKK_DSA:
8920Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_PRIME(
8930Sstevel@tonic-gate 				    object_p));
8940Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_SUBPRIME(
8950Sstevel@tonic-gate 				    object_p));
8960Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_BASE(
8970Sstevel@tonic-gate 				    object_p));
8980Sstevel@tonic-gate 				bigint_attr_cleanup(OBJ_PRI_DSA_VALUE(
8990Sstevel@tonic-gate 				    object_p));
9000Sstevel@tonic-gate 				break;
9014219Smcpowers 
9024219Smcpowers 			case CKK_DH:
9034219Smcpowers 				bigint_attr_cleanup(OBJ_PRI_DH_PRIME(object_p));
9044219Smcpowers 				bigint_attr_cleanup(OBJ_PRI_DH_BASE(object_p));
9054219Smcpowers 				bigint_attr_cleanup(OBJ_PRI_DH_VALUE(object_p));
9064219Smcpowers 				break;
9074219Smcpowers 
9084219Smcpowers 			case CKK_EC:
9094219Smcpowers 				bigint_attr_cleanup(OBJ_PRI_EC_VALUE(object_p));
9104219Smcpowers 				break;
9110Sstevel@tonic-gate 			}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 			/* Release Private Key Object struct. */
9140Sstevel@tonic-gate 			free(OBJ_PRI(object_p));
9150Sstevel@tonic-gate 			OBJ_PRI(object_p) = NULL;
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 		break;
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate  * Parse the common attributes. Return to caller with appropriate return
9240Sstevel@tonic-gate  * value to indicate if the supplied template specifies a valid attribute
9250Sstevel@tonic-gate  * with a valid value.
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate CK_RV
kernel_parse_common_attrs(CK_ATTRIBUTE_PTR template,kernel_session_t * sp,uint64_t * attr_mask_p)9280Sstevel@tonic-gate kernel_parse_common_attrs(CK_ATTRIBUTE_PTR template, kernel_session_t *sp,
9290Sstevel@tonic-gate     uint64_t *attr_mask_p)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
9330Sstevel@tonic-gate 	kernel_slot_t *pslot = slot_table[sp->ses_slotid];
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	switch (template->type) {
9360Sstevel@tonic-gate 	case CKA_CLASS:
9370Sstevel@tonic-gate 		break;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/* default boolean attributes */
9400Sstevel@tonic-gate 	case CKA_TOKEN:
9410Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == TRUE) {
9420Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 		break;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	case CKA_PRIVATE:
9470Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == TRUE) {
9480Sstevel@tonic-gate 			/*
9494219Smcpowers 			 * Cannot create a private object if the token
9504219Smcpowers 			 * has a keystore and the user isn't logged in.
9510Sstevel@tonic-gate 			 */
9524219Smcpowers 			if (pslot->sl_func_list.fl_object_create &&
9534219Smcpowers 			    pslot->sl_state != CKU_USER) {
9540Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
9550Sstevel@tonic-gate 			} else {
9560Sstevel@tonic-gate 				*attr_mask_p |= PRIVATE_BOOL_ON;
9570Sstevel@tonic-gate 			}
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	case CKA_MODIFIABLE:
9620Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == FALSE) {
9630Sstevel@tonic-gate 			*attr_mask_p &= ~MODIFIABLE_BOOL_ON;
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 		break;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	case CKA_LABEL:
9680Sstevel@tonic-gate 		break;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	default:
9710Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	return (rv);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * Build a Public Key Object.
9820Sstevel@tonic-gate  *
9830Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
9840Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
9850Sstevel@tonic-gate  *   with appropriate return value.
9860Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
9870Sstevel@tonic-gate  *   attributes that have boolean type.
9880Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
9890Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
9900Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
9910Sstevel@tonic-gate  * - Allocate storage for the Public Key object.
9920Sstevel@tonic-gate  * - Build the Public Key object according to the key type. Allocate
9930Sstevel@tonic-gate  *   storage to hold the big integer value for the supplied attributes
9940Sstevel@tonic-gate  *   that are required for a certain key type.
9950Sstevel@tonic-gate  *
9960Sstevel@tonic-gate  */
9970Sstevel@tonic-gate CK_RV
kernel_build_public_key_object(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,kernel_object_t * new_object,kernel_session_t * sp,uint_t mode)9980Sstevel@tonic-gate kernel_build_public_key_object(CK_ATTRIBUTE_PTR template,
9994219Smcpowers     CK_ULONG ulAttrNum,	kernel_object_t *new_object, kernel_session_t *sp,
10004219Smcpowers     uint_t mode)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	int		i;
10040Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
10050Sstevel@tonic-gate 	uint64_t	attr_mask = PUBLIC_KEY_DEFAULT;
10060Sstevel@tonic-gate 	CK_RV 		rv = CKR_OK;
10070Sstevel@tonic-gate 	int		isLabel = 0;
10080Sstevel@tonic-gate 	/* Must set flags */
10090Sstevel@tonic-gate 	int		isModulus = 0;
10100Sstevel@tonic-gate 	int		isPubExpo = 0;
10110Sstevel@tonic-gate 	int		isPrime = 0;
10120Sstevel@tonic-gate 	int		isSubprime = 0;
10130Sstevel@tonic-gate 	int		isBase = 0;
10140Sstevel@tonic-gate 	int		isValue = 0;
10154219Smcpowers 	int		isPoint = 0;
10164219Smcpowers 	int		isParams = 0;
10170Sstevel@tonic-gate 	/* Must not set flags */
10180Sstevel@tonic-gate 	int		isModulusBits = 0;
10190Sstevel@tonic-gate 	CK_ULONG	modulus_bits = 0;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	biginteger_t	modulus;
10220Sstevel@tonic-gate 	biginteger_t	pubexpo;
10230Sstevel@tonic-gate 	biginteger_t	prime;
10240Sstevel@tonic-gate 	biginteger_t	subprime;
10250Sstevel@tonic-gate 	biginteger_t	base;
10260Sstevel@tonic-gate 	biginteger_t	value;
10274219Smcpowers 	biginteger_t	point;
10280Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
10294219Smcpowers 	CK_ATTRIBUTE	param_tmp;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	public_key_obj_t  *pbk;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	/* prevent bigint_attr_cleanup from freeing invalid attr value */
10340Sstevel@tonic-gate 	(void) memset(&modulus, 0x0, sizeof (biginteger_t));
10350Sstevel@tonic-gate 	(void) memset(&pubexpo, 0x0, sizeof (biginteger_t));
10360Sstevel@tonic-gate 	(void) memset(&prime, 0x0, sizeof (biginteger_t));
10370Sstevel@tonic-gate 	(void) memset(&subprime, 0x0, sizeof (biginteger_t));
10380Sstevel@tonic-gate 	(void) memset(&base, 0x0, sizeof (biginteger_t));
10390Sstevel@tonic-gate 	(void) memset(&value, 0x0, sizeof (biginteger_t));
10404219Smcpowers 	(void) memset(&point, 0x0, sizeof (biginteger_t));
10410Sstevel@tonic-gate 	string_tmp.pValue = NULL;
10424219Smcpowers 	param_tmp.pValue = NULL;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		/* Public Key Object Attributes */
10470Sstevel@tonic-gate 		switch (template[i].type) {
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		/* common key attributes */
10500Sstevel@tonic-gate 		case CKA_KEY_TYPE:
10510Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
10520Sstevel@tonic-gate 			break;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		case CKA_ID:
10550Sstevel@tonic-gate 		case CKA_START_DATE:
10560Sstevel@tonic-gate 		case CKA_END_DATE:
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		/* common public key attribute */
10590Sstevel@tonic-gate 		case CKA_SUBJECT:
10600Sstevel@tonic-gate 			/*
10610Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
10620Sstevel@tonic-gate 			 * value with byte array type, and add it to
10630Sstevel@tonic-gate 			 * the extra attribute list of the object.
10640Sstevel@tonic-gate 			 */
10650Sstevel@tonic-gate 			rv = kernel_add_extra_attr(&template[i],
10660Sstevel@tonic-gate 			    new_object);
10670Sstevel@tonic-gate 			if (rv != CKR_OK) {
10680Sstevel@tonic-gate 				goto fail_cleanup;
10690Sstevel@tonic-gate 			}
10700Sstevel@tonic-gate 			break;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 		/*
10730Sstevel@tonic-gate 		 * The following key related attribute types must
10740Sstevel@tonic-gate 		 * not be specified by C_CreateObject.
10750Sstevel@tonic-gate 		 */
10760Sstevel@tonic-gate 		case CKA_LOCAL:
10770Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
10780Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
10790Sstevel@tonic-gate 			goto fail_cleanup;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		/* Key related boolean attributes */
10820Sstevel@tonic-gate 		case CKA_DERIVE:
10830Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
10840Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
10850Sstevel@tonic-gate 			break;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 		case CKA_ENCRYPT:
10880Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
10890Sstevel@tonic-gate 				attr_mask |= ENCRYPT_BOOL_ON;
10900Sstevel@tonic-gate 			else
10910Sstevel@tonic-gate 				attr_mask &= ~ENCRYPT_BOOL_ON;
10920Sstevel@tonic-gate 			break;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		case CKA_VERIFY:
10950Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
10960Sstevel@tonic-gate 				attr_mask |= VERIFY_BOOL_ON;
10970Sstevel@tonic-gate 			else
10980Sstevel@tonic-gate 				attr_mask &= ~VERIFY_BOOL_ON;
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		case CKA_VERIFY_RECOVER:
11020Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
11030Sstevel@tonic-gate 				attr_mask |= VERIFY_RECOVER_BOOL_ON;
11040Sstevel@tonic-gate 			else
11050Sstevel@tonic-gate 				attr_mask &= ~VERIFY_RECOVER_BOOL_ON;
11060Sstevel@tonic-gate 			break;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		case CKA_WRAP:
11090Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
11100Sstevel@tonic-gate 				attr_mask |= WRAP_BOOL_ON;
11110Sstevel@tonic-gate 			break;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		case CKA_TRUSTED:
11140Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
11150Sstevel@tonic-gate 				attr_mask |= TRUSTED_BOOL_ON;
11160Sstevel@tonic-gate 			break;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 		/*
11190Sstevel@tonic-gate 		 * The following key related attribute types must
11200Sstevel@tonic-gate 		 * be specified according to the key type by
11210Sstevel@tonic-gate 		 * C_CreateObject.
11220Sstevel@tonic-gate 		 */
11230Sstevel@tonic-gate 		case CKA_MODULUS:
11240Sstevel@tonic-gate 			isModulus = 1;
11250Sstevel@tonic-gate 			/*
11260Sstevel@tonic-gate 			 * Copyin big integer attribute from template
11270Sstevel@tonic-gate 			 * to a local variable.
11280Sstevel@tonic-gate 			 */
11290Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&modulus,
11300Sstevel@tonic-gate 			    &template[i]);
11310Sstevel@tonic-gate 			if (rv != CKR_OK)
11320Sstevel@tonic-gate 				goto fail_cleanup;
11330Sstevel@tonic-gate 			break;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
11360Sstevel@tonic-gate 			isPubExpo = 1;
11370Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&pubexpo,
11380Sstevel@tonic-gate 			    &template[i]);
11390Sstevel@tonic-gate 			if (rv != CKR_OK)
11400Sstevel@tonic-gate 				goto fail_cleanup;
11410Sstevel@tonic-gate 			break;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		case CKA_PRIME:
11440Sstevel@tonic-gate 			isPrime = 1;
11450Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime,
11460Sstevel@tonic-gate 			    &template[i]);
11470Sstevel@tonic-gate 			if (rv != CKR_OK)
11480Sstevel@tonic-gate 				goto fail_cleanup;
11490Sstevel@tonic-gate 			break;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 		case CKA_SUBPRIME:
11520Sstevel@tonic-gate 			isSubprime = 1;
11530Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&subprime,
11540Sstevel@tonic-gate 			    &template[i]);
11550Sstevel@tonic-gate 			if (rv != CKR_OK)
11560Sstevel@tonic-gate 				goto fail_cleanup;
11570Sstevel@tonic-gate 			break;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		case CKA_BASE:
11600Sstevel@tonic-gate 			isBase = 1;
11610Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&base,
11620Sstevel@tonic-gate 			    &template[i]);
11630Sstevel@tonic-gate 			if (rv != CKR_OK)
11640Sstevel@tonic-gate 				goto fail_cleanup;
11650Sstevel@tonic-gate 			break;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		case CKA_VALUE:
11680Sstevel@tonic-gate 			isValue = 1;
11690Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&value,
11700Sstevel@tonic-gate 			    &template[i]);
11710Sstevel@tonic-gate 			if (rv != CKR_OK)
11720Sstevel@tonic-gate 				goto fail_cleanup;
11730Sstevel@tonic-gate 			break;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 		case CKA_MODULUS_BITS:
11760Sstevel@tonic-gate 			isModulusBits = 1;
11770Sstevel@tonic-gate 			get_ulong_attr_from_template(&modulus_bits,
11780Sstevel@tonic-gate 			    &template[i]);
11790Sstevel@tonic-gate 			break;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 		case CKA_LABEL:
11820Sstevel@tonic-gate 			isLabel = 1;
11830Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
11840Sstevel@tonic-gate 			    &template[i]);
11850Sstevel@tonic-gate 			if (rv != CKR_OK)
11860Sstevel@tonic-gate 				goto fail_cleanup;
11870Sstevel@tonic-gate 			break;
11880Sstevel@tonic-gate 
11894219Smcpowers 		case CKA_EC_POINT:
11904219Smcpowers 			isPoint = 1;
11914219Smcpowers 			rv = get_bigint_attr_from_template(&point,
11924219Smcpowers 			    &template[i]);
11934219Smcpowers 			if (rv != CKR_OK)
11944219Smcpowers 				goto fail_cleanup;
11954219Smcpowers 			break;
11964219Smcpowers 
11974219Smcpowers 		case CKA_EC_PARAMS:
11984219Smcpowers 			isParams = 1;
11994219Smcpowers 			rv = get_string_from_template(&param_tmp,
12004219Smcpowers 			    &template[i]);
12014219Smcpowers 			if (rv != CKR_OK)
12024219Smcpowers 				goto fail_cleanup;
12034219Smcpowers 			break;
12044219Smcpowers 
12050Sstevel@tonic-gate 		default:
12060Sstevel@tonic-gate 			rv = kernel_parse_common_attrs(&template[i], sp,
12070Sstevel@tonic-gate 			    &attr_mask);
12080Sstevel@tonic-gate 			if (rv != CKR_OK)
12090Sstevel@tonic-gate 				goto fail_cleanup;
12100Sstevel@tonic-gate 			break;
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 	} /* For */
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	/* Allocate storage for Public Key Object. */
12150Sstevel@tonic-gate 	pbk = calloc(1, sizeof (public_key_obj_t));
12160Sstevel@tonic-gate 	if (pbk == NULL) {
12170Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
12180Sstevel@tonic-gate 		goto fail_cleanup;
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	new_object->object_class_u.public_key = pbk;
12220Sstevel@tonic-gate 	new_object->class = CKO_PUBLIC_KEY;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (keytype == (CK_KEY_TYPE)~0UL) {
12250Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
12260Sstevel@tonic-gate 		goto fail_cleanup;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	new_object->key_type = keytype;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	/* Supported key types of the Public Key Object */
12310Sstevel@tonic-gate 	switch (keytype) {
12320Sstevel@tonic-gate 	case CKK_RSA:
12334219Smcpowers 		if (mode == KERNEL_CREATE_OBJ) {
12344219Smcpowers 			if (isModulusBits || isPrime || isSubprime ||
12354219Smcpowers 			    isBase|| isValue) {
12364219Smcpowers 				rv = CKR_TEMPLATE_INCONSISTENT;
12374219Smcpowers 				goto fail_cleanup;
12384219Smcpowers 			}
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 		if (isModulus && isPubExpo) {
12420Sstevel@tonic-gate 			/*
12430Sstevel@tonic-gate 			 * Copy big integer attribute value to the
12440Sstevel@tonic-gate 			 * designated place in the public key object.
12450Sstevel@tonic-gate 			 */
12464219Smcpowers 			copy_bigint_attr(&modulus,
12474219Smcpowers 			    KEY_PUB_RSA_MOD(pbk));
12484219Smcpowers 
12494219Smcpowers 			copy_bigint_attr(&pubexpo,
12504219Smcpowers 			    KEY_PUB_RSA_PUBEXPO(pbk));
12510Sstevel@tonic-gate 		} else {
12520Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
12530Sstevel@tonic-gate 			goto fail_cleanup;
12540Sstevel@tonic-gate 		}
12554219Smcpowers 
12564219Smcpowers 		/* must be generating a RSA key pair by value */
12574219Smcpowers 		if (isModulusBits) {
12584219Smcpowers 			KEY_PUB_RSA_MOD_BITS(pbk) = modulus_bits;
12594219Smcpowers 		}
12600Sstevel@tonic-gate 		break;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	case CKK_DSA:
12630Sstevel@tonic-gate 		if (isModulusBits || isModulus || isPubExpo) {
12640Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
12650Sstevel@tonic-gate 			goto fail_cleanup;
12660Sstevel@tonic-gate 		}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 		if (!(isPrime && isSubprime && isBase && isValue)) {
12690Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
12700Sstevel@tonic-gate 			goto fail_cleanup;
12710Sstevel@tonic-gate 		}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 		copy_bigint_attr(&prime, KEY_PUB_DSA_PRIME(pbk));
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 		copy_bigint_attr(&subprime, KEY_PUB_DSA_SUBPRIME(pbk));
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		copy_bigint_attr(&base, KEY_PUB_DSA_BASE(pbk));
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		copy_bigint_attr(&value, KEY_PUB_DSA_VALUE(pbk));
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		break;
12824219Smcpowers 
12834219Smcpowers 	case CKK_DH:
12844219Smcpowers 		if (!(isPrime && isBase && isValue)) {
12854219Smcpowers 			rv = CKR_TEMPLATE_INCOMPLETE;
12864219Smcpowers 			goto fail_cleanup;
12874219Smcpowers 		}
12884219Smcpowers 
12894219Smcpowers 		copy_bigint_attr(&prime, KEY_PUB_DH_PRIME(pbk));
12904219Smcpowers 
12914219Smcpowers 		copy_bigint_attr(&base, KEY_PUB_DH_BASE(pbk));
12924219Smcpowers 
12934219Smcpowers 		copy_bigint_attr(&value, KEY_PUB_DH_VALUE(pbk));
12944219Smcpowers 
12954219Smcpowers 		break;
12964219Smcpowers 
12974219Smcpowers 	case CKK_EC:
12984219Smcpowers 		if (!isPoint || !isParams) {
12994219Smcpowers 			rv = CKR_TEMPLATE_INCOMPLETE;
13004219Smcpowers 			goto fail_cleanup;
13014219Smcpowers 		}
13024219Smcpowers 
13034219Smcpowers 		copy_bigint_attr(&point, KEY_PUB_EC_POINT(pbk));
13044219Smcpowers 		rv = kernel_add_extra_attr(&param_tmp, new_object);
13054219Smcpowers 		if (rv != CKR_OK)
13064219Smcpowers 			goto fail_cleanup;
13074219Smcpowers 		string_attr_cleanup(&param_tmp);
13084219Smcpowers 		break;
13090Sstevel@tonic-gate 	default:
13100Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
13110Sstevel@tonic-gate 		goto fail_cleanup;
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	/* Set up object. */
13150Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
13160Sstevel@tonic-gate 	if (isLabel) {
13170Sstevel@tonic-gate 		rv = kernel_add_extra_attr(&string_tmp, new_object);
13180Sstevel@tonic-gate 		if (rv != CKR_OK)
13190Sstevel@tonic-gate 			goto fail_cleanup;
13200Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	return (rv);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate fail_cleanup:
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	bigint_attr_cleanup(&modulus);
13300Sstevel@tonic-gate 	bigint_attr_cleanup(&pubexpo);
13310Sstevel@tonic-gate 	bigint_attr_cleanup(&prime);
13320Sstevel@tonic-gate 	bigint_attr_cleanup(&subprime);
13330Sstevel@tonic-gate 	bigint_attr_cleanup(&base);
13340Sstevel@tonic-gate 	bigint_attr_cleanup(&value);
13354219Smcpowers 	bigint_attr_cleanup(&point);
13360Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
13374219Smcpowers 	string_attr_cleanup(&param_tmp);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	kernel_cleanup_object(new_object);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	return (rv);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate /*
13490Sstevel@tonic-gate  * Build a Private Key Object.
13500Sstevel@tonic-gate  *
13510Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
13520Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
13530Sstevel@tonic-gate  *   with appropriate return value.
13540Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
13550Sstevel@tonic-gate  *   attributes that have boolean type.
13560Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
13570Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
13580Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
13590Sstevel@tonic-gate  * - Allocate storage for the Private Key object.
13600Sstevel@tonic-gate  * - Build the Private Key object according to the key type. Allocate
13610Sstevel@tonic-gate  *   storage to hold the big integer value for the supplied attributes
13620Sstevel@tonic-gate  *   that are required for a certain key type.
13630Sstevel@tonic-gate  *
13640Sstevel@tonic-gate  */
13650Sstevel@tonic-gate CK_RV
kernel_build_private_key_object(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,kernel_object_t * new_object,kernel_session_t * sp,uint_t mode)13660Sstevel@tonic-gate kernel_build_private_key_object(CK_ATTRIBUTE_PTR template,
13674219Smcpowers     CK_ULONG ulAttrNum,	kernel_object_t *new_object, kernel_session_t *sp,
13684219Smcpowers     uint_t mode)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	ulong_t		i;
13710Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
13720Sstevel@tonic-gate 	uint64_t	attr_mask = PRIVATE_KEY_DEFAULT;
13730Sstevel@tonic-gate 	CK_RV 		rv = CKR_OK;
13740Sstevel@tonic-gate 	int		isLabel = 0;
13750Sstevel@tonic-gate 	/* Must set flags */
13760Sstevel@tonic-gate 	int		isModulus = 0;
13770Sstevel@tonic-gate 	int		isPriExpo = 0;
13780Sstevel@tonic-gate 	int		isPrime = 0;
13790Sstevel@tonic-gate 	int		isSubprime = 0;
13800Sstevel@tonic-gate 	int		isBase = 0;
13810Sstevel@tonic-gate 	int		isValue = 0;
13824219Smcpowers 	int		isParams = 0;
13830Sstevel@tonic-gate 	/* Must not set flags */
13840Sstevel@tonic-gate 	int		isValueBits = 0;
13850Sstevel@tonic-gate 	CK_ULONG	value_bits = 0;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	/* Private Key RSA optional */
13880Sstevel@tonic-gate 	int		isPubExpo = 0;
13890Sstevel@tonic-gate 	int		isPrime1 = 0;
13900Sstevel@tonic-gate 	int		isPrime2 = 0;
13910Sstevel@tonic-gate 	int		isExpo1 = 0;
13920Sstevel@tonic-gate 	int		isExpo2 = 0;
13930Sstevel@tonic-gate 	int		isCoef = 0;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	biginteger_t	modulus;
13960Sstevel@tonic-gate 	biginteger_t	priexpo;
13970Sstevel@tonic-gate 	biginteger_t	prime;
13980Sstevel@tonic-gate 	biginteger_t	subprime;
13990Sstevel@tonic-gate 	biginteger_t	base;
14000Sstevel@tonic-gate 	biginteger_t	value;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	biginteger_t	pubexpo;
14030Sstevel@tonic-gate 	biginteger_t	prime1;
14040Sstevel@tonic-gate 	biginteger_t	prime2;
14050Sstevel@tonic-gate 	biginteger_t	expo1;
14060Sstevel@tonic-gate 	biginteger_t	expo2;
14070Sstevel@tonic-gate 	biginteger_t	coef;
14080Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
14094219Smcpowers 	CK_ATTRIBUTE	param_tmp;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	private_key_obj_t *pvk;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	/* prevent bigint_attr_cleanup from freeing invalid attr value */
14140Sstevel@tonic-gate 	(void) memset(&modulus, 0x0, sizeof (biginteger_t));
14150Sstevel@tonic-gate 	(void) memset(&priexpo, 0x0, sizeof (biginteger_t));
14160Sstevel@tonic-gate 	(void) memset(&prime, 0x0, sizeof (biginteger_t));
14170Sstevel@tonic-gate 	(void) memset(&subprime, 0x0, sizeof (biginteger_t));
14180Sstevel@tonic-gate 	(void) memset(&base, 0x0, sizeof (biginteger_t));
14190Sstevel@tonic-gate 	(void) memset(&value, 0x0, sizeof (biginteger_t));
14200Sstevel@tonic-gate 	(void) memset(&pubexpo, 0x0, sizeof (biginteger_t));
14210Sstevel@tonic-gate 	(void) memset(&prime1, 0x0, sizeof (biginteger_t));
14220Sstevel@tonic-gate 	(void) memset(&prime2, 0x0, sizeof (biginteger_t));
14230Sstevel@tonic-gate 	(void) memset(&expo1, 0x0, sizeof (biginteger_t));
14240Sstevel@tonic-gate 	(void) memset(&expo2, 0x0, sizeof (biginteger_t));
14250Sstevel@tonic-gate 	(void) memset(&coef, 0x0, sizeof (biginteger_t));
14260Sstevel@tonic-gate 	string_tmp.pValue = NULL;
14274219Smcpowers 	param_tmp.pValue = NULL;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 		/* Private Key Object Attributes */
14320Sstevel@tonic-gate 		switch (template[i].type) {
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 		/* common key attributes */
14350Sstevel@tonic-gate 		case CKA_KEY_TYPE:
14360Sstevel@tonic-gate 			keytype = *((CK_KEY_TYPE*)template[i].pValue);
14370Sstevel@tonic-gate 			break;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 		case CKA_ID:
14400Sstevel@tonic-gate 		case CKA_START_DATE:
14410Sstevel@tonic-gate 		case CKA_END_DATE:
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		/* common private key attribute */
14440Sstevel@tonic-gate 		case CKA_SUBJECT:
14450Sstevel@tonic-gate 			/*
14460Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
14470Sstevel@tonic-gate 			 * value with byte array type, and add it to
14480Sstevel@tonic-gate 			 * the extra attribute list of the object.
14490Sstevel@tonic-gate 			 */
14500Sstevel@tonic-gate 			rv = kernel_add_extra_attr(&template[i],
14510Sstevel@tonic-gate 			    new_object);
14520Sstevel@tonic-gate 			if (rv != CKR_OK) {
14530Sstevel@tonic-gate 				goto fail_cleanup;
14540Sstevel@tonic-gate 			}
14550Sstevel@tonic-gate 			break;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		/*
14580Sstevel@tonic-gate 		 * The following key related attribute types must
14590Sstevel@tonic-gate 		 * not be specified by C_CreateObject.
14600Sstevel@tonic-gate 		 */
14610Sstevel@tonic-gate 		case CKA_LOCAL:
14620Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
14630Sstevel@tonic-gate 		case CKA_AUTH_PIN_FLAGS:
14640Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
14650Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
14660Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
14670Sstevel@tonic-gate 			goto fail_cleanup;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 		/* Key related boolean attributes */
14700Sstevel@tonic-gate 		case CKA_DERIVE:
14710Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
14720Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
14730Sstevel@tonic-gate 			break;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		case CKA_SENSITIVE:
14760Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
14770Sstevel@tonic-gate 				attr_mask |= SENSITIVE_BOOL_ON;
14780Sstevel@tonic-gate 			break;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 		case CKA_SECONDARY_AUTH:
14810Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue) {
14820Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
14830Sstevel@tonic-gate 				goto fail_cleanup;
14840Sstevel@tonic-gate 			}
14850Sstevel@tonic-gate 			break;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		case CKA_DECRYPT:
14880Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
14890Sstevel@tonic-gate 				attr_mask |= DECRYPT_BOOL_ON;
14900Sstevel@tonic-gate 			else
14910Sstevel@tonic-gate 				attr_mask &= ~DECRYPT_BOOL_ON;
14920Sstevel@tonic-gate 			break;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 		case CKA_SIGN:
14950Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
14960Sstevel@tonic-gate 				attr_mask |= SIGN_BOOL_ON;
14970Sstevel@tonic-gate 			else
14980Sstevel@tonic-gate 				attr_mask &= ~SIGN_BOOL_ON;
14990Sstevel@tonic-gate 			break;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 		case CKA_SIGN_RECOVER:
15020Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
15030Sstevel@tonic-gate 				attr_mask |= SIGN_RECOVER_BOOL_ON;
15040Sstevel@tonic-gate 			else
15050Sstevel@tonic-gate 				attr_mask &= ~SIGN_RECOVER_BOOL_ON;
15060Sstevel@tonic-gate 			break;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		case CKA_UNWRAP:
15090Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
15100Sstevel@tonic-gate 				attr_mask |= UNWRAP_BOOL_ON;
15110Sstevel@tonic-gate 			break;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
15140Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
15150Sstevel@tonic-gate 				attr_mask |= EXTRACTABLE_BOOL_ON;
15160Sstevel@tonic-gate 			else
15170Sstevel@tonic-gate 				attr_mask &= ~EXTRACTABLE_BOOL_ON;
15180Sstevel@tonic-gate 			break;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		/*
15210Sstevel@tonic-gate 		 * The following key related attribute types must
15220Sstevel@tonic-gate 		 * be specified according to the key type by
15230Sstevel@tonic-gate 		 * C_CreateObject.
15240Sstevel@tonic-gate 		 */
15250Sstevel@tonic-gate 		case CKA_MODULUS:
15260Sstevel@tonic-gate 			isModulus = 1;
15270Sstevel@tonic-gate 			/*
15280Sstevel@tonic-gate 			 * Copyin big integer attribute from template
15290Sstevel@tonic-gate 			 * to a local variable.
15300Sstevel@tonic-gate 			 */
15310Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&modulus,
15320Sstevel@tonic-gate 			    &template[i]);
15330Sstevel@tonic-gate 			if (rv != CKR_OK)
15340Sstevel@tonic-gate 				goto fail_cleanup;
15350Sstevel@tonic-gate 			break;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
15380Sstevel@tonic-gate 			isPubExpo = 1;
15390Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&pubexpo,
15400Sstevel@tonic-gate 			    &template[i]);
15410Sstevel@tonic-gate 			if (rv != CKR_OK)
15420Sstevel@tonic-gate 				goto fail_cleanup;
15430Sstevel@tonic-gate 			break;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
15460Sstevel@tonic-gate 			isPriExpo = 1;
15470Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&priexpo,
15480Sstevel@tonic-gate 			    &template[i]);
15490Sstevel@tonic-gate 			if (rv != CKR_OK)
15500Sstevel@tonic-gate 				goto fail_cleanup;
15510Sstevel@tonic-gate 			break;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 		case CKA_PRIME_1:
15540Sstevel@tonic-gate 			isPrime1 = 1;
15550Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime1,
15560Sstevel@tonic-gate 			    &template[i]);
15570Sstevel@tonic-gate 			if (rv != CKR_OK)
15580Sstevel@tonic-gate 				goto fail_cleanup;
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		case CKA_PRIME_2:
15620Sstevel@tonic-gate 			isPrime2 = 1;
15630Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime2,
15640Sstevel@tonic-gate 			    &template[i]);
15650Sstevel@tonic-gate 			if (rv != CKR_OK)
15660Sstevel@tonic-gate 				goto fail_cleanup;
15670Sstevel@tonic-gate 			break;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 		case CKA_EXPONENT_1:
15700Sstevel@tonic-gate 			isExpo1 = 1;
15710Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&expo1,
15720Sstevel@tonic-gate 			    &template[i]);
15730Sstevel@tonic-gate 			if (rv != CKR_OK)
15740Sstevel@tonic-gate 				goto fail_cleanup;
15750Sstevel@tonic-gate 			break;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		case CKA_EXPONENT_2:
15780Sstevel@tonic-gate 			isExpo2 = 1;
15790Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&expo2,
15800Sstevel@tonic-gate 			    &template[i]);
15810Sstevel@tonic-gate 			if (rv != CKR_OK)
15820Sstevel@tonic-gate 				goto fail_cleanup;
15830Sstevel@tonic-gate 			break;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 		case CKA_COEFFICIENT:
15860Sstevel@tonic-gate 			isCoef = 1;
15870Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&coef,
15880Sstevel@tonic-gate 			    &template[i]);
15890Sstevel@tonic-gate 			if (rv != CKR_OK)
15900Sstevel@tonic-gate 				goto fail_cleanup;
15910Sstevel@tonic-gate 			break;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 		case CKA_PRIME:
15940Sstevel@tonic-gate 			isPrime = 1;
15950Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&prime,
15960Sstevel@tonic-gate 			    &template[i]);
15970Sstevel@tonic-gate 			if (rv != CKR_OK)
15980Sstevel@tonic-gate 				goto fail_cleanup;
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 		case CKA_SUBPRIME:
16020Sstevel@tonic-gate 			isSubprime = 1;
16030Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&subprime,
16040Sstevel@tonic-gate 			    &template[i]);
16050Sstevel@tonic-gate 			if (rv != CKR_OK)
16060Sstevel@tonic-gate 				goto fail_cleanup;
16070Sstevel@tonic-gate 			break;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 		case CKA_BASE:
16100Sstevel@tonic-gate 			isBase = 1;
16110Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&base,
16120Sstevel@tonic-gate 			    &template[i]);
16130Sstevel@tonic-gate 			if (rv != CKR_OK)
16140Sstevel@tonic-gate 				goto fail_cleanup;
16150Sstevel@tonic-gate 			break;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 		case CKA_VALUE:
16180Sstevel@tonic-gate 			isValue = 1;
16190Sstevel@tonic-gate 			rv = get_bigint_attr_from_template(&value,
16200Sstevel@tonic-gate 			    &template[i]);
16210Sstevel@tonic-gate 			if (rv != CKR_OK)
16220Sstevel@tonic-gate 				goto fail_cleanup;
16230Sstevel@tonic-gate 			break;
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 		case CKA_VALUE_BITS:
16260Sstevel@tonic-gate 			isValueBits = 1;
16270Sstevel@tonic-gate 			get_ulong_attr_from_template(&value_bits,
16280Sstevel@tonic-gate 			    &template[i]);
16290Sstevel@tonic-gate 			break;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		case CKA_LABEL:
16320Sstevel@tonic-gate 			isLabel = 1;
16330Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
16340Sstevel@tonic-gate 			    &template[i]);
16350Sstevel@tonic-gate 			if (rv != CKR_OK)
16360Sstevel@tonic-gate 				goto fail_cleanup;
16370Sstevel@tonic-gate 			break;
16380Sstevel@tonic-gate 
16394219Smcpowers 		case CKA_EC_PARAMS:
16404219Smcpowers 			isParams = 1;
16414219Smcpowers 			rv = get_string_from_template(&param_tmp,
16424219Smcpowers 			    &template[i]);
16434219Smcpowers 			if (rv != CKR_OK)
16444219Smcpowers 				goto fail_cleanup;
16454219Smcpowers 			break;
16464219Smcpowers 
16470Sstevel@tonic-gate 		default:
16480Sstevel@tonic-gate 			rv = kernel_parse_common_attrs(&template[i], sp,
16490Sstevel@tonic-gate 			    &attr_mask);
16500Sstevel@tonic-gate 			if (rv != CKR_OK)
16510Sstevel@tonic-gate 				goto fail_cleanup;
16520Sstevel@tonic-gate 			break;
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 	} /* For */
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/* Allocate storage for Private Key Object. */
16580Sstevel@tonic-gate 	pvk = calloc(1, sizeof (private_key_obj_t));
16590Sstevel@tonic-gate 	if (pvk == NULL) {
16600Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
16610Sstevel@tonic-gate 		goto fail_cleanup;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	new_object->object_class_u.private_key = pvk;
16650Sstevel@tonic-gate 	new_object->class = CKO_PRIVATE_KEY;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	if (keytype == (CK_KEY_TYPE)~0UL) {
16680Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
16690Sstevel@tonic-gate 		goto fail_cleanup;
16700Sstevel@tonic-gate 	}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	new_object->key_type = keytype;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/* Supported key types of the Private Key Object */
16750Sstevel@tonic-gate 	switch (keytype) {
16760Sstevel@tonic-gate 	case CKK_RSA:
16770Sstevel@tonic-gate 		if (isPrime || isSubprime || isBase || isValue ||
16780Sstevel@tonic-gate 		    isValueBits) {
16790Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
16800Sstevel@tonic-gate 			goto fail_cleanup;
16810Sstevel@tonic-gate 		}
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 		if (isModulus && isPriExpo) {
16840Sstevel@tonic-gate 			/*
16850Sstevel@tonic-gate 			 * Copy big integer attribute value to the
16860Sstevel@tonic-gate 			 * designated place in the Private Key object.
16870Sstevel@tonic-gate 			 */
16880Sstevel@tonic-gate 			copy_bigint_attr(&modulus, KEY_PRI_RSA_MOD(pvk));
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			copy_bigint_attr(&priexpo, KEY_PRI_RSA_PRIEXPO(pvk));
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 		} else {
16930Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
16940Sstevel@tonic-gate 			goto fail_cleanup;
16950Sstevel@tonic-gate 		}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 		/* The following attributes are optional. */
16980Sstevel@tonic-gate 		if (isPubExpo) {
16990Sstevel@tonic-gate 			copy_bigint_attr(&pubexpo, KEY_PRI_RSA_PUBEXPO(pvk));
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 		if (isPrime1) {
17030Sstevel@tonic-gate 			copy_bigint_attr(&prime1, KEY_PRI_RSA_PRIME1(pvk));
17040Sstevel@tonic-gate 		}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		if (isPrime2) {
17070Sstevel@tonic-gate 			copy_bigint_attr(&prime2, KEY_PRI_RSA_PRIME2(pvk));
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 		if (isExpo1) {
17110Sstevel@tonic-gate 			copy_bigint_attr(&expo1, KEY_PRI_RSA_EXPO1(pvk));
17120Sstevel@tonic-gate 		}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 		if (isExpo2) {
17150Sstevel@tonic-gate 			copy_bigint_attr(&expo2, KEY_PRI_RSA_EXPO2(pvk));
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		if (isCoef) {
17190Sstevel@tonic-gate 			copy_bigint_attr(&coef, KEY_PRI_RSA_COEF(pvk));
17200Sstevel@tonic-gate 		}
17210Sstevel@tonic-gate 		break;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	case CKK_DSA:
17240Sstevel@tonic-gate 		if (isModulus || isPubExpo || isPriExpo || isPrime1 ||
17250Sstevel@tonic-gate 		    isPrime2 || isExpo1 || isExpo2 || isCoef ||
17260Sstevel@tonic-gate 		    isValueBits) {
17270Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
17280Sstevel@tonic-gate 			goto fail_cleanup;
17290Sstevel@tonic-gate 		}
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 		if (!(isPrime && isSubprime && isBase && isValue)) {
17320Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
17330Sstevel@tonic-gate 			goto fail_cleanup;
17340Sstevel@tonic-gate 		}
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 		copy_bigint_attr(&prime, KEY_PRI_DSA_PRIME(pvk));
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		copy_bigint_attr(&subprime, KEY_PRI_DSA_SUBPRIME(pvk));
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 		copy_bigint_attr(&base, KEY_PRI_DSA_BASE(pvk));
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 		copy_bigint_attr(&value, KEY_PRI_DSA_VALUE(pvk));
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 		break;
17450Sstevel@tonic-gate 
17464219Smcpowers 	case CKK_DH:
17474219Smcpowers 		if (mode == KERNEL_CREATE_OBJ && isValueBits) {
17484219Smcpowers 			rv = CKR_TEMPLATE_INCONSISTENT;
17494219Smcpowers 			goto fail_cleanup;
17504219Smcpowers 		}
17514219Smcpowers 		if (!(isPrime && isBase && isValue)) {
17524219Smcpowers 			rv = CKR_TEMPLATE_INCOMPLETE;
17534219Smcpowers 			goto fail_cleanup;
17544219Smcpowers 		}
17554219Smcpowers 
17564219Smcpowers 		copy_bigint_attr(&prime, KEY_PRI_DH_PRIME(pvk));
17574219Smcpowers 
17584219Smcpowers 		copy_bigint_attr(&base, KEY_PRI_DH_BASE(pvk));
17594219Smcpowers 
17604219Smcpowers 		copy_bigint_attr(&value, KEY_PRI_DH_VALUE(pvk));
17614219Smcpowers 
17624219Smcpowers 		KEY_PRI_DH_VAL_BITS(pvk) = (isValueBits) ? value_bits : 0;
17634219Smcpowers 
17644219Smcpowers 		break;
17654219Smcpowers 
17664219Smcpowers 	case CKK_EC:
17674219Smcpowers 		if (!isValue || !isParams) {
17684219Smcpowers 			rv = CKR_TEMPLATE_INCOMPLETE;
17694219Smcpowers 			goto fail_cleanup;
17704219Smcpowers 		}
17714219Smcpowers 
17724219Smcpowers 		copy_bigint_attr(&value, KEY_PRI_EC_VALUE(pvk));
17734219Smcpowers 		rv = kernel_add_extra_attr(&param_tmp, new_object);
17744219Smcpowers 		if (rv != CKR_OK)
17754219Smcpowers 			goto fail_cleanup;
17764219Smcpowers 		string_attr_cleanup(&param_tmp);
17774219Smcpowers 		break;
17780Sstevel@tonic-gate 	default:
17790Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
17800Sstevel@tonic-gate 		goto fail_cleanup;
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/* Set up object. */
17840Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
17850Sstevel@tonic-gate 	if (isLabel) {
17860Sstevel@tonic-gate 		rv = kernel_add_extra_attr(&string_tmp, new_object);
17870Sstevel@tonic-gate 		if (rv != CKR_OK)
17880Sstevel@tonic-gate 			goto fail_cleanup;
17890Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	return (rv);
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate fail_cleanup:
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	bigint_attr_cleanup(&modulus);
17990Sstevel@tonic-gate 	bigint_attr_cleanup(&priexpo);
18000Sstevel@tonic-gate 	bigint_attr_cleanup(&prime);
18010Sstevel@tonic-gate 	bigint_attr_cleanup(&subprime);
18020Sstevel@tonic-gate 	bigint_attr_cleanup(&base);
18030Sstevel@tonic-gate 	bigint_attr_cleanup(&value);
18040Sstevel@tonic-gate 	bigint_attr_cleanup(&pubexpo);
18050Sstevel@tonic-gate 	bigint_attr_cleanup(&prime1);
18060Sstevel@tonic-gate 	bigint_attr_cleanup(&prime2);
18070Sstevel@tonic-gate 	bigint_attr_cleanup(&expo1);
18080Sstevel@tonic-gate 	bigint_attr_cleanup(&expo2);
18090Sstevel@tonic-gate 	bigint_attr_cleanup(&coef);
18100Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
18114219Smcpowers 	string_attr_cleanup(&param_tmp);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	/*
18140Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
18150Sstevel@tonic-gate 	 */
18160Sstevel@tonic-gate 	kernel_cleanup_object(new_object);
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	return (rv);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate /*
18230Sstevel@tonic-gate  * Build a Secret Key Object.
18240Sstevel@tonic-gate  *
18250Sstevel@tonic-gate  * - Parse the object's template, and when an error is detected such as
18260Sstevel@tonic-gate  *   invalid attribute type, invalid attribute value, etc., return
18270Sstevel@tonic-gate  *   with appropriate return value.
18280Sstevel@tonic-gate  * - Set up attribute mask field in the object for the supplied common
18290Sstevel@tonic-gate  *   attributes that have boolean type.
18300Sstevel@tonic-gate  * - Build the attribute_info struct to hold the value of each supplied
18310Sstevel@tonic-gate  *   attribute that has byte array type. Link attribute_info structs
18320Sstevel@tonic-gate  *   together to form the extra attribute list of the object.
18330Sstevel@tonic-gate  * - Allocate storage for the Secret Key object.
18340Sstevel@tonic-gate  * - Build the Secret Key object. Allocate storage to hold the big integer
18350Sstevel@tonic-gate  *   value for the attribute CKA_VALUE that is required for all the key
18360Sstevel@tonic-gate  *   types supported by secret key object.
18370Sstevel@tonic-gate  *
18380Sstevel@tonic-gate  */
18390Sstevel@tonic-gate CK_RV
kernel_build_secret_key_object(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,kernel_object_t * new_object,kernel_session_t * sp)18400Sstevel@tonic-gate kernel_build_secret_key_object(CK_ATTRIBUTE_PTR template,
18410Sstevel@tonic-gate     CK_ULONG ulAttrNum,	kernel_object_t *new_object, kernel_session_t *sp)
18420Sstevel@tonic-gate {
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	int		i;
18450Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = (CK_KEY_TYPE)~0UL;
18460Sstevel@tonic-gate 	uint64_t	attr_mask = SECRET_KEY_DEFAULT;
18470Sstevel@tonic-gate 	CK_RV 		rv = CKR_OK;
18480Sstevel@tonic-gate 	int		isLabel = 0;
18490Sstevel@tonic-gate 	/* Must set flags */
18500Sstevel@tonic-gate 	int		isValue = 0;
18510Sstevel@tonic-gate 	/* Must not set flags */
18520Sstevel@tonic-gate 	int		isValueLen = 0;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	CK_ATTRIBUTE	string_tmp;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	secret_key_obj_t  *sck;
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	string_tmp.pValue = NULL;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	/* Allocate storage for Secret Key Object. */
18610Sstevel@tonic-gate 	sck = calloc(1, sizeof (secret_key_obj_t));
18620Sstevel@tonic-gate 	if (sck == NULL) {
18630Sstevel@tonic-gate 		rv = CKR_HOST_MEMORY;
18640Sstevel@tonic-gate 		goto fail_cleanup;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	new_object->object_class_u.secret_key = sck;
18680Sstevel@tonic-gate 	new_object->class = CKO_SECRET_KEY;
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	for (i = 0; i < ulAttrNum; i++) {
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 		/* Secret Key Object Attributes */
18730Sstevel@tonic-gate 		switch (template[i].type) {
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 		/* common key attributes */
18760Sstevel@tonic-gate 		case CKA_KEY_TYPE:
18770Sstevel@tonic-gate 		keytype = *((CK_KEY_TYPE*)template[i].pValue);
18780Sstevel@tonic-gate 			break;
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		case CKA_ID:
18810Sstevel@tonic-gate 		case CKA_START_DATE:
18820Sstevel@tonic-gate 		case CKA_END_DATE:
18830Sstevel@tonic-gate 			/*
18840Sstevel@tonic-gate 			 * Allocate storage to hold the attribute
18850Sstevel@tonic-gate 			 * value with byte array type, and add it to
18860Sstevel@tonic-gate 			 * the extra attribute list of the object.
18870Sstevel@tonic-gate 			 */
18880Sstevel@tonic-gate 			rv = kernel_add_extra_attr(&template[i],
18890Sstevel@tonic-gate 			    new_object);
18900Sstevel@tonic-gate 			if (rv != CKR_OK) {
18910Sstevel@tonic-gate 				goto fail_cleanup;
18920Sstevel@tonic-gate 			}
18930Sstevel@tonic-gate 			break;
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 		/*
18960Sstevel@tonic-gate 		 * The following key related attribute types must
18970Sstevel@tonic-gate 		 * not be specified by C_CreateObject.
18980Sstevel@tonic-gate 		 */
18990Sstevel@tonic-gate 		case CKA_LOCAL:
19000Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
19010Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
19020Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
19030Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCONSISTENT;
19040Sstevel@tonic-gate 			goto fail_cleanup;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 		/* Key related boolean attributes */
19070Sstevel@tonic-gate 		case CKA_DERIVE:
19080Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19090Sstevel@tonic-gate 				attr_mask |= DERIVE_BOOL_ON;
19100Sstevel@tonic-gate 			break;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 		case CKA_SENSITIVE:
19130Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19140Sstevel@tonic-gate 				attr_mask |= SENSITIVE_BOOL_ON;
19150Sstevel@tonic-gate 			break;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		case CKA_ENCRYPT:
19180Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19190Sstevel@tonic-gate 				attr_mask |= ENCRYPT_BOOL_ON;
19200Sstevel@tonic-gate 			else
19210Sstevel@tonic-gate 				attr_mask &= ~ENCRYPT_BOOL_ON;
19220Sstevel@tonic-gate 			break;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 		case CKA_DECRYPT:
19250Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19260Sstevel@tonic-gate 				attr_mask |= DECRYPT_BOOL_ON;
19270Sstevel@tonic-gate 			else
19280Sstevel@tonic-gate 				attr_mask &= ~DECRYPT_BOOL_ON;
19290Sstevel@tonic-gate 			break;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		case CKA_SIGN:
19320Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19330Sstevel@tonic-gate 				attr_mask |= SIGN_BOOL_ON;
19340Sstevel@tonic-gate 			else
19350Sstevel@tonic-gate 				attr_mask &= ~SIGN_BOOL_ON;
19360Sstevel@tonic-gate 			break;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 		case CKA_VERIFY:
19390Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19400Sstevel@tonic-gate 				attr_mask |= VERIFY_BOOL_ON;
19410Sstevel@tonic-gate 			else
19420Sstevel@tonic-gate 				attr_mask &= ~VERIFY_BOOL_ON;
19430Sstevel@tonic-gate 			break;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 		case CKA_WRAP:
19460Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19470Sstevel@tonic-gate 				attr_mask |= WRAP_BOOL_ON;
19480Sstevel@tonic-gate 			break;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		case CKA_UNWRAP:
19510Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19520Sstevel@tonic-gate 				attr_mask |= UNWRAP_BOOL_ON;
19530Sstevel@tonic-gate 			break;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
19560Sstevel@tonic-gate 			if (*(CK_BBOOL *)template[i].pValue)
19570Sstevel@tonic-gate 				attr_mask |= EXTRACTABLE_BOOL_ON;
19580Sstevel@tonic-gate 			else
19590Sstevel@tonic-gate 				attr_mask &= ~EXTRACTABLE_BOOL_ON;
19600Sstevel@tonic-gate 			break;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 		case CKA_VALUE:
19630Sstevel@tonic-gate 			isValue = 1;
19640Sstevel@tonic-gate 			if ((template[i].ulValueLen == 0) ||
19650Sstevel@tonic-gate 			    (template[i].pValue == NULL)) {
19660Sstevel@tonic-gate 				rv = CKR_ATTRIBUTE_VALUE_INVALID;
19670Sstevel@tonic-gate 				goto fail_cleanup;
19680Sstevel@tonic-gate 			}
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 			/*
19710Sstevel@tonic-gate 			 * Copyin attribute from template
19720Sstevel@tonic-gate 			 * to a local variable.
19730Sstevel@tonic-gate 			 */
19740Sstevel@tonic-gate 			sck->sk_value = malloc(template[i].ulValueLen);
19750Sstevel@tonic-gate 			if (sck->sk_value == NULL) {
19760Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
19770Sstevel@tonic-gate 				goto fail_cleanup;
19780Sstevel@tonic-gate 			}
19790Sstevel@tonic-gate 			(void) memcpy(sck->sk_value, template[i].pValue,
19800Sstevel@tonic-gate 			    template[i].ulValueLen);
19810Sstevel@tonic-gate 			sck->sk_value_len = template[i].ulValueLen;
19820Sstevel@tonic-gate 			break;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 		case CKA_VALUE_LEN:
19850Sstevel@tonic-gate 			isValueLen = 1;
19860Sstevel@tonic-gate 			break;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 		case CKA_LABEL:
19890Sstevel@tonic-gate 			isLabel = 1;
19900Sstevel@tonic-gate 			rv = get_string_from_template(&string_tmp,
19910Sstevel@tonic-gate 			    &template[i]);
19920Sstevel@tonic-gate 			if (rv != CKR_OK)
19930Sstevel@tonic-gate 				goto fail_cleanup;
19940Sstevel@tonic-gate 			break;
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 		default:
19970Sstevel@tonic-gate 			rv = kernel_parse_common_attrs(&template[i], sp,
19980Sstevel@tonic-gate 			    &attr_mask);
19990Sstevel@tonic-gate 			if (rv != CKR_OK)
20000Sstevel@tonic-gate 				goto fail_cleanup;
20010Sstevel@tonic-gate 			break;
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 		}
20040Sstevel@tonic-gate 	} /* For */
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	if (keytype == (CK_KEY_TYPE)~0UL) {
20070Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCOMPLETE;
20080Sstevel@tonic-gate 		goto fail_cleanup;
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	new_object->key_type = keytype;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	/* Supported key types of the Secret Key Object */
20140Sstevel@tonic-gate 	switch (keytype) {
20150Sstevel@tonic-gate 	case CKK_RC4:
20160Sstevel@tonic-gate 		if (!isValue) {
20170Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20180Sstevel@tonic-gate 			goto fail_cleanup;
20190Sstevel@tonic-gate 		}
20200Sstevel@tonic-gate 		if ((sck->sk_value_len < ARCFOUR_MIN_KEY_BYTES) ||
20210Sstevel@tonic-gate 		    (sck->sk_value_len > ARCFOUR_MAX_KEY_BYTES)) {
20220Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
20230Sstevel@tonic-gate 			goto fail_cleanup;
20240Sstevel@tonic-gate 		}
20250Sstevel@tonic-gate 		break;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	case CKK_GENERIC_SECRET:
20280Sstevel@tonic-gate 		if (!isValue) {
20290Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20300Sstevel@tonic-gate 			goto fail_cleanup;
20310Sstevel@tonic-gate 		}
20320Sstevel@tonic-gate 		break;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	case CKK_AES:
20350Sstevel@tonic-gate 		if (!isValue) {
20360Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20370Sstevel@tonic-gate 			goto fail_cleanup;
20380Sstevel@tonic-gate 		}
20390Sstevel@tonic-gate 		if (sck->sk_value_len < AES_MIN_KEY_BYTES) {
20400Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
20410Sstevel@tonic-gate 			goto fail_cleanup;
20420Sstevel@tonic-gate 		}
20430Sstevel@tonic-gate 		break;
20440Sstevel@tonic-gate 
2045676Sizick 	case CKK_BLOWFISH:
2046676Sizick 		if (!isValue) {
2047676Sizick 			rv = CKR_TEMPLATE_INCOMPLETE;
2048676Sizick 			goto fail_cleanup;
2049676Sizick 		}
2050676Sizick 		if (sck->sk_value_len < BLOWFISH_MINBYTES) {
2051676Sizick 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
2052676Sizick 			goto fail_cleanup;
2053676Sizick 		}
2054676Sizick 		break;
2055676Sizick 
20560Sstevel@tonic-gate 	case CKK_DES:
20570Sstevel@tonic-gate 		if (!isValue) {
20580Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20590Sstevel@tonic-gate 			goto fail_cleanup;
20600Sstevel@tonic-gate 		}
20610Sstevel@tonic-gate 		if (sck->sk_value_len != DES_KEYSIZE) {
20620Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
20630Sstevel@tonic-gate 			goto fail_cleanup;
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 		break;
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	case CKK_DES2:
20680Sstevel@tonic-gate 		if (!isValue) {
20690Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20700Sstevel@tonic-gate 			goto fail_cleanup;
20710Sstevel@tonic-gate 		}
20720Sstevel@tonic-gate 		if (sck->sk_value_len != DES2_KEYSIZE) {
20730Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
20740Sstevel@tonic-gate 			goto fail_cleanup;
20750Sstevel@tonic-gate 		}
20760Sstevel@tonic-gate 		break;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	case CKK_DES3:
20790Sstevel@tonic-gate 		if (!isValue) {
20800Sstevel@tonic-gate 			rv = CKR_TEMPLATE_INCOMPLETE;
20810Sstevel@tonic-gate 			goto fail_cleanup;
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 		if (sck->sk_value_len != DES3_KEYSIZE) {
20840Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_VALUE_INVALID;
20850Sstevel@tonic-gate 			goto fail_cleanup;
20860Sstevel@tonic-gate 		}
20870Sstevel@tonic-gate 		break;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	default:
20900Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
20910Sstevel@tonic-gate 		goto fail_cleanup;
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	if (isValueLen) {
20950Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
20960Sstevel@tonic-gate 		goto fail_cleanup;
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	/* Set up object. */
21000Sstevel@tonic-gate 	new_object->bool_attr_mask = attr_mask;
21010Sstevel@tonic-gate 	if (isLabel) {
21020Sstevel@tonic-gate 		rv = kernel_add_extra_attr(&string_tmp, new_object);
21030Sstevel@tonic-gate 		if (rv != CKR_OK)
21040Sstevel@tonic-gate 			goto fail_cleanup;
21050Sstevel@tonic-gate 		string_attr_cleanup(&string_tmp);
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	return (rv);
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate fail_cleanup:
21110Sstevel@tonic-gate 	/*
21120Sstevel@tonic-gate 	 * cleanup the storage allocated to the local variables.
21130Sstevel@tonic-gate 	 */
21140Sstevel@tonic-gate 	string_attr_cleanup(&string_tmp);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	/*
21170Sstevel@tonic-gate 	 * cleanup the storage allocated inside the object itself.
21180Sstevel@tonic-gate 	 */
21190Sstevel@tonic-gate 	kernel_cleanup_object(new_object);
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	return (rv);
21220Sstevel@tonic-gate }
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate /*
21260Sstevel@tonic-gate  * Validate the attribute types in the object's template. Then,
21270Sstevel@tonic-gate  * call the appropriate build function according to the class of
21280Sstevel@tonic-gate  * the object specified in the template.
21290Sstevel@tonic-gate  *
21300Sstevel@tonic-gate  * Note: The following classes of objects are supported:
21310Sstevel@tonic-gate  * - CKO_SECRET_KEY
21320Sstevel@tonic-gate  * - CKO_PUBLIC_KEY
21330Sstevel@tonic-gate  * - CKO_PRIVATE_KEY
21340Sstevel@tonic-gate  */
21350Sstevel@tonic-gate CK_RV
kernel_build_object(CK_ATTRIBUTE_PTR template,CK_ULONG ulAttrNum,kernel_object_t * new_object,kernel_session_t * sp,uint_t mode)21360Sstevel@tonic-gate kernel_build_object(CK_ATTRIBUTE_PTR template, CK_ULONG ulAttrNum,
21374219Smcpowers     kernel_object_t *new_object, kernel_session_t *sp, uint_t mode)
21380Sstevel@tonic-gate {
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	CK_OBJECT_CLASS class = (CK_OBJECT_CLASS)~0UL;
21410Sstevel@tonic-gate 	CK_RV 		rv = CKR_OK;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	if (template == NULL) {
21440Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	/* Validate the attribute type in the template. */
21480Sstevel@tonic-gate 	rv = kernel_validate_attr(template, ulAttrNum, &class);
21490Sstevel@tonic-gate 	if (rv != CKR_OK)
21500Sstevel@tonic-gate 		return (rv);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	if (class == (CK_OBJECT_CLASS)~0UL)
21530Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCOMPLETE);
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	/*
21560Sstevel@tonic-gate 	 * Call the appropriate function based on the supported class
21570Sstevel@tonic-gate 	 * of the object.
21580Sstevel@tonic-gate 	 */
21590Sstevel@tonic-gate 	switch (class) {
21600Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
21610Sstevel@tonic-gate 		rv = kernel_build_public_key_object(template, ulAttrNum,
21624219Smcpowers 		    new_object, sp, mode);
21630Sstevel@tonic-gate 		break;
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
21660Sstevel@tonic-gate 		rv = kernel_build_private_key_object(template, ulAttrNum,
21674219Smcpowers 		    new_object, sp, mode);
21680Sstevel@tonic-gate 		break;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	case CKO_SECRET_KEY:
21710Sstevel@tonic-gate 		rv = kernel_build_secret_key_object(template, ulAttrNum,
21720Sstevel@tonic-gate 		    new_object, sp);
21730Sstevel@tonic-gate 		break;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	case CKO_DOMAIN_PARAMETERS:
21760Sstevel@tonic-gate 	case CKO_DATA:
21770Sstevel@tonic-gate 	case CKO_CERTIFICATE:
21780Sstevel@tonic-gate 	case CKO_HW_FEATURE:
21790Sstevel@tonic-gate 	case CKO_VENDOR_DEFINED:
21800Sstevel@tonic-gate 	default:
21810Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_VALUE_INVALID);
21820Sstevel@tonic-gate 	}
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 	return (rv);
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate /*
21890Sstevel@tonic-gate  * Get the value of a requested attribute that is common to all supported
21900Sstevel@tonic-gate  * classes (i.e. public key, private key, secret key classes).
21910Sstevel@tonic-gate  */
21920Sstevel@tonic-gate CK_RV
kernel_get_common_attrs(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)21930Sstevel@tonic-gate kernel_get_common_attrs(kernel_object_t *object_p, CK_ATTRIBUTE_PTR template)
21940Sstevel@tonic-gate {
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	switch (template->type) {
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	case CKA_CLASS:
22010Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->class,
22020Sstevel@tonic-gate 		    template));
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	/* default boolean attributes */
22050Sstevel@tonic-gate 	case CKA_TOKEN:
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
22080Sstevel@tonic-gate 		if (template->pValue == NULL) {
22090Sstevel@tonic-gate 			return (CKR_OK);
22100Sstevel@tonic-gate 		}
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 		/*
22130Sstevel@tonic-gate 		 * A token object will not be created in the library, so we
22140Sstevel@tonic-gate 		 * return FALSE.
22150Sstevel@tonic-gate 		 */
22160Sstevel@tonic-gate 		*((CK_BBOOL *)template->pValue) = B_FALSE;
22170Sstevel@tonic-gate 		break;
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 	case CKA_PRIVATE:
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
22220Sstevel@tonic-gate 		if (template->pValue == NULL) {
22230Sstevel@tonic-gate 			return (CKR_OK);
22240Sstevel@tonic-gate 		}
22250Sstevel@tonic-gate 		if (object_p->bool_attr_mask & PRIVATE_BOOL_ON) {
22260Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
22270Sstevel@tonic-gate 		} else {
22280Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
22290Sstevel@tonic-gate 		}
22300Sstevel@tonic-gate 		break;
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	case CKA_MODIFIABLE:
22330Sstevel@tonic-gate 		template->ulValueLen = sizeof (CK_BBOOL);
22340Sstevel@tonic-gate 		if (template->pValue == NULL) {
22350Sstevel@tonic-gate 			return (CKR_OK);
22360Sstevel@tonic-gate 		}
22370Sstevel@tonic-gate 		if ((object_p->bool_attr_mask) & MODIFIABLE_BOOL_ON)
22380Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_TRUE;
22390Sstevel@tonic-gate 		else
22400Sstevel@tonic-gate 			*((CK_BBOOL *)template->pValue) = B_FALSE;
22410Sstevel@tonic-gate 		break;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	case CKA_LABEL:
22440Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
22450Sstevel@tonic-gate 		    template));
22460Sstevel@tonic-gate 		break;
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	default:
22490Sstevel@tonic-gate 		/*
22500Sstevel@tonic-gate 		 * The specified attribute for the object is invalid.
22510Sstevel@tonic-gate 		 * (the object does not possess such an attribute.)
22520Sstevel@tonic-gate 		 */
22530Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
22540Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
22550Sstevel@tonic-gate 	}
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	return (rv);
22580Sstevel@tonic-gate }
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate /*
22610Sstevel@tonic-gate  * Get the value of a requested attribute that is common to all key objects
22620Sstevel@tonic-gate  * (i.e. public key, private key and secret key).
22630Sstevel@tonic-gate  */
22640Sstevel@tonic-gate CK_RV
kernel_get_common_key_attrs(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)22650Sstevel@tonic-gate kernel_get_common_key_attrs(kernel_object_t *object_p,
22660Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template)
22670Sstevel@tonic-gate {
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	switch (template->type) {
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	case CKA_KEY_TYPE:
22720Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->key_type,
22730Sstevel@tonic-gate 		    template));
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	case CKA_ID:
22760Sstevel@tonic-gate 	case CKA_START_DATE:
22770Sstevel@tonic-gate 	case CKA_END_DATE:
22780Sstevel@tonic-gate 		/*
22790Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
22800Sstevel@tonic-gate 		 */
22810Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
22820Sstevel@tonic-gate 		    template));
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	/* Key related boolean attributes */
22850Sstevel@tonic-gate 	case CKA_LOCAL:
22860Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
22870Sstevel@tonic-gate 		    LOCAL_BOOL_ON, template));
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	case CKA_DERIVE:
22900Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
22910Sstevel@tonic-gate 		    DERIVE_BOOL_ON, template));
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	case CKA_KEY_GEN_MECHANISM:
22940Sstevel@tonic-gate 		return (get_ulong_attr_from_object(object_p->mechanism,
22950Sstevel@tonic-gate 		    template));
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	default:
22980Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
22990Sstevel@tonic-gate 	}
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate /*
23040Sstevel@tonic-gate  * Get the value of a requested attribute of a Public Key Object.
23050Sstevel@tonic-gate  *
23060Sstevel@tonic-gate  * Rule: All the attributes in the public key object can be revealed.
23070Sstevel@tonic-gate  */
23080Sstevel@tonic-gate CK_RV
kernel_get_public_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)23090Sstevel@tonic-gate kernel_get_public_key_attribute(kernel_object_t *object_p,
23100Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template)
23110Sstevel@tonic-gate {
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
23140Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	switch (template->type) {
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	case CKA_SUBJECT:
23194219Smcpowers 	case CKA_EC_PARAMS:
23200Sstevel@tonic-gate 		/*
23210Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
23220Sstevel@tonic-gate 		 */
23230Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
23240Sstevel@tonic-gate 		    template));
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	/* Key related boolean attributes */
23270Sstevel@tonic-gate 	case CKA_ENCRYPT:
23280Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
23290Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 	case CKA_VERIFY:
23320Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
23330Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	case CKA_VERIFY_RECOVER:
23360Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
23370Sstevel@tonic-gate 		    VERIFY_RECOVER_BOOL_ON, template));
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	case CKA_WRAP:
23400Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
23410Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	case CKA_TRUSTED:
23440Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
23450Sstevel@tonic-gate 		    TRUSTED_BOOL_ON, template));
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	case CKA_MODULUS:
23480Sstevel@tonic-gate 		/*
23490Sstevel@tonic-gate 		 * This attribute is valid only for RSA public key
23500Sstevel@tonic-gate 		 * object.
23510Sstevel@tonic-gate 		 */
23520Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
23530Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
23540Sstevel@tonic-gate 			    OBJ_PUB_RSA_MOD(object_p), template));
23550Sstevel@tonic-gate 		} else {
23560Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
23570Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
23580Sstevel@tonic-gate 		}
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
23610Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
23620Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
23630Sstevel@tonic-gate 			    OBJ_PUB_RSA_PUBEXPO(object_p), template));
23640Sstevel@tonic-gate 		} else {
23650Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
23660Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
23670Sstevel@tonic-gate 		}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	case CKA_MODULUS_BITS:
23700Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
23710Sstevel@tonic-gate 			return (get_ulong_attr_from_object(
23720Sstevel@tonic-gate 			    OBJ_PUB_RSA_MOD_BITS(object_p), template));
23730Sstevel@tonic-gate 		} else {
23740Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
23750Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
23760Sstevel@tonic-gate 		}
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	case CKA_PRIME:
23790Sstevel@tonic-gate 		switch (keytype) {
23800Sstevel@tonic-gate 		case CKK_DSA:
23810Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
23820Sstevel@tonic-gate 			    OBJ_PUB_DSA_PRIME(object_p), template));
23834219Smcpowers 		case CKK_DH:
23844219Smcpowers 			return (get_bigint_attr_from_object(
23854219Smcpowers 			    OBJ_PUB_DH_PRIME(object_p), template));
23860Sstevel@tonic-gate 		default:
23870Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
23880Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
23890Sstevel@tonic-gate 		}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	case CKA_SUBPRIME:
23920Sstevel@tonic-gate 		switch (keytype) {
23930Sstevel@tonic-gate 		case CKK_DSA:
23940Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
23950Sstevel@tonic-gate 			    OBJ_PUB_DSA_SUBPRIME(object_p), template));
23960Sstevel@tonic-gate 		default:
23970Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
23980Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
23990Sstevel@tonic-gate 		}
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	case CKA_BASE:
24020Sstevel@tonic-gate 		switch (keytype) {
24030Sstevel@tonic-gate 		case CKK_DSA:
24040Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
24050Sstevel@tonic-gate 			    OBJ_PUB_DSA_BASE(object_p), template));
24064219Smcpowers 		case CKK_DH:
24074219Smcpowers 			return (get_bigint_attr_from_object(
24084219Smcpowers 			    OBJ_PUB_DH_BASE(object_p), template));
24090Sstevel@tonic-gate 		default:
24100Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
24110Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
24120Sstevel@tonic-gate 		}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	case CKA_VALUE:
24150Sstevel@tonic-gate 		switch (keytype) {
24160Sstevel@tonic-gate 		case CKK_DSA:
24170Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
24180Sstevel@tonic-gate 			    OBJ_PUB_DSA_VALUE(object_p), template));
24194219Smcpowers 		case CKK_DH:
24204219Smcpowers 			return (get_bigint_attr_from_object(
24214219Smcpowers 			    OBJ_PUB_DH_VALUE(object_p), template));
24220Sstevel@tonic-gate 		default:
24230Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
24240Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
24250Sstevel@tonic-gate 		}
24260Sstevel@tonic-gate 
24274219Smcpowers 	case CKA_EC_POINT:
24284219Smcpowers 		switch (keytype) {
24294219Smcpowers 		case CKK_EC:
24304219Smcpowers 			return (get_bigint_attr_from_object(
24314219Smcpowers 			    OBJ_PUB_EC_POINT(object_p), template));
24324219Smcpowers 		default:
24334219Smcpowers 			template->ulValueLen = (CK_ULONG)-1;
24344219Smcpowers 			return (CKR_ATTRIBUTE_TYPE_INVALID);
24354219Smcpowers 		}
24360Sstevel@tonic-gate 	default:
24370Sstevel@tonic-gate 		/*
24380Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
24390Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
24400Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
24410Sstevel@tonic-gate 		 * attribute from the list of common attributes.
24420Sstevel@tonic-gate 		 */
24430Sstevel@tonic-gate 		rv = kernel_get_common_key_attrs(object_p, template);
24440Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
24450Sstevel@tonic-gate 			rv = kernel_get_common_attrs(object_p, template);
24460Sstevel@tonic-gate 		}
24470Sstevel@tonic-gate 		break;
24480Sstevel@tonic-gate 	}
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	return (rv);
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate /*
24550Sstevel@tonic-gate  * Get the value of a requested attribute of a Private Key Object.
24560Sstevel@tonic-gate  *
24570Sstevel@tonic-gate  * Rule: All the attributes in the private key object can be revealed
24580Sstevel@tonic-gate  *       except those marked with footnote number "7" when the object
24590Sstevel@tonic-gate  *       has its CKA_SENSITIVE attribute set to TRUE or its
24600Sstevel@tonic-gate  *       CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
24610Sstevel@tonic-gate  */
24620Sstevel@tonic-gate CK_RV
kernel_get_private_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)24630Sstevel@tonic-gate kernel_get_private_key_attribute(kernel_object_t *object_p,
24640Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template)
24650Sstevel@tonic-gate {
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
24680Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	/*
24720Sstevel@tonic-gate 	 * If the following specified attributes for the private key
24730Sstevel@tonic-gate 	 * object cannot be revealed because the object is sensitive
24740Sstevel@tonic-gate 	 * or unextractable, then the ulValueLen is set to -1.
24750Sstevel@tonic-gate 	 */
24760Sstevel@tonic-gate 	if ((object_p->bool_attr_mask & SENSITIVE_BOOL_ON) ||
24770Sstevel@tonic-gate 	    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 		switch (template->type) {
24800Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
24810Sstevel@tonic-gate 		case CKA_PRIME_1:
24820Sstevel@tonic-gate 		case CKA_PRIME_2:
24830Sstevel@tonic-gate 		case CKA_EXPONENT_1:
24840Sstevel@tonic-gate 		case CKA_EXPONENT_2:
24850Sstevel@tonic-gate 		case CKA_COEFFICIENT:
24860Sstevel@tonic-gate 		case CKA_VALUE:
24870Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
24880Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_SENSITIVE);
24890Sstevel@tonic-gate 		}
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	switch (template->type) {
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 	case CKA_SUBJECT:
24954219Smcpowers 	case CKA_EC_PARAMS:
24960Sstevel@tonic-gate 		/*
24970Sstevel@tonic-gate 		 * The above extra attributes have byte array type.
24980Sstevel@tonic-gate 		 */
24990Sstevel@tonic-gate 		return (get_extra_attr_from_object(object_p,
25000Sstevel@tonic-gate 		    template));
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	/* Key related boolean attributes */
25030Sstevel@tonic-gate 	case CKA_SENSITIVE:
25040Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25050Sstevel@tonic-gate 		    SENSITIVE_BOOL_ON, template));
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	case CKA_SECONDARY_AUTH:
25080Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25090Sstevel@tonic-gate 		    SECONDARY_AUTH_BOOL_ON, template));
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	case CKA_DECRYPT:
25120Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25130Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	case CKA_SIGN:
25160Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25170Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	case CKA_SIGN_RECOVER:
25200Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25210Sstevel@tonic-gate 		    SIGN_RECOVER_BOOL_ON, template));
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	case CKA_UNWRAP:
25240Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25250Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
25280Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25290Sstevel@tonic-gate 		    EXTRACTABLE_BOOL_ON, template));
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	case CKA_ALWAYS_SENSITIVE:
25320Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25330Sstevel@tonic-gate 		    ALWAYS_SENSITIVE_BOOL_ON, template));
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	case CKA_NEVER_EXTRACTABLE:
25360Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
25370Sstevel@tonic-gate 		    NEVER_EXTRACTABLE_BOOL_ON, template));
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	case CKA_MODULUS:
25400Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25410Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25420Sstevel@tonic-gate 			    OBJ_PRI_RSA_MOD(object_p), template));
25430Sstevel@tonic-gate 		} else {
25440Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25450Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25460Sstevel@tonic-gate 			break;
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
25500Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25510Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25520Sstevel@tonic-gate 			    OBJ_PRI_RSA_PUBEXPO(object_p), template));
25530Sstevel@tonic-gate 		} else {
25540Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25550Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25560Sstevel@tonic-gate 			break;
25570Sstevel@tonic-gate 		}
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	case CKA_PRIVATE_EXPONENT:
25600Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25610Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25620Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIEXPO(object_p), template));
25630Sstevel@tonic-gate 		} else {
25640Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25650Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25660Sstevel@tonic-gate 			break;
25670Sstevel@tonic-gate 		}
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	case CKA_PRIME_1:
25700Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25710Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25720Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIME1(object_p), template));
25730Sstevel@tonic-gate 		} else {
25740Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25750Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25760Sstevel@tonic-gate 			break;
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	case CKA_PRIME_2:
25800Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25810Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25820Sstevel@tonic-gate 			    OBJ_PRI_RSA_PRIME2(object_p), template));
25830Sstevel@tonic-gate 		} else {
25840Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25850Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25860Sstevel@tonic-gate 			break;
25870Sstevel@tonic-gate 		}
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	case CKA_EXPONENT_1:
25900Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
25910Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
25920Sstevel@tonic-gate 			    OBJ_PRI_RSA_EXPO1(object_p), template));
25930Sstevel@tonic-gate 		} else {
25940Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
25950Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
25960Sstevel@tonic-gate 			break;
25970Sstevel@tonic-gate 		}
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	case CKA_EXPONENT_2:
26000Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
26010Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26020Sstevel@tonic-gate 			    OBJ_PRI_RSA_EXPO2(object_p), template));
26030Sstevel@tonic-gate 		} else {
26040Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26050Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
26060Sstevel@tonic-gate 			break;
26070Sstevel@tonic-gate 		}
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	case CKA_COEFFICIENT:
26100Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
26110Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26120Sstevel@tonic-gate 			    OBJ_PRI_RSA_COEF(object_p), template));
26130Sstevel@tonic-gate 		} else {
26140Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26150Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
26160Sstevel@tonic-gate 			break;
26170Sstevel@tonic-gate 		}
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	case CKA_VALUE_BITS:
26204219Smcpowers 		if (keytype == CKK_DH) {
26214219Smcpowers 			return (get_ulong_attr_from_object(
26224219Smcpowers 			    OBJ_PRI_DH_VAL_BITS(object_p), template));
26234219Smcpowers 		} else {
26244219Smcpowers 			template->ulValueLen = (CK_ULONG)-1;
26254219Smcpowers 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
26264219Smcpowers 			break;
26274219Smcpowers 		}
26284219Smcpowers 
26290Sstevel@tonic-gate 	case CKA_PRIME:
26300Sstevel@tonic-gate 		switch (keytype) {
26310Sstevel@tonic-gate 		case CKK_DSA:
26320Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26330Sstevel@tonic-gate 			    OBJ_PRI_DSA_PRIME(object_p), template));
26344219Smcpowers 		case CKK_DH:
26354219Smcpowers 			return (get_bigint_attr_from_object(
26364219Smcpowers 			    OBJ_PRI_DH_PRIME(object_p), template));
26370Sstevel@tonic-gate 		default:
26380Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26390Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
26400Sstevel@tonic-gate 		}
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	case CKA_SUBPRIME:
26430Sstevel@tonic-gate 		switch (keytype) {
26440Sstevel@tonic-gate 		case CKK_DSA:
26450Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26460Sstevel@tonic-gate 			    OBJ_PRI_DSA_SUBPRIME(object_p), template));
26470Sstevel@tonic-gate 		default:
26480Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26490Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
26500Sstevel@tonic-gate 		}
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	case CKA_BASE:
26530Sstevel@tonic-gate 		switch (keytype) {
26540Sstevel@tonic-gate 		case CKK_DSA:
26550Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26560Sstevel@tonic-gate 			    OBJ_PRI_DSA_BASE(object_p), template));
26574219Smcpowers 		case CKK_DH:
26584219Smcpowers 			return (get_bigint_attr_from_object(
26594219Smcpowers 			    OBJ_PRI_DH_BASE(object_p), template));
26600Sstevel@tonic-gate 		default:
26610Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26620Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
26630Sstevel@tonic-gate 		}
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	case CKA_VALUE:
26660Sstevel@tonic-gate 		switch (keytype) {
26670Sstevel@tonic-gate 		case CKK_DSA:
26680Sstevel@tonic-gate 			return (get_bigint_attr_from_object(
26690Sstevel@tonic-gate 			    OBJ_PRI_DSA_VALUE(object_p), template));
26704219Smcpowers 		case CKK_DH:
26714219Smcpowers 			return (get_bigint_attr_from_object(
26724219Smcpowers 			    OBJ_PRI_DH_VALUE(object_p), template));
26734219Smcpowers 		case CKK_EC:
26744219Smcpowers 			return (get_bigint_attr_from_object(
26754219Smcpowers 			    OBJ_PRI_EC_VALUE(object_p), template));
26760Sstevel@tonic-gate 		default:
26770Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
26780Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_TYPE_INVALID);
26790Sstevel@tonic-gate 		}
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	default:
26820Sstevel@tonic-gate 		/*
26830Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
26840Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
26850Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
26860Sstevel@tonic-gate 		 * attribute from the list of common attributes.
26870Sstevel@tonic-gate 		 */
26880Sstevel@tonic-gate 		rv = kernel_get_common_key_attrs(object_p, template);
26890Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
26900Sstevel@tonic-gate 			rv = kernel_get_common_attrs(object_p, template);
26910Sstevel@tonic-gate 		}
26920Sstevel@tonic-gate 		break;
26930Sstevel@tonic-gate 	}
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	return (rv);
26960Sstevel@tonic-gate }
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate /*
27000Sstevel@tonic-gate  * Get the value of a requested attribute of a Secret Key Object.
27010Sstevel@tonic-gate  *
27020Sstevel@tonic-gate  * Rule: All the attributes in the secret key object can be revealed
27030Sstevel@tonic-gate  *       except those marked with footnote number "7" when the object
27040Sstevel@tonic-gate  *       has its CKA_SENSITIVE attribute set to TRUE or its
27050Sstevel@tonic-gate  *       CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
27060Sstevel@tonic-gate  */
27070Sstevel@tonic-gate CK_RV
kernel_get_secret_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)27080Sstevel@tonic-gate kernel_get_secret_key_attribute(kernel_object_t *object_p,
27090Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template)
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
27130Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	switch (template->type) {
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 	/* Key related boolean attributes */
27180Sstevel@tonic-gate 	case CKA_SENSITIVE:
27190Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27200Sstevel@tonic-gate 		    SENSITIVE_BOOL_ON, template));
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	case CKA_ENCRYPT:
27230Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27240Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	case CKA_DECRYPT:
27270Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27280Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	case CKA_SIGN:
27310Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27320Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	case CKA_VERIFY:
27350Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27360Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	case CKA_WRAP:
27390Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27400Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	case CKA_UNWRAP:
27430Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27440Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
27470Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27480Sstevel@tonic-gate 		    EXTRACTABLE_BOOL_ON, template));
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 	case CKA_ALWAYS_SENSITIVE:
27510Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27520Sstevel@tonic-gate 		    ALWAYS_SENSITIVE_BOOL_ON, template));
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	case CKA_NEVER_EXTRACTABLE:
27550Sstevel@tonic-gate 		return (get_bool_attr_from_object(object_p,
27560Sstevel@tonic-gate 		    NEVER_EXTRACTABLE_BOOL_ON, template));
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 	case CKA_VALUE:
27590Sstevel@tonic-gate 		/*
27600Sstevel@tonic-gate 		 * If the specified attribute for the secret key object
27610Sstevel@tonic-gate 		 * cannot be revealed because the object is sensitive
27620Sstevel@tonic-gate 		 * or unextractable, then the ulValueLen is set to -1.
27630Sstevel@tonic-gate 		 */
27640Sstevel@tonic-gate 		if ((object_p->bool_attr_mask & SENSITIVE_BOOL_ON) ||
27650Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
27660Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
27670Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_SENSITIVE);
27680Sstevel@tonic-gate 		}
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 		switch (keytype) {
27710Sstevel@tonic-gate 		case CKK_RC4:
27720Sstevel@tonic-gate 		case CKK_GENERIC_SECRET:
27730Sstevel@tonic-gate 		case CKK_RC5:
27740Sstevel@tonic-gate 		case CKK_DES:
27750Sstevel@tonic-gate 		case CKK_DES2:
27760Sstevel@tonic-gate 		case CKK_DES3:
27770Sstevel@tonic-gate 		case CKK_CDMF:
27780Sstevel@tonic-gate 		case CKK_AES:
2779676Sizick 		case CKK_BLOWFISH:
27800Sstevel@tonic-gate 			/*
27810Sstevel@tonic-gate 			 * Copy secret key object attributes to template.
27820Sstevel@tonic-gate 			 */
27830Sstevel@tonic-gate 			if (template->pValue == NULL) {
27840Sstevel@tonic-gate 				template->ulValueLen =
27850Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(object_p);
27860Sstevel@tonic-gate 				return (CKR_OK);
27870Sstevel@tonic-gate 			}
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 			if (OBJ_SEC_VALUE(object_p) == NULL) {
27900Sstevel@tonic-gate 				template->ulValueLen = 0;
27910Sstevel@tonic-gate 				return (CKR_OK);
27920Sstevel@tonic-gate 			}
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 			if (template->ulValueLen >=
27950Sstevel@tonic-gate 			    OBJ_SEC_VALUE_LEN(object_p)) {
27960Sstevel@tonic-gate 				(void) memcpy(template->pValue,
27970Sstevel@tonic-gate 				    OBJ_SEC_VALUE(object_p),
27980Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(object_p));
27990Sstevel@tonic-gate 				template->ulValueLen =
28000Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(object_p);
28010Sstevel@tonic-gate 				return (CKR_OK);
28020Sstevel@tonic-gate 			} else {
28030Sstevel@tonic-gate 				template->ulValueLen = (CK_ULONG)-1;
28040Sstevel@tonic-gate 				return (CKR_BUFFER_TOO_SMALL);
28050Sstevel@tonic-gate 			}
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 		default:
28080Sstevel@tonic-gate 			template->ulValueLen = (CK_ULONG)-1;
28090Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_TYPE_INVALID;
28100Sstevel@tonic-gate 			break;
28110Sstevel@tonic-gate 		}
28120Sstevel@tonic-gate 		break;
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	case CKA_VALUE_LEN:
28150Sstevel@tonic-gate 		return (get_ulong_attr_from_object(OBJ_SEC_VALUE_LEN(object_p),
28160Sstevel@tonic-gate 		    template));
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	default:
28190Sstevel@tonic-gate 		/*
28200Sstevel@tonic-gate 		 * First, get the value of the request attribute defined
28210Sstevel@tonic-gate 		 * in the list of common key attributes. If the request
28220Sstevel@tonic-gate 		 * attribute is not found in that list, then get the
28230Sstevel@tonic-gate 		 * attribute from the list of common attributes.
28240Sstevel@tonic-gate 		 */
28250Sstevel@tonic-gate 		rv = kernel_get_common_key_attrs(object_p, template);
28260Sstevel@tonic-gate 		if (rv == CKR_ATTRIBUTE_TYPE_INVALID) {
28270Sstevel@tonic-gate 			rv = kernel_get_common_attrs(object_p, template);
28280Sstevel@tonic-gate 		}
28290Sstevel@tonic-gate 		break;
28300Sstevel@tonic-gate 	}
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	return (rv);
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate /*
28400Sstevel@tonic-gate  * Call the appropriate get attribute function according to the class
28410Sstevel@tonic-gate  * of object.
28420Sstevel@tonic-gate  *
28430Sstevel@tonic-gate  * The caller of this function holds the lock on the object.
28440Sstevel@tonic-gate  */
28450Sstevel@tonic-gate CK_RV
kernel_get_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template)28460Sstevel@tonic-gate kernel_get_attribute(kernel_object_t *object_p, CK_ATTRIBUTE_PTR template)
28470Sstevel@tonic-gate {
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
28500Sstevel@tonic-gate 	CK_OBJECT_CLASS class = object_p->class;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	switch (class) {
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
28550Sstevel@tonic-gate 		rv =  kernel_get_public_key_attribute(object_p, template);
28560Sstevel@tonic-gate 		break;
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
28590Sstevel@tonic-gate 		rv = kernel_get_private_key_attribute(object_p, template);
28600Sstevel@tonic-gate 		break;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	case CKO_SECRET_KEY:
28630Sstevel@tonic-gate 		rv = kernel_get_secret_key_attribute(object_p, template);
28640Sstevel@tonic-gate 		break;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	default:
28670Sstevel@tonic-gate 		/*
28680Sstevel@tonic-gate 		 * If the specified attribute for the object is invalid
28690Sstevel@tonic-gate 		 * (the object does not possess such as attribute), then
28700Sstevel@tonic-gate 		 * the ulValueLen is modified to hold the value -1.
28710Sstevel@tonic-gate 		 */
28720Sstevel@tonic-gate 		template->ulValueLen = (CK_ULONG)-1;
28730Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_TYPE_INVALID);
28740Sstevel@tonic-gate 	}
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	return (rv);
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate }
28790Sstevel@tonic-gate 
28800Sstevel@tonic-gate /*
28810Sstevel@tonic-gate  * Set the value of an attribute that is common to all key objects
28820Sstevel@tonic-gate  * (i.e. public key, private key and secret key).
28830Sstevel@tonic-gate  */
28840Sstevel@tonic-gate CK_RV
kernel_set_common_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template,boolean_t copy,kernel_session_t * sp)28850Sstevel@tonic-gate kernel_set_common_key_attribute(kernel_object_t *object_p,
28860Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template, boolean_t copy, kernel_session_t *sp)
28870Sstevel@tonic-gate {
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	kernel_slot_t *pslot = slot_table[sp->ses_slotid];
28900Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
28910Sstevel@tonic-gate 
28920Sstevel@tonic-gate 	switch (template->type) {
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	case CKA_LABEL:
28950Sstevel@tonic-gate 		/*
28960Sstevel@tonic-gate 		 * Only the LABEL can be modified in the common storage
28970Sstevel@tonic-gate 		 * object attributes after the object is created.
28980Sstevel@tonic-gate 		 */
28990Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
29000Sstevel@tonic-gate 		    CKA_LABEL, template));
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	case CKA_ID:
29030Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
29040Sstevel@tonic-gate 		    CKA_ID, template));
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	case CKA_START_DATE:
29070Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
29080Sstevel@tonic-gate 		    CKA_START_DATE, template));
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 	case CKA_END_DATE:
29110Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
29120Sstevel@tonic-gate 		    CKA_END_DATE, template));
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	case CKA_DERIVE:
29150Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
29160Sstevel@tonic-gate 		    DERIVE_BOOL_ON, template));
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	case CKA_CLASS:
29190Sstevel@tonic-gate 	case CKA_KEY_TYPE:
29200Sstevel@tonic-gate 	case CKA_LOCAL:
29210Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_READ_ONLY);
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	case CKA_PRIVATE:
29240Sstevel@tonic-gate 		if (!copy) {
29250Sstevel@tonic-gate 			/* called from C_SetAttributeValue() */
29260Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
29270Sstevel@tonic-gate 		}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 		/* called from C_CopyObject() */
29300Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) != B_TRUE) {
29310Sstevel@tonic-gate 			return (CKR_OK);
29320Sstevel@tonic-gate 		}
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 		(void) pthread_mutex_lock(&pslot->sl_mutex);
29354219Smcpowers 		/*
29364219Smcpowers 		 * Cannot create a private object if the token
29374219Smcpowers 		 * has a keystore and the user isn't logged in.
29384219Smcpowers 		 */
29394219Smcpowers 		if (pslot->sl_func_list.fl_object_create &&
29404219Smcpowers 		    pslot->sl_state != CKU_USER) {
29410Sstevel@tonic-gate 			rv = CKR_USER_NOT_LOGGED_IN;
29420Sstevel@tonic-gate 		} else {
29430Sstevel@tonic-gate 			rv = set_bool_attr_to_object(object_p,
29440Sstevel@tonic-gate 			    PRIVATE_BOOL_ON, template);
29450Sstevel@tonic-gate 		}
29460Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&pslot->sl_mutex);
29470Sstevel@tonic-gate 		return (rv);
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	case CKA_MODIFIABLE:
29500Sstevel@tonic-gate 		if (copy) {
29510Sstevel@tonic-gate 			rv = set_bool_attr_to_object(object_p,
29520Sstevel@tonic-gate 			    MODIFIABLE_BOOL_ON, template);
29530Sstevel@tonic-gate 		} else {
29540Sstevel@tonic-gate 			rv = CKR_ATTRIBUTE_READ_ONLY;
29550Sstevel@tonic-gate 		}
29560Sstevel@tonic-gate 		return (rv);
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	default:
29590Sstevel@tonic-gate 		return (CKR_TEMPLATE_INCONSISTENT);
29600Sstevel@tonic-gate 	}
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate }
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate /*
29660Sstevel@tonic-gate  * Set the value of an attribute of a Public Key Object.
29670Sstevel@tonic-gate  *
29680Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
29690Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
29700Sstevel@tonic-gate  */
29710Sstevel@tonic-gate CK_RV
kernel_set_public_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template,boolean_t copy,kernel_session_t * sp)29720Sstevel@tonic-gate kernel_set_public_key_attribute(kernel_object_t *object_p,
29730Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template, boolean_t copy, kernel_session_t *sp)
29740Sstevel@tonic-gate {
29750Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 	switch (template->type) {
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	case CKA_SUBJECT:
29800Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
29810Sstevel@tonic-gate 		    CKA_SUBJECT, template));
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	case CKA_ENCRYPT:
29840Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
29850Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	case CKA_VERIFY:
29880Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
29890Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	case CKA_VERIFY_RECOVER:
29920Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
29930Sstevel@tonic-gate 		    VERIFY_RECOVER_BOOL_ON, template));
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	case CKA_WRAP:
29960Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
29970Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	case CKA_MODULUS:
30000Sstevel@tonic-gate 	case CKA_MODULUS_BITS:
30010Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
30020Sstevel@tonic-gate 		if (keytype == CKK_RSA)
30030Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
30040Sstevel@tonic-gate 		break;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	case CKA_SUBPRIME:
30070Sstevel@tonic-gate 	case CKA_PRIME:
30080Sstevel@tonic-gate 	case CKA_BASE:
30090Sstevel@tonic-gate 	case CKA_VALUE:
30100Sstevel@tonic-gate 		if (keytype == CKK_DSA)
30110Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
30120Sstevel@tonic-gate 		break;
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	default:
30150Sstevel@tonic-gate 		/*
30160Sstevel@tonic-gate 		 * Set the value of a common key attribute.
30170Sstevel@tonic-gate 		 */
30180Sstevel@tonic-gate 		return (kernel_set_common_key_attribute(object_p,
30190Sstevel@tonic-gate 		    template, copy, sp));
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	}
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 	/*
30240Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
30250Sstevel@tonic-gate 	 * and requested attribute is invalid.
30260Sstevel@tonic-gate 	 */
30270Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
30280Sstevel@tonic-gate }
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate /*
30320Sstevel@tonic-gate  * Set the value of an attribute of a Private Key Object.
30330Sstevel@tonic-gate  *
30340Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
30350Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
30360Sstevel@tonic-gate  */
30370Sstevel@tonic-gate CK_RV
kernel_set_private_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template,boolean_t copy,kernel_session_t * sp)30380Sstevel@tonic-gate kernel_set_private_key_attribute(kernel_object_t *object_p,
30390Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template, boolean_t copy, kernel_session_t *sp)
30400Sstevel@tonic-gate {
30410Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	switch (template->type) {
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	case CKA_SUBJECT:
30460Sstevel@tonic-gate 		return (set_extra_attr_to_object(object_p,
30470Sstevel@tonic-gate 		    CKA_SUBJECT, template));
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	case CKA_SENSITIVE:
30500Sstevel@tonic-gate 		/*
30510Sstevel@tonic-gate 		 * Cannot set SENSITIVE to FALSE if it is already ON.
30520Sstevel@tonic-gate 		 */
30530Sstevel@tonic-gate 		if (((*(CK_BBOOL *)template->pValue) == B_FALSE) &&
30540Sstevel@tonic-gate 		    (object_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
30550Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
30560Sstevel@tonic-gate 		}
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 		if (*(CK_BBOOL *)template->pValue)
30590Sstevel@tonic-gate 			object_p->bool_attr_mask |= SENSITIVE_BOOL_ON;
30600Sstevel@tonic-gate 		return (CKR_OK);
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	case CKA_DECRYPT:
30630Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
30640Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	case CKA_SIGN:
30670Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
30680Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	case CKA_SIGN_RECOVER:
30710Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
30720Sstevel@tonic-gate 		    SIGN_RECOVER_BOOL_ON, template));
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	case CKA_UNWRAP:
30750Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
30760Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
30790Sstevel@tonic-gate 		/*
30800Sstevel@tonic-gate 		 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
30810Sstevel@tonic-gate 		 */
30820Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) &&
30830Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
30840Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
30850Sstevel@tonic-gate 		}
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_FALSE)
30880Sstevel@tonic-gate 			object_p->bool_attr_mask &= ~EXTRACTABLE_BOOL_ON;
30890Sstevel@tonic-gate 		return (CKR_OK);
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 	case CKA_MODULUS:
30920Sstevel@tonic-gate 	case CKA_PUBLIC_EXPONENT:
30930Sstevel@tonic-gate 	case CKA_PRIVATE_EXPONENT:
30940Sstevel@tonic-gate 	case CKA_PRIME_1:
30950Sstevel@tonic-gate 	case CKA_PRIME_2:
30960Sstevel@tonic-gate 	case CKA_EXPONENT_1:
30970Sstevel@tonic-gate 	case CKA_EXPONENT_2:
30980Sstevel@tonic-gate 	case CKA_COEFFICIENT:
30990Sstevel@tonic-gate 		if (keytype == CKK_RSA) {
31000Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
31010Sstevel@tonic-gate 		}
31020Sstevel@tonic-gate 		break;
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 	case CKA_SUBPRIME:
31050Sstevel@tonic-gate 	case CKA_PRIME:
31060Sstevel@tonic-gate 	case CKA_BASE:
31070Sstevel@tonic-gate 	case CKA_VALUE:
31080Sstevel@tonic-gate 		if (keytype == CKK_DSA)
31090Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
31100Sstevel@tonic-gate 		break;
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 	default:
31130Sstevel@tonic-gate 		/*
31140Sstevel@tonic-gate 		 * Set the value of a common key attribute.
31150Sstevel@tonic-gate 		 */
31160Sstevel@tonic-gate 		return (kernel_set_common_key_attribute(object_p,
31170Sstevel@tonic-gate 		    template, copy, sp));
31180Sstevel@tonic-gate 	}
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	/*
31210Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
31220Sstevel@tonic-gate 	 * and requested attribute is invalid.
31230Sstevel@tonic-gate 	 */
31240Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
31250Sstevel@tonic-gate }
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate /*
31300Sstevel@tonic-gate  * Set the value of an attribute of a Secret Key Object.
31310Sstevel@tonic-gate  *
31320Sstevel@tonic-gate  * Rule: The attributes marked with footnote number "8" in the PKCS11
31330Sstevel@tonic-gate  *       spec may be modified (p.88 in PKCS11 spec.).
31340Sstevel@tonic-gate  */
31350Sstevel@tonic-gate CK_RV
kernel_set_secret_key_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template,boolean_t copy,kernel_session_t * sp)31360Sstevel@tonic-gate kernel_set_secret_key_attribute(kernel_object_t *object_p,
31370Sstevel@tonic-gate     CK_ATTRIBUTE_PTR template, boolean_t copy, kernel_session_t *sp)
31380Sstevel@tonic-gate {
31390Sstevel@tonic-gate 	CK_KEY_TYPE	keytype = object_p->key_type;
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	switch (template->type) {
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	case CKA_SENSITIVE:
31440Sstevel@tonic-gate 		/*
31450Sstevel@tonic-gate 		 * Cannot set SENSITIVE to FALSE if it is already ON.
31460Sstevel@tonic-gate 		 */
31470Sstevel@tonic-gate 		if (((*(CK_BBOOL *)template->pValue) == B_FALSE) &&
31480Sstevel@tonic-gate 		    (object_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
31490Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
31500Sstevel@tonic-gate 		}
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 		if (*(CK_BBOOL *)template->pValue)
31530Sstevel@tonic-gate 			object_p->bool_attr_mask |= SENSITIVE_BOOL_ON;
31540Sstevel@tonic-gate 		return (CKR_OK);
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 	case CKA_ENCRYPT:
31570Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31580Sstevel@tonic-gate 		    ENCRYPT_BOOL_ON, template));
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 	case CKA_DECRYPT:
31610Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31620Sstevel@tonic-gate 		    DECRYPT_BOOL_ON, template));
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate 	case CKA_SIGN:
31650Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31660Sstevel@tonic-gate 		    SIGN_BOOL_ON, template));
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	case CKA_VERIFY:
31690Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31700Sstevel@tonic-gate 		    VERIFY_BOOL_ON, template));
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	case CKA_WRAP:
31730Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31740Sstevel@tonic-gate 		    WRAP_BOOL_ON, template));
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	case CKA_UNWRAP:
31770Sstevel@tonic-gate 		return (set_bool_attr_to_object(object_p,
31780Sstevel@tonic-gate 		    UNWRAP_BOOL_ON, template));
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	case CKA_EXTRACTABLE:
31810Sstevel@tonic-gate 		/*
31820Sstevel@tonic-gate 		 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
31830Sstevel@tonic-gate 		 */
31840Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) &&
31850Sstevel@tonic-gate 		    !(object_p->bool_attr_mask & EXTRACTABLE_BOOL_ON)) {
31860Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
31870Sstevel@tonic-gate 		}
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 		if ((*(CK_BBOOL *)template->pValue) == B_FALSE)
31900Sstevel@tonic-gate 			object_p->bool_attr_mask &= ~EXTRACTABLE_BOOL_ON;
31910Sstevel@tonic-gate 		return (CKR_OK);
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate 	case CKA_VALUE:
31940Sstevel@tonic-gate 		return (CKR_ATTRIBUTE_READ_ONLY);
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 	case CKA_VALUE_LEN:
31970Sstevel@tonic-gate 		if ((keytype == CKK_RC4) ||
31980Sstevel@tonic-gate 		    (keytype == CKK_GENERIC_SECRET) ||
3199676Sizick 		    (keytype == CKK_AES) ||
3200676Sizick 		    (keytype == CKK_BLOWFISH))
32010Sstevel@tonic-gate 			return (CKR_ATTRIBUTE_READ_ONLY);
32020Sstevel@tonic-gate 		break;
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 	default:
32050Sstevel@tonic-gate 		/*
32060Sstevel@tonic-gate 		 * Set the value of a common key attribute.
32070Sstevel@tonic-gate 		 */
32080Sstevel@tonic-gate 		return (kernel_set_common_key_attribute(object_p,
32090Sstevel@tonic-gate 		    template, copy, sp));
32100Sstevel@tonic-gate 	}
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 	/*
32130Sstevel@tonic-gate 	 * If we got this far, then the combination of key type
32140Sstevel@tonic-gate 	 * and requested attribute is invalid.
32150Sstevel@tonic-gate 	 */
32160Sstevel@tonic-gate 	return (CKR_ATTRIBUTE_TYPE_INVALID);
32170Sstevel@tonic-gate }
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate /*
32210Sstevel@tonic-gate  * Call the appropriate set attribute function according to the class
32220Sstevel@tonic-gate  * of object.
32230Sstevel@tonic-gate  *
32240Sstevel@tonic-gate  * The caller of this function does not hold the lock on the original
32250Sstevel@tonic-gate  * object, since this function is setting the attribute on the new object
32260Sstevel@tonic-gate  * that is being modified.
32270Sstevel@tonic-gate  *
32280Sstevel@tonic-gate  */
32290Sstevel@tonic-gate CK_RV
kernel_set_attribute(kernel_object_t * object_p,CK_ATTRIBUTE_PTR template,boolean_t copy,kernel_session_t * sp)32300Sstevel@tonic-gate kernel_set_attribute(kernel_object_t *object_p, CK_ATTRIBUTE_PTR template,
32310Sstevel@tonic-gate     boolean_t copy, kernel_session_t *sp)
32320Sstevel@tonic-gate {
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	CK_RV		rv = CKR_OK;
32350Sstevel@tonic-gate 	CK_OBJECT_CLASS	class = object_p->class;
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate 	switch (class) {
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 	case CKO_PUBLIC_KEY:
32400Sstevel@tonic-gate 		rv = kernel_set_public_key_attribute(object_p, template,
32410Sstevel@tonic-gate 		    copy, sp);
32420Sstevel@tonic-gate 		break;
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	case CKO_PRIVATE_KEY:
32450Sstevel@tonic-gate 		rv = kernel_set_private_key_attribute(object_p, template,
32460Sstevel@tonic-gate 		    copy, sp);
32470Sstevel@tonic-gate 		break;
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	case CKO_SECRET_KEY:
32500Sstevel@tonic-gate 		rv = kernel_set_secret_key_attribute(object_p, template,
32510Sstevel@tonic-gate 		    copy, sp);
32520Sstevel@tonic-gate 		break;
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 	default:
32550Sstevel@tonic-gate 		/*
32560Sstevel@tonic-gate 		 * If the template specifies a value of an attribute
32570Sstevel@tonic-gate 		 * which is incompatible with other existing attributes
32580Sstevel@tonic-gate 		 * of the object, then fails with return code
32590Sstevel@tonic-gate 		 * CKR_TEMPLATE_INCONSISTENT.
32600Sstevel@tonic-gate 		 */
32610Sstevel@tonic-gate 		rv = CKR_TEMPLATE_INCONSISTENT;
32620Sstevel@tonic-gate 		break;
32630Sstevel@tonic-gate 	}
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	return (rv);
32660Sstevel@tonic-gate }
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate static CK_RV
copy_bigint(biginteger_t * new_bigint,biginteger_t * old_bigint)32700Sstevel@tonic-gate copy_bigint(biginteger_t *new_bigint, biginteger_t *old_bigint)
32710Sstevel@tonic-gate {
32720Sstevel@tonic-gate 	new_bigint->big_value =
32730Sstevel@tonic-gate 	    malloc((sizeof (CK_BYTE) * new_bigint->big_value_len));
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	if (new_bigint->big_value == NULL) {
32760Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
32770Sstevel@tonic-gate 	}
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	(void) memcpy(new_bigint->big_value, old_bigint->big_value,
32800Sstevel@tonic-gate 	    (sizeof (CK_BYTE) * new_bigint->big_value_len));
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	return (CKR_OK);
32830Sstevel@tonic-gate }
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate static void
free_public_key_attr(public_key_obj_t * pbk,CK_KEY_TYPE key_type)32860Sstevel@tonic-gate free_public_key_attr(public_key_obj_t *pbk, CK_KEY_TYPE key_type)
32870Sstevel@tonic-gate {
32880Sstevel@tonic-gate 	if (pbk == NULL) {
32890Sstevel@tonic-gate 		return;
32900Sstevel@tonic-gate 	}
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	switch (key_type) {
32930Sstevel@tonic-gate 		case CKK_RSA:
32940Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_RSA_MOD(pbk));
32950Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_RSA_PUBEXPO(pbk));
32960Sstevel@tonic-gate 			break;
32970Sstevel@tonic-gate 		case CKK_DSA:
32980Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_PRIME(pbk));
32990Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_SUBPRIME(pbk));
33000Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_BASE(pbk));
33010Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PUB_DSA_VALUE(pbk));
33020Sstevel@tonic-gate 			break;
33030Sstevel@tonic-gate 		default:
33040Sstevel@tonic-gate 			break;
33050Sstevel@tonic-gate 	}
33060Sstevel@tonic-gate 	free(pbk);
33070Sstevel@tonic-gate }
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate CK_RV
kernel_copy_public_key_attr(public_key_obj_t * old_pub_key_obj_p,public_key_obj_t ** new_pub_key_obj_p,CK_KEY_TYPE key_type)33110Sstevel@tonic-gate kernel_copy_public_key_attr(public_key_obj_t *old_pub_key_obj_p,
33120Sstevel@tonic-gate     public_key_obj_t **new_pub_key_obj_p, CK_KEY_TYPE key_type)
33130Sstevel@tonic-gate {
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	public_key_obj_t *pbk;
33160Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	pbk = calloc(1, sizeof (public_key_obj_t));
33190Sstevel@tonic-gate 	if (pbk == NULL) {
33200Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
33210Sstevel@tonic-gate 	}
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 	switch (key_type) {
33240Sstevel@tonic-gate 		case CKK_RSA:
33250Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_RSA(pbk),
33260Sstevel@tonic-gate 			    KEY_PUB_RSA(old_pub_key_obj_p),
33270Sstevel@tonic-gate 			    sizeof (rsa_pub_key_t));
33280Sstevel@tonic-gate 			/* copy modulus */
33290Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_RSA_MOD(pbk),
33300Sstevel@tonic-gate 			    KEY_PUB_RSA_MOD(old_pub_key_obj_p));
33310Sstevel@tonic-gate 			if (rv != CKR_OK) {
33320Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33330Sstevel@tonic-gate 				return (rv);
33340Sstevel@tonic-gate 			}
33350Sstevel@tonic-gate 			/* copy public exponent */
33360Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_RSA_PUBEXPO(pbk),
33370Sstevel@tonic-gate 			    KEY_PUB_RSA_PUBEXPO(old_pub_key_obj_p));
33380Sstevel@tonic-gate 			if (rv != CKR_OK) {
33390Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33400Sstevel@tonic-gate 				return (rv);
33410Sstevel@tonic-gate 			}
33420Sstevel@tonic-gate 			break;
33430Sstevel@tonic-gate 		case CKK_DSA:
33440Sstevel@tonic-gate 			(void) memcpy(KEY_PUB_DSA(pbk),
33450Sstevel@tonic-gate 			    KEY_PUB_DSA(old_pub_key_obj_p),
33460Sstevel@tonic-gate 			    sizeof (dsa_pub_key_t));
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 			/* copy prime */
33490Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_PRIME(pbk),
33500Sstevel@tonic-gate 			    KEY_PUB_DSA_PRIME(old_pub_key_obj_p));
33510Sstevel@tonic-gate 			if (rv != CKR_OK) {
33520Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33530Sstevel@tonic-gate 				return (rv);
33540Sstevel@tonic-gate 			}
33550Sstevel@tonic-gate 
33560Sstevel@tonic-gate 			/* copy subprime */
33570Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_SUBPRIME(pbk),
33580Sstevel@tonic-gate 			    KEY_PUB_DSA_SUBPRIME(old_pub_key_obj_p));
33590Sstevel@tonic-gate 			if (rv != CKR_OK) {
33600Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33610Sstevel@tonic-gate 				return (rv);
33620Sstevel@tonic-gate 			}
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 			/* copy base */
33650Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_BASE(pbk),
33660Sstevel@tonic-gate 			    KEY_PUB_DSA_BASE(old_pub_key_obj_p));
33670Sstevel@tonic-gate 			if (rv != CKR_OK) {
33680Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33690Sstevel@tonic-gate 				return (rv);
33700Sstevel@tonic-gate 			}
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate 			/* copy value */
33730Sstevel@tonic-gate 			rv = copy_bigint(KEY_PUB_DSA_VALUE(pbk),
33740Sstevel@tonic-gate 			    KEY_PUB_DSA_VALUE(old_pub_key_obj_p));
33750Sstevel@tonic-gate 			if (rv != CKR_OK) {
33760Sstevel@tonic-gate 				free_public_key_attr(pbk, key_type);
33770Sstevel@tonic-gate 				return (rv);
33780Sstevel@tonic-gate 			}
33790Sstevel@tonic-gate 			break;
33800Sstevel@tonic-gate 		default:
33810Sstevel@tonic-gate 			break;
33820Sstevel@tonic-gate 	}
33830Sstevel@tonic-gate 	*new_pub_key_obj_p = pbk;
33840Sstevel@tonic-gate 	return (rv);
33850Sstevel@tonic-gate }
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate static void
free_private_key_attr(private_key_obj_t * pbk,CK_KEY_TYPE key_type)33890Sstevel@tonic-gate free_private_key_attr(private_key_obj_t *pbk, CK_KEY_TYPE key_type)
33900Sstevel@tonic-gate {
33910Sstevel@tonic-gate 	if (pbk == NULL) {
33920Sstevel@tonic-gate 		return;
33930Sstevel@tonic-gate 	}
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	switch (key_type) {
33960Sstevel@tonic-gate 		case CKK_RSA:
33970Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_MOD(pbk));
33980Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(pbk));
33990Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(pbk));
34000Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(pbk));
34010Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(pbk));
34020Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(pbk));
34030Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(pbk));
34040Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_RSA_COEF(pbk));
34050Sstevel@tonic-gate 			break;
34060Sstevel@tonic-gate 		case CKK_DSA:
34070Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_PRIME(pbk));
34080Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(pbk));
34090Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_BASE(pbk));
34100Sstevel@tonic-gate 			bigint_attr_cleanup(KEY_PRI_DSA_VALUE(pbk));
34110Sstevel@tonic-gate 			break;
34120Sstevel@tonic-gate 		default:
34130Sstevel@tonic-gate 			break;
34140Sstevel@tonic-gate 	}
34150Sstevel@tonic-gate 	free(pbk);
34160Sstevel@tonic-gate }
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate CK_RV
kernel_copy_private_key_attr(private_key_obj_t * old_pri_key_obj_p,private_key_obj_t ** new_pri_key_obj_p,CK_KEY_TYPE key_type)34190Sstevel@tonic-gate kernel_copy_private_key_attr(private_key_obj_t *old_pri_key_obj_p,
34200Sstevel@tonic-gate     private_key_obj_t **new_pri_key_obj_p, CK_KEY_TYPE key_type)
34210Sstevel@tonic-gate {
34220Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
34230Sstevel@tonic-gate 	private_key_obj_t *pbk;
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	pbk = calloc(1, sizeof (private_key_obj_t));
34260Sstevel@tonic-gate 	if (pbk == NULL) {
34270Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
34280Sstevel@tonic-gate 	}
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 	switch (key_type) {
34310Sstevel@tonic-gate 		case CKK_RSA:
34320Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_RSA(pbk),
34330Sstevel@tonic-gate 			    KEY_PRI_RSA(old_pri_key_obj_p),
34340Sstevel@tonic-gate 			    sizeof (rsa_pri_key_t));
34350Sstevel@tonic-gate 			/* copy modulus */
34360Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_MOD(pbk),
34370Sstevel@tonic-gate 			    KEY_PRI_RSA_MOD(old_pri_key_obj_p));
34380Sstevel@tonic-gate 			if (rv != CKR_OK) {
34390Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34400Sstevel@tonic-gate 				return (rv);
34410Sstevel@tonic-gate 			}
34420Sstevel@tonic-gate 			/* copy public exponent */
34430Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PUBEXPO(pbk),
34440Sstevel@tonic-gate 			    KEY_PRI_RSA_PUBEXPO(old_pri_key_obj_p));
34450Sstevel@tonic-gate 			if (rv != CKR_OK) {
34460Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34470Sstevel@tonic-gate 				return (rv);
34480Sstevel@tonic-gate 			}
34490Sstevel@tonic-gate 			/* copy private exponent */
34500Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIEXPO(pbk),
34510Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIEXPO(old_pri_key_obj_p));
34520Sstevel@tonic-gate 			if (rv != CKR_OK) {
34530Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34540Sstevel@tonic-gate 				return (rv);
34550Sstevel@tonic-gate 			}
34560Sstevel@tonic-gate 			/* copy prime_1 */
34570Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIME1(pbk),
34580Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIME1(old_pri_key_obj_p));
34590Sstevel@tonic-gate 			if (rv != CKR_OK) {
34600Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34610Sstevel@tonic-gate 				return (rv);
34620Sstevel@tonic-gate 			}
34630Sstevel@tonic-gate 			/* copy prime_2 */
34640Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_PRIME2(pbk),
34650Sstevel@tonic-gate 			    KEY_PRI_RSA_PRIME2(old_pri_key_obj_p));
34660Sstevel@tonic-gate 			if (rv != CKR_OK) {
34670Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34680Sstevel@tonic-gate 				return (rv);
34690Sstevel@tonic-gate 			}
34700Sstevel@tonic-gate 			/* copy exponent_1 */
34710Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_EXPO1(pbk),
34720Sstevel@tonic-gate 			    KEY_PRI_RSA_EXPO1(old_pri_key_obj_p));
34730Sstevel@tonic-gate 			if (rv != CKR_OK) {
34740Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34750Sstevel@tonic-gate 				return (rv);
34760Sstevel@tonic-gate 			}
34770Sstevel@tonic-gate 			/* copy exponent_2 */
34780Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_EXPO2(pbk),
34790Sstevel@tonic-gate 			    KEY_PRI_RSA_EXPO2(old_pri_key_obj_p));
34800Sstevel@tonic-gate 			if (rv != CKR_OK) {
34810Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34820Sstevel@tonic-gate 				return (rv);
34830Sstevel@tonic-gate 			}
34840Sstevel@tonic-gate 			/* copy coefficient */
34850Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_RSA_COEF(pbk),
34860Sstevel@tonic-gate 			    KEY_PRI_RSA_COEF(old_pri_key_obj_p));
34870Sstevel@tonic-gate 			if (rv != CKR_OK) {
34880Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
34890Sstevel@tonic-gate 				return (rv);
34900Sstevel@tonic-gate 			}
34910Sstevel@tonic-gate 			break;
34920Sstevel@tonic-gate 		case CKK_DSA:
34930Sstevel@tonic-gate 			(void) memcpy(KEY_PRI_DSA(pbk),
34940Sstevel@tonic-gate 			    KEY_PRI_DSA(old_pri_key_obj_p),
34950Sstevel@tonic-gate 			    sizeof (dsa_pri_key_t));
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 			/* copy prime */
34980Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_PRIME(pbk),
34990Sstevel@tonic-gate 			    KEY_PRI_DSA_PRIME(old_pri_key_obj_p));
35000Sstevel@tonic-gate 			if (rv != CKR_OK) {
35010Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
35020Sstevel@tonic-gate 				return (rv);
35030Sstevel@tonic-gate 			}
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 			/* copy subprime */
35060Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_SUBPRIME(pbk),
35070Sstevel@tonic-gate 			    KEY_PRI_DSA_SUBPRIME(old_pri_key_obj_p));
35080Sstevel@tonic-gate 			if (rv != CKR_OK) {
35090Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
35100Sstevel@tonic-gate 				return (rv);
35110Sstevel@tonic-gate 			}
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 			/* copy base */
35140Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_BASE(pbk),
35150Sstevel@tonic-gate 			    KEY_PRI_DSA_BASE(old_pri_key_obj_p));
35160Sstevel@tonic-gate 			if (rv != CKR_OK) {
35170Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
35180Sstevel@tonic-gate 				return (rv);
35190Sstevel@tonic-gate 			}
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 			/* copy value */
35220Sstevel@tonic-gate 			rv = copy_bigint(KEY_PRI_DSA_VALUE(pbk),
35230Sstevel@tonic-gate 			    KEY_PRI_DSA_VALUE(old_pri_key_obj_p));
35240Sstevel@tonic-gate 			if (rv != CKR_OK) {
35250Sstevel@tonic-gate 				free_private_key_attr(pbk, key_type);
35260Sstevel@tonic-gate 				return (rv);
35270Sstevel@tonic-gate 			}
35280Sstevel@tonic-gate 			break;
35290Sstevel@tonic-gate 		default:
35300Sstevel@tonic-gate 			break;
35310Sstevel@tonic-gate 	}
35320Sstevel@tonic-gate 	*new_pri_key_obj_p = pbk;
35330Sstevel@tonic-gate 	return (rv);
35340Sstevel@tonic-gate }
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 
35370Sstevel@tonic-gate CK_RV
kernel_copy_secret_key_attr(secret_key_obj_t * old_secret_key_obj_p,secret_key_obj_t ** new_secret_key_obj_p)35380Sstevel@tonic-gate kernel_copy_secret_key_attr(secret_key_obj_t *old_secret_key_obj_p,
35390Sstevel@tonic-gate     secret_key_obj_t **new_secret_key_obj_p)
35400Sstevel@tonic-gate {
35410Sstevel@tonic-gate 	secret_key_obj_t *sk;
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	sk = malloc(sizeof (secret_key_obj_t));
35440Sstevel@tonic-gate 	if (sk == NULL) {
35450Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
35460Sstevel@tonic-gate 	}
35470Sstevel@tonic-gate 	(void) memcpy(sk, old_secret_key_obj_p, sizeof (secret_key_obj_t));
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 	/* copy the secret key value */
35500Sstevel@tonic-gate 	sk->sk_value = malloc((sizeof (CK_BYTE) * sk->sk_value_len));
35510Sstevel@tonic-gate 	if (sk->sk_value == NULL) {
35520Sstevel@tonic-gate 		free(sk);
35530Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
35540Sstevel@tonic-gate 	}
35550Sstevel@tonic-gate 	(void) memcpy(sk->sk_value, old_secret_key_obj_p->sk_value,
35560Sstevel@tonic-gate 	    (sizeof (CK_BYTE) * sk->sk_value_len));
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	*new_secret_key_obj_p = sk;
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	return (CKR_OK);
35610Sstevel@tonic-gate }
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate /*
35660Sstevel@tonic-gate  * If CKA_CLASS not given, guess CKA_CLASS using
35670Sstevel@tonic-gate  * attributes on template .
35680Sstevel@tonic-gate  *
35690Sstevel@tonic-gate  * Some attributes are specific to an object class.  If one or more
35700Sstevel@tonic-gate  * of these attributes are in the template, make a list of classes
35710Sstevel@tonic-gate  * that can have these attributes.  This would speed up the search later,
35720Sstevel@tonic-gate  * because we can immediately skip an object if the class of that
3573*7518SViswanathan.Kannappan@Sun.COM  * object can not possibly contain one of the attributes.
35740Sstevel@tonic-gate  *
35750Sstevel@tonic-gate  */
35760Sstevel@tonic-gate void
kernel_process_find_attr(CK_OBJECT_CLASS * pclasses,CK_ULONG * num_result_pclasses,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount)35770Sstevel@tonic-gate kernel_process_find_attr(CK_OBJECT_CLASS *pclasses,
35780Sstevel@tonic-gate     CK_ULONG *num_result_pclasses, CK_ATTRIBUTE_PTR pTemplate,
35790Sstevel@tonic-gate     CK_ULONG ulCount)
35800Sstevel@tonic-gate {
35810Sstevel@tonic-gate 	ulong_t i;
35820Sstevel@tonic-gate 	int j;
35830Sstevel@tonic-gate 	boolean_t pub_found = B_FALSE,
35840Sstevel@tonic-gate 	    priv_found = B_FALSE,
35850Sstevel@tonic-gate 	    secret_found = B_FALSE,
35860Sstevel@tonic-gate 	    domain_found = B_FALSE,
35870Sstevel@tonic-gate 	    hardware_found = B_FALSE,
35880Sstevel@tonic-gate 	    cert_found = B_FALSE;
35890Sstevel@tonic-gate 	int num_pub_key_attrs, num_priv_key_attrs,
35900Sstevel@tonic-gate 	    num_secret_key_attrs, num_domain_attrs,
35910Sstevel@tonic-gate 	    num_hardware_attrs, num_cert_attrs;
35920Sstevel@tonic-gate 	int num_pclasses = 0;
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate 	for (i = 0; i < ulCount; i++) {
35950Sstevel@tonic-gate 		if (pTemplate[i].type == CKA_CLASS) {
35960Sstevel@tonic-gate 			/*
35970Sstevel@tonic-gate 			 * don't need to guess the class, it is specified.
35980Sstevel@tonic-gate 			 * Just record the class, and return.
35990Sstevel@tonic-gate 			 */
36000Sstevel@tonic-gate 			pclasses[0] =
36010Sstevel@tonic-gate 			    (*((CK_OBJECT_CLASS *)pTemplate[i].pValue));
36020Sstevel@tonic-gate 			*num_result_pclasses = 1;
36030Sstevel@tonic-gate 			return;
36040Sstevel@tonic-gate 		}
36050Sstevel@tonic-gate 	}
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	num_pub_key_attrs =
36080Sstevel@tonic-gate 	    sizeof (PUB_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36090Sstevel@tonic-gate 	num_priv_key_attrs =
36100Sstevel@tonic-gate 	    sizeof (PRIV_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36110Sstevel@tonic-gate 	num_secret_key_attrs =
36120Sstevel@tonic-gate 	    sizeof (SECRET_KEY_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36130Sstevel@tonic-gate 	num_domain_attrs =
36140Sstevel@tonic-gate 	    sizeof (DOMAIN_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36150Sstevel@tonic-gate 	num_hardware_attrs =
36160Sstevel@tonic-gate 	    sizeof (HARDWARE_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36170Sstevel@tonic-gate 	num_cert_attrs =
36180Sstevel@tonic-gate 	    sizeof (CERT_ATTRS) / sizeof (CK_ATTRIBUTE_TYPE);
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	/*
36210Sstevel@tonic-gate 	 * Get the list of objects class that might contain
36220Sstevel@tonic-gate 	 * some attributes.
36230Sstevel@tonic-gate 	 */
36240Sstevel@tonic-gate 	for (i = 0; i < ulCount; i++) {
36250Sstevel@tonic-gate 		/*
36260Sstevel@tonic-gate 		 * only check if this attribute can belong to public key object
36270Sstevel@tonic-gate 		 * class if public key object isn't already in the list
36280Sstevel@tonic-gate 		 */
36290Sstevel@tonic-gate 		if (!pub_found) {
36300Sstevel@tonic-gate 			for (j = 0; j < num_pub_key_attrs; j++) {
36310Sstevel@tonic-gate 				if (pTemplate[i].type == PUB_KEY_ATTRS[j]) {
36320Sstevel@tonic-gate 					pub_found = B_TRUE;
36330Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36340Sstevel@tonic-gate 					    CKO_PUBLIC_KEY;
36350Sstevel@tonic-gate 					break;
36360Sstevel@tonic-gate 				}
36370Sstevel@tonic-gate 			}
36380Sstevel@tonic-gate 		}
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 		if (!priv_found) {
36410Sstevel@tonic-gate 			for (j = 0; j < num_priv_key_attrs; j++) {
36420Sstevel@tonic-gate 				if (pTemplate[i].type == PRIV_KEY_ATTRS[j]) {
36430Sstevel@tonic-gate 					priv_found = B_TRUE;
36440Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36450Sstevel@tonic-gate 					    CKO_PRIVATE_KEY;
36460Sstevel@tonic-gate 					break;
36470Sstevel@tonic-gate 				}
36480Sstevel@tonic-gate 			}
36490Sstevel@tonic-gate 		}
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 		if (!secret_found) {
36520Sstevel@tonic-gate 			for (j = 0; j < num_secret_key_attrs; j++) {
36530Sstevel@tonic-gate 				if (pTemplate[i].type == SECRET_KEY_ATTRS[j]) {
36540Sstevel@tonic-gate 					secret_found = B_TRUE;
36550Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36560Sstevel@tonic-gate 					    CKO_SECRET_KEY;
36570Sstevel@tonic-gate 					break;
36580Sstevel@tonic-gate 				}
36590Sstevel@tonic-gate 			}
36600Sstevel@tonic-gate 		}
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 		if (!domain_found) {
36630Sstevel@tonic-gate 			for (j = 0; j < num_domain_attrs; j++) {
36640Sstevel@tonic-gate 				if (pTemplate[i].type == DOMAIN_ATTRS[j]) {
36650Sstevel@tonic-gate 					domain_found = B_TRUE;
36660Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36670Sstevel@tonic-gate 					    CKO_DOMAIN_PARAMETERS;
36680Sstevel@tonic-gate 					break;
36690Sstevel@tonic-gate 				}
36700Sstevel@tonic-gate 			}
36710Sstevel@tonic-gate 		}
36720Sstevel@tonic-gate 
36730Sstevel@tonic-gate 		if (!hardware_found) {
36740Sstevel@tonic-gate 			for (j = 0; j < num_hardware_attrs; j++) {
36750Sstevel@tonic-gate 				if (pTemplate[i].type == HARDWARE_ATTRS[j]) {
36760Sstevel@tonic-gate 					hardware_found = B_TRUE;
36770Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36780Sstevel@tonic-gate 					    CKO_HW_FEATURE;
36790Sstevel@tonic-gate 					break;
36800Sstevel@tonic-gate 				}
36810Sstevel@tonic-gate 			}
36820Sstevel@tonic-gate 		}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 		if (!cert_found) {
36850Sstevel@tonic-gate 			for (j = 0; j < num_cert_attrs; j++) {
36860Sstevel@tonic-gate 				if (pTemplate[i].type == CERT_ATTRS[j]) {
36870Sstevel@tonic-gate 					cert_found = B_TRUE;
36880Sstevel@tonic-gate 					pclasses[num_pclasses++] =
36890Sstevel@tonic-gate 					    CKO_CERTIFICATE;
36900Sstevel@tonic-gate 					break;
36910Sstevel@tonic-gate 				}
36920Sstevel@tonic-gate 			}
36930Sstevel@tonic-gate 		}
36940Sstevel@tonic-gate 	}
36950Sstevel@tonic-gate 	*num_result_pclasses = num_pclasses;
36960Sstevel@tonic-gate }
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate boolean_t
kernel_find_match_attrs(kernel_object_t * obj,CK_OBJECT_CLASS * pclasses,CK_ULONG num_pclasses,CK_ATTRIBUTE * template,CK_ULONG num_attr)37000Sstevel@tonic-gate kernel_find_match_attrs(kernel_object_t *obj, CK_OBJECT_CLASS *pclasses,
37010Sstevel@tonic-gate     CK_ULONG num_pclasses, CK_ATTRIBUTE *template, CK_ULONG num_attr)
37020Sstevel@tonic-gate {
37030Sstevel@tonic-gate 	ulong_t i;
37040Sstevel@tonic-gate 	CK_ATTRIBUTE *tmpl_attr, *obj_attr;
37050Sstevel@tonic-gate 	uint64_t attr_mask;
37060Sstevel@tonic-gate 	biginteger_t *bigint;
37070Sstevel@tonic-gate 	boolean_t compare_attr, compare_bigint, compare_boolean;
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 	/*
37100Sstevel@tonic-gate 	 * Check if the class of this object match with any
3711*7518SViswanathan.Kannappan@Sun.COM 	 * of object classes that can possibly contain the
37120Sstevel@tonic-gate 	 * requested attributes.
37130Sstevel@tonic-gate 	 */
37140Sstevel@tonic-gate 	if (num_pclasses > 0) {
37150Sstevel@tonic-gate 		for (i = 0; i < num_pclasses; i++) {
37160Sstevel@tonic-gate 			if (obj->class == pclasses[i]) {
37170Sstevel@tonic-gate 				break;
37180Sstevel@tonic-gate 			}
37190Sstevel@tonic-gate 		}
37200Sstevel@tonic-gate 		if (i == num_pclasses) {
37210Sstevel@tonic-gate 			/*
3722*7518SViswanathan.Kannappan@Sun.COM 			 * this object can't possibly contain one or
37230Sstevel@tonic-gate 			 * more attributes, don't need to check this object
37240Sstevel@tonic-gate 			 */
37250Sstevel@tonic-gate 			return (B_FALSE);
37260Sstevel@tonic-gate 		}
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	/* need to examine everything */
37300Sstevel@tonic-gate 	for (i = 0; i < num_attr; i++) {
37310Sstevel@tonic-gate 		tmpl_attr = &(template[i]);
37320Sstevel@tonic-gate 		compare_attr = B_FALSE;
37330Sstevel@tonic-gate 		compare_bigint = B_FALSE;
37340Sstevel@tonic-gate 		compare_boolean = B_FALSE;
37350Sstevel@tonic-gate 		switch (tmpl_attr->type) {
37360Sstevel@tonic-gate 		/* First, check the most common attributes */
37370Sstevel@tonic-gate 		case CKA_CLASS:
37380Sstevel@tonic-gate 			if (*((CK_OBJECT_CLASS *)tmpl_attr->pValue) !=
37390Sstevel@tonic-gate 			    obj->class) {
37400Sstevel@tonic-gate 				return (B_FALSE);
37410Sstevel@tonic-gate 			}
37420Sstevel@tonic-gate 			break;
37430Sstevel@tonic-gate 		case CKA_KEY_TYPE:
37440Sstevel@tonic-gate 			if (*((CK_KEY_TYPE *)tmpl_attr->pValue) !=
37450Sstevel@tonic-gate 			    obj->key_type) {
37460Sstevel@tonic-gate 				return (B_FALSE);
37470Sstevel@tonic-gate 			}
37480Sstevel@tonic-gate 			break;
37490Sstevel@tonic-gate 		case CKA_ENCRYPT:
37500Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & ENCRYPT_BOOL_ON;
37510Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37520Sstevel@tonic-gate 			break;
37530Sstevel@tonic-gate 		case CKA_DECRYPT:
37540Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & DECRYPT_BOOL_ON;
37550Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37560Sstevel@tonic-gate 			break;
37570Sstevel@tonic-gate 		case CKA_WRAP:
37580Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & WRAP_BOOL_ON;
37590Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37600Sstevel@tonic-gate 			break;
37610Sstevel@tonic-gate 		case CKA_UNWRAP:
37620Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & UNWRAP_BOOL_ON;
37630Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37640Sstevel@tonic-gate 			break;
37650Sstevel@tonic-gate 		case CKA_SIGN:
37660Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & SIGN_BOOL_ON;
37670Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37680Sstevel@tonic-gate 			break;
37690Sstevel@tonic-gate 		case CKA_SIGN_RECOVER:
37700Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
37710Sstevel@tonic-gate 			    SIGN_RECOVER_BOOL_ON;
37720Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37730Sstevel@tonic-gate 			break;
37740Sstevel@tonic-gate 		case CKA_VERIFY:
37750Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & VERIFY_BOOL_ON;
37760Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37770Sstevel@tonic-gate 			break;
37780Sstevel@tonic-gate 		case CKA_VERIFY_RECOVER:
37790Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
37800Sstevel@tonic-gate 			    VERIFY_RECOVER_BOOL_ON;
37810Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37820Sstevel@tonic-gate 			break;
37830Sstevel@tonic-gate 		case CKA_DERIVE:
37840Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & DERIVE_BOOL_ON;
37850Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37860Sstevel@tonic-gate 			break;
37870Sstevel@tonic-gate 		case CKA_LOCAL:
37880Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & LOCAL_BOOL_ON;
37890Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37900Sstevel@tonic-gate 			break;
37910Sstevel@tonic-gate 		case CKA_SENSITIVE:
37920Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & SENSITIVE_BOOL_ON;
37930Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37940Sstevel@tonic-gate 			break;
37950Sstevel@tonic-gate 		case CKA_SECONDARY_AUTH:
37960Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
37970Sstevel@tonic-gate 			    SECONDARY_AUTH_BOOL_ON;
37980Sstevel@tonic-gate 			compare_boolean = B_TRUE;
37990Sstevel@tonic-gate 			break;
38000Sstevel@tonic-gate 		case CKA_TRUSTED:
38010Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & TRUSTED_BOOL_ON;
38020Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38030Sstevel@tonic-gate 			break;
38040Sstevel@tonic-gate 		case CKA_EXTRACTABLE:
38050Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
38060Sstevel@tonic-gate 			    EXTRACTABLE_BOOL_ON;
38070Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38080Sstevel@tonic-gate 			break;
38090Sstevel@tonic-gate 		case CKA_ALWAYS_SENSITIVE:
38100Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
38110Sstevel@tonic-gate 			    ALWAYS_SENSITIVE_BOOL_ON;
38120Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38130Sstevel@tonic-gate 			break;
38140Sstevel@tonic-gate 		case CKA_NEVER_EXTRACTABLE:
38150Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) &
38160Sstevel@tonic-gate 			    NEVER_EXTRACTABLE_BOOL_ON;
38170Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38180Sstevel@tonic-gate 			break;
38190Sstevel@tonic-gate 		case CKA_TOKEN:
38200Sstevel@tonic-gate 			/*
38210Sstevel@tonic-gate 			 * CKA_TOKEN value is not applicable to an object
38220Sstevel@tonic-gate 			 * created in the library, it should only contain
38230Sstevel@tonic-gate 			 * the default value FALSE
38240Sstevel@tonic-gate 			 */
38250Sstevel@tonic-gate 			attr_mask = 0;
38260Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38270Sstevel@tonic-gate 			break;
38280Sstevel@tonic-gate 		case CKA_PRIVATE:
38290Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & PRIVATE_BOOL_ON;
38300Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38310Sstevel@tonic-gate 			break;
38320Sstevel@tonic-gate 		case CKA_MODIFIABLE:
38330Sstevel@tonic-gate 			attr_mask = (obj->bool_attr_mask) & MODIFIABLE_BOOL_ON;
38340Sstevel@tonic-gate 			compare_boolean = B_TRUE;
38350Sstevel@tonic-gate 			break;
38360Sstevel@tonic-gate 		case CKA_SUBJECT:
38370Sstevel@tonic-gate 		case CKA_ID:
38380Sstevel@tonic-gate 		case CKA_START_DATE:
38390Sstevel@tonic-gate 		case CKA_END_DATE:
38400Sstevel@tonic-gate 		case CKA_KEY_GEN_MECHANISM:
38410Sstevel@tonic-gate 		case CKA_LABEL:
38420Sstevel@tonic-gate 			/* find these attributes from extra_attrlistp */
38430Sstevel@tonic-gate 			obj_attr = get_extra_attr(tmpl_attr->type, obj);
38440Sstevel@tonic-gate 			compare_attr = B_TRUE;
38450Sstevel@tonic-gate 			break;
38460Sstevel@tonic-gate 		case CKA_VALUE_LEN:
38470Sstevel@tonic-gate 			/* only secret key has this attribute */
38480Sstevel@tonic-gate 			if (obj->class == CKO_SECRET_KEY) {
38490Sstevel@tonic-gate 				if (*((CK_ULONG *)tmpl_attr->pValue) !=
38500Sstevel@tonic-gate 				    OBJ_SEC_VALUE_LEN(obj)) {
38510Sstevel@tonic-gate 					return (B_FALSE);
38520Sstevel@tonic-gate 				}
38530Sstevel@tonic-gate 			} else {
38540Sstevel@tonic-gate 				return (B_FALSE);
38550Sstevel@tonic-gate 			}
38560Sstevel@tonic-gate 			break;
38570Sstevel@tonic-gate 		case CKA_VALUE:
38580Sstevel@tonic-gate 			switch (obj->class) {
38590Sstevel@tonic-gate 			case CKO_SECRET_KEY:
38600Sstevel@tonic-gate 				/*
38610Sstevel@tonic-gate 				 * secret_key_obj_t is the same as
38620Sstevel@tonic-gate 				 * biginteger_t
38630Sstevel@tonic-gate 				 */
38640Sstevel@tonic-gate 				bigint = (biginteger_t *)OBJ_SEC(obj);
38650Sstevel@tonic-gate 				break;
38660Sstevel@tonic-gate 			case CKO_PRIVATE_KEY:
38670Sstevel@tonic-gate 				if (obj->key_type == CKK_DSA) {
38680Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_VALUE(obj);
38690Sstevel@tonic-gate 				} else {
38700Sstevel@tonic-gate 					return (B_FALSE);
38710Sstevel@tonic-gate 				}
38720Sstevel@tonic-gate 				break;
38730Sstevel@tonic-gate 			case CKO_PUBLIC_KEY:
38740Sstevel@tonic-gate 				if (obj->key_type == CKK_DSA) {
38750Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_VALUE(obj);
38760Sstevel@tonic-gate 				} else {
38770Sstevel@tonic-gate 					return (B_FALSE);
38780Sstevel@tonic-gate 				}
38790Sstevel@tonic-gate 				break;
38800Sstevel@tonic-gate 			default:
38810Sstevel@tonic-gate 				return (B_FALSE);
38820Sstevel@tonic-gate 			}
38830Sstevel@tonic-gate 			compare_bigint = B_TRUE;
38840Sstevel@tonic-gate 			break;
38850Sstevel@tonic-gate 		case CKA_MODULUS:
38860Sstevel@tonic-gate 			/* only RSA public and private key have this attr */
38870Sstevel@tonic-gate 			if (obj->key_type == CKK_RSA) {
38880Sstevel@tonic-gate 				if (obj->class == CKO_PUBLIC_KEY) {
38890Sstevel@tonic-gate 					bigint = OBJ_PUB_RSA_MOD(obj);
38900Sstevel@tonic-gate 				} else if (obj->class == CKO_PRIVATE_KEY) {
38910Sstevel@tonic-gate 					bigint = OBJ_PRI_RSA_MOD(obj);
38920Sstevel@tonic-gate 				} else {
38930Sstevel@tonic-gate 					return (B_FALSE);
38940Sstevel@tonic-gate 				}
38950Sstevel@tonic-gate 				compare_bigint = B_TRUE;
38960Sstevel@tonic-gate 			} else {
38970Sstevel@tonic-gate 				return (B_FALSE);
38980Sstevel@tonic-gate 			}
38990Sstevel@tonic-gate 			break;
39000Sstevel@tonic-gate 		case CKA_MODULUS_BITS:
39010Sstevel@tonic-gate 			/* only RSA public key has this attribute */
39020Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39030Sstevel@tonic-gate 			    (obj->class == CKO_PUBLIC_KEY)) {
39040Sstevel@tonic-gate 				CK_ULONG mod_bits = OBJ_PUB_RSA_MOD_BITS(obj);
39050Sstevel@tonic-gate 				if (mod_bits !=
39060Sstevel@tonic-gate 				    *((CK_ULONG *)tmpl_attr->pValue)) {
39070Sstevel@tonic-gate 					return (B_FALSE);
39080Sstevel@tonic-gate 				}
39090Sstevel@tonic-gate 			} else {
39100Sstevel@tonic-gate 				return (B_FALSE);
39110Sstevel@tonic-gate 			}
39120Sstevel@tonic-gate 			break;
39130Sstevel@tonic-gate 		case CKA_PUBLIC_EXPONENT:
39140Sstevel@tonic-gate 			/* only RSA public and private key have this attr */
39150Sstevel@tonic-gate 			if (obj->key_type == CKK_RSA) {
39160Sstevel@tonic-gate 				if (obj->class == CKO_PUBLIC_KEY) {
39170Sstevel@tonic-gate 					bigint = OBJ_PUB_RSA_PUBEXPO(obj);
39180Sstevel@tonic-gate 				} else if (obj->class == CKO_PRIVATE_KEY) {
39190Sstevel@tonic-gate 					bigint = OBJ_PRI_RSA_PUBEXPO(obj);
39200Sstevel@tonic-gate 				} else {
39210Sstevel@tonic-gate 					return (B_FALSE);
39220Sstevel@tonic-gate 				}
39230Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39240Sstevel@tonic-gate 			} else {
39250Sstevel@tonic-gate 				return (B_FALSE);
39260Sstevel@tonic-gate 			}
39270Sstevel@tonic-gate 			break;
39280Sstevel@tonic-gate 		case CKA_PRIVATE_EXPONENT:
39290Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39300Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39310Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39320Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIEXPO(obj);
39330Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39340Sstevel@tonic-gate 			} else {
39350Sstevel@tonic-gate 				return (B_FALSE);
39360Sstevel@tonic-gate 			}
39370Sstevel@tonic-gate 			break;
39380Sstevel@tonic-gate 		case CKA_PRIME_1:
39390Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39400Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39410Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39420Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIME1(obj);
39430Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39440Sstevel@tonic-gate 			} else {
39450Sstevel@tonic-gate 				return (B_FALSE);
39460Sstevel@tonic-gate 			}
39470Sstevel@tonic-gate 			break;
39480Sstevel@tonic-gate 		case CKA_PRIME_2:
39490Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39500Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39510Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39520Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_PRIME2(obj);
39530Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39540Sstevel@tonic-gate 			} else {
39550Sstevel@tonic-gate 				return (B_FALSE);
39560Sstevel@tonic-gate 			}
39570Sstevel@tonic-gate 			break;
39580Sstevel@tonic-gate 		case CKA_EXPONENT_1:
39590Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39600Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39610Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39620Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_EXPO1(obj);
39630Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39640Sstevel@tonic-gate 			} else {
39650Sstevel@tonic-gate 				return (B_FALSE);
39660Sstevel@tonic-gate 			}
39670Sstevel@tonic-gate 			break;
39680Sstevel@tonic-gate 		case CKA_EXPONENT_2:
39690Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39700Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39710Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39720Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_EXPO2(obj);
39730Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39740Sstevel@tonic-gate 			} else {
39750Sstevel@tonic-gate 				return (B_FALSE);
39760Sstevel@tonic-gate 			}
39770Sstevel@tonic-gate 			break;
39780Sstevel@tonic-gate 		case CKA_COEFFICIENT:
39790Sstevel@tonic-gate 			/* only RSA private key has this attribute */
39800Sstevel@tonic-gate 			if ((obj->key_type == CKK_RSA) &&
39810Sstevel@tonic-gate 			    (obj->class == CKO_PRIVATE_KEY)) {
39820Sstevel@tonic-gate 				bigint = OBJ_PRI_RSA_COEF(obj);
39830Sstevel@tonic-gate 				compare_bigint = B_TRUE;
39840Sstevel@tonic-gate 			} else {
39850Sstevel@tonic-gate 				return (B_FALSE);
39860Sstevel@tonic-gate 			}
39870Sstevel@tonic-gate 			break;
39880Sstevel@tonic-gate 		case CKA_VALUE_BITS:
39890Sstevel@tonic-gate 			return (B_FALSE);
39900Sstevel@tonic-gate 		case CKA_PRIME:
39910Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
39920Sstevel@tonic-gate 				switch (obj->key_type) {
39930Sstevel@tonic-gate 				case CKK_DSA:
39940Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_PRIME(obj);
39950Sstevel@tonic-gate 					break;
39960Sstevel@tonic-gate 				default:
39970Sstevel@tonic-gate 					return (B_FALSE);
39980Sstevel@tonic-gate 				}
39990Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
40000Sstevel@tonic-gate 				switch (obj->key_type) {
40010Sstevel@tonic-gate 				case CKK_DSA:
40020Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_PRIME(obj);
40030Sstevel@tonic-gate 					break;
40040Sstevel@tonic-gate 				default:
40050Sstevel@tonic-gate 					return (B_FALSE);
40060Sstevel@tonic-gate 				}
40070Sstevel@tonic-gate 			} else {
40080Sstevel@tonic-gate 				return (B_FALSE);
40090Sstevel@tonic-gate 			}
40100Sstevel@tonic-gate 			compare_bigint = B_TRUE;
40110Sstevel@tonic-gate 			break;
40120Sstevel@tonic-gate 		case CKA_SUBPRIME:
40130Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
40140Sstevel@tonic-gate 				switch (obj->key_type) {
40150Sstevel@tonic-gate 				case CKK_DSA:
40160Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_SUBPRIME(obj);
40170Sstevel@tonic-gate 					break;
40180Sstevel@tonic-gate 				default:
40190Sstevel@tonic-gate 					return (B_FALSE);
40200Sstevel@tonic-gate 				}
40210Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
40220Sstevel@tonic-gate 				switch (obj->key_type) {
40230Sstevel@tonic-gate 				case CKK_DSA:
40240Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_SUBPRIME(obj);
40250Sstevel@tonic-gate 					break;
40260Sstevel@tonic-gate 				default:
40270Sstevel@tonic-gate 					return (B_FALSE);
40280Sstevel@tonic-gate 				}
40290Sstevel@tonic-gate 			} else {
40300Sstevel@tonic-gate 				return (B_FALSE);
40310Sstevel@tonic-gate 			}
40320Sstevel@tonic-gate 			compare_bigint = B_TRUE;
40330Sstevel@tonic-gate 			break;
40340Sstevel@tonic-gate 		case CKA_BASE:
40350Sstevel@tonic-gate 			if (obj->class == CKO_PUBLIC_KEY) {
40360Sstevel@tonic-gate 				switch (obj->key_type) {
40370Sstevel@tonic-gate 				case CKK_DSA:
40380Sstevel@tonic-gate 					bigint = OBJ_PUB_DSA_BASE(obj);
40390Sstevel@tonic-gate 					break;
40400Sstevel@tonic-gate 				default:
40410Sstevel@tonic-gate 					return (B_FALSE);
40420Sstevel@tonic-gate 				}
40430Sstevel@tonic-gate 			} else if (obj->class == CKO_PRIVATE_KEY) {
40440Sstevel@tonic-gate 				switch (obj->key_type) {
40450Sstevel@tonic-gate 				case CKK_DSA:
40460Sstevel@tonic-gate 					bigint = OBJ_PRI_DSA_BASE(obj);
40470Sstevel@tonic-gate 					break;
40480Sstevel@tonic-gate 				default:
40490Sstevel@tonic-gate 					return (B_FALSE);
40500Sstevel@tonic-gate 				}
40510Sstevel@tonic-gate 			} else {
40520Sstevel@tonic-gate 				return (B_FALSE);
40530Sstevel@tonic-gate 			}
40540Sstevel@tonic-gate 			compare_bigint = B_TRUE;
40550Sstevel@tonic-gate 			break;
40560Sstevel@tonic-gate 		case CKA_PRIME_BITS:
40570Sstevel@tonic-gate 			return (B_FALSE);
40580Sstevel@tonic-gate 		case CKA_SUBPRIME_BITS:
40590Sstevel@tonic-gate 			return (B_FALSE);
40600Sstevel@tonic-gate 		default:
40610Sstevel@tonic-gate 			/*
40620Sstevel@tonic-gate 			 * any other attributes are currently not supported.
40630Sstevel@tonic-gate 			 * so, it's not possible for them to be in the
40640Sstevel@tonic-gate 			 * object
40650Sstevel@tonic-gate 			 */
40660Sstevel@tonic-gate 			return (B_FALSE);
40670Sstevel@tonic-gate 		}
40680Sstevel@tonic-gate 		if (compare_boolean) {
40690Sstevel@tonic-gate 			CK_BBOOL bval;
40700Sstevel@tonic-gate 
40710Sstevel@tonic-gate 			if (attr_mask) {
40720Sstevel@tonic-gate 				bval = TRUE;
40730Sstevel@tonic-gate 			} else {
40740Sstevel@tonic-gate 				bval = FALSE;
40750Sstevel@tonic-gate 			}
40760Sstevel@tonic-gate 			if (bval != *((CK_BBOOL *)tmpl_attr->pValue)) {
40770Sstevel@tonic-gate 				return (B_FALSE);
40780Sstevel@tonic-gate 			}
40790Sstevel@tonic-gate 		} else if (compare_bigint) {
40800Sstevel@tonic-gate 			if (bigint == NULL) {
40810Sstevel@tonic-gate 				return (B_FALSE);
40820Sstevel@tonic-gate 			}
40830Sstevel@tonic-gate 			if (tmpl_attr->ulValueLen != bigint->big_value_len) {
40840Sstevel@tonic-gate 				return (B_FALSE);
40850Sstevel@tonic-gate 			}
40860Sstevel@tonic-gate 			if (memcmp(tmpl_attr->pValue, bigint->big_value,
40870Sstevel@tonic-gate 			    tmpl_attr->ulValueLen) != 0) {
40880Sstevel@tonic-gate 				return (B_FALSE);
40890Sstevel@tonic-gate 			}
40900Sstevel@tonic-gate 		} else if (compare_attr) {
40910Sstevel@tonic-gate 			if (obj_attr == NULL) {
40920Sstevel@tonic-gate 				/*
40930Sstevel@tonic-gate 				 * The attribute type is valid, and its value
40940Sstevel@tonic-gate 				 * has not been initialized in the object. In
40950Sstevel@tonic-gate 				 * this case, it only matches the template's
40960Sstevel@tonic-gate 				 * attribute if the template's value length
40970Sstevel@tonic-gate 				 * is 0.
40980Sstevel@tonic-gate 				 */
40990Sstevel@tonic-gate 				if (tmpl_attr->ulValueLen != 0)
41000Sstevel@tonic-gate 					return (B_FALSE);
41010Sstevel@tonic-gate 			} else {
41020Sstevel@tonic-gate 				if (tmpl_attr->ulValueLen !=
41030Sstevel@tonic-gate 				    obj_attr->ulValueLen) {
41040Sstevel@tonic-gate 					return (B_FALSE);
41050Sstevel@tonic-gate 				}
41060Sstevel@tonic-gate 				if (memcmp(tmpl_attr->pValue, obj_attr->pValue,
41070Sstevel@tonic-gate 				    tmpl_attr->ulValueLen) != 0) {
41080Sstevel@tonic-gate 					return (B_FALSE);
41090Sstevel@tonic-gate 				}
41100Sstevel@tonic-gate 			}
41110Sstevel@tonic-gate 		}
41120Sstevel@tonic-gate 	}
41130Sstevel@tonic-gate 	return (B_TRUE);
41140Sstevel@tonic-gate }
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate CK_ATTRIBUTE_PTR
get_extra_attr(CK_ATTRIBUTE_TYPE type,kernel_object_t * obj)41170Sstevel@tonic-gate get_extra_attr(CK_ATTRIBUTE_TYPE type, kernel_object_t *obj)
41180Sstevel@tonic-gate {
41190Sstevel@tonic-gate 	CK_ATTRIBUTE_INFO_PTR tmp;
41200Sstevel@tonic-gate 
41210Sstevel@tonic-gate 	tmp = obj->extra_attrlistp;
41220Sstevel@tonic-gate 	while (tmp != NULL) {
41230Sstevel@tonic-gate 		if (tmp->attr.type == type) {
41240Sstevel@tonic-gate 			return (&(tmp->attr));
41250Sstevel@tonic-gate 		}
41260Sstevel@tonic-gate 		tmp = tmp->next;
41270Sstevel@tonic-gate 	}
41280Sstevel@tonic-gate 	/* if get there, the specified attribute is not found */
41290Sstevel@tonic-gate 	return (NULL);
41300Sstevel@tonic-gate }
4131