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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <cryptoutil.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <security/cryptoki.h>
35 #include "softGlobal.h"
36 #include "softCrypt.h"
37 #include "softSession.h"
38 #include "softObject.h"
40 #include "softKeystore.h"
41 #include "softKeystoreUtil.h"
45 soft_session_t token_session
;
48 * soft_gen_hashed_pin()
52 * pPin: pointer to caller provided Pin
53 * result: output argument which contains the address of the
54 * pointer to the hashed pin
55 * salt: input argument (if non-NULL), or
56 * output argument (if NULL):
57 * address of pointer to the "salt" of the hashed pin
61 * Generate a hashed pin using system provided crypt(3C) function.
66 * -1: some error occurred while generating the hashed pin
70 soft_gen_hashed_pin(CK_UTF8CHAR_PTR pPin
, char **result
, char **salt
)
74 struct passwd pwd
, *pw
;
75 char pwdbuf
[PWD_BUFFER_SIZE
];
76 boolean_t new_salt
= B_FALSE
;
79 * We need to get the passwd entry of the application, which is required
80 * by the crypt_gensalt() below.
83 if (getpwuid_r(uid
, &pwd
, pwdbuf
, PWD_BUFFER_SIZE
, &pw
) != 0) {
90 * crypt_gensalt() will allocate memory to store the new salt.
91 * on return. Pass "$5" here to default to crypt_sha256 since
92 * SHA256 is a FIPS 140-2 certified algorithm and we shouldn't
93 * assume the system default is that strong.
95 if ((*salt
= crypt_gensalt("$5", pw
)) == NULL
) {
100 if ((*result
= crypt((char *)pPin
, *salt
)) == NULL
) {
110 * Authenticate user's PIN for C_Login.
113 soft_verify_pin(CK_UTF8CHAR_PTR pPin
, CK_ULONG ulPinLen
)
116 char *user_cryptpin
= NULL
;
117 char *ks_cryptpin
= NULL
;
119 uchar_t
*tmp_pin
= NULL
;
120 boolean_t pin_initialized
= B_FALSE
;
124 * Check to see if keystore is initialized.
126 rv
= soft_keystore_pin_initialized(&pin_initialized
, &ks_cryptpin
,
132 * Authenticate user's PIN for C_Login.
134 if (pin_initialized
) {
136 if (soft_keystore_get_pin_salt(&salt
) < 0) {
137 rv
= CKR_FUNCTION_FAILED
;
142 * Generate the hashed value based on the user's supplied pin.
144 tmp_pin
= malloc(ulPinLen
+ 1);
145 if (tmp_pin
== NULL
) {
146 rv
= CKR_HOST_MEMORY
;
150 (void) memcpy(tmp_pin
, pPin
, ulPinLen
);
151 tmp_pin
[ulPinLen
] = '\0';
153 if (soft_gen_hashed_pin(tmp_pin
, &user_cryptpin
, &salt
) < 0) {
154 rv
= CKR_FUNCTION_FAILED
;
159 * Compare hash value of the user supplied PIN with
160 * hash value of the keystore PIN.
162 if (strcmp(user_cryptpin
, ks_cryptpin
) != 0) {
163 rv
= CKR_PIN_INCORRECT
;
168 * Provide the user's PIN to low-level keystore so that
169 * it can use it to generate encryption key as needed for
170 * encryption/decryption of the private objects in
173 if (soft_keystore_authpin(tmp_pin
) != 0) {
174 rv
= CKR_FUNCTION_FAILED
;
181 * The PIN is not initialized in the keystore
182 * We will let it pass the authentication anyway but set the
183 * "userpin_change_needed" flag so that the application
184 * will get CKR_PIN_EXPIRED by other C_functions such as
185 * C_CreateObject, C_FindObjectInit, C_GenerateKey etc.
187 soft_slot
.userpin_change_needed
= 1;
200 * The second level C_SetPIN function.
203 soft_setpin(CK_UTF8CHAR_PTR pOldPin
, CK_ULONG ulOldPinLen
,
204 CK_UTF8CHAR_PTR pNewPin
, CK_ULONG ulNewPinLen
)
207 char *user_cryptpin
= NULL
;
208 char *ks_cryptpin
= NULL
;
210 boolean_t pin_initialized
= B_FALSE
;
211 uchar_t
*tmp_old_pin
= NULL
, *tmp_new_pin
= NULL
;
215 * Check to see if keystore is initialized.
217 rv
= soft_keystore_pin_initialized(&pin_initialized
, &ks_cryptpin
,
223 * Authenticate user's PIN for C_SetPIN.
225 if (pin_initialized
) {
227 * Generate the hashed value based on the user supplied PIN.
229 if (soft_keystore_get_pin_salt(&salt
) < 0) {
230 rv
= CKR_FUNCTION_FAILED
;
234 tmp_old_pin
= malloc(ulOldPinLen
+ 1);
235 if (tmp_old_pin
== NULL
) {
236 rv
= CKR_HOST_MEMORY
;
239 (void) memcpy(tmp_old_pin
, pOldPin
, ulOldPinLen
);
240 tmp_old_pin
[ulOldPinLen
] = '\0';
242 if (soft_gen_hashed_pin(tmp_old_pin
, &user_cryptpin
,
244 rv
= CKR_FUNCTION_FAILED
;
249 * Compare hashed value of the user supplied PIN with the
250 * hashed value of the keystore PIN.
252 if (strcmp(user_cryptpin
, ks_cryptpin
) != 0) {
253 rv
= CKR_PIN_INCORRECT
;
258 * This is the first time to setpin, the oldpin must be
261 if (strncmp("changeme", (const char *)pOldPin
,
263 rv
= CKR_PIN_INCORRECT
;
268 tmp_new_pin
= malloc(ulNewPinLen
+ 1);
269 if (tmp_new_pin
== NULL
) {
270 rv
= CKR_HOST_MEMORY
;
273 (void) memcpy(tmp_new_pin
, pNewPin
, ulNewPinLen
);
274 tmp_new_pin
[ulNewPinLen
] = '\0';
277 * Set the new pin after the old pin is authenticated.
279 if (soft_keystore_setpin(tmp_old_pin
, tmp_new_pin
, B_FALSE
)) {
280 rv
= CKR_FUNCTION_FAILED
;
283 (void) pthread_mutex_lock(&soft_giant_mutex
);
284 soft_slot
.userpin_change_needed
= 0;
285 (void) pthread_mutex_unlock(&soft_giant_mutex
);
299 * soft_keystore_pack_obj()
303 * obj: pointer to the soft_object_t of the token object to
305 * ks_buf: output argument which contains the address of the
306 * pointer to the buf of the packed token object
307 * soft_keystore_pack_obj() will allocate memory for the buf,
308 * it is caller's responsibility to free it.
309 * len: output argument which contains the address of the
310 * buffer length of the packed token object
314 * Pack the in-core token object into the keystore format.
319 * Other: some error occurred while packing the object
323 soft_keystore_pack_obj(soft_object_t
*obj
, uchar_t
**ks_buf
, size_t *len
)
326 ks_attr_hdr_t attr_hdr
;
327 CK_ATTRIBUTE_INFO_PTR extra_attr
;
329 ulong_t len_attrs
= 0;
335 (void) memset(&hdr
, 0, sizeof (ks_obj_hdr_t
));
338 * The first part of the packed format contains
339 * the ks_obj_hdr_t struct.
341 hdr
.class = SWAP64((uint64_t)obj
->class);
342 hdr
.key_type
= SWAP64((uint64_t)obj
->key_type
);
343 hdr
.cert_type
= SWAP64((uint64_t)obj
->cert_type
);
344 hdr
.bool_attr_mask
= SWAP64(obj
->bool_attr_mask
);
345 hdr
.mechanism
= SWAP64((uint64_t)obj
->mechanism
);
346 hdr
.object_type
= obj
->object_type
;
349 * The second part of the packed format contains
350 * the attributes from the extra atrribute list.
352 extra_attr
= obj
->extra_attrlistp
;
356 len_attrs
+= ROUNDUP(extra_attr
->attr
.ulValueLen
, 8);
357 extra_attr
= extra_attr
->next
;
359 hdr
.num_attrs
= SWAP32(num_attrs
);
360 ks_len
= soft_pack_object_size(obj
);
361 ks_len
+= sizeof (ks_obj_hdr_t
) + len_attrs
+
362 2 * num_attrs
* sizeof (uint64_t);
363 buf
= calloc(1, ks_len
);
365 return (CKR_HOST_MEMORY
);
367 (void) memcpy(buf
, &hdr
, sizeof (ks_obj_hdr_t
));
368 buf1
= buf
+ sizeof (ks_obj_hdr_t
);
369 extra_attr
= obj
->extra_attrlistp
;
370 for (i
= 0; i
< num_attrs
; i
++) {
371 attr_hdr
.type
= SWAP64((uint64_t)extra_attr
->attr
.type
);
372 attr_hdr
.ulValueLen
=
373 SWAP64((uint64_t)extra_attr
->attr
.ulValueLen
);
374 (void) memcpy(buf1
, &attr_hdr
, sizeof (ks_attr_hdr_t
));
375 buf1
= buf1
+ sizeof (ks_attr_hdr_t
);
376 (void) memcpy(buf1
, extra_attr
->attr
.pValue
,
377 extra_attr
->attr
.ulValueLen
);
378 buf1
= buf1
+ ROUNDUP(extra_attr
->attr
.ulValueLen
, 8);
379 extra_attr
= extra_attr
->next
;
383 * The third part of the packed format contains
386 rv
= soft_pack_object(obj
, buf1
);
395 * soft_keystore_unpack_obj()
399 * obj: pointer to the soft_object_t to store the unpacked
401 * ks_obj: input argument which contains the pointer to the
402 * ks_obj_t struct of packed token object to be unpacked
406 * Unpack the token object in keystore format to in-core soft_object_t.
411 * Other: some error occurred while unpacking the object
415 soft_keystore_unpack_obj(soft_object_t
*obj
, ks_obj_t
*ks_obj
)
420 ks_attr_hdr_t
*attr_hdr
;
421 CK_ATTRIBUTE
template;
426 * Unpack the common area.
428 (void) strcpy((char *)obj
->ks_handle
.name
,
429 (char *)ks_obj
->ks_handle
.name
);
430 obj
->ks_handle
.public = ks_obj
->ks_handle
.public;
431 /* LINTED: pointer alignment */
432 hdr
= (ks_obj_hdr_t
*)ks_obj
->buf
;
433 obj
->version
= ks_obj
->obj_version
;
434 obj
->class = (CK_OBJECT_CLASS
)(SWAP64(hdr
->class));
435 obj
->key_type
= (CK_KEY_TYPE
)(SWAP64(hdr
->key_type
));
436 obj
->cert_type
= (CK_CERTIFICATE_TYPE
)(SWAP64(hdr
->cert_type
));
437 obj
->bool_attr_mask
= SWAP64(hdr
->bool_attr_mask
);
438 obj
->mechanism
= (CK_MECHANISM_TYPE
)(SWAP64(hdr
->mechanism
));
439 obj
->object_type
= hdr
->object_type
;
442 * Initialize other stuffs which were not from keystore.
444 (void) pthread_mutex_init(&obj
->object_mutex
, NULL
);
445 obj
->magic_marker
= SOFTTOKEN_OBJECT_MAGIC
;
446 obj
->session_handle
= (CK_SESSION_HANDLE
)NULL
;
448 buf
= ks_obj
->buf
+ sizeof (ks_obj_hdr_t
);
451 * Unpack extra attribute list.
453 for (i
= 0; i
< SWAP32(hdr
->num_attrs
); i
++) {
454 /* LINTED: pointer alignment */
455 attr_hdr
= (ks_attr_hdr_t
*)buf
;
456 (void) memset(&template, 0, sizeof (CK_ATTRIBUTE
));
457 template.type
= (CK_ATTRIBUTE_TYPE
)(SWAP64(attr_hdr
->type
));
458 template.ulValueLen
= (CK_ULONG
)(SWAP64(attr_hdr
->ulValueLen
));
459 buf
= buf
+ sizeof (ks_attr_hdr_t
);
460 /* Allocate storage for the value of the attribute. */
461 if (template.ulValueLen
> 0) {
462 template.pValue
= malloc(template.ulValueLen
);
463 if (template.pValue
== NULL
) {
464 return (CKR_HOST_MEMORY
);
466 (void) memcpy(template.pValue
, buf
,
467 template.ulValueLen
);
470 rv
= soft_add_extra_attr(&template, obj
);
471 if (template.pValue
) {
472 free(template.pValue
);
479 buf
= buf
+ ROUNDUP(template.ulValueLen
, 8);
483 * Unpack the key itself.
485 rv
= soft_unpack_object(obj
, buf
);
492 * soft_unpack_obj_attribute()
496 * buf: contains the packed data (attributes) from keystore
497 * key_dest: the key attribute will be unpacked and save in key_dest
498 * cert_dest: the certificate attribute will be unpacked an
500 * offset: length of the current attribute occupies.
501 * The caller should use this returned "offset" to
502 * advance the buffer pointer to next attribute.
503 * cert: TRUE for certificate (use cert_dest)
504 * FALSE for key (use key_dest)
508 * Unpack the attribute from keystore format to the big integer format.
513 * Other: some error occurred while unpacking the object attribute
517 soft_unpack_obj_attribute(uchar_t
*buf
, biginteger_t
*key_dest
,
518 cert_attr_t
**cert_dest
, ulong_t
*offset
, boolean_t cert
)
522 CK_ATTRIBUTE
template;
524 /* LINTED: pointer alignment */
525 template.ulValueLen
= SWAP64(*(uint64_t *)buf
);
526 buf
= buf
+ sizeof (uint64_t);
527 template.pValue
= malloc(template.ulValueLen
);
528 if (template.pValue
== NULL
) {
529 return (CKR_HOST_MEMORY
);
532 (void) memcpy(template.pValue
, buf
, template.ulValueLen
);
534 rv
= get_cert_attr_from_template(cert_dest
, &template);
536 rv
= get_bigint_attr_from_template(key_dest
, &template);
539 free(template.pValue
);
544 *offset
= sizeof (uint64_t) + template.ulValueLen
;
550 * Calculate the total buffer length required to store the
551 * object key (the third part) in a keystore format.
554 soft_pack_object_size(soft_object_t
*objp
)
557 CK_OBJECT_CLASS
class = objp
->class;
558 CK_KEY_TYPE keytype
= objp
->key_type
;
559 CK_CERTIFICATE_TYPE certtype
= objp
->cert_type
;
566 * modulus_bits + modulus_len + modulus +
567 * pubexpo_len + pubexpo
569 return (ROUNDUP(((biginteger_t
*)
570 OBJ_PUB_RSA_MOD(objp
))->big_value_len
, 8) +
571 ROUNDUP(((biginteger_t
*)
572 OBJ_PUB_RSA_PUBEXPO(objp
))->big_value_len
, 8) +
573 3 * sizeof (uint64_t));
577 * prime_len + prime + subprime_len + subprime +
578 * base_len + base + value_len + value
580 return (ROUNDUP(((biginteger_t
*)
581 OBJ_PUB_DSA_PRIME(objp
))->big_value_len
, 8) +
582 ROUNDUP(((biginteger_t
*)
583 OBJ_PUB_DSA_SUBPRIME(objp
))->big_value_len
, 8) +
584 ROUNDUP(((biginteger_t
*)
585 OBJ_PUB_DSA_BASE(objp
))->big_value_len
, 8) +
586 ROUNDUP(((biginteger_t
*)
587 OBJ_PUB_DSA_VALUE(objp
))->big_value_len
, 8) +
588 4 * sizeof (uint64_t));
591 * ec_point_len + ec_point
593 return (ROUNDUP(((biginteger_t
*)
594 OBJ_PUB_EC_POINT(objp
))->big_value_len
, 8) +
598 * prime_len + prime + base_len + base +
601 return (ROUNDUP(((biginteger_t
*)
602 OBJ_PUB_DH_PRIME(objp
))->big_value_len
, 8) +
603 ROUNDUP(((biginteger_t
*)
604 OBJ_PUB_DH_BASE(objp
))->big_value_len
, 8) +
605 ROUNDUP(((biginteger_t
*)
606 OBJ_PUB_DH_VALUE(objp
))->big_value_len
, 8) +
607 3 * sizeof (uint64_t));
611 * prime_len + prime + base_len + base +
612 * subprime_len + subprime + value_len + value
614 return (ROUNDUP(((biginteger_t
*)
615 OBJ_PUB_DH942_PRIME(objp
))->big_value_len
, 8) +
616 ROUNDUP(((biginteger_t
*)
617 OBJ_PUB_DH942_BASE(objp
))->big_value_len
, 8) +
618 ROUNDUP(((biginteger_t
*)
619 OBJ_PUB_DH942_SUBPRIME(objp
))->big_value_len
, 8) +
620 ROUNDUP(((biginteger_t
*)
621 OBJ_PUB_DH942_VALUE(objp
))->big_value_len
, 8) +
622 4 * sizeof (uint64_t));
627 case CKO_PRIVATE_KEY
:
631 * modulus_len + modulus + pubexpo_len + pubexpo +
632 * priexpo_len + priexpo + prime1_len + prime1 +
633 * prime2_len + prime2 + expo1_len + expo1 +
634 * expo2_len + expo2 + coef_len + coef
636 return (ROUNDUP(((biginteger_t
*)
637 OBJ_PRI_RSA_MOD(objp
))->big_value_len
, 8) +
638 ROUNDUP(((biginteger_t
*)
639 OBJ_PRI_RSA_PUBEXPO(objp
))->big_value_len
, 8) +
640 ROUNDUP(((biginteger_t
*)
641 OBJ_PRI_RSA_PRIEXPO(objp
))->big_value_len
, 8) +
642 ROUNDUP(((biginteger_t
*)
643 OBJ_PRI_RSA_PRIME1(objp
))->big_value_len
, 8) +
644 ROUNDUP(((biginteger_t
*)
645 OBJ_PRI_RSA_PRIME2(objp
))->big_value_len
, 8) +
646 ROUNDUP(((biginteger_t
*)
647 OBJ_PRI_RSA_EXPO1(objp
))->big_value_len
, 8) +
648 ROUNDUP(((biginteger_t
*)
649 OBJ_PRI_RSA_EXPO2(objp
))->big_value_len
, 8) +
650 ROUNDUP(((biginteger_t
*)
651 OBJ_PRI_RSA_COEF(objp
))->big_value_len
, 8) +
652 8 * sizeof (uint64_t));
656 * prime_len + prime + subprime_len + subprime +
657 * base_len + base + value_len + value
659 return (ROUNDUP(((biginteger_t
*)
660 OBJ_PRI_DSA_PRIME(objp
))->big_value_len
, 8) +
661 ROUNDUP(((biginteger_t
*)
662 OBJ_PRI_DSA_SUBPRIME(objp
))->big_value_len
, 8) +
663 ROUNDUP(((biginteger_t
*)
664 OBJ_PRI_DSA_BASE(objp
))->big_value_len
, 8) +
665 ROUNDUP(((biginteger_t
*)
666 OBJ_PRI_DSA_VALUE(objp
))->big_value_len
, 8) +
667 4 * sizeof (uint64_t));
671 * value_bits + prime_len + prime + base_len + base +
674 return (ROUNDUP(((biginteger_t
*)
675 OBJ_PRI_DH_PRIME(objp
))->big_value_len
, 8) +
676 ROUNDUP(((biginteger_t
*)
677 OBJ_PRI_DH_BASE(objp
))->big_value_len
, 8) +
678 ROUNDUP(((biginteger_t
*)
679 OBJ_PRI_DH_VALUE(objp
))->big_value_len
, 8) +
680 4 * sizeof (uint64_t));
686 return (ROUNDUP(((biginteger_t
*)
687 OBJ_PRI_EC_VALUE(objp
))->big_value_len
, 8) +
692 * prime_len + prime + base_len + base +
693 * subprime_len + subprime + value_len + value
695 return (ROUNDUP(((biginteger_t
*)
696 OBJ_PRI_DH942_PRIME(objp
))->big_value_len
, 8) +
697 ROUNDUP(((biginteger_t
*)
698 OBJ_PRI_DH942_BASE(objp
))->big_value_len
, 8) +
699 ROUNDUP(((biginteger_t
*)
700 OBJ_PRI_DH942_SUBPRIME(objp
))->big_value_len
, 8) +
701 ROUNDUP(((biginteger_t
*)
702 OBJ_PRI_DH942_VALUE(objp
))->big_value_len
, 8) +
703 4 * sizeof (uint64_t));
713 return (ROUNDUP(OBJ_SEC_VALUE_LEN(objp
), 8) +
716 case CKO_CERTIFICATE
:
720 * subject_len + subject + value_len + value
722 return (ROUNDUP(((cert_attr_t
*)
723 X509_CERT_SUBJECT(objp
))->length
, 8) +
724 ROUNDUP(((cert_attr_t
*)
725 X509_CERT_VALUE(objp
))->length
, 8) +
726 2 * sizeof (uint64_t));
728 case CKC_X_509_ATTR_CERT
:
730 * owner_len + owner + value_len + value
732 return (ROUNDUP(((cert_attr_t
*)
733 X509_ATTR_CERT_OWNER(objp
))->length
, 8) +
734 ROUNDUP(((cert_attr_t
*)
735 X509_ATTR_CERT_VALUE(objp
))->length
, 8) +
736 2 * sizeof (uint64_t));
740 case CKO_DOMAIN_PARAMETERS
:
748 * Pack the object key (the third part) from the soft_object_t
749 * into the keystore format.
752 soft_pack_object(soft_object_t
*objp
, uchar_t
*buf
)
755 CK_OBJECT_CLASS
class = objp
->class;
756 CK_KEY_TYPE keytype
= objp
->key_type
;
757 CK_CERTIFICATE_TYPE certtype
= objp
->cert_type
;
765 tmp_val
= SWAP64((uint64_t)OBJ_PUB_RSA_MOD_BITS(objp
));
766 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
767 buf
= buf
+ sizeof (uint64_t);
769 /* modulus_len + modulus */
770 tmp_val
= SWAP64((uint64_t)(((biginteger_t
*)
771 OBJ_PUB_RSA_MOD(objp
))->big_value_len
));
772 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
773 buf
= buf
+ sizeof (uint64_t);
775 (void) memcpy(buf
, (char *)(((biginteger_t
*)
776 OBJ_PUB_RSA_MOD(objp
))->big_value
),
778 OBJ_PUB_RSA_MOD(objp
))->big_value_len
);
779 buf
= buf
+ ROUNDUP(((biginteger_t
*)
780 OBJ_PUB_RSA_MOD(objp
))->big_value_len
, 8);
782 /* pubexpo_len + pubexpo */
783 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
784 OBJ_PUB_RSA_PUBEXPO(objp
))->big_value_len
);
785 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
786 buf
= buf
+ sizeof (uint64_t);
788 (void) memcpy(buf
, (char *)(((biginteger_t
*)
789 OBJ_PUB_RSA_PUBEXPO(objp
))->big_value
),
791 OBJ_PUB_RSA_PUBEXPO(objp
))->big_value_len
);
795 /* prime_len + prime */
796 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
797 OBJ_PUB_DSA_PRIME(objp
))->big_value_len
);
798 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
799 buf
= buf
+ sizeof (uint64_t);
801 (void) memcpy(buf
, (char *)((biginteger_t
*)
802 OBJ_PUB_DSA_PRIME(objp
))->big_value
,
804 OBJ_PUB_DSA_PRIME(objp
))->big_value_len
);
805 buf
= buf
+ ROUNDUP(((biginteger_t
*)
806 OBJ_PUB_DSA_PRIME(objp
))->big_value_len
, 8);
808 /* subprime_len + subprime */
809 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
810 OBJ_PUB_DSA_SUBPRIME(objp
))->big_value_len
);
811 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
812 buf
= buf
+ sizeof (uint64_t);
814 (void) memcpy(buf
, (char *)((biginteger_t
*)
815 OBJ_PUB_DSA_SUBPRIME(objp
))->big_value
,
817 OBJ_PUB_DSA_SUBPRIME(objp
))->big_value_len
);
818 buf
= buf
+ ROUNDUP(((biginteger_t
*)
819 OBJ_PUB_DSA_SUBPRIME(objp
))->big_value_len
, 8);
821 /* base_len + base */
822 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
823 OBJ_PUB_DSA_BASE(objp
))->big_value_len
);
824 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
825 buf
= buf
+ sizeof (uint64_t);
827 (void) memcpy(buf
, (char *)((biginteger_t
*)
828 OBJ_PUB_DSA_BASE(objp
))->big_value
,
830 OBJ_PUB_DSA_BASE(objp
))->big_value_len
);
831 buf
= buf
+ ROUNDUP(((biginteger_t
*)
832 OBJ_PUB_DSA_BASE(objp
))->big_value_len
, 8);
834 /* value_len + value */
835 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
836 OBJ_PUB_DSA_VALUE(objp
))->big_value_len
);
837 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
838 buf
= buf
+ sizeof (uint64_t);
840 (void) memcpy(buf
, (char *)((biginteger_t
*)
841 OBJ_PUB_DSA_VALUE(objp
))->big_value
,
843 OBJ_PUB_DSA_VALUE(objp
))->big_value_len
);
847 /* point_len + point */
848 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
849 OBJ_PUB_EC_POINT(objp
))->big_value_len
);
850 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
851 buf
= buf
+ sizeof (uint64_t);
853 (void) memcpy(buf
, (char *)((biginteger_t
*)
854 OBJ_PUB_EC_POINT(objp
))->big_value
,
856 OBJ_PUB_EC_POINT(objp
))->big_value_len
);
860 /* prime_len + prime */
861 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
862 OBJ_PUB_DH_PRIME(objp
))->big_value_len
);
863 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
864 buf
= buf
+ sizeof (uint64_t);
866 (void) memcpy(buf
, (char *)((biginteger_t
*)
867 OBJ_PUB_DH_PRIME(objp
))->big_value
,
869 OBJ_PUB_DH_PRIME(objp
))->big_value_len
);
870 buf
= buf
+ ROUNDUP(((biginteger_t
*)
871 OBJ_PUB_DH_PRIME(objp
))->big_value_len
, 8);
873 /* base_len + base */
874 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
875 OBJ_PUB_DH_BASE(objp
))->big_value_len
);
876 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
877 buf
= buf
+ sizeof (uint64_t);
879 (void) memcpy(buf
, (char *)((biginteger_t
*)
880 OBJ_PUB_DH_BASE(objp
))->big_value
,
882 OBJ_PUB_DH_BASE(objp
))->big_value_len
);
883 buf
= buf
+ ROUNDUP(((biginteger_t
*)
884 OBJ_PUB_DH_BASE(objp
))->big_value_len
, 8);
886 /* value_len + value */
887 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
888 OBJ_PUB_DH_VALUE(objp
))->big_value_len
);
889 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
890 buf
= buf
+ sizeof (uint64_t);
892 (void) memcpy(buf
, (char *)((biginteger_t
*)
893 OBJ_PUB_DH_VALUE(objp
))->big_value
,
895 OBJ_PUB_DH_VALUE(objp
))->big_value_len
);
900 /* prime_len + prime */
901 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
902 OBJ_PUB_DH942_PRIME(objp
))->big_value_len
);
903 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
904 buf
= buf
+ sizeof (uint64_t);
906 (void) memcpy(buf
, (char *)((biginteger_t
*)
907 OBJ_PUB_DH942_PRIME(objp
))->big_value
,
909 OBJ_PUB_DH942_PRIME(objp
))->big_value_len
);
910 buf
= buf
+ ROUNDUP(((biginteger_t
*)
911 OBJ_PUB_DH942_PRIME(objp
))->big_value_len
, 8);
913 /* base_len + base */
914 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
915 OBJ_PUB_DH942_BASE(objp
))->big_value_len
);
916 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
917 buf
= buf
+ sizeof (uint64_t);
919 (void) memcpy(buf
, (char *)((biginteger_t
*)
920 OBJ_PUB_DH942_BASE(objp
))->big_value
,
922 OBJ_PUB_DH942_BASE(objp
))->big_value_len
);
923 buf
= buf
+ ROUNDUP(((biginteger_t
*)
924 OBJ_PUB_DH942_BASE(objp
))->big_value_len
, 8);
926 /* subprime_len + subprime */
927 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
928 OBJ_PUB_DH942_SUBPRIME(objp
))->big_value_len
);
929 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
930 buf
= buf
+ sizeof (uint64_t);
932 (void) memcpy(buf
, (char *)((biginteger_t
*)
933 OBJ_PUB_DH942_SUBPRIME(objp
))->big_value
,
935 OBJ_PUB_DH942_SUBPRIME(objp
))->big_value_len
);
936 buf
= buf
+ ROUNDUP(((biginteger_t
*)
937 OBJ_PUB_DH942_SUBPRIME(objp
))->big_value_len
, 8);
939 /* value_len + value */
940 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
941 OBJ_PUB_DH942_VALUE(objp
))->big_value_len
);
942 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
943 buf
= buf
+ sizeof (uint64_t);
945 (void) memcpy(buf
, (char *)((biginteger_t
*)
946 OBJ_PUB_DH942_VALUE(objp
))->big_value
,
948 OBJ_PUB_DH942_VALUE(objp
))->big_value_len
);
955 case CKO_PRIVATE_KEY
:
958 /* modulus_len + modulus */
959 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
960 OBJ_PRI_RSA_MOD(objp
))->big_value_len
);
961 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
962 buf
= buf
+ sizeof (uint64_t);
964 (void) memcpy(buf
, (char *)((biginteger_t
*)
965 OBJ_PRI_RSA_MOD(objp
))->big_value
,
967 OBJ_PRI_RSA_MOD(objp
))->big_value_len
);
968 buf
= buf
+ ROUNDUP(((biginteger_t
*)
969 OBJ_PRI_RSA_MOD(objp
))->big_value_len
, 8);
971 /* pubexpo_len + pubexpo */
972 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
973 OBJ_PRI_RSA_PUBEXPO(objp
))->big_value_len
);
974 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
975 buf
= buf
+ sizeof (uint64_t);
977 (void) memcpy(buf
, (char *)((biginteger_t
*)
978 OBJ_PRI_RSA_PUBEXPO(objp
))->big_value
,
980 OBJ_PRI_RSA_PUBEXPO(objp
))->big_value_len
);
981 buf
= buf
+ ROUNDUP(((biginteger_t
*)
982 OBJ_PRI_RSA_PUBEXPO(objp
))->big_value_len
, 8);
984 /* priexpo_len + priexpo */
985 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
986 OBJ_PRI_RSA_PRIEXPO(objp
))->big_value_len
);
987 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
988 buf
= buf
+ sizeof (uint64_t);
990 (void) memcpy(buf
, (char *)((biginteger_t
*)
991 OBJ_PRI_RSA_PRIEXPO(objp
))->big_value
,
993 OBJ_PRI_RSA_PRIEXPO(objp
))->big_value_len
);
994 buf
= buf
+ ROUNDUP(((biginteger_t
*)
995 OBJ_PRI_RSA_PRIEXPO(objp
))->big_value_len
, 8);
997 /* prime1_len + prime1 */
998 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
999 OBJ_PRI_RSA_PRIME1(objp
))->big_value_len
);
1000 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1001 buf
= buf
+ sizeof (uint64_t);
1003 (void) memcpy(buf
, (char *)((biginteger_t
*)
1004 OBJ_PRI_RSA_PRIME1(objp
))->big_value
,
1006 OBJ_PRI_RSA_PRIME1(objp
))->big_value_len
);
1007 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1008 OBJ_PRI_RSA_PRIME1(objp
))->big_value_len
, 8);
1010 /* prime2_len + prime2 */
1011 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1012 OBJ_PRI_RSA_PRIME2(objp
))->big_value_len
);
1013 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1014 buf
= buf
+ sizeof (uint64_t);
1016 (void) memcpy(buf
, (char *)((biginteger_t
*)
1017 OBJ_PRI_RSA_PRIME2(objp
))->big_value
,
1019 OBJ_PRI_RSA_PRIME2(objp
))->big_value_len
);
1020 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1021 OBJ_PRI_RSA_PRIME2(objp
))->big_value_len
, 8);
1023 /* expo1_len + expo1 */
1024 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1025 OBJ_PRI_RSA_EXPO1(objp
))->big_value_len
);
1026 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1027 buf
= buf
+ sizeof (uint64_t);
1029 (void) memcpy(buf
, (char *)((biginteger_t
*)
1030 OBJ_PRI_RSA_EXPO1(objp
))->big_value
,
1032 OBJ_PRI_RSA_EXPO1(objp
))->big_value_len
);
1033 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1034 OBJ_PRI_RSA_EXPO1(objp
))->big_value_len
, 8);
1036 /* expo2_len + expo2 */
1037 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1038 OBJ_PRI_RSA_EXPO2(objp
))->big_value_len
);
1039 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1040 buf
= buf
+ sizeof (uint64_t);
1042 (void) memcpy(buf
, (char *)((biginteger_t
*)
1043 OBJ_PRI_RSA_EXPO2(objp
))->big_value
,
1045 OBJ_PRI_RSA_EXPO2(objp
))->big_value_len
);
1046 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1047 OBJ_PRI_RSA_EXPO2(objp
))->big_value_len
, 8);
1049 /* coef_len + coef */
1050 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1051 OBJ_PRI_RSA_COEF(objp
))->big_value_len
);
1052 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1053 buf
= buf
+ sizeof (uint64_t);
1055 (void) memcpy(buf
, (char *)((biginteger_t
*)
1056 OBJ_PRI_RSA_COEF(objp
))->big_value
,
1058 OBJ_PRI_RSA_COEF(objp
))->big_value_len
);
1059 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1060 OBJ_PRI_RSA_COEF(objp
))->big_value_len
, 8);
1065 /* prime_len + prime */
1066 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1067 OBJ_PRI_DSA_PRIME(objp
))->big_value_len
);
1068 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1069 buf
= buf
+ sizeof (uint64_t);
1071 (void) memcpy(buf
, (char *)((biginteger_t
*)
1072 OBJ_PRI_DSA_PRIME(objp
))->big_value
,
1074 OBJ_PRI_DSA_PRIME(objp
))->big_value_len
);
1075 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1076 OBJ_PRI_DSA_PRIME(objp
))->big_value_len
, 8);
1078 /* subprime_len + subprime */
1079 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1080 OBJ_PRI_DSA_SUBPRIME(objp
))->big_value_len
);
1081 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1082 buf
= buf
+ sizeof (uint64_t);
1084 (void) memcpy(buf
, (char *)((biginteger_t
*)
1085 OBJ_PRI_DSA_SUBPRIME(objp
))->big_value
,
1087 OBJ_PRI_DSA_SUBPRIME(objp
))->big_value_len
);
1088 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1089 OBJ_PRI_DSA_SUBPRIME(objp
))->big_value_len
, 8);
1091 /* base_len + base */
1092 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1093 OBJ_PRI_DSA_BASE(objp
))->big_value_len
);
1094 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1095 buf
= buf
+ sizeof (uint64_t);
1097 (void) memcpy(buf
, (char *)((biginteger_t
*)
1098 OBJ_PRI_DSA_BASE(objp
))->big_value
,
1100 OBJ_PRI_DSA_BASE(objp
))->big_value_len
);
1101 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1102 OBJ_PRI_DSA_BASE(objp
))->big_value_len
, 8);
1104 /* value_len + value */
1105 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1106 OBJ_PRI_DSA_VALUE(objp
))->big_value_len
);
1107 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1108 buf
= buf
+ sizeof (uint64_t);
1110 (void) memcpy(buf
, (char *)((biginteger_t
*)
1111 OBJ_PRI_DSA_VALUE(objp
))->big_value
,
1113 OBJ_PRI_DSA_VALUE(objp
))->big_value_len
);
1117 /* value_len + value */
1118 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1119 OBJ_PRI_EC_VALUE(objp
))->big_value_len
);
1120 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1121 buf
= buf
+ sizeof (uint64_t);
1123 (void) memcpy(buf
, (char *)((biginteger_t
*)
1124 OBJ_PRI_EC_VALUE(objp
))->big_value
,
1126 OBJ_PRI_EC_VALUE(objp
))->big_value_len
);
1131 tmp_val
= SWAP64((uint64_t)OBJ_PRI_DH_VAL_BITS(objp
));
1132 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1133 buf
= buf
+ sizeof (uint64_t);
1135 /* prime_len + prime */
1136 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1137 OBJ_PRI_DH_PRIME(objp
))->big_value_len
);
1138 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1139 buf
= buf
+ sizeof (uint64_t);
1141 (void) memcpy(buf
, (char *)((biginteger_t
*)
1142 OBJ_PRI_DH_PRIME(objp
))->big_value
,
1144 OBJ_PRI_DH_PRIME(objp
))->big_value_len
);
1145 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1146 OBJ_PRI_DH_PRIME(objp
))->big_value_len
, 8);
1148 /* base_len + base */
1149 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1150 OBJ_PRI_DH_BASE(objp
))->big_value_len
);
1151 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1152 buf
= buf
+ sizeof (uint64_t);
1154 (void) memcpy(buf
, (char *)((biginteger_t
*)
1155 OBJ_PRI_DH_BASE(objp
))->big_value
,
1157 OBJ_PRI_DH_BASE(objp
))->big_value_len
);
1158 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1159 OBJ_PRI_DH_BASE(objp
))->big_value_len
, 8);
1161 /* value_len + value */
1162 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1163 OBJ_PRI_DH_VALUE(objp
))->big_value_len
);
1164 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1165 buf
= buf
+ sizeof (uint64_t);
1167 (void) memcpy(buf
, (char *)((biginteger_t
*)
1168 OBJ_PRI_DH_VALUE(objp
))->big_value
,
1170 OBJ_PRI_DH_VALUE(objp
))->big_value_len
);
1175 /* prime_len + prime */
1176 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1177 OBJ_PRI_DH942_PRIME(objp
))->big_value_len
);
1178 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1179 buf
= buf
+ sizeof (uint64_t);
1181 (void) memcpy(buf
, (char *)((biginteger_t
*)
1182 OBJ_PRI_DH942_PRIME(objp
))->big_value
,
1184 OBJ_PRI_DH942_PRIME(objp
))->big_value_len
);
1185 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1186 OBJ_PRI_DH942_PRIME(objp
))->big_value_len
, 8);
1188 /* base_len + base */
1189 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1190 OBJ_PRI_DH942_BASE(objp
))->big_value_len
);
1191 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1192 buf
= buf
+ sizeof (uint64_t);
1194 (void) memcpy(buf
, (char *)((biginteger_t
*)
1195 OBJ_PRI_DH942_BASE(objp
))->big_value
,
1197 OBJ_PRI_DH942_BASE(objp
))->big_value_len
);
1198 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1199 OBJ_PRI_DH942_BASE(objp
))->big_value_len
, 8);
1201 /* subprime_len + subprime */
1202 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1203 OBJ_PRI_DH942_SUBPRIME(objp
))->big_value_len
);
1204 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1205 buf
= buf
+ sizeof (uint64_t);
1207 (void) memcpy(buf
, (char *)((biginteger_t
*)
1208 OBJ_PRI_DH942_SUBPRIME(objp
))->big_value
,
1210 OBJ_PRI_DH942_SUBPRIME(objp
))->big_value_len
);
1211 buf
= buf
+ ROUNDUP(((biginteger_t
*)
1212 OBJ_PRI_DH942_SUBPRIME(objp
))->big_value_len
, 8);
1214 /* value_len + value */
1215 tmp_val
= SWAP64((uint64_t)((biginteger_t
*)
1216 OBJ_PRI_DH942_VALUE(objp
))->big_value_len
);
1217 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1218 buf
= buf
+ sizeof (uint64_t);
1220 (void) memcpy(buf
, (char *)((biginteger_t
*)
1221 OBJ_PRI_DH942_VALUE(objp
))->big_value
,
1223 OBJ_PRI_DH942_VALUE(objp
))->big_value_len
);
1231 case CKO_SECRET_KEY
:
1232 /* value_len + value */
1233 tmp_val
= SWAP64((uint64_t)OBJ_SEC_VALUE_LEN(objp
));
1234 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1235 buf
= buf
+ sizeof (uint64_t);
1237 if (OBJ_SEC_VALUE_LEN(objp
) > 0) {
1238 (void) memcpy(buf
, (char *)OBJ_SEC_VALUE(objp
),
1239 OBJ_SEC_VALUE_LEN(objp
));
1240 buf
= buf
+ ROUNDUP(OBJ_SEC_VALUE_LEN(objp
), 8);
1245 case CKO_CERTIFICATE
:
1249 /* subject_len + subject */
1250 tmp_val
= SWAP64((uint64_t)(((cert_attr_t
*)
1251 X509_CERT_SUBJECT(objp
))->length
));
1252 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1253 buf
= buf
+ sizeof (uint64_t);
1255 (void) memcpy(buf
, (char *)((cert_attr_t
*)
1256 X509_CERT_SUBJECT(objp
))->value
,
1258 X509_CERT_SUBJECT(objp
))->length
);
1259 buf
= buf
+ ROUNDUP(((cert_attr_t
*)
1260 X509_CERT_SUBJECT(objp
))->length
, 8);
1262 /* value_len + value */
1263 tmp_val
= SWAP64((uint64_t)(((cert_attr_t
*)
1264 X509_CERT_VALUE(objp
))->length
));
1265 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1266 buf
= buf
+ sizeof (uint64_t);
1268 (void) memcpy(buf
, (char *)((cert_attr_t
*)
1269 X509_CERT_VALUE(objp
))->value
,
1271 X509_CERT_VALUE(objp
))->length
);
1274 case CKC_X_509_ATTR_CERT
:
1275 /* owner_len + owner */
1276 tmp_val
= SWAP64((uint64_t)(((cert_attr_t
*)
1277 X509_ATTR_CERT_OWNER(objp
))->length
));
1278 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1279 buf
= buf
+ sizeof (uint64_t);
1281 (void) memcpy(buf
, (char *)((cert_attr_t
*)
1282 X509_ATTR_CERT_OWNER(objp
))->value
,
1284 X509_ATTR_CERT_OWNER(objp
))->length
);
1285 buf
= buf
+ ROUNDUP(((cert_attr_t
*)
1286 X509_ATTR_CERT_OWNER(objp
))->length
, 8);
1288 /* value_len + value */
1289 tmp_val
= SWAP64((uint64_t)(((cert_attr_t
*)
1290 X509_ATTR_CERT_VALUE(objp
))->length
));
1291 (void) memcpy(buf
, (char *)&tmp_val
, sizeof (uint64_t));
1292 buf
= buf
+ sizeof (uint64_t);
1294 (void) memcpy(buf
, (char *)((cert_attr_t
*)
1295 X509_ATTR_CERT_VALUE(objp
))->value
,
1297 X509_ATTR_CERT_VALUE(objp
))->length
);
1302 case CKO_DOMAIN_PARAMETERS
:
1310 * Unpack the object key in keystore format (the third part)
1311 * into soft_object_t.
1314 soft_unpack_object(soft_object_t
*objp
, uchar_t
*buf
)
1317 public_key_obj_t
*pbk
;
1318 private_key_obj_t
*pvk
;
1319 secret_key_obj_t
*sck
;
1320 certificate_obj_t
*cert
;
1321 CK_OBJECT_CLASS
class = objp
->class;
1322 CK_KEY_TYPE keytype
= objp
->key_type
;
1323 CK_CERTIFICATE_TYPE certtype
= objp
->cert_type
;
1325 biginteger_t modulus
;
1326 biginteger_t pubexpo
;
1328 biginteger_t subprime
;
1332 biginteger_t priexpo
;
1333 biginteger_t prime1
;
1334 biginteger_t prime2
;
1342 /* prevent bigint_attr_cleanup from freeing invalid attr value */
1343 (void) memset(&modulus
, 0x0, sizeof (biginteger_t
));
1344 (void) memset(&pubexpo
, 0x0, sizeof (biginteger_t
));
1345 (void) memset(&prime
, 0x0, sizeof (biginteger_t
));
1346 (void) memset(&subprime
, 0x0, sizeof (biginteger_t
));
1347 (void) memset(&base
, 0x0, sizeof (biginteger_t
));
1348 (void) memset(&value
, 0x0, sizeof (biginteger_t
));
1350 (void) memset(&priexpo
, 0x0, sizeof (biginteger_t
));
1351 (void) memset(&prime1
, 0x0, sizeof (biginteger_t
));
1352 (void) memset(&prime2
, 0x0, sizeof (biginteger_t
));
1353 (void) memset(&expo1
, 0x0, sizeof (biginteger_t
));
1354 (void) memset(&expo2
, 0x0, sizeof (biginteger_t
));
1355 (void) memset(&coef
, 0x0, sizeof (biginteger_t
));
1359 case CKO_PUBLIC_KEY
:
1360 /* Allocate storage for Public Key Object. */
1361 pbk
= calloc(1, sizeof (public_key_obj_t
));
1363 rv
= CKR_HOST_MEMORY
;
1367 objp
->object_class_u
.public_key
= pbk
;
1370 case CKK_RSA
: /* modulus_bits */
1371 (void) memcpy(&tmp_val
, buf
, sizeof (uint64_t));
1372 KEY_PUB_RSA_MOD_BITS(pbk
) = (CK_ULONG
)(SWAP64(tmp_val
));
1373 buf
= buf
+ sizeof (uint64_t);
1376 if ((rv
= soft_unpack_obj_attribute(buf
, &modulus
,
1377 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1380 copy_bigint_attr(&modulus
, KEY_PUB_RSA_MOD(pbk
));
1382 buf
+= ROUNDUP(offset
, 8);
1385 if ((rv
= soft_unpack_obj_attribute(buf
, &pubexpo
,
1386 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1389 copy_bigint_attr(&pubexpo
, KEY_PUB_RSA_PUBEXPO(pbk
));
1395 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1396 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1399 copy_bigint_attr(&prime
, KEY_PUB_DSA_PRIME(pbk
));
1401 buf
+= ROUNDUP(offset
, 8);
1404 if ((rv
= soft_unpack_obj_attribute(buf
, &subprime
,
1405 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1408 copy_bigint_attr(&subprime
, KEY_PUB_DSA_SUBPRIME(pbk
));
1410 buf
+= ROUNDUP(offset
, 8);
1413 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1414 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1417 copy_bigint_attr(&base
, KEY_PUB_DSA_BASE(pbk
));
1419 buf
+= ROUNDUP(offset
, 8);
1422 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1423 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1426 copy_bigint_attr(&value
, KEY_PUB_DSA_VALUE(pbk
));
1432 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1433 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1436 copy_bigint_attr(&prime
, KEY_PUB_DH_PRIME(pbk
));
1438 buf
+= ROUNDUP(offset
, 8);
1441 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1442 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1445 copy_bigint_attr(&base
, KEY_PUB_DH_BASE(pbk
));
1447 buf
+= ROUNDUP(offset
, 8);
1450 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1451 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1454 copy_bigint_attr(&value
, KEY_PUB_DH_VALUE(pbk
));
1460 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1461 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1464 copy_bigint_attr(&value
, KEY_PUB_EC_POINT(pbk
));
1469 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1470 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1473 copy_bigint_attr(&prime
, KEY_PUB_DH942_PRIME(pbk
));
1475 buf
+= ROUNDUP(offset
, 8);
1478 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1479 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1482 copy_bigint_attr(&base
, KEY_PUB_DH942_BASE(pbk
));
1484 buf
+= ROUNDUP(offset
, 8);
1487 if ((rv
= soft_unpack_obj_attribute(buf
, &subprime
,
1488 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1491 copy_bigint_attr(&subprime
,
1492 KEY_PUB_DH942_SUBPRIME(pbk
));
1494 buf
+= ROUNDUP(offset
, 8);
1497 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1498 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1501 copy_bigint_attr(&value
, KEY_PUB_DH942_VALUE(pbk
));
1508 case CKO_PRIVATE_KEY
:
1509 /* Allocate storage for Private Key Object. */
1510 pvk
= calloc(1, sizeof (private_key_obj_t
));
1512 rv
= CKR_HOST_MEMORY
;
1516 objp
->object_class_u
.private_key
= pvk
;
1521 if ((rv
= soft_unpack_obj_attribute(buf
, &modulus
,
1522 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1525 copy_bigint_attr(&modulus
, KEY_PRI_RSA_MOD(pvk
));
1527 buf
+= ROUNDUP(offset
, 8);
1530 if ((rv
= soft_unpack_obj_attribute(buf
, &pubexpo
,
1531 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1534 copy_bigint_attr(&pubexpo
, KEY_PRI_RSA_PUBEXPO(pvk
));
1536 buf
+= ROUNDUP(offset
, 8);
1539 if ((rv
= soft_unpack_obj_attribute(buf
, &priexpo
,
1540 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1543 copy_bigint_attr(&priexpo
, KEY_PRI_RSA_PRIEXPO(pvk
));
1545 buf
+= ROUNDUP(offset
, 8);
1548 if ((rv
= soft_unpack_obj_attribute(buf
, &prime1
,
1549 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1552 copy_bigint_attr(&prime1
, KEY_PRI_RSA_PRIME1(pvk
));
1554 buf
+= ROUNDUP(offset
, 8);
1557 if ((rv
= soft_unpack_obj_attribute(buf
, &prime2
,
1558 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1561 copy_bigint_attr(&prime2
, KEY_PRI_RSA_PRIME2(pvk
));
1563 buf
+= ROUNDUP(offset
, 8);
1566 if ((rv
= soft_unpack_obj_attribute(buf
, &expo1
,
1567 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1570 copy_bigint_attr(&expo1
, KEY_PRI_RSA_EXPO1(pvk
));
1572 buf
+= ROUNDUP(offset
, 8);
1575 if ((rv
= soft_unpack_obj_attribute(buf
, &expo2
,
1576 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1579 copy_bigint_attr(&expo2
, KEY_PRI_RSA_EXPO2(pvk
));
1581 buf
+= ROUNDUP(offset
, 8);
1584 if ((rv
= soft_unpack_obj_attribute(buf
, &coef
,
1585 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1588 copy_bigint_attr(&coef
, KEY_PRI_RSA_COEF(pvk
));
1594 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1595 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1598 copy_bigint_attr(&prime
, KEY_PRI_DSA_PRIME(pvk
));
1600 buf
+= ROUNDUP(offset
, 8);
1603 if ((rv
= soft_unpack_obj_attribute(buf
, &subprime
,
1604 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1607 copy_bigint_attr(&subprime
, KEY_PRI_DSA_SUBPRIME(pvk
));
1609 buf
+= ROUNDUP(offset
, 8);
1612 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1613 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1616 copy_bigint_attr(&base
, KEY_PRI_DSA_BASE(pvk
));
1618 buf
+= ROUNDUP(offset
, 8);
1621 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1622 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1625 copy_bigint_attr(&value
, KEY_PRI_DSA_VALUE(pvk
));
1631 (void) memcpy(&tmp_val
, buf
, sizeof (uint64_t));
1632 KEY_PRI_DH_VAL_BITS(pvk
) = (CK_ULONG
)(SWAP64(tmp_val
));
1633 buf
= buf
+ sizeof (uint64_t);
1636 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1637 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1640 copy_bigint_attr(&prime
, KEY_PRI_DH_PRIME(pvk
));
1642 buf
+= ROUNDUP(offset
, 8);
1645 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1646 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1649 copy_bigint_attr(&base
, KEY_PRI_DH_BASE(pvk
));
1651 buf
+= ROUNDUP(offset
, 8);
1654 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1655 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1658 copy_bigint_attr(&value
, KEY_PRI_DH_VALUE(pvk
));
1664 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1665 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1668 copy_bigint_attr(&value
, KEY_PRI_EC_VALUE(pvk
));
1673 if ((rv
= soft_unpack_obj_attribute(buf
, &prime
,
1674 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1677 copy_bigint_attr(&prime
, KEY_PRI_DH942_PRIME(pvk
));
1679 buf
+= ROUNDUP(offset
, 8);
1682 if ((rv
= soft_unpack_obj_attribute(buf
, &base
,
1683 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1686 copy_bigint_attr(&base
, KEY_PRI_DH942_BASE(pvk
));
1688 buf
+= ROUNDUP(offset
, 8);
1691 if ((rv
= soft_unpack_obj_attribute(buf
, &subprime
,
1692 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1695 copy_bigint_attr(&subprime
, KEY_PRI_DH942_BASE(pvk
));
1697 buf
+= ROUNDUP(offset
, 8);
1700 if ((rv
= soft_unpack_obj_attribute(buf
, &value
,
1701 NULL
, &offset
, B_FALSE
)) != CKR_OK
)
1704 copy_bigint_attr(&value
, KEY_PRI_DH942_VALUE(pvk
));
1711 case CKO_SECRET_KEY
:
1712 /* Allocate storage for Secret Key Object. */
1713 sck
= calloc(1, sizeof (secret_key_obj_t
));
1715 return (CKR_HOST_MEMORY
);
1718 objp
->object_class_u
.secret_key
= sck
;
1721 (void) memcpy((void *)&tmp_val
, buf
, sizeof (uint64_t));
1722 OBJ_SEC_VALUE_LEN(objp
) = (CK_ULONG
)(SWAP64(tmp_val
));
1723 buf
= buf
+ sizeof (uint64_t);
1725 if (OBJ_SEC_VALUE_LEN(objp
) > 0) {
1726 OBJ_SEC_VALUE(objp
) = malloc(OBJ_SEC_VALUE_LEN(objp
));
1727 if (OBJ_SEC_VALUE(objp
) == NULL
) {
1729 return (CKR_HOST_MEMORY
);
1731 (void) memcpy(OBJ_SEC_VALUE(objp
), buf
,
1732 OBJ_SEC_VALUE_LEN(objp
));
1734 buf
= buf
+ ROUNDUP(OBJ_SEC_VALUE_LEN(objp
), 8);
1739 case CKO_CERTIFICATE
:
1740 /* Allocate storage for Certificate Object. */
1741 cert
= calloc(1, sizeof (certificate_obj_t
));
1743 return (CKR_HOST_MEMORY
);
1745 (void) memset(cert
, 0, sizeof (certificate_obj_t
));
1747 cert
->certificate_type
= certtype
;
1748 objp
->object_class_u
.certificate
= cert
;
1753 if ((rv
= soft_unpack_obj_attribute(buf
, NULL
,
1754 &cert
->cert_type_u
.x509
.subject
,
1755 &offset
, B_TRUE
)) != CKR_OK
) {
1760 buf
+= ROUNDUP(offset
, 8);
1763 if ((rv
= soft_unpack_obj_attribute(buf
, NULL
,
1764 &cert
->cert_type_u
.x509
.value
,
1765 &offset
, B_TRUE
)) != CKR_OK
) {
1772 case CKC_X_509_ATTR_CERT
:
1774 if ((rv
= soft_unpack_obj_attribute(buf
, NULL
,
1775 &cert
->cert_type_u
.x509_attr
.owner
,
1776 &offset
, B_TRUE
)) != CKR_OK
) {
1781 buf
+= ROUNDUP(offset
, 8);
1784 if ((rv
= soft_unpack_obj_attribute(buf
, NULL
,
1785 &cert
->cert_type_u
.x509_attr
.value
,
1786 &offset
, B_TRUE
)) != CKR_OK
) {
1796 case CKO_DOMAIN_PARAMETERS
:
1803 * cleanup the storage allocated to the local variables.
1807 bigint_attr_cleanup(&modulus
);
1808 bigint_attr_cleanup(&pubexpo
);
1809 bigint_attr_cleanup(&prime
);
1810 bigint_attr_cleanup(&subprime
);
1811 bigint_attr_cleanup(&base
);
1812 bigint_attr_cleanup(&value
);
1817 * cleanup the storage allocated to the local variables.
1821 bigint_attr_cleanup(&modulus
);
1822 bigint_attr_cleanup(&priexpo
);
1823 bigint_attr_cleanup(&prime
);
1824 bigint_attr_cleanup(&subprime
);
1825 bigint_attr_cleanup(&base
);
1826 bigint_attr_cleanup(&value
);
1827 bigint_attr_cleanup(&pubexpo
);
1828 bigint_attr_cleanup(&prime1
);
1829 bigint_attr_cleanup(&prime2
);
1830 bigint_attr_cleanup(&expo1
);
1831 bigint_attr_cleanup(&expo2
);
1832 bigint_attr_cleanup(&coef
);
1838 * Store the token object to a keystore file.
1841 soft_put_object_to_keystore(soft_object_t
*objp
)
1848 rv
= soft_keystore_pack_obj(objp
, &buf
, &len
);
1852 (void) pthread_mutex_lock(&soft_slot
.slot_mutex
);
1853 if (objp
->object_type
== TOKEN_PUBLIC
) {
1854 if ((soft_keystore_put_new_obj(buf
, len
, B_TRUE
,
1855 B_FALSE
, &objp
->ks_handle
)) == -1) {
1856 (void) pthread_mutex_unlock(&soft_slot
.slot_mutex
);
1858 return (CKR_FUNCTION_FAILED
);
1861 if ((soft_keystore_put_new_obj(buf
, len
, B_FALSE
,
1862 B_FALSE
, &objp
->ks_handle
)) == -1) {
1863 (void) pthread_mutex_unlock(&soft_slot
.slot_mutex
);
1865 return (CKR_FUNCTION_FAILED
);
1868 (void) pthread_mutex_unlock(&soft_slot
.slot_mutex
);
1875 * Modify the in-core token object and then write it to
1879 soft_modify_object_to_keystore(soft_object_t
*objp
)
1886 rv
= soft_keystore_pack_obj(objp
, &buf
, &len
);
1890 /* B_TRUE: caller has held a writelock on the keystore */
1891 if (soft_keystore_modify_obj(&objp
->ks_handle
, buf
, len
,
1893 return (CKR_FUNCTION_FAILED
);
1902 * Read the token object from the keystore file.
1905 soft_get_token_objects_from_keystore(ks_search_type_t type
)
1908 ks_obj_t
*ks_obj
= NULL
, *ks_obj_next
;
1909 soft_object_t
*new_objp
= NULL
;
1911 /* Load the token object from keystore based on the object type */
1912 rv
= soft_keystore_get_objs(type
, &ks_obj
, B_FALSE
);
1919 new_objp
= calloc(1, sizeof (soft_object_t
));
1920 if (new_objp
== NULL
) {
1921 rv
= CKR_HOST_MEMORY
;
1924 /* Convert the keystore format to memory format */
1925 rv
= soft_keystore_unpack_obj(new_objp
, ks_obj
);
1927 if (new_objp
->class == CKO_CERTIFICATE
)
1928 soft_cleanup_cert_object(new_objp
);
1930 soft_cleanup_object(new_objp
);
1934 soft_add_token_object_to_slot(new_objp
);
1936 /* Free the ks_obj list */
1937 ks_obj_next
= ks_obj
->next
;
1940 ks_obj
= ks_obj_next
;
1947 ks_obj_next
= ks_obj
->next
;
1950 ks_obj
= ks_obj_next
;
1956 * soft_gen_crypt_key()
1960 * pPIN: pointer to caller provided Pin
1961 * key: output argument which contains the address of the
1962 * pointer to encryption key in the soft_object_t.
1963 * It is caller's responsibility to call soft_delete_object()
1964 * if this key is no longer in use.
1965 * saltdata: input argument (if non-NULL), or
1966 * output argument (if NULL):
1967 * address of pointer to the "salt" of the encryption key
1971 * Generate an encryption key of the input PIN.
1976 * Other: some error occurred while generating the encryption key
1980 soft_gen_crypt_key(uchar_t
*pPIN
, soft_object_t
**key
, CK_BYTE
**saltdata
)
1982 CK_OBJECT_CLASS
class = CKO_SECRET_KEY
;
1983 CK_ATTRIBUTE tmpl
[5];
1986 CK_MECHANISM Mechanism
;
1987 CK_PKCS5_PBKD2_PARAMS params
;
1988 CK_BYTE salt
[PBKD2_SALT_SIZE
];
1989 CK_ULONG keylen
= AES_MIN_KEY_BYTES
;
1990 CK_KEY_TYPE keytype
= CKK_AES
;
1991 static CK_BBOOL truevalue
= TRUE
;
1992 soft_object_t
*secret_key
;
1993 CK_OBJECT_HANDLE hKey
;
1994 CK_ULONG passwd_size
;
1997 return (CKR_FUNCTION_FAILED
);
1999 tmpl
[attrs
].type
= CKA_CLASS
;
2000 tmpl
[attrs
].pValue
= &class;
2001 tmpl
[attrs
].ulValueLen
= sizeof (class);
2004 tmpl
[attrs
].type
= CKA_KEY_TYPE
;
2005 tmpl
[attrs
].pValue
= &keytype
;
2006 tmpl
[attrs
].ulValueLen
= sizeof (keytype
);
2009 tmpl
[attrs
].type
= CKA_ENCRYPT
;
2010 tmpl
[attrs
].pValue
= &truevalue
;
2011 tmpl
[attrs
].ulValueLen
= sizeof (CK_BBOOL
);
2014 tmpl
[attrs
].type
= CKA_DECRYPT
;
2015 tmpl
[attrs
].pValue
= &truevalue
;
2016 tmpl
[attrs
].ulValueLen
= sizeof (CK_BBOOL
);
2019 tmpl
[attrs
].type
= CKA_VALUE_LEN
;
2020 tmpl
[attrs
].pValue
= &keylen
;
2021 tmpl
[attrs
].ulValueLen
= sizeof (keylen
);
2024 if (*saltdata
== NULL
) {
2025 bzero(salt
, sizeof (salt
));
2026 (void) pkcs11_get_nzero_urandom(salt
, sizeof (salt
));
2027 *saltdata
= malloc(PBKD2_SALT_SIZE
);
2028 if (*saltdata
== NULL
)
2029 return (CKR_HOST_MEMORY
);
2030 (void) memcpy(*saltdata
, salt
, PBKD2_SALT_SIZE
);
2032 bzero(salt
, sizeof (salt
));
2033 (void) memcpy(salt
, *saltdata
, PBKD2_SALT_SIZE
);
2036 Mechanism
.mechanism
= CKM_PKCS5_PBKD2
;
2037 Mechanism
.pParameter
= ¶ms
;
2038 Mechanism
.ulParameterLen
= sizeof (params
);
2039 passwd_size
= (CK_ULONG
)strlen((const char *)pPIN
);
2041 params
.saltSource
= CKZ_SALT_SPECIFIED
;
2042 params
.pSaltSourceData
= (void *)salt
;
2043 params
.ulSaltSourceDataLen
= sizeof (salt
);
2044 params
.iterations
= PBKD2_ITERATIONS
;
2045 params
.prf
= CKP_PKCS5_PBKD2_HMAC_SHA1
;
2046 params
.pPrfData
= NULL
;
2047 params
.ulPrfDataLen
= 0;
2048 params
.pPassword
= (CK_UTF8CHAR_PTR
)pPIN
;
2049 params
.ulPasswordLen
= &passwd_size
;
2051 rv
= soft_gen_keyobject(tmpl
, attrs
, &hKey
, &token_session
,
2052 CKO_SECRET_KEY
, CKK_AES
, 0, SOFT_GEN_KEY
, B_TRUE
);
2058 /* Obtain the secret object pointer. */
2059 secret_key
= (soft_object_t
*)hKey
;
2060 keylen
= OBJ_SEC_VALUE_LEN(secret_key
);
2061 if ((OBJ_SEC_VALUE(secret_key
) = malloc(keylen
)) == NULL
) {
2062 soft_delete_object(&token_session
, secret_key
,
2064 return (CKR_HOST_MEMORY
);
2067 rv
= soft_generate_pkcs5_pbkdf2_key(&token_session
, &Mechanism
,
2071 soft_delete_object(&token_session
, secret_key
,
2081 * soft_gen_hmac_key()
2085 * pPIN: pointer to caller provided Pin
2086 * key: output argument which contains the address of the
2087 * pointer to hmac key in the soft_object_t.
2088 * It is caller's responsibility to call soft_delete_object()
2089 * if this key is no longer in use.
2090 * saltdata: input argument (if non-NULL), or
2091 * output argument (if NULL):
2092 * address of pointer to the "salt" of the hmac key
2096 * Generate a hmac key of the input PIN.
2101 * Other: some error occurred while generating the hmac key
2105 soft_gen_hmac_key(uchar_t
*pPIN
, soft_object_t
**key
, CK_BYTE
**saltdata
)
2107 CK_OBJECT_CLASS
class = CKO_SECRET_KEY
;
2108 CK_ATTRIBUTE tmpl
[5];
2111 CK_MECHANISM Mechanism
;
2112 CK_PKCS5_PBKD2_PARAMS params
;
2113 CK_BYTE salt
[PBKD2_SALT_SIZE
];
2114 CK_ULONG keylen
= 16;
2115 CK_KEY_TYPE keytype
= CKK_GENERIC_SECRET
;
2116 static CK_BBOOL truevalue
= TRUE
;
2117 soft_object_t
*secret_key
;
2118 CK_OBJECT_HANDLE hKey
;
2119 CK_ULONG passwd_size
;
2122 return (CKR_FUNCTION_FAILED
);
2124 tmpl
[attrs
].type
= CKA_CLASS
;
2125 tmpl
[attrs
].pValue
= &class;
2126 tmpl
[attrs
].ulValueLen
= sizeof (class);
2129 tmpl
[attrs
].type
= CKA_KEY_TYPE
;
2130 tmpl
[attrs
].pValue
= &keytype
;
2131 tmpl
[attrs
].ulValueLen
= sizeof (keytype
);
2134 tmpl
[attrs
].type
= CKA_SIGN
;
2135 tmpl
[attrs
].pValue
= &truevalue
;
2136 tmpl
[attrs
].ulValueLen
= sizeof (CK_BBOOL
);
2139 tmpl
[attrs
].type
= CKA_VERIFY
;
2140 tmpl
[attrs
].pValue
= &truevalue
;
2141 tmpl
[attrs
].ulValueLen
= sizeof (CK_BBOOL
);
2144 tmpl
[attrs
].type
= CKA_VALUE_LEN
;
2145 tmpl
[attrs
].pValue
= &keylen
;
2146 tmpl
[attrs
].ulValueLen
= sizeof (keylen
);
2149 if (*saltdata
== NULL
) {
2150 bzero(salt
, sizeof (salt
));
2151 (void) pkcs11_get_nzero_urandom(salt
, sizeof (salt
));
2152 *saltdata
= malloc(PBKD2_SALT_SIZE
);
2153 if (*saltdata
== NULL
)
2154 return (CKR_HOST_MEMORY
);
2155 (void) memcpy(*saltdata
, salt
, PBKD2_SALT_SIZE
);
2157 bzero(salt
, sizeof (salt
));
2158 (void) memcpy(salt
, *saltdata
, PBKD2_SALT_SIZE
);
2161 Mechanism
.mechanism
= CKM_PKCS5_PBKD2
;
2162 Mechanism
.pParameter
= ¶ms
;
2163 Mechanism
.ulParameterLen
= sizeof (params
);
2164 passwd_size
= (CK_ULONG
)strlen((const char *)pPIN
);
2166 params
.saltSource
= CKZ_SALT_SPECIFIED
;
2167 params
.pSaltSourceData
= (void *)salt
;
2168 params
.ulSaltSourceDataLen
= sizeof (salt
);
2169 params
.iterations
= PBKD2_ITERATIONS
;
2170 params
.prf
= CKP_PKCS5_PBKD2_HMAC_SHA1
;
2171 params
.pPrfData
= NULL
;
2172 params
.ulPrfDataLen
= 0;
2173 params
.pPassword
= (CK_UTF8CHAR_PTR
)pPIN
;
2174 params
.ulPasswordLen
= &passwd_size
;
2176 rv
= soft_gen_keyobject(tmpl
, attrs
, &hKey
, &token_session
,
2177 CKO_SECRET_KEY
, CKK_GENERIC_SECRET
, 0, SOFT_GEN_KEY
, B_TRUE
);
2183 /* Obtain the secret object pointer. */
2184 secret_key
= (soft_object_t
*)hKey
;
2185 keylen
= OBJ_SEC_VALUE_LEN(secret_key
);
2186 if ((OBJ_SEC_VALUE(secret_key
) = malloc(keylen
)) == NULL
) {
2187 soft_delete_object(&token_session
, secret_key
,
2189 return (CKR_HOST_MEMORY
);
2192 rv
= soft_generate_pkcs5_pbkdf2_key(&token_session
, &Mechanism
,
2196 soft_delete_object(&token_session
, secret_key
,
2206 * The token session is just a psuedo session (a place holder)
2207 * to hold some information during encryption/decryption and
2208 * sign/verify operations when writing/reading the keystore
2212 soft_init_token_session(void)
2216 token_session
.magic_marker
= SOFTTOKEN_SESSION_MAGIC
;
2217 token_session
.pApplication
= NULL_PTR
;
2218 token_session
.Notify
= NULL
;
2219 token_session
.flags
= CKF_SERIAL_SESSION
;
2220 token_session
.state
= CKS_RO_PUBLIC_SESSION
;
2221 token_session
.object_list
= NULL
;
2222 token_session
.ses_refcnt
= 0;
2223 token_session
.ses_close_sync
= 0;
2224 token_session
.next
= NULL
;
2225 token_session
.prev
= NULL
;
2227 /* Initialize the lock for the token session */
2228 if (pthread_mutex_init(&token_session
.session_mutex
, NULL
) != 0) {
2229 return (CKR_CANT_LOCK
);
2232 (void) pthread_cond_init(&token_session
.ses_free_cond
, NULL
);
2239 soft_destroy_token_session(void)
2242 (void) pthread_cond_destroy(&token_session
.ses_free_cond
);
2243 (void) pthread_mutex_destroy(&token_session
.session_mutex
);
2248 * Encrypt/Decrypt the private token object when dealing with the keystore.
2249 * This function only applies to the private token object.
2252 soft_keystore_crypt(soft_object_t
*key_p
, uchar_t
*ivec
, boolean_t encrypt
,
2253 CK_BYTE_PTR in
, CK_ULONG in_len
, CK_BYTE_PTR out
, CK_ULONG_PTR out_len
)
2256 soft_aes_ctx_t
*soft_aes_ctx
;
2258 CK_ULONG tmplen
, tmplen1
;
2261 * The caller will pass NULL for "out" (output buffer) to find out
2262 * the output buffer size that it need to allocate for the encrption
2266 mech
.mechanism
= CKM_AES_CBC_PAD
;
2267 mech
.pParameter
= (void *)ivec
;
2268 mech
.ulParameterLen
= AES_BLOCK_LEN
;
2271 rv
= soft_aes_crypt_init_common(&token_session
, &mech
,
2274 rv
= soft_aes_crypt_init_common(&token_session
, &mech
,
2281 (void) pthread_mutex_lock(&token_session
.session_mutex
);
2285 (soft_aes_ctx_t
*)token_session
.encrypt
.context
;
2288 (soft_aes_ctx_t
*)token_session
.decrypt
.context
;
2290 /* Copy Initialization Vector (IV) into the context. */
2291 (void) memcpy(soft_aes_ctx
->ivec
, ivec
, AES_BLOCK_LEN
);
2293 /* Allocate a context for AES cipher-block chaining. */
2294 soft_aes_ctx
->aes_cbc
= (void *)aes_cbc_ctx_init(
2295 soft_aes_ctx
->key_sched
, soft_aes_ctx
->keysched_len
,
2296 soft_aes_ctx
->ivec
);
2298 if (soft_aes_ctx
->aes_cbc
== NULL
) {
2299 bzero(soft_aes_ctx
->key_sched
,
2300 soft_aes_ctx
->keysched_len
);
2301 free(soft_aes_ctx
->key_sched
);
2303 free(token_session
.encrypt
.context
);
2304 token_session
.encrypt
.context
= NULL
;
2306 free(token_session
.encrypt
.context
);
2307 token_session
.encrypt
.context
= NULL
;
2310 (void) pthread_mutex_unlock(&token_session
.
2312 return (CKR_HOST_MEMORY
);
2315 (void) pthread_mutex_unlock(&token_session
.session_mutex
);
2317 * Since out == NULL, the soft_aes_xxcrypt_common() will
2318 * simply return the output buffer length to the caller.
2321 rv
= soft_aes_encrypt_common(&token_session
, in
,
2322 in_len
, out
, out_len
, B_FALSE
);
2324 rv
= soft_aes_decrypt_common(&token_session
, in
,
2325 in_len
, out
, out_len
, B_FALSE
);
2330 * The caller has allocated the output buffer, so that we
2331 * are doing the real encryption/decryption this time.
2335 rv
= soft_aes_encrypt_common(&token_session
, in
,
2336 in_len
, out
, &tmplen
, B_TRUE
);
2338 tmplen1
= *out_len
- tmplen
;
2339 rv
= soft_encrypt_final(&token_session
,
2340 out
+tmplen
, &tmplen1
);
2341 *out_len
= tmplen
+ tmplen1
;
2344 rv
= soft_aes_decrypt_common(&token_session
, in
,
2345 in_len
, out
, &tmplen
, B_TRUE
);
2347 tmplen1
= *out_len
- tmplen
;
2348 rv
= soft_decrypt_final(&token_session
,
2349 out
+tmplen
, &tmplen1
);
2350 *out_len
= tmplen
+ tmplen1
;
2360 * Sign/Verify the private token object for checking its data integrity
2361 * when dealing with the keystore.
2362 * This function only applies to the private token object.
2365 soft_keystore_hmac(soft_object_t
*key_p
, boolean_t sign
,
2366 CK_BYTE_PTR in
, CK_ULONG in_len
, CK_BYTE_PTR out
, CK_ULONG_PTR out_len
)
2371 mech
.mechanism
= CKM_MD5_HMAC
;
2372 mech
.pParameter
= NULL_PTR
;
2373 mech
.ulParameterLen
= 0;
2375 rv
= soft_hmac_sign_verify_init_common(&token_session
, &mech
,
2382 rv
= soft_sign(&token_session
, in
, in_len
, out
, out_len
);
2384 rv
= soft_verify(&token_session
, in
, in_len
, out
, *out_len
);