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.
31 #include <sys/types.h>
33 #include "metaGlobal.h"
35 /* Size of the template for creating key used for wrap/unwrap */
36 #define WRAP_KEY_TEMPLATE_SIZE 7
39 * Information necessary to create keys for C_WrapKey/C_UnwrapKey
41 typedef struct _wrap_info
{
42 CK_OBJECT_CLASS
class; /* class of the key for wrap/unwrap */
43 CK_KEY_TYPE key_type
; /* key type of key for wrap/unwrap */
44 CK_ULONG key_length
; /* length of key */
45 CK_MECHANISM_TYPE mech_type
; /* mech used for wrap/unwrap */
46 CK_ULONG iv_length
; /* length of iv for mech */
48 boolean_t src_supports
;
49 boolean_t dst_supports
;
52 extern pthread_rwlock_t meta_sessionlist_lock
;
53 extern meta_session_t
*meta_sessionlist_head
;
55 static wrap_info_t common_wrap_info
[] = {
56 {CKO_SECRET_KEY
, CKK_AES
, 16, CKM_AES_CBC_PAD
, 16, B_FALSE
, B_FALSE
},
57 {CKO_SECRET_KEY
, CKK_DES3
, 24, CKM_DES3_CBC_PAD
, 8, B_FALSE
, B_FALSE
},
58 {CKO_SECRET_KEY
, CKK_DES
, 8, CKM_DES_CBC_PAD
, 8, B_FALSE
, B_FALSE
},
61 static unsigned int num_common_wrap_info
=
62 sizeof (common_wrap_info
) / sizeof (wrap_info_t
);
64 static wrap_info_t special_wrap_info
[] = {
65 {CKO_SECRET_KEY
, CKK_SKIPJACK
, 12, CKM_SKIPJACK_WRAP
, 0,
67 {CKO_SECRET_KEY
, CKK_BATON
, 40, CKM_BATON_WRAP
, 0,
69 {CKO_SECRET_KEY
, CKK_JUNIPER
, 40, CKM_JUNIPER_WRAP
, 0,
72 static unsigned int num_special_wrap_info
=
73 sizeof (special_wrap_info
) / sizeof (wrap_info_t
);
75 static wrap_info_t rsa_wrap_info
[] = {
76 {CKO_PUBLIC_KEY
, CKK_RSA
, 0, CKM_RSA_PKCS
, 0,
78 {CKO_PUBLIC_KEY
, CKK_RSA
, 0, CKM_RSA_X_509
, 0,
81 static unsigned int num_rsa_wrap_info
=
82 sizeof (rsa_wrap_info
) / sizeof (wrap_info_t
);
85 static pthread_rwlock_t meta_objectclose_lock
;
86 static pthread_rwlock_t tokenobject_list_lock
;
87 static meta_object_t
*tokenobject_list_head
;
89 CK_BBOOL falsevalue
= FALSE
;
90 CK_BBOOL truevalue
= TRUE
;
93 * Public and private exponent, and Module value for
94 * creating the RSA public/private key.
97 static CK_BYTE PubExpo
[3] = {0x01, 0x00, 0x01};
98 CK_BYTE PriExpo
[128] = {
99 0x8e, 0xc9, 0x70, 0x57, 0x6b, 0xcd, 0xfb, 0xa9,
100 0x19, 0xad, 0xcd, 0x91, 0x69, 0xd5, 0x52, 0xec,
101 0x72, 0x1e, 0x45, 0x15, 0x06, 0xdc, 0x65, 0x2d,
102 0x98, 0xc4, 0xce, 0x33, 0x54, 0x15, 0x70, 0x8d,
103 0xfa, 0x65, 0xea, 0x53, 0x44, 0xf3, 0x3e, 0x3f,
104 0xb4, 0x4c, 0x60, 0xd5, 0x01, 0x2d, 0xa4, 0x12,
105 0x99, 0xbf, 0x3f, 0x0b, 0xcd, 0xbb, 0x24, 0x10,
106 0x60, 0x30, 0x5e, 0x58, 0xf8, 0x59, 0xaa, 0xd1,
107 0x63, 0x3b, 0xbc, 0xcb, 0x94, 0x58, 0x38, 0x24,
108 0xfc, 0x65, 0x25, 0xc5, 0xa6, 0x51, 0xa2, 0x2e,
109 0xf1, 0x5e, 0xf5, 0xc1, 0xf5, 0x46, 0xf7, 0xbd,
110 0xc7, 0x62, 0xa8, 0xe2, 0x27, 0xd6, 0x94, 0x5b,
111 0xd3, 0xa2, 0xb5, 0x76, 0x42, 0x67, 0x6b, 0x86,
112 0x91, 0x97, 0x4d, 0x07, 0x92, 0x00, 0x4a, 0xdf,
113 0x0b, 0x65, 0x64, 0x05, 0x03, 0x48, 0x27, 0xeb,
114 0xce, 0x9a, 0x49, 0x7f, 0x3e, 0x10, 0xe0, 0x01};
116 static CK_BYTE Modulus
[128] = {
117 0x94, 0x32, 0xb9, 0x12, 0x1d, 0x68, 0x2c, 0xda,
118 0x2b, 0xe0, 0xe4, 0x97, 0x1b, 0x4d, 0xdc, 0x43,
119 0xdf, 0x38, 0x6e, 0x7b, 0x9f, 0x07, 0x58, 0xae,
120 0x9d, 0x82, 0x1e, 0xc7, 0xbc, 0x92, 0xbf, 0xd3,
121 0xce, 0x00, 0xbb, 0x91, 0xc9, 0x79, 0x06, 0x03,
122 0x1f, 0xbc, 0x9f, 0x94, 0x75, 0x29, 0x5f, 0xd7,
123 0xc5, 0xf3, 0x73, 0x8a, 0xa4, 0x35, 0x43, 0x7a,
124 0x00, 0x32, 0x97, 0x3e, 0x86, 0xef, 0x70, 0x6f,
125 0x18, 0x56, 0x15, 0xaa, 0x6a, 0x87, 0xe7, 0x8d,
126 0x7d, 0xdd, 0x1f, 0xa4, 0xe4, 0x31, 0xd4, 0x7a,
127 0x8c, 0x0e, 0x20, 0xd2, 0x23, 0xf5, 0x57, 0x3c,
128 0x1b, 0xa8, 0x44, 0xa4, 0x57, 0x8f, 0x33, 0x52,
129 0xad, 0x83, 0xae, 0x4a, 0x97, 0xa6, 0x1e, 0xa6,
130 0x2b, 0xfa, 0xea, 0xeb, 0x6e, 0x71, 0xb8, 0xb6,
131 0x0a, 0x36, 0xed, 0x83, 0xce, 0xb0, 0xdf, 0xc1,
132 0xd4, 0x3a, 0xe9, 0x99, 0x6f, 0xf3, 0x96, 0xb7};
135 meta_clone_template_setup(meta_object_t
*object
,
136 const generic_attr_t
*attributes
, size_t num_attributes
);
139 * meta_objectManager_initialize
141 * Called from meta_Initialize. Initializes all the variables used
142 * by the object manager.
145 meta_objectManager_initialize()
147 if (pthread_rwlock_init(&meta_objectclose_lock
, NULL
) != 0) {
148 return (CKR_FUNCTION_FAILED
);
151 if (pthread_rwlock_init(&tokenobject_list_lock
, NULL
) != 0) {
152 (void) pthread_rwlock_destroy(&meta_objectclose_lock
);
153 return (CKR_FUNCTION_FAILED
);
156 tokenobject_list_head
= NULL
;
162 meta_objectManager_finalize()
165 * If there are still any token object in the list, need to
166 * deactivate all of them.
168 (void) meta_token_object_deactivate(ALL_TOKEN
);
170 (void) pthread_rwlock_destroy(&meta_objectclose_lock
);
171 (void) pthread_rwlock_destroy(&tokenobject_list_lock
);
179 * Convert a CK_OBJECT_HANDLE to the corresponding metaobject. If
180 * successful, a reader-lock on the object will be held to indicate
181 * that it's in use. Call OBJRELEASE() when finished.
185 meta_handle2object(CK_OBJECT_HANDLE hObject
, meta_object_t
**object
)
187 meta_object_t
*tmp_object
= (meta_object_t
*)(hObject
);
189 /* Check for bad args (eg CK_INVALID_HANDLE, which is 0/NULL). */
190 if (tmp_object
== NULL
) {
192 return (CKR_OBJECT_HANDLE_INVALID
);
196 /* Lock to ensure the magic-check + read-lock is atomic. */
197 (void) pthread_rwlock_rdlock(&meta_objectclose_lock
);
199 if (tmp_object
->magic_marker
!= METASLOT_OBJECT_MAGIC
) {
200 (void) pthread_rwlock_unlock(&meta_objectclose_lock
);
202 return (CKR_OBJECT_HANDLE_INVALID
);
204 (void) pthread_rwlock_rdlock(&tmp_object
->object_lock
);
205 (void) pthread_rwlock_unlock(&meta_objectclose_lock
);
207 *object
= tmp_object
;
215 * Creates a new metaobject, but does not yet add it to the object list.
216 * Once the caller has finished initializing the object (by setting
217 * object attributes), meta_object_add should be called. This two-step
218 * process prevents others from seeing the object until fully intitialized.
222 meta_object_alloc(meta_session_t
*session
, meta_object_t
**object
)
224 meta_object_t
*new_object
;
227 /* Allocate memory for the object. */
228 new_object
= calloc(1, sizeof (meta_object_t
));
229 if (new_object
== NULL
)
230 return (CKR_HOST_MEMORY
);
232 num_slots
= meta_slotManager_get_slotcount();
234 new_object
->clones
= calloc(num_slots
, sizeof (slot_object_t
*));
235 if (new_object
->clones
== NULL
) {
237 return (CKR_HOST_MEMORY
);
240 new_object
->tried_create_clone
= calloc(num_slots
, sizeof (boolean_t
));
241 if (new_object
->tried_create_clone
== NULL
) {
242 free(new_object
->clones
);
244 return (CKR_HOST_MEMORY
);
247 /* Initialize the object fields. */
248 new_object
->magic_marker
= METASLOT_OBJECT_MAGIC
;
249 (void) pthread_rwlock_init(&new_object
->object_lock
, NULL
);
250 (void) pthread_rwlock_init(&new_object
->attribute_lock
, NULL
);
251 (void) pthread_mutex_init(&new_object
->clone_create_lock
, NULL
);
252 (void) pthread_mutex_init(&new_object
->isClosingObject_lock
, NULL
);
253 new_object
->creator_session
= session
;
255 *object
= new_object
;
262 * meta_object_get_attr
264 * Get attribute values to fill in attribute values
265 * being kept in the metaslot object. The following 4 attributes
266 * in the meta_object_t structure will be filled in:
267 * isToken, isPrivate, isSensitive, isExtractable
269 * It's basically an easy way to do a C_GetAttributeValue.
270 * So, the hSession argument is assumed
271 * to be valid, and the pointer to meta_object_t is also assumed
275 meta_object_get_attr(slot_session_t
*slot_session
, CK_OBJECT_HANDLE hObject
,
276 meta_object_t
*object
)
278 CK_BBOOL is_sensitive
= object
->isSensitive
;
279 CK_BBOOL is_extractable
= object
->isExtractable
;
280 CK_BBOOL is_token
= B_FALSE
, is_private
= B_FALSE
;
282 CK_OBJECT_CLASS
class;
283 CK_ATTRIBUTE attrs
[3];
285 CK_SESSION_HANDLE hSession
= slot_session
->hSession
;
286 CK_SLOT_ID fw_st_id
= slot_session
->fw_st_id
;
289 attrs
[0].type
= CKA_CLASS
;
290 attrs
[0].pValue
= &class;
291 attrs
[0].ulValueLen
= sizeof (class);
293 if (object
->isFreeObject
!= FREE_ENABLED
) {
294 attrs
[1].type
= CKA_TOKEN
;
295 attrs
[1].pValue
= &is_token
;
296 attrs
[1].ulValueLen
= sizeof (is_token
);
301 * If this is a freeobject, we already know the Private value
302 * and we don't want to overwrite it with the wrong value
304 if (object
->isFreeObject
<= FREE_DISABLED
) {
305 attrs
[count
].type
= CKA_PRIVATE
;
306 attrs
[count
].pValue
= &is_private
;
307 attrs
[count
].ulValueLen
= sizeof (is_private
);
310 is_private
= object
->isPrivate
;
312 rv
= FUNCLIST(fw_st_id
)->C_GetAttributeValue(hSession
, hObject
,
320 case CKO_PRIVATE_KEY
:
322 /* Only need to check these for private & secret keys */
323 attrs
[0].type
= CKA_EXTRACTABLE
;
324 attrs
[0].pValue
= &is_extractable
;
325 attrs
[0].ulValueLen
= sizeof (is_extractable
);
329 * If this is a freeobject, we already know the Sensitive
330 * value and we don't want to overwrite it with the wrong
334 if (object
->isFreeObject
<= FREE_DISABLED
) {
335 attrs
[1].type
= CKA_SENSITIVE
;
336 attrs
[1].pValue
= &is_sensitive
;
337 attrs
[1].ulValueLen
= sizeof (is_sensitive
);
341 * We only need the key type if this is the first
342 * time we've looked at the object
344 if (object
->isFreeObject
== FREE_UNCHECKED
) {
345 attrs
[2].type
= CKA_KEY_TYPE
;
346 attrs
[2].pValue
= &keytype
;
347 attrs
[2].ulValueLen
= sizeof (keytype
);
355 if (object
->isFreeObject
== FREE_UNCHECKED
) {
356 attrs
[count
].type
= CKA_KEY_TYPE
;
357 attrs
[count
].pValue
= &keytype
;
358 attrs
[count
].ulValueLen
= sizeof (keytype
);
361 is_sensitive
= CK_FALSE
;
362 is_extractable
= CK_TRUE
;
366 object
->isFreeObject
= FREE_DISABLED
;
367 is_sensitive
= CK_FALSE
;
368 is_extractable
= CK_TRUE
;
372 rv
= FUNCLIST(fw_st_id
)->C_GetAttributeValue(hSession
, hObject
,
378 if (object
->isFreeObject
== FREE_UNCHECKED
) {
379 if (keytype
== CKK_EC
|| keytype
== CKK_RSA
||
381 if (metaslot_config
.auto_key_migrate
) {
382 object
->isFreeObject
= FREE_DISABLED
;
383 object
->isFreeToken
= FREE_DISABLED
;
386 object
->isFreeObject
= FREE_ENABLED
;
388 object
->isFreeToken
= FREE_ENABLED
;
390 object
->isFreeObject
= FREE_DISABLED
;
396 object
->isToken
= is_token
;
397 object
->isPrivate
= is_private
;
398 object
->isSensitive
= is_sensitive
;
399 object
->isExtractable
= is_extractable
;
406 * meta_object_activate
408 * Add a new metaobject to the list of objects. See also meta_object_create,
409 * which would be called to create an object before it is added.
412 meta_object_activate(meta_object_t
*new_object
)
414 pthread_rwlock_t
*list_lock
;
415 meta_object_t
**list_head
;
418 * For session objects, we keep the list in the session that created
419 * this object, because this object will be destroyed when that session
422 * For token objects, the list is global (ie, not associated with any
423 * particular session).
425 if (new_object
->isToken
) {
426 list_lock
= &tokenobject_list_lock
;
427 list_head
= &tokenobject_list_head
;
429 list_lock
= &new_object
->creator_session
->object_list_lock
;
430 list_head
= &new_object
->creator_session
->object_list_head
;
433 /* Add object to the list of objects. */
434 (void) pthread_rwlock_wrlock(list_lock
);
435 INSERT_INTO_LIST(*list_head
, new_object
);
436 (void) pthread_rwlock_unlock(list_lock
);
441 * meta_object_deactivate
443 * Removes the object from the list of valid meta objects. Note
444 * that this function does not clean up any allocated
445 * resources (memory, object clones, etc). Cleaning up of
446 * allocated resources is done by calling the meta_object_dealloc()
450 meta_object_deactivate(meta_object_t
*object
, boolean_t have_list_lock
,
451 boolean_t have_object_lock
)
453 pthread_rwlock_t
*list_lock
;
454 meta_object_t
**list_head
;
456 if (!have_object_lock
) {
457 (void) pthread_rwlock_rdlock(&object
->object_lock
);
460 (void) pthread_mutex_lock(&object
->isClosingObject_lock
);
461 if (object
->isClosingObject
) {
462 /* Lost a delete race. */
463 (void) pthread_mutex_unlock(&object
->isClosingObject_lock
);
465 return (CKR_OBJECT_HANDLE_INVALID
);
467 object
->isClosingObject
= B_TRUE
;
468 (void) pthread_mutex_unlock(&object
->isClosingObject_lock
);
470 if (object
->isToken
|| (object
->isFreeToken
== FREE_ENABLED
)) {
471 list_lock
= &tokenobject_list_lock
;
472 list_head
= &tokenobject_list_head
;
474 list_lock
= &object
->creator_session
->object_list_lock
;
475 list_head
= &object
->creator_session
->object_list_head
;
479 * Remove object from the object list. Once removed, it will not
480 * be possible for another thread to begin using the object.
482 (void) pthread_rwlock_wrlock(&meta_objectclose_lock
);
483 if (!have_list_lock
) {
484 (void) pthread_rwlock_wrlock(list_lock
);
488 object
->magic_marker
= METASLOT_OBJECT_BADMAGIC
;
490 * Can't use the regular REMOVE_FROM_LIST() function because
491 * that will miss the "error cleanup" situation where object is not yet
492 * in the list (object->next == NULL && object->prev == NULL)
494 if (*list_head
== object
) {
495 /* Object is the first one in the list */
497 *list_head
= object
->next
;
498 object
->next
->prev
= NULL
;
500 /* Object is the only one in the list */
503 } else if (object
->next
!= NULL
|| object
->prev
!= NULL
) {
505 object
->prev
->next
= object
->next
;
506 object
->next
->prev
= object
->prev
;
508 /* Object is the last one in the list */
509 object
->prev
->next
= NULL
;
513 if (!have_list_lock
) {
514 (void) pthread_rwlock_unlock(list_lock
);
516 (void) pthread_rwlock_unlock(&meta_objectclose_lock
);
519 * Wait for anyone already using object to finish, by obtaining
520 * a writer-lock (need to release our reader-lock first). Once we
521 * get the write lock, we can just release it and finish cleaning
524 (void) pthread_rwlock_unlock(&object
->object_lock
); /* rdlock */
525 (void) pthread_rwlock_wrlock(&object
->object_lock
);
526 (void) pthread_rwlock_unlock(&object
->object_lock
); /* wrlock */
534 * meta_object_dealloc
536 * Performs final object cleanup, releasing any allocated memory and
537 * destroying any clones on other slots. Caller is assumed to have
538 * called meta_object_deactivate() before this function.
540 * Caller is assumed to have only reference to object, but should have
543 * If "nukeSourceObj" argument is true, we will actually delete the
544 * object from the underlying slot.
547 meta_object_dealloc(meta_session_t
*session
, meta_object_t
*object
,
548 boolean_t nukeSourceObj
)
550 CK_RV rv
, save_rv
= CKR_OK
;
551 CK_ULONG slotnum
, num_slots
;
554 /* First, delete all the clones of this object on other slots. */
555 num_slots
= meta_slotManager_get_slotcount();
556 for (slotnum
= 0; slotnum
< num_slots
; slotnum
++) {
557 slot_session_t
*obj_session
;
558 slot_object_t
*clone
;
560 clone
= object
->clones
[slotnum
];
563 if (nukeSourceObj
|| (!object
->isToken
&&
564 !(object
->isFreeToken
== FREE_ENABLED
&&
565 get_keystore_slotnum() == slotnum
))) {
567 rv
= meta_get_slot_session(slotnum
, &obj_session
,
569 object
->creator_session
->session_flags
:
570 session
->session_flags
);
573 rv
= FUNCLIST(obj_session
->fw_st_id
)->\
574 C_DestroyObject(obj_session
->hSession
,
577 meta_release_slot_session(obj_session
);
578 if ((rv
!= CKR_OK
) && (save_rv
== CKR_OK
)) {
585 meta_slot_object_deactivate(clone
);
586 meta_slot_object_dealloc(clone
);
588 object
->clones
[slotnum
] = NULL
;
591 /* Now erase and delete any attributes in the metaobject. */
592 dealloc_attributes(object
->attributes
, object
->num_attributes
);
594 free(object
->clones
);
595 free(object
->tried_create_clone
);
597 if (object
->clone_template
) {
598 for (i
= 0; i
< object
->clone_template_size
; i
++) {
599 free(((object
->clone_template
)[i
]).pValue
);
601 free(object
->clone_template
);
604 /* Cleanup remaining object fields. */
605 (void) pthread_rwlock_destroy(&object
->object_lock
);
606 (void) pthread_rwlock_destroy(&object
->attribute_lock
);
607 (void) pthread_mutex_destroy(&object
->isClosingObject_lock
);
608 (void) pthread_mutex_destroy(&object
->clone_create_lock
);
610 meta_object_delay_free(object
);
617 * meta_slot_object_alloc
620 meta_slot_object_alloc(slot_object_t
**object
) {
621 slot_object_t
*new_object
;
623 new_object
= calloc(1, sizeof (slot_object_t
));
624 if (new_object
== NULL
)
625 return (CKR_HOST_MEMORY
);
627 *object
= new_object
;
633 * meta_slot_object_activate
636 meta_slot_object_activate(slot_object_t
*object
,
637 slot_session_t
*creator_session
, boolean_t isToken
)
639 object
->creator_session
= creator_session
;
642 extern slot_data_t
*slots
;
645 slot
= &(slots
[object
->creator_session
->slotnum
]);
647 (void) pthread_rwlock_wrlock(&slot
->tokenobject_list_lock
);
648 INSERT_INTO_LIST(slot
->tokenobject_list_head
, object
);
649 (void) pthread_rwlock_unlock(&slot
->tokenobject_list_lock
);
651 slot_session_t
*session
= object
->creator_session
;
653 /* Add to session's list of session objects. */
654 (void) pthread_rwlock_wrlock(&session
->object_list_lock
);
655 INSERT_INTO_LIST(session
->object_list_head
, object
);
656 (void) pthread_rwlock_unlock(&session
->object_list_lock
);
660 * This set tells the slot object that we are in the token list,
661 * but does not cause harm with the metaobject knowing the object
662 * isn't a token, but a freetoken
665 object
->isToken
= isToken
;
670 * meta_slot_object_deactivate
672 * Remove the specified slot object from the appropriate object list.
675 meta_slot_object_deactivate(slot_object_t
*object
)
677 slot_object_t
**list_head
;
678 pthread_rwlock_t
*list_lock
;
680 if (object
->isToken
) {
681 extern slot_data_t
*slots
;
684 slot
= &(slots
[object
->creator_session
->slotnum
]);
686 list_head
= &slot
->tokenobject_list_head
;
687 list_lock
= &slot
->tokenobject_list_lock
;
689 list_head
= &object
->creator_session
->object_list_head
;
690 list_lock
= &object
->creator_session
->object_list_lock
;
693 (void) pthread_rwlock_wrlock(list_lock
);
694 REMOVE_FROM_LIST(*list_head
, object
);
695 (void) pthread_rwlock_unlock(list_lock
);
700 * meta_slot_object_dealloc
703 meta_slot_object_dealloc(slot_object_t
*object
)
705 /* Not much cleanup for slot objects, unlike meta objects... */
713 * When a key is generated/derived/unwrapped, the attribute values
714 * created by the token are not immediately read into our copy of the
715 * attributes. We defer this work until we actually need to know.
718 meta_object_copyin(meta_object_t
*object
)
721 slot_session_t
*session
= NULL
;
722 CK_ATTRIBUTE
*attrs
= NULL
, *attrs_with_val
= NULL
;
723 slot_object_t
*slot_object
= NULL
;
724 CK_ULONG num_attrs
= 0, i
, num_attrs_with_val
;
725 CK_SESSION_HANDLE hSession
;
728 /* Make sure no one else is looking at attributes. */
729 (void) pthread_rwlock_wrlock(&object
->attribute_lock
);
731 /* Did we just lose a copyin race with another thread */
732 if (object
->attributes
!= NULL
) {
736 slot_object
= object
->clones
[object
->master_clone_slotnum
];
738 rv
= meta_get_slot_session(object
->master_clone_slotnum
, &session
,
739 object
->creator_session
->session_flags
);
745 * first, get the master template of all the attributes
748 rv
= get_master_attributes_by_object(session
, slot_object
,
749 &(object
->attributes
), &(object
->num_attributes
));
755 * Get value for each attribute items.
757 * Some attributes are required by the given object type.
758 * Some are optional. Get all the values first, and then
759 * make sure we have value for all required values,
761 attrs
= calloc(object
->num_attributes
, sizeof (CK_ATTRIBUTE
));
763 rv
= CKR_HOST_MEMORY
;
768 for (i
= 0; i
< object
->num_attributes
; i
++) {
770 ((object
->attributes
[i
]).attribute
).type
;
772 num_attrs
= object
->num_attributes
;
774 hSession
= session
->hSession
;
775 fw_st_id
= session
->fw_st_id
;
777 /* first, call C_GetAttributeValue() to get size for each attribute */
778 rv
= FUNCLIST(fw_st_id
)->C_GetAttributeValue(hSession
,
779 slot_object
->hObject
, attrs
, num_attrs
);
781 * If the return value is not CKR_OK, allow it to be
782 * CKR_ATTRIBUTE_TYPE_INVALID for now.
783 * Some attributes defined in PKCS#11 version 2.11
784 * might not be defined in earlier versions. We will
785 * TRY to work with those providers if the attribute
788 if ((rv
!= CKR_OK
) && (rv
!= CKR_ATTRIBUTE_TYPE_INVALID
)) {
789 rv
= CKR_FUNCTION_FAILED
; /* make sure rv is appropriate */
795 * Since we don't know how many attributes have
796 * values at this time, just assume all of them
797 * have values so we save one loop to count the number
798 * of attributes that have value.
800 attrs_with_val
= calloc(num_attrs
, sizeof (CK_ATTRIBUTE
));
801 if (attrs_with_val
== NULL
) {
802 rv
= CKR_HOST_MEMORY
;
807 num_attrs_with_val
= 0;
808 for (i
= 0; i
< num_attrs
; i
++) {
809 if (!(((CK_LONG
)(attrs
[i
].ulValueLen
)) > 0)) {
810 /* if it isn't an optional attr, len should be > 0 */
811 if (!object
->attributes
[i
].canBeEmptyValue
) {
812 rv
= CKR_FUNCTION_FAILED
;
816 attrs_with_val
[num_attrs_with_val
].type
= attrs
[i
].type
;
817 attrs_with_val
[num_attrs_with_val
].ulValueLen
=
819 attrs_with_val
[num_attrs_with_val
].pValue
=
820 malloc(attrs
[i
].ulValueLen
);
821 if (attrs_with_val
[num_attrs_with_val
].pValue
== NULL
) {
822 rv
= CKR_HOST_MEMORY
;
825 num_attrs_with_val
++;
829 rv
= FUNCLIST(fw_st_id
)->C_GetAttributeValue(hSession
,
830 slot_object
->hObject
, attrs_with_val
, num_attrs_with_val
);
835 /* store these values into the meta object */
836 for (i
= 0; i
< num_attrs_with_val
; i
++) {
837 rv
= attribute_set_value(&(attrs_with_val
[i
]),
838 object
->attributes
, object
->num_attributes
);
845 (void) pthread_rwlock_unlock(&object
->attribute_lock
);
848 meta_release_slot_session(session
);
851 for (i
= 0; i
< num_attrs
; i
++) {
852 if (attrs
[i
].pValue
!= NULL
) {
853 free(attrs
[i
].pValue
);
859 if (attrs_with_val
) {
860 for (i
= 0; i
< num_attrs
; i
++) {
861 if (attrs_with_val
[i
].pValue
!= NULL
) {
862 free(attrs_with_val
[i
].pValue
);
865 free(attrs_with_val
);
871 * Create an object to be used for wrapping and unwrapping.
872 * The same template will be used for all wrapping/unwrapping keys all
877 create_wrap_unwrap_key(slot_session_t
*slot_session
, CK_OBJECT_HANDLE
*hObject
,
878 wrap_info_t
*wrap_info
, char *key_data
, CK_ULONG key_len
)
881 CK_OBJECT_CLASS objclass
;
885 CK_ATTRIBUTE
template[WRAP_KEY_TEMPLATE_SIZE
];
888 objclass
= wrap_info
->class;
889 template[i
].type
= CKA_CLASS
;
890 template[i
].pValue
= &objclass
;
891 template[i
].ulValueLen
= sizeof (objclass
);
894 keytype
= wrap_info
->key_type
;
895 template[i
].type
= CKA_KEY_TYPE
;
896 template[i
].pValue
= &keytype
;
897 template[i
].ulValueLen
= sizeof (keytype
);
900 template[i
].type
= CKA_TOKEN
;
901 template[i
].pValue
= &falsevalue
;
902 template[i
].ulValueLen
= sizeof (falsevalue
);
905 if (objclass
== CKO_SECRET_KEY
) {
907 template[i
].type
= CKA_VALUE
;
908 template[i
].pValue
= key_data
;
909 template[i
].ulValueLen
= key_len
;
912 template[i
].type
= CKA_WRAP
;
913 template[i
].pValue
= &truevalue
;
914 template[i
].ulValueLen
= sizeof (truevalue
);
917 template[i
].type
= CKA_UNWRAP
;
918 template[i
].pValue
= &truevalue
;
919 template[i
].ulValueLen
= sizeof (truevalue
);
921 /* Modulus is the same for rsa public and private key */
923 template[i
].type
= CKA_MODULUS
;
924 template[i
].pValue
= Modulus
;
925 template[i
].ulValueLen
= sizeof (Modulus
);
927 if (objclass
== CKO_PUBLIC_KEY
) {
930 template[i
].type
= CKA_PUBLIC_EXPONENT
;
931 template[i
].pValue
= PubExpo
;
932 template[i
].ulValueLen
= sizeof (PubExpo
);
935 template[i
].type
= CKA_WRAP
;
936 template[i
].pValue
= &truevalue
;
937 template[i
].ulValueLen
= sizeof (truevalue
);
939 /* RSA private key */
941 template[i
].type
= CKA_PRIVATE_EXPONENT
;
942 template[i
].pValue
= PriExpo
;
943 template[i
].ulValueLen
= sizeof (PriExpo
);
946 template[i
].type
= CKA_UNWRAP
;
947 template[i
].pValue
= &truevalue
;
948 template[i
].ulValueLen
= sizeof (truevalue
);
952 rv
= FUNCLIST(slot_session
->fw_st_id
)->C_CreateObject(
953 slot_session
->hSession
, template, i
+ 1, hObject
);
960 * Create a clone of a non-sensitive and extractable object.
961 * If the template required for creating the clone doesn't exist,
962 * it will be retrieved from the master clone.
965 clone_by_create(meta_object_t
*object
, slot_object_t
*new_clone
,
966 slot_session_t
*dst_slot_session
)
969 int free_token_index
= -1;
971 if (object
->attributes
== NULL
) {
972 rv
= meta_object_copyin(object
);
978 if (object
->clone_template
== NULL
) {
979 rv
= meta_clone_template_setup(object
, object
->attributes
,
980 object
->num_attributes
);
986 if (object
->isFreeToken
== FREE_ENABLED
) {
987 if (dst_slot_session
->slotnum
== get_keystore_slotnum())
988 free_token_index
= set_template_boolean(CKA_TOKEN
,
989 object
->clone_template
,
990 object
->clone_template_size
, B_FALSE
, &truevalue
);
992 free_token_index
= set_template_boolean(CKA_TOKEN
,
993 object
->clone_template
,
994 object
->clone_template_size
, B_FALSE
, &falsevalue
);
997 /* Create the clone... */
998 rv
= FUNCLIST(dst_slot_session
->fw_st_id
)->C_CreateObject(
999 dst_slot_session
->hSession
, object
->clone_template
,
1000 object
->clone_template_size
, &(new_clone
->hObject
));
1002 if (free_token_index
!= -1) {
1003 free_token_index
= set_template_boolean(CKA_TOKEN
,
1004 object
->clone_template
, object
->clone_template_size
,
1005 B_FALSE
, &falsevalue
);
1016 * Goes through the list of wraping mechanisms, and returns the first
1017 * one that is supported by both the source and the destination slot.
1018 * If none of the mechanisms are supported by both slot, return the
1019 * first mechanism that's supported by the source slot
1022 find_best_match_wrap_mech(wrap_info_t
*wrap_info
, int num_info
,
1023 CK_ULONG src_slotnum
, CK_ULONG dst_slotnum
, int *first_both_mech
,
1024 int *first_src_mech
)
1028 boolean_t src_supports
, dst_supports
;
1030 CK_MECHANISM_INFO mech_info
;
1032 mech_info
.flags
= CKF_WRAP
;
1034 for (i
= 0; i
< num_info
; i
++) {
1035 src_supports
= B_FALSE
;
1036 dst_supports
= B_FALSE
;
1038 rv
= meta_mechManager_slot_supports_mech(
1039 (wrap_info
[i
]).mech_type
, src_slotnum
,
1040 &src_supports
, NULL
, B_FALSE
, &mech_info
);
1045 rv
= meta_mechManager_slot_supports_mech(
1046 (wrap_info
[i
]).mech_type
, dst_slotnum
,
1047 &dst_supports
, NULL
, B_FALSE
, &mech_info
);
1052 /* both source and destination supports the mech */
1053 if ((src_supports
) && (dst_supports
)) {
1054 *first_both_mech
= i
;
1058 if ((src_supports
) && (*first_src_mech
== -1)) {
1059 *first_src_mech
= i
;
1066 * Determine the wrapping/unwrapping mechanism to be used
1068 * If possible, select a mechanism that's supported by both source
1069 * and destination slot. If none of the mechanisms are supported
1070 * by both slot, then, select the first one supported by
1075 get_wrap_mechanism(CK_OBJECT_CLASS obj_class
, CK_KEY_TYPE key_type
,
1076 CK_ULONG src_slotnum
, CK_ULONG dst_slotnum
, wrap_info_t
*wrap_info
)
1078 wrap_info_t
*wrap_info_to_search
= NULL
;
1079 unsigned int num_wrap_info
;
1082 boolean_t src_supports
= B_FALSE
, dst_supports
= B_FALSE
;
1083 int first_src_mech
, rsa_first_src_mech
, first_both_mech
;
1084 CK_MECHANISM_INFO mech_info
;
1086 mech_info
.flags
= CKF_WRAP
;
1088 if ((obj_class
== CKO_PRIVATE_KEY
) && (key_type
== CKK_KEA
)) {
1090 * only SKIPJACK keys can be used for wrapping
1094 for (i
= 0; i
< num_special_wrap_info
; i
++) {
1095 if ((special_wrap_info
[i
]).mech_type
1096 != CKM_SKIPJACK_WRAP
) {
1100 src_supports
= B_FALSE
;
1101 dst_supports
= B_FALSE
;
1103 rv
= meta_mechManager_slot_supports_mech(
1104 (special_wrap_info
[i
]).mech_type
, src_slotnum
,
1105 &src_supports
, NULL
, B_FALSE
, &mech_info
);
1110 rv
= meta_mechManager_slot_supports_mech(
1111 (special_wrap_info
[i
]).mech_type
, dst_slotnum
,
1112 &dst_supports
, NULL
, B_FALSE
, &mech_info
);
1119 * both src and dst supports the mech or
1120 * only the src supports the mech
1122 (void) memcpy(wrap_info
,
1123 &(special_wrap_info
[i
]),
1124 sizeof (wrap_info_t
));
1126 wrap_info
->src_supports
= src_supports
;
1127 wrap_info
->dst_supports
= dst_supports
;
1135 * if we are here, that means neither the source slot
1136 * nor the destination slots supports CKM_SKIPJACK_WRAP.
1138 rv
= CKR_FUNCTION_FAILED
;
1142 if ((key_type
== CKK_SKIPJACK
) || (key_type
== CKK_BATON
) ||
1143 (key_type
== CKK_JUNIPER
)) {
1144 /* special key types */
1145 wrap_info_to_search
= special_wrap_info
;
1146 num_wrap_info
= num_special_wrap_info
;
1148 /* use the regular wrapping mechanisms */
1149 wrap_info_to_search
= common_wrap_info
;
1150 num_wrap_info
= num_common_wrap_info
;
1153 first_both_mech
= -1;
1154 first_src_mech
= -1;
1156 rv
= find_best_match_wrap_mech(wrap_info_to_search
, num_wrap_info
,
1157 src_slotnum
, dst_slotnum
, &first_both_mech
, &first_src_mech
);
1162 if (first_both_mech
!= -1) {
1163 (void) memcpy(wrap_info
,
1164 &(wrap_info_to_search
[first_both_mech
]),
1165 sizeof (wrap_info_t
));
1167 wrap_info
->src_supports
= B_TRUE
;
1168 wrap_info
->dst_supports
= B_TRUE
;
1174 * If we are here, we did not find a mechanism that's supported
1175 * by both source and destination slot.
1177 * If it is a secret key, can also try to wrap it with
1180 if (obj_class
== CKO_SECRET_KEY
) {
1181 first_both_mech
= -1;
1182 rsa_first_src_mech
= -1;
1184 rv
= find_best_match_wrap_mech(rsa_wrap_info
,
1185 num_rsa_wrap_info
, src_slotnum
, dst_slotnum
,
1186 &first_both_mech
, &rsa_first_src_mech
);
1192 if (first_both_mech
> -1) {
1193 (void) memcpy(wrap_info
,
1194 &(rsa_wrap_info
[first_both_mech
]),
1195 sizeof (wrap_info_t
));
1197 wrap_info
->src_supports
= B_TRUE
;
1198 wrap_info
->dst_supports
= B_TRUE
;
1205 * if we are here, that means none of the mechanisms are supported
1206 * by both the source and the destination
1208 if (first_src_mech
> -1) {
1209 /* source slot support one of the secret key mechs */
1210 (void) memcpy(wrap_info
,
1211 &(wrap_info_to_search
[first_src_mech
]),
1212 sizeof (wrap_info_t
));
1213 wrap_info
->src_supports
= B_TRUE
;
1214 wrap_info
->dst_supports
= B_FALSE
;
1216 } else if (rsa_first_src_mech
> -1) {
1217 /* source slot support one of the RSA mechs */
1218 (void) memcpy(wrap_info
, &(rsa_wrap_info
[rsa_first_src_mech
]),
1219 sizeof (wrap_info_t
));
1221 wrap_info
->src_supports
= B_TRUE
;
1222 wrap_info
->dst_supports
= B_FALSE
;
1225 /* neither source nor destination support any wrap mechs */
1226 rv
= CKR_FUNCTION_FAILED
;
1235 * This is called if the object to be cloned is a sensitive object
1238 clone_by_wrap(meta_object_t
*object
, slot_object_t
*new_clone
,
1239 slot_session_t
*dst_slot_session
)
1241 slot_session_t
*src_slot_session
= NULL
;
1242 CK_OBJECT_HANDLE wrappingKey
= 0, unwrappingKey
= 0;
1243 CK_MECHANISM wrappingMech
;
1244 CK_BYTE
*wrappedKey
= NULL
;
1245 CK_ULONG wrappedKeyLen
= 0;
1246 slot_object_t
*slot_object
= NULL
;
1248 CK_OBJECT_HANDLE unwrapped_obj
;
1249 meta_object_t
*tmp_meta_obj
= NULL
;
1250 slot_object_t
*tmp_slot_obj
= NULL
;
1251 CK_OBJECT_CLASS obj_class
;
1252 CK_KEY_TYPE key_type
;
1253 meta_session_t
*tmp_meta_session
= NULL
;
1254 CK_ATTRIBUTE unwrap_template
[4];
1255 char key_data
[1024]; /* should be big enough for any key size */
1256 char ivbuf
[1024]; /* should be big enough for any mech */
1257 wrap_info_t wrap_info
;
1258 CK_ULONG key_len
, unwrap_template_size
;
1260 slot_object
= object
->clones
[object
->master_clone_slotnum
];
1262 rv
= meta_get_slot_session(object
->master_clone_slotnum
,
1263 &src_slot_session
, object
->creator_session
->session_flags
);
1269 * get the object class and key type for unwrap template
1270 * This information will also be used for determining
1271 * which wrap mechanism and which key to use for
1272 * doing the wrapping
1274 unwrap_template
[0].type
= CKA_CLASS
;
1275 unwrap_template
[0].pValue
= &obj_class
;
1276 unwrap_template
[0].ulValueLen
= sizeof (obj_class
);
1278 unwrap_template
[1].type
= CKA_KEY_TYPE
;
1279 unwrap_template
[1].pValue
= &key_type
;
1280 unwrap_template
[1].ulValueLen
= sizeof (key_type
);
1282 rv
= FUNCLIST(src_slot_session
->fw_st_id
)->C_GetAttributeValue(
1283 src_slot_session
->hSession
, slot_object
->hObject
,
1284 unwrap_template
, 2);
1289 rv
= get_wrap_mechanism(obj_class
, key_type
, src_slot_session
->slotnum
,
1290 dst_slot_session
->slotnum
, &wrap_info
);
1296 * read number of bytes required from random device for
1297 * creating a secret key for wrapping and unwrapping
1299 if (wrap_info
.class == CKO_SECRET_KEY
) {
1302 * /dev/urandom will be used for generating the key used
1303 * for doing the wrap/unwrap. It's should be ok to
1304 * use /dev/urandom because this key is used for this
1305 * one time operation only. It doesn't need to be stored.
1307 key_len
= wrap_info
.key_length
;
1308 if (pkcs11_get_urandom(key_data
, key_len
) < 0) {
1309 rv
= CKR_FUNCTION_FAILED
;
1313 if (wrap_info
.iv_length
> 0) {
1314 if (pkcs11_get_urandom(
1315 ivbuf
, wrap_info
.iv_length
) < 0) {
1316 rv
= CKR_FUNCTION_FAILED
;
1322 /* create the wrapping key */
1323 rv
= create_wrap_unwrap_key(src_slot_session
, &wrappingKey
,
1324 &wrap_info
, key_data
, key_len
);
1329 wrappingMech
.mechanism
= wrap_info
.mech_type
;
1330 wrappingMech
.pParameter
= ((wrap_info
.iv_length
> 0) ? ivbuf
: NULL
);
1331 wrappingMech
.ulParameterLen
= wrap_info
.iv_length
;
1333 /* get the size of the wrapped key */
1334 rv
= FUNCLIST(src_slot_session
->fw_st_id
)->C_WrapKey(
1335 src_slot_session
->hSession
, &wrappingMech
,
1336 wrappingKey
, slot_object
->hObject
, NULL
, &wrappedKeyLen
);
1342 wrappedKey
= malloc(wrappedKeyLen
* sizeof (CK_BYTE
));
1343 if (wrappedKey
== NULL
) {
1344 rv
= CKR_HOST_MEMORY
;
1348 /* do the actual key wrapping */
1349 rv
= FUNCLIST(src_slot_session
->fw_st_id
)->C_WrapKey(
1350 src_slot_session
->hSession
, &wrappingMech
,
1351 wrappingKey
, slot_object
->hObject
, wrappedKey
, &wrappedKeyLen
);
1357 /* explicitly force the unwrapped object to be not sensitive */
1358 unwrap_template
[2].type
= CKA_SENSITIVE
;
1359 unwrap_template
[2].pValue
= &falsevalue
;
1360 unwrap_template
[2].ulValueLen
= sizeof (falsevalue
);
1362 unwrap_template
[3].type
= CKA_TOKEN
;
1363 unwrap_template
[3].pValue
= &falsevalue
;
1364 unwrap_template
[3].ulValueLen
= sizeof (falsevalue
);
1366 unwrap_template_size
=
1367 sizeof (unwrap_template
) / sizeof (CK_ATTRIBUTE
);
1369 if (!wrap_info
.dst_supports
) {
1371 * if we know for sure that the destination slot doesn't
1372 * support the wrapping mechanism, no point in trying.
1373 * go directly to unwrap in source slot, and create key
1376 goto unwrap_in_source
;
1379 /* create the unwrapping key in destination slot */
1380 if (wrap_info
.key_type
== CKK_RSA
) {
1381 /* for RSA key, the unwrapping key need to be private key */
1382 wrap_info
.class = CKO_PRIVATE_KEY
;
1384 rv
= create_wrap_unwrap_key(dst_slot_session
,
1385 &unwrappingKey
, &wrap_info
, key_data
, key_len
);
1390 rv
= FUNCLIST(dst_slot_session
->fw_st_id
)->C_UnwrapKey(
1391 dst_slot_session
->hSession
, &wrappingMech
,
1392 unwrappingKey
, wrappedKey
, wrappedKeyLen
, unwrap_template
,
1393 unwrap_template_size
, &(new_clone
->hObject
));
1399 * There seemed to be a problem with unwrapping in the
1401 * Try to do the unwrap in the src slot so it becomes
1402 * a non-sensitive object, then, get all the attributes
1403 * and create the object in the destination slot
1407 if (wrap_info
.class == CKO_SECRET_KEY
) {
1408 /* unwrap with same key used for wrapping */
1409 rv
= FUNCLIST(src_slot_session
->fw_st_id
)->C_UnwrapKey(
1410 src_slot_session
->hSession
,
1411 &wrappingMech
, wrappingKey
, wrappedKey
,
1412 wrappedKeyLen
, unwrap_template
,
1413 unwrap_template_size
, &(unwrapped_obj
));
1416 * If the object is wrapping with RSA public key, need
1417 * need to create RSA private key for unwrapping
1419 wrap_info
.class = CKO_PRIVATE_KEY
;
1420 rv
= create_wrap_unwrap_key(src_slot_session
,
1421 &unwrappingKey
, &wrap_info
, key_data
, key_len
);
1425 rv
= FUNCLIST(src_slot_session
->fw_st_id
)->C_UnwrapKey(
1426 src_slot_session
->hSession
,
1427 &wrappingMech
, unwrappingKey
, wrappedKey
,
1428 wrappedKeyLen
, unwrap_template
,
1429 unwrap_template_size
, &(unwrapped_obj
));
1437 rv
= meta_session_alloc(&tmp_meta_session
);
1442 tmp_meta_session
->session_flags
= CKF_SERIAL_SESSION
;
1444 rv
= meta_object_alloc(tmp_meta_session
, &tmp_meta_obj
);
1449 rv
= meta_slot_object_alloc(&tmp_slot_obj
);
1454 tmp_meta_obj
->master_clone_slotnum
= src_slot_session
->slotnum
;
1455 tmp_slot_obj
->hObject
= unwrapped_obj
;
1456 tmp_meta_obj
->clones
[tmp_meta_obj
->master_clone_slotnum
]
1458 meta_slot_object_activate(tmp_slot_obj
, src_slot_session
,
1460 tmp_slot_obj
= NULL
;
1462 rv
= clone_by_create(tmp_meta_obj
, new_clone
,
1470 if (unwrappingKey
) {
1471 (void) FUNCLIST(dst_slot_session
->fw_st_id
)->C_DestroyObject(
1472 dst_slot_session
->hSession
, unwrappingKey
);
1476 (void) FUNCLIST(src_slot_session
->fw_st_id
)->C_DestroyObject(
1477 src_slot_session
->hSession
, wrappingKey
);
1481 (void) meta_slot_object_dealloc(tmp_slot_obj
);
1485 (void) meta_object_dealloc(tmp_meta_session
, tmp_meta_obj
,
1489 if (tmp_meta_session
) {
1490 (void) meta_session_dealloc(tmp_meta_session
);
1497 if (src_slot_session
) {
1498 meta_release_slot_session(src_slot_session
);
1507 * meta_object_get_clone
1509 * Creates a "clone" of a metaobject on the specified slot. A clone is a
1510 * copy of the object.
1512 * Clones are cached, so that they can be reused with subsequent operations.
1515 meta_object_get_clone(meta_object_t
*object
,
1516 CK_ULONG slot_num
, slot_session_t
*slot_session
,
1517 slot_object_t
**clone
)
1520 slot_object_t
*newclone
= NULL
;
1522 /* Does a clone already exist? */
1523 if (object
->clones
[slot_num
] != NULL
) {
1524 *clone
= object
->clones
[slot_num
];
1528 if ((object
->isSensitive
) && (object
->isToken
) &&
1529 (!metaslot_auto_key_migrate
)) {
1531 * if the object is a sensitive token object, and auto
1532 * key migrate is not allowed, will not create the clone
1535 return (CKR_FUNCTION_FAILED
);
1538 /* object attributes can't be extracted and attributes are not known */
1539 if ((!object
->isExtractable
) && (object
->attributes
== NULL
)) {
1540 return (CKR_FUNCTION_FAILED
);
1543 (void) pthread_mutex_lock(&object
->clone_create_lock
);
1545 /* Maybe someone just created one? */
1546 if (object
->clones
[slot_num
] != NULL
) {
1547 *clone
= object
->clones
[slot_num
];
1552 * has an attempt already been made to create this object in
1553 * slot? If yes, and there's no clone, as indicated above,
1554 * that means this object can't be created in this slot.
1556 if (object
->tried_create_clone
[slot_num
]) {
1557 (void) pthread_mutex_unlock(&object
->clone_create_lock
);
1558 return (CKR_FUNCTION_FAILED
);
1561 rv
= meta_slot_object_alloc(&newclone
);
1565 object
->tried_create_clone
[slot_num
] = B_TRUE
;
1568 * If this object is sensitive and we do not have not copied in the
1569 * attributes via FreeObject functionality, then we need to wrap it off
1570 * the provider. If we do have attributes, we can just create the
1574 if (object
->isSensitive
&& object
->attributes
== NULL
) {
1575 rv
= clone_by_wrap(object
, newclone
, slot_session
);
1577 rv
= clone_by_create(object
, newclone
, slot_session
);
1584 object
->clones
[slot_num
] = newclone
;
1585 meta_slot_object_activate(newclone
, slot_session
, object
->isToken
);
1590 (void) pthread_mutex_unlock(&object
->clone_create_lock
);
1593 meta_slot_object_dealloc(newclone
);
1600 * meta_setup_clone_template
1602 * Create a clone template for the specified object.
1605 meta_clone_template_setup(meta_object_t
*object
,
1606 const generic_attr_t
*attributes
, size_t num_attributes
)
1609 CK_ATTRIBUTE
*clone_template
;
1612 clone_template
= malloc(num_attributes
* sizeof (CK_ATTRIBUTE
));
1613 if (clone_template
== NULL
) {
1614 rv
= CKR_HOST_MEMORY
;
1618 /* Don't allow attributes to change while we look at them. */
1619 (void) pthread_rwlock_rdlock(&object
->attribute_lock
);
1621 for (i
= 0; i
< num_attributes
; i
++) {
1622 if (!attributes
[i
].isCloneAttr
||
1623 (attributes
[i
].attribute
.type
== CKA_TOKEN
&&
1624 object
->isFreeToken
== FREE_DISABLED
)) {
1627 if ((!(attributes
[i
].hasValueForClone
)) &&
1628 (attributes
[i
].canBeEmptyValue
)) {
1632 clone_template
[c
].type
= attributes
[i
].attribute
.type
;
1633 clone_template
[c
].ulValueLen
=
1634 attributes
[i
].attribute
.ulValueLen
;
1635 /* Allocate space to store the attribute value. */
1636 clone_template
[c
].pValue
= malloc(clone_template
[c
].ulValueLen
);
1637 if (clone_template
[c
].pValue
== NULL
) {
1638 free(clone_template
);
1639 rv
= CKR_HOST_MEMORY
;
1640 (void) pthread_rwlock_unlock(&object
->attribute_lock
);
1644 (void) memcpy(clone_template
[c
].pValue
,
1645 object
->attributes
[i
].attribute
.pValue
,
1646 clone_template
[c
].ulValueLen
);
1650 (void) pthread_rwlock_unlock(&object
->attribute_lock
);
1652 object
->clone_template
= clone_template
;
1653 object
->clone_template_size
= c
;
1661 * meta_object_find_by_handle
1663 * Search for an existing metaobject, using the object handle of a clone
1664 * on a particular slot.
1666 * Returns a matching metaobject, or NULL if no match was found.
1669 meta_object_find_by_handle(CK_OBJECT_HANDLE hObject
, CK_ULONG slotnum
,
1670 boolean_t token_only
)
1672 meta_object_t
*object
= NULL
, *tmp_obj
;
1673 meta_session_t
*session
;
1676 (void) pthread_rwlock_rdlock(&meta_sessionlist_lock
);
1677 session
= meta_sessionlist_head
;
1678 while (session
!= NULL
) {
1679 /* lock the objects list while we look at it */
1680 (void) pthread_rwlock_rdlock(
1681 &(session
->object_list_lock
));
1682 tmp_obj
= session
->object_list_head
;
1683 while (tmp_obj
!= NULL
) {
1684 slot_object_t
*slot_object
;
1686 (void) pthread_rwlock_rdlock(
1687 &(tmp_obj
->object_lock
));
1688 slot_object
= tmp_obj
->clones
[slotnum
];
1689 if (slot_object
!= NULL
) {
1690 if (slot_object
->hObject
== hObject
) {
1694 (void) pthread_rwlock_unlock(
1695 &(tmp_obj
->object_lock
));
1696 if (object
!= NULL
) {
1699 tmp_obj
= tmp_obj
->next
;
1701 (void) pthread_rwlock_unlock(
1702 &(session
->object_list_lock
));
1703 if (object
!= NULL
) {
1706 session
= session
->next
;
1708 (void) pthread_rwlock_unlock(&meta_sessionlist_lock
);
1711 if (object
!= NULL
) {
1712 /* found the object, no need to look further */
1717 * Look at list of token objects
1719 (void) pthread_rwlock_rdlock(&tokenobject_list_lock
);
1720 tmp_obj
= tokenobject_list_head
;
1722 while (tmp_obj
!= NULL
) {
1723 slot_object_t
*slot_object
;
1725 (void) pthread_rwlock_rdlock(&(tmp_obj
->object_lock
));
1726 slot_object
= tmp_obj
->clones
[slotnum
];
1727 if (slot_object
!= NULL
) {
1728 if (slot_object
->hObject
== hObject
)
1731 (void) pthread_rwlock_unlock(&(tmp_obj
->object_lock
));
1732 if (object
!= NULL
) {
1735 tmp_obj
= tmp_obj
->next
;
1737 (void) pthread_rwlock_unlock(&tokenobject_list_lock
);
1743 meta_token_object_deactivate(token_obj_type_t token_type
)
1745 meta_object_t
*object
, *tmp_object
;
1746 CK_RV save_rv
= CKR_OK
, rv
;
1748 /* get a write lock on the token object list */
1749 (void) pthread_rwlock_wrlock(&tokenobject_list_lock
);
1751 object
= tokenobject_list_head
;
1753 /* go through each object and delete the one with matching type */
1754 while (object
!= NULL
) {
1755 tmp_object
= object
->next
;
1757 if ((token_type
== ALL_TOKEN
) ||
1758 ((object
->isPrivate
) && (token_type
== PRIVATE_TOKEN
)) ||
1759 ((!object
->isPrivate
) && (token_type
== PUBLIC_TOKEN
))) {
1760 rv
= meta_object_deactivate(object
, B_TRUE
, B_FALSE
);
1761 if ((rv
!= CKR_OK
) && (save_rv
== CKR_OK
)) {
1765 rv
= meta_object_dealloc(NULL
, object
, B_FALSE
);
1766 if ((rv
!= CKR_OK
) && (save_rv
== CKR_OK
)) {
1771 object
= tmp_object
;
1774 (void) pthread_rwlock_unlock(&tokenobject_list_lock
);
1779 * This function adds the to-be-freed meta object to a linked list.
1780 * When the number of objects queued in the linked list reaches the
1781 * maximum threshold MAX_OBJ_TO_BE_FREED, it will free the first
1782 * object (FIFO) in the list.
1785 meta_object_delay_free(meta_object_t
*objp
)
1789 (void) pthread_mutex_lock(&obj_delay_freed
.obj_to_be_free_mutex
);
1791 /* Add the newly deleted object at the end of the list */
1793 if (obj_delay_freed
.first
== NULL
) {
1794 obj_delay_freed
.last
= objp
;
1795 obj_delay_freed
.first
= objp
;
1797 obj_delay_freed
.last
->next
= objp
;
1798 obj_delay_freed
.last
= objp
;
1801 if (++obj_delay_freed
.count
>= MAX_OBJ_TO_BE_FREED
) {
1803 * Free the first object in the list only if
1804 * the total count reaches maximum threshold.
1806 obj_delay_freed
.count
--;
1807 tmp
= obj_delay_freed
.first
->next
;
1808 free(obj_delay_freed
.first
);
1809 obj_delay_freed
.first
= tmp
;
1811 (void) pthread_mutex_unlock(&obj_delay_freed
.obj_to_be_free_mutex
);
1816 * This function checks if the object passed can be a freeobject.
1818 * If there is more than one provider that supports the supported freeobject
1819 * mechanisms then allow freeobjects to be an option.
1823 meta_freeobject_check(meta_session_t
*session
, meta_object_t
*object
,
1824 CK_MECHANISM
*pMech
, CK_ATTRIBUTE
*tmpl
, CK_ULONG tmpl_len
,
1825 CK_KEY_TYPE keytype
)
1827 mech_support_info_t
*info
= &(session
->mech_support_info
);
1830 * If key migration is turned off, or the object does not has any of
1831 * the required flags and there is only one slot, then we don't need
1834 if (!metaslot_auto_key_migrate
||
1835 (!object
->isToken
&& !object
->isSensitive
&&
1836 meta_slotManager_get_slotcount() < 2))
1840 * If this call is for key generation, check pMech for supported
1843 if (pMech
!= NULL
) {
1844 if (pMech
->mechanism
== CKM_RSA_PKCS_KEY_PAIR_GEN
||
1845 pMech
->mechanism
== CKM_EC_KEY_PAIR_GEN
||
1846 pMech
->mechanism
== CKM_DH_PKCS_KEY_PAIR_GEN
||
1847 pMech
->mechanism
== CKM_DH_PKCS_DERIVE
)
1848 info
->mech
= pMech
->mechanism
;
1853 * If this call is for an object creation, look inside the template
1854 * for supported FreeObject mechs
1856 } else if (tmpl_len
> 0) {
1857 if (!get_template_ulong(CKA_KEY_TYPE
, tmpl
, tmpl_len
, &keytype
))
1862 info
->mech
= CKM_RSA_PKCS_KEY_PAIR_GEN
;
1865 info
->mech
= CKM_EC_KEY_PAIR_GEN
;
1868 info
->mech
= CKM_DH_PKCS_KEY_PAIR_GEN
;
1876 /* Get the slot that support this mech... */
1877 if (meta_mechManager_get_slots(info
, B_FALSE
, NULL
) != CKR_OK
)
1881 * If there is only one slot with the mech or the first slot in
1882 * the list is the keystore slot, we should bail.
1884 if (info
->num_supporting_slots
< 2 &&
1885 info
->supporting_slots
[0]->slotnum
== get_keystore_slotnum())
1888 if (object
->isToken
)
1889 object
->isFreeToken
= FREE_ALLOWED_KEY
;
1891 object
->isFreeToken
= FREE_DISABLED
;
1893 object
->isFreeObject
= FREE_ALLOWED_KEY
;
1898 object
->isFreeToken
= FREE_DISABLED
;
1899 object
->isFreeObject
= FREE_DISABLED
;
1904 * This function assumes meta_freeobject_check() has just been called and set
1905 * the isFreeObject and/or isFreeToken vars to FREE_ALLOWED_KEY.
1907 * If the template value for CKA_PRIVATE, CKA_SENSITIVE and/or CKA_TOKEN are
1908 * true, then isFreeObject is fully enabled. In addition isFreeToken is
1909 * enabled if is CKA_TOKEN true.
1911 * If create is true, we are doing a C_CreateObject operation and don't
1912 * handle CKA_PRIVATE & CKA_SENSITIVE flags, we only care about CKA_TOKEN.
1916 meta_freeobject_set(meta_object_t
*object
, CK_ATTRIBUTE
*tmpl
,
1917 CK_ULONG tmpl_len
, boolean_t create
)
1920 /* This check should never be true, if it is, it's a bug */
1921 if (object
->isFreeObject
< FREE_ALLOWED_KEY
)
1925 /* Turn off the Sensitive flag */
1926 if (object
->isSensitive
) {
1927 if (set_template_boolean(CKA_SENSITIVE
, tmpl
, tmpl_len
,
1928 B_TRUE
, &falsevalue
) == -1)
1931 object
->isFreeObject
= FREE_ENABLED
;
1934 /* Turn off the Private flag */
1935 if (object
->isPrivate
) {
1936 if (set_template_boolean(CKA_PRIVATE
, tmpl
, tmpl_len
,
1937 B_TRUE
, &falsevalue
) == -1)
1940 object
->isFreeObject
= FREE_ENABLED
;
1944 if (object
->isToken
) {
1945 object
->isToken
= B_FALSE
;
1946 object
->isFreeToken
= FREE_ENABLED
;
1947 object
->isFreeObject
= FREE_ENABLED
;
1949 object
->isFreeToken
= FREE_DISABLED
;
1952 * If isFreeObject is not in the FREE_ENABLED state yet, it can be
1953 * turned off because the object doesn't not need to be a FreeObject.
1955 if (object
->isFreeObject
== FREE_ALLOWED_KEY
)
1956 object
->isFreeObject
= FREE_DISABLED
;
1961 object
->isFreeToken
= FREE_DISABLED
;
1962 object
->isFreeObject
= FREE_DISABLED
;
1967 * This function sets the CKA_TOKEN flag on a given object template depending
1968 * if the slot being used is a keystore.
1970 * If the object is a token, but the slot is not the system keystore or has
1971 * no keystore, then set the template to token = false; otherwise it's true.
1972 * In addition we know ahead of time what the value is, so if the value is
1973 * already correct, bypass the setting function
1976 meta_freetoken_set(CK_ULONG slot_num
, CK_BBOOL
*current_value
,
1977 CK_ATTRIBUTE
*tmpl
, CK_ULONG tmpl_len
)
1980 if (slot_num
== get_keystore_slotnum()) {
1981 if (*current_value
== TRUE
)
1984 if (set_template_boolean(CKA_TOKEN
, tmpl
, tmpl_len
, B_TRUE
,
1986 return (CKR_FUNCTION_FAILED
);
1990 if (*current_value
== FALSE
)
1993 if (set_template_boolean(CKA_TOKEN
, tmpl
, tmpl_len
, B_TRUE
,
1995 return (CKR_FUNCTION_FAILED
);
1997 *current_value
= FALSE
;
2004 * Cloning function for meta_freeobject_clone() to use. This function
2005 * is streamlined because we know what the object is and this should
2006 * not be called as a generic cloner.
2010 meta_freeobject_clone_maker(meta_session_t
*session
, meta_object_t
*object
,
2014 slot_object_t
*slot_object
= NULL
;
2015 slot_session_t
*slot_session
= NULL
;
2018 rv
= meta_slot_object_alloc(&slot_object
);
2022 rv
= meta_get_slot_session(slotnum
, &slot_session
,
2023 session
->session_flags
);
2027 rv
= clone_by_create(object
, slot_object
, slot_session
);
2029 object
->clones
[slotnum
] = slot_object
;
2030 meta_slot_object_activate(slot_object
, slot_session
, B_TRUE
);
2034 meta_release_slot_session(slot_session
);
2040 * This function is called when a object is a FreeObject.
2042 * What we are given is an object that has been generated on a provider
2043 * that is not its final usage place. That maybe because:
2044 * 1) it's a token and needs to be stored in keystore.
2045 * 2) it was to be a private/sensitive object that we modified so we could know
2046 * the important attributes for cloning before we make it private/sensitive.
2050 meta_freeobject_clone(meta_session_t
*session
, meta_object_t
*object
)
2053 CK_ULONG keystore_slotnum
;
2054 CK_ATTRIBUTE attr
[2];
2055 boolean_t failover
= B_FALSE
;
2057 if (object
->attributes
== NULL
) {
2058 rv
= meta_object_copyin(object
);
2063 if (object
->isPrivate
) {
2064 CK_OBJECT_HANDLE new_clone
;
2065 CK_ULONG slotnum
= object
->master_clone_slotnum
;
2066 slot_session_t
*slot_session
;
2068 attr
[0].type
= CKA_PRIVATE
;
2069 attr
[0].pValue
= &truevalue
;
2070 attr
[0].ulValueLen
= sizeof (truevalue
);
2072 /* Set the master attribute list */
2073 rv
= attribute_set_value(attr
, object
->attributes
,
2074 object
->num_attributes
);
2076 return (CKR_FUNCTION_FAILED
);
2078 /* Get a slot session */
2079 rv
= meta_get_slot_session(slotnum
, &slot_session
,
2080 session
->session_flags
);
2084 /* Create the new CKA_PRIVATE one */
2085 rv
= FUNCLIST(slot_session
->fw_st_id
)->\
2086 C_CopyObject(slot_session
->hSession
,
2087 object
->clones
[slotnum
]->hObject
, attr
, 1, &new_clone
);
2089 if (rv
== CKR_USER_NOT_LOGGED_IN
) {
2091 * If the CopyObject fails, we may be using a provider
2092 * that has a keystore that is not the default
2093 * keystore set in metaslot or has object management
2094 * abilities. In which case we should write this
2095 * object to metaslot's keystore and let the failover.
2096 * rest of the function know we've changed providers.
2099 keystore_slotnum
= get_keystore_slotnum();
2100 if (object
->clones
[keystore_slotnum
] == NULL
) {
2101 rv
= meta_freeobject_clone_maker(session
,
2102 object
, keystore_slotnum
);
2107 object
->master_clone_slotnum
= keystore_slotnum
;
2109 } else if (rv
!= CKR_OK
) {
2110 meta_release_slot_session(slot_session
);
2113 /* Remove the old object */
2114 rv
= FUNCLIST(slot_session
->fw_st_id
)-> \
2115 C_DestroyObject(slot_session
->hSession
,
2116 object
->clones
[slotnum
]->hObject
);
2118 meta_release_slot_session(slot_session
);
2123 object
->clones
[slotnum
]->hObject
= new_clone
;
2125 object
->clones
[slotnum
] = NULL
;
2127 meta_release_slot_session(slot_session
);
2131 if (object
->isSensitive
) {
2132 slot_session_t
*slot_session
;
2133 CK_ULONG slotnum
= object
->master_clone_slotnum
;
2135 attr
[0].type
= CKA_SENSITIVE
;
2136 attr
[0].pValue
= &truevalue
;
2137 attr
[0].ulValueLen
= sizeof (truevalue
);
2138 rv
= attribute_set_value(attr
, object
->attributes
,
2139 object
->num_attributes
);
2143 rv
= meta_get_slot_session(slotnum
, &slot_session
,
2144 session
->session_flags
);
2146 rv
= FUNCLIST(slot_session
->fw_st_id
)-> \
2147 C_SetAttributeValue(slot_session
->hSession
,
2148 object
->clones
[slotnum
]->hObject
, attr
, 1);
2150 meta_release_slot_session(slot_session
);
2154 if (object
->isFreeToken
== FREE_ENABLED
|| failover
) {
2155 keystore_slotnum
= get_keystore_slotnum();
2156 if (object
->clones
[keystore_slotnum
] == NULL
) {
2157 rv
= meta_freeobject_clone_maker(session
, object
,
2162 object
->master_clone_slotnum
= keystore_slotnum
;
2164 object
->isFreeToken
= FREE_ENABLED
;
2167 object
->isFreeObject
= FREE_ENABLED
;
2171 object
->isFreeToken
= FREE_DISABLED
;
2172 object
->isFreeObject
= FREE_DISABLED
;