blob: 5f1bdbddcfcfa5921737d90d57ab4f33b6ebb102 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000042#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000043#include "polarssl/des.h"
44#include "polarssl/md2.h"
45#include "polarssl/md4.h"
46#include "polarssl/md5.h"
47#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000048#include "polarssl/sha2.h"
49#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000050#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52#include <string.h>
53#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <time.h>
55
Paul Bakker335db3f2011-04-25 15:28:35 +000056#if defined(POLARSSL_FS_IO)
57#include <stdio.h>
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/*
61 * ASN.1 DER decoding routines
62 */
63static int asn1_get_len( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000064 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +000065 size_t *len )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
67 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +000068 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 if( ( **p & 0x80 ) == 0 )
71 *len = *(*p)++;
72 else
73 {
74 switch( **p & 0x7F )
75 {
76 case 1:
77 if( ( end - *p ) < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000078 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 *len = (*p)[1];
81 (*p) += 2;
82 break;
83
84 case 2:
85 if( ( end - *p ) < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000086 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 *len = ( (*p)[1] << 8 ) | (*p)[2];
89 (*p) += 3;
90 break;
91
Paul Bakkerc4909d92011-10-12 09:52:22 +000092 case 3:
93 if( ( end - *p ) < 4 )
94 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
95
96 *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3];
97 (*p) += 4;
98 break;
99
100 case 4:
101 if( ( end - *p ) < 5 )
102 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
103
104 *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | (*p)[4];
105 (*p) += 5;
106 break;
107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000109 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 }
111 }
112
Paul Bakker23986e52011-04-24 08:57:21 +0000113 if( *len > (size_t) ( end - *p ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000114 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 return( 0 );
117}
118
119static int asn1_get_tag( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 size_t *len, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000124 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126 if( **p != tag )
Paul Bakker40e46942009-01-03 21:51:57 +0000127 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129 (*p)++;
130
131 return( asn1_get_len( p, end, len ) );
132}
133
134static int asn1_get_bool( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000135 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 int *val )
137{
Paul Bakker23986e52011-04-24 08:57:21 +0000138 int ret;
139 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
142 return( ret );
143
144 if( len != 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000145 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 *val = ( **p != 0 ) ? 1 : 0;
148 (*p)++;
149
150 return( 0 );
151}
152
153static int asn1_get_int( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 int *val )
156{
Paul Bakker23986e52011-04-24 08:57:21 +0000157 int ret;
158 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
161 return( ret );
162
Paul Bakker27fdf462011-06-09 13:55:13 +0000163 if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000164 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 *val = 0;
167
168 while( len-- > 0 )
169 {
170 *val = ( *val << 8 ) | **p;
171 (*p)++;
172 }
173
174 return( 0 );
175}
176
177static int asn1_get_mpi( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 mpi *X )
180{
Paul Bakker23986e52011-04-24 08:57:21 +0000181 int ret;
182 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
185 return( ret );
186
187 ret = mpi_read_binary( X, *p, len );
188
189 *p += len;
190
191 return( ret );
192}
193
Paul Bakker74111d32011-01-15 16:57:55 +0000194static int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
195 x509_bitstring *bs)
196{
197 int ret;
198
199 /* Certificate type is a single byte bitstring */
200 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
201 return( ret );
202
203 /* Check length, subtract one for actual bit string length */
204 if ( bs->len < 1 )
205 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
206 bs->len -= 1;
207
208 /* Get number of unused bits, ensure unused bits <= 7 */
209 bs->unused_bits = **p;
210 if( bs->unused_bits > 7 )
211 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
212 (*p)++;
213
214 /* Get actual bitstring */
215 bs->p = *p;
216 *p += bs->len;
217
218 if( *p != end )
219 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
220
221 return 0;
222}
223
224
225/*
226 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
227 */
228static int asn1_get_sequence_of( unsigned char **p,
229 const unsigned char *end,
230 x509_sequence *cur,
231 int tag)
232{
Paul Bakker23986e52011-04-24 08:57:21 +0000233 int ret;
234 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000235 x509_buf *buf;
236
237 /* Get main sequence tag */
238 if( ( ret = asn1_get_tag( p, end, &len,
239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
240 return( ret );
241
242 if( *p + len != end )
243 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
244
245 while( *p < end )
246 {
247 buf = &(cur->buf);
248 buf->tag = **p;
249
250 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
251 return( ret );
252
253 buf->p = *p;
254 *p += buf->len;
255
256 /* Allocate and assign next pointer */
257 if (*p < end)
258 {
259 cur->next = (x509_sequence *) malloc(
260 sizeof( x509_sequence ) );
261
262 if( cur->next == NULL )
263 return( 1 );
264
265 cur = cur->next;
266 }
267 }
268
269 /* Set final sequence entry's next pointer to NULL */
270 cur->next = NULL;
271
272 if( *p != end )
273 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
274
275 return( 0 );
276}
277
Paul Bakker5121ce52009-01-03 21:22:43 +0000278/*
279 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
280 */
281static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 int *ver )
284{
Paul Bakker23986e52011-04-24 08:57:21 +0000285 int ret;
286 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 if( ( ret = asn1_get_tag( p, end, &len,
289 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
290 {
Paul Bakker40e46942009-01-03 21:51:57 +0000291 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 return( *ver = 0 );
293
294 return( ret );
295 }
296
297 end = *p + len;
298
299 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000300 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000303 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000304 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 return( 0 );
307}
308
309/*
Paul Bakker3329d1f2011-10-12 09:55:01 +0000310 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
311 */
312static int x509_crl_get_version( unsigned char **p,
313 const unsigned char *end,
314 int *ver )
315{
316 int ret;
317
318 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
319 {
320 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
321 return( *ver = 0 );
322
323 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
324 }
325
326 return( 0 );
327}
328
329/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * CertificateSerialNumber ::= INTEGER
331 */
332static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 x509_buf *serial )
335{
336 int ret;
337
338 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000339 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000340 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
343 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000344 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000345 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347 serial->tag = *(*p)++;
348
349 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 serial->p = *p;
353 *p += serial->len;
354
355 return( 0 );
356}
357
358/*
359 * AlgorithmIdentifier ::= SEQUENCE {
360 * algorithm OBJECT IDENTIFIER,
361 * parameters ANY DEFINED BY algorithm OPTIONAL }
362 */
363static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 x509_buf *alg )
366{
Paul Bakker23986e52011-04-24 08:57:21 +0000367 int ret;
368 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 if( ( ret = asn1_get_tag( p, end, &len,
371 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000372 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 end = *p + len;
375 alg->tag = **p;
376
377 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000378 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 alg->p = *p;
381 *p += alg->len;
382
383 if( *p == end )
384 return( 0 );
385
386 /*
387 * assume the algorithm parameters must be NULL
388 */
389 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000390 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000393 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000394 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 return( 0 );
397}
398
399/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 * AttributeTypeAndValue ::= SEQUENCE {
401 * type AttributeType,
402 * value AttributeValue }
403 *
404 * AttributeType ::= OBJECT IDENTIFIER
405 *
406 * AttributeValue ::= ANY DEFINED BY AttributeType
407 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000408static int x509_get_attr_type_value( unsigned char **p,
409 const unsigned char *end,
410 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000411{
Paul Bakker23986e52011-04-24 08:57:21 +0000412 int ret;
413 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 x509_buf *oid;
415 x509_buf *val;
416
417 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 oid = &cur->oid;
422 oid->tag = **p;
423
424 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000425 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 oid->p = *p;
428 *p += oid->len;
429
430 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000431 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
435 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
436 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000438 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 val = &cur->val;
441 val->tag = *(*p)++;
442
443 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000444 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446 val->p = *p;
447 *p += val->len;
448
449 cur->next = NULL;
450
Paul Bakker400ff6f2011-02-20 10:40:16 +0000451 return( 0 );
452}
453
454/*
455 * RelativeDistinguishedName ::=
456 * SET OF AttributeTypeAndValue
457 *
458 * AttributeTypeAndValue ::= SEQUENCE {
459 * type AttributeType,
460 * value AttributeValue }
461 *
462 * AttributeType ::= OBJECT IDENTIFIER
463 *
464 * AttributeValue ::= ANY DEFINED BY AttributeType
465 */
466static int x509_get_name( unsigned char **p,
467 const unsigned char *end,
468 x509_name *cur )
469{
Paul Bakker23986e52011-04-24 08:57:21 +0000470 int ret;
471 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000472 const unsigned char *end2;
473 x509_name *use;
474
475 if( ( ret = asn1_get_tag( p, end, &len,
476 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000477 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000478
479 end2 = end;
480 end = *p + len;
481 use = cur;
482
483 do
484 {
485 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
486 return( ret );
487
488 if( *p != end )
489 {
490 use->next = (x509_name *) malloc(
491 sizeof( x509_name ) );
492
493 if( use->next == NULL )
494 return( 1 );
495
496 memset( use->next, 0, sizeof( x509_name ) );
497
498 use = use->next;
499 }
500 }
501 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 /*
504 * recurse until end of SEQUENCE is reached
505 */
506 if( *p == end2 )
507 return( 0 );
508
509 cur->next = (x509_name *) malloc(
510 sizeof( x509_name ) );
511
512 if( cur->next == NULL )
513 return( 1 );
514
515 return( x509_get_name( p, end2, cur->next ) );
516}
517
518/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * Time ::= CHOICE {
520 * utcTime UTCTime,
521 * generalTime GeneralizedTime }
522 */
Paul Bakker91200182010-02-18 21:26:15 +0000523static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000524 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000525 x509_time *time )
526{
Paul Bakker23986e52011-04-24 08:57:21 +0000527 int ret;
528 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000530 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000531
Paul Bakker91200182010-02-18 21:26:15 +0000532 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000533 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
534 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000535
Paul Bakker91200182010-02-18 21:26:15 +0000536 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000537
Paul Bakker91200182010-02-18 21:26:15 +0000538 if ( tag == ASN1_UTC_TIME )
539 {
540 (*p)++;
541 ret = asn1_get_len( p, end, &len );
542
543 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000544 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000545
Paul Bakker91200182010-02-18 21:26:15 +0000546 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000547 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
548 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549
Paul Bakker91200182010-02-18 21:26:15 +0000550 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
551 &time->year, &time->mon, &time->day,
552 &time->hour, &time->min, &time->sec ) < 5 )
553 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000554
Paul Bakker400ff6f2011-02-20 10:40:16 +0000555 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000556 time->year += 1900;
557
558 *p += len;
559
560 return( 0 );
561 }
562 else if ( tag == ASN1_GENERALIZED_TIME )
563 {
564 (*p)++;
565 ret = asn1_get_len( p, end, &len );
566
567 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000568 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000569
570 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000571 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
572 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000573
574 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
575 &time->year, &time->mon, &time->day,
576 &time->hour, &time->min, &time->sec ) < 5 )
577 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
578
579 *p += len;
580
581 return( 0 );
582 }
583 else
Paul Bakker9d781402011-05-09 16:17:09 +0000584 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585}
586
587
588/*
589 * Validity ::= SEQUENCE {
590 * notBefore Time,
591 * notAfter Time }
592 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000593static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 x509_time *from,
596 x509_time *to )
597{
Paul Bakker23986e52011-04-24 08:57:21 +0000598 int ret;
599 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 if( ( ret = asn1_get_tag( p, end, &len,
602 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000603 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
605 end = *p + len;
606
Paul Bakker91200182010-02-18 21:26:15 +0000607 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Paul Bakker91200182010-02-18 21:26:15 +0000610 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000614 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000615 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617 return( 0 );
618}
619
620/*
621 * SubjectPublicKeyInfo ::= SEQUENCE {
622 * algorithm AlgorithmIdentifier,
623 * subjectPublicKey BIT STRING }
624 */
625static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000626 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 x509_buf *pk_alg_oid,
628 mpi *N, mpi *E )
629{
Paul Bakker23986e52011-04-24 08:57:21 +0000630 int ret, can_handle;
631 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 unsigned char *end2;
633
634 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
635 return( ret );
636
637 /*
638 * only RSA public keys handled at this time
639 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000640 can_handle = 0;
641
642 if( pk_alg_oid->len == 9 &&
643 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
644 can_handle = 1;
645
646 if( pk_alg_oid->len == 9 &&
647 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
648 {
649 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
650 can_handle = 1;
651
652 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
653 can_handle = 1;
654 }
655
656 if( pk_alg_oid->len == 5 &&
657 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
658 can_handle = 1;
659
660 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000661 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000664 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000667 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000668 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
670 end2 = *p + len;
671
672 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000673 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 /*
676 * RSAPublicKey ::= SEQUENCE {
677 * modulus INTEGER, -- n
678 * publicExponent INTEGER -- e
679 * }
680 */
681 if( ( ret = asn1_get_tag( p, end2, &len,
682 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000683 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000686 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000687 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
690 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000695 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 return( 0 );
698}
699
700static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000701 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 x509_buf *sig )
703{
Paul Bakker23986e52011-04-24 08:57:21 +0000704 int ret;
705 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707 sig->tag = **p;
708
709 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Paul Bakker74111d32011-01-15 16:57:55 +0000712
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000714 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
716 sig->len = len;
717 sig->p = *p;
718
719 *p += len;
720
721 return( 0 );
722}
723
724/*
725 * X.509 v2/v3 unique identifier (not parsed)
726 */
727static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000728 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 x509_buf *uid, int n )
730{
731 int ret;
732
733 if( *p == end )
734 return( 0 );
735
736 uid->tag = **p;
737
738 if( ( ret = asn1_get_tag( p, end, &uid->len,
739 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
740 {
Paul Bakker40e46942009-01-03 21:51:57 +0000741 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 return( 0 );
743
744 return( ret );
745 }
746
747 uid->p = *p;
748 *p += uid->len;
749
750 return( 0 );
751}
752
753/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000754 * X.509 Extensions (No parsing of extensions, pointer should
755 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 */
757static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000758 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000759 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760{
Paul Bakker23986e52011-04-24 08:57:21 +0000761 int ret;
762 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764 if( *p == end )
765 return( 0 );
766
767 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000770 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773 ext->p = *p;
774 end = *p + ext->len;
775
776 /*
777 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
778 *
779 * Extension ::= SEQUENCE {
780 * extnID OBJECT IDENTIFIER,
781 * critical BOOLEAN DEFAULT FALSE,
782 * extnValue OCTET STRING }
783 */
784 if( ( ret = asn1_get_tag( p, end, &len,
785 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000786 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
788 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000790 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakkerd98030e2009-05-02 15:13:40 +0000792 return( 0 );
793}
794
795/*
796 * X.509 CRL v2 extensions (no extensions parsed yet.)
797 */
798static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000799 const unsigned char *end,
800 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000801{
Paul Bakker23986e52011-04-24 08:57:21 +0000802 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000803 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000804
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000805 /* Get explicit tag */
806 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000807 {
808 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
809 return( 0 );
810
811 return( ret );
812 }
813
814 while( *p < end )
815 {
816 if( ( ret = asn1_get_tag( p, end, &len,
817 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000818 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000819
820 *p += len;
821 }
822
823 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000824 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000825 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
826
827 return( 0 );
828}
829
Paul Bakker74111d32011-01-15 16:57:55 +0000830static int x509_get_basic_constraints( unsigned char **p,
831 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000832 int *ca_istrue,
833 int *max_pathlen )
834{
Paul Bakker23986e52011-04-24 08:57:21 +0000835 int ret;
836 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000837
838 /*
839 * BasicConstraints ::= SEQUENCE {
840 * cA BOOLEAN DEFAULT FALSE,
841 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
842 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000843 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000844 *max_pathlen = 0; /* endless */
845
846 if( ( ret = asn1_get_tag( p, end, &len,
847 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000849
850 if( *p == end )
851 return 0;
852
Paul Bakker3cccddb2011-01-16 21:46:31 +0000853 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000854 {
855 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000856 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000857
858 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000859 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000860
Paul Bakker3cccddb2011-01-16 21:46:31 +0000861 if( *ca_istrue != 0 )
862 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000863 }
864
865 if( *p == end )
866 return 0;
867
868 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000869 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000870
871 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000872 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000873 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
874
875 (*max_pathlen)++;
876
Paul Bakker74111d32011-01-15 16:57:55 +0000877 return 0;
878}
879
880static int x509_get_ns_cert_type( unsigned char **p,
881 const unsigned char *end,
882 unsigned char *ns_cert_type)
883{
884 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000885 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000886
887 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000888 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000889
890 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000891 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000892 POLARSSL_ERR_ASN1_INVALID_LENGTH );
893
894 /* Get actual bitstring */
895 *ns_cert_type = *bs.p;
896 return 0;
897}
898
899static int x509_get_key_usage( unsigned char **p,
900 const unsigned char *end,
901 unsigned char *key_usage)
902{
903 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000904 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000905
906 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000907 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000908
909 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000910 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000911 POLARSSL_ERR_ASN1_INVALID_LENGTH );
912
913 /* Get actual bitstring */
914 *key_usage = *bs.p;
915 return 0;
916}
917
Paul Bakkerd98030e2009-05-02 15:13:40 +0000918/*
Paul Bakker74111d32011-01-15 16:57:55 +0000919 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
920 *
921 * KeyPurposeId ::= OBJECT IDENTIFIER
922 */
923static int x509_get_ext_key_usage( unsigned char **p,
924 const unsigned char *end,
925 x509_sequence *ext_key_usage)
926{
927 int ret;
928
929 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000930 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000931
932 /* Sequence length must be >= 1 */
933 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000934 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000935 POLARSSL_ERR_ASN1_INVALID_LENGTH );
936
937 return 0;
938}
939
940/*
941 * X.509 v3 extensions
942 *
943 * TODO: Perform all of the basic constraints tests required by the RFC
944 * TODO: Set values for undetected extensions to a sane default?
945 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000946 */
947static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000948 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000949 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000950{
Paul Bakker23986e52011-04-24 08:57:21 +0000951 int ret;
952 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000953 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000954
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000955 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000956 {
957 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
958 return( 0 );
959
960 return( ret );
961 }
962
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 while( *p < end )
964 {
Paul Bakker74111d32011-01-15 16:57:55 +0000965 /*
966 * Extension ::= SEQUENCE {
967 * extnID OBJECT IDENTIFIER,
968 * critical BOOLEAN DEFAULT FALSE,
969 * extnValue OCTET STRING }
970 */
971 x509_buf extn_oid = {0, 0, NULL};
972 int is_critical = 0; /* DEFAULT FALSE */
973
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 if( ( ret = asn1_get_tag( p, end, &len,
975 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000976 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000978 end_ext_data = *p + len;
979
Paul Bakker74111d32011-01-15 16:57:55 +0000980 /* Get extension ID */
981 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Paul Bakker74111d32011-01-15 16:57:55 +0000983 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000984 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Paul Bakker74111d32011-01-15 16:57:55 +0000986 extn_oid.p = *p;
987 *p += extn_oid.len;
988
989 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000990 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000991 POLARSSL_ERR_ASN1_OUT_OF_DATA );
992
993 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000994 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000995 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000996 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker74111d32011-01-15 16:57:55 +0000998 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000999 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001000 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001001 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001003 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001004
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001005 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001006 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001007 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Paul Bakker74111d32011-01-15 16:57:55 +00001009 /*
1010 * Detect supported extensions
1011 */
1012 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
1013 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 {
Paul Bakker74111d32011-01-15 16:57:55 +00001015 /* Parse basic constraints */
1016 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +00001017 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001018 return ( ret );
1019 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 }
Paul Bakker74111d32011-01-15 16:57:55 +00001021 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
1022 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
1023 {
1024 /* Parse netscape certificate type */
1025 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1026 &crt->ns_cert_type ) ) != 0 )
1027 return ( ret );
1028 crt->ext_types |= EXT_NS_CERT_TYPE;
1029 }
1030 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
1031 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
1032 {
1033 /* Parse key usage */
1034 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1035 &crt->key_usage ) ) != 0 )
1036 return ( ret );
1037 crt->ext_types |= EXT_KEY_USAGE;
1038 }
1039 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1040 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1041 {
1042 /* Parse extended key usage */
1043 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1044 &crt->ext_key_usage ) ) != 0 )
1045 return ( ret );
1046 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1047 }
1048 else
1049 {
1050 /* No parser found, skip extension */
1051 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Paul Bakker5c721f92011-07-27 16:51:09 +00001053#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001054 if( is_critical )
1055 {
1056 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001057 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001058 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1059 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001060#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 }
1063
1064 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001065 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001066 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001067
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 return( 0 );
1069}
1070
1071/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001072 * X.509 CRL Entries
1073 */
1074static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001075 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001076 x509_crl_entry *entry )
1077{
Paul Bakker23986e52011-04-24 08:57:21 +00001078 int ret;
1079 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001080 x509_crl_entry *cur_entry = entry;
1081
1082 if( *p == end )
1083 return( 0 );
1084
Paul Bakker9be19372009-07-27 20:21:53 +00001085 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001086 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1087 {
1088 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1089 return( 0 );
1090
1091 return( ret );
1092 }
1093
Paul Bakker9be19372009-07-27 20:21:53 +00001094 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001095
1096 while( *p < end )
1097 {
Paul Bakker23986e52011-04-24 08:57:21 +00001098 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001099
1100 if( ( ret = asn1_get_tag( p, end, &len2,
1101 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1102 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001103 return( ret );
1104 }
1105
Paul Bakker9be19372009-07-27 20:21:53 +00001106 cur_entry->raw.tag = **p;
1107 cur_entry->raw.p = *p;
1108 cur_entry->raw.len = len2;
1109
Paul Bakkerd98030e2009-05-02 15:13:40 +00001110 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1111 return( ret );
1112
Paul Bakker91200182010-02-18 21:26:15 +00001113 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001114 return( ret );
1115
1116 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1117 return( ret );
1118
Paul Bakker74111d32011-01-15 16:57:55 +00001119 if ( *p < end )
1120 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001121 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1122 cur_entry = cur_entry->next;
1123 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1124 }
1125 }
1126
1127 return( 0 );
1128}
1129
Paul Bakker27d66162010-03-17 06:56:01 +00001130static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1131{
1132 if( sig_oid->len == 9 &&
1133 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1134 {
1135 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1136 {
1137 *sig_alg = sig_oid->p[8];
1138 return( 0 );
1139 }
1140
1141 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1142 {
1143 *sig_alg = sig_oid->p[8];
1144 return( 0 );
1145 }
1146
1147 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1148 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001149 if( sig_oid->len == 5 &&
1150 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1151 {
1152 *sig_alg = SIG_RSA_SHA1;
1153 return( 0 );
1154 }
Paul Bakker27d66162010-03-17 06:56:01 +00001155
1156 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1157}
1158
Paul Bakkerd98030e2009-05-02 15:13:40 +00001159/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 * Parse one or more certificates and add them to the chained list
1161 */
Paul Bakker23986e52011-04-24 08:57:21 +00001162int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001163{
Paul Bakker23986e52011-04-24 08:57:21 +00001164 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001165 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 unsigned char *p, *end;
1167 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001168#if defined(POLARSSL_PEM_C)
1169 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001170 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001171#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 crt = chain;
1174
Paul Bakker320a4b52009-03-28 18:52:39 +00001175 /*
1176 * Check for valid input
1177 */
1178 if( crt == NULL || buf == NULL )
1179 return( 1 );
1180
Paul Bakkere9581d62009-03-28 20:29:25 +00001181 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 crt = crt->next;
1183
1184 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001185 * Add new certificate on the end of the chain if needed.
1186 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001187 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001188 {
1189 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1190
Paul Bakker7d06ad22009-05-02 15:53:56 +00001191 if( crt->next == NULL )
1192 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001193 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001194 return( 1 );
1195 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001196
Paul Bakker7d06ad22009-05-02 15:53:56 +00001197 crt = crt->next;
1198 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001199 }
1200
Paul Bakker96743fc2011-02-12 14:30:57 +00001201#if defined(POLARSSL_PEM_C)
1202 pem_init( &pem );
1203 ret = pem_read_buffer( &pem,
1204 "-----BEGIN CERTIFICATE-----",
1205 "-----END CERTIFICATE-----",
1206 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Paul Bakker96743fc2011-02-12 14:30:57 +00001208 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001210 /*
1211 * Was PEM encoded
1212 */
1213 buflen -= use_len;
1214 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001215
1216 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001217 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001219 p = pem.buf;
1220 pem.buf = NULL;
1221 len = pem.buflen;
1222 pem_free( &pem );
1223 }
1224 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1225 {
1226 pem_free( &pem );
1227 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 }
1229 else
1230 {
1231 /*
1232 * nope, copy the raw DER data
1233 */
1234 p = (unsigned char *) malloc( len = buflen );
1235
1236 if( p == NULL )
1237 return( 1 );
1238
1239 memcpy( p, buf, buflen );
1240
1241 buflen = 0;
1242 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001243#else
1244 p = (unsigned char *) malloc( len = buflen );
1245
1246 if( p == NULL )
1247 return( 1 );
1248
1249 memcpy( p, buf, buflen );
1250
1251 buflen = 0;
1252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 crt->raw.p = p;
1255 crt->raw.len = len;
1256 end = p + len;
1257
1258 /*
1259 * Certificate ::= SEQUENCE {
1260 * tbsCertificate TBSCertificate,
1261 * signatureAlgorithm AlgorithmIdentifier,
1262 * signatureValue BIT STRING }
1263 */
1264 if( ( ret = asn1_get_tag( &p, end, &len,
1265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1266 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001267 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001268 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
1270
Paul Bakker23986e52011-04-24 08:57:21 +00001271 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001275 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
1277
1278 /*
1279 * TBSCertificate ::= SEQUENCE {
1280 */
1281 crt->tbs.p = p;
1282
1283 if( ( ret = asn1_get_tag( &p, end, &len,
1284 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1285 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001286 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001287 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 }
1289
1290 end = p + len;
1291 crt->tbs.len = end - crt->tbs.p;
1292
1293 /*
1294 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1295 *
1296 * CertificateSerialNumber ::= INTEGER
1297 *
1298 * signature AlgorithmIdentifier
1299 */
1300 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1301 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1302 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1303 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001304 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 return( ret );
1306 }
1307
1308 crt->version++;
1309
1310 if( crt->version > 3 )
1311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001313 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
Paul Bakker27d66162010-03-17 06:56:01 +00001316 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001318 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001319 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
1321
1322 /*
1323 * issuer Name
1324 */
1325 crt->issuer_raw.p = p;
1326
1327 if( ( ret = asn1_get_tag( &p, end, &len,
1328 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001331 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
1334 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339
1340 crt->issuer_raw.len = p - crt->issuer_raw.p;
1341
1342 /*
1343 * Validity ::= SEQUENCE {
1344 * notBefore Time,
1345 * notAfter Time }
1346 *
1347 */
1348 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1349 &crt->valid_to ) ) != 0 )
1350 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001351 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 return( ret );
1353 }
1354
1355 /*
1356 * subject Name
1357 */
1358 crt->subject_raw.p = p;
1359
1360 if( ( ret = asn1_get_tag( &p, end, &len,
1361 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001364 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366
1367 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 return( ret );
1371 }
1372
1373 crt->subject_raw.len = p - crt->subject_raw.p;
1374
1375 /*
1376 * SubjectPublicKeyInfo ::= SEQUENCE
1377 * algorithm AlgorithmIdentifier,
1378 * subjectPublicKey BIT STRING }
1379 */
1380 if( ( ret = asn1_get_tag( &p, end, &len,
1381 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1382 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001383 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001384 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386
1387 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1388 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1389 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001390 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 return( ret );
1392 }
1393
1394 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1395 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001396 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 return( ret );
1398 }
1399
1400 crt->rsa.len = mpi_size( &crt->rsa.N );
1401
1402 /*
1403 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1404 * -- If present, version shall be v2 or v3
1405 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1406 * -- If present, version shall be v2 or v3
1407 * extensions [3] EXPLICIT Extensions OPTIONAL
1408 * -- If present, version shall be v3
1409 */
1410 if( crt->version == 2 || crt->version == 3 )
1411 {
1412 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1413 if( ret != 0 )
1414 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 return( ret );
1417 }
1418 }
1419
1420 if( crt->version == 2 || crt->version == 3 )
1421 {
1422 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1423 if( ret != 0 )
1424 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001425 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 return( ret );
1427 }
1428 }
1429
1430 if( crt->version == 3 )
1431 {
Paul Bakker74111d32011-01-15 16:57:55 +00001432 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 if( ret != 0 )
1434 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001435 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 return( ret );
1437 }
1438 }
1439
1440 if( p != end )
1441 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001442 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001443 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001444 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 }
1446
1447 end = crt->raw.p + crt->raw.len;
1448
1449 /*
1450 * signatureAlgorithm AlgorithmIdentifier,
1451 * signatureValue BIT STRING
1452 */
1453 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1454 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 return( ret );
1457 }
1458
Paul Bakker320a4b52009-03-28 18:52:39 +00001459 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001462 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 }
1464
1465 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1466 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001467 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 return( ret );
1469 }
1470
1471 if( p != end )
1472 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001473 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001474 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001475 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
1477
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001479 {
1480 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1481
Paul Bakker7d06ad22009-05-02 15:53:56 +00001482 if( crt->next == NULL )
1483 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001484 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001485 return( 1 );
1486 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001487
Paul Bakker7d06ad22009-05-02 15:53:56 +00001488 crt = crt->next;
1489 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001490
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
1494 return( 0 );
1495}
1496
1497/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498 * Parse one or more CRLs and add them to the chained list
1499 */
Paul Bakker23986e52011-04-24 08:57:21 +00001500int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001501{
Paul Bakker23986e52011-04-24 08:57:21 +00001502 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001503 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001504 unsigned char *p, *end;
1505 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001506#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001507 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001508 pem_context pem;
1509#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001510
1511 crl = chain;
1512
1513 /*
1514 * Check for valid input
1515 */
1516 if( crl == NULL || buf == NULL )
1517 return( 1 );
1518
1519 while( crl->version != 0 && crl->next != NULL )
1520 crl = crl->next;
1521
1522 /*
1523 * Add new CRL on the end of the chain if needed.
1524 */
1525 if ( crl->version != 0 && crl->next == NULL)
1526 {
1527 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1528
Paul Bakker7d06ad22009-05-02 15:53:56 +00001529 if( crl->next == NULL )
1530 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001531 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001532 return( 1 );
1533 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001534
Paul Bakker7d06ad22009-05-02 15:53:56 +00001535 crl = crl->next;
1536 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001537 }
1538
Paul Bakker96743fc2011-02-12 14:30:57 +00001539#if defined(POLARSSL_PEM_C)
1540 pem_init( &pem );
1541 ret = pem_read_buffer( &pem,
1542 "-----BEGIN X509 CRL-----",
1543 "-----END X509 CRL-----",
1544 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001545
Paul Bakker96743fc2011-02-12 14:30:57 +00001546 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001547 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001548 /*
1549 * Was PEM encoded
1550 */
1551 buflen -= use_len;
1552 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001553
1554 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001555 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001557 p = pem.buf;
1558 pem.buf = NULL;
1559 len = pem.buflen;
1560 pem_free( &pem );
1561 }
1562 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1563 {
1564 pem_free( &pem );
1565 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566 }
1567 else
1568 {
1569 /*
1570 * nope, copy the raw DER data
1571 */
1572 p = (unsigned char *) malloc( len = buflen );
1573
1574 if( p == NULL )
1575 return( 1 );
1576
1577 memcpy( p, buf, buflen );
1578
1579 buflen = 0;
1580 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001581#else
1582 p = (unsigned char *) malloc( len = buflen );
1583
1584 if( p == NULL )
1585 return( 1 );
1586
1587 memcpy( p, buf, buflen );
1588
1589 buflen = 0;
1590#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001591
1592 crl->raw.p = p;
1593 crl->raw.len = len;
1594 end = p + len;
1595
1596 /*
1597 * CertificateList ::= SEQUENCE {
1598 * tbsCertList TBSCertList,
1599 * signatureAlgorithm AlgorithmIdentifier,
1600 * signatureValue BIT STRING }
1601 */
1602 if( ( ret = asn1_get_tag( &p, end, &len,
1603 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1604 {
1605 x509_crl_free( crl );
1606 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1607 }
1608
Paul Bakker23986e52011-04-24 08:57:21 +00001609 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610 {
1611 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001612 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1614 }
1615
1616 /*
1617 * TBSCertList ::= SEQUENCE {
1618 */
1619 crl->tbs.p = p;
1620
1621 if( ( ret = asn1_get_tag( &p, end, &len,
1622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1623 {
1624 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001625 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626 }
1627
1628 end = p + len;
1629 crl->tbs.len = end - crl->tbs.p;
1630
1631 /*
1632 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1633 * -- if present, MUST be v2
1634 *
1635 * signature AlgorithmIdentifier
1636 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001637 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001638 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1639 {
1640 x509_crl_free( crl );
1641 return( ret );
1642 }
1643
1644 crl->version++;
1645
1646 if( crl->version > 2 )
1647 {
1648 x509_crl_free( crl );
1649 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1650 }
1651
Paul Bakker27d66162010-03-17 06:56:01 +00001652 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653 {
1654 x509_crl_free( crl );
1655 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1656 }
1657
1658 /*
1659 * issuer Name
1660 */
1661 crl->issuer_raw.p = p;
1662
1663 if( ( ret = asn1_get_tag( &p, end, &len,
1664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1665 {
1666 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001667 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668 }
1669
1670 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1671 {
1672 x509_crl_free( crl );
1673 return( ret );
1674 }
1675
1676 crl->issuer_raw.len = p - crl->issuer_raw.p;
1677
1678 /*
1679 * thisUpdate Time
1680 * nextUpdate Time OPTIONAL
1681 */
Paul Bakker91200182010-02-18 21:26:15 +00001682 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683 {
1684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687
Paul Bakker91200182010-02-18 21:26:15 +00001688 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001689 {
Paul Bakker9d781402011-05-09 16:17:09 +00001690 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001691 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001692 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001693 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001694 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 x509_crl_free( crl );
1696 return( ret );
1697 }
1698 }
1699
1700 /*
1701 * revokedCertificates SEQUENCE OF SEQUENCE {
1702 * userCertificate CertificateSerialNumber,
1703 * revocationDate Time,
1704 * crlEntryExtensions Extensions OPTIONAL
1705 * -- if present, MUST be v2
1706 * } OPTIONAL
1707 */
1708 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1709 {
1710 x509_crl_free( crl );
1711 return( ret );
1712 }
1713
1714 /*
1715 * crlExtensions EXPLICIT Extensions OPTIONAL
1716 * -- if present, MUST be v2
1717 */
1718 if( crl->version == 2 )
1719 {
1720 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1721
1722 if( ret != 0 )
1723 {
1724 x509_crl_free( crl );
1725 return( ret );
1726 }
1727 }
1728
1729 if( p != end )
1730 {
1731 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001732 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1734 }
1735
1736 end = crl->raw.p + crl->raw.len;
1737
1738 /*
1739 * signatureAlgorithm AlgorithmIdentifier,
1740 * signatureValue BIT STRING
1741 */
1742 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1743 {
1744 x509_crl_free( crl );
1745 return( ret );
1746 }
1747
1748 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1749 {
1750 x509_crl_free( crl );
1751 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1752 }
1753
1754 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1755 {
1756 x509_crl_free( crl );
1757 return( ret );
1758 }
1759
1760 if( p != end )
1761 {
1762 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001763 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1765 }
1766
1767 if( buflen > 0 )
1768 {
1769 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1770
Paul Bakker7d06ad22009-05-02 15:53:56 +00001771 if( crl->next == NULL )
1772 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001774 return( 1 );
1775 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776
Paul Bakker7d06ad22009-05-02 15:53:56 +00001777 crl = crl->next;
1778 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779
1780 return( x509parse_crl( crl, buf, buflen ) );
1781 }
1782
1783 return( 0 );
1784}
1785
Paul Bakker335db3f2011-04-25 15:28:35 +00001786#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001787/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001788 * Load all data from a file into a given buffer.
1789 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001790int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001791{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001793
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 if( ( f = fopen( path, "rb" ) ) == NULL )
1795 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001796
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797 fseek( f, 0, SEEK_END );
1798 *n = (size_t) ftell( f );
1799 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001800
Paul Bakkerd98030e2009-05-02 15:13:40 +00001801 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1802 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001803
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 if( fread( *buf, 1, *n, f ) != *n )
1805 {
1806 fclose( f );
1807 free( *buf );
1808 return( 1 );
1809 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001810
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001812
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001814
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001816}
1817
1818/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001819 * Load one or more certificates and add them to the chained list
1820 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001821int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001822{
1823 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 size_t n;
1825 unsigned char *buf;
1826
Paul Bakker2b245eb2009-04-19 18:44:26 +00001827 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 return( 1 );
1829
Paul Bakker27fdf462011-06-09 13:55:13 +00001830 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
1832 memset( buf, 0, n + 1 );
1833 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
1835 return( ret );
1836}
1837
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838/*
1839 * Load one or more CRLs and add them to the chained list
1840 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001841int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842{
1843 int ret;
1844 size_t n;
1845 unsigned char *buf;
1846
1847 if ( load_file( path, &buf, &n ) )
1848 return( 1 );
1849
Paul Bakker27fdf462011-06-09 13:55:13 +00001850 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001851
1852 memset( buf, 0, n + 1 );
1853 free( buf );
1854
1855 return( ret );
1856}
1857
Paul Bakker5121ce52009-01-03 21:22:43 +00001858/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001859 * Load and parse a private RSA key
1860 */
1861int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1862{
1863 int ret;
1864 size_t n;
1865 unsigned char *buf;
1866
1867 if ( load_file( path, &buf, &n ) )
1868 return( 1 );
1869
1870 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001871 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001872 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001873 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001874 (unsigned char *) pwd, strlen( pwd ) );
1875
1876 memset( buf, 0, n + 1 );
1877 free( buf );
1878
1879 return( ret );
1880}
1881
1882/*
1883 * Load and parse a public RSA key
1884 */
1885int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1886{
1887 int ret;
1888 size_t n;
1889 unsigned char *buf;
1890
1891 if ( load_file( path, &buf, &n ) )
1892 return( 1 );
1893
Paul Bakker27fdf462011-06-09 13:55:13 +00001894 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001895
1896 memset( buf, 0, n + 1 );
1897 free( buf );
1898
1899 return( ret );
1900}
1901#endif /* POLARSSL_FS_IO */
1902
1903/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 * Parse a private RSA key
1905 */
Paul Bakker23986e52011-04-24 08:57:21 +00001906int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1907 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001908{
Paul Bakker23986e52011-04-24 08:57:21 +00001909 int ret;
1910 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001912 unsigned char *p_alt;
1913 x509_buf pk_alg_oid;
1914
Paul Bakker96743fc2011-02-12 14:30:57 +00001915#if defined(POLARSSL_PEM_C)
1916 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Paul Bakker96743fc2011-02-12 14:30:57 +00001918 pem_init( &pem );
1919 ret = pem_read_buffer( &pem,
1920 "-----BEGIN RSA PRIVATE KEY-----",
1921 "-----END RSA PRIVATE KEY-----",
1922 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Paul Bakkered56b222011-07-13 11:26:43 +00001924 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1925 {
1926 ret = pem_read_buffer( &pem,
1927 "-----BEGIN PRIVATE KEY-----",
1928 "-----END PRIVATE KEY-----",
1929 key, pwd, pwdlen, &len );
1930 }
1931
Paul Bakker96743fc2011-02-12 14:30:57 +00001932 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001934 /*
1935 * Was PEM encoded
1936 */
1937 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001939 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001940 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001941 pem_free( &pem );
1942 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
Paul Bakker96743fc2011-02-12 14:30:57 +00001945 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1946#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001947 ((void) pwd);
1948 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001949 p = (unsigned char *) key;
1950#endif
1951 end = p + keylen;
1952
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001954 * Note: Depending on the type of private key file one can expect either a
1955 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1956 *
1957 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001958 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001959 * algorithm AlgorithmIdentifier,
1960 * PrivateKey BIT STRING
1961 * }
1962 *
1963 * AlgorithmIdentifier ::= SEQUENCE {
1964 * algorithm OBJECT IDENTIFIER,
1965 * parameters ANY DEFINED BY algorithm OPTIONAL
1966 * }
1967 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 * RSAPrivateKey ::= SEQUENCE {
1969 * version Version,
1970 * modulus INTEGER, -- n
1971 * publicExponent INTEGER, -- e
1972 * privateExponent INTEGER, -- d
1973 * prime1 INTEGER, -- p
1974 * prime2 INTEGER, -- q
1975 * exponent1 INTEGER, -- d mod (p-1)
1976 * exponent2 INTEGER, -- d mod (q-1)
1977 * coefficient INTEGER, -- (inverse of q) mod p
1978 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1979 * }
1980 */
1981 if( ( ret = asn1_get_tag( &p, end, &len,
1982 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1983 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001984#if defined(POLARSSL_PEM_C)
1985 pem_free( &pem );
1986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001988 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 }
1990
1991 end = p + len;
1992
1993 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1994 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001995#if defined(POLARSSL_PEM_C)
1996 pem_free( &pem );
1997#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001998 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001999 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 }
2001
2002 if( rsa->ver != 0 )
2003 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002004#if defined(POLARSSL_PEM_C)
2005 pem_free( &pem );
2006#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002008 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 }
2010
Paul Bakkered56b222011-07-13 11:26:43 +00002011 p_alt = p;
2012
2013 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2014 {
2015 // Assume that we have the PKCS#1 format if wrong
2016 // tag was encountered
2017 //
2018 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2019 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2020 {
2021#if defined(POLARSSL_PEM_C)
2022 pem_free( &pem );
2023#endif
2024 rsa_free( rsa );
2025 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2026 }
2027 }
2028 else
2029 {
2030 int can_handle;
2031
2032 /*
2033 * only RSA keys handled at this time
2034 */
2035 can_handle = 0;
2036
2037 if( pk_alg_oid.len == 9 &&
2038 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2039 can_handle = 1;
2040
2041 if( pk_alg_oid.len == 9 &&
2042 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2043 {
2044 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2045 can_handle = 1;
2046
2047 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2048 can_handle = 1;
2049 }
2050
2051 if( pk_alg_oid.len == 5 &&
2052 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2053 can_handle = 1;
2054
2055 if( can_handle == 0 )
2056 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2057
2058 /*
2059 * Parse the PKCS#8 format
2060 */
2061
2062 p = p_alt;
2063 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2064 {
2065#if defined(POLARSSL_PEM_C)
2066 pem_free( &pem );
2067#endif
2068 rsa_free( rsa );
2069 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2070 }
2071
2072 if( ( end - p ) < 1 )
2073 {
2074#if defined(POLARSSL_PEM_C)
2075 pem_free( &pem );
2076#endif
2077 rsa_free( rsa );
2078 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2079 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2080 }
2081
2082 end = p + len;
2083
2084 if( ( ret = asn1_get_tag( &p, end, &len,
2085 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2086 {
2087#if defined(POLARSSL_PEM_C)
2088 pem_free( &pem );
2089#endif
2090 rsa_free( rsa );
2091 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2092 }
2093
2094 end = p + len;
2095
2096 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2097 {
2098#if defined(POLARSSL_PEM_C)
2099 pem_free( &pem );
2100#endif
2101 rsa_free( rsa );
2102 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2103 }
2104
2105 if( rsa->ver != 0 )
2106 {
2107#if defined(POLARSSL_PEM_C)
2108 pem_free( &pem );
2109#endif
2110 rsa_free( rsa );
2111 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2112 }
2113 }
2114
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2116 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2117 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2118 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2119 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2120 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2121 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2122 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2123 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002124#if defined(POLARSSL_PEM_C)
2125 pem_free( &pem );
2126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002128 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 }
2130
2131 rsa->len = mpi_size( &rsa->N );
2132
2133 if( p != end )
2134 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002135#if defined(POLARSSL_PEM_C)
2136 pem_free( &pem );
2137#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002140 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
2143 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2144 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002145#if defined(POLARSSL_PEM_C)
2146 pem_free( &pem );
2147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 rsa_free( rsa );
2149 return( ret );
2150 }
2151
Paul Bakker96743fc2011-02-12 14:30:57 +00002152#if defined(POLARSSL_PEM_C)
2153 pem_free( &pem );
2154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
2156 return( 0 );
2157}
2158
2159/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002160 * Parse a public RSA key
2161 */
Paul Bakker23986e52011-04-24 08:57:21 +00002162int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002163{
Paul Bakker23986e52011-04-24 08:57:21 +00002164 int ret;
2165 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002166 unsigned char *p, *end;
2167 x509_buf alg_oid;
2168#if defined(POLARSSL_PEM_C)
2169 pem_context pem;
2170
2171 pem_init( &pem );
2172 ret = pem_read_buffer( &pem,
2173 "-----BEGIN PUBLIC KEY-----",
2174 "-----END PUBLIC KEY-----",
2175 key, NULL, 0, &len );
2176
2177 if( ret == 0 )
2178 {
2179 /*
2180 * Was PEM encoded
2181 */
2182 keylen = pem.buflen;
2183 }
2184 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2185 {
2186 pem_free( &pem );
2187 return( ret );
2188 }
2189
2190 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2191#else
2192 p = (unsigned char *) key;
2193#endif
2194 end = p + keylen;
2195
2196 /*
2197 * PublicKeyInfo ::= SEQUENCE {
2198 * algorithm AlgorithmIdentifier,
2199 * PublicKey BIT STRING
2200 * }
2201 *
2202 * AlgorithmIdentifier ::= SEQUENCE {
2203 * algorithm OBJECT IDENTIFIER,
2204 * parameters ANY DEFINED BY algorithm OPTIONAL
2205 * }
2206 *
2207 * RSAPublicKey ::= SEQUENCE {
2208 * modulus INTEGER, -- n
2209 * publicExponent INTEGER -- e
2210 * }
2211 */
2212
2213 if( ( ret = asn1_get_tag( &p, end, &len,
2214 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2215 {
2216#if defined(POLARSSL_PEM_C)
2217 pem_free( &pem );
2218#endif
2219 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002220 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002221 }
2222
2223 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2224 {
2225#if defined(POLARSSL_PEM_C)
2226 pem_free( &pem );
2227#endif
2228 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002229 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002230 }
2231
2232 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2233 {
2234#if defined(POLARSSL_PEM_C)
2235 pem_free( &pem );
2236#endif
2237 rsa_free( rsa );
2238 return( ret );
2239 }
2240
2241 rsa->len = mpi_size( &rsa->N );
2242
2243#if defined(POLARSSL_PEM_C)
2244 pem_free( &pem );
2245#endif
2246
2247 return( 0 );
2248}
2249
Paul Bakkereaa89f82011-04-04 21:36:15 +00002250#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002251/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002252 * Parse DHM parameters
2253 */
Paul Bakker23986e52011-04-24 08:57:21 +00002254int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002255{
Paul Bakker23986e52011-04-24 08:57:21 +00002256 int ret;
2257 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002258 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002259#if defined(POLARSSL_PEM_C)
2260 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002261
Paul Bakker96743fc2011-02-12 14:30:57 +00002262 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002263
Paul Bakker96743fc2011-02-12 14:30:57 +00002264 ret = pem_read_buffer( &pem,
2265 "-----BEGIN DH PARAMETERS-----",
2266 "-----END DH PARAMETERS-----",
2267 dhmin, NULL, 0, &dhminlen );
2268
2269 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002270 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002271 /*
2272 * Was PEM encoded
2273 */
2274 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002275 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002276 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002277 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002278 pem_free( &pem );
2279 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002280 }
2281
Paul Bakker96743fc2011-02-12 14:30:57 +00002282 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2283#else
2284 p = (unsigned char *) dhmin;
2285#endif
2286 end = p + dhminlen;
2287
Paul Bakker1b57b062011-01-06 15:48:19 +00002288 memset( dhm, 0, sizeof( dhm_context ) );
2289
Paul Bakker1b57b062011-01-06 15:48:19 +00002290 /*
2291 * DHParams ::= SEQUENCE {
2292 * prime INTEGER, -- P
2293 * generator INTEGER, -- g
2294 * }
2295 */
2296 if( ( ret = asn1_get_tag( &p, end, &len,
2297 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2298 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002299#if defined(POLARSSL_PEM_C)
2300 pem_free( &pem );
2301#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002303 }
2304
2305 end = p + len;
2306
2307 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2308 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2309 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002310#if defined(POLARSSL_PEM_C)
2311 pem_free( &pem );
2312#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002313 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002314 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002315 }
2316
2317 if( p != end )
2318 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002319#if defined(POLARSSL_PEM_C)
2320 pem_free( &pem );
2321#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002322 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002323 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002324 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2325 }
2326
Paul Bakker96743fc2011-02-12 14:30:57 +00002327#if defined(POLARSSL_PEM_C)
2328 pem_free( &pem );
2329#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002330
2331 return( 0 );
2332}
2333
Paul Bakker335db3f2011-04-25 15:28:35 +00002334#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002335/*
2336 * Load and parse a private RSA key
2337 */
2338int x509parse_dhmfile( dhm_context *dhm, const char *path )
2339{
2340 int ret;
2341 size_t n;
2342 unsigned char *buf;
2343
2344 if ( load_file( path, &buf, &n ) )
2345 return( 1 );
2346
Paul Bakker27fdf462011-06-09 13:55:13 +00002347 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002348
2349 memset( buf, 0, n + 1 );
2350 free( buf );
2351
2352 return( ret );
2353}
Paul Bakker335db3f2011-04-25 15:28:35 +00002354#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002355#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002356
Paul Bakker5121ce52009-01-03 21:22:43 +00002357#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002358#include <stdarg.h>
2359
2360#if !defined vsnprintf
2361#define vsnprintf _vsnprintf
2362#endif // vsnprintf
2363
2364/*
2365 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2366 * Result value is not size of buffer needed, but -1 if no fit is possible.
2367 *
2368 * This fuction tries to 'fix' this by at least suggesting enlarging the
2369 * size by 20.
2370 */
2371int compat_snprintf(char *str, size_t size, const char *format, ...)
2372{
2373 va_list ap;
2374 int res = -1;
2375
2376 va_start( ap, format );
2377
2378 res = vsnprintf( str, size, format, ap );
2379
2380 va_end( ap );
2381
2382 // No quick fix possible
2383 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002384 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385
2386 return res;
2387}
2388
2389#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002390#endif
2391
Paul Bakkerd98030e2009-05-02 15:13:40 +00002392#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2393
2394#define SAFE_SNPRINTF() \
2395{ \
2396 if( ret == -1 ) \
2397 return( -1 ); \
2398 \
Paul Bakker23986e52011-04-24 08:57:21 +00002399 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002400 p[n - 1] = '\0'; \
2401 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2402 } \
2403 \
Paul Bakker23986e52011-04-24 08:57:21 +00002404 n -= (unsigned int) ret; \
2405 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002406}
2407
Paul Bakker5121ce52009-01-03 21:22:43 +00002408/*
2409 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002410 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002412int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002413{
Paul Bakker23986e52011-04-24 08:57:21 +00002414 int ret;
2415 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002417 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 char s[128], *p;
2419
2420 memset( s, 0, sizeof( s ) );
2421
2422 name = dn;
2423 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002424 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 while( name != NULL )
2427 {
Paul Bakker74111d32011-01-15 16:57:55 +00002428 if( name != dn )
2429 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002430 ret = snprintf( p, n, ", " );
2431 SAFE_SNPRINTF();
2432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
2434 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2435 {
2436 switch( name->oid.p[2] )
2437 {
2438 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002439 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002442 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
2444 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002445 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
2447 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002448 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
2450 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002451 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002454 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
2456 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002457 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 name->oid.p[2] );
2459 break;
2460 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002461 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 }
2463 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2464 {
2465 switch( name->oid.p[8] )
2466 {
2467 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002468 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
2470 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002471 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 name->oid.p[8] );
2473 break;
2474 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002475 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477 else
Paul Bakker74111d32011-01-15 16:57:55 +00002478 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002479 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002480 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
2483 for( i = 0; i < name->val.len; i++ )
2484 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002485 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 break;
2487
2488 c = name->val.p[i];
2489 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2490 s[i] = '?';
2491 else s[i] = c;
2492 }
2493 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 ret = snprintf( p, n, "%s", s );
2495 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 name = name->next;
2497 }
2498
Paul Bakker23986e52011-04-24 08:57:21 +00002499 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002500}
2501
2502/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002503 * Store the serial in printable form into buf; no more
2504 * than size characters will be written
2505 */
2506int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2507{
Paul Bakker23986e52011-04-24 08:57:21 +00002508 int ret;
2509 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002510 char *p;
2511
2512 p = buf;
2513 n = size;
2514
2515 nr = ( serial->len <= 32 )
2516 ? serial->len : 32;
2517
2518 for( i = 0; i < nr; i++ )
2519 {
2520 ret = snprintf( p, n, "%02X%s",
2521 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2522 SAFE_SNPRINTF();
2523 }
2524
Paul Bakker23986e52011-04-24 08:57:21 +00002525 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002526}
2527
2528/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002529 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002531int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2532 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533{
Paul Bakker23986e52011-04-24 08:57:21 +00002534 int ret;
2535 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002536 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
2538 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002539 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002543 SAFE_SNPRINTF();
2544 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002546 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Paul Bakkerdd476992011-01-16 21:34:59 +00002548 ret = x509parse_serial_gets( p, n, &crt->serial);
2549 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Paul Bakkerd98030e2009-05-02 15:13:40 +00002551 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2552 SAFE_SNPRINTF();
2553 ret = x509parse_dn_gets( p, n, &crt->issuer );
2554 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002555
Paul Bakkerd98030e2009-05-02 15:13:40 +00002556 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2557 SAFE_SNPRINTF();
2558 ret = x509parse_dn_gets( p, n, &crt->subject );
2559 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Paul Bakkerd98030e2009-05-02 15:13:40 +00002561 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2563 crt->valid_from.year, crt->valid_from.mon,
2564 crt->valid_from.day, crt->valid_from.hour,
2565 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002566 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Paul Bakkerd98030e2009-05-02 15:13:40 +00002568 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2570 crt->valid_to.year, crt->valid_to.mon,
2571 crt->valid_to.day, crt->valid_to.hour,
2572 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakkerd98030e2009-05-02 15:13:40 +00002575 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2576 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Paul Bakker27d66162010-03-17 06:56:01 +00002578 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002580 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2581 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2582 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2583 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2584 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2585 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2586 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2587 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2588 default: ret = snprintf( p, n, "???" ); break;
2589 }
2590 SAFE_SNPRINTF();
2591
2592 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002593 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002594 SAFE_SNPRINTF();
2595
Paul Bakker23986e52011-04-24 08:57:21 +00002596 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597}
2598
Paul Bakker74111d32011-01-15 16:57:55 +00002599/* Compare a given OID string with an OID x509_buf * */
2600#define OID_CMP(oid_str, oid_buf) \
2601 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2602 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2603
2604/*
2605 * Return an informational string describing the given OID
2606 */
2607const char *x509_oid_get_description( x509_buf *oid )
2608{
2609 if ( oid == NULL )
2610 return ( NULL );
2611
2612 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2613 return( STRING_SERVER_AUTH );
2614
2615 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2616 return( STRING_CLIENT_AUTH );
2617
2618 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2619 return( STRING_CODE_SIGNING );
2620
2621 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2622 return( STRING_EMAIL_PROTECTION );
2623
2624 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2625 return( STRING_TIME_STAMPING );
2626
2627 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2628 return( STRING_OCSP_SIGNING );
2629
2630 return( NULL );
2631}
2632
2633/* Return the x.y.z.... style numeric string for the given OID */
2634int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2635{
Paul Bakker23986e52011-04-24 08:57:21 +00002636 int ret;
2637 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002638 unsigned int value;
2639 char *p;
2640
2641 p = buf;
2642 n = size;
2643
2644 /* First byte contains first two dots */
2645 if( oid->len > 0 )
2646 {
2647 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2648 SAFE_SNPRINTF();
2649 }
2650
2651 /* TODO: value can overflow in value. */
2652 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002653 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002654 {
2655 value <<= 7;
2656 value += oid->p[i] & 0x7F;
2657
2658 if( !( oid->p[i] & 0x80 ) )
2659 {
2660 /* Last byte */
2661 ret = snprintf( p, n, ".%d", value );
2662 SAFE_SNPRINTF();
2663 value = 0;
2664 }
2665 }
2666
Paul Bakker23986e52011-04-24 08:57:21 +00002667 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002668}
2669
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670/*
2671 * Return an informational string about the CRL.
2672 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002673int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2674 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002675{
Paul Bakker23986e52011-04-24 08:57:21 +00002676 int ret;
2677 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002678 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002679 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680
2681 p = buf;
2682 n = size;
2683
2684 ret = snprintf( p, n, "%sCRL version : %d",
2685 prefix, crl->version );
2686 SAFE_SNPRINTF();
2687
2688 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2689 SAFE_SNPRINTF();
2690 ret = x509parse_dn_gets( p, n, &crl->issuer );
2691 SAFE_SNPRINTF();
2692
2693 ret = snprintf( p, n, "\n%sthis update : " \
2694 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2695 crl->this_update.year, crl->this_update.mon,
2696 crl->this_update.day, crl->this_update.hour,
2697 crl->this_update.min, crl->this_update.sec );
2698 SAFE_SNPRINTF();
2699
2700 ret = snprintf( p, n, "\n%snext update : " \
2701 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2702 crl->next_update.year, crl->next_update.mon,
2703 crl->next_update.day, crl->next_update.hour,
2704 crl->next_update.min, crl->next_update.sec );
2705 SAFE_SNPRINTF();
2706
2707 entry = &crl->entry;
2708
2709 ret = snprintf( p, n, "\n%sRevoked certificates:",
2710 prefix );
2711 SAFE_SNPRINTF();
2712
Paul Bakker9be19372009-07-27 20:21:53 +00002713 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002714 {
2715 ret = snprintf( p, n, "\n%sserial number: ",
2716 prefix );
2717 SAFE_SNPRINTF();
2718
2719 nr = ( entry->serial.len <= 32 )
2720 ? entry->serial.len : 32;
2721
Paul Bakker74111d32011-01-15 16:57:55 +00002722 for( i = 0; i < nr; i++ )
2723 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002724 ret = snprintf( p, n, "%02X%s",
2725 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2726 SAFE_SNPRINTF();
2727 }
2728
2729 ret = snprintf( p, n, " revocation date: " \
2730 "%04d-%02d-%02d %02d:%02d:%02d",
2731 entry->revocation_date.year, entry->revocation_date.mon,
2732 entry->revocation_date.day, entry->revocation_date.hour,
2733 entry->revocation_date.min, entry->revocation_date.sec );
2734 SAFE_SNPRINTF();
2735
2736 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
Paul Bakkerd98030e2009-05-02 15:13:40 +00002739 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2740 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Paul Bakker27d66162010-03-17 06:56:01 +00002742 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 {
2744 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2745 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2746 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2747 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2748 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2749 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2750 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2751 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2752 default: ret = snprintf( p, n, "???" ); break;
2753 }
2754 SAFE_SNPRINTF();
2755
Paul Bakker1e27bb22009-07-19 20:25:25 +00002756 ret = snprintf( p, n, "\n" );
2757 SAFE_SNPRINTF();
2758
Paul Bakker23986e52011-04-24 08:57:21 +00002759 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760}
2761
2762/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002763 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002765int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002766{
2767 struct tm *lt;
2768 time_t tt;
2769
2770 tt = time( NULL );
2771 lt = localtime( &tt );
2772
Paul Bakker40ea7de2009-05-03 10:18:48 +00002773 if( lt->tm_year > to->year - 1900 )
2774 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Paul Bakker40ea7de2009-05-03 10:18:48 +00002776 if( lt->tm_year == to->year - 1900 &&
2777 lt->tm_mon > to->mon - 1 )
2778 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
Paul Bakker40ea7de2009-05-03 10:18:48 +00002780 if( lt->tm_year == to->year - 1900 &&
2781 lt->tm_mon == to->mon - 1 &&
2782 lt->tm_mday > to->day )
2783 return( 1 );
2784
Paul Bakkerb6194992011-01-16 21:40:22 +00002785 if( lt->tm_year == to->year - 1900 &&
2786 lt->tm_mon == to->mon - 1 &&
2787 lt->tm_mday == to->day &&
2788 lt->tm_hour > to->hour - 1)
2789 return( 1 );
2790
2791 if( lt->tm_year == to->year - 1900 &&
2792 lt->tm_mon == to->mon - 1 &&
2793 lt->tm_mday == to->day &&
2794 lt->tm_hour == to->hour - 1 &&
2795 lt->tm_min > to->min - 1 )
2796 return( 1 );
2797
2798 if( lt->tm_year == to->year - 1900 &&
2799 lt->tm_mon == to->mon - 1 &&
2800 lt->tm_mday == to->day &&
2801 lt->tm_hour == to->hour - 1 &&
2802 lt->tm_min == to->min - 1 &&
2803 lt->tm_sec > to->sec - 1 )
2804 return( 1 );
2805
Paul Bakker40ea7de2009-05-03 10:18:48 +00002806 return( 0 );
2807}
2808
2809/*
2810 * Return 1 if the certificate is revoked, or 0 otherwise.
2811 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002812int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002813{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002814 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002815
2816 while( cur != NULL && cur->serial.len != 0 )
2817 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002818 if( crt->serial.len == cur->serial.len &&
2819 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002820 {
2821 if( x509parse_time_expired( &cur->revocation_date ) )
2822 return( 1 );
2823 }
2824
2825 cur = cur->next;
2826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
2828 return( 0 );
2829}
2830
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002831/*
2832 * Wrapper for x509 hashes.
2833 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002834 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002835 */
Paul Bakker23986e52011-04-24 08:57:21 +00002836static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 unsigned char *out )
2838{
2839 switch( alg )
2840 {
Paul Bakker40e46942009-01-03 21:51:57 +00002841#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002842 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002843#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002844#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002845 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002846#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002847#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002848 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002849#endif
2850#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002851 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002852#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002853#if defined(POLARSSL_SHA2_C)
2854 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2855 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2856#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002857#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002858 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2859 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2860#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002862 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863 break;
2864 }
2865}
2866
2867/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002868 * Check that the given certificate is valid accoring to the CRL.
2869 */
2870static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2871 x509_crl *crl_list)
2872{
2873 int flags = 0;
2874 int hash_id;
2875 unsigned char hash[64];
2876
2877 /*
2878 * TODO: What happens if no CRL is present?
2879 * Suggestion: Revocation state should be unknown if no CRL is present.
2880 * For backwards compatibility this is not yet implemented.
2881 */
2882
2883 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2884 {
2885 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2886 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2887 crl_list->issuer_raw.len ) != 0 )
2888 {
2889 crl_list = crl_list->next;
2890 continue;
2891 }
2892
2893 /*
2894 * Check if CRL is correctly signed by the trusted CA
2895 */
2896 hash_id = crl_list->sig_alg;
2897
2898 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2899
2900 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2901 0, hash, crl_list->sig.p ) == 0 )
2902 {
2903 /*
2904 * CRL is not trusted
2905 */
2906 flags |= BADCRL_NOT_TRUSTED;
2907 break;
2908 }
2909
2910 /*
2911 * Check for validity of CRL (Do not drop out)
2912 */
2913 if( x509parse_time_expired( &crl_list->next_update ) )
2914 flags |= BADCRL_EXPIRED;
2915
2916 /*
2917 * Check if certificate is revoked
2918 */
2919 if( x509parse_revoked(crt, crl_list) )
2920 {
2921 flags |= BADCERT_REVOKED;
2922 break;
2923 }
2924
2925 crl_list = crl_list->next;
2926 }
2927 return flags;
2928}
2929
2930/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 * Verify the certificate validity
2932 */
2933int x509parse_verify( x509_cert *crt,
2934 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002935 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002936 const char *cn, int *flags,
2937 int (*f_vrfy)(void *, x509_cert *, int, int),
2938 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939{
Paul Bakker23986e52011-04-24 08:57:21 +00002940 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 int hash_id;
2942 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002943 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002945 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Paul Bakker40ea7de2009-05-03 10:18:48 +00002947 *flags = 0;
2948
2949 if( x509parse_time_expired( &crt->valid_to ) )
2950 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
2952 if( cn != NULL )
2953 {
2954 name = &crt->subject;
2955 cn_len = strlen( cn );
2956
2957 while( name != NULL )
2958 {
2959 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2960 memcmp( name->val.p, cn, cn_len ) == 0 &&
2961 name->val.len == cn_len )
2962 break;
2963
2964 name = name->next;
2965 }
2966
2967 if( name == NULL )
2968 *flags |= BADCERT_CN_MISMATCH;
2969 }
2970
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 /*
2972 * Iterate upwards in the given cert chain,
2973 * ignoring any upper cert with CA != TRUE.
2974 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002975 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
2977 pathlen = 1;
2978
Paul Bakker76fd75a2011-01-16 21:12:10 +00002979 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002981 if( parent->ca_istrue == 0 ||
2982 crt->issuer_raw.len != parent->subject_raw.len ||
2983 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 crt->issuer_raw.len ) != 0 )
2985 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002986 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 continue;
2988 }
2989
Paul Bakker27d66162010-03-17 06:56:01 +00002990 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
2992 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2993
Paul Bakker76fd75a2011-01-16 21:12:10 +00002994 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2995 crt->sig.p ) != 0 )
2996 *flags |= BADCERT_NOT_TRUSTED;
2997
2998 /* Check trusted CA's CRL for the given crt */
2999 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003000
3001 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003002 if( NULL != f_vrfy )
3003 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003004 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003005 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003006 else
3007 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003008 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003009 else if( *flags != 0 )
3010 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
3012 pathlen++;
3013
Paul Bakker76fd75a2011-01-16 21:12:10 +00003014 crt = parent;
3015 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016 }
3017
3018 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003019 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003021 *flags |= BADCERT_NOT_TRUSTED;
3022
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003023 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 {
3025 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3026 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3027 crt->issuer_raw.len ) != 0 )
3028 {
3029 trust_ca = trust_ca->next;
3030 continue;
3031 }
3032
3033 if( trust_ca->max_pathlen > 0 &&
3034 trust_ca->max_pathlen < pathlen )
3035 break;
3036
Paul Bakker27d66162010-03-17 06:56:01 +00003037 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
3039 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3040
3041 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3042 0, hash, crt->sig.p ) == 0 )
3043 {
3044 /*
3045 * cert. is signed by a trusted CA
3046 */
3047 *flags &= ~BADCERT_NOT_TRUSTED;
3048 break;
3049 }
3050
3051 trust_ca = trust_ca->next;
3052 }
3053
Paul Bakker76fd75a2011-01-16 21:12:10 +00003054 /* Check trusted CA's CRL for the given crt */
3055 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003056
3057 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003058 if( NULL != f_vrfy )
3059 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003060 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003061 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003062 else
3063 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003064 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003065 else if( *flags != 0 )
3066 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003067
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 return( 0 );
3069}
3070
3071/*
3072 * Unallocate all certificate data
3073 */
3074void x509_free( x509_cert *crt )
3075{
3076 x509_cert *cert_cur = crt;
3077 x509_cert *cert_prv;
3078 x509_name *name_cur;
3079 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003080 x509_sequence *seq_cur;
3081 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
3083 if( crt == NULL )
3084 return;
3085
3086 do
3087 {
3088 rsa_free( &cert_cur->rsa );
3089
3090 name_cur = cert_cur->issuer.next;
3091 while( name_cur != NULL )
3092 {
3093 name_prv = name_cur;
3094 name_cur = name_cur->next;
3095 memset( name_prv, 0, sizeof( x509_name ) );
3096 free( name_prv );
3097 }
3098
3099 name_cur = cert_cur->subject.next;
3100 while( name_cur != NULL )
3101 {
3102 name_prv = name_cur;
3103 name_cur = name_cur->next;
3104 memset( name_prv, 0, sizeof( x509_name ) );
3105 free( name_prv );
3106 }
3107
Paul Bakker74111d32011-01-15 16:57:55 +00003108 seq_cur = cert_cur->ext_key_usage.next;
3109 while( seq_cur != NULL )
3110 {
3111 seq_prv = seq_cur;
3112 seq_cur = seq_cur->next;
3113 memset( seq_prv, 0, sizeof( x509_sequence ) );
3114 free( seq_prv );
3115 }
3116
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 if( cert_cur->raw.p != NULL )
3118 {
3119 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3120 free( cert_cur->raw.p );
3121 }
3122
3123 cert_cur = cert_cur->next;
3124 }
3125 while( cert_cur != NULL );
3126
3127 cert_cur = crt;
3128 do
3129 {
3130 cert_prv = cert_cur;
3131 cert_cur = cert_cur->next;
3132
3133 memset( cert_prv, 0, sizeof( x509_cert ) );
3134 if( cert_prv != crt )
3135 free( cert_prv );
3136 }
3137 while( cert_cur != NULL );
3138}
3139
Paul Bakkerd98030e2009-05-02 15:13:40 +00003140/*
3141 * Unallocate all CRL data
3142 */
3143void x509_crl_free( x509_crl *crl )
3144{
3145 x509_crl *crl_cur = crl;
3146 x509_crl *crl_prv;
3147 x509_name *name_cur;
3148 x509_name *name_prv;
3149 x509_crl_entry *entry_cur;
3150 x509_crl_entry *entry_prv;
3151
3152 if( crl == NULL )
3153 return;
3154
3155 do
3156 {
3157 name_cur = crl_cur->issuer.next;
3158 while( name_cur != NULL )
3159 {
3160 name_prv = name_cur;
3161 name_cur = name_cur->next;
3162 memset( name_prv, 0, sizeof( x509_name ) );
3163 free( name_prv );
3164 }
3165
3166 entry_cur = crl_cur->entry.next;
3167 while( entry_cur != NULL )
3168 {
3169 entry_prv = entry_cur;
3170 entry_cur = entry_cur->next;
3171 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3172 free( entry_prv );
3173 }
3174
3175 if( crl_cur->raw.p != NULL )
3176 {
3177 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3178 free( crl_cur->raw.p );
3179 }
3180
3181 crl_cur = crl_cur->next;
3182 }
3183 while( crl_cur != NULL );
3184
3185 crl_cur = crl;
3186 do
3187 {
3188 crl_prv = crl_cur;
3189 crl_cur = crl_cur->next;
3190
3191 memset( crl_prv, 0, sizeof( x509_crl ) );
3192 if( crl_prv != crl )
3193 free( crl_prv );
3194 }
3195 while( crl_cur != NULL );
3196}
3197
Paul Bakker40e46942009-01-03 21:51:57 +00003198#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Paul Bakker40e46942009-01-03 21:51:57 +00003200#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
3202/*
3203 * Checkup routine
3204 */
3205int x509_self_test( int verbose )
3206{
Paul Bakker5690efc2011-05-26 13:16:06 +00003207#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003208 int ret;
3209 int flags;
3210 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 x509_cert cacert;
3212 x509_cert clicert;
3213 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003214#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003215 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003216#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
3218 if( verbose != 0 )
3219 printf( " X.509 certificate load: " );
3220
3221 memset( &clicert, 0, sizeof( x509_cert ) );
3222
3223 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3224 strlen( test_cli_crt ) );
3225 if( ret != 0 )
3226 {
3227 if( verbose != 0 )
3228 printf( "failed\n" );
3229
3230 return( ret );
3231 }
3232
3233 memset( &cacert, 0, sizeof( x509_cert ) );
3234
3235 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3236 strlen( test_ca_crt ) );
3237 if( ret != 0 )
3238 {
3239 if( verbose != 0 )
3240 printf( "failed\n" );
3241
3242 return( ret );
3243 }
3244
3245 if( verbose != 0 )
3246 printf( "passed\n X.509 private key load: " );
3247
3248 i = strlen( test_ca_key );
3249 j = strlen( test_ca_pwd );
3250
Paul Bakker66b78b22011-03-25 14:22:50 +00003251 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3252
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 if( ( ret = x509parse_key( &rsa,
3254 (unsigned char *) test_ca_key, i,
3255 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3256 {
3257 if( verbose != 0 )
3258 printf( "failed\n" );
3259
3260 return( ret );
3261 }
3262
3263 if( verbose != 0 )
3264 printf( "passed\n X.509 signature verify: ");
3265
Paul Bakker23986e52011-04-24 08:57:21 +00003266 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 if( ret != 0 )
3268 {
Paul Bakker23986e52011-04-24 08:57:21 +00003269 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 if( verbose != 0 )
3271 printf( "failed\n" );
3272
3273 return( ret );
3274 }
3275
Paul Bakker5690efc2011-05-26 13:16:06 +00003276#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003278 printf( "passed\n X.509 DHM parameter load: " );
3279
3280 i = strlen( test_dhm_params );
3281 j = strlen( test_ca_pwd );
3282
3283 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3284 {
3285 if( verbose != 0 )
3286 printf( "failed\n" );
3287
3288 return( ret );
3289 }
3290
3291 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
3295 x509_free( &cacert );
3296 x509_free( &clicert );
3297 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003298#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003299 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
3302 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003303#else
3304 ((void) verbose);
3305 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3306#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003307}
3308
3309#endif
3310
3311#endif