summaryrefslogtreecommitdiff
path: root/p11-capi-rsa.c
blob: 18fc4eba503b02c5fdd6edbbd7e77524538c70da (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
/* 
 * Copyright (C) 2008 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 "p11-capi.h"
#include "p11-capi-der.h"
#include "p11-capi-key.h"
#include "p11-capi-object.h"

/* 
 * Portions derived from NSS source files: 
 *     lib/ckfw/capi/crsa.c
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Netscape security libraries.
 *
 * The Initial Developer of the Original Code is
 * Red Hat, Inc.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Bob Relyea (rrelyea@redhat.com)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#define SSL3_SHAMD5_HASH_SIZE  36 /* LEN_MD5 (16) + LEN_SHA1 (20) */

/*
 * PKCS #11 sign for RSA expects to take a fully DER-encoded hash value, 
 * which includes the hash OID. CAPI expects to take a Hash Context. While 
 * CAPI does have the capability of setting a raw hash value, it does not 
 * have the ability to sign an arbitrary value. This function tries to
 * reduce the passed in data into something that CAPI could actually sign.
 */
static CK_BYTE_PTR
parse_rsa_pkcs_der_hash(CK_BYTE_PTR input, CK_ULONG n_input,
                        ALG_ID* algorithm, CK_ULONG_PTR n_hash)
{
	BYTE* algid;
	BYTE* oid;
	BYTE* hash_data;
	BYTE* oid_str;
	DWORD n_oid;
	DWORD n_algid;

	/*
	 * there are 2 types of hashes NSS typically tries to sign, regular
	 * RSA signature format (with encoded DER_OIDS), and SSL3 Signed hashes.
	 * CAPI knows not to add any oids to SSL3_Signed hashes, so if we have any
	 * random hash that is exactly the same size as an SSL3 hash, then we can
	 * just pass the data through. CAPI has know way of knowing if the value
	 * is really a combined hash or some other arbitrary data, so it's safe to
	 * handle this case first.
	 */
	if(SSL3_SHAMD5_HASH_SIZE == n_input)
	{
		*n_hash = n_input;
		*algorithm = CALG_SSL3_SHAMD5;
		return input;
	}

	/* make sure we have a sequence tag */
	if((P11C_DER_SEQUENCE | P11C_DER_CONSTRUCTED) != *input)
		return NULL;

	/* 
	 * parse the input block to get 1) the hash oid, and 2) the raw hash value.
	 * unfortunatly CAPI doesn't have a builtin function to do this work, so
	 * we go ahead and do it by hand here.
	 *
	 * format is:
	 *  SEQUENCE {
	 *     SECQUENCE { // algid
	 *       OID {}    // oid
	 *       ANY {}    // optional params 
	 *     }
	 *     OCTECT {}   // hash
	 */

	/* unwrap */
	algid = p11c_der_unwrap(input, n_input, &n_algid, NULL);
	if(!algid)
		return NULL;
	
	/* make sure there is not extra data at the end */
	if(algid + n_algid != input + n_input)
		return NULL;

	/* wasn't an algid */
	if((P11C_DER_SEQUENCE | P11C_DER_CONSTRUCTED) != *algid)
		return NULL;

	oid = p11c_der_unwrap(algid, n_algid, &n_oid, &hash_data);
	if(!oid || !hash_data)
		return NULL;

	if(algorithm)
	{
		/* 
		 * get the real oid as a string. Again, Microsoft does not
		 * export anything that does this for us 
		 */
		oid_str = p11c_der_read_oid(oid, n_oid);
		if(!oid_str)
			return NULL;

		/* look up the hash alg from the oid (fortunately CAPI does to this) */ 
		*algorithm = CertOIDToAlgId(oid_str);
		free(oid_str);
	}

	/* wasn't a hash? */
	if(P11C_DER_OCTET_STRING != *hash_data)
		return NULL;

	/* get the real raw hash */
	return p11c_der_unwrap(hash_data, n_algid - (hash_data - algid), 
	                         n_hash, NULL);
}

CK_RV
p11c_rsa_pkcs_sign_init(P11cObjectData *keydata, void** operation)
{
	CRYPT_KEY_PROV_INFO* prov_info;

	ASSERT(keydata);
	ASSERT(operation);
	ASSERT(!*operation);
	
	prov_info = p11c_key_object_data_get_prov_info(keydata);
	if(prov_info->dwProvType != PROV_RSA_FULL)
		return CKR_KEY_TYPE_INCONSISTENT;

	*operation = keydata;
	return CKR_OK;
}

CK_RV
p11c_rsa_pkcs_sign_perform (CK_BYTE_PTR data, CK_ULONG n_data,
                            CK_BYTE_PTR signature, CK_ULONG_PTR n_signature,
                            void** operation)
{
	CRYPT_KEY_PROV_INFO* prov_info;
	P11cObjectData* keydata;
	ALG_ID algorithm;
	BYTE* hash_data;
	DWORD n_hash_data;
	BOOL capifail;
	DWORD len, check;
	DWORD bits;
	CK_RV ret;

	HCRYPTPROV prov = 0;
	HCRYPTHASH hash = 0;


	ASSERT(operation);
	ASSERT(*operation);

	if(!data || !n_data)
		return CKR_ARGUMENTS_BAD;

	keydata = (P11cObjectData*)*operation;

	prov_info = p11c_key_object_data_get_prov_info(keydata);
	ASSERT(prov_info);

	/* Calculate the number of bits */
	bits = p11c_key_object_data_get_bits (keydata);
	if(!bits)
		return CKR_GENERAL_ERROR;

	/* Want to know the length */
	if(!signature) 
	{
		*n_signature = bits / 8;
		return CKR_OK;
	}

	/* TODO: Support arbitrary input on Vista */

	/*
	 * PKCS #11 sign for RSA expects to take a fully DER-encoded hash value, 
	 * which includes the hash OID. CAPI expects to take a Hash Context. While 
	 * CAPI does have the capability of setting a raw hash value, it does not 
	 * have the ability to sign an arbitrary value. This function tries to
	 * reduce the passed in data into something that CAPI could actually sign.
	 */
	hash_data = parse_rsa_pkcs_der_hash(data, n_data, &algorithm, &n_hash_data);
	if(!hash_data)
		return CKR_DATA_INVALID;

	capifail = TRUE;
	if(CryptAcquireContextW(&prov, prov_info->pwszContainerName, prov_info->pwszProvName,
	                        prov_info->dwProvType, 0))
	{
		if(CryptCreateHash(prov, algorithm, 0, 0, &hash))
		{
			/* make sure the hash lens match before we set it */
			len = sizeof(DWORD);
			if(CryptGetHashParam(hash, HP_HASHSIZE, (BYTE*)&check, &len, 0))
			{
				if(check != n_hash_data) 
				{
					capifail = FALSE;
					ret = CKR_DATA_INVALID;
				}

				/* 
				 * we have an explicit hash, set it, note that the length is
		 		 * implicit by the hashAlg used in create 
				 */
				if(CryptSetHashParam(hash, HP_HASHVAL, hash_data, 0))
				{
					/* OK, we have the data in a hash structure, sign it! */
					if(CryptSignHash(hash, prov_info->dwKeySpec, 
					                 NULL, 0, signature, n_signature))
					{
						/* 
						 * OK, Microsoft likes to do things completely 
						 * differently than anyone else. We need to reverse 
						 * the data we recieved here 
						 */
						if(signature)
							p11c_reverse_memory(signature, *n_signature);
	
						capifail = FALSE;
						ret = CKR_OK;
					}
				}
			}
		}
	}

	if(capifail)
		ret = p11c_winerr_to_ckr(GetLastError());

	if(hash)
		CryptDestroyHash(hash);
	if(prov)
		CryptReleaseContext(prov, 0);

	return ret;
}

void
p11c_rsa_pkcs_sign_cleanup (void* operation)
{
	/* Nothing to do */
}


CK_RV
p11c_rsa_pkcs_decrypt_init(P11cObjectData* keydata, void** operation)
{
	CRYPT_KEY_PROV_INFO* prov_info;

	ASSERT(keydata);
	ASSERT(operation);
	ASSERT(!*operation);
	
	prov_info = p11c_key_object_data_get_prov_info(keydata);
	if(prov_info->dwProvType != PROV_RSA_FULL)
		return CKR_KEY_TYPE_INCONSISTENT;

	*operation = keydata;
	return CKR_OK;
}

CK_RV
p11c_rsa_pkcs_decrypt_perform(CK_BYTE_PTR encdata, CK_ULONG n_encdata,
                              CK_BYTE_PTR result, CK_ULONG_PTR n_result,
                              void** operation)
{
	CRYPT_KEY_PROV_INFO* prov_info;
	P11cObjectData* keydata;
	BOOL capifail;
	DWORD bits, error;
	CK_RV ret;

	HCRYPTPROV prov = 0;
	HCRYPTKEY key = 0;
	void* buffer = NULL;

	ASSERT(operation);
	ASSERT(*operation);
	ASSERT(encdata);
	ASSERT(n_encdata);

	keydata = (P11cObjectData*)*operation;

	prov_info = p11c_key_object_data_get_prov_info(keydata);
	ASSERT(prov_info);

	/* Calculate the number of bits */
	bits = p11c_key_object_data_get_bits (keydata);
	if(!bits)
		return CKR_GENERAL_ERROR;

	/* Want to know the length */
	if(!result) 
	{
		*n_result = bits / 8;
		return CKR_OK;
	}

	/* 
	 * Copy the input, since CAPI operates in place, and 
	 * we must also reverse it properly.
	 */
	buffer = malloc(n_encdata);
	if(!buffer)
		return CKR_HOST_MEMORY;

	memcpy(buffer, encdata, n_encdata);
	p11c_reverse_memory(buffer, n_encdata);

	capifail = TRUE;
	if(CryptAcquireContextW(&prov, prov_info->pwszContainerName, prov_info->pwszProvName,
	                        prov_info->dwProvType, 0))
	{
		if(CryptGetUserKey(prov, prov_info->dwKeySpec, &key))
		{
			*n_result = n_encdata;
			if(CryptDecrypt(key, 0, TRUE, 0, buffer, n_result))
			{
				capifail = FALSE;
				ret = CKR_OK;
			}
		}
	}

	if(capifail)
	{
		error = GetLastError();
		switch(error)
		{
		case NTE_BAD_DATA:
			ret = CKR_ENCRYPTED_DATA_INVALID;
		default:
			ret = p11c_winerr_to_ckr(error);
		};
	}

	/* Copy the memory out to the result buffer */
	if(ret == CKR_OK)
		ret = p11c_return_data_raw(result, n_result, buffer, *n_result);

	if(key)
		CryptDestroyKey(key);
	if(prov)
		CryptReleaseContext(prov, 0);
	if(buffer)
		free(buffer);

	return ret;
}

void
p11c_rsa_pkcs_decrypt_cleanup(void* operation)
{
	/* Nothing to do */
}

void
p11c_rsa_pkcs_get_info(CK_MECHANISM_TYPE mech, CK_MECHANISM_INFO_PTR info)
{
	ASSERT(mech == CKM_RSA_PKCS);
	ASSERT(info != NULL);

	info->ulMinKeySize = 384;
	info->ulMaxKeySize = 16384;
	info->flags = 0;
}