summaryrefslogtreecommitdiff
path: root/src/object.c
blob: 1b9427aeaa4735a5a41dc29e2497c684879fc348 (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

#include "p11-tests.h"

#include <stdlib.h>
#include <string.h>

/* An very likey to be invalid attribute */
#define CKA_INVALID (CKA_VENDOR_DEFINED | 0x00FFFFFF)

/* ----------------------------------------------------------------------------------
 * TESTS
 */

static int
test_create_object(CK_SESSION_HANDLE session_rw, CK_SESSION_HANDLE session_ro)
{
	P11T_SECTION("C_CreateObject");
	P11T_CHECK_NOTE("Not Tested");
	return CONTINUE;
}

static int
test_copy_object(CK_SESSION_HANDLE session_rw, CK_SESSION_HANDLE session_ro)
{
	P11T_SECTION("C_CopyObject");
	P11T_CHECK_NOTE("Not Tested");
	return CONTINUE;
}

static int
test_destroy_object(CK_SESSION_HANDLE session_rw, CK_SESSION_HANDLE session_ro)
{
	P11T_SECTION("C_DestroyObject");
	P11T_CHECK_NOTE("Not Tested");
	return CONTINUE;
}

static int
test_find_objects(CK_SESSION_HANDLE session)
{
	CK_OBJECT_HANDLE objects[1024];
	CK_OBJECT_HANDLE extra;
	CK_ATTRIBUTE dummy;
	CK_ULONG object_count, count;
	CK_RV rv;

	assert(p11t_module_funcs);

	P11T_SECTION("C_FindObjectsInit");

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjectsInit)((CK_SESSION_HANDLE)-99, &dummy, 0);
		P11T_CHECK_RV("Invalid session", rv, CKR_SESSION_HANDLE_INVALID);

		rv = (p11t_module_funcs->C_FindObjectsInit)((CK_SESSION_HANDLE)-99, NULL, 1);
		P11T_CHECK_RV("Attribute count without buffer", rv, CKR_ARGUMENTS_BAD);

	}

	rv = (p11t_module_funcs->C_FindObjectsInit)(session, &dummy, 0);
	P11T_CHECK_RV("Find all objects", rv, CKR_OK);

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjectsInit)(session, &dummy, 0);
		P11T_CHECK_RV("Double call", rv, CKR_OPERATION_ACTIVE);

	}

	P11T_SECTION("C_FindObjects");

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjects)((CK_SESSION_HANDLE)-99, NULL, 0, &object_count);
		P11T_CHECK_RV("Invalid session", rv, CKR_SESSION_HANDLE_INVALID);

		rv = (p11t_module_funcs->C_FindObjects)(session, NULL, 0, NULL);
		P11T_CHECK_RV("Null object count", rv, CKR_ARGUMENTS_BAD);

		rv = (p11t_module_funcs->C_FindObjects)(session, objects, 1, &count);
		P11T_CHECK_RV("Retrieve a single object before remainder", rv, CKR_OK);
		if(count != 0)
			P11T_CHECK_ULONG("If asked for a single object, return one.", count, 1);
	}

	do
	{
		rv = (p11t_module_funcs->C_FindObjects)(session, objects, 1024, &count);
		P11T_CHECK_RV("Retrieve remaining objects", rv, CKR_OK);
	}
	while(count == 1024);

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjects(session, &extra, 1, &count));
		P11T_CHECK_RV("Extra call after retrieving all objects", rv, CKR_OK);
		P11T_CHECK_ULONG("Should return no objects in extra call", count, 0);
	}

	P11T_SECTION("C_FindObjectsFinal");

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjectsFinal)((CK_SESSION_HANDLE)-88);
		P11T_CHECK_RV("Invalid session", rv, CKR_SESSION_HANDLE_INVALID);
	}

	rv = (p11t_module_funcs->C_FindObjectsFinal)(session);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_FindObjectsFinal)(session);
		P11T_CHECK_RV("Extra call", rv, CKR_OPERATION_NOT_INITIALIZED);

		P11T_SECTION("C_FindObjects");

		rv = (p11t_module_funcs->C_FindObjects)(session, &extra, 1, &count);
		P11T_CHECK_RV("Out of order call", rv, CKR_OPERATION_NOT_INITIALIZED);
	}

	return CONTINUE;
}

static int
test_get_attribute_value(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
	CK_OBJECT_CLASS klass;
	CK_ATTRIBUTE attrs[5];
	CK_BYTE buffer[8];
	CK_BBOOL bools[3];
	CK_RV rv;

	assert(p11t_module_funcs);

	P11T_SECTION("C_GetAttributeValue");

	attrs[0].type = CKA_CLASS;

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_GetAttributeValue)((CK_SESSION_HANDLE)-57, object, attrs, 1);
		P11T_CHECK_RV("Invalid session", rv, CKR_SESSION_HANDLE_INVALID);

		rv = (p11t_module_funcs->C_GetAttributeValue)(session, (CK_OBJECT_HANDLE)-58, attrs, 1);
		P11T_CHECK_RV("Invalid object", rv, CKR_OBJECT_HANDLE_INVALID);

		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, NULL, 1);
		P11T_CHECK_RV("No template", rv, CKR_ARGUMENTS_BAD);

		attrs[0].pValue = buffer;
		attrs[0].ulValueLen = 2;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 1);
		P11T_CHECK_RV("Buffer too small", rv, CKR_BUFFER_TOO_SMALL);
		P11T_CHECK_ULONG("Buffer too small should return size", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));
	}

	attrs[0].pValue = NULL;
	attrs[0].ulValueLen = 0;
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 1);
	P11T_CHECK_RV("Retrieve attribute length", rv, CKR_OK);
	P11T_CHECK_ULONG("Size of object class attribute.", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));

	attrs[0].pValue = buffer;
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 1);
	P11T_CHECK_RV("Retrieve single attribute", rv, CKR_OK);

	/* The actual class of this object */
	klass = *((CK_OBJECT_CLASS*)attrs[0].pValue);

	if(p11t_test_unexpected)
	{
		/*
		 * Now we do a bit of testing to retrieve a valid and invalid attribute
		 * at the same time.
		 */

		attrs[0].type = CKA_INVALID;
		attrs[1].type = CKA_CLASS;
		attrs[0].ulValueLen = attrs[1].ulValueLen = 0;
		attrs[0].pValue = attrs[0].pValue = NULL;

		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 2);
		P11T_CHECK_RV("With one invalid attribute, no buffer", rv, CKR_ATTRIBUTE_TYPE_INVALID);
		P11T_CHECK_ULONG("Should set invalid attribute to -1", attrs[0].ulValueLen, CK_INVALID);
		P11T_CHECK_ULONG("Should set valid attribute to size", attrs[1].ulValueLen, sizeof(CK_OBJECT_CLASS));

		attrs[1].pValue = buffer;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 2);
		P11T_CHECK_RV("With one invalid attribute, with buffer", rv, CKR_ATTRIBUTE_TYPE_INVALID);
		P11T_CHECK_ULONG("Should set invalid attribute to -1", attrs[0].ulValueLen, CK_INVALID);
		P11T_CHECK_ULONG("Should set attribute size, in partially valid set", attrs[1].ulValueLen, sizeof(CK_OBJECT_CLASS));
		P11T_CHECK_ULONG("Should set valid attribute, in partially valid set", *((CK_OBJECT_CLASS*)attrs[1].pValue), klass);
	}

	/*
	 * Now we do a bit of testing of other attributes and retrieving sets.
	 * Since we can't list which attributes an object has, we only
	 * work with 'storage' type attributes and their standard 4 attributes.
	 */

	switch(klass)
	{
	case CKO_CERTIFICATE:
	case CKO_DATA:
	case CKO_PRIVATE_KEY:
	case CKO_PUBLIC_KEY:
	case CKO_SECRET_KEY:
		break;
	default:
		return CONTINUE;
	}

	memset(attrs, 0, sizeof(attrs));
	attrs[0].type = CKA_CLASS;
	attrs[1].type = CKA_TOKEN;
	attrs[2].type = CKA_PRIVATE;
	attrs[3].type = CKA_MODIFIABLE;
	attrs[4].type = CKA_LABEL;

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 5);
	P11T_CHECK_RV("Multiple attributes, no buffer", rv, CKR_OK);
	P11T_CHECK_ULONG("Size of CKA_CLASS", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));
	P11T_CHECK_ULONG("Size of CKA_TOKEN", attrs[1].ulValueLen, sizeof(CK_BBOOL));
	P11T_CHECK_ULONG("Size of CKA_PRIVATE", attrs[2].ulValueLen, sizeof(CK_BBOOL));
	P11T_CHECK_ULONG("Size of CKA_MODIFIABLE", attrs[3].ulValueLen, sizeof(CK_BBOOL));

	if(attrs[4].ulValueLen)
		attrs[4].pValue = malloc(attrs[4].ulValueLen);
	attrs[3].pValue = &bools[0];
	attrs[2].pValue = &bools[1];
	attrs[1].pValue = &bools[2];

	if(p11t_test_unexpected)
	{
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 5);
		P11T_CHECK_RV("Multiple attributes, some buffers", rv, CKR_OK);
		P11T_CHECK_ULONG("Size of CKA_CLASS", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));
		P11T_CHECK_BOOL("CKA_TOKEN boolean value", *((CK_BBOOL*)attrs[1].pValue));
		P11T_CHECK_BOOL("CKA_PRIVATE boolean value", *((CK_BBOOL*)attrs[2].pValue));
		P11T_CHECK_BOOL("CKA_MODIFIABLE boolean value", *((CK_BBOOL*)attrs[3].pValue));

		attrs[0].pValue = buffer;
		attrs[0].ulValueLen = 2;

		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 5);
		P11T_CHECK_RV("Multiple attributes, one small buffer", rv, CKR_BUFFER_TOO_SMALL);
		P11T_CHECK_ULONG("Size of CKA_CLASS", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));
		P11T_CHECK_BOOL("CKA_TOKEN boolean value", *((CK_BBOOL*)attrs[1].pValue));
		P11T_CHECK_BOOL("CKA_PRIVATE boolean value", *((CK_BBOOL*)attrs[2].pValue));
		P11T_CHECK_BOOL("CKA_MODIFIABLE boolean value", *((CK_BBOOL*)attrs[3].pValue));
	}

	attrs[0].pValue = buffer;
	attrs[0].ulValueLen = sizeof(CK_OBJECT_CLASS);

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, 5);
	P11T_CHECK_RV("Multiple attributes, with buffers", rv, CKR_OK);
	P11T_CHECK_ULONG("Size of CKA_CLASS", attrs[0].ulValueLen, sizeof(CK_OBJECT_CLASS));
	P11T_CHECK_ULONG("CKA_CLASS value", *((CK_ULONG*)attrs[0].pValue), klass);
	P11T_CHECK_BOOL("CKA_TOKEN boolean value", *((CK_BBOOL*)attrs[1].pValue));
	P11T_CHECK_BOOL("CKA_PRIVATE boolean value", *((CK_BBOOL*)attrs[2].pValue));
	P11T_CHECK_BOOL("CKA_MODIFIABLE boolean value", *((CK_BBOOL*)attrs[3].pValue));
	P11T_CHECK_STRING("CKA_LABEL value", attrs[4].pValue, attrs[4].ulValueLen);

	return CONTINUE;
}

static int
test_objects(CK_SESSION_HANDLE session)
{
	CK_OBJECT_HANDLE_PTR objects;
	CK_ULONG n_objects;
	CK_ULONG i;

	assert(p11t_module_funcs);

	objects = p11t_object_find(session, NULL, 0, &n_objects);
	if(!objects || !n_objects)
		return CONTINUE;

	for(i = 0; i < n_objects; ++i)
	{
		P11T_SECTION("C_GetObjectSize");
		P11T_CHECK_NOTE("Not Tested");

		test_get_attribute_value(session, objects[i]);
	}

	free (objects);
	return CONTINUE;
}

void
p11t_object_tests(void)
{
	CK_SESSION_HANDLE session_rw;
	CK_SESSION_HANDLE session_ro;
	CK_SLOT_ID slot;
	CK_ULONG i;

	for(i = 0; i < p11t_slot_count; ++i)
	{
		slot = p11t_slot_get_id(i);
		session_rw = p11t_session_open(slot, 1);
		session_ro = p11t_session_open(slot, 0);

		/* We need to have the readonly session and log in for tests to proceed */
		if(session_ro == CK_INVALID)
			continue;

		test_find_objects(session_ro);
		test_objects(session_ro);

		if(!p11t_session_login(session_ro))
			continue;

		test_find_objects(session_ro);
		test_objects(session_ro);

		if(session_rw != CK_INVALID)
		{
			test_find_objects(session_rw);
			test_objects(session_rw);
			test_create_object(session_rw, session_ro);
			test_copy_object(session_rw, session_ro);
			test_destroy_object(session_rw, session_ro);
		}

		p11t_session_close_all(slot);
	}
}

/* ----------------------------------------------------------------------------------
 * METHODS
 */

CK_OBJECT_HANDLE_PTR
p11t_object_find(CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR attrs,
                 CK_ULONG n_attrs, CK_ULONG_PTR n_objects)
{
	CK_OBJECT_HANDLE_PTR objects;
	CK_ATTRIBUTE dummy_attr;

	assert(p11t_module_funcs);

	if(!attrs)
		attrs = &dummy_attr;

	if((p11t_module_funcs->C_FindObjectsInit)(session, attrs, n_attrs) != CKR_OK)
		return NULL;

	objects = calloc(1024, sizeof(CK_OBJECT_HANDLE));
	if((p11t_module_funcs->C_FindObjects)(session, objects, 1024, n_objects) != CKR_OK)
	{
		free(objects);
		return NULL;
	}

	if((p11t_module_funcs->C_FindObjectsFinal)(session) != CKR_OK)
	{
		free(objects);
		return NULL;
	}

	if(!*n_objects)
	{
		free(objects);
		return NULL;
	}

	return objects;
}

CK_OBJECT_HANDLE
p11t_object_find_one(CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR attrs,
                     CK_ULONG n_attrs)
{
	CK_OBJECT_HANDLE object;
	CK_ULONG count;

	assert(p11t_module_funcs);

	if((p11t_module_funcs->C_FindObjectsInit)(session, attrs, n_attrs) != CKR_OK)
		return CK_INVALID;

	if((p11t_module_funcs->C_FindObjects)(session, &object, 1, &count) != CKR_OK)
		return CK_INVALID;

	if(count != 1)
		return CK_INVALID;

	return object;
}


int
p11t_object_get(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
                CK_ATTRIBUTE_PTR attrs, CK_ULONG count)
{
	CK_RV rv;

	assert(p11t_module_funcs);

	if(!count)
		return 1;

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, count);
	switch(rv)
	{
	case CKR_OK:
	case CKR_BUFFER_TOO_SMALL:
	case CKR_ATTRIBUTE_TYPE_INVALID:
	case CKR_ATTRIBUTE_SENSITIVE:
		return 1;
	}

	return 0;
}