2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <strings.h>
2N/A#include <errno.h>
2N/A#include <security/cryptoki.h>
2N/A#include <cryptoutil.h>
2N/A
2N/A#include "kmsGlobal.h"
2N/A#include "kmsObject.h"
2N/A#include "kmsSession.h"
2N/A#include "kmsSlot.h"
2N/A#include "kmsKeystoreUtil.h"
2N/A
2N/Akms_object_t *
2N/Akms_new_object()
2N/A{
2N/A kms_object_t *obj;
2N/A
2N/A obj = calloc(1, sizeof (kms_object_t));
2N/A if (obj == NULL)
2N/A return (NULL);
2N/A
2N/A (void) pthread_cond_init(&obj->obj_free_cond, NULL);
2N/A (void) pthread_mutex_init(&obj->object_mutex, NULL);
2N/A obj->magic_marker = KMSTOKEN_OBJECT_MAGIC;
2N/A
2N/A return (obj);
2N/A}
2N/A
2N/A/*
2N/A * Add an object to the session's object list.
2N/A *
2N/A * This function will acquire the lock on the session, and release
2N/A * that lock after adding the object to the session's object list.
2N/A */
2N/Avoid
2N/Akms_add_object_to_session(kms_object_t *objp, kms_session_t *sp)
2N/A{
2N/A /* Acquire the session lock. */
2N/A (void) pthread_mutex_lock(&sp->session_mutex);
2N/A
2N/A /* Insert the new object in front of session's object list. */
2N/A if (sp->object_list == NULL) {
2N/A sp->object_list = objp;
2N/A objp->next = NULL;
2N/A objp->prev = NULL;
2N/A } else {
2N/A sp->object_list->prev = objp;
2N/A objp->next = sp->object_list;
2N/A objp->prev = NULL;
2N/A sp->object_list = objp;
2N/A }
2N/A
2N/A /* Release the session lock. */
2N/A (void) pthread_mutex_unlock(&sp->session_mutex);
2N/A}
2N/A
2N/A/*
2N/A * Clean up and release the storage allocated to the object.
2N/A *
2N/A * The function is called either with the object lock being held
2N/A * (by caller kms_delete_object()), or there is no object lock
2N/A * yet (by kms_build_XXX_object() during creating an object).
2N/A */
2N/Avoid
2N/Akms_cleanup_object(kms_object_t *objp)
2N/A{
2N/A /*
2N/A * Free the storage allocated to a secret key object.
2N/A */
2N/A if (objp->class == CKO_SECRET_KEY) {
2N/A if (OBJ_SEC(objp) != NULL && OBJ_SEC_VALUE(objp) != NULL) {
2N/A bzero(OBJ_SEC_VALUE(objp), OBJ_SEC_VALUE_LEN(objp));
2N/A free(OBJ_SEC_VALUE(objp));
2N/A OBJ_SEC_VALUE(objp) = NULL;
2N/A OBJ_SEC_VALUE_LEN(objp) = 0;
2N/A }
2N/A if (OBJ_SEC(objp) != NULL)
2N/A free(OBJ_SEC(objp));
2N/A
2N/A OBJ_SEC(objp) = NULL;
2N/A }
2N/A
2N/A /*
2N/A * Free the storage allocated to the extra attribute list.
2N/A */
2N/A kms_cleanup_extra_attr(objp);
2N/A}
2N/A
2N/Avoid
2N/Akms_free_object(kms_object_t *obj)
2N/A{
2N/A (void) pthread_cond_destroy(&obj->obj_free_cond);
2N/A (void) pthread_mutex_destroy(&obj->object_mutex);
2N/A
2N/A kms_cleanup_object(obj);
2N/A
2N/A free(obj);
2N/A}
2N/A
2N/A/*
2N/A * Create a new object. Copy the attributes that can be modified
2N/A * (in the boolean attribute mask field and extra attribute list)
2N/A * from the old object to the new object.
2N/A *
2N/A * The caller of this function holds the lock on the old object.
2N/A */
2N/ACK_RV
2N/Akms_copy_object(kms_object_t *old_object, kms_object_t **new_object,
2N/A boolean_t copy_everything, kms_session_t *sp)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A kms_object_t *new_objp = NULL;
2N/A CK_ATTRIBUTE_INFO_PTR attrp;
2N/A
2N/A /* Allocate new object. */
2N/A new_objp = kms_new_object();
2N/A if (new_objp == NULL)
2N/A return (CKR_HOST_MEMORY);
2N/A
2N/A new_objp->class = old_object->class;
2N/A new_objp->bool_attr_mask = old_object->bool_attr_mask;
2N/A
2N/A attrp = old_object->extra_attrlistp;
2N/A while (attrp) {
2N/A /*
2N/A * Copy the attribute_info struct from the old
2N/A * object to a new attribute_info struct, and add
2N/A * that new struct to the extra attribute list
2N/A * of the new object.
2N/A */
2N/A rv = kms_copy_extra_attr(attrp, new_objp);
2N/A if (rv != CKR_OK) {
2N/A kms_free_object(new_objp);
2N/A return (rv);
2N/A }
2N/A attrp = attrp->next;
2N/A }
2N/A
2N/A *new_object = new_objp;
2N/A
2N/A if (!copy_everything) {
2N/A /* done with copying all information that can be modified */
2N/A return (CKR_OK);
2N/A }
2N/A
2N/A /*
2N/A * Copy the rest of the object.
2N/A * Certain fields that are not appropriate for coping will be
2N/A * initialized.
2N/A */
2N/A new_objp->key_type = old_object->key_type;
2N/A new_objp->magic_marker = old_object->magic_marker;
2N/A new_objp->mechanism = old_object->mechanism;
2N/A new_objp->session_handle = (CK_SESSION_HANDLE)sp;
2N/A
2N/A /* copy key related information */
2N/A switch (new_objp->class) {
2N/A case CKO_SECRET_KEY:
2N/A rv = kms_copy_secret_key_attr(OBJ_SEC(old_object),
2N/A &(OBJ_SEC(new_objp)));
2N/A break;
2N/A default:
2N/A /* should never be this case */
2N/A break;
2N/A }
2N/A if (rv != CKR_OK) {
2N/A kms_free_object(new_objp);
2N/A *new_object = NULL;
2N/A }
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Copy the attributes (in the boolean attribute mask field and
2N/A * extra attribute list) from the new object back to the original
2N/A * object. Also, clean up and release all the storage in the extra
2N/A * attribute list of the original object.
2N/A *
2N/A * The caller of this function holds the lock on the old object.
2N/A */
2N/Avoid
2N/Akms_merge_object(kms_object_t *old_object, kms_object_t *new_object)
2N/A{
2N/A old_object->bool_attr_mask = new_object->bool_attr_mask;
2N/A kms_cleanup_extra_attr(old_object);
2N/A old_object->extra_attrlistp = new_object->extra_attrlistp;
2N/A}
2N/A
2N/A/*
2N/A * Create a new object struct. If it is a session object, add the object to
2N/A * the session's object list. If it is a token object, add it to the slot's
2N/A * token object list. The caller does not hold the slot lock.
2N/A */
2N/ACK_RV
2N/Akms_add_object(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
2N/A CK_ULONG *objecthandle_p, kms_session_t *sp)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A kms_object_t *new_objp = NULL;
2N/A kms_slot_t *pslot;
2N/A CK_ATTRIBUTE pritmpl;
2N/A CK_BBOOL is_pri_obj, is_token_obj;
2N/A
2N/A new_objp = kms_new_object();
2N/A if (new_objp == NULL)
2N/A return (CKR_HOST_MEMORY);
2N/A
2N/A rv = kms_build_object(pTemplate, ulCount, new_objp);
2N/A if (rv != CKR_OK)
2N/A goto fail_cleanup;
2N/A
2N/A /* Cannot create a token object with a READ-ONLY session */
2N/A pritmpl.type = CKA_TOKEN;
2N/A pritmpl.pValue = &is_token_obj;
2N/A pritmpl.ulValueLen = sizeof (is_token_obj);
2N/A rv = kms_get_attribute(new_objp, &pritmpl);
2N/A if (rv != CKR_OK)
2N/A goto fail_cleanup;
2N/A
2N/A if (is_token_obj && sp->ses_RO) {
2N/A rv = CKR_SESSION_READ_ONLY;
2N/A goto fail_cleanup;
2N/A }
2N/A
2N/A /*
2N/A * If the KMS supports object creation, create the object
2N/A * in the KMS. Otherwise, create the object in the library.
2N/A */
2N/A
2N/A /* Get the CKA_PRIVATE value of this object. */
2N/A pritmpl.type = CKA_PRIVATE;
2N/A pritmpl.pValue = &is_pri_obj;
2N/A pritmpl.ulValueLen = sizeof (is_pri_obj);
2N/A
2N/A rv = kms_get_attribute(new_objp, &pritmpl);
2N/A if (rv != CKR_OK) {
2N/A goto fail_cleanup;
2N/A }
2N/A
2N/A /* Set the PRIVATE_BOOL_ON and TOKEN_BOOL_ON attributes */
2N/A if (is_pri_obj)
2N/A new_objp->bool_attr_mask |= PRIVATE_BOOL_ON;
2N/A else
2N/A new_objp->bool_attr_mask &= ~PRIVATE_BOOL_ON;
2N/A
2N/A if (is_token_obj)
2N/A new_objp->bool_attr_mask |= TOKEN_BOOL_ON;
2N/A else
2N/A new_objp->bool_attr_mask &= ~TOKEN_BOOL_ON;
2N/A
2N/A new_objp->session_handle = (CK_SESSION_HANDLE)sp;
2N/A
2N/A if (is_token_obj) {
2N/A /* Add the new object to the slot's token object list. */
2N/A pslot = get_slotinfo();
2N/A kms_add_token_object_to_slot(new_objp, pslot);
2N/A } else {
2N/A /* Add the new object to the session's object list. */
2N/A kms_add_object_to_session(new_objp, sp);
2N/A }
2N/A
2N/A /* Type casting the address of an object struct to an object handle. */
2N/A if (rv == CKR_OK)
2N/A *objecthandle_p = (CK_ULONG)new_objp;
2N/A
2N/Afail_cleanup:
2N/A if (rv != CKR_OK) {
2N/A kms_free_object(new_objp);
2N/A }
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Remove an object from the session's object list.
2N/A *
2N/A * The caller of this function holds the session lock.
2N/A */
2N/ACK_RV
2N/Akms_remove_object_from_session(kms_object_t *objp, kms_session_t *sp)
2N/A{
2N/A kms_object_t *tmp_objp;
2N/A boolean_t found = B_FALSE;
2N/A
2N/A /*
2N/A * Remove the object from the session's object list.
2N/A */
2N/A if ((sp == NULL) ||
2N/A (sp->magic_marker != KMSTOKEN_SESSION_MAGIC)) {
2N/A return (CKR_SESSION_HANDLE_INVALID);
2N/A }
2N/A
2N/A if ((sp->object_list == NULL) || (objp == NULL) ||
2N/A (objp->magic_marker != KMSTOKEN_OBJECT_MAGIC)) {
2N/A return (CKR_OBJECT_HANDLE_INVALID);
2N/A }
2N/A
2N/A tmp_objp = sp->object_list;
2N/A while (tmp_objp) {
2N/A if (tmp_objp == objp) {
2N/A found = B_TRUE;
2N/A break;
2N/A }
2N/A tmp_objp = tmp_objp->next;
2N/A }
2N/A if (!found)
2N/A return (CKR_OBJECT_HANDLE_INVALID);
2N/A
2N/A if (sp->object_list == objp) {
2N/A /* Object is the first one in the list. */
2N/A if (objp->next) {
2N/A sp->object_list = objp->next;
2N/A objp->next->prev = NULL;
2N/A } else {
2N/A /* Object is the only one in the list. */
2N/A sp->object_list = NULL;
2N/A }
2N/A } else {
2N/A /* Object is not the first one in the list. */
2N/A if (objp->next) {
2N/A /* Object is in the middle of the list. */
2N/A objp->prev->next = objp->next;
2N/A objp->next->prev = objp->prev;
2N/A } else {
2N/A /* Object is the last one in the list. */
2N/A objp->prev->next = NULL;
2N/A }
2N/A }
2N/A return (CKR_OK);
2N/A}
2N/A
2N/A/*
2N/A * This function adds the to-be-freed session object to a linked list.
2N/A * When the number of objects queued in the linked list reaches the
2N/A * maximum threshold MAX_OBJ_TO_BE_FREED, it will free the first
2N/A * object (FIFO) in the list.
2N/A */
2N/Avoid
2N/Akms_object_delay_free(kms_object_t *objp)
2N/A{
2N/A kms_object_t *tmp;
2N/A
2N/A (void) pthread_mutex_lock(&obj_delay_freed.obj_to_be_free_mutex);
2N/A
2N/A /* Add the newly deleted object at the end of the list */
2N/A objp->next = NULL;
2N/A if (obj_delay_freed.first == NULL) {
2N/A obj_delay_freed.last = objp;
2N/A obj_delay_freed.first = objp;
2N/A } else {
2N/A obj_delay_freed.last->next = objp;
2N/A obj_delay_freed.last = objp;
2N/A }
2N/A
2N/A if (++obj_delay_freed.count >= MAX_OBJ_TO_BE_FREED) {
2N/A /*
2N/A * Free the first object in the list only if
2N/A * the total count reaches maximum threshold.
2N/A */
2N/A obj_delay_freed.count--;
2N/A tmp = obj_delay_freed.first->next;
2N/A kms_free_object(obj_delay_freed.first);
2N/A obj_delay_freed.first = tmp;
2N/A }
2N/A (void) pthread_mutex_unlock(&obj_delay_freed.obj_to_be_free_mutex);
2N/A}
2N/A
2N/Astatic void
2N/Akms_delete_object_cleanup(kms_object_t *objp, boolean_t force)
2N/A{
2N/A /* Acquire the lock on the object. */
2N/A (void) pthread_mutex_lock(&objp->object_mutex);
2N/A
2N/A /*
2N/A * Make sure another thread hasn't freed the object.
2N/A */
2N/A if (objp->magic_marker != KMSTOKEN_OBJECT_MAGIC) {
2N/A (void) pthread_mutex_unlock(&objp->object_mutex);
2N/A return;
2N/A }
2N/A
2N/A /*
2N/A * The deletion of an object must be blocked when the object
2N/A * reference count is not zero. This means if any object related
2N/A * operation starts prior to the delete object operation gets in,
2N/A * the object deleting thread must wait for the non-deleting
2N/A * operation to be completed before it can proceed the delete
2N/A * operation.
2N/A *
2N/A * Unless we are being forced to shut everything down, this only
2N/A * happens if the library's _fini() is running not if someone
2N/A * explicitly called C_Finalize().
2N/A */
2N/A if (force) {
2N/A objp->obj_refcnt = 0;
2N/A }
2N/A
2N/A while (objp->obj_refcnt != 0) {
2N/A /*
2N/A * We set the OBJECT_REFCNT_WAITING flag before we put
2N/A * this deleting thread in a wait state, so other non-deleting
2N/A * operation thread will signal to wake it up only when
2N/A * the object reference count becomes zero and this flag
2N/A * is set.
2N/A */
2N/A objp->obj_delete_sync |= OBJECT_REFCNT_WAITING;
2N/A (void) pthread_cond_wait(&objp->obj_free_cond,
2N/A &objp->object_mutex);
2N/A }
2N/A
2N/A objp->obj_delete_sync &= ~OBJECT_REFCNT_WAITING;
2N/A
2N/A /* Mark object as no longer valid. */
2N/A objp->magic_marker = 0;
2N/A kms_cleanup_object(objp);
2N/A
2N/A objp->obj_delete_sync &= ~OBJECT_IS_DELETING;
2N/A (void) pthread_mutex_unlock(&objp->object_mutex);
2N/A
2N/A if (objp->bool_attr_mask & TOKEN_BOOL_ON)
2N/A free(objp);
2N/A else
2N/A kms_object_delay_free(objp);
2N/A}
2N/A
2N/A/*
2N/A * Delete a session object:
2N/A * - Remove the object from the session's object list.
2N/A * - Release the storage allocated to the object.
2N/A *
2N/A * The boolean argument ses_lock_held is used to indicate that whether
2N/A * the caller holds the session lock or not.
2N/A * - When called by kms_delete_all_objects_in_session() or
2N/A * kms_delete_pri_objects_in_slot() -- ses_lock_held = TRUE.
2N/A *
2N/A * The boolean argument wrapper_only is used to indicate that whether
2N/A * the caller only wants to clean up the object wrapper from the library and
2N/A * needs not to make an call to KMS.
2N/A * - This argument only applies to the object created in the provider level.
2N/A * - When called by kms_cleanup_pri_objects_in_slot(), wrapper_only is TRUE.
2N/A * - When called by C_DestroyObject(), wrapper_only is FALSE.
2N/A * - When called by kms_delete_all_objects_in_session(), the value of
2N/A * wrapper_only depends on its caller.
2N/A */
2N/ACK_RV
2N/Akms_delete_object(kms_session_t *sp, kms_object_t *objp,
2N/A boolean_t ses_lock_held, boolean_t wrapper_only)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A
2N/A /*
2N/A * Check to see if the caller holds the lock on the session.
2N/A * If not, we need to acquire that lock in order to proceed.
2N/A */
2N/A if (!ses_lock_held) {
2N/A /* Acquire the session lock. */
2N/A (void) pthread_mutex_lock(&sp->session_mutex);
2N/A }
2N/A
2N/A /* Remove the object from the session's object list first. */
2N/A if ((rv = kms_remove_object_from_session(objp, sp))) {
2N/A if (!ses_lock_held)
2N/A (void) pthread_mutex_unlock(&sp->session_mutex);
2N/A return (rv);
2N/A }
2N/A
2N/A if (!wrapper_only)
2N/A (void) pthread_mutex_unlock(&sp->session_mutex);
2N/A
2N/A kms_delete_object_cleanup(objp, wrapper_only);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Delete all the objects in a session. The caller holds the lock
2N/A * on the session. If the wrapper_only argument is TRUE, the caller only
2N/A * want to clean up object wrappers in the library.
2N/A */
2N/Avoid
2N/Akms_delete_all_objects_in_session(kms_session_t *sp,
2N/A boolean_t wrapper_only)
2N/A{
2N/A kms_object_t *objp = sp->object_list;
2N/A kms_object_t *objp1;
2N/A
2N/A /* Delete all the objects in the session. */
2N/A while (objp) {
2N/A objp1 = objp->next;
2N/A (void) kms_delete_object(sp, objp, B_TRUE,
2N/A wrapper_only);
2N/A
2N/A objp = objp1;
2N/A }
2N/A}
2N/A
2N/Astatic CK_RV
2N/Aadd_to_search_result(kms_object_t *obj, find_context_t *fcontext,
2N/A CK_ULONG *num_result_alloc)
2N/A{
2N/A /*
2N/A * allocate space for storing results if the currently
2N/A * allocated space is not enough
2N/A */
2N/A if (*num_result_alloc <= fcontext->num_results) {
2N/A fcontext->objs_found = realloc(fcontext->objs_found,
2N/A sizeof (kms_object_t *) * (*num_result_alloc + BUFSIZ));
2N/A if (fcontext->objs_found == NULL) {
2N/A return (CKR_HOST_MEMORY);
2N/A }
2N/A *num_result_alloc += BUFSIZ;
2N/A }
2N/A
2N/A (fcontext->objs_found)[(fcontext->num_results)++] = obj;
2N/A return (CKR_OK);
2N/A}
2N/A
2N/Astatic CK_RV
2N/Asearch_for_objects(kms_session_t *sp, CK_ATTRIBUTE_PTR pTemplate,
2N/A CK_ULONG ulCount, find_context_t *fcontext)
2N/A{
2N/A kms_session_t *session_p;
2N/A kms_object_t *obj;
2N/A CK_OBJECT_CLASS pclasses[6]; /* classes attrs possibly exist */
2N/A CK_OBJECT_CLASS findclass = 0;
2N/A CK_ULONG num_pclasses; /* number of possible classes */
2N/A CK_ULONG num_result_alloc = 0; /* spaces allocated for results */
2N/A CK_RV rv = CKR_OK;
2N/A kms_slot_t *pslot = NULL;
2N/A boolean_t token_specified = B_FALSE;
2N/A boolean_t token_flag_val = B_FALSE;
2N/A int i;
2N/A char *label = NULL;
2N/A
2N/A if (ulCount > 0) {
2N/A /* there are some search requirement */
2N/A kms_process_find_attr(pclasses, &num_pclasses,
2N/A pTemplate, ulCount);
2N/A }
2N/A
2N/A /*
2N/A * Look through template and see if it explicitly specifies
2N/A * whether we need to look for token objects and also to see
2N/A * if a specific label was specified.
2N/A */
2N/A for (i = 0; i < ulCount; i++) {
2N/A if (pTemplate[i].type == CKA_TOKEN) {
2N/A token_specified = B_TRUE;
2N/A token_flag_val = *((CK_BBOOL *)pTemplate[i].pValue);
2N/A }
2N/A if (pTemplate[i].type == CKA_LABEL) {
2N/A label = (char *)pTemplate[i].pValue;
2N/A }
2N/A if (pTemplate[i].type == CKA_CLASS) {
2N/A findclass = *((CK_OBJECT_CLASS *)pTemplate[i].pValue);
2N/A }
2N/A }
2N/A
2N/A pslot = get_slotinfo();
2N/A
2N/A /* Acquire the slot lock */
2N/A if (token_flag_val || !token_specified) {
2N/A int found = 0;
2N/A (void) pthread_mutex_lock(&pslot->sl_mutex);
2N/A /*
2N/A * Make sure the object list is current.
2N/A */
2N/A rv = KMS_RefreshObjectList(sp, pslot);
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A return (rv);
2N/A }
2N/A
2N/A obj = pslot->sl_tobj_list;
2N/A while (obj) {
2N/A (void) pthread_mutex_lock(&obj->object_mutex);
2N/A if (((token_specified) && (ulCount > 1)) ||
2N/A ((!token_specified) && (ulCount > 0))) {
2N/A if (kms_find_match_attrs(obj, pclasses,
2N/A num_pclasses, pTemplate, ulCount)) {
2N/A rv = add_to_search_result(
2N/A obj, fcontext, &num_result_alloc);
2N/A found++;
2N/A }
2N/A } else {
2N/A /* no search criteria, just record the object */
2N/A rv = add_to_search_result(obj, fcontext,
2N/A &num_result_alloc);
2N/A found++;
2N/A }
2N/A (void) pthread_mutex_unlock(&obj->object_mutex);
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A goto cleanup;
2N/A }
2N/A obj = obj->next;
2N/A }
2N/A /*
2N/A * If the caller specified a label but it was not found,
2N/A * query the KMS to see if it exists there but is not in the
2N/A * local list.
2N/A */
2N/A if (!found && label != NULL && findclass == CKO_SECRET_KEY) {
2N/A kms_object_t *pObj = NULL;
2N/A rv = KMS_RetrieveKeyObj(sp, label, &pObj);
2N/A if (rv == CKR_OK && pObj != NULL) {
2N/A /*
2N/A * If we DID find the object, add it to the
2N/A * slot token object list and label list.
2N/A */
2N/A rv = add_to_search_result(pObj, fcontext,
2N/A &num_result_alloc);
2N/A if (rv != CKR_OK) {
2N/A kms_free_object(pObj);
2N/A goto token_done;
2N/A }
2N/A
2N/A add_label_node(&sp->objlabel_tree, label);
2N/A rv = kms_update_label_file(sp);
2N/A if (rv != CKR_OK) {
2N/A kms_free_object(pObj);
2N/A goto token_done;
2N/A }
2N/A
2N/A /*
2N/A * If all went well, add it to the head of
2N/A * the token object list.
2N/A */
2N/A pObj->next = pslot->sl_tobj_list;
2N/A pObj->prev = NULL;
2N/A pslot->sl_tobj_list = pObj;
2N/A } else {
2N/A /*
2N/A * This just means the wrong label was used
2N/A * and the key really doesn't exist in the
2N/A * KMS.
2N/A */
2N/A rv = CKR_OK;
2N/A }
2N/A }
2N/Atoken_done:
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A }
2N/A if (token_flag_val || rv != CKR_OK) {
2N/A return (rv);
2N/A }
2N/A
2N/A /*
2N/A * Go through all objects in each session.
2N/A * Acquire individual session lock for the session
2N/A * we are searching.
2N/A */
2N/A session_p = pslot->sl_sess_list;
2N/A while (session_p) {
2N/A (void) pthread_mutex_lock(&session_p->session_mutex);
2N/A obj = session_p->object_list;
2N/A while (obj) {
2N/A (void) pthread_mutex_lock(&obj->object_mutex);
2N/A if (ulCount > 0) {
2N/A if (kms_find_match_attrs(obj, pclasses,
2N/A num_pclasses, pTemplate, ulCount)) {
2N/A rv = add_to_search_result(
2N/A obj, fcontext, &num_result_alloc);
2N/A }
2N/A } else {
2N/A /* no search criteria, just record the object */
2N/A rv = add_to_search_result(obj, fcontext,
2N/A &num_result_alloc);
2N/A }
2N/A (void) pthread_mutex_unlock(&obj->object_mutex);
2N/A if (rv != CKR_OK) {
2N/A (void) pthread_mutex_unlock(
2N/A &session_p->session_mutex);
2N/A goto cleanup;
2N/A }
2N/A obj = obj->next;
2N/A }
2N/A (void) pthread_mutex_unlock(&session_p->session_mutex);
2N/A session_p = session_p->next;
2N/A }
2N/A
2N/Acleanup:
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Initialize the context for C_FindObjects() calls
2N/A */
2N/ACK_RV
2N/Akms_find_objects_init(kms_session_t *sp, CK_ATTRIBUTE_PTR pTemplate,
2N/A CK_ULONG ulCount)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A CK_OBJECT_CLASS class; /* for kms_validate_attr(). Value unused */
2N/A find_context_t *fcontext;
2N/A
2N/A if (ulCount) {
2N/A rv = kms_validate_attr(pTemplate, ulCount, &class);
2N/A /* Make sure all attributes in template are valid */
2N/A if (rv != CKR_OK) {
2N/A return (rv);
2N/A }
2N/A }
2N/A
2N/A /* prepare the find context */
2N/A fcontext = calloc(1, sizeof (find_context_t));
2N/A if (fcontext == NULL) {
2N/A return (CKR_HOST_MEMORY);
2N/A }
2N/A
2N/A rv = search_for_objects(sp, pTemplate, ulCount, fcontext);
2N/A if (rv != CKR_OK) {
2N/A free(fcontext);
2N/A return (rv);
2N/A }
2N/A
2N/A /* store the find_context in the session */
2N/A sp->find_objects.context = (CK_VOID_PTR)fcontext;
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/Avoid
2N/Akms_find_objects_final(kms_session_t *sp)
2N/A{
2N/A find_context_t *fcontext;
2N/A
2N/A fcontext = sp->find_objects.context;
2N/A sp->find_objects.context = NULL;
2N/A sp->find_objects.flags = 0;
2N/A if (fcontext->objs_found != NULL) {
2N/A free(fcontext->objs_found);
2N/A }
2N/A
2N/A free(fcontext);
2N/A}
2N/A
2N/ACK_RV
2N/Akms_find_objects(kms_session_t *sp, CK_OBJECT_HANDLE *obj_found,
2N/A CK_ULONG max_obj_requested, CK_ULONG *found_obj_count)
2N/A{
2N/A find_context_t *fcontext;
2N/A CK_ULONG num_obj_found = 0;
2N/A CK_ULONG i;
2N/A kms_object_t *obj;
2N/A
2N/A fcontext = sp->find_objects.context;
2N/A
2N/A for (i = fcontext->next_result_index;
2N/A ((num_obj_found < max_obj_requested) &&
2N/A (i < fcontext->num_results));
2N/A i++) {
2N/A obj = fcontext->objs_found[i];
2N/A if (obj != NULL) {
2N/A (void) pthread_mutex_lock(&obj->object_mutex);
2N/A /* a sanity check to make sure the obj is still valid */
2N/A if (obj->magic_marker == KMSTOKEN_OBJECT_MAGIC) {
2N/A obj_found[num_obj_found] =
2N/A (CK_OBJECT_HANDLE)obj;
2N/A num_obj_found++;
2N/A }
2N/A (void) pthread_mutex_unlock(&obj->object_mutex);
2N/A }
2N/A }
2N/A fcontext->next_result_index = i;
2N/A *found_obj_count = num_obj_found;
2N/A return (CKR_OK);
2N/A}
2N/A
2N/A/*
2N/A * Add an token object to the token object list in slot.
2N/A *
2N/A * This function will acquire the lock on the slot, and release
2N/A * that lock after adding the object to the slot's token object list.
2N/A */
2N/Avoid
2N/Akms_add_token_object_to_slot(kms_object_t *objp, kms_slot_t *pslot)
2N/A{
2N/A /* Acquire the slot lock. */
2N/A (void) pthread_mutex_lock(&pslot->sl_mutex);
2N/A
2N/A /* Insert the new object in front of slot's token object list. */
2N/A if (pslot->sl_tobj_list == NULL) {
2N/A pslot->sl_tobj_list = objp;
2N/A objp->next = NULL;
2N/A objp->prev = NULL;
2N/A } else {
2N/A pslot->sl_tobj_list->prev = objp;
2N/A objp->next = pslot->sl_tobj_list;
2N/A objp->prev = NULL;
2N/A pslot->sl_tobj_list = objp;
2N/A }
2N/A
2N/A /* Release the slot lock. */
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A}
2N/A
2N/A/*
2N/A * Remove an token object from the slot's token object list.
2N/A * This routine is called by kms_delete_token_object().
2N/A * The caller of this function hold the slot lock.
2N/A */
2N/Avoid
2N/Akms_remove_token_object_from_slot(kms_slot_t *pslot,
2N/A kms_object_t *objp)
2N/A{
2N/A
2N/A if (pslot->sl_tobj_list == objp) {
2N/A /* Object is the first one in the list */
2N/A if (objp->next) {
2N/A pslot->sl_tobj_list = objp->next;
2N/A objp->next->prev = NULL;
2N/A } else {
2N/A /* Object is the only one in the list. */
2N/A pslot->sl_tobj_list = NULL;
2N/A }
2N/A } else {
2N/A /* Object is not the first one in the list. */
2N/A if (objp->next) {
2N/A /* Object is in the middle of the list. */
2N/A if (objp->prev)
2N/A objp->prev->next = objp->next;
2N/A objp->next->prev = objp->prev;
2N/A } else if (objp->prev) {
2N/A /* Object is the last one in the list. */
2N/A objp->prev->next = NULL;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Delete a token object:
2N/A * - Remove the object from the slot's token object list.
2N/A * - Release the storage allocated to the object.
2N/A *
2N/A * The boolean argument slot_lock_held is used to indicate that whether
2N/A * the caller holds the slot lock or not. When the caller does not hold
2N/A * the slot lock, this function will acquire that lock in order to proceed,
2N/A * and also release that lock before returning to caller.
2N/A *
2N/A * The boolean argument wrapper_only is used to indicate that whether
2N/A * the caller only wants to the object wrapper from library.
2N/A */
2N/ACK_RV
2N/Akms_delete_token_object(kms_slot_t *pslot, kms_session_t *sp,
2N/A kms_object_t *objp, boolean_t slot_lock_held, boolean_t wrapper_only)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A
2N/A if (!slot_lock_held) {
2N/A (void) pthread_mutex_lock(&pslot->sl_mutex);
2N/A }
2N/A if (!wrapper_only && objp->class == CKO_SECRET_KEY) {
2N/A /* Delete from KMS */
2N/A rv = KMS_DestroyKey(sp, objp);
2N/A }
2N/A
2N/A /* Remove the object from the slot's token object list first. */
2N/A kms_remove_token_object_from_slot(pslot, objp);
2N/A
2N/A /* Release the slot lock if the call doesn't hold the lock. */
2N/A if (!slot_lock_held) {
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A }
2N/A
2N/A kms_delete_object_cleanup(objp, wrapper_only);
2N/A
2N/A return (rv);
2N/A}
2N/A
2N/A/*
2N/A * Clean up private object wrappers in this slot. The caller holds the slot
2N/A * lock.
2N/A */
2N/Avoid
2N/Akms_cleanup_pri_objects_in_slot(kms_slot_t *pslot,
2N/A kms_session_t *cur_sp)
2N/A{
2N/A kms_session_t *session_p;
2N/A kms_object_t *objp;
2N/A kms_object_t *objp1;
2N/A
2N/A /*
2N/A * Delete every private token object from
2N/A * the slot token object list.
2N/A */
2N/A (void) pthread_mutex_lock(&pslot->sl_mutex);
2N/A objp = pslot->sl_tobj_list;
2N/A while (objp) {
2N/A objp1 = objp->next;
2N/A /*
2N/A * The first TRUE boolean argument indicates that the caller
2N/A * hold the slot lock. The second TRUE boolean argument
2N/A * indicates that the caller just wants to clean up the object
2N/A * wrapper from the library only.
2N/A */
2N/A if (objp->bool_attr_mask & PRIVATE_BOOL_ON) {
2N/A (void) kms_delete_token_object(pslot, cur_sp, objp,
2N/A B_TRUE, B_TRUE);
2N/A }
2N/A objp = objp1;
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&pslot->sl_mutex);
2N/A /*
2N/A * Walk through all the sessions in this slot and delete every
2N/A * private object.
2N/A */
2N/A session_p = pslot->sl_sess_list;
2N/A while (session_p) {
2N/A
2N/A /* Delete all the objects in the session. */
2N/A objp = session_p->object_list;
2N/A while (objp) {
2N/A objp1 = objp->next;
2N/A /*
2N/A * The FALSE boolean argument indicates that the
2N/A * caller does not hold the session lock. The TRUE
2N/A * boolean argument indicates that the caller just
2N/A * want to clean upt the object wrapper from the
2N/A * library only.
2N/A */
2N/A if (objp->bool_attr_mask & PRIVATE_BOOL_ON) {
2N/A (void) kms_delete_object(session_p,
2N/A objp, B_FALSE, B_TRUE);
2N/A }
2N/A objp = objp1;
2N/A }
2N/A
2N/A session_p = session_p->next;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Get the object size in bytes for the objects created in the library.
2N/A */
2N/ACK_RV
2N/Akms_get_object_size(kms_object_t *obj, CK_ULONG_PTR pulSize)
2N/A{
2N/A CK_RV rv = CKR_OK;
2N/A CK_ULONG obj_size;
2N/A
2N/A obj_size = sizeof (kms_object_t);
2N/A
2N/A switch (obj->class) {
2N/A case CKO_SECRET_KEY:
2N/A obj_size += OBJ_SEC_VALUE_LEN(obj);
2N/A break;
2N/A
2N/A default:
2N/A rv = CKR_OBJECT_HANDLE_INVALID;
2N/A }
2N/A
2N/A if (rv == CKR_OK) {
2N/A *pulSize = obj_size;
2N/A }
2N/A
2N/A return (rv);
2N/A}