dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / pkcs11 / pkcs11_softtoken / common / softKeystore.c
blobe3581d340925b9b0ed1e9adae828561bc8cefda3
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <crypt.h>
26 #include <cryptoutil.h>
27 #include <pwd.h>
28 #include <pthread.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <strings.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"
39 #include "softKeys.h"
40 #include "softKeystore.h"
41 #include "softKeystoreUtil.h"
42 #include "softMAC.h"
43 #include "softOps.h"
45 soft_session_t token_session;
48 * soft_gen_hashed_pin()
50 * Arguments:
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
59 * Description:
61 * Generate a hashed pin using system provided crypt(3C) function.
63 * Returns:
65 * 0: no error
66 * -1: some error occurred while generating the hashed pin
69 int
70 soft_gen_hashed_pin(CK_UTF8CHAR_PTR pPin, char **result, char **salt)
73 uid_t uid;
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.
82 uid = geteuid();
83 if (getpwuid_r(uid, &pwd, pwdbuf, PWD_BUFFER_SIZE, &pw) != 0) {
84 return (-1);
87 if (*salt == NULL) {
88 new_salt = B_TRUE;
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) {
96 return (-1);
100 if ((*result = crypt((char *)pPin, *salt)) == NULL) {
101 if (new_salt)
102 free(*salt);
103 return (-1);
106 return (0);
110 * Authenticate user's PIN for C_Login.
112 CK_RV
113 soft_verify_pin(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
116 char *user_cryptpin = NULL;
117 char *ks_cryptpin = NULL;
118 char *salt = NULL;
119 uchar_t *tmp_pin = NULL;
120 boolean_t pin_initialized = B_FALSE;
121 CK_RV rv = CKR_OK;
124 * Check to see if keystore is initialized.
126 rv = soft_keystore_pin_initialized(&pin_initialized, &ks_cryptpin,
127 B_FALSE);
128 if (rv != CKR_OK)
129 return (rv);
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;
138 goto cleanup;
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;
147 goto cleanup;
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;
155 goto cleanup;
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;
164 goto cleanup;
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
171 * keystore.
173 if (soft_keystore_authpin(tmp_pin) != 0) {
174 rv = CKR_FUNCTION_FAILED;
175 } else {
176 rv = CKR_OK;
178 goto cleanup;
179 } else {
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;
188 rv = CKR_OK;
191 cleanup:
192 free(salt);
193 free(tmp_pin);
194 free(ks_cryptpin);
196 return (rv);
200 * The second level C_SetPIN function.
202 CK_RV
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;
209 char *salt = NULL;
210 boolean_t pin_initialized = B_FALSE;
211 uchar_t *tmp_old_pin = NULL, *tmp_new_pin = NULL;
212 CK_RV rv = CKR_OK;
215 * Check to see if keystore is initialized.
217 rv = soft_keystore_pin_initialized(&pin_initialized, &ks_cryptpin,
218 B_FALSE);
219 if (rv != CKR_OK)
220 return (rv);
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;
231 goto cleanup;
234 tmp_old_pin = malloc(ulOldPinLen + 1);
235 if (tmp_old_pin == NULL) {
236 rv = CKR_HOST_MEMORY;
237 goto cleanup;
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,
243 &salt) < 0) {
244 rv = CKR_FUNCTION_FAILED;
245 goto cleanup;
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;
254 goto cleanup;
256 } else {
258 * This is the first time to setpin, the oldpin must be
259 * "changeme".
261 if (strncmp("changeme", (const char *)pOldPin,
262 ulOldPinLen) != 0) {
263 rv = CKR_PIN_INCORRECT;
264 goto cleanup;
268 tmp_new_pin = malloc(ulNewPinLen + 1);
269 if (tmp_new_pin == NULL) {
270 rv = CKR_HOST_MEMORY;
271 goto cleanup;
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;
281 goto cleanup;
282 } else {
283 (void) pthread_mutex_lock(&soft_giant_mutex);
284 soft_slot.userpin_change_needed = 0;
285 (void) pthread_mutex_unlock(&soft_giant_mutex);
286 rv = CKR_OK;
289 cleanup:
290 free(salt);
291 free(ks_cryptpin);
292 free(tmp_old_pin);
293 free(tmp_new_pin);
295 return (rv);
299 * soft_keystore_pack_obj()
301 * Arguments:
303 * obj: pointer to the soft_object_t of the token object to
304 * be packed
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
312 * Description:
314 * Pack the in-core token object into the keystore format.
316 * Returns:
318 * CKR_OK: no error
319 * Other: some error occurred while packing the object
322 CK_RV
323 soft_keystore_pack_obj(soft_object_t *obj, uchar_t **ks_buf, size_t *len)
325 ks_obj_hdr_t hdr;
326 ks_attr_hdr_t attr_hdr;
327 CK_ATTRIBUTE_INFO_PTR extra_attr;
328 int num_attrs = 0;
329 ulong_t len_attrs = 0;
330 size_t ks_len;
331 uchar_t *buf, *buf1;
332 CK_RV rv;
333 int i;
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;
354 while (extra_attr) {
355 num_attrs++;
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);
364 if (buf == NULL) {
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
384 * the key itself.
386 rv = soft_pack_object(obj, buf1);
387 *len = ks_len;
388 *ks_buf = buf;
390 return (rv);
395 * soft_keystore_unpack_obj()
397 * Arguments:
399 * obj: pointer to the soft_object_t to store the unpacked
400 * token object
401 * ks_obj: input argument which contains the pointer to the
402 * ks_obj_t struct of packed token object to be unpacked
404 * Description:
406 * Unpack the token object in keystore format to in-core soft_object_t.
408 * Returns:
410 * CKR_OK: no error
411 * Other: some error occurred while unpacking the object
414 CK_RV
415 soft_keystore_unpack_obj(soft_object_t *obj, ks_obj_t *ks_obj)
418 CK_RV rv;
419 ks_obj_hdr_t *hdr;
420 ks_attr_hdr_t *attr_hdr;
421 CK_ATTRIBUTE template;
422 int i;
423 uchar_t *buf;
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);
475 if (rv != CKR_OK) {
476 return (rv);
479 buf = buf + ROUNDUP(template.ulValueLen, 8);
483 * Unpack the key itself.
485 rv = soft_unpack_object(obj, buf);
486 return (rv);
492 * soft_unpack_obj_attribute()
494 * Arguments:
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
499 * in cert_dest
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)
506 * Description:
508 * Unpack the attribute from keystore format to the big integer format.
510 * Returns:
512 * CKR_OK: no error
513 * Other: some error occurred while unpacking the object attribute
516 CK_RV
517 soft_unpack_obj_attribute(uchar_t *buf, biginteger_t *key_dest,
518 cert_attr_t **cert_dest, ulong_t *offset, boolean_t cert)
521 CK_RV rv;
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);
533 if (cert) {
534 rv = get_cert_attr_from_template(cert_dest, &template);
535 } else {
536 rv = get_bigint_attr_from_template(key_dest, &template);
539 free(template.pValue);
540 if (rv != CKR_OK) {
541 return (rv);
544 *offset = sizeof (uint64_t) + template.ulValueLen;
545 return (CKR_OK);
550 * Calculate the total buffer length required to store the
551 * object key (the third part) in a keystore format.
553 ulong_t
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;
561 switch (class) {
562 case CKO_PUBLIC_KEY:
563 switch (keytype) {
564 case CKK_RSA:
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));
575 case CKK_DSA:
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));
589 case CKK_EC:
591 * ec_point_len + ec_point
593 return (ROUNDUP(((biginteger_t *)
594 OBJ_PUB_EC_POINT(objp))->big_value_len, 8) +
595 sizeof (uint64_t));
596 case CKK_DH:
598 * prime_len + prime + base_len + base +
599 * value_len + value
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));
609 case CKK_X9_42_DH:
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));
623 } /* keytype */
625 break;
627 case CKO_PRIVATE_KEY:
628 switch (keytype) {
629 case CKK_RSA:
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));
654 case CKK_DSA:
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));
669 case CKK_DH:
671 * value_bits + prime_len + prime + base_len + base +
672 * value_len + value
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));
682 case CKK_EC:
684 * value_len + value
686 return (ROUNDUP(((biginteger_t *)
687 OBJ_PRI_EC_VALUE(objp))->big_value_len, 8) +
688 sizeof (uint64_t));
690 case CKK_X9_42_DH:
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));
705 } /* keytype */
707 break;
709 case CKO_SECRET_KEY:
711 * value_len + value
713 return (ROUNDUP(OBJ_SEC_VALUE_LEN(objp), 8) +
714 sizeof (uint64_t));
716 case CKO_CERTIFICATE:
717 switch (certtype) {
718 case CKC_X_509:
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));
738 return (0);
740 case CKO_DOMAIN_PARAMETERS:
742 return (0);
744 return (0);
748 * Pack the object key (the third part) from the soft_object_t
749 * into the keystore format.
751 CK_RV
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;
758 uint64_t tmp_val;
760 switch (class) {
761 case CKO_PUBLIC_KEY:
762 switch (keytype) {
763 case CKK_RSA:
764 /* modulus_bits */
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),
777 ((biginteger_t *)
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),
790 ((biginteger_t *)
791 OBJ_PUB_RSA_PUBEXPO(objp))->big_value_len);
792 break;
794 case CKK_DSA:
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,
803 ((biginteger_t *)
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,
816 ((biginteger_t *)
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,
829 ((biginteger_t *)
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,
842 ((biginteger_t *)
843 OBJ_PUB_DSA_VALUE(objp))->big_value_len);
845 break;
846 case CKK_EC:
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,
855 ((biginteger_t *)
856 OBJ_PUB_EC_POINT(objp))->big_value_len);
857 break;
859 case CKK_DH:
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,
868 ((biginteger_t *)
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,
881 ((biginteger_t *)
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,
894 ((biginteger_t *)
895 OBJ_PUB_DH_VALUE(objp))->big_value_len);
897 break;
899 case CKK_X9_42_DH:
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,
908 ((biginteger_t *)
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,
921 ((biginteger_t *)
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,
934 ((biginteger_t *)
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,
947 ((biginteger_t *)
948 OBJ_PUB_DH942_VALUE(objp))->big_value_len);
950 break;
951 } /* keytype */
953 break;
955 case CKO_PRIVATE_KEY:
956 switch (keytype) {
957 case CKK_RSA:
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,
966 ((biginteger_t *)
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,
979 ((biginteger_t *)
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,
992 ((biginteger_t *)
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,
1005 ((biginteger_t *)
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,
1018 ((biginteger_t *)
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,
1031 ((biginteger_t *)
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,
1044 ((biginteger_t *)
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,
1057 ((biginteger_t *)
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);
1062 break;
1064 case CKK_DSA:
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,
1073 ((biginteger_t *)
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,
1086 ((biginteger_t *)
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,
1099 ((biginteger_t *)
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,
1112 ((biginteger_t *)
1113 OBJ_PRI_DSA_VALUE(objp))->big_value_len);
1115 break;
1116 case CKK_EC:
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,
1125 ((biginteger_t *)
1126 OBJ_PRI_EC_VALUE(objp))->big_value_len);
1127 break;
1129 case CKK_DH:
1130 /* value_bits */
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,
1143 ((biginteger_t *)
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,
1156 ((biginteger_t *)
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,
1169 ((biginteger_t *)
1170 OBJ_PRI_DH_VALUE(objp))->big_value_len);
1172 break;
1174 case CKK_X9_42_DH:
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,
1183 ((biginteger_t *)
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,
1196 ((biginteger_t *)
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,
1209 ((biginteger_t *)
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,
1222 ((biginteger_t *)
1223 OBJ_PRI_DH942_VALUE(objp))->big_value_len);
1225 break;
1227 } /* keytype */
1229 break;
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);
1243 break;
1245 case CKO_CERTIFICATE:
1247 switch (certtype) {
1248 case CKC_X_509:
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,
1257 ((cert_attr_t *)
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,
1270 ((cert_attr_t *)
1271 X509_CERT_VALUE(objp))->length);
1272 break;
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,
1283 ((cert_attr_t *)
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,
1296 ((cert_attr_t *)
1297 X509_ATTR_CERT_VALUE(objp))->length);
1298 break;
1300 break;
1302 case CKO_DOMAIN_PARAMETERS:
1304 return (0);
1306 return (CKR_OK);
1310 * Unpack the object key in keystore format (the third part)
1311 * into soft_object_t.
1313 CK_RV
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;
1327 biginteger_t prime;
1328 biginteger_t subprime;
1329 biginteger_t base;
1330 biginteger_t value;
1332 biginteger_t priexpo;
1333 biginteger_t prime1;
1334 biginteger_t prime2;
1335 biginteger_t expo1;
1336 biginteger_t expo2;
1337 biginteger_t coef;
1338 CK_RV rv = CKR_OK;
1339 ulong_t offset = 0;
1340 uint64_t tmp_val;
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));
1357 switch (class) {
1359 case CKO_PUBLIC_KEY:
1360 /* Allocate storage for Public Key Object. */
1361 pbk = calloc(1, sizeof (public_key_obj_t));
1362 if (pbk == NULL) {
1363 rv = CKR_HOST_MEMORY;
1364 return (rv);
1367 objp->object_class_u.public_key = pbk;
1369 switch (keytype) {
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);
1375 /* modulus */
1376 if ((rv = soft_unpack_obj_attribute(buf, &modulus,
1377 NULL, &offset, B_FALSE)) != CKR_OK)
1378 goto pub_cleanup;
1380 copy_bigint_attr(&modulus, KEY_PUB_RSA_MOD(pbk));
1382 buf += ROUNDUP(offset, 8);
1384 /* pubexpo */
1385 if ((rv = soft_unpack_obj_attribute(buf, &pubexpo,
1386 NULL, &offset, B_FALSE)) != CKR_OK)
1387 goto pub_cleanup;
1389 copy_bigint_attr(&pubexpo, KEY_PUB_RSA_PUBEXPO(pbk));
1391 break;
1393 case CKK_DSA:
1394 /* prime */
1395 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1396 NULL, &offset, B_FALSE)) != CKR_OK)
1397 goto pub_cleanup;
1399 copy_bigint_attr(&prime, KEY_PUB_DSA_PRIME(pbk));
1401 buf += ROUNDUP(offset, 8);
1403 /* subprime */
1404 if ((rv = soft_unpack_obj_attribute(buf, &subprime,
1405 NULL, &offset, B_FALSE)) != CKR_OK)
1406 goto pub_cleanup;
1408 copy_bigint_attr(&subprime, KEY_PUB_DSA_SUBPRIME(pbk));
1410 buf += ROUNDUP(offset, 8);
1412 /* base */
1413 if ((rv = soft_unpack_obj_attribute(buf, &base,
1414 NULL, &offset, B_FALSE)) != CKR_OK)
1415 goto pub_cleanup;
1417 copy_bigint_attr(&base, KEY_PUB_DSA_BASE(pbk));
1419 buf += ROUNDUP(offset, 8);
1421 /* value */
1422 if ((rv = soft_unpack_obj_attribute(buf, &value,
1423 NULL, &offset, B_FALSE)) != CKR_OK)
1424 goto pub_cleanup;
1426 copy_bigint_attr(&value, KEY_PUB_DSA_VALUE(pbk));
1428 break;
1430 case CKK_DH:
1431 /* prime */
1432 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1433 NULL, &offset, B_FALSE)) != CKR_OK)
1434 goto pub_cleanup;
1436 copy_bigint_attr(&prime, KEY_PUB_DH_PRIME(pbk));
1438 buf += ROUNDUP(offset, 8);
1440 /* base */
1441 if ((rv = soft_unpack_obj_attribute(buf, &base,
1442 NULL, &offset, B_FALSE)) != CKR_OK)
1443 goto pub_cleanup;
1445 copy_bigint_attr(&base, KEY_PUB_DH_BASE(pbk));
1447 buf += ROUNDUP(offset, 8);
1449 /* value */
1450 if ((rv = soft_unpack_obj_attribute(buf, &value,
1451 NULL, &offset, B_FALSE)) != CKR_OK)
1452 goto pub_cleanup;
1454 copy_bigint_attr(&value, KEY_PUB_DH_VALUE(pbk));
1456 break;
1458 case CKK_EC:
1459 /* ec_point */
1460 if ((rv = soft_unpack_obj_attribute(buf, &value,
1461 NULL, &offset, B_FALSE)) != CKR_OK)
1462 goto pri_cleanup;
1464 copy_bigint_attr(&value, KEY_PUB_EC_POINT(pbk));
1465 break;
1467 case CKK_X9_42_DH:
1468 /* prime */
1469 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1470 NULL, &offset, B_FALSE)) != CKR_OK)
1471 goto pub_cleanup;
1473 copy_bigint_attr(&prime, KEY_PUB_DH942_PRIME(pbk));
1475 buf += ROUNDUP(offset, 8);
1477 /* base */
1478 if ((rv = soft_unpack_obj_attribute(buf, &base,
1479 NULL, &offset, B_FALSE)) != CKR_OK)
1480 goto pub_cleanup;
1482 copy_bigint_attr(&base, KEY_PUB_DH942_BASE(pbk));
1484 buf += ROUNDUP(offset, 8);
1486 /* subprime */
1487 if ((rv = soft_unpack_obj_attribute(buf, &subprime,
1488 NULL, &offset, B_FALSE)) != CKR_OK)
1489 goto pub_cleanup;
1491 copy_bigint_attr(&subprime,
1492 KEY_PUB_DH942_SUBPRIME(pbk));
1494 buf += ROUNDUP(offset, 8);
1496 /* value */
1497 if ((rv = soft_unpack_obj_attribute(buf, &value,
1498 NULL, &offset, B_FALSE)) != CKR_OK)
1499 goto pub_cleanup;
1501 copy_bigint_attr(&value, KEY_PUB_DH942_VALUE(pbk));
1503 break;
1504 } /* keytype */
1506 break;
1508 case CKO_PRIVATE_KEY:
1509 /* Allocate storage for Private Key Object. */
1510 pvk = calloc(1, sizeof (private_key_obj_t));
1511 if (pvk == NULL) {
1512 rv = CKR_HOST_MEMORY;
1513 return (rv);
1516 objp->object_class_u.private_key = pvk;
1518 switch (keytype) {
1519 case CKK_RSA:
1520 /* modulus */
1521 if ((rv = soft_unpack_obj_attribute(buf, &modulus,
1522 NULL, &offset, B_FALSE)) != CKR_OK)
1523 goto pri_cleanup;
1525 copy_bigint_attr(&modulus, KEY_PRI_RSA_MOD(pvk));
1527 buf += ROUNDUP(offset, 8);
1529 /* pubexpo */
1530 if ((rv = soft_unpack_obj_attribute(buf, &pubexpo,
1531 NULL, &offset, B_FALSE)) != CKR_OK)
1532 goto pri_cleanup;
1534 copy_bigint_attr(&pubexpo, KEY_PRI_RSA_PUBEXPO(pvk));
1536 buf += ROUNDUP(offset, 8);
1538 /* priexpo */
1539 if ((rv = soft_unpack_obj_attribute(buf, &priexpo,
1540 NULL, &offset, B_FALSE)) != CKR_OK)
1541 goto pri_cleanup;
1543 copy_bigint_attr(&priexpo, KEY_PRI_RSA_PRIEXPO(pvk));
1545 buf += ROUNDUP(offset, 8);
1547 /* prime1 */
1548 if ((rv = soft_unpack_obj_attribute(buf, &prime1,
1549 NULL, &offset, B_FALSE)) != CKR_OK)
1550 goto pri_cleanup;
1552 copy_bigint_attr(&prime1, KEY_PRI_RSA_PRIME1(pvk));
1554 buf += ROUNDUP(offset, 8);
1556 /* prime2 */
1557 if ((rv = soft_unpack_obj_attribute(buf, &prime2,
1558 NULL, &offset, B_FALSE)) != CKR_OK)
1559 goto pri_cleanup;
1561 copy_bigint_attr(&prime2, KEY_PRI_RSA_PRIME2(pvk));
1563 buf += ROUNDUP(offset, 8);
1565 /* expo1 */
1566 if ((rv = soft_unpack_obj_attribute(buf, &expo1,
1567 NULL, &offset, B_FALSE)) != CKR_OK)
1568 goto pri_cleanup;
1570 copy_bigint_attr(&expo1, KEY_PRI_RSA_EXPO1(pvk));
1572 buf += ROUNDUP(offset, 8);
1574 /* expo2 */
1575 if ((rv = soft_unpack_obj_attribute(buf, &expo2,
1576 NULL, &offset, B_FALSE)) != CKR_OK)
1577 goto pri_cleanup;
1579 copy_bigint_attr(&expo2, KEY_PRI_RSA_EXPO2(pvk));
1581 buf += ROUNDUP(offset, 8);
1583 /* coef */
1584 if ((rv = soft_unpack_obj_attribute(buf, &coef,
1585 NULL, &offset, B_FALSE)) != CKR_OK)
1586 goto pri_cleanup;
1588 copy_bigint_attr(&coef, KEY_PRI_RSA_COEF(pvk));
1590 break;
1592 case CKK_DSA:
1593 /* prime */
1594 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1595 NULL, &offset, B_FALSE)) != CKR_OK)
1596 goto pri_cleanup;
1598 copy_bigint_attr(&prime, KEY_PRI_DSA_PRIME(pvk));
1600 buf += ROUNDUP(offset, 8);
1602 /* subprime */
1603 if ((rv = soft_unpack_obj_attribute(buf, &subprime,
1604 NULL, &offset, B_FALSE)) != CKR_OK)
1605 goto pri_cleanup;
1607 copy_bigint_attr(&subprime, KEY_PRI_DSA_SUBPRIME(pvk));
1609 buf += ROUNDUP(offset, 8);
1611 /* base */
1612 if ((rv = soft_unpack_obj_attribute(buf, &base,
1613 NULL, &offset, B_FALSE)) != CKR_OK)
1614 goto pri_cleanup;
1616 copy_bigint_attr(&base, KEY_PRI_DSA_BASE(pvk));
1618 buf += ROUNDUP(offset, 8);
1620 /* value */
1621 if ((rv = soft_unpack_obj_attribute(buf, &value,
1622 NULL, &offset, B_FALSE)) != CKR_OK)
1623 goto pri_cleanup;
1625 copy_bigint_attr(&value, KEY_PRI_DSA_VALUE(pvk));
1627 break;
1629 case CKK_DH:
1630 /* value_bits */
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);
1635 /* prime */
1636 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1637 NULL, &offset, B_FALSE)) != CKR_OK)
1638 goto pri_cleanup;
1640 copy_bigint_attr(&prime, KEY_PRI_DH_PRIME(pvk));
1642 buf += ROUNDUP(offset, 8);
1644 /* base */
1645 if ((rv = soft_unpack_obj_attribute(buf, &base,
1646 NULL, &offset, B_FALSE)) != CKR_OK)
1647 goto pri_cleanup;
1649 copy_bigint_attr(&base, KEY_PRI_DH_BASE(pvk));
1651 buf += ROUNDUP(offset, 8);
1653 /* value */
1654 if ((rv = soft_unpack_obj_attribute(buf, &value,
1655 NULL, &offset, B_FALSE)) != CKR_OK)
1656 goto pri_cleanup;
1658 copy_bigint_attr(&value, KEY_PRI_DH_VALUE(pvk));
1660 break;
1662 case CKK_EC:
1663 /* value */
1664 if ((rv = soft_unpack_obj_attribute(buf, &value,
1665 NULL, &offset, B_FALSE)) != CKR_OK)
1666 goto pri_cleanup;
1668 copy_bigint_attr(&value, KEY_PRI_EC_VALUE(pvk));
1669 break;
1671 case CKK_X9_42_DH:
1672 /* prime */
1673 if ((rv = soft_unpack_obj_attribute(buf, &prime,
1674 NULL, &offset, B_FALSE)) != CKR_OK)
1675 goto pri_cleanup;
1677 copy_bigint_attr(&prime, KEY_PRI_DH942_PRIME(pvk));
1679 buf += ROUNDUP(offset, 8);
1681 /* base */
1682 if ((rv = soft_unpack_obj_attribute(buf, &base,
1683 NULL, &offset, B_FALSE)) != CKR_OK)
1684 goto pri_cleanup;
1686 copy_bigint_attr(&base, KEY_PRI_DH942_BASE(pvk));
1688 buf += ROUNDUP(offset, 8);
1690 /* subprime */
1691 if ((rv = soft_unpack_obj_attribute(buf, &subprime,
1692 NULL, &offset, B_FALSE)) != CKR_OK)
1693 goto pri_cleanup;
1695 copy_bigint_attr(&subprime, KEY_PRI_DH942_BASE(pvk));
1697 buf += ROUNDUP(offset, 8);
1699 /* value */
1700 if ((rv = soft_unpack_obj_attribute(buf, &value,
1701 NULL, &offset, B_FALSE)) != CKR_OK)
1702 goto pri_cleanup;
1704 copy_bigint_attr(&value, KEY_PRI_DH942_VALUE(pvk));
1706 break;
1707 } /* keytype */
1709 break;
1711 case CKO_SECRET_KEY:
1712 /* Allocate storage for Secret Key Object. */
1713 sck = calloc(1, sizeof (secret_key_obj_t));
1714 if (sck == NULL) {
1715 return (CKR_HOST_MEMORY);
1718 objp->object_class_u.secret_key = sck;
1720 /* value */
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) {
1728 free(sck);
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);
1737 return (rv);
1739 case CKO_CERTIFICATE:
1740 /* Allocate storage for Certificate Object. */
1741 cert = calloc(1, sizeof (certificate_obj_t));
1742 if (cert == NULL) {
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;
1750 switch (certtype) {
1751 case CKC_X_509:
1752 /* subject */
1753 if ((rv = soft_unpack_obj_attribute(buf, NULL,
1754 &cert->cert_type_u.x509.subject,
1755 &offset, B_TRUE)) != CKR_OK) {
1756 free(cert);
1757 return (rv);
1760 buf += ROUNDUP(offset, 8);
1762 /* value */
1763 if ((rv = soft_unpack_obj_attribute(buf, NULL,
1764 &cert->cert_type_u.x509.value,
1765 &offset, B_TRUE)) != CKR_OK) {
1766 free(cert);
1767 return (rv);
1770 break;
1772 case CKC_X_509_ATTR_CERT:
1773 /* owner */
1774 if ((rv = soft_unpack_obj_attribute(buf, NULL,
1775 &cert->cert_type_u.x509_attr.owner,
1776 &offset, B_TRUE)) != CKR_OK) {
1777 free(cert);
1778 return (rv);
1781 buf += ROUNDUP(offset, 8);
1783 /* value */
1784 if ((rv = soft_unpack_obj_attribute(buf, NULL,
1785 &cert->cert_type_u.x509_attr.value,
1786 &offset, B_TRUE)) != CKR_OK) {
1787 free(cert);
1788 return (rv);
1791 break;
1794 return (rv);
1796 case CKO_DOMAIN_PARAMETERS:
1798 break;
1801 pub_cleanup:
1803 * cleanup the storage allocated to the local variables.
1805 if (rv != CKR_OK)
1806 free(pbk);
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);
1813 return (rv);
1815 pri_cleanup:
1817 * cleanup the storage allocated to the local variables.
1819 if (rv != CKR_OK)
1820 free(pvk);
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);
1833 return (rv);
1838 * Store the token object to a keystore file.
1840 CK_RV
1841 soft_put_object_to_keystore(soft_object_t *objp)
1844 uchar_t *buf;
1845 size_t len;
1846 CK_RV rv;
1848 rv = soft_keystore_pack_obj(objp, &buf, &len);
1849 if (rv != CKR_OK)
1850 return (rv);
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);
1857 free(buf);
1858 return (CKR_FUNCTION_FAILED);
1860 } else {
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);
1864 free(buf);
1865 return (CKR_FUNCTION_FAILED);
1868 (void) pthread_mutex_unlock(&soft_slot.slot_mutex);
1869 free(buf);
1870 return (CKR_OK);
1875 * Modify the in-core token object and then write it to
1876 * a keystore file.
1878 CK_RV
1879 soft_modify_object_to_keystore(soft_object_t *objp)
1882 uchar_t *buf;
1883 size_t len;
1884 CK_RV rv;
1886 rv = soft_keystore_pack_obj(objp, &buf, &len);
1887 if (rv != CKR_OK)
1888 return (rv);
1890 /* B_TRUE: caller has held a writelock on the keystore */
1891 if (soft_keystore_modify_obj(&objp->ks_handle, buf, len,
1892 B_TRUE) < 0) {
1893 return (CKR_FUNCTION_FAILED);
1896 free(buf);
1897 return (CKR_OK);
1902 * Read the token object from the keystore file.
1904 CK_RV
1905 soft_get_token_objects_from_keystore(ks_search_type_t type)
1907 CK_RV rv;
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);
1913 if (rv != CKR_OK) {
1914 return (rv);
1917 while (ks_obj) {
1919 new_objp = calloc(1, sizeof (soft_object_t));
1920 if (new_objp == NULL) {
1921 rv = CKR_HOST_MEMORY;
1922 goto cleanup;
1924 /* Convert the keystore format to memory format */
1925 rv = soft_keystore_unpack_obj(new_objp, ks_obj);
1926 if (rv != CKR_OK) {
1927 if (new_objp->class == CKO_CERTIFICATE)
1928 soft_cleanup_cert_object(new_objp);
1929 else
1930 soft_cleanup_object(new_objp);
1931 goto cleanup;
1934 soft_add_token_object_to_slot(new_objp);
1936 /* Free the ks_obj list */
1937 ks_obj_next = ks_obj->next;
1938 free(ks_obj->buf);
1939 free(ks_obj);
1940 ks_obj = ks_obj_next;
1943 return (CKR_OK);
1945 cleanup:
1946 while (ks_obj) {
1947 ks_obj_next = ks_obj->next;
1948 free(ks_obj->buf);
1949 free(ks_obj);
1950 ks_obj = ks_obj_next;
1952 return (rv);
1956 * soft_gen_crypt_key()
1958 * Arguments:
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
1969 * Description:
1971 * Generate an encryption key of the input PIN.
1973 * Returns:
1975 * CKR_OK: no error
1976 * Other: some error occurred while generating the encryption key
1979 CK_RV
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];
1984 int attrs = 0;
1985 CK_RV rv;
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;
1996 if (pPIN == NULL)
1997 return (CKR_FUNCTION_FAILED);
1999 tmpl[attrs].type = CKA_CLASS;
2000 tmpl[attrs].pValue = &class;
2001 tmpl[attrs].ulValueLen = sizeof (class);
2002 attrs++;
2004 tmpl[attrs].type = CKA_KEY_TYPE;
2005 tmpl[attrs].pValue = &keytype;
2006 tmpl[attrs].ulValueLen = sizeof (keytype);
2007 attrs++;
2009 tmpl[attrs].type = CKA_ENCRYPT;
2010 tmpl[attrs].pValue = &truevalue;
2011 tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
2012 attrs++;
2014 tmpl[attrs].type = CKA_DECRYPT;
2015 tmpl[attrs].pValue = &truevalue;
2016 tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
2017 attrs++;
2019 tmpl[attrs].type = CKA_VALUE_LEN;
2020 tmpl[attrs].pValue = &keylen;
2021 tmpl[attrs].ulValueLen = sizeof (keylen);
2022 attrs++;
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);
2031 } else {
2032 bzero(salt, sizeof (salt));
2033 (void) memcpy(salt, *saltdata, PBKD2_SALT_SIZE);
2036 Mechanism.mechanism = CKM_PKCS5_PBKD2;
2037 Mechanism.pParameter = &params;
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);
2054 if (rv != CKR_OK) {
2055 return (rv);
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,
2063 B_FALSE, B_FALSE);
2064 return (CKR_HOST_MEMORY);
2067 rv = soft_generate_pkcs5_pbkdf2_key(&token_session, &Mechanism,
2068 secret_key);
2070 if (rv != CKR_OK)
2071 soft_delete_object(&token_session, secret_key,
2072 B_FALSE, B_FALSE);
2073 else
2074 *key = secret_key;
2076 return (rv);
2081 * soft_gen_hmac_key()
2083 * Arguments:
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
2094 * Description:
2096 * Generate a hmac key of the input PIN.
2098 * Returns:
2100 * CKR_OK: no error
2101 * Other: some error occurred while generating the hmac key
2104 CK_RV
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];
2109 int attrs = 0;
2110 CK_RV rv;
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;
2121 if (pPIN == NULL)
2122 return (CKR_FUNCTION_FAILED);
2124 tmpl[attrs].type = CKA_CLASS;
2125 tmpl[attrs].pValue = &class;
2126 tmpl[attrs].ulValueLen = sizeof (class);
2127 attrs++;
2129 tmpl[attrs].type = CKA_KEY_TYPE;
2130 tmpl[attrs].pValue = &keytype;
2131 tmpl[attrs].ulValueLen = sizeof (keytype);
2132 attrs++;
2134 tmpl[attrs].type = CKA_SIGN;
2135 tmpl[attrs].pValue = &truevalue;
2136 tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
2137 attrs++;
2139 tmpl[attrs].type = CKA_VERIFY;
2140 tmpl[attrs].pValue = &truevalue;
2141 tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
2142 attrs++;
2144 tmpl[attrs].type = CKA_VALUE_LEN;
2145 tmpl[attrs].pValue = &keylen;
2146 tmpl[attrs].ulValueLen = sizeof (keylen);
2147 attrs++;
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);
2156 } else {
2157 bzero(salt, sizeof (salt));
2158 (void) memcpy(salt, *saltdata, PBKD2_SALT_SIZE);
2161 Mechanism.mechanism = CKM_PKCS5_PBKD2;
2162 Mechanism.pParameter = &params;
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);
2179 if (rv != CKR_OK) {
2180 return (rv);
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,
2188 B_FALSE, B_FALSE);
2189 return (CKR_HOST_MEMORY);
2192 rv = soft_generate_pkcs5_pbkdf2_key(&token_session, &Mechanism,
2193 secret_key);
2195 if (rv != CKR_OK)
2196 soft_delete_object(&token_session, secret_key,
2197 B_FALSE, B_FALSE);
2198 else
2199 *key = secret_key;
2201 return (rv);
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
2209 * token object.
2211 CK_RV
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);
2234 return (CKR_OK);
2238 void
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.
2251 CK_RV
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)
2255 CK_MECHANISM mech;
2256 soft_aes_ctx_t *soft_aes_ctx;
2257 CK_RV rv;
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
2263 * or decryption.
2265 if (out == NULL) {
2266 mech.mechanism = CKM_AES_CBC_PAD;
2267 mech.pParameter = (void *)ivec;
2268 mech.ulParameterLen = AES_BLOCK_LEN;
2270 if (encrypt)
2271 rv = soft_aes_crypt_init_common(&token_session, &mech,
2272 key_p, B_TRUE);
2273 else
2274 rv = soft_aes_crypt_init_common(&token_session, &mech,
2275 key_p, B_FALSE);
2277 if (rv != CKR_OK)
2278 return (rv);
2281 (void) pthread_mutex_lock(&token_session.session_mutex);
2283 if (encrypt)
2284 soft_aes_ctx =
2285 (soft_aes_ctx_t *)token_session.encrypt.context;
2286 else
2287 soft_aes_ctx =
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);
2302 if (encrypt) {
2303 free(token_session.encrypt.context);
2304 token_session.encrypt.context = NULL;
2305 } else {
2306 free(token_session.encrypt.context);
2307 token_session.encrypt.context = NULL;
2310 (void) pthread_mutex_unlock(&token_session.
2311 session_mutex);
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.
2320 if (encrypt) {
2321 rv = soft_aes_encrypt_common(&token_session, in,
2322 in_len, out, out_len, B_FALSE);
2323 } else {
2324 rv = soft_aes_decrypt_common(&token_session, in,
2325 in_len, out, out_len, B_FALSE);
2328 } else {
2330 * The caller has allocated the output buffer, so that we
2331 * are doing the real encryption/decryption this time.
2333 tmplen = *out_len;
2334 if (encrypt) {
2335 rv = soft_aes_encrypt_common(&token_session, in,
2336 in_len, out, &tmplen, B_TRUE);
2337 if (rv == CKR_OK) {
2338 tmplen1 = *out_len - tmplen;
2339 rv = soft_encrypt_final(&token_session,
2340 out+tmplen, &tmplen1);
2341 *out_len = tmplen + tmplen1;
2343 } else {
2344 rv = soft_aes_decrypt_common(&token_session, in,
2345 in_len, out, &tmplen, B_TRUE);
2346 if (rv == CKR_OK) {
2347 tmplen1 = *out_len - tmplen;
2348 rv = soft_decrypt_final(&token_session,
2349 out+tmplen, &tmplen1);
2350 *out_len = tmplen + tmplen1;
2355 return (rv);
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.
2364 CK_RV
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)
2368 CK_MECHANISM mech;
2369 CK_RV rv;
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,
2376 key_p, sign);
2378 if (rv != CKR_OK)
2379 return (rv);
2381 if (sign) {
2382 rv = soft_sign(&token_session, in, in_len, out, out_len);
2383 } else {
2384 rv = soft_verify(&token_session, in, in_len, out, *out_len);
2387 return (rv);