summaryrefslogtreecommitdiff
path: root/p11-kit/filter.c
blob: 09e1a452c8967cca3e874c925fc0b0074c0ba571 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (c) 2016, Red Hat Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Daiki Ueno
 */

#include "config.h"

#include "attrs.h"
#include "buffer.h"
#include "constants.h"
#include "debug.h"
#include "filter.h"
#include "iter.h"
#include "message.h"
#include "p11-kit.h"
#include "virtual.h"

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>

typedef struct {
	p11_virtual virt;
	CK_X_FUNCTION_LIST *lower;
	p11_destroyer destroyer;
	p11_array *entries;
	bool allowed;
	bool initialized;
	CK_SLOT_ID *slots;
	CK_ULONG n_slots;
	CK_ULONG max_slots;
} FilterData;

extern int p11_match_uri_token_info (CK_TOKEN_INFO_PTR one,
				     CK_TOKEN_INFO_PTR two);

static bool
filter_match_token (FilterData *filter, CK_TOKEN_INFO *token)
{
	unsigned int i;

	for (i = 0; i < filter->entries->num; i++) {
		CK_TOKEN_INFO *entry = filter->entries->elem[i];
		bool matched = p11_match_uri_token_info (entry, token);

		if ((filter->allowed && matched) ||
		    (!filter->allowed && !matched))
			return true;
	}

	return false;
}

static bool
filter_add_slot (FilterData *filter, CK_SLOT_ID slot)
{
	if (filter->n_slots >= filter->max_slots) {
		filter->max_slots = filter->max_slots * 2 + 1;
		filter->slots = realloc (filter->slots,
					 filter->max_slots * sizeof (CK_SLOT_ID));
		if (filter->slots == NULL)
			return false;
	}
	filter->slots[filter->n_slots++] = slot;
	return true;
}

static CK_RV
filter_ensure (FilterData *filter)
{
	CK_FUNCTION_LIST *lower = NULL;
	P11KitIter *iter = NULL;
	CK_RV rv = CKR_OK;

	if (filter->slots != NULL) {
		free (filter->slots);
		filter->slots = NULL;
	}
	filter->n_slots = 0;
	filter->max_slots = 0;

	iter = p11_kit_iter_new (NULL,
				 P11_KIT_ITER_WITH_TOKENS |
				 P11_KIT_ITER_WITHOUT_OBJECTS);
	if (iter == NULL) {
		rv = CKR_HOST_MEMORY;
		goto out;
	}

	lower = p11_virtual_wrap (filter->virt.lower_module, NULL);
	if (lower == NULL) {
		rv = CKR_HOST_MEMORY;
		goto out;
	}

	p11_kit_iter_begin_with (iter, lower, 0, CK_INVALID_HANDLE);
	while (p11_kit_iter_next (iter) == CKR_OK) {
		CK_TOKEN_INFO *token;

		token = p11_kit_iter_get_token (iter);
		if (filter_match_token (filter, token)) {
			CK_SLOT_ID slot;

			slot = p11_kit_iter_get_slot (iter);
			if (!filter_add_slot (filter, slot)) {
				rv = CKR_HOST_MEMORY;
				goto out;
			}
		}
	}

	rv = CKR_OK;
 out:
	p11_kit_iter_free (iter);
	if (lower)
		p11_virtual_unwrap (lower);
	return rv;
}

static void
filter_reinit (FilterData *filter)
{
	CK_RV rv;

	rv = filter_ensure (filter);
	if (rv == CKR_OK)
		filter->initialized = true;
	else {
		filter->initialized = false;
		p11_message ("filter cannot be initialized");
	}
}

static CK_RV
filter_C_Initialize (CK_X_FUNCTION_LIST *self,
		     CK_VOID_PTR pInitArgs)
{
	FilterData *filter = (FilterData *)self;
	CK_RV rv;

	rv = filter->lower->C_Initialize (filter->lower, pInitArgs);
	if (rv == CKR_OK)
		filter_reinit (filter);
	return rv;
}

static CK_RV
filter_C_Finalize (CK_X_FUNCTION_LIST *self,
		   CK_VOID_PTR pReserved)
{
	FilterData *filter = (FilterData *)self;

	free (filter->slots);
	filter->n_slots = 0;
	p11_array_clear (filter->entries);
	filter->initialized = false;
	filter->allowed = false;

	return filter->lower->C_Finalize (filter->lower, pReserved);
}

static CK_RV
filter_C_GetSlotList (CK_X_FUNCTION_LIST *self,
		      CK_BBOOL tokenPresent,
		      CK_SLOT_ID_PTR pSlotList,
		      CK_ULONG_PTR pulCount)
{
	FilterData *filter = (FilterData *)self;
	CK_ULONG count;

	if (pulCount == NULL)
		return CKR_ARGUMENTS_BAD;

	count = *pulCount;
	*pulCount = filter->n_slots;

	if (pSlotList == NULL)
		return CKR_OK;

	if (filter->n_slots > count)
		return CKR_BUFFER_TOO_SMALL;

	for (count = 0; count < filter->n_slots; count++)
		pSlotList[count] = count;
	*pulCount = filter->n_slots;
	return CKR_OK;
}

static CK_RV
filter_C_GetSlotInfo (CK_X_FUNCTION_LIST *self,
		      CK_SLOT_ID slotID,
		      CK_SLOT_INFO_PTR pInfo)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_GetSlotInfo (filter->lower, filter->slots[slotID], pInfo);
}

static CK_RV
filter_C_GetTokenInfo (CK_X_FUNCTION_LIST *self,
		       CK_SLOT_ID slotID,
		       CK_TOKEN_INFO_PTR pInfo)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_GetTokenInfo (filter->lower, filter->slots[slotID], pInfo);
}

static CK_RV
filter_C_GetMechanismList (CK_X_FUNCTION_LIST *self,
			   CK_SLOT_ID slotID,
			   CK_MECHANISM_TYPE_PTR pMechanismList,
			   CK_ULONG_PTR pulCount)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_GetMechanismList (filter->lower,
						  filter->slots[slotID],
						  pMechanismList,
						  pulCount);
}

static CK_RV
filter_C_GetMechanismInfo (CK_X_FUNCTION_LIST *self,
			   CK_SLOT_ID slotID,
			   CK_MECHANISM_TYPE type,
			   CK_MECHANISM_INFO_PTR pInfo)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_GetMechanismInfo (filter->lower,
						  filter->slots[slotID],
						  type,
						  pInfo);
}

static CK_RV
filter_C_InitToken (CK_X_FUNCTION_LIST *self,
		    CK_SLOT_ID slotID,
		    CK_UTF8CHAR_PTR pPin,
		    CK_ULONG ulPinLen,
		    CK_UTF8CHAR_PTR pLabel)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_InitToken (filter->lower, filter->slots[slotID],
					   pPin, ulPinLen, pLabel);
}

static CK_RV
filter_C_WaitForSlotEvent (CK_X_FUNCTION_LIST *self,
			   CK_FLAGS flags,
			   CK_SLOT_ID_PTR pSlot,
			   CK_VOID_PTR pReserved)
{
	return CKR_FUNCTION_NOT_SUPPORTED;
}

static CK_RV
filter_C_OpenSession (CK_X_FUNCTION_LIST *self,
		      CK_SLOT_ID slotID,
		      CK_FLAGS flags,
		      CK_VOID_PTR pApplication,
		      CK_NOTIFY Notify,
		      CK_SESSION_HANDLE_PTR phSession)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_OpenSession (filter->lower,
					     filter->slots[slotID], flags,
					     pApplication, Notify,
					     phSession);
}

static CK_RV
filter_C_CloseAllSessions (CK_X_FUNCTION_LIST *self,
			   CK_SLOT_ID slotID)
{
	FilterData *filter = (FilterData *)self;

	if (slotID >= filter->n_slots)
		return CKR_SLOT_ID_INVALID;

	return filter->lower->C_CloseAllSessions (filter->lower,
						  filter->slots[slotID]);
}

void
p11_filter_release (void *data)
{
	FilterData *filter = (FilterData *)data;

	return_if_fail (data != NULL);
	p11_virtual_uninit (&filter->virt);
	p11_array_free (filter->entries);
	free (filter);
}

p11_virtual *
p11_filter_subclass (p11_virtual *lower,
		     p11_destroyer destroyer)
{
	FilterData *filter;
	CK_X_FUNCTION_LIST functions;

	filter = calloc (1, sizeof (FilterData));
	return_val_if_fail (filter != NULL, NULL);

	memcpy (&functions, &p11_virtual_stack, sizeof (CK_X_FUNCTION_LIST));
	functions.C_Initialize = filter_C_Initialize;
	functions.C_Finalize = filter_C_Finalize;
	functions.C_GetSlotList = filter_C_GetSlotList;
	functions.C_GetSlotInfo = filter_C_GetSlotInfo;
	functions.C_GetTokenInfo = filter_C_GetTokenInfo;
	functions.C_GetMechanismList = filter_C_GetMechanismList;
	functions.C_GetMechanismInfo = filter_C_GetMechanismInfo;
	functions.C_InitToken = filter_C_InitToken;
	functions.C_WaitForSlotEvent = filter_C_WaitForSlotEvent;
	functions.C_OpenSession = filter_C_OpenSession;
	functions.C_CloseAllSessions = filter_C_CloseAllSessions;

	p11_virtual_init (&filter->virt, &functions, lower, destroyer);
	filter->lower = &lower->funcs;
	filter->entries = p11_array_new ((p11_destroyer)free);
	return &filter->virt;
}

void
p11_filter_allow_token (p11_virtual *virt,
			CK_TOKEN_INFO *token)
{
	FilterData *filter = (FilterData *)virt;
	CK_TOKEN_INFO *token_copy;

	return_if_fail (filter->allowed || filter->entries->num == 0);
	filter->allowed = true;

	token_copy = memdup (token, sizeof (CK_TOKEN_INFO));
	return_if_fail (token_copy != NULL);

	if (!p11_array_push (filter->entries, token_copy))
		return_if_reached ();

	if (filter->initialized)
		filter_reinit (filter);
}

void
p11_filter_deny_token (p11_virtual *virt,
		       CK_TOKEN_INFO *token)
{
	FilterData *filter = (FilterData *)virt;
	CK_TOKEN_INFO *token_copy;

	return_if_fail (!filter->allowed || filter->entries->num == 0);
	filter->allowed = false;

	token_copy = memdup (token, sizeof (CK_TOKEN_INFO));
	return_if_fail (token_copy != NULL);

	if (!p11_array_push (filter->entries, token_copy))
		return_if_reached ();

	if (filter->initialized)
		filter_reinit (filter);
}