summaryrefslogtreecommitdiff
path: root/src/certificate.c
blob: 36e6b5b82021204cc757ee5c538380867a3be342 (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

#include "p11-tests.h"

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

#include <openssl/x509.h>

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

static const char*
test_x509_name(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
               CK_ATTRIBUTE_TYPE attr_type, const char *attr_name,
               X509_NAME* compare)
{
	CK_BYTE_PTR ptr, encoded;
	CK_ATTRIBUTE attr;
	const char *msg;
	CK_RV rv;
	int len;

	/*
	 * We return any messages, so this function can be called for
	 * different X509 dns.
	 */

	attr.type = attr_type;
	attr.pValue = NULL;
	attr.ulValueLen = 0;
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	if(rv != CKR_OK)
		return p11t_msg_rv(rv);

	attr.pValue = malloc(attr.ulValueLen);
	assert(attr.pValue);

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	if(rv != CKR_OK)
		return p11t_msg_rv(rv);

	msg = p11t_certificate_validate_dn(attr.pValue, attr.ulValueLen);
	if(msg != NULL)
		return msg;

	/* Serialize the compare one */
	len = i2d_X509_NAME(compare, NULL);
	assert(len >= 0);
	encoded = malloc(len);
	assert(encoded);
	ptr = encoded;
	len = i2d_X509_NAME(compare, &ptr);
	assert(len >= 0);

	if(len != attr.ulValueLen || memcmp(encoded, attr.pValue, len) != 0)
		return "Encoding of the DN didn't match what was in certificate";

	free(attr.pValue);
	free(encoded);

	return NULL;
}

static int
test_x509_cross_search(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
                       CK_ATTRIBUTE_TYPE one, CK_ATTRIBUTE_TYPE two,
                       const char *description)
{
	CK_OBJECT_HANDLE_PTR objects;
	CK_ULONG n_objects;
	CK_ATTRIBUTE attrs[2];
	CK_ULONG i, n_attrs = 0;
	CK_BBOOL success = CK_FALSE;
	CK_RV rv;

	P11T_SECTION("CKC_X_509");

	if(one != CK_INVALID)
	{
		attrs[n_attrs].type = one;
		attrs[n_attrs].pValue = NULL;
		attrs[n_attrs].ulValueLen = 0;
		++n_attrs;
	}

	if(two != CK_INVALID)
	{
		attrs[n_attrs].type = two;
		attrs[n_attrs].pValue = NULL;
		attrs[n_attrs].ulValueLen = 0;
		++n_attrs;
	}

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, n_attrs);
	P11T_CHECK_RV("Cross find certificates by attributes", rv, CKR_OK);

	for(i = 0; i < n_attrs; ++i)
	{
		attrs[i].pValue = malloc(attrs[i].ulValueLen);
		assert(attrs[i].pValue);
	}

	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, attrs, n_attrs);
	P11T_CHECK_RV("Cross find certificates by attributes", rv, CKR_OK);

	/* Now find all the objects with the same */
	objects = p11t_object_find(session, attrs, n_attrs, &n_objects);
	for(i = 0; objects && i < n_objects; ++i)
	{
		if(objects[i] == object)
		{
			success = CK_TRUE;
			break;
		}
	}

	for(i = 0; i < n_attrs; ++i)
		free(attrs[i].pValue);

	free(objects);

	if(!success)
	{
		p11t_check_fail("Couldn't find certificates by attributes %s", description);
		return STOP;
	}

	return CONTINUE;
}

static int
test_x509_certificate(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
	CK_BYTE check[4096];
	CK_BYTE buffer[4096];
	CK_ATTRIBUTE attr;
	CK_BYTE_PTR der_value;
	CK_BYTE_PTR ptr;
	CK_ULONG n_der_value;
	CK_ULONG java_midp;
	const char* msg;
	X509* cert;
	CK_RV rv;
	int len;

	P11T_SECTION("CKC_X_509");

	/* Get the certificate DER */
	attr.type = CKA_VALUE;
	attr.ulValueLen = 0;
	attr.pValue = NULL;
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	P11T_CHECK_RV("CKA_VALUE", rv, CKR_OK);
	attr.pValue = der_value = malloc(attr.ulValueLen);
	assert(der_value);
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	P11T_CHECK_RV("CKA_VALUE", rv, CKR_OK);
	n_der_value = attr.ulValueLen;

	/* Have OpenSSL parse it */
	ptr = der_value;
	cert = d2i_X509(NULL, (const unsigned char**)&ptr, n_der_value);
	if(cert == NULL)
		P11T_CHECK_FAIL_MSG("CKA_VALUE", p11t_msg_openssl());
	if(ptr - der_value != n_der_value)
		P11T_CHECK_FAIL_MSG("CKA_VALUE", "Extra trailing bytes");


	/* Cross check the certificate with the CKA_CHECK_VALUE */
	if(p11t_test_unexpected)
	{
		attr.type = CKA_CHECK_VALUE;
		attr.pValue = buffer;
		attr.ulValueLen = sizeof(buffer);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_CHECK_VALUE", rv, CKR_OK);
		SHA1(der_value, n_der_value, check);
		if(memcmp(check, buffer, 3) != 0)
			P11T_CHECK_FAIL_MSG("CKA_CHECK_VALUE", "not equal to first 3 bytes of SHA1 hash of CKA_VALUE");
	}

	/* CKA_SUBJECT */
	msg = test_x509_name(session, object, CKA_SUBJECT, "CKA_SUBJECT", cert->cert_info->subject);
	if(msg != NULL)
		P11T_CHECK_FAIL_MSG("CKA_SUBJECT", msg);

	/* CKA_ISSUER */
	msg = test_x509_name(session, object, CKA_ISSUER, "CKA_ISSUER", cert->cert_info->issuer);
	if(msg != NULL)
		P11T_CHECK_FAIL_MSG("CKA_ISSUER", msg);

	/* CKA_SERIAL_NUMBER */
	attr.type = CKA_SERIAL_NUMBER;
	attr.pValue = check;
	attr.ulValueLen = sizeof(check);
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	P11T_CHECK_RV("CKA_SERIAL_NUMBER", rv, CKR_OK);
	ptr = buffer;
	len = i2d_ASN1_INTEGER(cert->cert_info->serialNumber, &ptr);
	assert(len >= 0);
	if(attr.ulValueLen != len || memcmp(check, buffer, len) != 0)
		P11T_CHECK_FAIL_MSG("CKA_SERIAL_NUMBER", "serial number does not match one encoded in CKA_VALUE");

	if(p11t_test_unexpected)
	{
		/* CKA_URL */
		attr.type = CKA_URL;
		attr.pValue = check;
		attr.ulValueLen = sizeof(check);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_URL", rv, CKR_OK);

		/* CKA_HASH_OF_SUBJECT_PUBLIC_KEY */
		attr.type = CKA_HASH_OF_SUBJECT_PUBLIC_KEY;
		attr.pValue = check;
		attr.ulValueLen = sizeof(check);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_HASH_OF_SUBJECT_PUBLIC_KEY", rv, CKR_OK);

		/* CKA_HASH_OF_ISSUER_PUBLIC_KEY */
		attr.type = CKA_HASH_OF_ISSUER_PUBLIC_KEY;
		attr.pValue = check;
		attr.ulValueLen = sizeof(check);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_HASH_OF_ISSUER_PUBLIC_KEY", rv, CKR_OK);

		/* CKA_JAVA_MIDP_SECURITY_DOMAIN */
		attr.type = CKA_JAVA_MIDP_SECURITY_DOMAIN;
		attr.pValue = &java_midp;
		attr.ulValueLen = sizeof(java_midp);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_JAVA_MIDP_SECURITY_DOMAIN", rv, CKR_OK);
		if(java_midp != 0 && java_midp != 1 && java_midp != 2 && java_midp != 3)
			P11T_CHECK_FAIL_MSG("CKA_JAVA_MIDP_SECURITY_DOMAIN", "Unrecognized value");

		/* CKA_ID */
		attr.type = CKA_ID;
		attr.pValue = check;
		attr.ulValueLen = sizeof(check);
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_ID", rv, CKR_OK);
		if(attr.ulValueLen == 0)
			P11T_CHECK_FAIL_MSG("CKA_ID", "zero length CKA_ID");
	}

	test_x509_cross_search(session, object, CKA_ID, CK_INVALID, "CKA_ID");
	test_x509_cross_search(session, object, CKA_SERIAL_NUMBER, CKA_ISSUER, "CKA_SERIAL_NUMBER, CKA_ISSUER");

	X509_free(cert);
	free(der_value);

	return CONTINUE;
}

static int
test_certificate_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
	CK_CERTIFICATE_TYPE cert_type;
	CK_BYTE check_value[256];
	CK_ATTRIBUTE attr;
	CK_ULONG category;
	CK_BBOOL trusted;
	CK_DATE start_date;
	CK_DATE end_date;
	CK_RV rv;

	P11T_SECTION("CKO_CERTIFICATE");

	attr.type = CKA_CERTIFICATE_TYPE;
	attr.ulValueLen = sizeof(cert_type);
	attr.pValue = &cert_type;
	rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
	P11T_CHECK_RV("CKA_CERTIFICATE_TYPE", rv, CKR_OK);

	if(p11t_test_unexpected)
	{
		attr.type = CKA_TRUSTED;
		attr.ulValueLen = sizeof(trusted);
		attr.pValue = &trusted;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_TRUSTED", rv, CKR_OK);
		P11T_CHECK_BOOL("CKA_TRUSTED", trusted);

		attr.type = CKA_CERTIFICATE_CATEGORY;
		attr.ulValueLen = sizeof(category);
		attr.pValue = &category;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_CERTIFICATE_CATEGORY", rv, CKR_OK);
		if(category != 1 && category != 2 && category != 3 && category != 0)
			P11T_CHECK_FAIL_MSG("CKA_CERTIFICATE_CATEGORY", "invalid value");

		attr.type = CKA_CHECK_VALUE;
		attr.ulValueLen = sizeof(check_value);
		attr.pValue = check_value;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_CHECK_VALUE", rv, CKR_OK);
		if(attr.ulValueLen != 3)
			P11T_CHECK_FAIL_MSG("CKA_CHECK_VALUE", "length must be 3 bytes");

		attr.type = CKA_START_DATE;
		attr.ulValueLen = sizeof(start_date);
		attr.pValue = &start_date;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_START_DATE", rv, CKR_OK);
		if(attr.ulValueLen)
			P11T_CHECK_DATE("CKA_START_DATE", &start_date);

		attr.type = CKA_END_DATE;
		attr.ulValueLen = sizeof(end_date);
		attr.pValue = &end_date;
		rv = (p11t_module_funcs->C_GetAttributeValue)(session, object, &attr, 1);
		P11T_CHECK_RV("CKA_END_DATE", rv, CKR_OK);
		if(attr.ulValueLen)
			P11T_CHECK_DATE("CKA_END_DATE", &end_date);
	}

	test_x509_certificate(session, object);

	return CONTINUE;
}

void
p11t_certificate_tests(void)
{
	CK_OBJECT_CLASS klass = CKO_CERTIFICATE;
	CK_OBJECT_HANDLE_PTR objects;
	CK_SESSION_HANDLE session;
	CK_ATTRIBUTE attrs[1];
	CK_ULONG j, i, n_objects;
	CK_SLOT_ID slot;

	for(j = 0; j < p11t_slot_count; ++j)
	{
		slot = p11t_slot_get_id(j);
		session = p11t_session_open(slot, 0);
		if(!session)
			continue;

		attrs[0].type = CKA_CLASS;
		attrs[0].ulValueLen = sizeof(klass);
		attrs[0].pValue = &klass;

		objects = p11t_object_find(session, attrs, 1, &n_objects);
		for(i = 0; objects && i < n_objects; ++i)
			test_certificate_object(session, objects[i]);
		free(objects);

		p11t_session_close(session);
	}
}

const char*
p11t_certificate_validate_dn(CK_BYTE_PTR der, CK_ULONG n_der)
{
	CK_BYTE_PTR ptr;
	X509_NAME* name;

	/* Let openssl parse it */
	ptr = der;
	name = d2i_X509_NAME(NULL, (const unsigned char**)&ptr, n_der);
	if(name == NULL)
		return p11t_msg_openssl();
	if(ptr - der != n_der)
		return "Extra trailing bytes";

	X509_NAME_free(name);

	return NULL;
}