blob: 3a67539632b71a2b2fc18b94466f4468ca14225c [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 Bakker40e46942009-01-03 21:51:57 +0000285 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287 if( *p != end )
Paul Bakker40e46942009-01-03 21:51:57 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION |
289 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 Bakker40e46942009-01-03 21:51:57 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL |
305 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 Bakker40e46942009-01-03 21:51:57 +0000309 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL |
310 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 Bakker40e46942009-01-03 21:51:57 +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 Bakker40e46942009-01-03 21:51:57 +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 Bakker40e46942009-01-03 21:51:57 +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 Bakker40e46942009-01-03 21:51:57 +0000355 return( POLARSSL_ERR_X509_CERT_INVALID_ALG | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357 if( *p != end )
Paul Bakker40e46942009-01-03 21:51:57 +0000358 return( POLARSSL_ERR_X509_CERT_INVALID_ALG |
359 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 Bakker40e46942009-01-03 21:51:57 +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 Bakker40e46942009-01-03 21:51:57 +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 Bakker40e46942009-01-03 21:51:57 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_NAME |
397 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 Bakker40e46942009-01-03 21:51:57 +0000402 return( POLARSSL_ERR_X509_CERT_INVALID_NAME |
403 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 Bakker40e46942009-01-03 21:51:57 +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 )
442 return( POLARSSL_ERR_X509_CERT_INVALID_NAME | ret );
443
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 )
498 return( POLARSSL_ERR_X509_CERT_INVALID_DATE | POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000499
Paul Bakker91200182010-02-18 21:26:15 +0000500 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000501
Paul Bakker91200182010-02-18 21:26:15 +0000502 if ( tag == ASN1_UTC_TIME )
503 {
504 (*p)++;
505 ret = asn1_get_len( p, end, &len );
506
507 if( ret != 0 )
508 return( POLARSSL_ERR_X509_CERT_INVALID_DATE | ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000509
Paul Bakker91200182010-02-18 21:26:15 +0000510 memset( date, 0, sizeof( date ) );
511 memcpy( date, *p, ( len < (int) sizeof( date ) - 1 ) ?
512 len : (int) sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000513
Paul Bakker91200182010-02-18 21:26:15 +0000514 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
515 &time->year, &time->mon, &time->day,
516 &time->hour, &time->min, &time->sec ) < 5 )
517 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000518
Paul Bakker400ff6f2011-02-20 10:40:16 +0000519 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000520 time->year += 1900;
521
522 *p += len;
523
524 return( 0 );
525 }
526 else if ( tag == ASN1_GENERALIZED_TIME )
527 {
528 (*p)++;
529 ret = asn1_get_len( p, end, &len );
530
531 if( ret != 0 )
532 return( POLARSSL_ERR_X509_CERT_INVALID_DATE | ret );
533
534 memset( date, 0, sizeof( date ) );
535 memcpy( date, *p, ( len < (int) sizeof( date ) - 1 ) ?
536 len : (int) sizeof( date ) - 1 );
537
538 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
539 &time->year, &time->mon, &time->day,
540 &time->hour, &time->min, &time->sec ) < 5 )
541 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
542
543 *p += len;
544
545 return( 0 );
546 }
547 else
548 return( POLARSSL_ERR_X509_CERT_INVALID_DATE | POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549}
550
551
552/*
553 * Validity ::= SEQUENCE {
554 * notBefore Time,
555 * notAfter Time }
556 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000557static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000558 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 x509_time *from,
560 x509_time *to )
561{
Paul Bakker23986e52011-04-24 08:57:21 +0000562 int ret;
563 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565 if( ( ret = asn1_get_tag( p, end, &len,
566 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000567 return( POLARSSL_ERR_X509_CERT_INVALID_DATE | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 end = *p + len;
570
Paul Bakker91200182010-02-18 21:26:15 +0000571 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000572 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Paul Bakker91200182010-02-18 21:26:15 +0000574 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000575 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 if( *p != end )
Paul Bakker40e46942009-01-03 21:51:57 +0000578 return( POLARSSL_ERR_X509_CERT_INVALID_DATE |
579 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 return( 0 );
582}
583
584/*
585 * SubjectPublicKeyInfo ::= SEQUENCE {
586 * algorithm AlgorithmIdentifier,
587 * subjectPublicKey BIT STRING }
588 */
589static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000590 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 x509_buf *pk_alg_oid,
592 mpi *N, mpi *E )
593{
Paul Bakker23986e52011-04-24 08:57:21 +0000594 int ret, can_handle;
595 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 unsigned char *end2;
597
598 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
599 return( ret );
600
601 /*
602 * only RSA public keys handled at this time
603 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000604 can_handle = 0;
605
606 if( pk_alg_oid->len == 9 &&
607 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
608 can_handle = 1;
609
610 if( pk_alg_oid->len == 9 &&
611 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
612 {
613 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
614 can_handle = 1;
615
616 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
617 can_handle = 1;
618 }
619
620 if( pk_alg_oid->len == 5 &&
621 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
622 can_handle = 1;
623
624 if( can_handle == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000625 return( POLARSSL_ERR_X509_CERT_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
627 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY |
632 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634 end2 = *p + len;
635
636 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000637 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639 /*
640 * RSAPublicKey ::= SEQUENCE {
641 * modulus INTEGER, -- n
642 * publicExponent INTEGER -- e
643 * }
644 */
645 if( ( ret = asn1_get_tag( p, end2, &len,
646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 if( *p + len != end2 )
Paul Bakker40e46942009-01-03 21:51:57 +0000650 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY |
651 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
654 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000655 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657 if( *p != end )
Paul Bakker40e46942009-01-03 21:51:57 +0000658 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY |
659 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661 return( 0 );
662}
663
664static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000665 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 x509_buf *sig )
667{
Paul Bakker23986e52011-04-24 08:57:21 +0000668 int ret;
669 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 sig->tag = **p;
672
673 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000674 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Paul Bakker74111d32011-01-15 16:57:55 +0000676
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000678 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680 sig->len = len;
681 sig->p = *p;
682
683 *p += len;
684
685 return( 0 );
686}
687
688/*
689 * X.509 v2/v3 unique identifier (not parsed)
690 */
691static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000692 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 x509_buf *uid, int n )
694{
695 int ret;
696
697 if( *p == end )
698 return( 0 );
699
700 uid->tag = **p;
701
702 if( ( ret = asn1_get_tag( p, end, &uid->len,
703 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
704 {
Paul Bakker40e46942009-01-03 21:51:57 +0000705 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 return( 0 );
707
708 return( ret );
709 }
710
711 uid->p = *p;
712 *p += uid->len;
713
714 return( 0 );
715}
716
717/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000718 * X.509 Extensions (No parsing of extensions, pointer should
719 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 */
721static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000722 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000723 x509_buf *ext )
Paul Bakker5121ce52009-01-03 21:22:43 +0000724{
Paul Bakker23986e52011-04-24 08:57:21 +0000725 int ret;
726 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728 if( *p == end )
729 return( 0 );
730
731 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000732
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 if( ( ret = asn1_get_tag( p, end, &ext->len,
734 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737 ext->p = *p;
738 end = *p + ext->len;
739
740 /*
741 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
742 *
743 * Extension ::= SEQUENCE {
744 * extnID OBJECT IDENTIFIER,
745 * critical BOOLEAN DEFAULT FALSE,
746 * extnValue OCTET STRING }
747 */
748 if( ( ret = asn1_get_tag( p, end, &len,
749 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752 if( end != *p + len )
Paul Bakker40e46942009-01-03 21:51:57 +0000753 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
754 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Paul Bakkerd98030e2009-05-02 15:13:40 +0000756 return( 0 );
757}
758
759/*
760 * X.509 CRL v2 extensions (no extensions parsed yet.)
761 */
762static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000763 const unsigned char *end,
764 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000765{
Paul Bakker23986e52011-04-24 08:57:21 +0000766 int ret;
767 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000768
769 if( ( ret = x509_get_ext( p, end, ext ) ) != 0 )
770 {
771 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
772 return( 0 );
773
774 return( ret );
775 }
776
777 while( *p < end )
778 {
779 if( ( ret = asn1_get_tag( p, end, &len,
780 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
782
783 *p += len;
784 }
785
786 if( *p != end )
787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
788 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
789
790 return( 0 );
791}
792
Paul Bakker74111d32011-01-15 16:57:55 +0000793static int x509_get_basic_constraints( unsigned char **p,
794 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000795 int *ca_istrue,
796 int *max_pathlen )
797{
Paul Bakker23986e52011-04-24 08:57:21 +0000798 int ret;
799 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000800
801 /*
802 * BasicConstraints ::= SEQUENCE {
803 * cA BOOLEAN DEFAULT FALSE,
804 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
805 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000806 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000807 *max_pathlen = 0; /* endless */
808
809 if( ( ret = asn1_get_tag( p, end, &len,
810 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
811 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
812
813 if( *p == end )
814 return 0;
815
Paul Bakker3cccddb2011-01-16 21:46:31 +0000816 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000817 {
818 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000819 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000820
821 if( ret != 0 )
822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
823
Paul Bakker3cccddb2011-01-16 21:46:31 +0000824 if( *ca_istrue != 0 )
825 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000826 }
827
828 if( *p == end )
829 return 0;
830
831 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
832 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
833
834 if( *p != end )
835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
836 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
837
838 (*max_pathlen)++;
839
Paul Bakker74111d32011-01-15 16:57:55 +0000840 return 0;
841}
842
843static int x509_get_ns_cert_type( unsigned char **p,
844 const unsigned char *end,
845 unsigned char *ns_cert_type)
846{
847 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000848 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000849
850 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
852
853 if( bs.len != 1 )
854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
855 POLARSSL_ERR_ASN1_INVALID_LENGTH );
856
857 /* Get actual bitstring */
858 *ns_cert_type = *bs.p;
859 return 0;
860}
861
862static int x509_get_key_usage( unsigned char **p,
863 const unsigned char *end,
864 unsigned char *key_usage)
865{
866 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000867 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000868
869 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
871
872 if( bs.len != 1 )
873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
874 POLARSSL_ERR_ASN1_INVALID_LENGTH );
875
876 /* Get actual bitstring */
877 *key_usage = *bs.p;
878 return 0;
879}
880
Paul Bakkerd98030e2009-05-02 15:13:40 +0000881/*
Paul Bakker74111d32011-01-15 16:57:55 +0000882 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
883 *
884 * KeyPurposeId ::= OBJECT IDENTIFIER
885 */
886static int x509_get_ext_key_usage( unsigned char **p,
887 const unsigned char *end,
888 x509_sequence *ext_key_usage)
889{
890 int ret;
891
892 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
893 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
894
895 /* Sequence length must be >= 1 */
896 if( ext_key_usage->buf.p == NULL )
897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
898 POLARSSL_ERR_ASN1_INVALID_LENGTH );
899
900 return 0;
901}
902
903/*
904 * X.509 v3 extensions
905 *
906 * TODO: Perform all of the basic constraints tests required by the RFC
907 * TODO: Set values for undetected extensions to a sane default?
908 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000909 */
910static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000911 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000912 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000913{
Paul Bakker23986e52011-04-24 08:57:21 +0000914 int ret;
915 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000916 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000917
Paul Bakker74111d32011-01-15 16:57:55 +0000918 if( ( ret = x509_get_ext( p, end, &crt->v3_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000919 {
920 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
921 return( 0 );
922
923 return( ret );
924 }
925
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 while( *p < end )
927 {
Paul Bakker74111d32011-01-15 16:57:55 +0000928 /*
929 * Extension ::= SEQUENCE {
930 * extnID OBJECT IDENTIFIER,
931 * critical BOOLEAN DEFAULT FALSE,
932 * extnValue OCTET STRING }
933 */
934 x509_buf extn_oid = {0, 0, NULL};
935 int is_critical = 0; /* DEFAULT FALSE */
936
Paul Bakker5121ce52009-01-03 21:22:43 +0000937 if( ( ret = asn1_get_tag( p, end, &len,
938 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000939 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000940
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000941 end_ext_data = *p + len;
942
Paul Bakker74111d32011-01-15 16:57:55 +0000943 /* Get extension ID */
944 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakker74111d32011-01-15 16:57:55 +0000946 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
947 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Paul Bakker74111d32011-01-15 16:57:55 +0000949 extn_oid.p = *p;
950 *p += extn_oid.len;
951
952 if( ( end - *p ) < 1 )
953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
954 POLARSSL_ERR_ASN1_OUT_OF_DATA );
955
956 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000957 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000958 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
959 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Paul Bakker74111d32011-01-15 16:57:55 +0000961 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000962 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000964 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000966 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000967
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000968 if( end_ext_octet != end_ext_data )
969 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
970 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Paul Bakker74111d32011-01-15 16:57:55 +0000972 /*
973 * Detect supported extensions
974 */
975 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
976 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 {
Paul Bakker74111d32011-01-15 16:57:55 +0000978 /* Parse basic constraints */
979 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000980 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000981 return ( ret );
982 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000983 }
Paul Bakker74111d32011-01-15 16:57:55 +0000984 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
985 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
986 {
987 /* Parse netscape certificate type */
988 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
989 &crt->ns_cert_type ) ) != 0 )
990 return ( ret );
991 crt->ext_types |= EXT_NS_CERT_TYPE;
992 }
993 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
994 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
995 {
996 /* Parse key usage */
997 if( ( ret = x509_get_key_usage( p, end_ext_octet,
998 &crt->key_usage ) ) != 0 )
999 return ( ret );
1000 crt->ext_types |= EXT_KEY_USAGE;
1001 }
1002 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1003 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1004 {
1005 /* Parse extended key usage */
1006 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1007 &crt->ext_key_usage ) ) != 0 )
1008 return ( ret );
1009 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1010 }
1011 else
1012 {
1013 /* No parser found, skip extension */
1014 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Paul Bakker74111d32011-01-15 16:57:55 +00001016 if( is_critical )
1017 {
1018 /* Data is marked as critical: fail */
1019 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
1020 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1021 }
1022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 }
1024
1025 if( *p != end )
Paul Bakker40e46942009-01-03 21:51:57 +00001026 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS |
1027 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 return( 0 );
1030}
1031
1032/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001033 * X.509 CRL Entries
1034 */
1035static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001036 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 x509_crl_entry *entry )
1038{
Paul Bakker23986e52011-04-24 08:57:21 +00001039 int ret;
1040 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *cur_entry = entry;
1042
1043 if( *p == end )
1044 return( 0 );
1045
Paul Bakker9be19372009-07-27 20:21:53 +00001046 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001047 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1048 {
1049 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1050 return( 0 );
1051
1052 return( ret );
1053 }
1054
Paul Bakker9be19372009-07-27 20:21:53 +00001055 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056
1057 while( *p < end )
1058 {
Paul Bakker23986e52011-04-24 08:57:21 +00001059 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 if( ( ret = asn1_get_tag( p, end, &len2,
1062 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1063 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001064 return( ret );
1065 }
1066
Paul Bakker9be19372009-07-27 20:21:53 +00001067 cur_entry->raw.tag = **p;
1068 cur_entry->raw.p = *p;
1069 cur_entry->raw.len = len2;
1070
Paul Bakkerd98030e2009-05-02 15:13:40 +00001071 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1072 return( ret );
1073
Paul Bakker91200182010-02-18 21:26:15 +00001074 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 return( ret );
1076
1077 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1078 return( ret );
1079
Paul Bakker74111d32011-01-15 16:57:55 +00001080 if ( *p < end )
1081 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1083 cur_entry = cur_entry->next;
1084 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1085 }
1086 }
1087
1088 return( 0 );
1089}
1090
Paul Bakker27d66162010-03-17 06:56:01 +00001091static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1092{
1093 if( sig_oid->len == 9 &&
1094 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1095 {
1096 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1097 {
1098 *sig_alg = sig_oid->p[8];
1099 return( 0 );
1100 }
1101
1102 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1103 {
1104 *sig_alg = sig_oid->p[8];
1105 return( 0 );
1106 }
1107
1108 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1109 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001110 if( sig_oid->len == 5 &&
1111 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1112 {
1113 *sig_alg = SIG_RSA_SHA1;
1114 return( 0 );
1115 }
Paul Bakker27d66162010-03-17 06:56:01 +00001116
1117 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1118}
1119
Paul Bakkerd98030e2009-05-02 15:13:40 +00001120/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 * Parse one or more certificates and add them to the chained list
1122 */
Paul Bakker23986e52011-04-24 08:57:21 +00001123int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124{
Paul Bakker23986e52011-04-24 08:57:21 +00001125 int ret;
1126 size_t len, use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 unsigned char *p, *end;
1128 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001129#if defined(POLARSSL_PEM_C)
1130 pem_context pem;
1131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133 crt = chain;
1134
Paul Bakker320a4b52009-03-28 18:52:39 +00001135 /*
1136 * Check for valid input
1137 */
1138 if( crt == NULL || buf == NULL )
1139 return( 1 );
1140
Paul Bakkere9581d62009-03-28 20:29:25 +00001141 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 crt = crt->next;
1143
1144 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001145 * Add new certificate on the end of the chain if needed.
1146 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001147 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001148 {
1149 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1150
Paul Bakker7d06ad22009-05-02 15:53:56 +00001151 if( crt->next == NULL )
1152 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001153 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001154 return( 1 );
1155 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001156
Paul Bakker7d06ad22009-05-02 15:53:56 +00001157 crt = crt->next;
1158 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001159 }
1160
Paul Bakker96743fc2011-02-12 14:30:57 +00001161#if defined(POLARSSL_PEM_C)
1162 pem_init( &pem );
1163 ret = pem_read_buffer( &pem,
1164 "-----BEGIN CERTIFICATE-----",
1165 "-----END CERTIFICATE-----",
1166 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167
Paul Bakker96743fc2011-02-12 14:30:57 +00001168 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001170 /*
1171 * Was PEM encoded
1172 */
1173 buflen -= use_len;
1174 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
1176 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001177 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001179 p = pem.buf;
1180 pem.buf = NULL;
1181 len = pem.buflen;
1182 pem_free( &pem );
1183 }
1184 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1185 {
1186 pem_free( &pem );
1187 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
1189 else
1190 {
1191 /*
1192 * nope, copy the raw DER data
1193 */
1194 p = (unsigned char *) malloc( len = buflen );
1195
1196 if( p == NULL )
1197 return( 1 );
1198
1199 memcpy( p, buf, buflen );
1200
1201 buflen = 0;
1202 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001203#else
1204 p = (unsigned char *) malloc( len = buflen );
1205
1206 if( p == NULL )
1207 return( 1 );
1208
1209 memcpy( p, buf, buflen );
1210
1211 buflen = 0;
1212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
1214 crt->raw.p = p;
1215 crt->raw.len = len;
1216 end = p + len;
1217
1218 /*
1219 * Certificate ::= SEQUENCE {
1220 * tbsCertificate TBSCertificate,
1221 * signatureAlgorithm AlgorithmIdentifier,
1222 * signatureValue BIT STRING }
1223 */
1224 if( ( ret = asn1_get_tag( &p, end, &len,
1225 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1226 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001227 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001228 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001229 }
1230
Paul Bakker23986e52011-04-24 08:57:21 +00001231 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001233 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001234 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1235 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 }
1237
1238 /*
1239 * TBSCertificate ::= SEQUENCE {
1240 */
1241 crt->tbs.p = p;
1242
1243 if( ( ret = asn1_get_tag( &p, end, &len,
1244 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001247 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 }
1249
1250 end = p + len;
1251 crt->tbs.len = end - crt->tbs.p;
1252
1253 /*
1254 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1255 *
1256 * CertificateSerialNumber ::= INTEGER
1257 *
1258 * signature AlgorithmIdentifier
1259 */
1260 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1261 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1262 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 return( ret );
1266 }
1267
1268 crt->version++;
1269
1270 if( crt->version > 3 )
1271 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001272 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001273 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Paul Bakker27d66162010-03-17 06:56:01 +00001276 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001278 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001279 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 }
1281
1282 /*
1283 * issuer Name
1284 */
1285 crt->issuer_raw.p = p;
1286
1287 if( ( ret = asn1_get_tag( &p, end, &len,
1288 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1289 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001290 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001291 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 }
1293
1294 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1295 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 return( ret );
1298 }
1299
1300 crt->issuer_raw.len = p - crt->issuer_raw.p;
1301
1302 /*
1303 * Validity ::= SEQUENCE {
1304 * notBefore Time,
1305 * notAfter Time }
1306 *
1307 */
1308 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1309 &crt->valid_to ) ) != 0 )
1310 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001311 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 return( ret );
1313 }
1314
1315 /*
1316 * subject Name
1317 */
1318 crt->subject_raw.p = p;
1319
1320 if( ( ret = asn1_get_tag( &p, end, &len,
1321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1322 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001323 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
1327 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1328 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001329 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 return( ret );
1331 }
1332
1333 crt->subject_raw.len = p - crt->subject_raw.p;
1334
1335 /*
1336 * SubjectPublicKeyInfo ::= SEQUENCE
1337 * algorithm AlgorithmIdentifier,
1338 * subjectPublicKey BIT STRING }
1339 */
1340 if( ( ret = asn1_get_tag( &p, end, &len,
1341 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001344 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
1347 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1348 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1349 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001350 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 return( ret );
1352 }
1353
1354 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1355 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001356 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 return( ret );
1358 }
1359
1360 crt->rsa.len = mpi_size( &crt->rsa.N );
1361
1362 /*
1363 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1364 * -- If present, version shall be v2 or v3
1365 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1366 * -- If present, version shall be v2 or v3
1367 * extensions [3] EXPLICIT Extensions OPTIONAL
1368 * -- If present, version shall be v3
1369 */
1370 if( crt->version == 2 || crt->version == 3 )
1371 {
1372 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1373 if( ret != 0 )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378 }
1379
1380 if( crt->version == 2 || crt->version == 3 )
1381 {
1382 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1383 if( ret != 0 )
1384 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001385 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 return( ret );
1387 }
1388 }
1389
1390 if( crt->version == 3 )
1391 {
Paul Bakker74111d32011-01-15 16:57:55 +00001392 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 if( ret != 0 )
1394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 return( ret );
1397 }
1398 }
1399
1400 if( p != end )
1401 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001402 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001403 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1404 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 }
1406
1407 end = crt->raw.p + crt->raw.len;
1408
1409 /*
1410 * signatureAlgorithm AlgorithmIdentifier,
1411 * signatureValue BIT STRING
1412 */
1413 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1414 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 return( ret );
1417 }
1418
Paul Bakker320a4b52009-03-28 18:52:39 +00001419 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001421 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001422 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 }
1424
1425 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1426 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001427 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 return( ret );
1429 }
1430
1431 if( p != end )
1432 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001433 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001434 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1435 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
1437
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001439 {
1440 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1441
Paul Bakker7d06ad22009-05-02 15:53:56 +00001442 if( crt->next == NULL )
1443 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001444 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 return( 1 );
1446 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001447
Paul Bakker7d06ad22009-05-02 15:53:56 +00001448 crt = crt->next;
1449 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001450
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
1454 return( 0 );
1455}
1456
1457/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001458 * Parse one or more CRLs and add them to the chained list
1459 */
Paul Bakker23986e52011-04-24 08:57:21 +00001460int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001461{
Paul Bakker23986e52011-04-24 08:57:21 +00001462 int ret;
1463 size_t len, use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001464 unsigned char *p, *end;
1465 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001466#if defined(POLARSSL_PEM_C)
1467 pem_context pem;
1468#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001469
1470 crl = chain;
1471
1472 /*
1473 * Check for valid input
1474 */
1475 if( crl == NULL || buf == NULL )
1476 return( 1 );
1477
1478 while( crl->version != 0 && crl->next != NULL )
1479 crl = crl->next;
1480
1481 /*
1482 * Add new CRL on the end of the chain if needed.
1483 */
1484 if ( crl->version != 0 && crl->next == NULL)
1485 {
1486 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1487
Paul Bakker7d06ad22009-05-02 15:53:56 +00001488 if( crl->next == NULL )
1489 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001490 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001491 return( 1 );
1492 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001493
Paul Bakker7d06ad22009-05-02 15:53:56 +00001494 crl = crl->next;
1495 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001496 }
1497
Paul Bakker96743fc2011-02-12 14:30:57 +00001498#if defined(POLARSSL_PEM_C)
1499 pem_init( &pem );
1500 ret = pem_read_buffer( &pem,
1501 "-----BEGIN X509 CRL-----",
1502 "-----END X509 CRL-----",
1503 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001504
Paul Bakker96743fc2011-02-12 14:30:57 +00001505 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001507 /*
1508 * Was PEM encoded
1509 */
1510 buflen -= use_len;
1511 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001512
1513 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001514 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001515 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001516 p = pem.buf;
1517 pem.buf = NULL;
1518 len = pem.buflen;
1519 pem_free( &pem );
1520 }
1521 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1522 {
1523 pem_free( &pem );
1524 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525 }
1526 else
1527 {
1528 /*
1529 * nope, copy the raw DER data
1530 */
1531 p = (unsigned char *) malloc( len = buflen );
1532
1533 if( p == NULL )
1534 return( 1 );
1535
1536 memcpy( p, buf, buflen );
1537
1538 buflen = 0;
1539 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001540#else
1541 p = (unsigned char *) malloc( len = buflen );
1542
1543 if( p == NULL )
1544 return( 1 );
1545
1546 memcpy( p, buf, buflen );
1547
1548 buflen = 0;
1549#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550
1551 crl->raw.p = p;
1552 crl->raw.len = len;
1553 end = p + len;
1554
1555 /*
1556 * CertificateList ::= SEQUENCE {
1557 * tbsCertList TBSCertList,
1558 * signatureAlgorithm AlgorithmIdentifier,
1559 * signatureValue BIT STRING }
1560 */
1561 if( ( ret = asn1_get_tag( &p, end, &len,
1562 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1563 {
1564 x509_crl_free( crl );
1565 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1566 }
1567
Paul Bakker23986e52011-04-24 08:57:21 +00001568 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001569 {
1570 x509_crl_free( crl );
1571 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1572 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1573 }
1574
1575 /*
1576 * TBSCertList ::= SEQUENCE {
1577 */
1578 crl->tbs.p = p;
1579
1580 if( ( ret = asn1_get_tag( &p, end, &len,
1581 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1582 {
1583 x509_crl_free( crl );
1584 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
1585 }
1586
1587 end = p + len;
1588 crl->tbs.len = end - crl->tbs.p;
1589
1590 /*
1591 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1592 * -- if present, MUST be v2
1593 *
1594 * signature AlgorithmIdentifier
1595 */
1596 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1597 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1598 {
1599 x509_crl_free( crl );
1600 return( ret );
1601 }
1602
1603 crl->version++;
1604
1605 if( crl->version > 2 )
1606 {
1607 x509_crl_free( crl );
1608 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1609 }
1610
Paul Bakker27d66162010-03-17 06:56:01 +00001611 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 {
1613 x509_crl_free( crl );
1614 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1615 }
1616
1617 /*
1618 * issuer Name
1619 */
1620 crl->issuer_raw.p = p;
1621
1622 if( ( ret = asn1_get_tag( &p, end, &len,
1623 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1624 {
1625 x509_crl_free( crl );
1626 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
1627 }
1628
1629 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1630 {
1631 x509_crl_free( crl );
1632 return( ret );
1633 }
1634
1635 crl->issuer_raw.len = p - crl->issuer_raw.p;
1636
1637 /*
1638 * thisUpdate Time
1639 * nextUpdate Time OPTIONAL
1640 */
Paul Bakker91200182010-02-18 21:26:15 +00001641 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642 {
1643 x509_crl_free( crl );
1644 return( ret );
1645 }
1646
Paul Bakker91200182010-02-18 21:26:15 +00001647 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648 {
Paul Bakker635f4b42009-07-20 20:34:41 +00001649 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE |
Paul Bakker9be19372009-07-27 20:21:53 +00001650 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
1651 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE |
1652 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001653 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654 x509_crl_free( crl );
1655 return( ret );
1656 }
1657 }
1658
1659 /*
1660 * revokedCertificates SEQUENCE OF SEQUENCE {
1661 * userCertificate CertificateSerialNumber,
1662 * revocationDate Time,
1663 * crlEntryExtensions Extensions OPTIONAL
1664 * -- if present, MUST be v2
1665 * } OPTIONAL
1666 */
1667 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1668 {
1669 x509_crl_free( crl );
1670 return( ret );
1671 }
1672
1673 /*
1674 * crlExtensions EXPLICIT Extensions OPTIONAL
1675 * -- if present, MUST be v2
1676 */
1677 if( crl->version == 2 )
1678 {
1679 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1680
1681 if( ret != 0 )
1682 {
1683 x509_crl_free( crl );
1684 return( ret );
1685 }
1686 }
1687
1688 if( p != end )
1689 {
1690 x509_crl_free( crl );
1691 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1692 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1693 }
1694
1695 end = crl->raw.p + crl->raw.len;
1696
1697 /*
1698 * signatureAlgorithm AlgorithmIdentifier,
1699 * signatureValue BIT STRING
1700 */
1701 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1702 {
1703 x509_crl_free( crl );
1704 return( ret );
1705 }
1706
1707 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1708 {
1709 x509_crl_free( crl );
1710 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1711 }
1712
1713 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1714 {
1715 x509_crl_free( crl );
1716 return( ret );
1717 }
1718
1719 if( p != end )
1720 {
1721 x509_crl_free( crl );
1722 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT |
1723 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1724 }
1725
1726 if( buflen > 0 )
1727 {
1728 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1729
Paul Bakker7d06ad22009-05-02 15:53:56 +00001730 if( crl->next == NULL )
1731 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001733 return( 1 );
1734 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735
Paul Bakker7d06ad22009-05-02 15:53:56 +00001736 crl = crl->next;
1737 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738
1739 return( x509parse_crl( crl, buf, buflen ) );
1740 }
1741
1742 return( 0 );
1743}
1744
Paul Bakker335db3f2011-04-25 15:28:35 +00001745#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001746/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001747 * Load all data from a file into a given buffer.
1748 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001749int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001750{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001752
Paul Bakkerd98030e2009-05-02 15:13:40 +00001753 if( ( f = fopen( path, "rb" ) ) == NULL )
1754 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001755
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756 fseek( f, 0, SEEK_END );
1757 *n = (size_t) ftell( f );
1758 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001759
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1761 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001762
Paul Bakkerd98030e2009-05-02 15:13:40 +00001763 if( fread( *buf, 1, *n, f ) != *n )
1764 {
1765 fclose( f );
1766 free( *buf );
1767 return( 1 );
1768 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001769
Paul Bakkerd98030e2009-05-02 15:13:40 +00001770 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001771
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001773
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001775}
1776
1777/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 * Load one or more certificates and add them to the chained list
1779 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001780int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001781{
1782 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 size_t n;
1784 unsigned char *buf;
1785
Paul Bakker2b245eb2009-04-19 18:44:26 +00001786 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 return( 1 );
1788
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 ret = x509parse_crt( chain, buf, (int) n );
1790
1791 memset( buf, 0, n + 1 );
1792 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
1794 return( ret );
1795}
1796
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797/*
1798 * Load one or more CRLs and add them to the chained list
1799 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001800int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001801{
1802 int ret;
1803 size_t n;
1804 unsigned char *buf;
1805
1806 if ( load_file( path, &buf, &n ) )
1807 return( 1 );
1808
1809 ret = x509parse_crl( chain, buf, (int) n );
1810
1811 memset( buf, 0, n + 1 );
1812 free( buf );
1813
1814 return( ret );
1815}
1816
Paul Bakker5121ce52009-01-03 21:22:43 +00001817/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001818 * Load and parse a private RSA key
1819 */
1820int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1821{
1822 int ret;
1823 size_t n;
1824 unsigned char *buf;
1825
1826 if ( load_file( path, &buf, &n ) )
1827 return( 1 );
1828
1829 if( pwd == NULL )
1830 ret = x509parse_key( rsa, buf, (int) n, NULL, 0 );
1831 else
1832 ret = x509parse_key( rsa, buf, (int) n,
1833 (unsigned char *) pwd, strlen( pwd ) );
1834
1835 memset( buf, 0, n + 1 );
1836 free( buf );
1837
1838 return( ret );
1839}
1840
1841/*
1842 * Load and parse a public RSA key
1843 */
1844int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1845{
1846 int ret;
1847 size_t n;
1848 unsigned char *buf;
1849
1850 if ( load_file( path, &buf, &n ) )
1851 return( 1 );
1852
1853 ret = x509parse_public_key( rsa, buf, (int) n );
1854
1855 memset( buf, 0, n + 1 );
1856 free( buf );
1857
1858 return( ret );
1859}
1860#endif /* POLARSSL_FS_IO */
1861
1862/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 * Parse a private RSA key
1864 */
Paul Bakker23986e52011-04-24 08:57:21 +00001865int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1866 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867{
Paul Bakker23986e52011-04-24 08:57:21 +00001868 int ret;
1869 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00001871#if defined(POLARSSL_PEM_C)
1872 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Paul Bakker96743fc2011-02-12 14:30:57 +00001874 pem_init( &pem );
1875 ret = pem_read_buffer( &pem,
1876 "-----BEGIN RSA PRIVATE KEY-----",
1877 "-----END RSA PRIVATE KEY-----",
1878 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Paul Bakker96743fc2011-02-12 14:30:57 +00001880 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001882 /*
1883 * Was PEM encoded
1884 */
1885 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001887 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001888 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001889 pem_free( &pem );
1890 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001891 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
Paul Bakker96743fc2011-02-12 14:30:57 +00001893 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1894#else
1895 p = (unsigned char *) key;
1896#endif
1897 end = p + keylen;
1898
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 /*
1900 * RSAPrivateKey ::= SEQUENCE {
1901 * version Version,
1902 * modulus INTEGER, -- n
1903 * publicExponent INTEGER, -- e
1904 * privateExponent INTEGER, -- d
1905 * prime1 INTEGER, -- p
1906 * prime2 INTEGER, -- q
1907 * exponent1 INTEGER, -- d mod (p-1)
1908 * exponent2 INTEGER, -- d mod (q-1)
1909 * coefficient INTEGER, -- (inverse of q) mod p
1910 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1911 * }
1912 */
1913 if( ( ret = asn1_get_tag( &p, end, &len,
1914 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1915 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001916#if defined(POLARSSL_PEM_C)
1917 pem_free( &pem );
1918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 rsa_free( rsa );
Paul Bakker40e46942009-01-03 21:51:57 +00001920 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 }
1922
1923 end = p + len;
1924
1925 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1926 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001927#if defined(POLARSSL_PEM_C)
1928 pem_free( &pem );
1929#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 rsa_free( rsa );
Paul Bakker40e46942009-01-03 21:51:57 +00001931 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 }
1933
1934 if( rsa->ver != 0 )
1935 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001936#if defined(POLARSSL_PEM_C)
1937 pem_free( &pem );
1938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 rsa_free( rsa );
Paul Bakker40e46942009-01-03 21:51:57 +00001940 return( ret | POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
1942
1943 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
1944 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
1945 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
1946 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
1947 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
1948 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
1949 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
1950 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
1951 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001952#if defined(POLARSSL_PEM_C)
1953 pem_free( &pem );
1954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 rsa_free( rsa );
Paul Bakker40e46942009-01-03 21:51:57 +00001956 return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 }
1958
1959 rsa->len = mpi_size( &rsa->N );
1960
1961 if( p != end )
1962 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001963#if defined(POLARSSL_PEM_C)
1964 pem_free( &pem );
1965#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 rsa_free( rsa );
Paul Bakker40e46942009-01-03 21:51:57 +00001967 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
1968 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
1970
1971 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
1972 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001973#if defined(POLARSSL_PEM_C)
1974 pem_free( &pem );
1975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 rsa_free( rsa );
1977 return( ret );
1978 }
1979
Paul Bakker96743fc2011-02-12 14:30:57 +00001980#if defined(POLARSSL_PEM_C)
1981 pem_free( &pem );
1982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001983
1984 return( 0 );
1985}
1986
1987/*
Paul Bakker53019ae2011-03-25 13:58:48 +00001988 * Parse a public RSA key
1989 */
Paul Bakker23986e52011-04-24 08:57:21 +00001990int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00001991{
Paul Bakker23986e52011-04-24 08:57:21 +00001992 int ret;
1993 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00001994 unsigned char *p, *end;
1995 x509_buf alg_oid;
1996#if defined(POLARSSL_PEM_C)
1997 pem_context pem;
1998
1999 pem_init( &pem );
2000 ret = pem_read_buffer( &pem,
2001 "-----BEGIN PUBLIC KEY-----",
2002 "-----END PUBLIC KEY-----",
2003 key, NULL, 0, &len );
2004
2005 if( ret == 0 )
2006 {
2007 /*
2008 * Was PEM encoded
2009 */
2010 keylen = pem.buflen;
2011 }
2012 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2013 {
2014 pem_free( &pem );
2015 return( ret );
2016 }
2017
2018 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2019#else
2020 p = (unsigned char *) key;
2021#endif
2022 end = p + keylen;
2023
2024 /*
2025 * PublicKeyInfo ::= SEQUENCE {
2026 * algorithm AlgorithmIdentifier,
2027 * PublicKey BIT STRING
2028 * }
2029 *
2030 * AlgorithmIdentifier ::= SEQUENCE {
2031 * algorithm OBJECT IDENTIFIER,
2032 * parameters ANY DEFINED BY algorithm OPTIONAL
2033 * }
2034 *
2035 * RSAPublicKey ::= SEQUENCE {
2036 * modulus INTEGER, -- n
2037 * publicExponent INTEGER -- e
2038 * }
2039 */
2040
2041 if( ( ret = asn1_get_tag( &p, end, &len,
2042 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2043 {
2044#if defined(POLARSSL_PEM_C)
2045 pem_free( &pem );
2046#endif
2047 rsa_free( rsa );
2048 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT | ret );
2049 }
2050
2051 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2052 {
2053#if defined(POLARSSL_PEM_C)
2054 pem_free( &pem );
2055#endif
2056 rsa_free( rsa );
2057 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
2058 }
2059
2060 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2061 {
2062#if defined(POLARSSL_PEM_C)
2063 pem_free( &pem );
2064#endif
2065 rsa_free( rsa );
2066 return( ret );
2067 }
2068
2069 rsa->len = mpi_size( &rsa->N );
2070
2071#if defined(POLARSSL_PEM_C)
2072 pem_free( &pem );
2073#endif
2074
2075 return( 0 );
2076}
2077
Paul Bakkereaa89f82011-04-04 21:36:15 +00002078#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002079/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002080 * Parse DHM parameters
2081 */
Paul Bakker23986e52011-04-24 08:57:21 +00002082int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002083{
Paul Bakker23986e52011-04-24 08:57:21 +00002084 int ret;
2085 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002086 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002087#if defined(POLARSSL_PEM_C)
2088 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002089
Paul Bakker96743fc2011-02-12 14:30:57 +00002090 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002091
Paul Bakker96743fc2011-02-12 14:30:57 +00002092 ret = pem_read_buffer( &pem,
2093 "-----BEGIN DH PARAMETERS-----",
2094 "-----END DH PARAMETERS-----",
2095 dhmin, NULL, 0, &dhminlen );
2096
2097 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002098 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002099 /*
2100 * Was PEM encoded
2101 */
2102 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002103 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002104 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002105 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002106 pem_free( &pem );
2107 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002108 }
2109
Paul Bakker96743fc2011-02-12 14:30:57 +00002110 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2111#else
2112 p = (unsigned char *) dhmin;
2113#endif
2114 end = p + dhminlen;
2115
Paul Bakker1b57b062011-01-06 15:48:19 +00002116 memset( dhm, 0, sizeof( dhm_context ) );
2117
Paul Bakker1b57b062011-01-06 15:48:19 +00002118 /*
2119 * DHParams ::= SEQUENCE {
2120 * prime INTEGER, -- P
2121 * generator INTEGER, -- g
2122 * }
2123 */
2124 if( ( ret = asn1_get_tag( &p, end, &len,
2125 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2126 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002127#if defined(POLARSSL_PEM_C)
2128 pem_free( &pem );
2129#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002130 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
2131 }
2132
2133 end = p + len;
2134
2135 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2136 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2137 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002138#if defined(POLARSSL_PEM_C)
2139 pem_free( &pem );
2140#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002141 dhm_free( dhm );
2142 return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2143 }
2144
2145 if( p != end )
2146 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002147#if defined(POLARSSL_PEM_C)
2148 pem_free( &pem );
2149#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002150 dhm_free( dhm );
2151 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
2152 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2153 }
2154
Paul Bakker96743fc2011-02-12 14:30:57 +00002155#if defined(POLARSSL_PEM_C)
2156 pem_free( &pem );
2157#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002158
2159 return( 0 );
2160}
2161
Paul Bakker335db3f2011-04-25 15:28:35 +00002162#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002163/*
2164 * Load and parse a private RSA key
2165 */
2166int x509parse_dhmfile( dhm_context *dhm, const char *path )
2167{
2168 int ret;
2169 size_t n;
2170 unsigned char *buf;
2171
2172 if ( load_file( path, &buf, &n ) )
2173 return( 1 );
2174
2175 ret = x509parse_dhm( dhm, buf, (int) n);
2176
2177 memset( buf, 0, n + 1 );
2178 free( buf );
2179
2180 return( ret );
2181}
Paul Bakker335db3f2011-04-25 15:28:35 +00002182#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002183#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002184
Paul Bakker5121ce52009-01-03 21:22:43 +00002185#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002186#include <stdarg.h>
2187
2188#if !defined vsnprintf
2189#define vsnprintf _vsnprintf
2190#endif // vsnprintf
2191
2192/*
2193 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2194 * Result value is not size of buffer needed, but -1 if no fit is possible.
2195 *
2196 * This fuction tries to 'fix' this by at least suggesting enlarging the
2197 * size by 20.
2198 */
2199int compat_snprintf(char *str, size_t size, const char *format, ...)
2200{
2201 va_list ap;
2202 int res = -1;
2203
2204 va_start( ap, format );
2205
2206 res = vsnprintf( str, size, format, ap );
2207
2208 va_end( ap );
2209
2210 // No quick fix possible
2211 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002212 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002213
2214 return res;
2215}
2216
2217#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002218#endif
2219
Paul Bakkerd98030e2009-05-02 15:13:40 +00002220#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2221
2222#define SAFE_SNPRINTF() \
2223{ \
2224 if( ret == -1 ) \
2225 return( -1 ); \
2226 \
Paul Bakker23986e52011-04-24 08:57:21 +00002227 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002228 p[n - 1] = '\0'; \
2229 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2230 } \
2231 \
Paul Bakker23986e52011-04-24 08:57:21 +00002232 n -= (unsigned int) ret; \
2233 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002234}
2235
Paul Bakker5121ce52009-01-03 21:22:43 +00002236/*
2237 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002238 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002240int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241{
Paul Bakker23986e52011-04-24 08:57:21 +00002242 int ret;
2243 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002245 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002246 char s[128], *p;
2247
2248 memset( s, 0, sizeof( s ) );
2249
2250 name = dn;
2251 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002252 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002253
2254 while( name != NULL )
2255 {
Paul Bakker74111d32011-01-15 16:57:55 +00002256 if( name != dn )
2257 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002258 ret = snprintf( p, n, ", " );
2259 SAFE_SNPRINTF();
2260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
2262 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2263 {
2264 switch( name->oid.p[2] )
2265 {
2266 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002267 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
2269 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002270 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002271
2272 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002273 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
2275 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002276 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
2278 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002279 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002282 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
2284 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002285 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 name->oid.p[2] );
2287 break;
2288 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002289 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
2291 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2292 {
2293 switch( name->oid.p[8] )
2294 {
2295 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002296 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
2298 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002299 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 name->oid.p[8] );
2301 break;
2302 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002303 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305 else
Paul Bakker74111d32011-01-15 16:57:55 +00002306 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002307 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002308 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002310
2311 for( i = 0; i < name->val.len; i++ )
2312 {
2313 if( i >= (int) sizeof( s ) - 1 )
2314 break;
2315
2316 c = name->val.p[i];
2317 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2318 s[i] = '?';
2319 else s[i] = c;
2320 }
2321 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002322 ret = snprintf( p, n, "%s", s );
2323 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002324 name = name->next;
2325 }
2326
Paul Bakker23986e52011-04-24 08:57:21 +00002327 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328}
2329
2330/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002331 * Store the serial in printable form into buf; no more
2332 * than size characters will be written
2333 */
2334int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2335{
Paul Bakker23986e52011-04-24 08:57:21 +00002336 int ret;
2337 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002338 char *p;
2339
2340 p = buf;
2341 n = size;
2342
2343 nr = ( serial->len <= 32 )
2344 ? serial->len : 32;
2345
2346 for( i = 0; i < nr; i++ )
2347 {
2348 ret = snprintf( p, n, "%02X%s",
2349 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2350 SAFE_SNPRINTF();
2351 }
2352
Paul Bakker23986e52011-04-24 08:57:21 +00002353 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002354}
2355
2356/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002357 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002359int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2360 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002361{
Paul Bakker23986e52011-04-24 08:57:21 +00002362 int ret;
2363 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002364 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002365
2366 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002367 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
Paul Bakkerd98030e2009-05-02 15:13:40 +00002369 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 SAFE_SNPRINTF();
2372 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002374 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Paul Bakkerdd476992011-01-16 21:34:59 +00002376 ret = x509parse_serial_gets( p, n, &crt->serial);
2377 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Paul Bakkerd98030e2009-05-02 15:13:40 +00002379 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2380 SAFE_SNPRINTF();
2381 ret = x509parse_dn_gets( p, n, &crt->issuer );
2382 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Paul Bakkerd98030e2009-05-02 15:13:40 +00002384 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2385 SAFE_SNPRINTF();
2386 ret = x509parse_dn_gets( p, n, &crt->subject );
2387 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Paul Bakkerd98030e2009-05-02 15:13:40 +00002389 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2391 crt->valid_from.year, crt->valid_from.mon,
2392 crt->valid_from.day, crt->valid_from.hour,
2393 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002394 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2398 crt->valid_to.year, crt->valid_to.mon,
2399 crt->valid_to.day, crt->valid_to.hour,
2400 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002401 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Paul Bakkerd98030e2009-05-02 15:13:40 +00002403 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2404 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Paul Bakker27d66162010-03-17 06:56:01 +00002406 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2409 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2410 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2411 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2412 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2413 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2414 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2415 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2416 default: ret = snprintf( p, n, "???" ); break;
2417 }
2418 SAFE_SNPRINTF();
2419
2420 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002421 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002422 SAFE_SNPRINTF();
2423
Paul Bakker23986e52011-04-24 08:57:21 +00002424 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002425}
2426
Paul Bakker74111d32011-01-15 16:57:55 +00002427/* Compare a given OID string with an OID x509_buf * */
2428#define OID_CMP(oid_str, oid_buf) \
2429 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2430 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2431
2432/*
2433 * Return an informational string describing the given OID
2434 */
2435const char *x509_oid_get_description( x509_buf *oid )
2436{
2437 if ( oid == NULL )
2438 return ( NULL );
2439
2440 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2441 return( STRING_SERVER_AUTH );
2442
2443 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2444 return( STRING_CLIENT_AUTH );
2445
2446 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2447 return( STRING_CODE_SIGNING );
2448
2449 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2450 return( STRING_EMAIL_PROTECTION );
2451
2452 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2453 return( STRING_TIME_STAMPING );
2454
2455 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2456 return( STRING_OCSP_SIGNING );
2457
2458 return( NULL );
2459}
2460
2461/* Return the x.y.z.... style numeric string for the given OID */
2462int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2463{
Paul Bakker23986e52011-04-24 08:57:21 +00002464 int ret;
2465 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002466 unsigned int value;
2467 char *p;
2468
2469 p = buf;
2470 n = size;
2471
2472 /* First byte contains first two dots */
2473 if( oid->len > 0 )
2474 {
2475 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2476 SAFE_SNPRINTF();
2477 }
2478
2479 /* TODO: value can overflow in value. */
2480 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002481 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002482 {
2483 value <<= 7;
2484 value += oid->p[i] & 0x7F;
2485
2486 if( !( oid->p[i] & 0x80 ) )
2487 {
2488 /* Last byte */
2489 ret = snprintf( p, n, ".%d", value );
2490 SAFE_SNPRINTF();
2491 value = 0;
2492 }
2493 }
2494
Paul Bakker23986e52011-04-24 08:57:21 +00002495 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002496}
2497
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498/*
2499 * Return an informational string about the CRL.
2500 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002501int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2502 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503{
Paul Bakker23986e52011-04-24 08:57:21 +00002504 int ret;
2505 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002507 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002508
2509 p = buf;
2510 n = size;
2511
2512 ret = snprintf( p, n, "%sCRL version : %d",
2513 prefix, crl->version );
2514 SAFE_SNPRINTF();
2515
2516 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2517 SAFE_SNPRINTF();
2518 ret = x509parse_dn_gets( p, n, &crl->issuer );
2519 SAFE_SNPRINTF();
2520
2521 ret = snprintf( p, n, "\n%sthis update : " \
2522 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2523 crl->this_update.year, crl->this_update.mon,
2524 crl->this_update.day, crl->this_update.hour,
2525 crl->this_update.min, crl->this_update.sec );
2526 SAFE_SNPRINTF();
2527
2528 ret = snprintf( p, n, "\n%snext update : " \
2529 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2530 crl->next_update.year, crl->next_update.mon,
2531 crl->next_update.day, crl->next_update.hour,
2532 crl->next_update.min, crl->next_update.sec );
2533 SAFE_SNPRINTF();
2534
2535 entry = &crl->entry;
2536
2537 ret = snprintf( p, n, "\n%sRevoked certificates:",
2538 prefix );
2539 SAFE_SNPRINTF();
2540
Paul Bakker9be19372009-07-27 20:21:53 +00002541 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002542 {
2543 ret = snprintf( p, n, "\n%sserial number: ",
2544 prefix );
2545 SAFE_SNPRINTF();
2546
2547 nr = ( entry->serial.len <= 32 )
2548 ? entry->serial.len : 32;
2549
Paul Bakker74111d32011-01-15 16:57:55 +00002550 for( i = 0; i < nr; i++ )
2551 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002552 ret = snprintf( p, n, "%02X%s",
2553 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2554 SAFE_SNPRINTF();
2555 }
2556
2557 ret = snprintf( p, n, " revocation date: " \
2558 "%04d-%02d-%02d %02d:%02d:%02d",
2559 entry->revocation_date.year, entry->revocation_date.mon,
2560 entry->revocation_date.day, entry->revocation_date.hour,
2561 entry->revocation_date.min, entry->revocation_date.sec );
2562 SAFE_SNPRINTF();
2563
2564 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 }
2566
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2568 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Paul Bakker27d66162010-03-17 06:56:01 +00002570 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002571 {
2572 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2573 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2574 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2575 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2576 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2577 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2578 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2579 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2580 default: ret = snprintf( p, n, "???" ); break;
2581 }
2582 SAFE_SNPRINTF();
2583
Paul Bakker1e27bb22009-07-19 20:25:25 +00002584 ret = snprintf( p, n, "\n" );
2585 SAFE_SNPRINTF();
2586
Paul Bakker23986e52011-04-24 08:57:21 +00002587 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588}
2589
2590/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002591 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002593int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594{
2595 struct tm *lt;
2596 time_t tt;
2597
2598 tt = time( NULL );
2599 lt = localtime( &tt );
2600
Paul Bakker40ea7de2009-05-03 10:18:48 +00002601 if( lt->tm_year > to->year - 1900 )
2602 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Paul Bakker40ea7de2009-05-03 10:18:48 +00002604 if( lt->tm_year == to->year - 1900 &&
2605 lt->tm_mon > to->mon - 1 )
2606 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Paul Bakker40ea7de2009-05-03 10:18:48 +00002608 if( lt->tm_year == to->year - 1900 &&
2609 lt->tm_mon == to->mon - 1 &&
2610 lt->tm_mday > to->day )
2611 return( 1 );
2612
Paul Bakkerb6194992011-01-16 21:40:22 +00002613 if( lt->tm_year == to->year - 1900 &&
2614 lt->tm_mon == to->mon - 1 &&
2615 lt->tm_mday == to->day &&
2616 lt->tm_hour > to->hour - 1)
2617 return( 1 );
2618
2619 if( lt->tm_year == to->year - 1900 &&
2620 lt->tm_mon == to->mon - 1 &&
2621 lt->tm_mday == to->day &&
2622 lt->tm_hour == to->hour - 1 &&
2623 lt->tm_min > to->min - 1 )
2624 return( 1 );
2625
2626 if( lt->tm_year == to->year - 1900 &&
2627 lt->tm_mon == to->mon - 1 &&
2628 lt->tm_mday == to->day &&
2629 lt->tm_hour == to->hour - 1 &&
2630 lt->tm_min == to->min - 1 &&
2631 lt->tm_sec > to->sec - 1 )
2632 return( 1 );
2633
Paul Bakker40ea7de2009-05-03 10:18:48 +00002634 return( 0 );
2635}
2636
2637/*
2638 * Return 1 if the certificate is revoked, or 0 otherwise.
2639 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002640int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002641{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002642 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002643
2644 while( cur != NULL && cur->serial.len != 0 )
2645 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002646 if( crt->serial.len == cur->serial.len &&
2647 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002648 {
2649 if( x509parse_time_expired( &cur->revocation_date ) )
2650 return( 1 );
2651 }
2652
2653 cur = cur->next;
2654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655
2656 return( 0 );
2657}
2658
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002659/*
2660 * Wrapper for x509 hashes.
2661 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002662 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002663 */
Paul Bakker23986e52011-04-24 08:57:21 +00002664static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 unsigned char *out )
2666{
2667 switch( alg )
2668 {
Paul Bakker40e46942009-01-03 21:51:57 +00002669#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002670 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002672#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002673 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002675#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002676 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002677#endif
2678#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002679 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002680#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002681#if defined(POLARSSL_SHA2_C)
2682 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2683 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2684#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002685#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002686 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2687 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002690 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 break;
2692 }
2693}
2694
2695/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002696 * Check that the given certificate is valid accoring to the CRL.
2697 */
2698static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2699 x509_crl *crl_list)
2700{
2701 int flags = 0;
2702 int hash_id;
2703 unsigned char hash[64];
2704
2705 /*
2706 * TODO: What happens if no CRL is present?
2707 * Suggestion: Revocation state should be unknown if no CRL is present.
2708 * For backwards compatibility this is not yet implemented.
2709 */
2710
2711 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2712 {
2713 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2714 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2715 crl_list->issuer_raw.len ) != 0 )
2716 {
2717 crl_list = crl_list->next;
2718 continue;
2719 }
2720
2721 /*
2722 * Check if CRL is correctly signed by the trusted CA
2723 */
2724 hash_id = crl_list->sig_alg;
2725
2726 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2727
2728 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2729 0, hash, crl_list->sig.p ) == 0 )
2730 {
2731 /*
2732 * CRL is not trusted
2733 */
2734 flags |= BADCRL_NOT_TRUSTED;
2735 break;
2736 }
2737
2738 /*
2739 * Check for validity of CRL (Do not drop out)
2740 */
2741 if( x509parse_time_expired( &crl_list->next_update ) )
2742 flags |= BADCRL_EXPIRED;
2743
2744 /*
2745 * Check if certificate is revoked
2746 */
2747 if( x509parse_revoked(crt, crl_list) )
2748 {
2749 flags |= BADCERT_REVOKED;
2750 break;
2751 }
2752
2753 crl_list = crl_list->next;
2754 }
2755 return flags;
2756}
2757
2758/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 * Verify the certificate validity
2760 */
2761int x509parse_verify( x509_cert *crt,
2762 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002763 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002764 const char *cn, int *flags,
2765 int (*f_vrfy)(void *, x509_cert *, int, int),
2766 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767{
Paul Bakker23986e52011-04-24 08:57:21 +00002768 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 int hash_id;
2770 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002771 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002773 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakker40ea7de2009-05-03 10:18:48 +00002775 *flags = 0;
2776
2777 if( x509parse_time_expired( &crt->valid_to ) )
2778 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
2780 if( cn != NULL )
2781 {
2782 name = &crt->subject;
2783 cn_len = strlen( cn );
2784
2785 while( name != NULL )
2786 {
2787 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2788 memcmp( name->val.p, cn, cn_len ) == 0 &&
2789 name->val.len == cn_len )
2790 break;
2791
2792 name = name->next;
2793 }
2794
2795 if( name == NULL )
2796 *flags |= BADCERT_CN_MISMATCH;
2797 }
2798
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 /*
2800 * Iterate upwards in the given cert chain,
2801 * ignoring any upper cert with CA != TRUE.
2802 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002803 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 pathlen = 1;
2806
Paul Bakker76fd75a2011-01-16 21:12:10 +00002807 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002809 if( parent->ca_istrue == 0 ||
2810 crt->issuer_raw.len != parent->subject_raw.len ||
2811 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 crt->issuer_raw.len ) != 0 )
2813 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002814 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 continue;
2816 }
2817
Paul Bakker27d66162010-03-17 06:56:01 +00002818 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
2820 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2821
Paul Bakker76fd75a2011-01-16 21:12:10 +00002822 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2823 crt->sig.p ) != 0 )
2824 *flags |= BADCERT_NOT_TRUSTED;
2825
2826 /* Check trusted CA's CRL for the given crt */
2827 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002828
2829 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002830 if( NULL != f_vrfy )
2831 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002832 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002833 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002834 else
2835 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002836 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002837 else if( *flags != 0 )
2838 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
2840 pathlen++;
2841
Paul Bakker76fd75a2011-01-16 21:12:10 +00002842 crt = parent;
2843 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 }
2845
2846 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002847 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002849 *flags |= BADCERT_NOT_TRUSTED;
2850
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002851 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 {
2853 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2854 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2855 crt->issuer_raw.len ) != 0 )
2856 {
2857 trust_ca = trust_ca->next;
2858 continue;
2859 }
2860
2861 if( trust_ca->max_pathlen > 0 &&
2862 trust_ca->max_pathlen < pathlen )
2863 break;
2864
Paul Bakker27d66162010-03-17 06:56:01 +00002865 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
2867 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2868
2869 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2870 0, hash, crt->sig.p ) == 0 )
2871 {
2872 /*
2873 * cert. is signed by a trusted CA
2874 */
2875 *flags &= ~BADCERT_NOT_TRUSTED;
2876 break;
2877 }
2878
2879 trust_ca = trust_ca->next;
2880 }
2881
Paul Bakker76fd75a2011-01-16 21:12:10 +00002882 /* Check trusted CA's CRL for the given crt */
2883 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002884
2885 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002886 if( NULL != f_vrfy )
2887 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002888 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002889 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002890 else
2891 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002892 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002893 else if( *flags != 0 )
2894 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002895
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 return( 0 );
2897}
2898
2899/*
2900 * Unallocate all certificate data
2901 */
2902void x509_free( x509_cert *crt )
2903{
2904 x509_cert *cert_cur = crt;
2905 x509_cert *cert_prv;
2906 x509_name *name_cur;
2907 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00002908 x509_sequence *seq_cur;
2909 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
2911 if( crt == NULL )
2912 return;
2913
2914 do
2915 {
2916 rsa_free( &cert_cur->rsa );
2917
2918 name_cur = cert_cur->issuer.next;
2919 while( name_cur != NULL )
2920 {
2921 name_prv = name_cur;
2922 name_cur = name_cur->next;
2923 memset( name_prv, 0, sizeof( x509_name ) );
2924 free( name_prv );
2925 }
2926
2927 name_cur = cert_cur->subject.next;
2928 while( name_cur != NULL )
2929 {
2930 name_prv = name_cur;
2931 name_cur = name_cur->next;
2932 memset( name_prv, 0, sizeof( x509_name ) );
2933 free( name_prv );
2934 }
2935
Paul Bakker74111d32011-01-15 16:57:55 +00002936 seq_cur = cert_cur->ext_key_usage.next;
2937 while( seq_cur != NULL )
2938 {
2939 seq_prv = seq_cur;
2940 seq_cur = seq_cur->next;
2941 memset( seq_prv, 0, sizeof( x509_sequence ) );
2942 free( seq_prv );
2943 }
2944
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 if( cert_cur->raw.p != NULL )
2946 {
2947 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2948 free( cert_cur->raw.p );
2949 }
2950
2951 cert_cur = cert_cur->next;
2952 }
2953 while( cert_cur != NULL );
2954
2955 cert_cur = crt;
2956 do
2957 {
2958 cert_prv = cert_cur;
2959 cert_cur = cert_cur->next;
2960
2961 memset( cert_prv, 0, sizeof( x509_cert ) );
2962 if( cert_prv != crt )
2963 free( cert_prv );
2964 }
2965 while( cert_cur != NULL );
2966}
2967
Paul Bakkerd98030e2009-05-02 15:13:40 +00002968/*
2969 * Unallocate all CRL data
2970 */
2971void x509_crl_free( x509_crl *crl )
2972{
2973 x509_crl *crl_cur = crl;
2974 x509_crl *crl_prv;
2975 x509_name *name_cur;
2976 x509_name *name_prv;
2977 x509_crl_entry *entry_cur;
2978 x509_crl_entry *entry_prv;
2979
2980 if( crl == NULL )
2981 return;
2982
2983 do
2984 {
2985 name_cur = crl_cur->issuer.next;
2986 while( name_cur != NULL )
2987 {
2988 name_prv = name_cur;
2989 name_cur = name_cur->next;
2990 memset( name_prv, 0, sizeof( x509_name ) );
2991 free( name_prv );
2992 }
2993
2994 entry_cur = crl_cur->entry.next;
2995 while( entry_cur != NULL )
2996 {
2997 entry_prv = entry_cur;
2998 entry_cur = entry_cur->next;
2999 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3000 free( entry_prv );
3001 }
3002
3003 if( crl_cur->raw.p != NULL )
3004 {
3005 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3006 free( crl_cur->raw.p );
3007 }
3008
3009 crl_cur = crl_cur->next;
3010 }
3011 while( crl_cur != NULL );
3012
3013 crl_cur = crl;
3014 do
3015 {
3016 crl_prv = crl_cur;
3017 crl_cur = crl_cur->next;
3018
3019 memset( crl_prv, 0, sizeof( x509_crl ) );
3020 if( crl_prv != crl )
3021 free( crl_prv );
3022 }
3023 while( crl_cur != NULL );
3024}
3025
Paul Bakker40e46942009-01-03 21:51:57 +00003026#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker40e46942009-01-03 21:51:57 +00003028#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
3030/*
3031 * Checkup routine
3032 */
3033int x509_self_test( int verbose )
3034{
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003035#if defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
3037 int flags;
3038 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 x509_cert cacert;
3040 x509_cert clicert;
3041 rsa_context rsa;
Paul Bakker1b57b062011-01-06 15:48:19 +00003042 dhm_context dhm;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
3044 if( verbose != 0 )
3045 printf( " X.509 certificate load: " );
3046
3047 memset( &clicert, 0, sizeof( x509_cert ) );
3048
3049 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3050 strlen( test_cli_crt ) );
3051 if( ret != 0 )
3052 {
3053 if( verbose != 0 )
3054 printf( "failed\n" );
3055
3056 return( ret );
3057 }
3058
3059 memset( &cacert, 0, sizeof( x509_cert ) );
3060
3061 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3062 strlen( test_ca_crt ) );
3063 if( ret != 0 )
3064 {
3065 if( verbose != 0 )
3066 printf( "failed\n" );
3067
3068 return( ret );
3069 }
3070
3071 if( verbose != 0 )
3072 printf( "passed\n X.509 private key load: " );
3073
3074 i = strlen( test_ca_key );
3075 j = strlen( test_ca_pwd );
3076
Paul Bakker66b78b22011-03-25 14:22:50 +00003077 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3078
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 if( ( ret = x509parse_key( &rsa,
3080 (unsigned char *) test_ca_key, i,
3081 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3082 {
3083 if( verbose != 0 )
3084 printf( "failed\n" );
3085
3086 return( ret );
3087 }
3088
3089 if( verbose != 0 )
3090 printf( "passed\n X.509 signature verify: ");
3091
Paul Bakker23986e52011-04-24 08:57:21 +00003092 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 if( ret != 0 )
3094 {
Paul Bakker23986e52011-04-24 08:57:21 +00003095 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 if( verbose != 0 )
3097 printf( "failed\n" );
3098
3099 return( ret );
3100 }
3101
3102 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003103 printf( "passed\n X.509 DHM parameter load: " );
3104
3105 i = strlen( test_dhm_params );
3106 j = strlen( test_ca_pwd );
3107
3108 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3109 {
3110 if( verbose != 0 )
3111 printf( "failed\n" );
3112
3113 return( ret );
3114 }
3115
3116 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 printf( "passed\n\n" );
3118
3119 x509_free( &cacert );
3120 x509_free( &clicert );
3121 rsa_free( &rsa );
Paul Bakker1b57b062011-01-06 15:48:19 +00003122 dhm_free( &dhm );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123
3124 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003125#else
3126 ((void) verbose);
3127 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003129}
3130
3131#endif
3132
3133#endif