summaryrefslogtreecommitdiff
path: root/lib/auth_rsa.c
blob: 9b72d9ceead2260f2f1e90013fe5fcb79edd692c (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
/*
 *      Copyright (C) 2000,2001 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUTLS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <defines.h>
#include "gnutls_int.h"
#include "gnutls_auth_int.h"
#include "gnutls_errors.h"
#include "gnutls_dh.h"
#include "gnutls_num.h"
#include "cert_asn1.h"
#include "cert_der.h"
#include "gnutls_datum.h"
#include "auth_x509.h"

#if 0
int gen_rsa_server_kx(GNUTLS_KEY, opaque **);
#endif
int gen_rsa_certificate(GNUTLS_KEY, opaque **);
int proc_rsa_client_kx( GNUTLS_KEY, opaque*, int);

MOD_AUTH_STRUCT rsa_auth_struct = {
	"RSA",
	gen_rsa_certificate,
/*	not needed!!! gen_rsa_server_kx, */ NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	proc_rsa_client_kx,
	NULL,
	NULL
};

typedef struct {
	gnutls_datum rsa_modulus;
	gnutls_datum rsa_exponent;
} RSA_Params;

/* This function will calculate the SHA/MD5 signature in server kx.
 * This is needed by the protocol.
 */
int _gnutls_calc_rsa_signature( GNUTLS_KEY key, const opaque* data, int data_size, opaque* dst) {
	void* md5;
	void* sha;
	int ret = 0;
	GNUTLS_MAC_HANDLE td;
	
	td = gnutls_hash_init(GNUTLS_MAC_MD5);
	if (td==GNUTLS_HASH_FAILED) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	gnutls_hash( td, key->client_random, 32);
	gnutls_hash( td, key->server_random, 32);
	gnutls_hash( td, data, data_size);
	
	md5 = gnutls_hash_deinit(td);
	if (md5==NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	memcpy( dst, md5, 16);
	gnutls_free(md5);


	td = gnutls_hash_init(GNUTLS_MAC_SHA);
	if (td==GNUTLS_HASH_FAILED) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	gnutls_hash( td, key->client_random, 32);
	gnutls_hash( td, key->server_random, 32);
	gnutls_hash( td, data, data_size);
	
	sha = gnutls_hash_deinit(td);
	if (sha==NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	memcpy( &dst[16], sha, 20);
	gnutls_free(sha);
	
	return ret;
}

#if 0
/* This function reads the RSA parameters from the given(?) certificate.
 */
static int _gnutls_get_rsa_params( GNUTLS_KEY key, RSA_Params * params, gnutls_datum cert)
{
	int ret = 0, result;
	opaque str[5*1024];
	int len = sizeof(str);

	create_structure("certificate2", "PKIX1Explicit88.Certificate");

	result = get_der("certificate2", cert.data, cert.size);
	if (result != ASN_OK) {
		gnutls_assert();
		return GNUTLS_E_PARSING_ERROR;
	}

	/* Verify sign */
	result =
	    read_value("certificate2.tbsCertificate.subjectPublicKeyInfo.algorithm", str, &len);
	if (result != ASN_OK) {
		gnutls_assert();
		delete_structure("certificate2");
		return GNUTLS_E_PARSING_ERROR;
	}

	if (!strcmp(str, "1 2 840 113549 1 1 1")) { /* pkix-1 1 - RSA */
		len = sizeof(str);
		result =
		    read_value("certificate2.tbsCertificate.subjectPublicKeyInfo.parameters", str, &len);
		delete_structure("certificate2");

		if (result != ASN_OK) {
			gnutls_assert();
			delete_structure("certificate2");
			return GNUTLS_E_PARSING_ERROR;
		}

		create_structure("rsapublickey", "PKIX1Explicit88.RSAPublicKey");
		result = get_der("rsapublickey", str, len);
		if (result != ASN_OK) {
			gnutls_assert();
			delete_structure("rsapublickey");
			return GNUTLS_E_PARSING_ERROR;
		}	

		result =
		    read_value("rsapublickey.modulus", str, &len);
		if (result != ASN_OK) {
			gnutls_assert();
			delete_structure("rsapublickey");
			return GNUTLS_E_PARSING_ERROR;
		}

		if (gcry_mpi_scan(&key->A,
			  GCRYMPI_FMT_USG, str, &len) != 0) {
			gnutls_assert();
			delete_structure("rsapublickey");
			return GNUTLS_E_MPI_SCAN_FAILED;
		}
		if (gnutls_set_datum(&params->rsa_modulus, str, len) < 0) {
			gnutls_assert();
			delete_structure("rsapublickey");
			return GNUTLS_E_MEMORY_ERROR;
		}

		len = sizeof(str);
		result =
		    read_value("rsapublickey.publicExponent", str, &len);
		if (result != ASN_OK) {
			gnutls_assert();
			delete_structure("rsapublickey");
			gnutls_free_datum(&params->rsa_modulus);
			_gnutls_mpi_release(&key->A);
			return GNUTLS_E_PARSING_ERROR;
		}

		if (gcry_mpi_scan(&key->B,
			  GCRYMPI_FMT_USG, str, &len) != 0) {
			gnutls_assert();
			_gnutls_mpi_release(&key->A);
			gnutls_free_datum(&params->rsa_modulus);
			delete_structure("rsapublickey");
			return GNUTLS_E_MPI_SCAN_FAILED;
		}
		if (gnutls_set_datum(&params->rsa_exponent, str, len) < 0) {
			_gnutls_mpi_release(&key->A);
			gnutls_free_datum(&params->rsa_modulus);
			delete_structure("rsapublickey");
			return GNUTLS_E_MEMORY_ERROR;	
		}

		delete_structure("rsapublickey");

	}

	delete_structure("certificate2");

	return ret;
}
#endif

/* This function reads the RSA parameters from the given private key
 * cert is not a certificate but a der structure containing the private
 * key(s).
 */
static int _gnutls_get_private_rsa_params( GNUTLS_KEY key, gnutls_datum cert)
{
	int ret = 0, result;
	opaque str[5*1024];
	int len = sizeof(str);

	create_structure("rsa_key", "PKCS-1");

	result = get_der("rsa_key", cert.data, cert.size);
	if (result != ASN_OK) {
		gnutls_assert();
		return GNUTLS_E_PARSING_ERROR;
	}

	result =
	    read_value("rsa_key.RSAPrivateKey.privateExponent", str, &len);
	if (result != ASN_OK) {
		gnutls_assert();
		delete_structure("rsa_key");
		return GNUTLS_E_PARSING_ERROR;
	}
	if (gcry_mpi_scan(&key->u,
		  GCRYMPI_FMT_USG, str, &len) != 0) {
		gnutls_assert();
		delete_structure("rsa_key");
		return GNUTLS_E_MPI_SCAN_FAILED;
	}


	len = sizeof(str);
	result =
	    read_value("rsa_key.modulus", str, &len);
	if (result != ASN_OK) {
		gnutls_assert();
		delete_structure("rsa_key");
		_gnutls_mpi_release(&key->u);
		return GNUTLS_E_PARSING_ERROR;
	}

	if (gcry_mpi_scan(&key->A,
		  GCRYMPI_FMT_USG, str, &len) != 0) {
		gnutls_assert();
		delete_structure("rsa_key");
		_gnutls_mpi_release(&key->u);
		return GNUTLS_E_MPI_SCAN_FAILED;
	}

	delete_structure("rsa_key");

	return ret;
}

#if 0 /* wow ... this was not needed ! */
int gen_rsa_server_kx(GNUTLS_KEY key, opaque ** data)
{
	RSA_Params params;
	const X509PKI_SERVER_CREDENTIALS *cred;
	int ret;
	opaque* pdata;

	cred = _gnutls_get_cred(key, GNUTLS_X509PKI, NULL);
	if (cred == NULL) {
		gnutls_assert();
		return GNUTLS_E_INSUFICIENT_CRED;
	}

	ret =
	    _gnutls_get_rsa_params(key, &params, cred->pkey);

	if (ret < 0) {
		gnutls_assert();
		return ret;
	}

	ret = params.rsa_modulus.size +
		        params.rsa_modulus.size + 16 + 20 + 4;
	(*data) = gnutls_malloc( ret);
	
	pdata = (*data);
	if (pdata == NULL) return GNUTLS_E_MEMORY_ERROR;
	
	WRITEdatum16(pdata, params.rsa_modulus);
	pdata += params.rsa_modulus.size;

	WRITEdatum16(pdata, params.rsa_exponent);
	pdata += params.rsa_exponent.size;

	gnutls_free_datum(&params.rsa_modulus);
	gnutls_free_datum(&params.rsa_modulus);

	ret = _gnutls_calc_rsa_signature( key, (*data), ret-20-16, pdata);
	if (ret< 0) {
		gnutls_free((*data));
		gnutls_assert();
		return ret;
	}

	return ret;	
}
#endif

int gen_rsa_certificate(GNUTLS_KEY key, opaque ** data)
{
	const X509PKI_SERVER_CREDENTIALS *cred;
	int ret, i;
	opaque* pdata;
	gnutls_datum* apr_cert_list;
	gnutls_datum apr_pkey;
	int apr_cert_list_length;
	
	cred = _gnutls_get_cred(key, GNUTLS_X509PKI, NULL);
	if (cred == NULL) {
		gnutls_assert();
		return GNUTLS_E_INSUFICIENT_CRED;
	}

	if (cred->ncerts==0) {
		gnutls_assert();
		return GNUTLS_E_INSUFICIENT_CRED;
	}

	/* FIXME: FIND APPROPRIATE CERTIFICATE - depending on hostname 
	 */
	apr_cert_list = cred->cert_list[0];
	apr_cert_list_length = cred->cert_list_length[0];
	apr_pkey = cred->pkey[0];

	ret = 3;
	for (i=0;i<apr_cert_list_length;i++) {
		ret += apr_cert_list[i].size + 3; 
					/* hold size
					 * for uint24 */
	}

	(*data) = gnutls_malloc(ret);
	pdata = (*data);
	
	if (pdata == NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	
	WRITEuint24( ret-3, pdata);
	pdata+=3;
	for (i=0;i<apr_cert_list_length;i++) {
		WRITEdatum24( pdata, apr_cert_list[i]); 
		pdata += 3 + apr_cert_list[i].size;
	}

	/* read the rsa parameters now, since later we will
	 * now know which certificate we used!
	 */
	ret =
	    _gnutls_get_private_rsa_params(key, apr_pkey);

	if (ret < 0) {
		gnutls_assert();
		return ret;
	}

	return ret;	
}


int proc_rsa_client_kx( GNUTLS_KEY key, opaque* data, int data_size) {
	const X509PKI_SERVER_CREDENTIALS * cred;

	cred = _gnutls_get_cred(key, GNUTLS_X509PKI, NULL);
	if (cred == NULL) {
		gnutls_assert();
		return GNUTLS_E_INSUFICIENT_CRED;
	}

	return 0;
}