blob: c5da0fc34fe5edcc5014e5ccc4322319122cac12 [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 }
95 }
96
Paul Bakker23986e52011-04-24 08:57:21 +000097 if( *len > (size_t) ( end - *p ) )
Paul Bakker40e46942009-01-03 21:51:57 +000098 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 return( 0 );
101}
102
103static int asn1_get_tag( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000104 const unsigned char *end,
Paul Bakker23986e52011-04-24 08:57:21 +0000105 size_t *len, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000106{
107 if( ( end - *p ) < 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000108 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110 if( **p != tag )
Paul Bakker40e46942009-01-03 21:51:57 +0000111 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 (*p)++;
114
115 return( asn1_get_len( p, end, len ) );
116}
117
118static int asn1_get_bool( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000119 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 int *val )
121{
Paul Bakker23986e52011-04-24 08:57:21 +0000122 int ret;
123 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
126 return( ret );
127
128 if( len != 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000129 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131 *val = ( **p != 0 ) ? 1 : 0;
132 (*p)++;
133
134 return( 0 );
135}
136
137static int asn1_get_int( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 int *val )
140{
Paul Bakker23986e52011-04-24 08:57:21 +0000141 int ret;
142 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
145 return( ret );
146
Paul Bakker27fdf462011-06-09 13:55:13 +0000147 if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000148 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 *val = 0;
151
152 while( len-- > 0 )
153 {
154 *val = ( *val << 8 ) | **p;
155 (*p)++;
156 }
157
158 return( 0 );
159}
160
161static int asn1_get_mpi( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000162 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 mpi *X )
164{
Paul Bakker23986e52011-04-24 08:57:21 +0000165 int ret;
166 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
169 return( ret );
170
171 ret = mpi_read_binary( X, *p, len );
172
173 *p += len;
174
175 return( ret );
176}
177
Paul Bakker74111d32011-01-15 16:57:55 +0000178static int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
179 x509_bitstring *bs)
180{
181 int ret;
182
183 /* Certificate type is a single byte bitstring */
184 if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
185 return( ret );
186
187 /* Check length, subtract one for actual bit string length */
188 if ( bs->len < 1 )
189 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
190 bs->len -= 1;
191
192 /* Get number of unused bits, ensure unused bits <= 7 */
193 bs->unused_bits = **p;
194 if( bs->unused_bits > 7 )
195 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
196 (*p)++;
197
198 /* Get actual bitstring */
199 bs->p = *p;
200 *p += bs->len;
201
202 if( *p != end )
203 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
204
205 return 0;
206}
207
208
209/*
210 * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
211 */
212static int asn1_get_sequence_of( unsigned char **p,
213 const unsigned char *end,
214 x509_sequence *cur,
215 int tag)
216{
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int ret;
218 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000219 x509_buf *buf;
220
221 /* Get main sequence tag */
222 if( ( ret = asn1_get_tag( p, end, &len,
223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
224 return( ret );
225
226 if( *p + len != end )
227 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
228
229 while( *p < end )
230 {
231 buf = &(cur->buf);
232 buf->tag = **p;
233
234 if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
235 return( ret );
236
237 buf->p = *p;
238 *p += buf->len;
239
240 /* Allocate and assign next pointer */
241 if (*p < end)
242 {
243 cur->next = (x509_sequence *) malloc(
244 sizeof( x509_sequence ) );
245
246 if( cur->next == NULL )
247 return( 1 );
248
249 cur = cur->next;
250 }
251 }
252
253 /* Set final sequence entry's next pointer to NULL */
254 cur->next = NULL;
255
256 if( *p != end )
257 return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
258
259 return( 0 );
260}
261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262/*
263 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
264 */
265static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 int *ver )
268{
Paul Bakker23986e52011-04-24 08:57:21 +0000269 int ret;
270 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272 if( ( ret = asn1_get_tag( p, end, &len,
273 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
274 {
Paul Bakker40e46942009-01-03 21:51:57 +0000275 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 return( *ver = 0 );
277
278 return( ret );
279 }
280
281 end = *p + len;
282
283 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000284 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000287 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000288 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290 return( 0 );
291}
292
293/*
294 * CertificateSerialNumber ::= INTEGER
295 */
296static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000297 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 x509_buf *serial )
299{
300 int ret;
301
302 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000303 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000304 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
307 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000308 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000309 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 serial->tag = *(*p)++;
312
313 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000314 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 serial->p = *p;
317 *p += serial->len;
318
319 return( 0 );
320}
321
322/*
323 * AlgorithmIdentifier ::= SEQUENCE {
324 * algorithm OBJECT IDENTIFIER,
325 * parameters ANY DEFINED BY algorithm OPTIONAL }
326 */
327static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000328 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 x509_buf *alg )
330{
Paul Bakker23986e52011-04-24 08:57:21 +0000331 int ret;
332 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334 if( ( ret = asn1_get_tag( p, end, &len,
335 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000336 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338 end = *p + len;
339 alg->tag = **p;
340
341 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000342 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344 alg->p = *p;
345 *p += alg->len;
346
347 if( *p == end )
348 return( 0 );
349
350 /*
351 * assume the algorithm parameters must be NULL
352 */
353 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000354 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000357 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000358 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
360 return( 0 );
361}
362
363/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 * AttributeTypeAndValue ::= SEQUENCE {
365 * type AttributeType,
366 * value AttributeValue }
367 *
368 * AttributeType ::= OBJECT IDENTIFIER
369 *
370 * AttributeValue ::= ANY DEFINED BY AttributeType
371 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000372static int x509_get_attr_type_value( unsigned char **p,
373 const unsigned char *end,
374 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000375{
Paul Bakker23986e52011-04-24 08:57:21 +0000376 int ret;
377 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 x509_buf *oid;
379 x509_buf *val;
380
381 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000383 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 oid = &cur->oid;
386 oid->tag = **p;
387
388 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000389 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 oid->p = *p;
392 *p += oid->len;
393
394 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000395 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000396 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
399 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
400 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000401 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000402 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404 val = &cur->val;
405 val->tag = *(*p)++;
406
407 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000408 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 val->p = *p;
411 *p += val->len;
412
413 cur->next = NULL;
414
Paul Bakker400ff6f2011-02-20 10:40:16 +0000415 return( 0 );
416}
417
418/*
419 * RelativeDistinguishedName ::=
420 * SET OF AttributeTypeAndValue
421 *
422 * AttributeTypeAndValue ::= SEQUENCE {
423 * type AttributeType,
424 * value AttributeValue }
425 *
426 * AttributeType ::= OBJECT IDENTIFIER
427 *
428 * AttributeValue ::= ANY DEFINED BY AttributeType
429 */
430static int x509_get_name( unsigned char **p,
431 const unsigned char *end,
432 x509_name *cur )
433{
Paul Bakker23986e52011-04-24 08:57:21 +0000434 int ret;
435 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000436 const unsigned char *end2;
437 x509_name *use;
438
439 if( ( ret = asn1_get_tag( p, end, &len,
440 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000441 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000442
443 end2 = end;
444 end = *p + len;
445 use = cur;
446
447 do
448 {
449 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
450 return( ret );
451
452 if( *p != end )
453 {
454 use->next = (x509_name *) malloc(
455 sizeof( x509_name ) );
456
457 if( use->next == NULL )
458 return( 1 );
459
460 memset( use->next, 0, sizeof( x509_name ) );
461
462 use = use->next;
463 }
464 }
465 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 /*
468 * recurse until end of SEQUENCE is reached
469 */
470 if( *p == end2 )
471 return( 0 );
472
473 cur->next = (x509_name *) malloc(
474 sizeof( x509_name ) );
475
476 if( cur->next == NULL )
477 return( 1 );
478
479 return( x509_get_name( p, end2, cur->next ) );
480}
481
482/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * Time ::= CHOICE {
484 * utcTime UTCTime,
485 * generalTime GeneralizedTime }
486 */
Paul Bakker91200182010-02-18 21:26:15 +0000487static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000488 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000489 x509_time *time )
490{
Paul Bakker23986e52011-04-24 08:57:21 +0000491 int ret;
492 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000493 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000494 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000495
Paul Bakker91200182010-02-18 21:26:15 +0000496 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000497 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
498 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000508 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 ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000511 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
512 len : 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000532 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000533
534 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000535 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
536 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000537
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
Paul Bakker9d781402011-05-09 16:17:09 +0000548 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 Bakker9d781402011-05-09 16:17:09 +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 Bakker9d781402011-05-09 16:17:09 +0000578 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000579 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 Bakker9d781402011-05-09 16:17:09 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000632 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 Bakker9d781402011-05-09 16:17:09 +0000647 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000650 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000651 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 Bakker9d781402011-05-09 16:17:09 +0000655 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000658 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000659 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 Bakker9d781402011-05-09 16:17:09 +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 Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000753 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000754 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000782
783 *p += len;
784 }
785
786 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000788 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000811 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000812
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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000832 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000833
834 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000836 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000852
853 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000855 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000871
872 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000874 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000893 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000894
895 /* Sequence length must be >= 1 */
896 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000898 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 Bakker9d781402011-05-09 16:17:09 +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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000947 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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000954 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 ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000959 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 Bakker9d781402011-05-09 16:17:09 +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 )
Paul Bakker9d781402011-05-09 16:17:09 +0000969 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000970 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 */
Paul Bakker9d781402011-05-09 16:17:09 +00001019 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001020 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1021 }
1022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 }
1024
1025 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001026 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001027 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;
Paul Bakker5690efc2011-05-26 13:16:06 +00001126 size_t 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;
Paul Bakker5690efc2011-05-26 13:16:06 +00001131 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 crt = chain;
1135
Paul Bakker320a4b52009-03-28 18:52:39 +00001136 /*
1137 * Check for valid input
1138 */
1139 if( crt == NULL || buf == NULL )
1140 return( 1 );
1141
Paul Bakkere9581d62009-03-28 20:29:25 +00001142 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 crt = crt->next;
1144
1145 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001146 * Add new certificate on the end of the chain if needed.
1147 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001148 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001149 {
1150 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1151
Paul Bakker7d06ad22009-05-02 15:53:56 +00001152 if( crt->next == NULL )
1153 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001154 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001155 return( 1 );
1156 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001157
Paul Bakker7d06ad22009-05-02 15:53:56 +00001158 crt = crt->next;
1159 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001160 }
1161
Paul Bakker96743fc2011-02-12 14:30:57 +00001162#if defined(POLARSSL_PEM_C)
1163 pem_init( &pem );
1164 ret = pem_read_buffer( &pem,
1165 "-----BEGIN CERTIFICATE-----",
1166 "-----END CERTIFICATE-----",
1167 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Paul Bakker96743fc2011-02-12 14:30:57 +00001169 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001171 /*
1172 * Was PEM encoded
1173 */
1174 buflen -= use_len;
1175 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
1177 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001178 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001180 p = pem.buf;
1181 pem.buf = NULL;
1182 len = pem.buflen;
1183 pem_free( &pem );
1184 }
1185 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1186 {
1187 pem_free( &pem );
1188 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190 else
1191 {
1192 /*
1193 * nope, copy the raw DER data
1194 */
1195 p = (unsigned char *) malloc( len = buflen );
1196
1197 if( p == NULL )
1198 return( 1 );
1199
1200 memcpy( p, buf, buflen );
1201
1202 buflen = 0;
1203 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001204#else
1205 p = (unsigned char *) malloc( len = buflen );
1206
1207 if( p == NULL )
1208 return( 1 );
1209
1210 memcpy( p, buf, buflen );
1211
1212 buflen = 0;
1213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 crt->raw.p = p;
1216 crt->raw.len = len;
1217 end = p + len;
1218
1219 /*
1220 * Certificate ::= SEQUENCE {
1221 * tbsCertificate TBSCertificate,
1222 * signatureAlgorithm AlgorithmIdentifier,
1223 * signatureValue BIT STRING }
1224 */
1225 if( ( ret = asn1_get_tag( &p, end, &len,
1226 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1227 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001228 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001229 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
Paul Bakker23986e52011-04-24 08:57:21 +00001232 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001234 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001235 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001236 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
1238
1239 /*
1240 * TBSCertificate ::= SEQUENCE {
1241 */
1242 crt->tbs.p = p;
1243
1244 if( ( ret = asn1_get_tag( &p, end, &len,
1245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1246 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001248 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 }
1250
1251 end = p + len;
1252 crt->tbs.len = end - crt->tbs.p;
1253
1254 /*
1255 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1256 *
1257 * CertificateSerialNumber ::= INTEGER
1258 *
1259 * signature AlgorithmIdentifier
1260 */
1261 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1262 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1263 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 crt->version++;
1270
1271 if( crt->version > 3 )
1272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001274 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
Paul Bakker27d66162010-03-17 06:56:01 +00001277 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001279 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001280 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
1283 /*
1284 * issuer Name
1285 */
1286 crt->issuer_raw.p = p;
1287
1288 if( ( ret = asn1_get_tag( &p, end, &len,
1289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
1295 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->issuer_raw.len = p - crt->issuer_raw.p;
1302
1303 /*
1304 * Validity ::= SEQUENCE {
1305 * notBefore Time,
1306 * notAfter Time }
1307 *
1308 */
1309 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1310 &crt->valid_to ) ) != 0 )
1311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 return( ret );
1314 }
1315
1316 /*
1317 * subject Name
1318 */
1319 crt->subject_raw.p = p;
1320
1321 if( ( ret = asn1_get_tag( &p, end, &len,
1322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
1328 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 crt->subject_raw.len = p - crt->subject_raw.p;
1335
1336 /*
1337 * SubjectPublicKeyInfo ::= SEQUENCE
1338 * algorithm AlgorithmIdentifier,
1339 * subjectPublicKey BIT STRING }
1340 */
1341 if( ( ret = asn1_get_tag( &p, end, &len,
1342 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001345 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1349 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1350 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001351 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 return( ret );
1353 }
1354
1355 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 return( ret );
1359 }
1360
1361 crt->rsa.len = mpi_size( &crt->rsa.N );
1362
1363 /*
1364 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1365 * -- If present, version shall be v2 or v3
1366 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1367 * -- If present, version shall be v2 or v3
1368 * extensions [3] EXPLICIT Extensions OPTIONAL
1369 * -- If present, version shall be v3
1370 */
1371 if( crt->version == 2 || crt->version == 3 )
1372 {
1373 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1374 if( ret != 0 )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 return( ret );
1378 }
1379 }
1380
1381 if( crt->version == 2 || crt->version == 3 )
1382 {
1383 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1384 if( ret != 0 )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 return( ret );
1388 }
1389 }
1390
1391 if( crt->version == 3 )
1392 {
Paul Bakker74111d32011-01-15 16:57:55 +00001393 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 if( ret != 0 )
1395 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001396 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 return( ret );
1398 }
1399 }
1400
1401 if( p != end )
1402 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001403 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001404 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001405 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
1408 end = crt->raw.p + crt->raw.len;
1409
1410 /*
1411 * signatureAlgorithm AlgorithmIdentifier,
1412 * signatureValue BIT STRING
1413 */
1414 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1415 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 return( ret );
1418 }
1419
Paul Bakker320a4b52009-03-28 18:52:39 +00001420 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001423 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
1425
1426 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1427 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001428 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 return( ret );
1430 }
1431
1432 if( p != end )
1433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001435 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001436 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
1438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001440 {
1441 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1442
Paul Bakker7d06ad22009-05-02 15:53:56 +00001443 if( crt->next == NULL )
1444 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001445 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 return( 1 );
1447 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001448
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 crt = crt->next;
1450 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001451
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
1455 return( 0 );
1456}
1457
1458/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001459 * Parse one or more CRLs and add them to the chained list
1460 */
Paul Bakker23986e52011-04-24 08:57:21 +00001461int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001462{
Paul Bakker23986e52011-04-24 08:57:21 +00001463 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001464 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001465 unsigned char *p, *end;
1466 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001467#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001468 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001469 pem_context pem;
1470#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001471
1472 crl = chain;
1473
1474 /*
1475 * Check for valid input
1476 */
1477 if( crl == NULL || buf == NULL )
1478 return( 1 );
1479
1480 while( crl->version != 0 && crl->next != NULL )
1481 crl = crl->next;
1482
1483 /*
1484 * Add new CRL on the end of the chain if needed.
1485 */
1486 if ( crl->version != 0 && crl->next == NULL)
1487 {
1488 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1489
Paul Bakker7d06ad22009-05-02 15:53:56 +00001490 if( crl->next == NULL )
1491 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001493 return( 1 );
1494 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001495
Paul Bakker7d06ad22009-05-02 15:53:56 +00001496 crl = crl->next;
1497 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498 }
1499
Paul Bakker96743fc2011-02-12 14:30:57 +00001500#if defined(POLARSSL_PEM_C)
1501 pem_init( &pem );
1502 ret = pem_read_buffer( &pem,
1503 "-----BEGIN X509 CRL-----",
1504 "-----END X509 CRL-----",
1505 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506
Paul Bakker96743fc2011-02-12 14:30:57 +00001507 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001508 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001509 /*
1510 * Was PEM encoded
1511 */
1512 buflen -= use_len;
1513 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001514
1515 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001516 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001517 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001518 p = pem.buf;
1519 pem.buf = NULL;
1520 len = pem.buflen;
1521 pem_free( &pem );
1522 }
1523 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1524 {
1525 pem_free( &pem );
1526 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001527 }
1528 else
1529 {
1530 /*
1531 * nope, copy the raw DER data
1532 */
1533 p = (unsigned char *) malloc( len = buflen );
1534
1535 if( p == NULL )
1536 return( 1 );
1537
1538 memcpy( p, buf, buflen );
1539
1540 buflen = 0;
1541 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001542#else
1543 p = (unsigned char *) malloc( len = buflen );
1544
1545 if( p == NULL )
1546 return( 1 );
1547
1548 memcpy( p, buf, buflen );
1549
1550 buflen = 0;
1551#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552
1553 crl->raw.p = p;
1554 crl->raw.len = len;
1555 end = p + len;
1556
1557 /*
1558 * CertificateList ::= SEQUENCE {
1559 * tbsCertList TBSCertList,
1560 * signatureAlgorithm AlgorithmIdentifier,
1561 * signatureValue BIT STRING }
1562 */
1563 if( ( ret = asn1_get_tag( &p, end, &len,
1564 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1565 {
1566 x509_crl_free( crl );
1567 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1568 }
1569
Paul Bakker23986e52011-04-24 08:57:21 +00001570 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 {
1572 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001573 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1575 }
1576
1577 /*
1578 * TBSCertList ::= SEQUENCE {
1579 */
1580 crl->tbs.p = p;
1581
1582 if( ( ret = asn1_get_tag( &p, end, &len,
1583 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1584 {
1585 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001586 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 }
1588
1589 end = p + len;
1590 crl->tbs.len = end - crl->tbs.p;
1591
1592 /*
1593 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1594 * -- if present, MUST be v2
1595 *
1596 * signature AlgorithmIdentifier
1597 */
1598 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1599 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1600 {
1601 x509_crl_free( crl );
1602 return( ret );
1603 }
1604
1605 crl->version++;
1606
1607 if( crl->version > 2 )
1608 {
1609 x509_crl_free( crl );
1610 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1611 }
1612
Paul Bakker27d66162010-03-17 06:56:01 +00001613 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 {
1615 x509_crl_free( crl );
1616 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1617 }
1618
1619 /*
1620 * issuer Name
1621 */
1622 crl->issuer_raw.p = p;
1623
1624 if( ( ret = asn1_get_tag( &p, end, &len,
1625 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1626 {
1627 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001628 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 }
1630
1631 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1632 {
1633 x509_crl_free( crl );
1634 return( ret );
1635 }
1636
1637 crl->issuer_raw.len = p - crl->issuer_raw.p;
1638
1639 /*
1640 * thisUpdate Time
1641 * nextUpdate Time OPTIONAL
1642 */
Paul Bakker91200182010-02-18 21:26:15 +00001643 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 {
1645 x509_crl_free( crl );
1646 return( ret );
1647 }
1648
Paul Bakker91200182010-02-18 21:26:15 +00001649 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 {
Paul Bakker9d781402011-05-09 16:17:09 +00001651 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001652 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001653 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001654 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001655 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 x509_crl_free( crl );
1657 return( ret );
1658 }
1659 }
1660
1661 /*
1662 * revokedCertificates SEQUENCE OF SEQUENCE {
1663 * userCertificate CertificateSerialNumber,
1664 * revocationDate Time,
1665 * crlEntryExtensions Extensions OPTIONAL
1666 * -- if present, MUST be v2
1667 * } OPTIONAL
1668 */
1669 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1670 {
1671 x509_crl_free( crl );
1672 return( ret );
1673 }
1674
1675 /*
1676 * crlExtensions EXPLICIT Extensions OPTIONAL
1677 * -- if present, MUST be v2
1678 */
1679 if( crl->version == 2 )
1680 {
1681 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1682
1683 if( ret != 0 )
1684 {
1685 x509_crl_free( crl );
1686 return( ret );
1687 }
1688 }
1689
1690 if( p != end )
1691 {
1692 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001693 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1695 }
1696
1697 end = crl->raw.p + crl->raw.len;
1698
1699 /*
1700 * signatureAlgorithm AlgorithmIdentifier,
1701 * signatureValue BIT STRING
1702 */
1703 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
1706 return( ret );
1707 }
1708
1709 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1710 {
1711 x509_crl_free( crl );
1712 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1713 }
1714
1715 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1716 {
1717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720
1721 if( p != end )
1722 {
1723 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001724 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1726 }
1727
1728 if( buflen > 0 )
1729 {
1730 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1731
Paul Bakker7d06ad22009-05-02 15:53:56 +00001732 if( crl->next == NULL )
1733 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001735 return( 1 );
1736 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001737
Paul Bakker7d06ad22009-05-02 15:53:56 +00001738 crl = crl->next;
1739 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740
1741 return( x509parse_crl( crl, buf, buflen ) );
1742 }
1743
1744 return( 0 );
1745}
1746
Paul Bakker335db3f2011-04-25 15:28:35 +00001747#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001748/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001749 * Load all data from a file into a given buffer.
1750 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001751int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001752{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001753 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001754
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 if( ( f = fopen( path, "rb" ) ) == NULL )
1756 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001757
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758 fseek( f, 0, SEEK_END );
1759 *n = (size_t) ftell( f );
1760 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001761
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1763 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001764
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765 if( fread( *buf, 1, *n, f ) != *n )
1766 {
1767 fclose( f );
1768 free( *buf );
1769 return( 1 );
1770 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001771
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001773
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001775
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001777}
1778
1779/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 * Load one or more certificates and add them to the chained list
1781 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001782int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783{
1784 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 size_t n;
1786 unsigned char *buf;
1787
Paul Bakker2b245eb2009-04-19 18:44:26 +00001788 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 return( 1 );
1790
Paul Bakker27fdf462011-06-09 13:55:13 +00001791 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 memset( buf, 0, n + 1 );
1794 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
1796 return( ret );
1797}
1798
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799/*
1800 * Load one or more CRLs and add them to the chained list
1801 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001802int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001803{
1804 int ret;
1805 size_t n;
1806 unsigned char *buf;
1807
1808 if ( load_file( path, &buf, &n ) )
1809 return( 1 );
1810
Paul Bakker27fdf462011-06-09 13:55:13 +00001811 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812
1813 memset( buf, 0, n + 1 );
1814 free( buf );
1815
1816 return( ret );
1817}
1818
Paul Bakker5121ce52009-01-03 21:22:43 +00001819/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001820 * Load and parse a private RSA key
1821 */
1822int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1823{
1824 int ret;
1825 size_t n;
1826 unsigned char *buf;
1827
1828 if ( load_file( path, &buf, &n ) )
1829 return( 1 );
1830
1831 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001832 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001833 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001834 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001835 (unsigned char *) pwd, strlen( pwd ) );
1836
1837 memset( buf, 0, n + 1 );
1838 free( buf );
1839
1840 return( ret );
1841}
1842
1843/*
1844 * Load and parse a public RSA key
1845 */
1846int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1847{
1848 int ret;
1849 size_t n;
1850 unsigned char *buf;
1851
1852 if ( load_file( path, &buf, &n ) )
1853 return( 1 );
1854
Paul Bakker27fdf462011-06-09 13:55:13 +00001855 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001856
1857 memset( buf, 0, n + 1 );
1858 free( buf );
1859
1860 return( ret );
1861}
1862#endif /* POLARSSL_FS_IO */
1863
1864/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 * Parse a private RSA key
1866 */
Paul Bakker23986e52011-04-24 08:57:21 +00001867int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1868 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001869{
Paul Bakker23986e52011-04-24 08:57:21 +00001870 int ret;
1871 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00001873#if defined(POLARSSL_PEM_C)
1874 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Paul Bakker96743fc2011-02-12 14:30:57 +00001876 pem_init( &pem );
1877 ret = pem_read_buffer( &pem,
1878 "-----BEGIN RSA PRIVATE KEY-----",
1879 "-----END RSA PRIVATE KEY-----",
1880 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Paul Bakker96743fc2011-02-12 14:30:57 +00001882 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001884 /*
1885 * Was PEM encoded
1886 */
1887 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001889 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001890 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001891 pem_free( &pem );
1892 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001893 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Paul Bakker96743fc2011-02-12 14:30:57 +00001895 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1896#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001897 ((void) pwd);
1898 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001899 p = (unsigned char *) key;
1900#endif
1901 end = p + keylen;
1902
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 /*
1904 * RSAPrivateKey ::= SEQUENCE {
1905 * version Version,
1906 * modulus INTEGER, -- n
1907 * publicExponent INTEGER, -- e
1908 * privateExponent INTEGER, -- d
1909 * prime1 INTEGER, -- p
1910 * prime2 INTEGER, -- q
1911 * exponent1 INTEGER, -- d mod (p-1)
1912 * exponent2 INTEGER, -- d mod (q-1)
1913 * coefficient INTEGER, -- (inverse of q) mod p
1914 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1915 * }
1916 */
1917 if( ( ret = asn1_get_tag( &p, end, &len,
1918 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1919 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001920#if defined(POLARSSL_PEM_C)
1921 pem_free( &pem );
1922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001924 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 }
1926
1927 end = p + len;
1928
1929 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1930 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001931#if defined(POLARSSL_PEM_C)
1932 pem_free( &pem );
1933#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001935 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 }
1937
1938 if( rsa->ver != 0 )
1939 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001940#if defined(POLARSSL_PEM_C)
1941 pem_free( &pem );
1942#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001944 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 }
1946
1947 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
1948 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
1949 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
1950 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
1951 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
1952 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
1953 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
1954 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
1955 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001956#if defined(POLARSSL_PEM_C)
1957 pem_free( &pem );
1958#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001960 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 }
1962
1963 rsa->len = mpi_size( &rsa->N );
1964
1965 if( p != end )
1966 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001967#if defined(POLARSSL_PEM_C)
1968 pem_free( &pem );
1969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001971 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001972 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 }
1974
1975 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
1976 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001977#if defined(POLARSSL_PEM_C)
1978 pem_free( &pem );
1979#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 rsa_free( rsa );
1981 return( ret );
1982 }
1983
Paul Bakker96743fc2011-02-12 14:30:57 +00001984#if defined(POLARSSL_PEM_C)
1985 pem_free( &pem );
1986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988 return( 0 );
1989}
1990
1991/*
Paul Bakker53019ae2011-03-25 13:58:48 +00001992 * Parse a public RSA key
1993 */
Paul Bakker23986e52011-04-24 08:57:21 +00001994int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00001995{
Paul Bakker23986e52011-04-24 08:57:21 +00001996 int ret;
1997 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00001998 unsigned char *p, *end;
1999 x509_buf alg_oid;
2000#if defined(POLARSSL_PEM_C)
2001 pem_context pem;
2002
2003 pem_init( &pem );
2004 ret = pem_read_buffer( &pem,
2005 "-----BEGIN PUBLIC KEY-----",
2006 "-----END PUBLIC KEY-----",
2007 key, NULL, 0, &len );
2008
2009 if( ret == 0 )
2010 {
2011 /*
2012 * Was PEM encoded
2013 */
2014 keylen = pem.buflen;
2015 }
2016 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2017 {
2018 pem_free( &pem );
2019 return( ret );
2020 }
2021
2022 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2023#else
2024 p = (unsigned char *) key;
2025#endif
2026 end = p + keylen;
2027
2028 /*
2029 * PublicKeyInfo ::= SEQUENCE {
2030 * algorithm AlgorithmIdentifier,
2031 * PublicKey BIT STRING
2032 * }
2033 *
2034 * AlgorithmIdentifier ::= SEQUENCE {
2035 * algorithm OBJECT IDENTIFIER,
2036 * parameters ANY DEFINED BY algorithm OPTIONAL
2037 * }
2038 *
2039 * RSAPublicKey ::= SEQUENCE {
2040 * modulus INTEGER, -- n
2041 * publicExponent INTEGER -- e
2042 * }
2043 */
2044
2045 if( ( ret = asn1_get_tag( &p, end, &len,
2046 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2047 {
2048#if defined(POLARSSL_PEM_C)
2049 pem_free( &pem );
2050#endif
2051 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002052 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002053 }
2054
2055 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2056 {
2057#if defined(POLARSSL_PEM_C)
2058 pem_free( &pem );
2059#endif
2060 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002061 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002062 }
2063
2064 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2065 {
2066#if defined(POLARSSL_PEM_C)
2067 pem_free( &pem );
2068#endif
2069 rsa_free( rsa );
2070 return( ret );
2071 }
2072
2073 rsa->len = mpi_size( &rsa->N );
2074
2075#if defined(POLARSSL_PEM_C)
2076 pem_free( &pem );
2077#endif
2078
2079 return( 0 );
2080}
2081
Paul Bakkereaa89f82011-04-04 21:36:15 +00002082#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002083/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002084 * Parse DHM parameters
2085 */
Paul Bakker23986e52011-04-24 08:57:21 +00002086int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002087{
Paul Bakker23986e52011-04-24 08:57:21 +00002088 int ret;
2089 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002090 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002091#if defined(POLARSSL_PEM_C)
2092 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002093
Paul Bakker96743fc2011-02-12 14:30:57 +00002094 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002095
Paul Bakker96743fc2011-02-12 14:30:57 +00002096 ret = pem_read_buffer( &pem,
2097 "-----BEGIN DH PARAMETERS-----",
2098 "-----END DH PARAMETERS-----",
2099 dhmin, NULL, 0, &dhminlen );
2100
2101 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002102 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002103 /*
2104 * Was PEM encoded
2105 */
2106 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002107 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002108 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002109 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002110 pem_free( &pem );
2111 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002112 }
2113
Paul Bakker96743fc2011-02-12 14:30:57 +00002114 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2115#else
2116 p = (unsigned char *) dhmin;
2117#endif
2118 end = p + dhminlen;
2119
Paul Bakker1b57b062011-01-06 15:48:19 +00002120 memset( dhm, 0, sizeof( dhm_context ) );
2121
Paul Bakker1b57b062011-01-06 15:48:19 +00002122 /*
2123 * DHParams ::= SEQUENCE {
2124 * prime INTEGER, -- P
2125 * generator INTEGER, -- g
2126 * }
2127 */
2128 if( ( ret = asn1_get_tag( &p, end, &len,
2129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2130 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002131#if defined(POLARSSL_PEM_C)
2132 pem_free( &pem );
2133#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002134 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002135 }
2136
2137 end = p + len;
2138
2139 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2140 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2141 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002142#if defined(POLARSSL_PEM_C)
2143 pem_free( &pem );
2144#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002145 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002146 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002147 }
2148
2149 if( p != end )
2150 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002151#if defined(POLARSSL_PEM_C)
2152 pem_free( &pem );
2153#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002154 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002155 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002156 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2157 }
2158
Paul Bakker96743fc2011-02-12 14:30:57 +00002159#if defined(POLARSSL_PEM_C)
2160 pem_free( &pem );
2161#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002162
2163 return( 0 );
2164}
2165
Paul Bakker335db3f2011-04-25 15:28:35 +00002166#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002167/*
2168 * Load and parse a private RSA key
2169 */
2170int x509parse_dhmfile( dhm_context *dhm, const char *path )
2171{
2172 int ret;
2173 size_t n;
2174 unsigned char *buf;
2175
2176 if ( load_file( path, &buf, &n ) )
2177 return( 1 );
2178
Paul Bakker27fdf462011-06-09 13:55:13 +00002179 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002180
2181 memset( buf, 0, n + 1 );
2182 free( buf );
2183
2184 return( ret );
2185}
Paul Bakker335db3f2011-04-25 15:28:35 +00002186#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002187#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002188
Paul Bakker5121ce52009-01-03 21:22:43 +00002189#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002190#include <stdarg.h>
2191
2192#if !defined vsnprintf
2193#define vsnprintf _vsnprintf
2194#endif // vsnprintf
2195
2196/*
2197 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2198 * Result value is not size of buffer needed, but -1 if no fit is possible.
2199 *
2200 * This fuction tries to 'fix' this by at least suggesting enlarging the
2201 * size by 20.
2202 */
2203int compat_snprintf(char *str, size_t size, const char *format, ...)
2204{
2205 va_list ap;
2206 int res = -1;
2207
2208 va_start( ap, format );
2209
2210 res = vsnprintf( str, size, format, ap );
2211
2212 va_end( ap );
2213
2214 // No quick fix possible
2215 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002216 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002217
2218 return res;
2219}
2220
2221#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002222#endif
2223
Paul Bakkerd98030e2009-05-02 15:13:40 +00002224#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2225
2226#define SAFE_SNPRINTF() \
2227{ \
2228 if( ret == -1 ) \
2229 return( -1 ); \
2230 \
Paul Bakker23986e52011-04-24 08:57:21 +00002231 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002232 p[n - 1] = '\0'; \
2233 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2234 } \
2235 \
Paul Bakker23986e52011-04-24 08:57:21 +00002236 n -= (unsigned int) ret; \
2237 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002238}
2239
Paul Bakker5121ce52009-01-03 21:22:43 +00002240/*
2241 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002242 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002244int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245{
Paul Bakker23986e52011-04-24 08:57:21 +00002246 int ret;
2247 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002249 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 char s[128], *p;
2251
2252 memset( s, 0, sizeof( s ) );
2253
2254 name = dn;
2255 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002256 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 while( name != NULL )
2259 {
Paul Bakker74111d32011-01-15 16:57:55 +00002260 if( name != dn )
2261 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002262 ret = snprintf( p, n, ", " );
2263 SAFE_SNPRINTF();
2264 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
2266 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2267 {
2268 switch( name->oid.p[2] )
2269 {
2270 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002271 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002274 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002275
2276 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002277 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002280 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
2282 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002283 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002284
2285 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002286 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002287
2288 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002289 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 name->oid.p[2] );
2291 break;
2292 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002293 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
2295 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2296 {
2297 switch( name->oid.p[8] )
2298 {
2299 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002300 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
2302 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002303 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 name->oid.p[8] );
2305 break;
2306 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002307 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
2309 else
Paul Bakker74111d32011-01-15 16:57:55 +00002310 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002311 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002312 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
2315 for( i = 0; i < name->val.len; i++ )
2316 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002317 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 break;
2319
2320 c = name->val.p[i];
2321 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2322 s[i] = '?';
2323 else s[i] = c;
2324 }
2325 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002326 ret = snprintf( p, n, "%s", s );
2327 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 name = name->next;
2329 }
2330
Paul Bakker23986e52011-04-24 08:57:21 +00002331 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002332}
2333
2334/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002335 * Store the serial in printable form into buf; no more
2336 * than size characters will be written
2337 */
2338int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2339{
Paul Bakker23986e52011-04-24 08:57:21 +00002340 int ret;
2341 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002342 char *p;
2343
2344 p = buf;
2345 n = size;
2346
2347 nr = ( serial->len <= 32 )
2348 ? serial->len : 32;
2349
2350 for( i = 0; i < nr; i++ )
2351 {
2352 ret = snprintf( p, n, "%02X%s",
2353 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2354 SAFE_SNPRINTF();
2355 }
2356
Paul Bakker23986e52011-04-24 08:57:21 +00002357 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002358}
2359
2360/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002361 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002362 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002363int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2364 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002365{
Paul Bakker23986e52011-04-24 08:57:21 +00002366 int ret;
2367 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002368 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
Paul Bakkerd98030e2009-05-02 15:13:40 +00002373 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002375 SAFE_SNPRINTF();
2376 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Paul Bakkerdd476992011-01-16 21:34:59 +00002380 ret = x509parse_serial_gets( p, n, &crt->serial);
2381 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Paul Bakkerd98030e2009-05-02 15:13:40 +00002383 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2384 SAFE_SNPRINTF();
2385 ret = x509parse_dn_gets( p, n, &crt->issuer );
2386 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002387
Paul Bakkerd98030e2009-05-02 15:13:40 +00002388 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2389 SAFE_SNPRINTF();
2390 ret = x509parse_dn_gets( p, n, &crt->subject );
2391 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Paul Bakkerd98030e2009-05-02 15:13:40 +00002393 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2395 crt->valid_from.year, crt->valid_from.mon,
2396 crt->valid_from.day, crt->valid_from.hour,
2397 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002398 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Paul Bakkerd98030e2009-05-02 15:13:40 +00002400 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2402 crt->valid_to.year, crt->valid_to.mon,
2403 crt->valid_to.day, crt->valid_to.hour,
2404 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002405 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Paul Bakkerd98030e2009-05-02 15:13:40 +00002407 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2408 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Paul Bakker27d66162010-03-17 06:56:01 +00002410 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002412 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2413 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2414 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2415 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2416 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2417 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2418 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2419 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2420 default: ret = snprintf( p, n, "???" ); break;
2421 }
2422 SAFE_SNPRINTF();
2423
2424 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002425 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002426 SAFE_SNPRINTF();
2427
Paul Bakker23986e52011-04-24 08:57:21 +00002428 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002429}
2430
Paul Bakker74111d32011-01-15 16:57:55 +00002431/* Compare a given OID string with an OID x509_buf * */
2432#define OID_CMP(oid_str, oid_buf) \
2433 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2434 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2435
2436/*
2437 * Return an informational string describing the given OID
2438 */
2439const char *x509_oid_get_description( x509_buf *oid )
2440{
2441 if ( oid == NULL )
2442 return ( NULL );
2443
2444 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2445 return( STRING_SERVER_AUTH );
2446
2447 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2448 return( STRING_CLIENT_AUTH );
2449
2450 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2451 return( STRING_CODE_SIGNING );
2452
2453 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2454 return( STRING_EMAIL_PROTECTION );
2455
2456 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2457 return( STRING_TIME_STAMPING );
2458
2459 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2460 return( STRING_OCSP_SIGNING );
2461
2462 return( NULL );
2463}
2464
2465/* Return the x.y.z.... style numeric string for the given OID */
2466int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2467{
Paul Bakker23986e52011-04-24 08:57:21 +00002468 int ret;
2469 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002470 unsigned int value;
2471 char *p;
2472
2473 p = buf;
2474 n = size;
2475
2476 /* First byte contains first two dots */
2477 if( oid->len > 0 )
2478 {
2479 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2480 SAFE_SNPRINTF();
2481 }
2482
2483 /* TODO: value can overflow in value. */
2484 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002485 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002486 {
2487 value <<= 7;
2488 value += oid->p[i] & 0x7F;
2489
2490 if( !( oid->p[i] & 0x80 ) )
2491 {
2492 /* Last byte */
2493 ret = snprintf( p, n, ".%d", value );
2494 SAFE_SNPRINTF();
2495 value = 0;
2496 }
2497 }
2498
Paul Bakker23986e52011-04-24 08:57:21 +00002499 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002500}
2501
Paul Bakkerd98030e2009-05-02 15:13:40 +00002502/*
2503 * Return an informational string about the CRL.
2504 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002505int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2506 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507{
Paul Bakker23986e52011-04-24 08:57:21 +00002508 int ret;
2509 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002510 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002511 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002512
2513 p = buf;
2514 n = size;
2515
2516 ret = snprintf( p, n, "%sCRL version : %d",
2517 prefix, crl->version );
2518 SAFE_SNPRINTF();
2519
2520 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2521 SAFE_SNPRINTF();
2522 ret = x509parse_dn_gets( p, n, &crl->issuer );
2523 SAFE_SNPRINTF();
2524
2525 ret = snprintf( p, n, "\n%sthis update : " \
2526 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2527 crl->this_update.year, crl->this_update.mon,
2528 crl->this_update.day, crl->this_update.hour,
2529 crl->this_update.min, crl->this_update.sec );
2530 SAFE_SNPRINTF();
2531
2532 ret = snprintf( p, n, "\n%snext update : " \
2533 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2534 crl->next_update.year, crl->next_update.mon,
2535 crl->next_update.day, crl->next_update.hour,
2536 crl->next_update.min, crl->next_update.sec );
2537 SAFE_SNPRINTF();
2538
2539 entry = &crl->entry;
2540
2541 ret = snprintf( p, n, "\n%sRevoked certificates:",
2542 prefix );
2543 SAFE_SNPRINTF();
2544
Paul Bakker9be19372009-07-27 20:21:53 +00002545 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002546 {
2547 ret = snprintf( p, n, "\n%sserial number: ",
2548 prefix );
2549 SAFE_SNPRINTF();
2550
2551 nr = ( entry->serial.len <= 32 )
2552 ? entry->serial.len : 32;
2553
Paul Bakker74111d32011-01-15 16:57:55 +00002554 for( i = 0; i < nr; i++ )
2555 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002556 ret = snprintf( p, n, "%02X%s",
2557 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2558 SAFE_SNPRINTF();
2559 }
2560
2561 ret = snprintf( p, n, " revocation date: " \
2562 "%04d-%02d-%02d %02d:%02d:%02d",
2563 entry->revocation_date.year, entry->revocation_date.mon,
2564 entry->revocation_date.day, entry->revocation_date.hour,
2565 entry->revocation_date.min, entry->revocation_date.sec );
2566 SAFE_SNPRINTF();
2567
2568 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
2570
Paul Bakkerd98030e2009-05-02 15:13:40 +00002571 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2572 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Paul Bakker27d66162010-03-17 06:56:01 +00002574 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002575 {
2576 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2577 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2578 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2579 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2580 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2581 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2582 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2583 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2584 default: ret = snprintf( p, n, "???" ); break;
2585 }
2586 SAFE_SNPRINTF();
2587
Paul Bakker1e27bb22009-07-19 20:25:25 +00002588 ret = snprintf( p, n, "\n" );
2589 SAFE_SNPRINTF();
2590
Paul Bakker23986e52011-04-24 08:57:21 +00002591 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592}
2593
2594/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002595 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002597int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598{
2599 struct tm *lt;
2600 time_t tt;
2601
2602 tt = time( NULL );
2603 lt = localtime( &tt );
2604
Paul Bakker40ea7de2009-05-03 10:18:48 +00002605 if( lt->tm_year > to->year - 1900 )
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 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Paul Bakker40ea7de2009-05-03 10:18:48 +00002612 if( lt->tm_year == to->year - 1900 &&
2613 lt->tm_mon == to->mon - 1 &&
2614 lt->tm_mday > to->day )
2615 return( 1 );
2616
Paul Bakkerb6194992011-01-16 21:40:22 +00002617 if( lt->tm_year == to->year - 1900 &&
2618 lt->tm_mon == to->mon - 1 &&
2619 lt->tm_mday == to->day &&
2620 lt->tm_hour > to->hour - 1)
2621 return( 1 );
2622
2623 if( lt->tm_year == to->year - 1900 &&
2624 lt->tm_mon == to->mon - 1 &&
2625 lt->tm_mday == to->day &&
2626 lt->tm_hour == to->hour - 1 &&
2627 lt->tm_min > to->min - 1 )
2628 return( 1 );
2629
2630 if( lt->tm_year == to->year - 1900 &&
2631 lt->tm_mon == to->mon - 1 &&
2632 lt->tm_mday == to->day &&
2633 lt->tm_hour == to->hour - 1 &&
2634 lt->tm_min == to->min - 1 &&
2635 lt->tm_sec > to->sec - 1 )
2636 return( 1 );
2637
Paul Bakker40ea7de2009-05-03 10:18:48 +00002638 return( 0 );
2639}
2640
2641/*
2642 * Return 1 if the certificate is revoked, or 0 otherwise.
2643 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002644int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002645{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002646 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002647
2648 while( cur != NULL && cur->serial.len != 0 )
2649 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002650 if( crt->serial.len == cur->serial.len &&
2651 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002652 {
2653 if( x509parse_time_expired( &cur->revocation_date ) )
2654 return( 1 );
2655 }
2656
2657 cur = cur->next;
2658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
2660 return( 0 );
2661}
2662
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002663/*
2664 * Wrapper for x509 hashes.
2665 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002666 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002667 */
Paul Bakker23986e52011-04-24 08:57:21 +00002668static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 unsigned char *out )
2670{
2671 switch( alg )
2672 {
Paul Bakker40e46942009-01-03 21:51:57 +00002673#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002674 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002676#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002677 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002679#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002680 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002681#endif
2682#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002683 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002684#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002685#if defined(POLARSSL_SHA2_C)
2686 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2687 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2688#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002689#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002690 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2691 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2692#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002694 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 break;
2696 }
2697}
2698
2699/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002700 * Check that the given certificate is valid accoring to the CRL.
2701 */
2702static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2703 x509_crl *crl_list)
2704{
2705 int flags = 0;
2706 int hash_id;
2707 unsigned char hash[64];
2708
2709 /*
2710 * TODO: What happens if no CRL is present?
2711 * Suggestion: Revocation state should be unknown if no CRL is present.
2712 * For backwards compatibility this is not yet implemented.
2713 */
2714
2715 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2716 {
2717 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2718 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2719 crl_list->issuer_raw.len ) != 0 )
2720 {
2721 crl_list = crl_list->next;
2722 continue;
2723 }
2724
2725 /*
2726 * Check if CRL is correctly signed by the trusted CA
2727 */
2728 hash_id = crl_list->sig_alg;
2729
2730 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2731
2732 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2733 0, hash, crl_list->sig.p ) == 0 )
2734 {
2735 /*
2736 * CRL is not trusted
2737 */
2738 flags |= BADCRL_NOT_TRUSTED;
2739 break;
2740 }
2741
2742 /*
2743 * Check for validity of CRL (Do not drop out)
2744 */
2745 if( x509parse_time_expired( &crl_list->next_update ) )
2746 flags |= BADCRL_EXPIRED;
2747
2748 /*
2749 * Check if certificate is revoked
2750 */
2751 if( x509parse_revoked(crt, crl_list) )
2752 {
2753 flags |= BADCERT_REVOKED;
2754 break;
2755 }
2756
2757 crl_list = crl_list->next;
2758 }
2759 return flags;
2760}
2761
2762/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 * Verify the certificate validity
2764 */
2765int x509parse_verify( x509_cert *crt,
2766 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002767 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002768 const char *cn, int *flags,
2769 int (*f_vrfy)(void *, x509_cert *, int, int),
2770 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771{
Paul Bakker23986e52011-04-24 08:57:21 +00002772 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 int hash_id;
2774 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002775 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002777 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Paul Bakker40ea7de2009-05-03 10:18:48 +00002779 *flags = 0;
2780
2781 if( x509parse_time_expired( &crt->valid_to ) )
2782 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
2784 if( cn != NULL )
2785 {
2786 name = &crt->subject;
2787 cn_len = strlen( cn );
2788
2789 while( name != NULL )
2790 {
2791 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2792 memcmp( name->val.p, cn, cn_len ) == 0 &&
2793 name->val.len == cn_len )
2794 break;
2795
2796 name = name->next;
2797 }
2798
2799 if( name == NULL )
2800 *flags |= BADCERT_CN_MISMATCH;
2801 }
2802
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 /*
2804 * Iterate upwards in the given cert chain,
2805 * ignoring any upper cert with CA != TRUE.
2806 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002807 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
2809 pathlen = 1;
2810
Paul Bakker76fd75a2011-01-16 21:12:10 +00002811 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002813 if( parent->ca_istrue == 0 ||
2814 crt->issuer_raw.len != parent->subject_raw.len ||
2815 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 crt->issuer_raw.len ) != 0 )
2817 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002818 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 continue;
2820 }
2821
Paul Bakker27d66162010-03-17 06:56:01 +00002822 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
2824 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2825
Paul Bakker76fd75a2011-01-16 21:12:10 +00002826 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2827 crt->sig.p ) != 0 )
2828 *flags |= BADCERT_NOT_TRUSTED;
2829
2830 /* Check trusted CA's CRL for the given crt */
2831 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002832
2833 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002834 if( NULL != f_vrfy )
2835 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002836 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002837 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002838 else
2839 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002840 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002841 else if( *flags != 0 )
2842 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 pathlen++;
2845
Paul Bakker76fd75a2011-01-16 21:12:10 +00002846 crt = parent;
2847 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 }
2849
2850 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002851 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002853 *flags |= BADCERT_NOT_TRUSTED;
2854
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002855 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 {
2857 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2858 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2859 crt->issuer_raw.len ) != 0 )
2860 {
2861 trust_ca = trust_ca->next;
2862 continue;
2863 }
2864
2865 if( trust_ca->max_pathlen > 0 &&
2866 trust_ca->max_pathlen < pathlen )
2867 break;
2868
Paul Bakker27d66162010-03-17 06:56:01 +00002869 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002870
2871 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2872
2873 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2874 0, hash, crt->sig.p ) == 0 )
2875 {
2876 /*
2877 * cert. is signed by a trusted CA
2878 */
2879 *flags &= ~BADCERT_NOT_TRUSTED;
2880 break;
2881 }
2882
2883 trust_ca = trust_ca->next;
2884 }
2885
Paul Bakker76fd75a2011-01-16 21:12:10 +00002886 /* Check trusted CA's CRL for the given crt */
2887 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002888
2889 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002890 if( NULL != f_vrfy )
2891 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002892 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002893 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002894 else
2895 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002896 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002897 else if( *flags != 0 )
2898 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002899
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 return( 0 );
2901}
2902
2903/*
2904 * Unallocate all certificate data
2905 */
2906void x509_free( x509_cert *crt )
2907{
2908 x509_cert *cert_cur = crt;
2909 x509_cert *cert_prv;
2910 x509_name *name_cur;
2911 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00002912 x509_sequence *seq_cur;
2913 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
2915 if( crt == NULL )
2916 return;
2917
2918 do
2919 {
2920 rsa_free( &cert_cur->rsa );
2921
2922 name_cur = cert_cur->issuer.next;
2923 while( name_cur != NULL )
2924 {
2925 name_prv = name_cur;
2926 name_cur = name_cur->next;
2927 memset( name_prv, 0, sizeof( x509_name ) );
2928 free( name_prv );
2929 }
2930
2931 name_cur = cert_cur->subject.next;
2932 while( name_cur != NULL )
2933 {
2934 name_prv = name_cur;
2935 name_cur = name_cur->next;
2936 memset( name_prv, 0, sizeof( x509_name ) );
2937 free( name_prv );
2938 }
2939
Paul Bakker74111d32011-01-15 16:57:55 +00002940 seq_cur = cert_cur->ext_key_usage.next;
2941 while( seq_cur != NULL )
2942 {
2943 seq_prv = seq_cur;
2944 seq_cur = seq_cur->next;
2945 memset( seq_prv, 0, sizeof( x509_sequence ) );
2946 free( seq_prv );
2947 }
2948
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 if( cert_cur->raw.p != NULL )
2950 {
2951 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2952 free( cert_cur->raw.p );
2953 }
2954
2955 cert_cur = cert_cur->next;
2956 }
2957 while( cert_cur != NULL );
2958
2959 cert_cur = crt;
2960 do
2961 {
2962 cert_prv = cert_cur;
2963 cert_cur = cert_cur->next;
2964
2965 memset( cert_prv, 0, sizeof( x509_cert ) );
2966 if( cert_prv != crt )
2967 free( cert_prv );
2968 }
2969 while( cert_cur != NULL );
2970}
2971
Paul Bakkerd98030e2009-05-02 15:13:40 +00002972/*
2973 * Unallocate all CRL data
2974 */
2975void x509_crl_free( x509_crl *crl )
2976{
2977 x509_crl *crl_cur = crl;
2978 x509_crl *crl_prv;
2979 x509_name *name_cur;
2980 x509_name *name_prv;
2981 x509_crl_entry *entry_cur;
2982 x509_crl_entry *entry_prv;
2983
2984 if( crl == NULL )
2985 return;
2986
2987 do
2988 {
2989 name_cur = crl_cur->issuer.next;
2990 while( name_cur != NULL )
2991 {
2992 name_prv = name_cur;
2993 name_cur = name_cur->next;
2994 memset( name_prv, 0, sizeof( x509_name ) );
2995 free( name_prv );
2996 }
2997
2998 entry_cur = crl_cur->entry.next;
2999 while( entry_cur != NULL )
3000 {
3001 entry_prv = entry_cur;
3002 entry_cur = entry_cur->next;
3003 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3004 free( entry_prv );
3005 }
3006
3007 if( crl_cur->raw.p != NULL )
3008 {
3009 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3010 free( crl_cur->raw.p );
3011 }
3012
3013 crl_cur = crl_cur->next;
3014 }
3015 while( crl_cur != NULL );
3016
3017 crl_cur = crl;
3018 do
3019 {
3020 crl_prv = crl_cur;
3021 crl_cur = crl_cur->next;
3022
3023 memset( crl_prv, 0, sizeof( x509_crl ) );
3024 if( crl_prv != crl )
3025 free( crl_prv );
3026 }
3027 while( crl_cur != NULL );
3028}
3029
Paul Bakker40e46942009-01-03 21:51:57 +00003030#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Paul Bakker40e46942009-01-03 21:51:57 +00003032#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
3034/*
3035 * Checkup routine
3036 */
3037int x509_self_test( int verbose )
3038{
Paul Bakker5690efc2011-05-26 13:16:06 +00003039#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003040 int ret;
3041 int flags;
3042 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 x509_cert cacert;
3044 x509_cert clicert;
3045 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003046#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003047 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
3050 if( verbose != 0 )
3051 printf( " X.509 certificate load: " );
3052
3053 memset( &clicert, 0, sizeof( x509_cert ) );
3054
3055 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3056 strlen( test_cli_crt ) );
3057 if( ret != 0 )
3058 {
3059 if( verbose != 0 )
3060 printf( "failed\n" );
3061
3062 return( ret );
3063 }
3064
3065 memset( &cacert, 0, sizeof( x509_cert ) );
3066
3067 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3068 strlen( test_ca_crt ) );
3069 if( ret != 0 )
3070 {
3071 if( verbose != 0 )
3072 printf( "failed\n" );
3073
3074 return( ret );
3075 }
3076
3077 if( verbose != 0 )
3078 printf( "passed\n X.509 private key load: " );
3079
3080 i = strlen( test_ca_key );
3081 j = strlen( test_ca_pwd );
3082
Paul Bakker66b78b22011-03-25 14:22:50 +00003083 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3084
Paul Bakker5121ce52009-01-03 21:22:43 +00003085 if( ( ret = x509parse_key( &rsa,
3086 (unsigned char *) test_ca_key, i,
3087 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3088 {
3089 if( verbose != 0 )
3090 printf( "failed\n" );
3091
3092 return( ret );
3093 }
3094
3095 if( verbose != 0 )
3096 printf( "passed\n X.509 signature verify: ");
3097
Paul Bakker23986e52011-04-24 08:57:21 +00003098 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 if( ret != 0 )
3100 {
Paul Bakker23986e52011-04-24 08:57:21 +00003101 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 if( verbose != 0 )
3103 printf( "failed\n" );
3104
3105 return( ret );
3106 }
3107
Paul Bakker5690efc2011-05-26 13:16:06 +00003108#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003110 printf( "passed\n X.509 DHM parameter load: " );
3111
3112 i = strlen( test_dhm_params );
3113 j = strlen( test_ca_pwd );
3114
3115 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3116 {
3117 if( verbose != 0 )
3118 printf( "failed\n" );
3119
3120 return( ret );
3121 }
3122
3123 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
3127 x509_free( &cacert );
3128 x509_free( &clicert );
3129 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003130#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003131 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
3134 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003135#else
3136 ((void) verbose);
3137 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003139}
3140
3141#endif
3142
3143#endif