17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5034448feSmcpowers * Common Development and Distribution License (the "License").
6034448feSmcpowers * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22034448feSmcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*33f5ff17SMilan Jurik * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include "metaGlobal.h"
317c478bd9Sstevel@tonic-gate #include "metaAttrMasters.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate static void
347c478bd9Sstevel@tonic-gate find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
357c478bd9Sstevel@tonic-gate size_t num_attributes, generic_attr_t **found_attribute);
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * get_master_attributes_by_object
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * Returns an (statically allocated) set of object attributes, as determined by
417c478bd9Sstevel@tonic-gate * class and keytype of the supplied object. The attributes are only
427c478bd9Sstevel@tonic-gate * initialized to default values.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate CK_RV
get_master_attributes_by_object(slot_session_t * session,slot_object_t * slot_object,generic_attr_t ** attributes,size_t * num_attributes)457c478bd9Sstevel@tonic-gate get_master_attributes_by_object(slot_session_t *session,
467c478bd9Sstevel@tonic-gate slot_object_t *slot_object, generic_attr_t **attributes,
477c478bd9Sstevel@tonic-gate size_t *num_attributes)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate CK_RV rv;
507c478bd9Sstevel@tonic-gate CK_ATTRIBUTE attr;
517c478bd9Sstevel@tonic-gate CK_OBJECT_CLASS class;
527c478bd9Sstevel@tonic-gate CK_ULONG subtype = CK_UNAVAILABLE_INFORMATION;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /* first get the class */
557c478bd9Sstevel@tonic-gate attr.type = CKA_CLASS;
567c478bd9Sstevel@tonic-gate attr.pValue = &class;
577c478bd9Sstevel@tonic-gate attr.ulValueLen = sizeof (class);
587c478bd9Sstevel@tonic-gate rv = FUNCLIST(session->fw_st_id)->C_GetAttributeValue(
597c478bd9Sstevel@tonic-gate session->hSession, slot_object->hObject, &attr, 1);
607c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
617c478bd9Sstevel@tonic-gate return (rv);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate attr.pValue = &subtype;
657c478bd9Sstevel@tonic-gate attr.ulValueLen = sizeof (subtype);
667c478bd9Sstevel@tonic-gate switch (class) {
677c478bd9Sstevel@tonic-gate case CKO_CERTIFICATE:
687c478bd9Sstevel@tonic-gate attr.type = CKA_CERTIFICATE_TYPE;
697c478bd9Sstevel@tonic-gate break;
707c478bd9Sstevel@tonic-gate case CKO_HW_FEATURE:
717c478bd9Sstevel@tonic-gate attr.type = CKA_HW_FEATURE_TYPE;
727c478bd9Sstevel@tonic-gate break;
737c478bd9Sstevel@tonic-gate case CKO_PUBLIC_KEY:
747c478bd9Sstevel@tonic-gate case CKO_PRIVATE_KEY:
757c478bd9Sstevel@tonic-gate case CKO_SECRET_KEY:
767c478bd9Sstevel@tonic-gate case CKO_DOMAIN_PARAMETERS:
777c478bd9Sstevel@tonic-gate attr.type = CKA_KEY_TYPE;
787c478bd9Sstevel@tonic-gate break;
797c478bd9Sstevel@tonic-gate case CKO_DATA:
807c478bd9Sstevel@tonic-gate goto get_attr;
817c478bd9Sstevel@tonic-gate default:
827c478bd9Sstevel@tonic-gate /* should never be here */
837c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate rv = FUNCLIST(session->fw_st_id)->C_GetAttributeValue(
867c478bd9Sstevel@tonic-gate session->hSession, slot_object->hObject, &attr, 1);
877c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
887c478bd9Sstevel@tonic-gate return (rv);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate get_attr:
927c478bd9Sstevel@tonic-gate rv = get_master_attributes_by_type(class, subtype,
937c478bd9Sstevel@tonic-gate attributes, num_attributes);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate return (rv);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * get_master_attributes_by_template
1007c478bd9Sstevel@tonic-gate *
1017c478bd9Sstevel@tonic-gate * Returns an (statically allocated) set of object attributes, as determined by
1027c478bd9Sstevel@tonic-gate * the supplied object template. The template is only used to determine the
1037c478bd9Sstevel@tonic-gate * class/subclass of the object. The attributes are only initialized to
1047c478bd9Sstevel@tonic-gate * default values.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate CK_RV
get_master_attributes_by_template(CK_ATTRIBUTE * template,CK_ULONG template_size,generic_attr_t ** attributes,size_t * num_attributes)1077c478bd9Sstevel@tonic-gate get_master_attributes_by_template(
1087c478bd9Sstevel@tonic-gate CK_ATTRIBUTE *template, CK_ULONG template_size,
1097c478bd9Sstevel@tonic-gate generic_attr_t **attributes, size_t *num_attributes)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate CK_OBJECT_CLASS class;
1127c478bd9Sstevel@tonic-gate CK_ULONG subtype = CK_UNAVAILABLE_INFORMATION;
1137c478bd9Sstevel@tonic-gate boolean_t found;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate found = get_template_ulong(CKA_CLASS, template, template_size, &class);
1167c478bd9Sstevel@tonic-gate if (!found) {
1177c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate switch (class) {
1217c478bd9Sstevel@tonic-gate case CKO_CERTIFICATE:
1227c478bd9Sstevel@tonic-gate found = get_template_ulong(CKA_CERTIFICATE_TYPE,
1237c478bd9Sstevel@tonic-gate template, template_size, &subtype);
1247c478bd9Sstevel@tonic-gate break;
1257c478bd9Sstevel@tonic-gate case CKO_HW_FEATURE:
1267c478bd9Sstevel@tonic-gate found = get_template_ulong(CKA_HW_FEATURE_TYPE,
1277c478bd9Sstevel@tonic-gate template, template_size, &subtype);
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case CKO_PUBLIC_KEY:
1307c478bd9Sstevel@tonic-gate case CKO_PRIVATE_KEY:
1317c478bd9Sstevel@tonic-gate case CKO_SECRET_KEY:
1327c478bd9Sstevel@tonic-gate case CKO_DOMAIN_PARAMETERS:
1337c478bd9Sstevel@tonic-gate found = get_template_ulong(CKA_KEY_TYPE,
1347c478bd9Sstevel@tonic-gate template, template_size, &subtype);
1357c478bd9Sstevel@tonic-gate break;
1367c478bd9Sstevel@tonic-gate case CKO_DATA:
1377c478bd9Sstevel@tonic-gate /* CKO_DATA has no subtype, just pretend it is found */
1387c478bd9Sstevel@tonic-gate found = B_TRUE;
1397c478bd9Sstevel@tonic-gate default:
1407c478bd9Sstevel@tonic-gate /* unknown object class */
1417c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (!found) {
1457c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate return (get_master_attributes_by_type(class, subtype,
1497c478bd9Sstevel@tonic-gate attributes, num_attributes));
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate * get_master_template_by_type
1547c478bd9Sstevel@tonic-gate *
1557c478bd9Sstevel@tonic-gate * Returns an (statically allocated) set of object attributes, as determined
1567c478bd9Sstevel@tonic-gate * by the specified class and subtype. The attributes are initialized to default
1577c478bd9Sstevel@tonic-gate * values.
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate CK_RV
get_master_template_by_type(CK_OBJECT_CLASS class,CK_ULONG subtype,generic_attr_t ** attributes,size_t * num_attributes)1607c478bd9Sstevel@tonic-gate get_master_template_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
1617c478bd9Sstevel@tonic-gate generic_attr_t **attributes, size_t *num_attributes)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate generic_attr_t *master_template = NULL;
1647c478bd9Sstevel@tonic-gate size_t master_template_size = 0;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate switch (class) {
1677c478bd9Sstevel@tonic-gate case CKO_HW_FEATURE:
1687c478bd9Sstevel@tonic-gate switch (subtype) {
1697c478bd9Sstevel@tonic-gate case CKO_HW_FEATURE:
1707c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_HW_CLOCK;
1717c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_HW_CLOCK);
1727c478bd9Sstevel@tonic-gate break;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate case CKH_MONOTONIC_COUNTER:
1757c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_HW_MONOTONIC;
1767c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_HW_MONOTONIC);
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate default:
1807c478bd9Sstevel@tonic-gate /* Unsupported. */
1817c478bd9Sstevel@tonic-gate break;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate case CKO_DATA:
1867c478bd9Sstevel@tonic-gate /* Objects of this class have no subtype. */
1877c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_DATA;
1887c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_DATA);
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate case CKO_CERTIFICATE:
1927c478bd9Sstevel@tonic-gate switch (subtype) {
1937c478bd9Sstevel@tonic-gate case CKC_X_509:
1947c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_CERT_X509;
1957c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_CERT_X509);
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate case CKC_X_509_ATTR_CERT:
1997c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_CERT_X509ATTR;
2007c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_CERT_X509ATTR);
2017c478bd9Sstevel@tonic-gate break;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate default:
2047c478bd9Sstevel@tonic-gate /* Unsupported. */
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate case CKO_PUBLIC_KEY:
2107c478bd9Sstevel@tonic-gate switch (subtype) {
2117c478bd9Sstevel@tonic-gate case CKK_RSA:
2127c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_RSA;
2137c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_RSA);
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate case CKK_DSA:
2177c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_DSA;
2187c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_DSA);
2197c478bd9Sstevel@tonic-gate break;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate case CKK_EC:
2227c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_EC;
2237c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_EC);
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate case CKK_DH:
2277c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_DH;
2287c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_DH);
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate case CKK_X9_42_DH:
2327c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_X942DH;
2337c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_X942DH);
2347c478bd9Sstevel@tonic-gate break;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate case CKK_KEA:
2377c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PUBKEY_KEA;
2387c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PUBKEY_KEA);
2397c478bd9Sstevel@tonic-gate break;
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate default:
2427c478bd9Sstevel@tonic-gate /* Unsupported. */
2437c478bd9Sstevel@tonic-gate break;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate break;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate case CKO_PRIVATE_KEY:
2487c478bd9Sstevel@tonic-gate switch (subtype) {
2497c478bd9Sstevel@tonic-gate case CKK_RSA:
2507c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_RSA;
2517c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_RSA);
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate case CKK_DSA:
2557c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_DSA;
2567c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_DSA);
2577c478bd9Sstevel@tonic-gate break;
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate case CKK_EC:
2607c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_EC;
2617c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_EC);
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate case CKK_DH:
2657c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_DH;
2667c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_DH);
2677c478bd9Sstevel@tonic-gate break;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate case CKK_X9_42_DH:
2707c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_X942DH;
2717c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_X942DH);
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate case CKK_KEA:
2757c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_PRIVKEY_KEA;
2767c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_PRIVKEY_KEA);
2777c478bd9Sstevel@tonic-gate break;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate default:
2807c478bd9Sstevel@tonic-gate /* Unsupported. */
2817c478bd9Sstevel@tonic-gate break;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate break;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate case CKO_SECRET_KEY:
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate * The only difference between secret keys is that some
2887c478bd9Sstevel@tonic-gate * are valiable length (eg CKK_AES), while others are not
2897c478bd9Sstevel@tonic-gate * (eg CKK_DES) -- and do not have a CKA_VALUE_LEN attribute.
2907c478bd9Sstevel@tonic-gate *
2917c478bd9Sstevel@tonic-gate * FUTURE(?): Consider using obj_seckey_withlen for unknown
2927c478bd9Sstevel@tonic-gate * keytypes. This is the most likely choice, as new algorithms
2937c478bd9Sstevel@tonic-gate * seem to support variable length keys. That's not the default
2947c478bd9Sstevel@tonic-gate * now, because if people have implemented new key types with
2957c478bd9Sstevel@tonic-gate * different attribute sets (like the mess of public/private
2967c478bd9Sstevel@tonic-gate * key types), then incorrect behaviour would result. It's
2977c478bd9Sstevel@tonic-gate * easier to relax this restriction than to tighten it (which
2987c478bd9Sstevel@tonic-gate * would introduce a regression to anyone relying on this
2997c478bd9Sstevel@tonic-gate * working for unknown key types).
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate switch (subtype) {
3037c478bd9Sstevel@tonic-gate case CKK_DES:
3047c478bd9Sstevel@tonic-gate case CKK_DES2:
3057c478bd9Sstevel@tonic-gate case CKK_DES3:
3067c478bd9Sstevel@tonic-gate case CKK_IDEA:
3077c478bd9Sstevel@tonic-gate case CKK_CDMF:
3087c478bd9Sstevel@tonic-gate case CKK_SKIPJACK:
3097c478bd9Sstevel@tonic-gate case CKK_BATON:
3107c478bd9Sstevel@tonic-gate case CKK_JUNIPER:
3117c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_SECKEY;
3127c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_SECKEY);
3137c478bd9Sstevel@tonic-gate break;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate case CKK_GENERIC_SECRET:
3167c478bd9Sstevel@tonic-gate case CKK_RC2:
3177c478bd9Sstevel@tonic-gate case CKK_RC4:
3187c478bd9Sstevel@tonic-gate case CKK_RC5:
3197c478bd9Sstevel@tonic-gate case CKK_AES:
320f66d273dSizick case CKK_BLOWFISH:
3217c478bd9Sstevel@tonic-gate case CKK_CAST:
3227c478bd9Sstevel@tonic-gate case CKK_CAST3:
3237c478bd9Sstevel@tonic-gate case CKK_CAST128:
3247c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_SECKEY_WITHLEN;
3257c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_SECKEY_WITHLEN);
3267c478bd9Sstevel@tonic-gate break;
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate default:
3297c478bd9Sstevel@tonic-gate /* Unsupported. */
3307c478bd9Sstevel@tonic-gate break;
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate break;
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate case CKO_DOMAIN_PARAMETERS:
3357c478bd9Sstevel@tonic-gate switch (subtype) {
3367c478bd9Sstevel@tonic-gate case CKK_DSA:
3377c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_DOM_DSA;
3387c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_DOM_DSA);
3397c478bd9Sstevel@tonic-gate break;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate case CKK_DH:
3427c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_DOM_DH;
3437c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_DOM_DH);
3447c478bd9Sstevel@tonic-gate break;
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate case CKK_X9_42_DH:
3477c478bd9Sstevel@tonic-gate master_template = (generic_attr_t *)OBJ_DOM_X942DH;
3487c478bd9Sstevel@tonic-gate master_template_size = sizeof (OBJ_DOM_X942DH);
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate default:
3527c478bd9Sstevel@tonic-gate /* Unsupported. */
3537c478bd9Sstevel@tonic-gate break;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate break;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate default:
3587c478bd9Sstevel@tonic-gate /* Unsupported. */
3597c478bd9Sstevel@tonic-gate break;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate /* Requested object is unknown or invalid. */
3637c478bd9Sstevel@tonic-gate if (master_template == NULL)
3647c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
3657c478bd9Sstevel@tonic-gate else {
3667c478bd9Sstevel@tonic-gate *attributes = master_template;
3677c478bd9Sstevel@tonic-gate *num_attributes = master_template_size;
3687c478bd9Sstevel@tonic-gate return (CKR_OK);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate * get_master_attributes_by_type
3757c478bd9Sstevel@tonic-gate *
3767c478bd9Sstevel@tonic-gate * Returns an (statically allocated) set of object attributes, as determined by
3777c478bd9Sstevel@tonic-gate * the specified class and subtype. The attributes are initialized to default
3787c478bd9Sstevel@tonic-gate * values.
3797c478bd9Sstevel@tonic-gate */
3807c478bd9Sstevel@tonic-gate CK_RV
get_master_attributes_by_type(CK_OBJECT_CLASS class,CK_ULONG subtype,generic_attr_t ** attributes,size_t * num_attributes)3817c478bd9Sstevel@tonic-gate get_master_attributes_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
3827c478bd9Sstevel@tonic-gate generic_attr_t **attributes, size_t *num_attributes)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate CK_RV rv;
3857c478bd9Sstevel@tonic-gate generic_attr_t *master_template = NULL;
3867c478bd9Sstevel@tonic-gate generic_attr_t *new_attributes;
3877c478bd9Sstevel@tonic-gate size_t i, num_new_attributes, master_template_size = 0;
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /* Determine the appropriate master template needed. */
3907c478bd9Sstevel@tonic-gate rv = get_master_template_by_type(class, subtype,
3917c478bd9Sstevel@tonic-gate &master_template, &master_template_size);
3927c478bd9Sstevel@tonic-gate if (rv != CKR_OK)
3937c478bd9Sstevel@tonic-gate return (rv);
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /* Duplicate the master template. */
3967c478bd9Sstevel@tonic-gate new_attributes = malloc(master_template_size);
3977c478bd9Sstevel@tonic-gate if (new_attributes == NULL)
3987c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate (void) memcpy(new_attributes, master_template, master_template_size);
4017c478bd9Sstevel@tonic-gate num_new_attributes = master_template_size / sizeof (generic_attr_t);
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate /* Set the pointer in the appropriate storage area. */
4047c478bd9Sstevel@tonic-gate for (i = 0; i < num_new_attributes; i++) {
4057c478bd9Sstevel@tonic-gate generic_attr_t *attr;
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate attr = new_attributes + i;
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate switch (attr->attribute.ulValueLen) {
4107c478bd9Sstevel@tonic-gate case (sizeof (CK_ULONG)):
4117c478bd9Sstevel@tonic-gate attr->attribute.pValue = &attr->generic_ulong;
4127c478bd9Sstevel@tonic-gate break;
4137c478bd9Sstevel@tonic-gate case (sizeof (CK_BBOOL)):
4147c478bd9Sstevel@tonic-gate attr->attribute.pValue = &attr->generic_bbool;
4157c478bd9Sstevel@tonic-gate break;
4167c478bd9Sstevel@tonic-gate default:
4177c478bd9Sstevel@tonic-gate attr->attribute.pValue = attr->generic_data;
4187c478bd9Sstevel@tonic-gate break;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /* Secret keys share a common template, so set the key type here. */
4247c478bd9Sstevel@tonic-gate if (class == CKO_SECRET_KEY) {
4257c478bd9Sstevel@tonic-gate /* Keytype / subtype is always the second attribute. */
4267c478bd9Sstevel@tonic-gate new_attributes[1].generic_ulong = subtype;
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate *attributes = new_attributes;
4307c478bd9Sstevel@tonic-gate *num_attributes = num_new_attributes;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate return (CKR_OK);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * get_master_attributes_by_duplication
4387c478bd9Sstevel@tonic-gate *
4397c478bd9Sstevel@tonic-gate * Returns an (statically allocated) set of object attributes, as copied from an
4407c478bd9Sstevel@tonic-gate * existing set of attributes. The new attributes inherit the values from
4417c478bd9Sstevel@tonic-gate * the old attributes.
4427c478bd9Sstevel@tonic-gate */
4437c478bd9Sstevel@tonic-gate CK_RV
get_master_attributes_by_duplication(generic_attr_t * src_attrs,size_t num_src_attrs,generic_attr_t ** dst_attrs,size_t * num_dst_attrs)4447c478bd9Sstevel@tonic-gate get_master_attributes_by_duplication(
4457c478bd9Sstevel@tonic-gate generic_attr_t *src_attrs, size_t num_src_attrs,
4467c478bd9Sstevel@tonic-gate generic_attr_t **dst_attrs, size_t *num_dst_attrs)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
4497c478bd9Sstevel@tonic-gate generic_attr_t *new_attrs, *src, *dst;
4507c478bd9Sstevel@tonic-gate size_t i;
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate new_attrs = malloc(sizeof (generic_attr_t) * num_src_attrs);
4537c478bd9Sstevel@tonic-gate if (new_attrs == NULL)
4547c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate for (i = 0; i < num_src_attrs; i++) {
4577c478bd9Sstevel@tonic-gate src = src_attrs + i;
4587c478bd9Sstevel@tonic-gate dst = new_attrs + i;
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate *dst = *src;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate /* Adjust pointers in dst so that they don't point to src. */
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if (src->isMalloced) {
4657c478bd9Sstevel@tonic-gate dst->attribute.pValue =
4667c478bd9Sstevel@tonic-gate malloc(src->attribute.ulValueLen);
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate if (dst->attribute.pValue == NULL) {
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate * Continue on error, so that the cleanup
4717c478bd9Sstevel@tonic-gate * routine doesn't see pointers to src_attrs.
4727c478bd9Sstevel@tonic-gate */
4737c478bd9Sstevel@tonic-gate dst->attribute.ulValueLen = 0;
4747c478bd9Sstevel@tonic-gate rv = CKR_HOST_MEMORY;
4757c478bd9Sstevel@tonic-gate continue;
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate } else if (src->attribute.pValue == &src->generic_bbool) {
4787c478bd9Sstevel@tonic-gate dst->attribute.pValue = &dst->generic_bbool;
4797c478bd9Sstevel@tonic-gate } else if (src->attribute.pValue == &src->generic_ulong) {
4807c478bd9Sstevel@tonic-gate dst->attribute.pValue = &dst->generic_ulong;
4817c478bd9Sstevel@tonic-gate } else if (src->attribute.pValue == &src->generic_data) {
4827c478bd9Sstevel@tonic-gate dst->attribute.pValue = &dst->generic_data;
4837c478bd9Sstevel@tonic-gate } else {
4847c478bd9Sstevel@tonic-gate /* This shouldn't happen. */
4857c478bd9Sstevel@tonic-gate dst->attribute.pValue = NULL;
4867c478bd9Sstevel@tonic-gate dst->attribute.ulValueLen = 0;
4877c478bd9Sstevel@tonic-gate rv = CKR_GENERAL_ERROR;
4887c478bd9Sstevel@tonic-gate num_src_attrs = i + 1;
4897c478bd9Sstevel@tonic-gate break;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate (void) memcpy(dst->attribute.pValue, src->attribute.pValue,
4937c478bd9Sstevel@tonic-gate src->attribute.ulValueLen);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate if (rv != CKR_OK) {
4977c478bd9Sstevel@tonic-gate dealloc_attributes(new_attrs, num_src_attrs);
4987c478bd9Sstevel@tonic-gate } else {
4997c478bd9Sstevel@tonic-gate *dst_attrs = new_attrs;
5007c478bd9Sstevel@tonic-gate *num_dst_attrs = num_src_attrs;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate return (rv);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate * dealloc_attributes
5097c478bd9Sstevel@tonic-gate *
5107c478bd9Sstevel@tonic-gate * Deallocates the storage used for a set of attributes. The attribute
5117c478bd9Sstevel@tonic-gate * values are zeroed out before being free'd.
5127c478bd9Sstevel@tonic-gate */
5137c478bd9Sstevel@tonic-gate void
dealloc_attributes(generic_attr_t * attributes,size_t num_attributes)5147c478bd9Sstevel@tonic-gate dealloc_attributes(generic_attr_t *attributes, size_t num_attributes)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate size_t i;
5177c478bd9Sstevel@tonic-gate generic_attr_t *attr;
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate for (i = 0; i < num_attributes; i++) {
5207c478bd9Sstevel@tonic-gate attr = attributes + i;
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate * Zero-out any attribute values. We could do this just for
5247c478bd9Sstevel@tonic-gate * attributes with isSensitive == True, but it's not much
5257c478bd9Sstevel@tonic-gate * extra work to just do them all. [Most attributes are just
5267c478bd9Sstevel@tonic-gate * 1 or 4 bytes]
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate bzero(attr->attribute.pValue, attr->attribute.ulValueLen);
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate if (attr->isMalloced)
5317c478bd9Sstevel@tonic-gate free(attr->attribute.pValue);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate free(attributes);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * attribute_set_value
5407c478bd9Sstevel@tonic-gate *
5417c478bd9Sstevel@tonic-gate * Sets the value of the specified attribute. Any portion of the old value
5427c478bd9Sstevel@tonic-gate * which will not be overwritten by the new value is zeroed out.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate CK_RV
attribute_set_value(CK_ATTRIBUTE * new_attr,generic_attr_t * attributes,size_t num_attributes)5457c478bd9Sstevel@tonic-gate attribute_set_value(CK_ATTRIBUTE *new_attr,
5467c478bd9Sstevel@tonic-gate generic_attr_t *attributes, size_t num_attributes)
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate generic_attr_t *attr = NULL;
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate if (new_attr == NULL)
5517c478bd9Sstevel@tonic-gate return (CKR_TEMPLATE_INCOMPLETE);
5527c478bd9Sstevel@tonic-gate else if (new_attr->pValue == NULL) {
5537c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_VALUE_INVALID);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate find_attribute(new_attr->type, attributes, num_attributes, &attr);
5577c478bd9Sstevel@tonic-gate if (attr == NULL) {
5587c478bd9Sstevel@tonic-gate return (CKR_ATTRIBUTE_TYPE_INVALID);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate /* Store the new value. */
5627c478bd9Sstevel@tonic-gate if (attr->attribute.ulValueLen >= new_attr->ulValueLen) {
5637c478bd9Sstevel@tonic-gate /* Existing storage is sufficient to store new value. */
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate /* bzero() out any data that won't be overwritten. */
5667c478bd9Sstevel@tonic-gate bzero((char *)attr->attribute.pValue + new_attr->ulValueLen,
5677c478bd9Sstevel@tonic-gate attr->attribute.ulValueLen - new_attr->ulValueLen);
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate } else if (new_attr->ulValueLen <= sizeof (attr->generic_data)) {
5707c478bd9Sstevel@tonic-gate /* Use generic storage to avoid a malloc. */
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate bzero(attr->attribute.pValue, attr->attribute.ulValueLen);
5737c478bd9Sstevel@tonic-gate if (attr->isMalloced) {
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate * If app sets a large value (triggering a malloc),
5767c478bd9Sstevel@tonic-gate * then sets a tiny value, and finally again sets
5777c478bd9Sstevel@tonic-gate * a large value (phew!) we could end up here.
5787c478bd9Sstevel@tonic-gate *
5797c478bd9Sstevel@tonic-gate * FUTURE?: Store the original malloc size, so that
5807c478bd9Sstevel@tonic-gate * we can regrow the value up to the original size.
5817c478bd9Sstevel@tonic-gate * This might avoid some heap churn for pathalogic
5827c478bd9Sstevel@tonic-gate * applications.
5837c478bd9Sstevel@tonic-gate */
5847c478bd9Sstevel@tonic-gate free(attr->attribute.pValue);
5857c478bd9Sstevel@tonic-gate attr->isMalloced = B_FALSE;
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate attr->attribute.pValue = attr->generic_data;
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate } else {
5917c478bd9Sstevel@tonic-gate /* Need to allocate storage for the new value. */
5927c478bd9Sstevel@tonic-gate void *newStorage;
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate newStorage = malloc(new_attr->ulValueLen);
5957c478bd9Sstevel@tonic-gate if (newStorage == NULL)
5967c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
5977c478bd9Sstevel@tonic-gate bzero(attr->attribute.pValue, attr->attribute.ulValueLen);
5987c478bd9Sstevel@tonic-gate attr->attribute.pValue = newStorage;
5997c478bd9Sstevel@tonic-gate attr->isMalloced = B_TRUE;
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate (void) memcpy(attr->attribute.pValue, new_attr->pValue,
6037c478bd9Sstevel@tonic-gate new_attr->ulValueLen);
6047c478bd9Sstevel@tonic-gate attr->attribute.ulValueLen = new_attr->ulValueLen;
6057c478bd9Sstevel@tonic-gate attr->hasValueForClone = B_TRUE;
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate return (CKR_OK);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate * find_attribute
6137c478bd9Sstevel@tonic-gate *
6147c478bd9Sstevel@tonic-gate * Passes a pointer to the requested attribute, or NULL if not found.
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate static void
find_attribute(CK_ATTRIBUTE_TYPE attrtype,generic_attr_t * attributes,size_t num_attributes,generic_attr_t ** found_attribute)6177c478bd9Sstevel@tonic-gate find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
6187c478bd9Sstevel@tonic-gate size_t num_attributes, generic_attr_t **found_attribute)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate generic_attr_t *attr;
6217c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE;
6227c478bd9Sstevel@tonic-gate size_t i;
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /* Find the requested attribute. */
6257c478bd9Sstevel@tonic-gate for (i = 0, attr = attributes; i < num_attributes; i++, attr++) {
6267c478bd9Sstevel@tonic-gate if (attr->attribute.type == attrtype) {
6277c478bd9Sstevel@tonic-gate found = B_TRUE;
6287c478bd9Sstevel@tonic-gate break;
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate *found_attribute = found ? attr : NULL;
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * get_template_ulong
6387c478bd9Sstevel@tonic-gate *
6397c478bd9Sstevel@tonic-gate * Look for the specified ulong-size attribute, and retrieve its value. The
6407c478bd9Sstevel@tonic-gate * return value specifies if the attribute was found (or not).
6417c478bd9Sstevel@tonic-gate */
6427c478bd9Sstevel@tonic-gate boolean_t
get_template_ulong(CK_ATTRIBUTE_TYPE type,CK_ATTRIBUTE * attributes,CK_ULONG num_attributes,CK_ULONG * result)6437c478bd9Sstevel@tonic-gate get_template_ulong(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
6447c478bd9Sstevel@tonic-gate CK_ULONG num_attributes, CK_ULONG *result)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE;
6477c478bd9Sstevel@tonic-gate CK_ULONG i;
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate for (i = 0; i < num_attributes; i++) {
6507c478bd9Sstevel@tonic-gate if (attributes[i].type == type) {
6517c478bd9Sstevel@tonic-gate CK_ULONG *value = attributes[i].pValue;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate *result = *value;
6547c478bd9Sstevel@tonic-gate found = B_TRUE;
6557c478bd9Sstevel@tonic-gate break;
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate return (found);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate * get_template_boolean
6657c478bd9Sstevel@tonic-gate *
6667c478bd9Sstevel@tonic-gate * Look for the specified boolean attribute, and retrieve its value. The
6677c478bd9Sstevel@tonic-gate * return value specifies if the attribute was found (or not).
6687c478bd9Sstevel@tonic-gate */
6697c478bd9Sstevel@tonic-gate boolean_t
get_template_boolean(CK_ATTRIBUTE_TYPE type,CK_ATTRIBUTE * attributes,CK_ULONG num_attributes,boolean_t * result)6707c478bd9Sstevel@tonic-gate get_template_boolean(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
6717c478bd9Sstevel@tonic-gate CK_ULONG num_attributes, boolean_t *result)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE;
6747c478bd9Sstevel@tonic-gate CK_ULONG i;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate for (i = 0; i < num_attributes; i++) {
6777c478bd9Sstevel@tonic-gate if (attributes[i].type == type) {
6787c478bd9Sstevel@tonic-gate CK_BBOOL *value = attributes[i].pValue;
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate if (*value == CK_FALSE)
6817c478bd9Sstevel@tonic-gate *result = B_FALSE;
6827c478bd9Sstevel@tonic-gate else
6837c478bd9Sstevel@tonic-gate *result = B_TRUE;
6847c478bd9Sstevel@tonic-gate
6857c478bd9Sstevel@tonic-gate found = B_TRUE;
6867c478bd9Sstevel@tonic-gate break;
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate return (found);
6917c478bd9Sstevel@tonic-gate }
692034448feSmcpowers
693034448feSmcpowers /*
694034448feSmcpowers * set_template_boolean
695034448feSmcpowers *
696034448feSmcpowers * Look for the specified boolean attribute, and set its value.
697034448feSmcpowers *
698034448feSmcpowers * if 'local' is true, it sets the pointer to the value in the template a new
699034448feSmcpowers * location. There should be no memory leak created by this because we are
700034448feSmcpowers * only doing this to booleans which should not be malloc'ed.
701034448feSmcpowers *
702034448feSmcpowers * if 'local' is false, it sets its value.
703034448feSmcpowers *
704034448feSmcpowers * The return value specifies if the attribute was found (or not).
705034448feSmcpowers */
706034448feSmcpowers int
set_template_boolean(CK_ATTRIBUTE_TYPE type,CK_ATTRIBUTE * attributes,CK_ULONG num_attributes,boolean_t local,CK_BBOOL * value)707034448feSmcpowers set_template_boolean(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
708034448feSmcpowers CK_ULONG num_attributes, boolean_t local, CK_BBOOL *value)
709034448feSmcpowers {
710034448feSmcpowers int i;
711034448feSmcpowers
712034448feSmcpowers for (i = 0; i < num_attributes; i++) {
713034448feSmcpowers if (attributes[i].type == type) {
714034448feSmcpowers if (local)
715034448feSmcpowers attributes[i].pValue = value;
716034448feSmcpowers else
717034448feSmcpowers *((CK_BBOOL *)attributes[i].pValue) = *value;
718034448feSmcpowers
719034448feSmcpowers return (i);
720034448feSmcpowers }
721034448feSmcpowers }
722034448feSmcpowers
723034448feSmcpowers return (-1);
724034448feSmcpowers }
725