xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_kms/common/kmsSlottable.c (revision 12720:3db6e0082404)
1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM  *
4*12720SWyllys.Ingersoll@Sun.COM  * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM  * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM  * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM  *
8*12720SWyllys.Ingersoll@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM  * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM  * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM  *
13*12720SWyllys.Ingersoll@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM  *
19*12720SWyllys.Ingersoll@Sun.COM  * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM  */
21*12720SWyllys.Ingersoll@Sun.COM /*
22*12720SWyllys.Ingersoll@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*12720SWyllys.Ingersoll@Sun.COM  */
24*12720SWyllys.Ingersoll@Sun.COM 
25*12720SWyllys.Ingersoll@Sun.COM #include <errno.h>
26*12720SWyllys.Ingersoll@Sun.COM #include <security/cryptoki.h>
27*12720SWyllys.Ingersoll@Sun.COM #include "kmsGlobal.h"
28*12720SWyllys.Ingersoll@Sun.COM #include "kmsSlot.h"
29*12720SWyllys.Ingersoll@Sun.COM 
30*12720SWyllys.Ingersoll@Sun.COM static kms_slot_t	*slotinfo = NULL;
31*12720SWyllys.Ingersoll@Sun.COM 
32*12720SWyllys.Ingersoll@Sun.COM /*
33*12720SWyllys.Ingersoll@Sun.COM  * Initialize the slotinfo record.
34*12720SWyllys.Ingersoll@Sun.COM  *
35*12720SWyllys.Ingersoll@Sun.COM  * This function is called from C_Initialize() only.  Since C_Initialize()
36*12720SWyllys.Ingersoll@Sun.COM  * holds the global mutex lock, there is no need to acquire another lock
37*12720SWyllys.Ingersoll@Sun.COM  * in this routine to protect the slot table.
38*12720SWyllys.Ingersoll@Sun.COM  */
39*12720SWyllys.Ingersoll@Sun.COM CK_RV
kms_slottable_init()40*12720SWyllys.Ingersoll@Sun.COM kms_slottable_init()
41*12720SWyllys.Ingersoll@Sun.COM {
42*12720SWyllys.Ingersoll@Sun.COM 	CK_RV rv = CKR_OK;
43*12720SWyllys.Ingersoll@Sun.COM 
44*12720SWyllys.Ingersoll@Sun.COM 	/* Allocate space for the slot table */
45*12720SWyllys.Ingersoll@Sun.COM 	slotinfo = calloc(KMS_SLOTS, sizeof (kms_slot_t));
46*12720SWyllys.Ingersoll@Sun.COM 	if (slotinfo == NULL)
47*12720SWyllys.Ingersoll@Sun.COM 		return (CKR_HOST_MEMORY);
48*12720SWyllys.Ingersoll@Sun.COM 
49*12720SWyllys.Ingersoll@Sun.COM 	slotinfo->sl_sess_list = NULL;
50*12720SWyllys.Ingersoll@Sun.COM 	slotinfo->sl_tobj_list = NULL;
51*12720SWyllys.Ingersoll@Sun.COM 	slotinfo->sl_state = CKU_PUBLIC;
52*12720SWyllys.Ingersoll@Sun.COM 
53*12720SWyllys.Ingersoll@Sun.COM 	/* Initialize this slot's mutex */
54*12720SWyllys.Ingersoll@Sun.COM 	if (pthread_mutex_init(&slotinfo->sl_mutex, NULL) != 0) {
55*12720SWyllys.Ingersoll@Sun.COM 		(void) free(slotinfo);
56*12720SWyllys.Ingersoll@Sun.COM 		slotinfo = NULL;
57*12720SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
58*12720SWyllys.Ingersoll@Sun.COM 	}
59*12720SWyllys.Ingersoll@Sun.COM 
60*12720SWyllys.Ingersoll@Sun.COM 	return (rv);
61*12720SWyllys.Ingersoll@Sun.COM }
62*12720SWyllys.Ingersoll@Sun.COM 
63*12720SWyllys.Ingersoll@Sun.COM void
cleanup_slottable()64*12720SWyllys.Ingersoll@Sun.COM cleanup_slottable()
65*12720SWyllys.Ingersoll@Sun.COM {
66*12720SWyllys.Ingersoll@Sun.COM 	if (slotinfo != NULL) {
67*12720SWyllys.Ingersoll@Sun.COM 		(void) pthread_mutex_destroy(&slotinfo->sl_mutex);
68*12720SWyllys.Ingersoll@Sun.COM 		(void) free(slotinfo);
69*12720SWyllys.Ingersoll@Sun.COM 		slotinfo = NULL;
70*12720SWyllys.Ingersoll@Sun.COM 	}
71*12720SWyllys.Ingersoll@Sun.COM }
72*12720SWyllys.Ingersoll@Sun.COM 
73*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *
get_slotinfo()74*12720SWyllys.Ingersoll@Sun.COM get_slotinfo()
75*12720SWyllys.Ingersoll@Sun.COM {
76*12720SWyllys.Ingersoll@Sun.COM 	return (slotinfo);
77*12720SWyllys.Ingersoll@Sun.COM }
78