summaryrefslogtreecommitdiff
path: root/ckcapi-token.c
blob: c9b6f348078ee68da5ba7066d754ab480860af33 (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
/* 
 * Copyright (C) 2007 Stef Walter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "ckcapi.h"
#include "ckcapi-object.h"
#include "ckcapi-token.h"

static CkCapiArray* object_array = NULL;
static CkCapiHash* object_hash = NULL;
static CkCapiArray* logged_in_slots = NULL;

typedef struct _SlotInfo
{
	const char* capi_store;
	const char* display_name;
	CK_ULONG slot_flags;
}
SlotInfo;

#define SLOT_OFFSET  0x00001000

static SlotInfo slot_info[] = {
	{ "My", "Personal Certificates", CKCAPI_SLOT_TRUSTED | CKCAPI_SLOT_CERTS },
	{ "AddressBook", "Address Book Certificates", CKCAPI_SLOT_CERTS },
	{ "CA", "Certificate Authorities", CKCAPI_SLOT_CA | CKCAPI_SLOT_CERTS },  
	{ "Root", "Root Authorities", CKCAPI_SLOT_TRUSTED | CKCAPI_SLOT_CA | CKCAPI_SLOT_CERTS }, 
	{ "Trust", "Trust", CKCAPI_SLOT_CERTS }, 
	{ "TrustedPeople", "Trusted People", CKCAPI_SLOT_TRUSTED | CKCAPI_SLOT_CERTS }, 
	{ "AuthRoot", "Auth Root", CKCAPI_SLOT_CERTS },
	{ NULL, "All User Keys", CKCAPI_SLOT_ANYKEY }
};

#define SLOT_TO_OFFSET(slot) \
	((slot) & ~(SLOT_OFFSET))

#define OFFSET_TO_SLOT(offset) \
	((offset) | SLOT_OFFSET)

unsigned int 
ckcapi_token_get_count(void)
{
	return sizeof(slot_info) / sizeof(slot_info[0]);
}

CK_SLOT_ID
ckcapi_token_get_slot_id(unsigned int offset)
{
	ASSERT(offset < ckcapi_token_get_count());
	return OFFSET_TO_SLOT(offset);
}

CK_BBOOL
ckcapi_token_is_valid(CK_SLOT_ID slot)
{
	unsigned int offset = SLOT_TO_OFFSET(slot);
	return offset >= 0 && offset < ckcapi_token_get_count();
}

const char*
ckcapi_token_get_display_name(CK_SLOT_ID slot)
{
	unsigned int offset = SLOT_TO_OFFSET(slot);
	ASSERT(ckcapi_token_is_valid(slot));
	ASSERT(slot_info[offset].display_name); 
	return slot_info[offset].display_name;
}

const char*
ckcapi_token_get_store_name(CK_SLOT_ID slot)
{
	unsigned int offset = SLOT_TO_OFFSET(slot);
	ASSERT(ckcapi_token_is_valid(slot));
	return slot_info[offset].capi_store;
}

CK_ULONG
ckcapi_token_get_flags(CK_SLOT_ID slot)
{
	unsigned int offset = SLOT_TO_OFFSET(slot);
	ASSERT(ckcapi_token_is_valid(slot));
	return slot_info[offset].slot_flags;
}

static void 
object_free(CkCapiObject* obj)
{
	ASSERT(obj);
	ASSERT(obj->obj_funcs);
	ASSERT(obj->obj_funcs->release);
	(obj->obj_funcs->release)(obj);
}

void
ckcapi_token_cleanup_all(void)
{
	size_t i;

	ckcapi_lock_global();

		if(object_hash)
		{
			ckcapi_hash_free(object_hash, NULL);
			object_hash = NULL;
		}

		if(object_array)
		{
			for(i = 1; i < object_array->len; ++i)
			{
				ASSERT(ckcapi_array_index(object_array, CkCapiObject*, i));
				object_free(ckcapi_array_index(object_array, CkCapiObject*, i));
			}

			ckcapi_array_free(object_array, TRUE);
			object_array = NULL;
		}

		if(logged_in_slots)
		{
			ckcapi_array_free(logged_in_slots, TRUE);
			logged_in_slots = NULL;
		}

	ckcapi_unlock_global();
}

CK_OBJECT_HANDLE
ckcapi_token_get_max_handle(void)
{
	if(!object_array)
		return 0;
	return object_array->len;
}

CkCapiObject*
ckcapi_token_lookup_object(CK_SLOT_ID slot, CK_OBJECT_HANDLE obj)
{
	/* This must be called without any locks held */

	CkCapiObject* ret = NULL;

	ASSERT(slot);
	ASSERT(obj > 0);
	
	ckcapi_lock_global();
	
		if(object_array && obj < object_array->len)
			ret = ckcapi_array_index(object_array, CkCapiObject*, obj);

	ckcapi_unlock_global();

	/* Must belong to the right slot */
	if(ret && ret->slot != slot)
		ret = NULL;

	return ret;
}

static unsigned int
object_hash_func(const void* a)
{
	CkCapiObject* obj = (CkCapiObject*)a;
	unsigned int hash = ckcapi_hash_pointer(obj->obj_funcs);
	hash ^= (obj->obj_funcs->hash_object)(obj);
	return hash;
}

static int
object_equal_func(const void* a, const void* b)
{
	CkCapiObject* ca = (CkCapiObject*)a;
	CkCapiObject* cb = (CkCapiObject*)b;
	if(ca == cb)
		return 1;
	if(ca->obj_funcs != cb->obj_funcs)
		return 0;
	return (ca->obj_funcs->equal_object)(ca, cb);
}

CK_RV
ckcapi_token_register_object(CK_SLOT_ID slot, CkCapiObject* obj)
{
	CkCapiObject* prev;
	CK_RV ret = CKR_OK;

	ASSERT(slot);
	ASSERT(obj->id == 0);

	DBG(("registering object"));

	ckcapi_lock_global();

		if(!object_array)
		{
			object_array = ckcapi_array_sized_new(0, 1, sizeof(CkCapiObject*), 16);
			if(object_array) 
			{
				/* A blank entry for '0' */
				CkCapiObject* blank = NULL;
				ckcapi_array_append(object_array, blank);
			}

			object_hash = ckcapi_hash_new(object_hash_func, object_equal_func);

			if(!object_array || !object_hash)
			{
				/* Allocation failed above */
				ret = CKR_HOST_MEMORY;
			}
		}

		if(ret == CKR_OK)
		{
			ASSERT(object_array);
			ASSERT(object_hash);

			/* Look in the hash and find a previous object */
			prev = ckcapi_hash_get(object_hash, obj);
			if(prev)
			{
				/* Register it in the previous object's place */
				obj->id = prev->id;
				ASSERT(prev->id < object_array->len);
				if(ckcapi_hash_set(object_hash, obj, obj))
				{
					ckcapi_array_index(object_array, CkCapiObject*, obj->id) = obj;
					object_free(prev);
					DBGO(obj, "found old object id");
				}
				else
				{
					ret = CKR_HOST_MEMORY;
				}
			}
			else
			{
				/* Register it at the end of the array */
				obj->id = object_array->len;
				ASSERT(obj->id > 0);
				if(ckcapi_hash_set(object_hash, obj, obj))
				{
					if(ckcapi_array_append(object_array, obj))
					{
						DBGO(obj, "registered new object id");
					}
					else
					{
						ret = CKR_HOST_MEMORY;

						/* Roll back our addition */
						ckcapi_hash_rem(object_hash, obj);
					}
				}
				else
				{
					ret = CKR_HOST_MEMORY;
				}
			}
		}

		if(ret == CKR_OK)
			obj->slot = slot;

	ckcapi_unlock_global();

	return ret;

}

CK_BBOOL
ckcapi_token_is_logged_in(CK_SLOT_ID slot)
{
	unsigned int count, offset;

	ASSERT(ckcapi_token_is_valid(slot));

	if(!logged_in_slots)
		return CK_FALSE;

	offset = SLOT_TO_OFFSET(slot);
	count = ckcapi_token_get_count();

	ASSERT(logged_in_slots->len == count && offset < count);
	return ckcapi_array_index(logged_in_slots, CK_BBOOL, offset);
}

CK_RV
ckcapi_token_login(CK_SLOT_ID slot)
{
	unsigned int i, count;
	unsigned int offset;
	CK_BBOOL value;

	ASSERT(ckcapi_token_is_valid(slot));

	offset = SLOT_TO_OFFSET(slot);
	count = ckcapi_token_get_count();

	if(!logged_in_slots)
	{
		logged_in_slots = ckcapi_array_sized_new(0, 1, sizeof(CK_BBOOL), count);
		if(!logged_in_slots)
			return CKR_HOST_MEMORY;

		value = CK_FALSE;
		for(i = 0; i < count; ++i)
			ckcapi_array_append(logged_in_slots, value);

	}

	ASSERT(logged_in_slots->len == count && offset < count);
	if(ckcapi_array_index(logged_in_slots, CK_BBOOL, offset))
		return CKR_USER_ALREADY_LOGGED_IN;

	ckcapi_array_index(logged_in_slots, CK_BBOOL, offset) = CK_TRUE;
	return CKR_OK;
}

CK_RV
ckcapi_token_logout(CK_SLOT_ID slot)
{
	unsigned int count, offset;

	ASSERT(ckcapi_token_is_valid(slot));

	if(!logged_in_slots)
		return CKR_USER_NOT_LOGGED_IN;

	offset = SLOT_TO_OFFSET(slot);
	count = ckcapi_token_get_count();

	ASSERT(logged_in_slots->len == count && offset < count);
	if(!ckcapi_array_index(logged_in_slots, CK_BBOOL, offset))
		return CKR_USER_NOT_LOGGED_IN;

	ckcapi_array_index(logged_in_slots, CK_BBOOL, offset) = CK_FALSE;
	return CKR_OK;
}