summaryrefslogtreecommitdiff
path: root/usr/src/lib/pkcs11/libpkcs11/common/pkcs11Slottable.c
blob: 9dae720e22a065e94f922b8b3f2d0d779f901015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <dlfcn.h>
#include <stdlib.h>
#include <pthread.h>
#include <strings.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Slot.h"
#include "metaGlobal.h"

pkcs11_slottable_t *slottable = NULL;

/*
 * pkcs11_slottable_initialize initizializes the global slottable.
 * This slottable will contain information about the plugged in
 * slots, including their mapped slotID.  This function should only
 * be called by C_Intialize.
 */
CK_RV
pkcs11_slottable_initialize() {


	pkcs11_slottable_t *stmp = malloc(sizeof (pkcs11_slottable_t));

	if (stmp == NULL)
		return (CKR_HOST_MEMORY);

	stmp->st_first = 1;
	stmp->st_cur_size = 0;
	stmp->st_last = 0;
	stmp->st_slots = NULL;

	if (pthread_mutex_init(&stmp->st_mutex, NULL) != 0) {
		free(stmp);
		return (CKR_FUNCTION_FAILED);
	}
	/* Set up for possible threads later */
	stmp->st_event_slot = 0;
	stmp->st_thr_count = 0;
	stmp->st_wfse_active = B_FALSE;
	stmp->st_blocking = B_FALSE;
	stmp->st_list_signaled = B_FALSE;

	(void) pthread_cond_init(&stmp->st_wait_cond, NULL);
	(void) pthread_mutex_init(&stmp->st_start_mutex, NULL);
	(void) pthread_cond_init(&stmp->st_start_cond, NULL);

	slottable = stmp;

	return (CKR_OK);

}

/*
 * pkcs11_slottable_increase should only be called from C_Initialize().
 * It is called after the first call to C_GetSlotList() and is used to
 * increase the size of the slottable, as needed, to contain the next
 * set of slots that C_Initialize() is currently mapping into the framework.
 */
CK_RV
pkcs11_slottable_increase(ulong_t increment) {

	pkcs11_slot_t **tmpslots;
	ulong_t newsize;

	(void) pthread_mutex_lock(&slottable->st_mutex);

	/* Add 1 to cover space for the metaslot */
	newsize = slottable->st_last + increment + 1;

	/* Check to see if we already have enough space */
	if (slottable->st_cur_size >= newsize) {
		(void) pthread_mutex_unlock(&slottable->st_mutex);
		return (CKR_OK);
	}

	tmpslots = realloc
	    (slottable->st_slots, newsize * sizeof (pkcs11_slot_t *));

	if (tmpslots == NULL) {
		(void) pthread_mutex_unlock(&slottable->st_mutex);
		return (CKR_HOST_MEMORY);
	}

	slottable->st_slots = tmpslots;
	slottable->st_cur_size = newsize;

	(void) pthread_mutex_unlock(&slottable->st_mutex);

	return (CKR_OK);
}

/*
 * pkcs11_slot_allocate should only be called from C_Initialize().
 * We won't know if the metaslot will be used until after all of
 * the other slots have been allocated.
 */
CK_RV
pkcs11_slot_allocate(CK_SLOT_ID *pslot_id) {

	pkcs11_slot_t *tmpslot;

	tmpslot = malloc(sizeof (pkcs11_slot_t));

	if (tmpslot == NULL)
		return (CKR_HOST_MEMORY);

	bzero(tmpslot, sizeof (pkcs11_slot_t));

	tmpslot->sl_wfse_state = WFSE_CLEAR;
	tmpslot->sl_enabledpol = B_FALSE;
	tmpslot->sl_no_wfse = B_FALSE;

	/* Initialize this slot's mutex */
	if (pthread_mutex_init(&tmpslot->sl_mutex, NULL) != 0) {
		free(tmpslot);
		return (CKR_FUNCTION_FAILED);
	}

	(void) pthread_mutex_lock(&slottable->st_mutex);

	slottable->st_last++;

	*pslot_id = slottable->st_last;

	slottable->st_slots[*pslot_id] = tmpslot;

	(void) pthread_mutex_unlock(&slottable->st_mutex);

	return (CKR_OK);

}

/*
 * pkcs11_slottable_delete should only be called by C_Finalize(),
 * or by C_Initialize() in error conditions.
 */
CK_RV
pkcs11_slottable_delete() {

	ulong_t i;
	uint32_t prov_id;
	int32_t last_prov_id = -1;
	pkcs11_slot_t *cur_slot;

	(void) pthread_mutex_lock(&slottable->st_mutex);

	for (i = slottable->st_first; i <= slottable->st_last; i++) {

		if (slottable->st_slots[i] != NULL) {

			cur_slot = slottable->st_slots[i];
			prov_id = cur_slot->sl_prov_id;

			(void) pthread_mutex_lock(&cur_slot->sl_mutex);

			/*
			 * For the first slot from this provider, do
			 * extra cleanup.
			 */
			if (prov_id != last_prov_id) {

				if (cur_slot->sl_wfse_state == WFSE_ACTIVE) {
					(void) pthread_cancel
					    (cur_slot->sl_tid);
				}

				/*
				 * Only call C_Finalize of plug-in if we
				 * get here from an explicit C_Finalize
				 * call from an application.  Otherwise,
				 * there is a risk that the application may
				 * have directly dlopened this provider and
				 * we could interrupt their work.  Plug-ins
				 * should have their own _fini function to
				 * clean up when they are no longer referenced.
				 */
				if ((cur_slot->sl_func_list != NULL) &&
				    (!fini_called)) {
					(void) cur_slot->
					    sl_func_list->C_Finalize(NULL);
				}

				/* metaslot won't have a sl_dldesc! */
				if (cur_slot->sl_dldesc != NULL) {
					(void) dlclose(cur_slot->sl_dldesc);
				}

				/*
				 * Each provider maintains one disabled
				 * mechanism list for each of its slots to use.
				 */
				if (cur_slot->sl_pol_mechs != NULL)
					free(cur_slot->sl_pol_mechs);
			}

			if (cur_slot->sl_wfse_args != NULL) {
				free(cur_slot->sl_wfse_args);
			}

			(void) pthread_mutex_unlock(&cur_slot->sl_mutex);

			/*
			 * Cleanup the session list.  This must
			 * happen after the mutext is unlocked
			 * because session_delete tries to lock it
			 * again.
			 */
			pkcs11_sessionlist_delete(cur_slot);

			(void) pthread_mutex_destroy(&cur_slot->sl_mutex);

			free(cur_slot);
			cur_slot = NULL;
			last_prov_id = prov_id;
		}
	}

	(void) pthread_cond_destroy(&slottable->st_wait_cond);
	(void) pthread_mutex_destroy(&slottable->st_start_mutex);
	(void) pthread_cond_destroy(&slottable->st_start_cond);

	free(slottable->st_slots);

	(void) pthread_mutex_unlock(&slottable->st_mutex);

	(void) pthread_mutex_destroy(&slottable->st_mutex);

	free(slottable);

	slottable = NULL;

	return (CKR_OK);

}

/*
 * pkcs11_is_valid_slot verifies that the slot ID passed to the
 * framework is valid.
 */
CK_RV
pkcs11_is_valid_slot(CK_SLOT_ID slot_id) {

	if ((slot_id < slottable->st_first) ||
	    (slot_id > slottable->st_last)) {
		return (CKR_SLOT_ID_INVALID);
	} else if (slottable->st_slots[slot_id] != NULL) {
		return (CKR_OK);
	} else {
		return (CKR_SLOT_ID_INVALID);
	}
}


/*
 * pkcs11_validate_and_convert_slotid verifies whether the slot ID
 * passed to the framework is valid, and convert it to the
 * true slot ID maintained in the framework data structures
 * accordingly.
 *
 * This is necessary because when metaslot is enabled, the slot
 * providing persistent object storage is "hidden".
 *
 * The real ID is returned in the "real_slot_id" argument regardless conversion
 * is done or not.
 */
CK_RV
pkcs11_validate_and_convert_slotid(CK_SLOT_ID slot_id,
    CK_SLOT_ID *real_slot_id) {

	if (!metaslot_enabled) {
		*real_slot_id = slot_id;
	} else {
		/* need to do conversion */
		if (slot_id >= metaslot_keystore_slotid) {
			*real_slot_id = slot_id + 1;
		} else {
			*real_slot_id = slot_id;
		}
	}
	return (pkcs11_is_valid_slot(*real_slot_id));
}