blob: e359ca7acb4e0241889b702d22d36b843fb70ed9 [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/*
310 * CertificateSerialNumber ::= INTEGER
311 */
312static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000313 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 x509_buf *serial )
315{
316 int ret;
317
318 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000319 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000320 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
323 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000324 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000325 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 serial->tag = *(*p)++;
328
329 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000330 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 serial->p = *p;
333 *p += serial->len;
334
335 return( 0 );
336}
337
338/*
339 * AlgorithmIdentifier ::= SEQUENCE {
340 * algorithm OBJECT IDENTIFIER,
341 * parameters ANY DEFINED BY algorithm OPTIONAL }
342 */
343static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000344 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 x509_buf *alg )
346{
Paul Bakker23986e52011-04-24 08:57:21 +0000347 int ret;
348 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350 if( ( ret = asn1_get_tag( p, end, &len,
351 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000352 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354 end = *p + len;
355 alg->tag = **p;
356
357 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000358 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
360 alg->p = *p;
361 *p += alg->len;
362
363 if( *p == end )
364 return( 0 );
365
366 /*
367 * assume the algorithm parameters must be NULL
368 */
369 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000370 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000373 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000374 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 return( 0 );
377}
378
379/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 * AttributeTypeAndValue ::= SEQUENCE {
381 * type AttributeType,
382 * value AttributeValue }
383 *
384 * AttributeType ::= OBJECT IDENTIFIER
385 *
386 * AttributeValue ::= ANY DEFINED BY AttributeType
387 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000388static int x509_get_attr_type_value( unsigned char **p,
389 const unsigned char *end,
390 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000391{
Paul Bakker23986e52011-04-24 08:57:21 +0000392 int ret;
393 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 x509_buf *oid;
395 x509_buf *val;
396
397 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000399 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 oid = &cur->oid;
402 oid->tag = **p;
403
404 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000405 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 oid->p = *p;
408 *p += oid->len;
409
410 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000411 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000412 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
415 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
416 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000417 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000418 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 val = &cur->val;
421 val->tag = *(*p)++;
422
423 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000424 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 val->p = *p;
427 *p += val->len;
428
429 cur->next = NULL;
430
Paul Bakker400ff6f2011-02-20 10:40:16 +0000431 return( 0 );
432}
433
434/*
435 * RelativeDistinguishedName ::=
436 * SET OF AttributeTypeAndValue
437 *
438 * AttributeTypeAndValue ::= SEQUENCE {
439 * type AttributeType,
440 * value AttributeValue }
441 *
442 * AttributeType ::= OBJECT IDENTIFIER
443 *
444 * AttributeValue ::= ANY DEFINED BY AttributeType
445 */
446static int x509_get_name( unsigned char **p,
447 const unsigned char *end,
448 x509_name *cur )
449{
Paul Bakker23986e52011-04-24 08:57:21 +0000450 int ret;
451 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000452 const unsigned char *end2;
453 x509_name *use;
454
455 if( ( ret = asn1_get_tag( p, end, &len,
456 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000458
459 end2 = end;
460 end = *p + len;
461 use = cur;
462
463 do
464 {
465 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
466 return( ret );
467
468 if( *p != end )
469 {
470 use->next = (x509_name *) malloc(
471 sizeof( x509_name ) );
472
473 if( use->next == NULL )
474 return( 1 );
475
476 memset( use->next, 0, sizeof( x509_name ) );
477
478 use = use->next;
479 }
480 }
481 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 /*
484 * recurse until end of SEQUENCE is reached
485 */
486 if( *p == end2 )
487 return( 0 );
488
489 cur->next = (x509_name *) malloc(
490 sizeof( x509_name ) );
491
492 if( cur->next == NULL )
493 return( 1 );
494
495 return( x509_get_name( p, end2, cur->next ) );
496}
497
498/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 * Time ::= CHOICE {
500 * utcTime UTCTime,
501 * generalTime GeneralizedTime }
502 */
Paul Bakker91200182010-02-18 21:26:15 +0000503static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000504 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000505 x509_time *time )
506{
Paul Bakker23986e52011-04-24 08:57:21 +0000507 int ret;
508 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000509 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000510 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000511
Paul Bakker91200182010-02-18 21:26:15 +0000512 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000513 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
514 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000515
Paul Bakker91200182010-02-18 21:26:15 +0000516 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000517
Paul Bakker91200182010-02-18 21:26:15 +0000518 if ( tag == ASN1_UTC_TIME )
519 {
520 (*p)++;
521 ret = asn1_get_len( p, end, &len );
522
523 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000525
Paul Bakker91200182010-02-18 21:26:15 +0000526 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000527 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
528 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529
Paul Bakker91200182010-02-18 21:26:15 +0000530 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
531 &time->year, &time->mon, &time->day,
532 &time->hour, &time->min, &time->sec ) < 5 )
533 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000534
Paul Bakker400ff6f2011-02-20 10:40:16 +0000535 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000536 time->year += 1900;
537
538 *p += len;
539
540 return( 0 );
541 }
542 else if ( tag == ASN1_GENERALIZED_TIME )
543 {
544 (*p)++;
545 ret = asn1_get_len( p, end, &len );
546
547 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000548 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000549
550 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000551 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
552 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000553
554 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
555 &time->year, &time->mon, &time->day,
556 &time->hour, &time->min, &time->sec ) < 5 )
557 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
558
559 *p += len;
560
561 return( 0 );
562 }
563 else
Paul Bakker9d781402011-05-09 16:17:09 +0000564 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000565}
566
567
568/*
569 * Validity ::= SEQUENCE {
570 * notBefore Time,
571 * notAfter Time }
572 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000573static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000574 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 x509_time *from,
576 x509_time *to )
577{
Paul Bakker23986e52011-04-24 08:57:21 +0000578 int ret;
579 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( ( ret = asn1_get_tag( p, end, &len,
582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000583 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 end = *p + len;
586
Paul Bakker91200182010-02-18 21:26:15 +0000587 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000588 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Paul Bakker91200182010-02-18 21:26:15 +0000590 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000591 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000594 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000595 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597 return( 0 );
598}
599
600/*
601 * SubjectPublicKeyInfo ::= SEQUENCE {
602 * algorithm AlgorithmIdentifier,
603 * subjectPublicKey BIT STRING }
604 */
605static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 x509_buf *pk_alg_oid,
608 mpi *N, mpi *E )
609{
Paul Bakker23986e52011-04-24 08:57:21 +0000610 int ret, can_handle;
611 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 unsigned char *end2;
613
614 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
615 return( ret );
616
617 /*
618 * only RSA public keys handled at this time
619 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000620 can_handle = 0;
621
622 if( pk_alg_oid->len == 9 &&
623 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
624 can_handle = 1;
625
626 if( pk_alg_oid->len == 9 &&
627 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
628 {
629 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
630 can_handle = 1;
631
632 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
633 can_handle = 1;
634 }
635
636 if( pk_alg_oid->len == 5 &&
637 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
638 can_handle = 1;
639
640 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000641 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000644 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000648 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 end2 = *p + len;
651
652 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000653 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655 /*
656 * RSAPublicKey ::= SEQUENCE {
657 * modulus INTEGER, -- n
658 * publicExponent INTEGER -- e
659 * }
660 */
661 if( ( ret = asn1_get_tag( p, end2, &len,
662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000663 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
665 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000666 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000667 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
670 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000671 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000674 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000675 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677 return( 0 );
678}
679
680static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000681 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 x509_buf *sig )
683{
Paul Bakker23986e52011-04-24 08:57:21 +0000684 int ret;
685 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
687 sig->tag = **p;
688
689 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000690 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000691
Paul Bakker74111d32011-01-15 16:57:55 +0000692
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
696 sig->len = len;
697 sig->p = *p;
698
699 *p += len;
700
701 return( 0 );
702}
703
704/*
705 * X.509 v2/v3 unique identifier (not parsed)
706 */
707static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000708 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 x509_buf *uid, int n )
710{
711 int ret;
712
713 if( *p == end )
714 return( 0 );
715
716 uid->tag = **p;
717
718 if( ( ret = asn1_get_tag( p, end, &uid->len,
719 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
720 {
Paul Bakker40e46942009-01-03 21:51:57 +0000721 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 return( 0 );
723
724 return( ret );
725 }
726
727 uid->p = *p;
728 *p += uid->len;
729
730 return( 0 );
731}
732
733/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000734 * X.509 Extensions (No parsing of extensions, pointer should
735 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 */
737static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000738 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000739 x509_buf *ext )
Paul Bakker5121ce52009-01-03 21:22:43 +0000740{
Paul Bakker23986e52011-04-24 08:57:21 +0000741 int ret;
742 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
744 if( *p == end )
745 return( 0 );
746
747 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000748
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 if( ( ret = asn1_get_tag( p, end, &ext->len,
750 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753 ext->p = *p;
754 end = *p + ext->len;
755
756 /*
757 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
758 *
759 * Extension ::= SEQUENCE {
760 * extnID OBJECT IDENTIFIER,
761 * critical BOOLEAN DEFAULT FALSE,
762 * extnValue OCTET STRING }
763 */
764 if( ( ret = asn1_get_tag( p, end, &len,
765 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
768 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000770 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakkerd98030e2009-05-02 15:13:40 +0000772 return( 0 );
773}
774
775/*
776 * X.509 CRL v2 extensions (no extensions parsed yet.)
777 */
778static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000779 const unsigned char *end,
780 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000781{
Paul Bakker23986e52011-04-24 08:57:21 +0000782 int ret;
783 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000784
785 if( ( ret = x509_get_ext( p, end, ext ) ) != 0 )
786 {
787 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
788 return( 0 );
789
790 return( ret );
791 }
792
793 while( *p < end )
794 {
795 if( ( ret = asn1_get_tag( p, end, &len,
796 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000797 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000798
799 *p += len;
800 }
801
802 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000804 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
805
806 return( 0 );
807}
808
Paul Bakker74111d32011-01-15 16:57:55 +0000809static int x509_get_basic_constraints( unsigned char **p,
810 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000811 int *ca_istrue,
812 int *max_pathlen )
813{
Paul Bakker23986e52011-04-24 08:57:21 +0000814 int ret;
815 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000816
817 /*
818 * BasicConstraints ::= SEQUENCE {
819 * cA BOOLEAN DEFAULT FALSE,
820 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
821 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000822 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000823 *max_pathlen = 0; /* endless */
824
825 if( ( ret = asn1_get_tag( p, end, &len,
826 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000827 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000828
829 if( *p == end )
830 return 0;
831
Paul Bakker3cccddb2011-01-16 21:46:31 +0000832 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000833 {
834 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000835 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000836
837 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000839
Paul Bakker3cccddb2011-01-16 21:46:31 +0000840 if( *ca_istrue != 0 )
841 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000842 }
843
844 if( *p == end )
845 return 0;
846
847 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000852 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
853
854 (*max_pathlen)++;
855
Paul Bakker74111d32011-01-15 16:57:55 +0000856 return 0;
857}
858
859static int x509_get_ns_cert_type( unsigned char **p,
860 const unsigned char *end,
861 unsigned char *ns_cert_type)
862{
863 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000864 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000865
866 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000868
869 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000871 POLARSSL_ERR_ASN1_INVALID_LENGTH );
872
873 /* Get actual bitstring */
874 *ns_cert_type = *bs.p;
875 return 0;
876}
877
878static int x509_get_key_usage( unsigned char **p,
879 const unsigned char *end,
880 unsigned char *key_usage)
881{
882 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000883 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000884
885 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000887
888 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000890 POLARSSL_ERR_ASN1_INVALID_LENGTH );
891
892 /* Get actual bitstring */
893 *key_usage = *bs.p;
894 return 0;
895}
896
Paul Bakkerd98030e2009-05-02 15:13:40 +0000897/*
Paul Bakker74111d32011-01-15 16:57:55 +0000898 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
899 *
900 * KeyPurposeId ::= OBJECT IDENTIFIER
901 */
902static int x509_get_ext_key_usage( unsigned char **p,
903 const unsigned char *end,
904 x509_sequence *ext_key_usage)
905{
906 int ret;
907
908 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000910
911 /* Sequence length must be >= 1 */
912 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000913 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000914 POLARSSL_ERR_ASN1_INVALID_LENGTH );
915
916 return 0;
917}
918
919/*
920 * X.509 v3 extensions
921 *
922 * TODO: Perform all of the basic constraints tests required by the RFC
923 * TODO: Set values for undetected extensions to a sane default?
924 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000925 */
926static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000927 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000928 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000929{
Paul Bakker23986e52011-04-24 08:57:21 +0000930 int ret;
931 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000932 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933
Paul Bakker74111d32011-01-15 16:57:55 +0000934 if( ( ret = x509_get_ext( p, end, &crt->v3_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000935 {
936 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
937 return( 0 );
938
939 return( ret );
940 }
941
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 while( *p < end )
943 {
Paul Bakker74111d32011-01-15 16:57:55 +0000944 /*
945 * Extension ::= SEQUENCE {
946 * extnID OBJECT IDENTIFIER,
947 * critical BOOLEAN DEFAULT FALSE,
948 * extnValue OCTET STRING }
949 */
950 x509_buf extn_oid = {0, 0, NULL};
951 int is_critical = 0; /* DEFAULT FALSE */
952
Paul Bakker5121ce52009-01-03 21:22:43 +0000953 if( ( ret = asn1_get_tag( p, end, &len,
954 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000955 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000957 end_ext_data = *p + len;
958
Paul Bakker74111d32011-01-15 16:57:55 +0000959 /* Get extension ID */
960 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Paul Bakker74111d32011-01-15 16:57:55 +0000962 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000963 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Paul Bakker74111d32011-01-15 16:57:55 +0000965 extn_oid.p = *p;
966 *p += extn_oid.len;
967
968 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000969 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000970 POLARSSL_ERR_ASN1_OUT_OF_DATA );
971
972 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000973 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000974 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000975 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Paul Bakker74111d32011-01-15 16:57:55 +0000977 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000978 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000980 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000982 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000983
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000984 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000985 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000986 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Paul Bakker74111d32011-01-15 16:57:55 +0000988 /*
989 * Detect supported extensions
990 */
991 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
992 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 {
Paul Bakker74111d32011-01-15 16:57:55 +0000994 /* Parse basic constraints */
995 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000996 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000997 return ( ret );
998 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 }
Paul Bakker74111d32011-01-15 16:57:55 +00001000 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
1001 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
1002 {
1003 /* Parse netscape certificate type */
1004 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1005 &crt->ns_cert_type ) ) != 0 )
1006 return ( ret );
1007 crt->ext_types |= EXT_NS_CERT_TYPE;
1008 }
1009 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
1010 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
1011 {
1012 /* Parse key usage */
1013 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1014 &crt->key_usage ) ) != 0 )
1015 return ( ret );
1016 crt->ext_types |= EXT_KEY_USAGE;
1017 }
1018 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1019 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1020 {
1021 /* Parse extended key usage */
1022 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1023 &crt->ext_key_usage ) ) != 0 )
1024 return ( ret );
1025 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1026 }
1027 else
1028 {
1029 /* No parser found, skip extension */
1030 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Paul Bakker5c721f92011-07-27 16:51:09 +00001032#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001033 if( is_critical )
1034 {
1035 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001036 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001037 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1038 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001039#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001040 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 }
1042
1043 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001044 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001045 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 return( 0 );
1048}
1049
1050/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 * X.509 CRL Entries
1052 */
1053static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001054 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001055 x509_crl_entry *entry )
1056{
Paul Bakker23986e52011-04-24 08:57:21 +00001057 int ret;
1058 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001059 x509_crl_entry *cur_entry = entry;
1060
1061 if( *p == end )
1062 return( 0 );
1063
Paul Bakker9be19372009-07-27 20:21:53 +00001064 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1066 {
1067 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1068 return( 0 );
1069
1070 return( ret );
1071 }
1072
Paul Bakker9be19372009-07-27 20:21:53 +00001073 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001074
1075 while( *p < end )
1076 {
Paul Bakker23986e52011-04-24 08:57:21 +00001077 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078
1079 if( ( ret = asn1_get_tag( p, end, &len2,
1080 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1081 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 return( ret );
1083 }
1084
Paul Bakker9be19372009-07-27 20:21:53 +00001085 cur_entry->raw.tag = **p;
1086 cur_entry->raw.p = *p;
1087 cur_entry->raw.len = len2;
1088
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1090 return( ret );
1091
Paul Bakker91200182010-02-18 21:26:15 +00001092 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 return( ret );
1094
1095 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1096 return( ret );
1097
Paul Bakker74111d32011-01-15 16:57:55 +00001098 if ( *p < end )
1099 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001100 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1101 cur_entry = cur_entry->next;
1102 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1103 }
1104 }
1105
1106 return( 0 );
1107}
1108
Paul Bakker27d66162010-03-17 06:56:01 +00001109static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1110{
1111 if( sig_oid->len == 9 &&
1112 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1113 {
1114 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1115 {
1116 *sig_alg = sig_oid->p[8];
1117 return( 0 );
1118 }
1119
1120 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1121 {
1122 *sig_alg = sig_oid->p[8];
1123 return( 0 );
1124 }
1125
1126 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1127 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001128 if( sig_oid->len == 5 &&
1129 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1130 {
1131 *sig_alg = SIG_RSA_SHA1;
1132 return( 0 );
1133 }
Paul Bakker27d66162010-03-17 06:56:01 +00001134
1135 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1136}
1137
Paul Bakkerd98030e2009-05-02 15:13:40 +00001138/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 * Parse one or more certificates and add them to the chained list
1140 */
Paul Bakker23986e52011-04-24 08:57:21 +00001141int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001142{
Paul Bakker23986e52011-04-24 08:57:21 +00001143 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001144 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 unsigned char *p, *end;
1146 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001147#if defined(POLARSSL_PEM_C)
1148 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001149 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 crt = chain;
1153
Paul Bakker320a4b52009-03-28 18:52:39 +00001154 /*
1155 * Check for valid input
1156 */
1157 if( crt == NULL || buf == NULL )
1158 return( 1 );
1159
Paul Bakkere9581d62009-03-28 20:29:25 +00001160 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 crt = crt->next;
1162
1163 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001164 * Add new certificate on the end of the chain if needed.
1165 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001166 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001167 {
1168 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1169
Paul Bakker7d06ad22009-05-02 15:53:56 +00001170 if( crt->next == NULL )
1171 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001172 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001173 return( 1 );
1174 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001175
Paul Bakker7d06ad22009-05-02 15:53:56 +00001176 crt = crt->next;
1177 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001178 }
1179
Paul Bakker96743fc2011-02-12 14:30:57 +00001180#if defined(POLARSSL_PEM_C)
1181 pem_init( &pem );
1182 ret = pem_read_buffer( &pem,
1183 "-----BEGIN CERTIFICATE-----",
1184 "-----END CERTIFICATE-----",
1185 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
Paul Bakker96743fc2011-02-12 14:30:57 +00001187 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001189 /*
1190 * Was PEM encoded
1191 */
1192 buflen -= use_len;
1193 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
1195 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001196 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001198 p = pem.buf;
1199 pem.buf = NULL;
1200 len = pem.buflen;
1201 pem_free( &pem );
1202 }
1203 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1204 {
1205 pem_free( &pem );
1206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
1208 else
1209 {
1210 /*
1211 * nope, copy the raw DER data
1212 */
1213 p = (unsigned char *) malloc( len = buflen );
1214
1215 if( p == NULL )
1216 return( 1 );
1217
1218 memcpy( p, buf, buflen );
1219
1220 buflen = 0;
1221 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001222#else
1223 p = (unsigned char *) malloc( len = buflen );
1224
1225 if( p == NULL )
1226 return( 1 );
1227
1228 memcpy( p, buf, buflen );
1229
1230 buflen = 0;
1231#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
1233 crt->raw.p = p;
1234 crt->raw.len = len;
1235 end = p + len;
1236
1237 /*
1238 * Certificate ::= SEQUENCE {
1239 * tbsCertificate TBSCertificate,
1240 * signatureAlgorithm AlgorithmIdentifier,
1241 * signatureValue BIT STRING }
1242 */
1243 if( ( ret = asn1_get_tag( &p, end, &len,
1244 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001247 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 }
1249
Paul Bakker23986e52011-04-24 08:57:21 +00001250 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001253 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001254 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
1256
1257 /*
1258 * TBSCertificate ::= SEQUENCE {
1259 */
1260 crt->tbs.p = p;
1261
1262 if( ( ret = asn1_get_tag( &p, end, &len,
1263 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001266 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
1268
1269 end = p + len;
1270 crt->tbs.len = end - crt->tbs.p;
1271
1272 /*
1273 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1274 *
1275 * CertificateSerialNumber ::= INTEGER
1276 *
1277 * signature AlgorithmIdentifier
1278 */
1279 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1280 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1281 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1282 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001283 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 return( ret );
1285 }
1286
1287 crt->version++;
1288
1289 if( crt->version > 3 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001292 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
Paul Bakker27d66162010-03-17 06:56:01 +00001295 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001298 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 }
1300
1301 /*
1302 * issuer Name
1303 */
1304 crt->issuer_raw.p = p;
1305
1306 if( ( ret = asn1_get_tag( &p, end, &len,
1307 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001310 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
1312
1313 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1314 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001315 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 return( ret );
1317 }
1318
1319 crt->issuer_raw.len = p - crt->issuer_raw.p;
1320
1321 /*
1322 * Validity ::= SEQUENCE {
1323 * notBefore Time,
1324 * notAfter Time }
1325 *
1326 */
1327 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1328 &crt->valid_to ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 /*
1335 * subject Name
1336 */
1337 crt->subject_raw.p = p;
1338
1339 if( ( ret = asn1_get_tag( &p, end, &len,
1340 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001343 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
1345
1346 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1347 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001348 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 return( ret );
1350 }
1351
1352 crt->subject_raw.len = p - crt->subject_raw.p;
1353
1354 /*
1355 * SubjectPublicKeyInfo ::= SEQUENCE
1356 * algorithm AlgorithmIdentifier,
1357 * subjectPublicKey BIT STRING }
1358 */
1359 if( ( ret = asn1_get_tag( &p, end, &len,
1360 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1361 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001362 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001363 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365
1366 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1367 &crt->rsa.N, &crt->rsa.E ) ) != 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 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378
1379 crt->rsa.len = mpi_size( &crt->rsa.N );
1380
1381 /*
1382 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1383 * -- If present, version shall be v2 or v3
1384 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1385 * -- If present, version shall be v2 or v3
1386 * extensions [3] EXPLICIT Extensions OPTIONAL
1387 * -- If present, version shall be v3
1388 */
1389 if( crt->version == 2 || crt->version == 3 )
1390 {
1391 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1392 if( ret != 0 )
1393 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001394 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 return( ret );
1396 }
1397 }
1398
1399 if( crt->version == 2 || crt->version == 3 )
1400 {
1401 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1402 if( ret != 0 )
1403 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001404 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 return( ret );
1406 }
1407 }
1408
1409 if( crt->version == 3 )
1410 {
Paul Bakker74111d32011-01-15 16:57:55 +00001411 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 if( ret != 0 )
1413 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001414 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 return( ret );
1416 }
1417 }
1418
1419 if( p != end )
1420 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001421 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001422 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001423 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
1425
1426 end = crt->raw.p + crt->raw.len;
1427
1428 /*
1429 * signatureAlgorithm AlgorithmIdentifier,
1430 * signatureValue BIT STRING
1431 */
1432 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 return( ret );
1436 }
1437
Paul Bakker320a4b52009-03-28 18:52:39 +00001438 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001440 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001441 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443
1444 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1445 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 return( ret );
1448 }
1449
1450 if( p != end )
1451 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001452 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001453 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001454 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 }
1456
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001458 {
1459 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1460
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 if( crt->next == NULL )
1462 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001463 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001464 return( 1 );
1465 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001466
Paul Bakker7d06ad22009-05-02 15:53:56 +00001467 crt = crt->next;
1468 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001469
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
1473 return( 0 );
1474}
1475
1476/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001477 * Parse one or more CRLs and add them to the chained list
1478 */
Paul Bakker23986e52011-04-24 08:57:21 +00001479int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001480{
Paul Bakker23986e52011-04-24 08:57:21 +00001481 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001482 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001483 unsigned char *p, *end;
1484 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001485#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001486 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001487 pem_context pem;
1488#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001489
1490 crl = chain;
1491
1492 /*
1493 * Check for valid input
1494 */
1495 if( crl == NULL || buf == NULL )
1496 return( 1 );
1497
1498 while( crl->version != 0 && crl->next != NULL )
1499 crl = crl->next;
1500
1501 /*
1502 * Add new CRL on the end of the chain if needed.
1503 */
1504 if ( crl->version != 0 && crl->next == NULL)
1505 {
1506 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1507
Paul Bakker7d06ad22009-05-02 15:53:56 +00001508 if( crl->next == NULL )
1509 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001510 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001511 return( 1 );
1512 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001513
Paul Bakker7d06ad22009-05-02 15:53:56 +00001514 crl = crl->next;
1515 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516 }
1517
Paul Bakker96743fc2011-02-12 14:30:57 +00001518#if defined(POLARSSL_PEM_C)
1519 pem_init( &pem );
1520 ret = pem_read_buffer( &pem,
1521 "-----BEGIN X509 CRL-----",
1522 "-----END X509 CRL-----",
1523 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001524
Paul Bakker96743fc2011-02-12 14:30:57 +00001525 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001526 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001527 /*
1528 * Was PEM encoded
1529 */
1530 buflen -= use_len;
1531 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532
1533 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001534 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001536 p = pem.buf;
1537 pem.buf = NULL;
1538 len = pem.buflen;
1539 pem_free( &pem );
1540 }
1541 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1542 {
1543 pem_free( &pem );
1544 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001545 }
1546 else
1547 {
1548 /*
1549 * nope, copy the raw DER data
1550 */
1551 p = (unsigned char *) malloc( len = buflen );
1552
1553 if( p == NULL )
1554 return( 1 );
1555
1556 memcpy( p, buf, buflen );
1557
1558 buflen = 0;
1559 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001560#else
1561 p = (unsigned char *) malloc( len = buflen );
1562
1563 if( p == NULL )
1564 return( 1 );
1565
1566 memcpy( p, buf, buflen );
1567
1568 buflen = 0;
1569#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001570
1571 crl->raw.p = p;
1572 crl->raw.len = len;
1573 end = p + len;
1574
1575 /*
1576 * CertificateList ::= SEQUENCE {
1577 * tbsCertList TBSCertList,
1578 * signatureAlgorithm AlgorithmIdentifier,
1579 * signatureValue BIT STRING }
1580 */
1581 if( ( ret = asn1_get_tag( &p, end, &len,
1582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1583 {
1584 x509_crl_free( crl );
1585 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1586 }
1587
Paul Bakker23986e52011-04-24 08:57:21 +00001588 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 {
1590 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001591 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001592 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1593 }
1594
1595 /*
1596 * TBSCertList ::= SEQUENCE {
1597 */
1598 crl->tbs.p = p;
1599
1600 if( ( ret = asn1_get_tag( &p, end, &len,
1601 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1602 {
1603 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001604 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001605 }
1606
1607 end = p + len;
1608 crl->tbs.len = end - crl->tbs.p;
1609
1610 /*
1611 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1612 * -- if present, MUST be v2
1613 *
1614 * signature AlgorithmIdentifier
1615 */
1616 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1617 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1618 {
1619 x509_crl_free( crl );
1620 return( ret );
1621 }
1622
1623 crl->version++;
1624
1625 if( crl->version > 2 )
1626 {
1627 x509_crl_free( crl );
1628 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1629 }
1630
Paul Bakker27d66162010-03-17 06:56:01 +00001631 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632 {
1633 x509_crl_free( crl );
1634 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1635 }
1636
1637 /*
1638 * issuer Name
1639 */
1640 crl->issuer_raw.p = p;
1641
1642 if( ( ret = asn1_get_tag( &p, end, &len,
1643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 }
1648
1649 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1650 {
1651 x509_crl_free( crl );
1652 return( ret );
1653 }
1654
1655 crl->issuer_raw.len = p - crl->issuer_raw.p;
1656
1657 /*
1658 * thisUpdate Time
1659 * nextUpdate Time OPTIONAL
1660 */
Paul Bakker91200182010-02-18 21:26:15 +00001661 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 {
1663 x509_crl_free( crl );
1664 return( ret );
1665 }
1666
Paul Bakker91200182010-02-18 21:26:15 +00001667 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668 {
Paul Bakker9d781402011-05-09 16:17:09 +00001669 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001670 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001671 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001672 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001673 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001674 x509_crl_free( crl );
1675 return( ret );
1676 }
1677 }
1678
1679 /*
1680 * revokedCertificates SEQUENCE OF SEQUENCE {
1681 * userCertificate CertificateSerialNumber,
1682 * revocationDate Time,
1683 * crlEntryExtensions Extensions OPTIONAL
1684 * -- if present, MUST be v2
1685 * } OPTIONAL
1686 */
1687 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1688 {
1689 x509_crl_free( crl );
1690 return( ret );
1691 }
1692
1693 /*
1694 * crlExtensions EXPLICIT Extensions OPTIONAL
1695 * -- if present, MUST be v2
1696 */
1697 if( crl->version == 2 )
1698 {
1699 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1700
1701 if( ret != 0 )
1702 {
1703 x509_crl_free( crl );
1704 return( ret );
1705 }
1706 }
1707
1708 if( p != end )
1709 {
1710 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001711 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1713 }
1714
1715 end = crl->raw.p + crl->raw.len;
1716
1717 /*
1718 * signatureAlgorithm AlgorithmIdentifier,
1719 * signatureValue BIT STRING
1720 */
1721 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1722 {
1723 x509_crl_free( crl );
1724 return( ret );
1725 }
1726
1727 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1728 {
1729 x509_crl_free( crl );
1730 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1731 }
1732
1733 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1734 {
1735 x509_crl_free( crl );
1736 return( ret );
1737 }
1738
1739 if( p != end )
1740 {
1741 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001742 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001743 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1744 }
1745
1746 if( buflen > 0 )
1747 {
1748 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1749
Paul Bakker7d06ad22009-05-02 15:53:56 +00001750 if( crl->next == NULL )
1751 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001753 return( 1 );
1754 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755
Paul Bakker7d06ad22009-05-02 15:53:56 +00001756 crl = crl->next;
1757 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758
1759 return( x509parse_crl( crl, buf, buflen ) );
1760 }
1761
1762 return( 0 );
1763}
1764
Paul Bakker335db3f2011-04-25 15:28:35 +00001765#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001767 * Load all data from a file into a given buffer.
1768 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001769int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001770{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001772
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 if( ( f = fopen( path, "rb" ) ) == NULL )
1774 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001775
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776 fseek( f, 0, SEEK_END );
1777 *n = (size_t) ftell( f );
1778 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001779
Paul Bakkerd98030e2009-05-02 15:13:40 +00001780 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1781 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001782
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783 if( fread( *buf, 1, *n, f ) != *n )
1784 {
1785 fclose( f );
1786 free( *buf );
1787 return( 1 );
1788 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001789
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001791
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001793
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001795}
1796
1797/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 * Load one or more certificates and add them to the chained list
1799 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001800int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001801{
1802 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001803 size_t n;
1804 unsigned char *buf;
1805
Paul Bakker2b245eb2009-04-19 18:44:26 +00001806 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 return( 1 );
1808
Paul Bakker27fdf462011-06-09 13:55:13 +00001809 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
1811 memset( buf, 0, n + 1 );
1812 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 return( ret );
1815}
1816
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817/*
1818 * Load one or more CRLs and add them to the chained list
1819 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001820int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821{
1822 int ret;
1823 size_t n;
1824 unsigned char *buf;
1825
1826 if ( load_file( path, &buf, &n ) )
1827 return( 1 );
1828
Paul Bakker27fdf462011-06-09 13:55:13 +00001829 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001830
1831 memset( buf, 0, n + 1 );
1832 free( buf );
1833
1834 return( ret );
1835}
1836
Paul Bakker5121ce52009-01-03 21:22:43 +00001837/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001838 * Load and parse a private RSA key
1839 */
1840int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1841{
1842 int ret;
1843 size_t n;
1844 unsigned char *buf;
1845
1846 if ( load_file( path, &buf, &n ) )
1847 return( 1 );
1848
1849 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001850 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001851 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001852 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001853 (unsigned char *) pwd, strlen( pwd ) );
1854
1855 memset( buf, 0, n + 1 );
1856 free( buf );
1857
1858 return( ret );
1859}
1860
1861/*
1862 * Load and parse a public RSA key
1863 */
1864int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1865{
1866 int ret;
1867 size_t n;
1868 unsigned char *buf;
1869
1870 if ( load_file( path, &buf, &n ) )
1871 return( 1 );
1872
Paul Bakker27fdf462011-06-09 13:55:13 +00001873 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001874
1875 memset( buf, 0, n + 1 );
1876 free( buf );
1877
1878 return( ret );
1879}
1880#endif /* POLARSSL_FS_IO */
1881
1882/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 * Parse a private RSA key
1884 */
Paul Bakker23986e52011-04-24 08:57:21 +00001885int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1886 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001887{
Paul Bakker23986e52011-04-24 08:57:21 +00001888 int ret;
1889 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001891 unsigned char *p_alt;
1892 x509_buf pk_alg_oid;
1893
Paul Bakker96743fc2011-02-12 14:30:57 +00001894#if defined(POLARSSL_PEM_C)
1895 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Paul Bakker96743fc2011-02-12 14:30:57 +00001897 pem_init( &pem );
1898 ret = pem_read_buffer( &pem,
1899 "-----BEGIN RSA PRIVATE KEY-----",
1900 "-----END RSA PRIVATE KEY-----",
1901 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
Paul Bakkered56b222011-07-13 11:26:43 +00001903 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1904 {
1905 ret = pem_read_buffer( &pem,
1906 "-----BEGIN PRIVATE KEY-----",
1907 "-----END PRIVATE KEY-----",
1908 key, pwd, pwdlen, &len );
1909 }
1910
Paul Bakker96743fc2011-02-12 14:30:57 +00001911 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001913 /*
1914 * Was PEM encoded
1915 */
1916 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001918 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001919 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001920 pem_free( &pem );
1921 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Paul Bakker96743fc2011-02-12 14:30:57 +00001924 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1925#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001926 ((void) pwd);
1927 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001928 p = (unsigned char *) key;
1929#endif
1930 end = p + keylen;
1931
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001933 * Note: Depending on the type of private key file one can expect either a
1934 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1935 *
1936 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001937 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001938 * algorithm AlgorithmIdentifier,
1939 * PrivateKey BIT STRING
1940 * }
1941 *
1942 * AlgorithmIdentifier ::= SEQUENCE {
1943 * algorithm OBJECT IDENTIFIER,
1944 * parameters ANY DEFINED BY algorithm OPTIONAL
1945 * }
1946 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 * RSAPrivateKey ::= SEQUENCE {
1948 * version Version,
1949 * modulus INTEGER, -- n
1950 * publicExponent INTEGER, -- e
1951 * privateExponent INTEGER, -- d
1952 * prime1 INTEGER, -- p
1953 * prime2 INTEGER, -- q
1954 * exponent1 INTEGER, -- d mod (p-1)
1955 * exponent2 INTEGER, -- d mod (q-1)
1956 * coefficient INTEGER, -- (inverse of q) mod p
1957 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1958 * }
1959 */
1960 if( ( ret = asn1_get_tag( &p, end, &len,
1961 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1962 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001963#if defined(POLARSSL_PEM_C)
1964 pem_free( &pem );
1965#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001967 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001968 }
1969
1970 end = p + len;
1971
1972 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1973 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001974#if defined(POLARSSL_PEM_C)
1975 pem_free( &pem );
1976#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001978 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
1981 if( rsa->ver != 0 )
1982 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001983#if defined(POLARSSL_PEM_C)
1984 pem_free( &pem );
1985#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001987 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
1989
Paul Bakkered56b222011-07-13 11:26:43 +00001990 p_alt = p;
1991
1992 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1993 {
1994 // Assume that we have the PKCS#1 format if wrong
1995 // tag was encountered
1996 //
1997 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1998 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1999 {
2000#if defined(POLARSSL_PEM_C)
2001 pem_free( &pem );
2002#endif
2003 rsa_free( rsa );
2004 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2005 }
2006 }
2007 else
2008 {
2009 int can_handle;
2010
2011 /*
2012 * only RSA keys handled at this time
2013 */
2014 can_handle = 0;
2015
2016 if( pk_alg_oid.len == 9 &&
2017 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2018 can_handle = 1;
2019
2020 if( pk_alg_oid.len == 9 &&
2021 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2022 {
2023 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2024 can_handle = 1;
2025
2026 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2027 can_handle = 1;
2028 }
2029
2030 if( pk_alg_oid.len == 5 &&
2031 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2032 can_handle = 1;
2033
2034 if( can_handle == 0 )
2035 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2036
2037 /*
2038 * Parse the PKCS#8 format
2039 */
2040
2041 p = p_alt;
2042 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2043 {
2044#if defined(POLARSSL_PEM_C)
2045 pem_free( &pem );
2046#endif
2047 rsa_free( rsa );
2048 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2049 }
2050
2051 if( ( end - p ) < 1 )
2052 {
2053#if defined(POLARSSL_PEM_C)
2054 pem_free( &pem );
2055#endif
2056 rsa_free( rsa );
2057 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2058 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2059 }
2060
2061 end = p + len;
2062
2063 if( ( ret = asn1_get_tag( &p, end, &len,
2064 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2065 {
2066#if defined(POLARSSL_PEM_C)
2067 pem_free( &pem );
2068#endif
2069 rsa_free( rsa );
2070 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2071 }
2072
2073 end = p + len;
2074
2075 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2076 {
2077#if defined(POLARSSL_PEM_C)
2078 pem_free( &pem );
2079#endif
2080 rsa_free( rsa );
2081 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2082 }
2083
2084 if( rsa->ver != 0 )
2085 {
2086#if defined(POLARSSL_PEM_C)
2087 pem_free( &pem );
2088#endif
2089 rsa_free( rsa );
2090 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2091 }
2092 }
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2095 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2096 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2097 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2098 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2099 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2100 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2101 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2102 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002103#if defined(POLARSSL_PEM_C)
2104 pem_free( &pem );
2105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002107 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 }
2109
2110 rsa->len = mpi_size( &rsa->N );
2111
2112 if( p != end )
2113 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002114#if defined(POLARSSL_PEM_C)
2115 pem_free( &pem );
2116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002118 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002119 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 }
2121
2122 if( ( ret = rsa_check_privkey( rsa ) ) != 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 );
2128 return( ret );
2129 }
2130
Paul Bakker96743fc2011-02-12 14:30:57 +00002131#if defined(POLARSSL_PEM_C)
2132 pem_free( &pem );
2133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
2135 return( 0 );
2136}
2137
2138/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002139 * Parse a public RSA key
2140 */
Paul Bakker23986e52011-04-24 08:57:21 +00002141int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002142{
Paul Bakker23986e52011-04-24 08:57:21 +00002143 int ret;
2144 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002145 unsigned char *p, *end;
2146 x509_buf alg_oid;
2147#if defined(POLARSSL_PEM_C)
2148 pem_context pem;
2149
2150 pem_init( &pem );
2151 ret = pem_read_buffer( &pem,
2152 "-----BEGIN PUBLIC KEY-----",
2153 "-----END PUBLIC KEY-----",
2154 key, NULL, 0, &len );
2155
2156 if( ret == 0 )
2157 {
2158 /*
2159 * Was PEM encoded
2160 */
2161 keylen = pem.buflen;
2162 }
2163 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2164 {
2165 pem_free( &pem );
2166 return( ret );
2167 }
2168
2169 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2170#else
2171 p = (unsigned char *) key;
2172#endif
2173 end = p + keylen;
2174
2175 /*
2176 * PublicKeyInfo ::= SEQUENCE {
2177 * algorithm AlgorithmIdentifier,
2178 * PublicKey BIT STRING
2179 * }
2180 *
2181 * AlgorithmIdentifier ::= SEQUENCE {
2182 * algorithm OBJECT IDENTIFIER,
2183 * parameters ANY DEFINED BY algorithm OPTIONAL
2184 * }
2185 *
2186 * RSAPublicKey ::= SEQUENCE {
2187 * modulus INTEGER, -- n
2188 * publicExponent INTEGER -- e
2189 * }
2190 */
2191
2192 if( ( ret = asn1_get_tag( &p, end, &len,
2193 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2194 {
2195#if defined(POLARSSL_PEM_C)
2196 pem_free( &pem );
2197#endif
2198 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002199 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002200 }
2201
2202 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2203 {
2204#if defined(POLARSSL_PEM_C)
2205 pem_free( &pem );
2206#endif
2207 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002208 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002209 }
2210
2211 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2212 {
2213#if defined(POLARSSL_PEM_C)
2214 pem_free( &pem );
2215#endif
2216 rsa_free( rsa );
2217 return( ret );
2218 }
2219
2220 rsa->len = mpi_size( &rsa->N );
2221
2222#if defined(POLARSSL_PEM_C)
2223 pem_free( &pem );
2224#endif
2225
2226 return( 0 );
2227}
2228
Paul Bakkereaa89f82011-04-04 21:36:15 +00002229#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002230/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002231 * Parse DHM parameters
2232 */
Paul Bakker23986e52011-04-24 08:57:21 +00002233int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002234{
Paul Bakker23986e52011-04-24 08:57:21 +00002235 int ret;
2236 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002237 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002238#if defined(POLARSSL_PEM_C)
2239 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002240
Paul Bakker96743fc2011-02-12 14:30:57 +00002241 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002242
Paul Bakker96743fc2011-02-12 14:30:57 +00002243 ret = pem_read_buffer( &pem,
2244 "-----BEGIN DH PARAMETERS-----",
2245 "-----END DH PARAMETERS-----",
2246 dhmin, NULL, 0, &dhminlen );
2247
2248 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002249 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002250 /*
2251 * Was PEM encoded
2252 */
2253 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002254 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002255 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002256 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002257 pem_free( &pem );
2258 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002259 }
2260
Paul Bakker96743fc2011-02-12 14:30:57 +00002261 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2262#else
2263 p = (unsigned char *) dhmin;
2264#endif
2265 end = p + dhminlen;
2266
Paul Bakker1b57b062011-01-06 15:48:19 +00002267 memset( dhm, 0, sizeof( dhm_context ) );
2268
Paul Bakker1b57b062011-01-06 15:48:19 +00002269 /*
2270 * DHParams ::= SEQUENCE {
2271 * prime INTEGER, -- P
2272 * generator INTEGER, -- g
2273 * }
2274 */
2275 if( ( ret = asn1_get_tag( &p, end, &len,
2276 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2277 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002278#if defined(POLARSSL_PEM_C)
2279 pem_free( &pem );
2280#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002281 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002282 }
2283
2284 end = p + len;
2285
2286 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2287 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2288 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002289#if defined(POLARSSL_PEM_C)
2290 pem_free( &pem );
2291#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002292 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002293 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002294 }
2295
2296 if( p != end )
2297 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002298#if defined(POLARSSL_PEM_C)
2299 pem_free( &pem );
2300#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002301 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002303 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2304 }
2305
Paul Bakker96743fc2011-02-12 14:30:57 +00002306#if defined(POLARSSL_PEM_C)
2307 pem_free( &pem );
2308#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002309
2310 return( 0 );
2311}
2312
Paul Bakker335db3f2011-04-25 15:28:35 +00002313#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002314/*
2315 * Load and parse a private RSA key
2316 */
2317int x509parse_dhmfile( dhm_context *dhm, const char *path )
2318{
2319 int ret;
2320 size_t n;
2321 unsigned char *buf;
2322
2323 if ( load_file( path, &buf, &n ) )
2324 return( 1 );
2325
Paul Bakker27fdf462011-06-09 13:55:13 +00002326 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002327
2328 memset( buf, 0, n + 1 );
2329 free( buf );
2330
2331 return( ret );
2332}
Paul Bakker335db3f2011-04-25 15:28:35 +00002333#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002334#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002335
Paul Bakker5121ce52009-01-03 21:22:43 +00002336#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002337#include <stdarg.h>
2338
2339#if !defined vsnprintf
2340#define vsnprintf _vsnprintf
2341#endif // vsnprintf
2342
2343/*
2344 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2345 * Result value is not size of buffer needed, but -1 if no fit is possible.
2346 *
2347 * This fuction tries to 'fix' this by at least suggesting enlarging the
2348 * size by 20.
2349 */
2350int compat_snprintf(char *str, size_t size, const char *format, ...)
2351{
2352 va_list ap;
2353 int res = -1;
2354
2355 va_start( ap, format );
2356
2357 res = vsnprintf( str, size, format, ap );
2358
2359 va_end( ap );
2360
2361 // No quick fix possible
2362 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002363 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002364
2365 return res;
2366}
2367
2368#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002369#endif
2370
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2372
2373#define SAFE_SNPRINTF() \
2374{ \
2375 if( ret == -1 ) \
2376 return( -1 ); \
2377 \
Paul Bakker23986e52011-04-24 08:57:21 +00002378 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002379 p[n - 1] = '\0'; \
2380 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2381 } \
2382 \
Paul Bakker23986e52011-04-24 08:57:21 +00002383 n -= (unsigned int) ret; \
2384 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385}
2386
Paul Bakker5121ce52009-01-03 21:22:43 +00002387/*
2388 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002391int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002392{
Paul Bakker23986e52011-04-24 08:57:21 +00002393 int ret;
2394 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002396 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 char s[128], *p;
2398
2399 memset( s, 0, sizeof( s ) );
2400
2401 name = dn;
2402 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002403 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
2405 while( name != NULL )
2406 {
Paul Bakker74111d32011-01-15 16:57:55 +00002407 if( name != dn )
2408 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002409 ret = snprintf( p, n, ", " );
2410 SAFE_SNPRINTF();
2411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2414 {
2415 switch( name->oid.p[2] )
2416 {
2417 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002421 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
2423 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002424 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002427 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002430 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
2432 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002433 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
2435 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002436 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 name->oid.p[2] );
2438 break;
2439 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002440 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 }
2442 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2443 {
2444 switch( name->oid.p[8] )
2445 {
2446 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002447 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
2449 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002450 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 name->oid.p[8] );
2452 break;
2453 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002454 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
2456 else
Paul Bakker74111d32011-01-15 16:57:55 +00002457 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002458 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002459 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
2462 for( i = 0; i < name->val.len; i++ )
2463 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002464 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 break;
2466
2467 c = name->val.p[i];
2468 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2469 s[i] = '?';
2470 else s[i] = c;
2471 }
2472 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002473 ret = snprintf( p, n, "%s", s );
2474 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 name = name->next;
2476 }
2477
Paul Bakker23986e52011-04-24 08:57:21 +00002478 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479}
2480
2481/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002482 * Store the serial in printable form into buf; no more
2483 * than size characters will be written
2484 */
2485int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2486{
Paul Bakker23986e52011-04-24 08:57:21 +00002487 int ret;
2488 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002489 char *p;
2490
2491 p = buf;
2492 n = size;
2493
2494 nr = ( serial->len <= 32 )
2495 ? serial->len : 32;
2496
2497 for( i = 0; i < nr; i++ )
2498 {
2499 ret = snprintf( p, n, "%02X%s",
2500 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2501 SAFE_SNPRINTF();
2502 }
2503
Paul Bakker23986e52011-04-24 08:57:21 +00002504 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002505}
2506
2507/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002508 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002510int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2511 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002512{
Paul Bakker23986e52011-04-24 08:57:21 +00002513 int ret;
2514 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
2517 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002518 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002522 SAFE_SNPRINTF();
2523 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002524 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerdd476992011-01-16 21:34:59 +00002527 ret = x509parse_serial_gets( p, n, &crt->serial);
2528 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Paul Bakkerd98030e2009-05-02 15:13:40 +00002530 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2531 SAFE_SNPRINTF();
2532 ret = x509parse_dn_gets( p, n, &crt->issuer );
2533 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2536 SAFE_SNPRINTF();
2537 ret = x509parse_dn_gets( p, n, &crt->subject );
2538 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Paul Bakkerd98030e2009-05-02 15:13:40 +00002540 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2542 crt->valid_from.year, crt->valid_from.mon,
2543 crt->valid_from.day, crt->valid_from.hour,
2544 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002545 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Paul Bakkerd98030e2009-05-02 15:13:40 +00002547 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2549 crt->valid_to.year, crt->valid_to.mon,
2550 crt->valid_to.day, crt->valid_to.hour,
2551 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002552 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Paul Bakkerd98030e2009-05-02 15:13:40 +00002554 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2555 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002556
Paul Bakker27d66162010-03-17 06:56:01 +00002557 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002559 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2560 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2561 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2562 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2563 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2564 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2565 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2566 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2567 default: ret = snprintf( p, n, "???" ); break;
2568 }
2569 SAFE_SNPRINTF();
2570
2571 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002572 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 SAFE_SNPRINTF();
2574
Paul Bakker23986e52011-04-24 08:57:21 +00002575 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002576}
2577
Paul Bakker74111d32011-01-15 16:57:55 +00002578/* Compare a given OID string with an OID x509_buf * */
2579#define OID_CMP(oid_str, oid_buf) \
2580 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2581 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2582
2583/*
2584 * Return an informational string describing the given OID
2585 */
2586const char *x509_oid_get_description( x509_buf *oid )
2587{
2588 if ( oid == NULL )
2589 return ( NULL );
2590
2591 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2592 return( STRING_SERVER_AUTH );
2593
2594 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2595 return( STRING_CLIENT_AUTH );
2596
2597 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2598 return( STRING_CODE_SIGNING );
2599
2600 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2601 return( STRING_EMAIL_PROTECTION );
2602
2603 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2604 return( STRING_TIME_STAMPING );
2605
2606 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2607 return( STRING_OCSP_SIGNING );
2608
2609 return( NULL );
2610}
2611
2612/* Return the x.y.z.... style numeric string for the given OID */
2613int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2614{
Paul Bakker23986e52011-04-24 08:57:21 +00002615 int ret;
2616 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002617 unsigned int value;
2618 char *p;
2619
2620 p = buf;
2621 n = size;
2622
2623 /* First byte contains first two dots */
2624 if( oid->len > 0 )
2625 {
2626 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2627 SAFE_SNPRINTF();
2628 }
2629
2630 /* TODO: value can overflow in value. */
2631 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002632 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002633 {
2634 value <<= 7;
2635 value += oid->p[i] & 0x7F;
2636
2637 if( !( oid->p[i] & 0x80 ) )
2638 {
2639 /* Last byte */
2640 ret = snprintf( p, n, ".%d", value );
2641 SAFE_SNPRINTF();
2642 value = 0;
2643 }
2644 }
2645
Paul Bakker23986e52011-04-24 08:57:21 +00002646 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002647}
2648
Paul Bakkerd98030e2009-05-02 15:13:40 +00002649/*
2650 * Return an informational string about the CRL.
2651 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002652int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2653 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002654{
Paul Bakker23986e52011-04-24 08:57:21 +00002655 int ret;
2656 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002657 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002658 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659
2660 p = buf;
2661 n = size;
2662
2663 ret = snprintf( p, n, "%sCRL version : %d",
2664 prefix, crl->version );
2665 SAFE_SNPRINTF();
2666
2667 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2668 SAFE_SNPRINTF();
2669 ret = x509parse_dn_gets( p, n, &crl->issuer );
2670 SAFE_SNPRINTF();
2671
2672 ret = snprintf( p, n, "\n%sthis update : " \
2673 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2674 crl->this_update.year, crl->this_update.mon,
2675 crl->this_update.day, crl->this_update.hour,
2676 crl->this_update.min, crl->this_update.sec );
2677 SAFE_SNPRINTF();
2678
2679 ret = snprintf( p, n, "\n%snext update : " \
2680 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2681 crl->next_update.year, crl->next_update.mon,
2682 crl->next_update.day, crl->next_update.hour,
2683 crl->next_update.min, crl->next_update.sec );
2684 SAFE_SNPRINTF();
2685
2686 entry = &crl->entry;
2687
2688 ret = snprintf( p, n, "\n%sRevoked certificates:",
2689 prefix );
2690 SAFE_SNPRINTF();
2691
Paul Bakker9be19372009-07-27 20:21:53 +00002692 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002693 {
2694 ret = snprintf( p, n, "\n%sserial number: ",
2695 prefix );
2696 SAFE_SNPRINTF();
2697
2698 nr = ( entry->serial.len <= 32 )
2699 ? entry->serial.len : 32;
2700
Paul Bakker74111d32011-01-15 16:57:55 +00002701 for( i = 0; i < nr; i++ )
2702 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002703 ret = snprintf( p, n, "%02X%s",
2704 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2705 SAFE_SNPRINTF();
2706 }
2707
2708 ret = snprintf( p, n, " revocation date: " \
2709 "%04d-%02d-%02d %02d:%02d:%02d",
2710 entry->revocation_date.year, entry->revocation_date.mon,
2711 entry->revocation_date.day, entry->revocation_date.hour,
2712 entry->revocation_date.min, entry->revocation_date.sec );
2713 SAFE_SNPRINTF();
2714
2715 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 }
2717
Paul Bakkerd98030e2009-05-02 15:13:40 +00002718 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2719 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakker27d66162010-03-17 06:56:01 +00002721 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002722 {
2723 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2724 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2725 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2726 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2727 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2728 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2729 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2730 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2731 default: ret = snprintf( p, n, "???" ); break;
2732 }
2733 SAFE_SNPRINTF();
2734
Paul Bakker1e27bb22009-07-19 20:25:25 +00002735 ret = snprintf( p, n, "\n" );
2736 SAFE_SNPRINTF();
2737
Paul Bakker23986e52011-04-24 08:57:21 +00002738 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739}
2740
2741/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002742 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002744int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002745{
2746 struct tm *lt;
2747 time_t tt;
2748
2749 tt = time( NULL );
2750 lt = localtime( &tt );
2751
Paul Bakker40ea7de2009-05-03 10:18:48 +00002752 if( lt->tm_year > to->year - 1900 )
2753 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakker40ea7de2009-05-03 10:18:48 +00002755 if( lt->tm_year == to->year - 1900 &&
2756 lt->tm_mon > to->mon - 1 )
2757 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Paul Bakker40ea7de2009-05-03 10:18:48 +00002759 if( lt->tm_year == to->year - 1900 &&
2760 lt->tm_mon == to->mon - 1 &&
2761 lt->tm_mday > to->day )
2762 return( 1 );
2763
Paul Bakkerb6194992011-01-16 21:40:22 +00002764 if( lt->tm_year == to->year - 1900 &&
2765 lt->tm_mon == to->mon - 1 &&
2766 lt->tm_mday == to->day &&
2767 lt->tm_hour > to->hour - 1)
2768 return( 1 );
2769
2770 if( lt->tm_year == to->year - 1900 &&
2771 lt->tm_mon == to->mon - 1 &&
2772 lt->tm_mday == to->day &&
2773 lt->tm_hour == to->hour - 1 &&
2774 lt->tm_min > to->min - 1 )
2775 return( 1 );
2776
2777 if( lt->tm_year == to->year - 1900 &&
2778 lt->tm_mon == to->mon - 1 &&
2779 lt->tm_mday == to->day &&
2780 lt->tm_hour == to->hour - 1 &&
2781 lt->tm_min == to->min - 1 &&
2782 lt->tm_sec > to->sec - 1 )
2783 return( 1 );
2784
Paul Bakker40ea7de2009-05-03 10:18:48 +00002785 return( 0 );
2786}
2787
2788/*
2789 * Return 1 if the certificate is revoked, or 0 otherwise.
2790 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002791int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002792{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002793 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002794
2795 while( cur != NULL && cur->serial.len != 0 )
2796 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002797 if( crt->serial.len == cur->serial.len &&
2798 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002799 {
2800 if( x509parse_time_expired( &cur->revocation_date ) )
2801 return( 1 );
2802 }
2803
2804 cur = cur->next;
2805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 return( 0 );
2808}
2809
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002810/*
2811 * Wrapper for x509 hashes.
2812 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002813 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002814 */
Paul Bakker23986e52011-04-24 08:57:21 +00002815static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 unsigned char *out )
2817{
2818 switch( alg )
2819 {
Paul Bakker40e46942009-01-03 21:51:57 +00002820#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002821 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002822#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002823#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002824 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002826#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002827 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002828#endif
2829#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002830 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002831#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002832#if defined(POLARSSL_SHA2_C)
2833 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2834 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2835#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002836#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002837 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2838 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002841 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 break;
2843 }
2844}
2845
2846/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002847 * Check that the given certificate is valid accoring to the CRL.
2848 */
2849static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2850 x509_crl *crl_list)
2851{
2852 int flags = 0;
2853 int hash_id;
2854 unsigned char hash[64];
2855
2856 /*
2857 * TODO: What happens if no CRL is present?
2858 * Suggestion: Revocation state should be unknown if no CRL is present.
2859 * For backwards compatibility this is not yet implemented.
2860 */
2861
2862 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2863 {
2864 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2865 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2866 crl_list->issuer_raw.len ) != 0 )
2867 {
2868 crl_list = crl_list->next;
2869 continue;
2870 }
2871
2872 /*
2873 * Check if CRL is correctly signed by the trusted CA
2874 */
2875 hash_id = crl_list->sig_alg;
2876
2877 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2878
2879 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2880 0, hash, crl_list->sig.p ) == 0 )
2881 {
2882 /*
2883 * CRL is not trusted
2884 */
2885 flags |= BADCRL_NOT_TRUSTED;
2886 break;
2887 }
2888
2889 /*
2890 * Check for validity of CRL (Do not drop out)
2891 */
2892 if( x509parse_time_expired( &crl_list->next_update ) )
2893 flags |= BADCRL_EXPIRED;
2894
2895 /*
2896 * Check if certificate is revoked
2897 */
2898 if( x509parse_revoked(crt, crl_list) )
2899 {
2900 flags |= BADCERT_REVOKED;
2901 break;
2902 }
2903
2904 crl_list = crl_list->next;
2905 }
2906 return flags;
2907}
2908
2909/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 * Verify the certificate validity
2911 */
2912int x509parse_verify( x509_cert *crt,
2913 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002914 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002915 const char *cn, int *flags,
2916 int (*f_vrfy)(void *, x509_cert *, int, int),
2917 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002918{
Paul Bakker23986e52011-04-24 08:57:21 +00002919 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 int hash_id;
2921 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002922 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002924 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker40ea7de2009-05-03 10:18:48 +00002926 *flags = 0;
2927
2928 if( x509parse_time_expired( &crt->valid_to ) )
2929 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 if( cn != NULL )
2932 {
2933 name = &crt->subject;
2934 cn_len = strlen( cn );
2935
2936 while( name != NULL )
2937 {
2938 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2939 memcmp( name->val.p, cn, cn_len ) == 0 &&
2940 name->val.len == cn_len )
2941 break;
2942
2943 name = name->next;
2944 }
2945
2946 if( name == NULL )
2947 *flags |= BADCERT_CN_MISMATCH;
2948 }
2949
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 /*
2951 * Iterate upwards in the given cert chain,
2952 * ignoring any upper cert with CA != TRUE.
2953 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002954 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
2956 pathlen = 1;
2957
Paul Bakker76fd75a2011-01-16 21:12:10 +00002958 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002960 if( parent->ca_istrue == 0 ||
2961 crt->issuer_raw.len != parent->subject_raw.len ||
2962 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 crt->issuer_raw.len ) != 0 )
2964 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002965 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 continue;
2967 }
2968
Paul Bakker27d66162010-03-17 06:56:01 +00002969 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
2971 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2972
Paul Bakker76fd75a2011-01-16 21:12:10 +00002973 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2974 crt->sig.p ) != 0 )
2975 *flags |= BADCERT_NOT_TRUSTED;
2976
2977 /* Check trusted CA's CRL for the given crt */
2978 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002979
2980 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002981 if( NULL != f_vrfy )
2982 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002983 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002984 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002985 else
2986 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002987 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002988 else if( *flags != 0 )
2989 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
2991 pathlen++;
2992
Paul Bakker76fd75a2011-01-16 21:12:10 +00002993 crt = parent;
2994 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 }
2996
2997 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002998 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003000 *flags |= BADCERT_NOT_TRUSTED;
3001
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003002 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 {
3004 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3005 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3006 crt->issuer_raw.len ) != 0 )
3007 {
3008 trust_ca = trust_ca->next;
3009 continue;
3010 }
3011
3012 if( trust_ca->max_pathlen > 0 &&
3013 trust_ca->max_pathlen < pathlen )
3014 break;
3015
Paul Bakker27d66162010-03-17 06:56:01 +00003016 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
3018 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3019
3020 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3021 0, hash, crt->sig.p ) == 0 )
3022 {
3023 /*
3024 * cert. is signed by a trusted CA
3025 */
3026 *flags &= ~BADCERT_NOT_TRUSTED;
3027 break;
3028 }
3029
3030 trust_ca = trust_ca->next;
3031 }
3032
Paul Bakker76fd75a2011-01-16 21:12:10 +00003033 /* Check trusted CA's CRL for the given crt */
3034 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003035
3036 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003037 if( NULL != f_vrfy )
3038 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003039 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003040 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003041 else
3042 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003043 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003044 else if( *flags != 0 )
3045 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003046
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 return( 0 );
3048}
3049
3050/*
3051 * Unallocate all certificate data
3052 */
3053void x509_free( x509_cert *crt )
3054{
3055 x509_cert *cert_cur = crt;
3056 x509_cert *cert_prv;
3057 x509_name *name_cur;
3058 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003059 x509_sequence *seq_cur;
3060 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
3062 if( crt == NULL )
3063 return;
3064
3065 do
3066 {
3067 rsa_free( &cert_cur->rsa );
3068
3069 name_cur = cert_cur->issuer.next;
3070 while( name_cur != NULL )
3071 {
3072 name_prv = name_cur;
3073 name_cur = name_cur->next;
3074 memset( name_prv, 0, sizeof( x509_name ) );
3075 free( name_prv );
3076 }
3077
3078 name_cur = cert_cur->subject.next;
3079 while( name_cur != NULL )
3080 {
3081 name_prv = name_cur;
3082 name_cur = name_cur->next;
3083 memset( name_prv, 0, sizeof( x509_name ) );
3084 free( name_prv );
3085 }
3086
Paul Bakker74111d32011-01-15 16:57:55 +00003087 seq_cur = cert_cur->ext_key_usage.next;
3088 while( seq_cur != NULL )
3089 {
3090 seq_prv = seq_cur;
3091 seq_cur = seq_cur->next;
3092 memset( seq_prv, 0, sizeof( x509_sequence ) );
3093 free( seq_prv );
3094 }
3095
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 if( cert_cur->raw.p != NULL )
3097 {
3098 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3099 free( cert_cur->raw.p );
3100 }
3101
3102 cert_cur = cert_cur->next;
3103 }
3104 while( cert_cur != NULL );
3105
3106 cert_cur = crt;
3107 do
3108 {
3109 cert_prv = cert_cur;
3110 cert_cur = cert_cur->next;
3111
3112 memset( cert_prv, 0, sizeof( x509_cert ) );
3113 if( cert_prv != crt )
3114 free( cert_prv );
3115 }
3116 while( cert_cur != NULL );
3117}
3118
Paul Bakkerd98030e2009-05-02 15:13:40 +00003119/*
3120 * Unallocate all CRL data
3121 */
3122void x509_crl_free( x509_crl *crl )
3123{
3124 x509_crl *crl_cur = crl;
3125 x509_crl *crl_prv;
3126 x509_name *name_cur;
3127 x509_name *name_prv;
3128 x509_crl_entry *entry_cur;
3129 x509_crl_entry *entry_prv;
3130
3131 if( crl == NULL )
3132 return;
3133
3134 do
3135 {
3136 name_cur = crl_cur->issuer.next;
3137 while( name_cur != NULL )
3138 {
3139 name_prv = name_cur;
3140 name_cur = name_cur->next;
3141 memset( name_prv, 0, sizeof( x509_name ) );
3142 free( name_prv );
3143 }
3144
3145 entry_cur = crl_cur->entry.next;
3146 while( entry_cur != NULL )
3147 {
3148 entry_prv = entry_cur;
3149 entry_cur = entry_cur->next;
3150 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3151 free( entry_prv );
3152 }
3153
3154 if( crl_cur->raw.p != NULL )
3155 {
3156 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3157 free( crl_cur->raw.p );
3158 }
3159
3160 crl_cur = crl_cur->next;
3161 }
3162 while( crl_cur != NULL );
3163
3164 crl_cur = crl;
3165 do
3166 {
3167 crl_prv = crl_cur;
3168 crl_cur = crl_cur->next;
3169
3170 memset( crl_prv, 0, sizeof( x509_crl ) );
3171 if( crl_prv != crl )
3172 free( crl_prv );
3173 }
3174 while( crl_cur != NULL );
3175}
3176
Paul Bakker40e46942009-01-03 21:51:57 +00003177#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003178
Paul Bakker40e46942009-01-03 21:51:57 +00003179#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
3181/*
3182 * Checkup routine
3183 */
3184int x509_self_test( int verbose )
3185{
Paul Bakker5690efc2011-05-26 13:16:06 +00003186#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003187 int ret;
3188 int flags;
3189 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 x509_cert cacert;
3191 x509_cert clicert;
3192 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003193#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003194 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003195#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
3197 if( verbose != 0 )
3198 printf( " X.509 certificate load: " );
3199
3200 memset( &clicert, 0, sizeof( x509_cert ) );
3201
3202 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3203 strlen( test_cli_crt ) );
3204 if( ret != 0 )
3205 {
3206 if( verbose != 0 )
3207 printf( "failed\n" );
3208
3209 return( ret );
3210 }
3211
3212 memset( &cacert, 0, sizeof( x509_cert ) );
3213
3214 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3215 strlen( test_ca_crt ) );
3216 if( ret != 0 )
3217 {
3218 if( verbose != 0 )
3219 printf( "failed\n" );
3220
3221 return( ret );
3222 }
3223
3224 if( verbose != 0 )
3225 printf( "passed\n X.509 private key load: " );
3226
3227 i = strlen( test_ca_key );
3228 j = strlen( test_ca_pwd );
3229
Paul Bakker66b78b22011-03-25 14:22:50 +00003230 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3231
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 if( ( ret = x509parse_key( &rsa,
3233 (unsigned char *) test_ca_key, i,
3234 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3235 {
3236 if( verbose != 0 )
3237 printf( "failed\n" );
3238
3239 return( ret );
3240 }
3241
3242 if( verbose != 0 )
3243 printf( "passed\n X.509 signature verify: ");
3244
Paul Bakker23986e52011-04-24 08:57:21 +00003245 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 if( ret != 0 )
3247 {
Paul Bakker23986e52011-04-24 08:57:21 +00003248 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 if( verbose != 0 )
3250 printf( "failed\n" );
3251
3252 return( ret );
3253 }
3254
Paul Bakker5690efc2011-05-26 13:16:06 +00003255#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003257 printf( "passed\n X.509 DHM parameter load: " );
3258
3259 i = strlen( test_dhm_params );
3260 j = strlen( test_ca_pwd );
3261
3262 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3263 {
3264 if( verbose != 0 )
3265 printf( "failed\n" );
3266
3267 return( ret );
3268 }
3269
3270 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 x509_free( &cacert );
3275 x509_free( &clicert );
3276 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003277#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003278 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003279#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
3281 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003282#else
3283 ((void) verbose);
3284 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3285#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003286}
3287
3288#endif
3289
3290#endif