blob: 85671e062bb41b8b1cb3977053c569ba80c2cf7d [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
92 default:
Paul Bakker40e46942009-01-03 21:51:57 +000093 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +000094 break;
95 }
96 }
97
Paul Bakker23986e52011-04-24 08:57:21 +000098 if( *len > (size_t) ( end - *p ) )
Paul Bakker40e46942009-01-03 21:51:57 +000099 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101 return( 0 );
102}
103
104static int asn1_get_tag( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +0000106 size_t *len, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000107{
108 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000109 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 if( **p != tag )
Paul Bakker40e46942009-01-03 21:51:57 +0000112 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 (*p)++;
115
116 return( asn1_get_len( p, end, len ) );
117}
118
119static int asn1_get_bool( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 int *val )
122{
Paul Bakker23986e52011-04-24 08:57:21 +0000123 int ret;
124 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
127 return( ret );
128
129 if( len != 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000130 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132 *val = ( **p != 0 ) ? 1 : 0;
133 (*p)++;
134
135 return( 0 );
136}
137
138static int asn1_get_int( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000139 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 int *val )
141{
Paul Bakker23986e52011-04-24 08:57:21 +0000142 int ret;
143 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
146 return( ret );
147
148 if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000149 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 *val = 0;
152
153 while( len-- > 0 )
154 {
155 *val = ( *val << 8 ) | **p;
156 (*p)++;
157 }
158
159 return( 0 );
160}
161
162static int asn1_get_mpi( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000163 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 mpi *X )
165{
Paul Bakker23986e52011-04-24 08:57:21 +0000166 int ret;
167 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
170 return( ret );
171
172 ret = mpi_read_binary( X, *p, len );
173
174 *p += len;
175
176 return( ret );
177}
178
Paul Bakker74111d32011-01-15 16:57:55 +0000179static int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
180 x509_bitstring *bs)
181{
182 int ret;
183
184 /* Certificate type is a single byte bitstring */
185 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
186 return( ret );
187
188 /* Check length, subtract one for actual bit string length */
189 if ( bs->len < 1 )
190 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
191 bs->len -= 1;
192
193 /* Get number of unused bits, ensure unused bits <= 7 */
194 bs->unused_bits = **p;
195 if( bs->unused_bits > 7 )
196 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
197 (*p)++;
198
199 /* Get actual bitstring */
200 bs->p = *p;
201 *p += bs->len;
202
203 if( *p != end )
204 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
205
206 return 0;
207}
208
209
210/*
211 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
212 */
213static int asn1_get_sequence_of( unsigned char **p,
214 const unsigned char *end,
215 x509_sequence *cur,
216 int tag)
217{
Paul Bakker23986e52011-04-24 08:57:21 +0000218 int ret;
219 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000220 x509_buf *buf;
221
222 /* Get main sequence tag */
223 if( ( ret = asn1_get_tag( p, end, &len,
224 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
225 return( ret );
226
227 if( *p + len != end )
228 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
229
230 while( *p < end )
231 {
232 buf = &(cur->buf);
233 buf->tag = **p;
234
235 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
236 return( ret );
237
238 buf->p = *p;
239 *p += buf->len;
240
241 /* Allocate and assign next pointer */
242 if (*p < end)
243 {
244 cur->next = (x509_sequence *) malloc(
245 sizeof( x509_sequence ) );
246
247 if( cur->next == NULL )
248 return( 1 );
249
250 cur = cur->next;
251 }
252 }
253
254 /* Set final sequence entry's next pointer to NULL */
255 cur->next = NULL;
256
257 if( *p != end )
258 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
259
260 return( 0 );
261}
262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263/*
264 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
265 */
266static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000267 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 int *ver )
269{
Paul Bakker23986e52011-04-24 08:57:21 +0000270 int ret;
271 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
273 if( ( ret = asn1_get_tag( p, end, &len,
274 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
275 {
Paul Bakker40e46942009-01-03 21:51:57 +0000276 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 return( *ver = 0 );
278
279 return( ret );
280 }
281
282 end = *p + len;
283
284 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000285 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000289 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291 return( 0 );
292}
293
294/*
295 * CertificateSerialNumber ::= INTEGER
296 */
297static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000298 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 x509_buf *serial )
300{
301 int ret;
302
303 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
308 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000309 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000310 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 serial->tag = *(*p)++;
313
314 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000315 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 serial->p = *p;
318 *p += serial->len;
319
320 return( 0 );
321}
322
323/*
324 * AlgorithmIdentifier ::= SEQUENCE {
325 * algorithm OBJECT IDENTIFIER,
326 * parameters ANY DEFINED BY algorithm OPTIONAL }
327 */
328static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000329 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 x509_buf *alg )
331{
Paul Bakker23986e52011-04-24 08:57:21 +0000332 int ret;
333 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 if( ( ret = asn1_get_tag( p, end, &len,
336 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000337 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339 end = *p + len;
340 alg->tag = **p;
341
342 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000343 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 alg->p = *p;
346 *p += alg->len;
347
348 if( *p == end )
349 return( 0 );
350
351 /*
352 * assume the algorithm parameters must be NULL
353 */
354 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000355 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000358 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000359 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
361 return( 0 );
362}
363
364/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 * AttributeTypeAndValue ::= SEQUENCE {
366 * type AttributeType,
367 * value AttributeValue }
368 *
369 * AttributeType ::= OBJECT IDENTIFIER
370 *
371 * AttributeValue ::= ANY DEFINED BY AttributeType
372 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000373static int x509_get_attr_type_value( unsigned char **p,
374 const unsigned char *end,
375 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000376{
Paul Bakker23986e52011-04-24 08:57:21 +0000377 int ret;
378 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 x509_buf *oid;
380 x509_buf *val;
381
382 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000384 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 oid = &cur->oid;
387 oid->tag = **p;
388
389 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000390 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 oid->p = *p;
393 *p += oid->len;
394
395 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000397 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
400 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
401 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000402 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000403 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 val = &cur->val;
406 val->tag = *(*p)++;
407
408 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000409 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411 val->p = *p;
412 *p += val->len;
413
414 cur->next = NULL;
415
Paul Bakker400ff6f2011-02-20 10:40:16 +0000416 return( 0 );
417}
418
419/*
420 * RelativeDistinguishedName ::=
421 * SET OF AttributeTypeAndValue
422 *
423 * AttributeTypeAndValue ::= SEQUENCE {
424 * type AttributeType,
425 * value AttributeValue }
426 *
427 * AttributeType ::= OBJECT IDENTIFIER
428 *
429 * AttributeValue ::= ANY DEFINED BY AttributeType
430 */
431static int x509_get_name( unsigned char **p,
432 const unsigned char *end,
433 x509_name *cur )
434{
Paul Bakker23986e52011-04-24 08:57:21 +0000435 int ret;
436 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000437 const unsigned char *end2;
438 x509_name *use;
439
440 if( ( ret = asn1_get_tag( p, end, &len,
441 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000442 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000443
444 end2 = end;
445 end = *p + len;
446 use = cur;
447
448 do
449 {
450 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
451 return( ret );
452
453 if( *p != end )
454 {
455 use->next = (x509_name *) malloc(
456 sizeof( x509_name ) );
457
458 if( use->next == NULL )
459 return( 1 );
460
461 memset( use->next, 0, sizeof( x509_name ) );
462
463 use = use->next;
464 }
465 }
466 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * recurse until end of SEQUENCE is reached
470 */
471 if( *p == end2 )
472 return( 0 );
473
474 cur->next = (x509_name *) malloc(
475 sizeof( x509_name ) );
476
477 if( cur->next == NULL )
478 return( 1 );
479
480 return( x509_get_name( p, end2, cur->next ) );
481}
482
483/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 * Time ::= CHOICE {
485 * utcTime UTCTime,
486 * generalTime GeneralizedTime }
487 */
Paul Bakker91200182010-02-18 21:26:15 +0000488static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000489 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000490 x509_time *time )
491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 int ret;
493 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000494 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000495 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000496
Paul Bakker91200182010-02-18 21:26:15 +0000497 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000498 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
499 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000500
Paul Bakker91200182010-02-18 21:26:15 +0000501 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000502
Paul Bakker91200182010-02-18 21:26:15 +0000503 if ( tag == ASN1_UTC_TIME )
504 {
505 (*p)++;
506 ret = asn1_get_len( p, end, &len );
507
508 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000509 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000510
Paul Bakker91200182010-02-18 21:26:15 +0000511 memset( date, 0, sizeof( date ) );
512 memcpy( date, *p, ( len < (int) sizeof( date ) - 1 ) ?
513 len : (int) sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000514
Paul Bakker91200182010-02-18 21:26:15 +0000515 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
516 &time->year, &time->mon, &time->day,
517 &time->hour, &time->min, &time->sec ) < 5 )
518 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000519
Paul Bakker400ff6f2011-02-20 10:40:16 +0000520 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000521 time->year += 1900;
522
523 *p += len;
524
525 return( 0 );
526 }
527 else if ( tag == ASN1_GENERALIZED_TIME )
528 {
529 (*p)++;
530 ret = asn1_get_len( p, end, &len );
531
532 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000533 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000534
535 memset( date, 0, sizeof( date ) );
536 memcpy( date, *p, ( len < (int) sizeof( date ) - 1 ) ?
537 len : (int) sizeof( date ) - 1 );
538
539 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
540 &time->year, &time->mon, &time->day,
541 &time->hour, &time->min, &time->sec ) < 5 )
542 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
543
544 *p += len;
545
546 return( 0 );
547 }
548 else
Paul Bakker9d781402011-05-09 16:17:09 +0000549 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000550}
551
552
553/*
554 * Validity ::= SEQUENCE {
555 * notBefore Time,
556 * notAfter Time }
557 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000558static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000559 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 x509_time *from,
561 x509_time *to )
562{
Paul Bakker23986e52011-04-24 08:57:21 +0000563 int ret;
564 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 if( ( ret = asn1_get_tag( p, end, &len,
567 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000568 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 end = *p + len;
571
Paul Bakker91200182010-02-18 21:26:15 +0000572 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000573 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
Paul Bakker91200182010-02-18 21:26:15 +0000575 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000579 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000580 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 return( 0 );
583}
584
585/*
586 * SubjectPublicKeyInfo ::= SEQUENCE {
587 * algorithm AlgorithmIdentifier,
588 * subjectPublicKey BIT STRING }
589 */
590static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000591 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 x509_buf *pk_alg_oid,
593 mpi *N, mpi *E )
594{
Paul Bakker23986e52011-04-24 08:57:21 +0000595 int ret, can_handle;
596 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 unsigned char *end2;
598
599 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
600 return( ret );
601
602 /*
603 * only RSA public keys handled at this time
604 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000605 can_handle = 0;
606
607 if( pk_alg_oid->len == 9 &&
608 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
609 can_handle = 1;
610
611 if( pk_alg_oid->len == 9 &&
612 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
613 {
614 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
615 can_handle = 1;
616
617 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
618 can_handle = 1;
619 }
620
621 if( pk_alg_oid->len == 5 &&
622 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
623 can_handle = 1;
624
625 if( can_handle == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000626 return( POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000629 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000632 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000633 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635 end2 = *p + len;
636
637 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000638 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 /*
641 * RSAPublicKey ::= SEQUENCE {
642 * modulus INTEGER, -- n
643 * publicExponent INTEGER -- e
644 * }
645 */
646 if( ( ret = asn1_get_tag( p, end2, &len,
647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000648 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000651 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000652 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
655 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000656 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000659 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000660 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662 return( 0 );
663}
664
665static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000666 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 x509_buf *sig )
668{
Paul Bakker23986e52011-04-24 08:57:21 +0000669 int ret;
670 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 sig->tag = **p;
673
674 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000675 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker74111d32011-01-15 16:57:55 +0000677
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000679 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
681 sig->len = len;
682 sig->p = *p;
683
684 *p += len;
685
686 return( 0 );
687}
688
689/*
690 * X.509 v2/v3 unique identifier (not parsed)
691 */
692static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000693 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 x509_buf *uid, int n )
695{
696 int ret;
697
698 if( *p == end )
699 return( 0 );
700
701 uid->tag = **p;
702
703 if( ( ret = asn1_get_tag( p, end, &uid->len,
704 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
705 {
Paul Bakker40e46942009-01-03 21:51:57 +0000706 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 return( 0 );
708
709 return( ret );
710 }
711
712 uid->p = *p;
713 *p += uid->len;
714
715 return( 0 );
716}
717
718/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000719 * X.509 Extensions (No parsing of extensions, pointer should
720 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 */
722static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000723 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000724 x509_buf *ext )
Paul Bakker5121ce52009-01-03 21:22:43 +0000725{
Paul Bakker23986e52011-04-24 08:57:21 +0000726 int ret;
727 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729 if( *p == end )
730 return( 0 );
731
732 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000733
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 if( ( ret = asn1_get_tag( p, end, &ext->len,
735 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
738 ext->p = *p;
739 end = *p + ext->len;
740
741 /*
742 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
743 *
744 * Extension ::= SEQUENCE {
745 * extnID OBJECT IDENTIFIER,
746 * critical BOOLEAN DEFAULT FALSE,
747 * extnValue OCTET STRING }
748 */
749 if( ( ret = asn1_get_tag( p, end, &len,
750 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000751 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000754 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000755 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Paul Bakkerd98030e2009-05-02 15:13:40 +0000757 return( 0 );
758}
759
760/*
761 * X.509 CRL v2 extensions (no extensions parsed yet.)
762 */
763static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000764 const unsigned char *end,
765 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000766{
Paul Bakker23986e52011-04-24 08:57:21 +0000767 int ret;
768 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000769
770 if( ( ret = x509_get_ext( p, end, ext ) ) != 0 )
771 {
772 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
773 return( 0 );
774
775 return( ret );
776 }
777
778 while( *p < end )
779 {
780 if( ( ret = asn1_get_tag( p, end, &len,
781 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000782 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000783
784 *p += len;
785 }
786
787 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000788 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000789 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
790
791 return( 0 );
792}
793
Paul Bakker74111d32011-01-15 16:57:55 +0000794static int x509_get_basic_constraints( unsigned char **p,
795 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000796 int *ca_istrue,
797 int *max_pathlen )
798{
Paul Bakker23986e52011-04-24 08:57:21 +0000799 int ret;
800 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000801
802 /*
803 * BasicConstraints ::= SEQUENCE {
804 * cA BOOLEAN DEFAULT FALSE,
805 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
806 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000807 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000808 *max_pathlen = 0; /* endless */
809
810 if( ( ret = asn1_get_tag( p, end, &len,
811 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000812 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000813
814 if( *p == end )
815 return 0;
816
Paul Bakker3cccddb2011-01-16 21:46:31 +0000817 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000818 {
819 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000820 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000821
822 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000823 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000824
Paul Bakker3cccddb2011-01-16 21:46:31 +0000825 if( *ca_istrue != 0 )
826 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000827 }
828
829 if( *p == end )
830 return 0;
831
832 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000834
835 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000836 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000837 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
838
839 (*max_pathlen)++;
840
Paul Bakker74111d32011-01-15 16:57:55 +0000841 return 0;
842}
843
844static int x509_get_ns_cert_type( unsigned char **p,
845 const unsigned char *end,
846 unsigned char *ns_cert_type)
847{
848 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000849 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000850
851 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000852 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000853
854 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000856 POLARSSL_ERR_ASN1_INVALID_LENGTH );
857
858 /* Get actual bitstring */
859 *ns_cert_type = *bs.p;
860 return 0;
861}
862
863static int x509_get_key_usage( unsigned char **p,
864 const unsigned char *end,
865 unsigned char *key_usage)
866{
867 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000868 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000869
870 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000871 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000872
873 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000874 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000875 POLARSSL_ERR_ASN1_INVALID_LENGTH );
876
877 /* Get actual bitstring */
878 *key_usage = *bs.p;
879 return 0;
880}
881
Paul Bakkerd98030e2009-05-02 15:13:40 +0000882/*
Paul Bakker74111d32011-01-15 16:57:55 +0000883 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
884 *
885 * KeyPurposeId ::= OBJECT IDENTIFIER
886 */
887static int x509_get_ext_key_usage( unsigned char **p,
888 const unsigned char *end,
889 x509_sequence *ext_key_usage)
890{
891 int ret;
892
893 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000894 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000895
896 /* Sequence length must be >= 1 */
897 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000898 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000899 POLARSSL_ERR_ASN1_INVALID_LENGTH );
900
901 return 0;
902}
903
904/*
905 * X.509 v3 extensions
906 *
907 * TODO: Perform all of the basic constraints tests required by the RFC
908 * TODO: Set values for undetected extensions to a sane default?
909 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000910 */
911static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000912 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000913 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000914{
Paul Bakker23986e52011-04-24 08:57:21 +0000915 int ret;
916 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000917 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000918
Paul Bakker74111d32011-01-15 16:57:55 +0000919 if( ( ret = x509_get_ext( p, end, &crt->v3_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000920 {
921 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
922 return( 0 );
923
924 return( ret );
925 }
926
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 while( *p < end )
928 {
Paul Bakker74111d32011-01-15 16:57:55 +0000929 /*
930 * Extension ::= SEQUENCE {
931 * extnID OBJECT IDENTIFIER,
932 * critical BOOLEAN DEFAULT FALSE,
933 * extnValue OCTET STRING }
934 */
935 x509_buf extn_oid = {0, 0, NULL};
936 int is_critical = 0; /* DEFAULT FALSE */
937
Paul Bakker5121ce52009-01-03 21:22:43 +0000938 if( ( ret = asn1_get_tag( p, end, &len,
939 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000942 end_ext_data = *p + len;
943
Paul Bakker74111d32011-01-15 16:57:55 +0000944 /* Get extension ID */
945 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Paul Bakker74111d32011-01-15 16:57:55 +0000947 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000948 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
Paul Bakker74111d32011-01-15 16:57:55 +0000950 extn_oid.p = *p;
951 *p += extn_oid.len;
952
953 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000954 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000955 POLARSSL_ERR_ASN1_OUT_OF_DATA );
956
957 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000958 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000959 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000960 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Paul Bakker74111d32011-01-15 16:57:55 +0000962 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000963 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000965 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000967 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000968
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000969 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000970 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000971 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Paul Bakker74111d32011-01-15 16:57:55 +0000973 /*
974 * Detect supported extensions
975 */
976 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
977 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 {
Paul Bakker74111d32011-01-15 16:57:55 +0000979 /* Parse basic constraints */
980 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000981 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000982 return ( ret );
983 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 }
Paul Bakker74111d32011-01-15 16:57:55 +0000985 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
986 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
987 {
988 /* Parse netscape certificate type */
989 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
990 &crt->ns_cert_type ) ) != 0 )
991 return ( ret );
992 crt->ext_types |= EXT_NS_CERT_TYPE;
993 }
994 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
995 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
996 {
997 /* Parse key usage */
998 if( ( ret = x509_get_key_usage( p, end_ext_octet,
999 &crt->key_usage ) ) != 0 )
1000 return ( ret );
1001 crt->ext_types |= EXT_KEY_USAGE;
1002 }
1003 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1004 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1005 {
1006 /* Parse extended key usage */
1007 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1008 &crt->ext_key_usage ) ) != 0 )
1009 return ( ret );
1010 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1011 }
1012 else
1013 {
1014 /* No parser found, skip extension */
1015 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Paul Bakker74111d32011-01-15 16:57:55 +00001017 if( is_critical )
1018 {
1019 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001020 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001021 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1022 }
1023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001024 }
1025
1026 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001027 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001028 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 return( 0 );
1031}
1032
1033/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001034 * X.509 CRL Entries
1035 */
1036static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001037 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001038 x509_crl_entry *entry )
1039{
Paul Bakker23986e52011-04-24 08:57:21 +00001040 int ret;
1041 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001042 x509_crl_entry *cur_entry = entry;
1043
1044 if( *p == end )
1045 return( 0 );
1046
Paul Bakker9be19372009-07-27 20:21:53 +00001047 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001048 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1049 {
1050 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1051 return( 0 );
1052
1053 return( ret );
1054 }
1055
Paul Bakker9be19372009-07-27 20:21:53 +00001056 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001057
1058 while( *p < end )
1059 {
Paul Bakker23986e52011-04-24 08:57:21 +00001060 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001061
1062 if( ( ret = asn1_get_tag( p, end, &len2,
1063 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1064 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065 return( ret );
1066 }
1067
Paul Bakker9be19372009-07-27 20:21:53 +00001068 cur_entry->raw.tag = **p;
1069 cur_entry->raw.p = *p;
1070 cur_entry->raw.len = len2;
1071
Paul Bakkerd98030e2009-05-02 15:13:40 +00001072 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1073 return( ret );
1074
Paul Bakker91200182010-02-18 21:26:15 +00001075 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001076 return( ret );
1077
1078 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1079 return( ret );
1080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 if ( *p < end )
1082 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001083 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1084 cur_entry = cur_entry->next;
1085 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1086 }
1087 }
1088
1089 return( 0 );
1090}
1091
Paul Bakker27d66162010-03-17 06:56:01 +00001092static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1093{
1094 if( sig_oid->len == 9 &&
1095 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1096 {
1097 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1098 {
1099 *sig_alg = sig_oid->p[8];
1100 return( 0 );
1101 }
1102
1103 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1104 {
1105 *sig_alg = sig_oid->p[8];
1106 return( 0 );
1107 }
1108
1109 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1110 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001111 if( sig_oid->len == 5 &&
1112 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1113 {
1114 *sig_alg = SIG_RSA_SHA1;
1115 return( 0 );
1116 }
Paul Bakker27d66162010-03-17 06:56:01 +00001117
1118 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1119}
1120
Paul Bakkerd98030e2009-05-02 15:13:40 +00001121/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 * Parse one or more certificates and add them to the chained list
1123 */
Paul Bakker23986e52011-04-24 08:57:21 +00001124int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001125{
Paul Bakker23986e52011-04-24 08:57:21 +00001126 int ret;
1127 size_t len, use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 unsigned char *p, *end;
1129 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001130#if defined(POLARSSL_PEM_C)
1131 pem_context pem;
1132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 crt = chain;
1135
Paul Bakker320a4b52009-03-28 18:52:39 +00001136 /*
1137 * Check for valid input
1138 */
1139 if( crt == NULL || buf == NULL )
1140 return( 1 );
1141
Paul Bakkere9581d62009-03-28 20:29:25 +00001142 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 crt = crt->next;
1144
1145 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001146 * Add new certificate on the end of the chain if needed.
1147 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001148 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001149 {
1150 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1151
Paul Bakker7d06ad22009-05-02 15:53:56 +00001152 if( crt->next == NULL )
1153 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001154 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001155 return( 1 );
1156 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001157
Paul Bakker7d06ad22009-05-02 15:53:56 +00001158 crt = crt->next;
1159 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001160 }
1161
Paul Bakker96743fc2011-02-12 14:30:57 +00001162#if defined(POLARSSL_PEM_C)
1163 pem_init( &pem );
1164 ret = pem_read_buffer( &pem,
1165 "-----BEGIN CERTIFICATE-----",
1166 "-----END CERTIFICATE-----",
1167 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Paul Bakker96743fc2011-02-12 14:30:57 +00001169 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001171 /*
1172 * Was PEM encoded
1173 */
1174 buflen -= use_len;
1175 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
1177 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001178 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001180 p = pem.buf;
1181 pem.buf = NULL;
1182 len = pem.buflen;
1183 pem_free( &pem );
1184 }
1185 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1186 {
1187 pem_free( &pem );
1188 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190 else
1191 {
1192 /*
1193 * nope, copy the raw DER data
1194 */
1195 p = (unsigned char *) malloc( len = buflen );
1196
1197 if( p == NULL )
1198 return( 1 );
1199
1200 memcpy( p, buf, buflen );
1201
1202 buflen = 0;
1203 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001204#else
1205 p = (unsigned char *) malloc( len = buflen );
1206
1207 if( p == NULL )
1208 return( 1 );
1209
1210 memcpy( p, buf, buflen );
1211
1212 buflen = 0;
1213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 crt->raw.p = p;
1216 crt->raw.len = len;
1217 end = p + len;
1218
1219 /*
1220 * Certificate ::= SEQUENCE {
1221 * tbsCertificate TBSCertificate,
1222 * signatureAlgorithm AlgorithmIdentifier,
1223 * signatureValue BIT STRING }
1224 */
1225 if( ( ret = asn1_get_tag( &p, end, &len,
1226 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1227 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001228 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001229 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
Paul Bakker23986e52011-04-24 08:57:21 +00001232 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001234 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001235 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001236 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
1238
1239 /*
1240 * TBSCertificate ::= SEQUENCE {
1241 */
1242 crt->tbs.p = p;
1243
1244 if( ( ret = asn1_get_tag( &p, end, &len,
1245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1246 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001248 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 }
1250
1251 end = p + len;
1252 crt->tbs.len = end - crt->tbs.p;
1253
1254 /*
1255 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1256 *
1257 * CertificateSerialNumber ::= INTEGER
1258 *
1259 * signature AlgorithmIdentifier
1260 */
1261 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1262 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1263 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 crt->version++;
1270
1271 if( crt->version > 3 )
1272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001274 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
Paul Bakker27d66162010-03-17 06:56:01 +00001277 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001279 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001280 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
1283 /*
1284 * issuer Name
1285 */
1286 crt->issuer_raw.p = p;
1287
1288 if( ( ret = asn1_get_tag( &p, end, &len,
1289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
1295 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->issuer_raw.len = p - crt->issuer_raw.p;
1302
1303 /*
1304 * Validity ::= SEQUENCE {
1305 * notBefore Time,
1306 * notAfter Time }
1307 *
1308 */
1309 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1310 &crt->valid_to ) ) != 0 )
1311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 return( ret );
1314 }
1315
1316 /*
1317 * subject Name
1318 */
1319 crt->subject_raw.p = p;
1320
1321 if( ( ret = asn1_get_tag( &p, end, &len,
1322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
1328 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 crt->subject_raw.len = p - crt->subject_raw.p;
1335
1336 /*
1337 * SubjectPublicKeyInfo ::= SEQUENCE
1338 * algorithm AlgorithmIdentifier,
1339 * subjectPublicKey BIT STRING }
1340 */
1341 if( ( ret = asn1_get_tag( &p, end, &len,
1342 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001345 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1349 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1350 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001351 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 return( ret );
1353 }
1354
1355 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 return( ret );
1359 }
1360
1361 crt->rsa.len = mpi_size( &crt->rsa.N );
1362
1363 /*
1364 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1365 * -- If present, version shall be v2 or v3
1366 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1367 * -- If present, version shall be v2 or v3
1368 * extensions [3] EXPLICIT Extensions OPTIONAL
1369 * -- If present, version shall be v3
1370 */
1371 if( crt->version == 2 || crt->version == 3 )
1372 {
1373 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1374 if( ret != 0 )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 return( ret );
1378 }
1379 }
1380
1381 if( crt->version == 2 || crt->version == 3 )
1382 {
1383 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1384 if( ret != 0 )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 return( ret );
1388 }
1389 }
1390
1391 if( crt->version == 3 )
1392 {
Paul Bakker74111d32011-01-15 16:57:55 +00001393 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 if( ret != 0 )
1395 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001396 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 return( ret );
1398 }
1399 }
1400
1401 if( p != end )
1402 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001403 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001404 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001405 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
1408 end = crt->raw.p + crt->raw.len;
1409
1410 /*
1411 * signatureAlgorithm AlgorithmIdentifier,
1412 * signatureValue BIT STRING
1413 */
1414 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1415 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 return( ret );
1418 }
1419
Paul Bakker320a4b52009-03-28 18:52:39 +00001420 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001423 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
1425
1426 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1427 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001428 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 return( ret );
1430 }
1431
1432 if( p != end )
1433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001435 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001436 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
1438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001440 {
1441 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1442
Paul Bakker7d06ad22009-05-02 15:53:56 +00001443 if( crt->next == NULL )
1444 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001445 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 return( 1 );
1447 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001448
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 crt = crt->next;
1450 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001451
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
1455 return( 0 );
1456}
1457
1458/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001459 * Parse one or more CRLs and add them to the chained list
1460 */
Paul Bakker23986e52011-04-24 08:57:21 +00001461int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001462{
Paul Bakker23986e52011-04-24 08:57:21 +00001463 int ret;
1464 size_t len, use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001465 unsigned char *p, *end;
1466 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001467#if defined(POLARSSL_PEM_C)
1468 pem_context pem;
1469#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001470
1471 crl = chain;
1472
1473 /*
1474 * Check for valid input
1475 */
1476 if( crl == NULL || buf == NULL )
1477 return( 1 );
1478
1479 while( crl->version != 0 && crl->next != NULL )
1480 crl = crl->next;
1481
1482 /*
1483 * Add new CRL on the end of the chain if needed.
1484 */
1485 if ( crl->version != 0 && crl->next == NULL)
1486 {
1487 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1488
Paul Bakker7d06ad22009-05-02 15:53:56 +00001489 if( crl->next == NULL )
1490 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001491 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001492 return( 1 );
1493 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001494
Paul Bakker7d06ad22009-05-02 15:53:56 +00001495 crl = crl->next;
1496 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001497 }
1498
Paul Bakker96743fc2011-02-12 14:30:57 +00001499#if defined(POLARSSL_PEM_C)
1500 pem_init( &pem );
1501 ret = pem_read_buffer( &pem,
1502 "-----BEGIN X509 CRL-----",
1503 "-----END X509 CRL-----",
1504 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001505
Paul Bakker96743fc2011-02-12 14:30:57 +00001506 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001507 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001508 /*
1509 * Was PEM encoded
1510 */
1511 buflen -= use_len;
1512 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001513
1514 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001515 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001517 p = pem.buf;
1518 pem.buf = NULL;
1519 len = pem.buflen;
1520 pem_free( &pem );
1521 }
1522 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1523 {
1524 pem_free( &pem );
1525 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001526 }
1527 else
1528 {
1529 /*
1530 * nope, copy the raw DER data
1531 */
1532 p = (unsigned char *) malloc( len = buflen );
1533
1534 if( p == NULL )
1535 return( 1 );
1536
1537 memcpy( p, buf, buflen );
1538
1539 buflen = 0;
1540 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001541#else
1542 p = (unsigned char *) malloc( len = buflen );
1543
1544 if( p == NULL )
1545 return( 1 );
1546
1547 memcpy( p, buf, buflen );
1548
1549 buflen = 0;
1550#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001551
1552 crl->raw.p = p;
1553 crl->raw.len = len;
1554 end = p + len;
1555
1556 /*
1557 * CertificateList ::= SEQUENCE {
1558 * tbsCertList TBSCertList,
1559 * signatureAlgorithm AlgorithmIdentifier,
1560 * signatureValue BIT STRING }
1561 */
1562 if( ( ret = asn1_get_tag( &p, end, &len,
1563 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1564 {
1565 x509_crl_free( crl );
1566 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1567 }
1568
Paul Bakker23986e52011-04-24 08:57:21 +00001569 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001570 {
1571 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001572 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001573 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1574 }
1575
1576 /*
1577 * TBSCertList ::= SEQUENCE {
1578 */
1579 crl->tbs.p = p;
1580
1581 if( ( ret = asn1_get_tag( &p, end, &len,
1582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1583 {
1584 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001585 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001586 }
1587
1588 end = p + len;
1589 crl->tbs.len = end - crl->tbs.p;
1590
1591 /*
1592 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1593 * -- if present, MUST be v2
1594 *
1595 * signature AlgorithmIdentifier
1596 */
1597 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1598 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1599 {
1600 x509_crl_free( crl );
1601 return( ret );
1602 }
1603
1604 crl->version++;
1605
1606 if( crl->version > 2 )
1607 {
1608 x509_crl_free( crl );
1609 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1610 }
1611
Paul Bakker27d66162010-03-17 06:56:01 +00001612 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613 {
1614 x509_crl_free( crl );
1615 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1616 }
1617
1618 /*
1619 * issuer Name
1620 */
1621 crl->issuer_raw.p = p;
1622
1623 if( ( ret = asn1_get_tag( &p, end, &len,
1624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1625 {
1626 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001627 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001628 }
1629
1630 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1631 {
1632 x509_crl_free( crl );
1633 return( ret );
1634 }
1635
1636 crl->issuer_raw.len = p - crl->issuer_raw.p;
1637
1638 /*
1639 * thisUpdate Time
1640 * nextUpdate Time OPTIONAL
1641 */
Paul Bakker91200182010-02-18 21:26:15 +00001642 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 {
1644 x509_crl_free( crl );
1645 return( ret );
1646 }
1647
Paul Bakker91200182010-02-18 21:26:15 +00001648 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 {
Paul Bakker9d781402011-05-09 16:17:09 +00001650 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001651 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001652 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001653 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001654 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001655 x509_crl_free( crl );
1656 return( ret );
1657 }
1658 }
1659
1660 /*
1661 * revokedCertificates SEQUENCE OF SEQUENCE {
1662 * userCertificate CertificateSerialNumber,
1663 * revocationDate Time,
1664 * crlEntryExtensions Extensions OPTIONAL
1665 * -- if present, MUST be v2
1666 * } OPTIONAL
1667 */
1668 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1669 {
1670 x509_crl_free( crl );
1671 return( ret );
1672 }
1673
1674 /*
1675 * crlExtensions EXPLICIT Extensions OPTIONAL
1676 * -- if present, MUST be v2
1677 */
1678 if( crl->version == 2 )
1679 {
1680 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1681
1682 if( ret != 0 )
1683 {
1684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687 }
1688
1689 if( p != end )
1690 {
1691 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001692 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1694 }
1695
1696 end = crl->raw.p + crl->raw.len;
1697
1698 /*
1699 * signatureAlgorithm AlgorithmIdentifier,
1700 * signatureValue BIT STRING
1701 */
1702 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1703 {
1704 x509_crl_free( crl );
1705 return( ret );
1706 }
1707
1708 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1709 {
1710 x509_crl_free( crl );
1711 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1712 }
1713
1714 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1715 {
1716 x509_crl_free( crl );
1717 return( ret );
1718 }
1719
1720 if( p != end )
1721 {
1722 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001723 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1725 }
1726
1727 if( buflen > 0 )
1728 {
1729 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1730
Paul Bakker7d06ad22009-05-02 15:53:56 +00001731 if( crl->next == NULL )
1732 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001734 return( 1 );
1735 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001736
Paul Bakker7d06ad22009-05-02 15:53:56 +00001737 crl = crl->next;
1738 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739
1740 return( x509parse_crl( crl, buf, buflen ) );
1741 }
1742
1743 return( 0 );
1744}
1745
Paul Bakker335db3f2011-04-25 15:28:35 +00001746#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001747/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001748 * Load all data from a file into a given buffer.
1749 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001750int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001751{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001753
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754 if( ( f = fopen( path, "rb" ) ) == NULL )
1755 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001756
Paul Bakkerd98030e2009-05-02 15:13:40 +00001757 fseek( f, 0, SEEK_END );
1758 *n = (size_t) ftell( f );
1759 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001760
Paul Bakkerd98030e2009-05-02 15:13:40 +00001761 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1762 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001763
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 if( fread( *buf, 1, *n, f ) != *n )
1765 {
1766 fclose( f );
1767 free( *buf );
1768 return( 1 );
1769 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001770
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001772
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001774
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001776}
1777
1778/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 * Load one or more certificates and add them to the chained list
1780 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001781int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001782{
1783 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 size_t n;
1785 unsigned char *buf;
1786
Paul Bakker2b245eb2009-04-19 18:44:26 +00001787 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 return( 1 );
1789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 ret = x509parse_crt( chain, buf, (int) n );
1791
1792 memset( buf, 0, n + 1 );
1793 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
1795 return( ret );
1796}
1797
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798/*
1799 * Load one or more CRLs and add them to the chained list
1800 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001801int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802{
1803 int ret;
1804 size_t n;
1805 unsigned char *buf;
1806
1807 if ( load_file( path, &buf, &n ) )
1808 return( 1 );
1809
1810 ret = x509parse_crl( chain, buf, (int) n );
1811
1812 memset( buf, 0, n + 1 );
1813 free( buf );
1814
1815 return( ret );
1816}
1817
Paul Bakker5121ce52009-01-03 21:22:43 +00001818/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001819 * Load and parse a private RSA key
1820 */
1821int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1822{
1823 int ret;
1824 size_t n;
1825 unsigned char *buf;
1826
1827 if ( load_file( path, &buf, &n ) )
1828 return( 1 );
1829
1830 if( pwd == NULL )
1831 ret = x509parse_key( rsa, buf, (int) n, NULL, 0 );
1832 else
1833 ret = x509parse_key( rsa, buf, (int) n,
1834 (unsigned char *) pwd, strlen( pwd ) );
1835
1836 memset( buf, 0, n + 1 );
1837 free( buf );
1838
1839 return( ret );
1840}
1841
1842/*
1843 * Load and parse a public RSA key
1844 */
1845int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1846{
1847 int ret;
1848 size_t n;
1849 unsigned char *buf;
1850
1851 if ( load_file( path, &buf, &n ) )
1852 return( 1 );
1853
1854 ret = x509parse_public_key( rsa, buf, (int) n );
1855
1856 memset( buf, 0, n + 1 );
1857 free( buf );
1858
1859 return( ret );
1860}
1861#endif /* POLARSSL_FS_IO */
1862
1863/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001864 * Parse a private RSA key
1865 */
Paul Bakker23986e52011-04-24 08:57:21 +00001866int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1867 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001868{
Paul Bakker23986e52011-04-24 08:57:21 +00001869 int ret;
1870 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00001872#if defined(POLARSSL_PEM_C)
1873 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
Paul Bakker96743fc2011-02-12 14:30:57 +00001875 pem_init( &pem );
1876 ret = pem_read_buffer( &pem,
1877 "-----BEGIN RSA PRIVATE KEY-----",
1878 "-----END RSA PRIVATE KEY-----",
1879 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Paul Bakker96743fc2011-02-12 14:30:57 +00001881 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001883 /*
1884 * Was PEM encoded
1885 */
1886 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001888 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001889 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001890 pem_free( &pem );
1891 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001892 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Paul Bakker96743fc2011-02-12 14:30:57 +00001894 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1895#else
1896 p = (unsigned char *) key;
1897#endif
1898 end = p + keylen;
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 /*
1901 * RSAPrivateKey ::= SEQUENCE {
1902 * version Version,
1903 * modulus INTEGER, -- n
1904 * publicExponent INTEGER, -- e
1905 * privateExponent INTEGER, -- d
1906 * prime1 INTEGER, -- p
1907 * prime2 INTEGER, -- q
1908 * exponent1 INTEGER, -- d mod (p-1)
1909 * exponent2 INTEGER, -- d mod (q-1)
1910 * coefficient INTEGER, -- (inverse of q) mod p
1911 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1912 * }
1913 */
1914 if( ( ret = asn1_get_tag( &p, end, &len,
1915 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1916 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001917#if defined(POLARSSL_PEM_C)
1918 pem_free( &pem );
1919#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001921 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 }
1923
1924 end = p + len;
1925
1926 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1927 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001928#if defined(POLARSSL_PEM_C)
1929 pem_free( &pem );
1930#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001932 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 }
1934
1935 if( rsa->ver != 0 )
1936 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001937#if defined(POLARSSL_PEM_C)
1938 pem_free( &pem );
1939#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001941 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 }
1943
1944 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
1945 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
1946 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
1947 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
1948 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
1949 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
1950 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
1951 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
1952 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001953#if defined(POLARSSL_PEM_C)
1954 pem_free( &pem );
1955#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001957 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 }
1959
1960 rsa->len = mpi_size( &rsa->N );
1961
1962 if( p != end )
1963 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001964#if defined(POLARSSL_PEM_C)
1965 pem_free( &pem );
1966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001968 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001969 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 }
1971
1972 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
1973 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001974#if defined(POLARSSL_PEM_C)
1975 pem_free( &pem );
1976#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 rsa_free( rsa );
1978 return( ret );
1979 }
1980
Paul Bakker96743fc2011-02-12 14:30:57 +00001981#if defined(POLARSSL_PEM_C)
1982 pem_free( &pem );
1983#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
1985 return( 0 );
1986}
1987
1988/*
Paul Bakker53019ae2011-03-25 13:58:48 +00001989 * Parse a public RSA key
1990 */
Paul Bakker23986e52011-04-24 08:57:21 +00001991int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00001992{
Paul Bakker23986e52011-04-24 08:57:21 +00001993 int ret;
1994 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00001995 unsigned char *p, *end;
1996 x509_buf alg_oid;
1997#if defined(POLARSSL_PEM_C)
1998 pem_context pem;
1999
2000 pem_init( &pem );
2001 ret = pem_read_buffer( &pem,
2002 "-----BEGIN PUBLIC KEY-----",
2003 "-----END PUBLIC KEY-----",
2004 key, NULL, 0, &len );
2005
2006 if( ret == 0 )
2007 {
2008 /*
2009 * Was PEM encoded
2010 */
2011 keylen = pem.buflen;
2012 }
2013 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2014 {
2015 pem_free( &pem );
2016 return( ret );
2017 }
2018
2019 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2020#else
2021 p = (unsigned char *) key;
2022#endif
2023 end = p + keylen;
2024
2025 /*
2026 * PublicKeyInfo ::= SEQUENCE {
2027 * algorithm AlgorithmIdentifier,
2028 * PublicKey BIT STRING
2029 * }
2030 *
2031 * AlgorithmIdentifier ::= SEQUENCE {
2032 * algorithm OBJECT IDENTIFIER,
2033 * parameters ANY DEFINED BY algorithm OPTIONAL
2034 * }
2035 *
2036 * RSAPublicKey ::= SEQUENCE {
2037 * modulus INTEGER, -- n
2038 * publicExponent INTEGER -- e
2039 * }
2040 */
2041
2042 if( ( ret = asn1_get_tag( &p, end, &len,
2043 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2044 {
2045#if defined(POLARSSL_PEM_C)
2046 pem_free( &pem );
2047#endif
2048 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002049 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002050 }
2051
2052 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2053 {
2054#if defined(POLARSSL_PEM_C)
2055 pem_free( &pem );
2056#endif
2057 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002058 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002059 }
2060
2061 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2062 {
2063#if defined(POLARSSL_PEM_C)
2064 pem_free( &pem );
2065#endif
2066 rsa_free( rsa );
2067 return( ret );
2068 }
2069
2070 rsa->len = mpi_size( &rsa->N );
2071
2072#if defined(POLARSSL_PEM_C)
2073 pem_free( &pem );
2074#endif
2075
2076 return( 0 );
2077}
2078
Paul Bakkereaa89f82011-04-04 21:36:15 +00002079#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002080/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002081 * Parse DHM parameters
2082 */
Paul Bakker23986e52011-04-24 08:57:21 +00002083int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002084{
Paul Bakker23986e52011-04-24 08:57:21 +00002085 int ret;
2086 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002087 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002088#if defined(POLARSSL_PEM_C)
2089 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002090
Paul Bakker96743fc2011-02-12 14:30:57 +00002091 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002092
Paul Bakker96743fc2011-02-12 14:30:57 +00002093 ret = pem_read_buffer( &pem,
2094 "-----BEGIN DH PARAMETERS-----",
2095 "-----END DH PARAMETERS-----",
2096 dhmin, NULL, 0, &dhminlen );
2097
2098 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002099 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002100 /*
2101 * Was PEM encoded
2102 */
2103 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002104 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002105 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002106 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002107 pem_free( &pem );
2108 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002109 }
2110
Paul Bakker96743fc2011-02-12 14:30:57 +00002111 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2112#else
2113 p = (unsigned char *) dhmin;
2114#endif
2115 end = p + dhminlen;
2116
Paul Bakker1b57b062011-01-06 15:48:19 +00002117 memset( dhm, 0, sizeof( dhm_context ) );
2118
Paul Bakker1b57b062011-01-06 15:48:19 +00002119 /*
2120 * DHParams ::= SEQUENCE {
2121 * prime INTEGER, -- P
2122 * generator INTEGER, -- g
2123 * }
2124 */
2125 if( ( ret = asn1_get_tag( &p, end, &len,
2126 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2127 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002128#if defined(POLARSSL_PEM_C)
2129 pem_free( &pem );
2130#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002131 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002132 }
2133
2134 end = p + len;
2135
2136 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2137 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2138 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002139#if defined(POLARSSL_PEM_C)
2140 pem_free( &pem );
2141#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002142 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002143 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002144 }
2145
2146 if( p != end )
2147 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002148#if defined(POLARSSL_PEM_C)
2149 pem_free( &pem );
2150#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002151 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002152 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002153 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2154 }
2155
Paul Bakker96743fc2011-02-12 14:30:57 +00002156#if defined(POLARSSL_PEM_C)
2157 pem_free( &pem );
2158#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002159
2160 return( 0 );
2161}
2162
Paul Bakker335db3f2011-04-25 15:28:35 +00002163#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002164/*
2165 * Load and parse a private RSA key
2166 */
2167int x509parse_dhmfile( dhm_context *dhm, const char *path )
2168{
2169 int ret;
2170 size_t n;
2171 unsigned char *buf;
2172
2173 if ( load_file( path, &buf, &n ) )
2174 return( 1 );
2175
2176 ret = x509parse_dhm( dhm, buf, (int) n);
2177
2178 memset( buf, 0, n + 1 );
2179 free( buf );
2180
2181 return( ret );
2182}
Paul Bakker335db3f2011-04-25 15:28:35 +00002183#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002184#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002185
Paul Bakker5121ce52009-01-03 21:22:43 +00002186#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002187#include <stdarg.h>
2188
2189#if !defined vsnprintf
2190#define vsnprintf _vsnprintf
2191#endif // vsnprintf
2192
2193/*
2194 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2195 * Result value is not size of buffer needed, but -1 if no fit is possible.
2196 *
2197 * This fuction tries to 'fix' this by at least suggesting enlarging the
2198 * size by 20.
2199 */
2200int compat_snprintf(char *str, size_t size, const char *format, ...)
2201{
2202 va_list ap;
2203 int res = -1;
2204
2205 va_start( ap, format );
2206
2207 res = vsnprintf( str, size, format, ap );
2208
2209 va_end( ap );
2210
2211 // No quick fix possible
2212 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002213 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002214
2215 return res;
2216}
2217
2218#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002219#endif
2220
Paul Bakkerd98030e2009-05-02 15:13:40 +00002221#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2222
2223#define SAFE_SNPRINTF() \
2224{ \
2225 if( ret == -1 ) \
2226 return( -1 ); \
2227 \
Paul Bakker23986e52011-04-24 08:57:21 +00002228 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002229 p[n - 1] = '\0'; \
2230 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2231 } \
2232 \
Paul Bakker23986e52011-04-24 08:57:21 +00002233 n -= (unsigned int) ret; \
2234 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002235}
2236
Paul Bakker5121ce52009-01-03 21:22:43 +00002237/*
2238 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002239 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002241int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002242{
Paul Bakker23986e52011-04-24 08:57:21 +00002243 int ret;
2244 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002246 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 char s[128], *p;
2248
2249 memset( s, 0, sizeof( s ) );
2250
2251 name = dn;
2252 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002253 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
2255 while( name != NULL )
2256 {
Paul Bakker74111d32011-01-15 16:57:55 +00002257 if( name != dn )
2258 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002259 ret = snprintf( p, n, ", " );
2260 SAFE_SNPRINTF();
2261 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
2263 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2264 {
2265 switch( name->oid.p[2] )
2266 {
2267 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002268 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
2270 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002271 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002274 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
2276 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002277 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002280 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
2282 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002283 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
2285 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002286 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 name->oid.p[2] );
2288 break;
2289 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002290 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2293 {
2294 switch( name->oid.p[8] )
2295 {
2296 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002297 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002300 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 name->oid.p[8] );
2302 break;
2303 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002304 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306 else
Paul Bakker74111d32011-01-15 16:57:55 +00002307 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002308 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002309 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002311
2312 for( i = 0; i < name->val.len; i++ )
2313 {
2314 if( i >= (int) sizeof( s ) - 1 )
2315 break;
2316
2317 c = name->val.p[i];
2318 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2319 s[i] = '?';
2320 else s[i] = c;
2321 }
2322 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002323 ret = snprintf( p, n, "%s", s );
2324 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 name = name->next;
2326 }
2327
Paul Bakker23986e52011-04-24 08:57:21 +00002328 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329}
2330
2331/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002332 * Store the serial in printable form into buf; no more
2333 * than size characters will be written
2334 */
2335int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2336{
Paul Bakker23986e52011-04-24 08:57:21 +00002337 int ret;
2338 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002339 char *p;
2340
2341 p = buf;
2342 n = size;
2343
2344 nr = ( serial->len <= 32 )
2345 ? serial->len : 32;
2346
2347 for( i = 0; i < nr; i++ )
2348 {
2349 ret = snprintf( p, n, "%02X%s",
2350 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2351 SAFE_SNPRINTF();
2352 }
2353
Paul Bakker23986e52011-04-24 08:57:21 +00002354 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002355}
2356
2357/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002358 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002360int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2361 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002362{
Paul Bakker23986e52011-04-24 08:57:21 +00002363 int ret;
2364 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002365 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002368 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
Paul Bakkerd98030e2009-05-02 15:13:40 +00002370 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002372 SAFE_SNPRINTF();
2373 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002375 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
Paul Bakkerdd476992011-01-16 21:34:59 +00002377 ret = x509parse_serial_gets( p, n, &crt->serial);
2378 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Paul Bakkerd98030e2009-05-02 15:13:40 +00002380 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2381 SAFE_SNPRINTF();
2382 ret = x509parse_dn_gets( p, n, &crt->issuer );
2383 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002384
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2386 SAFE_SNPRINTF();
2387 ret = x509parse_dn_gets( p, n, &crt->subject );
2388 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Paul Bakkerd98030e2009-05-02 15:13:40 +00002390 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2392 crt->valid_from.year, crt->valid_from.mon,
2393 crt->valid_from.day, crt->valid_from.hour,
2394 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002395 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Paul Bakkerd98030e2009-05-02 15:13:40 +00002397 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2399 crt->valid_to.year, crt->valid_to.mon,
2400 crt->valid_to.day, crt->valid_to.hour,
2401 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002402 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Paul Bakkerd98030e2009-05-02 15:13:40 +00002404 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2405 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Paul Bakker27d66162010-03-17 06:56:01 +00002407 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002409 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2410 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2411 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2412 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2413 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2414 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2415 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2416 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2417 default: ret = snprintf( p, n, "???" ); break;
2418 }
2419 SAFE_SNPRINTF();
2420
2421 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002422 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002423 SAFE_SNPRINTF();
2424
Paul Bakker23986e52011-04-24 08:57:21 +00002425 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002426}
2427
Paul Bakker74111d32011-01-15 16:57:55 +00002428/* Compare a given OID string with an OID x509_buf * */
2429#define OID_CMP(oid_str, oid_buf) \
2430 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2431 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2432
2433/*
2434 * Return an informational string describing the given OID
2435 */
2436const char *x509_oid_get_description( x509_buf *oid )
2437{
2438 if ( oid == NULL )
2439 return ( NULL );
2440
2441 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2442 return( STRING_SERVER_AUTH );
2443
2444 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2445 return( STRING_CLIENT_AUTH );
2446
2447 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2448 return( STRING_CODE_SIGNING );
2449
2450 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2451 return( STRING_EMAIL_PROTECTION );
2452
2453 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2454 return( STRING_TIME_STAMPING );
2455
2456 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2457 return( STRING_OCSP_SIGNING );
2458
2459 return( NULL );
2460}
2461
2462/* Return the x.y.z.... style numeric string for the given OID */
2463int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2464{
Paul Bakker23986e52011-04-24 08:57:21 +00002465 int ret;
2466 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002467 unsigned int value;
2468 char *p;
2469
2470 p = buf;
2471 n = size;
2472
2473 /* First byte contains first two dots */
2474 if( oid->len > 0 )
2475 {
2476 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2477 SAFE_SNPRINTF();
2478 }
2479
2480 /* TODO: value can overflow in value. */
2481 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002482 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002483 {
2484 value <<= 7;
2485 value += oid->p[i] & 0x7F;
2486
2487 if( !( oid->p[i] & 0x80 ) )
2488 {
2489 /* Last byte */
2490 ret = snprintf( p, n, ".%d", value );
2491 SAFE_SNPRINTF();
2492 value = 0;
2493 }
2494 }
2495
Paul Bakker23986e52011-04-24 08:57:21 +00002496 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002497}
2498
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499/*
2500 * Return an informational string about the CRL.
2501 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002502int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2503 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002504{
Paul Bakker23986e52011-04-24 08:57:21 +00002505 int ret;
2506 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002508 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002509
2510 p = buf;
2511 n = size;
2512
2513 ret = snprintf( p, n, "%sCRL version : %d",
2514 prefix, crl->version );
2515 SAFE_SNPRINTF();
2516
2517 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2518 SAFE_SNPRINTF();
2519 ret = x509parse_dn_gets( p, n, &crl->issuer );
2520 SAFE_SNPRINTF();
2521
2522 ret = snprintf( p, n, "\n%sthis update : " \
2523 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2524 crl->this_update.year, crl->this_update.mon,
2525 crl->this_update.day, crl->this_update.hour,
2526 crl->this_update.min, crl->this_update.sec );
2527 SAFE_SNPRINTF();
2528
2529 ret = snprintf( p, n, "\n%snext update : " \
2530 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2531 crl->next_update.year, crl->next_update.mon,
2532 crl->next_update.day, crl->next_update.hour,
2533 crl->next_update.min, crl->next_update.sec );
2534 SAFE_SNPRINTF();
2535
2536 entry = &crl->entry;
2537
2538 ret = snprintf( p, n, "\n%sRevoked certificates:",
2539 prefix );
2540 SAFE_SNPRINTF();
2541
Paul Bakker9be19372009-07-27 20:21:53 +00002542 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002543 {
2544 ret = snprintf( p, n, "\n%sserial number: ",
2545 prefix );
2546 SAFE_SNPRINTF();
2547
2548 nr = ( entry->serial.len <= 32 )
2549 ? entry->serial.len : 32;
2550
Paul Bakker74111d32011-01-15 16:57:55 +00002551 for( i = 0; i < nr; i++ )
2552 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 ret = snprintf( p, n, "%02X%s",
2554 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2555 SAFE_SNPRINTF();
2556 }
2557
2558 ret = snprintf( p, n, " revocation date: " \
2559 "%04d-%02d-%02d %02d:%02d:%02d",
2560 entry->revocation_date.year, entry->revocation_date.mon,
2561 entry->revocation_date.day, entry->revocation_date.hour,
2562 entry->revocation_date.min, entry->revocation_date.sec );
2563 SAFE_SNPRINTF();
2564
2565 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Paul Bakkerd98030e2009-05-02 15:13:40 +00002568 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2569 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Paul Bakker27d66162010-03-17 06:56:01 +00002571 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002572 {
2573 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2574 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2575 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2576 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2577 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2578 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2579 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2580 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2581 default: ret = snprintf( p, n, "???" ); break;
2582 }
2583 SAFE_SNPRINTF();
2584
Paul Bakker1e27bb22009-07-19 20:25:25 +00002585 ret = snprintf( p, n, "\n" );
2586 SAFE_SNPRINTF();
2587
Paul Bakker23986e52011-04-24 08:57:21 +00002588 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589}
2590
2591/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002592 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002594int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595{
2596 struct tm *lt;
2597 time_t tt;
2598
2599 tt = time( NULL );
2600 lt = localtime( &tt );
2601
Paul Bakker40ea7de2009-05-03 10:18:48 +00002602 if( lt->tm_year > to->year - 1900 )
2603 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
Paul Bakker40ea7de2009-05-03 10:18:48 +00002605 if( lt->tm_year == to->year - 1900 &&
2606 lt->tm_mon > to->mon - 1 )
2607 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608
Paul Bakker40ea7de2009-05-03 10:18:48 +00002609 if( lt->tm_year == to->year - 1900 &&
2610 lt->tm_mon == to->mon - 1 &&
2611 lt->tm_mday > to->day )
2612 return( 1 );
2613
Paul Bakkerb6194992011-01-16 21:40:22 +00002614 if( lt->tm_year == to->year - 1900 &&
2615 lt->tm_mon == to->mon - 1 &&
2616 lt->tm_mday == to->day &&
2617 lt->tm_hour > to->hour - 1)
2618 return( 1 );
2619
2620 if( lt->tm_year == to->year - 1900 &&
2621 lt->tm_mon == to->mon - 1 &&
2622 lt->tm_mday == to->day &&
2623 lt->tm_hour == to->hour - 1 &&
2624 lt->tm_min > to->min - 1 )
2625 return( 1 );
2626
2627 if( lt->tm_year == to->year - 1900 &&
2628 lt->tm_mon == to->mon - 1 &&
2629 lt->tm_mday == to->day &&
2630 lt->tm_hour == to->hour - 1 &&
2631 lt->tm_min == to->min - 1 &&
2632 lt->tm_sec > to->sec - 1 )
2633 return( 1 );
2634
Paul Bakker40ea7de2009-05-03 10:18:48 +00002635 return( 0 );
2636}
2637
2638/*
2639 * Return 1 if the certificate is revoked, or 0 otherwise.
2640 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002641int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002642{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002643 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002644
2645 while( cur != NULL && cur->serial.len != 0 )
2646 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002647 if( crt->serial.len == cur->serial.len &&
2648 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002649 {
2650 if( x509parse_time_expired( &cur->revocation_date ) )
2651 return( 1 );
2652 }
2653
2654 cur = cur->next;
2655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
2657 return( 0 );
2658}
2659
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002660/*
2661 * Wrapper for x509 hashes.
2662 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002663 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002664 */
Paul Bakker23986e52011-04-24 08:57:21 +00002665static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 unsigned char *out )
2667{
2668 switch( alg )
2669 {
Paul Bakker40e46942009-01-03 21:51:57 +00002670#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002671 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002672#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002673#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002674 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002676#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002677 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002678#endif
2679#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002680 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002681#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002682#if defined(POLARSSL_SHA2_C)
2683 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2684 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2685#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002686#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002687 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2688 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002691 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 break;
2693 }
2694}
2695
2696/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002697 * Check that the given certificate is valid accoring to the CRL.
2698 */
2699static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2700 x509_crl *crl_list)
2701{
2702 int flags = 0;
2703 int hash_id;
2704 unsigned char hash[64];
2705
2706 /*
2707 * TODO: What happens if no CRL is present?
2708 * Suggestion: Revocation state should be unknown if no CRL is present.
2709 * For backwards compatibility this is not yet implemented.
2710 */
2711
2712 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2713 {
2714 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2715 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2716 crl_list->issuer_raw.len ) != 0 )
2717 {
2718 crl_list = crl_list->next;
2719 continue;
2720 }
2721
2722 /*
2723 * Check if CRL is correctly signed by the trusted CA
2724 */
2725 hash_id = crl_list->sig_alg;
2726
2727 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2728
2729 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2730 0, hash, crl_list->sig.p ) == 0 )
2731 {
2732 /*
2733 * CRL is not trusted
2734 */
2735 flags |= BADCRL_NOT_TRUSTED;
2736 break;
2737 }
2738
2739 /*
2740 * Check for validity of CRL (Do not drop out)
2741 */
2742 if( x509parse_time_expired( &crl_list->next_update ) )
2743 flags |= BADCRL_EXPIRED;
2744
2745 /*
2746 * Check if certificate is revoked
2747 */
2748 if( x509parse_revoked(crt, crl_list) )
2749 {
2750 flags |= BADCERT_REVOKED;
2751 break;
2752 }
2753
2754 crl_list = crl_list->next;
2755 }
2756 return flags;
2757}
2758
2759/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 * Verify the certificate validity
2761 */
2762int x509parse_verify( x509_cert *crt,
2763 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002764 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002765 const char *cn, int *flags,
2766 int (*f_vrfy)(void *, x509_cert *, int, int),
2767 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Paul Bakker23986e52011-04-24 08:57:21 +00002769 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 int hash_id;
2771 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002772 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002774 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Paul Bakker40ea7de2009-05-03 10:18:48 +00002776 *flags = 0;
2777
2778 if( x509parse_time_expired( &crt->valid_to ) )
2779 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002780
2781 if( cn != NULL )
2782 {
2783 name = &crt->subject;
2784 cn_len = strlen( cn );
2785
2786 while( name != NULL )
2787 {
2788 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2789 memcmp( name->val.p, cn, cn_len ) == 0 &&
2790 name->val.len == cn_len )
2791 break;
2792
2793 name = name->next;
2794 }
2795
2796 if( name == NULL )
2797 *flags |= BADCERT_CN_MISMATCH;
2798 }
2799
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 /*
2801 * Iterate upwards in the given cert chain,
2802 * ignoring any upper cert with CA != TRUE.
2803 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002804 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
2806 pathlen = 1;
2807
Paul Bakker76fd75a2011-01-16 21:12:10 +00002808 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002810 if( parent->ca_istrue == 0 ||
2811 crt->issuer_raw.len != parent->subject_raw.len ||
2812 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 crt->issuer_raw.len ) != 0 )
2814 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002815 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 continue;
2817 }
2818
Paul Bakker27d66162010-03-17 06:56:01 +00002819 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2822
Paul Bakker76fd75a2011-01-16 21:12:10 +00002823 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2824 crt->sig.p ) != 0 )
2825 *flags |= BADCERT_NOT_TRUSTED;
2826
2827 /* Check trusted CA's CRL for the given crt */
2828 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002829
2830 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002831 if( NULL != f_vrfy )
2832 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002833 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002834 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002835 else
2836 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002837 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002838 else if( *flags != 0 )
2839 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 pathlen++;
2842
Paul Bakker76fd75a2011-01-16 21:12:10 +00002843 crt = parent;
2844 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 }
2846
2847 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002848 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002850 *flags |= BADCERT_NOT_TRUSTED;
2851
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002852 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 {
2854 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2855 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2856 crt->issuer_raw.len ) != 0 )
2857 {
2858 trust_ca = trust_ca->next;
2859 continue;
2860 }
2861
2862 if( trust_ca->max_pathlen > 0 &&
2863 trust_ca->max_pathlen < pathlen )
2864 break;
2865
Paul Bakker27d66162010-03-17 06:56:01 +00002866 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
2868 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2869
2870 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2871 0, hash, crt->sig.p ) == 0 )
2872 {
2873 /*
2874 * cert. is signed by a trusted CA
2875 */
2876 *flags &= ~BADCERT_NOT_TRUSTED;
2877 break;
2878 }
2879
2880 trust_ca = trust_ca->next;
2881 }
2882
Paul Bakker76fd75a2011-01-16 21:12:10 +00002883 /* Check trusted CA's CRL for the given crt */
2884 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002885
2886 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002887 if( NULL != f_vrfy )
2888 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002889 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002890 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002891 else
2892 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002893 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002894 else if( *flags != 0 )
2895 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 return( 0 );
2898}
2899
2900/*
2901 * Unallocate all certificate data
2902 */
2903void x509_free( x509_cert *crt )
2904{
2905 x509_cert *cert_cur = crt;
2906 x509_cert *cert_prv;
2907 x509_name *name_cur;
2908 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00002909 x509_sequence *seq_cur;
2910 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 if( crt == NULL )
2913 return;
2914
2915 do
2916 {
2917 rsa_free( &cert_cur->rsa );
2918
2919 name_cur = cert_cur->issuer.next;
2920 while( name_cur != NULL )
2921 {
2922 name_prv = name_cur;
2923 name_cur = name_cur->next;
2924 memset( name_prv, 0, sizeof( x509_name ) );
2925 free( name_prv );
2926 }
2927
2928 name_cur = cert_cur->subject.next;
2929 while( name_cur != NULL )
2930 {
2931 name_prv = name_cur;
2932 name_cur = name_cur->next;
2933 memset( name_prv, 0, sizeof( x509_name ) );
2934 free( name_prv );
2935 }
2936
Paul Bakker74111d32011-01-15 16:57:55 +00002937 seq_cur = cert_cur->ext_key_usage.next;
2938 while( seq_cur != NULL )
2939 {
2940 seq_prv = seq_cur;
2941 seq_cur = seq_cur->next;
2942 memset( seq_prv, 0, sizeof( x509_sequence ) );
2943 free( seq_prv );
2944 }
2945
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 if( cert_cur->raw.p != NULL )
2947 {
2948 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2949 free( cert_cur->raw.p );
2950 }
2951
2952 cert_cur = cert_cur->next;
2953 }
2954 while( cert_cur != NULL );
2955
2956 cert_cur = crt;
2957 do
2958 {
2959 cert_prv = cert_cur;
2960 cert_cur = cert_cur->next;
2961
2962 memset( cert_prv, 0, sizeof( x509_cert ) );
2963 if( cert_prv != crt )
2964 free( cert_prv );
2965 }
2966 while( cert_cur != NULL );
2967}
2968
Paul Bakkerd98030e2009-05-02 15:13:40 +00002969/*
2970 * Unallocate all CRL data
2971 */
2972void x509_crl_free( x509_crl *crl )
2973{
2974 x509_crl *crl_cur = crl;
2975 x509_crl *crl_prv;
2976 x509_name *name_cur;
2977 x509_name *name_prv;
2978 x509_crl_entry *entry_cur;
2979 x509_crl_entry *entry_prv;
2980
2981 if( crl == NULL )
2982 return;
2983
2984 do
2985 {
2986 name_cur = crl_cur->issuer.next;
2987 while( name_cur != NULL )
2988 {
2989 name_prv = name_cur;
2990 name_cur = name_cur->next;
2991 memset( name_prv, 0, sizeof( x509_name ) );
2992 free( name_prv );
2993 }
2994
2995 entry_cur = crl_cur->entry.next;
2996 while( entry_cur != NULL )
2997 {
2998 entry_prv = entry_cur;
2999 entry_cur = entry_cur->next;
3000 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3001 free( entry_prv );
3002 }
3003
3004 if( crl_cur->raw.p != NULL )
3005 {
3006 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3007 free( crl_cur->raw.p );
3008 }
3009
3010 crl_cur = crl_cur->next;
3011 }
3012 while( crl_cur != NULL );
3013
3014 crl_cur = crl;
3015 do
3016 {
3017 crl_prv = crl_cur;
3018 crl_cur = crl_cur->next;
3019
3020 memset( crl_prv, 0, sizeof( x509_crl ) );
3021 if( crl_prv != crl )
3022 free( crl_prv );
3023 }
3024 while( crl_cur != NULL );
3025}
3026
Paul Bakker40e46942009-01-03 21:51:57 +00003027#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakker40e46942009-01-03 21:51:57 +00003029#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
3031/*
3032 * Checkup routine
3033 */
3034int x509_self_test( int verbose )
3035{
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003036#if defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003037 int ret;
3038 int flags;
3039 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 x509_cert cacert;
3041 x509_cert clicert;
3042 rsa_context rsa;
Paul Bakker1b57b062011-01-06 15:48:19 +00003043 dhm_context dhm;
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
3045 if( verbose != 0 )
3046 printf( " X.509 certificate load: " );
3047
3048 memset( &clicert, 0, sizeof( x509_cert ) );
3049
3050 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3051 strlen( test_cli_crt ) );
3052 if( ret != 0 )
3053 {
3054 if( verbose != 0 )
3055 printf( "failed\n" );
3056
3057 return( ret );
3058 }
3059
3060 memset( &cacert, 0, sizeof( x509_cert ) );
3061
3062 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3063 strlen( test_ca_crt ) );
3064 if( ret != 0 )
3065 {
3066 if( verbose != 0 )
3067 printf( "failed\n" );
3068
3069 return( ret );
3070 }
3071
3072 if( verbose != 0 )
3073 printf( "passed\n X.509 private key load: " );
3074
3075 i = strlen( test_ca_key );
3076 j = strlen( test_ca_pwd );
3077
Paul Bakker66b78b22011-03-25 14:22:50 +00003078 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3079
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 if( ( ret = x509parse_key( &rsa,
3081 (unsigned char *) test_ca_key, i,
3082 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3083 {
3084 if( verbose != 0 )
3085 printf( "failed\n" );
3086
3087 return( ret );
3088 }
3089
3090 if( verbose != 0 )
3091 printf( "passed\n X.509 signature verify: ");
3092
Paul Bakker23986e52011-04-24 08:57:21 +00003093 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 if( ret != 0 )
3095 {
Paul Bakker23986e52011-04-24 08:57:21 +00003096 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 if( verbose != 0 )
3098 printf( "failed\n" );
3099
3100 return( ret );
3101 }
3102
3103 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003104 printf( "passed\n X.509 DHM parameter load: " );
3105
3106 i = strlen( test_dhm_params );
3107 j = strlen( test_ca_pwd );
3108
3109 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3110 {
3111 if( verbose != 0 )
3112 printf( "failed\n" );
3113
3114 return( ret );
3115 }
3116
3117 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 printf( "passed\n\n" );
3119
3120 x509_free( &cacert );
3121 x509_free( &clicert );
3122 rsa_free( &rsa );
Paul Bakker1b57b062011-01-06 15:48:19 +00003123 dhm_free( &dhm );
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
3125 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003126#else
3127 ((void) verbose);
3128 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003130}
3131
3132#endif
3133
3134#endif