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 if (extra_attr
->attr
.pValue
)
333 * All extra attributes in the extra attribute
334 * list have pValue points to the value of the
335 * attribute (with simple byte array type).
336 * Free the storage for the value of the attribute.
338 free(extra_attr
->attr
.pValue
);
340 /* Free the storage for the attribute_info struct. */
345 object_p
->extra_attrlistp
= NULL
;
350 * Create the attribute_info struct to hold the object's attribute,
351 * and add it to the extra attribute list of an object.
354 kernel_add_extra_attr(CK_ATTRIBUTE_PTR
template, kernel_object_t
*object_p
)
357 CK_ATTRIBUTE_INFO_PTR attrp
;
359 /* Allocate the storage for the attribute_info struct. */
360 attrp
= calloc(1, sizeof (attribute_info_t
));
362 return (CKR_HOST_MEMORY
);
365 /* Set up attribute_info struct. */
366 attrp
->attr
.type
= template->type
;
367 attrp
->attr
.ulValueLen
= template->ulValueLen
;
369 if ((template->pValue
!= NULL
) &&
370 (template->ulValueLen
> 0)) {
371 /* Allocate storage for the value of the attribute. */
372 attrp
->attr
.pValue
= malloc(template->ulValueLen
);
373 if (attrp
->attr
.pValue
== NULL
) {
375 return (CKR_HOST_MEMORY
);
378 (void) memcpy(attrp
->attr
.pValue
, template->pValue
,
379 template->ulValueLen
);
381 attrp
->attr
.pValue
= NULL
;
384 /* Insert the new attribute in front of extra attribute list. */
385 if (object_p
->extra_attrlistp
== NULL
) {
386 object_p
->extra_attrlistp
= attrp
;
389 attrp
->next
= object_p
->extra_attrlistp
;
390 object_p
->extra_attrlistp
= attrp
;
398 * Copy the attribute_info struct from the old object to a new attribute_info
399 * struct, and add that new struct to the extra attribute list of the new
403 kernel_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp
,
404 kernel_object_t
*object_p
)
406 CK_ATTRIBUTE_INFO_PTR attrp
;
408 /* Allocate attribute_info struct. */
409 attrp
= calloc(1, sizeof (attribute_info_t
));
411 return (CKR_HOST_MEMORY
);
414 attrp
->attr
.type
= old_attrp
->attr
.type
;
415 attrp
->attr
.ulValueLen
= old_attrp
->attr
.ulValueLen
;
417 if ((old_attrp
->attr
.pValue
!= NULL
) &&
418 (old_attrp
->attr
.ulValueLen
> 0)) {
419 attrp
->attr
.pValue
= malloc(old_attrp
->attr
.ulValueLen
);
420 if (attrp
->attr
.pValue
== NULL
) {
422 return (CKR_HOST_MEMORY
);
425 (void) memcpy(attrp
->attr
.pValue
, old_attrp
->attr
.pValue
,
426 old_attrp
->attr
.ulValueLen
);
428 attrp
->attr
.pValue
= NULL
;
431 /* Insert the new attribute in front of extra attribute list */
432 if (object_p
->extra_attrlistp
== NULL
) {
433 object_p
->extra_attrlistp
= attrp
;
436 attrp
->next
= object_p
->extra_attrlistp
;
437 object_p
->extra_attrlistp
= attrp
;
445 * Get the attribute triple from the extra attribute list in the object
446 * (if the specified attribute type is found), and copy it to a template.
447 * Note the type of the attribute to be copied is specified by the template,
448 * and the storage is pre-allocated for the atrribute value in the template
449 * for doing the copy.
452 get_extra_attr_from_object(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
455 CK_ATTRIBUTE_INFO_PTR extra_attr
;
456 CK_ATTRIBUTE_TYPE type
= template->type
;
458 extra_attr
= object_p
->extra_attrlistp
;
461 if (type
== extra_attr
->attr
.type
) {
465 /* Does not match, try next one. */
466 extra_attr
= extra_attr
->next
;
470 if (extra_attr
== NULL
) {
471 /* A valid but un-initialized attribute. */
472 template->ulValueLen
= 0;
477 * We found the attribute in the extra attribute list.
479 if (template->pValue
== NULL
) {
480 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
484 if (template->ulValueLen
>= extra_attr
->attr
.ulValueLen
) {
486 * The buffer provided by the application is large
487 * enough to hold the value of the attribute.
489 (void) memcpy(template->pValue
, extra_attr
->attr
.pValue
,
490 extra_attr
->attr
.ulValueLen
);
491 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
495 * The buffer provided by the application does
496 * not have enough space to hold the value.
498 template->ulValueLen
= (CK_ULONG
)-1;
499 return (CKR_BUFFER_TOO_SMALL
);
505 * Modify the attribute triple in the extra attribute list of the object
506 * if the specified attribute type is found. Otherwise, just add it to
510 set_extra_attr_to_object(kernel_object_t
*object_p
, CK_ATTRIBUTE_TYPE type
,
511 CK_ATTRIBUTE_PTR
template)
514 CK_ATTRIBUTE_INFO_PTR extra_attr
;
516 extra_attr
= object_p
->extra_attrlistp
;
519 if (type
== extra_attr
->attr
.type
) {
523 /* Does not match, try next one. */
524 extra_attr
= extra_attr
->next
;
528 if (extra_attr
== NULL
) {
530 * This attribute is a new one, go ahead adding it to
531 * the extra attribute list.
533 return (kernel_add_extra_attr(template, object_p
));
536 /* We found the attribute in the extra attribute list. */
537 if ((template->pValue
!= NULL
) &&
538 (template->ulValueLen
> 0)) {
539 if (template->ulValueLen
> extra_attr
->attr
.ulValueLen
) {
540 /* The old buffer is too small to hold the new value. */
541 if (extra_attr
->attr
.pValue
!= NULL
)
542 /* Free storage for the old attribute value. */
543 free(extra_attr
->attr
.pValue
);
545 /* Allocate storage for the new attribute value. */
546 extra_attr
->attr
.pValue
= malloc(template->ulValueLen
);
547 if (extra_attr
->attr
.pValue
== NULL
) {
548 return (CKR_HOST_MEMORY
);
552 /* Replace the attribute with new value. */
553 extra_attr
->attr
.ulValueLen
= template->ulValueLen
;
554 (void) memcpy(extra_attr
->attr
.pValue
, template->pValue
,
555 template->ulValueLen
);
557 extra_attr
->attr
.pValue
= NULL
;
565 * Copy the big integer attribute value from template to a biginteger_t struct.
568 get_bigint_attr_from_template(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
571 if ((template->pValue
!= NULL
) &&
572 (template->ulValueLen
> 0)) {
573 /* Allocate storage for the value of the attribute. */
574 big
->big_value
= malloc(template->ulValueLen
);
575 if (big
->big_value
== NULL
) {
576 return (CKR_HOST_MEMORY
);
579 (void) memcpy(big
->big_value
, template->pValue
,
580 template->ulValueLen
);
581 big
->big_value_len
= template->ulValueLen
;
583 big
->big_value
= NULL
;
584 big
->big_value_len
= 0;
592 * Copy the big integer attribute value from a biginteger_t struct in the
593 * object to a template.
596 get_bigint_attr_from_object(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
599 if (template->pValue
== NULL
) {
600 template->ulValueLen
= big
->big_value_len
;
604 if (big
->big_value
== NULL
) {
605 template->ulValueLen
= 0;
609 if (template->ulValueLen
>= big
->big_value_len
) {
611 * The buffer provided by the application is large
612 * enough to hold the value of the attribute.
614 (void) memcpy(template->pValue
, big
->big_value
,
616 template->ulValueLen
= big
->big_value_len
;
620 * The buffer provided by the application does
621 * not have enough space to hold the value.
623 template->ulValueLen
= (CK_ULONG
)-1;
624 return (CKR_BUFFER_TOO_SMALL
);
630 * Copy the boolean data type attribute value from an object for the
631 * specified attribute to the template.
634 get_bool_attr_from_object(kernel_object_t
*object_p
, CK_ULONG bool_flag
,
635 CK_ATTRIBUTE_PTR
template)
638 if (template->pValue
== NULL
) {
639 template->ulValueLen
= sizeof (CK_BBOOL
);
643 if (template->ulValueLen
>= sizeof (CK_BBOOL
)) {
645 * The buffer provided by the application is large
646 * enough to hold the value of the attribute.
648 if (object_p
->bool_attr_mask
& bool_flag
) {
649 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
651 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
654 template->ulValueLen
= sizeof (CK_BBOOL
);
658 * The buffer provided by the application does
659 * not have enough space to hold the value.
661 template->ulValueLen
= (CK_ULONG
)-1;
662 return (CKR_BUFFER_TOO_SMALL
);
667 * Set the boolean data type attribute value in the object.
670 set_bool_attr_to_object(kernel_object_t
*object_p
, CK_ULONG bool_flag
,
671 CK_ATTRIBUTE_PTR
template)
674 if (*(CK_BBOOL
*)template->pValue
)
675 object_p
->bool_attr_mask
|= bool_flag
;
677 object_p
->bool_attr_mask
&= ~bool_flag
;
684 * Copy the CK_ULONG data type attribute value from an object to the
688 get_ulong_attr_from_object(CK_ULONG value
, CK_ATTRIBUTE_PTR
template)
691 if (template->pValue
== NULL
) {
692 template->ulValueLen
= sizeof (CK_ULONG
);
696 if (template->ulValueLen
>= sizeof (CK_ULONG
)) {
698 * The buffer provided by the application is large
699 * enough to hold the value of the attribute.
701 *(CK_ULONG_PTR
)template->pValue
= value
;
702 template->ulValueLen
= sizeof (CK_ULONG
);
706 * The buffer provided by the application does
707 * not have enough space to hold the value.
709 template->ulValueLen
= (CK_ULONG
)-1;
710 return (CKR_BUFFER_TOO_SMALL
);
716 * Copy the CK_ULONG data type attribute value from a template to the
720 get_ulong_attr_from_template(CK_ULONG
*value
, CK_ATTRIBUTE_PTR
template)
723 if (template->pValue
!= NULL
) {
724 *value
= *(CK_ULONG_PTR
)template->pValue
;
732 * Copy the big integer attribute value from source's biginteger_t to
733 * destination's biginteger_t.
736 copy_bigint_attr(biginteger_t
*src
, biginteger_t
*dst
)
739 if ((src
->big_value
!= NULL
) &&
740 (src
->big_value_len
> 0)) {
742 * To do the copy, just have dst's big_value points
745 dst
->big_value
= src
->big_value
;
746 dst
->big_value_len
= src
->big_value_len
;
749 * After the copy, nullify the src's big_value pointer.
750 * It prevents any double freeing the value.
752 src
->big_value
= NULL
;
753 src
->big_value_len
= 0;
755 dst
->big_value
= NULL
;
756 dst
->big_value_len
= 0;
763 get_string_from_template(CK_ATTRIBUTE_PTR dest
, CK_ATTRIBUTE_PTR src
)
765 if ((src
->pValue
!= NULL
) &&
766 (src
->ulValueLen
> 0)) {
767 /* Allocate storage for the value of the attribute. */
768 dest
->pValue
= malloc(src
->ulValueLen
);
769 if (dest
->pValue
== NULL
) {
770 return (CKR_HOST_MEMORY
);
773 (void) memcpy(dest
->pValue
, src
->pValue
,
775 dest
->ulValueLen
= src
->ulValueLen
;
776 dest
->type
= src
->type
;
779 dest
->ulValueLen
= 0;
780 dest
->type
= src
->type
;
788 string_attr_cleanup(CK_ATTRIBUTE_PTR
template)
791 if (template->pValue
) {
792 free(template->pValue
);
793 template->pValue
= NULL
;
794 template->ulValueLen
= 0;
799 * Release the storage allocated for object attribute with big integer
803 bigint_attr_cleanup(biginteger_t
*big
)
809 if (big
->big_value
) {
810 (void) memset(big
->big_value
, 0, big
->big_value_len
);
811 free(big
->big_value
);
812 big
->big_value
= NULL
;
813 big
->big_value_len
= 0;
819 * Clean up and release all the storage allocated to hold the big integer
820 * attributes associated with the type (i.e. class) of the object. Also,
821 * release the storage allocated to the type of the object.
824 kernel_cleanup_object_bigint_attrs(kernel_object_t
*object_p
)
827 CK_OBJECT_CLASS
class = object_p
->class;
828 CK_KEY_TYPE keytype
= object_p
->key_type
;
833 if (OBJ_PUB(object_p
)) {
836 bigint_attr_cleanup(OBJ_PUB_RSA_MOD(
838 bigint_attr_cleanup(OBJ_PUB_RSA_PUBEXPO(
843 bigint_attr_cleanup(OBJ_PUB_DSA_PRIME(
845 bigint_attr_cleanup(OBJ_PUB_DSA_SUBPRIME(
847 bigint_attr_cleanup(OBJ_PUB_DSA_BASE(
849 bigint_attr_cleanup(OBJ_PUB_DSA_VALUE(
854 bigint_attr_cleanup(OBJ_PUB_DH_PRIME(object_p
));
855 bigint_attr_cleanup(OBJ_PUB_DH_BASE(object_p
));
856 bigint_attr_cleanup(OBJ_PUB_DH_VALUE(object_p
));
860 bigint_attr_cleanup(OBJ_PUB_EC_POINT(object_p
));
864 /* Release Public Key Object struct */
865 free(OBJ_PUB(object_p
));
866 OBJ_PUB(object_p
) = NULL
;
870 case CKO_PRIVATE_KEY
:
871 if (OBJ_PRI(object_p
)) {
874 bigint_attr_cleanup(OBJ_PRI_RSA_MOD(
876 bigint_attr_cleanup(OBJ_PRI_RSA_PUBEXPO(
878 bigint_attr_cleanup(OBJ_PRI_RSA_PRIEXPO(
880 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME1(
882 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME2(
884 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO1(
886 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO2(
888 bigint_attr_cleanup(OBJ_PRI_RSA_COEF(
893 bigint_attr_cleanup(OBJ_PRI_DSA_PRIME(
895 bigint_attr_cleanup(OBJ_PRI_DSA_SUBPRIME(
897 bigint_attr_cleanup(OBJ_PRI_DSA_BASE(
899 bigint_attr_cleanup(OBJ_PRI_DSA_VALUE(
904 bigint_attr_cleanup(OBJ_PRI_DH_PRIME(object_p
));
905 bigint_attr_cleanup(OBJ_PRI_DH_BASE(object_p
));
906 bigint_attr_cleanup(OBJ_PRI_DH_VALUE(object_p
));
910 bigint_attr_cleanup(OBJ_PRI_EC_VALUE(object_p
));
914 /* Release Private Key Object struct. */
915 free(OBJ_PRI(object_p
));
916 OBJ_PRI(object_p
) = NULL
;
924 * Parse the common attributes. Return to caller with appropriate return
925 * value to indicate if the supplied template specifies a valid attribute
926 * with a valid value.
929 kernel_parse_common_attrs(CK_ATTRIBUTE_PTR
template, kernel_session_t
*sp
,
930 uint64_t *attr_mask_p
)
934 kernel_slot_t
*pslot
= slot_table
[sp
->ses_slotid
];
936 switch (template->type
) {
940 /* default boolean attributes */
942 if ((*(CK_BBOOL
*)template->pValue
) == TRUE
) {
943 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
948 if ((*(CK_BBOOL
*)template->pValue
) == TRUE
) {
950 * Cannot create a private object if the token
951 * has a keystore and the user isn't logged in.
953 if (pslot
->sl_func_list
.fl_object_create
&&
954 pslot
->sl_state
!= CKU_USER
) {
955 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
957 *attr_mask_p
|= PRIVATE_BOOL_ON
;
963 if ((*(CK_BBOOL
*)template->pValue
) == FALSE
) {
964 *attr_mask_p
&= ~MODIFIABLE_BOOL_ON
;
972 rv
= CKR_TEMPLATE_INCONSISTENT
;
982 * Build a Public Key Object.
984 * - Parse the object's template, and when an error is detected such as
985 * invalid attribute type, invalid attribute value, etc., return
986 * with appropriate return value.
987 * - Set up attribute mask field in the object for the supplied common
988 * attributes that have boolean type.
989 * - Build the attribute_info struct to hold the value of each supplied
990 * attribute that has byte array type. Link attribute_info structs
991 * together to form the extra attribute list of the object.
992 * - Allocate storage for the Public Key object.
993 * - Build the Public Key object according to the key type. Allocate
994 * storage to hold the big integer value for the supplied attributes
995 * that are required for a certain key type.
999 kernel_build_public_key_object(CK_ATTRIBUTE_PTR
template,
1000 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
,
1005 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1006 uint64_t attr_mask
= PUBLIC_KEY_DEFAULT
;
1009 /* Must set flags */
1018 /* Must not set flags */
1019 int isModulusBits
= 0;
1020 CK_ULONG modulus_bits
= 0;
1022 biginteger_t modulus
;
1023 biginteger_t pubexpo
;
1025 biginteger_t subprime
;
1029 CK_ATTRIBUTE string_tmp
;
1030 CK_ATTRIBUTE param_tmp
;
1032 public_key_obj_t
*pbk
;
1034 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1035 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1036 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1037 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1038 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1039 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1040 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1041 (void) memset(&point
, 0x0, sizeof (biginteger_t
));
1042 string_tmp
.pValue
= NULL
;
1043 param_tmp
.pValue
= NULL
;
1045 for (i
= 0; i
< ulAttrNum
; i
++) {
1047 /* Public Key Object Attributes */
1048 switch (template[i
].type
) {
1050 /* common key attributes */
1052 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1056 case CKA_START_DATE
:
1059 /* common public key attribute */
1062 * Allocate storage to hold the attribute
1063 * value with byte array type, and add it to
1064 * the extra attribute list of the object.
1066 rv
= kernel_add_extra_attr(&template[i
],
1074 * The following key related attribute types must
1075 * not be specified by C_CreateObject.
1078 case CKA_KEY_GEN_MECHANISM
:
1079 rv
= CKR_TEMPLATE_INCONSISTENT
;
1082 /* Key related boolean attributes */
1084 if (*(CK_BBOOL
*)template[i
].pValue
)
1085 attr_mask
|= DERIVE_BOOL_ON
;
1089 if (*(CK_BBOOL
*)template[i
].pValue
)
1090 attr_mask
|= ENCRYPT_BOOL_ON
;
1092 attr_mask
&= ~ENCRYPT_BOOL_ON
;
1096 if (*(CK_BBOOL
*)template[i
].pValue
)
1097 attr_mask
|= VERIFY_BOOL_ON
;
1099 attr_mask
&= ~VERIFY_BOOL_ON
;
1102 case CKA_VERIFY_RECOVER
:
1103 if (*(CK_BBOOL
*)template[i
].pValue
)
1104 attr_mask
|= VERIFY_RECOVER_BOOL_ON
;
1106 attr_mask
&= ~VERIFY_RECOVER_BOOL_ON
;
1110 if (*(CK_BBOOL
*)template[i
].pValue
)
1111 attr_mask
|= WRAP_BOOL_ON
;
1115 if (*(CK_BBOOL
*)template[i
].pValue
)
1116 attr_mask
|= TRUSTED_BOOL_ON
;
1120 * The following key related attribute types must
1121 * be specified according to the key type by
1127 * Copyin big integer attribute from template
1128 * to a local variable.
1130 rv
= get_bigint_attr_from_template(&modulus
,
1136 case CKA_PUBLIC_EXPONENT
:
1138 rv
= get_bigint_attr_from_template(&pubexpo
,
1146 rv
= get_bigint_attr_from_template(&prime
,
1154 rv
= get_bigint_attr_from_template(&subprime
,
1162 rv
= get_bigint_attr_from_template(&base
,
1170 rv
= get_bigint_attr_from_template(&value
,
1176 case CKA_MODULUS_BITS
:
1178 get_ulong_attr_from_template(&modulus_bits
,
1184 rv
= get_string_from_template(&string_tmp
,
1192 rv
= get_bigint_attr_from_template(&point
,
1200 rv
= get_string_from_template(¶m_tmp
,
1207 rv
= kernel_parse_common_attrs(&template[i
], sp
,
1215 /* Allocate storage for Public Key Object. */
1216 pbk
= calloc(1, sizeof (public_key_obj_t
));
1218 rv
= CKR_HOST_MEMORY
;
1222 new_object
->object_class_u
.public_key
= pbk
;
1223 new_object
->class = CKO_PUBLIC_KEY
;
1225 if (keytype
== (CK_KEY_TYPE
)~0UL) {
1226 rv
= CKR_TEMPLATE_INCOMPLETE
;
1229 new_object
->key_type
= keytype
;
1231 /* Supported key types of the Public Key Object */
1234 if (mode
== KERNEL_CREATE_OBJ
) {
1235 if (isModulusBits
|| isPrime
|| isSubprime
||
1237 rv
= CKR_TEMPLATE_INCONSISTENT
;
1242 if (isModulus
&& isPubExpo
) {
1244 * Copy big integer attribute value to the
1245 * designated place in the public key object.
1247 copy_bigint_attr(&modulus
,
1248 KEY_PUB_RSA_MOD(pbk
));
1250 copy_bigint_attr(&pubexpo
,
1251 KEY_PUB_RSA_PUBEXPO(pbk
));
1253 rv
= CKR_TEMPLATE_INCOMPLETE
;
1257 /* must be generating a RSA key pair by value */
1258 if (isModulusBits
) {
1259 KEY_PUB_RSA_MOD_BITS(pbk
) = modulus_bits
;
1264 if (isModulusBits
|| isModulus
|| isPubExpo
) {
1265 rv
= CKR_TEMPLATE_INCONSISTENT
;
1269 if (!(isPrime
&& isSubprime
&& isBase
&& isValue
)) {
1270 rv
= CKR_TEMPLATE_INCOMPLETE
;
1274 copy_bigint_attr(&prime
, KEY_PUB_DSA_PRIME(pbk
));
1276 copy_bigint_attr(&subprime
, KEY_PUB_DSA_SUBPRIME(pbk
));
1278 copy_bigint_attr(&base
, KEY_PUB_DSA_BASE(pbk
));
1280 copy_bigint_attr(&value
, KEY_PUB_DSA_VALUE(pbk
));
1285 if (!(isPrime
&& isBase
&& isValue
)) {
1286 rv
= CKR_TEMPLATE_INCOMPLETE
;
1290 copy_bigint_attr(&prime
, KEY_PUB_DH_PRIME(pbk
));
1292 copy_bigint_attr(&base
, KEY_PUB_DH_BASE(pbk
));
1294 copy_bigint_attr(&value
, KEY_PUB_DH_VALUE(pbk
));
1299 if (!isPoint
|| !isParams
) {
1300 rv
= CKR_TEMPLATE_INCOMPLETE
;
1304 copy_bigint_attr(&point
, KEY_PUB_EC_POINT(pbk
));
1305 rv
= kernel_add_extra_attr(¶m_tmp
, new_object
);
1308 string_attr_cleanup(¶m_tmp
);
1311 rv
= CKR_TEMPLATE_INCONSISTENT
;
1315 /* Set up object. */
1316 new_object
->bool_attr_mask
= attr_mask
;
1318 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
1321 string_attr_cleanup(&string_tmp
);
1328 * cleanup the storage allocated to the local variables.
1330 bigint_attr_cleanup(&modulus
);
1331 bigint_attr_cleanup(&pubexpo
);
1332 bigint_attr_cleanup(&prime
);
1333 bigint_attr_cleanup(&subprime
);
1334 bigint_attr_cleanup(&base
);
1335 bigint_attr_cleanup(&value
);
1336 bigint_attr_cleanup(&point
);
1337 string_attr_cleanup(&string_tmp
);
1338 string_attr_cleanup(¶m_tmp
);
1341 * cleanup the storage allocated inside the object itself.
1343 kernel_cleanup_object(new_object
);
1350 * Build a Private Key Object.
1352 * - Parse the object's template, and when an error is detected such as
1353 * invalid attribute type, invalid attribute value, etc., return
1354 * with appropriate return value.
1355 * - Set up attribute mask field in the object for the supplied common
1356 * attributes that have boolean type.
1357 * - Build the attribute_info struct to hold the value of each supplied
1358 * attribute that has byte array type. Link attribute_info structs
1359 * together to form the extra attribute list of the object.
1360 * - Allocate storage for the Private Key object.
1361 * - Build the Private Key object according to the key type. Allocate
1362 * storage to hold the big integer value for the supplied attributes
1363 * that are required for a certain key type.
1367 kernel_build_private_key_object(CK_ATTRIBUTE_PTR
template,
1368 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
,
1372 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1373 uint64_t attr_mask
= PRIVATE_KEY_DEFAULT
;
1376 /* Must set flags */
1384 /* Must not set flags */
1385 int isValueBits
= 0;
1386 CK_ULONG value_bits
= 0;
1388 /* Private Key RSA optional */
1396 biginteger_t modulus
;
1397 biginteger_t priexpo
;
1399 biginteger_t subprime
;
1403 biginteger_t pubexpo
;
1404 biginteger_t prime1
;
1405 biginteger_t prime2
;
1409 CK_ATTRIBUTE string_tmp
;
1410 CK_ATTRIBUTE param_tmp
;
1412 private_key_obj_t
*pvk
;
1414 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1415 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1416 (void) memset(&priexpo
, 0x0, sizeof (biginteger_t
));
1417 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1418 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1419 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1420 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1421 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1422 (void) memset(&prime1
, 0x0, sizeof (biginteger_t
));
1423 (void) memset(&prime2
, 0x0, sizeof (biginteger_t
));
1424 (void) memset(&expo1
, 0x0, sizeof (biginteger_t
));
1425 (void) memset(&expo2
, 0x0, sizeof (biginteger_t
));
1426 (void) memset(&coef
, 0x0, sizeof (biginteger_t
));
1427 string_tmp
.pValue
= NULL
;
1428 param_tmp
.pValue
= NULL
;
1430 for (i
= 0; i
< ulAttrNum
; i
++) {
1432 /* Private Key Object Attributes */
1433 switch (template[i
].type
) {
1435 /* common key attributes */
1437 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1441 case CKA_START_DATE
:
1444 /* common private key attribute */
1447 * Allocate storage to hold the attribute
1448 * value with byte array type, and add it to
1449 * the extra attribute list of the object.
1451 rv
= kernel_add_extra_attr(&template[i
],
1459 * The following key related attribute types must
1460 * not be specified by C_CreateObject.
1463 case CKA_KEY_GEN_MECHANISM
:
1464 case CKA_AUTH_PIN_FLAGS
:
1465 case CKA_ALWAYS_SENSITIVE
:
1466 case CKA_NEVER_EXTRACTABLE
:
1467 rv
= CKR_TEMPLATE_INCONSISTENT
;
1470 /* Key related boolean attributes */
1472 if (*(CK_BBOOL
*)template[i
].pValue
)
1473 attr_mask
|= DERIVE_BOOL_ON
;
1477 if (*(CK_BBOOL
*)template[i
].pValue
)
1478 attr_mask
|= SENSITIVE_BOOL_ON
;
1481 case CKA_SECONDARY_AUTH
:
1482 if (*(CK_BBOOL
*)template[i
].pValue
) {
1483 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1489 if (*(CK_BBOOL
*)template[i
].pValue
)
1490 attr_mask
|= DECRYPT_BOOL_ON
;
1492 attr_mask
&= ~DECRYPT_BOOL_ON
;
1496 if (*(CK_BBOOL
*)template[i
].pValue
)
1497 attr_mask
|= SIGN_BOOL_ON
;
1499 attr_mask
&= ~SIGN_BOOL_ON
;
1502 case CKA_SIGN_RECOVER
:
1503 if (*(CK_BBOOL
*)template[i
].pValue
)
1504 attr_mask
|= SIGN_RECOVER_BOOL_ON
;
1506 attr_mask
&= ~SIGN_RECOVER_BOOL_ON
;
1510 if (*(CK_BBOOL
*)template[i
].pValue
)
1511 attr_mask
|= UNWRAP_BOOL_ON
;
1514 case CKA_EXTRACTABLE
:
1515 if (*(CK_BBOOL
*)template[i
].pValue
)
1516 attr_mask
|= EXTRACTABLE_BOOL_ON
;
1518 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
1522 * The following key related attribute types must
1523 * be specified according to the key type by
1529 * Copyin big integer attribute from template
1530 * to a local variable.
1532 rv
= get_bigint_attr_from_template(&modulus
,
1538 case CKA_PUBLIC_EXPONENT
:
1540 rv
= get_bigint_attr_from_template(&pubexpo
,
1546 case CKA_PRIVATE_EXPONENT
:
1548 rv
= get_bigint_attr_from_template(&priexpo
,
1556 rv
= get_bigint_attr_from_template(&prime1
,
1564 rv
= get_bigint_attr_from_template(&prime2
,
1570 case CKA_EXPONENT_1
:
1572 rv
= get_bigint_attr_from_template(&expo1
,
1578 case CKA_EXPONENT_2
:
1580 rv
= get_bigint_attr_from_template(&expo2
,
1586 case CKA_COEFFICIENT
:
1588 rv
= get_bigint_attr_from_template(&coef
,
1596 rv
= get_bigint_attr_from_template(&prime
,
1604 rv
= get_bigint_attr_from_template(&subprime
,
1612 rv
= get_bigint_attr_from_template(&base
,
1620 rv
= get_bigint_attr_from_template(&value
,
1626 case CKA_VALUE_BITS
:
1628 get_ulong_attr_from_template(&value_bits
,
1634 rv
= get_string_from_template(&string_tmp
,
1642 rv
= get_string_from_template(¶m_tmp
,
1649 rv
= kernel_parse_common_attrs(&template[i
], sp
,
1658 /* Allocate storage for Private Key Object. */
1659 pvk
= calloc(1, sizeof (private_key_obj_t
));
1661 rv
= CKR_HOST_MEMORY
;
1665 new_object
->object_class_u
.private_key
= pvk
;
1666 new_object
->class = CKO_PRIVATE_KEY
;
1668 if (keytype
== (CK_KEY_TYPE
)~0UL) {
1669 rv
= CKR_TEMPLATE_INCOMPLETE
;
1673 new_object
->key_type
= keytype
;
1675 /* Supported key types of the Private Key Object */
1678 if (isPrime
|| isSubprime
|| isBase
|| isValue
||
1680 rv
= CKR_TEMPLATE_INCONSISTENT
;
1684 if (isModulus
&& isPriExpo
) {
1686 * Copy big integer attribute value to the
1687 * designated place in the Private Key object.
1689 copy_bigint_attr(&modulus
, KEY_PRI_RSA_MOD(pvk
));
1691 copy_bigint_attr(&priexpo
, KEY_PRI_RSA_PRIEXPO(pvk
));
1694 rv
= CKR_TEMPLATE_INCOMPLETE
;
1698 /* The following attributes are optional. */
1700 copy_bigint_attr(&pubexpo
, KEY_PRI_RSA_PUBEXPO(pvk
));
1704 copy_bigint_attr(&prime1
, KEY_PRI_RSA_PRIME1(pvk
));
1708 copy_bigint_attr(&prime2
, KEY_PRI_RSA_PRIME2(pvk
));
1712 copy_bigint_attr(&expo1
, KEY_PRI_RSA_EXPO1(pvk
));
1716 copy_bigint_attr(&expo2
, KEY_PRI_RSA_EXPO2(pvk
));
1720 copy_bigint_attr(&coef
, KEY_PRI_RSA_COEF(pvk
));
1725 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
1726 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
1728 rv
= CKR_TEMPLATE_INCONSISTENT
;
1732 if (!(isPrime
&& isSubprime
&& isBase
&& isValue
)) {
1733 rv
= CKR_TEMPLATE_INCOMPLETE
;
1737 copy_bigint_attr(&prime
, KEY_PRI_DSA_PRIME(pvk
));
1739 copy_bigint_attr(&subprime
, KEY_PRI_DSA_SUBPRIME(pvk
));
1741 copy_bigint_attr(&base
, KEY_PRI_DSA_BASE(pvk
));
1743 copy_bigint_attr(&value
, KEY_PRI_DSA_VALUE(pvk
));
1748 if (mode
== KERNEL_CREATE_OBJ
&& isValueBits
) {
1749 rv
= CKR_TEMPLATE_INCONSISTENT
;
1752 if (!(isPrime
&& isBase
&& isValue
)) {
1753 rv
= CKR_TEMPLATE_INCOMPLETE
;
1757 copy_bigint_attr(&prime
, KEY_PRI_DH_PRIME(pvk
));
1759 copy_bigint_attr(&base
, KEY_PRI_DH_BASE(pvk
));
1761 copy_bigint_attr(&value
, KEY_PRI_DH_VALUE(pvk
));
1763 KEY_PRI_DH_VAL_BITS(pvk
) = (isValueBits
) ? value_bits
: 0;
1768 if (!isValue
|| !isParams
) {
1769 rv
= CKR_TEMPLATE_INCOMPLETE
;
1773 copy_bigint_attr(&value
, KEY_PRI_EC_VALUE(pvk
));
1774 rv
= kernel_add_extra_attr(¶m_tmp
, new_object
);
1777 string_attr_cleanup(¶m_tmp
);
1780 rv
= CKR_TEMPLATE_INCONSISTENT
;
1784 /* Set up object. */
1785 new_object
->bool_attr_mask
= attr_mask
;
1787 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
1790 string_attr_cleanup(&string_tmp
);
1797 * cleanup the storage allocated to the local variables.
1799 bigint_attr_cleanup(&modulus
);
1800 bigint_attr_cleanup(&priexpo
);
1801 bigint_attr_cleanup(&prime
);
1802 bigint_attr_cleanup(&subprime
);
1803 bigint_attr_cleanup(&base
);
1804 bigint_attr_cleanup(&value
);
1805 bigint_attr_cleanup(&pubexpo
);
1806 bigint_attr_cleanup(&prime1
);
1807 bigint_attr_cleanup(&prime2
);
1808 bigint_attr_cleanup(&expo1
);
1809 bigint_attr_cleanup(&expo2
);
1810 bigint_attr_cleanup(&coef
);
1811 string_attr_cleanup(&string_tmp
);
1812 string_attr_cleanup(¶m_tmp
);
1815 * cleanup the storage allocated inside the object itself.
1817 kernel_cleanup_object(new_object
);
1824 * Build a Secret Key Object.
1826 * - Parse the object's template, and when an error is detected such as
1827 * invalid attribute type, invalid attribute value, etc., return
1828 * with appropriate return value.
1829 * - Set up attribute mask field in the object for the supplied common
1830 * attributes that have boolean type.
1831 * - Build the attribute_info struct to hold the value of each supplied
1832 * attribute that has byte array type. Link attribute_info structs
1833 * together to form the extra attribute list of the object.
1834 * - Allocate storage for the Secret Key object.
1835 * - Build the Secret Key object. Allocate storage to hold the big integer
1836 * value for the attribute CKA_VALUE that is required for all the key
1837 * types supported by secret key object.
1841 kernel_build_secret_key_object(CK_ATTRIBUTE_PTR
template,
1842 CK_ULONG ulAttrNum
, kernel_object_t
*new_object
, kernel_session_t
*sp
)
1846 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1847 uint64_t attr_mask
= SECRET_KEY_DEFAULT
;
1850 /* Must set flags */
1852 /* Must not set flags */
1855 CK_ATTRIBUTE string_tmp
;
1857 secret_key_obj_t
*sck
;
1859 string_tmp
.pValue
= NULL
;
1861 /* Allocate storage for Secret Key Object. */
1862 sck
= calloc(1, sizeof (secret_key_obj_t
));
1864 rv
= CKR_HOST_MEMORY
;
1868 new_object
->object_class_u
.secret_key
= sck
;
1869 new_object
->class = CKO_SECRET_KEY
;
1871 for (i
= 0; i
< ulAttrNum
; i
++) {
1873 /* Secret Key Object Attributes */
1874 switch (template[i
].type
) {
1876 /* common key attributes */
1878 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1882 case CKA_START_DATE
:
1885 * Allocate storage to hold the attribute
1886 * value with byte array type, and add it to
1887 * the extra attribute list of the object.
1889 rv
= kernel_add_extra_attr(&template[i
],
1897 * The following key related attribute types must
1898 * not be specified by C_CreateObject.
1901 case CKA_KEY_GEN_MECHANISM
:
1902 case CKA_ALWAYS_SENSITIVE
:
1903 case CKA_NEVER_EXTRACTABLE
:
1904 rv
= CKR_TEMPLATE_INCONSISTENT
;
1907 /* Key related boolean attributes */
1909 if (*(CK_BBOOL
*)template[i
].pValue
)
1910 attr_mask
|= DERIVE_BOOL_ON
;
1914 if (*(CK_BBOOL
*)template[i
].pValue
)
1915 attr_mask
|= SENSITIVE_BOOL_ON
;
1919 if (*(CK_BBOOL
*)template[i
].pValue
)
1920 attr_mask
|= ENCRYPT_BOOL_ON
;
1922 attr_mask
&= ~ENCRYPT_BOOL_ON
;
1926 if (*(CK_BBOOL
*)template[i
].pValue
)
1927 attr_mask
|= DECRYPT_BOOL_ON
;
1929 attr_mask
&= ~DECRYPT_BOOL_ON
;
1933 if (*(CK_BBOOL
*)template[i
].pValue
)
1934 attr_mask
|= SIGN_BOOL_ON
;
1936 attr_mask
&= ~SIGN_BOOL_ON
;
1940 if (*(CK_BBOOL
*)template[i
].pValue
)
1941 attr_mask
|= VERIFY_BOOL_ON
;
1943 attr_mask
&= ~VERIFY_BOOL_ON
;
1947 if (*(CK_BBOOL
*)template[i
].pValue
)
1948 attr_mask
|= WRAP_BOOL_ON
;
1952 if (*(CK_BBOOL
*)template[i
].pValue
)
1953 attr_mask
|= UNWRAP_BOOL_ON
;
1956 case CKA_EXTRACTABLE
:
1957 if (*(CK_BBOOL
*)template[i
].pValue
)
1958 attr_mask
|= EXTRACTABLE_BOOL_ON
;
1960 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
1965 if ((template[i
].ulValueLen
== 0) ||
1966 (template[i
].pValue
== NULL
)) {
1967 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1972 * Copyin attribute from template
1973 * to a local variable.
1975 sck
->sk_value
= malloc(template[i
].ulValueLen
);
1976 if (sck
->sk_value
== NULL
) {
1977 rv
= CKR_HOST_MEMORY
;
1980 (void) memcpy(sck
->sk_value
, template[i
].pValue
,
1981 template[i
].ulValueLen
);
1982 sck
->sk_value_len
= template[i
].ulValueLen
;
1991 rv
= get_string_from_template(&string_tmp
,
1998 rv
= kernel_parse_common_attrs(&template[i
], sp
,
2007 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2008 rv
= CKR_TEMPLATE_INCOMPLETE
;
2012 new_object
->key_type
= keytype
;
2014 /* Supported key types of the Secret Key Object */
2018 rv
= CKR_TEMPLATE_INCOMPLETE
;
2021 if ((sck
->sk_value_len
< ARCFOUR_MIN_KEY_BYTES
) ||
2022 (sck
->sk_value_len
> ARCFOUR_MAX_KEY_BYTES
)) {
2023 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2028 case CKK_GENERIC_SECRET
:
2030 rv
= CKR_TEMPLATE_INCOMPLETE
;
2037 rv
= CKR_TEMPLATE_INCOMPLETE
;
2040 if (sck
->sk_value_len
< AES_MIN_KEY_BYTES
) {
2041 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2048 rv
= CKR_TEMPLATE_INCOMPLETE
;
2051 if (sck
->sk_value_len
< BLOWFISH_MINBYTES
) {
2052 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2059 rv
= CKR_TEMPLATE_INCOMPLETE
;
2062 if (sck
->sk_value_len
!= DES_KEYSIZE
) {
2063 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2070 rv
= CKR_TEMPLATE_INCOMPLETE
;
2073 if (sck
->sk_value_len
!= DES2_KEYSIZE
) {
2074 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2081 rv
= CKR_TEMPLATE_INCOMPLETE
;
2084 if (sck
->sk_value_len
!= DES3_KEYSIZE
) {
2085 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2091 rv
= CKR_TEMPLATE_INCONSISTENT
;
2096 rv
= CKR_TEMPLATE_INCONSISTENT
;
2100 /* Set up object. */
2101 new_object
->bool_attr_mask
= attr_mask
;
2103 rv
= kernel_add_extra_attr(&string_tmp
, new_object
);
2106 string_attr_cleanup(&string_tmp
);
2113 * cleanup the storage allocated to the local variables.
2115 string_attr_cleanup(&string_tmp
);
2118 * cleanup the storage allocated inside the object itself.
2120 kernel_cleanup_object(new_object
);
2127 * Validate the attribute types in the object's template. Then,
2128 * call the appropriate build function according to the class of
2129 * the object specified in the template.
2131 * Note: The following classes of objects are supported:
2137 kernel_build_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
2138 kernel_object_t
*new_object
, kernel_session_t
*sp
, uint_t mode
)
2141 CK_OBJECT_CLASS
class = (CK_OBJECT_CLASS
)~0UL;
2144 if (template == NULL
) {
2145 return (CKR_ARGUMENTS_BAD
);
2148 /* Validate the attribute type in the template. */
2149 rv
= kernel_validate_attr(template, ulAttrNum
, &class);
2153 if (class == (CK_OBJECT_CLASS
)~0UL)
2154 return (CKR_TEMPLATE_INCOMPLETE
);
2157 * Call the appropriate function based on the supported class
2161 case CKO_PUBLIC_KEY
:
2162 rv
= kernel_build_public_key_object(template, ulAttrNum
,
2163 new_object
, sp
, mode
);
2166 case CKO_PRIVATE_KEY
:
2167 rv
= kernel_build_private_key_object(template, ulAttrNum
,
2168 new_object
, sp
, mode
);
2171 case CKO_SECRET_KEY
:
2172 rv
= kernel_build_secret_key_object(template, ulAttrNum
,
2176 case CKO_DOMAIN_PARAMETERS
:
2178 case CKO_CERTIFICATE
:
2179 case CKO_HW_FEATURE
:
2180 case CKO_VENDOR_DEFINED
:
2182 return (CKR_ATTRIBUTE_VALUE_INVALID
);
2190 * Get the value of a requested attribute that is common to all supported
2191 * classes (i.e. public key, private key, secret key classes).
2194 kernel_get_common_attrs(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
2199 switch (template->type
) {
2202 return (get_ulong_attr_from_object(object_p
->class,
2205 /* default boolean attributes */
2208 template->ulValueLen
= sizeof (CK_BBOOL
);
2209 if (template->pValue
== NULL
) {
2214 * A token object will not be created in the library, so we
2217 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2222 template->ulValueLen
= sizeof (CK_BBOOL
);
2223 if (template->pValue
== NULL
) {
2226 if (object_p
->bool_attr_mask
& PRIVATE_BOOL_ON
) {
2227 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
2229 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2233 case CKA_MODIFIABLE
:
2234 template->ulValueLen
= sizeof (CK_BBOOL
);
2235 if (template->pValue
== NULL
) {
2238 if ((object_p
->bool_attr_mask
) & MODIFIABLE_BOOL_ON
)
2239 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
2241 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
2245 return (get_extra_attr_from_object(object_p
,
2250 * The specified attribute for the object is invalid.
2251 * (the object does not possess such an attribute.)
2253 template->ulValueLen
= (CK_ULONG
)-1;
2254 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2261 * Get the value of a requested attribute that is common to all key objects
2262 * (i.e. public key, private key and secret key).
2265 kernel_get_common_key_attrs(kernel_object_t
*object_p
,
2266 CK_ATTRIBUTE_PTR
template)
2269 switch (template->type
) {
2272 return (get_ulong_attr_from_object(object_p
->key_type
,
2276 case CKA_START_DATE
:
2279 * The above extra attributes have byte array type.
2281 return (get_extra_attr_from_object(object_p
,
2284 /* Key related boolean attributes */
2286 return (get_bool_attr_from_object(object_p
,
2287 LOCAL_BOOL_ON
, template));
2290 return (get_bool_attr_from_object(object_p
,
2291 DERIVE_BOOL_ON
, template));
2293 case CKA_KEY_GEN_MECHANISM
:
2294 return (get_ulong_attr_from_object(object_p
->mechanism
,
2298 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2304 * Get the value of a requested attribute of a Public Key Object.
2306 * Rule: All the attributes in the public key object can be revealed.
2309 kernel_get_public_key_attribute(kernel_object_t
*object_p
,
2310 CK_ATTRIBUTE_PTR
template)
2314 CK_KEY_TYPE keytype
= object_p
->key_type
;
2316 switch (template->type
) {
2321 * The above extra attributes have byte array type.
2323 return (get_extra_attr_from_object(object_p
,
2326 /* Key related boolean attributes */
2328 return (get_bool_attr_from_object(object_p
,
2329 ENCRYPT_BOOL_ON
, template));
2332 return (get_bool_attr_from_object(object_p
,
2333 VERIFY_BOOL_ON
, template));
2335 case CKA_VERIFY_RECOVER
:
2336 return (get_bool_attr_from_object(object_p
,
2337 VERIFY_RECOVER_BOOL_ON
, template));
2340 return (get_bool_attr_from_object(object_p
,
2341 WRAP_BOOL_ON
, template));
2344 return (get_bool_attr_from_object(object_p
,
2345 TRUSTED_BOOL_ON
, template));
2349 * This attribute is valid only for RSA public key
2352 if (keytype
== CKK_RSA
) {
2353 return (get_bigint_attr_from_object(
2354 OBJ_PUB_RSA_MOD(object_p
), template));
2356 template->ulValueLen
= (CK_ULONG
)-1;
2357 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2360 case CKA_PUBLIC_EXPONENT
:
2361 if (keytype
== CKK_RSA
) {
2362 return (get_bigint_attr_from_object(
2363 OBJ_PUB_RSA_PUBEXPO(object_p
), template));
2365 template->ulValueLen
= (CK_ULONG
)-1;
2366 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2369 case CKA_MODULUS_BITS
:
2370 if (keytype
== CKK_RSA
) {
2371 return (get_ulong_attr_from_object(
2372 OBJ_PUB_RSA_MOD_BITS(object_p
), template));
2374 template->ulValueLen
= (CK_ULONG
)-1;
2375 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2381 return (get_bigint_attr_from_object(
2382 OBJ_PUB_DSA_PRIME(object_p
), template));
2384 return (get_bigint_attr_from_object(
2385 OBJ_PUB_DH_PRIME(object_p
), template));
2387 template->ulValueLen
= (CK_ULONG
)-1;
2388 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2394 return (get_bigint_attr_from_object(
2395 OBJ_PUB_DSA_SUBPRIME(object_p
), template));
2397 template->ulValueLen
= (CK_ULONG
)-1;
2398 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2404 return (get_bigint_attr_from_object(
2405 OBJ_PUB_DSA_BASE(object_p
), template));
2407 return (get_bigint_attr_from_object(
2408 OBJ_PUB_DH_BASE(object_p
), template));
2410 template->ulValueLen
= (CK_ULONG
)-1;
2411 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2417 return (get_bigint_attr_from_object(
2418 OBJ_PUB_DSA_VALUE(object_p
), template));
2420 return (get_bigint_attr_from_object(
2421 OBJ_PUB_DH_VALUE(object_p
), template));
2423 template->ulValueLen
= (CK_ULONG
)-1;
2424 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2430 return (get_bigint_attr_from_object(
2431 OBJ_PUB_EC_POINT(object_p
), template));
2433 template->ulValueLen
= (CK_ULONG
)-1;
2434 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2438 * First, get the value of the request attribute defined
2439 * in the list of common key attributes. If the request
2440 * attribute is not found in that list, then get the
2441 * attribute from the list of common attributes.
2443 rv
= kernel_get_common_key_attrs(object_p
, template);
2444 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2445 rv
= kernel_get_common_attrs(object_p
, template);
2455 * Get the value of a requested attribute of a Private Key Object.
2457 * Rule: All the attributes in the private key object can be revealed
2458 * except those marked with footnote number "7" when the object
2459 * has its CKA_SENSITIVE attribute set to TRUE or its
2460 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
2463 kernel_get_private_key_attribute(kernel_object_t
*object_p
,
2464 CK_ATTRIBUTE_PTR
template)
2468 CK_KEY_TYPE keytype
= object_p
->key_type
;
2472 * If the following specified attributes for the private key
2473 * object cannot be revealed because the object is sensitive
2474 * or unextractable, then the ulValueLen is set to -1.
2476 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
2477 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
2479 switch (template->type
) {
2480 case CKA_PRIVATE_EXPONENT
:
2483 case CKA_EXPONENT_1
:
2484 case CKA_EXPONENT_2
:
2485 case CKA_COEFFICIENT
:
2487 template->ulValueLen
= (CK_ULONG
)-1;
2488 return (CKR_ATTRIBUTE_SENSITIVE
);
2492 switch (template->type
) {
2497 * The above extra attributes have byte array type.
2499 return (get_extra_attr_from_object(object_p
,
2502 /* Key related boolean attributes */
2504 return (get_bool_attr_from_object(object_p
,
2505 SENSITIVE_BOOL_ON
, template));
2507 case CKA_SECONDARY_AUTH
:
2508 return (get_bool_attr_from_object(object_p
,
2509 SECONDARY_AUTH_BOOL_ON
, template));
2512 return (get_bool_attr_from_object(object_p
,
2513 DECRYPT_BOOL_ON
, template));
2516 return (get_bool_attr_from_object(object_p
,
2517 SIGN_BOOL_ON
, template));
2519 case CKA_SIGN_RECOVER
:
2520 return (get_bool_attr_from_object(object_p
,
2521 SIGN_RECOVER_BOOL_ON
, template));
2524 return (get_bool_attr_from_object(object_p
,
2525 UNWRAP_BOOL_ON
, template));
2527 case CKA_EXTRACTABLE
:
2528 return (get_bool_attr_from_object(object_p
,
2529 EXTRACTABLE_BOOL_ON
, template));
2531 case CKA_ALWAYS_SENSITIVE
:
2532 return (get_bool_attr_from_object(object_p
,
2533 ALWAYS_SENSITIVE_BOOL_ON
, template));
2535 case CKA_NEVER_EXTRACTABLE
:
2536 return (get_bool_attr_from_object(object_p
,
2537 NEVER_EXTRACTABLE_BOOL_ON
, template));
2540 if (keytype
== CKK_RSA
) {
2541 return (get_bigint_attr_from_object(
2542 OBJ_PRI_RSA_MOD(object_p
), template));
2544 template->ulValueLen
= (CK_ULONG
)-1;
2545 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2549 case CKA_PUBLIC_EXPONENT
:
2550 if (keytype
== CKK_RSA
) {
2551 return (get_bigint_attr_from_object(
2552 OBJ_PRI_RSA_PUBEXPO(object_p
), template));
2554 template->ulValueLen
= (CK_ULONG
)-1;
2555 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2559 case CKA_PRIVATE_EXPONENT
:
2560 if (keytype
== CKK_RSA
) {
2561 return (get_bigint_attr_from_object(
2562 OBJ_PRI_RSA_PRIEXPO(object_p
), template));
2564 template->ulValueLen
= (CK_ULONG
)-1;
2565 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2570 if (keytype
== CKK_RSA
) {
2571 return (get_bigint_attr_from_object(
2572 OBJ_PRI_RSA_PRIME1(object_p
), template));
2574 template->ulValueLen
= (CK_ULONG
)-1;
2575 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2580 if (keytype
== CKK_RSA
) {
2581 return (get_bigint_attr_from_object(
2582 OBJ_PRI_RSA_PRIME2(object_p
), template));
2584 template->ulValueLen
= (CK_ULONG
)-1;
2585 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2589 case CKA_EXPONENT_1
:
2590 if (keytype
== CKK_RSA
) {
2591 return (get_bigint_attr_from_object(
2592 OBJ_PRI_RSA_EXPO1(object_p
), template));
2594 template->ulValueLen
= (CK_ULONG
)-1;
2595 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2599 case CKA_EXPONENT_2
:
2600 if (keytype
== CKK_RSA
) {
2601 return (get_bigint_attr_from_object(
2602 OBJ_PRI_RSA_EXPO2(object_p
), template));
2604 template->ulValueLen
= (CK_ULONG
)-1;
2605 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2609 case CKA_COEFFICIENT
:
2610 if (keytype
== CKK_RSA
) {
2611 return (get_bigint_attr_from_object(
2612 OBJ_PRI_RSA_COEF(object_p
), template));
2614 template->ulValueLen
= (CK_ULONG
)-1;
2615 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2619 case CKA_VALUE_BITS
:
2620 if (keytype
== CKK_DH
) {
2621 return (get_ulong_attr_from_object(
2622 OBJ_PRI_DH_VAL_BITS(object_p
), template));
2624 template->ulValueLen
= (CK_ULONG
)-1;
2625 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2632 return (get_bigint_attr_from_object(
2633 OBJ_PRI_DSA_PRIME(object_p
), template));
2635 return (get_bigint_attr_from_object(
2636 OBJ_PRI_DH_PRIME(object_p
), template));
2638 template->ulValueLen
= (CK_ULONG
)-1;
2639 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2645 return (get_bigint_attr_from_object(
2646 OBJ_PRI_DSA_SUBPRIME(object_p
), template));
2648 template->ulValueLen
= (CK_ULONG
)-1;
2649 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2655 return (get_bigint_attr_from_object(
2656 OBJ_PRI_DSA_BASE(object_p
), template));
2658 return (get_bigint_attr_from_object(
2659 OBJ_PRI_DH_BASE(object_p
), template));
2661 template->ulValueLen
= (CK_ULONG
)-1;
2662 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2668 return (get_bigint_attr_from_object(
2669 OBJ_PRI_DSA_VALUE(object_p
), template));
2671 return (get_bigint_attr_from_object(
2672 OBJ_PRI_DH_VALUE(object_p
), template));
2674 return (get_bigint_attr_from_object(
2675 OBJ_PRI_EC_VALUE(object_p
), template));
2677 template->ulValueLen
= (CK_ULONG
)-1;
2678 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2683 * First, get the value of the request attribute defined
2684 * in the list of common key attributes. If the request
2685 * attribute is not found in that list, then get the
2686 * attribute from the list of common attributes.
2688 rv
= kernel_get_common_key_attrs(object_p
, template);
2689 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2690 rv
= kernel_get_common_attrs(object_p
, template);
2700 * Get the value of a requested attribute of a Secret Key Object.
2702 * Rule: All the attributes in the secret key object can be revealed
2703 * except those marked with footnote number "7" when the object
2704 * has its CKA_SENSITIVE attribute set to TRUE or its
2705 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
2708 kernel_get_secret_key_attribute(kernel_object_t
*object_p
,
2709 CK_ATTRIBUTE_PTR
template)
2713 CK_KEY_TYPE keytype
= object_p
->key_type
;
2715 switch (template->type
) {
2717 /* Key related boolean attributes */
2719 return (get_bool_attr_from_object(object_p
,
2720 SENSITIVE_BOOL_ON
, template));
2723 return (get_bool_attr_from_object(object_p
,
2724 ENCRYPT_BOOL_ON
, template));
2727 return (get_bool_attr_from_object(object_p
,
2728 DECRYPT_BOOL_ON
, template));
2731 return (get_bool_attr_from_object(object_p
,
2732 SIGN_BOOL_ON
, template));
2735 return (get_bool_attr_from_object(object_p
,
2736 VERIFY_BOOL_ON
, template));
2739 return (get_bool_attr_from_object(object_p
,
2740 WRAP_BOOL_ON
, template));
2743 return (get_bool_attr_from_object(object_p
,
2744 UNWRAP_BOOL_ON
, template));
2746 case CKA_EXTRACTABLE
:
2747 return (get_bool_attr_from_object(object_p
,
2748 EXTRACTABLE_BOOL_ON
, template));
2750 case CKA_ALWAYS_SENSITIVE
:
2751 return (get_bool_attr_from_object(object_p
,
2752 ALWAYS_SENSITIVE_BOOL_ON
, template));
2754 case CKA_NEVER_EXTRACTABLE
:
2755 return (get_bool_attr_from_object(object_p
,
2756 NEVER_EXTRACTABLE_BOOL_ON
, template));
2760 * If the specified attribute for the secret key object
2761 * cannot be revealed because the object is sensitive
2762 * or unextractable, then the ulValueLen is set to -1.
2764 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
2765 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
2766 template->ulValueLen
= (CK_ULONG
)-1;
2767 return (CKR_ATTRIBUTE_SENSITIVE
);
2772 case CKK_GENERIC_SECRET
:
2781 * Copy secret key object attributes to template.
2783 if (template->pValue
== NULL
) {
2784 template->ulValueLen
=
2785 OBJ_SEC_VALUE_LEN(object_p
);
2789 if (OBJ_SEC_VALUE(object_p
) == NULL
) {
2790 template->ulValueLen
= 0;
2794 if (template->ulValueLen
>=
2795 OBJ_SEC_VALUE_LEN(object_p
)) {
2796 (void) memcpy(template->pValue
,
2797 OBJ_SEC_VALUE(object_p
),
2798 OBJ_SEC_VALUE_LEN(object_p
));
2799 template->ulValueLen
=
2800 OBJ_SEC_VALUE_LEN(object_p
);
2803 template->ulValueLen
= (CK_ULONG
)-1;
2804 return (CKR_BUFFER_TOO_SMALL
);
2808 template->ulValueLen
= (CK_ULONG
)-1;
2809 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
2815 return (get_ulong_attr_from_object(OBJ_SEC_VALUE_LEN(object_p
),
2820 * First, get the value of the request attribute defined
2821 * in the list of common key attributes. If the request
2822 * attribute is not found in that list, then get the
2823 * attribute from the list of common attributes.
2825 rv
= kernel_get_common_key_attrs(object_p
, template);
2826 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
2827 rv
= kernel_get_common_attrs(object_p
, template);
2840 * Call the appropriate get attribute function according to the class
2843 * The caller of this function holds the lock on the object.
2846 kernel_get_attribute(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
2850 CK_OBJECT_CLASS
class = object_p
->class;
2854 case CKO_PUBLIC_KEY
:
2855 rv
= kernel_get_public_key_attribute(object_p
, template);
2858 case CKO_PRIVATE_KEY
:
2859 rv
= kernel_get_private_key_attribute(object_p
, template);
2862 case CKO_SECRET_KEY
:
2863 rv
= kernel_get_secret_key_attribute(object_p
, template);
2868 * If the specified attribute for the object is invalid
2869 * (the object does not possess such as attribute), then
2870 * the ulValueLen is modified to hold the value -1.
2872 template->ulValueLen
= (CK_ULONG
)-1;
2873 return (CKR_ATTRIBUTE_TYPE_INVALID
);
2881 * Set the value of an attribute that is common to all key objects
2882 * (i.e. public key, private key and secret key).
2885 kernel_set_common_key_attribute(kernel_object_t
*object_p
,
2886 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
2889 kernel_slot_t
*pslot
= slot_table
[sp
->ses_slotid
];
2892 switch (template->type
) {
2896 * Only the LABEL can be modified in the common storage
2897 * object attributes after the object is created.
2899 return (set_extra_attr_to_object(object_p
,
2900 CKA_LABEL
, template));
2903 return (set_extra_attr_to_object(object_p
,
2906 case CKA_START_DATE
:
2907 return (set_extra_attr_to_object(object_p
,
2908 CKA_START_DATE
, template));
2911 return (set_extra_attr_to_object(object_p
,
2912 CKA_END_DATE
, template));
2915 return (set_bool_attr_to_object(object_p
,
2916 DERIVE_BOOL_ON
, template));
2921 return (CKR_ATTRIBUTE_READ_ONLY
);
2925 /* called from C_SetAttributeValue() */
2926 return (CKR_ATTRIBUTE_READ_ONLY
);
2929 /* called from C_CopyObject() */
2930 if ((*(CK_BBOOL
*)template->pValue
) != B_TRUE
) {
2934 (void) pthread_mutex_lock(&pslot
->sl_mutex
);
2936 * Cannot create a private object if the token
2937 * has a keystore and the user isn't logged in.
2939 if (pslot
->sl_func_list
.fl_object_create
&&
2940 pslot
->sl_state
!= CKU_USER
) {
2941 rv
= CKR_USER_NOT_LOGGED_IN
;
2943 rv
= set_bool_attr_to_object(object_p
,
2944 PRIVATE_BOOL_ON
, template);
2946 (void) pthread_mutex_unlock(&pslot
->sl_mutex
);
2949 case CKA_MODIFIABLE
:
2951 rv
= set_bool_attr_to_object(object_p
,
2952 MODIFIABLE_BOOL_ON
, template);
2954 rv
= CKR_ATTRIBUTE_READ_ONLY
;
2959 return (CKR_TEMPLATE_INCONSISTENT
);
2966 * Set the value of an attribute of a Public Key Object.
2968 * Rule: The attributes marked with footnote number "8" in the PKCS11
2969 * spec may be modified (p.88 in PKCS11 spec.).
2972 kernel_set_public_key_attribute(kernel_object_t
*object_p
,
2973 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
2975 CK_KEY_TYPE keytype
= object_p
->key_type
;
2977 switch (template->type
) {
2980 return (set_extra_attr_to_object(object_p
,
2981 CKA_SUBJECT
, template));
2984 return (set_bool_attr_to_object(object_p
,
2985 ENCRYPT_BOOL_ON
, template));
2988 return (set_bool_attr_to_object(object_p
,
2989 VERIFY_BOOL_ON
, template));
2991 case CKA_VERIFY_RECOVER
:
2992 return (set_bool_attr_to_object(object_p
,
2993 VERIFY_RECOVER_BOOL_ON
, template));
2996 return (set_bool_attr_to_object(object_p
,
2997 WRAP_BOOL_ON
, template));
3000 case CKA_MODULUS_BITS
:
3001 case CKA_PUBLIC_EXPONENT
:
3002 if (keytype
== CKK_RSA
)
3003 return (CKR_ATTRIBUTE_READ_ONLY
);
3010 if (keytype
== CKK_DSA
)
3011 return (CKR_ATTRIBUTE_READ_ONLY
);
3016 * Set the value of a common key attribute.
3018 return (kernel_set_common_key_attribute(object_p
,
3019 template, copy
, sp
));
3024 * If we got this far, then the combination of key type
3025 * and requested attribute is invalid.
3027 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3032 * Set the value of an attribute of a Private Key Object.
3034 * Rule: The attributes marked with footnote number "8" in the PKCS11
3035 * spec may be modified (p.88 in PKCS11 spec.).
3038 kernel_set_private_key_attribute(kernel_object_t
*object_p
,
3039 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
3041 CK_KEY_TYPE keytype
= object_p
->key_type
;
3043 switch (template->type
) {
3046 return (set_extra_attr_to_object(object_p
,
3047 CKA_SUBJECT
, template));
3051 * Cannot set SENSITIVE to FALSE if it is already ON.
3053 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
3054 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
3055 return (CKR_ATTRIBUTE_READ_ONLY
);
3058 if (*(CK_BBOOL
*)template->pValue
)
3059 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
3063 return (set_bool_attr_to_object(object_p
,
3064 DECRYPT_BOOL_ON
, template));
3067 return (set_bool_attr_to_object(object_p
,
3068 SIGN_BOOL_ON
, template));
3070 case CKA_SIGN_RECOVER
:
3071 return (set_bool_attr_to_object(object_p
,
3072 SIGN_RECOVER_BOOL_ON
, template));
3075 return (set_bool_attr_to_object(object_p
,
3076 UNWRAP_BOOL_ON
, template));
3078 case CKA_EXTRACTABLE
:
3080 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
3082 if ((*(CK_BBOOL
*)template->pValue
) &&
3083 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
3084 return (CKR_ATTRIBUTE_READ_ONLY
);
3087 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
3088 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
3092 case CKA_PUBLIC_EXPONENT
:
3093 case CKA_PRIVATE_EXPONENT
:
3096 case CKA_EXPONENT_1
:
3097 case CKA_EXPONENT_2
:
3098 case CKA_COEFFICIENT
:
3099 if (keytype
== CKK_RSA
) {
3100 return (CKR_ATTRIBUTE_READ_ONLY
);
3108 if (keytype
== CKK_DSA
)
3109 return (CKR_ATTRIBUTE_READ_ONLY
);
3114 * Set the value of a common key attribute.
3116 return (kernel_set_common_key_attribute(object_p
,
3117 template, copy
, sp
));
3121 * If we got this far, then the combination of key type
3122 * and requested attribute is invalid.
3124 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3130 * Set the value of an attribute of a Secret Key Object.
3132 * Rule: The attributes marked with footnote number "8" in the PKCS11
3133 * spec may be modified (p.88 in PKCS11 spec.).
3136 kernel_set_secret_key_attribute(kernel_object_t
*object_p
,
3137 CK_ATTRIBUTE_PTR
template, boolean_t copy
, kernel_session_t
*sp
)
3139 CK_KEY_TYPE keytype
= object_p
->key_type
;
3141 switch (template->type
) {
3145 * Cannot set SENSITIVE to FALSE if it is already ON.
3147 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
3148 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
3149 return (CKR_ATTRIBUTE_READ_ONLY
);
3152 if (*(CK_BBOOL
*)template->pValue
)
3153 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
3157 return (set_bool_attr_to_object(object_p
,
3158 ENCRYPT_BOOL_ON
, template));
3161 return (set_bool_attr_to_object(object_p
,
3162 DECRYPT_BOOL_ON
, template));
3165 return (set_bool_attr_to_object(object_p
,
3166 SIGN_BOOL_ON
, template));
3169 return (set_bool_attr_to_object(object_p
,
3170 VERIFY_BOOL_ON
, template));
3173 return (set_bool_attr_to_object(object_p
,
3174 WRAP_BOOL_ON
, template));
3177 return (set_bool_attr_to_object(object_p
,
3178 UNWRAP_BOOL_ON
, template));
3180 case CKA_EXTRACTABLE
:
3182 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
3184 if ((*(CK_BBOOL
*)template->pValue
) &&
3185 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
3186 return (CKR_ATTRIBUTE_READ_ONLY
);
3189 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
3190 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
3194 return (CKR_ATTRIBUTE_READ_ONLY
);
3197 if ((keytype
== CKK_RC4
) ||
3198 (keytype
== CKK_GENERIC_SECRET
) ||
3199 (keytype
== CKK_AES
) ||
3200 (keytype
== CKK_BLOWFISH
))
3201 return (CKR_ATTRIBUTE_READ_ONLY
);
3206 * Set the value of a common key attribute.
3208 return (kernel_set_common_key_attribute(object_p
,
3209 template, copy
, sp
));
3213 * If we got this far, then the combination of key type
3214 * and requested attribute is invalid.
3216 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3221 * Call the appropriate set attribute function according to the class
3224 * The caller of this function does not hold the lock on the original
3225 * object, since this function is setting the attribute on the new object
3226 * that is being modified.
3230 kernel_set_attribute(kernel_object_t
*object_p
, CK_ATTRIBUTE_PTR
template,
3231 boolean_t copy
, kernel_session_t
*sp
)
3235 CK_OBJECT_CLASS
class = object_p
->class;
3239 case CKO_PUBLIC_KEY
:
3240 rv
= kernel_set_public_key_attribute(object_p
, template,
3244 case CKO_PRIVATE_KEY
:
3245 rv
= kernel_set_private_key_attribute(object_p
, template,
3249 case CKO_SECRET_KEY
:
3250 rv
= kernel_set_secret_key_attribute(object_p
, template,
3256 * If the template specifies a value of an attribute
3257 * which is incompatible with other existing attributes
3258 * of the object, then fails with return code
3259 * CKR_TEMPLATE_INCONSISTENT.
3261 rv
= CKR_TEMPLATE_INCONSISTENT
;
3270 copy_bigint(biginteger_t
*new_bigint
, biginteger_t
*old_bigint
)
3272 new_bigint
->big_value
=
3273 malloc((sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
3275 if (new_bigint
->big_value
== NULL
) {
3276 return (CKR_HOST_MEMORY
);
3279 (void) memcpy(new_bigint
->big_value
, old_bigint
->big_value
,
3280 (sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
3286 free_public_key_attr(public_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
3294 bigint_attr_cleanup(KEY_PUB_RSA_MOD(pbk
));
3295 bigint_attr_cleanup(KEY_PUB_RSA_PUBEXPO(pbk
));
3298 bigint_attr_cleanup(KEY_PUB_DSA_PRIME(pbk
));
3299 bigint_attr_cleanup(KEY_PUB_DSA_SUBPRIME(pbk
));
3300 bigint_attr_cleanup(KEY_PUB_DSA_BASE(pbk
));
3301 bigint_attr_cleanup(KEY_PUB_DSA_VALUE(pbk
));
3311 kernel_copy_public_key_attr(public_key_obj_t
*old_pub_key_obj_p
,
3312 public_key_obj_t
**new_pub_key_obj_p
, CK_KEY_TYPE key_type
)
3315 public_key_obj_t
*pbk
;
3318 pbk
= calloc(1, sizeof (public_key_obj_t
));
3320 return (CKR_HOST_MEMORY
);
3325 (void) memcpy(KEY_PUB_RSA(pbk
),
3326 KEY_PUB_RSA(old_pub_key_obj_p
),
3327 sizeof (rsa_pub_key_t
));
3329 rv
= copy_bigint(KEY_PUB_RSA_MOD(pbk
),
3330 KEY_PUB_RSA_MOD(old_pub_key_obj_p
));
3332 free_public_key_attr(pbk
, key_type
);
3335 /* copy public exponent */
3336 rv
= copy_bigint(KEY_PUB_RSA_PUBEXPO(pbk
),
3337 KEY_PUB_RSA_PUBEXPO(old_pub_key_obj_p
));
3339 free_public_key_attr(pbk
, key_type
);
3344 (void) memcpy(KEY_PUB_DSA(pbk
),
3345 KEY_PUB_DSA(old_pub_key_obj_p
),
3346 sizeof (dsa_pub_key_t
));
3349 rv
= copy_bigint(KEY_PUB_DSA_PRIME(pbk
),
3350 KEY_PUB_DSA_PRIME(old_pub_key_obj_p
));
3352 free_public_key_attr(pbk
, key_type
);
3357 rv
= copy_bigint(KEY_PUB_DSA_SUBPRIME(pbk
),
3358 KEY_PUB_DSA_SUBPRIME(old_pub_key_obj_p
));
3360 free_public_key_attr(pbk
, key_type
);
3365 rv
= copy_bigint(KEY_PUB_DSA_BASE(pbk
),
3366 KEY_PUB_DSA_BASE(old_pub_key_obj_p
));
3368 free_public_key_attr(pbk
, key_type
);
3373 rv
= copy_bigint(KEY_PUB_DSA_VALUE(pbk
),
3374 KEY_PUB_DSA_VALUE(old_pub_key_obj_p
));
3376 free_public_key_attr(pbk
, key_type
);
3383 *new_pub_key_obj_p
= pbk
;
3389 free_private_key_attr(private_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
3397 bigint_attr_cleanup(KEY_PRI_RSA_MOD(pbk
));
3398 bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(pbk
));
3399 bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(pbk
));
3400 bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(pbk
));
3401 bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(pbk
));
3402 bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(pbk
));
3403 bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(pbk
));
3404 bigint_attr_cleanup(KEY_PRI_RSA_COEF(pbk
));
3407 bigint_attr_cleanup(KEY_PRI_DSA_PRIME(pbk
));
3408 bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(pbk
));
3409 bigint_attr_cleanup(KEY_PRI_DSA_BASE(pbk
));
3410 bigint_attr_cleanup(KEY_PRI_DSA_VALUE(pbk
));
3419 kernel_copy_private_key_attr(private_key_obj_t
*old_pri_key_obj_p
,
3420 private_key_obj_t
**new_pri_key_obj_p
, CK_KEY_TYPE key_type
)
3423 private_key_obj_t
*pbk
;
3425 pbk
= calloc(1, sizeof (private_key_obj_t
));
3427 return (CKR_HOST_MEMORY
);
3432 (void) memcpy(KEY_PRI_RSA(pbk
),
3433 KEY_PRI_RSA(old_pri_key_obj_p
),
3434 sizeof (rsa_pri_key_t
));
3436 rv
= copy_bigint(KEY_PRI_RSA_MOD(pbk
),
3437 KEY_PRI_RSA_MOD(old_pri_key_obj_p
));
3439 free_private_key_attr(pbk
, key_type
);
3442 /* copy public exponent */
3443 rv
= copy_bigint(KEY_PRI_RSA_PUBEXPO(pbk
),
3444 KEY_PRI_RSA_PUBEXPO(old_pri_key_obj_p
));
3446 free_private_key_attr(pbk
, key_type
);
3449 /* copy private exponent */
3450 rv
= copy_bigint(KEY_PRI_RSA_PRIEXPO(pbk
),
3451 KEY_PRI_RSA_PRIEXPO(old_pri_key_obj_p
));
3453 free_private_key_attr(pbk
, key_type
);
3457 rv
= copy_bigint(KEY_PRI_RSA_PRIME1(pbk
),
3458 KEY_PRI_RSA_PRIME1(old_pri_key_obj_p
));
3460 free_private_key_attr(pbk
, key_type
);
3464 rv
= copy_bigint(KEY_PRI_RSA_PRIME2(pbk
),
3465 KEY_PRI_RSA_PRIME2(old_pri_key_obj_p
));
3467 free_private_key_attr(pbk
, key_type
);
3470 /* copy exponent_1 */
3471 rv
= copy_bigint(KEY_PRI_RSA_EXPO1(pbk
),
3472 KEY_PRI_RSA_EXPO1(old_pri_key_obj_p
));
3474 free_private_key_attr(pbk
, key_type
);
3477 /* copy exponent_2 */
3478 rv
= copy_bigint(KEY_PRI_RSA_EXPO2(pbk
),
3479 KEY_PRI_RSA_EXPO2(old_pri_key_obj_p
));
3481 free_private_key_attr(pbk
, key_type
);
3484 /* copy coefficient */
3485 rv
= copy_bigint(KEY_PRI_RSA_COEF(pbk
),
3486 KEY_PRI_RSA_COEF(old_pri_key_obj_p
));
3488 free_private_key_attr(pbk
, key_type
);
3493 (void) memcpy(KEY_PRI_DSA(pbk
),
3494 KEY_PRI_DSA(old_pri_key_obj_p
),
3495 sizeof (dsa_pri_key_t
));
3498 rv
= copy_bigint(KEY_PRI_DSA_PRIME(pbk
),
3499 KEY_PRI_DSA_PRIME(old_pri_key_obj_p
));
3501 free_private_key_attr(pbk
, key_type
);
3506 rv
= copy_bigint(KEY_PRI_DSA_SUBPRIME(pbk
),
3507 KEY_PRI_DSA_SUBPRIME(old_pri_key_obj_p
));
3509 free_private_key_attr(pbk
, key_type
);
3514 rv
= copy_bigint(KEY_PRI_DSA_BASE(pbk
),
3515 KEY_PRI_DSA_BASE(old_pri_key_obj_p
));
3517 free_private_key_attr(pbk
, key_type
);
3522 rv
= copy_bigint(KEY_PRI_DSA_VALUE(pbk
),
3523 KEY_PRI_DSA_VALUE(old_pri_key_obj_p
));
3525 free_private_key_attr(pbk
, key_type
);
3532 *new_pri_key_obj_p
= pbk
;
3538 kernel_copy_secret_key_attr(secret_key_obj_t
*old_secret_key_obj_p
,
3539 secret_key_obj_t
**new_secret_key_obj_p
)
3541 secret_key_obj_t
*sk
;
3543 sk
= malloc(sizeof (secret_key_obj_t
));
3545 return (CKR_HOST_MEMORY
);
3547 (void) memcpy(sk
, old_secret_key_obj_p
, sizeof (secret_key_obj_t
));
3549 /* copy the secret key value */
3550 sk
->sk_value
= malloc((sizeof (CK_BYTE
) * sk
->sk_value_len
));
3551 if (sk
->sk_value
== NULL
) {
3553 return (CKR_HOST_MEMORY
);
3555 (void) memcpy(sk
->sk_value
, old_secret_key_obj_p
->sk_value
,
3556 (sizeof (CK_BYTE
) * sk
->sk_value_len
));
3558 *new_secret_key_obj_p
= sk
;
3566 * If CKA_CLASS not given, guess CKA_CLASS using
3567 * attributes on template .
3569 * Some attributes are specific to an object class. If one or more
3570 * of these attributes are in the template, make a list of classes
3571 * that can have these attributes. This would speed up the search later,
3572 * because we can immediately skip an object if the class of that
3573 * object can not possibly contain one of the attributes.
3577 kernel_process_find_attr(CK_OBJECT_CLASS
*pclasses
,
3578 CK_ULONG
*num_result_pclasses
, CK_ATTRIBUTE_PTR pTemplate
,
3583 boolean_t pub_found
= B_FALSE
,
3584 priv_found
= B_FALSE
,
3585 secret_found
= B_FALSE
,
3586 domain_found
= B_FALSE
,
3587 hardware_found
= B_FALSE
,
3588 cert_found
= B_FALSE
;
3589 int num_pub_key_attrs
, num_priv_key_attrs
,
3590 num_secret_key_attrs
, num_domain_attrs
,
3591 num_hardware_attrs
, num_cert_attrs
;
3592 int num_pclasses
= 0;
3594 for (i
= 0; i
< ulCount
; i
++) {
3595 if (pTemplate
[i
].type
== CKA_CLASS
) {
3597 * don't need to guess the class, it is specified.
3598 * Just record the class, and return.
3601 (*((CK_OBJECT_CLASS
*)pTemplate
[i
].pValue
));
3602 *num_result_pclasses
= 1;
3608 sizeof (PUB_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3609 num_priv_key_attrs
=
3610 sizeof (PRIV_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3611 num_secret_key_attrs
=
3612 sizeof (SECRET_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3614 sizeof (DOMAIN_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3615 num_hardware_attrs
=
3616 sizeof (HARDWARE_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3618 sizeof (CERT_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
3621 * Get the list of objects class that might contain
3624 for (i
= 0; i
< ulCount
; i
++) {
3626 * only check if this attribute can belong to public key object
3627 * class if public key object isn't already in the list
3630 for (j
= 0; j
< num_pub_key_attrs
; j
++) {
3631 if (pTemplate
[i
].type
== PUB_KEY_ATTRS
[j
]) {
3633 pclasses
[num_pclasses
++] =
3641 for (j
= 0; j
< num_priv_key_attrs
; j
++) {
3642 if (pTemplate
[i
].type
== PRIV_KEY_ATTRS
[j
]) {
3643 priv_found
= B_TRUE
;
3644 pclasses
[num_pclasses
++] =
3651 if (!secret_found
) {
3652 for (j
= 0; j
< num_secret_key_attrs
; j
++) {
3653 if (pTemplate
[i
].type
== SECRET_KEY_ATTRS
[j
]) {
3654 secret_found
= B_TRUE
;
3655 pclasses
[num_pclasses
++] =
3662 if (!domain_found
) {
3663 for (j
= 0; j
< num_domain_attrs
; j
++) {
3664 if (pTemplate
[i
].type
== DOMAIN_ATTRS
[j
]) {
3665 domain_found
= B_TRUE
;
3666 pclasses
[num_pclasses
++] =
3667 CKO_DOMAIN_PARAMETERS
;
3673 if (!hardware_found
) {
3674 for (j
= 0; j
< num_hardware_attrs
; j
++) {
3675 if (pTemplate
[i
].type
== HARDWARE_ATTRS
[j
]) {
3676 hardware_found
= B_TRUE
;
3677 pclasses
[num_pclasses
++] =
3685 for (j
= 0; j
< num_cert_attrs
; j
++) {
3686 if (pTemplate
[i
].type
== CERT_ATTRS
[j
]) {
3687 cert_found
= B_TRUE
;
3688 pclasses
[num_pclasses
++] =
3695 *num_result_pclasses
= num_pclasses
;
3700 kernel_find_match_attrs(kernel_object_t
*obj
, CK_OBJECT_CLASS
*pclasses
,
3701 CK_ULONG num_pclasses
, CK_ATTRIBUTE
*template, CK_ULONG num_attr
)
3704 CK_ATTRIBUTE
*tmpl_attr
, *obj_attr
;
3706 biginteger_t
*bigint
;
3707 boolean_t compare_attr
, compare_bigint
, compare_boolean
;
3710 * Check if the class of this object match with any
3711 * of object classes that can possibly contain the
3712 * requested attributes.
3714 if (num_pclasses
> 0) {
3715 for (i
= 0; i
< num_pclasses
; i
++) {
3716 if (obj
->class == pclasses
[i
]) {
3720 if (i
== num_pclasses
) {
3722 * this object can't possibly contain one or
3723 * more attributes, don't need to check this object
3729 /* need to examine everything */
3730 for (i
= 0; i
< num_attr
; i
++) {
3731 tmpl_attr
= &(template[i
]);
3732 compare_attr
= B_FALSE
;
3733 compare_bigint
= B_FALSE
;
3734 compare_boolean
= B_FALSE
;
3735 switch (tmpl_attr
->type
) {
3736 /* First, check the most common attributes */
3738 if (*((CK_OBJECT_CLASS
*)tmpl_attr
->pValue
) !=
3744 if (*((CK_KEY_TYPE
*)tmpl_attr
->pValue
) !=
3750 attr_mask
= (obj
->bool_attr_mask
) & ENCRYPT_BOOL_ON
;
3751 compare_boolean
= B_TRUE
;
3754 attr_mask
= (obj
->bool_attr_mask
) & DECRYPT_BOOL_ON
;
3755 compare_boolean
= B_TRUE
;
3758 attr_mask
= (obj
->bool_attr_mask
) & WRAP_BOOL_ON
;
3759 compare_boolean
= B_TRUE
;
3762 attr_mask
= (obj
->bool_attr_mask
) & UNWRAP_BOOL_ON
;
3763 compare_boolean
= B_TRUE
;
3766 attr_mask
= (obj
->bool_attr_mask
) & SIGN_BOOL_ON
;
3767 compare_boolean
= B_TRUE
;
3769 case CKA_SIGN_RECOVER
:
3770 attr_mask
= (obj
->bool_attr_mask
) &
3771 SIGN_RECOVER_BOOL_ON
;
3772 compare_boolean
= B_TRUE
;
3775 attr_mask
= (obj
->bool_attr_mask
) & VERIFY_BOOL_ON
;
3776 compare_boolean
= B_TRUE
;
3778 case CKA_VERIFY_RECOVER
:
3779 attr_mask
= (obj
->bool_attr_mask
) &
3780 VERIFY_RECOVER_BOOL_ON
;
3781 compare_boolean
= B_TRUE
;
3784 attr_mask
= (obj
->bool_attr_mask
) & DERIVE_BOOL_ON
;
3785 compare_boolean
= B_TRUE
;
3788 attr_mask
= (obj
->bool_attr_mask
) & LOCAL_BOOL_ON
;
3789 compare_boolean
= B_TRUE
;
3792 attr_mask
= (obj
->bool_attr_mask
) & SENSITIVE_BOOL_ON
;
3793 compare_boolean
= B_TRUE
;
3795 case CKA_SECONDARY_AUTH
:
3796 attr_mask
= (obj
->bool_attr_mask
) &
3797 SECONDARY_AUTH_BOOL_ON
;
3798 compare_boolean
= B_TRUE
;
3801 attr_mask
= (obj
->bool_attr_mask
) & TRUSTED_BOOL_ON
;
3802 compare_boolean
= B_TRUE
;
3804 case CKA_EXTRACTABLE
:
3805 attr_mask
= (obj
->bool_attr_mask
) &
3806 EXTRACTABLE_BOOL_ON
;
3807 compare_boolean
= B_TRUE
;
3809 case CKA_ALWAYS_SENSITIVE
:
3810 attr_mask
= (obj
->bool_attr_mask
) &
3811 ALWAYS_SENSITIVE_BOOL_ON
;
3812 compare_boolean
= B_TRUE
;
3814 case CKA_NEVER_EXTRACTABLE
:
3815 attr_mask
= (obj
->bool_attr_mask
) &
3816 NEVER_EXTRACTABLE_BOOL_ON
;
3817 compare_boolean
= B_TRUE
;
3821 * CKA_TOKEN value is not applicable to an object
3822 * created in the library, it should only contain
3823 * the default value FALSE
3826 compare_boolean
= B_TRUE
;
3829 attr_mask
= (obj
->bool_attr_mask
) & PRIVATE_BOOL_ON
;
3830 compare_boolean
= B_TRUE
;
3832 case CKA_MODIFIABLE
:
3833 attr_mask
= (obj
->bool_attr_mask
) & MODIFIABLE_BOOL_ON
;
3834 compare_boolean
= B_TRUE
;
3838 case CKA_START_DATE
:
3840 case CKA_KEY_GEN_MECHANISM
:
3842 /* find these attributes from extra_attrlistp */
3843 obj_attr
= get_extra_attr(tmpl_attr
->type
, obj
);
3844 compare_attr
= B_TRUE
;
3847 /* only secret key has this attribute */
3848 if (obj
->class == CKO_SECRET_KEY
) {
3849 if (*((CK_ULONG
*)tmpl_attr
->pValue
) !=
3850 OBJ_SEC_VALUE_LEN(obj
)) {
3858 switch (obj
->class) {
3859 case CKO_SECRET_KEY
:
3861 * secret_key_obj_t is the same as
3864 bigint
= (biginteger_t
*)OBJ_SEC(obj
);
3866 case CKO_PRIVATE_KEY
:
3867 if (obj
->key_type
== CKK_DSA
) {
3868 bigint
= OBJ_PRI_DSA_VALUE(obj
);
3873 case CKO_PUBLIC_KEY
:
3874 if (obj
->key_type
== CKK_DSA
) {
3875 bigint
= OBJ_PUB_DSA_VALUE(obj
);
3883 compare_bigint
= B_TRUE
;
3886 /* only RSA public and private key have this attr */
3887 if (obj
->key_type
== CKK_RSA
) {
3888 if (obj
->class == CKO_PUBLIC_KEY
) {
3889 bigint
= OBJ_PUB_RSA_MOD(obj
);
3890 } else if (obj
->class == CKO_PRIVATE_KEY
) {
3891 bigint
= OBJ_PRI_RSA_MOD(obj
);
3895 compare_bigint
= B_TRUE
;
3900 case CKA_MODULUS_BITS
:
3901 /* only RSA public key has this attribute */
3902 if ((obj
->key_type
== CKK_RSA
) &&
3903 (obj
->class == CKO_PUBLIC_KEY
)) {
3904 CK_ULONG mod_bits
= OBJ_PUB_RSA_MOD_BITS(obj
);
3906 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
3913 case CKA_PUBLIC_EXPONENT
:
3914 /* only RSA public and private key have this attr */
3915 if (obj
->key_type
== CKK_RSA
) {
3916 if (obj
->class == CKO_PUBLIC_KEY
) {
3917 bigint
= OBJ_PUB_RSA_PUBEXPO(obj
);
3918 } else if (obj
->class == CKO_PRIVATE_KEY
) {
3919 bigint
= OBJ_PRI_RSA_PUBEXPO(obj
);
3923 compare_bigint
= B_TRUE
;
3928 case CKA_PRIVATE_EXPONENT
:
3929 /* only RSA private key has this attribute */
3930 if ((obj
->key_type
== CKK_RSA
) &&
3931 (obj
->class == CKO_PRIVATE_KEY
)) {
3932 bigint
= OBJ_PRI_RSA_PRIEXPO(obj
);
3933 compare_bigint
= B_TRUE
;
3939 /* only RSA private key has this attribute */
3940 if ((obj
->key_type
== CKK_RSA
) &&
3941 (obj
->class == CKO_PRIVATE_KEY
)) {
3942 bigint
= OBJ_PRI_RSA_PRIME1(obj
);
3943 compare_bigint
= B_TRUE
;
3949 /* only RSA private key has this attribute */
3950 if ((obj
->key_type
== CKK_RSA
) &&
3951 (obj
->class == CKO_PRIVATE_KEY
)) {
3952 bigint
= OBJ_PRI_RSA_PRIME2(obj
);
3953 compare_bigint
= B_TRUE
;
3958 case CKA_EXPONENT_1
:
3959 /* only RSA private key has this attribute */
3960 if ((obj
->key_type
== CKK_RSA
) &&
3961 (obj
->class == CKO_PRIVATE_KEY
)) {
3962 bigint
= OBJ_PRI_RSA_EXPO1(obj
);
3963 compare_bigint
= B_TRUE
;
3968 case CKA_EXPONENT_2
:
3969 /* only RSA private key has this attribute */
3970 if ((obj
->key_type
== CKK_RSA
) &&
3971 (obj
->class == CKO_PRIVATE_KEY
)) {
3972 bigint
= OBJ_PRI_RSA_EXPO2(obj
);
3973 compare_bigint
= B_TRUE
;
3978 case CKA_COEFFICIENT
:
3979 /* only RSA private key has this attribute */
3980 if ((obj
->key_type
== CKK_RSA
) &&
3981 (obj
->class == CKO_PRIVATE_KEY
)) {
3982 bigint
= OBJ_PRI_RSA_COEF(obj
);
3983 compare_bigint
= B_TRUE
;
3988 case CKA_VALUE_BITS
:
3991 if (obj
->class == CKO_PUBLIC_KEY
) {
3992 switch (obj
->key_type
) {
3994 bigint
= OBJ_PUB_DSA_PRIME(obj
);
3999 } else if (obj
->class == CKO_PRIVATE_KEY
) {
4000 switch (obj
->key_type
) {
4002 bigint
= OBJ_PRI_DSA_PRIME(obj
);
4010 compare_bigint
= B_TRUE
;
4013 if (obj
->class == CKO_PUBLIC_KEY
) {
4014 switch (obj
->key_type
) {
4016 bigint
= OBJ_PUB_DSA_SUBPRIME(obj
);
4021 } else if (obj
->class == CKO_PRIVATE_KEY
) {
4022 switch (obj
->key_type
) {
4024 bigint
= OBJ_PRI_DSA_SUBPRIME(obj
);
4032 compare_bigint
= B_TRUE
;
4035 if (obj
->class == CKO_PUBLIC_KEY
) {
4036 switch (obj
->key_type
) {
4038 bigint
= OBJ_PUB_DSA_BASE(obj
);
4043 } else if (obj
->class == CKO_PRIVATE_KEY
) {
4044 switch (obj
->key_type
) {
4046 bigint
= OBJ_PRI_DSA_BASE(obj
);
4054 compare_bigint
= B_TRUE
;
4056 case CKA_PRIME_BITS
:
4058 case CKA_SUBPRIME_BITS
:
4062 * any other attributes are currently not supported.
4063 * so, it's not possible for them to be in the
4068 if (compare_boolean
) {
4076 if (bval
!= *((CK_BBOOL
*)tmpl_attr
->pValue
)) {
4079 } else if (compare_bigint
) {
4080 if (bigint
== NULL
) {
4083 if (tmpl_attr
->ulValueLen
!= bigint
->big_value_len
) {
4086 if (memcmp(tmpl_attr
->pValue
, bigint
->big_value
,
4087 tmpl_attr
->ulValueLen
) != 0) {
4090 } else if (compare_attr
) {
4091 if (obj_attr
== NULL
) {
4093 * The attribute type is valid, and its value
4094 * has not been initialized in the object. In
4095 * this case, it only matches the template's
4096 * attribute if the template's value length
4099 if (tmpl_attr
->ulValueLen
!= 0)
4102 if (tmpl_attr
->ulValueLen
!=
4103 obj_attr
->ulValueLen
) {
4106 if (memcmp(tmpl_attr
->pValue
, obj_attr
->pValue
,
4107 tmpl_attr
->ulValueLen
) != 0) {
4117 get_extra_attr(CK_ATTRIBUTE_TYPE type
, kernel_object_t
*obj
)
4119 CK_ATTRIBUTE_INFO_PTR tmp
;
4121 tmp
= obj
->extra_attrlistp
;
4122 while (tmp
!= NULL
) {
4123 if (tmp
->attr
.type
== type
) {
4124 return (&(tmp
->attr
));
4128 /* if get there, the specified attribute is not found */