blob: 58399eb4841ebe57d5ece8b3db0c59b3288c58d2 [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 Bakkered56b222011-07-13 11:26:43 +0000625 return( POLARSSL_ERR_X509_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 Bakker5c721f92011-07-27 16:51:09 +00001016#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001017 if( is_critical )
1018 {
1019 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001020 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001021 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1022 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001023#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 }
1026
1027 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001028 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001029 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 return( 0 );
1032}
1033
1034/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001035 * X.509 CRL Entries
1036 */
1037static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001038 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001039 x509_crl_entry *entry )
1040{
Paul Bakker23986e52011-04-24 08:57:21 +00001041 int ret;
1042 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001043 x509_crl_entry *cur_entry = entry;
1044
1045 if( *p == end )
1046 return( 0 );
1047
Paul Bakker9be19372009-07-27 20:21:53 +00001048 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1050 {
1051 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1052 return( 0 );
1053
1054 return( ret );
1055 }
1056
Paul Bakker9be19372009-07-27 20:21:53 +00001057 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001058
1059 while( *p < end )
1060 {
Paul Bakker23986e52011-04-24 08:57:21 +00001061 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001062
1063 if( ( ret = asn1_get_tag( p, end, &len2,
1064 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1065 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001066 return( ret );
1067 }
1068
Paul Bakker9be19372009-07-27 20:21:53 +00001069 cur_entry->raw.tag = **p;
1070 cur_entry->raw.p = *p;
1071 cur_entry->raw.len = len2;
1072
Paul Bakkerd98030e2009-05-02 15:13:40 +00001073 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1074 return( ret );
1075
Paul Bakker91200182010-02-18 21:26:15 +00001076 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001077 return( ret );
1078
1079 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1080 return( ret );
1081
Paul Bakker74111d32011-01-15 16:57:55 +00001082 if ( *p < end )
1083 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1085 cur_entry = cur_entry->next;
1086 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1087 }
1088 }
1089
1090 return( 0 );
1091}
1092
Paul Bakker27d66162010-03-17 06:56:01 +00001093static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1094{
1095 if( sig_oid->len == 9 &&
1096 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1097 {
1098 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1099 {
1100 *sig_alg = sig_oid->p[8];
1101 return( 0 );
1102 }
1103
1104 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1105 {
1106 *sig_alg = sig_oid->p[8];
1107 return( 0 );
1108 }
1109
1110 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1111 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001112 if( sig_oid->len == 5 &&
1113 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1114 {
1115 *sig_alg = SIG_RSA_SHA1;
1116 return( 0 );
1117 }
Paul Bakker27d66162010-03-17 06:56:01 +00001118
1119 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1120}
1121
Paul Bakkerd98030e2009-05-02 15:13:40 +00001122/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 * Parse one or more certificates and add them to the chained list
1124 */
Paul Bakker23986e52011-04-24 08:57:21 +00001125int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001126{
Paul Bakker23986e52011-04-24 08:57:21 +00001127 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001128 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 unsigned char *p, *end;
1130 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001131#if defined(POLARSSL_PEM_C)
1132 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001133 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001134#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
1136 crt = chain;
1137
Paul Bakker320a4b52009-03-28 18:52:39 +00001138 /*
1139 * Check for valid input
1140 */
1141 if( crt == NULL || buf == NULL )
1142 return( 1 );
1143
Paul Bakkere9581d62009-03-28 20:29:25 +00001144 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 crt = crt->next;
1146
1147 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001148 * Add new certificate on the end of the chain if needed.
1149 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001150 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001151 {
1152 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1153
Paul Bakker7d06ad22009-05-02 15:53:56 +00001154 if( crt->next == NULL )
1155 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001156 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001157 return( 1 );
1158 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001159
Paul Bakker7d06ad22009-05-02 15:53:56 +00001160 crt = crt->next;
1161 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001162 }
1163
Paul Bakker96743fc2011-02-12 14:30:57 +00001164#if defined(POLARSSL_PEM_C)
1165 pem_init( &pem );
1166 ret = pem_read_buffer( &pem,
1167 "-----BEGIN CERTIFICATE-----",
1168 "-----END CERTIFICATE-----",
1169 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
Paul Bakker96743fc2011-02-12 14:30:57 +00001171 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001173 /*
1174 * Was PEM encoded
1175 */
1176 buflen -= use_len;
1177 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
1179 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001180 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001182 p = pem.buf;
1183 pem.buf = NULL;
1184 len = pem.buflen;
1185 pem_free( &pem );
1186 }
1187 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1188 {
1189 pem_free( &pem );
1190 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
1192 else
1193 {
1194 /*
1195 * nope, copy the raw DER data
1196 */
1197 p = (unsigned char *) malloc( len = buflen );
1198
1199 if( p == NULL )
1200 return( 1 );
1201
1202 memcpy( p, buf, buflen );
1203
1204 buflen = 0;
1205 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001206#else
1207 p = (unsigned char *) malloc( len = buflen );
1208
1209 if( p == NULL )
1210 return( 1 );
1211
1212 memcpy( p, buf, buflen );
1213
1214 buflen = 0;
1215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001216
1217 crt->raw.p = p;
1218 crt->raw.len = len;
1219 end = p + len;
1220
1221 /*
1222 * Certificate ::= SEQUENCE {
1223 * tbsCertificate TBSCertificate,
1224 * signatureAlgorithm AlgorithmIdentifier,
1225 * signatureValue BIT STRING }
1226 */
1227 if( ( ret = asn1_get_tag( &p, end, &len,
1228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1229 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001230 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001231 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 }
1233
Paul Bakker23986e52011-04-24 08:57:21 +00001234 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001236 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001237 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001238 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
1240
1241 /*
1242 * TBSCertificate ::= SEQUENCE {
1243 */
1244 crt->tbs.p = p;
1245
1246 if( ( ret = asn1_get_tag( &p, end, &len,
1247 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001250 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 }
1252
1253 end = p + len;
1254 crt->tbs.len = end - crt->tbs.p;
1255
1256 /*
1257 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1258 *
1259 * CertificateSerialNumber ::= INTEGER
1260 *
1261 * signature AlgorithmIdentifier
1262 */
1263 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1264 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1265 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1266 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001267 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 return( ret );
1269 }
1270
1271 crt->version++;
1272
1273 if( crt->version > 3 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001276 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 }
1278
Paul Bakker27d66162010-03-17 06:56:01 +00001279 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001281 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001282 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 }
1284
1285 /*
1286 * issuer Name
1287 */
1288 crt->issuer_raw.p = p;
1289
1290 if( ( ret = asn1_get_tag( &p, end, &len,
1291 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1292 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001293 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001294 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
1296
1297 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1298 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001299 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 return( ret );
1301 }
1302
1303 crt->issuer_raw.len = p - crt->issuer_raw.p;
1304
1305 /*
1306 * Validity ::= SEQUENCE {
1307 * notBefore Time,
1308 * notAfter Time }
1309 *
1310 */
1311 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1312 &crt->valid_to ) ) != 0 )
1313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317
1318 /*
1319 * subject Name
1320 */
1321 crt->subject_raw.p = p;
1322
1323 if( ( ret = asn1_get_tag( &p, end, &len,
1324 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001327 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 }
1329
1330 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1331 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001332 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 return( ret );
1334 }
1335
1336 crt->subject_raw.len = p - crt->subject_raw.p;
1337
1338 /*
1339 * SubjectPublicKeyInfo ::= SEQUENCE
1340 * algorithm AlgorithmIdentifier,
1341 * subjectPublicKey BIT STRING }
1342 */
1343 if( ( ret = asn1_get_tag( &p, end, &len,
1344 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1345 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001346 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001347 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 }
1349
1350 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1351 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 return( ret );
1355 }
1356
1357 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1358 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001359 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 return( ret );
1361 }
1362
1363 crt->rsa.len = mpi_size( &crt->rsa.N );
1364
1365 /*
1366 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1367 * -- If present, version shall be v2 or v3
1368 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1369 * -- If present, version shall be v2 or v3
1370 * extensions [3] EXPLICIT Extensions OPTIONAL
1371 * -- If present, version shall be v3
1372 */
1373 if( crt->version == 2 || crt->version == 3 )
1374 {
1375 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1376 if( ret != 0 )
1377 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001378 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 return( ret );
1380 }
1381 }
1382
1383 if( crt->version == 2 || crt->version == 3 )
1384 {
1385 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1386 if( ret != 0 )
1387 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001388 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 return( ret );
1390 }
1391 }
1392
1393 if( crt->version == 3 )
1394 {
Paul Bakker74111d32011-01-15 16:57:55 +00001395 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 if( ret != 0 )
1397 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001398 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 return( ret );
1400 }
1401 }
1402
1403 if( p != end )
1404 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001405 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001406 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001407 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 }
1409
1410 end = crt->raw.p + crt->raw.len;
1411
1412 /*
1413 * signatureAlgorithm AlgorithmIdentifier,
1414 * signatureValue BIT STRING
1415 */
1416 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1417 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001418 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 return( ret );
1420 }
1421
Paul Bakker320a4b52009-03-28 18:52:39 +00001422 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001424 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001425 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 }
1427
1428 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1429 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001430 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 return( ret );
1432 }
1433
1434 if( p != end )
1435 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001436 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001437 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001438 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 }
1440
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001442 {
1443 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1444
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 if( crt->next == NULL )
1446 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001447 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001448 return( 1 );
1449 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001450
Paul Bakker7d06ad22009-05-02 15:53:56 +00001451 crt = crt->next;
1452 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001456
1457 return( 0 );
1458}
1459
1460/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001461 * Parse one or more CRLs and add them to the chained list
1462 */
Paul Bakker23986e52011-04-24 08:57:21 +00001463int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001464{
Paul Bakker23986e52011-04-24 08:57:21 +00001465 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001466 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001467 unsigned char *p, *end;
1468 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001469#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001470 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001471 pem_context pem;
1472#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001473
1474 crl = chain;
1475
1476 /*
1477 * Check for valid input
1478 */
1479 if( crl == NULL || buf == NULL )
1480 return( 1 );
1481
1482 while( crl->version != 0 && crl->next != NULL )
1483 crl = crl->next;
1484
1485 /*
1486 * Add new CRL on the end of the chain if needed.
1487 */
1488 if ( crl->version != 0 && crl->next == NULL)
1489 {
1490 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1491
Paul Bakker7d06ad22009-05-02 15:53:56 +00001492 if( crl->next == NULL )
1493 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001494 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001495 return( 1 );
1496 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001497
Paul Bakker7d06ad22009-05-02 15:53:56 +00001498 crl = crl->next;
1499 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001500 }
1501
Paul Bakker96743fc2011-02-12 14:30:57 +00001502#if defined(POLARSSL_PEM_C)
1503 pem_init( &pem );
1504 ret = pem_read_buffer( &pem,
1505 "-----BEGIN X509 CRL-----",
1506 "-----END X509 CRL-----",
1507 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001508
Paul Bakker96743fc2011-02-12 14:30:57 +00001509 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001510 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001511 /*
1512 * Was PEM encoded
1513 */
1514 buflen -= use_len;
1515 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516
1517 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001518 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001520 p = pem.buf;
1521 pem.buf = NULL;
1522 len = pem.buflen;
1523 pem_free( &pem );
1524 }
1525 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1526 {
1527 pem_free( &pem );
1528 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529 }
1530 else
1531 {
1532 /*
1533 * nope, copy the raw DER data
1534 */
1535 p = (unsigned char *) malloc( len = buflen );
1536
1537 if( p == NULL )
1538 return( 1 );
1539
1540 memcpy( p, buf, buflen );
1541
1542 buflen = 0;
1543 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001544#else
1545 p = (unsigned char *) malloc( len = buflen );
1546
1547 if( p == NULL )
1548 return( 1 );
1549
1550 memcpy( p, buf, buflen );
1551
1552 buflen = 0;
1553#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001554
1555 crl->raw.p = p;
1556 crl->raw.len = len;
1557 end = p + len;
1558
1559 /*
1560 * CertificateList ::= SEQUENCE {
1561 * tbsCertList TBSCertList,
1562 * signatureAlgorithm AlgorithmIdentifier,
1563 * signatureValue BIT STRING }
1564 */
1565 if( ( ret = asn1_get_tag( &p, end, &len,
1566 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1567 {
1568 x509_crl_free( crl );
1569 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1570 }
1571
Paul Bakker23986e52011-04-24 08:57:21 +00001572 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001573 {
1574 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001575 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001576 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1577 }
1578
1579 /*
1580 * TBSCertList ::= SEQUENCE {
1581 */
1582 crl->tbs.p = p;
1583
1584 if( ( ret = asn1_get_tag( &p, end, &len,
1585 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1586 {
1587 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001588 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 }
1590
1591 end = p + len;
1592 crl->tbs.len = end - crl->tbs.p;
1593
1594 /*
1595 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1596 * -- if present, MUST be v2
1597 *
1598 * signature AlgorithmIdentifier
1599 */
1600 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1601 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1602 {
1603 x509_crl_free( crl );
1604 return( ret );
1605 }
1606
1607 crl->version++;
1608
1609 if( crl->version > 2 )
1610 {
1611 x509_crl_free( crl );
1612 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1613 }
1614
Paul Bakker27d66162010-03-17 06:56:01 +00001615 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001616 {
1617 x509_crl_free( crl );
1618 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1619 }
1620
1621 /*
1622 * issuer Name
1623 */
1624 crl->issuer_raw.p = p;
1625
1626 if( ( ret = asn1_get_tag( &p, end, &len,
1627 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1628 {
1629 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001630 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631 }
1632
1633 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1634 {
1635 x509_crl_free( crl );
1636 return( ret );
1637 }
1638
1639 crl->issuer_raw.len = p - crl->issuer_raw.p;
1640
1641 /*
1642 * thisUpdate Time
1643 * nextUpdate Time OPTIONAL
1644 */
Paul Bakker91200182010-02-18 21:26:15 +00001645 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001646 {
1647 x509_crl_free( crl );
1648 return( ret );
1649 }
1650
Paul Bakker91200182010-02-18 21:26:15 +00001651 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652 {
Paul Bakker9d781402011-05-09 16:17:09 +00001653 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001654 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001655 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001656 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001657 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 x509_crl_free( crl );
1659 return( ret );
1660 }
1661 }
1662
1663 /*
1664 * revokedCertificates SEQUENCE OF SEQUENCE {
1665 * userCertificate CertificateSerialNumber,
1666 * revocationDate Time,
1667 * crlEntryExtensions Extensions OPTIONAL
1668 * -- if present, MUST be v2
1669 * } OPTIONAL
1670 */
1671 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1672 {
1673 x509_crl_free( crl );
1674 return( ret );
1675 }
1676
1677 /*
1678 * crlExtensions EXPLICIT Extensions OPTIONAL
1679 * -- if present, MUST be v2
1680 */
1681 if( crl->version == 2 )
1682 {
1683 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1684
1685 if( ret != 0 )
1686 {
1687 x509_crl_free( crl );
1688 return( ret );
1689 }
1690 }
1691
1692 if( p != end )
1693 {
1694 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001695 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001696 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1697 }
1698
1699 end = crl->raw.p + crl->raw.len;
1700
1701 /*
1702 * signatureAlgorithm AlgorithmIdentifier,
1703 * signatureValue BIT STRING
1704 */
1705 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1706 {
1707 x509_crl_free( crl );
1708 return( ret );
1709 }
1710
1711 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1712 {
1713 x509_crl_free( crl );
1714 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1715 }
1716
1717 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1718 {
1719 x509_crl_free( crl );
1720 return( ret );
1721 }
1722
1723 if( p != end )
1724 {
1725 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001726 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1728 }
1729
1730 if( buflen > 0 )
1731 {
1732 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1733
Paul Bakker7d06ad22009-05-02 15:53:56 +00001734 if( crl->next == NULL )
1735 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001736 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001737 return( 1 );
1738 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739
Paul Bakker7d06ad22009-05-02 15:53:56 +00001740 crl = crl->next;
1741 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001742
1743 return( x509parse_crl( crl, buf, buflen ) );
1744 }
1745
1746 return( 0 );
1747}
1748
Paul Bakker335db3f2011-04-25 15:28:35 +00001749#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001750/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001751 * Load all data from a file into a given buffer.
1752 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001753int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001754{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001756
Paul Bakkerd98030e2009-05-02 15:13:40 +00001757 if( ( f = fopen( path, "rb" ) ) == NULL )
1758 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001759
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760 fseek( f, 0, SEEK_END );
1761 *n = (size_t) ftell( f );
1762 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001763
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1765 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001766
Paul Bakkerd98030e2009-05-02 15:13:40 +00001767 if( fread( *buf, 1, *n, f ) != *n )
1768 {
1769 fclose( f );
1770 free( *buf );
1771 return( 1 );
1772 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001773
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001775
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001777
Paul Bakkerd98030e2009-05-02 15:13:40 +00001778 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001779}
1780
1781/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 * Load one or more certificates and add them to the chained list
1783 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001784int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785{
1786 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 size_t n;
1788 unsigned char *buf;
1789
Paul Bakker2b245eb2009-04-19 18:44:26 +00001790 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 return( 1 );
1792
Paul Bakker27fdf462011-06-09 13:55:13 +00001793 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
1795 memset( buf, 0, n + 1 );
1796 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
1798 return( ret );
1799}
1800
Paul Bakkerd98030e2009-05-02 15:13:40 +00001801/*
1802 * Load one or more CRLs and add them to the chained list
1803 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001804int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805{
1806 int ret;
1807 size_t n;
1808 unsigned char *buf;
1809
1810 if ( load_file( path, &buf, &n ) )
1811 return( 1 );
1812
Paul Bakker27fdf462011-06-09 13:55:13 +00001813 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814
1815 memset( buf, 0, n + 1 );
1816 free( buf );
1817
1818 return( ret );
1819}
1820
Paul Bakker5121ce52009-01-03 21:22:43 +00001821/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001822 * Load and parse a private RSA key
1823 */
1824int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1825{
1826 int ret;
1827 size_t n;
1828 unsigned char *buf;
1829
1830 if ( load_file( path, &buf, &n ) )
1831 return( 1 );
1832
1833 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001834 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001835 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001836 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001837 (unsigned char *) pwd, strlen( pwd ) );
1838
1839 memset( buf, 0, n + 1 );
1840 free( buf );
1841
1842 return( ret );
1843}
1844
1845/*
1846 * Load and parse a public RSA key
1847 */
1848int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1849{
1850 int ret;
1851 size_t n;
1852 unsigned char *buf;
1853
1854 if ( load_file( path, &buf, &n ) )
1855 return( 1 );
1856
Paul Bakker27fdf462011-06-09 13:55:13 +00001857 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001858
1859 memset( buf, 0, n + 1 );
1860 free( buf );
1861
1862 return( ret );
1863}
1864#endif /* POLARSSL_FS_IO */
1865
1866/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 * Parse a private RSA key
1868 */
Paul Bakker23986e52011-04-24 08:57:21 +00001869int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1870 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001871{
Paul Bakker23986e52011-04-24 08:57:21 +00001872 int ret;
1873 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001875 unsigned char *p_alt;
1876 x509_buf pk_alg_oid;
1877
Paul Bakker96743fc2011-02-12 14:30:57 +00001878#if defined(POLARSSL_PEM_C)
1879 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Paul Bakker96743fc2011-02-12 14:30:57 +00001881 pem_init( &pem );
1882 ret = pem_read_buffer( &pem,
1883 "-----BEGIN RSA PRIVATE KEY-----",
1884 "-----END RSA PRIVATE KEY-----",
1885 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Paul Bakkered56b222011-07-13 11:26:43 +00001887 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1888 {
1889 ret = pem_read_buffer( &pem,
1890 "-----BEGIN PRIVATE KEY-----",
1891 "-----END PRIVATE KEY-----",
1892 key, pwd, pwdlen, &len );
1893 }
1894
Paul Bakker96743fc2011-02-12 14:30:57 +00001895 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001897 /*
1898 * Was PEM encoded
1899 */
1900 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001902 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001903 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001904 pem_free( &pem );
1905 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001906 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Paul Bakker96743fc2011-02-12 14:30:57 +00001908 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1909#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001910 ((void) pwd);
1911 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001912 p = (unsigned char *) key;
1913#endif
1914 end = p + keylen;
1915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001917 * Note: Depending on the type of private key file one can expect either a
1918 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1919 *
1920 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001921 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001922 * algorithm AlgorithmIdentifier,
1923 * PrivateKey BIT STRING
1924 * }
1925 *
1926 * AlgorithmIdentifier ::= SEQUENCE {
1927 * algorithm OBJECT IDENTIFIER,
1928 * parameters ANY DEFINED BY algorithm OPTIONAL
1929 * }
1930 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 * RSAPrivateKey ::= SEQUENCE {
1932 * version Version,
1933 * modulus INTEGER, -- n
1934 * publicExponent INTEGER, -- e
1935 * privateExponent INTEGER, -- d
1936 * prime1 INTEGER, -- p
1937 * prime2 INTEGER, -- q
1938 * exponent1 INTEGER, -- d mod (p-1)
1939 * exponent2 INTEGER, -- d mod (q-1)
1940 * coefficient INTEGER, -- (inverse of q) mod p
1941 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1942 * }
1943 */
1944 if( ( ret = asn1_get_tag( &p, end, &len,
1945 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1946 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001947#if defined(POLARSSL_PEM_C)
1948 pem_free( &pem );
1949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001951 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 }
1953
1954 end = p + len;
1955
1956 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1957 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001958#if defined(POLARSSL_PEM_C)
1959 pem_free( &pem );
1960#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001962 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 }
1964
1965 if( rsa->ver != 0 )
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_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
1973
Paul Bakkered56b222011-07-13 11:26:43 +00001974 p_alt = p;
1975
1976 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1977 {
1978 // Assume that we have the PKCS#1 format if wrong
1979 // tag was encountered
1980 //
1981 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1982 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1983 {
1984#if defined(POLARSSL_PEM_C)
1985 pem_free( &pem );
1986#endif
1987 rsa_free( rsa );
1988 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1989 }
1990 }
1991 else
1992 {
1993 int can_handle;
1994
1995 /*
1996 * only RSA keys handled at this time
1997 */
1998 can_handle = 0;
1999
2000 if( pk_alg_oid.len == 9 &&
2001 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2002 can_handle = 1;
2003
2004 if( pk_alg_oid.len == 9 &&
2005 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2006 {
2007 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2008 can_handle = 1;
2009
2010 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2011 can_handle = 1;
2012 }
2013
2014 if( pk_alg_oid.len == 5 &&
2015 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2016 can_handle = 1;
2017
2018 if( can_handle == 0 )
2019 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2020
2021 /*
2022 * Parse the PKCS#8 format
2023 */
2024
2025 p = p_alt;
2026 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2027 {
2028#if defined(POLARSSL_PEM_C)
2029 pem_free( &pem );
2030#endif
2031 rsa_free( rsa );
2032 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2033 }
2034
2035 if( ( end - p ) < 1 )
2036 {
2037#if defined(POLARSSL_PEM_C)
2038 pem_free( &pem );
2039#endif
2040 rsa_free( rsa );
2041 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2042 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2043 }
2044
2045 end = p + len;
2046
2047 if( ( ret = asn1_get_tag( &p, end, &len,
2048 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2049 {
2050#if defined(POLARSSL_PEM_C)
2051 pem_free( &pem );
2052#endif
2053 rsa_free( rsa );
2054 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2055 }
2056
2057 end = p + len;
2058
2059 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2060 {
2061#if defined(POLARSSL_PEM_C)
2062 pem_free( &pem );
2063#endif
2064 rsa_free( rsa );
2065 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2066 }
2067
2068 if( rsa->ver != 0 )
2069 {
2070#if defined(POLARSSL_PEM_C)
2071 pem_free( &pem );
2072#endif
2073 rsa_free( rsa );
2074 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2075 }
2076 }
2077
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2079 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2080 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2081 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2082 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2083 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2084 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2085 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2086 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002087#if defined(POLARSSL_PEM_C)
2088 pem_free( &pem );
2089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002090 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002091 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
2094 rsa->len = mpi_size( &rsa->N );
2095
2096 if( p != end )
2097 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002098#if defined(POLARSSL_PEM_C)
2099 pem_free( &pem );
2100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002102 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002103 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 }
2105
2106 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2107 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002108#if defined(POLARSSL_PEM_C)
2109 pem_free( &pem );
2110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 rsa_free( rsa );
2112 return( ret );
2113 }
2114
Paul Bakker96743fc2011-02-12 14:30:57 +00002115#if defined(POLARSSL_PEM_C)
2116 pem_free( &pem );
2117#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
2119 return( 0 );
2120}
2121
2122/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002123 * Parse a public RSA key
2124 */
Paul Bakker23986e52011-04-24 08:57:21 +00002125int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002126{
Paul Bakker23986e52011-04-24 08:57:21 +00002127 int ret;
2128 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002129 unsigned char *p, *end;
2130 x509_buf alg_oid;
2131#if defined(POLARSSL_PEM_C)
2132 pem_context pem;
2133
2134 pem_init( &pem );
2135 ret = pem_read_buffer( &pem,
2136 "-----BEGIN PUBLIC KEY-----",
2137 "-----END PUBLIC KEY-----",
2138 key, NULL, 0, &len );
2139
2140 if( ret == 0 )
2141 {
2142 /*
2143 * Was PEM encoded
2144 */
2145 keylen = pem.buflen;
2146 }
2147 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2148 {
2149 pem_free( &pem );
2150 return( ret );
2151 }
2152
2153 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2154#else
2155 p = (unsigned char *) key;
2156#endif
2157 end = p + keylen;
2158
2159 /*
2160 * PublicKeyInfo ::= SEQUENCE {
2161 * algorithm AlgorithmIdentifier,
2162 * PublicKey BIT STRING
2163 * }
2164 *
2165 * AlgorithmIdentifier ::= SEQUENCE {
2166 * algorithm OBJECT IDENTIFIER,
2167 * parameters ANY DEFINED BY algorithm OPTIONAL
2168 * }
2169 *
2170 * RSAPublicKey ::= SEQUENCE {
2171 * modulus INTEGER, -- n
2172 * publicExponent INTEGER -- e
2173 * }
2174 */
2175
2176 if( ( ret = asn1_get_tag( &p, end, &len,
2177 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2178 {
2179#if defined(POLARSSL_PEM_C)
2180 pem_free( &pem );
2181#endif
2182 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002183 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002184 }
2185
2186 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2187 {
2188#if defined(POLARSSL_PEM_C)
2189 pem_free( &pem );
2190#endif
2191 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002192 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002193 }
2194
2195 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2196 {
2197#if defined(POLARSSL_PEM_C)
2198 pem_free( &pem );
2199#endif
2200 rsa_free( rsa );
2201 return( ret );
2202 }
2203
2204 rsa->len = mpi_size( &rsa->N );
2205
2206#if defined(POLARSSL_PEM_C)
2207 pem_free( &pem );
2208#endif
2209
2210 return( 0 );
2211}
2212
Paul Bakkereaa89f82011-04-04 21:36:15 +00002213#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002214/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002215 * Parse DHM parameters
2216 */
Paul Bakker23986e52011-04-24 08:57:21 +00002217int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002218{
Paul Bakker23986e52011-04-24 08:57:21 +00002219 int ret;
2220 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002221 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002222#if defined(POLARSSL_PEM_C)
2223 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002224
Paul Bakker96743fc2011-02-12 14:30:57 +00002225 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002226
Paul Bakker96743fc2011-02-12 14:30:57 +00002227 ret = pem_read_buffer( &pem,
2228 "-----BEGIN DH PARAMETERS-----",
2229 "-----END DH PARAMETERS-----",
2230 dhmin, NULL, 0, &dhminlen );
2231
2232 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002233 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002234 /*
2235 * Was PEM encoded
2236 */
2237 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002238 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002239 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002240 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002241 pem_free( &pem );
2242 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002243 }
2244
Paul Bakker96743fc2011-02-12 14:30:57 +00002245 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2246#else
2247 p = (unsigned char *) dhmin;
2248#endif
2249 end = p + dhminlen;
2250
Paul Bakker1b57b062011-01-06 15:48:19 +00002251 memset( dhm, 0, sizeof( dhm_context ) );
2252
Paul Bakker1b57b062011-01-06 15:48:19 +00002253 /*
2254 * DHParams ::= SEQUENCE {
2255 * prime INTEGER, -- P
2256 * generator INTEGER, -- g
2257 * }
2258 */
2259 if( ( ret = asn1_get_tag( &p, end, &len,
2260 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2261 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002262#if defined(POLARSSL_PEM_C)
2263 pem_free( &pem );
2264#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002265 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002266 }
2267
2268 end = p + len;
2269
2270 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2271 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2272 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002273#if defined(POLARSSL_PEM_C)
2274 pem_free( &pem );
2275#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002276 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002277 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002278 }
2279
2280 if( p != end )
2281 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002282#if defined(POLARSSL_PEM_C)
2283 pem_free( &pem );
2284#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002285 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002286 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002287 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2288 }
2289
Paul Bakker96743fc2011-02-12 14:30:57 +00002290#if defined(POLARSSL_PEM_C)
2291 pem_free( &pem );
2292#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002293
2294 return( 0 );
2295}
2296
Paul Bakker335db3f2011-04-25 15:28:35 +00002297#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002298/*
2299 * Load and parse a private RSA key
2300 */
2301int x509parse_dhmfile( dhm_context *dhm, const char *path )
2302{
2303 int ret;
2304 size_t n;
2305 unsigned char *buf;
2306
2307 if ( load_file( path, &buf, &n ) )
2308 return( 1 );
2309
Paul Bakker27fdf462011-06-09 13:55:13 +00002310 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002311
2312 memset( buf, 0, n + 1 );
2313 free( buf );
2314
2315 return( ret );
2316}
Paul Bakker335db3f2011-04-25 15:28:35 +00002317#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002318#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002319
Paul Bakker5121ce52009-01-03 21:22:43 +00002320#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002321#include <stdarg.h>
2322
2323#if !defined vsnprintf
2324#define vsnprintf _vsnprintf
2325#endif // vsnprintf
2326
2327/*
2328 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2329 * Result value is not size of buffer needed, but -1 if no fit is possible.
2330 *
2331 * This fuction tries to 'fix' this by at least suggesting enlarging the
2332 * size by 20.
2333 */
2334int compat_snprintf(char *str, size_t size, const char *format, ...)
2335{
2336 va_list ap;
2337 int res = -1;
2338
2339 va_start( ap, format );
2340
2341 res = vsnprintf( str, size, format, ap );
2342
2343 va_end( ap );
2344
2345 // No quick fix possible
2346 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002347 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002348
2349 return res;
2350}
2351
2352#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002353#endif
2354
Paul Bakkerd98030e2009-05-02 15:13:40 +00002355#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2356
2357#define SAFE_SNPRINTF() \
2358{ \
2359 if( ret == -1 ) \
2360 return( -1 ); \
2361 \
Paul Bakker23986e52011-04-24 08:57:21 +00002362 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002363 p[n - 1] = '\0'; \
2364 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2365 } \
2366 \
Paul Bakker23986e52011-04-24 08:57:21 +00002367 n -= (unsigned int) ret; \
2368 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002369}
2370
Paul Bakker5121ce52009-01-03 21:22:43 +00002371/*
2372 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002373 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002375int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002376{
Paul Bakker23986e52011-04-24 08:57:21 +00002377 int ret;
2378 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002380 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 char s[128], *p;
2382
2383 memset( s, 0, sizeof( s ) );
2384
2385 name = dn;
2386 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002387 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
2389 while( name != NULL )
2390 {
Paul Bakker74111d32011-01-15 16:57:55 +00002391 if( name != dn )
2392 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002393 ret = snprintf( p, n, ", " );
2394 SAFE_SNPRINTF();
2395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
2397 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2398 {
2399 switch( name->oid.p[2] )
2400 {
2401 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002402 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002405 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
2407 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
2410 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002417 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002418
2419 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002420 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 name->oid.p[2] );
2422 break;
2423 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002424 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2427 {
2428 switch( name->oid.p[8] )
2429 {
2430 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002431 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002434 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 name->oid.p[8] );
2436 break;
2437 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002438 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 }
2440 else
Paul Bakker74111d32011-01-15 16:57:55 +00002441 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002442 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002443 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
2446 for( i = 0; i < name->val.len; i++ )
2447 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002448 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 break;
2450
2451 c = name->val.p[i];
2452 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2453 s[i] = '?';
2454 else s[i] = c;
2455 }
2456 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002457 ret = snprintf( p, n, "%s", s );
2458 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 name = name->next;
2460 }
2461
Paul Bakker23986e52011-04-24 08:57:21 +00002462 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463}
2464
2465/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002466 * Store the serial in printable form into buf; no more
2467 * than size characters will be written
2468 */
2469int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2470{
Paul Bakker23986e52011-04-24 08:57:21 +00002471 int ret;
2472 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002473 char *p;
2474
2475 p = buf;
2476 n = size;
2477
2478 nr = ( serial->len <= 32 )
2479 ? serial->len : 32;
2480
2481 for( i = 0; i < nr; i++ )
2482 {
2483 ret = snprintf( p, n, "%02X%s",
2484 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2485 SAFE_SNPRINTF();
2486 }
2487
Paul Bakker23986e52011-04-24 08:57:21 +00002488 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002489}
2490
2491/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002492 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002494int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2495 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496{
Paul Bakker23986e52011-04-24 08:57:21 +00002497 int ret;
2498 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
2501 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002502 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Paul Bakkerd98030e2009-05-02 15:13:40 +00002504 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 SAFE_SNPRINTF();
2507 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002509 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Paul Bakkerdd476992011-01-16 21:34:59 +00002511 ret = x509parse_serial_gets( p, n, &crt->serial);
2512 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002513
Paul Bakkerd98030e2009-05-02 15:13:40 +00002514 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2515 SAFE_SNPRINTF();
2516 ret = x509parse_dn_gets( p, n, &crt->issuer );
2517 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Paul Bakkerd98030e2009-05-02 15:13:40 +00002519 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2520 SAFE_SNPRINTF();
2521 ret = x509parse_dn_gets( p, n, &crt->subject );
2522 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Paul Bakkerd98030e2009-05-02 15:13:40 +00002524 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2526 crt->valid_from.year, crt->valid_from.mon,
2527 crt->valid_from.day, crt->valid_from.hour,
2528 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002529 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Paul Bakkerd98030e2009-05-02 15:13:40 +00002531 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2533 crt->valid_to.year, crt->valid_to.mon,
2534 crt->valid_to.day, crt->valid_to.hour,
2535 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002536 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Paul Bakkerd98030e2009-05-02 15:13:40 +00002538 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2539 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Paul Bakker27d66162010-03-17 06:56:01 +00002541 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002543 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2544 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2545 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2546 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2547 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2548 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2549 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2550 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2551 default: ret = snprintf( p, n, "???" ); break;
2552 }
2553 SAFE_SNPRINTF();
2554
2555 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002556 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002557 SAFE_SNPRINTF();
2558
Paul Bakker23986e52011-04-24 08:57:21 +00002559 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002560}
2561
Paul Bakker74111d32011-01-15 16:57:55 +00002562/* Compare a given OID string with an OID x509_buf * */
2563#define OID_CMP(oid_str, oid_buf) \
2564 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2565 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2566
2567/*
2568 * Return an informational string describing the given OID
2569 */
2570const char *x509_oid_get_description( x509_buf *oid )
2571{
2572 if ( oid == NULL )
2573 return ( NULL );
2574
2575 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2576 return( STRING_SERVER_AUTH );
2577
2578 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2579 return( STRING_CLIENT_AUTH );
2580
2581 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2582 return( STRING_CODE_SIGNING );
2583
2584 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2585 return( STRING_EMAIL_PROTECTION );
2586
2587 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2588 return( STRING_TIME_STAMPING );
2589
2590 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2591 return( STRING_OCSP_SIGNING );
2592
2593 return( NULL );
2594}
2595
2596/* Return the x.y.z.... style numeric string for the given OID */
2597int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2598{
Paul Bakker23986e52011-04-24 08:57:21 +00002599 int ret;
2600 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002601 unsigned int value;
2602 char *p;
2603
2604 p = buf;
2605 n = size;
2606
2607 /* First byte contains first two dots */
2608 if( oid->len > 0 )
2609 {
2610 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2611 SAFE_SNPRINTF();
2612 }
2613
2614 /* TODO: value can overflow in value. */
2615 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002616 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002617 {
2618 value <<= 7;
2619 value += oid->p[i] & 0x7F;
2620
2621 if( !( oid->p[i] & 0x80 ) )
2622 {
2623 /* Last byte */
2624 ret = snprintf( p, n, ".%d", value );
2625 SAFE_SNPRINTF();
2626 value = 0;
2627 }
2628 }
2629
Paul Bakker23986e52011-04-24 08:57:21 +00002630 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002631}
2632
Paul Bakkerd98030e2009-05-02 15:13:40 +00002633/*
2634 * Return an informational string about the CRL.
2635 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002636int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2637 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002638{
Paul Bakker23986e52011-04-24 08:57:21 +00002639 int ret;
2640 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002642 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643
2644 p = buf;
2645 n = size;
2646
2647 ret = snprintf( p, n, "%sCRL version : %d",
2648 prefix, crl->version );
2649 SAFE_SNPRINTF();
2650
2651 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2652 SAFE_SNPRINTF();
2653 ret = x509parse_dn_gets( p, n, &crl->issuer );
2654 SAFE_SNPRINTF();
2655
2656 ret = snprintf( p, n, "\n%sthis update : " \
2657 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2658 crl->this_update.year, crl->this_update.mon,
2659 crl->this_update.day, crl->this_update.hour,
2660 crl->this_update.min, crl->this_update.sec );
2661 SAFE_SNPRINTF();
2662
2663 ret = snprintf( p, n, "\n%snext update : " \
2664 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2665 crl->next_update.year, crl->next_update.mon,
2666 crl->next_update.day, crl->next_update.hour,
2667 crl->next_update.min, crl->next_update.sec );
2668 SAFE_SNPRINTF();
2669
2670 entry = &crl->entry;
2671
2672 ret = snprintf( p, n, "\n%sRevoked certificates:",
2673 prefix );
2674 SAFE_SNPRINTF();
2675
Paul Bakker9be19372009-07-27 20:21:53 +00002676 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002677 {
2678 ret = snprintf( p, n, "\n%sserial number: ",
2679 prefix );
2680 SAFE_SNPRINTF();
2681
2682 nr = ( entry->serial.len <= 32 )
2683 ? entry->serial.len : 32;
2684
Paul Bakker74111d32011-01-15 16:57:55 +00002685 for( i = 0; i < nr; i++ )
2686 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002687 ret = snprintf( p, n, "%02X%s",
2688 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2689 SAFE_SNPRINTF();
2690 }
2691
2692 ret = snprintf( p, n, " revocation date: " \
2693 "%04d-%02d-%02d %02d:%02d:%02d",
2694 entry->revocation_date.year, entry->revocation_date.mon,
2695 entry->revocation_date.day, entry->revocation_date.hour,
2696 entry->revocation_date.min, entry->revocation_date.sec );
2697 SAFE_SNPRINTF();
2698
2699 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 }
2701
Paul Bakkerd98030e2009-05-02 15:13:40 +00002702 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2703 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
Paul Bakker27d66162010-03-17 06:56:01 +00002705 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706 {
2707 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2708 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2709 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2710 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2711 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2712 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2713 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2714 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2715 default: ret = snprintf( p, n, "???" ); break;
2716 }
2717 SAFE_SNPRINTF();
2718
Paul Bakker1e27bb22009-07-19 20:25:25 +00002719 ret = snprintf( p, n, "\n" );
2720 SAFE_SNPRINTF();
2721
Paul Bakker23986e52011-04-24 08:57:21 +00002722 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723}
2724
2725/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002726 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002728int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002729{
2730 struct tm *lt;
2731 time_t tt;
2732
2733 tt = time( NULL );
2734 lt = localtime( &tt );
2735
Paul Bakker40ea7de2009-05-03 10:18:48 +00002736 if( lt->tm_year > to->year - 1900 )
2737 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Paul Bakker40ea7de2009-05-03 10:18:48 +00002739 if( lt->tm_year == to->year - 1900 &&
2740 lt->tm_mon > to->mon - 1 )
2741 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakker40ea7de2009-05-03 10:18:48 +00002743 if( lt->tm_year == to->year - 1900 &&
2744 lt->tm_mon == to->mon - 1 &&
2745 lt->tm_mday > to->day )
2746 return( 1 );
2747
Paul Bakkerb6194992011-01-16 21:40:22 +00002748 if( lt->tm_year == to->year - 1900 &&
2749 lt->tm_mon == to->mon - 1 &&
2750 lt->tm_mday == to->day &&
2751 lt->tm_hour > to->hour - 1)
2752 return( 1 );
2753
2754 if( lt->tm_year == to->year - 1900 &&
2755 lt->tm_mon == to->mon - 1 &&
2756 lt->tm_mday == to->day &&
2757 lt->tm_hour == to->hour - 1 &&
2758 lt->tm_min > to->min - 1 )
2759 return( 1 );
2760
2761 if( lt->tm_year == to->year - 1900 &&
2762 lt->tm_mon == to->mon - 1 &&
2763 lt->tm_mday == to->day &&
2764 lt->tm_hour == to->hour - 1 &&
2765 lt->tm_min == to->min - 1 &&
2766 lt->tm_sec > to->sec - 1 )
2767 return( 1 );
2768
Paul Bakker40ea7de2009-05-03 10:18:48 +00002769 return( 0 );
2770}
2771
2772/*
2773 * Return 1 if the certificate is revoked, or 0 otherwise.
2774 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002775int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002776{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002777 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002778
2779 while( cur != NULL && cur->serial.len != 0 )
2780 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002781 if( crt->serial.len == cur->serial.len &&
2782 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002783 {
2784 if( x509parse_time_expired( &cur->revocation_date ) )
2785 return( 1 );
2786 }
2787
2788 cur = cur->next;
2789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
2791 return( 0 );
2792}
2793
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002794/*
2795 * Wrapper for x509 hashes.
2796 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002797 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002798 */
Paul Bakker23986e52011-04-24 08:57:21 +00002799static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002800 unsigned char *out )
2801{
2802 switch( alg )
2803 {
Paul Bakker40e46942009-01-03 21:51:57 +00002804#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002805 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002806#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002807#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002808 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002810#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002811 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002812#endif
2813#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002814 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002815#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002816#if defined(POLARSSL_SHA2_C)
2817 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2818 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2819#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002820#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002821 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2822 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2823#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002825 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 break;
2827 }
2828}
2829
2830/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002831 * Check that the given certificate is valid accoring to the CRL.
2832 */
2833static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2834 x509_crl *crl_list)
2835{
2836 int flags = 0;
2837 int hash_id;
2838 unsigned char hash[64];
2839
2840 /*
2841 * TODO: What happens if no CRL is present?
2842 * Suggestion: Revocation state should be unknown if no CRL is present.
2843 * For backwards compatibility this is not yet implemented.
2844 */
2845
2846 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2847 {
2848 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2849 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2850 crl_list->issuer_raw.len ) != 0 )
2851 {
2852 crl_list = crl_list->next;
2853 continue;
2854 }
2855
2856 /*
2857 * Check if CRL is correctly signed by the trusted CA
2858 */
2859 hash_id = crl_list->sig_alg;
2860
2861 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2862
2863 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2864 0, hash, crl_list->sig.p ) == 0 )
2865 {
2866 /*
2867 * CRL is not trusted
2868 */
2869 flags |= BADCRL_NOT_TRUSTED;
2870 break;
2871 }
2872
2873 /*
2874 * Check for validity of CRL (Do not drop out)
2875 */
2876 if( x509parse_time_expired( &crl_list->next_update ) )
2877 flags |= BADCRL_EXPIRED;
2878
2879 /*
2880 * Check if certificate is revoked
2881 */
2882 if( x509parse_revoked(crt, crl_list) )
2883 {
2884 flags |= BADCERT_REVOKED;
2885 break;
2886 }
2887
2888 crl_list = crl_list->next;
2889 }
2890 return flags;
2891}
2892
2893/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 * Verify the certificate validity
2895 */
2896int x509parse_verify( x509_cert *crt,
2897 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002898 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002899 const char *cn, int *flags,
2900 int (*f_vrfy)(void *, x509_cert *, int, int),
2901 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002902{
Paul Bakker23986e52011-04-24 08:57:21 +00002903 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 int hash_id;
2905 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002906 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002908 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakker40ea7de2009-05-03 10:18:48 +00002910 *flags = 0;
2911
2912 if( x509parse_time_expired( &crt->valid_to ) )
2913 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
2915 if( cn != NULL )
2916 {
2917 name = &crt->subject;
2918 cn_len = strlen( cn );
2919
2920 while( name != NULL )
2921 {
2922 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2923 memcmp( name->val.p, cn, cn_len ) == 0 &&
2924 name->val.len == cn_len )
2925 break;
2926
2927 name = name->next;
2928 }
2929
2930 if( name == NULL )
2931 *flags |= BADCERT_CN_MISMATCH;
2932 }
2933
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 /*
2935 * Iterate upwards in the given cert chain,
2936 * ignoring any upper cert with CA != TRUE.
2937 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002938 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 pathlen = 1;
2941
Paul Bakker76fd75a2011-01-16 21:12:10 +00002942 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002944 if( parent->ca_istrue == 0 ||
2945 crt->issuer_raw.len != parent->subject_raw.len ||
2946 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 crt->issuer_raw.len ) != 0 )
2948 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002949 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 continue;
2951 }
2952
Paul Bakker27d66162010-03-17 06:56:01 +00002953 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
2955 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2956
Paul Bakker76fd75a2011-01-16 21:12:10 +00002957 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2958 crt->sig.p ) != 0 )
2959 *flags |= BADCERT_NOT_TRUSTED;
2960
2961 /* Check trusted CA's CRL for the given crt */
2962 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002963
2964 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002965 if( NULL != f_vrfy )
2966 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002967 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002968 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002969 else
2970 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002971 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002972 else if( *flags != 0 )
2973 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
2975 pathlen++;
2976
Paul Bakker76fd75a2011-01-16 21:12:10 +00002977 crt = parent;
2978 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980
2981 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002982 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002984 *flags |= BADCERT_NOT_TRUSTED;
2985
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002986 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 {
2988 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2989 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2990 crt->issuer_raw.len ) != 0 )
2991 {
2992 trust_ca = trust_ca->next;
2993 continue;
2994 }
2995
2996 if( trust_ca->max_pathlen > 0 &&
2997 trust_ca->max_pathlen < pathlen )
2998 break;
2999
Paul Bakker27d66162010-03-17 06:56:01 +00003000 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
3002 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3003
3004 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3005 0, hash, crt->sig.p ) == 0 )
3006 {
3007 /*
3008 * cert. is signed by a trusted CA
3009 */
3010 *flags &= ~BADCERT_NOT_TRUSTED;
3011 break;
3012 }
3013
3014 trust_ca = trust_ca->next;
3015 }
3016
Paul Bakker76fd75a2011-01-16 21:12:10 +00003017 /* Check trusted CA's CRL for the given crt */
3018 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003019
3020 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003021 if( NULL != f_vrfy )
3022 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003023 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003024 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003025 else
3026 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003027 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003028 else if( *flags != 0 )
3029 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003030
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 return( 0 );
3032}
3033
3034/*
3035 * Unallocate all certificate data
3036 */
3037void x509_free( x509_cert *crt )
3038{
3039 x509_cert *cert_cur = crt;
3040 x509_cert *cert_prv;
3041 x509_name *name_cur;
3042 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003043 x509_sequence *seq_cur;
3044 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
3046 if( crt == NULL )
3047 return;
3048
3049 do
3050 {
3051 rsa_free( &cert_cur->rsa );
3052
3053 name_cur = cert_cur->issuer.next;
3054 while( name_cur != NULL )
3055 {
3056 name_prv = name_cur;
3057 name_cur = name_cur->next;
3058 memset( name_prv, 0, sizeof( x509_name ) );
3059 free( name_prv );
3060 }
3061
3062 name_cur = cert_cur->subject.next;
3063 while( name_cur != NULL )
3064 {
3065 name_prv = name_cur;
3066 name_cur = name_cur->next;
3067 memset( name_prv, 0, sizeof( x509_name ) );
3068 free( name_prv );
3069 }
3070
Paul Bakker74111d32011-01-15 16:57:55 +00003071 seq_cur = cert_cur->ext_key_usage.next;
3072 while( seq_cur != NULL )
3073 {
3074 seq_prv = seq_cur;
3075 seq_cur = seq_cur->next;
3076 memset( seq_prv, 0, sizeof( x509_sequence ) );
3077 free( seq_prv );
3078 }
3079
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 if( cert_cur->raw.p != NULL )
3081 {
3082 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3083 free( cert_cur->raw.p );
3084 }
3085
3086 cert_cur = cert_cur->next;
3087 }
3088 while( cert_cur != NULL );
3089
3090 cert_cur = crt;
3091 do
3092 {
3093 cert_prv = cert_cur;
3094 cert_cur = cert_cur->next;
3095
3096 memset( cert_prv, 0, sizeof( x509_cert ) );
3097 if( cert_prv != crt )
3098 free( cert_prv );
3099 }
3100 while( cert_cur != NULL );
3101}
3102
Paul Bakkerd98030e2009-05-02 15:13:40 +00003103/*
3104 * Unallocate all CRL data
3105 */
3106void x509_crl_free( x509_crl *crl )
3107{
3108 x509_crl *crl_cur = crl;
3109 x509_crl *crl_prv;
3110 x509_name *name_cur;
3111 x509_name *name_prv;
3112 x509_crl_entry *entry_cur;
3113 x509_crl_entry *entry_prv;
3114
3115 if( crl == NULL )
3116 return;
3117
3118 do
3119 {
3120 name_cur = crl_cur->issuer.next;
3121 while( name_cur != NULL )
3122 {
3123 name_prv = name_cur;
3124 name_cur = name_cur->next;
3125 memset( name_prv, 0, sizeof( x509_name ) );
3126 free( name_prv );
3127 }
3128
3129 entry_cur = crl_cur->entry.next;
3130 while( entry_cur != NULL )
3131 {
3132 entry_prv = entry_cur;
3133 entry_cur = entry_cur->next;
3134 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3135 free( entry_prv );
3136 }
3137
3138 if( crl_cur->raw.p != NULL )
3139 {
3140 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3141 free( crl_cur->raw.p );
3142 }
3143
3144 crl_cur = crl_cur->next;
3145 }
3146 while( crl_cur != NULL );
3147
3148 crl_cur = crl;
3149 do
3150 {
3151 crl_prv = crl_cur;
3152 crl_cur = crl_cur->next;
3153
3154 memset( crl_prv, 0, sizeof( x509_crl ) );
3155 if( crl_prv != crl )
3156 free( crl_prv );
3157 }
3158 while( crl_cur != NULL );
3159}
3160
Paul Bakker40e46942009-01-03 21:51:57 +00003161#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Paul Bakker40e46942009-01-03 21:51:57 +00003163#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
3165/*
3166 * Checkup routine
3167 */
3168int x509_self_test( int verbose )
3169{
Paul Bakker5690efc2011-05-26 13:16:06 +00003170#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003171 int ret;
3172 int flags;
3173 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 x509_cert cacert;
3175 x509_cert clicert;
3176 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003177#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003178 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003179#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
3181 if( verbose != 0 )
3182 printf( " X.509 certificate load: " );
3183
3184 memset( &clicert, 0, sizeof( x509_cert ) );
3185
3186 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3187 strlen( test_cli_crt ) );
3188 if( ret != 0 )
3189 {
3190 if( verbose != 0 )
3191 printf( "failed\n" );
3192
3193 return( ret );
3194 }
3195
3196 memset( &cacert, 0, sizeof( x509_cert ) );
3197
3198 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3199 strlen( test_ca_crt ) );
3200 if( ret != 0 )
3201 {
3202 if( verbose != 0 )
3203 printf( "failed\n" );
3204
3205 return( ret );
3206 }
3207
3208 if( verbose != 0 )
3209 printf( "passed\n X.509 private key load: " );
3210
3211 i = strlen( test_ca_key );
3212 j = strlen( test_ca_pwd );
3213
Paul Bakker66b78b22011-03-25 14:22:50 +00003214 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3215
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 if( ( ret = x509parse_key( &rsa,
3217 (unsigned char *) test_ca_key, i,
3218 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3219 {
3220 if( verbose != 0 )
3221 printf( "failed\n" );
3222
3223 return( ret );
3224 }
3225
3226 if( verbose != 0 )
3227 printf( "passed\n X.509 signature verify: ");
3228
Paul Bakker23986e52011-04-24 08:57:21 +00003229 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 if( ret != 0 )
3231 {
Paul Bakker23986e52011-04-24 08:57:21 +00003232 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 if( verbose != 0 )
3234 printf( "failed\n" );
3235
3236 return( ret );
3237 }
3238
Paul Bakker5690efc2011-05-26 13:16:06 +00003239#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003241 printf( "passed\n X.509 DHM parameter load: " );
3242
3243 i = strlen( test_dhm_params );
3244 j = strlen( test_ca_pwd );
3245
3246 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3247 {
3248 if( verbose != 0 )
3249 printf( "failed\n" );
3250
3251 return( ret );
3252 }
3253
3254 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258 x509_free( &cacert );
3259 x509_free( &clicert );
3260 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003261#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003262 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003263#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003266#else
3267 ((void) verbose);
3268 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003270}
3271
3272#endif
3273
3274#endif