blob: bfc4b586a22e94cf24a4665fdc802fb727d7df65 [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/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
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 Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010046#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010048#endif
49#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010051#endif
52#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010054#endif
55#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010057#endif
58#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000059#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010060#endif
61#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000062#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010063#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000064#include "polarssl/dhm.h"
Paul Bakkerf1f21fe2013-06-24 19:17:19 +020065#include "polarssl/pkcs12.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67#include <string.h>
68#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000069#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000070#include <windows.h>
71#else
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Paul Bakker335db3f2011-04-25 15:28:35 +000075#if defined(POLARSSL_FS_IO)
76#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000077#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000078#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020079#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000080#include <dirent.h>
81#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000082#endif
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084/*
Paul Bakker5121ce52009-01-03 21:22:43 +000085 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
86 */
87static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000088 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000089 int *ver )
90{
Paul Bakker23986e52011-04-24 08:57:21 +000091 int ret;
92 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 if( ( ret = asn1_get_tag( p, end, &len,
95 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
96 {
Paul Bakker40e46942009-01-03 21:51:57 +000097 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000098 {
99 *ver = 0;
100 return( 0 );
101 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103 return( ret );
104 }
105
106 end = *p + len;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000109 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000112 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000113 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 return( 0 );
116}
117
118/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000119 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000120 */
121static int x509_crl_get_version( unsigned char **p,
122 const unsigned char *end,
123 int *ver )
124{
125 int ret;
126
127 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
128 {
129 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000130 {
131 *ver = 0;
132 return( 0 );
133 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000134
135 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
136 }
137
138 return( 0 );
139}
140
141/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 * CertificateSerialNumber ::= INTEGER
143 */
144static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000145 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 x509_buf *serial )
147{
148 int ret;
149
150 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000151 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000152 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
155 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000156 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000157 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 serial->tag = *(*p)++;
160
161 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000162 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 serial->p = *p;
165 *p += serial->len;
166
167 return( 0 );
168}
169
170/*
171 * AlgorithmIdentifier ::= SEQUENCE {
172 * algorithm OBJECT IDENTIFIER,
173 * parameters ANY DEFINED BY algorithm OPTIONAL }
174 */
175static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000176 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 x509_buf *alg )
178{
Paul Bakker23986e52011-04-24 08:57:21 +0000179 int ret;
180 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182 if( ( ret = asn1_get_tag( p, end, &len,
183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000184 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 end = *p + len;
187 alg->tag = **p;
188
189 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000190 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 alg->p = *p;
193 *p += alg->len;
194
195 if( *p == end )
196 return( 0 );
197
198 /*
199 * assume the algorithm parameters must be NULL
200 */
201 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000202 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000205 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000206 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 return( 0 );
209}
210
211/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 * AttributeTypeAndValue ::= SEQUENCE {
213 * type AttributeType,
214 * value AttributeValue }
215 *
216 * AttributeType ::= OBJECT IDENTIFIER
217 *
218 * AttributeValue ::= ANY DEFINED BY AttributeType
219 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000220static int x509_get_attr_type_value( unsigned char **p,
221 const unsigned char *end,
222 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223{
Paul Bakker23986e52011-04-24 08:57:21 +0000224 int ret;
225 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 x509_buf *oid;
227 x509_buf *val;
228
229 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000231 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 oid = &cur->oid;
234 oid->tag = **p;
235
236 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000237 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 oid->p = *p;
240 *p += oid->len;
241
242 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000243 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000244 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
246 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
247 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
248 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000250 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 val = &cur->val;
253 val->tag = *(*p)++;
254
255 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000256 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258 val->p = *p;
259 *p += val->len;
260
261 cur->next = NULL;
262
Paul Bakker400ff6f2011-02-20 10:40:16 +0000263 return( 0 );
264}
265
266/*
267 * RelativeDistinguishedName ::=
268 * SET OF AttributeTypeAndValue
269 *
270 * AttributeTypeAndValue ::= SEQUENCE {
271 * type AttributeType,
272 * value AttributeValue }
273 *
274 * AttributeType ::= OBJECT IDENTIFIER
275 *
276 * AttributeValue ::= ANY DEFINED BY AttributeType
277 */
278static int x509_get_name( unsigned char **p,
279 const unsigned char *end,
280 x509_name *cur )
281{
Paul Bakker23986e52011-04-24 08:57:21 +0000282 int ret;
283 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000284 const unsigned char *end2;
285 x509_name *use;
286
287 if( ( ret = asn1_get_tag( p, end, &len,
288 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000289 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000290
291 end2 = end;
292 end = *p + len;
293 use = cur;
294
295 do
296 {
297 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
298 return( ret );
299
300 if( *p != end )
301 {
302 use->next = (x509_name *) malloc(
303 sizeof( x509_name ) );
304
305 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000306 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000307
308 memset( use->next, 0, sizeof( x509_name ) );
309
310 use = use->next;
311 }
312 }
313 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 /*
316 * recurse until end of SEQUENCE is reached
317 */
318 if( *p == end2 )
319 return( 0 );
320
321 cur->next = (x509_name *) malloc(
322 sizeof( x509_name ) );
323
324 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000325 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Paul Bakker430ffbe2012-05-01 08:14:20 +0000327 memset( cur->next, 0, sizeof( x509_name ) );
328
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 return( x509_get_name( p, end2, cur->next ) );
330}
331
332/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 * Time ::= CHOICE {
334 * utcTime UTCTime,
335 * generalTime GeneralizedTime }
336 */
Paul Bakker91200182010-02-18 21:26:15 +0000337static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000339 x509_time *time )
340{
Paul Bakker23986e52011-04-24 08:57:21 +0000341 int ret;
342 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000344 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000345
Paul Bakker91200182010-02-18 21:26:15 +0000346 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000347 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
348 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker91200182010-02-18 21:26:15 +0000350 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000351
Paul Bakker91200182010-02-18 21:26:15 +0000352 if ( tag == ASN1_UTC_TIME )
353 {
354 (*p)++;
355 ret = asn1_get_len( p, end, &len );
356
357 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000358 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000359
Paul Bakker91200182010-02-18 21:26:15 +0000360 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000361 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
362 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000363
Paul Bakker91200182010-02-18 21:26:15 +0000364 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
365 &time->year, &time->mon, &time->day,
366 &time->hour, &time->min, &time->sec ) < 5 )
367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368
Paul Bakker400ff6f2011-02-20 10:40:16 +0000369 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000370 time->year += 1900;
371
372 *p += len;
373
374 return( 0 );
375 }
376 else if ( tag == ASN1_GENERALIZED_TIME )
377 {
378 (*p)++;
379 ret = asn1_get_len( p, end, &len );
380
381 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000382 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000383
384 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000385 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
386 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000387
388 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
389 &time->year, &time->mon, &time->day,
390 &time->hour, &time->min, &time->sec ) < 5 )
391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
392
393 *p += len;
394
395 return( 0 );
396 }
397 else
Paul Bakker9d781402011-05-09 16:17:09 +0000398 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000399}
400
401
402/*
403 * Validity ::= SEQUENCE {
404 * notBefore Time,
405 * notAfter Time }
406 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000407static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000408 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 x509_time *from,
410 x509_time *to )
411{
Paul Bakker23986e52011-04-24 08:57:21 +0000412 int ret;
413 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 if( ( ret = asn1_get_tag( p, end, &len,
416 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000417 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 end = *p + len;
420
Paul Bakker91200182010-02-18 21:26:15 +0000421 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
Paul Bakker91200182010-02-18 21:26:15 +0000424 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000425 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000428 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000429 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 return( 0 );
432}
433
434/*
435 * SubjectPublicKeyInfo ::= SEQUENCE {
436 * algorithm AlgorithmIdentifier,
437 * subjectPublicKey BIT STRING }
438 */
439static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000440 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 x509_buf *pk_alg_oid,
442 mpi *N, mpi *E )
443{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200444 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000445 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200447 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
450 return( ret );
451
452 /*
453 * only RSA public keys handled at this time
454 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200455 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000456 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000462 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000463 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 end2 = *p + len;
466
467 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 /*
471 * RSAPublicKey ::= SEQUENCE {
472 * modulus INTEGER, -- n
473 * publicExponent INTEGER -- e
474 * }
475 */
476 if( ( ret = asn1_get_tag( p, end2, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000481 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000482 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
485 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000486 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
495static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 x509_buf *sig )
498{
Paul Bakker23986e52011-04-24 08:57:21 +0000499 int ret;
500 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Paul Bakker8afa70d2012-02-11 18:42:45 +0000502 if( ( end - *p ) < 1 )
503 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
504 POLARSSL_ERR_ASN1_OUT_OF_DATA );
505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 sig->tag = **p;
507
508 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Paul Bakker74111d32011-01-15 16:57:55 +0000511
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000513 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 sig->len = len;
516 sig->p = *p;
517
518 *p += len;
519
520 return( 0 );
521}
522
523/*
524 * X.509 v2/v3 unique identifier (not parsed)
525 */
526static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 x509_buf *uid, int n )
529{
530 int ret;
531
532 if( *p == end )
533 return( 0 );
534
535 uid->tag = **p;
536
537 if( ( ret = asn1_get_tag( p, end, &uid->len,
538 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
539 {
Paul Bakker40e46942009-01-03 21:51:57 +0000540 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 return( 0 );
542
543 return( ret );
544 }
545
546 uid->p = *p;
547 *p += uid->len;
548
549 return( 0 );
550}
551
552/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000553 * X.509 Extensions (No parsing of extensions, pointer should
554 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 */
556static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000557 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000558 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000559{
Paul Bakker23986e52011-04-24 08:57:21 +0000560 int ret;
561 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 if( *p == end )
564 return( 0 );
565
566 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000569 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 ext->p = *p;
573 end = *p + ext->len;
574
575 /*
576 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
577 *
578 * Extension ::= SEQUENCE {
579 * extnID OBJECT IDENTIFIER,
580 * critical BOOLEAN DEFAULT FALSE,
581 * extnValue OCTET STRING }
582 */
583 if( ( ret = asn1_get_tag( p, end, &len,
584 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000585 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000588 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000589 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Paul Bakkerd98030e2009-05-02 15:13:40 +0000591 return( 0 );
592}
593
594/*
595 * X.509 CRL v2 extensions (no extensions parsed yet.)
596 */
597static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598 const unsigned char *end,
599 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000600{
Paul Bakker23986e52011-04-24 08:57:21 +0000601 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000602 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000603
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000604 /* Get explicit tag */
605 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000606 {
607 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
608 return( 0 );
609
610 return( ret );
611 }
612
613 while( *p < end )
614 {
615 if( ( ret = asn1_get_tag( p, end, &len,
616 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000617 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000618
619 *p += len;
620 }
621
622 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000624 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
625
626 return( 0 );
627}
628
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000629/*
630 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
631 */
632static int x509_get_crl_entry_ext( unsigned char **p,
633 const unsigned char *end,
634 x509_buf *ext )
635{
636 int ret;
637 size_t len = 0;
638
639 /* OPTIONAL */
640 if (end <= *p)
641 return( 0 );
642
643 ext->tag = **p;
644 ext->p = *p;
645
646 /*
647 * Get CRL-entry extension sequence header
648 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
649 */
650 if( ( ret = asn1_get_tag( p, end, &ext->len,
651 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
652 {
653 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
654 {
655 ext->p = NULL;
656 return( 0 );
657 }
658 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
659 }
660
661 end = *p + ext->len;
662
663 if( end != *p + ext->len )
664 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
665 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
666
667 while( *p < end )
668 {
669 if( ( ret = asn1_get_tag( p, end, &len,
670 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
671 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
672
673 *p += len;
674 }
675
676 if( *p != end )
677 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
678 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
679
680 return( 0 );
681}
682
Paul Bakker74111d32011-01-15 16:57:55 +0000683static int x509_get_basic_constraints( unsigned char **p,
684 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000685 int *ca_istrue,
686 int *max_pathlen )
687{
Paul Bakker23986e52011-04-24 08:57:21 +0000688 int ret;
689 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000690
691 /*
692 * BasicConstraints ::= SEQUENCE {
693 * cA BOOLEAN DEFAULT FALSE,
694 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
695 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000696 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000697 *max_pathlen = 0; /* endless */
698
699 if( ( ret = asn1_get_tag( p, end, &len,
700 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000701 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000702
703 if( *p == end )
704 return 0;
705
Paul Bakker3cccddb2011-01-16 21:46:31 +0000706 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000707 {
708 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000709 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000710
711 if( ret != 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
Paul Bakker3cccddb2011-01-16 21:46:31 +0000714 if( *ca_istrue != 0 )
715 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000716 }
717
718 if( *p == end )
719 return 0;
720
721 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000722 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000723
724 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000725 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000726 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
727
728 (*max_pathlen)++;
729
Paul Bakker74111d32011-01-15 16:57:55 +0000730 return 0;
731}
732
733static int x509_get_ns_cert_type( unsigned char **p,
734 const unsigned char *end,
735 unsigned char *ns_cert_type)
736{
737 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000738 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000739
740 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000741 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000742
743 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000744 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000745 POLARSSL_ERR_ASN1_INVALID_LENGTH );
746
747 /* Get actual bitstring */
748 *ns_cert_type = *bs.p;
749 return 0;
750}
751
752static int x509_get_key_usage( unsigned char **p,
753 const unsigned char *end,
754 unsigned char *key_usage)
755{
756 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000757 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000758
759 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000760 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000761
Paul Bakker94a67962012-08-23 13:03:52 +0000762 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000763 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000764 POLARSSL_ERR_ASN1_INVALID_LENGTH );
765
766 /* Get actual bitstring */
767 *key_usage = *bs.p;
768 return 0;
769}
770
Paul Bakkerd98030e2009-05-02 15:13:40 +0000771/*
Paul Bakker74111d32011-01-15 16:57:55 +0000772 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
773 *
774 * KeyPurposeId ::= OBJECT IDENTIFIER
775 */
776static int x509_get_ext_key_usage( unsigned char **p,
777 const unsigned char *end,
778 x509_sequence *ext_key_usage)
779{
780 int ret;
781
782 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000783 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000784
785 /* Sequence length must be >= 1 */
786 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000788 POLARSSL_ERR_ASN1_INVALID_LENGTH );
789
790 return 0;
791}
792
793/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000794 * SubjectAltName ::= GeneralNames
795 *
796 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
797 *
798 * GeneralName ::= CHOICE {
799 * otherName [0] OtherName,
800 * rfc822Name [1] IA5String,
801 * dNSName [2] IA5String,
802 * x400Address [3] ORAddress,
803 * directoryName [4] Name,
804 * ediPartyName [5] EDIPartyName,
805 * uniformResourceIdentifier [6] IA5String,
806 * iPAddress [7] OCTET STRING,
807 * registeredID [8] OBJECT IDENTIFIER }
808 *
809 * OtherName ::= SEQUENCE {
810 * type-id OBJECT IDENTIFIER,
811 * value [0] EXPLICIT ANY DEFINED BY type-id }
812 *
813 * EDIPartyName ::= SEQUENCE {
814 * nameAssigner [0] DirectoryString OPTIONAL,
815 * partyName [1] DirectoryString }
816 *
817 * NOTE: PolarSSL only parses and uses dNSName at this point.
818 */
819static int x509_get_subject_alt_name( unsigned char **p,
820 const unsigned char *end,
821 x509_sequence *subject_alt_name )
822{
823 int ret;
824 size_t len, tag_len;
825 asn1_buf *buf;
826 unsigned char tag;
827 asn1_sequence *cur = subject_alt_name;
828
829 /* Get main sequence tag */
830 if( ( ret = asn1_get_tag( p, end, &len,
831 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
832 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
833
834 if( *p + len != end )
835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
836 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
837
838 while( *p < end )
839 {
840 if( ( end - *p ) < 1 )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_OUT_OF_DATA );
843
844 tag = **p;
845 (*p)++;
846 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
848
849 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
850 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
851 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
852
853 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
854 {
855 *p += tag_len;
856 continue;
857 }
858
859 buf = &(cur->buf);
860 buf->tag = tag;
861 buf->p = *p;
862 buf->len = tag_len;
863 *p += buf->len;
864
865 /* Allocate and assign next pointer */
866 if (*p < end)
867 {
868 cur->next = (asn1_sequence *) malloc(
869 sizeof( asn1_sequence ) );
870
871 if( cur->next == NULL )
872 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
873 POLARSSL_ERR_ASN1_MALLOC_FAILED );
874
Paul Bakker535e97d2012-08-23 10:49:55 +0000875 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000876 cur = cur->next;
877 }
878 }
879
880 /* Set final sequence entry's next pointer to NULL */
881 cur->next = NULL;
882
883 if( *p != end )
884 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
885 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
886
887 return( 0 );
888}
889
890/*
Paul Bakker74111d32011-01-15 16:57:55 +0000891 * X.509 v3 extensions
892 *
893 * TODO: Perform all of the basic constraints tests required by the RFC
894 * TODO: Set values for undetected extensions to a sane default?
895 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000896 */
897static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000898 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000899 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000900{
Paul Bakker23986e52011-04-24 08:57:21 +0000901 int ret;
902 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000903 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000904
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000905 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000906 {
907 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
908 return( 0 );
909
910 return( ret );
911 }
912
Paul Bakker5121ce52009-01-03 21:22:43 +0000913 while( *p < end )
914 {
Paul Bakker74111d32011-01-15 16:57:55 +0000915 /*
916 * Extension ::= SEQUENCE {
917 * extnID OBJECT IDENTIFIER,
918 * critical BOOLEAN DEFAULT FALSE,
919 * extnValue OCTET STRING }
920 */
921 x509_buf extn_oid = {0, 0, NULL};
922 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200923 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000924
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 if( ( ret = asn1_get_tag( p, end, &len,
926 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000927 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000929 end_ext_data = *p + len;
930
Paul Bakker74111d32011-01-15 16:57:55 +0000931 /* Get extension ID */
932 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Bakker74111d32011-01-15 16:57:55 +0000934 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000935 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Paul Bakker74111d32011-01-15 16:57:55 +0000937 extn_oid.p = *p;
938 *p += extn_oid.len;
939
940 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000941 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000942 POLARSSL_ERR_ASN1_OUT_OF_DATA );
943
944 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000945 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000946 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000947 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Paul Bakker74111d32011-01-15 16:57:55 +0000949 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000954 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000955
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000956 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000958 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker74111d32011-01-15 16:57:55 +0000960 /*
961 * Detect supported extensions
962 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200963 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
964
965 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000966 {
967 /* No parser found, skip extension */
968 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000969
Paul Bakker5c721f92011-07-27 16:51:09 +0000970#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000971 if( is_critical )
972 {
973 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000974 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000975 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
976 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000977#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200978 continue;
979 }
980
981 crt->ext_types |= ext_type;
982
983 switch( ext_type )
984 {
985 case EXT_BASIC_CONSTRAINTS:
986 /* Parse basic constraints */
987 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
988 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
989 return ( ret );
990 break;
991
992 case EXT_KEY_USAGE:
993 /* Parse key usage */
994 if( ( ret = x509_get_key_usage( p, end_ext_octet,
995 &crt->key_usage ) ) != 0 )
996 return ( ret );
997 break;
998
999 case EXT_EXTENDED_KEY_USAGE:
1000 /* Parse extended key usage */
1001 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1002 &crt->ext_key_usage ) ) != 0 )
1003 return ( ret );
1004 break;
1005
1006 case EXT_SUBJECT_ALT_NAME:
1007 /* Parse subject alt name */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
1010 return ( ret );
1011 break;
1012
1013 case EXT_NS_CERT_TYPE:
1014 /* Parse netscape certificate type */
1015 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1016 &crt->ns_cert_type ) ) != 0 )
1017 return ( ret );
1018 break;
1019
1020 default:
1021 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001022 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 }
1024
1025 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001026 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001027 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 return( 0 );
1030}
1031
1032/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001033 * X.509 CRL Entries
1034 */
1035static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001036 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001037 x509_crl_entry *entry )
1038{
Paul Bakker23986e52011-04-24 08:57:21 +00001039 int ret;
1040 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001041 x509_crl_entry *cur_entry = entry;
1042
1043 if( *p == end )
1044 return( 0 );
1045
Paul Bakker9be19372009-07-27 20:21:53 +00001046 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001047 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1048 {
1049 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1050 return( 0 );
1051
1052 return( ret );
1053 }
1054
Paul Bakker9be19372009-07-27 20:21:53 +00001055 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001056
1057 while( *p < end )
1058 {
Paul Bakker23986e52011-04-24 08:57:21 +00001059 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001060 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001061
1062 if( ( ret = asn1_get_tag( p, end, &len2,
1063 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1064 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065 return( ret );
1066 }
1067
Paul Bakker9be19372009-07-27 20:21:53 +00001068 cur_entry->raw.tag = **p;
1069 cur_entry->raw.p = *p;
1070 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001071 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001072
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001073 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001074 return( ret );
1075
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001076 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001077 return( ret );
1078
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001079 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001080 return( ret );
1081
Paul Bakker74111d32011-01-15 16:57:55 +00001082 if ( *p < end )
1083 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001085
1086 if( cur_entry->next == NULL )
1087 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1088
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 cur_entry = cur_entry->next;
1090 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1091 }
1092 }
1093
1094 return( 0 );
1095}
1096
Paul Bakkerc70b9822013-04-07 22:00:46 +02001097static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1098 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001099{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001100 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001101
Paul Bakkerc70b9822013-04-07 22:00:46 +02001102 if( ret != 0 )
1103 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001104
Paul Bakkerc70b9822013-04-07 22:00:46 +02001105 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001106}
1107
Paul Bakkerd98030e2009-05-02 15:13:40 +00001108/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001109 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 */
Paul Bakker42c65812013-06-24 19:21:59 +02001111int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1112 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113{
Paul Bakker23986e52011-04-24 08:57:21 +00001114 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001115 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001116 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker320a4b52009-03-28 18:52:39 +00001118 /*
1119 * Check for valid input
1120 */
1121 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001122 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001123
Paul Bakker96743fc2011-02-12 14:30:57 +00001124 p = (unsigned char *) malloc( len = buflen );
1125
1126 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001127 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001128
1129 memcpy( p, buf, buflen );
1130
1131 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133 crt->raw.p = p;
1134 crt->raw.len = len;
1135 end = p + len;
1136
1137 /*
1138 * Certificate ::= SEQUENCE {
1139 * tbsCertificate TBSCertificate,
1140 * signatureAlgorithm AlgorithmIdentifier,
1141 * signatureValue BIT STRING }
1142 */
1143 if( ( ret = asn1_get_tag( &p, end, &len,
1144 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1145 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001146 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001147 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 }
1149
Paul Bakkerb00ca422012-09-25 12:10:00 +00001150 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001152 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001153 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001154 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001156 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001157
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 /*
1159 * TBSCertificate ::= SEQUENCE {
1160 */
1161 crt->tbs.p = p;
1162
1163 if( ( ret = asn1_get_tag( &p, end, &len,
1164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1165 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001166 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001167 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 }
1169
1170 end = p + len;
1171 crt->tbs.len = end - crt->tbs.p;
1172
1173 /*
1174 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1175 *
1176 * CertificateSerialNumber ::= INTEGER
1177 *
1178 * signature AlgorithmIdentifier
1179 */
1180 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1181 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1182 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1183 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001184 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 return( ret );
1186 }
1187
1188 crt->version++;
1189
1190 if( crt->version > 3 )
1191 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001192 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001193 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 }
1195
Paul Bakkerc70b9822013-04-07 22:00:46 +02001196 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1197 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001199 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001200 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 }
1202
1203 /*
1204 * issuer Name
1205 */
1206 crt->issuer_raw.p = p;
1207
1208 if( ( ret = asn1_get_tag( &p, end, &len,
1209 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001212 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
1215 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 return( ret );
1219 }
1220
1221 crt->issuer_raw.len = p - crt->issuer_raw.p;
1222
1223 /*
1224 * Validity ::= SEQUENCE {
1225 * notBefore Time,
1226 * notAfter Time }
1227 *
1228 */
1229 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1230 &crt->valid_to ) ) != 0 )
1231 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001232 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 return( ret );
1234 }
1235
1236 /*
1237 * subject Name
1238 */
1239 crt->subject_raw.p = p;
1240
1241 if( ( ret = asn1_get_tag( &p, end, &len,
1242 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1243 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001244 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001245 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 }
1247
Paul Bakkercefb3962012-06-27 11:51:09 +00001248 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 return( ret );
1252 }
1253
1254 crt->subject_raw.len = p - crt->subject_raw.p;
1255
1256 /*
1257 * SubjectPublicKeyInfo ::= SEQUENCE
1258 * algorithm AlgorithmIdentifier,
1259 * subjectPublicKey BIT STRING }
1260 */
1261 if( ( ret = asn1_get_tag( &p, end, &len,
1262 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001265 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
1267
1268 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1269 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 return( ret );
1273 }
1274
1275 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1276 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001277 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 return( ret );
1279 }
1280
1281 crt->rsa.len = mpi_size( &crt->rsa.N );
1282
1283 /*
1284 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1285 * -- If present, version shall be v2 or v3
1286 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1287 * -- If present, version shall be v2 or v3
1288 * extensions [3] EXPLICIT Extensions OPTIONAL
1289 * -- If present, version shall be v3
1290 */
1291 if( crt->version == 2 || crt->version == 3 )
1292 {
1293 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1294 if( ret != 0 )
1295 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 return( ret );
1298 }
1299 }
1300
1301 if( crt->version == 2 || crt->version == 3 )
1302 {
1303 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1304 if( ret != 0 )
1305 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001306 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 return( ret );
1308 }
1309 }
1310
1311 if( crt->version == 3 )
1312 {
Paul Bakker74111d32011-01-15 16:57:55 +00001313 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 if( ret != 0 )
1315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319 }
1320
1321 if( p != end )
1322 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001323 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001324 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001325 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
Paul Bakkerb00ca422012-09-25 12:10:00 +00001328 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 /*
1331 * signatureAlgorithm AlgorithmIdentifier,
1332 * signatureValue BIT STRING
1333 */
1334 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339
Paul Bakker535e97d2012-08-23 10:49:55 +00001340 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1341 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001344 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
1347 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1348 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001349 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 return( ret );
1351 }
1352
1353 if( p != end )
1354 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001355 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001356 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001357 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
1359
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001360 return( 0 );
1361}
1362
1363/*
Paul Bakker42c65812013-06-24 19:21:59 +02001364 * Parse one X.509 certificate in DER format from a buffer and add them to a
1365 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001366 */
Paul Bakker42c65812013-06-24 19:21:59 +02001367int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001368{
Paul Bakker42c65812013-06-24 19:21:59 +02001369 int ret;
1370 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001371
1372 /*
1373 * Check for valid input
1374 */
1375 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001376 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001377
1378 while( crt->version != 0 && crt->next != NULL )
1379 {
1380 prev = crt;
1381 crt = crt->next;
1382 }
1383
1384 /*
1385 * Add new certificate on the end of the chain if needed.
1386 */
1387 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001388 {
1389 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1390
Paul Bakker7d06ad22009-05-02 15:53:56 +00001391 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001392 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001393
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001394 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 crt = crt->next;
1396 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Paul Bakker42c65812013-06-24 19:21:59 +02001399 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1400 {
1401 if( prev )
1402 prev->next = NULL;
1403
1404 if( crt != chain )
1405 free( crt );
1406
1407 return( ret );
1408 }
1409
1410 return( 0 );
1411}
1412
1413/*
1414 * Parse one or more PEM certificates from a buffer and add them to the chained list
1415 */
1416int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1417{
1418 int ret, success = 0, first_error = 0, total_failed = 0;
1419 int buf_format = X509_FORMAT_DER;
1420
1421 /*
1422 * Check for valid input
1423 */
1424 if( chain == NULL || buf == NULL )
1425 return( POLARSSL_ERR_X509_INVALID_INPUT );
1426
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427 /*
1428 * Determine buffer content. Buffer contains either one DER certificate or
1429 * one or more PEM certificates.
1430 */
1431#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001432 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001433 buf_format = X509_FORMAT_PEM;
1434#endif
1435
1436 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001437 return x509parse_crt_der( chain, buf, buflen );
1438
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001439#if defined(POLARSSL_PEM_C)
1440 if( buf_format == X509_FORMAT_PEM )
1441 {
1442 pem_context pem;
1443
1444 while( buflen > 0 )
1445 {
1446 size_t use_len;
1447 pem_init( &pem );
1448
1449 ret = pem_read_buffer( &pem,
1450 "-----BEGIN CERTIFICATE-----",
1451 "-----END CERTIFICATE-----",
1452 buf, NULL, 0, &use_len );
1453
1454 if( ret == 0 )
1455 {
1456 /*
1457 * Was PEM encoded
1458 */
1459 buflen -= use_len;
1460 buf += use_len;
1461 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001462 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1463 {
1464 return( ret );
1465 }
Paul Bakker00b28602013-06-24 13:02:41 +02001466 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001467 {
1468 pem_free( &pem );
1469
Paul Bakker5ed3b342013-06-24 19:05:46 +02001470 /*
1471 * PEM header and footer were found
1472 */
1473 buflen -= use_len;
1474 buf += use_len;
1475
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001476 if( first_error == 0 )
1477 first_error = ret;
1478
1479 continue;
1480 }
1481 else
1482 break;
1483
Paul Bakker42c65812013-06-24 19:21:59 +02001484 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001485
1486 pem_free( &pem );
1487
1488 if( ret != 0 )
1489 {
1490 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001491 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001493 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001495
1496 if( first_error == 0 )
1497 first_error = ret;
1498
Paul Bakker42c65812013-06-24 19:21:59 +02001499 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 continue;
1501 }
1502
1503 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001504 }
1505 }
1506#endif
1507
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001508 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001509 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001510 else if( first_error )
1511 return( first_error );
1512 else
1513 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
1516/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001517 * Parse one or more CRLs and add them to the chained list
1518 */
Paul Bakker23986e52011-04-24 08:57:21 +00001519int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001520{
Paul Bakker23986e52011-04-24 08:57:21 +00001521 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001522 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001523 unsigned char *p, *end;
1524 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001525#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001526 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001527 pem_context pem;
1528#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001529
1530 crl = chain;
1531
1532 /*
1533 * Check for valid input
1534 */
1535 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001536 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001537
1538 while( crl->version != 0 && crl->next != NULL )
1539 crl = crl->next;
1540
1541 /*
1542 * Add new CRL on the end of the chain if needed.
1543 */
1544 if ( crl->version != 0 && crl->next == NULL)
1545 {
1546 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1547
Paul Bakker7d06ad22009-05-02 15:53:56 +00001548 if( crl->next == NULL )
1549 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001551 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001552 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001553
Paul Bakker7d06ad22009-05-02 15:53:56 +00001554 crl = crl->next;
1555 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001556 }
1557
Paul Bakker96743fc2011-02-12 14:30:57 +00001558#if defined(POLARSSL_PEM_C)
1559 pem_init( &pem );
1560 ret = pem_read_buffer( &pem,
1561 "-----BEGIN X509 CRL-----",
1562 "-----END X509 CRL-----",
1563 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
Paul Bakker96743fc2011-02-12 14:30:57 +00001565 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001567 /*
1568 * Was PEM encoded
1569 */
1570 buflen -= use_len;
1571 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001572
1573 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001574 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001575 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001576 p = pem.buf;
1577 pem.buf = NULL;
1578 len = pem.buflen;
1579 pem_free( &pem );
1580 }
Paul Bakker00b28602013-06-24 13:02:41 +02001581 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001582 {
1583 pem_free( &pem );
1584 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 }
1586 else
1587 {
1588 /*
1589 * nope, copy the raw DER data
1590 */
1591 p = (unsigned char *) malloc( len = buflen );
1592
1593 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001594 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001595
1596 memcpy( p, buf, buflen );
1597
1598 buflen = 0;
1599 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001600#else
1601 p = (unsigned char *) malloc( len = buflen );
1602
1603 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001604 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001605
1606 memcpy( p, buf, buflen );
1607
1608 buflen = 0;
1609#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610
1611 crl->raw.p = p;
1612 crl->raw.len = len;
1613 end = p + len;
1614
1615 /*
1616 * CertificateList ::= SEQUENCE {
1617 * tbsCertList TBSCertList,
1618 * signatureAlgorithm AlgorithmIdentifier,
1619 * signatureValue BIT STRING }
1620 */
1621 if( ( ret = asn1_get_tag( &p, end, &len,
1622 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1623 {
1624 x509_crl_free( crl );
1625 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1626 }
1627
Paul Bakker23986e52011-04-24 08:57:21 +00001628 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 {
1630 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001631 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1633 }
1634
1635 /*
1636 * TBSCertList ::= SEQUENCE {
1637 */
1638 crl->tbs.p = p;
1639
1640 if( ( ret = asn1_get_tag( &p, end, &len,
1641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1642 {
1643 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001644 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645 }
1646
1647 end = p + len;
1648 crl->tbs.len = end - crl->tbs.p;
1649
1650 /*
1651 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1652 * -- if present, MUST be v2
1653 *
1654 * signature AlgorithmIdentifier
1655 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001656 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1658 {
1659 x509_crl_free( crl );
1660 return( ret );
1661 }
1662
1663 crl->version++;
1664
1665 if( crl->version > 2 )
1666 {
1667 x509_crl_free( crl );
1668 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1669 }
1670
Paul Bakkerc70b9822013-04-07 22:00:46 +02001671 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1672 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001673 {
1674 x509_crl_free( crl );
1675 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1676 }
1677
1678 /*
1679 * issuer Name
1680 */
1681 crl->issuer_raw.p = p;
1682
1683 if( ( ret = asn1_get_tag( &p, end, &len,
1684 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1685 {
1686 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001687 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001688 }
1689
1690 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1691 {
1692 x509_crl_free( crl );
1693 return( ret );
1694 }
1695
1696 crl->issuer_raw.len = p - crl->issuer_raw.p;
1697
1698 /*
1699 * thisUpdate Time
1700 * nextUpdate Time OPTIONAL
1701 */
Paul Bakker91200182010-02-18 21:26:15 +00001702 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001703 {
1704 x509_crl_free( crl );
1705 return( ret );
1706 }
1707
Paul Bakker91200182010-02-18 21:26:15 +00001708 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709 {
Paul Bakker9d781402011-05-09 16:17:09 +00001710 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001711 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001712 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001713 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001714 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001715 x509_crl_free( crl );
1716 return( ret );
1717 }
1718 }
1719
1720 /*
1721 * revokedCertificates SEQUENCE OF SEQUENCE {
1722 * userCertificate CertificateSerialNumber,
1723 * revocationDate Time,
1724 * crlEntryExtensions Extensions OPTIONAL
1725 * -- if present, MUST be v2
1726 * } OPTIONAL
1727 */
1728 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1729 {
1730 x509_crl_free( crl );
1731 return( ret );
1732 }
1733
1734 /*
1735 * crlExtensions EXPLICIT Extensions OPTIONAL
1736 * -- if present, MUST be v2
1737 */
1738 if( crl->version == 2 )
1739 {
1740 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1741
1742 if( ret != 0 )
1743 {
1744 x509_crl_free( crl );
1745 return( ret );
1746 }
1747 }
1748
1749 if( p != end )
1750 {
1751 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001752 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001753 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1754 }
1755
1756 end = crl->raw.p + crl->raw.len;
1757
1758 /*
1759 * signatureAlgorithm AlgorithmIdentifier,
1760 * signatureValue BIT STRING
1761 */
1762 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1763 {
1764 x509_crl_free( crl );
1765 return( ret );
1766 }
1767
Paul Bakker535e97d2012-08-23 10:49:55 +00001768 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1769 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001770 {
1771 x509_crl_free( crl );
1772 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1773 }
1774
1775 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1776 {
1777 x509_crl_free( crl );
1778 return( ret );
1779 }
1780
1781 if( p != end )
1782 {
1783 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001784 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001785 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1786 }
1787
1788 if( buflen > 0 )
1789 {
1790 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1791
Paul Bakker7d06ad22009-05-02 15:53:56 +00001792 if( crl->next == NULL )
1793 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001795 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001796 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797
Paul Bakker7d06ad22009-05-02 15:53:56 +00001798 crl = crl->next;
1799 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001800
1801 return( x509parse_crl( crl, buf, buflen ) );
1802 }
1803
1804 return( 0 );
1805}
1806
Paul Bakker335db3f2011-04-25 15:28:35 +00001807#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001808/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001809 * Load all data from a file into a given buffer.
1810 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001811int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001812{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001814
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001816 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001817
Paul Bakkerd98030e2009-05-02 15:13:40 +00001818 fseek( f, 0, SEEK_END );
1819 *n = (size_t) ftell( f );
1820 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001821
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001823 {
1824 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001825 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001826 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001827
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 if( fread( *buf, 1, *n, f ) != *n )
1829 {
1830 fclose( f );
1831 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001832 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001834
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001836
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001840}
1841
1842/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 * Load one or more certificates and add them to the chained list
1844 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001845int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001846{
1847 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 size_t n;
1849 unsigned char *buf;
1850
Paul Bakker69e095c2011-12-10 21:55:01 +00001851 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1852 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Paul Bakker69e095c2011-12-10 21:55:01 +00001854 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
1856 memset( buf, 0, n + 1 );
1857 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
1859 return( ret );
1860}
1861
Paul Bakker8d914582012-06-04 12:46:42 +00001862int x509parse_crtpath( x509_cert *chain, const char *path )
1863{
1864 int ret = 0;
1865#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001866 int w_ret;
1867 WCHAR szDir[MAX_PATH];
1868 char filename[MAX_PATH];
1869 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001870 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001871
Paul Bakker97872ac2012-11-02 12:53:26 +00001872 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001873 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001874
1875 if( len > MAX_PATH - 3 )
1876 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001877
Paul Bakker3338b792012-10-01 21:13:10 +00001878 memset( szDir, 0, sizeof(szDir) );
1879 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001880 memcpy( filename, path, len );
1881 filename[len++] = '\\';
1882 p = filename + len;
1883 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001884
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001885 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001886
Paul Bakker97872ac2012-11-02 12:53:26 +00001887 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001888 if (hFind == INVALID_HANDLE_VALUE)
1889 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1890
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001891 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001892 do
1893 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001894 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001895
Paul Bakkere4791f32012-06-04 21:29:15 +00001896 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001897 continue;
1898
Paul Bakker3338b792012-10-01 21:13:10 +00001899 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1900 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001901 p, len - 1,
1902 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001903
Paul Bakker3338b792012-10-01 21:13:10 +00001904 w_ret = x509parse_crtfile( chain, filename );
1905 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001906 ret++;
1907 else
1908 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001909 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001910 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001911
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001912 if (GetLastError() != ERROR_NO_MORE_FILES)
1913 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001914
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001915cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001916 FindClose( hFind );
1917#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001918 int t_ret, i;
1919 struct stat sb;
1920 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001921 char entry_name[255];
1922 DIR *dir = opendir( path );
1923
1924 if( dir == NULL)
1925 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1926
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001927 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001928 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001929 if( result == NULL )
1930 break;
1931
1932 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1933
1934 i = stat( entry_name, &sb );
1935
1936 if( i == -1 )
1937 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1938
1939 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001940 continue;
1941
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001942 // Ignore parse errors
1943 //
Paul Bakker8d914582012-06-04 12:46:42 +00001944 t_ret = x509parse_crtfile( chain, entry_name );
1945 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001946 ret++;
1947 else
1948 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001949 }
1950 closedir( dir );
1951#endif
1952
1953 return( ret );
1954}
1955
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956/*
1957 * Load one or more CRLs and add them to the chained list
1958 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001959int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001960{
1961 int ret;
1962 size_t n;
1963 unsigned char *buf;
1964
Paul Bakker69e095c2011-12-10 21:55:01 +00001965 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1966 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001967
Paul Bakker27fdf462011-06-09 13:55:13 +00001968 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001969
1970 memset( buf, 0, n + 1 );
1971 free( buf );
1972
1973 return( ret );
1974}
1975
Paul Bakker5121ce52009-01-03 21:22:43 +00001976/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001977 * Load and parse a private RSA key
1978 */
1979int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1980{
1981 int ret;
1982 size_t n;
1983 unsigned char *buf;
1984
Paul Bakker69e095c2011-12-10 21:55:01 +00001985 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1986 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001987
1988 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001989 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001990 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001991 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001992 (unsigned char *) pwd, strlen( pwd ) );
1993
1994 memset( buf, 0, n + 1 );
1995 free( buf );
1996
1997 return( ret );
1998}
1999
2000/*
2001 * Load and parse a public RSA key
2002 */
2003int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2004{
2005 int ret;
2006 size_t n;
2007 unsigned char *buf;
2008
Paul Bakker69e095c2011-12-10 21:55:01 +00002009 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2010 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002011
Paul Bakker27fdf462011-06-09 13:55:13 +00002012 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002013
2014 memset( buf, 0, n + 1 );
2015 free( buf );
2016
2017 return( ret );
2018}
2019#endif /* POLARSSL_FS_IO */
2020
2021/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002022 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002024static int x509parse_key_pkcs1_der( rsa_context *rsa,
2025 const unsigned char *key,
2026 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002027{
Paul Bakker23986e52011-04-24 08:57:21 +00002028 int ret;
2029 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002031
Paul Bakker96743fc2011-02-12 14:30:57 +00002032 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002033 end = p + keylen;
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002036 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002037 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 * RSAPrivateKey ::= SEQUENCE {
2039 * version Version,
2040 * modulus INTEGER, -- n
2041 * publicExponent INTEGER, -- e
2042 * privateExponent INTEGER, -- d
2043 * prime1 INTEGER, -- p
2044 * prime2 INTEGER, -- q
2045 * exponent1 INTEGER, -- d mod (p-1)
2046 * exponent2 INTEGER, -- d mod (q-1)
2047 * coefficient INTEGER, -- (inverse of q) mod p
2048 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2049 * }
2050 */
2051 if( ( ret = asn1_get_tag( &p, end, &len,
2052 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2053 {
Paul Bakker9d781402011-05-09 16:17:09 +00002054 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 }
2056
2057 end = p + len;
2058
2059 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2060 {
Paul Bakker9d781402011-05-09 16:17:09 +00002061 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063
2064 if( rsa->ver != 0 )
2065 {
Paul Bakker9d781402011-05-09 16:17:09 +00002066 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
2068
2069 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2070 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2071 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2072 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2073 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2074 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2075 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2076 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2077 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002079 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 }
2081
2082 rsa->len = mpi_size( &rsa->N );
2083
2084 if( p != end )
2085 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002087 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002088 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
2090
2091 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2092 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 rsa_free( rsa );
2094 return( ret );
2095 }
2096
Paul Bakkere2f50402013-06-24 19:00:59 +02002097 return( 0 );
2098}
2099
2100/*
2101 * Parse an unencrypted PKCS#8 encoded private RSA key
2102 */
2103static int x509parse_key_pkcs8_unencrypted_der(
2104 rsa_context *rsa,
2105 const unsigned char *key,
2106 size_t keylen )
2107{
2108 int ret;
2109 size_t len;
2110 unsigned char *p, *end;
2111 x509_buf pk_alg_oid;
2112 pk_type_t pk_alg = POLARSSL_PK_NONE;
2113
2114 p = (unsigned char *) key;
2115 end = p + keylen;
2116
2117 /*
2118 * This function parses the PrivatKeyInfo object (PKCS#8)
2119 *
2120 * PrivateKeyInfo ::= SEQUENCE {
2121 * version Version,
2122 * algorithm AlgorithmIdentifier,
2123 * PrivateKey BIT STRING
2124 * }
2125 *
2126 * AlgorithmIdentifier ::= SEQUENCE {
2127 * algorithm OBJECT IDENTIFIER,
2128 * parameters ANY DEFINED BY algorithm OPTIONAL
2129 * }
2130 *
2131 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2132 */
2133 if( ( ret = asn1_get_tag( &p, end, &len,
2134 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2135 {
2136 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2137 }
2138
2139 end = p + len;
2140
2141 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2142 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2143
2144 if( rsa->ver != 0 )
2145 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2146
2147 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2148 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2149
2150 /*
2151 * only RSA keys handled at this time
2152 */
2153 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2154 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2155
2156 /*
2157 * Get the OCTET STRING and parse the PKCS#1 format inside
2158 */
2159 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2161
2162 if( ( end - p ) < 1 )
2163 {
2164 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2165 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2166 }
2167
2168 end = p + len;
2169
2170 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2171 return( ret );
2172
2173 return( 0 );
2174}
2175
2176/*
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002177 * Parse an unencrypted PKCS#8 encoded private RSA key
2178 */
2179static int x509parse_key_pkcs8_encrypted_der(
2180 rsa_context *rsa,
2181 const unsigned char *key,
2182 size_t keylen,
2183 const unsigned char *pwd,
2184 size_t pwdlen )
2185{
2186 int ret;
2187 size_t len;
2188 unsigned char *p, *end, *end2;
2189 x509_buf pbe_alg_oid, pbe_params;
2190 unsigned char buf[2048];
2191
2192 memset(buf, 0, 2048);
2193
2194 p = (unsigned char *) key;
2195 end = p + keylen;
2196
2197 /*
2198 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2199 *
2200 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2201 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2202 * encryptedData EncryptedData
2203 * }
2204 *
2205 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2206 *
2207 * EncryptedData ::= OCTET STRING
2208 *
2209 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2210 */
2211 if( ( ret = asn1_get_tag( &p, end, &len,
2212 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2213 {
2214 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2215 }
2216
2217 end = p + len;
2218
2219 if( ( ret = asn1_get_tag( &p, end, &len,
2220 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2221 {
2222 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2223 }
2224
2225 end2 = p + len;
2226
2227 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2228 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2229
2230 pbe_alg_oid.p = p;
2231 p += pbe_alg_oid.len;
2232
2233 /*
2234 * Store the algorithm parameters
2235 */
2236 pbe_params.p = p;
2237 pbe_params.len = end2 - p;
2238 p += pbe_params.len;
2239
2240 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2241 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2242
2243 // buf has been sized to 2048 bytes
2244 if( len > 2048 )
2245 return( POLARSSL_ERR_X509_INVALID_INPUT );
2246
2247 /*
2248 * Decrypt EncryptedData with appropriate PDE
2249 */
2250 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2251 {
2252 if( ( ret = pkcs12_pbe_sha1_des3_ede_cbc( &pbe_params,
2253 PKCS12_PBE_DECRYPT,
2254 pwd, pwdlen,
2255 p, len, buf ) ) != 0 )
2256 {
2257 return( ret );
2258 }
2259 }
2260 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2261 {
2262 if( ( ret = pkcs12_pbe_sha1_des2_ede_cbc( &pbe_params,
2263 PKCS12_PBE_DECRYPT,
2264 pwd, pwdlen,
2265 p, len, buf ) ) != 0 )
2266 {
2267 return( ret );
2268 }
2269 }
2270 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2271 {
2272 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2273 PKCS12_PBE_DECRYPT,
2274 pwd, pwdlen,
2275 p, len, buf ) ) != 0 )
2276 {
2277 return( ret );
2278 }
2279 }
2280 else
2281 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2282
2283 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2284}
2285
2286/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002287 * Parse a private RSA key
2288 */
2289int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2290 const unsigned char *pwd, size_t pwdlen )
2291{
2292 int ret;
2293
Paul Bakker96743fc2011-02-12 14:30:57 +00002294#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002295 size_t len;
2296 pem_context pem;
2297
2298 pem_init( &pem );
2299 ret = pem_read_buffer( &pem,
2300 "-----BEGIN RSA PRIVATE KEY-----",
2301 "-----END RSA PRIVATE KEY-----",
2302 key, pwd, pwdlen, &len );
2303 if( ret == 0 )
2304 {
2305 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2306 {
2307 rsa_free( rsa );
2308 }
2309
2310 pem_free( &pem );
2311 return( ret );
2312 }
2313 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2314 {
2315 pem_free( &pem );
2316 return( ret );
2317 }
2318
2319 ret = pem_read_buffer( &pem,
2320 "-----BEGIN PRIVATE KEY-----",
2321 "-----END PRIVATE KEY-----",
2322 key, NULL, 0, &len );
2323 if( ret == 0 )
2324 {
2325 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2326 pem.buf, pem.buflen ) ) != 0 )
2327 {
2328 rsa_free( rsa );
2329 }
2330
2331 pem_free( &pem );
2332 return( ret );
2333 }
2334 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2335 {
2336 pem_free( &pem );
2337 return( ret );
2338 }
2339
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002340 ret = pem_read_buffer( &pem,
2341 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2342 "-----END ENCRYPTED PRIVATE KEY-----",
2343 key, NULL, 0, &len );
2344 if( ret == 0 )
2345 {
2346 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2347 pem.buf, pem.buflen,
2348 pwd, pwdlen ) ) != 0 )
2349 {
2350 rsa_free( rsa );
2351 }
2352
2353 pem_free( &pem );
2354 return( ret );
2355 }
2356 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2357 {
2358 pem_free( &pem );
2359 return( ret );
2360 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002361#else
2362 ((void) pwd);
2363 ((void) pwdlen);
2364#endif /* POLARSSL_PEM_C */
2365
2366 // At this point we only know it's not a PEM formatted key. Could be any
2367 // of the known DER encoded private key formats
2368 //
2369 // We try the different DER format parsers to see if one passes without
2370 // error
2371 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002372 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2373 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002374 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002375 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002377
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002378 rsa_free( rsa );
2379 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2380 return( 0 );
2381
2382 rsa_free( rsa );
2383 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2384 return( 0 );
2385
2386 rsa_free( rsa );
2387 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388}
2389
2390/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002391 * Parse a public RSA key
2392 */
Paul Bakker23986e52011-04-24 08:57:21 +00002393int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002394{
Paul Bakker23986e52011-04-24 08:57:21 +00002395 int ret;
2396 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002397 unsigned char *p, *end;
2398 x509_buf alg_oid;
2399#if defined(POLARSSL_PEM_C)
2400 pem_context pem;
2401
2402 pem_init( &pem );
2403 ret = pem_read_buffer( &pem,
2404 "-----BEGIN PUBLIC KEY-----",
2405 "-----END PUBLIC KEY-----",
2406 key, NULL, 0, &len );
2407
2408 if( ret == 0 )
2409 {
2410 /*
2411 * Was PEM encoded
2412 */
2413 keylen = pem.buflen;
2414 }
Paul Bakker00b28602013-06-24 13:02:41 +02002415 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002416 {
2417 pem_free( &pem );
2418 return( ret );
2419 }
2420
2421 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2422#else
2423 p = (unsigned char *) key;
2424#endif
2425 end = p + keylen;
2426
2427 /*
2428 * PublicKeyInfo ::= SEQUENCE {
2429 * algorithm AlgorithmIdentifier,
2430 * PublicKey BIT STRING
2431 * }
2432 *
2433 * AlgorithmIdentifier ::= SEQUENCE {
2434 * algorithm OBJECT IDENTIFIER,
2435 * parameters ANY DEFINED BY algorithm OPTIONAL
2436 * }
2437 *
2438 * RSAPublicKey ::= SEQUENCE {
2439 * modulus INTEGER, -- n
2440 * publicExponent INTEGER -- e
2441 * }
2442 */
2443
2444 if( ( ret = asn1_get_tag( &p, end, &len,
2445 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2446 {
2447#if defined(POLARSSL_PEM_C)
2448 pem_free( &pem );
2449#endif
2450 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002451 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002452 }
2453
2454 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2455 {
2456#if defined(POLARSSL_PEM_C)
2457 pem_free( &pem );
2458#endif
2459 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002460 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002461 }
2462
2463 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2464 {
2465#if defined(POLARSSL_PEM_C)
2466 pem_free( &pem );
2467#endif
2468 rsa_free( rsa );
2469 return( ret );
2470 }
2471
2472 rsa->len = mpi_size( &rsa->N );
2473
2474#if defined(POLARSSL_PEM_C)
2475 pem_free( &pem );
2476#endif
2477
2478 return( 0 );
2479}
2480
Paul Bakkereaa89f82011-04-04 21:36:15 +00002481#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002482/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002483 * Parse DHM parameters
2484 */
Paul Bakker23986e52011-04-24 08:57:21 +00002485int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002486{
Paul Bakker23986e52011-04-24 08:57:21 +00002487 int ret;
2488 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002489 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002490#if defined(POLARSSL_PEM_C)
2491 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002492
Paul Bakker96743fc2011-02-12 14:30:57 +00002493 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002494
Paul Bakker96743fc2011-02-12 14:30:57 +00002495 ret = pem_read_buffer( &pem,
2496 "-----BEGIN DH PARAMETERS-----",
2497 "-----END DH PARAMETERS-----",
2498 dhmin, NULL, 0, &dhminlen );
2499
2500 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002501 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002502 /*
2503 * Was PEM encoded
2504 */
2505 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002506 }
Paul Bakker00b28602013-06-24 13:02:41 +02002507 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002508 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002509 pem_free( &pem );
2510 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002511 }
2512
Paul Bakker96743fc2011-02-12 14:30:57 +00002513 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2514#else
2515 p = (unsigned char *) dhmin;
2516#endif
2517 end = p + dhminlen;
2518
Paul Bakker1b57b062011-01-06 15:48:19 +00002519 memset( dhm, 0, sizeof( dhm_context ) );
2520
Paul Bakker1b57b062011-01-06 15:48:19 +00002521 /*
2522 * DHParams ::= SEQUENCE {
2523 * prime INTEGER, -- P
2524 * generator INTEGER, -- g
2525 * }
2526 */
2527 if( ( ret = asn1_get_tag( &p, end, &len,
2528 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2529 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002530#if defined(POLARSSL_PEM_C)
2531 pem_free( &pem );
2532#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002533 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002534 }
2535
2536 end = p + len;
2537
2538 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2539 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2540 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002541#if defined(POLARSSL_PEM_C)
2542 pem_free( &pem );
2543#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002544 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002545 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002546 }
2547
2548 if( p != end )
2549 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002550#if defined(POLARSSL_PEM_C)
2551 pem_free( &pem );
2552#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002553 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002554 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002555 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2556 }
2557
Paul Bakker96743fc2011-02-12 14:30:57 +00002558#if defined(POLARSSL_PEM_C)
2559 pem_free( &pem );
2560#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002561
2562 return( 0 );
2563}
2564
Paul Bakker335db3f2011-04-25 15:28:35 +00002565#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002566/*
2567 * Load and parse a private RSA key
2568 */
2569int x509parse_dhmfile( dhm_context *dhm, const char *path )
2570{
2571 int ret;
2572 size_t n;
2573 unsigned char *buf;
2574
Paul Bakker69e095c2011-12-10 21:55:01 +00002575 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2576 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002577
Paul Bakker27fdf462011-06-09 13:55:13 +00002578 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002579
2580 memset( buf, 0, n + 1 );
2581 free( buf );
2582
2583 return( ret );
2584}
Paul Bakker335db3f2011-04-25 15:28:35 +00002585#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002586#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002587
Paul Bakker5121ce52009-01-03 21:22:43 +00002588#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589#include <stdarg.h>
2590
2591#if !defined vsnprintf
2592#define vsnprintf _vsnprintf
2593#endif // vsnprintf
2594
2595/*
2596 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2597 * Result value is not size of buffer needed, but -1 if no fit is possible.
2598 *
2599 * This fuction tries to 'fix' this by at least suggesting enlarging the
2600 * size by 20.
2601 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002602static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002603{
2604 va_list ap;
2605 int res = -1;
2606
2607 va_start( ap, format );
2608
2609 res = vsnprintf( str, size, format, ap );
2610
2611 va_end( ap );
2612
2613 // No quick fix possible
2614 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002615 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002616
2617 return res;
2618}
2619
2620#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002621#endif
2622
Paul Bakkerd98030e2009-05-02 15:13:40 +00002623#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2624
2625#define SAFE_SNPRINTF() \
2626{ \
2627 if( ret == -1 ) \
2628 return( -1 ); \
2629 \
Paul Bakker23986e52011-04-24 08:57:21 +00002630 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002631 p[n - 1] = '\0'; \
2632 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2633 } \
2634 \
Paul Bakker23986e52011-04-24 08:57:21 +00002635 n -= (unsigned int) ret; \
2636 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002637}
2638
Paul Bakker5121ce52009-01-03 21:22:43 +00002639/*
2640 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002643int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002644{
Paul Bakker23986e52011-04-24 08:57:21 +00002645 int ret;
2646 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002648 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002649 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 char s[128], *p;
2651
2652 memset( s, 0, sizeof( s ) );
2653
2654 name = dn;
2655 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002656 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657
2658 while( name != NULL )
2659 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002660 if( !name->oid.p )
2661 {
2662 name = name->next;
2663 continue;
2664 }
2665
Paul Bakker74111d32011-01-15 16:57:55 +00002666 if( name != dn )
2667 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002668 ret = snprintf( p, n, ", " );
2669 SAFE_SNPRINTF();
2670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002671
Paul Bakkerc70b9822013-04-07 22:00:46 +02002672 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Paul Bakkerc70b9822013-04-07 22:00:46 +02002674 if( ret == 0 )
2675 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002677 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002678 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
2680 for( i = 0; i < name->val.len; i++ )
2681 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002682 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 break;
2684
2685 c = name->val.p[i];
2686 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2687 s[i] = '?';
2688 else s[i] = c;
2689 }
2690 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002691 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002692 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 name = name->next;
2694 }
2695
Paul Bakker23986e52011-04-24 08:57:21 +00002696 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697}
2698
2699/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002700 * Store the serial in printable form into buf; no more
2701 * than size characters will be written
2702 */
2703int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2704{
Paul Bakker23986e52011-04-24 08:57:21 +00002705 int ret;
2706 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002707 char *p;
2708
2709 p = buf;
2710 n = size;
2711
2712 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002713 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002714
2715 for( i = 0; i < nr; i++ )
2716 {
Paul Bakker93048802011-12-05 14:38:06 +00002717 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002718 continue;
2719
Paul Bakkerdd476992011-01-16 21:34:59 +00002720 ret = snprintf( p, n, "%02X%s",
2721 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2722 SAFE_SNPRINTF();
2723 }
2724
Paul Bakker03c7c252011-11-25 12:37:37 +00002725 if( nr != serial->len )
2726 {
2727 ret = snprintf( p, n, "...." );
2728 SAFE_SNPRINTF();
2729 }
2730
Paul Bakker23986e52011-04-24 08:57:21 +00002731 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002732}
2733
2734/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002737int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2738 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002739{
Paul Bakker23986e52011-04-24 08:57:21 +00002740 int ret;
2741 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002742 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002743 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
2745 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002746 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750 SAFE_SNPRINTF();
2751 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002753 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
Paul Bakkerdd476992011-01-16 21:34:59 +00002755 ret = x509parse_serial_gets( p, n, &crt->serial);
2756 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Paul Bakkerd98030e2009-05-02 15:13:40 +00002758 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2759 SAFE_SNPRINTF();
2760 ret = x509parse_dn_gets( p, n, &crt->issuer );
2761 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
Paul Bakkerd98030e2009-05-02 15:13:40 +00002763 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2764 SAFE_SNPRINTF();
2765 ret = x509parse_dn_gets( p, n, &crt->subject );
2766 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Paul Bakkerd98030e2009-05-02 15:13:40 +00002768 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2770 crt->valid_from.year, crt->valid_from.mon,
2771 crt->valid_from.day, crt->valid_from.hour,
2772 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002773 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
Paul Bakkerd98030e2009-05-02 15:13:40 +00002775 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2777 crt->valid_to.year, crt->valid_to.mon,
2778 crt->valid_to.day, crt->valid_to.hour,
2779 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002780 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Paul Bakkerc70b9822013-04-07 22:00:46 +02002782 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Paul Bakkerc70b9822013-04-07 22:00:46 +02002785 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2786 if( ret != 0 )
2787 ret = snprintf( p, n, "???" );
2788 else
2789 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 SAFE_SNPRINTF();
2791
2792 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002793 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794 SAFE_SNPRINTF();
2795
Paul Bakker23986e52011-04-24 08:57:21 +00002796 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002797}
2798
Paul Bakker74111d32011-01-15 16:57:55 +00002799/*
2800 * Return an informational string describing the given OID
2801 */
2802const char *x509_oid_get_description( x509_buf *oid )
2803{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002804 const char *desc = NULL;
2805 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002806
Paul Bakkerc70b9822013-04-07 22:00:46 +02002807 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002808
Paul Bakkerc70b9822013-04-07 22:00:46 +02002809 if( ret != 0 )
2810 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002811
Paul Bakkerc70b9822013-04-07 22:00:46 +02002812 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002813}
2814
2815/* Return the x.y.z.... style numeric string for the given OID */
2816int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2817{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002818 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002819}
2820
Paul Bakkerd98030e2009-05-02 15:13:40 +00002821/*
2822 * Return an informational string about the CRL.
2823 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002824int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2825 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002826{
Paul Bakker23986e52011-04-24 08:57:21 +00002827 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002828 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002829 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002830 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002831 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002832
2833 p = buf;
2834 n = size;
2835
2836 ret = snprintf( p, n, "%sCRL version : %d",
2837 prefix, crl->version );
2838 SAFE_SNPRINTF();
2839
2840 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2841 SAFE_SNPRINTF();
2842 ret = x509parse_dn_gets( p, n, &crl->issuer );
2843 SAFE_SNPRINTF();
2844
2845 ret = snprintf( p, n, "\n%sthis update : " \
2846 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2847 crl->this_update.year, crl->this_update.mon,
2848 crl->this_update.day, crl->this_update.hour,
2849 crl->this_update.min, crl->this_update.sec );
2850 SAFE_SNPRINTF();
2851
2852 ret = snprintf( p, n, "\n%snext update : " \
2853 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2854 crl->next_update.year, crl->next_update.mon,
2855 crl->next_update.day, crl->next_update.hour,
2856 crl->next_update.min, crl->next_update.sec );
2857 SAFE_SNPRINTF();
2858
2859 entry = &crl->entry;
2860
2861 ret = snprintf( p, n, "\n%sRevoked certificates:",
2862 prefix );
2863 SAFE_SNPRINTF();
2864
Paul Bakker9be19372009-07-27 20:21:53 +00002865 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002866 {
2867 ret = snprintf( p, n, "\n%sserial number: ",
2868 prefix );
2869 SAFE_SNPRINTF();
2870
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002871 ret = x509parse_serial_gets( p, n, &entry->serial);
2872 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002873
Paul Bakkerd98030e2009-05-02 15:13:40 +00002874 ret = snprintf( p, n, " revocation date: " \
2875 "%04d-%02d-%02d %02d:%02d:%02d",
2876 entry->revocation_date.year, entry->revocation_date.mon,
2877 entry->revocation_date.day, entry->revocation_date.hour,
2878 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002879 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002880
2881 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 }
2883
Paul Bakkerc70b9822013-04-07 22:00:46 +02002884 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002885 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Paul Bakkerc70b9822013-04-07 22:00:46 +02002887 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2888 if( ret != 0 )
2889 ret = snprintf( p, n, "???" );
2890 else
2891 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002892 SAFE_SNPRINTF();
2893
Paul Bakker1e27bb22009-07-19 20:25:25 +00002894 ret = snprintf( p, n, "\n" );
2895 SAFE_SNPRINTF();
2896
Paul Bakker23986e52011-04-24 08:57:21 +00002897 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898}
2899
2900/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002901 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002903int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002904{
Paul Bakkercce9d772011-11-18 14:26:47 +00002905 int year, mon, day;
2906 int hour, min, sec;
2907
2908#if defined(_WIN32)
2909 SYSTEMTIME st;
2910
2911 GetLocalTime(&st);
2912
2913 year = st.wYear;
2914 mon = st.wMonth;
2915 day = st.wDay;
2916 hour = st.wHour;
2917 min = st.wMinute;
2918 sec = st.wSecond;
2919#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 struct tm *lt;
2921 time_t tt;
2922
2923 tt = time( NULL );
2924 lt = localtime( &tt );
2925
Paul Bakkercce9d772011-11-18 14:26:47 +00002926 year = lt->tm_year + 1900;
2927 mon = lt->tm_mon + 1;
2928 day = lt->tm_mday;
2929 hour = lt->tm_hour;
2930 min = lt->tm_min;
2931 sec = lt->tm_sec;
2932#endif
2933
2934 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002935 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakkercce9d772011-11-18 14:26:47 +00002937 if( year == to->year &&
2938 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002939 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakkercce9d772011-11-18 14:26:47 +00002941 if( year == to->year &&
2942 mon == to->mon &&
2943 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002944 return( 1 );
2945
Paul Bakkercce9d772011-11-18 14:26:47 +00002946 if( year == to->year &&
2947 mon == to->mon &&
2948 day == to->day &&
2949 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002950 return( 1 );
2951
Paul Bakkercce9d772011-11-18 14:26:47 +00002952 if( year == to->year &&
2953 mon == to->mon &&
2954 day == to->day &&
2955 hour == to->hour &&
2956 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002957 return( 1 );
2958
Paul Bakkercce9d772011-11-18 14:26:47 +00002959 if( year == to->year &&
2960 mon == to->mon &&
2961 day == to->day &&
2962 hour == to->hour &&
2963 min == to->min &&
2964 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002965 return( 1 );
2966
Paul Bakker40ea7de2009-05-03 10:18:48 +00002967 return( 0 );
2968}
2969
2970/*
2971 * Return 1 if the certificate is revoked, or 0 otherwise.
2972 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002973int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002974{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002975 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002976
2977 while( cur != NULL && cur->serial.len != 0 )
2978 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002979 if( crt->serial.len == cur->serial.len &&
2980 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002981 {
2982 if( x509parse_time_expired( &cur->revocation_date ) )
2983 return( 1 );
2984 }
2985
2986 cur = cur->next;
2987 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
2989 return( 0 );
2990}
2991
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002992/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002993 * Check that the given certificate is valid accoring to the CRL.
2994 */
2995static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2996 x509_crl *crl_list)
2997{
2998 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002999 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3000 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003001
Paul Bakker915275b2012-09-28 07:10:55 +00003002 if( ca == NULL )
3003 return( flags );
3004
Paul Bakker76fd75a2011-01-16 21:12:10 +00003005 /*
3006 * TODO: What happens if no CRL is present?
3007 * Suggestion: Revocation state should be unknown if no CRL is present.
3008 * For backwards compatibility this is not yet implemented.
3009 */
3010
Paul Bakker915275b2012-09-28 07:10:55 +00003011 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003012 {
Paul Bakker915275b2012-09-28 07:10:55 +00003013 if( crl_list->version == 0 ||
3014 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003015 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3016 crl_list->issuer_raw.len ) != 0 )
3017 {
3018 crl_list = crl_list->next;
3019 continue;
3020 }
3021
3022 /*
3023 * Check if CRL is correctly signed by the trusted CA
3024 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003025 md_info = md_info_from_type( crl_list->sig_md );
3026 if( md_info == NULL )
3027 {
3028 /*
3029 * Cannot check 'unknown' hash
3030 */
3031 flags |= BADCRL_NOT_TRUSTED;
3032 break;
3033 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003034
Paul Bakkerc70b9822013-04-07 22:00:46 +02003035 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003036
Paul Bakkerc70b9822013-04-07 22:00:46 +02003037 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003038 0, hash, crl_list->sig.p ) == 0 )
3039 {
3040 /*
3041 * CRL is not trusted
3042 */
3043 flags |= BADCRL_NOT_TRUSTED;
3044 break;
3045 }
3046
3047 /*
3048 * Check for validity of CRL (Do not drop out)
3049 */
3050 if( x509parse_time_expired( &crl_list->next_update ) )
3051 flags |= BADCRL_EXPIRED;
3052
3053 /*
3054 * Check if certificate is revoked
3055 */
3056 if( x509parse_revoked(crt, crl_list) )
3057 {
3058 flags |= BADCERT_REVOKED;
3059 break;
3060 }
3061
3062 crl_list = crl_list->next;
3063 }
3064 return flags;
3065}
3066
Paul Bakker57b12982012-02-11 17:38:38 +00003067int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003068{
3069 size_t i;
3070 size_t cn_idx = 0;
3071
Paul Bakker57b12982012-02-11 17:38:38 +00003072 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003073 return( 0 );
3074
3075 for( i = 0; i < strlen( cn ); ++i )
3076 {
3077 if( cn[i] == '.' )
3078 {
3079 cn_idx = i;
3080 break;
3081 }
3082 }
3083
3084 if( cn_idx == 0 )
3085 return( 0 );
3086
Paul Bakker535e97d2012-08-23 10:49:55 +00003087 if( strlen( cn ) - cn_idx == name->len - 1 &&
3088 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003089 {
3090 return( 1 );
3091 }
3092
3093 return( 0 );
3094}
3095
Paul Bakker915275b2012-09-28 07:10:55 +00003096static int x509parse_verify_top(
3097 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003098 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003099 int (*f_vrfy)(void *, x509_cert *, int, int *),
3100 void *p_vrfy )
3101{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003102 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003103 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003104 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3105 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003106
3107 if( x509parse_time_expired( &child->valid_to ) )
3108 *flags |= BADCERT_EXPIRED;
3109
3110 /*
3111 * Child is the top of the chain. Check against the trust_ca list.
3112 */
3113 *flags |= BADCERT_NOT_TRUSTED;
3114
3115 while( trust_ca != NULL )
3116 {
3117 if( trust_ca->version == 0 ||
3118 child->issuer_raw.len != trust_ca->subject_raw.len ||
3119 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3120 child->issuer_raw.len ) != 0 )
3121 {
3122 trust_ca = trust_ca->next;
3123 continue;
3124 }
3125
Paul Bakker9a736322012-11-14 12:39:52 +00003126 /*
3127 * Reduce path_len to check against if top of the chain is
3128 * the same as the trusted CA
3129 */
3130 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3131 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3132 child->issuer_raw.len ) == 0 )
3133 {
3134 check_path_cnt--;
3135 }
3136
Paul Bakker915275b2012-09-28 07:10:55 +00003137 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003138 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003139 {
3140 trust_ca = trust_ca->next;
3141 continue;
3142 }
3143
Paul Bakkerc70b9822013-04-07 22:00:46 +02003144 md_info = md_info_from_type( child->sig_md );
3145 if( md_info == NULL )
3146 {
3147 /*
3148 * Cannot check 'unknown' hash
3149 */
3150 continue;
3151 }
Paul Bakker915275b2012-09-28 07:10:55 +00003152
Paul Bakkerc70b9822013-04-07 22:00:46 +02003153 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003154
Paul Bakkerc70b9822013-04-07 22:00:46 +02003155 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003156 0, hash, child->sig.p ) != 0 )
3157 {
3158 trust_ca = trust_ca->next;
3159 continue;
3160 }
3161
3162 /*
3163 * Top of chain is signed by a trusted CA
3164 */
3165 *flags &= ~BADCERT_NOT_TRUSTED;
3166 break;
3167 }
3168
Paul Bakker9a736322012-11-14 12:39:52 +00003169 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003170 * If top of chain is not the same as the trusted CA send a verify request
3171 * to the callback for any issues with validity and CRL presence for the
3172 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003173 */
3174 if( trust_ca != NULL &&
3175 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3176 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3177 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003178 {
3179 /* Check trusted CA's CRL for then chain's top crt */
3180 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3181
3182 if( x509parse_time_expired( &trust_ca->valid_to ) )
3183 ca_flags |= BADCERT_EXPIRED;
3184
Paul Bakker915275b2012-09-28 07:10:55 +00003185 if( NULL != f_vrfy )
3186 {
Paul Bakker9a736322012-11-14 12:39:52 +00003187 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003188 return( ret );
3189 }
3190 }
3191
3192 /* Call callback on top cert */
3193 if( NULL != f_vrfy )
3194 {
Paul Bakker9a736322012-11-14 12:39:52 +00003195 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003196 return( ret );
3197 }
3198
Paul Bakker915275b2012-09-28 07:10:55 +00003199 *flags |= ca_flags;
3200
3201 return( 0 );
3202}
3203
3204static int x509parse_verify_child(
3205 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003206 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003207 int (*f_vrfy)(void *, x509_cert *, int, int *),
3208 void *p_vrfy )
3209{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003210 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003211 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003212 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003213 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003214 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003215
3216 if( x509parse_time_expired( &child->valid_to ) )
3217 *flags |= BADCERT_EXPIRED;
3218
Paul Bakkerc70b9822013-04-07 22:00:46 +02003219 md_info = md_info_from_type( child->sig_md );
3220 if( md_info == NULL )
3221 {
3222 /*
3223 * Cannot check 'unknown' hash
3224 */
Paul Bakker915275b2012-09-28 07:10:55 +00003225 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003226 }
3227 else
3228 {
3229 md( md_info, child->tbs.p, child->tbs.len, hash );
3230
3231 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3232 child->sig.p ) != 0 )
3233 *flags |= BADCERT_NOT_TRUSTED;
3234 }
3235
Paul Bakker915275b2012-09-28 07:10:55 +00003236 /* Check trusted CA's CRL for the given crt */
3237 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3238
3239 grandparent = parent->next;
3240
3241 while( grandparent != NULL )
3242 {
3243 if( grandparent->version == 0 ||
3244 grandparent->ca_istrue == 0 ||
3245 parent->issuer_raw.len != grandparent->subject_raw.len ||
3246 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3247 parent->issuer_raw.len ) != 0 )
3248 {
3249 grandparent = grandparent->next;
3250 continue;
3251 }
3252 break;
3253 }
3254
Paul Bakker915275b2012-09-28 07:10:55 +00003255 if( grandparent != NULL )
3256 {
3257 /*
3258 * Part of the chain
3259 */
Paul Bakker9a736322012-11-14 12:39:52 +00003260 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003261 if( ret != 0 )
3262 return( ret );
3263 }
3264 else
3265 {
Paul Bakker9a736322012-11-14 12:39:52 +00003266 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003267 if( ret != 0 )
3268 return( ret );
3269 }
3270
3271 /* child is verified to be a child of the parent, call verify callback */
3272 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003273 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003274 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003275
3276 *flags |= parent_flags;
3277
3278 return( 0 );
3279}
3280
Paul Bakker76fd75a2011-01-16 21:12:10 +00003281/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 * Verify the certificate validity
3283 */
3284int x509parse_verify( x509_cert *crt,
3285 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003286 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003287 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003288 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003289 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003290{
Paul Bakker23986e52011-04-24 08:57:21 +00003291 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003292 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003293 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003294 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003296 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Paul Bakker40ea7de2009-05-03 10:18:48 +00003298 *flags = 0;
3299
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 if( cn != NULL )
3301 {
3302 name = &crt->subject;
3303 cn_len = strlen( cn );
3304
Paul Bakker4d2c1242012-05-10 14:12:46 +00003305 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003307 cur = &crt->subject_alt_names;
3308
3309 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003310 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003311 if( cur->buf.len == cn_len &&
3312 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003313 break;
3314
Paul Bakker535e97d2012-08-23 10:49:55 +00003315 if( cur->buf.len > 2 &&
3316 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003317 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003318 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakker4d2c1242012-05-10 14:12:46 +00003320 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003321 }
3322
3323 if( cur == NULL )
3324 *flags |= BADCERT_CN_MISMATCH;
3325 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003326 else
3327 {
3328 while( name != NULL )
3329 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003330 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003331 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003332 if( name->val.len == cn_len &&
3333 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003334 break;
3335
Paul Bakker535e97d2012-08-23 10:49:55 +00003336 if( name->val.len > 2 &&
3337 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003338 x509_wildcard_verify( cn, &name->val ) )
3339 break;
3340 }
3341
3342 name = name->next;
3343 }
3344
3345 if( name == NULL )
3346 *flags |= BADCERT_CN_MISMATCH;
3347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 }
3349
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003351 * Iterate upwards in the given cert chain, to find our crt parent.
3352 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003354 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
Paul Bakker76fd75a2011-01-16 21:12:10 +00003356 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003358 if( parent->ca_istrue == 0 ||
3359 crt->issuer_raw.len != parent->subject_raw.len ||
3360 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 crt->issuer_raw.len ) != 0 )
3362 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003363 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 continue;
3365 }
Paul Bakker915275b2012-09-28 07:10:55 +00003366 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 }
3368
Paul Bakker915275b2012-09-28 07:10:55 +00003369 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 {
Paul Bakker915275b2012-09-28 07:10:55 +00003371 /*
3372 * Part of the chain
3373 */
Paul Bakker9a736322012-11-14 12:39:52 +00003374 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003375 if( ret != 0 )
3376 return( ret );
3377 }
3378 else
Paul Bakker74111d32011-01-15 16:57:55 +00003379 {
Paul Bakker9a736322012-11-14 12:39:52 +00003380 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003381 if( ret != 0 )
3382 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003383 }
Paul Bakker915275b2012-09-28 07:10:55 +00003384
3385 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003386 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003387
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 return( 0 );
3389}
3390
3391/*
3392 * Unallocate all certificate data
3393 */
3394void x509_free( x509_cert *crt )
3395{
3396 x509_cert *cert_cur = crt;
3397 x509_cert *cert_prv;
3398 x509_name *name_cur;
3399 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003400 x509_sequence *seq_cur;
3401 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003402
3403 if( crt == NULL )
3404 return;
3405
3406 do
3407 {
3408 rsa_free( &cert_cur->rsa );
3409
3410 name_cur = cert_cur->issuer.next;
3411 while( name_cur != NULL )
3412 {
3413 name_prv = name_cur;
3414 name_cur = name_cur->next;
3415 memset( name_prv, 0, sizeof( x509_name ) );
3416 free( name_prv );
3417 }
3418
3419 name_cur = cert_cur->subject.next;
3420 while( name_cur != NULL )
3421 {
3422 name_prv = name_cur;
3423 name_cur = name_cur->next;
3424 memset( name_prv, 0, sizeof( x509_name ) );
3425 free( name_prv );
3426 }
3427
Paul Bakker74111d32011-01-15 16:57:55 +00003428 seq_cur = cert_cur->ext_key_usage.next;
3429 while( seq_cur != NULL )
3430 {
3431 seq_prv = seq_cur;
3432 seq_cur = seq_cur->next;
3433 memset( seq_prv, 0, sizeof( x509_sequence ) );
3434 free( seq_prv );
3435 }
3436
Paul Bakker8afa70d2012-02-11 18:42:45 +00003437 seq_cur = cert_cur->subject_alt_names.next;
3438 while( seq_cur != NULL )
3439 {
3440 seq_prv = seq_cur;
3441 seq_cur = seq_cur->next;
3442 memset( seq_prv, 0, sizeof( x509_sequence ) );
3443 free( seq_prv );
3444 }
3445
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 if( cert_cur->raw.p != NULL )
3447 {
3448 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3449 free( cert_cur->raw.p );
3450 }
3451
3452 cert_cur = cert_cur->next;
3453 }
3454 while( cert_cur != NULL );
3455
3456 cert_cur = crt;
3457 do
3458 {
3459 cert_prv = cert_cur;
3460 cert_cur = cert_cur->next;
3461
3462 memset( cert_prv, 0, sizeof( x509_cert ) );
3463 if( cert_prv != crt )
3464 free( cert_prv );
3465 }
3466 while( cert_cur != NULL );
3467}
3468
Paul Bakkerd98030e2009-05-02 15:13:40 +00003469/*
3470 * Unallocate all CRL data
3471 */
3472void x509_crl_free( x509_crl *crl )
3473{
3474 x509_crl *crl_cur = crl;
3475 x509_crl *crl_prv;
3476 x509_name *name_cur;
3477 x509_name *name_prv;
3478 x509_crl_entry *entry_cur;
3479 x509_crl_entry *entry_prv;
3480
3481 if( crl == NULL )
3482 return;
3483
3484 do
3485 {
3486 name_cur = crl_cur->issuer.next;
3487 while( name_cur != NULL )
3488 {
3489 name_prv = name_cur;
3490 name_cur = name_cur->next;
3491 memset( name_prv, 0, sizeof( x509_name ) );
3492 free( name_prv );
3493 }
3494
3495 entry_cur = crl_cur->entry.next;
3496 while( entry_cur != NULL )
3497 {
3498 entry_prv = entry_cur;
3499 entry_cur = entry_cur->next;
3500 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3501 free( entry_prv );
3502 }
3503
3504 if( crl_cur->raw.p != NULL )
3505 {
3506 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3507 free( crl_cur->raw.p );
3508 }
3509
3510 crl_cur = crl_cur->next;
3511 }
3512 while( crl_cur != NULL );
3513
3514 crl_cur = crl;
3515 do
3516 {
3517 crl_prv = crl_cur;
3518 crl_cur = crl_cur->next;
3519
3520 memset( crl_prv, 0, sizeof( x509_crl ) );
3521 if( crl_prv != crl )
3522 free( crl_prv );
3523 }
3524 while( crl_cur != NULL );
3525}
3526
Paul Bakker40e46942009-01-03 21:51:57 +00003527#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Paul Bakker40e46942009-01-03 21:51:57 +00003529#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
3531/*
3532 * Checkup routine
3533 */
3534int x509_self_test( int verbose )
3535{
Paul Bakker5690efc2011-05-26 13:16:06 +00003536#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003537 int ret;
3538 int flags;
3539 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 x509_cert cacert;
3541 x509_cert clicert;
3542 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003543#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003544 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
3547 if( verbose != 0 )
3548 printf( " X.509 certificate load: " );
3549
3550 memset( &clicert, 0, sizeof( x509_cert ) );
3551
Paul Bakker3c2122f2013-06-24 19:03:14 +02003552 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003553 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 if( ret != 0 )
3555 {
3556 if( verbose != 0 )
3557 printf( "failed\n" );
3558
3559 return( ret );
3560 }
3561
3562 memset( &cacert, 0, sizeof( x509_cert ) );
3563
Paul Bakker3c2122f2013-06-24 19:03:14 +02003564 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003565 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 if( ret != 0 )
3567 {
3568 if( verbose != 0 )
3569 printf( "failed\n" );
3570
3571 return( ret );
3572 }
3573
3574 if( verbose != 0 )
3575 printf( "passed\n X.509 private key load: " );
3576
3577 i = strlen( test_ca_key );
3578 j = strlen( test_ca_pwd );
3579
Paul Bakker66b78b22011-03-25 14:22:50 +00003580 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3581
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003583 (const unsigned char *) test_ca_key, i,
3584 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 {
3586 if( verbose != 0 )
3587 printf( "failed\n" );
3588
3589 return( ret );
3590 }
3591
3592 if( verbose != 0 )
3593 printf( "passed\n X.509 signature verify: ");
3594
Paul Bakker23986e52011-04-24 08:57:21 +00003595 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 if( ret != 0 )
3597 {
Paul Bakker23986e52011-04-24 08:57:21 +00003598 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 if( verbose != 0 )
3600 printf( "failed\n" );
3601
3602 return( ret );
3603 }
3604
Paul Bakker5690efc2011-05-26 13:16:06 +00003605#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003607 printf( "passed\n X.509 DHM parameter load: " );
3608
3609 i = strlen( test_dhm_params );
3610 j = strlen( test_ca_pwd );
3611
Paul Bakker3c2122f2013-06-24 19:03:14 +02003612 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003613 {
3614 if( verbose != 0 )
3615 printf( "failed\n" );
3616
3617 return( ret );
3618 }
3619
3620 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
3624 x509_free( &cacert );
3625 x509_free( &clicert );
3626 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003627#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003628 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003629#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003630
3631 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003632#else
3633 ((void) verbose);
3634 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3635#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003636}
3637
3638#endif
3639
3640#endif