summaryrefslogtreecommitdiff
path: root/src/rsa.c
blob: fa3d37206fdfa97eeb4c87ae67ff8b46a2b74fa3 (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448

#include "p11-tests.h"

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

#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/objects.h>
#include <openssl/x509.h>

static int
test_rsa_decrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
                 CK_MECHANISM_TYPE mech_type, RSA* rsa)
{
	CK_BYTE encrypted[P11T_BLOCK];
	CK_BYTE decrypted[P11T_BLOCK];
	CK_MECHANISM mech;
	const CK_BYTE* data;
	CK_ULONG n_data, n_decrypted;
	int size, n_encrypted;
	CK_RV rv;

	data = p11t_test_data;
	n_data = p11t_test_data_size;

	size = RSA_size(rsa);
	assert(size);
	assert(size < sizeof(encrypted));
	assert(size < sizeof(decrypted));
	assert(size < (int)n_data);

	switch(mech_type)
	{
	case CKM_RSA_PKCS:
		P11T_CHECK_NOTE("CKM_RSA_PKCS");
		n_data = size - 11;
		n_encrypted = RSA_public_encrypt(n_data, data, encrypted, rsa, RSA_PKCS1_PADDING);
		assert(n_encrypted == size);
		break;
	case CKM_RSA_X_509:
		P11T_CHECK_NOTE("CKM_RSA_X_509");
		n_data = size;
		n_encrypted = RSA_public_encrypt(n_data, data, encrypted, rsa, RSA_NO_PADDING);
		assert(n_encrypted == size);
		break;
	default:
		return CONTINUE;
	};

	mech.mechanism = mech_type;
	mech.pParameter = NULL;
	mech.ulParameterLen = 0;

	P11T_SECTION("C_DecryptInit");

	rv = (p11t_module_funcs->C_DecryptInit)(session, &mech, key);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	P11T_SECTION("C_Decrypt");

	n_decrypted = sizeof(decrypted);
	rv = (p11t_module_funcs->C_Decrypt)(session, encrypted, n_encrypted, decrypted, &n_decrypted);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	if(n_decrypted != n_data)
		P11T_CHECK_FAIL("RSA decrypt failed, wrong length");
	if(memcmp(data, decrypted, n_data) != 0)
		P11T_CHECK_FAIL("RSA decrypt failed, mangled data");

	return CONTINUE;
}

static void
hash_for_rsa_ssl3_sign(const CK_BYTE* data, CK_ULONG n_data,
                       CK_BYTE_PTR output, CK_ULONG_PTR n_output)
{
	assert(*n_output >= MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH);
	MD5(data, n_data, output);
	SHA1(data, n_data, output + MD5_DIGEST_LENGTH);
	*n_output = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
}

static void
hash_for_rsa_pkcs_sign(int algo, int wrap, const CK_BYTE* data,
                       CK_ULONG n_data, CK_BYTE_PTR output, CK_ULONG_PTR n_output)
{
	CK_BYTE hash[EVP_MAX_MD_SIZE];
	X509_SIG sig;
	X509_ALGOR algor;
	ASN1_TYPE parameter;
	ASN1_OCTET_STRING digest;
	EVP_MD_CTX ctx;
	const EVP_MD* evp;
	size_t n_hash;
	int val;

	if(algo == NID_md5_sha1)
	{
		hash_for_rsa_ssl3_sign(data, n_data, output, n_output);
		return;
	}

	evp = EVP_get_digestbynid(algo);
	assert(evp);

	val = EVP_DigestInit(&ctx, evp);
	assert(val >= 0);
	EVP_DigestUpdate(&ctx, data, n_data);
	EVP_DigestFinal(&ctx, hash, &n_hash);

	/* Not wrapping? */
	if(!wrap)
	{
		assert(*n_output >= n_hash);
		*n_output = (CK_ULONG)n_hash;
		memcpy(output, hash, n_hash);
		return;
	}

	sig.algor = &algor;
	sig.algor->algorithm = OBJ_nid2obj(algo);
	assert(sig.algor->algorithm);
	assert(sig.algor->algorithm->length);
	parameter.type = V_ASN1_NULL;
	parameter.value.ptr = NULL;
	sig.algor->parameter = &parameter;
	sig.digest = &digest;
	sig.digest->data = hash;
	sig.digest->length = (int)n_hash;

	val = i2d_X509_SIG(&sig, &output);
	assert(*n_output >= (CK_ULONG)val);
	*n_output = val;
}

static int
test_rsa_pkcs_sign_hash(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
                        RSA* rsa, int algo)
{
	CK_BYTE hash[P11T_BLOCK];
	CK_BYTE sig[P11T_BLOCK];
	CK_MECHANISM mech;
	CK_ULONG n_hash, n_sig;
	CK_RV rv;
	int res;

	/* Hash the data with appropriate wrappers */
	n_hash = sizeof(hash);
	hash_for_rsa_pkcs_sign(algo, 1, p11t_test_data, p11t_test_data_size, hash, &n_hash);

	mech.mechanism = CKM_RSA_PKCS;
	mech.pParameter = NULL;
	mech.ulParameterLen = 0;

	P11T_SECTION("C_SignInit");

	rv = (p11t_module_funcs->C_SignInit)(session, &mech, key);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	P11T_SECTION("C_Sign");

	n_sig = sizeof(sig);
	rv = (p11t_module_funcs->C_Sign)(session, hash, n_hash, sig, &n_sig);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	/* Hash the data again this time without wrapping */
	n_hash = sizeof(hash);
	hash_for_rsa_pkcs_sign(algo, 0, p11t_test_data, p11t_test_data_size, hash, &n_hash);

	/* Verify the signature */
	res = RSA_verify(algo, hash, n_hash, sig, n_sig, rsa);
	if(res != 1)
		P11T_CHECK_FAIL("RSA PKCS#1.5 or SSLv3 signature did not verify");

	return CONTINUE;
}

static int
test_rsa_x509_sign(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key, RSA* rsa)
{
	const CK_BYTE* data;
	CK_BYTE sig[P11T_BLOCK];
	CK_BYTE check[P11T_BLOCK];
	CK_MECHANISM mech;
	CK_ULONG n_data, n_sig;
	CK_RV rv;
	int res, size;

	size = RSA_size(rsa);
	data = p11t_test_data;
	n_data = size / 2;
	assert(n_data <= p11t_test_data_size);

	mech.mechanism = CKM_RSA_X_509;
	mech.pParameter = NULL;
	mech.ulParameterLen = 0;

	P11T_SECTION("C_SignInit");

	rv = (p11t_module_funcs->C_SignInit)(session, &mech, key);
	P11T_CHECK_RV("RSA X509 Call", rv, CKR_OK);

	P11T_SECTION("C_Sign");

	n_sig = sizeof(sig);
	rv = (p11t_module_funcs->C_Sign)(session, (CK_BYTE*)data, n_data, sig, &n_sig);
	P11T_CHECK_RV("RSA X509 Call", rv, CKR_OK);
	P11T_CHECK_ULONG("C_Sign: rsa x509 result length", n_sig, size);

	res = RSA_public_decrypt(n_sig, sig, check, rsa, RSA_NO_PADDING);
	if(res < 0)
		P11T_CHECK_FAIL("RSA x509 signature was invalid");

	assert(res > (int)n_data);
	if(memcmp(check + (res - n_data), data, n_data) != 0)
		P11T_CHECK_FAIL("RSA x509 signature did not verify");

	return CONTINUE;
}

static int
test_rsa_sign(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
              CK_MECHANISM_TYPE mech_type, RSA* rsa)
{
	P11T_SECTION("C_Sign");

	switch(mech_type)
	{
	case CKM_RSA_PKCS:
		P11T_CHECK_NOTE("CKM_RSA_PKCS (SHA1)");
		test_rsa_pkcs_sign_hash(session, key, rsa, NID_sha1);
		P11T_CHECK_NOTE("CKM_RSA_PKCS (MD5)");
		test_rsa_pkcs_sign_hash(session, key, rsa, NID_md5);
		P11T_CHECK_NOTE("CKM_RSA_PKCS (SHA1/MD5/SSL3)");
		test_rsa_pkcs_sign_hash(session, key, rsa, NID_md5_sha1);
		break;
	case CKM_RSA_X_509:
		P11T_CHECK_NOTE("CKM_RSA_X_509");
		test_rsa_x509_sign(session, key, rsa);
		break;
	};

	return CONTINUE;
}

static int
test_rsa_private_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
                     CK_MECHANISM_TYPE mech_type)
{
	CK_ATTRIBUTE attrs[2];
	CK_BBOOL can_decrypt = 0;
	CK_BBOOL can_sign = 0;
	RSA *rsa;

	attrs[0].type = CKA_DECRYPT;
	attrs[0].ulValueLen = sizeof(CK_BBOOL);
	attrs[0].pValue = &can_decrypt;
	attrs[1].type = CKA_SIGN;
	attrs[1].ulValueLen = sizeof(CK_BBOOL);
	attrs[1].pValue = &can_sign;

	if(!p11t_object_get(session, key, attrs, 2))
		return CONTINUE;

	rsa = p11t_key_export_public_rsa(session, key);
	if(!rsa)
		return CONTINUE;

	if(can_decrypt)
		test_rsa_decrypt(session, key, mech_type, rsa);

	if(can_sign)
		test_rsa_sign(session, key, mech_type, rsa);

	RSA_free(rsa);

	return CONTINUE;
}

static int
test_rsa_encrypt(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
                 CK_MECHANISM_TYPE mech_type, RSA* rsa)
{
	CK_BYTE encrypted[P11T_BLOCK];
	CK_BYTE check[P11T_BLOCK];
	CK_OBJECT_HANDLE privkey;
	CK_MECHANISM mech;
	const CK_BYTE* data;
	CK_ULONG n_data, n_check, n_encrypted;
	int size;
	CK_RV rv;

	data = p11t_test_data;
	n_data = p11t_test_data_size;

	P11T_SECTION("C_Encrypt");

	size = RSA_size(rsa);
	assert(size);
	assert(size < sizeof(encrypted));
	assert(size < sizeof(check));
	assert(size < (int)n_data);

	switch(mech_type)
	{
	case CKM_RSA_PKCS:
		P11T_CHECK_NOTE("CKM_RSA_PKCS");
		n_data = size - 11;
		break;
	case CKM_RSA_X_509:
		P11T_CHECK_NOTE("CKM_RSA_X_509");
		n_data = size;
		break;
	default:
		return CONTINUE;
	};

	mech.mechanism = mech_type;
	mech.pParameter = NULL;
	mech.ulParameterLen = 0;

	P11T_SECTION("C_EncryptInit");

	/* Now ask PKCS#11 to decrypt it */
	rv = (p11t_module_funcs->C_EncryptInit)(session, &mech, key);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	P11T_SECTION("C_Encrypt");

	n_encrypted = sizeof(encrypted);
	rv = (p11t_module_funcs->C_Encrypt)(session, (CK_BYTE*)data, n_data, encrypted, &n_encrypted);
	P11T_CHECK_RV("Normal call", rv, CKR_OK);

	if(size != n_encrypted)
		P11T_CHECK_FAIL("RSA encrypt failed, wrong length");

	/* We need to find a private key in order to validate */
	privkey = p11t_key_get_private(session, key);
	if(privkey == CK_INVALID)
		return CONTINUE;

	P11T_SECTION("C_DecryptInit");

	rv = (p11t_module_funcs->C_DecryptInit)(session, &mech, privkey);
	P11T_CHECK_RV("Module encrypted data", rv, CKR_OK);

	P11T_SECTION("C_Decrypt");

	n_check = sizeof(check);
	rv = (p11t_module_funcs->C_Decrypt)(session, encrypted, n_encrypted, check, &n_check);
	P11T_CHECK_RV("Module encrypted data", rv, CKR_OK);

	if(n_check != n_data)
		P11T_CHECK_FAIL("RSA validate failed, wrong length");
	if(memcmp(data, check, n_data) != 0)
		P11T_CHECK_FAIL("RSA validate failed, bad data");

	return CONTINUE;
}

static int
test_rsa_public_key(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key,
                    CK_MECHANISM_TYPE mech_type)
{
	CK_ATTRIBUTE attrs[2];
	CK_BBOOL can_encrypt = 0;
	CK_BBOOL can_verify = 0;
	RSA *rsa;

	attrs[0].type = CKA_ENCRYPT;
	attrs[0].ulValueLen = sizeof(CK_BBOOL);
	attrs[0].pValue = &can_encrypt;
	attrs[1].type = CKA_VERIFY;
	attrs[1].ulValueLen = sizeof(CK_BBOOL);
	attrs[1].pValue = &can_verify;

	if(!p11t_object_get(session, key, attrs, 2))
		return CONTINUE;

	rsa = p11t_key_export_public_rsa(session, key);
	if(!rsa)
		return CONTINUE;

	if(can_encrypt)
		test_rsa_encrypt(session, key, mech_type, rsa);

	RSA_free(rsa);

	return CONTINUE;
}

static void
test_rsa_pkcs(CK_SLOT_ID slot, CK_MECHANISM_TYPE mech, CK_MECHANISM_INFO_PTR info)
{
	CK_SESSION_HANDLE session;
	CK_ATTRIBUTE attrs[2];
	CK_ATTRIBUTE attr;
	CK_OBJECT_CLASS klass;
	CK_KEY_TYPE key_type = CKK_RSA;
	CK_BBOOL token = CK_TRUE;
	CK_OBJECT_HANDLE_PTR keys;
	CK_ULONG i, n_keys;

	/* Find all keys that match this mechanism */
	attrs[0].type = CKA_TOKEN;
	attrs[0].ulValueLen = sizeof(token);
	attrs[0].pValue = &token;
	attrs[1].type = CKA_KEY_TYPE;
	attrs[1].ulValueLen = sizeof(key_type);
	attrs[1].pValue = &key_type;

	session = p11t_session_open(slot, 0);
	if(session == CK_INVALID)
		return;

	if(!p11t_session_login(session))
		return;

	keys = p11t_object_find(session, attrs, 2, &n_keys);
	if(!keys)
		return;

	for(i = 0; i < n_keys; ++i)
	{
		attr.type = CKA_CLASS;
		attr.ulValueLen = sizeof(klass);
		attr.pValue = &klass;

		if(p11t_object_get(session, keys[i], &attr, 1))
		{
			if(klass == CKO_PRIVATE_KEY)
				test_rsa_private_key(session, keys[i], mech);
			else if(klass == CKO_PUBLIC_KEY)
				test_rsa_public_key(session, keys[i], mech);
		}
	}

	free(keys);
}

void
p11t_rsa_tests(void)
{
	p11t_slot_for_each_mech(CKM_RSA_PKCS, test_rsa_pkcs);
}