blob: 31ec3461a0286b06e32127ebbc0cb802ad70d675 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000042#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000043#include "polarssl/des.h"
44#include "polarssl/md2.h"
45#include "polarssl/md4.h"
46#include "polarssl/md5.h"
47#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000048#include "polarssl/sha2.h"
49#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000050#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000051
52#include <string.h>
53#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <time.h>
55
Paul Bakker335db3f2011-04-25 15:28:35 +000056#if defined(POLARSSL_FS_IO)
57#include <stdio.h>
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/*
61 * ASN.1 DER decoding routines
62 */
63static int asn1_get_len( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000064 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +000065 size_t *len )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
67 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +000068 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 if( ( **p & 0x80 ) == 0 )
71 *len = *(*p)++;
72 else
73 {
74 switch( **p & 0x7F )
75 {
76 case 1:
77 if( ( end - *p ) < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000078 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 *len = (*p)[1];
81 (*p) += 2;
82 break;
83
84 case 2:
85 if( ( end - *p ) < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000086 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 *len = ( (*p)[1] << 8 ) | (*p)[2];
89 (*p) += 3;
90 break;
91
Paul Bakkerc4909d92011-10-12 09:52:22 +000092 case 3:
93 if( ( end - *p ) < 4 )
94 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
95
96 *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3];
97 (*p) += 4;
98 break;
99
100 case 4:
101 if( ( end - *p ) < 5 )
102 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
103
104 *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | (*p)[4];
105 (*p) += 5;
106 break;
107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000109 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 }
111 }
112
Paul Bakker23986e52011-04-24 08:57:21 +0000113 if( *len > (size_t) ( end - *p ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000114 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 return( 0 );
117}
118
119static int asn1_get_tag( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 size_t *len, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000124 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126 if( **p != tag )
Paul Bakker40e46942009-01-03 21:51:57 +0000127 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129 (*p)++;
130
131 return( asn1_get_len( p, end, len ) );
132}
133
134static int asn1_get_bool( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000135 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 int *val )
137{
Paul Bakker23986e52011-04-24 08:57:21 +0000138 int ret;
139 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
142 return( ret );
143
144 if( len != 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000145 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 *val = ( **p != 0 ) ? 1 : 0;
148 (*p)++;
149
150 return( 0 );
151}
152
153static int asn1_get_int( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 int *val )
156{
Paul Bakker23986e52011-04-24 08:57:21 +0000157 int ret;
158 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
161 return( ret );
162
Paul Bakker27fdf462011-06-09 13:55:13 +0000163 if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000164 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 *val = 0;
167
168 while( len-- > 0 )
169 {
170 *val = ( *val << 8 ) | **p;
171 (*p)++;
172 }
173
174 return( 0 );
175}
176
177static int asn1_get_mpi( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 mpi *X )
180{
Paul Bakker23986e52011-04-24 08:57:21 +0000181 int ret;
182 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
185 return( ret );
186
187 ret = mpi_read_binary( X, *p, len );
188
189 *p += len;
190
191 return( ret );
192}
193
Paul Bakker74111d32011-01-15 16:57:55 +0000194static int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
195 x509_bitstring *bs)
196{
197 int ret;
198
199 /* Certificate type is a single byte bitstring */
200 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
201 return( ret );
202
203 /* Check length, subtract one for actual bit string length */
204 if ( bs->len < 1 )
205 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
206 bs->len -= 1;
207
208 /* Get number of unused bits, ensure unused bits <= 7 */
209 bs->unused_bits = **p;
210 if( bs->unused_bits > 7 )
211 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
212 (*p)++;
213
214 /* Get actual bitstring */
215 bs->p = *p;
216 *p += bs->len;
217
218 if( *p != end )
219 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
220
221 return 0;
222}
223
224
225/*
226 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
227 */
228static int asn1_get_sequence_of( unsigned char **p,
229 const unsigned char *end,
230 x509_sequence *cur,
231 int tag)
232{
Paul Bakker23986e52011-04-24 08:57:21 +0000233 int ret;
234 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000235 x509_buf *buf;
236
237 /* Get main sequence tag */
238 if( ( ret = asn1_get_tag( p, end, &len,
239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
240 return( ret );
241
242 if( *p + len != end )
243 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
244
245 while( *p < end )
246 {
247 buf = &(cur->buf);
248 buf->tag = **p;
249
250 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
251 return( ret );
252
253 buf->p = *p;
254 *p += buf->len;
255
256 /* Allocate and assign next pointer */
257 if (*p < end)
258 {
259 cur->next = (x509_sequence *) malloc(
260 sizeof( x509_sequence ) );
261
262 if( cur->next == NULL )
263 return( 1 );
264
265 cur = cur->next;
266 }
267 }
268
269 /* Set final sequence entry's next pointer to NULL */
270 cur->next = NULL;
271
272 if( *p != end )
273 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
274
275 return( 0 );
276}
277
Paul Bakker5121ce52009-01-03 21:22:43 +0000278/*
279 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
280 */
281static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 int *ver )
284{
Paul Bakker23986e52011-04-24 08:57:21 +0000285 int ret;
286 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 if( ( ret = asn1_get_tag( p, end, &len,
289 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
290 {
Paul Bakker40e46942009-01-03 21:51:57 +0000291 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 return( *ver = 0 );
293
294 return( ret );
295 }
296
297 end = *p + len;
298
299 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000300 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000303 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000304 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 return( 0 );
307}
308
309/*
Paul Bakker3329d1f2011-10-12 09:55:01 +0000310 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
311 */
312static int x509_crl_get_version( unsigned char **p,
313 const unsigned char *end,
314 int *ver )
315{
316 int ret;
317
318 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
319 {
320 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
321 return( *ver = 0 );
322
323 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
324 }
325
326 return( 0 );
327}
328
329/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * CertificateSerialNumber ::= INTEGER
331 */
332static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 x509_buf *serial )
335{
336 int ret;
337
338 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000339 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000340 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
343 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000344 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000345 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347 serial->tag = *(*p)++;
348
349 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 serial->p = *p;
353 *p += serial->len;
354
355 return( 0 );
356}
357
358/*
359 * AlgorithmIdentifier ::= SEQUENCE {
360 * algorithm OBJECT IDENTIFIER,
361 * parameters ANY DEFINED BY algorithm OPTIONAL }
362 */
363static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 x509_buf *alg )
366{
Paul Bakker23986e52011-04-24 08:57:21 +0000367 int ret;
368 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 if( ( ret = asn1_get_tag( p, end, &len,
371 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000372 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 end = *p + len;
375 alg->tag = **p;
376
377 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000378 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 alg->p = *p;
381 *p += alg->len;
382
383 if( *p == end )
384 return( 0 );
385
386 /*
387 * assume the algorithm parameters must be NULL
388 */
389 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000390 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000393 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000394 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 return( 0 );
397}
398
399/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 * AttributeTypeAndValue ::= SEQUENCE {
401 * type AttributeType,
402 * value AttributeValue }
403 *
404 * AttributeType ::= OBJECT IDENTIFIER
405 *
406 * AttributeValue ::= ANY DEFINED BY AttributeType
407 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000408static int x509_get_attr_type_value( unsigned char **p,
409 const unsigned char *end,
410 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000411{
Paul Bakker23986e52011-04-24 08:57:21 +0000412 int ret;
413 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 x509_buf *oid;
415 x509_buf *val;
416
417 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 oid = &cur->oid;
422 oid->tag = **p;
423
424 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000425 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 oid->p = *p;
428 *p += oid->len;
429
430 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000431 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
435 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
436 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000438 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 val = &cur->val;
441 val->tag = *(*p)++;
442
443 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000444 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446 val->p = *p;
447 *p += val->len;
448
449 cur->next = NULL;
450
Paul Bakker400ff6f2011-02-20 10:40:16 +0000451 return( 0 );
452}
453
454/*
455 * RelativeDistinguishedName ::=
456 * SET OF AttributeTypeAndValue
457 *
458 * AttributeTypeAndValue ::= SEQUENCE {
459 * type AttributeType,
460 * value AttributeValue }
461 *
462 * AttributeType ::= OBJECT IDENTIFIER
463 *
464 * AttributeValue ::= ANY DEFINED BY AttributeType
465 */
466static int x509_get_name( unsigned char **p,
467 const unsigned char *end,
468 x509_name *cur )
469{
Paul Bakker23986e52011-04-24 08:57:21 +0000470 int ret;
471 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000472 const unsigned char *end2;
473 x509_name *use;
474
475 if( ( ret = asn1_get_tag( p, end, &len,
476 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000477 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000478
479 end2 = end;
480 end = *p + len;
481 use = cur;
482
483 do
484 {
485 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
486 return( ret );
487
488 if( *p != end )
489 {
490 use->next = (x509_name *) malloc(
491 sizeof( x509_name ) );
492
493 if( use->next == NULL )
494 return( 1 );
495
496 memset( use->next, 0, sizeof( x509_name ) );
497
498 use = use->next;
499 }
500 }
501 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 /*
504 * recurse until end of SEQUENCE is reached
505 */
506 if( *p == end2 )
507 return( 0 );
508
509 cur->next = (x509_name *) malloc(
510 sizeof( x509_name ) );
511
512 if( cur->next == NULL )
513 return( 1 );
514
515 return( x509_get_name( p, end2, cur->next ) );
516}
517
518/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * Time ::= CHOICE {
520 * utcTime UTCTime,
521 * generalTime GeneralizedTime }
522 */
Paul Bakker91200182010-02-18 21:26:15 +0000523static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000524 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000525 x509_time *time )
526{
Paul Bakker23986e52011-04-24 08:57:21 +0000527 int ret;
528 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000530 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000531
Paul Bakker91200182010-02-18 21:26:15 +0000532 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000533 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
534 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000535
Paul Bakker91200182010-02-18 21:26:15 +0000536 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000537
Paul Bakker91200182010-02-18 21:26:15 +0000538 if ( tag == ASN1_UTC_TIME )
539 {
540 (*p)++;
541 ret = asn1_get_len( p, end, &len );
542
543 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000544 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000545
Paul Bakker91200182010-02-18 21:26:15 +0000546 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000547 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
548 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549
Paul Bakker91200182010-02-18 21:26:15 +0000550 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
551 &time->year, &time->mon, &time->day,
552 &time->hour, &time->min, &time->sec ) < 5 )
553 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000554
Paul Bakker400ff6f2011-02-20 10:40:16 +0000555 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000556 time->year += 1900;
557
558 *p += len;
559
560 return( 0 );
561 }
562 else if ( tag == ASN1_GENERALIZED_TIME )
563 {
564 (*p)++;
565 ret = asn1_get_len( p, end, &len );
566
567 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000568 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000569
570 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000571 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
572 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000573
574 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
575 &time->year, &time->mon, &time->day,
576 &time->hour, &time->min, &time->sec ) < 5 )
577 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
578
579 *p += len;
580
581 return( 0 );
582 }
583 else
Paul Bakker9d781402011-05-09 16:17:09 +0000584 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585}
586
587
588/*
589 * Validity ::= SEQUENCE {
590 * notBefore Time,
591 * notAfter Time }
592 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000593static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 x509_time *from,
596 x509_time *to )
597{
Paul Bakker23986e52011-04-24 08:57:21 +0000598 int ret;
599 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 if( ( ret = asn1_get_tag( p, end, &len,
602 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000603 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
605 end = *p + len;
606
Paul Bakker91200182010-02-18 21:26:15 +0000607 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Paul Bakker91200182010-02-18 21:26:15 +0000610 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000614 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000615 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617 return( 0 );
618}
619
620/*
621 * SubjectPublicKeyInfo ::= SEQUENCE {
622 * algorithm AlgorithmIdentifier,
623 * subjectPublicKey BIT STRING }
624 */
625static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000626 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 x509_buf *pk_alg_oid,
628 mpi *N, mpi *E )
629{
Paul Bakker23986e52011-04-24 08:57:21 +0000630 int ret, can_handle;
631 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 unsigned char *end2;
633
634 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
635 return( ret );
636
637 /*
638 * only RSA public keys handled at this time
639 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000640 can_handle = 0;
641
642 if( pk_alg_oid->len == 9 &&
643 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
644 can_handle = 1;
645
646 if( pk_alg_oid->len == 9 &&
647 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
648 {
649 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
650 can_handle = 1;
651
652 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
653 can_handle = 1;
654 }
655
656 if( pk_alg_oid->len == 5 &&
657 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
658 can_handle = 1;
659
660 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000661 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000664 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
666 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000667 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000668 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
670 end2 = *p + len;
671
672 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000673 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 /*
676 * RSAPublicKey ::= SEQUENCE {
677 * modulus INTEGER, -- n
678 * publicExponent INTEGER -- e
679 * }
680 */
681 if( ( ret = asn1_get_tag( p, end2, &len,
682 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000683 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000686 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000687 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
690 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000695 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 return( 0 );
698}
699
700static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000701 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 x509_buf *sig )
703{
Paul Bakker23986e52011-04-24 08:57:21 +0000704 int ret;
705 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707 sig->tag = **p;
708
709 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Paul Bakker74111d32011-01-15 16:57:55 +0000712
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000714 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
716 sig->len = len;
717 sig->p = *p;
718
719 *p += len;
720
721 return( 0 );
722}
723
724/*
725 * X.509 v2/v3 unique identifier (not parsed)
726 */
727static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000728 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 x509_buf *uid, int n )
730{
731 int ret;
732
733 if( *p == end )
734 return( 0 );
735
736 uid->tag = **p;
737
738 if( ( ret = asn1_get_tag( p, end, &uid->len,
739 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
740 {
Paul Bakker40e46942009-01-03 21:51:57 +0000741 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 return( 0 );
743
744 return( ret );
745 }
746
747 uid->p = *p;
748 *p += uid->len;
749
750 return( 0 );
751}
752
753/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000754 * X.509 Extensions (No parsing of extensions, pointer should
755 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 */
757static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000758 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000759 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760{
Paul Bakker23986e52011-04-24 08:57:21 +0000761 int ret;
762 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
764 if( *p == end )
765 return( 0 );
766
767 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000770 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773 ext->p = *p;
774 end = *p + ext->len;
775
776 /*
777 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
778 *
779 * Extension ::= SEQUENCE {
780 * extnID OBJECT IDENTIFIER,
781 * critical BOOLEAN DEFAULT FALSE,
782 * extnValue OCTET STRING }
783 */
784 if( ( ret = asn1_get_tag( p, end, &len,
785 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000786 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
788 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000790 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakkerd98030e2009-05-02 15:13:40 +0000792 return( 0 );
793}
794
795/*
796 * X.509 CRL v2 extensions (no extensions parsed yet.)
797 */
798static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000799 const unsigned char *end,
800 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000801{
Paul Bakker23986e52011-04-24 08:57:21 +0000802 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000803 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000804
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000805 /* Get explicit tag */
806 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000807 {
808 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
809 return( 0 );
810
811 return( ret );
812 }
813
814 while( *p < end )
815 {
816 if( ( ret = asn1_get_tag( p, end, &len,
817 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000818 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000819
820 *p += len;
821 }
822
823 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000824 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000825 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
826
827 return( 0 );
828}
829
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000830/*
831 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
832 */
833static int x509_get_crl_entry_ext( unsigned char **p,
834 const unsigned char *end,
835 x509_buf *ext )
836{
837 int ret;
838 size_t len = 0;
839
840 /* OPTIONAL */
841 if (end <= *p)
842 return( 0 );
843
844 ext->tag = **p;
845 ext->p = *p;
846
847 /*
848 * Get CRL-entry extension sequence header
849 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
850 */
851 if( ( ret = asn1_get_tag( p, end, &ext->len,
852 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
853 {
854 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
855 {
856 ext->p = NULL;
857 return( 0 );
858 }
859 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
860 }
861
862 end = *p + ext->len;
863
864 if( end != *p + ext->len )
865 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
866 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
867
868 while( *p < end )
869 {
870 if( ( ret = asn1_get_tag( p, end, &len,
871 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
872 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
873
874 *p += len;
875 }
876
877 if( *p != end )
878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
879 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
880
881 return( 0 );
882}
883
Paul Bakker74111d32011-01-15 16:57:55 +0000884static int x509_get_basic_constraints( unsigned char **p,
885 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000886 int *ca_istrue,
887 int *max_pathlen )
888{
Paul Bakker23986e52011-04-24 08:57:21 +0000889 int ret;
890 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000891
892 /*
893 * BasicConstraints ::= SEQUENCE {
894 * cA BOOLEAN DEFAULT FALSE,
895 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
896 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000897 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000898 *max_pathlen = 0; /* endless */
899
900 if( ( ret = asn1_get_tag( p, end, &len,
901 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000902 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000903
904 if( *p == end )
905 return 0;
906
Paul Bakker3cccddb2011-01-16 21:46:31 +0000907 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000908 {
909 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000910 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000911
912 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000913 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000914
Paul Bakker3cccddb2011-01-16 21:46:31 +0000915 if( *ca_istrue != 0 )
916 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000917 }
918
919 if( *p == end )
920 return 0;
921
922 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000923 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000924
925 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
928
929 (*max_pathlen)++;
930
Paul Bakker74111d32011-01-15 16:57:55 +0000931 return 0;
932}
933
934static int x509_get_ns_cert_type( unsigned char **p,
935 const unsigned char *end,
936 unsigned char *ns_cert_type)
937{
938 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000939 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000940
941 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000942 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000943
944 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000946 POLARSSL_ERR_ASN1_INVALID_LENGTH );
947
948 /* Get actual bitstring */
949 *ns_cert_type = *bs.p;
950 return 0;
951}
952
953static int x509_get_key_usage( unsigned char **p,
954 const unsigned char *end,
955 unsigned char *key_usage)
956{
957 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000958 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000959
960 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000961 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000962
963 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000964 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000965 POLARSSL_ERR_ASN1_INVALID_LENGTH );
966
967 /* Get actual bitstring */
968 *key_usage = *bs.p;
969 return 0;
970}
971
Paul Bakkerd98030e2009-05-02 15:13:40 +0000972/*
Paul Bakker74111d32011-01-15 16:57:55 +0000973 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
974 *
975 * KeyPurposeId ::= OBJECT IDENTIFIER
976 */
977static int x509_get_ext_key_usage( unsigned char **p,
978 const unsigned char *end,
979 x509_sequence *ext_key_usage)
980{
981 int ret;
982
983 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000984 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000985
986 /* Sequence length must be >= 1 */
987 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000988 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000989 POLARSSL_ERR_ASN1_INVALID_LENGTH );
990
991 return 0;
992}
993
994/*
995 * X.509 v3 extensions
996 *
997 * TODO: Perform all of the basic constraints tests required by the RFC
998 * TODO: Set values for undetected extensions to a sane default?
999 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001000 */
1001static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001002 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001003 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001004{
Paul Bakker23986e52011-04-24 08:57:21 +00001005 int ret;
1006 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001007 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001008
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001009 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001010 {
1011 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1012 return( 0 );
1013
1014 return( ret );
1015 }
1016
Paul Bakker5121ce52009-01-03 21:22:43 +00001017 while( *p < end )
1018 {
Paul Bakker74111d32011-01-15 16:57:55 +00001019 /*
1020 * Extension ::= SEQUENCE {
1021 * extnID OBJECT IDENTIFIER,
1022 * critical BOOLEAN DEFAULT FALSE,
1023 * extnValue OCTET STRING }
1024 */
1025 x509_buf extn_oid = {0, 0, NULL};
1026 int is_critical = 0; /* DEFAULT FALSE */
1027
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 if( ( ret = asn1_get_tag( p, end, &len,
1029 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001030 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001032 end_ext_data = *p + len;
1033
Paul Bakker74111d32011-01-15 16:57:55 +00001034 /* Get extension ID */
1035 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Paul Bakker74111d32011-01-15 16:57:55 +00001037 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001038 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Paul Bakker74111d32011-01-15 16:57:55 +00001040 extn_oid.p = *p;
1041 *p += extn_oid.len;
1042
1043 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001044 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001045 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1046
1047 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001048 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001049 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001050 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Paul Bakker74111d32011-01-15 16:57:55 +00001052 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001053 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001055 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001057 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001058
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001059 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001060 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001061 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Paul Bakker74111d32011-01-15 16:57:55 +00001063 /*
1064 * Detect supported extensions
1065 */
1066 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
1067 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 {
Paul Bakker74111d32011-01-15 16:57:55 +00001069 /* Parse basic constraints */
1070 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +00001071 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001072 return ( ret );
1073 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 }
Paul Bakker74111d32011-01-15 16:57:55 +00001075 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
1076 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
1077 {
1078 /* Parse netscape certificate type */
1079 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1080 &crt->ns_cert_type ) ) != 0 )
1081 return ( ret );
1082 crt->ext_types |= EXT_NS_CERT_TYPE;
1083 }
1084 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
1085 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
1086 {
1087 /* Parse key usage */
1088 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1089 &crt->key_usage ) ) != 0 )
1090 return ( ret );
1091 crt->ext_types |= EXT_KEY_USAGE;
1092 }
1093 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1094 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1095 {
1096 /* Parse extended key usage */
1097 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1098 &crt->ext_key_usage ) ) != 0 )
1099 return ( ret );
1100 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1101 }
1102 else
1103 {
1104 /* No parser found, skip extension */
1105 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001106
Paul Bakker5c721f92011-07-27 16:51:09 +00001107#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001108 if( is_critical )
1109 {
1110 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001111 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001112 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1113 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001114#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 }
1117
1118 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001119 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001120 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 return( 0 );
1123}
1124
1125/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 * X.509 CRL Entries
1127 */
1128static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001129 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001130 x509_crl_entry *entry )
1131{
Paul Bakker23986e52011-04-24 08:57:21 +00001132 int ret;
1133 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001134 x509_crl_entry *cur_entry = entry;
1135
1136 if( *p == end )
1137 return( 0 );
1138
Paul Bakker9be19372009-07-27 20:21:53 +00001139 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001140 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1141 {
1142 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1143 return( 0 );
1144
1145 return( ret );
1146 }
1147
Paul Bakker9be19372009-07-27 20:21:53 +00001148 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001149
1150 while( *p < end )
1151 {
Paul Bakker23986e52011-04-24 08:57:21 +00001152 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001153 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154
1155 if( ( ret = asn1_get_tag( p, end, &len2,
1156 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1157 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 return( ret );
1159 }
1160
Paul Bakker9be19372009-07-27 20:21:53 +00001161 cur_entry->raw.tag = **p;
1162 cur_entry->raw.p = *p;
1163 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001164 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001165
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001166 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001167 return( ret );
1168
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001169 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001170 return( ret );
1171
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001172 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001173 return( ret );
1174
Paul Bakker74111d32011-01-15 16:57:55 +00001175 if ( *p < end )
1176 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001177 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1178 cur_entry = cur_entry->next;
1179 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1180 }
1181 }
1182
1183 return( 0 );
1184}
1185
Paul Bakker27d66162010-03-17 06:56:01 +00001186static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1187{
1188 if( sig_oid->len == 9 &&
1189 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1190 {
1191 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1192 {
1193 *sig_alg = sig_oid->p[8];
1194 return( 0 );
1195 }
1196
1197 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1198 {
1199 *sig_alg = sig_oid->p[8];
1200 return( 0 );
1201 }
1202
1203 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1204 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001205 if( sig_oid->len == 5 &&
1206 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1207 {
1208 *sig_alg = SIG_RSA_SHA1;
1209 return( 0 );
1210 }
Paul Bakker27d66162010-03-17 06:56:01 +00001211
1212 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1213}
1214
Paul Bakkerd98030e2009-05-02 15:13:40 +00001215/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 * Parse one or more certificates and add them to the chained list
1217 */
Paul Bakker23986e52011-04-24 08:57:21 +00001218int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219{
Paul Bakker23986e52011-04-24 08:57:21 +00001220 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001221 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 unsigned char *p, *end;
1223 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001224#if defined(POLARSSL_PEM_C)
1225 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001226 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001228
1229 crt = chain;
1230
Paul Bakker320a4b52009-03-28 18:52:39 +00001231 /*
1232 * Check for valid input
1233 */
1234 if( crt == NULL || buf == NULL )
1235 return( 1 );
1236
Paul Bakkere9581d62009-03-28 20:29:25 +00001237 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 crt = crt->next;
1239
1240 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001241 * Add new certificate on the end of the chain if needed.
1242 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001243 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001244 {
1245 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1246
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 if( crt->next == NULL )
1248 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001249 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 return( 1 );
1251 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001252
Paul Bakker7d06ad22009-05-02 15:53:56 +00001253 crt = crt->next;
1254 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001255 }
1256
Paul Bakker96743fc2011-02-12 14:30:57 +00001257#if defined(POLARSSL_PEM_C)
1258 pem_init( &pem );
1259 ret = pem_read_buffer( &pem,
1260 "-----BEGIN CERTIFICATE-----",
1261 "-----END CERTIFICATE-----",
1262 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Paul Bakker96743fc2011-02-12 14:30:57 +00001264 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001266 /*
1267 * Was PEM encoded
1268 */
1269 buflen -= use_len;
1270 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271
1272 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001273 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001275 p = pem.buf;
1276 pem.buf = NULL;
1277 len = pem.buflen;
1278 pem_free( &pem );
1279 }
1280 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1281 {
1282 pem_free( &pem );
1283 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285 else
1286 {
1287 /*
1288 * nope, copy the raw DER data
1289 */
1290 p = (unsigned char *) malloc( len = buflen );
1291
1292 if( p == NULL )
1293 return( 1 );
1294
1295 memcpy( p, buf, buflen );
1296
1297 buflen = 0;
1298 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001299#else
1300 p = (unsigned char *) malloc( len = buflen );
1301
1302 if( p == NULL )
1303 return( 1 );
1304
1305 memcpy( p, buf, buflen );
1306
1307 buflen = 0;
1308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001309
1310 crt->raw.p = p;
1311 crt->raw.len = len;
1312 end = p + len;
1313
1314 /*
1315 * Certificate ::= SEQUENCE {
1316 * tbsCertificate TBSCertificate,
1317 * signatureAlgorithm AlgorithmIdentifier,
1318 * signatureValue BIT STRING }
1319 */
1320 if( ( ret = asn1_get_tag( &p, end, &len,
1321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1322 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001323 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Paul Bakker23986e52011-04-24 08:57:21 +00001327 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001329 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001330 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
1334 /*
1335 * TBSCertificate ::= SEQUENCE {
1336 */
1337 crt->tbs.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 end = p + len;
1347 crt->tbs.len = end - crt->tbs.p;
1348
1349 /*
1350 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1351 *
1352 * CertificateSerialNumber ::= INTEGER
1353 *
1354 * signature AlgorithmIdentifier
1355 */
1356 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1357 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1358 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 return( ret );
1362 }
1363
1364 crt->version++;
1365
1366 if( crt->version > 3 )
1367 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001368 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001369 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
1371
Paul Bakker27d66162010-03-17 06:56:01 +00001372 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001374 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001375 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
1378 /*
1379 * issuer Name
1380 */
1381 crt->issuer_raw.p = p;
1382
1383 if( ( ret = asn1_get_tag( &p, end, &len,
1384 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001387 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
1389
1390 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1391 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001392 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 return( ret );
1394 }
1395
1396 crt->issuer_raw.len = p - crt->issuer_raw.p;
1397
1398 /*
1399 * Validity ::= SEQUENCE {
1400 * notBefore Time,
1401 * notAfter Time }
1402 *
1403 */
1404 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1405 &crt->valid_to ) ) != 0 )
1406 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001407 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 return( ret );
1409 }
1410
1411 /*
1412 * subject Name
1413 */
1414 crt->subject_raw.p = p;
1415
1416 if( ( ret = asn1_get_tag( &p, end, &len,
1417 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001420 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
1423 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1424 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001425 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 return( ret );
1427 }
1428
1429 crt->subject_raw.len = p - crt->subject_raw.p;
1430
1431 /*
1432 * SubjectPublicKeyInfo ::= SEQUENCE
1433 * algorithm AlgorithmIdentifier,
1434 * subjectPublicKey BIT STRING }
1435 */
1436 if( ( ret = asn1_get_tag( &p, end, &len,
1437 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1438 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001439 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001440 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
1443 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1444 &crt->rsa.N, &crt->rsa.E ) ) != 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( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1451 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001452 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 return( ret );
1454 }
1455
1456 crt->rsa.len = mpi_size( &crt->rsa.N );
1457
1458 /*
1459 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1460 * -- If present, version shall be v2 or v3
1461 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1462 * -- If present, version shall be v2 or v3
1463 * extensions [3] EXPLICIT Extensions OPTIONAL
1464 * -- If present, version shall be v3
1465 */
1466 if( crt->version == 2 || crt->version == 3 )
1467 {
1468 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1469 if( ret != 0 )
1470 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001471 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 return( ret );
1473 }
1474 }
1475
1476 if( crt->version == 2 || crt->version == 3 )
1477 {
1478 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1479 if( ret != 0 )
1480 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001481 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 return( ret );
1483 }
1484 }
1485
1486 if( crt->version == 3 )
1487 {
Paul Bakker74111d32011-01-15 16:57:55 +00001488 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 if( ret != 0 )
1490 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001491 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 return( ret );
1493 }
1494 }
1495
1496 if( p != end )
1497 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001498 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001499 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001500 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
1502
1503 end = crt->raw.p + crt->raw.len;
1504
1505 /*
1506 * signatureAlgorithm AlgorithmIdentifier,
1507 * signatureValue BIT STRING
1508 */
1509 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1510 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001511 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 return( ret );
1513 }
1514
Paul Bakker320a4b52009-03-28 18:52:39 +00001515 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001517 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001518 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 }
1520
1521 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1522 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001523 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 return( ret );
1525 }
1526
1527 if( p != end )
1528 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001529 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001530 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001531 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 }
1533
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001535 {
1536 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1537
Paul Bakker7d06ad22009-05-02 15:53:56 +00001538 if( crt->next == NULL )
1539 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001540 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001541 return( 1 );
1542 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001543
Paul Bakker7d06ad22009-05-02 15:53:56 +00001544 crt = crt->next;
1545 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001546
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
1550 return( 0 );
1551}
1552
1553/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001554 * Parse one or more CRLs and add them to the chained list
1555 */
Paul Bakker23986e52011-04-24 08:57:21 +00001556int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001557{
Paul Bakker23986e52011-04-24 08:57:21 +00001558 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001559 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001560 unsigned char *p, *end;
1561 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001562#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001563 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001564 pem_context pem;
1565#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566
1567 crl = chain;
1568
1569 /*
1570 * Check for valid input
1571 */
1572 if( crl == NULL || buf == NULL )
1573 return( 1 );
1574
1575 while( crl->version != 0 && crl->next != NULL )
1576 crl = crl->next;
1577
1578 /*
1579 * Add new CRL on the end of the chain if needed.
1580 */
1581 if ( crl->version != 0 && crl->next == NULL)
1582 {
1583 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1584
Paul Bakker7d06ad22009-05-02 15:53:56 +00001585 if( crl->next == NULL )
1586 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001588 return( 1 );
1589 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001590
Paul Bakker7d06ad22009-05-02 15:53:56 +00001591 crl = crl->next;
1592 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001593 }
1594
Paul Bakker96743fc2011-02-12 14:30:57 +00001595#if defined(POLARSSL_PEM_C)
1596 pem_init( &pem );
1597 ret = pem_read_buffer( &pem,
1598 "-----BEGIN X509 CRL-----",
1599 "-----END X509 CRL-----",
1600 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601
Paul Bakker96743fc2011-02-12 14:30:57 +00001602 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001603 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001604 /*
1605 * Was PEM encoded
1606 */
1607 buflen -= use_len;
1608 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609
1610 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001611 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001613 p = pem.buf;
1614 pem.buf = NULL;
1615 len = pem.buflen;
1616 pem_free( &pem );
1617 }
1618 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1619 {
1620 pem_free( &pem );
1621 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001622 }
1623 else
1624 {
1625 /*
1626 * nope, copy the raw DER data
1627 */
1628 p = (unsigned char *) malloc( len = buflen );
1629
1630 if( p == NULL )
1631 return( 1 );
1632
1633 memcpy( p, buf, buflen );
1634
1635 buflen = 0;
1636 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001637#else
1638 p = (unsigned char *) malloc( len = buflen );
1639
1640 if( p == NULL )
1641 return( 1 );
1642
1643 memcpy( p, buf, buflen );
1644
1645 buflen = 0;
1646#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647
1648 crl->raw.p = p;
1649 crl->raw.len = len;
1650 end = p + len;
1651
1652 /*
1653 * CertificateList ::= SEQUENCE {
1654 * tbsCertList TBSCertList,
1655 * signatureAlgorithm AlgorithmIdentifier,
1656 * signatureValue BIT STRING }
1657 */
1658 if( ( ret = asn1_get_tag( &p, end, &len,
1659 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1663 }
1664
Paul Bakker23986e52011-04-24 08:57:21 +00001665 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666 {
1667 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001668 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1670 }
1671
1672 /*
1673 * TBSCertList ::= SEQUENCE {
1674 */
1675 crl->tbs.p = p;
1676
1677 if( ( ret = asn1_get_tag( &p, end, &len,
1678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1679 {
1680 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001681 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001682 }
1683
1684 end = p + len;
1685 crl->tbs.len = end - crl->tbs.p;
1686
1687 /*
1688 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1689 * -- if present, MUST be v2
1690 *
1691 * signature AlgorithmIdentifier
1692 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001693 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1695 {
1696 x509_crl_free( crl );
1697 return( ret );
1698 }
1699
1700 crl->version++;
1701
1702 if( crl->version > 2 )
1703 {
1704 x509_crl_free( crl );
1705 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1706 }
1707
Paul Bakker27d66162010-03-17 06:56:01 +00001708 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709 {
1710 x509_crl_free( crl );
1711 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1712 }
1713
1714 /*
1715 * issuer Name
1716 */
1717 crl->issuer_raw.p = p;
1718
1719 if( ( ret = asn1_get_tag( &p, end, &len,
1720 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1721 {
1722 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001723 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001724 }
1725
1726 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1727 {
1728 x509_crl_free( crl );
1729 return( ret );
1730 }
1731
1732 crl->issuer_raw.len = p - crl->issuer_raw.p;
1733
1734 /*
1735 * thisUpdate Time
1736 * nextUpdate Time OPTIONAL
1737 */
Paul Bakker91200182010-02-18 21:26:15 +00001738 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739 {
1740 x509_crl_free( crl );
1741 return( ret );
1742 }
1743
Paul Bakker91200182010-02-18 21:26:15 +00001744 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001745 {
Paul Bakker9d781402011-05-09 16:17:09 +00001746 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001747 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001748 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001749 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001750 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 x509_crl_free( crl );
1752 return( ret );
1753 }
1754 }
1755
1756 /*
1757 * revokedCertificates SEQUENCE OF SEQUENCE {
1758 * userCertificate CertificateSerialNumber,
1759 * revocationDate Time,
1760 * crlEntryExtensions Extensions OPTIONAL
1761 * -- if present, MUST be v2
1762 * } OPTIONAL
1763 */
1764 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769
1770 /*
1771 * crlExtensions EXPLICIT Extensions OPTIONAL
1772 * -- if present, MUST be v2
1773 */
1774 if( crl->version == 2 )
1775 {
1776 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1777
1778 if( ret != 0 )
1779 {
1780 x509_crl_free( crl );
1781 return( ret );
1782 }
1783 }
1784
1785 if( p != end )
1786 {
1787 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001788 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001789 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1790 }
1791
1792 end = crl->raw.p + crl->raw.len;
1793
1794 /*
1795 * signatureAlgorithm AlgorithmIdentifier,
1796 * signatureValue BIT STRING
1797 */
1798 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1799 {
1800 x509_crl_free( crl );
1801 return( ret );
1802 }
1803
1804 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1805 {
1806 x509_crl_free( crl );
1807 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1808 }
1809
1810 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1811 {
1812 x509_crl_free( crl );
1813 return( ret );
1814 }
1815
1816 if( p != end )
1817 {
1818 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001819 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1821 }
1822
1823 if( buflen > 0 )
1824 {
1825 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1826
Paul Bakker7d06ad22009-05-02 15:53:56 +00001827 if( crl->next == NULL )
1828 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001829 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001830 return( 1 );
1831 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832
Paul Bakker7d06ad22009-05-02 15:53:56 +00001833 crl = crl->next;
1834 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835
1836 return( x509parse_crl( crl, buf, buflen ) );
1837 }
1838
1839 return( 0 );
1840}
1841
Paul Bakker335db3f2011-04-25 15:28:35 +00001842#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001843/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001844 * Load all data from a file into a given buffer.
1845 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001846int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001847{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001849
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 if( ( f = fopen( path, "rb" ) ) == NULL )
1851 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001852
Paul Bakkerd98030e2009-05-02 15:13:40 +00001853 fseek( f, 0, SEEK_END );
1854 *n = (size_t) ftell( f );
1855 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001856
Paul Bakkerd98030e2009-05-02 15:13:40 +00001857 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1858 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001859
Paul Bakkerd98030e2009-05-02 15:13:40 +00001860 if( fread( *buf, 1, *n, f ) != *n )
1861 {
1862 fclose( f );
1863 free( *buf );
1864 return( 1 );
1865 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001866
Paul Bakkerd98030e2009-05-02 15:13:40 +00001867 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001868
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001870
Paul Bakkerd98030e2009-05-02 15:13:40 +00001871 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001872}
1873
1874/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 * Load one or more certificates and add them to the chained list
1876 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001877int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001878{
1879 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 size_t n;
1881 unsigned char *buf;
1882
Paul Bakker2b245eb2009-04-19 18:44:26 +00001883 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 return( 1 );
1885
Paul Bakker27fdf462011-06-09 13:55:13 +00001886 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 memset( buf, 0, n + 1 );
1889 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
1891 return( ret );
1892}
1893
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894/*
1895 * Load one or more CRLs and add them to the chained list
1896 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001897int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898{
1899 int ret;
1900 size_t n;
1901 unsigned char *buf;
1902
1903 if ( load_file( path, &buf, &n ) )
1904 return( 1 );
1905
Paul Bakker27fdf462011-06-09 13:55:13 +00001906 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001907
1908 memset( buf, 0, n + 1 );
1909 free( buf );
1910
1911 return( ret );
1912}
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001915 * Load and parse a private RSA key
1916 */
1917int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1918{
1919 int ret;
1920 size_t n;
1921 unsigned char *buf;
1922
1923 if ( load_file( path, &buf, &n ) )
1924 return( 1 );
1925
1926 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001927 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001928 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001929 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001930 (unsigned char *) pwd, strlen( pwd ) );
1931
1932 memset( buf, 0, n + 1 );
1933 free( buf );
1934
1935 return( ret );
1936}
1937
1938/*
1939 * Load and parse a public RSA key
1940 */
1941int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1942{
1943 int ret;
1944 size_t n;
1945 unsigned char *buf;
1946
1947 if ( load_file( path, &buf, &n ) )
1948 return( 1 );
1949
Paul Bakker27fdf462011-06-09 13:55:13 +00001950 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001951
1952 memset( buf, 0, n + 1 );
1953 free( buf );
1954
1955 return( ret );
1956}
1957#endif /* POLARSSL_FS_IO */
1958
1959/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 * Parse a private RSA key
1961 */
Paul Bakker23986e52011-04-24 08:57:21 +00001962int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1963 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001964{
Paul Bakker23986e52011-04-24 08:57:21 +00001965 int ret;
1966 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001968 unsigned char *p_alt;
1969 x509_buf pk_alg_oid;
1970
Paul Bakker96743fc2011-02-12 14:30:57 +00001971#if defined(POLARSSL_PEM_C)
1972 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
Paul Bakker96743fc2011-02-12 14:30:57 +00001974 pem_init( &pem );
1975 ret = pem_read_buffer( &pem,
1976 "-----BEGIN RSA PRIVATE KEY-----",
1977 "-----END RSA PRIVATE KEY-----",
1978 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979
Paul Bakkered56b222011-07-13 11:26:43 +00001980 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1981 {
1982 ret = pem_read_buffer( &pem,
1983 "-----BEGIN PRIVATE KEY-----",
1984 "-----END PRIVATE KEY-----",
1985 key, pwd, pwdlen, &len );
1986 }
1987
Paul Bakker96743fc2011-02-12 14:30:57 +00001988 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001990 /*
1991 * Was PEM encoded
1992 */
1993 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001995 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001996 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001997 pem_free( &pem );
1998 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Paul Bakker96743fc2011-02-12 14:30:57 +00002001 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2002#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002003 ((void) pwd);
2004 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002005 p = (unsigned char *) key;
2006#endif
2007 end = p + keylen;
2008
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002010 * Note: Depending on the type of private key file one can expect either a
2011 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2012 *
2013 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002014 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002015 * algorithm AlgorithmIdentifier,
2016 * PrivateKey BIT STRING
2017 * }
2018 *
2019 * AlgorithmIdentifier ::= SEQUENCE {
2020 * algorithm OBJECT IDENTIFIER,
2021 * parameters ANY DEFINED BY algorithm OPTIONAL
2022 * }
2023 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 * RSAPrivateKey ::= SEQUENCE {
2025 * version Version,
2026 * modulus INTEGER, -- n
2027 * publicExponent INTEGER, -- e
2028 * privateExponent INTEGER, -- d
2029 * prime1 INTEGER, -- p
2030 * prime2 INTEGER, -- q
2031 * exponent1 INTEGER, -- d mod (p-1)
2032 * exponent2 INTEGER, -- d mod (q-1)
2033 * coefficient INTEGER, -- (inverse of q) mod p
2034 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2035 * }
2036 */
2037 if( ( ret = asn1_get_tag( &p, end, &len,
2038 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2039 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002040#if defined(POLARSSL_PEM_C)
2041 pem_free( &pem );
2042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002044 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
2047 end = p + len;
2048
2049 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2050 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002051#if defined(POLARSSL_PEM_C)
2052 pem_free( &pem );
2053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002055 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
2058 if( rsa->ver != 0 )
2059 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002060#if defined(POLARSSL_PEM_C)
2061 pem_free( &pem );
2062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002064 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 }
2066
Paul Bakkered56b222011-07-13 11:26:43 +00002067 p_alt = p;
2068
2069 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2070 {
2071 // Assume that we have the PKCS#1 format if wrong
2072 // tag was encountered
2073 //
2074 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2075 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
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 );
2082 }
2083 }
2084 else
2085 {
2086 int can_handle;
2087
2088 /*
2089 * only RSA keys handled at this time
2090 */
2091 can_handle = 0;
2092
2093 if( pk_alg_oid.len == 9 &&
2094 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2095 can_handle = 1;
2096
2097 if( pk_alg_oid.len == 9 &&
2098 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2099 {
2100 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2101 can_handle = 1;
2102
2103 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2104 can_handle = 1;
2105 }
2106
2107 if( pk_alg_oid.len == 5 &&
2108 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2109 can_handle = 1;
2110
2111 if( can_handle == 0 )
2112 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2113
2114 /*
2115 * Parse the PKCS#8 format
2116 */
2117
2118 p = p_alt;
2119 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2120 {
2121#if defined(POLARSSL_PEM_C)
2122 pem_free( &pem );
2123#endif
2124 rsa_free( rsa );
2125 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2126 }
2127
2128 if( ( end - p ) < 1 )
2129 {
2130#if defined(POLARSSL_PEM_C)
2131 pem_free( &pem );
2132#endif
2133 rsa_free( rsa );
2134 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2135 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2136 }
2137
2138 end = p + len;
2139
2140 if( ( ret = asn1_get_tag( &p, end, &len,
2141 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2142 {
2143#if defined(POLARSSL_PEM_C)
2144 pem_free( &pem );
2145#endif
2146 rsa_free( rsa );
2147 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2148 }
2149
2150 end = p + len;
2151
2152 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2153 {
2154#if defined(POLARSSL_PEM_C)
2155 pem_free( &pem );
2156#endif
2157 rsa_free( rsa );
2158 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2159 }
2160
2161 if( rsa->ver != 0 )
2162 {
2163#if defined(POLARSSL_PEM_C)
2164 pem_free( &pem );
2165#endif
2166 rsa_free( rsa );
2167 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2168 }
2169 }
2170
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2172 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2173 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2174 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2175 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2176 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2177 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2178 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2179 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002180#if defined(POLARSSL_PEM_C)
2181 pem_free( &pem );
2182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002184 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
2186
2187 rsa->len = mpi_size( &rsa->N );
2188
2189 if( p != end )
2190 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002191#if defined(POLARSSL_PEM_C)
2192 pem_free( &pem );
2193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002195 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002196 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
2198
2199 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2200 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002201#if defined(POLARSSL_PEM_C)
2202 pem_free( &pem );
2203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 rsa_free( rsa );
2205 return( ret );
2206 }
2207
Paul Bakker96743fc2011-02-12 14:30:57 +00002208#if defined(POLARSSL_PEM_C)
2209 pem_free( &pem );
2210#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002211
2212 return( 0 );
2213}
2214
2215/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002216 * Parse a public RSA key
2217 */
Paul Bakker23986e52011-04-24 08:57:21 +00002218int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002219{
Paul Bakker23986e52011-04-24 08:57:21 +00002220 int ret;
2221 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002222 unsigned char *p, *end;
2223 x509_buf alg_oid;
2224#if defined(POLARSSL_PEM_C)
2225 pem_context pem;
2226
2227 pem_init( &pem );
2228 ret = pem_read_buffer( &pem,
2229 "-----BEGIN PUBLIC KEY-----",
2230 "-----END PUBLIC KEY-----",
2231 key, NULL, 0, &len );
2232
2233 if( ret == 0 )
2234 {
2235 /*
2236 * Was PEM encoded
2237 */
2238 keylen = pem.buflen;
2239 }
2240 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2241 {
2242 pem_free( &pem );
2243 return( ret );
2244 }
2245
2246 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2247#else
2248 p = (unsigned char *) key;
2249#endif
2250 end = p + keylen;
2251
2252 /*
2253 * PublicKeyInfo ::= SEQUENCE {
2254 * algorithm AlgorithmIdentifier,
2255 * PublicKey BIT STRING
2256 * }
2257 *
2258 * AlgorithmIdentifier ::= SEQUENCE {
2259 * algorithm OBJECT IDENTIFIER,
2260 * parameters ANY DEFINED BY algorithm OPTIONAL
2261 * }
2262 *
2263 * RSAPublicKey ::= SEQUENCE {
2264 * modulus INTEGER, -- n
2265 * publicExponent INTEGER -- e
2266 * }
2267 */
2268
2269 if( ( ret = asn1_get_tag( &p, end, &len,
2270 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2271 {
2272#if defined(POLARSSL_PEM_C)
2273 pem_free( &pem );
2274#endif
2275 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002276 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002277 }
2278
2279 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2280 {
2281#if defined(POLARSSL_PEM_C)
2282 pem_free( &pem );
2283#endif
2284 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002285 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002286 }
2287
2288 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2289 {
2290#if defined(POLARSSL_PEM_C)
2291 pem_free( &pem );
2292#endif
2293 rsa_free( rsa );
2294 return( ret );
2295 }
2296
2297 rsa->len = mpi_size( &rsa->N );
2298
2299#if defined(POLARSSL_PEM_C)
2300 pem_free( &pem );
2301#endif
2302
2303 return( 0 );
2304}
2305
Paul Bakkereaa89f82011-04-04 21:36:15 +00002306#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002307/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002308 * Parse DHM parameters
2309 */
Paul Bakker23986e52011-04-24 08:57:21 +00002310int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002311{
Paul Bakker23986e52011-04-24 08:57:21 +00002312 int ret;
2313 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002314 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002315#if defined(POLARSSL_PEM_C)
2316 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002317
Paul Bakker96743fc2011-02-12 14:30:57 +00002318 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002319
Paul Bakker96743fc2011-02-12 14:30:57 +00002320 ret = pem_read_buffer( &pem,
2321 "-----BEGIN DH PARAMETERS-----",
2322 "-----END DH PARAMETERS-----",
2323 dhmin, NULL, 0, &dhminlen );
2324
2325 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002326 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002327 /*
2328 * Was PEM encoded
2329 */
2330 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002331 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002332 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002333 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002334 pem_free( &pem );
2335 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002336 }
2337
Paul Bakker96743fc2011-02-12 14:30:57 +00002338 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2339#else
2340 p = (unsigned char *) dhmin;
2341#endif
2342 end = p + dhminlen;
2343
Paul Bakker1b57b062011-01-06 15:48:19 +00002344 memset( dhm, 0, sizeof( dhm_context ) );
2345
Paul Bakker1b57b062011-01-06 15:48:19 +00002346 /*
2347 * DHParams ::= SEQUENCE {
2348 * prime INTEGER, -- P
2349 * generator INTEGER, -- g
2350 * }
2351 */
2352 if( ( ret = asn1_get_tag( &p, end, &len,
2353 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2354 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002355#if defined(POLARSSL_PEM_C)
2356 pem_free( &pem );
2357#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002358 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002359 }
2360
2361 end = p + len;
2362
2363 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2364 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2365 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002366#if defined(POLARSSL_PEM_C)
2367 pem_free( &pem );
2368#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002369 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002370 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002371 }
2372
2373 if( p != end )
2374 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002375#if defined(POLARSSL_PEM_C)
2376 pem_free( &pem );
2377#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002378 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002379 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002380 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2381 }
2382
Paul Bakker96743fc2011-02-12 14:30:57 +00002383#if defined(POLARSSL_PEM_C)
2384 pem_free( &pem );
2385#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002386
2387 return( 0 );
2388}
2389
Paul Bakker335db3f2011-04-25 15:28:35 +00002390#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002391/*
2392 * Load and parse a private RSA key
2393 */
2394int x509parse_dhmfile( dhm_context *dhm, const char *path )
2395{
2396 int ret;
2397 size_t n;
2398 unsigned char *buf;
2399
2400 if ( load_file( path, &buf, &n ) )
2401 return( 1 );
2402
Paul Bakker27fdf462011-06-09 13:55:13 +00002403 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002404
2405 memset( buf, 0, n + 1 );
2406 free( buf );
2407
2408 return( ret );
2409}
Paul Bakker335db3f2011-04-25 15:28:35 +00002410#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002411#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002412
Paul Bakker5121ce52009-01-03 21:22:43 +00002413#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414#include <stdarg.h>
2415
2416#if !defined vsnprintf
2417#define vsnprintf _vsnprintf
2418#endif // vsnprintf
2419
2420/*
2421 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2422 * Result value is not size of buffer needed, but -1 if no fit is possible.
2423 *
2424 * This fuction tries to 'fix' this by at least suggesting enlarging the
2425 * size by 20.
2426 */
2427int compat_snprintf(char *str, size_t size, const char *format, ...)
2428{
2429 va_list ap;
2430 int res = -1;
2431
2432 va_start( ap, format );
2433
2434 res = vsnprintf( str, size, format, ap );
2435
2436 va_end( ap );
2437
2438 // No quick fix possible
2439 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002440 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002441
2442 return res;
2443}
2444
2445#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002446#endif
2447
Paul Bakkerd98030e2009-05-02 15:13:40 +00002448#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2449
2450#define SAFE_SNPRINTF() \
2451{ \
2452 if( ret == -1 ) \
2453 return( -1 ); \
2454 \
Paul Bakker23986e52011-04-24 08:57:21 +00002455 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002456 p[n - 1] = '\0'; \
2457 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2458 } \
2459 \
Paul Bakker23986e52011-04-24 08:57:21 +00002460 n -= (unsigned int) ret; \
2461 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002462}
2463
Paul Bakker5121ce52009-01-03 21:22:43 +00002464/*
2465 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002466 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002468int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002469{
Paul Bakker23986e52011-04-24 08:57:21 +00002470 int ret;
2471 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002473 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 char s[128], *p;
2475
2476 memset( s, 0, sizeof( s ) );
2477
2478 name = dn;
2479 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002480 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
2482 while( name != NULL )
2483 {
Paul Bakker74111d32011-01-15 16:57:55 +00002484 if( name != dn )
2485 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002486 ret = snprintf( p, n, ", " );
2487 SAFE_SNPRINTF();
2488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
2490 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2491 {
2492 switch( name->oid.p[2] )
2493 {
2494 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002495 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
2500 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002501 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
2503 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002504 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
2509 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002510 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
2512 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002513 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 name->oid.p[2] );
2515 break;
2516 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002517 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 }
2519 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2520 {
2521 switch( name->oid.p[8] )
2522 {
2523 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002524 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
2526 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 name->oid.p[8] );
2529 break;
2530 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002531 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533 else
Paul Bakker74111d32011-01-15 16:57:55 +00002534 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002536 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
2539 for( i = 0; i < name->val.len; i++ )
2540 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002541 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 break;
2543
2544 c = name->val.p[i];
2545 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2546 s[i] = '?';
2547 else s[i] = c;
2548 }
2549 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002550 ret = snprintf( p, n, "%s", s );
2551 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 name = name->next;
2553 }
2554
Paul Bakker23986e52011-04-24 08:57:21 +00002555 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002556}
2557
2558/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002559 * Store the serial in printable form into buf; no more
2560 * than size characters will be written
2561 */
2562int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2563{
Paul Bakker23986e52011-04-24 08:57:21 +00002564 int ret;
2565 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002566 char *p;
2567
2568 p = buf;
2569 n = size;
2570
2571 nr = ( serial->len <= 32 )
2572 ? serial->len : 32;
2573
2574 for( i = 0; i < nr; i++ )
2575 {
2576 ret = snprintf( p, n, "%02X%s",
2577 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2578 SAFE_SNPRINTF();
2579 }
2580
Paul Bakker23986e52011-04-24 08:57:21 +00002581 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002582}
2583
2584/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002585 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002587int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2588 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002589{
Paul Bakker23986e52011-04-24 08:57:21 +00002590 int ret;
2591 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002592 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002595 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002599 SAFE_SNPRINTF();
2600 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002602 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Paul Bakkerdd476992011-01-16 21:34:59 +00002604 ret = x509parse_serial_gets( p, n, &crt->serial);
2605 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
Paul Bakkerd98030e2009-05-02 15:13:40 +00002607 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2608 SAFE_SNPRINTF();
2609 ret = x509parse_dn_gets( p, n, &crt->issuer );
2610 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakkerd98030e2009-05-02 15:13:40 +00002612 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2613 SAFE_SNPRINTF();
2614 ret = x509parse_dn_gets( p, n, &crt->subject );
2615 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Paul Bakkerd98030e2009-05-02 15:13:40 +00002617 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2619 crt->valid_from.year, crt->valid_from.mon,
2620 crt->valid_from.day, crt->valid_from.hour,
2621 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002622 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Paul Bakkerd98030e2009-05-02 15:13:40 +00002624 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2626 crt->valid_to.year, crt->valid_to.mon,
2627 crt->valid_to.day, crt->valid_to.hour,
2628 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002629 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
Paul Bakkerd98030e2009-05-02 15:13:40 +00002631 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2632 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
Paul Bakker27d66162010-03-17 06:56:01 +00002634 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002636 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2637 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2638 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2639 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2640 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2641 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2642 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2643 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2644 default: ret = snprintf( p, n, "???" ); break;
2645 }
2646 SAFE_SNPRINTF();
2647
2648 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002649 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002650 SAFE_SNPRINTF();
2651
Paul Bakker23986e52011-04-24 08:57:21 +00002652 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653}
2654
Paul Bakker74111d32011-01-15 16:57:55 +00002655/* Compare a given OID string with an OID x509_buf * */
2656#define OID_CMP(oid_str, oid_buf) \
2657 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2658 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2659
2660/*
2661 * Return an informational string describing the given OID
2662 */
2663const char *x509_oid_get_description( x509_buf *oid )
2664{
2665 if ( oid == NULL )
2666 return ( NULL );
2667
2668 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2669 return( STRING_SERVER_AUTH );
2670
2671 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2672 return( STRING_CLIENT_AUTH );
2673
2674 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2675 return( STRING_CODE_SIGNING );
2676
2677 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2678 return( STRING_EMAIL_PROTECTION );
2679
2680 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2681 return( STRING_TIME_STAMPING );
2682
2683 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2684 return( STRING_OCSP_SIGNING );
2685
2686 return( NULL );
2687}
2688
2689/* Return the x.y.z.... style numeric string for the given OID */
2690int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2691{
Paul Bakker23986e52011-04-24 08:57:21 +00002692 int ret;
2693 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002694 unsigned int value;
2695 char *p;
2696
2697 p = buf;
2698 n = size;
2699
2700 /* First byte contains first two dots */
2701 if( oid->len > 0 )
2702 {
2703 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2704 SAFE_SNPRINTF();
2705 }
2706
2707 /* TODO: value can overflow in value. */
2708 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002709 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002710 {
2711 value <<= 7;
2712 value += oid->p[i] & 0x7F;
2713
2714 if( !( oid->p[i] & 0x80 ) )
2715 {
2716 /* Last byte */
2717 ret = snprintf( p, n, ".%d", value );
2718 SAFE_SNPRINTF();
2719 value = 0;
2720 }
2721 }
2722
Paul Bakker23986e52011-04-24 08:57:21 +00002723 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002724}
2725
Paul Bakkerd98030e2009-05-02 15:13:40 +00002726/*
2727 * Return an informational string about the CRL.
2728 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002729int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2730 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002731{
Paul Bakker23986e52011-04-24 08:57:21 +00002732 int ret;
2733 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002734 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002735 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002736
2737 p = buf;
2738 n = size;
2739
2740 ret = snprintf( p, n, "%sCRL version : %d",
2741 prefix, crl->version );
2742 SAFE_SNPRINTF();
2743
2744 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2745 SAFE_SNPRINTF();
2746 ret = x509parse_dn_gets( p, n, &crl->issuer );
2747 SAFE_SNPRINTF();
2748
2749 ret = snprintf( p, n, "\n%sthis update : " \
2750 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2751 crl->this_update.year, crl->this_update.mon,
2752 crl->this_update.day, crl->this_update.hour,
2753 crl->this_update.min, crl->this_update.sec );
2754 SAFE_SNPRINTF();
2755
2756 ret = snprintf( p, n, "\n%snext update : " \
2757 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2758 crl->next_update.year, crl->next_update.mon,
2759 crl->next_update.day, crl->next_update.hour,
2760 crl->next_update.min, crl->next_update.sec );
2761 SAFE_SNPRINTF();
2762
2763 entry = &crl->entry;
2764
2765 ret = snprintf( p, n, "\n%sRevoked certificates:",
2766 prefix );
2767 SAFE_SNPRINTF();
2768
Paul Bakker9be19372009-07-27 20:21:53 +00002769 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002770 {
2771 ret = snprintf( p, n, "\n%sserial number: ",
2772 prefix );
2773 SAFE_SNPRINTF();
2774
2775 nr = ( entry->serial.len <= 32 )
2776 ? entry->serial.len : 32;
2777
Paul Bakker74111d32011-01-15 16:57:55 +00002778 for( i = 0; i < nr; i++ )
2779 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002780 ret = snprintf( p, n, "%02X%s",
2781 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2782 SAFE_SNPRINTF();
2783 }
2784
2785 ret = snprintf( p, n, " revocation date: " \
2786 "%04d-%02d-%02d %02d:%02d:%02d",
2787 entry->revocation_date.year, entry->revocation_date.mon,
2788 entry->revocation_date.day, entry->revocation_date.hour,
2789 entry->revocation_date.min, entry->revocation_date.sec );
2790 SAFE_SNPRINTF();
2791
2792 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
2794
Paul Bakkerd98030e2009-05-02 15:13:40 +00002795 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2796 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Paul Bakker27d66162010-03-17 06:56:01 +00002798 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002799 {
2800 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2801 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2802 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2803 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2804 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2805 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2806 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2807 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2808 default: ret = snprintf( p, n, "???" ); break;
2809 }
2810 SAFE_SNPRINTF();
2811
Paul Bakker1e27bb22009-07-19 20:25:25 +00002812 ret = snprintf( p, n, "\n" );
2813 SAFE_SNPRINTF();
2814
Paul Bakker23986e52011-04-24 08:57:21 +00002815 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816}
2817
2818/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002819 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002821int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822{
2823 struct tm *lt;
2824 time_t tt;
2825
2826 tt = time( NULL );
2827 lt = localtime( &tt );
2828
Paul Bakker40ea7de2009-05-03 10:18:48 +00002829 if( lt->tm_year > to->year - 1900 )
2830 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakker40ea7de2009-05-03 10:18:48 +00002832 if( lt->tm_year == to->year - 1900 &&
2833 lt->tm_mon > to->mon - 1 )
2834 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Paul Bakker40ea7de2009-05-03 10:18:48 +00002836 if( lt->tm_year == to->year - 1900 &&
2837 lt->tm_mon == to->mon - 1 &&
2838 lt->tm_mday > to->day )
2839 return( 1 );
2840
Paul Bakkerb6194992011-01-16 21:40:22 +00002841 if( lt->tm_year == to->year - 1900 &&
2842 lt->tm_mon == to->mon - 1 &&
2843 lt->tm_mday == to->day &&
2844 lt->tm_hour > to->hour - 1)
2845 return( 1 );
2846
2847 if( lt->tm_year == to->year - 1900 &&
2848 lt->tm_mon == to->mon - 1 &&
2849 lt->tm_mday == to->day &&
2850 lt->tm_hour == to->hour - 1 &&
2851 lt->tm_min > to->min - 1 )
2852 return( 1 );
2853
2854 if( lt->tm_year == to->year - 1900 &&
2855 lt->tm_mon == to->mon - 1 &&
2856 lt->tm_mday == to->day &&
2857 lt->tm_hour == to->hour - 1 &&
2858 lt->tm_min == to->min - 1 &&
2859 lt->tm_sec > to->sec - 1 )
2860 return( 1 );
2861
Paul Bakker40ea7de2009-05-03 10:18:48 +00002862 return( 0 );
2863}
2864
2865/*
2866 * Return 1 if the certificate is revoked, or 0 otherwise.
2867 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002868int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002869{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002870 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002871
2872 while( cur != NULL && cur->serial.len != 0 )
2873 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002874 if( crt->serial.len == cur->serial.len &&
2875 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002876 {
2877 if( x509parse_time_expired( &cur->revocation_date ) )
2878 return( 1 );
2879 }
2880
2881 cur = cur->next;
2882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
2884 return( 0 );
2885}
2886
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002887/*
2888 * Wrapper for x509 hashes.
2889 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002890 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002891 */
Paul Bakker23986e52011-04-24 08:57:21 +00002892static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 unsigned char *out )
2894{
2895 switch( alg )
2896 {
Paul Bakker40e46942009-01-03 21:51:57 +00002897#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002898 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002899#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002900#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002901 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002902#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002903#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002904 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002905#endif
2906#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002907 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002908#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002909#if defined(POLARSSL_SHA2_C)
2910 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2911 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2912#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002913#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002914 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2915 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2916#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002918 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 break;
2920 }
2921}
2922
2923/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002924 * Check that the given certificate is valid accoring to the CRL.
2925 */
2926static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2927 x509_crl *crl_list)
2928{
2929 int flags = 0;
2930 int hash_id;
2931 unsigned char hash[64];
2932
2933 /*
2934 * TODO: What happens if no CRL is present?
2935 * Suggestion: Revocation state should be unknown if no CRL is present.
2936 * For backwards compatibility this is not yet implemented.
2937 */
2938
2939 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2940 {
2941 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2942 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2943 crl_list->issuer_raw.len ) != 0 )
2944 {
2945 crl_list = crl_list->next;
2946 continue;
2947 }
2948
2949 /*
2950 * Check if CRL is correctly signed by the trusted CA
2951 */
2952 hash_id = crl_list->sig_alg;
2953
2954 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2955
2956 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2957 0, hash, crl_list->sig.p ) == 0 )
2958 {
2959 /*
2960 * CRL is not trusted
2961 */
2962 flags |= BADCRL_NOT_TRUSTED;
2963 break;
2964 }
2965
2966 /*
2967 * Check for validity of CRL (Do not drop out)
2968 */
2969 if( x509parse_time_expired( &crl_list->next_update ) )
2970 flags |= BADCRL_EXPIRED;
2971
2972 /*
2973 * Check if certificate is revoked
2974 */
2975 if( x509parse_revoked(crt, crl_list) )
2976 {
2977 flags |= BADCERT_REVOKED;
2978 break;
2979 }
2980
2981 crl_list = crl_list->next;
2982 }
2983 return flags;
2984}
2985
2986/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 * Verify the certificate validity
2988 */
2989int x509parse_verify( x509_cert *crt,
2990 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002991 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002992 const char *cn, int *flags,
2993 int (*f_vrfy)(void *, x509_cert *, int, int),
2994 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002995{
Paul Bakker23986e52011-04-24 08:57:21 +00002996 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 int hash_id;
2998 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002999 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00003001 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00003002
Paul Bakker40ea7de2009-05-03 10:18:48 +00003003 *flags = 0;
3004
3005 if( x509parse_time_expired( &crt->valid_to ) )
3006 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003007
3008 if( cn != NULL )
3009 {
3010 name = &crt->subject;
3011 cn_len = strlen( cn );
3012
3013 while( name != NULL )
3014 {
3015 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
3016 memcmp( name->val.p, cn, cn_len ) == 0 &&
3017 name->val.len == cn_len )
3018 break;
3019
3020 name = name->next;
3021 }
3022
3023 if( name == NULL )
3024 *flags |= BADCERT_CN_MISMATCH;
3025 }
3026
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 /*
3028 * Iterate upwards in the given cert chain,
3029 * ignoring any upper cert with CA != TRUE.
3030 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003031 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
3033 pathlen = 1;
3034
Paul Bakker76fd75a2011-01-16 21:12:10 +00003035 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003037 if( parent->ca_istrue == 0 ||
3038 crt->issuer_raw.len != parent->subject_raw.len ||
3039 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 crt->issuer_raw.len ) != 0 )
3041 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003042 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 continue;
3044 }
3045
Paul Bakker27d66162010-03-17 06:56:01 +00003046 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
3048 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3049
Paul Bakker76fd75a2011-01-16 21:12:10 +00003050 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3051 crt->sig.p ) != 0 )
3052 *flags |= BADCERT_NOT_TRUSTED;
3053
3054 /* Check trusted CA's CRL for the given crt */
3055 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003056
3057 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00003058 if( NULL != f_vrfy )
3059 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003060 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003061 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003062 else
3063 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003064 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003065 else if( *flags != 0 )
3066 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
3068 pathlen++;
3069
Paul Bakker76fd75a2011-01-16 21:12:10 +00003070 crt = parent;
3071 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
3073
3074 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003075 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003077 *flags |= BADCERT_NOT_TRUSTED;
3078
Paul Bakker7c6d4a42009-03-28 20:35:47 +00003079 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
3081 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
3082 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
3083 crt->issuer_raw.len ) != 0 )
3084 {
3085 trust_ca = trust_ca->next;
3086 continue;
3087 }
3088
3089 if( trust_ca->max_pathlen > 0 &&
3090 trust_ca->max_pathlen < pathlen )
3091 break;
3092
Paul Bakker27d66162010-03-17 06:56:01 +00003093 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
3095 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3096
3097 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3098 0, hash, crt->sig.p ) == 0 )
3099 {
3100 /*
3101 * cert. is signed by a trusted CA
3102 */
3103 *flags &= ~BADCERT_NOT_TRUSTED;
3104 break;
3105 }
3106
3107 trust_ca = trust_ca->next;
3108 }
3109
Paul Bakker76fd75a2011-01-16 21:12:10 +00003110 /* Check trusted CA's CRL for the given crt */
3111 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003112
3113 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003114 if( NULL != f_vrfy )
3115 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003116 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003117 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003118 else
3119 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003120 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003121 else if( *flags != 0 )
3122 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003123
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 return( 0 );
3125}
3126
3127/*
3128 * Unallocate all certificate data
3129 */
3130void x509_free( x509_cert *crt )
3131{
3132 x509_cert *cert_cur = crt;
3133 x509_cert *cert_prv;
3134 x509_name *name_cur;
3135 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003136 x509_sequence *seq_cur;
3137 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 if( crt == NULL )
3140 return;
3141
3142 do
3143 {
3144 rsa_free( &cert_cur->rsa );
3145
3146 name_cur = cert_cur->issuer.next;
3147 while( name_cur != NULL )
3148 {
3149 name_prv = name_cur;
3150 name_cur = name_cur->next;
3151 memset( name_prv, 0, sizeof( x509_name ) );
3152 free( name_prv );
3153 }
3154
3155 name_cur = cert_cur->subject.next;
3156 while( name_cur != NULL )
3157 {
3158 name_prv = name_cur;
3159 name_cur = name_cur->next;
3160 memset( name_prv, 0, sizeof( x509_name ) );
3161 free( name_prv );
3162 }
3163
Paul Bakker74111d32011-01-15 16:57:55 +00003164 seq_cur = cert_cur->ext_key_usage.next;
3165 while( seq_cur != NULL )
3166 {
3167 seq_prv = seq_cur;
3168 seq_cur = seq_cur->next;
3169 memset( seq_prv, 0, sizeof( x509_sequence ) );
3170 free( seq_prv );
3171 }
3172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 if( cert_cur->raw.p != NULL )
3174 {
3175 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3176 free( cert_cur->raw.p );
3177 }
3178
3179 cert_cur = cert_cur->next;
3180 }
3181 while( cert_cur != NULL );
3182
3183 cert_cur = crt;
3184 do
3185 {
3186 cert_prv = cert_cur;
3187 cert_cur = cert_cur->next;
3188
3189 memset( cert_prv, 0, sizeof( x509_cert ) );
3190 if( cert_prv != crt )
3191 free( cert_prv );
3192 }
3193 while( cert_cur != NULL );
3194}
3195
Paul Bakkerd98030e2009-05-02 15:13:40 +00003196/*
3197 * Unallocate all CRL data
3198 */
3199void x509_crl_free( x509_crl *crl )
3200{
3201 x509_crl *crl_cur = crl;
3202 x509_crl *crl_prv;
3203 x509_name *name_cur;
3204 x509_name *name_prv;
3205 x509_crl_entry *entry_cur;
3206 x509_crl_entry *entry_prv;
3207
3208 if( crl == NULL )
3209 return;
3210
3211 do
3212 {
3213 name_cur = crl_cur->issuer.next;
3214 while( name_cur != NULL )
3215 {
3216 name_prv = name_cur;
3217 name_cur = name_cur->next;
3218 memset( name_prv, 0, sizeof( x509_name ) );
3219 free( name_prv );
3220 }
3221
3222 entry_cur = crl_cur->entry.next;
3223 while( entry_cur != NULL )
3224 {
3225 entry_prv = entry_cur;
3226 entry_cur = entry_cur->next;
3227 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3228 free( entry_prv );
3229 }
3230
3231 if( crl_cur->raw.p != NULL )
3232 {
3233 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3234 free( crl_cur->raw.p );
3235 }
3236
3237 crl_cur = crl_cur->next;
3238 }
3239 while( crl_cur != NULL );
3240
3241 crl_cur = crl;
3242 do
3243 {
3244 crl_prv = crl_cur;
3245 crl_cur = crl_cur->next;
3246
3247 memset( crl_prv, 0, sizeof( x509_crl ) );
3248 if( crl_prv != crl )
3249 free( crl_prv );
3250 }
3251 while( crl_cur != NULL );
3252}
3253
Paul Bakker40e46942009-01-03 21:51:57 +00003254#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
Paul Bakker40e46942009-01-03 21:51:57 +00003256#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258/*
3259 * Checkup routine
3260 */
3261int x509_self_test( int verbose )
3262{
Paul Bakker5690efc2011-05-26 13:16:06 +00003263#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003264 int ret;
3265 int flags;
3266 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 x509_cert cacert;
3268 x509_cert clicert;
3269 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003270#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003271 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
3274 if( verbose != 0 )
3275 printf( " X.509 certificate load: " );
3276
3277 memset( &clicert, 0, sizeof( x509_cert ) );
3278
3279 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3280 strlen( test_cli_crt ) );
3281 if( ret != 0 )
3282 {
3283 if( verbose != 0 )
3284 printf( "failed\n" );
3285
3286 return( ret );
3287 }
3288
3289 memset( &cacert, 0, sizeof( x509_cert ) );
3290
3291 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3292 strlen( test_ca_crt ) );
3293 if( ret != 0 )
3294 {
3295 if( verbose != 0 )
3296 printf( "failed\n" );
3297
3298 return( ret );
3299 }
3300
3301 if( verbose != 0 )
3302 printf( "passed\n X.509 private key load: " );
3303
3304 i = strlen( test_ca_key );
3305 j = strlen( test_ca_pwd );
3306
Paul Bakker66b78b22011-03-25 14:22:50 +00003307 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 if( ( ret = x509parse_key( &rsa,
3310 (unsigned char *) test_ca_key, i,
3311 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3312 {
3313 if( verbose != 0 )
3314 printf( "failed\n" );
3315
3316 return( ret );
3317 }
3318
3319 if( verbose != 0 )
3320 printf( "passed\n X.509 signature verify: ");
3321
Paul Bakker23986e52011-04-24 08:57:21 +00003322 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 if( ret != 0 )
3324 {
Paul Bakker23986e52011-04-24 08:57:21 +00003325 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 if( verbose != 0 )
3327 printf( "failed\n" );
3328
3329 return( ret );
3330 }
3331
Paul Bakker5690efc2011-05-26 13:16:06 +00003332#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003334 printf( "passed\n X.509 DHM parameter load: " );
3335
3336 i = strlen( test_dhm_params );
3337 j = strlen( test_ca_pwd );
3338
3339 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3340 {
3341 if( verbose != 0 )
3342 printf( "failed\n" );
3343
3344 return( ret );
3345 }
3346
3347 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003349#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003350
3351 x509_free( &cacert );
3352 x509_free( &clicert );
3353 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003354#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003355 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003359#else
3360 ((void) verbose);
3361 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003363}
3364
3365#endif
3366
3367#endif