blob: 874cf0bb18ceeeb217e3e45b417300ed20afdab7 [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 Bakker74111d32011-01-15 16:57:55 +00001016 if( is_critical )
1017 {
1018 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001019 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001020 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1021 }
1022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 }
1024
1025 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001026 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001027 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 return( 0 );
1030}
1031
1032/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001033 * X.509 CRL Entries
1034 */
1035static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001036 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 x509_crl_entry *entry )
1038{
Paul Bakker23986e52011-04-24 08:57:21 +00001039 int ret;
1040 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *cur_entry = entry;
1042
1043 if( *p == end )
1044 return( 0 );
1045
Paul Bakker9be19372009-07-27 20:21:53 +00001046 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001047 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1048 {
1049 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1050 return( 0 );
1051
1052 return( ret );
1053 }
1054
Paul Bakker9be19372009-07-27 20:21:53 +00001055 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056
1057 while( *p < end )
1058 {
Paul Bakker23986e52011-04-24 08:57:21 +00001059 size_t len2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001060
1061 if( ( ret = asn1_get_tag( p, end, &len2,
1062 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1063 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001064 return( ret );
1065 }
1066
Paul Bakker9be19372009-07-27 20:21:53 +00001067 cur_entry->raw.tag = **p;
1068 cur_entry->raw.p = *p;
1069 cur_entry->raw.len = len2;
1070
Paul Bakkerd98030e2009-05-02 15:13:40 +00001071 if( ( ret = x509_get_serial( p, end, &cur_entry->serial ) ) != 0 )
1072 return( ret );
1073
Paul Bakker91200182010-02-18 21:26:15 +00001074 if( ( ret = x509_get_time( p, end, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 return( ret );
1076
1077 if( ( ret = x509_get_crl_ext( p, end, &cur_entry->entry_ext ) ) != 0 )
1078 return( ret );
1079
Paul Bakker74111d32011-01-15 16:57:55 +00001080 if ( *p < end )
1081 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
1083 cur_entry = cur_entry->next;
1084 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1085 }
1086 }
1087
1088 return( 0 );
1089}
1090
Paul Bakker27d66162010-03-17 06:56:01 +00001091static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1092{
1093 if( sig_oid->len == 9 &&
1094 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1095 {
1096 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1097 {
1098 *sig_alg = sig_oid->p[8];
1099 return( 0 );
1100 }
1101
1102 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1103 {
1104 *sig_alg = sig_oid->p[8];
1105 return( 0 );
1106 }
1107
1108 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1109 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001110 if( sig_oid->len == 5 &&
1111 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1112 {
1113 *sig_alg = SIG_RSA_SHA1;
1114 return( 0 );
1115 }
Paul Bakker27d66162010-03-17 06:56:01 +00001116
1117 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1118}
1119
Paul Bakkerd98030e2009-05-02 15:13:40 +00001120/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 * Parse one or more certificates and add them to the chained list
1122 */
Paul Bakker23986e52011-04-24 08:57:21 +00001123int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124{
Paul Bakker23986e52011-04-24 08:57:21 +00001125 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001126 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 unsigned char *p, *end;
1128 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001129#if defined(POLARSSL_PEM_C)
1130 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001131 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001132#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 crt = chain;
1135
Paul Bakker320a4b52009-03-28 18:52:39 +00001136 /*
1137 * Check for valid input
1138 */
1139 if( crt == NULL || buf == NULL )
1140 return( 1 );
1141
Paul Bakkere9581d62009-03-28 20:29:25 +00001142 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001143 crt = crt->next;
1144
1145 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001146 * Add new certificate on the end of the chain if needed.
1147 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001148 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001149 {
1150 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1151
Paul Bakker7d06ad22009-05-02 15:53:56 +00001152 if( crt->next == NULL )
1153 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001154 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001155 return( 1 );
1156 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001157
Paul Bakker7d06ad22009-05-02 15:53:56 +00001158 crt = crt->next;
1159 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001160 }
1161
Paul Bakker96743fc2011-02-12 14:30:57 +00001162#if defined(POLARSSL_PEM_C)
1163 pem_init( &pem );
1164 ret = pem_read_buffer( &pem,
1165 "-----BEGIN CERTIFICATE-----",
1166 "-----END CERTIFICATE-----",
1167 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
Paul Bakker96743fc2011-02-12 14:30:57 +00001169 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001171 /*
1172 * Was PEM encoded
1173 */
1174 buflen -= use_len;
1175 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
1177 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001178 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001180 p = pem.buf;
1181 pem.buf = NULL;
1182 len = pem.buflen;
1183 pem_free( &pem );
1184 }
1185 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1186 {
1187 pem_free( &pem );
1188 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190 else
1191 {
1192 /*
1193 * nope, copy the raw DER data
1194 */
1195 p = (unsigned char *) malloc( len = buflen );
1196
1197 if( p == NULL )
1198 return( 1 );
1199
1200 memcpy( p, buf, buflen );
1201
1202 buflen = 0;
1203 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001204#else
1205 p = (unsigned char *) malloc( len = buflen );
1206
1207 if( p == NULL )
1208 return( 1 );
1209
1210 memcpy( p, buf, buflen );
1211
1212 buflen = 0;
1213#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 crt->raw.p = p;
1216 crt->raw.len = len;
1217 end = p + len;
1218
1219 /*
1220 * Certificate ::= SEQUENCE {
1221 * tbsCertificate TBSCertificate,
1222 * signatureAlgorithm AlgorithmIdentifier,
1223 * signatureValue BIT STRING }
1224 */
1225 if( ( ret = asn1_get_tag( &p, end, &len,
1226 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1227 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001228 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001229 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
Paul Bakker23986e52011-04-24 08:57:21 +00001232 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001234 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001235 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001236 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
1238
1239 /*
1240 * TBSCertificate ::= SEQUENCE {
1241 */
1242 crt->tbs.p = p;
1243
1244 if( ( ret = asn1_get_tag( &p, end, &len,
1245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1246 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001248 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 }
1250
1251 end = p + len;
1252 crt->tbs.len = end - crt->tbs.p;
1253
1254 /*
1255 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1256 *
1257 * CertificateSerialNumber ::= INTEGER
1258 *
1259 * signature AlgorithmIdentifier
1260 */
1261 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1262 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1263 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 crt->version++;
1270
1271 if( crt->version > 3 )
1272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001274 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 }
1276
Paul Bakker27d66162010-03-17 06:56:01 +00001277 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001279 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001280 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
1283 /*
1284 * issuer Name
1285 */
1286 crt->issuer_raw.p = p;
1287
1288 if( ( ret = asn1_get_tag( &p, end, &len,
1289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
1295 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->issuer_raw.len = p - crt->issuer_raw.p;
1302
1303 /*
1304 * Validity ::= SEQUENCE {
1305 * notBefore Time,
1306 * notAfter Time }
1307 *
1308 */
1309 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1310 &crt->valid_to ) ) != 0 )
1311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 return( ret );
1314 }
1315
1316 /*
1317 * subject Name
1318 */
1319 crt->subject_raw.p = p;
1320
1321 if( ( ret = asn1_get_tag( &p, end, &len,
1322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
1328 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 crt->subject_raw.len = p - crt->subject_raw.p;
1335
1336 /*
1337 * SubjectPublicKeyInfo ::= SEQUENCE
1338 * algorithm AlgorithmIdentifier,
1339 * subjectPublicKey BIT STRING }
1340 */
1341 if( ( ret = asn1_get_tag( &p, end, &len,
1342 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001345 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
1348 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1349 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1350 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001351 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 return( ret );
1353 }
1354
1355 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 return( ret );
1359 }
1360
1361 crt->rsa.len = mpi_size( &crt->rsa.N );
1362
1363 /*
1364 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1365 * -- If present, version shall be v2 or v3
1366 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1367 * -- If present, version shall be v2 or v3
1368 * extensions [3] EXPLICIT Extensions OPTIONAL
1369 * -- If present, version shall be v3
1370 */
1371 if( crt->version == 2 || crt->version == 3 )
1372 {
1373 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1374 if( ret != 0 )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 return( ret );
1378 }
1379 }
1380
1381 if( crt->version == 2 || crt->version == 3 )
1382 {
1383 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1384 if( ret != 0 )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 return( ret );
1388 }
1389 }
1390
1391 if( crt->version == 3 )
1392 {
Paul Bakker74111d32011-01-15 16:57:55 +00001393 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 if( ret != 0 )
1395 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001396 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 return( ret );
1398 }
1399 }
1400
1401 if( p != end )
1402 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001403 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001404 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001405 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 }
1407
1408 end = crt->raw.p + crt->raw.len;
1409
1410 /*
1411 * signatureAlgorithm AlgorithmIdentifier,
1412 * signatureValue BIT STRING
1413 */
1414 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1415 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 return( ret );
1418 }
1419
Paul Bakker320a4b52009-03-28 18:52:39 +00001420 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001423 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 }
1425
1426 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1427 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001428 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 return( ret );
1430 }
1431
1432 if( p != end )
1433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001435 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001436 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
1438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001440 {
1441 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1442
Paul Bakker7d06ad22009-05-02 15:53:56 +00001443 if( crt->next == NULL )
1444 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001445 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 return( 1 );
1447 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001448
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 crt = crt->next;
1450 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001451
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
1455 return( 0 );
1456}
1457
1458/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001459 * Parse one or more CRLs and add them to the chained list
1460 */
Paul Bakker23986e52011-04-24 08:57:21 +00001461int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001462{
Paul Bakker23986e52011-04-24 08:57:21 +00001463 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001464 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001465 unsigned char *p, *end;
1466 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001467#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001468 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001469 pem_context pem;
1470#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001471
1472 crl = chain;
1473
1474 /*
1475 * Check for valid input
1476 */
1477 if( crl == NULL || buf == NULL )
1478 return( 1 );
1479
1480 while( crl->version != 0 && crl->next != NULL )
1481 crl = crl->next;
1482
1483 /*
1484 * Add new CRL on the end of the chain if needed.
1485 */
1486 if ( crl->version != 0 && crl->next == NULL)
1487 {
1488 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1489
Paul Bakker7d06ad22009-05-02 15:53:56 +00001490 if( crl->next == NULL )
1491 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001493 return( 1 );
1494 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001495
Paul Bakker7d06ad22009-05-02 15:53:56 +00001496 crl = crl->next;
1497 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498 }
1499
Paul Bakker96743fc2011-02-12 14:30:57 +00001500#if defined(POLARSSL_PEM_C)
1501 pem_init( &pem );
1502 ret = pem_read_buffer( &pem,
1503 "-----BEGIN X509 CRL-----",
1504 "-----END X509 CRL-----",
1505 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506
Paul Bakker96743fc2011-02-12 14:30:57 +00001507 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001508 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001509 /*
1510 * Was PEM encoded
1511 */
1512 buflen -= use_len;
1513 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001514
1515 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001516 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001517 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001518 p = pem.buf;
1519 pem.buf = NULL;
1520 len = pem.buflen;
1521 pem_free( &pem );
1522 }
1523 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1524 {
1525 pem_free( &pem );
1526 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001527 }
1528 else
1529 {
1530 /*
1531 * nope, copy the raw DER data
1532 */
1533 p = (unsigned char *) malloc( len = buflen );
1534
1535 if( p == NULL )
1536 return( 1 );
1537
1538 memcpy( p, buf, buflen );
1539
1540 buflen = 0;
1541 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001542#else
1543 p = (unsigned char *) malloc( len = buflen );
1544
1545 if( p == NULL )
1546 return( 1 );
1547
1548 memcpy( p, buf, buflen );
1549
1550 buflen = 0;
1551#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552
1553 crl->raw.p = p;
1554 crl->raw.len = len;
1555 end = p + len;
1556
1557 /*
1558 * CertificateList ::= SEQUENCE {
1559 * tbsCertList TBSCertList,
1560 * signatureAlgorithm AlgorithmIdentifier,
1561 * signatureValue BIT STRING }
1562 */
1563 if( ( ret = asn1_get_tag( &p, end, &len,
1564 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1565 {
1566 x509_crl_free( crl );
1567 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1568 }
1569
Paul Bakker23986e52011-04-24 08:57:21 +00001570 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 {
1572 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001573 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1575 }
1576
1577 /*
1578 * TBSCertList ::= SEQUENCE {
1579 */
1580 crl->tbs.p = p;
1581
1582 if( ( ret = asn1_get_tag( &p, end, &len,
1583 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1584 {
1585 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001586 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 }
1588
1589 end = p + len;
1590 crl->tbs.len = end - crl->tbs.p;
1591
1592 /*
1593 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1594 * -- if present, MUST be v2
1595 *
1596 * signature AlgorithmIdentifier
1597 */
1598 if( ( ret = x509_get_version( &p, end, &crl->version ) ) != 0 ||
1599 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1600 {
1601 x509_crl_free( crl );
1602 return( ret );
1603 }
1604
1605 crl->version++;
1606
1607 if( crl->version > 2 )
1608 {
1609 x509_crl_free( crl );
1610 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1611 }
1612
Paul Bakker27d66162010-03-17 06:56:01 +00001613 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 {
1615 x509_crl_free( crl );
1616 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1617 }
1618
1619 /*
1620 * issuer Name
1621 */
1622 crl->issuer_raw.p = p;
1623
1624 if( ( ret = asn1_get_tag( &p, end, &len,
1625 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1626 {
1627 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001628 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 }
1630
1631 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1632 {
1633 x509_crl_free( crl );
1634 return( ret );
1635 }
1636
1637 crl->issuer_raw.len = p - crl->issuer_raw.p;
1638
1639 /*
1640 * thisUpdate Time
1641 * nextUpdate Time OPTIONAL
1642 */
Paul Bakker91200182010-02-18 21:26:15 +00001643 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644 {
1645 x509_crl_free( crl );
1646 return( ret );
1647 }
1648
Paul Bakker91200182010-02-18 21:26:15 +00001649 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 {
Paul Bakker9d781402011-05-09 16:17:09 +00001651 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001652 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001653 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001654 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001655 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 x509_crl_free( crl );
1657 return( ret );
1658 }
1659 }
1660
1661 /*
1662 * revokedCertificates SEQUENCE OF SEQUENCE {
1663 * userCertificate CertificateSerialNumber,
1664 * revocationDate Time,
1665 * crlEntryExtensions Extensions OPTIONAL
1666 * -- if present, MUST be v2
1667 * } OPTIONAL
1668 */
1669 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1670 {
1671 x509_crl_free( crl );
1672 return( ret );
1673 }
1674
1675 /*
1676 * crlExtensions EXPLICIT Extensions OPTIONAL
1677 * -- if present, MUST be v2
1678 */
1679 if( crl->version == 2 )
1680 {
1681 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1682
1683 if( ret != 0 )
1684 {
1685 x509_crl_free( crl );
1686 return( ret );
1687 }
1688 }
1689
1690 if( p != end )
1691 {
1692 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001693 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1695 }
1696
1697 end = crl->raw.p + crl->raw.len;
1698
1699 /*
1700 * signatureAlgorithm AlgorithmIdentifier,
1701 * signatureValue BIT STRING
1702 */
1703 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
1706 return( ret );
1707 }
1708
1709 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1710 {
1711 x509_crl_free( crl );
1712 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1713 }
1714
1715 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1716 {
1717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720
1721 if( p != end )
1722 {
1723 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001724 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1726 }
1727
1728 if( buflen > 0 )
1729 {
1730 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1731
Paul Bakker7d06ad22009-05-02 15:53:56 +00001732 if( crl->next == NULL )
1733 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001735 return( 1 );
1736 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001737
Paul Bakker7d06ad22009-05-02 15:53:56 +00001738 crl = crl->next;
1739 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740
1741 return( x509parse_crl( crl, buf, buflen ) );
1742 }
1743
1744 return( 0 );
1745}
1746
Paul Bakker335db3f2011-04-25 15:28:35 +00001747#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001748/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001749 * Load all data from a file into a given buffer.
1750 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001751int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001752{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001753 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001754
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 if( ( f = fopen( path, "rb" ) ) == NULL )
1756 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001757
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758 fseek( f, 0, SEEK_END );
1759 *n = (size_t) ftell( f );
1760 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001761
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1763 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001764
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765 if( fread( *buf, 1, *n, f ) != *n )
1766 {
1767 fclose( f );
1768 free( *buf );
1769 return( 1 );
1770 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001771
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001773
Paul Bakkerd98030e2009-05-02 15:13:40 +00001774 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001775
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001777}
1778
1779/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 * Load one or more certificates and add them to the chained list
1781 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001782int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783{
1784 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 size_t n;
1786 unsigned char *buf;
1787
Paul Bakker2b245eb2009-04-19 18:44:26 +00001788 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 return( 1 );
1790
Paul Bakker27fdf462011-06-09 13:55:13 +00001791 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 memset( buf, 0, n + 1 );
1794 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
1796 return( ret );
1797}
1798
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799/*
1800 * Load one or more CRLs and add them to the chained list
1801 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001802int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001803{
1804 int ret;
1805 size_t n;
1806 unsigned char *buf;
1807
1808 if ( load_file( path, &buf, &n ) )
1809 return( 1 );
1810
Paul Bakker27fdf462011-06-09 13:55:13 +00001811 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812
1813 memset( buf, 0, n + 1 );
1814 free( buf );
1815
1816 return( ret );
1817}
1818
Paul Bakker5121ce52009-01-03 21:22:43 +00001819/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001820 * Load and parse a private RSA key
1821 */
1822int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1823{
1824 int ret;
1825 size_t n;
1826 unsigned char *buf;
1827
1828 if ( load_file( path, &buf, &n ) )
1829 return( 1 );
1830
1831 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001832 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001833 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001834 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001835 (unsigned char *) pwd, strlen( pwd ) );
1836
1837 memset( buf, 0, n + 1 );
1838 free( buf );
1839
1840 return( ret );
1841}
1842
1843/*
1844 * Load and parse a public RSA key
1845 */
1846int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1847{
1848 int ret;
1849 size_t n;
1850 unsigned char *buf;
1851
1852 if ( load_file( path, &buf, &n ) )
1853 return( 1 );
1854
Paul Bakker27fdf462011-06-09 13:55:13 +00001855 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001856
1857 memset( buf, 0, n + 1 );
1858 free( buf );
1859
1860 return( ret );
1861}
1862#endif /* POLARSSL_FS_IO */
1863
1864/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 * Parse a private RSA key
1866 */
Paul Bakker23986e52011-04-24 08:57:21 +00001867int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1868 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001869{
Paul Bakker23986e52011-04-24 08:57:21 +00001870 int ret;
1871 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001873 unsigned char *p_alt;
1874 x509_buf pk_alg_oid;
1875
Paul Bakker96743fc2011-02-12 14:30:57 +00001876#if defined(POLARSSL_PEM_C)
1877 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Paul Bakker96743fc2011-02-12 14:30:57 +00001879 pem_init( &pem );
1880 ret = pem_read_buffer( &pem,
1881 "-----BEGIN RSA PRIVATE KEY-----",
1882 "-----END RSA PRIVATE KEY-----",
1883 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Paul Bakkered56b222011-07-13 11:26:43 +00001885 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1886 {
1887 ret = pem_read_buffer( &pem,
1888 "-----BEGIN PRIVATE KEY-----",
1889 "-----END PRIVATE KEY-----",
1890 key, pwd, pwdlen, &len );
1891 }
1892
Paul Bakker96743fc2011-02-12 14:30:57 +00001893 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001895 /*
1896 * Was PEM encoded
1897 */
1898 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001900 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001901 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001902 pem_free( &pem );
1903 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Paul Bakker96743fc2011-02-12 14:30:57 +00001906 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1907#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001908 ((void) pwd);
1909 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001910 p = (unsigned char *) key;
1911#endif
1912 end = p + keylen;
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001915 * Note: Depending on the type of private key file one can expect either a
1916 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1917 *
1918 * PrivateKeyInfo ::= SEQUENCE {
1919 * algorithm AlgorithmIdentifier,
1920 * PrivateKey BIT STRING
1921 * }
1922 *
1923 * AlgorithmIdentifier ::= SEQUENCE {
1924 * algorithm OBJECT IDENTIFIER,
1925 * parameters ANY DEFINED BY algorithm OPTIONAL
1926 * }
1927 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 * RSAPrivateKey ::= SEQUENCE {
1929 * version Version,
1930 * modulus INTEGER, -- n
1931 * publicExponent INTEGER, -- e
1932 * privateExponent INTEGER, -- d
1933 * prime1 INTEGER, -- p
1934 * prime2 INTEGER, -- q
1935 * exponent1 INTEGER, -- d mod (p-1)
1936 * exponent2 INTEGER, -- d mod (q-1)
1937 * coefficient INTEGER, -- (inverse of q) mod p
1938 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1939 * }
1940 */
1941 if( ( ret = asn1_get_tag( &p, end, &len,
1942 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1943 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001944#if defined(POLARSSL_PEM_C)
1945 pem_free( &pem );
1946#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001948 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 }
1950
1951 end = p + len;
1952
1953 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1954 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001955#if defined(POLARSSL_PEM_C)
1956 pem_free( &pem );
1957#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001959 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 }
1961
1962 if( rsa->ver != 0 )
1963 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001964#if defined(POLARSSL_PEM_C)
1965 pem_free( &pem );
1966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001968 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 }
1970
Paul Bakkered56b222011-07-13 11:26:43 +00001971 p_alt = p;
1972
1973 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1974 {
1975 // Assume that we have the PKCS#1 format if wrong
1976 // tag was encountered
1977 //
1978 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1979 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1980 {
1981#if defined(POLARSSL_PEM_C)
1982 pem_free( &pem );
1983#endif
1984 rsa_free( rsa );
1985 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1986 }
1987 }
1988 else
1989 {
1990 int can_handle;
1991
1992 /*
1993 * only RSA keys handled at this time
1994 */
1995 can_handle = 0;
1996
1997 if( pk_alg_oid.len == 9 &&
1998 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1999 can_handle = 1;
2000
2001 if( pk_alg_oid.len == 9 &&
2002 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2003 {
2004 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2005 can_handle = 1;
2006
2007 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2008 can_handle = 1;
2009 }
2010
2011 if( pk_alg_oid.len == 5 &&
2012 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2013 can_handle = 1;
2014
2015 if( can_handle == 0 )
2016 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2017
2018 /*
2019 * Parse the PKCS#8 format
2020 */
2021
2022 p = p_alt;
2023 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2024 {
2025#if defined(POLARSSL_PEM_C)
2026 pem_free( &pem );
2027#endif
2028 rsa_free( rsa );
2029 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2030 }
2031
2032 if( ( end - p ) < 1 )
2033 {
2034#if defined(POLARSSL_PEM_C)
2035 pem_free( &pem );
2036#endif
2037 rsa_free( rsa );
2038 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2039 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2040 }
2041
2042 end = p + len;
2043
2044 if( ( ret = asn1_get_tag( &p, end, &len,
2045 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2046 {
2047#if defined(POLARSSL_PEM_C)
2048 pem_free( &pem );
2049#endif
2050 rsa_free( rsa );
2051 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2052 }
2053
2054 end = p + len;
2055
2056 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2057 {
2058#if defined(POLARSSL_PEM_C)
2059 pem_free( &pem );
2060#endif
2061 rsa_free( rsa );
2062 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2063 }
2064
2065 if( rsa->ver != 0 )
2066 {
2067#if defined(POLARSSL_PEM_C)
2068 pem_free( &pem );
2069#endif
2070 rsa_free( rsa );
2071 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2072 }
2073 }
2074
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2076 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2077 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2078 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2079 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2080 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2081 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2082 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2083 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002084#if defined(POLARSSL_PEM_C)
2085 pem_free( &pem );
2086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002088 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
2091 rsa->len = mpi_size( &rsa->N );
2092
2093 if( p != end )
2094 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002095#if defined(POLARSSL_PEM_C)
2096 pem_free( &pem );
2097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002099 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002100 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 }
2102
2103 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2104 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002105#if defined(POLARSSL_PEM_C)
2106 pem_free( &pem );
2107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 rsa_free( rsa );
2109 return( ret );
2110 }
2111
Paul Bakker96743fc2011-02-12 14:30:57 +00002112#if defined(POLARSSL_PEM_C)
2113 pem_free( &pem );
2114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 return( 0 );
2117}
2118
2119/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002120 * Parse a public RSA key
2121 */
Paul Bakker23986e52011-04-24 08:57:21 +00002122int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002123{
Paul Bakker23986e52011-04-24 08:57:21 +00002124 int ret;
2125 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002126 unsigned char *p, *end;
2127 x509_buf alg_oid;
2128#if defined(POLARSSL_PEM_C)
2129 pem_context pem;
2130
2131 pem_init( &pem );
2132 ret = pem_read_buffer( &pem,
2133 "-----BEGIN PUBLIC KEY-----",
2134 "-----END PUBLIC KEY-----",
2135 key, NULL, 0, &len );
2136
2137 if( ret == 0 )
2138 {
2139 /*
2140 * Was PEM encoded
2141 */
2142 keylen = pem.buflen;
2143 }
2144 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2145 {
2146 pem_free( &pem );
2147 return( ret );
2148 }
2149
2150 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2151#else
2152 p = (unsigned char *) key;
2153#endif
2154 end = p + keylen;
2155
2156 /*
2157 * PublicKeyInfo ::= SEQUENCE {
2158 * algorithm AlgorithmIdentifier,
2159 * PublicKey BIT STRING
2160 * }
2161 *
2162 * AlgorithmIdentifier ::= SEQUENCE {
2163 * algorithm OBJECT IDENTIFIER,
2164 * parameters ANY DEFINED BY algorithm OPTIONAL
2165 * }
2166 *
2167 * RSAPublicKey ::= SEQUENCE {
2168 * modulus INTEGER, -- n
2169 * publicExponent INTEGER -- e
2170 * }
2171 */
2172
2173 if( ( ret = asn1_get_tag( &p, end, &len,
2174 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2175 {
2176#if defined(POLARSSL_PEM_C)
2177 pem_free( &pem );
2178#endif
2179 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002180 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002181 }
2182
2183 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2184 {
2185#if defined(POLARSSL_PEM_C)
2186 pem_free( &pem );
2187#endif
2188 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002189 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002190 }
2191
2192 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2193 {
2194#if defined(POLARSSL_PEM_C)
2195 pem_free( &pem );
2196#endif
2197 rsa_free( rsa );
2198 return( ret );
2199 }
2200
2201 rsa->len = mpi_size( &rsa->N );
2202
2203#if defined(POLARSSL_PEM_C)
2204 pem_free( &pem );
2205#endif
2206
2207 return( 0 );
2208}
2209
Paul Bakkereaa89f82011-04-04 21:36:15 +00002210#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002211/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002212 * Parse DHM parameters
2213 */
Paul Bakker23986e52011-04-24 08:57:21 +00002214int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002215{
Paul Bakker23986e52011-04-24 08:57:21 +00002216 int ret;
2217 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002218 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002219#if defined(POLARSSL_PEM_C)
2220 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002221
Paul Bakker96743fc2011-02-12 14:30:57 +00002222 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002223
Paul Bakker96743fc2011-02-12 14:30:57 +00002224 ret = pem_read_buffer( &pem,
2225 "-----BEGIN DH PARAMETERS-----",
2226 "-----END DH PARAMETERS-----",
2227 dhmin, NULL, 0, &dhminlen );
2228
2229 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002230 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002231 /*
2232 * Was PEM encoded
2233 */
2234 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002235 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002236 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002237 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002238 pem_free( &pem );
2239 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002240 }
2241
Paul Bakker96743fc2011-02-12 14:30:57 +00002242 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2243#else
2244 p = (unsigned char *) dhmin;
2245#endif
2246 end = p + dhminlen;
2247
Paul Bakker1b57b062011-01-06 15:48:19 +00002248 memset( dhm, 0, sizeof( dhm_context ) );
2249
Paul Bakker1b57b062011-01-06 15:48:19 +00002250 /*
2251 * DHParams ::= SEQUENCE {
2252 * prime INTEGER, -- P
2253 * generator INTEGER, -- g
2254 * }
2255 */
2256 if( ( ret = asn1_get_tag( &p, end, &len,
2257 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2258 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002259#if defined(POLARSSL_PEM_C)
2260 pem_free( &pem );
2261#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002262 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002263 }
2264
2265 end = p + len;
2266
2267 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2268 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2269 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002270#if defined(POLARSSL_PEM_C)
2271 pem_free( &pem );
2272#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002273 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002274 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002275 }
2276
2277 if( p != end )
2278 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002279#if defined(POLARSSL_PEM_C)
2280 pem_free( &pem );
2281#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002282 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002283 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002284 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2285 }
2286
Paul Bakker96743fc2011-02-12 14:30:57 +00002287#if defined(POLARSSL_PEM_C)
2288 pem_free( &pem );
2289#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002290
2291 return( 0 );
2292}
2293
Paul Bakker335db3f2011-04-25 15:28:35 +00002294#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002295/*
2296 * Load and parse a private RSA key
2297 */
2298int x509parse_dhmfile( dhm_context *dhm, const char *path )
2299{
2300 int ret;
2301 size_t n;
2302 unsigned char *buf;
2303
2304 if ( load_file( path, &buf, &n ) )
2305 return( 1 );
2306
Paul Bakker27fdf462011-06-09 13:55:13 +00002307 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002308
2309 memset( buf, 0, n + 1 );
2310 free( buf );
2311
2312 return( ret );
2313}
Paul Bakker335db3f2011-04-25 15:28:35 +00002314#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002315#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002316
Paul Bakker5121ce52009-01-03 21:22:43 +00002317#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002318#include <stdarg.h>
2319
2320#if !defined vsnprintf
2321#define vsnprintf _vsnprintf
2322#endif // vsnprintf
2323
2324/*
2325 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2326 * Result value is not size of buffer needed, but -1 if no fit is possible.
2327 *
2328 * This fuction tries to 'fix' this by at least suggesting enlarging the
2329 * size by 20.
2330 */
2331int compat_snprintf(char *str, size_t size, const char *format, ...)
2332{
2333 va_list ap;
2334 int res = -1;
2335
2336 va_start( ap, format );
2337
2338 res = vsnprintf( str, size, format, ap );
2339
2340 va_end( ap );
2341
2342 // No quick fix possible
2343 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002344 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002345
2346 return res;
2347}
2348
2349#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002350#endif
2351
Paul Bakkerd98030e2009-05-02 15:13:40 +00002352#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2353
2354#define SAFE_SNPRINTF() \
2355{ \
2356 if( ret == -1 ) \
2357 return( -1 ); \
2358 \
Paul Bakker23986e52011-04-24 08:57:21 +00002359 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002360 p[n - 1] = '\0'; \
2361 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2362 } \
2363 \
Paul Bakker23986e52011-04-24 08:57:21 +00002364 n -= (unsigned int) ret; \
2365 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002366}
2367
Paul Bakker5121ce52009-01-03 21:22:43 +00002368/*
2369 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002370 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002371 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002372int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373{
Paul Bakker23986e52011-04-24 08:57:21 +00002374 int ret;
2375 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002377 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 char s[128], *p;
2379
2380 memset( s, 0, sizeof( s ) );
2381
2382 name = dn;
2383 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002384 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 while( name != NULL )
2387 {
Paul Bakker74111d32011-01-15 16:57:55 +00002388 if( name != dn )
2389 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002390 ret = snprintf( p, n, ", " );
2391 SAFE_SNPRINTF();
2392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002393
2394 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2395 {
2396 switch( name->oid.p[2] )
2397 {
2398 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002399 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
2401 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002402 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
2404 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002405 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
2407 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
2410 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002417 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 name->oid.p[2] );
2419 break;
2420 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002421 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2424 {
2425 switch( name->oid.p[8] )
2426 {
2427 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002428 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002431 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 name->oid.p[8] );
2433 break;
2434 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002435 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
2437 else
Paul Bakker74111d32011-01-15 16:57:55 +00002438 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002439 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002440 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002441 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
2443 for( i = 0; i < name->val.len; i++ )
2444 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002445 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 break;
2447
2448 c = name->val.p[i];
2449 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2450 s[i] = '?';
2451 else s[i] = c;
2452 }
2453 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002454 ret = snprintf( p, n, "%s", s );
2455 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 name = name->next;
2457 }
2458
Paul Bakker23986e52011-04-24 08:57:21 +00002459 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460}
2461
2462/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002463 * Store the serial in printable form into buf; no more
2464 * than size characters will be written
2465 */
2466int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2467{
Paul Bakker23986e52011-04-24 08:57:21 +00002468 int ret;
2469 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002470 char *p;
2471
2472 p = buf;
2473 n = size;
2474
2475 nr = ( serial->len <= 32 )
2476 ? serial->len : 32;
2477
2478 for( i = 0; i < nr; i++ )
2479 {
2480 ret = snprintf( p, n, "%02X%s",
2481 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2482 SAFE_SNPRINTF();
2483 }
2484
Paul Bakker23986e52011-04-24 08:57:21 +00002485 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002486}
2487
2488/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002489 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002491int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2492 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002493{
Paul Bakker23986e52011-04-24 08:57:21 +00002494 int ret;
2495 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002496 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002497
2498 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Paul Bakkerd98030e2009-05-02 15:13:40 +00002501 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 SAFE_SNPRINTF();
2504 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Paul Bakkerdd476992011-01-16 21:34:59 +00002508 ret = x509parse_serial_gets( p, n, &crt->serial);
2509 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002510
Paul Bakkerd98030e2009-05-02 15:13:40 +00002511 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2512 SAFE_SNPRINTF();
2513 ret = x509parse_dn_gets( p, n, &crt->issuer );
2514 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerd98030e2009-05-02 15:13:40 +00002516 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2517 SAFE_SNPRINTF();
2518 ret = x509parse_dn_gets( p, n, &crt->subject );
2519 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002520
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2523 crt->valid_from.year, crt->valid_from.mon,
2524 crt->valid_from.day, crt->valid_from.hour,
2525 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002526 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Paul Bakkerd98030e2009-05-02 15:13:40 +00002528 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2530 crt->valid_to.year, crt->valid_to.mon,
2531 crt->valid_to.day, crt->valid_to.hour,
2532 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002533 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002534
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2536 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
Paul Bakker27d66162010-03-17 06:56:01 +00002538 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002540 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2541 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2542 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2543 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2544 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2545 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2546 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2547 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2548 default: ret = snprintf( p, n, "???" ); break;
2549 }
2550 SAFE_SNPRINTF();
2551
2552 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002553 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002554 SAFE_SNPRINTF();
2555
Paul Bakker23986e52011-04-24 08:57:21 +00002556 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002557}
2558
Paul Bakker74111d32011-01-15 16:57:55 +00002559/* Compare a given OID string with an OID x509_buf * */
2560#define OID_CMP(oid_str, oid_buf) \
2561 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2562 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2563
2564/*
2565 * Return an informational string describing the given OID
2566 */
2567const char *x509_oid_get_description( x509_buf *oid )
2568{
2569 if ( oid == NULL )
2570 return ( NULL );
2571
2572 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2573 return( STRING_SERVER_AUTH );
2574
2575 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2576 return( STRING_CLIENT_AUTH );
2577
2578 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2579 return( STRING_CODE_SIGNING );
2580
2581 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2582 return( STRING_EMAIL_PROTECTION );
2583
2584 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2585 return( STRING_TIME_STAMPING );
2586
2587 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2588 return( STRING_OCSP_SIGNING );
2589
2590 return( NULL );
2591}
2592
2593/* Return the x.y.z.... style numeric string for the given OID */
2594int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2595{
Paul Bakker23986e52011-04-24 08:57:21 +00002596 int ret;
2597 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002598 unsigned int value;
2599 char *p;
2600
2601 p = buf;
2602 n = size;
2603
2604 /* First byte contains first two dots */
2605 if( oid->len > 0 )
2606 {
2607 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2608 SAFE_SNPRINTF();
2609 }
2610
2611 /* TODO: value can overflow in value. */
2612 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002613 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002614 {
2615 value <<= 7;
2616 value += oid->p[i] & 0x7F;
2617
2618 if( !( oid->p[i] & 0x80 ) )
2619 {
2620 /* Last byte */
2621 ret = snprintf( p, n, ".%d", value );
2622 SAFE_SNPRINTF();
2623 value = 0;
2624 }
2625 }
2626
Paul Bakker23986e52011-04-24 08:57:21 +00002627 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002628}
2629
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630/*
2631 * Return an informational string about the CRL.
2632 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002633int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2634 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002635{
Paul Bakker23986e52011-04-24 08:57:21 +00002636 int ret;
2637 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002638 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002639 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640
2641 p = buf;
2642 n = size;
2643
2644 ret = snprintf( p, n, "%sCRL version : %d",
2645 prefix, crl->version );
2646 SAFE_SNPRINTF();
2647
2648 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2649 SAFE_SNPRINTF();
2650 ret = x509parse_dn_gets( p, n, &crl->issuer );
2651 SAFE_SNPRINTF();
2652
2653 ret = snprintf( p, n, "\n%sthis update : " \
2654 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2655 crl->this_update.year, crl->this_update.mon,
2656 crl->this_update.day, crl->this_update.hour,
2657 crl->this_update.min, crl->this_update.sec );
2658 SAFE_SNPRINTF();
2659
2660 ret = snprintf( p, n, "\n%snext update : " \
2661 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2662 crl->next_update.year, crl->next_update.mon,
2663 crl->next_update.day, crl->next_update.hour,
2664 crl->next_update.min, crl->next_update.sec );
2665 SAFE_SNPRINTF();
2666
2667 entry = &crl->entry;
2668
2669 ret = snprintf( p, n, "\n%sRevoked certificates:",
2670 prefix );
2671 SAFE_SNPRINTF();
2672
Paul Bakker9be19372009-07-27 20:21:53 +00002673 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 {
2675 ret = snprintf( p, n, "\n%sserial number: ",
2676 prefix );
2677 SAFE_SNPRINTF();
2678
2679 nr = ( entry->serial.len <= 32 )
2680 ? entry->serial.len : 32;
2681
Paul Bakker74111d32011-01-15 16:57:55 +00002682 for( i = 0; i < nr; i++ )
2683 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684 ret = snprintf( p, n, "%02X%s",
2685 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2686 SAFE_SNPRINTF();
2687 }
2688
2689 ret = snprintf( p, n, " revocation date: " \
2690 "%04d-%02d-%02d %02d:%02d:%02d",
2691 entry->revocation_date.year, entry->revocation_date.mon,
2692 entry->revocation_date.day, entry->revocation_date.hour,
2693 entry->revocation_date.min, entry->revocation_date.sec );
2694 SAFE_SNPRINTF();
2695
2696 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
Paul Bakkerd98030e2009-05-02 15:13:40 +00002699 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2700 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Paul Bakker27d66162010-03-17 06:56:01 +00002702 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002703 {
2704 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2705 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2706 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2707 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2708 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2709 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2710 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2711 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2712 default: ret = snprintf( p, n, "???" ); break;
2713 }
2714 SAFE_SNPRINTF();
2715
Paul Bakker1e27bb22009-07-19 20:25:25 +00002716 ret = snprintf( p, n, "\n" );
2717 SAFE_SNPRINTF();
2718
Paul Bakker23986e52011-04-24 08:57:21 +00002719 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720}
2721
2722/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002723 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002725int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726{
2727 struct tm *lt;
2728 time_t tt;
2729
2730 tt = time( NULL );
2731 lt = localtime( &tt );
2732
Paul Bakker40ea7de2009-05-03 10:18:48 +00002733 if( lt->tm_year > to->year - 1900 )
2734 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735
Paul Bakker40ea7de2009-05-03 10:18:48 +00002736 if( lt->tm_year == to->year - 1900 &&
2737 lt->tm_mon > to->mon - 1 )
2738 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Paul Bakker40ea7de2009-05-03 10:18:48 +00002740 if( lt->tm_year == to->year - 1900 &&
2741 lt->tm_mon == to->mon - 1 &&
2742 lt->tm_mday > to->day )
2743 return( 1 );
2744
Paul Bakkerb6194992011-01-16 21:40:22 +00002745 if( lt->tm_year == to->year - 1900 &&
2746 lt->tm_mon == to->mon - 1 &&
2747 lt->tm_mday == to->day &&
2748 lt->tm_hour > to->hour - 1)
2749 return( 1 );
2750
2751 if( lt->tm_year == to->year - 1900 &&
2752 lt->tm_mon == to->mon - 1 &&
2753 lt->tm_mday == to->day &&
2754 lt->tm_hour == to->hour - 1 &&
2755 lt->tm_min > to->min - 1 )
2756 return( 1 );
2757
2758 if( lt->tm_year == to->year - 1900 &&
2759 lt->tm_mon == to->mon - 1 &&
2760 lt->tm_mday == to->day &&
2761 lt->tm_hour == to->hour - 1 &&
2762 lt->tm_min == to->min - 1 &&
2763 lt->tm_sec > to->sec - 1 )
2764 return( 1 );
2765
Paul Bakker40ea7de2009-05-03 10:18:48 +00002766 return( 0 );
2767}
2768
2769/*
2770 * Return 1 if the certificate is revoked, or 0 otherwise.
2771 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002772int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002773{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002774 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002775
2776 while( cur != NULL && cur->serial.len != 0 )
2777 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002778 if( crt->serial.len == cur->serial.len &&
2779 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002780 {
2781 if( x509parse_time_expired( &cur->revocation_date ) )
2782 return( 1 );
2783 }
2784
2785 cur = cur->next;
2786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
2788 return( 0 );
2789}
2790
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002791/*
2792 * Wrapper for x509 hashes.
2793 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002794 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002795 */
Paul Bakker23986e52011-04-24 08:57:21 +00002796static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 unsigned char *out )
2798{
2799 switch( alg )
2800 {
Paul Bakker40e46942009-01-03 21:51:57 +00002801#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002802 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002804#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002805 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002806#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002807#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002808 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002809#endif
2810#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002811 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002812#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002813#if defined(POLARSSL_SHA2_C)
2814 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2815 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2816#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002817#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002818 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2819 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002822 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 break;
2824 }
2825}
2826
2827/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002828 * Check that the given certificate is valid accoring to the CRL.
2829 */
2830static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2831 x509_crl *crl_list)
2832{
2833 int flags = 0;
2834 int hash_id;
2835 unsigned char hash[64];
2836
2837 /*
2838 * TODO: What happens if no CRL is present?
2839 * Suggestion: Revocation state should be unknown if no CRL is present.
2840 * For backwards compatibility this is not yet implemented.
2841 */
2842
2843 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2844 {
2845 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2846 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2847 crl_list->issuer_raw.len ) != 0 )
2848 {
2849 crl_list = crl_list->next;
2850 continue;
2851 }
2852
2853 /*
2854 * Check if CRL is correctly signed by the trusted CA
2855 */
2856 hash_id = crl_list->sig_alg;
2857
2858 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2859
2860 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2861 0, hash, crl_list->sig.p ) == 0 )
2862 {
2863 /*
2864 * CRL is not trusted
2865 */
2866 flags |= BADCRL_NOT_TRUSTED;
2867 break;
2868 }
2869
2870 /*
2871 * Check for validity of CRL (Do not drop out)
2872 */
2873 if( x509parse_time_expired( &crl_list->next_update ) )
2874 flags |= BADCRL_EXPIRED;
2875
2876 /*
2877 * Check if certificate is revoked
2878 */
2879 if( x509parse_revoked(crt, crl_list) )
2880 {
2881 flags |= BADCERT_REVOKED;
2882 break;
2883 }
2884
2885 crl_list = crl_list->next;
2886 }
2887 return flags;
2888}
2889
2890/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 * Verify the certificate validity
2892 */
2893int x509parse_verify( x509_cert *crt,
2894 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002895 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002896 const char *cn, int *flags,
2897 int (*f_vrfy)(void *, x509_cert *, int, int),
2898 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002899{
Paul Bakker23986e52011-04-24 08:57:21 +00002900 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 int hash_id;
2902 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002903 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002905 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakker40ea7de2009-05-03 10:18:48 +00002907 *flags = 0;
2908
2909 if( x509parse_time_expired( &crt->valid_to ) )
2910 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
2912 if( cn != NULL )
2913 {
2914 name = &crt->subject;
2915 cn_len = strlen( cn );
2916
2917 while( name != NULL )
2918 {
2919 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2920 memcmp( name->val.p, cn, cn_len ) == 0 &&
2921 name->val.len == cn_len )
2922 break;
2923
2924 name = name->next;
2925 }
2926
2927 if( name == NULL )
2928 *flags |= BADCERT_CN_MISMATCH;
2929 }
2930
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 /*
2932 * Iterate upwards in the given cert chain,
2933 * ignoring any upper cert with CA != TRUE.
2934 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002935 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
2937 pathlen = 1;
2938
Paul Bakker76fd75a2011-01-16 21:12:10 +00002939 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002941 if( parent->ca_istrue == 0 ||
2942 crt->issuer_raw.len != parent->subject_raw.len ||
2943 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 crt->issuer_raw.len ) != 0 )
2945 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002946 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 continue;
2948 }
2949
Paul Bakker27d66162010-03-17 06:56:01 +00002950 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
2952 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2953
Paul Bakker76fd75a2011-01-16 21:12:10 +00002954 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2955 crt->sig.p ) != 0 )
2956 *flags |= BADCERT_NOT_TRUSTED;
2957
2958 /* Check trusted CA's CRL for the given crt */
2959 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002960
2961 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002962 if( NULL != f_vrfy )
2963 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002964 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002965 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002966 else
2967 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002968 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002969 else if( *flags != 0 )
2970 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
2972 pathlen++;
2973
Paul Bakker76fd75a2011-01-16 21:12:10 +00002974 crt = parent;
2975 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
2977
2978 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002979 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002981 *flags |= BADCERT_NOT_TRUSTED;
2982
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002983 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 {
2985 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2986 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2987 crt->issuer_raw.len ) != 0 )
2988 {
2989 trust_ca = trust_ca->next;
2990 continue;
2991 }
2992
2993 if( trust_ca->max_pathlen > 0 &&
2994 trust_ca->max_pathlen < pathlen )
2995 break;
2996
Paul Bakker27d66162010-03-17 06:56:01 +00002997 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
2999 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3000
3001 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3002 0, hash, crt->sig.p ) == 0 )
3003 {
3004 /*
3005 * cert. is signed by a trusted CA
3006 */
3007 *flags &= ~BADCERT_NOT_TRUSTED;
3008 break;
3009 }
3010
3011 trust_ca = trust_ca->next;
3012 }
3013
Paul Bakker76fd75a2011-01-16 21:12:10 +00003014 /* Check trusted CA's CRL for the given crt */
3015 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003016
3017 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003018 if( NULL != f_vrfy )
3019 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003020 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003021 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003022 else
3023 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003024 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003025 else if( *flags != 0 )
3026 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003027
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 return( 0 );
3029}
3030
3031/*
3032 * Unallocate all certificate data
3033 */
3034void x509_free( x509_cert *crt )
3035{
3036 x509_cert *cert_cur = crt;
3037 x509_cert *cert_prv;
3038 x509_name *name_cur;
3039 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003040 x509_sequence *seq_cur;
3041 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
3043 if( crt == NULL )
3044 return;
3045
3046 do
3047 {
3048 rsa_free( &cert_cur->rsa );
3049
3050 name_cur = cert_cur->issuer.next;
3051 while( name_cur != NULL )
3052 {
3053 name_prv = name_cur;
3054 name_cur = name_cur->next;
3055 memset( name_prv, 0, sizeof( x509_name ) );
3056 free( name_prv );
3057 }
3058
3059 name_cur = cert_cur->subject.next;
3060 while( name_cur != NULL )
3061 {
3062 name_prv = name_cur;
3063 name_cur = name_cur->next;
3064 memset( name_prv, 0, sizeof( x509_name ) );
3065 free( name_prv );
3066 }
3067
Paul Bakker74111d32011-01-15 16:57:55 +00003068 seq_cur = cert_cur->ext_key_usage.next;
3069 while( seq_cur != NULL )
3070 {
3071 seq_prv = seq_cur;
3072 seq_cur = seq_cur->next;
3073 memset( seq_prv, 0, sizeof( x509_sequence ) );
3074 free( seq_prv );
3075 }
3076
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 if( cert_cur->raw.p != NULL )
3078 {
3079 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3080 free( cert_cur->raw.p );
3081 }
3082
3083 cert_cur = cert_cur->next;
3084 }
3085 while( cert_cur != NULL );
3086
3087 cert_cur = crt;
3088 do
3089 {
3090 cert_prv = cert_cur;
3091 cert_cur = cert_cur->next;
3092
3093 memset( cert_prv, 0, sizeof( x509_cert ) );
3094 if( cert_prv != crt )
3095 free( cert_prv );
3096 }
3097 while( cert_cur != NULL );
3098}
3099
Paul Bakkerd98030e2009-05-02 15:13:40 +00003100/*
3101 * Unallocate all CRL data
3102 */
3103void x509_crl_free( x509_crl *crl )
3104{
3105 x509_crl *crl_cur = crl;
3106 x509_crl *crl_prv;
3107 x509_name *name_cur;
3108 x509_name *name_prv;
3109 x509_crl_entry *entry_cur;
3110 x509_crl_entry *entry_prv;
3111
3112 if( crl == NULL )
3113 return;
3114
3115 do
3116 {
3117 name_cur = crl_cur->issuer.next;
3118 while( name_cur != NULL )
3119 {
3120 name_prv = name_cur;
3121 name_cur = name_cur->next;
3122 memset( name_prv, 0, sizeof( x509_name ) );
3123 free( name_prv );
3124 }
3125
3126 entry_cur = crl_cur->entry.next;
3127 while( entry_cur != NULL )
3128 {
3129 entry_prv = entry_cur;
3130 entry_cur = entry_cur->next;
3131 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3132 free( entry_prv );
3133 }
3134
3135 if( crl_cur->raw.p != NULL )
3136 {
3137 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3138 free( crl_cur->raw.p );
3139 }
3140
3141 crl_cur = crl_cur->next;
3142 }
3143 while( crl_cur != NULL );
3144
3145 crl_cur = crl;
3146 do
3147 {
3148 crl_prv = crl_cur;
3149 crl_cur = crl_cur->next;
3150
3151 memset( crl_prv, 0, sizeof( x509_crl ) );
3152 if( crl_prv != crl )
3153 free( crl_prv );
3154 }
3155 while( crl_cur != NULL );
3156}
3157
Paul Bakker40e46942009-01-03 21:51:57 +00003158#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker40e46942009-01-03 21:51:57 +00003160#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
3162/*
3163 * Checkup routine
3164 */
3165int x509_self_test( int verbose )
3166{
Paul Bakker5690efc2011-05-26 13:16:06 +00003167#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003168 int ret;
3169 int flags;
3170 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 x509_cert cacert;
3172 x509_cert clicert;
3173 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003174#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003175 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003176#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
3178 if( verbose != 0 )
3179 printf( " X.509 certificate load: " );
3180
3181 memset( &clicert, 0, sizeof( x509_cert ) );
3182
3183 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3184 strlen( test_cli_crt ) );
3185 if( ret != 0 )
3186 {
3187 if( verbose != 0 )
3188 printf( "failed\n" );
3189
3190 return( ret );
3191 }
3192
3193 memset( &cacert, 0, sizeof( x509_cert ) );
3194
3195 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3196 strlen( test_ca_crt ) );
3197 if( ret != 0 )
3198 {
3199 if( verbose != 0 )
3200 printf( "failed\n" );
3201
3202 return( ret );
3203 }
3204
3205 if( verbose != 0 )
3206 printf( "passed\n X.509 private key load: " );
3207
3208 i = strlen( test_ca_key );
3209 j = strlen( test_ca_pwd );
3210
Paul Bakker66b78b22011-03-25 14:22:50 +00003211 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ( ret = x509parse_key( &rsa,
3214 (unsigned char *) test_ca_key, i,
3215 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3216 {
3217 if( verbose != 0 )
3218 printf( "failed\n" );
3219
3220 return( ret );
3221 }
3222
3223 if( verbose != 0 )
3224 printf( "passed\n X.509 signature verify: ");
3225
Paul Bakker23986e52011-04-24 08:57:21 +00003226 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 if( ret != 0 )
3228 {
Paul Bakker23986e52011-04-24 08:57:21 +00003229 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 if( verbose != 0 )
3231 printf( "failed\n" );
3232
3233 return( ret );
3234 }
3235
Paul Bakker5690efc2011-05-26 13:16:06 +00003236#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003238 printf( "passed\n X.509 DHM parameter load: " );
3239
3240 i = strlen( test_dhm_params );
3241 j = strlen( test_ca_pwd );
3242
3243 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3244 {
3245 if( verbose != 0 )
3246 printf( "failed\n" );
3247
3248 return( ret );
3249 }
3250
3251 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003254
3255 x509_free( &cacert );
3256 x509_free( &clicert );
3257 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003258#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003259 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
3262 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003263#else
3264 ((void) verbose);
3265 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3266#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003267}
3268
3269#endif
3270
3271#endif