4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
29 #include <security/cryptoki.h>
30 #include <sys/crypto/common.h>
32 #include <blowfish_impl.h>
35 #include "kernelGlobal.h"
36 #include "kernelObject.h"
37 #include "kernelSession.h"
38 #include "kernelSlot.h"
42 * This attribute table is used by the kernel_lookup_attr()
43 * to validate the attributes.
45 CK_ATTRIBUTE_TYPE attr_map
[] = {
75 CKA_NEVER_EXTRACTABLE
,
88 * attributes that exists only in public key objects
89 * Note: some attributes may also exist in one or two
90 * other object classes, but they are also listed
91 * because not all object have them.
93 CK_ATTRIBUTE_TYPE PUB_KEY_ATTRS
[] =
113 * attributes that exists only in private key objects
114 * Note: some attributes may also exist in one or two
115 * other object classes, but they are also listed
116 * because not all object have them.
118 CK_ATTRIBUTE_TYPE PRIV_KEY_ATTRS
[] =
126 CKA_PRIVATE_EXPONENT
,
139 CKA_NEVER_EXTRACTABLE
,
140 CKA_ALWAYS_SENSITIVE
,
146 * attributes that exists only in secret key objects
147 * Note: some attributes may also exist in one or two
148 * other object classes, but they are also listed
149 * because not all object have them.
151 CK_ATTRIBUTE_TYPE SECRET_KEY_ATTRS
[] =
162 CKA_NEVER_EXTRACTABLE
,
167 * attributes that exists only in domain parameter objects
168 * Note: some attributes may also exist in one or two
169 * other object classes, but they are also listed
170 * because not all object have them.
172 CK_ATTRIBUTE_TYPE DOMAIN_ATTRS
[] =
183 * attributes that exists only in hardware feature objects
185 CK_ATTRIBUTE_TYPE HARDWARE_ATTRS
[] =
193 * attributes that exists only in certificate objects
195 CK_ATTRIBUTE_TYPE CERT_ATTRS
[] =
197 CKA_CERTIFICATE_TYPE
,
209 * Validate the attribute by using binary search algorithm.
212 kernel_lookup_attr(CK_ATTRIBUTE_TYPE type
)
215 size_t lower
, middle
, upper
;
218 upper
= (sizeof (attr_map
) / sizeof (CK_ATTRIBUTE_TYPE
)) - 1;
220 while (lower
<= upper
) {
221 /* Always starts from middle. */
222 middle
= (lower
+ upper
) / 2;
224 if (type
> attr_map
[middle
]) {
225 /* Adjust the lower bound to upper half. */
230 if (type
== attr_map
[middle
]) {
235 if (type
< attr_map
[middle
]) {
236 /* Adjust the upper bound to lower half. */
242 /* Failed to find the matching attribute from the attribute table. */
243 return (CKR_ATTRIBUTE_TYPE_INVALID
);
248 * Validate the attribute by using the following search algorithm:
250 * 1) Search for the most frequently used attributes first.
251 * 2) If not found, search for the usage-purpose attributes - these
252 * attributes have dense set of values, therefore compiler will
253 * optimize it with a branch table and branch to the appropriate
255 * 3) If still not found, use binary search for the rest of the
256 * attributes in the attr_map[] table.
259 kernel_validate_attr(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
260 CK_OBJECT_CLASS
*class)
266 for (i
= 0; i
< ulAttrNum
; i
++) {
267 /* First tier search */
268 switch (template[i
].type
) {
270 *class = *((CK_OBJECT_CLASS
*)template[i
].pValue
);
283 /* Second tier search */
284 switch (template[i
].type
) {
295 case CKA_SIGN_RECOVER
:
299 case CKA_VERIFY_RECOVER
:
304 /* Third tier search */
305 rv
= kernel_lookup_attr(template[i
].type
);
318 * Clean up and release all the storage in the extra attribute list
322 kernel_cleanup_extra_attr(kernel_object_t
*object_p
)
325 CK_ATTRIBUTE_INFO_PTR extra_attr
;
326 CK_ATTRIBUTE_INFO_PTR tmp
;
328 extra_attr
= object_p
->extra_attrlistp
;
330 tmp
= extra_attr
->next
;
331 free(extra_attr
->attr
.pValue
);
333 /* Free the storage for the attribute_info struct. */
338 object_p
->extra_attrlistp
= NULL
;
343 * Create the attribute_info struct to hold the object's attribute,
344 * and add it to the extra attribute list of an object.
347 kernel_add_extra_attr(CK_ATTRIBUTE_PTR
template, kernel_object_t
*object_p
)
350 CK_ATTRIBUTE_INFO_PTR attrp
;
352 /* Allocate the storage for the attribute_info struct. */
353 attrp
= calloc(1, sizeof (attribute_info_t
));
355 return (CKR_HOST_MEMORY
);
358 /* Set up attribute_info struct. */
359 attrp
->attr
.type
= template->type
;
360 attrp
->attr
.ulValueLen
= template->ulValueLen
;
362 if ((template->pValue
!= NULL
) &&
363 (template->ulValueLen
> 0)) {
364 /* Allocate storage for the value of the attribute. */
365 attrp
->attr
.pValue
= malloc(template->ulValueLen
);
366 if (attrp
->attr
.pValue
== NULL
) {
368 return (CKR_HOST_MEMORY
);
371 (void) memcpy(attrp
->attr
.pValue
, template->pValue
,
372 template->ulValueLen
);
374 attrp
->attr
.pValue
= NULL
;
377 /* Insert the new attribute in front of extra attribute list. */
378 if (object_p
->extra_attrlistp
== NULL
) {
379 object_p
->extra_attrlistp
= attrp
;
382 attrp
->next
= object_p
->extra_attrlistp
;
383 object_p
->extra_attrlistp
= attrp
;
391 * Copy the attribute_info struct from the old object to a new attribute_info
392 * struct, and add that new struct to the extra attribute list of the new
396 kernel_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp
,
397 kernel_object_t
*object_p
)
399 CK_ATTRIBUTE_INFO_PTR attrp
;
401 /* Allocate attribute_info struct. */
402 attrp
= calloc(1, sizeof (attribute_info_t
));
404 return (CKR_HOST_MEMORY
);
407 attrp
->attr
.type
= old_attrp
->attr
.type
;
408 attrp
->attr
.ulValueLen
= old_attrp
->attr
.ulValueLen
;
410 if ((old_attrp
->attr
.pValue
!= NULL
) &&
411 (old_attrp
->attr
.ulValueLen
> 0)) {
412 attrp
->attr
.pValue
= malloc(old_attrp
->attr
.ulValueLen
);
413 if (attrp
->attr
.pValue
== NULL
) {
415 return (CKR_HOST_MEMORY
);
418 (void) memcpy(attrp
->attr
.pValue
, old_attrp
->attr
.pValue
,
419 old_attrp
->attr
.ulValueLen
);
421 attrp
->attr
.pValue
= NULL
;
424 /* Insert the new attribute in front of extra attribute list */
425 if (object_p
->extra_attrlistp
== NULL
) {
426 object_p
->extra_attrlistp
= attrp
;
429 attrp
->next
= object_p
->extra_attrlistp
;
430 object_p
->extra_attrlistp
= attrp
;
438 * Get the attribute triple from the extra attribute list in the object
439 * (if the specified attribute type is found), and copy it to a template.
440 * Note the type of the attribute to be copied is specified by the template,
441 * and the storage is pre-allocated for the atrribute value in the template
442 * for doing the copy.
445 get_extra_attr_from_object(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
448 CK_ATTRIBUTE_INFO_PTR extra_attr
;
449 CK_ATTRIBUTE_TYPE type
= template->type
;
451 extra_attr
= object_p
->extra_attrlistp
;
454 if (type
== extra_attr
->attr
.type
) {
458 /* Does not match, try next one. */
459 extra_attr
= extra_attr
->next
;
463 if (extra_attr
== NULL
) {
464 /* A valid but un-initialized attribute. */
465 template->ulValueLen
= 0;
470 * We found the attribute in the extra attribute list.
472 if (template->pValue
== NULL
) {
473 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
477 if (template->ulValueLen
>= extra_attr
->attr
.ulValueLen
) {
479 * The buffer provided by the application is large
480 * enough to hold the value of the attribute.
482 (void) memcpy(template->pValue
, extra_attr
->attr
.pValue
,
483 extra_attr
->attr
.ulValueLen
);
484 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
488 * The buffer provided by the application does
489 * not have enough space to hold the value.
491 template->ulValueLen
= (CK_ULONG
)-1;
492 return (CKR_BUFFER_TOO_SMALL
);
498 * Modify the attribute triple in the extra attribute list of the object
499 * if the specified attribute type is found. Otherwise, just add it to
503 set_extra_attr_to_object(kernel_object_t
*object_p
, CK_ATTRIBUTE_TYPE type
,
504 CK_ATTRIBUTE_PTR
template)
507 CK_ATTRIBUTE_INFO_PTR extra_attr
;
509 extra_attr
= object_p
->extra_attrlistp
;
512 if (type
== extra_attr
->attr
.type
) {
516 /* Does not match, try next one. */
517 extra_attr
= extra_attr
->next
;
521 if (extra_attr
== NULL
) {
523 * This attribute is a new one, go ahead adding it to
524 * the extra attribute list.
526 return (kernel_add_extra_attr(template, object_p
));
529 /* We found the attribute in the extra attribute list. */
530 if ((template->pValue
!= NULL
) &&
531 (template->ulValueLen
> 0)) {
532 if (template->ulValueLen
> extra_attr
->attr
.ulValueLen
) {
533 /* The old buffer is too small to hold the new value. */
534 free(extra_attr
->attr
.pValue
);
536 /* Allocate storage for the new attribute value. */
537 extra_attr
->attr
.pValue
= malloc(template->ulValueLen
);
538 if (extra_attr
->attr
.pValue
== NULL
) {
539 return (CKR_HOST_MEMORY
);
543 /* Replace the attribute with new value. */
544 extra_attr
->attr
.ulValueLen
= template->ulValueLen
;
545 (void) memcpy(extra_attr
->attr
.pValue
, template->pValue
,
546 template->ulValueLen
);
548 extra_attr
->attr
.pValue
= NULL
;
556 * Copy the big integer attribute value from template to a biginteger_t struct.
559 get_bigint_attr_from_template(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
562 if ((template->pValue
!= NULL
) &&
563 (template->ulValueLen
> 0)) {
564 /* Allocate storage for the value of the attribute. */
565 big
->big_value
= malloc(template->ulValueLen
);
566 if (big
->big_value
== NULL
) {
567 return (CKR_HOST_MEMORY
);
570 (void) memcpy(big
->big_value
, template->pValue
,
571 template->ulValueLen
);
572 big
->big_value_len
= template->ulValueLen
;
574 big
->big_value
= NULL
;
575 big
->big_value_len
= 0;
583 * Copy the big integer attribute value from a biginteger_t struct in the
584 * object to a template.
587 get_bigint_attr_from_object(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
590 if (template->pValue
== NULL
) {
591 template->ulValueLen
= big
->big_value_len
;
595 if (big
->big_value
== NULL
) {
596 template->ulValueLen
= 0;
600 if (template->ulValueLen
>= big
->big_value_len
) {
602 * The buffer provided by the application is large
603 * enough to hold the value of the attribute.
605 (void) memcpy(template->pValue
, big
->big_value
,
607 template->ulValueLen
= big
->big_value_len
;
611 * The buffer provided by the application does
612 * not have enough space to hold the value.
614 template->ulValueLen
= (CK_ULONG
)-1;
615 return (CKR_BUFFER_TOO_SMALL
);
621 * Copy the boolean data type attribute value from an object for the
622 * specified attribute to the template.
625 get_bool_attr_from_object(kernel_object_t
*object_p
, CK_ULONG bool_flag
,
626 CK_ATTRIBUTE_PTR
template)
629 if (template->pValue
== NULL
) {
630 template->ulValueLen
= sizeof (CK_BBOOL
);
634 if (template->ulValueLen
>= sizeof (CK_BBOOL
)) {
636 * The buffer provided by the application is large
637 * enough to hold the value of the attribute.
639 if (object_p
->bool_attr_mask
& bool_flag
) {
640 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
642 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
645 template->ulValueLen
= sizeof (CK_BBOOL
);
649 * The buffer provided by the application does
650 * not have enough space to hold the value.
652 template->ulValueLen
= (CK_ULONG
)-1;
653 return (CKR_BUFFER_TOO_SMALL
);
658 * Set the boolean data type attribute value in the object.
661 set_bool_attr_to_object(kernel_object_t
*object_p
, CK_ULONG bool_flag
,
662 CK_ATTRIBUTE_PTR
template)
665 if (*(CK_BBOOL
*)template->pValue
)
666 object_p
->bool_attr_mask
|= bool_flag
;
668 object_p
->bool_attr_mask
&= ~bool_flag
;
675 * Copy the CK_ULONG data type attribute value from an object to the
679 get_ulong_attr_from_object(CK_ULONG value
, CK_ATTRIBUTE_PTR
template)
682 if (template->pValue
== NULL
) {
683 template->ulValueLen
= sizeof (CK_ULONG
);
687 if (template->ulValueLen
>= sizeof (CK_ULONG
)) {
689 * The buffer provided by the application is large
690 * enough to hold the value of the attribute.
692 *(CK_ULONG_PTR
)template->pValue
= value
;
693 template->ulValueLen
= sizeof (CK_ULONG
);
697 * The buffer provided by the application does
698 * not have enough space to hold the value.
700 template->ulValueLen
= (CK_ULONG
)-1;
701 return (CKR_BUFFER_TOO_SMALL
);
707 * Copy the CK_ULONG data type attribute value from a template to the
711 get_ulong_attr_from_template(CK_ULONG
*value
, CK_ATTRIBUTE_PTR
template)
714 if (template->pValue
!= NULL
) {
715 *value
= *(CK_ULONG_PTR
)template->pValue
;
723 * Copy the big integer attribute value from source's biginteger_t to
724 * destination's biginteger_t.
727 copy_bigint_attr(biginteger_t
*src
, biginteger_t
*dst
)
730 if ((src
->big_value
!= NULL
) &&
731 (src
->big_value_len
> 0)) {
733 * To do the copy, just have dst's big_value points
736 dst
->big_value
= src
->big_value
;
737 dst
->big_value_len
= src
->big_value_len
;
740 * After the copy, nullify the src's big_value pointer.
741 * It prevents any double freeing the value.
743 src
->big_value
= NULL
;
744 src
->big_value_len
= 0;
746 dst
->big_value
= NULL
;
747 dst
->big_value_len
= 0;
754 get_string_from_template(CK_ATTRIBUTE_PTR dest
, CK_ATTRIBUTE_PTR src
)
756 if ((src
->pValue
!= NULL
) &&
757 (src
->ulValueLen
> 0)) {
758 /* Allocate storage for the value of the attribute. */
759 dest
->pValue
= malloc(src
->ulValueLen
);
760 if (dest
->pValue
== NULL
) {
761 return (CKR_HOST_MEMORY
);
764 (void) memcpy(dest
->pValue
, src
->pValue
,
766 dest
->ulValueLen
= src
->ulValueLen
;
767 dest
->type
= src
->type
;
770 dest
->ulValueLen
= 0;
771 dest
->type
= src
->type
;
779 string_attr_cleanup(CK_ATTRIBUTE_PTR
template)
782 if (template->pValue
) {
783 free(template->pValue
);
784 template->pValue
= NULL
;
785 template->ulValueLen
= 0;
790 * Release the storage allocated for object attribute with big integer
794 bigint_attr_cleanup(biginteger_t
*big
)
800 if (big
->big_value
) {
801 (void) memset(big
->big_value
, 0, big
->big_value_len
);
802 free(big
->big_value
);
803 big
->big_value
= NULL
;
804 big
->big_value_len
= 0;
810 * Clean up and release all the storage allocated to hold the big integer
811 * attributes associated with the type (i.e. class) of the object. Also,
812 * release the storage allocated to the type of the object.
815 kernel_cleanup_object_bigint_attrs(kernel_object_t
*object_p
)
818 CK_OBJECT_CLASS
class = object_p
->class;
819 CK_KEY_TYPE keytype
= object_p
->key_type
;
824 if (OBJ_PUB(object_p
)) {
827 bigint_attr_cleanup(OBJ_PUB_RSA_MOD(
829 bigint_attr_cleanup(OBJ_PUB_RSA_PUBEXPO(
834 bigint_attr_cleanup(OBJ_PUB_DSA_PRIME(
836 bigint_attr_cleanup(OBJ_PUB_DSA_SUBPRIME(
838 bigint_attr_cleanup(OBJ_PUB_DSA_BASE(
840 bigint_attr_cleanup(OBJ_PUB_DSA_VALUE(
845 bigint_attr_cleanup(OBJ_PUB_DH_PRIME(object_p
));
846 bigint_attr_cleanup(OBJ_PUB_DH_BASE(object_p
));
847 bigint_attr_cleanup(OBJ_PUB_DH_VALUE(object_p
));
851 bigint_attr_cleanup(OBJ_PUB_EC_POINT(object_p
));
855 /* Release Public Key Object struct */
856 free(OBJ_PUB(object_p
));
857 OBJ_PUB(object_p
) = NULL
;
861 case CKO_PRIVATE_KEY
:
862 if (OBJ_PRI(object_p
)) {
865 bigint_attr_cleanup(OBJ_PRI_RSA_MOD(
867 bigint_attr_cleanup(OBJ_PRI_RSA_PUBEXPO(
869 bigint_attr_cleanup(OBJ_PRI_RSA_PRIEXPO(
871 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME1(
873 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME2(
875 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO1(
877 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO2(
879 bigint_attr_cleanup(OBJ_PRI_RSA_COEF(
884 bigint_attr_cleanup(OBJ_PRI_DSA_PRIME(
886 bigint_attr_cleanup(OBJ_PRI_DSA_SUBPRIME(
888 bigint_attr_cleanup(OBJ_PRI_DSA_BASE(
890 bigint_attr_cleanup(OBJ_PRI_DSA_VALUE(
895 bigint_attr_cleanup(OBJ_PRI_DH_PRIME(object_p
));
896 bigint_attr_cleanup(OBJ_PRI_DH_BASE(object_p
));
897 bigint_attr_cleanup(OBJ_PRI_DH_VALUE(object_p
));
901 bigint_attr_cleanup(OBJ_PRI_EC_VALUE(object_p
));
905 /* Release Private Key Object struct. */
906 free(OBJ_PRI(object_p
));
907 OBJ_PRI(object_p
) = NULL
;
915 * Parse the common attributes. Return to caller with appropriate return
916 * value to indicate if the supplied template specifies a valid attribute
917 * with a valid value.
920 kernel_parse_common_attrs(CK_ATTRIBUTE_PTR
template, kernel_session_t
*sp
,
921 uint64_t *attr_mask_p
)
925 kernel_slot_t
*pslot
= slot_table
[sp
->ses_slotid
];
927 switch (template->type
) {
931 /* default boolean attributes */
933 if ((*(CK_BBOOL
*)template->pValue
) == TRUE
) {
934 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
939 if ((*(CK_BBOOL
*)template->pValue
) == TRUE
) {
941 * Cannot create a private object if the token
942 * has a keystore and the user isn't logged in.
944 if (pslot
->sl_func_list
.fl_object_create
&&
945 pslot
->sl_state
!= CKU_USER
) {
946 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
948 *attr_mask_p
|= PRIVATE_BOOL_ON
;
954 if ((*(CK_BBOOL
*)template->pValue
) == FALSE
) {
955 *attr_mask_p
&= ~MODIFIABLE_BOOL_ON
;
963 rv
= CKR_TEMPLATE_INCONSISTENT
;
973 * Build a Public Key Object.
975 * - Parse the object's template, and when an error is detected such as
976 * invalid attribute type, invalid attribute value, etc., return
977 * with appropriate return value.
978 * - Set up attribute mask field in the object for the supplied common
979 * attributes that have boolean type.
980 * - Build the attribute_info struct to hold the value of each supplied
981 * attribute that has byte array type. Link attribute_info structs
982 * together to form the extra attribute list of the object.
983 * - Allocate storage for the Public Key object.
984 * - Build the Public Key object according to the key type. Allocate
985 * storage to hold the big integer value for the supplied attributes
986 * that are required for a certain key type.
990 kernel_build_public_key_object(CK_ATTRIBUTE_PTR
template,
991 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
,
996 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
997 uint64_t attr_mask
= PUBLIC_KEY_DEFAULT
;
1000 /* Must set flags */
1009 /* Must not set flags */
1010 int isModulusBits
= 0;
1011 CK_ULONG modulus_bits
= 0;
1013 biginteger_t modulus
;
1014 biginteger_t pubexpo
;
1016 biginteger_t subprime
;
1020 CK_ATTRIBUTE string_tmp
;
1021 CK_ATTRIBUTE param_tmp
;
1023 public_key_obj_t
*pbk
;
1025 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1026 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1027 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1028 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1029 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1030 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1031 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1032 (void) memset(&point
, 0x0, sizeof (biginteger_t
));
1033 string_tmp
.pValue
= NULL
;
1034 param_tmp
.pValue
= NULL
;
1036 for (i
= 0; i
< ulAttrNum
; i
++) {
1038 /* Public Key Object Attributes */
1039 switch (template[i
].type
) {
1041 /* common key attributes */
1043 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1047 case CKA_START_DATE
:
1050 /* common public key attribute */
1053 * Allocate storage to hold the attribute
1054 * value with byte array type, and add it to
1055 * the extra attribute list of the object.
1057 rv
= kernel_add_extra_attr(&template[i
],
1065 * The following key related attribute types must
1066 * not be specified by C_CreateObject.
1069 case CKA_KEY_GEN_MECHANISM
:
1070 rv
= CKR_TEMPLATE_INCONSISTENT
;
1073 /* Key related boolean attributes */
1075 if (*(CK_BBOOL
*)template[i
].pValue
)
1076 attr_mask
|= DERIVE_BOOL_ON
;
1080 if (*(CK_BBOOL
*)template[i
].pValue
)
1081 attr_mask
|= ENCRYPT_BOOL_ON
;
1083 attr_mask
&= ~ENCRYPT_BOOL_ON
;
1087 if (*(CK_BBOOL
*)template[i
].pValue
)
1088 attr_mask
|= VERIFY_BOOL_ON
;
1090 attr_mask
&= ~VERIFY_BOOL_ON
;
1093 case CKA_VERIFY_RECOVER
:
1094 if (*(CK_BBOOL
*)template[i
].pValue
)
1095 attr_mask
|= VERIFY_RECOVER_BOOL_ON
;
1097 attr_mask
&= ~VERIFY_RECOVER_BOOL_ON
;
1101 if (*(CK_BBOOL
*)template[i
].pValue
)
1102 attr_mask
|= WRAP_BOOL_ON
;
1106 if (*(CK_BBOOL
*)template[i
].pValue
)
1107 attr_mask
|= TRUSTED_BOOL_ON
;
1111 * The following key related attribute types must
1112 * be specified according to the key type by
1118 * Copyin big integer attribute from template
1119 * to a local variable.
1121 rv
= get_bigint_attr_from_template(&modulus
,
1127 case CKA_PUBLIC_EXPONENT
:
1129 rv
= get_bigint_attr_from_template(&pubexpo
,
1137 rv
= get_bigint_attr_from_template(&prime
,
1145 rv
= get_bigint_attr_from_template(&subprime
,
1153 rv
= get_bigint_attr_from_template(&base
,
1161 rv
= get_bigint_attr_from_template(&value
,
1167 case CKA_MODULUS_BITS
:
1169 get_ulong_attr_from_template(&modulus_bits
,
1175 rv
= get_string_from_template(&string_tmp
,
1183 rv
= get_bigint_attr_from_template(&point
,
1191 rv
= get_string_from_template(¶m_tmp
,
1198 rv
= kernel_parse_common_attrs(&template[i
], sp
,
1206 /* Allocate storage for Public Key Object. */
1207 pbk
= calloc(1, sizeof (public_key_obj_t
));
1209 rv
= CKR_HOST_MEMORY
;
1213 new_object
->object_class_u
.public_key
= pbk
;
1214 new_object
->class = CKO_PUBLIC_KEY
;
1216 if (keytype
== (CK_KEY_TYPE
)~0UL) {
1217 rv
= CKR_TEMPLATE_INCOMPLETE
;
1220 new_object
->key_type
= keytype
;
1222 /* Supported key types of the Public Key Object */
1225 if (mode
== KERNEL_CREATE_OBJ
) {
1226 if (isModulusBits
|| isPrime
|| isSubprime
||
1228 rv
= CKR_TEMPLATE_INCONSISTENT
;
1233 if (isModulus
&& isPubExpo
) {
1235 * Copy big integer attribute value to the
1236 * designated place in the public key object.
1238 copy_bigint_attr(&modulus
,
1239 KEY_PUB_RSA_MOD(pbk
));
1241 copy_bigint_attr(&pubexpo
,
1242 KEY_PUB_RSA_PUBEXPO(pbk
));
1244 rv
= CKR_TEMPLATE_INCOMPLETE
;
1248 /* must be generating a RSA key pair by value */
1249 if (isModulusBits
) {
1250 KEY_PUB_RSA_MOD_BITS(pbk
) = modulus_bits
;
1255 if (isModulusBits
|| isModulus
|| isPubExpo
) {
1256 rv
= CKR_TEMPLATE_INCONSISTENT
;
1260 if (!(isPrime
&& isSubprime
&& isBase
&& isValue
)) {
1261 rv
= CKR_TEMPLATE_INCOMPLETE
;
1265 copy_bigint_attr(&prime
, KEY_PUB_DSA_PRIME(pbk
));
1267 copy_bigint_attr(&subprime
, KEY_PUB_DSA_SUBPRIME(pbk
));
1269 copy_bigint_attr(&base
, KEY_PUB_DSA_BASE(pbk
));
1271 copy_bigint_attr(&value
, KEY_PUB_DSA_VALUE(pbk
));
1276 if (!(isPrime
&& isBase
&& isValue
)) {
1277 rv
= CKR_TEMPLATE_INCOMPLETE
;
1281 copy_bigint_attr(&prime
, KEY_PUB_DH_PRIME(pbk
));
1283 copy_bigint_attr(&base
, KEY_PUB_DH_BASE(pbk
));
1285 copy_bigint_attr(&value
, KEY_PUB_DH_VALUE(pbk
));
1290 if (!isPoint
|| !isParams
) {
1291 rv
= CKR_TEMPLATE_INCOMPLETE
;
1295 copy_bigint_attr(&point
, KEY_PUB_EC_POINT(pbk
));
1296 rv
= kernel_add_extra_attr(¶m_tmp
, new_object
);
1299 string_attr_cleanup(¶m_tmp
);
1302 rv
= CKR_TEMPLATE_INCONSISTENT
;
1306 /* Set up object. */
1307 new_object
->bool_attr_mask
= attr_mask
;
1309 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
1312 string_attr_cleanup(&string_tmp
);
1319 * cleanup the storage allocated to the local variables.
1321 bigint_attr_cleanup(&modulus
);
1322 bigint_attr_cleanup(&pubexpo
);
1323 bigint_attr_cleanup(&prime
);
1324 bigint_attr_cleanup(&subprime
);
1325 bigint_attr_cleanup(&base
);
1326 bigint_attr_cleanup(&value
);
1327 bigint_attr_cleanup(&point
);
1328 string_attr_cleanup(&string_tmp
);
1329 string_attr_cleanup(¶m_tmp
);
1332 * cleanup the storage allocated inside the object itself.
1334 kernel_cleanup_object(new_object
);
1341 * Build a Private Key Object.
1343 * - Parse the object's template, and when an error is detected such as
1344 * invalid attribute type, invalid attribute value, etc., return
1345 * with appropriate return value.
1346 * - Set up attribute mask field in the object for the supplied common
1347 * attributes that have boolean type.
1348 * - Build the attribute_info struct to hold the value of each supplied
1349 * attribute that has byte array type. Link attribute_info structs
1350 * together to form the extra attribute list of the object.
1351 * - Allocate storage for the Private Key object.
1352 * - Build the Private Key object according to the key type. Allocate
1353 * storage to hold the big integer value for the supplied attributes
1354 * that are required for a certain key type.
1358 kernel_build_private_key_object(CK_ATTRIBUTE_PTR
template,
1359 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
,
1363 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1364 uint64_t attr_mask
= PRIVATE_KEY_DEFAULT
;
1367 /* Must set flags */
1375 /* Must not set flags */
1376 int isValueBits
= 0;
1377 CK_ULONG value_bits
= 0;
1379 /* Private Key RSA optional */
1387 biginteger_t modulus
;
1388 biginteger_t priexpo
;
1390 biginteger_t subprime
;
1394 biginteger_t pubexpo
;
1395 biginteger_t prime1
;
1396 biginteger_t prime2
;
1400 CK_ATTRIBUTE string_tmp
;
1401 CK_ATTRIBUTE param_tmp
;
1403 private_key_obj_t
*pvk
;
1405 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1406 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1407 (void) memset(&priexpo
, 0x0, sizeof (biginteger_t
));
1408 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1409 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1410 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1411 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1412 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1413 (void) memset(&prime1
, 0x0, sizeof (biginteger_t
));
1414 (void) memset(&prime2
, 0x0, sizeof (biginteger_t
));
1415 (void) memset(&expo1
, 0x0, sizeof (biginteger_t
));
1416 (void) memset(&expo2
, 0x0, sizeof (biginteger_t
));
1417 (void) memset(&coef
, 0x0, sizeof (biginteger_t
));
1418 string_tmp
.pValue
= NULL
;
1419 param_tmp
.pValue
= NULL
;
1421 for (i
= 0; i
< ulAttrNum
; i
++) {
1423 /* Private Key Object Attributes */
1424 switch (template[i
].type
) {
1426 /* common key attributes */
1428 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1432 case CKA_START_DATE
:
1435 /* common private key attribute */
1438 * Allocate storage to hold the attribute
1439 * value with byte array type, and add it to
1440 * the extra attribute list of the object.
1442 rv
= kernel_add_extra_attr(&template[i
],
1450 * The following key related attribute types must
1451 * not be specified by C_CreateObject.
1454 case CKA_KEY_GEN_MECHANISM
:
1455 case CKA_AUTH_PIN_FLAGS
:
1456 case CKA_ALWAYS_SENSITIVE
:
1457 case CKA_NEVER_EXTRACTABLE
:
1458 rv
= CKR_TEMPLATE_INCONSISTENT
;
1461 /* Key related boolean attributes */
1463 if (*(CK_BBOOL
*)template[i
].pValue
)
1464 attr_mask
|= DERIVE_BOOL_ON
;
1468 if (*(CK_BBOOL
*)template[i
].pValue
)
1469 attr_mask
|= SENSITIVE_BOOL_ON
;
1472 case CKA_SECONDARY_AUTH
:
1473 if (*(CK_BBOOL
*)template[i
].pValue
) {
1474 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1480 if (*(CK_BBOOL
*)template[i
].pValue
)
1481 attr_mask
|= DECRYPT_BOOL_ON
;
1483 attr_mask
&= ~DECRYPT_BOOL_ON
;
1487 if (*(CK_BBOOL
*)template[i
].pValue
)
1488 attr_mask
|= SIGN_BOOL_ON
;
1490 attr_mask
&= ~SIGN_BOOL_ON
;
1493 case CKA_SIGN_RECOVER
:
1494 if (*(CK_BBOOL
*)template[i
].pValue
)
1495 attr_mask
|= SIGN_RECOVER_BOOL_ON
;
1497 attr_mask
&= ~SIGN_RECOVER_BOOL_ON
;
1501 if (*(CK_BBOOL
*)template[i
].pValue
)
1502 attr_mask
|= UNWRAP_BOOL_ON
;
1505 case CKA_EXTRACTABLE
:
1506 if (*(CK_BBOOL
*)template[i
].pValue
)
1507 attr_mask
|= EXTRACTABLE_BOOL_ON
;
1509 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
1513 * The following key related attribute types must
1514 * be specified according to the key type by
1520 * Copyin big integer attribute from template
1521 * to a local variable.
1523 rv
= get_bigint_attr_from_template(&modulus
,
1529 case CKA_PUBLIC_EXPONENT
:
1531 rv
= get_bigint_attr_from_template(&pubexpo
,
1537 case CKA_PRIVATE_EXPONENT
:
1539 rv
= get_bigint_attr_from_template(&priexpo
,
1547 rv
= get_bigint_attr_from_template(&prime1
,
1555 rv
= get_bigint_attr_from_template(&prime2
,
1561 case CKA_EXPONENT_1
:
1563 rv
= get_bigint_attr_from_template(&expo1
,
1569 case CKA_EXPONENT_2
:
1571 rv
= get_bigint_attr_from_template(&expo2
,
1577 case CKA_COEFFICIENT
:
1579 rv
= get_bigint_attr_from_template(&coef
,
1587 rv
= get_bigint_attr_from_template(&prime
,
1595 rv
= get_bigint_attr_from_template(&subprime
,
1603 rv
= get_bigint_attr_from_template(&base
,
1611 rv
= get_bigint_attr_from_template(&value
,
1617 case CKA_VALUE_BITS
:
1619 get_ulong_attr_from_template(&value_bits
,
1625 rv
= get_string_from_template(&string_tmp
,
1633 rv
= get_string_from_template(¶m_tmp
,
1640 rv
= kernel_parse_common_attrs(&template[i
], sp
,
1649 /* Allocate storage for Private Key Object. */
1650 pvk
= calloc(1, sizeof (private_key_obj_t
));
1652 rv
= CKR_HOST_MEMORY
;
1656 new_object
->object_class_u
.private_key
= pvk
;
1657 new_object
->class = CKO_PRIVATE_KEY
;
1659 if (keytype
== (CK_KEY_TYPE
)~0UL) {
1660 rv
= CKR_TEMPLATE_INCOMPLETE
;
1664 new_object
->key_type
= keytype
;
1666 /* Supported key types of the Private Key Object */
1669 if (isPrime
|| isSubprime
|| isBase
|| isValue
||
1671 rv
= CKR_TEMPLATE_INCONSISTENT
;
1675 if (isModulus
&& isPriExpo
) {
1677 * Copy big integer attribute value to the
1678 * designated place in the Private Key object.
1680 copy_bigint_attr(&modulus
, KEY_PRI_RSA_MOD(pvk
));
1682 copy_bigint_attr(&priexpo
, KEY_PRI_RSA_PRIEXPO(pvk
));
1685 rv
= CKR_TEMPLATE_INCOMPLETE
;
1689 /* The following attributes are optional. */
1691 copy_bigint_attr(&pubexpo
, KEY_PRI_RSA_PUBEXPO(pvk
));
1695 copy_bigint_attr(&prime1
, KEY_PRI_RSA_PRIME1(pvk
));
1699 copy_bigint_attr(&prime2
, KEY_PRI_RSA_PRIME2(pvk
));
1703 copy_bigint_attr(&expo1
, KEY_PRI_RSA_EXPO1(pvk
));
1707 copy_bigint_attr(&expo2
, KEY_PRI_RSA_EXPO2(pvk
));
1711 copy_bigint_attr(&coef
, KEY_PRI_RSA_COEF(pvk
));
1716 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
1717 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
1719 rv
= CKR_TEMPLATE_INCONSISTENT
;
1723 if (!(isPrime
&& isSubprime
&& isBase
&& isValue
)) {
1724 rv
= CKR_TEMPLATE_INCOMPLETE
;
1728 copy_bigint_attr(&prime
, KEY_PRI_DSA_PRIME(pvk
));
1730 copy_bigint_attr(&subprime
, KEY_PRI_DSA_SUBPRIME(pvk
));
1732 copy_bigint_attr(&base
, KEY_PRI_DSA_BASE(pvk
));
1734 copy_bigint_attr(&value
, KEY_PRI_DSA_VALUE(pvk
));
1739 if (mode
== KERNEL_CREATE_OBJ
&& isValueBits
) {
1740 rv
= CKR_TEMPLATE_INCONSISTENT
;
1743 if (!(isPrime
&& isBase
&& isValue
)) {
1744 rv
= CKR_TEMPLATE_INCOMPLETE
;
1748 copy_bigint_attr(&prime
, KEY_PRI_DH_PRIME(pvk
));
1750 copy_bigint_attr(&base
, KEY_PRI_DH_BASE(pvk
));
1752 copy_bigint_attr(&value
, KEY_PRI_DH_VALUE(pvk
));
1754 KEY_PRI_DH_VAL_BITS(pvk
) = (isValueBits
) ? value_bits
: 0;
1759 if (!isValue
|| !isParams
) {
1760 rv
= CKR_TEMPLATE_INCOMPLETE
;
1764 copy_bigint_attr(&value
, KEY_PRI_EC_VALUE(pvk
));
1765 rv
= kernel_add_extra_attr(¶m_tmp
, new_object
);
1768 string_attr_cleanup(¶m_tmp
);
1771 rv
= CKR_TEMPLATE_INCONSISTENT
;
1775 /* Set up object. */
1776 new_object
->bool_attr_mask
= attr_mask
;
1778 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
1781 string_attr_cleanup(&string_tmp
);
1788 * cleanup the storage allocated to the local variables.
1790 bigint_attr_cleanup(&modulus
);
1791 bigint_attr_cleanup(&priexpo
);
1792 bigint_attr_cleanup(&prime
);
1793 bigint_attr_cleanup(&subprime
);
1794 bigint_attr_cleanup(&base
);
1795 bigint_attr_cleanup(&value
);
1796 bigint_attr_cleanup(&pubexpo
);
1797 bigint_attr_cleanup(&prime1
);
1798 bigint_attr_cleanup(&prime2
);
1799 bigint_attr_cleanup(&expo1
);
1800 bigint_attr_cleanup(&expo2
);
1801 bigint_attr_cleanup(&coef
);
1802 string_attr_cleanup(&string_tmp
);
1803 string_attr_cleanup(¶m_tmp
);
1806 * cleanup the storage allocated inside the object itself.
1808 kernel_cleanup_object(new_object
);
1815 * Build a Secret Key Object.
1817 * - Parse the object's template, and when an error is detected such as
1818 * invalid attribute type, invalid attribute value, etc., return
1819 * with appropriate return value.
1820 * - Set up attribute mask field in the object for the supplied common
1821 * attributes that have boolean type.
1822 * - Build the attribute_info struct to hold the value of each supplied
1823 * attribute that has byte array type. Link attribute_info structs
1824 * together to form the extra attribute list of the object.
1825 * - Allocate storage for the Secret Key object.
1826 * - Build the Secret Key object. Allocate storage to hold the big integer
1827 * value for the attribute CKA_VALUE that is required for all the key
1828 * types supported by secret key object.
1832 kernel_build_secret_key_object(CK_ATTRIBUTE_PTR
template,
1833 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
)
1837 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1838 uint64_t attr_mask
= SECRET_KEY_DEFAULT
;
1841 /* Must set flags */
1843 /* Must not set flags */
1846 CK_ATTRIBUTE string_tmp
;
1848 secret_key_obj_t
*sck
;
1850 string_tmp
.pValue
= NULL
;
1852 /* Allocate storage for Secret Key Object. */
1853 sck
= calloc(1, sizeof (secret_key_obj_t
));
1855 rv
= CKR_HOST_MEMORY
;
1859 new_object
->object_class_u
.secret_key
= sck
;
1860 new_object
->class = CKO_SECRET_KEY
;
1862 for (i
= 0; i
< ulAttrNum
; i
++) {
1864 /* Secret Key Object Attributes */
1865 switch (template[i
].type
) {
1867 /* common key attributes */
1869 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1873 case CKA_START_DATE
:
1876 * Allocate storage to hold the attribute
1877 * value with byte array type, and add it to
1878 * the extra attribute list of the object.
1880 rv
= kernel_add_extra_attr(&template[i
],
1888 * The following key related attribute types must
1889 * not be specified by C_CreateObject.
1892 case CKA_KEY_GEN_MECHANISM
:
1893 case CKA_ALWAYS_SENSITIVE
:
1894 case CKA_NEVER_EXTRACTABLE
:
1895 rv
= CKR_TEMPLATE_INCONSISTENT
;
1898 /* Key related boolean attributes */
1900 if (*(CK_BBOOL
*)template[i
].pValue
)
1901 attr_mask
|= DERIVE_BOOL_ON
;
1905 if (*(CK_BBOOL
*)template[i
].pValue
)
1906 attr_mask
|= SENSITIVE_BOOL_ON
;
1910 if (*(CK_BBOOL
*)template[i
].pValue
)
1911 attr_mask
|= ENCRYPT_BOOL_ON
;
1913 attr_mask
&= ~ENCRYPT_BOOL_ON
;
1917 if (*(CK_BBOOL
*)template[i
].pValue
)
1918 attr_mask
|= DECRYPT_BOOL_ON
;
1920 attr_mask
&= ~DECRYPT_BOOL_ON
;
1924 if (*(CK_BBOOL
*)template[i
].pValue
)
1925 attr_mask
|= SIGN_BOOL_ON
;
1927 attr_mask
&= ~SIGN_BOOL_ON
;
1931 if (*(CK_BBOOL
*)template[i
].pValue
)
1932 attr_mask
|= VERIFY_BOOL_ON
;
1934 attr_mask
&= ~VERIFY_BOOL_ON
;
1938 if (*(CK_BBOOL
*)template[i
].pValue
)
1939 attr_mask
|= WRAP_BOOL_ON
;
1943 if (*(CK_BBOOL
*)template[i
].pValue
)
1944 attr_mask
|= UNWRAP_BOOL_ON
;
1947 case CKA_EXTRACTABLE
:
1948 if (*(CK_BBOOL
*)template[i
].pValue
)
1949 attr_mask
|= EXTRACTABLE_BOOL_ON
;
1951 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
1956 if ((template[i
].ulValueLen
== 0) ||
1957 (template[i
].pValue
== NULL
)) {
1958 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1963 * Copyin attribute from template
1964 * to a local variable.
1966 sck
->sk_value
= malloc(template[i
].ulValueLen
);
1967 if (sck
->sk_value
== NULL
) {
1968 rv
= CKR_HOST_MEMORY
;
1971 (void) memcpy(sck
->sk_value
, template[i
].pValue
,
1972 template[i
].ulValueLen
);
1973 sck
->sk_value_len
= template[i
].ulValueLen
;
1982 rv
= get_string_from_template(&string_tmp
,
1989 rv
= kernel_parse_common_attrs(&template[i
], sp
,
1998 if (keytype
== (CK_KEY_TYPE
)~0UL) {
1999 rv
= CKR_TEMPLATE_INCOMPLETE
;
2003 new_object
->key_type
= keytype
;
2005 /* Supported key types of the Secret Key Object */
2009 rv
= CKR_TEMPLATE_INCOMPLETE
;
2012 if ((sck
->sk_value_len
< ARCFOUR_MIN_KEY_BYTES
) ||
2013 (sck
->sk_value_len
> ARCFOUR_MAX_KEY_BYTES
)) {
2014 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2019 case CKK_GENERIC_SECRET
:
2021 rv
= CKR_TEMPLATE_INCOMPLETE
;
2028 rv
= CKR_TEMPLATE_INCOMPLETE
;
2031 if (sck
->sk_value_len
< AES_MIN_KEY_BYTES
) {
2032 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2039 rv
= CKR_TEMPLATE_INCOMPLETE
;
2042 if (sck
->sk_value_len
< BLOWFISH_MINBYTES
) {
2043 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2050 rv
= CKR_TEMPLATE_INCOMPLETE
;
2053 if (sck
->sk_value_len
!= DES_KEYSIZE
) {
2054 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2061 rv
= CKR_TEMPLATE_INCOMPLETE
;
2064 if (sck
->sk_value_len
!= DES2_KEYSIZE
) {
2065 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2072 rv
= CKR_TEMPLATE_INCOMPLETE
;
2075 if (sck
->sk_value_len
!= DES3_KEYSIZE
) {
2076 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2082 rv
= CKR_TEMPLATE_INCONSISTENT
;
2087 rv
= CKR_TEMPLATE_INCONSISTENT
;
2091 /* Set up object. */
2092 new_object
->bool_attr_mask
= attr_mask
;
2094 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
2097 string_attr_cleanup(&string_tmp
);
2104 * cleanup the storage allocated to the local variables.
2106 string_attr_cleanup(&string_tmp
);
2109 * cleanup the storage allocated inside the object itself.
2111 kernel_cleanup_object(new_object
);
2118 * Validate the attribute types in the object's template. Then,
2119 * call the appropriate build function according to the class of
2120 * the object specified in the template.
2122 * Note: The following classes of objects are supported:
2128 kernel_build_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
2129 kernel_object_t
*new_object
, kernel_session_t
*sp
, uint_t mode
)
2132 CK_OBJECT_CLASS
class = (CK_OBJECT_CLASS
)~0UL;
2135 if (template == NULL
) {
2136 return (CKR_ARGUMENTS_BAD
);
2139 /* Validate the attribute type in the template. */
2140 rv
= kernel_validate_attr(template, ulAttrNum
, &class);
2144 if (class == (CK_OBJECT_CLASS
)~0UL)
2145 return (CKR_TEMPLATE_INCOMPLETE
);
2148 * Call the appropriate function based on the supported class
2152 case CKO_PUBLIC_KEY
:
2153 rv
= kernel_build_public_key_object(template, ulAttrNum
,
2154 new_object
, sp
, mode
);
2157 case CKO_PRIVATE_KEY
:
2158 rv
= kernel_build_private_key_object(template, ulAttrNum
,
2159 new_object
, sp
, mode
);
2162 case CKO_SECRET_KEY
:
2163 rv
= kernel_build_secret_key_object(template, ulAttrNum
,
2167 case CKO_DOMAIN_PARAMETERS
:
2169 case CKO_CERTIFICATE
:
2170 case CKO_HW_FEATURE
:
2171 case CKO_VENDOR_DEFINED
:
2173 return (CKR_ATTRIBUTE_VALUE_INVALID
);
2181 * Get the value of a requested attribute that is common to all supported
2182 * classes (i.e. public key, private key, secret key classes).
2185 kernel_get_common_attrs(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
2190 switch (template->type
) {
2193 return (get_ulong_attr_from_object(object_p
->class,
2196 /* default boolean attributes */
2199 template->ulValueLen
= sizeof (CK_BBOOL
);
2200 if (template->pValue
== NULL
) {
2205 * A token object will not be created in the library, so we
2208 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2213 template->ulValueLen
= sizeof (CK_BBOOL
);
2214 if (template->pValue
== NULL
) {
2217 if (object_p
->bool_attr_mask
& PRIVATE_BOOL_ON
) {
2218 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
2220 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2224 case CKA_MODIFIABLE
:
2225 template->ulValueLen
= sizeof (CK_BBOOL
);
2226 if (template->pValue
== NULL
) {
2229 if ((object_p
->bool_attr_mask
) & MODIFIABLE_BOOL_ON
)
2230 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
2232 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2236 return (get_extra_attr_from_object(object_p
,
2241 * The specified attribute for the object is invalid.
2242 * (the object does not possess such an attribute.)
2244 template->ulValueLen
= (CK_ULONG
)-1;
2245 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2252 * Get the value of a requested attribute that is common to all key objects
2253 * (i.e. public key, private key and secret key).
2256 kernel_get_common_key_attrs(kernel_object_t
*object_p
,
2257 CK_ATTRIBUTE_PTR
template)
2260 switch (template->type
) {
2263 return (get_ulong_attr_from_object(object_p
->key_type
,
2267 case CKA_START_DATE
:
2270 * The above extra attributes have byte array type.
2272 return (get_extra_attr_from_object(object_p
,
2275 /* Key related boolean attributes */
2277 return (get_bool_attr_from_object(object_p
,
2278 LOCAL_BOOL_ON
, template));
2281 return (get_bool_attr_from_object(object_p
,
2282 DERIVE_BOOL_ON
, template));
2284 case CKA_KEY_GEN_MECHANISM
:
2285 return (get_ulong_attr_from_object(object_p
->mechanism
,
2289 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2295 * Get the value of a requested attribute of a Public Key Object.
2297 * Rule: All the attributes in the public key object can be revealed.
2300 kernel_get_public_key_attribute(kernel_object_t
*object_p
,
2301 CK_ATTRIBUTE_PTR
template)
2305 CK_KEY_TYPE keytype
= object_p
->key_type
;
2307 switch (template->type
) {
2312 * The above extra attributes have byte array type.
2314 return (get_extra_attr_from_object(object_p
,
2317 /* Key related boolean attributes */
2319 return (get_bool_attr_from_object(object_p
,
2320 ENCRYPT_BOOL_ON
, template));
2323 return (get_bool_attr_from_object(object_p
,
2324 VERIFY_BOOL_ON
, template));
2326 case CKA_VERIFY_RECOVER
:
2327 return (get_bool_attr_from_object(object_p
,
2328 VERIFY_RECOVER_BOOL_ON
, template));
2331 return (get_bool_attr_from_object(object_p
,
2332 WRAP_BOOL_ON
, template));
2335 return (get_bool_attr_from_object(object_p
,
2336 TRUSTED_BOOL_ON
, template));
2340 * This attribute is valid only for RSA public key
2343 if (keytype
== CKK_RSA
) {
2344 return (get_bigint_attr_from_object(
2345 OBJ_PUB_RSA_MOD(object_p
), template));
2347 template->ulValueLen
= (CK_ULONG
)-1;
2348 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2351 case CKA_PUBLIC_EXPONENT
:
2352 if (keytype
== CKK_RSA
) {
2353 return (get_bigint_attr_from_object(
2354 OBJ_PUB_RSA_PUBEXPO(object_p
), template));
2356 template->ulValueLen
= (CK_ULONG
)-1;
2357 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2360 case CKA_MODULUS_BITS
:
2361 if (keytype
== CKK_RSA
) {
2362 return (get_ulong_attr_from_object(
2363 OBJ_PUB_RSA_MOD_BITS(object_p
), template));
2365 template->ulValueLen
= (CK_ULONG
)-1;
2366 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2372 return (get_bigint_attr_from_object(
2373 OBJ_PUB_DSA_PRIME(object_p
), template));
2375 return (get_bigint_attr_from_object(
2376 OBJ_PUB_DH_PRIME(object_p
), template));
2378 template->ulValueLen
= (CK_ULONG
)-1;
2379 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2385 return (get_bigint_attr_from_object(
2386 OBJ_PUB_DSA_SUBPRIME(object_p
), template));
2388 template->ulValueLen
= (CK_ULONG
)-1;
2389 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2395 return (get_bigint_attr_from_object(
2396 OBJ_PUB_DSA_BASE(object_p
), template));
2398 return (get_bigint_attr_from_object(
2399 OBJ_PUB_DH_BASE(object_p
), template));
2401 template->ulValueLen
= (CK_ULONG
)-1;
2402 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2408 return (get_bigint_attr_from_object(
2409 OBJ_PUB_DSA_VALUE(object_p
), template));
2411 return (get_bigint_attr_from_object(
2412 OBJ_PUB_DH_VALUE(object_p
), template));
2414 template->ulValueLen
= (CK_ULONG
)-1;
2415 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2421 return (get_bigint_attr_from_object(
2422 OBJ_PUB_EC_POINT(object_p
), template));
2424 template->ulValueLen
= (CK_ULONG
)-1;
2425 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2429 * First, get the value of the request attribute defined
2430 * in the list of common key attributes. If the request
2431 * attribute is not found in that list, then get the
2432 * attribute from the list of common attributes.
2434 rv
= kernel_get_common_key_attrs(object_p
, template);
2435 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2436 rv
= kernel_get_common_attrs(object_p
, template);
2446 * Get the value of a requested attribute of a Private Key Object.
2448 * Rule: All the attributes in the private key object can be revealed
2449 * except those marked with footnote number "7" when the object
2450 * has its CKA_SENSITIVE attribute set to TRUE or its
2451 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
2454 kernel_get_private_key_attribute(kernel_object_t
*object_p
,
2455 CK_ATTRIBUTE_PTR
template)
2459 CK_KEY_TYPE keytype
= object_p
->key_type
;
2463 * If the following specified attributes for the private key
2464 * object cannot be revealed because the object is sensitive
2465 * or unextractable, then the ulValueLen is set to -1.
2467 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
2468 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
2470 switch (template->type
) {
2471 case CKA_PRIVATE_EXPONENT
:
2474 case CKA_EXPONENT_1
:
2475 case CKA_EXPONENT_2
:
2476 case CKA_COEFFICIENT
:
2478 template->ulValueLen
= (CK_ULONG
)-1;
2479 return (CKR_ATTRIBUTE_SENSITIVE
);
2483 switch (template->type
) {
2488 * The above extra attributes have byte array type.
2490 return (get_extra_attr_from_object(object_p
,
2493 /* Key related boolean attributes */
2495 return (get_bool_attr_from_object(object_p
,
2496 SENSITIVE_BOOL_ON
, template));
2498 case CKA_SECONDARY_AUTH
:
2499 return (get_bool_attr_from_object(object_p
,
2500 SECONDARY_AUTH_BOOL_ON
, template));
2503 return (get_bool_attr_from_object(object_p
,
2504 DECRYPT_BOOL_ON
, template));
2507 return (get_bool_attr_from_object(object_p
,
2508 SIGN_BOOL_ON
, template));
2510 case CKA_SIGN_RECOVER
:
2511 return (get_bool_attr_from_object(object_p
,
2512 SIGN_RECOVER_BOOL_ON
, template));
2515 return (get_bool_attr_from_object(object_p
,
2516 UNWRAP_BOOL_ON
, template));
2518 case CKA_EXTRACTABLE
:
2519 return (get_bool_attr_from_object(object_p
,
2520 EXTRACTABLE_BOOL_ON
, template));
2522 case CKA_ALWAYS_SENSITIVE
:
2523 return (get_bool_attr_from_object(object_p
,
2524 ALWAYS_SENSITIVE_BOOL_ON
, template));
2526 case CKA_NEVER_EXTRACTABLE
:
2527 return (get_bool_attr_from_object(object_p
,
2528 NEVER_EXTRACTABLE_BOOL_ON
, template));
2531 if (keytype
== CKK_RSA
) {
2532 return (get_bigint_attr_from_object(
2533 OBJ_PRI_RSA_MOD(object_p
), template));
2535 template->ulValueLen
= (CK_ULONG
)-1;
2536 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2540 case CKA_PUBLIC_EXPONENT
:
2541 if (keytype
== CKK_RSA
) {
2542 return (get_bigint_attr_from_object(
2543 OBJ_PRI_RSA_PUBEXPO(object_p
), template));
2545 template->ulValueLen
= (CK_ULONG
)-1;
2546 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2550 case CKA_PRIVATE_EXPONENT
:
2551 if (keytype
== CKK_RSA
) {
2552 return (get_bigint_attr_from_object(
2553 OBJ_PRI_RSA_PRIEXPO(object_p
), template));
2555 template->ulValueLen
= (CK_ULONG
)-1;
2556 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2561 if (keytype
== CKK_RSA
) {
2562 return (get_bigint_attr_from_object(
2563 OBJ_PRI_RSA_PRIME1(object_p
), template));
2565 template->ulValueLen
= (CK_ULONG
)-1;
2566 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2571 if (keytype
== CKK_RSA
) {
2572 return (get_bigint_attr_from_object(
2573 OBJ_PRI_RSA_PRIME2(object_p
), template));
2575 template->ulValueLen
= (CK_ULONG
)-1;
2576 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2580 case CKA_EXPONENT_1
:
2581 if (keytype
== CKK_RSA
) {
2582 return (get_bigint_attr_from_object(
2583 OBJ_PRI_RSA_EXPO1(object_p
), template));
2585 template->ulValueLen
= (CK_ULONG
)-1;
2586 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2590 case CKA_EXPONENT_2
:
2591 if (keytype
== CKK_RSA
) {
2592 return (get_bigint_attr_from_object(
2593 OBJ_PRI_RSA_EXPO2(object_p
), template));
2595 template->ulValueLen
= (CK_ULONG
)-1;
2596 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2600 case CKA_COEFFICIENT
:
2601 if (keytype
== CKK_RSA
) {
2602 return (get_bigint_attr_from_object(
2603 OBJ_PRI_RSA_COEF(object_p
), template));
2605 template->ulValueLen
= (CK_ULONG
)-1;
2606 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2610 case CKA_VALUE_BITS
:
2611 if (keytype
== CKK_DH
) {
2612 return (get_ulong_attr_from_object(
2613 OBJ_PRI_DH_VAL_BITS(object_p
), template));
2615 template->ulValueLen
= (CK_ULONG
)-1;
2616 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2623 return (get_bigint_attr_from_object(
2624 OBJ_PRI_DSA_PRIME(object_p
), template));
2626 return (get_bigint_attr_from_object(
2627 OBJ_PRI_DH_PRIME(object_p
), template));
2629 template->ulValueLen
= (CK_ULONG
)-1;
2630 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2636 return (get_bigint_attr_from_object(
2637 OBJ_PRI_DSA_SUBPRIME(object_p
), template));
2639 template->ulValueLen
= (CK_ULONG
)-1;
2640 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2646 return (get_bigint_attr_from_object(
2647 OBJ_PRI_DSA_BASE(object_p
), template));
2649 return (get_bigint_attr_from_object(
2650 OBJ_PRI_DH_BASE(object_p
), template));
2652 template->ulValueLen
= (CK_ULONG
)-1;
2653 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2659 return (get_bigint_attr_from_object(
2660 OBJ_PRI_DSA_VALUE(object_p
), template));
2662 return (get_bigint_attr_from_object(
2663 OBJ_PRI_DH_VALUE(object_p
), template));
2665 return (get_bigint_attr_from_object(
2666 OBJ_PRI_EC_VALUE(object_p
), template));
2668 template->ulValueLen
= (CK_ULONG
)-1;
2669 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2674 * First, get the value of the request attribute defined
2675 * in the list of common key attributes. If the request
2676 * attribute is not found in that list, then get the
2677 * attribute from the list of common attributes.
2679 rv
= kernel_get_common_key_attrs(object_p
, template);
2680 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2681 rv
= kernel_get_common_attrs(object_p
, template);
2691 * Get the value of a requested attribute of a Secret Key Object.
2693 * Rule: All the attributes in the secret key object can be revealed
2694 * except those marked with footnote number "7" when the object
2695 * has its CKA_SENSITIVE attribute set to TRUE or its
2696 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
2699 kernel_get_secret_key_attribute(kernel_object_t
*object_p
,
2700 CK_ATTRIBUTE_PTR
template)
2704 CK_KEY_TYPE keytype
= object_p
->key_type
;
2706 switch (template->type
) {
2708 /* Key related boolean attributes */
2710 return (get_bool_attr_from_object(object_p
,
2711 SENSITIVE_BOOL_ON
, template));
2714 return (get_bool_attr_from_object(object_p
,
2715 ENCRYPT_BOOL_ON
, template));
2718 return (get_bool_attr_from_object(object_p
,
2719 DECRYPT_BOOL_ON
, template));
2722 return (get_bool_attr_from_object(object_p
,
2723 SIGN_BOOL_ON
, template));
2726 return (get_bool_attr_from_object(object_p
,
2727 VERIFY_BOOL_ON
, template));
2730 return (get_bool_attr_from_object(object_p
,
2731 WRAP_BOOL_ON
, template));
2734 return (get_bool_attr_from_object(object_p
,
2735 UNWRAP_BOOL_ON
, template));
2737 case CKA_EXTRACTABLE
:
2738 return (get_bool_attr_from_object(object_p
,
2739 EXTRACTABLE_BOOL_ON
, template));
2741 case CKA_ALWAYS_SENSITIVE
:
2742 return (get_bool_attr_from_object(object_p
,
2743 ALWAYS_SENSITIVE_BOOL_ON
, template));
2745 case CKA_NEVER_EXTRACTABLE
:
2746 return (get_bool_attr_from_object(object_p
,
2747 NEVER_EXTRACTABLE_BOOL_ON
, template));
2751 * If the specified attribute for the secret key object
2752 * cannot be revealed because the object is sensitive
2753 * or unextractable, then the ulValueLen is set to -1.
2755 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
2756 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
2757 template->ulValueLen
= (CK_ULONG
)-1;
2758 return (CKR_ATTRIBUTE_SENSITIVE
);
2763 case CKK_GENERIC_SECRET
:
2772 * Copy secret key object attributes to template.
2774 if (template->pValue
== NULL
) {
2775 template->ulValueLen
=
2776 OBJ_SEC_VALUE_LEN(object_p
);
2780 if (OBJ_SEC_VALUE(object_p
) == NULL
) {
2781 template->ulValueLen
= 0;
2785 if (template->ulValueLen
>=
2786 OBJ_SEC_VALUE_LEN(object_p
)) {
2787 (void) memcpy(template->pValue
,
2788 OBJ_SEC_VALUE(object_p
),
2789 OBJ_SEC_VALUE_LEN(object_p
));
2790 template->ulValueLen
=
2791 OBJ_SEC_VALUE_LEN(object_p
);
2794 template->ulValueLen
= (CK_ULONG
)-1;
2795 return (CKR_BUFFER_TOO_SMALL
);
2799 template->ulValueLen
= (CK_ULONG
)-1;
2800 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2806 return (get_ulong_attr_from_object(OBJ_SEC_VALUE_LEN(object_p
),
2811 * First, get the value of the request attribute defined
2812 * in the list of common key attributes. If the request
2813 * attribute is not found in that list, then get the
2814 * attribute from the list of common attributes.
2816 rv
= kernel_get_common_key_attrs(object_p
, template);
2817 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2818 rv
= kernel_get_common_attrs(object_p
, template);
2831 * Call the appropriate get attribute function according to the class
2834 * The caller of this function holds the lock on the object.
2837 kernel_get_attribute(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
2841 CK_OBJECT_CLASS
class = object_p
->class;
2845 case CKO_PUBLIC_KEY
:
2846 rv
= kernel_get_public_key_attribute(object_p
, template);
2849 case CKO_PRIVATE_KEY
:
2850 rv
= kernel_get_private_key_attribute(object_p
, template);
2853 case CKO_SECRET_KEY
:
2854 rv
= kernel_get_secret_key_attribute(object_p
, template);
2859 * If the specified attribute for the object is invalid
2860 * (the object does not possess such as attribute), then
2861 * the ulValueLen is modified to hold the value -1.
2863 template->ulValueLen
= (CK_ULONG
)-1;
2864 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2872 * Set the value of an attribute that is common to all key objects
2873 * (i.e. public key, private key and secret key).
2876 kernel_set_common_key_attribute(kernel_object_t
*object_p
,
2877 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
2880 kernel_slot_t
*pslot
= slot_table
[sp
->ses_slotid
];
2883 switch (template->type
) {
2887 * Only the LABEL can be modified in the common storage
2888 * object attributes after the object is created.
2890 return (set_extra_attr_to_object(object_p
,
2891 CKA_LABEL
, template));
2894 return (set_extra_attr_to_object(object_p
,
2897 case CKA_START_DATE
:
2898 return (set_extra_attr_to_object(object_p
,
2899 CKA_START_DATE
, template));
2902 return (set_extra_attr_to_object(object_p
,
2903 CKA_END_DATE
, template));
2906 return (set_bool_attr_to_object(object_p
,
2907 DERIVE_BOOL_ON
, template));
2912 return (CKR_ATTRIBUTE_READ_ONLY
);
2916 /* called from C_SetAttributeValue() */
2917 return (CKR_ATTRIBUTE_READ_ONLY
);
2920 /* called from C_CopyObject() */
2921 if ((*(CK_BBOOL
*)template->pValue
) != B_TRUE
) {
2925 (void) pthread_mutex_lock(&pslot
->sl_mutex
);
2927 * Cannot create a private object if the token
2928 * has a keystore and the user isn't logged in.
2930 if (pslot
->sl_func_list
.fl_object_create
&&
2931 pslot
->sl_state
!= CKU_USER
) {
2932 rv
= CKR_USER_NOT_LOGGED_IN
;
2934 rv
= set_bool_attr_to_object(object_p
,
2935 PRIVATE_BOOL_ON
, template);
2937 (void) pthread_mutex_unlock(&pslot
->sl_mutex
);
2940 case CKA_MODIFIABLE
:
2942 rv
= set_bool_attr_to_object(object_p
,
2943 MODIFIABLE_BOOL_ON
, template);
2945 rv
= CKR_ATTRIBUTE_READ_ONLY
;
2950 return (CKR_TEMPLATE_INCONSISTENT
);
2957 * Set the value of an attribute of a Public Key Object.
2959 * Rule: The attributes marked with footnote number "8" in the PKCS11
2960 * spec may be modified (p.88 in PKCS11 spec.).
2963 kernel_set_public_key_attribute(kernel_object_t
*object_p
,
2964 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
2966 CK_KEY_TYPE keytype
= object_p
->key_type
;
2968 switch (template->type
) {
2971 return (set_extra_attr_to_object(object_p
,
2972 CKA_SUBJECT
, template));
2975 return (set_bool_attr_to_object(object_p
,
2976 ENCRYPT_BOOL_ON
, template));
2979 return (set_bool_attr_to_object(object_p
,
2980 VERIFY_BOOL_ON
, template));
2982 case CKA_VERIFY_RECOVER
:
2983 return (set_bool_attr_to_object(object_p
,
2984 VERIFY_RECOVER_BOOL_ON
, template));
2987 return (set_bool_attr_to_object(object_p
,
2988 WRAP_BOOL_ON
, template));
2991 case CKA_MODULUS_BITS
:
2992 case CKA_PUBLIC_EXPONENT
:
2993 if (keytype
== CKK_RSA
)
2994 return (CKR_ATTRIBUTE_READ_ONLY
);
3001 if (keytype
== CKK_DSA
)
3002 return (CKR_ATTRIBUTE_READ_ONLY
);
3007 * Set the value of a common key attribute.
3009 return (kernel_set_common_key_attribute(object_p
,
3010 template, copy
, sp
));
3015 * If we got this far, then the combination of key type
3016 * and requested attribute is invalid.
3018 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3023 * Set the value of an attribute of a Private Key Object.
3025 * Rule: The attributes marked with footnote number "8" in the PKCS11
3026 * spec may be modified (p.88 in PKCS11 spec.).
3029 kernel_set_private_key_attribute(kernel_object_t
*object_p
,
3030 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
3032 CK_KEY_TYPE keytype
= object_p
->key_type
;
3034 switch (template->type
) {
3037 return (set_extra_attr_to_object(object_p
,
3038 CKA_SUBJECT
, template));
3042 * Cannot set SENSITIVE to FALSE if it is already ON.
3044 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
3045 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
3046 return (CKR_ATTRIBUTE_READ_ONLY
);
3049 if (*(CK_BBOOL
*)template->pValue
)
3050 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
3054 return (set_bool_attr_to_object(object_p
,
3055 DECRYPT_BOOL_ON
, template));
3058 return (set_bool_attr_to_object(object_p
,
3059 SIGN_BOOL_ON
, template));
3061 case CKA_SIGN_RECOVER
:
3062 return (set_bool_attr_to_object(object_p
,
3063 SIGN_RECOVER_BOOL_ON
, template));
3066 return (set_bool_attr_to_object(object_p
,
3067 UNWRAP_BOOL_ON
, template));
3069 case CKA_EXTRACTABLE
:
3071 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
3073 if ((*(CK_BBOOL
*)template->pValue
) &&
3074 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
3075 return (CKR_ATTRIBUTE_READ_ONLY
);
3078 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
3079 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
3083 case CKA_PUBLIC_EXPONENT
:
3084 case CKA_PRIVATE_EXPONENT
:
3087 case CKA_EXPONENT_1
:
3088 case CKA_EXPONENT_2
:
3089 case CKA_COEFFICIENT
:
3090 if (keytype
== CKK_RSA
) {
3091 return (CKR_ATTRIBUTE_READ_ONLY
);
3099 if (keytype
== CKK_DSA
)
3100 return (CKR_ATTRIBUTE_READ_ONLY
);
3105 * Set the value of a common key attribute.
3107 return (kernel_set_common_key_attribute(object_p
,
3108 template, copy
, sp
));
3112 * If we got this far, then the combination of key type
3113 * and requested attribute is invalid.
3115 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3121 * Set the value of an attribute of a Secret Key Object.
3123 * Rule: The attributes marked with footnote number "8" in the PKCS11
3124 * spec may be modified (p.88 in PKCS11 spec.).
3127 kernel_set_secret_key_attribute(kernel_object_t
*object_p
,
3128 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
3130 CK_KEY_TYPE keytype
= object_p
->key_type
;
3132 switch (template->type
) {
3136 * Cannot set SENSITIVE to FALSE if it is already ON.
3138 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
3139 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
3140 return (CKR_ATTRIBUTE_READ_ONLY
);
3143 if (*(CK_BBOOL
*)template->pValue
)
3144 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
3148 return (set_bool_attr_to_object(object_p
,
3149 ENCRYPT_BOOL_ON
, template));
3152 return (set_bool_attr_to_object(object_p
,
3153 DECRYPT_BOOL_ON
, template));
3156 return (set_bool_attr_to_object(object_p
,
3157 SIGN_BOOL_ON
, template));
3160 return (set_bool_attr_to_object(object_p
,
3161 VERIFY_BOOL_ON
, template));
3164 return (set_bool_attr_to_object(object_p
,
3165 WRAP_BOOL_ON
, template));
3168 return (set_bool_attr_to_object(object_p
,
3169 UNWRAP_BOOL_ON
, template));
3171 case CKA_EXTRACTABLE
:
3173 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
3175 if ((*(CK_BBOOL
*)template->pValue
) &&
3176 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
3177 return (CKR_ATTRIBUTE_READ_ONLY
);
3180 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
3181 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
3185 return (CKR_ATTRIBUTE_READ_ONLY
);
3188 if ((keytype
== CKK_RC4
) ||
3189 (keytype
== CKK_GENERIC_SECRET
) ||
3190 (keytype
== CKK_AES
) ||
3191 (keytype
== CKK_BLOWFISH
))
3192 return (CKR_ATTRIBUTE_READ_ONLY
);
3197 * Set the value of a common key attribute.
3199 return (kernel_set_common_key_attribute(object_p
,
3200 template, copy
, sp
));
3204 * If we got this far, then the combination of key type
3205 * and requested attribute is invalid.
3207 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3212 * Call the appropriate set attribute function according to the class
3215 * The caller of this function does not hold the lock on the original
3216 * object, since this function is setting the attribute on the new object
3217 * that is being modified.
3221 kernel_set_attribute(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template,
3222 boolean_t copy
, kernel_session_t
*sp
)
3226 CK_OBJECT_CLASS
class = object_p
->class;
3230 case CKO_PUBLIC_KEY
:
3231 rv
= kernel_set_public_key_attribute(object_p
, template,
3235 case CKO_PRIVATE_KEY
:
3236 rv
= kernel_set_private_key_attribute(object_p
, template,
3240 case CKO_SECRET_KEY
:
3241 rv
= kernel_set_secret_key_attribute(object_p
, template,
3247 * If the template specifies a value of an attribute
3248 * which is incompatible with other existing attributes
3249 * of the object, then fails with return code
3250 * CKR_TEMPLATE_INCONSISTENT.
3252 rv
= CKR_TEMPLATE_INCONSISTENT
;
3261 copy_bigint(biginteger_t
*new_bigint
, biginteger_t
*old_bigint
)
3263 new_bigint
->big_value
=
3264 malloc((sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
3266 if (new_bigint
->big_value
== NULL
) {
3267 return (CKR_HOST_MEMORY
);
3270 (void) memcpy(new_bigint
->big_value
, old_bigint
->big_value
,
3271 (sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
3277 free_public_key_attr(public_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
3285 bigint_attr_cleanup(KEY_PUB_RSA_MOD(pbk
));
3286 bigint_attr_cleanup(KEY_PUB_RSA_PUBEXPO(pbk
));
3289 bigint_attr_cleanup(KEY_PUB_DSA_PRIME(pbk
));
3290 bigint_attr_cleanup(KEY_PUB_DSA_SUBPRIME(pbk
));
3291 bigint_attr_cleanup(KEY_PUB_DSA_BASE(pbk
));
3292 bigint_attr_cleanup(KEY_PUB_DSA_VALUE(pbk
));
3302 kernel_copy_public_key_attr(public_key_obj_t
*old_pub_key_obj_p
,
3303 public_key_obj_t
**new_pub_key_obj_p
, CK_KEY_TYPE key_type
)
3306 public_key_obj_t
*pbk
;
3309 pbk
= calloc(1, sizeof (public_key_obj_t
));
3311 return (CKR_HOST_MEMORY
);
3316 (void) memcpy(KEY_PUB_RSA(pbk
),
3317 KEY_PUB_RSA(old_pub_key_obj_p
),
3318 sizeof (rsa_pub_key_t
));
3320 rv
= copy_bigint(KEY_PUB_RSA_MOD(pbk
),
3321 KEY_PUB_RSA_MOD(old_pub_key_obj_p
));
3323 free_public_key_attr(pbk
, key_type
);
3326 /* copy public exponent */
3327 rv
= copy_bigint(KEY_PUB_RSA_PUBEXPO(pbk
),
3328 KEY_PUB_RSA_PUBEXPO(old_pub_key_obj_p
));
3330 free_public_key_attr(pbk
, key_type
);
3335 (void) memcpy(KEY_PUB_DSA(pbk
),
3336 KEY_PUB_DSA(old_pub_key_obj_p
),
3337 sizeof (dsa_pub_key_t
));
3340 rv
= copy_bigint(KEY_PUB_DSA_PRIME(pbk
),
3341 KEY_PUB_DSA_PRIME(old_pub_key_obj_p
));
3343 free_public_key_attr(pbk
, key_type
);
3348 rv
= copy_bigint(KEY_PUB_DSA_SUBPRIME(pbk
),
3349 KEY_PUB_DSA_SUBPRIME(old_pub_key_obj_p
));
3351 free_public_key_attr(pbk
, key_type
);
3356 rv
= copy_bigint(KEY_PUB_DSA_BASE(pbk
),
3357 KEY_PUB_DSA_BASE(old_pub_key_obj_p
));
3359 free_public_key_attr(pbk
, key_type
);
3364 rv
= copy_bigint(KEY_PUB_DSA_VALUE(pbk
),
3365 KEY_PUB_DSA_VALUE(old_pub_key_obj_p
));
3367 free_public_key_attr(pbk
, key_type
);
3374 *new_pub_key_obj_p
= pbk
;
3380 free_private_key_attr(private_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
3388 bigint_attr_cleanup(KEY_PRI_RSA_MOD(pbk
));
3389 bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(pbk
));
3390 bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(pbk
));
3391 bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(pbk
));
3392 bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(pbk
));
3393 bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(pbk
));
3394 bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(pbk
));
3395 bigint_attr_cleanup(KEY_PRI_RSA_COEF(pbk
));
3398 bigint_attr_cleanup(KEY_PRI_DSA_PRIME(pbk
));
3399 bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(pbk
));
3400 bigint_attr_cleanup(KEY_PRI_DSA_BASE(pbk
));
3401 bigint_attr_cleanup(KEY_PRI_DSA_VALUE(pbk
));
3410 kernel_copy_private_key_attr(private_key_obj_t
*old_pri_key_obj_p
,
3411 private_key_obj_t
**new_pri_key_obj_p
, CK_KEY_TYPE key_type
)
3414 private_key_obj_t
*pbk
;
3416 pbk
= calloc(1, sizeof (private_key_obj_t
));
3418 return (CKR_HOST_MEMORY
);
3423 (void) memcpy(KEY_PRI_RSA(pbk
),
3424 KEY_PRI_RSA(old_pri_key_obj_p
),
3425 sizeof (rsa_pri_key_t
));
3427 rv
= copy_bigint(KEY_PRI_RSA_MOD(pbk
),
3428 KEY_PRI_RSA_MOD(old_pri_key_obj_p
));
3430 free_private_key_attr(pbk
, key_type
);
3433 /* copy public exponent */
3434 rv
= copy_bigint(KEY_PRI_RSA_PUBEXPO(pbk
),
3435 KEY_PRI_RSA_PUBEXPO(old_pri_key_obj_p
));
3437 free_private_key_attr(pbk
, key_type
);
3440 /* copy private exponent */
3441 rv
= copy_bigint(KEY_PRI_RSA_PRIEXPO(pbk
),
3442 KEY_PRI_RSA_PRIEXPO(old_pri_key_obj_p
));
3444 free_private_key_attr(pbk
, key_type
);
3448 rv
= copy_bigint(KEY_PRI_RSA_PRIME1(pbk
),
3449 KEY_PRI_RSA_PRIME1(old_pri_key_obj_p
));
3451 free_private_key_attr(pbk
, key_type
);
3455 rv
= copy_bigint(KEY_PRI_RSA_PRIME2(pbk
),
3456 KEY_PRI_RSA_PRIME2(old_pri_key_obj_p
));
3458 free_private_key_attr(pbk
, key_type
);
3461 /* copy exponent_1 */
3462 rv
= copy_bigint(KEY_PRI_RSA_EXPO1(pbk
),
3463 KEY_PRI_RSA_EXPO1(old_pri_key_obj_p
));
3465 free_private_key_attr(pbk
, key_type
);
3468 /* copy exponent_2 */
3469 rv
= copy_bigint(KEY_PRI_RSA_EXPO2(pbk
),
3470 KEY_PRI_RSA_EXPO2(old_pri_key_obj_p
));
3472 free_private_key_attr(pbk
, key_type
);
3475 /* copy coefficient */
3476 rv
= copy_bigint(KEY_PRI_RSA_COEF(pbk
),
3477 KEY_PRI_RSA_COEF(old_pri_key_obj_p
));
3479 free_private_key_attr(pbk
, key_type
);
3484 (void) memcpy(KEY_PRI_DSA(pbk
),
3485 KEY_PRI_DSA(old_pri_key_obj_p
),
3486 sizeof (dsa_pri_key_t
));
3489 rv
= copy_bigint(KEY_PRI_DSA_PRIME(pbk
),
3490 KEY_PRI_DSA_PRIME(old_pri_key_obj_p
));
3492 free_private_key_attr(pbk
, key_type
);
3497 rv
= copy_bigint(KEY_PRI_DSA_SUBPRIME(pbk
),
3498 KEY_PRI_DSA_SUBPRIME(old_pri_key_obj_p
));
3500 free_private_key_attr(pbk
, key_type
);
3505 rv
= copy_bigint(KEY_PRI_DSA_BASE(pbk
),
3506 KEY_PRI_DSA_BASE(old_pri_key_obj_p
));
3508 free_private_key_attr(pbk
, key_type
);
3513 rv
= copy_bigint(KEY_PRI_DSA_VALUE(pbk
),
3514 KEY_PRI_DSA_VALUE(old_pri_key_obj_p
));
3516 free_private_key_attr(pbk
, key_type
);
3523 *new_pri_key_obj_p
= pbk
;
3529 kernel_copy_secret_key_attr(secret_key_obj_t
*old_secret_key_obj_p
,
3530 secret_key_obj_t
**new_secret_key_obj_p
)
3532 secret_key_obj_t
*sk
;
3534 sk
= malloc(sizeof (secret_key_obj_t
));
3536 return (CKR_HOST_MEMORY
);
3538 (void) memcpy(sk
, old_secret_key_obj_p
, sizeof (secret_key_obj_t
));
3540 /* copy the secret key value */
3541 sk
->sk_value
= malloc((sizeof (CK_BYTE
) * sk
->sk_value_len
));
3542 if (sk
->sk_value
== NULL
) {
3544 return (CKR_HOST_MEMORY
);
3546 (void) memcpy(sk
->sk_value
, old_secret_key_obj_p
->sk_value
,
3547 (sizeof (CK_BYTE
) * sk
->sk_value_len
));
3549 *new_secret_key_obj_p
= sk
;
3557 * If CKA_CLASS not given, guess CKA_CLASS using
3558 * attributes on template .
3560 * Some attributes are specific to an object class. If one or more
3561 * of these attributes are in the template, make a list of classes
3562 * that can have these attributes. This would speed up the search later,
3563 * because we can immediately skip an object if the class of that
3564 * object can not possibly contain one of the attributes.
3568 kernel_process_find_attr(CK_OBJECT_CLASS
*pclasses
,
3569 CK_ULONG
*num_result_pclasses
, CK_ATTRIBUTE_PTR pTemplate
,
3574 boolean_t pub_found
= B_FALSE
,
3575 priv_found
= B_FALSE
,
3576 secret_found
= B_FALSE
,
3577 domain_found
= B_FALSE
,
3578 hardware_found
= B_FALSE
,
3579 cert_found
= B_FALSE
;
3580 int num_pub_key_attrs
, num_priv_key_attrs
,
3581 num_secret_key_attrs
, num_domain_attrs
,
3582 num_hardware_attrs
, num_cert_attrs
;
3583 int num_pclasses
= 0;
3585 for (i
= 0; i
< ulCount
; i
++) {
3586 if (pTemplate
[i
].type
== CKA_CLASS
) {
3588 * don't need to guess the class, it is specified.
3589 * Just record the class, and return.
3592 (*((CK_OBJECT_CLASS
*)pTemplate
[i
].pValue
));
3593 *num_result_pclasses
= 1;
3599 sizeof (PUB_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3600 num_priv_key_attrs
=
3601 sizeof (PRIV_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3602 num_secret_key_attrs
=
3603 sizeof (SECRET_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3605 sizeof (DOMAIN_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3606 num_hardware_attrs
=
3607 sizeof (HARDWARE_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3609 sizeof (CERT_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3612 * Get the list of objects class that might contain
3615 for (i
= 0; i
< ulCount
; i
++) {
3617 * only check if this attribute can belong to public key object
3618 * class if public key object isn't already in the list
3621 for (j
= 0; j
< num_pub_key_attrs
; j
++) {
3622 if (pTemplate
[i
].type
== PUB_KEY_ATTRS
[j
]) {
3624 pclasses
[num_pclasses
++] =
3632 for (j
= 0; j
< num_priv_key_attrs
; j
++) {
3633 if (pTemplate
[i
].type
== PRIV_KEY_ATTRS
[j
]) {
3634 priv_found
= B_TRUE
;
3635 pclasses
[num_pclasses
++] =
3642 if (!secret_found
) {
3643 for (j
= 0; j
< num_secret_key_attrs
; j
++) {
3644 if (pTemplate
[i
].type
== SECRET_KEY_ATTRS
[j
]) {
3645 secret_found
= B_TRUE
;
3646 pclasses
[num_pclasses
++] =
3653 if (!domain_found
) {
3654 for (j
= 0; j
< num_domain_attrs
; j
++) {
3655 if (pTemplate
[i
].type
== DOMAIN_ATTRS
[j
]) {
3656 domain_found
= B_TRUE
;
3657 pclasses
[num_pclasses
++] =
3658 CKO_DOMAIN_PARAMETERS
;
3664 if (!hardware_found
) {
3665 for (j
= 0; j
< num_hardware_attrs
; j
++) {
3666 if (pTemplate
[i
].type
== HARDWARE_ATTRS
[j
]) {
3667 hardware_found
= B_TRUE
;
3668 pclasses
[num_pclasses
++] =
3676 for (j
= 0; j
< num_cert_attrs
; j
++) {
3677 if (pTemplate
[i
].type
== CERT_ATTRS
[j
]) {
3678 cert_found
= B_TRUE
;
3679 pclasses
[num_pclasses
++] =
3686 *num_result_pclasses
= num_pclasses
;
3691 kernel_find_match_attrs(kernel_object_t
*obj
, CK_OBJECT_CLASS
*pclasses
,
3692 CK_ULONG num_pclasses
, CK_ATTRIBUTE
*template, CK_ULONG num_attr
)
3695 CK_ATTRIBUTE
*tmpl_attr
, *obj_attr
;
3697 biginteger_t
*bigint
;
3698 boolean_t compare_attr
, compare_bigint
, compare_boolean
;
3701 * Check if the class of this object match with any
3702 * of object classes that can possibly contain the
3703 * requested attributes.
3705 if (num_pclasses
> 0) {
3706 for (i
= 0; i
< num_pclasses
; i
++) {
3707 if (obj
->class == pclasses
[i
]) {
3711 if (i
== num_pclasses
) {
3713 * this object can't possibly contain one or
3714 * more attributes, don't need to check this object
3720 /* need to examine everything */
3721 for (i
= 0; i
< num_attr
; i
++) {
3722 tmpl_attr
= &(template[i
]);
3723 compare_attr
= B_FALSE
;
3724 compare_bigint
= B_FALSE
;
3725 compare_boolean
= B_FALSE
;
3726 switch (tmpl_attr
->type
) {
3727 /* First, check the most common attributes */
3729 if (*((CK_OBJECT_CLASS
*)tmpl_attr
->pValue
) !=
3735 if (*((CK_KEY_TYPE
*)tmpl_attr
->pValue
) !=
3741 attr_mask
= (obj
->bool_attr_mask
) & ENCRYPT_BOOL_ON
;
3742 compare_boolean
= B_TRUE
;
3745 attr_mask
= (obj
->bool_attr_mask
) & DECRYPT_BOOL_ON
;
3746 compare_boolean
= B_TRUE
;
3749 attr_mask
= (obj
->bool_attr_mask
) & WRAP_BOOL_ON
;
3750 compare_boolean
= B_TRUE
;
3753 attr_mask
= (obj
->bool_attr_mask
) & UNWRAP_BOOL_ON
;
3754 compare_boolean
= B_TRUE
;
3757 attr_mask
= (obj
->bool_attr_mask
) & SIGN_BOOL_ON
;
3758 compare_boolean
= B_TRUE
;
3760 case CKA_SIGN_RECOVER
:
3761 attr_mask
= (obj
->bool_attr_mask
) &
3762 SIGN_RECOVER_BOOL_ON
;
3763 compare_boolean
= B_TRUE
;
3766 attr_mask
= (obj
->bool_attr_mask
) & VERIFY_BOOL_ON
;
3767 compare_boolean
= B_TRUE
;
3769 case CKA_VERIFY_RECOVER
:
3770 attr_mask
= (obj
->bool_attr_mask
) &
3771 VERIFY_RECOVER_BOOL_ON
;
3772 compare_boolean
= B_TRUE
;
3775 attr_mask
= (obj
->bool_attr_mask
) & DERIVE_BOOL_ON
;
3776 compare_boolean
= B_TRUE
;
3779 attr_mask
= (obj
->bool_attr_mask
) & LOCAL_BOOL_ON
;
3780 compare_boolean
= B_TRUE
;
3783 attr_mask
= (obj
->bool_attr_mask
) & SENSITIVE_BOOL_ON
;
3784 compare_boolean
= B_TRUE
;
3786 case CKA_SECONDARY_AUTH
:
3787 attr_mask
= (obj
->bool_attr_mask
) &
3788 SECONDARY_AUTH_BOOL_ON
;
3789 compare_boolean
= B_TRUE
;
3792 attr_mask
= (obj
->bool_attr_mask
) & TRUSTED_BOOL_ON
;
3793 compare_boolean
= B_TRUE
;
3795 case CKA_EXTRACTABLE
:
3796 attr_mask
= (obj
->bool_attr_mask
) &
3797 EXTRACTABLE_BOOL_ON
;
3798 compare_boolean
= B_TRUE
;
3800 case CKA_ALWAYS_SENSITIVE
:
3801 attr_mask
= (obj
->bool_attr_mask
) &
3802 ALWAYS_SENSITIVE_BOOL_ON
;
3803 compare_boolean
= B_TRUE
;
3805 case CKA_NEVER_EXTRACTABLE
:
3806 attr_mask
= (obj
->bool_attr_mask
) &
3807 NEVER_EXTRACTABLE_BOOL_ON
;
3808 compare_boolean
= B_TRUE
;
3812 * CKA_TOKEN value is not applicable to an object
3813 * created in the library, it should only contain
3814 * the default value FALSE
3817 compare_boolean
= B_TRUE
;
3820 attr_mask
= (obj
->bool_attr_mask
) & PRIVATE_BOOL_ON
;
3821 compare_boolean
= B_TRUE
;
3823 case CKA_MODIFIABLE
:
3824 attr_mask
= (obj
->bool_attr_mask
) & MODIFIABLE_BOOL_ON
;
3825 compare_boolean
= B_TRUE
;
3829 case CKA_START_DATE
:
3831 case CKA_KEY_GEN_MECHANISM
:
3833 /* find these attributes from extra_attrlistp */
3834 obj_attr
= get_extra_attr(tmpl_attr
->type
, obj
);
3835 compare_attr
= B_TRUE
;
3838 /* only secret key has this attribute */
3839 if (obj
->class == CKO_SECRET_KEY
) {
3840 if (*((CK_ULONG
*)tmpl_attr
->pValue
) !=
3841 OBJ_SEC_VALUE_LEN(obj
)) {
3849 switch (obj
->class) {
3850 case CKO_SECRET_KEY
:
3852 * secret_key_obj_t is the same as
3855 bigint
= (biginteger_t
*)OBJ_SEC(obj
);
3857 case CKO_PRIVATE_KEY
:
3858 if (obj
->key_type
== CKK_DSA
) {
3859 bigint
= OBJ_PRI_DSA_VALUE(obj
);
3864 case CKO_PUBLIC_KEY
:
3865 if (obj
->key_type
== CKK_DSA
) {
3866 bigint
= OBJ_PUB_DSA_VALUE(obj
);
3874 compare_bigint
= B_TRUE
;
3877 /* only RSA public and private key have this attr */
3878 if (obj
->key_type
== CKK_RSA
) {
3879 if (obj
->class == CKO_PUBLIC_KEY
) {
3880 bigint
= OBJ_PUB_RSA_MOD(obj
);
3881 } else if (obj
->class == CKO_PRIVATE_KEY
) {
3882 bigint
= OBJ_PRI_RSA_MOD(obj
);
3886 compare_bigint
= B_TRUE
;
3891 case CKA_MODULUS_BITS
:
3892 /* only RSA public key has this attribute */
3893 if ((obj
->key_type
== CKK_RSA
) &&
3894 (obj
->class == CKO_PUBLIC_KEY
)) {
3895 CK_ULONG mod_bits
= OBJ_PUB_RSA_MOD_BITS(obj
);
3897 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
3904 case CKA_PUBLIC_EXPONENT
:
3905 /* only RSA public and private key have this attr */
3906 if (obj
->key_type
== CKK_RSA
) {
3907 if (obj
->class == CKO_PUBLIC_KEY
) {
3908 bigint
= OBJ_PUB_RSA_PUBEXPO(obj
);
3909 } else if (obj
->class == CKO_PRIVATE_KEY
) {
3910 bigint
= OBJ_PRI_RSA_PUBEXPO(obj
);
3914 compare_bigint
= B_TRUE
;
3919 case CKA_PRIVATE_EXPONENT
:
3920 /* only RSA private key has this attribute */
3921 if ((obj
->key_type
== CKK_RSA
) &&
3922 (obj
->class == CKO_PRIVATE_KEY
)) {
3923 bigint
= OBJ_PRI_RSA_PRIEXPO(obj
);
3924 compare_bigint
= B_TRUE
;
3930 /* only RSA private key has this attribute */
3931 if ((obj
->key_type
== CKK_RSA
) &&
3932 (obj
->class == CKO_PRIVATE_KEY
)) {
3933 bigint
= OBJ_PRI_RSA_PRIME1(obj
);
3934 compare_bigint
= B_TRUE
;
3940 /* only RSA private key has this attribute */
3941 if ((obj
->key_type
== CKK_RSA
) &&
3942 (obj
->class == CKO_PRIVATE_KEY
)) {
3943 bigint
= OBJ_PRI_RSA_PRIME2(obj
);
3944 compare_bigint
= B_TRUE
;
3949 case CKA_EXPONENT_1
:
3950 /* only RSA private key has this attribute */
3951 if ((obj
->key_type
== CKK_RSA
) &&
3952 (obj
->class == CKO_PRIVATE_KEY
)) {
3953 bigint
= OBJ_PRI_RSA_EXPO1(obj
);
3954 compare_bigint
= B_TRUE
;
3959 case CKA_EXPONENT_2
:
3960 /* only RSA private key has this attribute */
3961 if ((obj
->key_type
== CKK_RSA
) &&
3962 (obj
->class == CKO_PRIVATE_KEY
)) {
3963 bigint
= OBJ_PRI_RSA_EXPO2(obj
);
3964 compare_bigint
= B_TRUE
;
3969 case CKA_COEFFICIENT
:
3970 /* only RSA private key has this attribute */
3971 if ((obj
->key_type
== CKK_RSA
) &&
3972 (obj
->class == CKO_PRIVATE_KEY
)) {
3973 bigint
= OBJ_PRI_RSA_COEF(obj
);
3974 compare_bigint
= B_TRUE
;
3979 case CKA_VALUE_BITS
:
3982 if (obj
->class == CKO_PUBLIC_KEY
) {
3983 switch (obj
->key_type
) {
3985 bigint
= OBJ_PUB_DSA_PRIME(obj
);
3990 } else if (obj
->class == CKO_PRIVATE_KEY
) {
3991 switch (obj
->key_type
) {
3993 bigint
= OBJ_PRI_DSA_PRIME(obj
);
4001 compare_bigint
= B_TRUE
;
4004 if (obj
->class == CKO_PUBLIC_KEY
) {
4005 switch (obj
->key_type
) {
4007 bigint
= OBJ_PUB_DSA_SUBPRIME(obj
);
4012 } else if (obj
->class == CKO_PRIVATE_KEY
) {
4013 switch (obj
->key_type
) {
4015 bigint
= OBJ_PRI_DSA_SUBPRIME(obj
);
4023 compare_bigint
= B_TRUE
;
4026 if (obj
->class == CKO_PUBLIC_KEY
) {
4027 switch (obj
->key_type
) {
4029 bigint
= OBJ_PUB_DSA_BASE(obj
);
4034 } else if (obj
->class == CKO_PRIVATE_KEY
) {
4035 switch (obj
->key_type
) {
4037 bigint
= OBJ_PRI_DSA_BASE(obj
);
4045 compare_bigint
= B_TRUE
;
4047 case CKA_PRIME_BITS
:
4049 case CKA_SUBPRIME_BITS
:
4053 * any other attributes are currently not supported.
4054 * so, it's not possible for them to be in the
4059 if (compare_boolean
) {
4067 if (bval
!= *((CK_BBOOL
*)tmpl_attr
->pValue
)) {
4070 } else if (compare_bigint
) {
4071 if (bigint
== NULL
) {
4074 if (tmpl_attr
->ulValueLen
!= bigint
->big_value_len
) {
4077 if (memcmp(tmpl_attr
->pValue
, bigint
->big_value
,
4078 tmpl_attr
->ulValueLen
) != 0) {
4081 } else if (compare_attr
) {
4082 if (obj_attr
== NULL
) {
4084 * The attribute type is valid, and its value
4085 * has not been initialized in the object. In
4086 * this case, it only matches the template's
4087 * attribute if the template's value length
4090 if (tmpl_attr
->ulValueLen
!= 0)
4093 if (tmpl_attr
->ulValueLen
!=
4094 obj_attr
->ulValueLen
) {
4097 if (memcmp(tmpl_attr
->pValue
, obj_attr
->pValue
,
4098 tmpl_attr
->ulValueLen
) != 0) {
4108 get_extra_attr(CK_ATTRIBUTE_TYPE type
, kernel_object_t
*obj
)
4110 CK_ATTRIBUTE_INFO_PTR tmp
;
4112 tmp
= obj
->extra_attrlistp
;
4113 while (tmp
!= NULL
) {
4114 if (tmp
->attr
.type
== type
) {
4115 return (&(tmp
->attr
));
4119 /* if get there, the specified attribute is not found */