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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/*
* Copyright (C) 2003 Nikos Mavroyanopoulos
* Copyright (C) 2004 Free Software Foundation
*
* This file is part of GNUTLS.
*
* The GNUTLS 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.1 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 <gnutls_int.h>
#include <gnutls_errors.h>
#include <gnutls_global.h>
#include <libtasn1.h>
#include <gnutls_datum.h>
#include "common.h"
#include "x509.h"
#include <gnutls_num.h>
#include "mpi.h"
/*
* some x509 certificate parsing functions that relate to MPI parameter
* extraction. This reads the BIT STRING subjectPublicKey.
* Returns 2 parameters (m,e).
*/
int _gnutls_x509_read_rsa_params(opaque * der, int dersize, mpi_t * params)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
if ((result = asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.RSAPublicKey", &spk))
!= ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_der_decoding(&spk, der, dersize, NULL);
if (result != ASN1_SUCCESS) {
gnutls_assert();
asn1_delete_structure(&spk);
return _gnutls_asn2err(result);
}
if ((result = _gnutls_x509_read_int(spk, "modulus", ¶ms[0])) < 0) {
gnutls_assert();
asn1_delete_structure(&spk);
return GNUTLS_E_ASN1_GENERIC_ERROR;
}
if ((result = _gnutls_x509_read_int(spk, "publicExponent",
¶ms[1])) < 0) {
gnutls_assert();
_gnutls_mpi_release(¶ms[0]);
asn1_delete_structure(&spk);
return GNUTLS_E_ASN1_GENERIC_ERROR;
}
asn1_delete_structure(&spk);
return 0;
}
/* reads p,q and g
* from the certificate (subjectPublicKey BIT STRING).
* params[0-2]
*/
int _gnutls_x509_read_dsa_params(opaque * der, int dersize, mpi_t * params)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
if ((result = asn1_create_element
(_gnutls_get_pkix(), "PKIX1.Dss-Parms", &spk)) != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_der_decoding(&spk, der, dersize, NULL);
if (result != ASN1_SUCCESS) {
gnutls_assert();
asn1_delete_structure(&spk);
return _gnutls_asn2err(result);
}
/* FIXME: If the parameters are not included in the certificate
* then the issuer's parameters should be used. This is not
* done yet.
*/
/* Read p */
if ((result = _gnutls_x509_read_int(spk, "p", ¶ms[0])) < 0) {
gnutls_assert();
asn1_delete_structure(&spk);
return GNUTLS_E_ASN1_GENERIC_ERROR;
}
/* Read q */
if ((result = _gnutls_x509_read_int(spk, "q", ¶ms[1])) < 0) {
gnutls_assert();
asn1_delete_structure(&spk);
_gnutls_mpi_release(¶ms[0]);
return GNUTLS_E_ASN1_GENERIC_ERROR;
}
/* Read g */
if ((result = _gnutls_x509_read_int(spk, "g", ¶ms[2])) < 0) {
gnutls_assert();
asn1_delete_structure(&spk);
_gnutls_mpi_release(¶ms[0]);
_gnutls_mpi_release(¶ms[1]);
return GNUTLS_E_ASN1_GENERIC_ERROR;
}
asn1_delete_structure(&spk);
return 0;
}
/* reads DSA's Y
* from the certificate
* only sets params[3]
*/
int _gnutls_x509_read_dsa_pubkey(opaque * der, int dersize, mpi_t * params)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
if ((result = asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.DSAPublicKey",
&spk)) != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = asn1_der_decoding(&spk, der, dersize, NULL);
if (result != ASN1_SUCCESS) {
gnutls_assert();
asn1_delete_structure(&spk);
return _gnutls_asn2err(result);
}
/* Read Y */
if ((result = _gnutls_x509_read_int(spk, "", ¶ms[3])) < 0) {
gnutls_assert();
asn1_delete_structure(&spk);
return _gnutls_asn2err(result);
}
asn1_delete_structure(&spk);
return 0;
}
/* Extracts DSA and RSA parameters from a certificate.
*/
int _gnutls_x509_crt_get_mpis(gnutls_x509_crt_t cert,
mpi_t * params, int *params_size)
{
int len, result;
opaque str[5 * 1024];
int pk_algorithm;
/* Read the algorithm's OID
*/
pk_algorithm = gnutls_x509_crt_get_pk_algorithm(cert, NULL);
/* Read the algorithm's parameters
*/
len = sizeof(str);
result = asn1_read_value(cert->cert,
"tbsCertificate.subjectPublicKeyInfo.subjectPublicKey",
str, &len);
len /= 8;
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
switch (pk_algorithm) {
case GNUTLS_PK_RSA:
/* params[0] is the modulus,
* params[1] is the exponent
*/
if (*params_size < RSA_PUBLIC_PARAMS) {
gnutls_assert();
/* internal error. Increase the mpi_ts in params */
return GNUTLS_E_INTERNAL_ERROR;
}
if ((result = _gnutls_x509_read_rsa_params(str, len, params)) < 0) {
gnutls_assert();
return result;
}
*params_size = RSA_PUBLIC_PARAMS;
return 0;
break;
case GNUTLS_PK_DSA:
/* params[0] is p,
* params[1] is q,
* params[2] is q,
* params[3] is pub.
*/
if (*params_size < DSA_PUBLIC_PARAMS) {
gnutls_assert();
/* internal error. Increase the mpi_ts in params */
return GNUTLS_E_INTERNAL_ERROR;
}
if ((result = _gnutls_x509_read_dsa_pubkey(str, len, params)) < 0) {
gnutls_assert();
return result;
}
/* Now read the parameters
*/
len = sizeof(str);
result = asn1_read_value(cert->cert,
"tbsCertificate.subjectPublicKeyInfo.algorithm.parameters",
str, &len);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if ((result = _gnutls_x509_read_dsa_params(str, len, params)) < 0) {
gnutls_assert();
return result;
}
*params_size = DSA_PUBLIC_PARAMS;
return 0;
break;
default:
/* other types like DH
* currently not supported
*/
gnutls_assert();
return GNUTLS_E_X509_CERTIFICATE_ERROR;
}
}
/*
* some x509 certificate functions that relate to MPI parameter
* setting. This writes the BIT STRING subjectPublicKey.
* Needs 2 parameters (m,e).
*
* Allocates the space used to store the DER data.
*/
int _gnutls_x509_write_rsa_params(mpi_t * params, int params_size,
gnutls_datum_t * der)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
der->data = NULL;
der->size = 0;
if (params_size < 2) {
gnutls_assert();
result = GNUTLS_E_INVALID_REQUEST;
goto cleanup;
}
if ((result = asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.RSAPublicKey", &spk))
!= ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = _gnutls_x509_write_int(spk, "modulus", params[0], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_write_int(spk, "publicExponent", params[1], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_der_encode(spk, "", der, 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
asn1_delete_structure(&spk);
return 0;
cleanup:
asn1_delete_structure(&spk);
return result;
}
/*
* This function writes and encodes the parameters for DSS or RSA keys.
* This is the "signatureAlgorithm" fields.
*/
int _gnutls_x509_write_sig_params(ASN1_TYPE dst, const char *dst_name,
gnutls_pk_algorithm_t pk_algorithm,
mpi_t * params, int params_size)
{
gnutls_datum_t der;
int result;
char name[128];
const char *pk;
_gnutls_str_cpy(name, sizeof(name), dst_name);
_gnutls_str_cat(name, sizeof(name), ".algorithm");
pk = _gnutls_x509_sign_to_oid(pk_algorithm, GNUTLS_MAC_SHA);
if (pk == NULL) {
gnutls_assert();
return GNUTLS_E_INVALID_REQUEST;
}
/* write the OID.
*/
result = asn1_write_value(dst, name, pk, 1);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
_gnutls_str_cpy(name, sizeof(name), dst_name);
_gnutls_str_cat(name, sizeof(name), ".parameters");
if (pk_algorithm == GNUTLS_PK_DSA) {
result = _gnutls_x509_write_dsa_params(params, params_size, &der);
if (result < 0) {
gnutls_assert();
return result;
}
result = asn1_write_value(dst, name, der.data, der.size);
_gnutls_free_datum(&der);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
} else { /* RSA */
result = asn1_write_value(dst, name, NULL, 0);
if (result != ASN1_SUCCESS && result != ASN1_ELEMENT_NOT_FOUND) {
/* Here we ignore the element not found error, since this
* may have been disabled before.
*/
gnutls_assert();
return _gnutls_asn2err(result);
}
}
return 0;
}
/*
* This function writes the parameters for DSS keys.
* Needs 3 parameters (p,q,g).
*
* Allocates the space used to store the DER data.
*/
int _gnutls_x509_write_dsa_params(mpi_t * params, int params_size,
gnutls_datum_t * der)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
der->data = NULL;
der->size = 0;
if (params_size < 3) {
gnutls_assert();
result = GNUTLS_E_INVALID_REQUEST;
goto cleanup;
}
if ((result = asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.DSAParameters", &spk))
!= ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = _gnutls_x509_write_int(spk, "p", params[0], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_write_int(spk, "q", params[1], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_write_int(spk, "g", params[2], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_der_encode(spk, "", der, 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
asn1_delete_structure(&spk);
return 0;
cleanup:
asn1_delete_structure(&spk);
return result;
}
/*
* This function writes the public parameters for DSS keys.
* Needs 1 parameter (y).
*
* Allocates the space used to store the DER data.
*/
int _gnutls_x509_write_dsa_public_key(mpi_t * params, int params_size,
gnutls_datum_t * der)
{
int result;
ASN1_TYPE spk = ASN1_TYPE_EMPTY;
der->data = NULL;
der->size = 0;
if (params_size < 3) {
gnutls_assert();
result = GNUTLS_E_INVALID_REQUEST;
goto cleanup;
}
if ((result = asn1_create_element
(_gnutls_get_gnutls_asn(), "GNUTLS.DSAPublicKey", &spk))
!= ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
result = _gnutls_x509_write_int(spk, "", params[3], 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
result = _gnutls_x509_der_encode(spk, "", der, 0);
if (result < 0) {
gnutls_assert();
goto cleanup;
}
asn1_delete_structure(&spk);
return 0;
cleanup:
asn1_delete_structure(&spk);
return result;
}
/* this function reads a (small) unsigned integer
* from asn1 structs. Combines the read and the convertion
* steps.
*/
int _gnutls_x509_read_uint(ASN1_TYPE node, const char *value,
unsigned int *ret)
{
int len, result;
opaque *tmpstr;
len = 0;
result = asn1_read_value(node, value, NULL, &len);
if (result != ASN1_MEM_ERROR) {
gnutls_assert();
return _gnutls_asn2err(result);
}
tmpstr = gnutls_alloca(len);
if (tmpstr == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
result = asn1_read_value(node, value, tmpstr, &len);
gnutls_afree(tmpstr);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
if (len == 1)
*ret = tmpstr[0];
else if (len == 2)
*ret = _gnutls_read_uint16(tmpstr);
else if (len == 3)
*ret = _gnutls_read_uint24(tmpstr);
else if (len == 4)
*ret = _gnutls_read_uint32(tmpstr);
else {
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
}
return 0;
}
/* Writes the specified integer into the specified node.
*/
int _gnutls_x509_write_uint32(ASN1_TYPE node, const char *value,
uint32 num)
{
opaque tmpstr[4];
int result;
_gnutls_write_uint32(num, tmpstr);
result = asn1_write_value(node, value, tmpstr, 4);
if (result != ASN1_SUCCESS) {
gnutls_assert();
return _gnutls_asn2err(result);
}
return 0;
}
|