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 2009 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>
33 #include <blowfish_impl.h>
37 #include "softGlobal.h"
38 #include "softObject.h"
39 #include "softSession.h"
40 #include "softKeystore.h"
41 #include "softKeystoreUtil.h"
42 #include "softCrypt.h"
46 * This attribute table is used by the soft_lookup_attr()
47 * to validate the attributes.
49 CK_ATTRIBUTE_TYPE attr_map
[] = {
79 CKA_NEVER_EXTRACTABLE
,
93 * attributes that exists only in public key objects
94 * Note: some attributes may also exist in one or two
95 * other object classes, but they are also listed
96 * because not all object have them.
98 CK_ATTRIBUTE_TYPE PUB_KEY_ATTRS
[] =
118 * attributes that exists only in private key objects
119 * Note: some attributes may also exist in one or two
120 * other object classes, but they are also listed
121 * because not all object have them.
123 CK_ATTRIBUTE_TYPE PRIV_KEY_ATTRS
[] =
131 CKA_PRIVATE_EXPONENT
,
144 CKA_NEVER_EXTRACTABLE
,
145 CKA_ALWAYS_SENSITIVE
,
150 * attributes that exists only in secret key objects
151 * Note: some attributes may also exist in one or two
152 * other object classes, but they are also listed
153 * because not all object have them.
155 CK_ATTRIBUTE_TYPE SECRET_KEY_ATTRS
[] =
166 CKA_NEVER_EXTRACTABLE
,
171 * attributes that exists only in domain parameter objects
172 * Note: some attributes may also exist in one or two
173 * other object classes, but they are also listed
174 * because not all object have them.
176 CK_ATTRIBUTE_TYPE DOMAIN_ATTRS
[] =
187 * attributes that exists only in hardware feature objects
190 CK_ATTRIBUTE_TYPE HARDWARE_ATTRS
[] =
198 * attributes that exists only in certificate objects
200 CK_ATTRIBUTE_TYPE CERT_ATTRS
[] =
202 CKA_CERTIFICATE_TYPE
,
215 * Validate the attribute by using binary search algorithm.
218 soft_lookup_attr(CK_ATTRIBUTE_TYPE type
)
221 size_t lower
, middle
, upper
;
224 upper
= (sizeof (attr_map
) / sizeof (CK_ATTRIBUTE_TYPE
)) - 1;
226 while (lower
<= upper
) {
227 /* Always starts from middle. */
228 middle
= (lower
+ upper
) / 2;
230 if (type
> attr_map
[middle
]) {
231 /* Adjust the lower bound to upper half. */
236 if (type
== attr_map
[middle
]) {
241 if (type
< attr_map
[middle
]) {
242 /* Adjust the upper bound to lower half. */
248 /* Failed to find the matching attribute from the attribute table. */
249 return (CKR_ATTRIBUTE_TYPE_INVALID
);
254 * Validate the attribute by using the following search algorithm:
256 * 1) Search for the most frequently used attributes first.
257 * 2) If not found, search for the usage-purpose attributes - these
258 * attributes have dense set of values, therefore compiler will
259 * optimize it with a branch table and branch to the appropriate
261 * 3) If still not found, use binary search for the rest of the
262 * attributes in the attr_map[] table.
265 soft_validate_attr(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
266 CK_OBJECT_CLASS
*class)
272 for (i
= 0; i
< ulAttrNum
; i
++) {
273 /* First tier search */
274 switch (template[i
].type
) {
276 *class = *((CK_OBJECT_CLASS
*)template[i
].pValue
);
289 /* Second tier search */
290 switch (template[i
].type
) {
301 case CKA_SIGN_RECOVER
:
305 case CKA_VERIFY_RECOVER
:
310 /* Third tier search */
311 rv
= soft_lookup_attr(template[i
].type
);
323 cleanup_cert_attr(cert_attr_t
*attr
)
327 (void) memset(attr
->value
, 0, attr
->length
);
336 copy_cert_attr(cert_attr_t
*src_attr
, cert_attr_t
**dest_attr
)
340 if (src_attr
== NULL
|| dest_attr
== NULL
)
341 return (CKR_HOST_MEMORY
);
343 if (src_attr
->value
== NULL
)
344 return (CKR_HOST_MEMORY
);
346 /* free memory if its already allocated */
347 if (*dest_attr
!= NULL
) {
348 if ((*dest_attr
)->value
!= (CK_BYTE
*)NULL
)
349 free((*dest_attr
)->value
);
351 *dest_attr
= malloc(sizeof (cert_attr_t
));
352 if (*dest_attr
== NULL
)
353 return (CKR_HOST_MEMORY
);
356 (*dest_attr
)->value
= NULL
;
357 (*dest_attr
)->length
= 0;
359 if (src_attr
->length
) {
360 (*dest_attr
)->value
= malloc(src_attr
->length
);
361 if ((*dest_attr
)->value
== NULL
) {
363 return (CKR_HOST_MEMORY
);
366 (void) memcpy((*dest_attr
)->value
, src_attr
->value
,
368 (*dest_attr
)->length
= src_attr
->length
;
375 soft_cleanup_cert_object(soft_object_t
*object_p
)
377 CK_CERTIFICATE_TYPE certtype
= object_p
->cert_type
;
379 if (object_p
->class != CKO_CERTIFICATE
||
380 OBJ_CERT(object_p
) == NULL
)
383 if (certtype
== CKC_X_509
) {
384 if (X509_CERT_SUBJECT(object_p
) != NULL
) {
385 cleanup_cert_attr(X509_CERT_SUBJECT(object_p
));
386 free(X509_CERT_SUBJECT(object_p
));
387 X509_CERT_SUBJECT(object_p
) = NULL
;
389 if (X509_CERT_VALUE(object_p
) != NULL
) {
390 cleanup_cert_attr(X509_CERT_VALUE(object_p
));
391 free(X509_CERT_VALUE(object_p
));
392 X509_CERT_VALUE(object_p
) = NULL
;
394 free(OBJ_CERT(object_p
));
395 } else if (certtype
== CKC_X_509_ATTR_CERT
) {
396 if (X509_ATTR_CERT_VALUE(object_p
) != NULL
) {
397 cleanup_cert_attr(X509_ATTR_CERT_VALUE(object_p
));
398 free(X509_ATTR_CERT_VALUE(object_p
));
399 X509_ATTR_CERT_VALUE(object_p
) = NULL
;
401 if (X509_ATTR_CERT_OWNER(object_p
) != NULL
) {
402 cleanup_cert_attr(X509_ATTR_CERT_OWNER(object_p
));
403 free(X509_ATTR_CERT_OWNER(object_p
));
404 X509_ATTR_CERT_OWNER(object_p
) = NULL
;
406 free(OBJ_CERT(object_p
));
411 * Clean up and release all the storage in the extra attribute list
415 soft_cleanup_extra_attr(soft_object_t
*object_p
)
418 CK_ATTRIBUTE_INFO_PTR extra_attr
;
419 CK_ATTRIBUTE_INFO_PTR tmp
;
421 extra_attr
= object_p
->extra_attrlistp
;
423 tmp
= extra_attr
->next
;
424 free(extra_attr
->attr
.pValue
);
426 /* Free the storage for the attribute_info struct. */
431 object_p
->extra_attrlistp
= NULL
;
436 * Create the attribute_info struct to hold the object's attribute,
437 * and add it to the extra attribute list of an object.
440 soft_add_extra_attr(CK_ATTRIBUTE_PTR
template, soft_object_t
*object_p
)
443 CK_ATTRIBUTE_INFO_PTR attrp
;
445 /* Allocate the storage for the attribute_info struct. */
446 attrp
= calloc(1, sizeof (attribute_info_t
));
448 return (CKR_HOST_MEMORY
);
451 /* Set up attribute_info struct. */
452 attrp
->attr
.type
= template->type
;
453 attrp
->attr
.ulValueLen
= template->ulValueLen
;
455 if ((template->pValue
!= NULL
) &&
456 (template->ulValueLen
> 0)) {
457 /* Allocate storage for the value of the attribute. */
458 attrp
->attr
.pValue
= malloc(template->ulValueLen
);
459 if (attrp
->attr
.pValue
== NULL
) {
461 return (CKR_HOST_MEMORY
);
464 (void) memcpy(attrp
->attr
.pValue
, template->pValue
,
465 template->ulValueLen
);
467 attrp
->attr
.pValue
= NULL
;
470 /* Insert the new attribute in front of extra attribute list. */
471 if (object_p
->extra_attrlistp
== NULL
) {
472 object_p
->extra_attrlistp
= attrp
;
475 attrp
->next
= object_p
->extra_attrlistp
;
476 object_p
->extra_attrlistp
= attrp
;
483 soft_copy_certificate(certificate_obj_t
*oldcert
, certificate_obj_t
**newcert
,
484 CK_CERTIFICATE_TYPE type
)
487 certificate_obj_t
*cert
;
489 x509_attr_cert_t x509_attr
;
491 cert
= calloc(1, sizeof (certificate_obj_t
));
493 return (CKR_HOST_MEMORY
);
496 if (type
== CKC_X_509
) {
497 x509
= oldcert
->cert_type_u
.x509
;
499 if ((rv
= copy_cert_attr(x509
.subject
,
500 &cert
->cert_type_u
.x509
.subject
)))
503 if ((rv
= copy_cert_attr(x509
.value
,
504 &cert
->cert_type_u
.x509
.value
)))
506 } else if (type
== CKC_X_509_ATTR_CERT
) {
507 x509_attr
= oldcert
->cert_type_u
.x509_attr
;
509 if ((rv
= copy_cert_attr(x509_attr
.owner
,
510 &cert
->cert_type_u
.x509_attr
.owner
)))
513 if ((rv
= copy_cert_attr(x509_attr
.value
,
514 &cert
->cert_type_u
.x509_attr
.value
)))
517 /* wrong certificate type */
518 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
526 * Copy the attribute_info struct from the old object to a new attribute_info
527 * struct, and add that new struct to the extra attribute list of the new
531 soft_copy_extra_attr(CK_ATTRIBUTE_INFO_PTR old_attrp
, soft_object_t
*object_p
)
533 CK_ATTRIBUTE_INFO_PTR attrp
;
535 /* Allocate attribute_info struct. */
536 attrp
= calloc(1, sizeof (attribute_info_t
));
538 return (CKR_HOST_MEMORY
);
541 attrp
->attr
.type
= old_attrp
->attr
.type
;
542 attrp
->attr
.ulValueLen
= old_attrp
->attr
.ulValueLen
;
544 if ((old_attrp
->attr
.pValue
!= NULL
) &&
545 (old_attrp
->attr
.ulValueLen
> 0)) {
546 attrp
->attr
.pValue
= malloc(old_attrp
->attr
.ulValueLen
);
547 if (attrp
->attr
.pValue
== NULL
) {
549 return (CKR_HOST_MEMORY
);
552 (void) memcpy(attrp
->attr
.pValue
, old_attrp
->attr
.pValue
,
553 old_attrp
->attr
.ulValueLen
);
555 attrp
->attr
.pValue
= NULL
;
558 /* Insert the new attribute in front of extra attribute list */
559 if (object_p
->extra_attrlistp
== NULL
) {
560 object_p
->extra_attrlistp
= attrp
;
563 attrp
->next
= object_p
->extra_attrlistp
;
564 object_p
->extra_attrlistp
= attrp
;
572 * Get the attribute triple from the extra attribute list in the object
573 * (if the specified attribute type is found), and copy it to a template.
574 * Note the type of the attribute to be copied is specified by the template,
575 * and the storage is pre-allocated for the atrribute value in the template
576 * for doing the copy.
579 get_extra_attr_from_object(soft_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
582 CK_ATTRIBUTE_INFO_PTR extra_attr
;
583 CK_ATTRIBUTE_TYPE type
= template->type
;
585 extra_attr
= object_p
->extra_attrlistp
;
588 if (type
== extra_attr
->attr
.type
) {
592 /* Does not match, try next one. */
593 extra_attr
= extra_attr
->next
;
597 if (extra_attr
== NULL
) {
598 /* A valid but un-initialized attribute. */
599 template->ulValueLen
= 0;
604 * We found the attribute in the extra attribute list.
606 if (template->pValue
== NULL
) {
607 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
611 if (template->ulValueLen
>= extra_attr
->attr
.ulValueLen
) {
613 * The buffer provided by the application is large
614 * enough to hold the value of the attribute.
616 (void) memcpy(template->pValue
, extra_attr
->attr
.pValue
,
617 extra_attr
->attr
.ulValueLen
);
618 template->ulValueLen
= extra_attr
->attr
.ulValueLen
;
622 * The buffer provided by the application does
623 * not have enough space to hold the value.
625 template->ulValueLen
= (CK_ULONG
)-1;
626 return (CKR_BUFFER_TOO_SMALL
);
632 * Modify the attribute triple in the extra attribute list of the object
633 * if the specified attribute type is found. Otherwise, just add it to
637 set_extra_attr_to_object(soft_object_t
*object_p
, CK_ATTRIBUTE_TYPE type
,
638 CK_ATTRIBUTE_PTR
template)
641 CK_ATTRIBUTE_INFO_PTR extra_attr
;
643 extra_attr
= object_p
->extra_attrlistp
;
646 if (type
== extra_attr
->attr
.type
) {
650 /* Does not match, try next one. */
651 extra_attr
= extra_attr
->next
;
655 if (extra_attr
== NULL
) {
657 * This attribute is a new one, go ahead adding it to
658 * the extra attribute list.
660 return (soft_add_extra_attr(template, object_p
));
663 /* We found the attribute in the extra attribute list. */
664 if ((template->pValue
!= NULL
) &&
665 (template->ulValueLen
> 0)) {
666 if (template->ulValueLen
> extra_attr
->attr
.ulValueLen
) {
667 /* The old buffer is too small to hold the new value. */
668 free(extra_attr
->attr
.pValue
);
670 /* Allocate storage for the new attribute value. */
671 extra_attr
->attr
.pValue
= malloc(template->ulValueLen
);
672 if (extra_attr
->attr
.pValue
== NULL
) {
673 return (CKR_HOST_MEMORY
);
677 /* Replace the attribute with new value. */
678 extra_attr
->attr
.ulValueLen
= template->ulValueLen
;
679 (void) memcpy(extra_attr
->attr
.pValue
, template->pValue
,
680 template->ulValueLen
);
682 extra_attr
->attr
.pValue
= NULL
;
690 * Copy the big integer attribute value from template to a biginteger_t struct.
693 get_bigint_attr_from_template(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
696 if ((template->pValue
!= NULL
) &&
697 (template->ulValueLen
> 0)) {
698 /* Allocate storage for the value of the attribute. */
699 big
->big_value
= malloc(template->ulValueLen
);
700 if (big
->big_value
== NULL
) {
701 return (CKR_HOST_MEMORY
);
704 (void) memcpy(big
->big_value
, template->pValue
,
705 template->ulValueLen
);
706 big
->big_value_len
= template->ulValueLen
;
708 big
->big_value
= NULL
;
709 big
->big_value_len
= 0;
717 * Copy the big integer attribute value from a biginteger_t struct in the
718 * object to a template.
721 get_bigint_attr_from_object(biginteger_t
*big
, CK_ATTRIBUTE_PTR
template)
724 if (template->pValue
== NULL
) {
725 template->ulValueLen
= big
->big_value_len
;
729 if (big
->big_value
== NULL
) {
730 template->ulValueLen
= 0;
734 if (template->ulValueLen
>= big
->big_value_len
) {
736 * The buffer provided by the application is large
737 * enough to hold the value of the attribute.
739 (void) memcpy(template->pValue
, big
->big_value
,
741 template->ulValueLen
= big
->big_value_len
;
745 * The buffer provided by the application does
746 * not have enough space to hold the value.
748 template->ulValueLen
= (CK_ULONG
)-1;
749 return (CKR_BUFFER_TOO_SMALL
);
755 * Copy the boolean data type attribute value from an object for the
756 * specified attribute to the template.
759 get_bool_attr_from_object(soft_object_t
*object_p
, CK_ULONG bool_flag
,
760 CK_ATTRIBUTE_PTR
template)
763 if (template->pValue
== NULL
) {
764 template->ulValueLen
= sizeof (CK_BBOOL
);
768 if (template->ulValueLen
>= sizeof (CK_BBOOL
)) {
770 * The buffer provided by the application is large
771 * enough to hold the value of the attribute.
773 if (object_p
->bool_attr_mask
& bool_flag
) {
774 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
776 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
779 template->ulValueLen
= sizeof (CK_BBOOL
);
783 * The buffer provided by the application does
784 * not have enough space to hold the value.
786 template->ulValueLen
= (CK_ULONG
)-1;
787 return (CKR_BUFFER_TOO_SMALL
);
792 * Set the boolean data type attribute value in the object.
795 set_bool_attr_to_object(soft_object_t
*object_p
, CK_ULONG bool_flag
,
796 CK_ATTRIBUTE_PTR
template)
799 if (*(CK_BBOOL
*)template->pValue
)
800 object_p
->bool_attr_mask
|= bool_flag
;
802 object_p
->bool_attr_mask
&= ~bool_flag
;
809 * Copy the CK_ULONG data type attribute value from an object to the
813 get_ulong_attr_from_object(CK_ULONG value
, CK_ATTRIBUTE_PTR
template)
816 if (template->pValue
== NULL
) {
817 template->ulValueLen
= sizeof (CK_ULONG
);
821 if (template->ulValueLen
>= sizeof (CK_ULONG
)) {
823 * The buffer provided by the application is large
824 * enough to hold the value of the attribute.
825 * It is also assumed to be correctly aligned.
827 *(CK_ULONG_PTR
)template->pValue
= value
;
828 template->ulValueLen
= sizeof (CK_ULONG
);
832 * The buffer provided by the application does
833 * not have enough space to hold the value.
835 template->ulValueLen
= (CK_ULONG
)-1;
836 return (CKR_BUFFER_TOO_SMALL
);
842 * Copy the CK_ULONG data type attribute value from a template to the
846 get_ulong_attr_from_template(CK_ULONG
*value
, CK_ATTRIBUTE_PTR
template)
849 if (template->ulValueLen
< sizeof (CK_ULONG
))
850 return (CKR_ATTRIBUTE_VALUE_INVALID
);
852 if (template->pValue
!= NULL
) {
853 *value
= *(CK_ULONG_PTR
)template->pValue
;
862 * Copy the big integer attribute value from source's biginteger_t to
863 * destination's biginteger_t.
866 copy_bigint_attr(biginteger_t
*src
, biginteger_t
*dst
)
869 if ((src
->big_value
!= NULL
) &&
870 (src
->big_value_len
> 0)) {
872 * To do the copy, just have dst's big_value points
875 dst
->big_value
= src
->big_value
;
876 dst
->big_value_len
= src
->big_value_len
;
879 * After the copy, nullify the src's big_value pointer.
880 * It prevents any double freeing the value.
882 src
->big_value
= NULL
;
883 src
->big_value_len
= 0;
885 dst
->big_value
= NULL
;
886 dst
->big_value_len
= 0;
891 get_string_from_template(CK_ATTRIBUTE_PTR dest
, CK_ATTRIBUTE_PTR src
)
893 if ((src
->pValue
!= NULL
) &&
894 (src
->ulValueLen
> 0)) {
895 /* Allocate storage for the value of the attribute. */
896 dest
->pValue
= malloc(src
->ulValueLen
);
897 if (dest
->pValue
== NULL
) {
898 return (CKR_HOST_MEMORY
);
901 (void) memcpy(dest
->pValue
, src
->pValue
,
903 dest
->ulValueLen
= src
->ulValueLen
;
904 dest
->type
= src
->type
;
907 dest
->ulValueLen
= 0;
908 dest
->type
= src
->type
;
916 get_cert_attr_from_template(cert_attr_t
**dest
, CK_ATTRIBUTE_PTR src
)
918 if (src
->pValue
!= NULL
&& src
->ulValueLen
> 0) {
920 * If the attribute was already set, clear out the
921 * existing value and release the memory.
924 if ((*dest
)->value
!= NULL
) {
925 (void) memset((*dest
)->value
, 0,
927 free((*dest
)->value
);
930 *dest
= malloc(sizeof (cert_attr_t
));
932 return (CKR_HOST_MEMORY
);
934 (void) memset(*dest
, 0, sizeof (cert_attr_t
));
936 (*dest
)->value
= malloc(src
->ulValueLen
);
937 if ((*dest
)->value
== NULL
) {
940 return (CKR_HOST_MEMORY
);
942 (void) memcpy((*dest
)->value
, src
->pValue
, src
->ulValueLen
);
943 (*dest
)->length
= src
->ulValueLen
;
950 * Copy the certificate attribute information to the template.
951 * If the template attribute is not big enough, set the ulValueLen=-1
952 * and return CKR_BUFFER_TOO_SMALL.
955 get_cert_attr_from_object(cert_attr_t
*src
, CK_ATTRIBUTE_PTR
template)
957 if (template->pValue
== NULL
) {
958 template->ulValueLen
= src
->length
;
960 } else if (template->ulValueLen
>= src
->length
) {
962 * The buffer provided by the application is large
963 * enough to hold the value of the attribute.
965 (void) memcpy(template->pValue
, src
->value
, src
->length
);
966 template->ulValueLen
= src
->length
;
970 * The buffer provided by the application does
971 * not have enough space to hold the value.
973 template->ulValueLen
= (CK_ULONG
)-1;
974 return (CKR_BUFFER_TOO_SMALL
);
979 string_attr_cleanup(CK_ATTRIBUTE_PTR
template)
982 if (template->pValue
) {
983 free(template->pValue
);
984 template->pValue
= NULL
;
985 template->ulValueLen
= 0;
990 * Release the storage allocated for object attribute with big integer
994 bigint_attr_cleanup(biginteger_t
*big
)
1000 if (big
->big_value
) {
1001 (void) memset(big
->big_value
, 0, big
->big_value_len
);
1002 free(big
->big_value
);
1003 big
->big_value
= NULL
;
1004 big
->big_value_len
= 0;
1010 * Clean up and release all the storage allocated to hold the big integer
1011 * attributes associated with the type (i.e. class) of the object. Also,
1012 * release the storage allocated to the type of the object.
1015 soft_cleanup_object_bigint_attrs(soft_object_t
*object_p
)
1018 CK_OBJECT_CLASS
class = object_p
->class;
1019 CK_KEY_TYPE keytype
= object_p
->key_type
;
1023 case CKO_PUBLIC_KEY
:
1024 if (OBJ_PUB(object_p
)) {
1027 bigint_attr_cleanup(OBJ_PUB_RSA_MOD(
1029 bigint_attr_cleanup(OBJ_PUB_RSA_PUBEXPO(
1034 bigint_attr_cleanup(OBJ_PUB_DSA_PRIME(
1036 bigint_attr_cleanup(OBJ_PUB_DSA_SUBPRIME(
1038 bigint_attr_cleanup(OBJ_PUB_DSA_BASE(
1040 bigint_attr_cleanup(OBJ_PUB_DSA_VALUE(
1045 bigint_attr_cleanup(OBJ_PUB_DH_PRIME(
1047 bigint_attr_cleanup(OBJ_PUB_DH_BASE(
1049 bigint_attr_cleanup(OBJ_PUB_DH_VALUE(
1054 bigint_attr_cleanup(OBJ_PUB_DH942_PRIME(
1056 bigint_attr_cleanup(OBJ_PUB_DH942_BASE(
1058 bigint_attr_cleanup(OBJ_PUB_DH942_SUBPRIME(
1060 bigint_attr_cleanup(OBJ_PUB_DH942_VALUE(
1064 bigint_attr_cleanup(OBJ_PUB_EC_POINT(
1069 /* Release Public Key Object struct */
1070 free(OBJ_PUB(object_p
));
1071 OBJ_PUB(object_p
) = NULL
;
1075 case CKO_PRIVATE_KEY
:
1076 if (OBJ_PRI(object_p
)) {
1079 bigint_attr_cleanup(OBJ_PRI_RSA_MOD(
1081 bigint_attr_cleanup(OBJ_PRI_RSA_PUBEXPO(
1083 bigint_attr_cleanup(OBJ_PRI_RSA_PRIEXPO(
1085 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME1(
1087 bigint_attr_cleanup(OBJ_PRI_RSA_PRIME2(
1089 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO1(
1091 bigint_attr_cleanup(OBJ_PRI_RSA_EXPO2(
1093 bigint_attr_cleanup(OBJ_PRI_RSA_COEF(
1098 bigint_attr_cleanup(OBJ_PRI_DSA_PRIME(
1100 bigint_attr_cleanup(OBJ_PRI_DSA_SUBPRIME(
1102 bigint_attr_cleanup(OBJ_PRI_DSA_BASE(
1104 bigint_attr_cleanup(OBJ_PRI_DSA_VALUE(
1109 bigint_attr_cleanup(OBJ_PRI_DH_PRIME(
1111 bigint_attr_cleanup(OBJ_PRI_DH_BASE(
1113 bigint_attr_cleanup(OBJ_PRI_DH_VALUE(
1118 bigint_attr_cleanup(OBJ_PRI_DH942_PRIME(
1120 bigint_attr_cleanup(OBJ_PRI_DH942_BASE(
1122 bigint_attr_cleanup(OBJ_PRI_DH942_SUBPRIME(
1124 bigint_attr_cleanup(OBJ_PRI_DH942_VALUE(
1129 bigint_attr_cleanup(OBJ_PRI_EC_VALUE(
1134 /* Release Private Key Object struct. */
1135 free(OBJ_PRI(object_p
));
1136 OBJ_PRI(object_p
) = NULL
;
1140 case CKO_SECRET_KEY
:
1141 if (OBJ_SEC(object_p
)) {
1142 /* cleanup key data area */
1143 if (OBJ_SEC_VALUE(object_p
) != NULL
&&
1144 OBJ_SEC_VALUE_LEN(object_p
) > 0) {
1145 (void) memset(OBJ_SEC_VALUE(object_p
), 0,
1146 OBJ_SEC_VALUE_LEN(object_p
));
1147 free(OBJ_SEC_VALUE(object_p
));
1149 /* cleanup key schedule data area */
1150 if (OBJ_KEY_SCHED(object_p
) != NULL
&&
1151 OBJ_KEY_SCHED_LEN(object_p
) > 0) {
1152 (void) memset(OBJ_KEY_SCHED(object_p
), 0,
1153 OBJ_KEY_SCHED_LEN(object_p
));
1154 free(OBJ_KEY_SCHED(object_p
));
1157 /* Release Secret Key Object struct. */
1158 free(OBJ_SEC(object_p
));
1159 OBJ_SEC(object_p
) = NULL
;
1163 case CKO_DOMAIN_PARAMETERS
:
1164 if (OBJ_DOM(object_p
)) {
1167 bigint_attr_cleanup(OBJ_DOM_DSA_PRIME(
1169 bigint_attr_cleanup(OBJ_DOM_DSA_SUBPRIME(
1171 bigint_attr_cleanup(OBJ_DOM_DSA_BASE(
1176 bigint_attr_cleanup(OBJ_DOM_DH_PRIME(
1178 bigint_attr_cleanup(OBJ_DOM_DH_BASE(
1183 bigint_attr_cleanup(OBJ_DOM_DH942_PRIME(
1185 bigint_attr_cleanup(OBJ_DOM_DH942_BASE(
1187 bigint_attr_cleanup(OBJ_DOM_DH942_SUBPRIME(
1192 /* Release Domain Parameters Object struct. */
1193 free(OBJ_DOM(object_p
));
1194 OBJ_DOM(object_p
) = NULL
;
1202 * Parse the common attributes. Return to caller with appropriate return
1203 * value to indicate if the supplied template specifies a valid attribute
1204 * with a valid value.
1207 soft_parse_common_attrs(CK_ATTRIBUTE_PTR
template, uchar_t
*object_type
)
1212 switch (template->type
) {
1216 /* default boolean attributes */
1218 if ((*(CK_BBOOL
*)template->pValue
) == B_TRUE
) {
1219 if (!soft_keystore_status(KEYSTORE_INITIALIZED
))
1220 return (CKR_DEVICE_REMOVED
);
1221 *object_type
|= TOKEN_OBJECT
;
1226 if ((*(CK_BBOOL
*)template->pValue
) == B_TRUE
) {
1227 (void) pthread_mutex_lock(&soft_giant_mutex
);
1228 if (!soft_slot
.authenticated
) {
1230 * Check if this is the special case when
1231 * the PIN is never initialized in the keystore.
1232 * If true, we will let it pass here and let
1233 * it fail with CKR_PIN_EXPIRED later on.
1235 if (!soft_slot
.userpin_change_needed
) {
1236 (void) pthread_mutex_unlock(
1238 return (CKR_USER_NOT_LOGGED_IN
);
1241 (void) pthread_mutex_unlock(&soft_giant_mutex
);
1242 *object_type
|= PRIVATE_OBJECT
;
1250 rv
= CKR_TEMPLATE_INCONSISTENT
;
1258 * Build a Public Key Object.
1260 * - Parse the object's template, and when an error is detected such as
1261 * invalid attribute type, invalid attribute value, etc., return
1262 * with appropriate return value.
1263 * - Set up attribute mask field in the object for the supplied common
1264 * attributes that have boolean type.
1265 * - Build the attribute_info struct to hold the value of each supplied
1266 * attribute that has byte array type. Link attribute_info structs
1267 * together to form the extra attribute list of the object.
1268 * - Allocate storage for the Public Key object.
1269 * - Build the Public Key object according to the key type. Allocate
1270 * storage to hold the big integer value for the supplied attributes
1271 * that are required for a certain key type.
1275 soft_build_public_key_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
1276 soft_object_t
*new_object
, CK_ULONG mode
, CK_KEY_TYPE key_type
)
1280 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1281 uint64_t attr_mask
= PUBLIC_KEY_DEFAULT
;
1284 /* Must set flags */
1293 /* Must not set flags */
1294 int isModulusBits
= 0;
1295 CK_ULONG modulus_bits
= 0;
1297 biginteger_t modulus
;
1298 biginteger_t pubexpo
;
1300 biginteger_t subprime
;
1304 CK_ATTRIBUTE string_tmp
;
1305 CK_ATTRIBUTE param_tmp
;
1307 public_key_obj_t
*pbk
;
1308 uchar_t object_type
= 0;
1310 CK_ATTRIBUTE defpubexpo
= { CKA_PUBLIC_EXPONENT
,
1311 (CK_BYTE_PTR
)DEFAULT_PUB_EXPO
, DEFAULT_PUB_EXPO_Len
};
1315 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1316 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1317 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1318 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1319 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1320 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1321 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1322 (void) memset(&point
, 0x0, sizeof (biginteger_t
));
1323 string_tmp
.pValue
= NULL
;
1324 param_tmp
.pValue
= NULL
;
1326 for (i
= 0; i
< ulAttrNum
; i
++) {
1328 /* Public Key Object Attributes */
1329 switch (template[i
].type
) {
1331 /* common key attributes */
1333 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1337 case CKA_START_DATE
:
1340 /* common public key attribute */
1343 * Allocate storage to hold the attribute
1344 * value with byte array type, and add it to
1345 * the extra attribute list of the object.
1347 rv
= soft_add_extra_attr(&template[i
],
1355 * The following key related attribute types must
1356 * not be specified by C_CreateObject, C_GenerateKey(Pair).
1359 case CKA_KEY_GEN_MECHANISM
:
1360 rv
= CKR_TEMPLATE_INCONSISTENT
;
1363 /* Key related boolean attributes */
1365 if (*(CK_BBOOL
*)template[i
].pValue
)
1366 attr_mask
|= DERIVE_BOOL_ON
;
1370 if (*(CK_BBOOL
*)template[i
].pValue
)
1371 attr_mask
|= ENCRYPT_BOOL_ON
;
1373 attr_mask
&= ~ENCRYPT_BOOL_ON
;
1377 if (*(CK_BBOOL
*)template[i
].pValue
)
1378 attr_mask
|= VERIFY_BOOL_ON
;
1380 attr_mask
&= ~VERIFY_BOOL_ON
;
1383 case CKA_VERIFY_RECOVER
:
1384 if (*(CK_BBOOL
*)template[i
].pValue
)
1385 attr_mask
|= VERIFY_RECOVER_BOOL_ON
;
1387 attr_mask
&= ~VERIFY_RECOVER_BOOL_ON
;
1391 if (*(CK_BBOOL
*)template[i
].pValue
)
1392 attr_mask
|= WRAP_BOOL_ON
;
1394 attr_mask
&= ~WRAP_BOOL_ON
;
1398 if (*(CK_BBOOL
*)template[i
].pValue
)
1399 attr_mask
|= TRUSTED_BOOL_ON
;
1402 case CKA_MODIFIABLE
:
1403 if ((*(CK_BBOOL
*)template[i
].pValue
) == B_FALSE
)
1404 attr_mask
|= NOT_MODIFIABLE_BOOL_ON
;
1408 * The following key related attribute types must
1409 * be specified according to the key type by
1416 * Copyin big integer attribute from template
1417 * to a local variable.
1419 rv
= get_bigint_attr_from_template(&modulus
,
1425 * Modulus length needs to be between min key length and
1428 if ((modulus
.big_value_len
<
1429 MIN_RSA_KEYLENGTH_IN_BYTES
) ||
1430 (modulus
.big_value_len
>
1431 MAX_RSA_KEYLENGTH_IN_BYTES
)) {
1432 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1437 case CKA_PUBLIC_EXPONENT
:
1439 rv
= get_bigint_attr_from_template(&pubexpo
,
1447 rv
= get_bigint_attr_from_template(&prime
,
1455 rv
= get_bigint_attr_from_template(&subprime
,
1463 rv
= get_bigint_attr_from_template(&base
,
1471 if (mode
== SOFT_CREATE_OBJ
) {
1472 if ((template[i
].ulValueLen
== 0) ||
1473 (template[i
].pValue
== NULL
)) {
1474 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1479 rv
= get_bigint_attr_from_template(&value
,
1485 case CKA_MODULUS_BITS
:
1487 rv
= get_ulong_attr_from_template(&modulus_bits
,
1495 rv
= get_string_from_template(&string_tmp
,
1503 rv
= get_string_from_template(¶m_tmp
, &template[i
]);
1510 rv
= get_bigint_attr_from_template(&point
,
1517 rv
= soft_parse_common_attrs(&template[i
],
1525 /* Allocate storage for Public Key Object. */
1526 pbk
= calloc(1, sizeof (public_key_obj_t
));
1528 rv
= CKR_HOST_MEMORY
;
1532 new_object
->object_class_u
.public_key
= pbk
;
1533 new_object
->class = CKO_PUBLIC_KEY
;
1535 if ((mode
== SOFT_CREATE_OBJ
) && (keytype
== (CK_KEY_TYPE
)~0UL)) {
1536 rv
= CKR_TEMPLATE_INCOMPLETE
;
1540 if ((mode
== SOFT_GEN_KEY
) && (keytype
== (CK_KEY_TYPE
)~0UL)) {
1544 if ((mode
== SOFT_GEN_KEY
) && (keytype
!= key_type
)) {
1546 * The key type specified in the template does not
1547 * match the implied key type based on the mechanism.
1549 rv
= CKR_TEMPLATE_INCONSISTENT
;
1553 new_object
->key_type
= keytype
;
1555 /* Supported key types of the Public Key Object */
1559 if (mode
== SOFT_CREATE_OBJ
) {
1560 if (isModulusBits
|| isPrime
|| isSubprime
||
1561 isBase
|| isValue
) {
1562 rv
= CKR_TEMPLATE_INCONSISTENT
;
1566 if (isModulus
&& isPubExpo
) {
1568 * Derive modulus_bits attribute from modulus.
1569 * Save modulus_bits integer value to the
1570 * designated place in the public key object.
1573 if (big_init(&n
, CHARLEN2BIGNUMLEN(
1574 modulus
.big_value_len
)) != BIG_OK
) {
1575 rv
= CKR_HOST_MEMORY
;
1579 bytestring2bignum(&n
, modulus
.big_value
,
1580 modulus
.big_value_len
);
1582 modulus_bits
= big_bitlength(&n
);
1583 KEY_PUB_RSA_MOD_BITS(pbk
) = modulus_bits
;
1587 * After modulus_bits has been computed,
1588 * it is safe to move modulus and pubexpo
1589 * big integer attribute value to the
1590 * designated place in the public key object.
1592 copy_bigint_attr(&modulus
,
1593 KEY_PUB_RSA_MOD(pbk
));
1595 copy_bigint_attr(&pubexpo
,
1596 KEY_PUB_RSA_PUBEXPO(pbk
));
1598 rv
= CKR_TEMPLATE_INCOMPLETE
;
1602 /* mode is SOFT_GEN_KEY */
1604 if (isModulus
|| isPrime
|| isSubprime
||
1605 isBase
|| isValue
) {
1606 rv
= CKR_TEMPLATE_INCONSISTENT
;
1611 if (isModulusBits
) {
1613 * Copy big integer attribute value to the
1614 * designated place in the public key object.
1616 KEY_PUB_RSA_MOD_BITS(pbk
) = modulus_bits
;
1618 rv
= CKR_TEMPLATE_INCOMPLETE
;
1623 * Use PKCS#11 default 0x010001 for public exponent
1624 * if not not specified in attribute template.
1628 rv
= get_bigint_attr_from_template(&pubexpo
,
1634 * Copy big integer attribute value to the
1635 * designated place in the public key object.
1637 copy_bigint_attr(&pubexpo
, KEY_PUB_RSA_PUBEXPO(pbk
));
1643 if (mode
== SOFT_CREATE_OBJ
) {
1644 if (isModulusBits
|| isModulus
|| isPubExpo
) {
1645 rv
= CKR_TEMPLATE_INCONSISTENT
;
1649 if (isPrime
&& isSubprime
&& isBase
&& isValue
) {
1650 copy_bigint_attr(&value
,
1651 KEY_PUB_DSA_VALUE(pbk
));
1653 rv
= CKR_TEMPLATE_INCOMPLETE
;
1657 if (isModulusBits
|| isModulus
|| isPubExpo
||
1659 rv
= CKR_TEMPLATE_INCONSISTENT
;
1663 if (!(isPrime
&& isSubprime
&& isBase
)) {
1664 rv
= CKR_TEMPLATE_INCOMPLETE
;
1669 copy_bigint_attr(&prime
, KEY_PUB_DSA_PRIME(pbk
));
1671 copy_bigint_attr(&subprime
, KEY_PUB_DSA_SUBPRIME(pbk
));
1673 copy_bigint_attr(&base
, KEY_PUB_DSA_BASE(pbk
));
1678 if (mode
== SOFT_CREATE_OBJ
) {
1679 if (isModulusBits
|| isModulus
|| isPubExpo
||
1681 rv
= CKR_TEMPLATE_INCONSISTENT
;
1685 if (isPrime
&& isBase
&& isValue
) {
1686 copy_bigint_attr(&value
,
1687 KEY_PUB_DH_VALUE(pbk
));
1689 rv
= CKR_TEMPLATE_INCOMPLETE
;
1693 if (isModulusBits
|| isModulus
|| isPubExpo
||
1694 isSubprime
|| isValue
) {
1695 rv
= CKR_TEMPLATE_INCONSISTENT
;
1699 if (!(isPrime
&& isBase
)) {
1700 rv
= CKR_TEMPLATE_INCOMPLETE
;
1705 copy_bigint_attr(&prime
, KEY_PUB_DH_PRIME(pbk
));
1707 copy_bigint_attr(&base
, KEY_PUB_DH_BASE(pbk
));
1712 if (mode
== SOFT_CREATE_OBJ
) {
1713 if (isModulusBits
|| isModulus
|| isPubExpo
) {
1714 rv
= CKR_TEMPLATE_INCONSISTENT
;
1718 if (isPrime
&& isSubprime
&& isBase
&& isValue
) {
1719 copy_bigint_attr(&value
,
1720 KEY_PUB_DH942_VALUE(pbk
));
1722 rv
= CKR_TEMPLATE_INCOMPLETE
;
1726 if (isModulusBits
|| isModulus
|| isPubExpo
||
1728 rv
= CKR_TEMPLATE_INCONSISTENT
;
1732 if (!(isPrime
&& isSubprime
&& isBase
)) {
1733 rv
= CKR_TEMPLATE_INCOMPLETE
;
1738 copy_bigint_attr(&prime
, KEY_PUB_DH942_PRIME(pbk
));
1740 copy_bigint_attr(&base
, KEY_PUB_DH942_BASE(pbk
));
1742 copy_bigint_attr(&subprime
, KEY_PUB_DH942_SUBPRIME(pbk
));
1747 if (mode
== SOFT_CREATE_OBJ
) {
1748 if (isModulusBits
|| isModulus
|| isPubExpo
||
1749 isPrime
|| isSubprime
|| isBase
|| isValue
) {
1750 rv
= CKR_TEMPLATE_INCONSISTENT
;
1753 } else if (!isECParam
|| !isECPoint
) {
1754 rv
= CKR_TEMPLATE_INCOMPLETE
;
1758 if (isModulusBits
|| isModulus
|| isPubExpo
||
1759 isPrime
|| isSubprime
|| isBase
|| isValue
) {
1760 rv
= CKR_TEMPLATE_INCONSISTENT
;
1763 } else if (!isECParam
) {
1764 rv
= CKR_TEMPLATE_INCOMPLETE
;
1770 copy_bigint_attr(&point
, KEY_PUB_EC_POINT(pbk
));
1772 rv
= soft_add_extra_attr(¶m_tmp
, new_object
);
1775 string_attr_cleanup(¶m_tmp
);
1779 rv
= CKR_TEMPLATE_INCONSISTENT
;
1783 /* Set up object. */
1784 new_object
->object_type
= object_type
;
1785 new_object
->bool_attr_mask
= attr_mask
;
1787 rv
= soft_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(&pubexpo
);
1801 bigint_attr_cleanup(&prime
);
1802 bigint_attr_cleanup(&subprime
);
1803 bigint_attr_cleanup(&base
);
1804 bigint_attr_cleanup(&value
);
1805 bigint_attr_cleanup(&point
);
1806 string_attr_cleanup(&string_tmp
);
1807 string_attr_cleanup(¶m_tmp
);
1810 * cleanup the storage allocated inside the object itself.
1812 soft_cleanup_object(new_object
);
1819 * Build a Private Key Object.
1821 * - Parse the object's template, and when an error is detected such as
1822 * invalid attribute type, invalid attribute value, etc., return
1823 * with appropriate return value.
1824 * - Set up attribute mask field in the object for the supplied common
1825 * attributes that have boolean type.
1826 * - Build the attribute_info struct to hold the value of each supplied
1827 * attribute that has byte array type. Link attribute_info structs
1828 * together to form the extra attribute list of the object.
1829 * - Allocate storage for the Private Key object.
1830 * - Build the Private Key object according to the key type. Allocate
1831 * storage to hold the big integer value for the supplied attributes
1832 * that are required for a certain key type.
1836 soft_build_private_key_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
1837 soft_object_t
*new_object
, CK_ULONG mode
, CK_KEY_TYPE key_type
)
1840 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
1841 uint64_t attr_mask
= PRIVATE_KEY_DEFAULT
;
1845 /* Must set flags unless mode == SOFT_UNWRAP_KEY */
1851 /* Must set flags if mode == SOFT_GEN_KEY */
1853 /* Must not set flags */
1854 int isValueBits
= 0;
1855 CK_ULONG value_bits
= 0;
1857 /* Private Key RSA optional */
1865 biginteger_t modulus
;
1866 biginteger_t priexpo
;
1868 biginteger_t subprime
;
1872 biginteger_t pubexpo
;
1873 biginteger_t prime1
;
1874 biginteger_t prime2
;
1878 CK_ATTRIBUTE string_tmp
;
1879 CK_ATTRIBUTE param_tmp
;
1882 private_key_obj_t
*pvk
;
1883 uchar_t object_type
= 0;
1885 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1886 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1887 (void) memset(&priexpo
, 0x0, sizeof (biginteger_t
));
1888 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1889 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1890 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1891 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1892 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1893 (void) memset(&prime1
, 0x0, sizeof (biginteger_t
));
1894 (void) memset(&prime2
, 0x0, sizeof (biginteger_t
));
1895 (void) memset(&expo1
, 0x0, sizeof (biginteger_t
));
1896 (void) memset(&expo2
, 0x0, sizeof (biginteger_t
));
1897 (void) memset(&coef
, 0x0, sizeof (biginteger_t
));
1898 string_tmp
.pValue
= NULL
;
1899 param_tmp
.pValue
= NULL
;
1903 for (i
= 0; i
< ulAttrNum
; i
++) {
1905 /* Private Key Object Attributes */
1906 switch (template[i
].type
) {
1907 /* common key attributes */
1909 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
1913 case CKA_START_DATE
:
1916 /* common private key attribute */
1919 * Allocate storage to hold the attribute
1920 * value with byte array type, and add it to
1921 * the extra attribute list of the object.
1923 rv
= soft_add_extra_attr(&template[i
],
1931 * The following key related attribute types must
1932 * not be specified by C_CreateObject or C_GenerateKey(Pair).
1935 case CKA_KEY_GEN_MECHANISM
:
1936 case CKA_AUTH_PIN_FLAGS
:
1937 case CKA_ALWAYS_SENSITIVE
:
1938 case CKA_NEVER_EXTRACTABLE
:
1939 rv
= CKR_TEMPLATE_INCONSISTENT
;
1942 /* Key related boolean attributes */
1944 if (*(CK_BBOOL
*)template[i
].pValue
)
1945 attr_mask
|= DERIVE_BOOL_ON
;
1949 if (*(CK_BBOOL
*)template[i
].pValue
)
1950 attr_mask
|= SENSITIVE_BOOL_ON
;
1953 case CKA_SECONDARY_AUTH
:
1954 if (*(CK_BBOOL
*)template[i
].pValue
) {
1955 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
1961 if (*(CK_BBOOL
*)template[i
].pValue
)
1962 attr_mask
|= DECRYPT_BOOL_ON
;
1964 attr_mask
&= ~DECRYPT_BOOL_ON
;
1968 if (*(CK_BBOOL
*)template[i
].pValue
)
1969 attr_mask
|= SIGN_BOOL_ON
;
1971 attr_mask
&= ~SIGN_BOOL_ON
;
1974 case CKA_SIGN_RECOVER
:
1975 if (*(CK_BBOOL
*)template[i
].pValue
)
1976 attr_mask
|= SIGN_RECOVER_BOOL_ON
;
1978 attr_mask
&= ~SIGN_RECOVER_BOOL_ON
;
1982 if (*(CK_BBOOL
*)template[i
].pValue
)
1983 attr_mask
|= UNWRAP_BOOL_ON
;
1985 attr_mask
&= ~UNWRAP_BOOL_ON
;
1988 case CKA_EXTRACTABLE
:
1989 if (*(CK_BBOOL
*)template[i
].pValue
)
1990 attr_mask
|= EXTRACTABLE_BOOL_ON
;
1992 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
1995 case CKA_MODIFIABLE
:
1996 if ((*(CK_BBOOL
*)template[i
].pValue
) == B_FALSE
)
1997 attr_mask
|= NOT_MODIFIABLE_BOOL_ON
;
2001 * The following key related attribute types must
2002 * be specified according to the key type by
2009 * Copyin big integer attribute from template
2010 * to a local variable.
2012 rv
= get_bigint_attr_from_template(&modulus
,
2018 * Modulus length needs to be between min key length and
2021 if ((modulus
.big_value_len
<
2022 MIN_RSA_KEYLENGTH_IN_BYTES
) ||
2023 (modulus
.big_value_len
>
2024 MAX_RSA_KEYLENGTH_IN_BYTES
)) {
2025 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2030 case CKA_PUBLIC_EXPONENT
:
2033 rv
= get_bigint_attr_from_template(&pubexpo
,
2039 case CKA_PRIVATE_EXPONENT
:
2042 rv
= get_bigint_attr_from_template(&priexpo
,
2050 rv
= get_bigint_attr_from_template(&prime1
,
2058 rv
= get_bigint_attr_from_template(&prime2
,
2064 case CKA_EXPONENT_1
:
2066 rv
= get_bigint_attr_from_template(&expo1
,
2072 case CKA_EXPONENT_2
:
2074 rv
= get_bigint_attr_from_template(&expo2
,
2080 case CKA_COEFFICIENT
:
2082 rv
= get_bigint_attr_from_template(&coef
,
2090 rv
= get_bigint_attr_from_template(&prime
,
2098 rv
= get_bigint_attr_from_template(&subprime
,
2106 rv
= get_bigint_attr_from_template(&base
,
2114 if (mode
== SOFT_CREATE_OBJ
) {
2115 if ((template[i
].ulValueLen
== 0) ||
2116 (template[i
].pValue
== NULL
)) {
2117 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2122 rv
= get_bigint_attr_from_template(&value
,
2128 case CKA_VALUE_BITS
:
2130 rv
= get_ulong_attr_from_template(&value_bits
,
2138 rv
= get_string_from_template(&string_tmp
,
2146 rv
= get_string_from_template(¶m_tmp
,
2153 rv
= soft_parse_common_attrs(&template[i
],
2162 /* Allocate storage for Private Key Object. */
2163 pvk
= calloc(1, sizeof (private_key_obj_t
));
2165 rv
= CKR_HOST_MEMORY
;
2169 new_object
->object_class_u
.private_key
= pvk
;
2170 new_object
->class = CKO_PRIVATE_KEY
;
2172 if ((mode
== SOFT_CREATE_OBJ
) && (keytype
== (CK_KEY_TYPE
)~0UL)) {
2173 rv
= CKR_TEMPLATE_INCOMPLETE
;
2177 if (mode
== SOFT_GEN_KEY
) {
2179 * The key type is not specified in the application's
2180 * template, so we use the implied key type based on
2183 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2187 /* If still unspecified, template is incomplete */
2188 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2189 rv
= CKR_TEMPLATE_INCOMPLETE
;
2194 * The key type specified in the template does not
2195 * match the implied key type based on the mechanism.
2197 if (keytype
!= key_type
) {
2198 rv
= CKR_TEMPLATE_INCONSISTENT
;
2203 if (mode
== SOFT_UNWRAP_KEY
) {
2205 * Note that, for mode SOFT_UNWRAP_KEY, key type is not
2206 * implied by the mechanism (key_type), so if it is not
2207 * specified from the attribute template (keytype), it is
2210 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2211 rv
= CKR_TEMPLATE_INCOMPLETE
;
2216 new_object
->key_type
= keytype
;
2218 /* Supported key types of the Private Key Object */
2221 if (isPrime
|| isSubprime
|| isBase
|| isValue
||
2223 rv
= CKR_TEMPLATE_INCONSISTENT
;
2227 if (mode
== SOFT_GEN_KEY
|| mode
== SOFT_UNWRAP_KEY
) {
2228 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
2229 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
) {
2230 rv
= CKR_TEMPLATE_INCONSISTENT
;
2236 if (isModulus
&& isPriExpo
) {
2238 * Copy big integer attribute value to the
2239 * designated place in the Private Key object.
2241 copy_bigint_attr(&modulus
, KEY_PRI_RSA_MOD(pvk
));
2243 copy_bigint_attr(&priexpo
, KEY_PRI_RSA_PRIEXPO(pvk
));
2245 rv
= CKR_TEMPLATE_INCOMPLETE
;
2249 /* The following attributes are optional. */
2251 copy_bigint_attr(&pubexpo
, KEY_PRI_RSA_PUBEXPO(pvk
));
2255 copy_bigint_attr(&prime1
, KEY_PRI_RSA_PRIME1(pvk
));
2259 copy_bigint_attr(&prime2
, KEY_PRI_RSA_PRIME2(pvk
));
2263 copy_bigint_attr(&expo1
, KEY_PRI_RSA_EXPO1(pvk
));
2267 copy_bigint_attr(&expo2
, KEY_PRI_RSA_EXPO2(pvk
));
2271 copy_bigint_attr(&coef
, KEY_PRI_RSA_COEF(pvk
));
2276 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
2277 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
2279 rv
= CKR_TEMPLATE_INCONSISTENT
;
2283 if (mode
== SOFT_GEN_KEY
|| mode
== SOFT_UNWRAP_KEY
) {
2284 if (isPrime
|| isSubprime
|| isBase
|| isValue
) {
2285 rv
= CKR_TEMPLATE_INCONSISTENT
;
2291 if (isPrime
&& isSubprime
&& isBase
&& isValue
) {
2293 * The private value x must be less than subprime q.
2294 * Size for big_init is in BIG_CHUNK_TYPE words.
2297 CHARLEN2BIGNUMLEN(value
.big_value_len
))
2299 rv
= CKR_HOST_MEMORY
;
2303 CHARLEN2BIGNUMLEN(subprime
.big_value_len
))
2305 rv
= CKR_HOST_MEMORY
;
2308 bytestring2bignum(&x
, value
.big_value
,
2309 value
.big_value_len
);
2310 bytestring2bignum(&q
, subprime
.big_value
,
2311 subprime
.big_value_len
);
2313 if (big_cmp_abs(&x
, &q
) > 0) {
2314 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2318 copy_bigint_attr(&prime
, KEY_PRI_DSA_PRIME(pvk
));
2320 copy_bigint_attr(&subprime
, KEY_PRI_DSA_SUBPRIME(pvk
));
2322 copy_bigint_attr(&base
, KEY_PRI_DSA_BASE(pvk
));
2324 copy_bigint_attr(&value
, KEY_PRI_DSA_VALUE(pvk
));
2326 rv
= CKR_TEMPLATE_INCOMPLETE
;
2332 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
2333 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
2335 rv
= CKR_TEMPLATE_INCONSISTENT
;
2339 /* CKA_VALUE_BITS is for key gen but not unwrap */
2340 if (mode
== SOFT_GEN_KEY
)
2341 KEY_PRI_DH_VAL_BITS(pvk
) = (isValueBits
) ?
2343 else if (mode
== SOFT_UNWRAP_KEY
) {
2345 rv
= CKR_TEMPLATE_INCONSISTENT
;
2350 if (mode
== SOFT_GEN_KEY
|| mode
== SOFT_UNWRAP_KEY
) {
2351 if (isPrime
|| isBase
|| isValue
) {
2352 rv
= CKR_TEMPLATE_INCONSISTENT
;
2359 rv
= CKR_TEMPLATE_INCONSISTENT
;
2363 if (isPrime
&& isBase
&& isValue
) {
2364 copy_bigint_attr(&prime
, KEY_PRI_DH_PRIME(pvk
));
2366 copy_bigint_attr(&base
, KEY_PRI_DH_BASE(pvk
));
2368 copy_bigint_attr(&value
, KEY_PRI_DH_VALUE(pvk
));
2370 rv
= CKR_TEMPLATE_INCOMPLETE
;
2376 if (isModulus
|| isPubExpo
|| isPriExpo
|| isPrime1
||
2377 isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
2379 rv
= CKR_TEMPLATE_INCONSISTENT
;
2383 if (mode
== SOFT_GEN_KEY
|| mode
== SOFT_UNWRAP_KEY
) {
2384 if (isPrime
|| isSubprime
|| isBase
|| isValue
) {
2385 rv
= CKR_TEMPLATE_INCONSISTENT
;
2391 if (isPrime
&& isSubprime
&& isBase
&& isValue
) {
2392 copy_bigint_attr(&prime
, KEY_PRI_DH942_PRIME(pvk
));
2394 copy_bigint_attr(&base
, KEY_PRI_DH942_BASE(pvk
));
2396 copy_bigint_attr(&subprime
,
2397 KEY_PRI_DH942_SUBPRIME(pvk
));
2399 copy_bigint_attr(&value
, KEY_PRI_DH942_VALUE(pvk
));
2401 rv
= CKR_TEMPLATE_INCOMPLETE
;
2407 if (isModulus
|| isPubExpo
|| isPrime
||
2408 isPrime1
|| isPrime2
|| isExpo1
|| isExpo2
|| isCoef
||
2409 isValueBits
|| isBase
) {
2410 rv
= CKR_TEMPLATE_INCONSISTENT
;
2413 } else if (isECParam
) {
2414 rv
= soft_add_extra_attr(¶m_tmp
, new_object
);
2417 string_attr_cleanup(¶m_tmp
);
2420 copy_bigint_attr(&value
, KEY_PRI_EC_VALUE(pvk
));
2425 rv
= CKR_TEMPLATE_INCONSISTENT
;
2429 /* Set up object. */
2430 new_object
->object_type
= object_type
;
2431 new_object
->bool_attr_mask
= attr_mask
;
2433 rv
= soft_add_extra_attr(&string_tmp
, new_object
);
2436 string_attr_cleanup(&string_tmp
);
2445 * cleanup the storage allocated to the local variables.
2447 bigint_attr_cleanup(&modulus
);
2448 bigint_attr_cleanup(&priexpo
);
2449 bigint_attr_cleanup(&prime
);
2450 bigint_attr_cleanup(&subprime
);
2451 bigint_attr_cleanup(&base
);
2452 bigint_attr_cleanup(&value
);
2453 bigint_attr_cleanup(&pubexpo
);
2454 bigint_attr_cleanup(&prime1
);
2455 bigint_attr_cleanup(&prime2
);
2456 bigint_attr_cleanup(&expo1
);
2457 bigint_attr_cleanup(&expo2
);
2458 bigint_attr_cleanup(&coef
);
2459 string_attr_cleanup(&string_tmp
);
2460 string_attr_cleanup(¶m_tmp
);
2465 * cleanup the storage allocated inside the object itself.
2467 soft_cleanup_object(new_object
);
2474 * Build a Secret Key Object.
2476 * - Parse the object's template, and when an error is detected such as
2477 * invalid attribute type, invalid attribute value, etc., return
2478 * with appropriate return value.
2479 * - Set up attribute mask field in the object for the supplied common
2480 * attributes that have boolean type.
2481 * - Build the attribute_info struct to hold the value of each supplied
2482 * attribute that has byte array type. Link attribute_info structs
2483 * together to form the extra attribute list of the object.
2484 * - Allocate storage for the Secret Key object.
2485 * - Build the Secret Key object. Allocate storage to hold the big integer
2486 * value for the attribute CKA_VALUE that is required for all the key
2487 * types supported by secret key object.
2488 * This function is called internally with mode = SOFT_CREATE_OBJ_INT.
2492 soft_build_secret_key_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
2493 soft_object_t
*new_object
, CK_ULONG mode
, CK_ULONG key_len
,
2494 CK_KEY_TYPE key_type
)
2498 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
2499 uint64_t attr_mask
= SECRET_KEY_DEFAULT
;
2502 /* Must set flags if mode != SOFT_UNWRAP_KEY, else must not set */
2504 /* Must not set flags if mode != SOFT_UNWRAP_KEY, else optional */
2507 CK_ATTRIBUTE string_tmp
;
2509 secret_key_obj_t
*sck
;
2510 uchar_t object_type
= 0;
2512 string_tmp
.pValue
= NULL
;
2514 /* Allocate storage for Secret Key Object. */
2515 sck
= calloc(1, sizeof (secret_key_obj_t
));
2517 rv
= CKR_HOST_MEMORY
;
2521 new_object
->object_class_u
.secret_key
= sck
;
2522 new_object
->class = CKO_SECRET_KEY
;
2524 for (i
= 0; i
< ulAttrNum
; i
++) {
2526 /* Secret Key Object Attributes */
2527 switch (template[i
].type
) {
2529 /* common key attributes */
2531 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
2535 case CKA_START_DATE
:
2538 * Allocate storage to hold the attribute
2539 * value with byte array type, and add it to
2540 * the extra attribute list of the object.
2542 rv
= soft_add_extra_attr(&template[i
],
2550 * The following key related attribute types must
2551 * not be specified by C_CreateObject and C_GenerateKey.
2554 case CKA_KEY_GEN_MECHANISM
:
2555 case CKA_ALWAYS_SENSITIVE
:
2556 case CKA_NEVER_EXTRACTABLE
:
2557 rv
= CKR_TEMPLATE_INCONSISTENT
;
2560 /* Key related boolean attributes */
2562 if (*(CK_BBOOL
*)template[i
].pValue
)
2563 attr_mask
|= DERIVE_BOOL_ON
;
2567 if (*(CK_BBOOL
*)template[i
].pValue
)
2568 attr_mask
|= SENSITIVE_BOOL_ON
;
2572 if (*(CK_BBOOL
*)template[i
].pValue
)
2573 attr_mask
|= ENCRYPT_BOOL_ON
;
2575 attr_mask
&= ~ENCRYPT_BOOL_ON
;
2579 if (*(CK_BBOOL
*)template[i
].pValue
)
2580 attr_mask
|= DECRYPT_BOOL_ON
;
2582 attr_mask
&= ~DECRYPT_BOOL_ON
;
2586 if (*(CK_BBOOL
*)template[i
].pValue
)
2587 attr_mask
|= SIGN_BOOL_ON
;
2589 attr_mask
&= ~SIGN_BOOL_ON
;
2593 if (*(CK_BBOOL
*)template[i
].pValue
)
2594 attr_mask
|= VERIFY_BOOL_ON
;
2596 attr_mask
&= ~VERIFY_BOOL_ON
;
2600 if (*(CK_BBOOL
*)template[i
].pValue
)
2601 attr_mask
|= WRAP_BOOL_ON
;
2603 attr_mask
&= ~WRAP_BOOL_ON
;
2607 if (*(CK_BBOOL
*)template[i
].pValue
)
2608 attr_mask
|= UNWRAP_BOOL_ON
;
2610 attr_mask
&= ~UNWRAP_BOOL_ON
;
2613 case CKA_EXTRACTABLE
:
2614 if (*(CK_BBOOL
*)template[i
].pValue
)
2615 attr_mask
|= EXTRACTABLE_BOOL_ON
;
2617 attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
2620 case CKA_MODIFIABLE
:
2621 if ((*(CK_BBOOL
*)template[i
].pValue
) == B_FALSE
)
2622 attr_mask
|= NOT_MODIFIABLE_BOOL_ON
;
2627 if (mode
== SOFT_CREATE_OBJ
) {
2628 if ((template[i
].ulValueLen
== 0) ||
2629 (template[i
].pValue
== NULL
)) {
2630 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2636 * Copyin attribute from template
2637 * to a local variable.
2639 rv
= get_bigint_attr_from_template((biginteger_t
*)sck
,
2647 rv
= get_ulong_attr_from_template(&sck
->sk_value_len
,
2655 rv
= get_string_from_template(&string_tmp
,
2662 rv
= soft_parse_common_attrs(&template[i
],
2672 case SOFT_CREATE_OBJ
:
2673 case SOFT_CREATE_OBJ_INT
:
2674 case SOFT_DERIVE_KEY_DH
:
2676 * The key type must be specified in the application's
2677 * template. Otherwise, returns error.
2679 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2680 rv
= CKR_TEMPLATE_INCOMPLETE
;
2686 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2688 * The key type is not specified in the application's
2689 * template, so we use the implied key type based on
2694 if (keytype
!= key_type
) {
2696 * The key type specified in the template
2697 * does not match the implied key type based
2700 rv
= CKR_TEMPLATE_INCONSISTENT
;
2706 * If a key_len is passed as a parameter, it has to
2707 * match the one found in the template.
2710 if (isValueLen
&& sck
->sk_value_len
!= key_len
) {
2711 rv
= CKR_TEMPLATE_INCONSISTENT
;
2715 sck
->sk_value_len
= key_len
;
2719 case SOFT_UNWRAP_KEY
:
2721 * Note that, for mode SOFT_UNWRAP_KEY, key type is not
2722 * implied by the mechanism (key_type), so if it is not
2723 * specified from the attribute template (keytype), it is
2726 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2727 rv
= CKR_TEMPLATE_INCOMPLETE
;
2732 case SOFT_DERIVE_KEY_OTHER
:
2734 * For CKM_MD5_KEY_DERIVATION & CKM_SHA1_KEY_DERIVATION, the
2735 * key type is optional.
2737 if (keytype
== (CK_KEY_TYPE
)~0UL) {
2744 case SOFT_CREATE_OBJ
:
2745 case SOFT_CREATE_OBJ_INT
:
2749 rv
= CKR_TEMPLATE_INCOMPLETE
;
2752 if ((sck
->sk_value_len
< ARCFOUR_MIN_KEY_BYTES
) ||
2753 (sck
->sk_value_len
> ARCFOUR_MAX_KEY_BYTES
)) {
2754 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2759 case CKK_GENERIC_SECRET
:
2761 rv
= CKR_TEMPLATE_INCOMPLETE
;
2768 rv
= CKR_TEMPLATE_INCOMPLETE
;
2771 if ((sck
->sk_value_len
!= AES_MIN_KEY_BYTES
) &&
2772 (sck
->sk_value_len
!= AES_192_KEY_BYTES
) &&
2773 (sck
->sk_value_len
!= AES_MAX_KEY_BYTES
)) {
2774 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2781 rv
= CKR_TEMPLATE_INCOMPLETE
;
2784 if ((sck
->sk_value_len
< BLOWFISH_MINBYTES
) ||
2785 (sck
->sk_value_len
> BLOWFISH_MAXBYTES
)) {
2786 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2794 rv
= CKR_TEMPLATE_INCOMPLETE
;
2797 if (sck
->sk_value_len
!= DES_KEYSIZE
) {
2798 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2805 rv
= CKR_TEMPLATE_INCOMPLETE
;
2808 if (sck
->sk_value_len
!= DES2_KEYSIZE
) {
2809 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2816 rv
= CKR_TEMPLATE_INCOMPLETE
;
2819 if (sck
->sk_value_len
!= DES3_KEYSIZE
) {
2820 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2826 rv
= CKR_TEMPLATE_INCONSISTENT
;
2832 * Templates for internal object creation come from
2833 * applications calls to C_DeriveKey(), for which it
2834 * is OKey to pass a CKA_VALUE_LEN attribute, as
2835 * long as it does not conflict with the length of the
2836 * CKA_VALUE attribute.
2838 if ((mode
!= SOFT_CREATE_OBJ_INT
) ||
2839 ((key_len
> 0) && sck
->sk_value_len
!= key_len
)) {
2840 rv
= CKR_TEMPLATE_INCONSISTENT
;
2847 /* CKA_VALUE must not be specified */
2849 rv
= CKR_TEMPLATE_INCONSISTENT
;
2855 * CKA_VALUE_LEN must be specified by C_GenerateKey
2856 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
2860 rv
= CKR_TEMPLATE_INCOMPLETE
;
2864 if ((sck
->sk_value_len
< ARCFOUR_MIN_KEY_BYTES
) ||
2865 (sck
->sk_value_len
> ARCFOUR_MAX_KEY_BYTES
)) {
2866 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2871 case CKK_GENERIC_SECRET
:
2872 /* arbitrary key length - no length checking */
2874 rv
= CKR_TEMPLATE_INCOMPLETE
;
2881 rv
= CKR_TEMPLATE_INCOMPLETE
;
2885 if ((sck
->sk_value_len
!= AES_MIN_KEY_BYTES
) &&
2886 (sck
->sk_value_len
!= AES_192_KEY_BYTES
) &&
2887 (sck
->sk_value_len
!= AES_MAX_KEY_BYTES
)) {
2888 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2896 rv
= CKR_TEMPLATE_INCOMPLETE
;
2899 if ((sck
->sk_value_len
< BLOWFISH_MINBYTES
) ||
2900 (sck
->sk_value_len
> BLOWFISH_MAXBYTES
)) {
2901 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2910 /* CKA_VALUE_LEN attribute does not apply to DES<n> */
2912 rv
= CKR_TEMPLATE_INCONSISTENT
;
2918 rv
= CKR_TEMPLATE_INCONSISTENT
;
2923 case SOFT_UNWRAP_KEY
:
2925 * According to v2.11 of PKCS#11 spec, neither CKA_VALUE nor
2926 * CKA_VALUE_LEN can be be specified; however v2.20 has this
2927 * restriction removed, perhaps because it makes it hard to
2928 * determine variable-length key sizes. This case statement
2929 * complied with v2.20.
2932 rv
= CKR_TEMPLATE_INCONSISTENT
;
2938 * CKA_VALUE_LEN is optional
2939 * if key is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET
2940 * and the unwrapping mech is *_CBC_PAD.
2942 * CKA_VALUE_LEN is required
2943 * if key is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET
2944 * and the unwrapping mech is *_ECB or *_CBC.
2946 * since mech is not known at this point, CKA_VALUE_LEN is
2947 * treated as optional and the caller needs to enforce it.
2951 if ((sck
->sk_value_len
<
2952 ARCFOUR_MIN_KEY_BYTES
) ||
2953 (sck
->sk_value_len
>
2954 ARCFOUR_MAX_KEY_BYTES
)) {
2955 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2961 case CKK_GENERIC_SECRET
:
2962 /* arbitrary key length - no length checking */
2967 if ((sck
->sk_value_len
!= AES_MIN_KEY_BYTES
) &&
2968 (sck
->sk_value_len
!= AES_192_KEY_BYTES
) &&
2969 (sck
->sk_value_len
!= AES_MAX_KEY_BYTES
)) {
2970 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2978 ((sck
->sk_value_len
< BLOWFISH_MINBYTES
) ||
2979 (sck
->sk_value_len
> BLOWFISH_MAXBYTES
))) {
2980 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
2988 /* CKA_VALUE_LEN attribute does not apply to DES<n> */
2990 rv
= CKR_TEMPLATE_INCONSISTENT
;
2996 rv
= CKR_TEMPLATE_INCONSISTENT
;
3001 case SOFT_DERIVE_KEY_DH
:
3002 /* CKA_VALUE must not be specified */
3004 rv
= CKR_TEMPLATE_INCONSISTENT
;
3010 * CKA_VALUE_LEN is optional
3011 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
3015 if ((sck
->sk_value_len
<
3016 ARCFOUR_MIN_KEY_BYTES
) ||
3017 (sck
->sk_value_len
>
3018 ARCFOUR_MAX_KEY_BYTES
)) {
3019 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
3025 case CKK_GENERIC_SECRET
:
3026 /* arbitrary key length - no length checking */
3031 if ((sck
->sk_value_len
!= AES_MIN_KEY_BYTES
) &&
3032 (sck
->sk_value_len
!= AES_192_KEY_BYTES
) &&
3033 (sck
->sk_value_len
!= AES_MAX_KEY_BYTES
)) {
3034 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
3043 ((sck
->sk_value_len
< BLOWFISH_MINBYTES
) ||
3044 (sck
->sk_value_len
> BLOWFISH_MAXBYTES
))) {
3045 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
3053 /* CKA_VALUE_LEN attribute does not apply to DES<n> */
3055 rv
= CKR_TEMPLATE_INCONSISTENT
;
3061 rv
= CKR_TEMPLATE_INCONSISTENT
;
3066 case SOFT_DERIVE_KEY_OTHER
:
3067 /* CKA_VALUE must not be specified */
3069 rv
= CKR_TEMPLATE_INCONSISTENT
;
3075 * CKA_VALUE_LEN is an optional attribute for
3076 * CKM_SHA1_KEY_DERIVATION and CKM_MD5_KEY_DERIVATION
3077 * if mech is CKK_RC4, CKK_AES, CKK_GENERIC_SECRET.
3080 case CKK_GENERIC_SECRET
:
3084 * No need to check key length value here, it will be
3085 * validated later in soft_key_derive_check_length().
3092 /* CKA_VALUE_LEN attribute does not apply to DES<n> */
3094 rv
= CKR_TEMPLATE_INCONSISTENT
;
3100 rv
= CKR_TEMPLATE_INCONSISTENT
;
3106 /* Set up object. */
3107 new_object
->key_type
= keytype
;
3108 new_object
->object_type
= object_type
;
3109 new_object
->bool_attr_mask
= attr_mask
;
3111 rv
= soft_add_extra_attr(&string_tmp
, new_object
);
3114 string_attr_cleanup(&string_tmp
);
3120 * cleanup the storage allocated to the local variables.
3122 bigint_attr_cleanup((biginteger_t
*)sck
);
3123 string_attr_cleanup(&string_tmp
);
3126 * cleanup the storage allocated inside the object itself.
3128 soft_cleanup_object(new_object
);
3135 * Build a Domain Parameter Object.
3137 * - Parse the object's template, and when an error is detected such as
3138 * invalid attribute type, invalid attribute value, etc., return
3139 * with appropriate return value.
3140 * - Allocate storage for the Domain Parameter object.
3141 * - Build the Domain Parameter object according to the key type. Allocate
3142 * storage to hold the big integer value for the supplied attributes
3143 * that are required for a certain key type.
3147 soft_build_domain_parameters_object(CK_ATTRIBUTE_PTR
template,
3148 CK_ULONG ulAttrNum
, soft_object_t
*new_object
)
3152 CK_KEY_TYPE keytype
= (CK_KEY_TYPE
)~0UL;
3155 /* Must set flags */
3159 /* Must not set flags */
3160 int isPrimeBits
= 0;
3161 int isSubPrimeBits
= 0;
3164 biginteger_t subprime
;
3166 CK_ATTRIBUTE string_tmp
;
3169 uchar_t object_type
= 0;
3171 /* prevent bigint_attr_cleanup from freeing invalid attr value */
3172 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
3173 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
3174 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
3175 string_tmp
.pValue
= NULL
;
3177 for (i
= 0; i
< ulAttrNum
; i
++) {
3179 /* Domain Parameters Object Attributes */
3180 switch (template[i
].type
) {
3182 /* common domain parameter attribute */
3184 keytype
= *((CK_KEY_TYPE
*)template[i
].pValue
);
3188 * The following common domain parameter attribute
3189 * must not be specified by C_CreateObject.
3192 rv
= CKR_TEMPLATE_INCONSISTENT
;
3196 * The following domain parameter attributes must be
3197 * specified according to the key type by
3203 * Copyin big integer attribute from template
3204 * to a local variable.
3206 rv
= get_bigint_attr_from_template(&prime
,
3214 rv
= get_bigint_attr_from_template(&subprime
,
3222 rv
= get_bigint_attr_from_template(&base
,
3228 case CKA_PRIME_BITS
:
3232 case CKA_SUB_PRIME_BITS
:
3238 rv
= get_string_from_template(&string_tmp
,
3245 rv
= soft_parse_common_attrs(&template[i
],
3254 /* Allocate storage for Domain Parameters Object. */
3255 dom
= calloc(1, sizeof (domain_obj_t
));
3257 rv
= CKR_HOST_MEMORY
;
3261 new_object
->object_class_u
.domain
= dom
;
3262 new_object
->class = CKO_DOMAIN_PARAMETERS
;
3264 if (keytype
== (CK_KEY_TYPE
)~0UL) {
3265 rv
= CKR_TEMPLATE_INCOMPLETE
;
3269 new_object
->key_type
= keytype
;
3271 /* Supported key types of the Domain Parameters Object */
3274 if (isPrimeBits
|| isSubPrimeBits
) {
3275 rv
= CKR_TEMPLATE_INCONSISTENT
;
3279 if (isPrime
&& isSubprime
&& isBase
) {
3281 * Copy big integer attribute value to the
3282 * designated place in the domain parameter
3285 copy_bigint_attr(&prime
, KEY_DOM_DSA_PRIME(dom
));
3287 copy_bigint_attr(&subprime
, KEY_DOM_DSA_SUBPRIME(dom
));
3289 copy_bigint_attr(&base
, KEY_DOM_DSA_BASE(dom
));
3291 rv
= CKR_TEMPLATE_INCOMPLETE
;
3297 if (isPrimeBits
|| isSubprime
|| isSubPrimeBits
) {
3298 rv
= CKR_TEMPLATE_INCONSISTENT
;
3302 if (isPrime
&& isBase
) {
3303 copy_bigint_attr(&prime
, KEY_DOM_DH_PRIME(dom
));
3305 copy_bigint_attr(&base
, KEY_DOM_DH_BASE(dom
));
3307 rv
= CKR_TEMPLATE_INCOMPLETE
;
3313 if (isPrimeBits
|| isSubPrimeBits
) {
3314 rv
= CKR_TEMPLATE_INCONSISTENT
;
3318 if (isPrime
&& isSubprime
&& isBase
) {
3319 copy_bigint_attr(&prime
, KEY_DOM_DH942_PRIME(dom
));
3321 copy_bigint_attr(&base
, KEY_DOM_DH942_BASE(dom
));
3323 copy_bigint_attr(&subprime
,
3324 KEY_DOM_DH942_SUBPRIME(dom
));
3326 rv
= CKR_TEMPLATE_INCOMPLETE
;
3332 rv
= CKR_TEMPLATE_INCONSISTENT
;
3336 new_object
->object_type
= object_type
;
3339 rv
= soft_add_extra_attr(&string_tmp
, new_object
);
3342 string_attr_cleanup(&string_tmp
);
3349 * cleanup the storage allocated to the local variables.
3351 bigint_attr_cleanup(&prime
);
3352 bigint_attr_cleanup(&subprime
);
3353 bigint_attr_cleanup(&base
);
3354 string_attr_cleanup(&string_tmp
);
3357 * cleanup the storage allocated inside the object itself.
3359 soft_cleanup_object(new_object
);
3365 * Build a Certificate Object
3367 * - Parse the object's template, and when an error is detected such as
3368 * invalid attribute type, invalid attribute value, etc., return
3369 * with appropriate return value.
3370 * - Allocate storage for the Certificate object
3373 soft_build_certificate_object(CK_ATTRIBUTE_PTR
template,
3374 CK_ULONG ulAttrNum
, soft_object_t
*new_object
,
3375 CK_CERTIFICATE_TYPE cert_type
)
3377 uint64_t attr_mask
= 0;
3382 int subject_set
= 0;
3383 certificate_obj_t
*cert
;
3384 /* certificate type defaults to the value given as a parameter */
3385 CK_CERTIFICATE_TYPE certtype
= cert_type
;
3386 CK_ATTRIBUTE string_tmp
;
3388 uchar_t object_type
= 0;
3391 * Look for the certificate type attribute and do some
3392 * sanity checking before creating the structures.
3394 for (i
= 0; i
< ulAttrNum
; i
++) {
3395 /* Certificate Object Attributes */
3396 switch (template[i
].type
) {
3397 case CKA_CERTIFICATE_TYPE
:
3399 *((CK_CERTIFICATE_TYPE
*)template[i
].pValue
);
3413 /* The certificate type MUST be specified */
3414 if (certtype
!= CKC_X_509
&& certtype
!= CKC_X_509_ATTR_CERT
)
3415 return (CKR_TEMPLATE_INCOMPLETE
);
3418 * For X.509 certs, the CKA_SUBJECT and CKA_VALUE
3419 * must be present at creation time.
3421 if (certtype
== CKC_X_509
&&
3422 (!subject_set
|| !value_set
))
3423 return (CKR_TEMPLATE_INCOMPLETE
);
3426 * For X.509 Attribute certs, the CKA_OWNER and CKA_VALUE
3427 * must be present at creation time.
3429 if (certtype
== CKC_X_509_ATTR_CERT
&&
3430 (!owner_set
|| !value_set
))
3431 return (CKR_TEMPLATE_INCOMPLETE
);
3433 string_tmp
.pValue
= NULL
;
3434 cert
= calloc(1, sizeof (certificate_obj_t
));
3436 return (CKR_HOST_MEMORY
);
3438 cert
->certificate_type
= certtype
;
3440 for (i
= 0; i
< ulAttrNum
; i
++) {
3441 /* Certificate Object Attributes */
3444 switch (template[i
].type
) {
3446 rv
= get_cert_attr_from_template(
3447 &cert
->cert_type_u
.x509
.subject
,
3451 rv
= get_cert_attr_from_template(
3452 &cert
->cert_type_u
.x509
.value
,
3457 rv
= get_string_from_template(
3465 case CKA_SERIAL_NUMBER
:
3466 rv
= soft_add_extra_attr(&template[i
],
3469 case CKA_MODIFIABLE
:
3470 if ((*(CK_BBOOL
*)template[i
].pValue
) ==
3473 NOT_MODIFIABLE_BOOL_ON
;
3475 case CKA_CERTIFICATE_TYPE
:
3478 rv
= soft_parse_common_attrs(
3479 &template[i
], &object_type
);
3484 case CKC_X_509_ATTR_CERT
:
3485 switch (template[i
].type
) {
3487 rv
= get_cert_attr_from_template(
3488 &cert
->cert_type_u
.x509_attr
.owner
,
3492 rv
= get_cert_attr_from_template(
3493 &cert
->cert_type_u
.x509_attr
.value
,
3498 rv
= get_string_from_template(
3499 &string_tmp
, &template[i
]);
3503 case CKA_SERIAL_NUMBER
:
3505 case CKA_ATTR_TYPES
:
3506 rv
= soft_add_extra_attr(&template[i
],
3510 case CKA_MODIFIABLE
:
3511 if ((*(CK_BBOOL
*)template[i
].pValue
) ==
3514 NOT_MODIFIABLE_BOOL_ON
;
3516 case CKA_CERTIFICATE_TYPE
:
3519 rv
= soft_parse_common_attrs(
3520 &template[i
], &object_type
);
3527 rv
= CKR_TEMPLATE_INCOMPLETE
;
3533 new_object
->object_class_u
.certificate
= cert
;
3534 new_object
->class = CKO_CERTIFICATE
;
3535 new_object
->object_type
= object_type
;
3536 new_object
->cert_type
= certtype
;
3537 new_object
->bool_attr_mask
= attr_mask
;
3539 rv
= soft_add_extra_attr(&string_tmp
, new_object
);
3542 string_attr_cleanup(&string_tmp
);
3548 soft_cleanup_cert_object(new_object
);
3555 * Validate the attribute types in the object's template. Then,
3556 * call the appropriate build function according to the class of
3557 * the object specified in the template.
3559 * Note: The following classes of objects are supported:
3563 * - CKO_DOMAIN_PARAMETERS
3568 soft_build_object(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
3569 soft_object_t
*new_object
)
3572 CK_OBJECT_CLASS
class = (CK_OBJECT_CLASS
)~0UL;
3575 if (template == NULL
) {
3576 return (CKR_ARGUMENTS_BAD
);
3579 /* Validate the attribute type in the template. */
3580 rv
= soft_validate_attr(template, ulAttrNum
, &class);
3584 * CKA_CLASS is a mandatory attribute for C_CreateObject
3586 if (class == (CK_OBJECT_CLASS
)~0UL)
3587 return (CKR_TEMPLATE_INCOMPLETE
);
3590 * Call the appropriate function based on the supported class
3594 case CKO_PUBLIC_KEY
:
3595 rv
= soft_build_public_key_object(template, ulAttrNum
,
3596 new_object
, SOFT_CREATE_OBJ
, (CK_KEY_TYPE
)~0UL);
3599 case CKO_PRIVATE_KEY
:
3600 rv
= soft_build_private_key_object(template, ulAttrNum
,
3601 new_object
, SOFT_CREATE_OBJ
, (CK_KEY_TYPE
)~0UL);
3604 case CKO_SECRET_KEY
:
3605 rv
= soft_build_secret_key_object(template, ulAttrNum
,
3606 new_object
, SOFT_CREATE_OBJ
, 0, (CK_KEY_TYPE
)~0UL);
3609 case CKO_DOMAIN_PARAMETERS
:
3610 rv
= soft_build_domain_parameters_object(template, ulAttrNum
,
3614 case CKO_CERTIFICATE
:
3615 rv
= soft_build_certificate_object(template, ulAttrNum
,
3616 new_object
, (CK_CERTIFICATE_TYPE
)~0UL);
3620 case CKO_HW_FEATURE
:
3621 case CKO_VENDOR_DEFINED
:
3623 return (CKR_ATTRIBUTE_VALUE_INVALID
);
3630 * Validate the attribute types in the object's template. Then,
3631 * call the appropriate build function according to the class of
3632 * the object specified in the template.
3636 soft_build_key(CK_ATTRIBUTE_PTR
template, CK_ULONG ulAttrNum
,
3637 soft_object_t
*new_object
, CK_OBJECT_CLASS
class, CK_KEY_TYPE key_type
,
3638 CK_ULONG key_len
, CK_ULONG mode
)
3642 CK_OBJECT_CLASS temp_class
= (CK_OBJECT_CLASS
)~0UL;
3644 /* Validate the attribute type in the template. */
3645 if ((template != NULL
) && (ulAttrNum
!= 0)) {
3646 rv
= soft_validate_attr(template, ulAttrNum
, &temp_class
);
3652 * If either the class from the parameter list ("class") or
3653 * the class from the template ("temp_class") is not specified,
3654 * try to use the other one.
3656 if (temp_class
== (CK_OBJECT_CLASS
)~0UL) {
3658 } else if (class == (CK_OBJECT_CLASS
)~0UL) {
3662 /* If object class is still not specified, template is incomplete. */
3663 if (class == (CK_OBJECT_CLASS
)~0UL)
3664 return (CKR_TEMPLATE_INCOMPLETE
);
3666 /* Class should match if specified in both parameters and template. */
3667 if (class != temp_class
)
3668 return (CKR_TEMPLATE_INCONSISTENT
);
3671 * Call the appropriate function based on the supported class
3675 case CKO_PUBLIC_KEY
:
3677 /* Unwrapping public keys is not supported. */
3678 if (mode
== SOFT_UNWRAP_KEY
) {
3679 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
3683 rv
= soft_build_public_key_object(template, ulAttrNum
,
3684 new_object
, mode
, key_type
);
3687 case CKO_PRIVATE_KEY
:
3689 rv
= soft_build_private_key_object(template, ulAttrNum
,
3690 new_object
, mode
, key_type
);
3693 case CKO_SECRET_KEY
:
3695 rv
= soft_build_secret_key_object(template, ulAttrNum
,
3696 new_object
, mode
, key_len
, key_type
);
3699 case CKO_DOMAIN_PARAMETERS
:
3701 /* Unwrapping domain parameters is not supported. */
3702 if (mode
== SOFT_UNWRAP_KEY
) {
3703 rv
= CKR_ATTRIBUTE_VALUE_INVALID
;
3707 rv
= soft_build_domain_parameters_object(template, ulAttrNum
,
3712 case CKO_CERTIFICATE
:
3713 case CKO_HW_FEATURE
:
3714 case CKO_VENDOR_DEFINED
:
3716 return (CKR_ATTRIBUTE_VALUE_INVALID
);
3724 * Get the value of a requested attribute that is common to all supported
3725 * classes (i.e. public key, private key, secret key, domain parameters,
3726 * and certificate classes).
3729 soft_get_common_attrs(soft_object_t
*object_p
, CK_ATTRIBUTE_PTR
template,
3730 uchar_t object_type
)
3735 switch (template->type
) {
3738 return (get_ulong_attr_from_object(object_p
->class,
3741 /* default boolean attributes */
3743 template->ulValueLen
= sizeof (CK_BBOOL
);
3744 if (template->pValue
== NULL
) {
3747 if (object_type
& TOKEN_OBJECT
)
3748 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
3750 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
3755 template->ulValueLen
= sizeof (CK_BBOOL
);
3756 if (template->pValue
== NULL
) {
3759 if (object_type
& PRIVATE_OBJECT
)
3760 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
3762 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
3765 case CKA_MODIFIABLE
:
3766 template->ulValueLen
= sizeof (CK_BBOOL
);
3767 if (template->pValue
== NULL
) {
3770 if ((object_p
->bool_attr_mask
) & NOT_MODIFIABLE_BOOL_ON
)
3771 *((CK_BBOOL
*)template->pValue
) = B_FALSE
;
3773 *((CK_BBOOL
*)template->pValue
) = B_TRUE
;
3777 return (get_extra_attr_from_object(object_p
,
3782 * The specified attribute for the object is invalid.
3783 * (the object does not possess such an attribute.)
3785 template->ulValueLen
= (CK_ULONG
)-1;
3786 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3793 * Get the value of a requested attribute that is common to all key objects
3794 * (i.e. public key, private key and secret key).
3797 soft_get_common_key_attrs(soft_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
3800 switch (template->type
) {
3803 return (get_ulong_attr_from_object(object_p
->key_type
,
3807 case CKA_START_DATE
:
3810 * The above extra attributes have byte array type.
3812 return (get_extra_attr_from_object(object_p
,
3815 /* Key related boolean attributes */
3817 return (get_bool_attr_from_object(object_p
,
3818 LOCAL_BOOL_ON
, template));
3821 return (get_bool_attr_from_object(object_p
,
3822 DERIVE_BOOL_ON
, template));
3824 case CKA_KEY_GEN_MECHANISM
:
3825 return (get_ulong_attr_from_object(object_p
->mechanism
,
3829 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3834 * Get the value of a requested attribute of a Public Key Object.
3836 * Rule: All the attributes in the public key object can be revealed.
3839 soft_get_public_key_attribute(soft_object_t
*object_p
,
3840 CK_ATTRIBUTE_PTR
template)
3844 CK_KEY_TYPE keytype
= object_p
->key_type
;
3846 switch (template->type
) {
3851 * The above extra attributes have byte array type.
3853 return (get_extra_attr_from_object(object_p
,
3856 /* Key related boolean attributes */
3858 return (get_bool_attr_from_object(object_p
,
3859 ENCRYPT_BOOL_ON
, template));
3862 return (get_bool_attr_from_object(object_p
,
3863 VERIFY_BOOL_ON
, template));
3865 case CKA_VERIFY_RECOVER
:
3866 return (get_bool_attr_from_object(object_p
,
3867 VERIFY_RECOVER_BOOL_ON
, template));
3870 return (get_bool_attr_from_object(object_p
,
3871 WRAP_BOOL_ON
, template));
3874 return (get_bool_attr_from_object(object_p
,
3875 TRUSTED_BOOL_ON
, template));
3879 * This attribute is valid only for RSA public key
3882 if (keytype
== CKK_RSA
) {
3883 return (get_bigint_attr_from_object(
3884 OBJ_PUB_RSA_MOD(object_p
), template));
3886 template->ulValueLen
= (CK_ULONG
)-1;
3887 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3890 case CKA_PUBLIC_EXPONENT
:
3891 if (keytype
== CKK_RSA
) {
3892 return (get_bigint_attr_from_object(
3893 OBJ_PUB_RSA_PUBEXPO(object_p
), template));
3895 template->ulValueLen
= (CK_ULONG
)-1;
3896 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3899 case CKA_MODULUS_BITS
:
3900 if (keytype
== CKK_RSA
) {
3901 return (get_ulong_attr_from_object(
3902 OBJ_PUB_RSA_MOD_BITS(object_p
), template));
3904 template->ulValueLen
= (CK_ULONG
)-1;
3905 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3911 return (get_bigint_attr_from_object(
3912 OBJ_PUB_DSA_PRIME(object_p
), template));
3915 return (get_bigint_attr_from_object(
3916 OBJ_PUB_DH_PRIME(object_p
), template));
3919 return (get_bigint_attr_from_object(
3920 OBJ_PUB_DH942_PRIME(object_p
), template));
3923 template->ulValueLen
= (CK_ULONG
)-1;
3924 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3930 return (get_bigint_attr_from_object(
3931 OBJ_PUB_DSA_SUBPRIME(object_p
), template));
3934 return (get_bigint_attr_from_object(
3935 OBJ_PUB_DH942_SUBPRIME(object_p
), template));
3938 template->ulValueLen
= (CK_ULONG
)-1;
3939 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3945 return (get_bigint_attr_from_object(
3946 OBJ_PUB_DSA_BASE(object_p
), template));
3949 return (get_bigint_attr_from_object(
3950 OBJ_PUB_DH_BASE(object_p
), template));
3953 return (get_bigint_attr_from_object(
3954 OBJ_PUB_DH942_BASE(object_p
), template));
3957 template->ulValueLen
= (CK_ULONG
)-1;
3958 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3962 return (get_bigint_attr_from_object(
3963 OBJ_PUB_EC_POINT(object_p
), template));
3968 return (get_bigint_attr_from_object(
3969 OBJ_PUB_DSA_VALUE(object_p
), template));
3972 return (get_bigint_attr_from_object(
3973 OBJ_PUB_DH_VALUE(object_p
), template));
3976 return (get_bigint_attr_from_object(
3977 OBJ_PUB_DH942_VALUE(object_p
), template));
3980 template->ulValueLen
= (CK_ULONG
)-1;
3981 return (CKR_ATTRIBUTE_TYPE_INVALID
);
3986 * First, get the value of the request attribute defined
3987 * in the list of common key attributes. If the request
3988 * attribute is not found in that list, then get the
3989 * attribute from the list of common attributes.
3991 rv
= soft_get_common_key_attrs(object_p
, template);
3992 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
3993 rv
= soft_get_common_attrs(object_p
, template,
3994 object_p
->object_type
);
4004 * Get the value of a requested attribute of a Private Key Object.
4006 * Rule: All the attributes in the private key object can be revealed
4007 * except those marked with footnote number "7" when the object
4008 * has its CKA_SENSITIVE attribute set to TRUE or its
4009 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
4012 soft_get_private_key_attribute(soft_object_t
*object_p
,
4013 CK_ATTRIBUTE_PTR
template)
4017 CK_KEY_TYPE keytype
= object_p
->key_type
;
4021 * If the following specified attributes for the private key
4022 * object cannot be revealed because the object is sensitive
4023 * or unextractable, then the ulValueLen is set to -1.
4025 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
4026 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
4028 switch (template->type
) {
4029 case CKA_PRIVATE_EXPONENT
:
4032 case CKA_EXPONENT_1
:
4033 case CKA_EXPONENT_2
:
4034 case CKA_COEFFICIENT
:
4036 template->ulValueLen
= (CK_ULONG
)-1;
4037 return (CKR_ATTRIBUTE_SENSITIVE
);
4041 switch (template->type
) {
4046 * The above extra attributes have byte array type.
4048 return (get_extra_attr_from_object(object_p
,
4051 /* Key related boolean attributes */
4053 return (get_bool_attr_from_object(object_p
,
4054 SENSITIVE_BOOL_ON
, template));
4056 case CKA_SECONDARY_AUTH
:
4057 return (get_bool_attr_from_object(object_p
,
4058 SECONDARY_AUTH_BOOL_ON
, template));
4061 return (get_bool_attr_from_object(object_p
,
4062 DECRYPT_BOOL_ON
, template));
4065 return (get_bool_attr_from_object(object_p
,
4066 SIGN_BOOL_ON
, template));
4068 case CKA_SIGN_RECOVER
:
4069 return (get_bool_attr_from_object(object_p
,
4070 SIGN_RECOVER_BOOL_ON
, template));
4073 return (get_bool_attr_from_object(object_p
,
4074 UNWRAP_BOOL_ON
, template));
4076 case CKA_EXTRACTABLE
:
4077 return (get_bool_attr_from_object(object_p
,
4078 EXTRACTABLE_BOOL_ON
, template));
4080 case CKA_ALWAYS_SENSITIVE
:
4081 return (get_bool_attr_from_object(object_p
,
4082 ALWAYS_SENSITIVE_BOOL_ON
, template));
4084 case CKA_NEVER_EXTRACTABLE
:
4085 return (get_bool_attr_from_object(object_p
,
4086 NEVER_EXTRACTABLE_BOOL_ON
, template));
4089 if (keytype
== CKK_RSA
) {
4090 return (get_bigint_attr_from_object(
4091 OBJ_PRI_RSA_MOD(object_p
), template));
4093 template->ulValueLen
= (CK_ULONG
)-1;
4094 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4098 case CKA_PUBLIC_EXPONENT
:
4099 if (keytype
== CKK_RSA
) {
4100 return (get_bigint_attr_from_object(
4101 OBJ_PRI_RSA_PUBEXPO(object_p
), template));
4103 template->ulValueLen
= (CK_ULONG
)-1;
4104 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4108 case CKA_PRIVATE_EXPONENT
:
4109 if (keytype
== CKK_RSA
) {
4110 return (get_bigint_attr_from_object(
4111 OBJ_PRI_RSA_PRIEXPO(object_p
), template));
4113 template->ulValueLen
= (CK_ULONG
)-1;
4114 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4119 if (keytype
== CKK_RSA
) {
4120 return (get_bigint_attr_from_object(
4121 OBJ_PRI_RSA_PRIME1(object_p
), template));
4123 template->ulValueLen
= (CK_ULONG
)-1;
4124 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4129 if (keytype
== CKK_RSA
) {
4130 return (get_bigint_attr_from_object(
4131 OBJ_PRI_RSA_PRIME2(object_p
), template));
4133 template->ulValueLen
= (CK_ULONG
)-1;
4134 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4138 case CKA_EXPONENT_1
:
4139 if (keytype
== CKK_RSA
) {
4140 return (get_bigint_attr_from_object(
4141 OBJ_PRI_RSA_EXPO1(object_p
), template));
4143 template->ulValueLen
= (CK_ULONG
)-1;
4144 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4148 case CKA_EXPONENT_2
:
4149 if (keytype
== CKK_RSA
) {
4150 return (get_bigint_attr_from_object(
4151 OBJ_PRI_RSA_EXPO2(object_p
), template));
4153 template->ulValueLen
= (CK_ULONG
)-1;
4154 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4158 case CKA_COEFFICIENT
:
4159 if (keytype
== CKK_RSA
) {
4160 return (get_bigint_attr_from_object(
4161 OBJ_PRI_RSA_COEF(object_p
), template));
4163 template->ulValueLen
= (CK_ULONG
)-1;
4164 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4168 case CKA_VALUE_BITS
:
4169 if (keytype
== CKK_DH
) {
4170 return (get_ulong_attr_from_object(
4171 OBJ_PRI_DH_VAL_BITS(object_p
), template));
4173 template->ulValueLen
= (CK_ULONG
)-1;
4174 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4181 return (get_bigint_attr_from_object(
4182 OBJ_PRI_DSA_PRIME(object_p
), template));
4185 return (get_bigint_attr_from_object(
4186 OBJ_PRI_DH_PRIME(object_p
), template));
4189 return (get_bigint_attr_from_object(
4190 OBJ_PRI_DH942_PRIME(object_p
), template));
4193 template->ulValueLen
= (CK_ULONG
)-1;
4194 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4200 return (get_bigint_attr_from_object(
4201 OBJ_PRI_DSA_SUBPRIME(object_p
), template));
4204 return (get_bigint_attr_from_object(
4205 OBJ_PRI_DH942_SUBPRIME(object_p
), template));
4208 template->ulValueLen
= (CK_ULONG
)-1;
4209 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4215 return (get_bigint_attr_from_object(
4216 OBJ_PRI_DSA_BASE(object_p
), template));
4219 return (get_bigint_attr_from_object(
4220 OBJ_PRI_DH_BASE(object_p
), template));
4223 return (get_bigint_attr_from_object(
4224 OBJ_PRI_DH942_BASE(object_p
), template));
4227 template->ulValueLen
= (CK_ULONG
)-1;
4228 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4234 return (get_bigint_attr_from_object(
4235 OBJ_PRI_DSA_VALUE(object_p
), template));
4238 return (get_bigint_attr_from_object(
4239 OBJ_PRI_DH_VALUE(object_p
), template));
4242 return (get_bigint_attr_from_object(
4243 OBJ_PRI_DH942_VALUE(object_p
), template));
4246 return (get_bigint_attr_from_object(
4247 OBJ_PRI_EC_VALUE(object_p
), template));
4250 template->ulValueLen
= (CK_ULONG
)-1;
4251 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4256 * First, get the value of the request attribute defined
4257 * in the list of common key attributes. If the request
4258 * attribute is not found in that list, then get the
4259 * attribute from the list of common attributes.
4261 rv
= soft_get_common_key_attrs(object_p
, template);
4262 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
4263 rv
= soft_get_common_attrs(object_p
, template,
4264 object_p
->object_type
);
4274 * Get the value of a requested attribute of a Secret Key Object.
4276 * Rule: All the attributes in the secret key object can be revealed
4277 * except those marked with footnote number "7" when the object
4278 * has its CKA_SENSITIVE attribute set to TRUE or its
4279 * CKA_EXTRACTABLE attribute set to FALSE (p.88 in PKCS11 spec.).
4282 soft_get_secret_key_attribute(soft_object_t
*object_p
,
4283 CK_ATTRIBUTE_PTR
template)
4287 CK_KEY_TYPE keytype
= object_p
->key_type
;
4289 switch (template->type
) {
4291 /* Key related boolean attributes */
4293 return (get_bool_attr_from_object(object_p
,
4294 SENSITIVE_BOOL_ON
, template));
4297 return (get_bool_attr_from_object(object_p
,
4298 ENCRYPT_BOOL_ON
, template));
4301 return (get_bool_attr_from_object(object_p
,
4302 DECRYPT_BOOL_ON
, template));
4305 return (get_bool_attr_from_object(object_p
,
4306 SIGN_BOOL_ON
, template));
4309 return (get_bool_attr_from_object(object_p
,
4310 VERIFY_BOOL_ON
, template));
4313 return (get_bool_attr_from_object(object_p
,
4314 WRAP_BOOL_ON
, template));
4317 return (get_bool_attr_from_object(object_p
,
4318 UNWRAP_BOOL_ON
, template));
4320 case CKA_EXTRACTABLE
:
4321 return (get_bool_attr_from_object(object_p
,
4322 EXTRACTABLE_BOOL_ON
, template));
4324 case CKA_ALWAYS_SENSITIVE
:
4325 return (get_bool_attr_from_object(object_p
,
4326 ALWAYS_SENSITIVE_BOOL_ON
, template));
4328 case CKA_NEVER_EXTRACTABLE
:
4329 return (get_bool_attr_from_object(object_p
,
4330 NEVER_EXTRACTABLE_BOOL_ON
, template));
4335 * If the specified attribute for the secret key object
4336 * cannot be revealed because the object is sensitive
4337 * or unextractable, then the ulValueLen is set to -1.
4339 if ((object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
) ||
4340 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
4341 template->ulValueLen
= (CK_ULONG
)-1;
4342 return (CKR_ATTRIBUTE_SENSITIVE
);
4347 case CKK_GENERIC_SECRET
:
4355 if (template->type
== CKA_VALUE_LEN
) {
4356 return (get_ulong_attr_from_object(
4357 OBJ_SEC_VALUE_LEN(object_p
),
4360 return (get_bigint_attr_from_object(
4361 (biginteger_t
*)OBJ_SEC(object_p
),
4365 template->ulValueLen
= (CK_ULONG
)-1;
4366 rv
= CKR_ATTRIBUTE_TYPE_INVALID
;
4373 * First, get the value of the request attribute defined
4374 * in the list of common key attributes. If the request
4375 * attribute is not found in that list, then get the
4376 * attribute from the list of common attributes.
4378 rv
= soft_get_common_key_attrs(object_p
, template);
4379 if (rv
== CKR_ATTRIBUTE_TYPE_INVALID
) {
4380 rv
= soft_get_common_attrs(object_p
, template,
4381 object_p
->object_type
);
4391 * Get the value of a requested attribute of a Domain Parameters Object.
4393 * Rule: All the attributes in the domain parameters object can be revealed.
4396 soft_get_domain_parameters_attribute(soft_object_t
*object_p
,
4397 CK_ATTRIBUTE_PTR
template)
4401 CK_KEY_TYPE keytype
= object_p
->key_type
;
4403 switch (template->type
) {
4406 return (get_ulong_attr_from_object(keytype
,
4410 return (get_bool_attr_from_object(object_p
,
4411 LOCAL_BOOL_ON
, template));
4416 return (get_bigint_attr_from_object(
4417 OBJ_DOM_DSA_PRIME(object_p
), template));
4420 return (get_bigint_attr_from_object(
4421 OBJ_DOM_DH_PRIME(object_p
), template));
4424 return (get_bigint_attr_from_object(
4425 OBJ_DOM_DH942_PRIME(object_p
), template));
4428 template->ulValueLen
= (CK_ULONG
)-1;
4429 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4435 return (get_bigint_attr_from_object(
4436 OBJ_DOM_DSA_SUBPRIME(object_p
), template));
4439 return (get_bigint_attr_from_object(
4440 OBJ_DOM_DH942_SUBPRIME(object_p
), template));
4443 template->ulValueLen
= (CK_ULONG
)-1;
4444 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4450 return (get_bigint_attr_from_object(
4451 OBJ_DOM_DSA_BASE(object_p
), template));
4454 return (get_bigint_attr_from_object(
4455 OBJ_DOM_DH_BASE(object_p
), template));
4458 return (get_bigint_attr_from_object(
4459 OBJ_DOM_DH942_BASE(object_p
), template));
4462 template->ulValueLen
= (CK_ULONG
)-1;
4463 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4466 case CKA_PRIME_BITS
:
4469 return (get_ulong_attr_from_object(
4470 OBJ_DOM_DSA_PRIME_BITS(object_p
), template));
4473 return (get_ulong_attr_from_object(
4474 OBJ_DOM_DH_PRIME_BITS(object_p
), template));
4477 return (get_ulong_attr_from_object(
4478 OBJ_DOM_DH942_PRIME_BITS(object_p
), template));
4481 template->ulValueLen
= (CK_ULONG
)-1;
4482 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4485 case CKA_SUB_PRIME_BITS
:
4488 return (get_ulong_attr_from_object(
4489 OBJ_DOM_DH942_SUBPRIME_BITS(object_p
), template));
4492 template->ulValueLen
= (CK_ULONG
)-1;
4493 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4498 * Get the value of a common attribute.
4500 rv
= soft_get_common_attrs(object_p
, template,
4501 object_p
->object_type
);
4509 * Get certificate attributes from an object.
4510 * return CKR_ATTRIBUTE_TYPE_INVALID if the requested type
4511 * does not exist in the certificate.
4514 soft_get_certificate_attribute(soft_object_t
*object_p
,
4515 CK_ATTRIBUTE_PTR
template)
4517 CK_CERTIFICATE_TYPE certtype
= object_p
->cert_type
;
4520 switch (template->type
) {
4522 if (certtype
== CKC_X_509
) {
4523 return (get_cert_attr_from_object(
4524 X509_CERT_SUBJECT(object_p
), template));
4528 if (certtype
== CKC_X_509
) {
4529 return (get_cert_attr_from_object(
4530 X509_CERT_VALUE(object_p
), template));
4531 } else if (certtype
== CKC_X_509_ATTR_CERT
) {
4532 return (get_cert_attr_from_object(
4533 X509_ATTR_CERT_VALUE(object_p
), template));
4537 if (certtype
== CKC_X_509_ATTR_CERT
) {
4538 return (get_cert_attr_from_object(
4539 X509_ATTR_CERT_OWNER(object_p
), template));
4542 case CKA_CERTIFICATE_TYPE
:
4543 src
.value
= (CK_BYTE
*)&certtype
;
4544 src
.length
= sizeof (certtype
);
4545 return (get_cert_attr_from_object(&src
, template));
4547 return (get_bool_attr_from_object(object_p
,
4548 TRUSTED_BOOL_ON
, template));
4551 case CKA_SERIAL_NUMBER
:
4553 case CKA_ATTR_TYPES
:
4554 return (get_extra_attr_from_object(object_p
,
4557 return (soft_get_common_attrs(object_p
, template,
4558 object_p
->object_type
));
4562 * If we got this far, then the combination of certificate type
4563 * and requested attribute is invalid.
4565 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4569 soft_set_certificate_attribute(soft_object_t
*object_p
,
4570 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4572 CK_CERTIFICATE_TYPE certtype
= object_p
->cert_type
;
4574 switch (template->type
) {
4576 if (certtype
== CKC_X_509
) {
4577 /* SUBJECT attr cannot be modified. */
4578 return (CKR_ATTRIBUTE_READ_ONLY
);
4582 if (certtype
== CKC_X_509_ATTR_CERT
) {
4583 /* OWNER attr cannot be modified. */
4584 return (CKR_ATTRIBUTE_READ_ONLY
);
4588 /* VALUE attr cannot be modified. */
4589 return (CKR_ATTRIBUTE_READ_ONLY
);
4592 if (certtype
== CKC_X_509
) {
4593 return (set_extra_attr_to_object(object_p
,
4594 template->type
, template));
4598 case CKA_ATTR_TYPES
:
4599 if (certtype
== CKC_X_509_ATTR_CERT
) {
4600 return (set_extra_attr_to_object(object_p
,
4601 template->type
, template));
4604 case CKA_SERIAL_NUMBER
:
4606 return (set_extra_attr_to_object(object_p
,
4607 template->type
, template));
4609 return (soft_set_common_storage_attribute(
4610 object_p
, template, copy
));
4614 * If we got this far, then the combination of certificate type
4615 * and requested attribute is invalid.
4617 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4621 * Call the appropriate get attribute function according to the class
4624 * The caller of this function holds the lock on the object.
4627 soft_get_attribute(soft_object_t
*object_p
, CK_ATTRIBUTE_PTR
template)
4631 CK_OBJECT_CLASS
class = object_p
->class;
4634 case CKO_PUBLIC_KEY
:
4635 rv
= soft_get_public_key_attribute(object_p
, template);
4638 case CKO_PRIVATE_KEY
:
4639 rv
= soft_get_private_key_attribute(object_p
, template);
4642 case CKO_SECRET_KEY
:
4643 rv
= soft_get_secret_key_attribute(object_p
, template);
4646 case CKO_DOMAIN_PARAMETERS
:
4647 rv
= soft_get_domain_parameters_attribute(object_p
, template);
4650 case CKO_CERTIFICATE
:
4651 rv
= soft_get_certificate_attribute(object_p
, template);
4656 * If the specified attribute for the object is invalid
4657 * (the object does not possess such as attribute), then
4658 * the ulValueLen is modified to hold the value -1.
4660 template->ulValueLen
= (CK_ULONG
)-1;
4661 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4669 soft_set_common_storage_attribute(soft_object_t
*object_p
,
4670 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4675 switch (template->type
) {
4679 if ((*(CK_BBOOL
*)template->pValue
) == B_TRUE
) {
4680 if (!soft_keystore_status(KEYSTORE_INITIALIZED
))
4681 return (CKR_DEVICE_REMOVED
);
4682 object_p
->object_type
|= TOKEN_OBJECT
;
4685 rv
= CKR_ATTRIBUTE_READ_ONLY
;
4692 if ((*(CK_BBOOL
*)template->pValue
) == B_TRUE
) {
4693 (void) pthread_mutex_lock(&soft_giant_mutex
);
4694 if (!soft_slot
.authenticated
) {
4696 * Check if this is the special case
4697 * when the PIN is never initialized
4698 * in the keystore. If true, we will
4699 * let it pass here and let it fail
4700 * with CKR_PIN_EXPIRED later on.
4702 if (!soft_slot
.userpin_change_needed
) {
4703 (void) pthread_mutex_unlock(
4705 return (CKR_USER_NOT_LOGGED_IN
);
4708 (void) pthread_mutex_unlock(&soft_giant_mutex
);
4709 object_p
->object_type
|= PRIVATE_OBJECT
;
4712 rv
= CKR_ATTRIBUTE_READ_ONLY
;
4716 case CKA_MODIFIABLE
:
4718 if ((*(CK_BBOOL
*)template->pValue
) == TRUE
)
4719 object_p
->bool_attr_mask
&=
4720 ~NOT_MODIFIABLE_BOOL_ON
;
4722 object_p
->bool_attr_mask
|=
4723 NOT_MODIFIABLE_BOOL_ON
;
4725 rv
= CKR_ATTRIBUTE_READ_ONLY
;
4730 rv
= CKR_ATTRIBUTE_READ_ONLY
;
4734 rv
= CKR_TEMPLATE_INCONSISTENT
;
4741 * Set the value of an attribute that is common to all key objects
4742 * (i.e. public key, private key and secret key).
4745 soft_set_common_key_attribute(soft_object_t
*object_p
,
4746 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4749 switch (template->type
) {
4753 * Only the LABEL can be modified in the common storage
4754 * object attributes after the object is created.
4756 return (set_extra_attr_to_object(object_p
,
4757 CKA_LABEL
, template));
4760 return (set_extra_attr_to_object(object_p
,
4763 case CKA_START_DATE
:
4764 return (set_extra_attr_to_object(object_p
,
4765 CKA_START_DATE
, template));
4768 return (set_extra_attr_to_object(object_p
,
4769 CKA_END_DATE
, template));
4772 return (set_bool_attr_to_object(object_p
,
4773 DERIVE_BOOL_ON
, template));
4777 case CKA_KEY_GEN_MECHANISM
:
4778 return (CKR_ATTRIBUTE_READ_ONLY
);
4781 return (soft_set_common_storage_attribute(object_p
,
4790 * Set the value of an attribute of a Public Key Object.
4792 * Rule: The attributes marked with footnote number "8" in the PKCS11
4793 * spec may be modified (p.88 in PKCS11 spec.).
4796 soft_set_public_key_attribute(soft_object_t
*object_p
,
4797 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4799 CK_KEY_TYPE keytype
= object_p
->key_type
;
4801 switch (template->type
) {
4804 return (set_extra_attr_to_object(object_p
,
4805 CKA_SUBJECT
, template));
4808 return (set_bool_attr_to_object(object_p
,
4809 ENCRYPT_BOOL_ON
, template));
4812 return (set_bool_attr_to_object(object_p
,
4813 VERIFY_BOOL_ON
, template));
4815 case CKA_VERIFY_RECOVER
:
4816 return (set_bool_attr_to_object(object_p
,
4817 VERIFY_RECOVER_BOOL_ON
, template));
4820 return (set_bool_attr_to_object(object_p
,
4821 WRAP_BOOL_ON
, template));
4824 case CKA_MODULUS_BITS
:
4825 case CKA_PUBLIC_EXPONENT
:
4826 if (keytype
== CKK_RSA
)
4827 return (CKR_ATTRIBUTE_READ_ONLY
);
4831 if ((keytype
== CKK_DSA
) ||
4832 (keytype
== CKK_X9_42_DH
))
4833 return (CKR_ATTRIBUTE_READ_ONLY
);
4839 if ((keytype
== CKK_DSA
) ||
4840 (keytype
== CKK_DH
) ||
4841 (keytype
== CKK_X9_42_DH
))
4842 return (CKR_ATTRIBUTE_READ_ONLY
);
4847 * Set the value of a common key attribute.
4849 return (soft_set_common_key_attribute(object_p
,
4854 * If we got this far, then the combination of key type
4855 * and requested attribute is invalid.
4857 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4862 * Set the value of an attribute of a Private Key Object.
4864 * Rule: The attributes marked with footnote number "8" in the PKCS11
4865 * spec may be modified (p.88 in PKCS11 spec.).
4868 soft_set_private_key_attribute(soft_object_t
*object_p
,
4869 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4871 CK_KEY_TYPE keytype
= object_p
->key_type
;
4873 switch (template->type
) {
4876 return (set_extra_attr_to_object(object_p
,
4877 CKA_SUBJECT
, template));
4881 * Cannot set SENSITIVE to FALSE if it is already ON.
4883 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
4884 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
4885 return (CKR_ATTRIBUTE_READ_ONLY
);
4888 if (*(CK_BBOOL
*)template->pValue
)
4889 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
4893 return (set_bool_attr_to_object(object_p
,
4894 DECRYPT_BOOL_ON
, template));
4897 return (set_bool_attr_to_object(object_p
,
4898 SIGN_BOOL_ON
, template));
4900 case CKA_SIGN_RECOVER
:
4901 return (set_bool_attr_to_object(object_p
,
4902 SIGN_RECOVER_BOOL_ON
, template));
4905 return (set_bool_attr_to_object(object_p
,
4906 UNWRAP_BOOL_ON
, template));
4908 case CKA_EXTRACTABLE
:
4910 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
4912 if ((*(CK_BBOOL
*)template->pValue
) &&
4913 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
4914 return (CKR_ATTRIBUTE_READ_ONLY
);
4917 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
4918 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
4922 case CKA_PUBLIC_EXPONENT
:
4923 case CKA_PRIVATE_EXPONENT
:
4926 case CKA_EXPONENT_1
:
4927 case CKA_EXPONENT_2
:
4928 case CKA_COEFFICIENT
:
4929 if (keytype
== CKK_RSA
) {
4930 return (CKR_ATTRIBUTE_READ_ONLY
);
4935 if ((keytype
== CKK_DSA
) ||
4936 (keytype
== CKK_X9_42_DH
))
4937 return (CKR_ATTRIBUTE_READ_ONLY
);
4943 if ((keytype
== CKK_DSA
) ||
4944 (keytype
== CKK_DH
) ||
4945 (keytype
== CKK_X9_42_DH
))
4946 return (CKR_ATTRIBUTE_READ_ONLY
);
4949 case CKA_VALUE_BITS
:
4950 if (keytype
== CKK_DH
)
4951 return (CKR_ATTRIBUTE_READ_ONLY
);
4956 * Set the value of a common key attribute.
4958 return (soft_set_common_key_attribute(object_p
,
4963 * If we got this far, then the combination of key type
4964 * and requested attribute is invalid.
4966 return (CKR_ATTRIBUTE_TYPE_INVALID
);
4970 * Set the value of an attribute of a Secret Key Object.
4972 * Rule: The attributes marked with footnote number "8" in the PKCS11
4973 * spec may be modified (p.88 in PKCS11 spec.).
4976 soft_set_secret_key_attribute(soft_object_t
*object_p
,
4977 CK_ATTRIBUTE_PTR
template, boolean_t copy
)
4979 CK_KEY_TYPE keytype
= object_p
->key_type
;
4981 switch (template->type
) {
4985 * Cannot set SENSITIVE to FALSE if it is already ON.
4987 if (((*(CK_BBOOL
*)template->pValue
) == B_FALSE
) &&
4988 (object_p
->bool_attr_mask
& SENSITIVE_BOOL_ON
)) {
4989 return (CKR_ATTRIBUTE_READ_ONLY
);
4992 if (*(CK_BBOOL
*)template->pValue
)
4993 object_p
->bool_attr_mask
|= SENSITIVE_BOOL_ON
;
4997 return (set_bool_attr_to_object(object_p
,
4998 ENCRYPT_BOOL_ON
, template));
5001 return (set_bool_attr_to_object(object_p
,
5002 DECRYPT_BOOL_ON
, template));
5005 return (set_bool_attr_to_object(object_p
,
5006 SIGN_BOOL_ON
, template));
5009 return (set_bool_attr_to_object(object_p
,
5010 VERIFY_BOOL_ON
, template));
5013 return (set_bool_attr_to_object(object_p
,
5014 WRAP_BOOL_ON
, template));
5017 return (set_bool_attr_to_object(object_p
,
5018 UNWRAP_BOOL_ON
, template));
5020 case CKA_EXTRACTABLE
:
5022 * Cannot set EXTRACTABLE to TRUE if it is already OFF.
5024 if ((*(CK_BBOOL
*)template->pValue
) &&
5025 !(object_p
->bool_attr_mask
& EXTRACTABLE_BOOL_ON
)) {
5026 return (CKR_ATTRIBUTE_READ_ONLY
);
5029 if ((*(CK_BBOOL
*)template->pValue
) == B_FALSE
)
5030 object_p
->bool_attr_mask
&= ~EXTRACTABLE_BOOL_ON
;
5034 return (CKR_ATTRIBUTE_READ_ONLY
);
5037 if ((keytype
== CKK_RC4
) ||
5038 (keytype
== CKK_GENERIC_SECRET
) ||
5039 (keytype
== CKK_AES
) ||
5040 (keytype
== CKK_BLOWFISH
))
5041 return (CKR_ATTRIBUTE_READ_ONLY
);
5046 * Set the value of a common key attribute.
5048 return (soft_set_common_key_attribute(object_p
,
5053 * If we got this far, then the combination of key type
5054 * and requested attribute is invalid.
5056 return (CKR_ATTRIBUTE_TYPE_INVALID
);
5061 * Call the appropriate set attribute function according to the class
5064 * The caller of this function does not hold the lock on the original
5065 * object, since this function is setting the attribute on the new object
5066 * that is being modified.
5068 * Argument copy: TRUE when called by C_CopyObject,
5069 * FALSE when called by C_SetAttributeValue.
5072 soft_set_attribute(soft_object_t
*object_p
, CK_ATTRIBUTE_PTR
template,
5077 CK_OBJECT_CLASS
class = object_p
->class;
5081 case CKO_PUBLIC_KEY
:
5082 rv
= soft_set_public_key_attribute(object_p
, template, copy
);
5085 case CKO_PRIVATE_KEY
:
5086 rv
= soft_set_private_key_attribute(object_p
, template, copy
);
5089 case CKO_SECRET_KEY
:
5090 rv
= soft_set_secret_key_attribute(object_p
, template, copy
);
5093 case CKO_DOMAIN_PARAMETERS
:
5094 switch (template->type
) {
5097 * Only the LABEL can be modified in the common
5098 * storage object attributes after the object is
5101 return (set_extra_attr_to_object(object_p
,
5102 CKA_LABEL
, template));
5104 return (CKR_TEMPLATE_INCONSISTENT
);
5106 case CKO_CERTIFICATE
:
5107 rv
= soft_set_certificate_attribute(object_p
, template, copy
);
5112 * If the template specifies a value of an attribute
5113 * which is incompatible with other existing attributes
5114 * of the object, then fails with return code
5115 * CKR_TEMPLATE_INCONSISTENT.
5117 rv
= CKR_TEMPLATE_INCONSISTENT
;
5125 soft_get_public_value(soft_object_t
*key
, CK_ATTRIBUTE_TYPE type
,
5126 uchar_t
*value
, uint32_t *value_len
)
5131 /* The following attributes belong to RSA */
5134 ((biginteger_t
*)OBJ_PUB_RSA_MOD(key
))->big_value_len
;
5136 /* This attribute MUST BE set */
5137 if (len
== 0 || len
> *value_len
) {
5138 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5142 (void) memcpy(value
,
5143 ((biginteger_t
*)OBJ_PUB_RSA_MOD(key
))->big_value
,
5148 case CKA_PUBLIC_EXPONENT
:
5150 ((biginteger_t
*)OBJ_PUB_RSA_PUBEXPO(key
))->big_value_len
;
5152 /* This attribute MUST BE set */
5153 if (len
== 0 || len
> *value_len
) {
5154 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5158 (void) memcpy(value
,
5159 ((biginteger_t
*)OBJ_PUB_RSA_PUBEXPO(key
))->big_value
,
5164 /* The following attributes belong to DSA and DH */
5167 if (key
->key_type
== CKK_DSA
)
5169 ((biginteger_t
*)OBJ_PUB_DSA_PRIME(key
))->
5173 ((biginteger_t
*)OBJ_PUB_DH_PRIME(key
))->
5176 /* This attribute MUST BE set */
5177 if (len
== 0 || len
> *value_len
) {
5178 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5182 if (key
->key_type
== CKK_DSA
)
5183 (void) memcpy(value
,
5184 ((biginteger_t
*)OBJ_PUB_DSA_PRIME(key
))->big_value
,
5187 (void) memcpy(value
,
5188 ((biginteger_t
*)OBJ_PUB_DH_PRIME(key
))->big_value
,
5195 ((biginteger_t
*)OBJ_PUB_DSA_SUBPRIME(key
))->big_value_len
;
5197 /* This attribute MUST BE set */
5198 if (len
== 0 || len
> *value_len
) {
5199 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5203 (void) memcpy(value
,
5204 ((biginteger_t
*)OBJ_PUB_DSA_SUBPRIME(key
))->big_value
,
5211 if (key
->key_type
== CKK_DSA
)
5213 ((biginteger_t
*)OBJ_PUB_DSA_BASE(key
))->
5217 ((biginteger_t
*)OBJ_PUB_DH_BASE(key
))->
5220 /* This attribute MUST BE set */
5221 if (len
== 0 || len
> *value_len
) {
5222 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5226 if (key
->key_type
== CKK_DSA
)
5227 (void) memcpy(value
,
5228 ((biginteger_t
*)OBJ_PUB_DSA_BASE(key
))->big_value
,
5231 (void) memcpy(value
,
5232 ((biginteger_t
*)OBJ_PUB_DH_BASE(key
))->big_value
,
5238 if (key
->key_type
== CKK_DSA
)
5240 ((biginteger_t
*)OBJ_PUB_DSA_VALUE(key
))->
5244 ((biginteger_t
*)OBJ_PUB_DH_VALUE(key
))->
5247 /* This attribute MUST BE set */
5248 if (len
== 0 || len
> *value_len
) {
5249 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5253 if (key
->key_type
== CKK_DSA
)
5254 (void) memcpy(value
,
5255 ((biginteger_t
*)OBJ_PUB_DSA_VALUE(key
))->big_value
,
5258 (void) memcpy(value
,
5259 ((biginteger_t
*)OBJ_PUB_DH_VALUE(key
))->big_value
,
5270 soft_get_private_value(soft_object_t
*key
, CK_ATTRIBUTE_TYPE type
,
5271 uchar_t
*value
, uint32_t *value_len
)
5278 /* The following attributes belong to RSA */
5281 ((biginteger_t
*)OBJ_PRI_RSA_MOD(key
))->big_value_len
;
5283 /* This attribute MUST BE set */
5284 if (len
== 0 || len
> *value_len
) {
5285 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5289 (void) memcpy(value
,
5290 ((biginteger_t
*)OBJ_PRI_RSA_MOD(key
))->big_value
,
5295 case CKA_PRIVATE_EXPONENT
:
5297 ((biginteger_t
*)OBJ_PRI_RSA_PRIEXPO(key
))->big_value_len
;
5299 /* This attribute MUST BE set */
5300 if (len
== 0 || len
> *value_len
) {
5301 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5305 (void) memcpy(value
,
5306 ((biginteger_t
*)OBJ_PRI_RSA_PRIEXPO(key
))->big_value
,
5313 ((biginteger_t
*)OBJ_PRI_RSA_PRIME1(key
))->big_value_len
;
5315 if (len
> *value_len
) {
5316 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5320 if (*value_len
== 0) {
5324 (void) memcpy(value
,
5325 ((biginteger_t
*)OBJ_PRI_RSA_PRIME1(key
))->big_value
,
5332 ((biginteger_t
*)OBJ_PRI_RSA_PRIME2(key
))->big_value_len
;
5334 if (len
> *value_len
) {
5335 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5339 if (*value_len
== 0) {
5343 (void) memcpy(value
,
5344 ((biginteger_t
*)OBJ_PRI_RSA_PRIME2(key
))->big_value
,
5349 case CKA_EXPONENT_1
:
5351 ((biginteger_t
*)OBJ_PRI_RSA_EXPO1(key
))->big_value_len
;
5353 if (len
> *value_len
) {
5354 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5358 if (*value_len
== 0) {
5362 (void) memcpy(value
,
5363 ((biginteger_t
*)OBJ_PRI_RSA_EXPO1(key
))->big_value
,
5368 case CKA_EXPONENT_2
:
5370 ((biginteger_t
*)OBJ_PRI_RSA_EXPO2(key
))->big_value_len
;
5372 if (len
> *value_len
) {
5373 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5377 if (*value_len
== 0) {
5381 (void) memcpy(value
,
5382 ((biginteger_t
*)OBJ_PRI_RSA_EXPO2(key
))->big_value
,
5387 case CKA_COEFFICIENT
:
5389 ((biginteger_t
*)OBJ_PRI_RSA_COEF(key
))->big_value_len
;
5391 if (len
> *value_len
) {
5392 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5396 if (*value_len
== 0) {
5400 (void) memcpy(value
,
5401 ((biginteger_t
*)OBJ_PRI_RSA_COEF(key
))->big_value
,
5406 /* The following attributes belong to DSA and DH */
5409 if (key
->key_type
== CKK_DSA
)
5411 ((biginteger_t
*)OBJ_PRI_DSA_PRIME(key
))->
5415 ((biginteger_t
*)OBJ_PRI_DH_PRIME(key
))->
5418 /* This attribute MUST BE set */
5419 if (len
== 0 || len
> *value_len
) {
5420 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5424 if (key
->key_type
== CKK_DSA
)
5425 (void) memcpy(value
,
5426 ((biginteger_t
*)OBJ_PRI_DSA_PRIME(key
))->big_value
,
5429 (void) memcpy(value
,
5430 ((biginteger_t
*)OBJ_PRI_DH_PRIME(key
))->big_value
,
5437 ((biginteger_t
*)OBJ_PRI_DSA_SUBPRIME(key
))->big_value_len
;
5439 /* This attribute MUST BE set */
5440 if (len
== 0 || len
> *value_len
) {
5441 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5445 (void) memcpy(value
,
5446 ((biginteger_t
*)OBJ_PRI_DSA_SUBPRIME(key
))->big_value
,
5453 if (key
->key_type
== CKK_DSA
)
5455 ((biginteger_t
*)OBJ_PRI_DSA_BASE(key
))->
5459 ((biginteger_t
*)OBJ_PRI_DH_BASE(key
))->
5462 /* This attribute MUST BE set */
5463 if (len
== 0 || len
> *value_len
) {
5464 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5468 if (key
->key_type
== CKK_DSA
)
5469 (void) memcpy(value
,
5470 ((biginteger_t
*)OBJ_PRI_DSA_BASE(key
))->big_value
,
5473 (void) memcpy(value
,
5474 ((biginteger_t
*)OBJ_PRI_DH_BASE(key
))->big_value
,
5480 if (key
->key_type
== CKK_DSA
) {
5482 ((biginteger_t
*)OBJ_PRI_DSA_VALUE(key
))->
5484 } else if (key
->key_type
== CKK_DH
) {
5486 ((biginteger_t
*)OBJ_PRI_DH_VALUE(key
))->
5490 ((biginteger_t
*)OBJ_PRI_EC_VALUE(key
))->
5494 /* This attribute MUST BE set */
5495 if (len
== 0 || len
> *value_len
) {
5496 return (CKR_ATTRIBUTE_VALUE_INVALID
);
5500 if (key
->key_type
== CKK_DSA
) {
5501 (void) memcpy(value
,
5502 ((biginteger_t
*)OBJ_PRI_DSA_VALUE(key
))->big_value
,
5504 } else if (key
->key_type
== CKK_DH
) {
5505 (void) memcpy(value
,
5506 ((biginteger_t
*)OBJ_PRI_DH_VALUE(key
))->big_value
,
5509 (void) memcpy(value
,
5510 ((biginteger_t
*)OBJ_PRI_EC_VALUE(key
))->big_value
,
5522 copy_bigint(biginteger_t
*new_bigint
, biginteger_t
*old_bigint
)
5524 new_bigint
->big_value
=
5525 malloc((sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
5527 if (new_bigint
->big_value
== NULL
) {
5528 return (CKR_HOST_MEMORY
);
5531 (void) memcpy(new_bigint
->big_value
, old_bigint
->big_value
,
5532 (sizeof (CK_BYTE
) * new_bigint
->big_value_len
));
5538 free_public_key_attr(public_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
5546 bigint_attr_cleanup(KEY_PUB_RSA_MOD(pbk
));
5547 bigint_attr_cleanup(KEY_PUB_RSA_PUBEXPO(pbk
));
5550 bigint_attr_cleanup(KEY_PUB_DSA_PRIME(pbk
));
5551 bigint_attr_cleanup(KEY_PUB_DSA_SUBPRIME(pbk
));
5552 bigint_attr_cleanup(KEY_PUB_DSA_BASE(pbk
));
5553 bigint_attr_cleanup(KEY_PUB_DSA_VALUE(pbk
));
5556 bigint_attr_cleanup(KEY_PUB_DH_PRIME(pbk
));
5557 bigint_attr_cleanup(KEY_PUB_DH_BASE(pbk
));
5558 bigint_attr_cleanup(KEY_PUB_DH_VALUE(pbk
));
5561 bigint_attr_cleanup(KEY_PUB_EC_POINT(pbk
));
5564 bigint_attr_cleanup(KEY_PUB_DH942_PRIME(pbk
));
5565 bigint_attr_cleanup(KEY_PUB_DH942_SUBPRIME(pbk
));
5566 bigint_attr_cleanup(KEY_PUB_DH942_BASE(pbk
));
5567 bigint_attr_cleanup(KEY_PUB_DH942_VALUE(pbk
));
5576 soft_copy_public_key_attr(public_key_obj_t
*old_pub_key_obj_p
,
5577 public_key_obj_t
**new_pub_key_obj_p
, CK_KEY_TYPE key_type
)
5580 public_key_obj_t
*pbk
;
5583 pbk
= calloc(1, sizeof (public_key_obj_t
));
5585 return (CKR_HOST_MEMORY
);
5590 (void) memcpy(KEY_PUB_RSA(pbk
),
5591 KEY_PUB_RSA(old_pub_key_obj_p
),
5592 sizeof (rsa_pub_key_t
));
5594 rv
= copy_bigint(KEY_PUB_RSA_MOD(pbk
),
5595 KEY_PUB_RSA_MOD(old_pub_key_obj_p
));
5597 free_public_key_attr(pbk
, key_type
);
5600 /* copy public exponent */
5601 rv
= copy_bigint(KEY_PUB_RSA_PUBEXPO(pbk
),
5602 KEY_PUB_RSA_PUBEXPO(old_pub_key_obj_p
));
5604 free_public_key_attr(pbk
, key_type
);
5609 (void) memcpy(KEY_PUB_DSA(pbk
),
5610 KEY_PUB_DSA(old_pub_key_obj_p
),
5611 sizeof (dsa_pub_key_t
));
5614 rv
= copy_bigint(KEY_PUB_DSA_PRIME(pbk
),
5615 KEY_PUB_DSA_PRIME(old_pub_key_obj_p
));
5617 free_public_key_attr(pbk
, key_type
);
5622 rv
= copy_bigint(KEY_PUB_DSA_SUBPRIME(pbk
),
5623 KEY_PUB_DSA_SUBPRIME(old_pub_key_obj_p
));
5625 free_public_key_attr(pbk
, key_type
);
5630 rv
= copy_bigint(KEY_PUB_DSA_BASE(pbk
),
5631 KEY_PUB_DSA_BASE(old_pub_key_obj_p
));
5633 free_public_key_attr(pbk
, key_type
);
5638 rv
= copy_bigint(KEY_PUB_DSA_VALUE(pbk
),
5639 KEY_PUB_DSA_VALUE(old_pub_key_obj_p
));
5641 free_public_key_attr(pbk
, key_type
);
5646 (void) memcpy(KEY_PUB_DH(pbk
),
5647 KEY_PUB_DH(old_pub_key_obj_p
),
5648 sizeof (dh_pub_key_t
));
5651 rv
= copy_bigint(KEY_PUB_DH_PRIME(pbk
),
5652 KEY_PUB_DH_PRIME(old_pub_key_obj_p
));
5654 free_public_key_attr(pbk
, key_type
);
5659 rv
= copy_bigint(KEY_PUB_DH_BASE(pbk
),
5660 KEY_PUB_DH_BASE(old_pub_key_obj_p
));
5662 free_public_key_attr(pbk
, key_type
);
5667 rv
= copy_bigint(KEY_PUB_DH_VALUE(pbk
),
5668 KEY_PUB_DH_VALUE(old_pub_key_obj_p
));
5670 free_public_key_attr(pbk
, key_type
);
5675 (void) memcpy(KEY_PUB_EC(pbk
),
5676 KEY_PUB_EC(old_pub_key_obj_p
),
5677 sizeof (ec_pub_key_t
));
5680 rv
= copy_bigint(KEY_PUB_EC_POINT(pbk
),
5681 KEY_PUB_EC_POINT(old_pub_key_obj_p
));
5683 free_public_key_attr(pbk
, key_type
);
5688 (void) memcpy(KEY_PUB_DH942(pbk
),
5689 KEY_PUB_DH942(old_pub_key_obj_p
),
5690 sizeof (dh942_pub_key_t
));
5693 rv
= copy_bigint(KEY_PUB_DH942_PRIME(pbk
),
5694 KEY_PUB_DH942_PRIME(old_pub_key_obj_p
));
5696 free_public_key_attr(pbk
, key_type
);
5701 rv
= copy_bigint(KEY_PUB_DH942_SUBPRIME(pbk
),
5702 KEY_PUB_DH942_SUBPRIME(old_pub_key_obj_p
));
5704 free_public_key_attr(pbk
, key_type
);
5709 rv
= copy_bigint(KEY_PUB_DH942_BASE(pbk
),
5710 KEY_PUB_DH942_BASE(old_pub_key_obj_p
));
5712 free_public_key_attr(pbk
, key_type
);
5717 rv
= copy_bigint(KEY_PUB_DH942_VALUE(pbk
),
5718 KEY_PUB_DH942_VALUE(old_pub_key_obj_p
));
5720 free_public_key_attr(pbk
, key_type
);
5727 *new_pub_key_obj_p
= pbk
;
5732 free_private_key_attr(private_key_obj_t
*pbk
, CK_KEY_TYPE key_type
)
5740 bigint_attr_cleanup(KEY_PRI_RSA_MOD(pbk
));
5741 bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(pbk
));
5742 bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(pbk
));
5743 bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(pbk
));
5744 bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(pbk
));
5745 bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(pbk
));
5746 bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(pbk
));
5747 bigint_attr_cleanup(KEY_PRI_RSA_COEF(pbk
));
5750 bigint_attr_cleanup(KEY_PRI_DSA_PRIME(pbk
));
5751 bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(pbk
));
5752 bigint_attr_cleanup(KEY_PRI_DSA_BASE(pbk
));
5753 bigint_attr_cleanup(KEY_PRI_DSA_VALUE(pbk
));
5756 bigint_attr_cleanup(KEY_PRI_DH_PRIME(pbk
));
5757 bigint_attr_cleanup(KEY_PRI_DH_BASE(pbk
));
5758 bigint_attr_cleanup(KEY_PRI_DH_VALUE(pbk
));
5761 bigint_attr_cleanup(KEY_PRI_EC_VALUE(pbk
));
5764 bigint_attr_cleanup(KEY_PRI_DH942_PRIME(pbk
));
5765 bigint_attr_cleanup(KEY_PRI_DH942_SUBPRIME(pbk
));
5766 bigint_attr_cleanup(KEY_PRI_DH942_BASE(pbk
));
5767 bigint_attr_cleanup(KEY_PRI_DH942_VALUE(pbk
));
5776 soft_copy_private_key_attr(private_key_obj_t
*old_pri_key_obj_p
,
5777 private_key_obj_t
**new_pri_key_obj_p
, CK_KEY_TYPE key_type
)
5780 private_key_obj_t
*pbk
;
5782 pbk
= calloc(1, sizeof (private_key_obj_t
));
5784 return (CKR_HOST_MEMORY
);
5789 (void) memcpy(KEY_PRI_RSA(pbk
),
5790 KEY_PRI_RSA(old_pri_key_obj_p
),
5791 sizeof (rsa_pri_key_t
));
5793 rv
= copy_bigint(KEY_PRI_RSA_MOD(pbk
),
5794 KEY_PRI_RSA_MOD(old_pri_key_obj_p
));
5796 free_private_key_attr(pbk
, key_type
);
5799 /* copy public exponent */
5800 rv
= copy_bigint(KEY_PRI_RSA_PUBEXPO(pbk
),
5801 KEY_PRI_RSA_PUBEXPO(old_pri_key_obj_p
));
5803 free_private_key_attr(pbk
, key_type
);
5806 /* copy private exponent */
5807 rv
= copy_bigint(KEY_PRI_RSA_PRIEXPO(pbk
),
5808 KEY_PRI_RSA_PRIEXPO(old_pri_key_obj_p
));
5810 free_private_key_attr(pbk
, key_type
);
5814 rv
= copy_bigint(KEY_PRI_RSA_PRIME1(pbk
),
5815 KEY_PRI_RSA_PRIME1(old_pri_key_obj_p
));
5817 free_private_key_attr(pbk
, key_type
);
5821 rv
= copy_bigint(KEY_PRI_RSA_PRIME2(pbk
),
5822 KEY_PRI_RSA_PRIME2(old_pri_key_obj_p
));
5824 free_private_key_attr(pbk
, key_type
);
5827 /* copy exponent_1 */
5828 rv
= copy_bigint(KEY_PRI_RSA_EXPO1(pbk
),
5829 KEY_PRI_RSA_EXPO1(old_pri_key_obj_p
));
5831 free_private_key_attr(pbk
, key_type
);
5834 /* copy exponent_2 */
5835 rv
= copy_bigint(KEY_PRI_RSA_EXPO2(pbk
),
5836 KEY_PRI_RSA_EXPO2(old_pri_key_obj_p
));
5838 free_private_key_attr(pbk
, key_type
);
5841 /* copy coefficient */
5842 rv
= copy_bigint(KEY_PRI_RSA_COEF(pbk
),
5843 KEY_PRI_RSA_COEF(old_pri_key_obj_p
));
5845 free_private_key_attr(pbk
, key_type
);
5850 (void) memcpy(KEY_PRI_DSA(pbk
),
5851 KEY_PRI_DSA(old_pri_key_obj_p
),
5852 sizeof (dsa_pri_key_t
));
5855 rv
= copy_bigint(KEY_PRI_DSA_PRIME(pbk
),
5856 KEY_PRI_DSA_PRIME(old_pri_key_obj_p
));
5858 free_private_key_attr(pbk
, key_type
);
5863 rv
= copy_bigint(KEY_PRI_DSA_SUBPRIME(pbk
),
5864 KEY_PRI_DSA_SUBPRIME(old_pri_key_obj_p
));
5866 free_private_key_attr(pbk
, key_type
);
5871 rv
= copy_bigint(KEY_PRI_DSA_BASE(pbk
),
5872 KEY_PRI_DSA_BASE(old_pri_key_obj_p
));
5874 free_private_key_attr(pbk
, key_type
);
5879 rv
= copy_bigint(KEY_PRI_DSA_VALUE(pbk
),
5880 KEY_PRI_DSA_VALUE(old_pri_key_obj_p
));
5882 free_private_key_attr(pbk
, key_type
);
5887 (void) memcpy(KEY_PRI_DH(pbk
),
5888 KEY_PRI_DH(old_pri_key_obj_p
),
5889 sizeof (dh_pri_key_t
));
5892 rv
= copy_bigint(KEY_PRI_DH_PRIME(pbk
),
5893 KEY_PRI_DH_PRIME(old_pri_key_obj_p
));
5895 free_private_key_attr(pbk
, key_type
);
5900 rv
= copy_bigint(KEY_PRI_DH_BASE(pbk
),
5901 KEY_PRI_DH_BASE(old_pri_key_obj_p
));
5903 free_private_key_attr(pbk
, key_type
);
5908 rv
= copy_bigint(KEY_PRI_DH_VALUE(pbk
),
5909 KEY_PRI_DH_VALUE(old_pri_key_obj_p
));
5911 free_private_key_attr(pbk
, key_type
);
5916 (void) memcpy(KEY_PRI_EC(pbk
),
5917 KEY_PRI_EC(old_pri_key_obj_p
),
5918 sizeof (ec_pri_key_t
));
5921 rv
= copy_bigint(KEY_PRI_EC_VALUE(pbk
),
5922 KEY_PRI_EC_VALUE(old_pri_key_obj_p
));
5924 free_private_key_attr(pbk
, key_type
);
5929 (void) memcpy(KEY_PRI_DH942(pbk
),
5930 KEY_PRI_DH942(old_pri_key_obj_p
),
5931 sizeof (dh942_pri_key_t
));
5934 rv
= copy_bigint(KEY_PRI_DH942_PRIME(pbk
),
5935 KEY_PRI_DH942_PRIME(old_pri_key_obj_p
));
5937 free_private_key_attr(pbk
, key_type
);
5942 rv
= copy_bigint(KEY_PRI_DH942_SUBPRIME(pbk
),
5943 KEY_PRI_DH942_SUBPRIME(old_pri_key_obj_p
));
5945 free_private_key_attr(pbk
, key_type
);
5950 rv
= copy_bigint(KEY_PRI_DH942_BASE(pbk
),
5951 KEY_PRI_DH942_BASE(old_pri_key_obj_p
));
5953 free_private_key_attr(pbk
, key_type
);
5958 rv
= copy_bigint(KEY_PRI_DH942_VALUE(pbk
),
5959 KEY_PRI_DH942_VALUE(old_pri_key_obj_p
));
5961 free_private_key_attr(pbk
, key_type
);
5968 *new_pri_key_obj_p
= pbk
;
5973 free_domain_attr(domain_obj_t
*domain
, CK_KEY_TYPE key_type
)
5975 if (domain
== NULL
) {
5981 bigint_attr_cleanup(KEY_DOM_DSA_PRIME(domain
));
5982 bigint_attr_cleanup(KEY_DOM_DSA_SUBPRIME(domain
));
5983 bigint_attr_cleanup(KEY_DOM_DSA_BASE(domain
));
5986 bigint_attr_cleanup(KEY_DOM_DH_PRIME(domain
));
5987 bigint_attr_cleanup(KEY_DOM_DH_BASE(domain
));
5990 bigint_attr_cleanup(KEY_DOM_DH942_PRIME(domain
));
5991 bigint_attr_cleanup(KEY_DOM_DH942_SUBPRIME(domain
));
5992 bigint_attr_cleanup(KEY_DOM_DH942_BASE(domain
));
6001 soft_copy_domain_attr(domain_obj_t
*old_domain_obj_p
,
6002 domain_obj_t
**new_domain_obj_p
, CK_KEY_TYPE key_type
)
6005 domain_obj_t
*domain
;
6007 domain
= calloc(1, sizeof (domain_obj_t
));
6008 if (domain
== NULL
) {
6009 return (CKR_HOST_MEMORY
);
6014 (void) memcpy(KEY_DOM_DSA(domain
),
6015 KEY_DOM_DSA(old_domain_obj_p
),
6016 sizeof (dsa_dom_key_t
));
6019 rv
= copy_bigint(KEY_DOM_DSA_PRIME(domain
),
6020 KEY_DOM_DSA_PRIME(old_domain_obj_p
));
6022 free_domain_attr(domain
, key_type
);
6027 rv
= copy_bigint(KEY_DOM_DSA_SUBPRIME(domain
),
6028 KEY_DOM_DSA_SUBPRIME(old_domain_obj_p
));
6030 free_domain_attr(domain
, key_type
);
6035 rv
= copy_bigint(KEY_DOM_DSA_BASE(domain
),
6036 KEY_DOM_DSA_BASE(old_domain_obj_p
));
6038 free_domain_attr(domain
, key_type
);
6044 (void) memcpy(KEY_DOM_DH(domain
),
6045 KEY_DOM_DH(old_domain_obj_p
),
6046 sizeof (dh_dom_key_t
));
6049 rv
= copy_bigint(KEY_DOM_DH_PRIME(domain
),
6050 KEY_DOM_DH_PRIME(old_domain_obj_p
));
6052 free_domain_attr(domain
, key_type
);
6057 rv
= copy_bigint(KEY_DOM_DH_BASE(domain
),
6058 KEY_DOM_DH_BASE(old_domain_obj_p
));
6060 free_domain_attr(domain
, key_type
);
6066 (void) memcpy(KEY_DOM_DH942(domain
),
6067 KEY_DOM_DH942(old_domain_obj_p
),
6068 sizeof (dh942_dom_key_t
));
6071 rv
= copy_bigint(KEY_DOM_DH942_PRIME(domain
),
6072 KEY_DOM_DH942_PRIME(old_domain_obj_p
));
6074 free_domain_attr(domain
, key_type
);
6079 rv
= copy_bigint(KEY_DOM_DH942_SUBPRIME(domain
),
6080 KEY_DOM_DH942_SUBPRIME(old_domain_obj_p
));
6082 free_domain_attr(domain
, key_type
);
6087 rv
= copy_bigint(KEY_DOM_DH942_BASE(domain
),
6088 KEY_DOM_DH942_BASE(old_domain_obj_p
));
6090 free_domain_attr(domain
, key_type
);
6098 *new_domain_obj_p
= domain
;
6103 soft_copy_secret_key_attr(secret_key_obj_t
*old_secret_key_obj_p
,
6104 secret_key_obj_t
**new_secret_key_obj_p
)
6106 secret_key_obj_t
*sk
;
6108 sk
= malloc(sizeof (secret_key_obj_t
));
6110 return (CKR_HOST_MEMORY
);
6112 (void) memcpy(sk
, old_secret_key_obj_p
, sizeof (secret_key_obj_t
));
6114 /* copy the secret key value */
6115 sk
->sk_value
= malloc((sizeof (CK_BYTE
) * sk
->sk_value_len
));
6116 if (sk
->sk_value
== NULL
) {
6118 return (CKR_HOST_MEMORY
);
6120 (void) memcpy(sk
->sk_value
, old_secret_key_obj_p
->sk_value
,
6121 (sizeof (CK_BYTE
) * sk
->sk_value_len
));
6124 * Copy the pre-expanded key schedule.
6126 if (old_secret_key_obj_p
->key_sched
!= NULL
&&
6127 old_secret_key_obj_p
->keysched_len
> 0) {
6128 sk
->key_sched
= malloc(old_secret_key_obj_p
->keysched_len
);
6129 if (sk
->key_sched
== NULL
) {
6131 return (CKR_HOST_MEMORY
);
6133 sk
->keysched_len
= old_secret_key_obj_p
->keysched_len
;
6134 (void) memcpy(sk
->key_sched
, old_secret_key_obj_p
->key_sched
,
6138 *new_secret_key_obj_p
= sk
;
6144 * If CKA_CLASS not given, guess CKA_CLASS using
6145 * attributes on template .
6147 * Some attributes are specific to an object class. If one or more
6148 * of these attributes are in the template, make a list of classes
6149 * that can have these attributes. This would speed up the search later,
6150 * because we can immediately skip an object if the class of that
6151 * object can not possibly contain one of the attributes.
6155 soft_process_find_attr(CK_OBJECT_CLASS
*pclasses
,
6156 CK_ULONG
*num_result_pclasses
, CK_ATTRIBUTE_PTR pTemplate
,
6161 boolean_t pub_found
= B_FALSE
,
6162 priv_found
= B_FALSE
,
6163 secret_found
= B_FALSE
,
6164 domain_found
= B_FALSE
,
6165 hardware_found
= B_FALSE
,
6166 cert_found
= B_FALSE
;
6167 int num_pub_key_attrs
, num_priv_key_attrs
,
6168 num_secret_key_attrs
, num_domain_attrs
,
6169 num_hardware_attrs
, num_cert_attrs
;
6170 int num_pclasses
= 0;
6172 for (i
= 0; i
< ulCount
; i
++) {
6173 if (pTemplate
[i
].type
== CKA_CLASS
) {
6175 * don't need to guess the class, it is specified.
6176 * Just record the class, and return.
6179 (*((CK_OBJECT_CLASS
*)pTemplate
[i
].pValue
));
6180 *num_result_pclasses
= 1;
6186 sizeof (PUB_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6187 num_priv_key_attrs
=
6188 sizeof (PRIV_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6189 num_secret_key_attrs
=
6190 sizeof (SECRET_KEY_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6192 sizeof (DOMAIN_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6193 num_hardware_attrs
=
6194 sizeof (HARDWARE_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6196 sizeof (CERT_ATTRS
) / sizeof (CK_ATTRIBUTE_TYPE
);
6199 * Get the list of objects class that might contain
6202 for (i
= 0; i
< ulCount
; i
++) {
6204 * only check if this attribute can belong to public key object
6205 * class if public key object isn't already in the list
6208 for (j
= 0; j
< num_pub_key_attrs
; j
++) {
6209 if (pTemplate
[i
].type
== PUB_KEY_ATTRS
[j
]) {
6211 pclasses
[num_pclasses
++] =
6219 for (j
= 0; j
< num_priv_key_attrs
; j
++) {
6220 if (pTemplate
[i
].type
== PRIV_KEY_ATTRS
[j
]) {
6221 priv_found
= B_TRUE
;
6222 pclasses
[num_pclasses
++] =
6229 if (!secret_found
) {
6230 for (j
= 0; j
< num_secret_key_attrs
; j
++) {
6231 if (pTemplate
[i
].type
== SECRET_KEY_ATTRS
[j
]) {
6232 secret_found
= B_TRUE
;
6233 pclasses
[num_pclasses
++] =
6240 if (!domain_found
) {
6241 for (j
= 0; j
< num_domain_attrs
; j
++) {
6242 if (pTemplate
[i
].type
== DOMAIN_ATTRS
[j
]) {
6243 domain_found
= B_TRUE
;
6244 pclasses
[num_pclasses
++] =
6245 CKO_DOMAIN_PARAMETERS
;
6251 if (!hardware_found
) {
6252 for (j
= 0; j
< num_hardware_attrs
; j
++) {
6253 if (pTemplate
[i
].type
== HARDWARE_ATTRS
[j
]) {
6254 hardware_found
= B_TRUE
;
6255 pclasses
[num_pclasses
++] =
6263 for (j
= 0; j
< num_cert_attrs
; j
++) {
6264 if (pTemplate
[i
].type
== CERT_ATTRS
[j
]) {
6265 cert_found
= B_TRUE
;
6266 pclasses
[num_pclasses
++] =
6273 *num_result_pclasses
= num_pclasses
;
6277 soft_find_match_attrs(soft_object_t
*obj
, CK_OBJECT_CLASS
*pclasses
,
6278 CK_ULONG num_pclasses
, CK_ATTRIBUTE
*template, CK_ULONG num_attr
)
6281 CK_ATTRIBUTE
*tmpl_attr
, *obj_attr
;
6282 cert_attr_t
*cert_attr
;
6284 biginteger_t
*bigint
;
6285 boolean_t compare_attr
, compare_bigint
, compare_boolean
;
6286 boolean_t compare_cert_val
, compare_cert_type
;
6289 * Check if the class of this object match with any
6290 * of object classes that can possibly contain the
6291 * requested attributes.
6293 if (num_pclasses
> 0) {
6294 for (i
= 0; i
< num_pclasses
; i
++) {
6295 if (obj
->class == pclasses
[i
]) {
6299 if (i
== num_pclasses
) {
6301 * this object can't possibly contain one or
6302 * more attributes, don't need to check this object
6308 /* need to examine everything */
6309 for (i
= 0; i
< num_attr
; i
++) {
6310 tmpl_attr
= &(template[i
]);
6311 compare_attr
= B_FALSE
;
6312 compare_bigint
= B_FALSE
;
6313 compare_boolean
= B_FALSE
;
6314 compare_cert_val
= B_FALSE
;
6315 compare_cert_type
= B_FALSE
;
6316 switch (tmpl_attr
->type
) {
6317 /* First, check the most common attributes */
6319 if (*((CK_OBJECT_CLASS
*)tmpl_attr
->pValue
) !=
6325 if (*((CK_KEY_TYPE
*)tmpl_attr
->pValue
) !=
6331 attr_mask
= (obj
->bool_attr_mask
) & ENCRYPT_BOOL_ON
;
6332 compare_boolean
= B_TRUE
;
6335 attr_mask
= (obj
->bool_attr_mask
) & DECRYPT_BOOL_ON
;
6336 compare_boolean
= B_TRUE
;
6339 attr_mask
= (obj
->bool_attr_mask
) & WRAP_BOOL_ON
;
6340 compare_boolean
= B_TRUE
;
6343 attr_mask
= (obj
->bool_attr_mask
) & UNWRAP_BOOL_ON
;
6344 compare_boolean
= B_TRUE
;
6347 attr_mask
= (obj
->bool_attr_mask
) & SIGN_BOOL_ON
;
6348 compare_boolean
= B_TRUE
;
6350 case CKA_SIGN_RECOVER
:
6351 attr_mask
= (obj
->bool_attr_mask
) &
6352 SIGN_RECOVER_BOOL_ON
;
6353 compare_boolean
= B_TRUE
;
6356 attr_mask
= (obj
->bool_attr_mask
) & VERIFY_BOOL_ON
;
6357 compare_boolean
= B_TRUE
;
6359 case CKA_VERIFY_RECOVER
:
6360 attr_mask
= (obj
->bool_attr_mask
) &
6361 VERIFY_RECOVER_BOOL_ON
;
6362 compare_boolean
= B_TRUE
;
6365 attr_mask
= (obj
->bool_attr_mask
) & DERIVE_BOOL_ON
;
6366 compare_boolean
= B_TRUE
;
6369 attr_mask
= (obj
->bool_attr_mask
) & LOCAL_BOOL_ON
;
6370 compare_boolean
= B_TRUE
;
6373 attr_mask
= (obj
->bool_attr_mask
) & SENSITIVE_BOOL_ON
;
6374 compare_boolean
= B_TRUE
;
6376 case CKA_SECONDARY_AUTH
:
6377 attr_mask
= (obj
->bool_attr_mask
) &
6378 SECONDARY_AUTH_BOOL_ON
;
6379 compare_boolean
= B_TRUE
;
6382 attr_mask
= (obj
->bool_attr_mask
) & TRUSTED_BOOL_ON
;
6383 compare_boolean
= B_TRUE
;
6385 case CKA_EXTRACTABLE
:
6386 attr_mask
= (obj
->bool_attr_mask
) &
6387 EXTRACTABLE_BOOL_ON
;
6388 compare_boolean
= B_TRUE
;
6390 case CKA_ALWAYS_SENSITIVE
:
6391 attr_mask
= (obj
->bool_attr_mask
) &
6392 ALWAYS_SENSITIVE_BOOL_ON
;
6393 compare_boolean
= B_TRUE
;
6395 case CKA_NEVER_EXTRACTABLE
:
6396 attr_mask
= (obj
->bool_attr_mask
) &
6397 NEVER_EXTRACTABLE_BOOL_ON
;
6398 compare_boolean
= B_TRUE
;
6401 attr_mask
= (obj
->object_type
) & TOKEN_OBJECT
;
6402 compare_boolean
= B_TRUE
;
6405 attr_mask
= (obj
->object_type
) & PRIVATE_OBJECT
;
6406 compare_boolean
= B_TRUE
;
6408 case CKA_MODIFIABLE
:
6411 attr_mask
= (obj
->bool_attr_mask
) &
6412 NOT_MODIFIABLE_BOOL_ON
;
6419 if (bval
!= *((CK_BBOOL
*)tmpl_attr
->pValue
)) {
6426 * For X.509 attribute certificate object, get its
6427 * CKA_OWNER attribute from the x509_attr_cert_t struct.
6429 if ((obj
->class == CKO_CERTIFICATE
) &&
6430 (obj
->cert_type
== CKC_X_509_ATTR_CERT
)) {
6431 cert_attr
= X509_ATTR_CERT_OWNER(obj
);
6432 compare_cert_val
= B_TRUE
;
6437 * For X.509 certificate object, get its CKA_SUBJECT
6438 * attribute from the x509_cert_t struct (not from
6439 * the extra_attrlistp).
6441 if ((obj
->class == CKO_CERTIFICATE
) &&
6442 (obj
->cert_type
== CKC_X_509
)) {
6443 cert_attr
= X509_CERT_SUBJECT(obj
);
6444 compare_cert_val
= B_TRUE
;
6449 case CKA_START_DATE
:
6451 case CKA_KEY_GEN_MECHANISM
:
6454 case CKA_SERIAL_NUMBER
:
6456 case CKA_ATTR_TYPES
:
6457 /* find these attributes from extra_attrlistp */
6458 obj_attr
= get_extra_attr(tmpl_attr
->type
, obj
);
6459 compare_attr
= B_TRUE
;
6461 case CKA_CERTIFICATE_TYPE
:
6462 compare_cert_type
= B_TRUE
;
6465 /* only secret key has this attribute */
6466 if (obj
->class == CKO_SECRET_KEY
) {
6467 if (*((CK_ULONG
*)tmpl_attr
->pValue
) !=
6468 OBJ_SEC_VALUE_LEN(obj
)) {
6476 switch (obj
->class) {
6477 case CKO_SECRET_KEY
:
6479 * secret_key_obj_t is the same as
6482 bigint
= (biginteger_t
*)OBJ_SEC(obj
);
6483 compare_bigint
= B_TRUE
;
6485 case CKO_PRIVATE_KEY
:
6486 if (obj
->key_type
== CKK_DSA
) {
6487 bigint
= OBJ_PRI_DSA_VALUE(obj
);
6488 } else if (obj
->key_type
== CKK_DH
) {
6489 bigint
= OBJ_PRI_DH_VALUE(obj
);
6490 } else if (obj
->key_type
== CKK_X9_42_DH
) {
6491 bigint
= OBJ_PRI_DH942_VALUE(obj
);
6495 compare_bigint
= B_TRUE
;
6497 case CKO_PUBLIC_KEY
:
6498 if (obj
->key_type
== CKK_DSA
) {
6499 bigint
= OBJ_PUB_DSA_VALUE(obj
);
6500 } else if (obj
->key_type
== CKK_DH
) {
6501 bigint
= OBJ_PUB_DH_VALUE(obj
);
6502 } else if (obj
->key_type
== CKK_X9_42_DH
) {
6503 bigint
= OBJ_PUB_DH942_VALUE(obj
);
6507 compare_bigint
= B_TRUE
;
6509 case CKO_CERTIFICATE
:
6510 if (obj
->cert_type
== CKC_X_509
) {
6511 cert_attr
= X509_CERT_VALUE(obj
);
6512 } else if (obj
->cert_type
==
6513 CKC_X_509_ATTR_CERT
) {
6514 cert_attr
= X509_ATTR_CERT_VALUE(obj
);
6516 compare_cert_val
= B_TRUE
;
6523 /* only RSA public and private key have this attr */
6524 if (obj
->key_type
== CKK_RSA
) {
6525 if (obj
->class == CKO_PUBLIC_KEY
) {
6526 bigint
= OBJ_PUB_RSA_MOD(obj
);
6527 } else if (obj
->class == CKO_PRIVATE_KEY
) {
6528 bigint
= OBJ_PRI_RSA_MOD(obj
);
6532 compare_bigint
= B_TRUE
;
6537 case CKA_MODULUS_BITS
:
6538 /* only RSA public key has this attribute */
6539 if ((obj
->key_type
== CKK_RSA
) &&
6540 (obj
->class == CKO_PUBLIC_KEY
)) {
6541 CK_ULONG mod_bits
= OBJ_PUB_RSA_MOD_BITS(obj
);
6543 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
6550 case CKA_PUBLIC_EXPONENT
:
6551 /* only RSA public and private key have this attr */
6552 if (obj
->key_type
== CKK_RSA
) {
6553 if (obj
->class == CKO_PUBLIC_KEY
) {
6554 bigint
= OBJ_PUB_RSA_PUBEXPO(obj
);
6555 } else if (obj
->class == CKO_PRIVATE_KEY
) {
6556 bigint
= OBJ_PRI_RSA_PUBEXPO(obj
);
6560 compare_bigint
= B_TRUE
;
6565 case CKA_PRIVATE_EXPONENT
:
6566 /* only RSA private key has this attribute */
6567 if ((obj
->key_type
== CKK_RSA
) &&
6568 (obj
->class == CKO_PRIVATE_KEY
)) {
6569 bigint
= OBJ_PRI_RSA_PRIEXPO(obj
);
6570 compare_bigint
= B_TRUE
;
6576 /* only RSA private key has this attribute */
6577 if ((obj
->key_type
== CKK_RSA
) &&
6578 (obj
->class == CKO_PRIVATE_KEY
)) {
6579 bigint
= OBJ_PRI_RSA_PRIME1(obj
);
6580 compare_bigint
= B_TRUE
;
6586 /* only RSA private key has this attribute */
6587 if ((obj
->key_type
== CKK_RSA
) &&
6588 (obj
->class == CKO_PRIVATE_KEY
)) {
6589 bigint
= OBJ_PRI_RSA_PRIME2(obj
);
6590 compare_bigint
= B_TRUE
;
6595 case CKA_EXPONENT_1
:
6596 /* only RSA private key has this attribute */
6597 if ((obj
->key_type
== CKK_RSA
) &&
6598 (obj
->class == CKO_PRIVATE_KEY
)) {
6599 bigint
= OBJ_PRI_RSA_EXPO1(obj
);
6600 compare_bigint
= B_TRUE
;
6605 case CKA_EXPONENT_2
:
6606 /* only RSA private key has this attribute */
6607 if ((obj
->key_type
== CKK_RSA
) &&
6608 (obj
->class == CKO_PRIVATE_KEY
)) {
6609 bigint
= OBJ_PRI_RSA_EXPO2(obj
);
6610 compare_bigint
= B_TRUE
;
6615 case CKA_COEFFICIENT
:
6616 /* only RSA private key has this attribute */
6617 if ((obj
->key_type
== CKK_RSA
) &&
6618 (obj
->class == CKO_PRIVATE_KEY
)) {
6619 bigint
= OBJ_PRI_RSA_COEF(obj
);
6620 compare_bigint
= B_TRUE
;
6625 case CKA_VALUE_BITS
:
6626 /* only Diffie-Hellman private key has this attr */
6627 if ((obj
->key_type
== CKK_DH
) &&
6628 (obj
->class == CKO_PRIVATE_KEY
)) {
6629 CK_ULONG val_bits
= OBJ_PRI_DH_VAL_BITS(obj
);
6631 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
6639 if (obj
->class == CKO_PUBLIC_KEY
) {
6640 switch (obj
->key_type
) {
6642 bigint
= OBJ_PUB_DSA_PRIME(obj
);
6645 bigint
= OBJ_PUB_DH_PRIME(obj
);
6648 bigint
= OBJ_PUB_DH942_PRIME(obj
);
6653 } else if (obj
->class == CKO_PRIVATE_KEY
) {
6654 switch (obj
->key_type
) {
6656 bigint
= OBJ_PRI_DSA_PRIME(obj
);
6659 bigint
= OBJ_PRI_DH_PRIME(obj
);
6662 bigint
= OBJ_PRI_DH942_PRIME(obj
);
6667 } else if (obj
->class == CKO_DOMAIN_PARAMETERS
) {
6668 switch (obj
->key_type
) {
6670 bigint
= OBJ_DOM_DSA_PRIME(obj
);
6673 bigint
= OBJ_DOM_DH_PRIME(obj
);
6676 bigint
= OBJ_DOM_DH942_PRIME(obj
);
6684 compare_bigint
= B_TRUE
;
6687 if (obj
->class == CKO_PUBLIC_KEY
) {
6688 switch (obj
->key_type
) {
6690 bigint
= OBJ_PUB_DSA_SUBPRIME(obj
);
6693 bigint
= OBJ_PUB_DH942_SUBPRIME(obj
);
6698 } else if (obj
->class == CKO_PRIVATE_KEY
) {
6699 switch (obj
->key_type
) {
6701 bigint
= OBJ_PRI_DSA_SUBPRIME(obj
);
6704 bigint
= OBJ_PRI_DH942_SUBPRIME(obj
);
6709 } else if (obj
->class == CKO_DOMAIN_PARAMETERS
) {
6710 switch (obj
->key_type
) {
6712 bigint
= OBJ_DOM_DSA_SUBPRIME(obj
);
6715 bigint
= OBJ_DOM_DH942_SUBPRIME(obj
);
6723 compare_bigint
= B_TRUE
;
6726 if (obj
->class == CKO_PUBLIC_KEY
) {
6727 switch (obj
->key_type
) {
6729 bigint
= OBJ_PUB_DSA_BASE(obj
);
6732 bigint
= OBJ_PUB_DH_BASE(obj
);
6735 bigint
= OBJ_PUB_DH942_BASE(obj
);
6740 } else if (obj
->class == CKO_PRIVATE_KEY
) {
6741 switch (obj
->key_type
) {
6743 bigint
= OBJ_PRI_DSA_BASE(obj
);
6746 bigint
= OBJ_PRI_DH_BASE(obj
);
6749 bigint
= OBJ_PRI_DH942_BASE(obj
);
6754 } else if (obj
->class == CKO_DOMAIN_PARAMETERS
) {
6755 switch (obj
->key_type
) {
6757 bigint
= OBJ_DOM_DSA_BASE(obj
);
6760 bigint
= OBJ_DOM_DH_BASE(obj
);
6763 bigint
= OBJ_DOM_DH942_BASE(obj
);
6771 compare_bigint
= B_TRUE
;
6773 case CKA_PRIME_BITS
:
6774 if (obj
->class == CKO_DOMAIN_PARAMETERS
) {
6775 CK_ULONG prime_bits
;
6776 if (obj
->key_type
== CKK_DSA
) {
6778 OBJ_DOM_DSA_PRIME_BITS(obj
);
6779 } else if (obj
->key_type
== CKK_DH
) {
6781 OBJ_DOM_DH_PRIME_BITS(obj
);
6782 } else if (obj
->key_type
== CKK_X9_42_DH
) {
6784 OBJ_DOM_DH942_PRIME_BITS(obj
);
6789 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
6796 case CKA_SUBPRIME_BITS
:
6797 if ((obj
->class == CKO_DOMAIN_PARAMETERS
) &&
6798 (obj
->key_type
== CKK_X9_42_DH
)) {
6799 CK_ULONG subprime_bits
=
6800 OBJ_DOM_DH942_SUBPRIME_BITS(obj
);
6801 if (subprime_bits
!=
6802 *((CK_ULONG
*)tmpl_attr
->pValue
)) {
6811 * any other attributes are currently not supported.
6812 * so, it's not possible for them to be in the
6817 if (compare_boolean
) {
6825 if (bval
!= *((CK_BBOOL
*)tmpl_attr
->pValue
)) {
6828 } else if (compare_bigint
) {
6829 if (bigint
== NULL
) {
6832 if (tmpl_attr
->ulValueLen
!= bigint
->big_value_len
) {
6835 if (memcmp(tmpl_attr
->pValue
, bigint
->big_value
,
6836 tmpl_attr
->ulValueLen
) != 0) {
6839 } else if (compare_attr
) {
6840 if (obj_attr
== NULL
) {
6842 * The attribute type is valid, and its value
6843 * has not been initialized in the object. In
6844 * this case, it only matches the template's
6845 * attribute if the template's value length
6848 if (tmpl_attr
->ulValueLen
!= 0)
6851 if (tmpl_attr
->ulValueLen
!=
6852 obj_attr
->ulValueLen
) {
6855 if (memcmp(tmpl_attr
->pValue
, obj_attr
->pValue
,
6856 tmpl_attr
->ulValueLen
) != 0) {
6860 } else if (compare_cert_val
) {
6861 if (cert_attr
== NULL
) {
6862 /* specific attribute not found */
6865 if (tmpl_attr
->ulValueLen
!= cert_attr
->length
) {
6868 if (memcmp(tmpl_attr
->pValue
, cert_attr
->value
,
6869 tmpl_attr
->ulValueLen
) != 0) {
6872 } else if (compare_cert_type
) {
6873 if (memcmp(tmpl_attr
->pValue
, &(obj
->cert_type
),
6874 tmpl_attr
->ulValueLen
) != 0) {
6883 get_extra_attr(CK_ATTRIBUTE_TYPE type
, soft_object_t
*obj
)
6885 CK_ATTRIBUTE_INFO_PTR tmp
;
6887 tmp
= obj
->extra_attrlistp
;
6888 while (tmp
!= NULL
) {
6889 if (tmp
->attr
.type
== type
) {
6890 return (&(tmp
->attr
));
6894 /* if get there, the specified attribute is not found */