blob: 9fc8831e08f0edec5c4e39d1fa785e231da9498c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, 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 Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000055#include <time.h>
56
Paul Bakker335db3f2011-04-25 15:28:35 +000057#if defined(POLARSSL_FS_IO)
58#include <stdio.h>
59#endif
60
Paul Bakker5121ce52009-01-03 21:22:43 +000061/*
Paul Bakker5121ce52009-01-03 21:22:43 +000062 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
63 */
64static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000065 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000066 int *ver )
67{
Paul Bakker23986e52011-04-24 08:57:21 +000068 int ret;
69 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71 if( ( ret = asn1_get_tag( p, end, &len,
72 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
73 {
Paul Bakker40e46942009-01-03 21:51:57 +000074 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000075 {
76 *ver = 0;
77 return( 0 );
78 }
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 return( ret );
81 }
82
83 end = *p + len;
84
85 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000086 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000089 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000090 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 return( 0 );
93}
94
95/*
Paul Bakkerfae618f2011-10-12 11:53:52 +000096 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +000097 */
98static int x509_crl_get_version( unsigned char **p,
99 const unsigned char *end,
100 int *ver )
101{
102 int ret;
103
104 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
105 {
106 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000107 {
108 *ver = 0;
109 return( 0 );
110 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000111
112 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
113 }
114
115 return( 0 );
116}
117
118/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * CertificateSerialNumber ::= INTEGER
120 */
121static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000122 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 x509_buf *serial )
124{
125 int ret;
126
127 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000128 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000129 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
132 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000133 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000134 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 serial->tag = *(*p)++;
137
138 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000139 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 serial->p = *p;
142 *p += serial->len;
143
144 return( 0 );
145}
146
147/*
148 * AlgorithmIdentifier ::= SEQUENCE {
149 * algorithm OBJECT IDENTIFIER,
150 * parameters ANY DEFINED BY algorithm OPTIONAL }
151 */
152static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 x509_buf *alg )
155{
Paul Bakker23986e52011-04-24 08:57:21 +0000156 int ret;
157 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 if( ( ret = asn1_get_tag( p, end, &len,
160 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000161 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 end = *p + len;
164 alg->tag = **p;
165
166 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000167 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 alg->p = *p;
170 *p += alg->len;
171
172 if( *p == end )
173 return( 0 );
174
175 /*
176 * assume the algorithm parameters must be NULL
177 */
178 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000182 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000183 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 return( 0 );
186}
187
188/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 * AttributeTypeAndValue ::= SEQUENCE {
190 * type AttributeType,
191 * value AttributeValue }
192 *
193 * AttributeType ::= OBJECT IDENTIFIER
194 *
195 * AttributeValue ::= ANY DEFINED BY AttributeType
196 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000197static int x509_get_attr_type_value( unsigned char **p,
198 const unsigned char *end,
199 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200{
Paul Bakker23986e52011-04-24 08:57:21 +0000201 int ret;
202 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 x509_buf *oid;
204 x509_buf *val;
205
206 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000208 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 oid = &cur->oid;
211 oid->tag = **p;
212
213 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000214 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216 oid->p = *p;
217 *p += oid->len;
218
219 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000220 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000221 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
224 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
225 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000226 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000227 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 val = &cur->val;
230 val->tag = *(*p)++;
231
232 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000233 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 val->p = *p;
236 *p += val->len;
237
238 cur->next = NULL;
239
Paul Bakker400ff6f2011-02-20 10:40:16 +0000240 return( 0 );
241}
242
243/*
244 * RelativeDistinguishedName ::=
245 * SET OF AttributeTypeAndValue
246 *
247 * AttributeTypeAndValue ::= SEQUENCE {
248 * type AttributeType,
249 * value AttributeValue }
250 *
251 * AttributeType ::= OBJECT IDENTIFIER
252 *
253 * AttributeValue ::= ANY DEFINED BY AttributeType
254 */
255static int x509_get_name( unsigned char **p,
256 const unsigned char *end,
257 x509_name *cur )
258{
Paul Bakker23986e52011-04-24 08:57:21 +0000259 int ret;
260 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000261 const unsigned char *end2;
262 x509_name *use;
263
264 if( ( ret = asn1_get_tag( p, end, &len,
265 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000266 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000267
268 end2 = end;
269 end = *p + len;
270 use = cur;
271
272 do
273 {
274 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
275 return( ret );
276
277 if( *p != end )
278 {
279 use->next = (x509_name *) malloc(
280 sizeof( x509_name ) );
281
282 if( use->next == NULL )
283 return( 1 );
284
285 memset( use->next, 0, sizeof( x509_name ) );
286
287 use = use->next;
288 }
289 }
290 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 /*
293 * recurse until end of SEQUENCE is reached
294 */
295 if( *p == end2 )
296 return( 0 );
297
298 cur->next = (x509_name *) malloc(
299 sizeof( x509_name ) );
300
301 if( cur->next == NULL )
302 return( 1 );
303
304 return( x509_get_name( p, end2, cur->next ) );
305}
306
307/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 * Time ::= CHOICE {
309 * utcTime UTCTime,
310 * generalTime GeneralizedTime }
311 */
Paul Bakker91200182010-02-18 21:26:15 +0000312static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000313 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000314 x509_time *time )
315{
Paul Bakker23986e52011-04-24 08:57:21 +0000316 int ret;
317 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000319 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320
Paul Bakker91200182010-02-18 21:26:15 +0000321 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000322 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
323 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324
Paul Bakker91200182010-02-18 21:26:15 +0000325 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326
Paul Bakker91200182010-02-18 21:26:15 +0000327 if ( tag == ASN1_UTC_TIME )
328 {
329 (*p)++;
330 ret = asn1_get_len( p, end, &len );
331
332 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000333 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000334
Paul Bakker91200182010-02-18 21:26:15 +0000335 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000336 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
337 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
340 &time->year, &time->mon, &time->day,
341 &time->hour, &time->min, &time->sec ) < 5 )
342 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343
Paul Bakker400ff6f2011-02-20 10:40:16 +0000344 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000345 time->year += 1900;
346
347 *p += len;
348
349 return( 0 );
350 }
351 else if ( tag == ASN1_GENERALIZED_TIME )
352 {
353 (*p)++;
354 ret = asn1_get_len( p, end, &len );
355
356 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000357 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000358
359 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000360 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
361 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000362
363 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
364 &time->year, &time->mon, &time->day,
365 &time->hour, &time->min, &time->sec ) < 5 )
366 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
367
368 *p += len;
369
370 return( 0 );
371 }
372 else
Paul Bakker9d781402011-05-09 16:17:09 +0000373 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000374}
375
376
377/*
378 * Validity ::= SEQUENCE {
379 * notBefore Time,
380 * notAfter Time }
381 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000382static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000383 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 x509_time *from,
385 x509_time *to )
386{
Paul Bakker23986e52011-04-24 08:57:21 +0000387 int ret;
388 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 if( ( ret = asn1_get_tag( p, end, &len,
391 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000392 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394 end = *p + len;
395
Paul Bakker91200182010-02-18 21:26:15 +0000396 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000397 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
Paul Bakker91200182010-02-18 21:26:15 +0000399 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000403 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000404 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 return( 0 );
407}
408
409/*
410 * SubjectPublicKeyInfo ::= SEQUENCE {
411 * algorithm AlgorithmIdentifier,
412 * subjectPublicKey BIT STRING }
413 */
414static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000415 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 x509_buf *pk_alg_oid,
417 mpi *N, mpi *E )
418{
Paul Bakker23986e52011-04-24 08:57:21 +0000419 int ret, can_handle;
420 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 unsigned char *end2;
422
423 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
424 return( ret );
425
426 /*
427 * only RSA public keys handled at this time
428 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000429 can_handle = 0;
430
431 if( pk_alg_oid->len == 9 &&
432 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
433 can_handle = 1;
434
435 if( pk_alg_oid->len == 9 &&
436 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
437 {
438 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
439 can_handle = 1;
440
441 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
442 can_handle = 1;
443 }
444
445 if( pk_alg_oid->len == 5 &&
446 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
447 can_handle = 1;
448
449 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000450 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000453 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000456 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000457 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 end2 = *p + len;
460
461 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000462 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 /*
465 * RSAPublicKey ::= SEQUENCE {
466 * modulus INTEGER, -- n
467 * publicExponent INTEGER -- e
468 * }
469 */
470 if( ( ret = asn1_get_tag( p, end2, &len,
471 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000472 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000475 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000476 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
479 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000480 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000483 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000484 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 return( 0 );
487}
488
489static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000490 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 x509_buf *sig )
492{
Paul Bakker23986e52011-04-24 08:57:21 +0000493 int ret;
494 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 sig->tag = **p;
497
498 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000499 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Paul Bakker74111d32011-01-15 16:57:55 +0000501
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 sig->len = len;
506 sig->p = *p;
507
508 *p += len;
509
510 return( 0 );
511}
512
513/*
514 * X.509 v2/v3 unique identifier (not parsed)
515 */
516static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000517 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 x509_buf *uid, int n )
519{
520 int ret;
521
522 if( *p == end )
523 return( 0 );
524
525 uid->tag = **p;
526
527 if( ( ret = asn1_get_tag( p, end, &uid->len,
528 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
529 {
Paul Bakker40e46942009-01-03 21:51:57 +0000530 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 return( 0 );
532
533 return( ret );
534 }
535
536 uid->p = *p;
537 *p += uid->len;
538
539 return( 0 );
540}
541
542/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000543 * X.509 Extensions (No parsing of extensions, pointer should
544 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 */
546static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000547 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000548 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000549{
Paul Bakker23986e52011-04-24 08:57:21 +0000550 int ret;
551 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 if( *p == end )
554 return( 0 );
555
556 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000557
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000559 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 ext->p = *p;
563 end = *p + ext->len;
564
565 /*
566 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
567 *
568 * Extension ::= SEQUENCE {
569 * extnID OBJECT IDENTIFIER,
570 * critical BOOLEAN DEFAULT FALSE,
571 * extnValue OCTET STRING }
572 */
573 if( ( ret = asn1_get_tag( p, end, &len,
574 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000575 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000578 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000579 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Paul Bakkerd98030e2009-05-02 15:13:40 +0000581 return( 0 );
582}
583
584/*
585 * X.509 CRL v2 extensions (no extensions parsed yet.)
586 */
587static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000588 const unsigned char *end,
589 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000590{
Paul Bakker23986e52011-04-24 08:57:21 +0000591 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000592 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000593
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000594 /* Get explicit tag */
595 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000596 {
597 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
598 return( 0 );
599
600 return( ret );
601 }
602
603 while( *p < end )
604 {
605 if( ( ret = asn1_get_tag( p, end, &len,
606 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000607 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608
609 *p += len;
610 }
611
612 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000613 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
615
616 return( 0 );
617}
618
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000619/*
620 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
621 */
622static int x509_get_crl_entry_ext( unsigned char **p,
623 const unsigned char *end,
624 x509_buf *ext )
625{
626 int ret;
627 size_t len = 0;
628
629 /* OPTIONAL */
630 if (end <= *p)
631 return( 0 );
632
633 ext->tag = **p;
634 ext->p = *p;
635
636 /*
637 * Get CRL-entry extension sequence header
638 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
639 */
640 if( ( ret = asn1_get_tag( p, end, &ext->len,
641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
642 {
643 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
644 {
645 ext->p = NULL;
646 return( 0 );
647 }
648 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
649 }
650
651 end = *p + ext->len;
652
653 if( end != *p + ext->len )
654 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
655 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
656
657 while( *p < end )
658 {
659 if( ( ret = asn1_get_tag( p, end, &len,
660 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
661 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
662
663 *p += len;
664 }
665
666 if( *p != end )
667 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
668 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
669
670 return( 0 );
671}
672
Paul Bakker74111d32011-01-15 16:57:55 +0000673static int x509_get_basic_constraints( unsigned char **p,
674 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000675 int *ca_istrue,
676 int *max_pathlen )
677{
Paul Bakker23986e52011-04-24 08:57:21 +0000678 int ret;
679 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000680
681 /*
682 * BasicConstraints ::= SEQUENCE {
683 * cA BOOLEAN DEFAULT FALSE,
684 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
685 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000686 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000687 *max_pathlen = 0; /* endless */
688
689 if( ( ret = asn1_get_tag( p, end, &len,
690 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000692
693 if( *p == end )
694 return 0;
695
Paul Bakker3cccddb2011-01-16 21:46:31 +0000696 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000697 {
698 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000699 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000700
701 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000702 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000703
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 if( *ca_istrue != 0 )
705 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000706 }
707
708 if( *p == end )
709 return 0;
710
711 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000712 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000713
714 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000715 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000716 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
717
718 (*max_pathlen)++;
719
Paul Bakker74111d32011-01-15 16:57:55 +0000720 return 0;
721}
722
723static int x509_get_ns_cert_type( unsigned char **p,
724 const unsigned char *end,
725 unsigned char *ns_cert_type)
726{
727 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000728 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000729
730 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000731 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000732
733 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000734 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000735 POLARSSL_ERR_ASN1_INVALID_LENGTH );
736
737 /* Get actual bitstring */
738 *ns_cert_type = *bs.p;
739 return 0;
740}
741
742static int x509_get_key_usage( unsigned char **p,
743 const unsigned char *end,
744 unsigned char *key_usage)
745{
746 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000747 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000748
749 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000750 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000751
752 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000753 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000754 POLARSSL_ERR_ASN1_INVALID_LENGTH );
755
756 /* Get actual bitstring */
757 *key_usage = *bs.p;
758 return 0;
759}
760
Paul Bakkerd98030e2009-05-02 15:13:40 +0000761/*
Paul Bakker74111d32011-01-15 16:57:55 +0000762 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
763 *
764 * KeyPurposeId ::= OBJECT IDENTIFIER
765 */
766static int x509_get_ext_key_usage( unsigned char **p,
767 const unsigned char *end,
768 x509_sequence *ext_key_usage)
769{
770 int ret;
771
772 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000773 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000774
775 /* Sequence length must be >= 1 */
776 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000778 POLARSSL_ERR_ASN1_INVALID_LENGTH );
779
780 return 0;
781}
782
783/*
784 * X.509 v3 extensions
785 *
786 * TODO: Perform all of the basic constraints tests required by the RFC
787 * TODO: Set values for undetected extensions to a sane default?
788 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000789 */
790static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000791 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000792 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000793{
Paul Bakker23986e52011-04-24 08:57:21 +0000794 int ret;
795 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000796 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000797
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000798 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000799 {
800 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
801 return( 0 );
802
803 return( ret );
804 }
805
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 while( *p < end )
807 {
Paul Bakker74111d32011-01-15 16:57:55 +0000808 /*
809 * Extension ::= SEQUENCE {
810 * extnID OBJECT IDENTIFIER,
811 * critical BOOLEAN DEFAULT FALSE,
812 * extnValue OCTET STRING }
813 */
814 x509_buf extn_oid = {0, 0, NULL};
815 int is_critical = 0; /* DEFAULT FALSE */
816
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 if( ( ret = asn1_get_tag( p, end, &len,
818 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000820
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000821 end_ext_data = *p + len;
822
Paul Bakker74111d32011-01-15 16:57:55 +0000823 /* Get extension ID */
824 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
Paul Bakker74111d32011-01-15 16:57:55 +0000826 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000827 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker74111d32011-01-15 16:57:55 +0000829 extn_oid.p = *p;
830 *p += extn_oid.len;
831
832 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000834 POLARSSL_ERR_ASN1_OUT_OF_DATA );
835
836 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000837 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000838 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker74111d32011-01-15 16:57:55 +0000841 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000842 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000843 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000844 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000846 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000847
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000848 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000849 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000850 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakker74111d32011-01-15 16:57:55 +0000852 /*
853 * Detect supported extensions
854 */
855 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
856 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 {
Paul Bakker74111d32011-01-15 16:57:55 +0000858 /* Parse basic constraints */
859 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000860 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000861 return ( ret );
862 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 }
Paul Bakker74111d32011-01-15 16:57:55 +0000864 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
865 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
866 {
867 /* Parse netscape certificate type */
868 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
869 &crt->ns_cert_type ) ) != 0 )
870 return ( ret );
871 crt->ext_types |= EXT_NS_CERT_TYPE;
872 }
873 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
874 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
875 {
876 /* Parse key usage */
877 if( ( ret = x509_get_key_usage( p, end_ext_octet,
878 &crt->key_usage ) ) != 0 )
879 return ( ret );
880 crt->ext_types |= EXT_KEY_USAGE;
881 }
882 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
883 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
884 {
885 /* Parse extended key usage */
886 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
887 &crt->ext_key_usage ) ) != 0 )
888 return ( ret );
889 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
890 }
891 else
892 {
893 /* No parser found, skip extension */
894 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
Paul Bakker5c721f92011-07-27 16:51:09 +0000896#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000897 if( is_critical )
898 {
899 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000900 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000901 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
902 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000903#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 }
906
907 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000909 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 return( 0 );
912}
913
914/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000915 * X.509 CRL Entries
916 */
917static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000918 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000919 x509_crl_entry *entry )
920{
Paul Bakker23986e52011-04-24 08:57:21 +0000921 int ret;
922 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000923 x509_crl_entry *cur_entry = entry;
924
925 if( *p == end )
926 return( 0 );
927
Paul Bakker9be19372009-07-27 20:21:53 +0000928 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000929 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
930 {
931 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
932 return( 0 );
933
934 return( ret );
935 }
936
Paul Bakker9be19372009-07-27 20:21:53 +0000937 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000938
939 while( *p < end )
940 {
Paul Bakker23986e52011-04-24 08:57:21 +0000941 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000942 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000943
944 if( ( ret = asn1_get_tag( p, end, &len2,
945 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
946 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947 return( ret );
948 }
949
Paul Bakker9be19372009-07-27 20:21:53 +0000950 cur_entry->raw.tag = **p;
951 cur_entry->raw.p = *p;
952 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000953 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000954
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000955 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000956 return( ret );
957
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000958 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000959 return( ret );
960
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000961 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000962 return( ret );
963
Paul Bakker74111d32011-01-15 16:57:55 +0000964 if ( *p < end )
965 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000966 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
967 cur_entry = cur_entry->next;
968 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
969 }
970 }
971
972 return( 0 );
973}
974
Paul Bakker27d66162010-03-17 06:56:01 +0000975static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
976{
977 if( sig_oid->len == 9 &&
978 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
979 {
980 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
981 {
982 *sig_alg = sig_oid->p[8];
983 return( 0 );
984 }
985
986 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
987 {
988 *sig_alg = sig_oid->p[8];
989 return( 0 );
990 }
991
992 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
993 }
Paul Bakker400ff6f2011-02-20 10:40:16 +0000994 if( sig_oid->len == 5 &&
995 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
996 {
997 *sig_alg = SIG_RSA_SHA1;
998 return( 0 );
999 }
Paul Bakker27d66162010-03-17 06:56:01 +00001000
1001 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1002}
1003
Paul Bakkerd98030e2009-05-02 15:13:40 +00001004/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 * Parse one or more certificates and add them to the chained list
1006 */
Paul Bakker23986e52011-04-24 08:57:21 +00001007int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001008{
Paul Bakker23986e52011-04-24 08:57:21 +00001009 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001010 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 unsigned char *p, *end;
1012 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001013#if defined(POLARSSL_PEM_C)
1014 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001015 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001016#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
1018 crt = chain;
1019
Paul Bakker320a4b52009-03-28 18:52:39 +00001020 /*
1021 * Check for valid input
1022 */
1023 if( crt == NULL || buf == NULL )
1024 return( 1 );
1025
Paul Bakkere9581d62009-03-28 20:29:25 +00001026 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 crt = crt->next;
1028
1029 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001030 * Add new certificate on the end of the chain if needed.
1031 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001032 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001033 {
1034 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1035
Paul Bakker7d06ad22009-05-02 15:53:56 +00001036 if( crt->next == NULL )
1037 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001038 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001039 return( 1 );
1040 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001041
Paul Bakker7d06ad22009-05-02 15:53:56 +00001042 crt = crt->next;
1043 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001044 }
1045
Paul Bakker96743fc2011-02-12 14:30:57 +00001046#if defined(POLARSSL_PEM_C)
1047 pem_init( &pem );
1048 ret = pem_read_buffer( &pem,
1049 "-----BEGIN CERTIFICATE-----",
1050 "-----END CERTIFICATE-----",
1051 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052
Paul Bakker96743fc2011-02-12 14:30:57 +00001053 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001055 /*
1056 * Was PEM encoded
1057 */
1058 buflen -= use_len;
1059 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
1061 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001062 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001064 p = pem.buf;
1065 pem.buf = NULL;
1066 len = pem.buflen;
1067 pem_free( &pem );
1068 }
1069 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1070 {
1071 pem_free( &pem );
1072 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
1074 else
1075 {
1076 /*
1077 * nope, copy the raw DER data
1078 */
1079 p = (unsigned char *) malloc( len = buflen );
1080
1081 if( p == NULL )
1082 return( 1 );
1083
1084 memcpy( p, buf, buflen );
1085
1086 buflen = 0;
1087 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001088#else
1089 p = (unsigned char *) malloc( len = buflen );
1090
1091 if( p == NULL )
1092 return( 1 );
1093
1094 memcpy( p, buf, buflen );
1095
1096 buflen = 0;
1097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
1099 crt->raw.p = p;
1100 crt->raw.len = len;
1101 end = p + len;
1102
1103 /*
1104 * Certificate ::= SEQUENCE {
1105 * tbsCertificate TBSCertificate,
1106 * signatureAlgorithm AlgorithmIdentifier,
1107 * signatureValue BIT STRING }
1108 */
1109 if( ( ret = asn1_get_tag( &p, end, &len,
1110 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1111 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001112 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001113 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 }
1115
Paul Bakker23986e52011-04-24 08:57:21 +00001116 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001118 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001119 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001120 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 }
1122
1123 /*
1124 * TBSCertificate ::= SEQUENCE {
1125 */
1126 crt->tbs.p = p;
1127
1128 if( ( ret = asn1_get_tag( &p, end, &len,
1129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1130 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001131 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001132 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 }
1134
1135 end = p + len;
1136 crt->tbs.len = end - crt->tbs.p;
1137
1138 /*
1139 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1140 *
1141 * CertificateSerialNumber ::= INTEGER
1142 *
1143 * signature AlgorithmIdentifier
1144 */
1145 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1146 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1147 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1148 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001149 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 return( ret );
1151 }
1152
1153 crt->version++;
1154
1155 if( crt->version > 3 )
1156 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001157 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001158 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 }
1160
Paul Bakker27d66162010-03-17 06:56:01 +00001161 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001163 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001164 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 /*
1168 * issuer Name
1169 */
1170 crt->issuer_raw.p = p;
1171
1172 if( ( ret = asn1_get_tag( &p, end, &len,
1173 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1174 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001175 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001176 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 }
1178
1179 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1180 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001181 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 return( ret );
1183 }
1184
1185 crt->issuer_raw.len = p - crt->issuer_raw.p;
1186
1187 /*
1188 * Validity ::= SEQUENCE {
1189 * notBefore Time,
1190 * notAfter Time }
1191 *
1192 */
1193 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1194 &crt->valid_to ) ) != 0 )
1195 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001196 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 return( ret );
1198 }
1199
1200 /*
1201 * subject Name
1202 */
1203 crt->subject_raw.p = p;
1204
1205 if( ( ret = asn1_get_tag( &p, end, &len,
1206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1207 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001208 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001209 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
1212 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 return( ret );
1216 }
1217
1218 crt->subject_raw.len = p - crt->subject_raw.p;
1219
1220 /*
1221 * SubjectPublicKeyInfo ::= SEQUENCE
1222 * algorithm AlgorithmIdentifier,
1223 * subjectPublicKey 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 Bakker9d781402011-05-09 16:17:09 +00001229 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
1232 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1233 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1234 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001235 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 return( ret );
1237 }
1238
1239 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1240 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001241 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 return( ret );
1243 }
1244
1245 crt->rsa.len = mpi_size( &crt->rsa.N );
1246
1247 /*
1248 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1249 * -- If present, version shall be v2 or v3
1250 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1251 * -- If present, version shall be v2 or v3
1252 * extensions [3] EXPLICIT Extensions OPTIONAL
1253 * -- If present, version shall be v3
1254 */
1255 if( crt->version == 2 || crt->version == 3 )
1256 {
1257 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1258 if( ret != 0 )
1259 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001260 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 return( ret );
1262 }
1263 }
1264
1265 if( crt->version == 2 || crt->version == 3 )
1266 {
1267 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1268 if( ret != 0 )
1269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 return( ret );
1272 }
1273 }
1274
1275 if( crt->version == 3 )
1276 {
Paul Bakker74111d32011-01-15 16:57:55 +00001277 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 if( ret != 0 )
1279 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001280 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 return( ret );
1282 }
1283 }
1284
1285 if( p != end )
1286 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001287 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001288 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001289 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 }
1291
1292 end = crt->raw.p + crt->raw.len;
1293
1294 /*
1295 * signatureAlgorithm AlgorithmIdentifier,
1296 * signatureValue BIT STRING
1297 */
1298 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1299 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001300 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 return( ret );
1302 }
1303
Paul Bakker320a4b52009-03-28 18:52:39 +00001304 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001306 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001307 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 }
1309
1310 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 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 if( p != end )
1317 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001318 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001319 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001320 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 }
1322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001324 {
1325 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1326
Paul Bakker7d06ad22009-05-02 15:53:56 +00001327 if( crt->next == NULL )
1328 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001329 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 return( 1 );
1331 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001332
Paul Bakker7d06ad22009-05-02 15:53:56 +00001333 crt = crt->next;
1334 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
1339 return( 0 );
1340}
1341
1342/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001343 * Parse one or more CRLs and add them to the chained list
1344 */
Paul Bakker23986e52011-04-24 08:57:21 +00001345int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001346{
Paul Bakker23986e52011-04-24 08:57:21 +00001347 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001348 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001349 unsigned char *p, *end;
1350 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001351#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001352 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001353 pem_context pem;
1354#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001355
1356 crl = chain;
1357
1358 /*
1359 * Check for valid input
1360 */
1361 if( crl == NULL || buf == NULL )
1362 return( 1 );
1363
1364 while( crl->version != 0 && crl->next != NULL )
1365 crl = crl->next;
1366
1367 /*
1368 * Add new CRL on the end of the chain if needed.
1369 */
1370 if ( crl->version != 0 && crl->next == NULL)
1371 {
1372 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1373
Paul Bakker7d06ad22009-05-02 15:53:56 +00001374 if( crl->next == NULL )
1375 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001376 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001377 return( 1 );
1378 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001379
Paul Bakker7d06ad22009-05-02 15:53:56 +00001380 crl = crl->next;
1381 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001382 }
1383
Paul Bakker96743fc2011-02-12 14:30:57 +00001384#if defined(POLARSSL_PEM_C)
1385 pem_init( &pem );
1386 ret = pem_read_buffer( &pem,
1387 "-----BEGIN X509 CRL-----",
1388 "-----END X509 CRL-----",
1389 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001390
Paul Bakker96743fc2011-02-12 14:30:57 +00001391 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001392 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001393 /*
1394 * Was PEM encoded
1395 */
1396 buflen -= use_len;
1397 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001398
1399 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001400 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001401 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001402 p = pem.buf;
1403 pem.buf = NULL;
1404 len = pem.buflen;
1405 pem_free( &pem );
1406 }
1407 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1408 {
1409 pem_free( &pem );
1410 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001411 }
1412 else
1413 {
1414 /*
1415 * nope, copy the raw DER data
1416 */
1417 p = (unsigned char *) malloc( len = buflen );
1418
1419 if( p == NULL )
1420 return( 1 );
1421
1422 memcpy( p, buf, buflen );
1423
1424 buflen = 0;
1425 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001426#else
1427 p = (unsigned char *) malloc( len = buflen );
1428
1429 if( p == NULL )
1430 return( 1 );
1431
1432 memcpy( p, buf, buflen );
1433
1434 buflen = 0;
1435#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001436
1437 crl->raw.p = p;
1438 crl->raw.len = len;
1439 end = p + len;
1440
1441 /*
1442 * CertificateList ::= SEQUENCE {
1443 * tbsCertList TBSCertList,
1444 * signatureAlgorithm AlgorithmIdentifier,
1445 * signatureValue BIT STRING }
1446 */
1447 if( ( ret = asn1_get_tag( &p, end, &len,
1448 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1449 {
1450 x509_crl_free( crl );
1451 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1452 }
1453
Paul Bakker23986e52011-04-24 08:57:21 +00001454 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001455 {
1456 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001457 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001458 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1459 }
1460
1461 /*
1462 * TBSCertList ::= SEQUENCE {
1463 */
1464 crl->tbs.p = p;
1465
1466 if( ( ret = asn1_get_tag( &p, end, &len,
1467 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1468 {
1469 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001470 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001471 }
1472
1473 end = p + len;
1474 crl->tbs.len = end - crl->tbs.p;
1475
1476 /*
1477 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1478 * -- if present, MUST be v2
1479 *
1480 * signature AlgorithmIdentifier
1481 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001482 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001483 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1484 {
1485 x509_crl_free( crl );
1486 return( ret );
1487 }
1488
1489 crl->version++;
1490
1491 if( crl->version > 2 )
1492 {
1493 x509_crl_free( crl );
1494 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1495 }
1496
Paul Bakker27d66162010-03-17 06:56:01 +00001497 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498 {
1499 x509_crl_free( crl );
1500 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1501 }
1502
1503 /*
1504 * issuer Name
1505 */
1506 crl->issuer_raw.p = p;
1507
1508 if( ( ret = asn1_get_tag( &p, end, &len,
1509 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1510 {
1511 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001512 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001513 }
1514
1515 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1516 {
1517 x509_crl_free( crl );
1518 return( ret );
1519 }
1520
1521 crl->issuer_raw.len = p - crl->issuer_raw.p;
1522
1523 /*
1524 * thisUpdate Time
1525 * nextUpdate Time OPTIONAL
1526 */
Paul Bakker91200182010-02-18 21:26:15 +00001527 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001528 {
1529 x509_crl_free( crl );
1530 return( ret );
1531 }
1532
Paul Bakker91200182010-02-18 21:26:15 +00001533 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001534 {
Paul Bakker9d781402011-05-09 16:17:09 +00001535 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001536 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001537 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001538 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001539 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001540 x509_crl_free( crl );
1541 return( ret );
1542 }
1543 }
1544
1545 /*
1546 * revokedCertificates SEQUENCE OF SEQUENCE {
1547 * userCertificate CertificateSerialNumber,
1548 * revocationDate Time,
1549 * crlEntryExtensions Extensions OPTIONAL
1550 * -- if present, MUST be v2
1551 * } OPTIONAL
1552 */
1553 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1554 {
1555 x509_crl_free( crl );
1556 return( ret );
1557 }
1558
1559 /*
1560 * crlExtensions EXPLICIT Extensions OPTIONAL
1561 * -- if present, MUST be v2
1562 */
1563 if( crl->version == 2 )
1564 {
1565 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1566
1567 if( ret != 0 )
1568 {
1569 x509_crl_free( crl );
1570 return( ret );
1571 }
1572 }
1573
1574 if( p != end )
1575 {
1576 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001577 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1579 }
1580
1581 end = crl->raw.p + crl->raw.len;
1582
1583 /*
1584 * signatureAlgorithm AlgorithmIdentifier,
1585 * signatureValue BIT STRING
1586 */
1587 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1588 {
1589 x509_crl_free( crl );
1590 return( ret );
1591 }
1592
1593 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1594 {
1595 x509_crl_free( crl );
1596 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1597 }
1598
1599 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1600 {
1601 x509_crl_free( crl );
1602 return( ret );
1603 }
1604
1605 if( p != end )
1606 {
1607 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001608 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1610 }
1611
1612 if( buflen > 0 )
1613 {
1614 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1615
Paul Bakker7d06ad22009-05-02 15:53:56 +00001616 if( crl->next == NULL )
1617 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001618 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001619 return( 1 );
1620 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001621
Paul Bakker7d06ad22009-05-02 15:53:56 +00001622 crl = crl->next;
1623 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624
1625 return( x509parse_crl( crl, buf, buflen ) );
1626 }
1627
1628 return( 0 );
1629}
1630
Paul Bakker335db3f2011-04-25 15:28:35 +00001631#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001633 * Load all data from a file into a given buffer.
1634 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001635int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001636{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001638
Paul Bakkerd98030e2009-05-02 15:13:40 +00001639 if( ( f = fopen( path, "rb" ) ) == NULL )
1640 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001641
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642 fseek( f, 0, SEEK_END );
1643 *n = (size_t) ftell( f );
1644 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001645
Paul Bakkerd98030e2009-05-02 15:13:40 +00001646 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1647 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001648
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 if( fread( *buf, 1, *n, f ) != *n )
1650 {
1651 fclose( f );
1652 free( *buf );
1653 return( 1 );
1654 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001655
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001657
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001659
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001661}
1662
1663/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 * Load one or more certificates and add them to the chained list
1665 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001666int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001667{
1668 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 size_t n;
1670 unsigned char *buf;
1671
Paul Bakker2b245eb2009-04-19 18:44:26 +00001672 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 return( 1 );
1674
Paul Bakker27fdf462011-06-09 13:55:13 +00001675 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
1677 memset( buf, 0, n + 1 );
1678 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680 return( ret );
1681}
1682
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683/*
1684 * Load one or more CRLs and add them to the chained list
1685 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001686int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687{
1688 int ret;
1689 size_t n;
1690 unsigned char *buf;
1691
1692 if ( load_file( path, &buf, &n ) )
1693 return( 1 );
1694
Paul Bakker27fdf462011-06-09 13:55:13 +00001695 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001696
1697 memset( buf, 0, n + 1 );
1698 free( buf );
1699
1700 return( ret );
1701}
1702
Paul Bakker5121ce52009-01-03 21:22:43 +00001703/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001704 * Load and parse a private RSA key
1705 */
1706int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1707{
1708 int ret;
1709 size_t n;
1710 unsigned char *buf;
1711
1712 if ( load_file( path, &buf, &n ) )
1713 return( 1 );
1714
1715 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001716 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001717 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001718 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001719 (unsigned char *) pwd, strlen( pwd ) );
1720
1721 memset( buf, 0, n + 1 );
1722 free( buf );
1723
1724 return( ret );
1725}
1726
1727/*
1728 * Load and parse a public RSA key
1729 */
1730int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1731{
1732 int ret;
1733 size_t n;
1734 unsigned char *buf;
1735
1736 if ( load_file( path, &buf, &n ) )
1737 return( 1 );
1738
Paul Bakker27fdf462011-06-09 13:55:13 +00001739 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001740
1741 memset( buf, 0, n + 1 );
1742 free( buf );
1743
1744 return( ret );
1745}
1746#endif /* POLARSSL_FS_IO */
1747
1748/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001749 * Parse a private RSA key
1750 */
Paul Bakker23986e52011-04-24 08:57:21 +00001751int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1752 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001753{
Paul Bakker23986e52011-04-24 08:57:21 +00001754 int ret;
1755 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001757 unsigned char *p_alt;
1758 x509_buf pk_alg_oid;
1759
Paul Bakker96743fc2011-02-12 14:30:57 +00001760#if defined(POLARSSL_PEM_C)
1761 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Paul Bakker96743fc2011-02-12 14:30:57 +00001763 pem_init( &pem );
1764 ret = pem_read_buffer( &pem,
1765 "-----BEGIN RSA PRIVATE KEY-----",
1766 "-----END RSA PRIVATE KEY-----",
1767 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Paul Bakkered56b222011-07-13 11:26:43 +00001769 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1770 {
1771 ret = pem_read_buffer( &pem,
1772 "-----BEGIN PRIVATE KEY-----",
1773 "-----END PRIVATE KEY-----",
1774 key, pwd, pwdlen, &len );
1775 }
1776
Paul Bakker96743fc2011-02-12 14:30:57 +00001777 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001779 /*
1780 * Was PEM encoded
1781 */
1782 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001784 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001785 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001786 pem_free( &pem );
1787 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001788 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Paul Bakker96743fc2011-02-12 14:30:57 +00001790 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1791#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001792 ((void) pwd);
1793 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001794 p = (unsigned char *) key;
1795#endif
1796 end = p + keylen;
1797
Paul Bakker5121ce52009-01-03 21:22:43 +00001798 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001799 * Note: Depending on the type of private key file one can expect either a
1800 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1801 *
1802 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001803 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001804 * algorithm AlgorithmIdentifier,
1805 * PrivateKey BIT STRING
1806 * }
1807 *
1808 * AlgorithmIdentifier ::= SEQUENCE {
1809 * algorithm OBJECT IDENTIFIER,
1810 * parameters ANY DEFINED BY algorithm OPTIONAL
1811 * }
1812 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 * RSAPrivateKey ::= SEQUENCE {
1814 * version Version,
1815 * modulus INTEGER, -- n
1816 * publicExponent INTEGER, -- e
1817 * privateExponent INTEGER, -- d
1818 * prime1 INTEGER, -- p
1819 * prime2 INTEGER, -- q
1820 * exponent1 INTEGER, -- d mod (p-1)
1821 * exponent2 INTEGER, -- d mod (q-1)
1822 * coefficient INTEGER, -- (inverse of q) mod p
1823 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1824 * }
1825 */
1826 if( ( ret = asn1_get_tag( &p, end, &len,
1827 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1828 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001829#if defined(POLARSSL_PEM_C)
1830 pem_free( &pem );
1831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001833 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 }
1835
1836 end = p + len;
1837
1838 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1839 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001840#if defined(POLARSSL_PEM_C)
1841 pem_free( &pem );
1842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001844 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 }
1846
1847 if( rsa->ver != 0 )
1848 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001849#if defined(POLARSSL_PEM_C)
1850 pem_free( &pem );
1851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001853 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 }
1855
Paul Bakkered56b222011-07-13 11:26:43 +00001856 p_alt = p;
1857
1858 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1859 {
1860 // Assume that we have the PKCS#1 format if wrong
1861 // tag was encountered
1862 //
1863 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1864 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1865 {
1866#if defined(POLARSSL_PEM_C)
1867 pem_free( &pem );
1868#endif
1869 rsa_free( rsa );
1870 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1871 }
1872 }
1873 else
1874 {
1875 int can_handle;
1876
1877 /*
1878 * only RSA keys handled at this time
1879 */
1880 can_handle = 0;
1881
1882 if( pk_alg_oid.len == 9 &&
1883 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1884 can_handle = 1;
1885
1886 if( pk_alg_oid.len == 9 &&
1887 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1888 {
1889 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1890 can_handle = 1;
1891
1892 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1893 can_handle = 1;
1894 }
1895
1896 if( pk_alg_oid.len == 5 &&
1897 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1898 can_handle = 1;
1899
1900 if( can_handle == 0 )
1901 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1902
1903 /*
1904 * Parse the PKCS#8 format
1905 */
1906
1907 p = p_alt;
1908 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1909 {
1910#if defined(POLARSSL_PEM_C)
1911 pem_free( &pem );
1912#endif
1913 rsa_free( rsa );
1914 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1915 }
1916
1917 if( ( end - p ) < 1 )
1918 {
1919#if defined(POLARSSL_PEM_C)
1920 pem_free( &pem );
1921#endif
1922 rsa_free( rsa );
1923 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1924 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1925 }
1926
1927 end = p + len;
1928
1929 if( ( ret = asn1_get_tag( &p, end, &len,
1930 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1931 {
1932#if defined(POLARSSL_PEM_C)
1933 pem_free( &pem );
1934#endif
1935 rsa_free( rsa );
1936 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1937 }
1938
1939 end = p + len;
1940
1941 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1942 {
1943#if defined(POLARSSL_PEM_C)
1944 pem_free( &pem );
1945#endif
1946 rsa_free( rsa );
1947 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1948 }
1949
1950 if( rsa->ver != 0 )
1951 {
1952#if defined(POLARSSL_PEM_C)
1953 pem_free( &pem );
1954#endif
1955 rsa_free( rsa );
1956 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
1957 }
1958 }
1959
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
1961 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
1962 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
1963 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
1964 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
1965 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
1966 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
1967 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
1968 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001969#if defined(POLARSSL_PEM_C)
1970 pem_free( &pem );
1971#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001973 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 }
1975
1976 rsa->len = mpi_size( &rsa->N );
1977
1978 if( p != end )
1979 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001980#if defined(POLARSSL_PEM_C)
1981 pem_free( &pem );
1982#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001984 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001985 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 }
1987
1988 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
1989 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001990#if defined(POLARSSL_PEM_C)
1991 pem_free( &pem );
1992#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 rsa_free( rsa );
1994 return( ret );
1995 }
1996
Paul Bakker96743fc2011-02-12 14:30:57 +00001997#if defined(POLARSSL_PEM_C)
1998 pem_free( &pem );
1999#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
2001 return( 0 );
2002}
2003
2004/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002005 * Parse a public RSA key
2006 */
Paul Bakker23986e52011-04-24 08:57:21 +00002007int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002008{
Paul Bakker23986e52011-04-24 08:57:21 +00002009 int ret;
2010 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002011 unsigned char *p, *end;
2012 x509_buf alg_oid;
2013#if defined(POLARSSL_PEM_C)
2014 pem_context pem;
2015
2016 pem_init( &pem );
2017 ret = pem_read_buffer( &pem,
2018 "-----BEGIN PUBLIC KEY-----",
2019 "-----END PUBLIC KEY-----",
2020 key, NULL, 0, &len );
2021
2022 if( ret == 0 )
2023 {
2024 /*
2025 * Was PEM encoded
2026 */
2027 keylen = pem.buflen;
2028 }
2029 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2030 {
2031 pem_free( &pem );
2032 return( ret );
2033 }
2034
2035 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2036#else
2037 p = (unsigned char *) key;
2038#endif
2039 end = p + keylen;
2040
2041 /*
2042 * PublicKeyInfo ::= SEQUENCE {
2043 * algorithm AlgorithmIdentifier,
2044 * PublicKey BIT STRING
2045 * }
2046 *
2047 * AlgorithmIdentifier ::= SEQUENCE {
2048 * algorithm OBJECT IDENTIFIER,
2049 * parameters ANY DEFINED BY algorithm OPTIONAL
2050 * }
2051 *
2052 * RSAPublicKey ::= SEQUENCE {
2053 * modulus INTEGER, -- n
2054 * publicExponent INTEGER -- e
2055 * }
2056 */
2057
2058 if( ( ret = asn1_get_tag( &p, end, &len,
2059 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2060 {
2061#if defined(POLARSSL_PEM_C)
2062 pem_free( &pem );
2063#endif
2064 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002065 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002066 }
2067
2068 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2069 {
2070#if defined(POLARSSL_PEM_C)
2071 pem_free( &pem );
2072#endif
2073 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002074 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002075 }
2076
2077 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2078 {
2079#if defined(POLARSSL_PEM_C)
2080 pem_free( &pem );
2081#endif
2082 rsa_free( rsa );
2083 return( ret );
2084 }
2085
2086 rsa->len = mpi_size( &rsa->N );
2087
2088#if defined(POLARSSL_PEM_C)
2089 pem_free( &pem );
2090#endif
2091
2092 return( 0 );
2093}
2094
Paul Bakkereaa89f82011-04-04 21:36:15 +00002095#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002096/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002097 * Parse DHM parameters
2098 */
Paul Bakker23986e52011-04-24 08:57:21 +00002099int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002100{
Paul Bakker23986e52011-04-24 08:57:21 +00002101 int ret;
2102 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002103 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002104#if defined(POLARSSL_PEM_C)
2105 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002106
Paul Bakker96743fc2011-02-12 14:30:57 +00002107 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002108
Paul Bakker96743fc2011-02-12 14:30:57 +00002109 ret = pem_read_buffer( &pem,
2110 "-----BEGIN DH PARAMETERS-----",
2111 "-----END DH PARAMETERS-----",
2112 dhmin, NULL, 0, &dhminlen );
2113
2114 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002115 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002116 /*
2117 * Was PEM encoded
2118 */
2119 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002120 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002121 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002122 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002123 pem_free( &pem );
2124 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002125 }
2126
Paul Bakker96743fc2011-02-12 14:30:57 +00002127 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2128#else
2129 p = (unsigned char *) dhmin;
2130#endif
2131 end = p + dhminlen;
2132
Paul Bakker1b57b062011-01-06 15:48:19 +00002133 memset( dhm, 0, sizeof( dhm_context ) );
2134
Paul Bakker1b57b062011-01-06 15:48:19 +00002135 /*
2136 * DHParams ::= SEQUENCE {
2137 * prime INTEGER, -- P
2138 * generator INTEGER, -- g
2139 * }
2140 */
2141 if( ( ret = asn1_get_tag( &p, end, &len,
2142 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2143 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002144#if defined(POLARSSL_PEM_C)
2145 pem_free( &pem );
2146#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002147 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002148 }
2149
2150 end = p + len;
2151
2152 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2153 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2154 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002155#if defined(POLARSSL_PEM_C)
2156 pem_free( &pem );
2157#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002158 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002159 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002160 }
2161
2162 if( p != end )
2163 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002164#if defined(POLARSSL_PEM_C)
2165 pem_free( &pem );
2166#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002167 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002168 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002169 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2170 }
2171
Paul Bakker96743fc2011-02-12 14:30:57 +00002172#if defined(POLARSSL_PEM_C)
2173 pem_free( &pem );
2174#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002175
2176 return( 0 );
2177}
2178
Paul Bakker335db3f2011-04-25 15:28:35 +00002179#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002180/*
2181 * Load and parse a private RSA key
2182 */
2183int x509parse_dhmfile( dhm_context *dhm, const char *path )
2184{
2185 int ret;
2186 size_t n;
2187 unsigned char *buf;
2188
2189 if ( load_file( path, &buf, &n ) )
2190 return( 1 );
2191
Paul Bakker27fdf462011-06-09 13:55:13 +00002192 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002193
2194 memset( buf, 0, n + 1 );
2195 free( buf );
2196
2197 return( ret );
2198}
Paul Bakker335db3f2011-04-25 15:28:35 +00002199#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002200#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002201
Paul Bakker5121ce52009-01-03 21:22:43 +00002202#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002203#include <stdarg.h>
2204
2205#if !defined vsnprintf
2206#define vsnprintf _vsnprintf
2207#endif // vsnprintf
2208
2209/*
2210 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2211 * Result value is not size of buffer needed, but -1 if no fit is possible.
2212 *
2213 * This fuction tries to 'fix' this by at least suggesting enlarging the
2214 * size by 20.
2215 */
2216int compat_snprintf(char *str, size_t size, const char *format, ...)
2217{
2218 va_list ap;
2219 int res = -1;
2220
2221 va_start( ap, format );
2222
2223 res = vsnprintf( str, size, format, ap );
2224
2225 va_end( ap );
2226
2227 // No quick fix possible
2228 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002229 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002230
2231 return res;
2232}
2233
2234#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002235#endif
2236
Paul Bakkerd98030e2009-05-02 15:13:40 +00002237#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2238
2239#define SAFE_SNPRINTF() \
2240{ \
2241 if( ret == -1 ) \
2242 return( -1 ); \
2243 \
Paul Bakker23986e52011-04-24 08:57:21 +00002244 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002245 p[n - 1] = '\0'; \
2246 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2247 } \
2248 \
Paul Bakker23986e52011-04-24 08:57:21 +00002249 n -= (unsigned int) ret; \
2250 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002251}
2252
Paul Bakker5121ce52009-01-03 21:22:43 +00002253/*
2254 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002255 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002257int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002258{
Paul Bakker23986e52011-04-24 08:57:21 +00002259 int ret;
2260 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002262 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 char s[128], *p;
2264
2265 memset( s, 0, sizeof( s ) );
2266
2267 name = dn;
2268 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002269 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
2271 while( name != NULL )
2272 {
Paul Bakker74111d32011-01-15 16:57:55 +00002273 if( name != dn )
2274 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002275 ret = snprintf( p, n, ", " );
2276 SAFE_SNPRINTF();
2277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
2279 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2280 {
2281 switch( name->oid.p[2] )
2282 {
2283 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002284 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
2286 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002287 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
2289 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002290 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
2292 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002293 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002296 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
2298 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002299 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002300
2301 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002302 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002303 name->oid.p[2] );
2304 break;
2305 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002306 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 }
2308 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2309 {
2310 switch( name->oid.p[8] )
2311 {
2312 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002313 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002314
2315 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002316 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 name->oid.p[8] );
2318 break;
2319 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002320 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 }
2322 else
Paul Bakker74111d32011-01-15 16:57:55 +00002323 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002324 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002325 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
2328 for( i = 0; i < name->val.len; i++ )
2329 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002330 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 break;
2332
2333 c = name->val.p[i];
2334 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2335 s[i] = '?';
2336 else s[i] = c;
2337 }
2338 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002339 ret = snprintf( p, n, "%s", s );
2340 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 name = name->next;
2342 }
2343
Paul Bakker23986e52011-04-24 08:57:21 +00002344 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345}
2346
2347/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002348 * Store the serial in printable form into buf; no more
2349 * than size characters will be written
2350 */
2351int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2352{
Paul Bakker23986e52011-04-24 08:57:21 +00002353 int ret;
2354 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002355 char *p;
2356
2357 p = buf;
2358 n = size;
2359
2360 nr = ( serial->len <= 32 )
2361 ? serial->len : 32;
2362
2363 for( i = 0; i < nr; i++ )
2364 {
2365 ret = snprintf( p, n, "%02X%s",
2366 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2367 SAFE_SNPRINTF();
2368 }
2369
Paul Bakker23986e52011-04-24 08:57:21 +00002370 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002371}
2372
2373/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002374 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002376int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2377 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378{
Paul Bakker23986e52011-04-24 08:57:21 +00002379 int ret;
2380 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002381 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002384 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
Paul Bakkerd98030e2009-05-02 15:13:40 +00002386 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002388 SAFE_SNPRINTF();
2389 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002391 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
Paul Bakkerdd476992011-01-16 21:34:59 +00002393 ret = x509parse_serial_gets( p, n, &crt->serial);
2394 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2397 SAFE_SNPRINTF();
2398 ret = x509parse_dn_gets( p, n, &crt->issuer );
2399 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
Paul Bakkerd98030e2009-05-02 15:13:40 +00002401 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2402 SAFE_SNPRINTF();
2403 ret = x509parse_dn_gets( p, n, &crt->subject );
2404 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Paul Bakkerd98030e2009-05-02 15:13:40 +00002406 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2408 crt->valid_from.year, crt->valid_from.mon,
2409 crt->valid_from.day, crt->valid_from.hour,
2410 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Paul Bakkerd98030e2009-05-02 15:13:40 +00002413 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002414 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2415 crt->valid_to.year, crt->valid_to.mon,
2416 crt->valid_to.day, crt->valid_to.hour,
2417 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Paul Bakkerd98030e2009-05-02 15:13:40 +00002420 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2421 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Paul Bakker27d66162010-03-17 06:56:01 +00002423 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002425 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2426 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2427 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2428 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2429 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2430 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2431 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2432 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2433 default: ret = snprintf( p, n, "???" ); break;
2434 }
2435 SAFE_SNPRINTF();
2436
2437 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002438 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002439 SAFE_SNPRINTF();
2440
Paul Bakker23986e52011-04-24 08:57:21 +00002441 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002442}
2443
Paul Bakker74111d32011-01-15 16:57:55 +00002444/* Compare a given OID string with an OID x509_buf * */
2445#define OID_CMP(oid_str, oid_buf) \
2446 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2447 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2448
2449/*
2450 * Return an informational string describing the given OID
2451 */
2452const char *x509_oid_get_description( x509_buf *oid )
2453{
2454 if ( oid == NULL )
2455 return ( NULL );
2456
2457 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2458 return( STRING_SERVER_AUTH );
2459
2460 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2461 return( STRING_CLIENT_AUTH );
2462
2463 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2464 return( STRING_CODE_SIGNING );
2465
2466 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2467 return( STRING_EMAIL_PROTECTION );
2468
2469 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2470 return( STRING_TIME_STAMPING );
2471
2472 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2473 return( STRING_OCSP_SIGNING );
2474
2475 return( NULL );
2476}
2477
2478/* Return the x.y.z.... style numeric string for the given OID */
2479int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2480{
Paul Bakker23986e52011-04-24 08:57:21 +00002481 int ret;
2482 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002483 unsigned int value;
2484 char *p;
2485
2486 p = buf;
2487 n = size;
2488
2489 /* First byte contains first two dots */
2490 if( oid->len > 0 )
2491 {
2492 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2493 SAFE_SNPRINTF();
2494 }
2495
2496 /* TODO: value can overflow in value. */
2497 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002498 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002499 {
2500 value <<= 7;
2501 value += oid->p[i] & 0x7F;
2502
2503 if( !( oid->p[i] & 0x80 ) )
2504 {
2505 /* Last byte */
2506 ret = snprintf( p, n, ".%d", value );
2507 SAFE_SNPRINTF();
2508 value = 0;
2509 }
2510 }
2511
Paul Bakker23986e52011-04-24 08:57:21 +00002512 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002513}
2514
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515/*
2516 * Return an informational string about the CRL.
2517 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002518int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2519 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520{
Paul Bakker23986e52011-04-24 08:57:21 +00002521 int ret;
2522 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002523 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002524 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525
2526 p = buf;
2527 n = size;
2528
2529 ret = snprintf( p, n, "%sCRL version : %d",
2530 prefix, crl->version );
2531 SAFE_SNPRINTF();
2532
2533 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2534 SAFE_SNPRINTF();
2535 ret = x509parse_dn_gets( p, n, &crl->issuer );
2536 SAFE_SNPRINTF();
2537
2538 ret = snprintf( p, n, "\n%sthis update : " \
2539 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2540 crl->this_update.year, crl->this_update.mon,
2541 crl->this_update.day, crl->this_update.hour,
2542 crl->this_update.min, crl->this_update.sec );
2543 SAFE_SNPRINTF();
2544
2545 ret = snprintf( p, n, "\n%snext update : " \
2546 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2547 crl->next_update.year, crl->next_update.mon,
2548 crl->next_update.day, crl->next_update.hour,
2549 crl->next_update.min, crl->next_update.sec );
2550 SAFE_SNPRINTF();
2551
2552 entry = &crl->entry;
2553
2554 ret = snprintf( p, n, "\n%sRevoked certificates:",
2555 prefix );
2556 SAFE_SNPRINTF();
2557
Paul Bakker9be19372009-07-27 20:21:53 +00002558 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002559 {
2560 ret = snprintf( p, n, "\n%sserial number: ",
2561 prefix );
2562 SAFE_SNPRINTF();
2563
2564 nr = ( entry->serial.len <= 32 )
2565 ? entry->serial.len : 32;
2566
Paul Bakker74111d32011-01-15 16:57:55 +00002567 for( i = 0; i < nr; i++ )
2568 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002569 ret = snprintf( p, n, "%02X%s",
2570 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2571 SAFE_SNPRINTF();
2572 }
2573
2574 ret = snprintf( p, n, " revocation date: " \
2575 "%04d-%02d-%02d %02d:%02d:%02d",
2576 entry->revocation_date.year, entry->revocation_date.mon,
2577 entry->revocation_date.day, entry->revocation_date.hour,
2578 entry->revocation_date.min, entry->revocation_date.sec );
2579 SAFE_SNPRINTF();
2580
2581 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
2583
Paul Bakkerd98030e2009-05-02 15:13:40 +00002584 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2585 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Paul Bakker27d66162010-03-17 06:56:01 +00002587 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002588 {
2589 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2590 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2591 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2592 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2593 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2594 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2595 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2596 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2597 default: ret = snprintf( p, n, "???" ); break;
2598 }
2599 SAFE_SNPRINTF();
2600
Paul Bakker1e27bb22009-07-19 20:25:25 +00002601 ret = snprintf( p, n, "\n" );
2602 SAFE_SNPRINTF();
2603
Paul Bakker23986e52011-04-24 08:57:21 +00002604 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605}
2606
2607/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002608 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002610int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611{
2612 struct tm *lt;
2613 time_t tt;
2614
2615 tt = time( NULL );
2616 lt = localtime( &tt );
2617
Paul Bakker40ea7de2009-05-03 10:18:48 +00002618 if( lt->tm_year > to->year - 1900 )
2619 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Paul Bakker40ea7de2009-05-03 10:18:48 +00002621 if( lt->tm_year == to->year - 1900 &&
2622 lt->tm_mon > to->mon - 1 )
2623 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakker40ea7de2009-05-03 10:18:48 +00002625 if( lt->tm_year == to->year - 1900 &&
2626 lt->tm_mon == to->mon - 1 &&
2627 lt->tm_mday > to->day )
2628 return( 1 );
2629
Paul Bakkerb6194992011-01-16 21:40:22 +00002630 if( lt->tm_year == to->year - 1900 &&
2631 lt->tm_mon == to->mon - 1 &&
2632 lt->tm_mday == to->day &&
2633 lt->tm_hour > to->hour - 1)
2634 return( 1 );
2635
2636 if( lt->tm_year == to->year - 1900 &&
2637 lt->tm_mon == to->mon - 1 &&
2638 lt->tm_mday == to->day &&
2639 lt->tm_hour == to->hour - 1 &&
2640 lt->tm_min > to->min - 1 )
2641 return( 1 );
2642
2643 if( lt->tm_year == to->year - 1900 &&
2644 lt->tm_mon == to->mon - 1 &&
2645 lt->tm_mday == to->day &&
2646 lt->tm_hour == to->hour - 1 &&
2647 lt->tm_min == to->min - 1 &&
2648 lt->tm_sec > to->sec - 1 )
2649 return( 1 );
2650
Paul Bakker40ea7de2009-05-03 10:18:48 +00002651 return( 0 );
2652}
2653
2654/*
2655 * Return 1 if the certificate is revoked, or 0 otherwise.
2656 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002657int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002658{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002659 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002660
2661 while( cur != NULL && cur->serial.len != 0 )
2662 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002663 if( crt->serial.len == cur->serial.len &&
2664 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002665 {
2666 if( x509parse_time_expired( &cur->revocation_date ) )
2667 return( 1 );
2668 }
2669
2670 cur = cur->next;
2671 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002672
2673 return( 0 );
2674}
2675
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002676/*
2677 * Wrapper for x509 hashes.
2678 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002679 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002680 */
Paul Bakker23986e52011-04-24 08:57:21 +00002681static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 unsigned char *out )
2683{
2684 switch( alg )
2685 {
Paul Bakker40e46942009-01-03 21:51:57 +00002686#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002687 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002688#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002689#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002690 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002692#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002693 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002694#endif
2695#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002696 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002697#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002698#if defined(POLARSSL_SHA2_C)
2699 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2700 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2701#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002702#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002703 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2704 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2705#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002707 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 break;
2709 }
2710}
2711
2712/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002713 * Check that the given certificate is valid accoring to the CRL.
2714 */
2715static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2716 x509_crl *crl_list)
2717{
2718 int flags = 0;
2719 int hash_id;
2720 unsigned char hash[64];
2721
2722 /*
2723 * TODO: What happens if no CRL is present?
2724 * Suggestion: Revocation state should be unknown if no CRL is present.
2725 * For backwards compatibility this is not yet implemented.
2726 */
2727
2728 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2729 {
2730 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2731 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2732 crl_list->issuer_raw.len ) != 0 )
2733 {
2734 crl_list = crl_list->next;
2735 continue;
2736 }
2737
2738 /*
2739 * Check if CRL is correctly signed by the trusted CA
2740 */
2741 hash_id = crl_list->sig_alg;
2742
2743 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2744
2745 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2746 0, hash, crl_list->sig.p ) == 0 )
2747 {
2748 /*
2749 * CRL is not trusted
2750 */
2751 flags |= BADCRL_NOT_TRUSTED;
2752 break;
2753 }
2754
2755 /*
2756 * Check for validity of CRL (Do not drop out)
2757 */
2758 if( x509parse_time_expired( &crl_list->next_update ) )
2759 flags |= BADCRL_EXPIRED;
2760
2761 /*
2762 * Check if certificate is revoked
2763 */
2764 if( x509parse_revoked(crt, crl_list) )
2765 {
2766 flags |= BADCERT_REVOKED;
2767 break;
2768 }
2769
2770 crl_list = crl_list->next;
2771 }
2772 return flags;
2773}
2774
2775/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 * Verify the certificate validity
2777 */
2778int x509parse_verify( x509_cert *crt,
2779 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002780 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002781 const char *cn, int *flags,
2782 int (*f_vrfy)(void *, x509_cert *, int, int),
2783 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002784{
Paul Bakker23986e52011-04-24 08:57:21 +00002785 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 int hash_id;
2787 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002788 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002790 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker40ea7de2009-05-03 10:18:48 +00002792 *flags = 0;
2793
2794 if( x509parse_time_expired( &crt->valid_to ) )
2795 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
2797 if( cn != NULL )
2798 {
2799 name = &crt->subject;
2800 cn_len = strlen( cn );
2801
2802 while( name != NULL )
2803 {
2804 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2805 memcmp( name->val.p, cn, cn_len ) == 0 &&
2806 name->val.len == cn_len )
2807 break;
2808
2809 name = name->next;
2810 }
2811
2812 if( name == NULL )
2813 *flags |= BADCERT_CN_MISMATCH;
2814 }
2815
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 /*
2817 * Iterate upwards in the given cert chain,
2818 * ignoring any upper cert with CA != TRUE.
2819 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002820 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
2822 pathlen = 1;
2823
Paul Bakker76fd75a2011-01-16 21:12:10 +00002824 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002826 if( parent->ca_istrue == 0 ||
2827 crt->issuer_raw.len != parent->subject_raw.len ||
2828 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 crt->issuer_raw.len ) != 0 )
2830 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002831 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 continue;
2833 }
2834
Paul Bakker27d66162010-03-17 06:56:01 +00002835 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2838
Paul Bakker76fd75a2011-01-16 21:12:10 +00002839 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2840 crt->sig.p ) != 0 )
2841 *flags |= BADCERT_NOT_TRUSTED;
2842
2843 /* Check trusted CA's CRL for the given crt */
2844 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002845
2846 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002847 if( NULL != f_vrfy )
2848 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002849 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002850 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002851 else
2852 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002853 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002854 else if( *flags != 0 )
2855 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 pathlen++;
2858
Paul Bakker76fd75a2011-01-16 21:12:10 +00002859 crt = parent;
2860 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 }
2862
2863 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002864 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002866 *flags |= BADCERT_NOT_TRUSTED;
2867
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002868 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 {
2870 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2871 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2872 crt->issuer_raw.len ) != 0 )
2873 {
2874 trust_ca = trust_ca->next;
2875 continue;
2876 }
2877
2878 if( trust_ca->max_pathlen > 0 &&
2879 trust_ca->max_pathlen < pathlen )
2880 break;
2881
Paul Bakker27d66162010-03-17 06:56:01 +00002882 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
2884 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2885
2886 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2887 0, hash, crt->sig.p ) == 0 )
2888 {
2889 /*
2890 * cert. is signed by a trusted CA
2891 */
2892 *flags &= ~BADCERT_NOT_TRUSTED;
2893 break;
2894 }
2895
2896 trust_ca = trust_ca->next;
2897 }
2898
Paul Bakker76fd75a2011-01-16 21:12:10 +00002899 /* Check trusted CA's CRL for the given crt */
2900 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002901
2902 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002903 if( NULL != f_vrfy )
2904 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002905 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002906 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002907 else
2908 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002909 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002910 else if( *flags != 0 )
2911 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002912
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 return( 0 );
2914}
2915
2916/*
2917 * Unallocate all certificate data
2918 */
2919void x509_free( x509_cert *crt )
2920{
2921 x509_cert *cert_cur = crt;
2922 x509_cert *cert_prv;
2923 x509_name *name_cur;
2924 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00002925 x509_sequence *seq_cur;
2926 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
2928 if( crt == NULL )
2929 return;
2930
2931 do
2932 {
2933 rsa_free( &cert_cur->rsa );
2934
2935 name_cur = cert_cur->issuer.next;
2936 while( name_cur != NULL )
2937 {
2938 name_prv = name_cur;
2939 name_cur = name_cur->next;
2940 memset( name_prv, 0, sizeof( x509_name ) );
2941 free( name_prv );
2942 }
2943
2944 name_cur = cert_cur->subject.next;
2945 while( name_cur != NULL )
2946 {
2947 name_prv = name_cur;
2948 name_cur = name_cur->next;
2949 memset( name_prv, 0, sizeof( x509_name ) );
2950 free( name_prv );
2951 }
2952
Paul Bakker74111d32011-01-15 16:57:55 +00002953 seq_cur = cert_cur->ext_key_usage.next;
2954 while( seq_cur != NULL )
2955 {
2956 seq_prv = seq_cur;
2957 seq_cur = seq_cur->next;
2958 memset( seq_prv, 0, sizeof( x509_sequence ) );
2959 free( seq_prv );
2960 }
2961
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 if( cert_cur->raw.p != NULL )
2963 {
2964 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2965 free( cert_cur->raw.p );
2966 }
2967
2968 cert_cur = cert_cur->next;
2969 }
2970 while( cert_cur != NULL );
2971
2972 cert_cur = crt;
2973 do
2974 {
2975 cert_prv = cert_cur;
2976 cert_cur = cert_cur->next;
2977
2978 memset( cert_prv, 0, sizeof( x509_cert ) );
2979 if( cert_prv != crt )
2980 free( cert_prv );
2981 }
2982 while( cert_cur != NULL );
2983}
2984
Paul Bakkerd98030e2009-05-02 15:13:40 +00002985/*
2986 * Unallocate all CRL data
2987 */
2988void x509_crl_free( x509_crl *crl )
2989{
2990 x509_crl *crl_cur = crl;
2991 x509_crl *crl_prv;
2992 x509_name *name_cur;
2993 x509_name *name_prv;
2994 x509_crl_entry *entry_cur;
2995 x509_crl_entry *entry_prv;
2996
2997 if( crl == NULL )
2998 return;
2999
3000 do
3001 {
3002 name_cur = crl_cur->issuer.next;
3003 while( name_cur != NULL )
3004 {
3005 name_prv = name_cur;
3006 name_cur = name_cur->next;
3007 memset( name_prv, 0, sizeof( x509_name ) );
3008 free( name_prv );
3009 }
3010
3011 entry_cur = crl_cur->entry.next;
3012 while( entry_cur != NULL )
3013 {
3014 entry_prv = entry_cur;
3015 entry_cur = entry_cur->next;
3016 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3017 free( entry_prv );
3018 }
3019
3020 if( crl_cur->raw.p != NULL )
3021 {
3022 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3023 free( crl_cur->raw.p );
3024 }
3025
3026 crl_cur = crl_cur->next;
3027 }
3028 while( crl_cur != NULL );
3029
3030 crl_cur = crl;
3031 do
3032 {
3033 crl_prv = crl_cur;
3034 crl_cur = crl_cur->next;
3035
3036 memset( crl_prv, 0, sizeof( x509_crl ) );
3037 if( crl_prv != crl )
3038 free( crl_prv );
3039 }
3040 while( crl_cur != NULL );
3041}
3042
Paul Bakker40e46942009-01-03 21:51:57 +00003043#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Paul Bakker40e46942009-01-03 21:51:57 +00003045#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
3047/*
3048 * Checkup routine
3049 */
3050int x509_self_test( int verbose )
3051{
Paul Bakker5690efc2011-05-26 13:16:06 +00003052#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003053 int ret;
3054 int flags;
3055 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 x509_cert cacert;
3057 x509_cert clicert;
3058 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003059#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003060 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003061#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
3063 if( verbose != 0 )
3064 printf( " X.509 certificate load: " );
3065
3066 memset( &clicert, 0, sizeof( x509_cert ) );
3067
3068 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3069 strlen( test_cli_crt ) );
3070 if( ret != 0 )
3071 {
3072 if( verbose != 0 )
3073 printf( "failed\n" );
3074
3075 return( ret );
3076 }
3077
3078 memset( &cacert, 0, sizeof( x509_cert ) );
3079
3080 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3081 strlen( test_ca_crt ) );
3082 if( ret != 0 )
3083 {
3084 if( verbose != 0 )
3085 printf( "failed\n" );
3086
3087 return( ret );
3088 }
3089
3090 if( verbose != 0 )
3091 printf( "passed\n X.509 private key load: " );
3092
3093 i = strlen( test_ca_key );
3094 j = strlen( test_ca_pwd );
3095
Paul Bakker66b78b22011-03-25 14:22:50 +00003096 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3097
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 if( ( ret = x509parse_key( &rsa,
3099 (unsigned char *) test_ca_key, i,
3100 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3101 {
3102 if( verbose != 0 )
3103 printf( "failed\n" );
3104
3105 return( ret );
3106 }
3107
3108 if( verbose != 0 )
3109 printf( "passed\n X.509 signature verify: ");
3110
Paul Bakker23986e52011-04-24 08:57:21 +00003111 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 if( ret != 0 )
3113 {
Paul Bakker23986e52011-04-24 08:57:21 +00003114 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 if( verbose != 0 )
3116 printf( "failed\n" );
3117
3118 return( ret );
3119 }
3120
Paul Bakker5690efc2011-05-26 13:16:06 +00003121#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003123 printf( "passed\n X.509 DHM parameter load: " );
3124
3125 i = strlen( test_dhm_params );
3126 j = strlen( test_ca_pwd );
3127
3128 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3129 {
3130 if( verbose != 0 )
3131 printf( "failed\n" );
3132
3133 return( ret );
3134 }
3135
3136 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003137 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
3140 x509_free( &cacert );
3141 x509_free( &clicert );
3142 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003143#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003144 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003145#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
3147 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003148#else
3149 ((void) verbose);
3150 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3151#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003152}
3153
3154#endif
3155
3156#endif