blob: 9f90b5ab6da0167e9042cf425f5881ead55e1fcb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, 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 Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200163/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200172{
173 int ret;
174 x509_buf alg_oid;
175
176 memset( params, 0, sizeof(asn1_buf) );
177
178 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
182 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
183
184 /*
185 * No parameters with RSA (only for EC)
186 */
187 if( *pk_alg == POLARSSL_PK_RSA &&
188 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
189 params->len != 0 ) )
190 {
191 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
192 }
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197/* Get an algorithm identifier without parameters (eg for signatures)
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AlgorithmIdentifier ::= SEQUENCE {
200 * algorithm OBJECT IDENTIFIER,
201 * parameters ANY DEFINED BY algorithm OPTIONAL }
202 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200203static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
204 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200208 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( 0 );
212}
213
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200214
215#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200216/* Get an EC group id from an ECParameters buffer
217 *
218 * ECParameters ::= CHOICE {
219 * namedCurve OBJECT IDENTIFIER
220 * -- implicitCurve NULL
221 * -- specifiedCurve SpecifiedECDomain
222 * }
223 */
224static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200225 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226{
227 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200231 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200232 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
233
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200234 params->p = *p;
235 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200236
237 if( *p != end )
238 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
239 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
240
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200241 return( 0 );
242}
243
244/*
245 * Use EC parameters to initialise an EC group
246 */
247static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
248{
249 int ret;
250 ecp_group_id grp_id;
251
252 if( oid_get_ec_grp( params, &grp_id ) != 0 )
253 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
254
255 /*
256 * grp may already be initilialized; if so, make sure IDs match
257 */
258 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
259 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
260
261 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
262 return( ret );
263
264 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200265}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200266#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * AttributeTypeAndValue ::= SEQUENCE {
270 * type AttributeType,
271 * value AttributeValue }
272 *
273 * AttributeType ::= OBJECT IDENTIFIER
274 *
275 * AttributeValue ::= ANY DEFINED BY AttributeType
276 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277static int x509_get_attr_type_value( unsigned char **p,
278 const unsigned char *end,
279 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 x509_buf *oid;
284 x509_buf *val;
285
286 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200290 if( ( end - *p ) < 1 )
291 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
292 POLARSSL_ERR_ASN1_OUT_OF_DATA );
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 oid = &cur->oid;
295 oid->tag = **p;
296
297 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 oid->p = *p;
301 *p += oid->len;
302
303 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
308 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
309 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000311 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 val = &cur->val;
314 val->tag = *(*p)++;
315
316 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319 val->p = *p;
320 *p += val->len;
321
322 cur->next = NULL;
323
Paul Bakker400ff6f2011-02-20 10:40:16 +0000324 return( 0 );
325}
326
327/*
328 * RelativeDistinguishedName ::=
329 * SET OF AttributeTypeAndValue
330 *
331 * AttributeTypeAndValue ::= SEQUENCE {
332 * type AttributeType,
333 * value AttributeValue }
334 *
335 * AttributeType ::= OBJECT IDENTIFIER
336 *
337 * AttributeValue ::= ANY DEFINED BY AttributeType
338 */
339static int x509_get_name( unsigned char **p,
340 const unsigned char *end,
341 x509_name *cur )
342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret;
344 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000345 const unsigned char *end2;
346 x509_name *use;
347
348 if( ( ret = asn1_get_tag( p, end, &len,
349 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000351
352 end2 = end;
353 end = *p + len;
354 use = cur;
355
356 do
357 {
358 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
359 return( ret );
360
361 if( *p != end )
362 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200363 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000364 sizeof( x509_name ) );
365
366 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000367 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368
369 memset( use->next, 0, sizeof( x509_name ) );
370
371 use = use->next;
372 }
373 }
374 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 /*
377 * recurse until end of SEQUENCE is reached
378 */
379 if( *p == end2 )
380 return( 0 );
381
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 sizeof( x509_name ) );
384
385 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000386 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Paul Bakker430ffbe2012-05-01 08:14:20 +0000388 memset( cur->next, 0, sizeof( x509_name ) );
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 return( x509_get_name( p, end2, cur->next ) );
391}
392
393/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * Time ::= CHOICE {
395 * utcTime UTCTime,
396 * generalTime GeneralizedTime }
397 */
Paul Bakker91200182010-02-18 21:26:15 +0000398static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400 x509_time *time )
401{
Paul Bakker23986e52011-04-24 08:57:21 +0000402 int ret;
403 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000405 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406
Paul Bakker91200182010-02-18 21:26:15 +0000407 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000408 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
409 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412
Paul Bakker91200182010-02-18 21:26:15 +0000413 if ( tag == ASN1_UTC_TIME )
414 {
415 (*p)++;
416 ret = asn1_get_len( p, end, &len );
417
418 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420
Paul Bakker91200182010-02-18 21:26:15 +0000421 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000422 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
423 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424
Paul Bakker91200182010-02-18 21:26:15 +0000425 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
426 &time->year, &time->mon, &time->day,
427 &time->hour, &time->min, &time->sec ) < 5 )
428 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429
Paul Bakker400ff6f2011-02-20 10:40:16 +0000430 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000431 time->year += 1900;
432
433 *p += len;
434
435 return( 0 );
436 }
437 else if ( tag == ASN1_GENERALIZED_TIME )
438 {
439 (*p)++;
440 ret = asn1_get_len( p, end, &len );
441
442 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000443 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000444
445 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000446 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
447 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000448
449 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
450 &time->year, &time->mon, &time->day,
451 &time->hour, &time->min, &time->sec ) < 5 )
452 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
453
454 *p += len;
455
456 return( 0 );
457 }
458 else
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000460}
461
462
463/*
464 * Validity ::= SEQUENCE {
465 * notBefore Time,
466 * notAfter Time }
467 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000469 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 x509_time *from,
471 x509_time *to )
472{
Paul Bakker23986e52011-04-24 08:57:21 +0000473 int ret;
474 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 if( ( ret = asn1_get_tag( p, end, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 end = *p + len;
481
Paul Bakker91200182010-02-18 21:26:15 +0000482 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker91200182010-02-18 21:26:15 +0000485 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000486 return( 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_DATE +
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
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200495#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200497 * RSAPublicKey ::= SEQUENCE {
498 * modulus INTEGER, -- n
499 * publicExponent INTEGER -- e
500 * }
501 */
502static int x509_get_rsapubkey( unsigned char **p,
503 const unsigned char *end,
504 rsa_context *rsa )
505{
506 int ret;
507 size_t len;
508
509 if( ( ret = asn1_get_tag( p, end, &len,
510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
511 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
512
513 if( *p + len != end )
514 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
515 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
516
517 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
518 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
519 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
520
Manuel Pégourié-Gonnardc13c0d42013-08-15 13:58:01 +0200521 if( *p != end )
522 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200525 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
526 return( ret );
527
528 rsa->len = mpi_size( &rsa->N );
529
530 return( 0 );
531}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200532#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200533
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200534#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200535/*
536 * EC public key is an EC point
537 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200538static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200539 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200540{
541 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200542
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200543 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200544 (const unsigned char *) *p, end - *p ) ) != 0 ||
545 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200546 {
547 ecp_keypair_free( key );
548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200549 }
550
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200551 /*
552 * We know ecp_point_read_binary consumed all bytes
553 */
554 *p = (unsigned char *) end;
555
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200556 return( 0 );
557}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200558#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200559
560/*
561 * SubjectPublicKeyInfo ::= SEQUENCE {
562 * algorithm AlgorithmIdentifier,
563 * subjectPublicKey BIT STRING }
564 */
565static int x509_get_pubkey( unsigned char **p,
566 const unsigned char *end,
567 pk_context *pk )
568{
569 int ret;
570 size_t len;
571 x509_buf alg_params;
572 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200573 const pk_info_t *pk_info;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200574
575 if( ( ret = asn1_get_tag( p, end, &len,
576 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
577 {
578 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
579 }
580
581 end = *p + len;
582
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200583 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200584 return( ret );
585
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200586 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200587 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
588
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200589 if( *p + len != end )
590 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
591 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
592
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200593 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
594 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
595
596 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200597 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200598
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200599#if defined(POLARSSL_RSA_C)
600 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200601 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200602 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
603 } else
604#endif /* POLARSSL_RSA_C */
605#if defined(POLARSSL_ECP_C)
606 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
607 {
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200608 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp );
609 if( ret == 0 )
610 ret = x509_get_ecpubkey( p, end, pk_ec( *pk ) );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200611 } else
612#endif /* POLARSSL_ECP_C */
613 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200614
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200615 if( ret == 0 && *p != end )
616 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
617 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
618
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200619 if( ret != 0 )
620 pk_free( pk );
621
622 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200623}
624
Paul Bakker5121ce52009-01-03 21:22:43 +0000625static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000626 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 x509_buf *sig )
628{
Paul Bakker23986e52011-04-24 08:57:21 +0000629 int ret;
630 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakker8afa70d2012-02-11 18:42:45 +0000632 if( ( end - *p ) < 1 )
633 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
634 POLARSSL_ERR_ASN1_OUT_OF_DATA );
635
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 sig->tag = **p;
637
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200638 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000639 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 sig->len = len;
642 sig->p = *p;
643
644 *p += len;
645
646 return( 0 );
647}
648
649/*
650 * X.509 v2/v3 unique identifier (not parsed)
651 */
652static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000653 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 x509_buf *uid, int n )
655{
656 int ret;
657
658 if( *p == end )
659 return( 0 );
660
661 uid->tag = **p;
662
663 if( ( ret = asn1_get_tag( p, end, &uid->len,
664 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
665 {
Paul Bakker40e46942009-01-03 21:51:57 +0000666 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 return( 0 );
668
669 return( ret );
670 }
671
672 uid->p = *p;
673 *p += uid->len;
674
675 return( 0 );
676}
677
678/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000679 * X.509 Extensions (No parsing of extensions, pointer should
680 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 */
682static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000683 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000684 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000685{
Paul Bakker23986e52011-04-24 08:57:21 +0000686 int ret;
687 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689 if( *p == end )
690 return( 0 );
691
692 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000693
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000695 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
698 ext->p = *p;
699 end = *p + ext->len;
700
701 /*
702 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
703 *
704 * Extension ::= SEQUENCE {
705 * extnID OBJECT IDENTIFIER,
706 * critical BOOLEAN DEFAULT FALSE,
707 * extnValue OCTET STRING }
708 */
709 if( ( ret = asn1_get_tag( p, end, &len,
710 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000711 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
713 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000714 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000715 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Paul Bakkerd98030e2009-05-02 15:13:40 +0000717 return( 0 );
718}
719
720/*
721 * X.509 CRL v2 extensions (no extensions parsed yet.)
722 */
723static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000724 const unsigned char *end,
725 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000726{
Paul Bakker23986e52011-04-24 08:57:21 +0000727 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000728 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000729
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000730 /* Get explicit tag */
731 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000732 {
733 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
734 return( 0 );
735
736 return( ret );
737 }
738
739 while( *p < end )
740 {
741 if( ( ret = asn1_get_tag( p, end, &len,
742 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000743 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000744
745 *p += len;
746 }
747
748 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000750 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
751
752 return( 0 );
753}
754
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000755/*
756 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
757 */
758static int x509_get_crl_entry_ext( unsigned char **p,
759 const unsigned char *end,
760 x509_buf *ext )
761{
762 int ret;
763 size_t len = 0;
764
765 /* OPTIONAL */
766 if (end <= *p)
767 return( 0 );
768
769 ext->tag = **p;
770 ext->p = *p;
771
772 /*
773 * Get CRL-entry extension sequence header
774 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
775 */
776 if( ( ret = asn1_get_tag( p, end, &ext->len,
777 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
778 {
779 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
780 {
781 ext->p = NULL;
782 return( 0 );
783 }
784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
785 }
786
787 end = *p + ext->len;
788
789 if( end != *p + ext->len )
790 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
791 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
792
793 while( *p < end )
794 {
795 if( ( ret = asn1_get_tag( p, end, &len,
796 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
797 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
798
799 *p += len;
800 }
801
802 if( *p != end )
803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
804 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
805
806 return( 0 );
807}
808
Paul Bakker74111d32011-01-15 16:57:55 +0000809static int x509_get_basic_constraints( unsigned char **p,
810 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000811 int *ca_istrue,
812 int *max_pathlen )
813{
Paul Bakker23986e52011-04-24 08:57:21 +0000814 int ret;
815 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000816
817 /*
818 * BasicConstraints ::= SEQUENCE {
819 * cA BOOLEAN DEFAULT FALSE,
820 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
821 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000822 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000823 *max_pathlen = 0; /* endless */
824
825 if( ( ret = asn1_get_tag( p, end, &len,
826 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000827 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000828
829 if( *p == end )
830 return 0;
831
Paul Bakker3cccddb2011-01-16 21:46:31 +0000832 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000833 {
834 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000835 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000836
837 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000839
Paul Bakker3cccddb2011-01-16 21:46:31 +0000840 if( *ca_istrue != 0 )
841 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000842 }
843
844 if( *p == end )
845 return 0;
846
847 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000849
850 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000852 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
853
854 (*max_pathlen)++;
855
Paul Bakker74111d32011-01-15 16:57:55 +0000856 return 0;
857}
858
859static int x509_get_ns_cert_type( unsigned char **p,
860 const unsigned char *end,
861 unsigned char *ns_cert_type)
862{
863 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000864 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000865
866 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000868
869 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000871 POLARSSL_ERR_ASN1_INVALID_LENGTH );
872
873 /* Get actual bitstring */
874 *ns_cert_type = *bs.p;
875 return 0;
876}
877
878static int x509_get_key_usage( unsigned char **p,
879 const unsigned char *end,
880 unsigned char *key_usage)
881{
882 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000883 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000884
885 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000887
Paul Bakker94a67962012-08-23 13:03:52 +0000888 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000890 POLARSSL_ERR_ASN1_INVALID_LENGTH );
891
892 /* Get actual bitstring */
893 *key_usage = *bs.p;
894 return 0;
895}
896
Paul Bakkerd98030e2009-05-02 15:13:40 +0000897/*
Paul Bakker74111d32011-01-15 16:57:55 +0000898 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
899 *
900 * KeyPurposeId ::= OBJECT IDENTIFIER
901 */
902static int x509_get_ext_key_usage( unsigned char **p,
903 const unsigned char *end,
904 x509_sequence *ext_key_usage)
905{
906 int ret;
907
908 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000910
911 /* Sequence length must be >= 1 */
912 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000913 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000914 POLARSSL_ERR_ASN1_INVALID_LENGTH );
915
916 return 0;
917}
918
919/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000920 * SubjectAltName ::= GeneralNames
921 *
922 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
923 *
924 * GeneralName ::= CHOICE {
925 * otherName [0] OtherName,
926 * rfc822Name [1] IA5String,
927 * dNSName [2] IA5String,
928 * x400Address [3] ORAddress,
929 * directoryName [4] Name,
930 * ediPartyName [5] EDIPartyName,
931 * uniformResourceIdentifier [6] IA5String,
932 * iPAddress [7] OCTET STRING,
933 * registeredID [8] OBJECT IDENTIFIER }
934 *
935 * OtherName ::= SEQUENCE {
936 * type-id OBJECT IDENTIFIER,
937 * value [0] EXPLICIT ANY DEFINED BY type-id }
938 *
939 * EDIPartyName ::= SEQUENCE {
940 * nameAssigner [0] DirectoryString OPTIONAL,
941 * partyName [1] DirectoryString }
942 *
943 * NOTE: PolarSSL only parses and uses dNSName at this point.
944 */
945static int x509_get_subject_alt_name( unsigned char **p,
946 const unsigned char *end,
947 x509_sequence *subject_alt_name )
948{
949 int ret;
950 size_t len, tag_len;
951 asn1_buf *buf;
952 unsigned char tag;
953 asn1_sequence *cur = subject_alt_name;
954
955 /* Get main sequence tag */
956 if( ( ret = asn1_get_tag( p, end, &len,
957 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
958 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
959
960 if( *p + len != end )
961 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
962 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
963
964 while( *p < end )
965 {
966 if( ( end - *p ) < 1 )
967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
968 POLARSSL_ERR_ASN1_OUT_OF_DATA );
969
970 tag = **p;
971 (*p)++;
972 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
973 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
974
975 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
976 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
977 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
978
979 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
980 {
981 *p += tag_len;
982 continue;
983 }
984
985 buf = &(cur->buf);
986 buf->tag = tag;
987 buf->p = *p;
988 buf->len = tag_len;
989 *p += buf->len;
990
991 /* Allocate and assign next pointer */
992 if (*p < end)
993 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200994 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000995 sizeof( asn1_sequence ) );
996
997 if( cur->next == NULL )
998 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
999 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1000
Paul Bakker535e97d2012-08-23 10:49:55 +00001001 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001002 cur = cur->next;
1003 }
1004 }
1005
1006 /* Set final sequence entry's next pointer to NULL */
1007 cur->next = NULL;
1008
1009 if( *p != end )
1010 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1011 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1012
1013 return( 0 );
1014}
1015
1016/*
Paul Bakker74111d32011-01-15 16:57:55 +00001017 * X.509 v3 extensions
1018 *
1019 * TODO: Perform all of the basic constraints tests required by the RFC
1020 * TODO: Set values for undetected extensions to a sane default?
1021 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001022 */
1023static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001024 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001025 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001026{
Paul Bakker23986e52011-04-24 08:57:21 +00001027 int ret;
1028 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001029 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001030
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001031 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001032 {
1033 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1034 return( 0 );
1035
1036 return( ret );
1037 }
1038
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 while( *p < end )
1040 {
Paul Bakker74111d32011-01-15 16:57:55 +00001041 /*
1042 * Extension ::= SEQUENCE {
1043 * extnID OBJECT IDENTIFIER,
1044 * critical BOOLEAN DEFAULT FALSE,
1045 * extnValue OCTET STRING }
1046 */
1047 x509_buf extn_oid = {0, 0, NULL};
1048 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001049 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001050
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 if( ( ret = asn1_get_tag( p, end, &len,
1052 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001053 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001055 end_ext_data = *p + len;
1056
Paul Bakker74111d32011-01-15 16:57:55 +00001057 /* Get extension ID */
1058 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Paul Bakker74111d32011-01-15 16:57:55 +00001060 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001061 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Paul Bakker74111d32011-01-15 16:57:55 +00001063 extn_oid.p = *p;
1064 *p += extn_oid.len;
1065
1066 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001067 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001068 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1069
1070 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001072 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001073 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakker74111d32011-01-15 16:57:55 +00001075 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001076 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001080 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001081
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001082 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001083 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001084 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Paul Bakker74111d32011-01-15 16:57:55 +00001086 /*
1087 * Detect supported extensions
1088 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001089 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1090
1091 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001092 {
1093 /* No parser found, skip extension */
1094 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker5c721f92011-07-27 16:51:09 +00001096#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001097 if( is_critical )
1098 {
1099 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001100 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001101 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1102 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001103#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001104 continue;
1105 }
1106
1107 crt->ext_types |= ext_type;
1108
1109 switch( ext_type )
1110 {
1111 case EXT_BASIC_CONSTRAINTS:
1112 /* Parse basic constraints */
1113 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1114 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1115 return ( ret );
1116 break;
1117
1118 case EXT_KEY_USAGE:
1119 /* Parse key usage */
1120 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1121 &crt->key_usage ) ) != 0 )
1122 return ( ret );
1123 break;
1124
1125 case EXT_EXTENDED_KEY_USAGE:
1126 /* Parse extended key usage */
1127 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1128 &crt->ext_key_usage ) ) != 0 )
1129 return ( ret );
1130 break;
1131
1132 case EXT_SUBJECT_ALT_NAME:
1133 /* Parse subject alt name */
1134 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1135 &crt->subject_alt_names ) ) != 0 )
1136 return ( ret );
1137 break;
1138
1139 case EXT_NS_CERT_TYPE:
1140 /* Parse netscape certificate type */
1141 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1142 &crt->ns_cert_type ) ) != 0 )
1143 return ( ret );
1144 break;
1145
1146 default:
1147 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 }
1150
1151 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001152 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001153 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 return( 0 );
1156}
1157
1158/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001159 * X.509 CRL Entries
1160 */
1161static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001162 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001163 x509_crl_entry *entry )
1164{
Paul Bakker23986e52011-04-24 08:57:21 +00001165 int ret;
1166 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001167 x509_crl_entry *cur_entry = entry;
1168
1169 if( *p == end )
1170 return( 0 );
1171
Paul Bakker9be19372009-07-27 20:21:53 +00001172 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001173 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1174 {
1175 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1176 return( 0 );
1177
1178 return( ret );
1179 }
1180
Paul Bakker9be19372009-07-27 20:21:53 +00001181 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182
1183 while( *p < end )
1184 {
Paul Bakker23986e52011-04-24 08:57:21 +00001185 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001186 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001187
1188 if( ( ret = asn1_get_tag( p, end, &len2,
1189 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1190 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001191 return( ret );
1192 }
1193
Paul Bakker9be19372009-07-27 20:21:53 +00001194 cur_entry->raw.tag = **p;
1195 cur_entry->raw.p = *p;
1196 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001197 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001198
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001199 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001200 return( ret );
1201
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001202 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001203 return( ret );
1204
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001205 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001206 return( ret );
1207
Paul Bakker74111d32011-01-15 16:57:55 +00001208 if ( *p < end )
1209 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001210 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001211
1212 if( cur_entry->next == NULL )
1213 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1214
Paul Bakkerd98030e2009-05-02 15:13:40 +00001215 cur_entry = cur_entry->next;
1216 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1217 }
1218 }
1219
1220 return( 0 );
1221}
1222
Paul Bakkerc70b9822013-04-07 22:00:46 +02001223static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1224 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001225{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001227
Paul Bakkerc70b9822013-04-07 22:00:46 +02001228 if( ret != 0 )
1229 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001230
Paul Bakkerc70b9822013-04-07 22:00:46 +02001231 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001232}
1233
Paul Bakkerd98030e2009-05-02 15:13:40 +00001234/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001235 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001237static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1238 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001239{
Paul Bakker23986e52011-04-24 08:57:21 +00001240 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001241 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001242 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
Paul Bakker320a4b52009-03-28 18:52:39 +00001244 /*
1245 * Check for valid input
1246 */
1247 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001248 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001249
Paul Bakker6e339b52013-07-03 13:37:05 +02001250 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001251
1252 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001253 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001254
1255 memcpy( p, buf, buflen );
1256
1257 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
1259 crt->raw.p = p;
1260 crt->raw.len = len;
1261 end = p + len;
1262
1263 /*
1264 * Certificate ::= SEQUENCE {
1265 * tbsCertificate TBSCertificate,
1266 * signatureAlgorithm AlgorithmIdentifier,
1267 * signatureValue BIT STRING }
1268 */
1269 if( ( ret = asn1_get_tag( &p, end, &len,
1270 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1271 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001272 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001273 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Paul Bakkerb00ca422012-09-25 12:10:00 +00001276 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001278 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001279 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001280 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001282 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001283
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 /*
1285 * TBSCertificate ::= SEQUENCE {
1286 */
1287 crt->tbs.p = p;
1288
1289 if( ( ret = asn1_get_tag( &p, end, &len,
1290 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1291 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001292 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001293 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 }
1295
1296 end = p + len;
1297 crt->tbs.len = end - crt->tbs.p;
1298
1299 /*
1300 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1301 *
1302 * CertificateSerialNumber ::= INTEGER
1303 *
1304 * signature AlgorithmIdentifier
1305 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001306 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1307 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1308 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001310 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 return( ret );
1312 }
1313
1314 crt->version++;
1315
1316 if( crt->version > 3 )
1317 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001318 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001319 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
1321
Paul Bakkerc70b9822013-04-07 22:00:46 +02001322 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1323 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001325 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001326 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328
1329 /*
1330 * issuer Name
1331 */
1332 crt->issuer_raw.p = p;
1333
1334 if( ( ret = asn1_get_tag( &p, end, &len,
1335 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1336 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001337 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001338 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
1340
1341 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 return( ret );
1345 }
1346
1347 crt->issuer_raw.len = p - crt->issuer_raw.p;
1348
1349 /*
1350 * Validity ::= SEQUENCE {
1351 * notBefore Time,
1352 * notAfter Time }
1353 *
1354 */
1355 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1356 &crt->valid_to ) ) != 0 )
1357 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001358 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 return( ret );
1360 }
1361
1362 /*
1363 * subject Name
1364 */
1365 crt->subject_raw.p = p;
1366
1367 if( ( ret = asn1_get_tag( &p, end, &len,
1368 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1369 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001370 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001371 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 }
1373
Paul Bakkercefb3962012-06-27 11:51:09 +00001374 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 return( ret );
1378 }
1379
1380 crt->subject_raw.len = p - crt->subject_raw.p;
1381
1382 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001383 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001385 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001387 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 return( ret );
1389 }
1390
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 /*
1392 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1393 * -- If present, version shall be v2 or v3
1394 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1395 * -- If present, version shall be v2 or v3
1396 * extensions [3] EXPLICIT Extensions OPTIONAL
1397 * -- If present, version shall be v3
1398 */
1399 if( crt->version == 2 || crt->version == 3 )
1400 {
1401 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1402 if( ret != 0 )
1403 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001404 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 return( ret );
1406 }
1407 }
1408
1409 if( crt->version == 2 || crt->version == 3 )
1410 {
1411 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1412 if( ret != 0 )
1413 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001414 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 return( ret );
1416 }
1417 }
1418
1419 if( crt->version == 3 )
1420 {
Paul Bakker74111d32011-01-15 16:57:55 +00001421 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 if( ret != 0 )
1423 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001424 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 return( ret );
1426 }
1427 }
1428
1429 if( p != end )
1430 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001431 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001432 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001433 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 }
1435
Paul Bakkerb00ca422012-09-25 12:10:00 +00001436 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
1438 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001439 * }
1440 * -- end of TBSCertificate
1441 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 * signatureAlgorithm AlgorithmIdentifier,
1443 * signatureValue BIT STRING
1444 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001445 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001447 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 return( ret );
1449 }
1450
Paul Bakker535e97d2012-08-23 10:49:55 +00001451 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1452 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001455 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
1457
1458 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1459 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001460 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 return( ret );
1462 }
1463
1464 if( p != end )
1465 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001466 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001467 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001468 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001471 return( 0 );
1472}
1473
1474/*
Paul Bakker42c65812013-06-24 19:21:59 +02001475 * Parse one X.509 certificate in DER format from a buffer and add them to a
1476 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477 */
Paul Bakker42c65812013-06-24 19:21:59 +02001478int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479{
Paul Bakker42c65812013-06-24 19:21:59 +02001480 int ret;
1481 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001482
1483 /*
1484 * Check for valid input
1485 */
1486 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001487 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001488
1489 while( crt->version != 0 && crt->next != NULL )
1490 {
1491 prev = crt;
1492 crt = crt->next;
1493 }
1494
1495 /*
1496 * Add new certificate on the end of the chain if needed.
1497 */
1498 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001499 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001500 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001501
Paul Bakker7d06ad22009-05-02 15:53:56 +00001502 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001503 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001504
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001505 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001506 crt = crt->next;
1507 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
Paul Bakker42c65812013-06-24 19:21:59 +02001510 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1511 {
1512 if( prev )
1513 prev->next = NULL;
1514
1515 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001516 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001517
1518 return( ret );
1519 }
1520
1521 return( 0 );
1522}
1523
1524/*
1525 * Parse one or more PEM certificates from a buffer and add them to the chained list
1526 */
1527int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1528{
1529 int ret, success = 0, first_error = 0, total_failed = 0;
1530 int buf_format = X509_FORMAT_DER;
1531
1532 /*
1533 * Check for valid input
1534 */
1535 if( chain == NULL || buf == NULL )
1536 return( POLARSSL_ERR_X509_INVALID_INPUT );
1537
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001538 /*
1539 * Determine buffer content. Buffer contains either one DER certificate or
1540 * one or more PEM certificates.
1541 */
1542#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001543 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544 buf_format = X509_FORMAT_PEM;
1545#endif
1546
1547 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001548 return x509parse_crt_der( chain, buf, buflen );
1549
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001550#if defined(POLARSSL_PEM_C)
1551 if( buf_format == X509_FORMAT_PEM )
1552 {
1553 pem_context pem;
1554
1555 while( buflen > 0 )
1556 {
1557 size_t use_len;
1558 pem_init( &pem );
1559
1560 ret = pem_read_buffer( &pem,
1561 "-----BEGIN CERTIFICATE-----",
1562 "-----END CERTIFICATE-----",
1563 buf, NULL, 0, &use_len );
1564
1565 if( ret == 0 )
1566 {
1567 /*
1568 * Was PEM encoded
1569 */
1570 buflen -= use_len;
1571 buf += use_len;
1572 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001573 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1574 {
1575 return( ret );
1576 }
Paul Bakker00b28602013-06-24 13:02:41 +02001577 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001578 {
1579 pem_free( &pem );
1580
Paul Bakker5ed3b342013-06-24 19:05:46 +02001581 /*
1582 * PEM header and footer were found
1583 */
1584 buflen -= use_len;
1585 buf += use_len;
1586
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001587 if( first_error == 0 )
1588 first_error = ret;
1589
1590 continue;
1591 }
1592 else
1593 break;
1594
Paul Bakker42c65812013-06-24 19:21:59 +02001595 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001596
1597 pem_free( &pem );
1598
1599 if( ret != 0 )
1600 {
1601 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001602 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001603 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001604 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001605 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606
1607 if( first_error == 0 )
1608 first_error = ret;
1609
Paul Bakker42c65812013-06-24 19:21:59 +02001610 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001611 continue;
1612 }
1613
1614 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001615 }
1616 }
1617#endif
1618
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001619 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001620 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001621 else if( first_error )
1622 return( first_error );
1623 else
1624 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625}
1626
1627/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001628 * Parse one or more CRLs and add them to the chained list
1629 */
Paul Bakker23986e52011-04-24 08:57:21 +00001630int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631{
Paul Bakker23986e52011-04-24 08:57:21 +00001632 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001633 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 unsigned char *p, *end;
1635 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001636#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001637 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001638 pem_context pem;
1639#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001640
1641 crl = chain;
1642
1643 /*
1644 * Check for valid input
1645 */
1646 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001647 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648
1649 while( crl->version != 0 && crl->next != NULL )
1650 crl = crl->next;
1651
1652 /*
1653 * Add new CRL on the end of the chain if needed.
1654 */
1655 if ( crl->version != 0 && crl->next == NULL)
1656 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001657 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658
Paul Bakker7d06ad22009-05-02 15:53:56 +00001659 if( crl->next == NULL )
1660 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001661 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001662 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001663 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664
Paul Bakker7d06ad22009-05-02 15:53:56 +00001665 crl = crl->next;
1666 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001667 }
1668
Paul Bakker96743fc2011-02-12 14:30:57 +00001669#if defined(POLARSSL_PEM_C)
1670 pem_init( &pem );
1671 ret = pem_read_buffer( &pem,
1672 "-----BEGIN X509 CRL-----",
1673 "-----END X509 CRL-----",
1674 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001675
Paul Bakker96743fc2011-02-12 14:30:57 +00001676 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001677 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001678 /*
1679 * Was PEM encoded
1680 */
1681 buflen -= use_len;
1682 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683
1684 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001685 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001686 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001687 p = pem.buf;
1688 pem.buf = NULL;
1689 len = pem.buflen;
1690 pem_free( &pem );
1691 }
Paul Bakker00b28602013-06-24 13:02:41 +02001692 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001693 {
1694 pem_free( &pem );
1695 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001696 }
1697 else
1698 {
1699 /*
1700 * nope, copy the raw DER data
1701 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001702 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001703
1704 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001705 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001706
1707 memcpy( p, buf, buflen );
1708
1709 buflen = 0;
1710 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001711#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001712 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001713
1714 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001715 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001716
1717 memcpy( p, buf, buflen );
1718
1719 buflen = 0;
1720#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721
1722 crl->raw.p = p;
1723 crl->raw.len = len;
1724 end = p + len;
1725
1726 /*
1727 * CertificateList ::= SEQUENCE {
1728 * tbsCertList TBSCertList,
1729 * signatureAlgorithm AlgorithmIdentifier,
1730 * signatureValue BIT STRING }
1731 */
1732 if( ( ret = asn1_get_tag( &p, end, &len,
1733 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1734 {
1735 x509_crl_free( crl );
1736 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1737 }
1738
Paul Bakker23986e52011-04-24 08:57:21 +00001739 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740 {
1741 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001742 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001743 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1744 }
1745
1746 /*
1747 * TBSCertList ::= SEQUENCE {
1748 */
1749 crl->tbs.p = p;
1750
1751 if( ( ret = asn1_get_tag( &p, end, &len,
1752 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1753 {
1754 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001755 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756 }
1757
1758 end = p + len;
1759 crl->tbs.len = end - crl->tbs.p;
1760
1761 /*
1762 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1763 * -- if present, MUST be v2
1764 *
1765 * signature AlgorithmIdentifier
1766 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001767 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001768 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769 {
1770 x509_crl_free( crl );
1771 return( ret );
1772 }
1773
1774 crl->version++;
1775
1776 if( crl->version > 2 )
1777 {
1778 x509_crl_free( crl );
1779 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1780 }
1781
Paul Bakkerc70b9822013-04-07 22:00:46 +02001782 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1783 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 {
1785 x509_crl_free( crl );
1786 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1787 }
1788
1789 /*
1790 * issuer Name
1791 */
1792 crl->issuer_raw.p = p;
1793
1794 if( ( ret = asn1_get_tag( &p, end, &len,
1795 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1796 {
1797 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001798 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799 }
1800
1801 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1802 {
1803 x509_crl_free( crl );
1804 return( ret );
1805 }
1806
1807 crl->issuer_raw.len = p - crl->issuer_raw.p;
1808
1809 /*
1810 * thisUpdate Time
1811 * nextUpdate Time OPTIONAL
1812 */
Paul Bakker91200182010-02-18 21:26:15 +00001813 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 {
1815 x509_crl_free( crl );
1816 return( ret );
1817 }
1818
Paul Bakker91200182010-02-18 21:26:15 +00001819 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 {
Paul Bakker9d781402011-05-09 16:17:09 +00001821 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001822 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001823 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001824 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001825 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001826 x509_crl_free( crl );
1827 return( ret );
1828 }
1829 }
1830
1831 /*
1832 * revokedCertificates SEQUENCE OF SEQUENCE {
1833 * userCertificate CertificateSerialNumber,
1834 * revocationDate Time,
1835 * crlEntryExtensions Extensions OPTIONAL
1836 * -- if present, MUST be v2
1837 * } OPTIONAL
1838 */
1839 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1840 {
1841 x509_crl_free( crl );
1842 return( ret );
1843 }
1844
1845 /*
1846 * crlExtensions EXPLICIT Extensions OPTIONAL
1847 * -- if present, MUST be v2
1848 */
1849 if( crl->version == 2 )
1850 {
1851 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1852
1853 if( ret != 0 )
1854 {
1855 x509_crl_free( crl );
1856 return( ret );
1857 }
1858 }
1859
1860 if( p != end )
1861 {
1862 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001863 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001864 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1865 }
1866
1867 end = crl->raw.p + crl->raw.len;
1868
1869 /*
1870 * signatureAlgorithm AlgorithmIdentifier,
1871 * signatureValue BIT STRING
1872 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001873 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 {
1875 x509_crl_free( crl );
1876 return( ret );
1877 }
1878
Paul Bakker535e97d2012-08-23 10:49:55 +00001879 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1880 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001881 {
1882 x509_crl_free( crl );
1883 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1884 }
1885
1886 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1887 {
1888 x509_crl_free( crl );
1889 return( ret );
1890 }
1891
1892 if( p != end )
1893 {
1894 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001895 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001896 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1897 }
1898
1899 if( buflen > 0 )
1900 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001901 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902
Paul Bakker7d06ad22009-05-02 15:53:56 +00001903 if( crl->next == NULL )
1904 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001905 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001906 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001907 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908
Paul Bakker7d06ad22009-05-02 15:53:56 +00001909 crl = crl->next;
1910 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001911
1912 return( x509parse_crl( crl, buf, buflen ) );
1913 }
1914
1915 return( 0 );
1916}
1917
Paul Bakker335db3f2011-04-25 15:28:35 +00001918#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001919/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001920 * Load all data from a file into a given buffer.
1921 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001922static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001923{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001924 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001925 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001926
Paul Bakkerd98030e2009-05-02 15:13:40 +00001927 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001928 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001929
Paul Bakkerd98030e2009-05-02 15:13:40 +00001930 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001931 if( ( size = ftell( f ) ) == -1 )
1932 {
1933 fclose( f );
1934 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1935 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001936 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001937
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001938 *n = (size_t) size;
1939
Paul Bakker694d3ae2013-08-19 14:23:38 +02001940 if( *n + 1 == 0 ||
1941 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001942 {
1943 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001944 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001945 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001946
Paul Bakkerd98030e2009-05-02 15:13:40 +00001947 if( fread( *buf, 1, *n, f ) != *n )
1948 {
1949 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001950 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001951 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001953
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001955
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001957
Paul Bakkerd98030e2009-05-02 15:13:40 +00001958 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001959}
1960
1961/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 * Load one or more certificates and add them to the chained list
1963 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001964int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001965{
1966 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 size_t n;
1968 unsigned char *buf;
1969
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001970 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001971 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
Paul Bakker69e095c2011-12-10 21:55:01 +00001973 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
1975 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001976 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
1978 return( ret );
1979}
1980
Paul Bakker8d914582012-06-04 12:46:42 +00001981int x509parse_crtpath( x509_cert *chain, const char *path )
1982{
1983 int ret = 0;
1984#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001985 int w_ret;
1986 WCHAR szDir[MAX_PATH];
1987 char filename[MAX_PATH];
1988 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001989 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001990
Paul Bakker97872ac2012-11-02 12:53:26 +00001991 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001992 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001993
1994 if( len > MAX_PATH - 3 )
1995 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001996
Paul Bakker3338b792012-10-01 21:13:10 +00001997 memset( szDir, 0, sizeof(szDir) );
1998 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001999 memcpy( filename, path, len );
2000 filename[len++] = '\\';
2001 p = filename + len;
2002 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002003
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002004 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002005
Paul Bakker97872ac2012-11-02 12:53:26 +00002006 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002007 if (hFind == INVALID_HANDLE_VALUE)
2008 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2009
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002010 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002011 do
2012 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002013 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002014
Paul Bakkere4791f32012-06-04 21:29:15 +00002015 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002016 continue;
2017
Paul Bakker3338b792012-10-01 21:13:10 +00002018 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2019 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002020 p, len - 1,
2021 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002022
Paul Bakker3338b792012-10-01 21:13:10 +00002023 w_ret = x509parse_crtfile( chain, filename );
2024 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002025 ret++;
2026 else
2027 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002028 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002029 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002030
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002031 if (GetLastError() != ERROR_NO_MORE_FILES)
2032 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002033
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002034cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002035 FindClose( hFind );
2036#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002037 int t_ret, i;
2038 struct stat sb;
2039 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002040 char entry_name[255];
2041 DIR *dir = opendir( path );
2042
2043 if( dir == NULL)
2044 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2045
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002046 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002047 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002048 if( result == NULL )
2049 break;
2050
2051 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2052
2053 i = stat( entry_name, &sb );
2054
2055 if( i == -1 )
2056 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2057
2058 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002059 continue;
2060
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002061 // Ignore parse errors
2062 //
Paul Bakker8d914582012-06-04 12:46:42 +00002063 t_ret = x509parse_crtfile( chain, entry_name );
2064 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002065 ret++;
2066 else
2067 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002068 }
2069 closedir( dir );
2070#endif
2071
2072 return( ret );
2073}
2074
Paul Bakkerd98030e2009-05-02 15:13:40 +00002075/*
2076 * Load one or more CRLs and add them to the chained list
2077 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002078int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002079{
2080 int ret;
2081 size_t n;
2082 unsigned char *buf;
2083
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002084 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002085 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002086
Paul Bakker27fdf462011-06-09 13:55:13 +00002087 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002088
2089 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002090 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002091
2092 return( ret );
2093}
2094
Paul Bakker5121ce52009-01-03 21:22:43 +00002095/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002096 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002097 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002098int x509parse_keyfile( pk_context *ctx,
2099 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002100{
2101 int ret;
2102 size_t n;
2103 unsigned char *buf;
2104
2105 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2106 return( ret );
2107
2108 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002109 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002110 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002111 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002112 (const unsigned char *) pwd, strlen( pwd ) );
2113
2114 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002115 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002116
2117 return( ret );
2118}
2119
2120/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002121 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002122 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002123int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002124{
2125 int ret;
2126 size_t n;
2127 unsigned char *buf;
2128
2129 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2130 return( ret );
2131
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002132 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002133
2134 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002135 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002136
2137 return( ret );
2138}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002139
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002140#if defined(POLARSSL_RSA_C)
2141/*
2142 * Load and parse a private RSA key
2143 */
2144int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2145{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002146 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002147 pk_context pk;
2148
2149 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002150
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002151 ret = x509parse_keyfile( &pk, path, pwd );
2152
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002153 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2154 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2155
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002156 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002157 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002158 else
2159 rsa_free( rsa );
2160
2161 pk_free( &pk );
2162
2163 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002164}
2165
2166/*
2167 * Load and parse a public RSA key
2168 */
2169int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2170{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002171 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002172 pk_context pk;
2173
2174 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002175
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002176 ret = x509parse_public_keyfile( &pk, path );
2177
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002178 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2179 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2180
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002181 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002182 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002183 else
2184 rsa_free( rsa );
2185
2186 pk_free( &pk );
2187
2188 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002189}
2190#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002191#endif /* POLARSSL_FS_IO */
2192
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002193#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002194/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002195 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002197static int x509parse_key_pkcs1_der( rsa_context *rsa,
2198 const unsigned char *key,
2199 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002200{
Paul Bakker23986e52011-04-24 08:57:21 +00002201 int ret;
2202 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002204
Paul Bakker96743fc2011-02-12 14:30:57 +00002205 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002206 end = p + keylen;
2207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002209 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002210 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 * RSAPrivateKey ::= SEQUENCE {
2212 * version Version,
2213 * modulus INTEGER, -- n
2214 * publicExponent INTEGER, -- e
2215 * privateExponent INTEGER, -- d
2216 * prime1 INTEGER, -- p
2217 * prime2 INTEGER, -- q
2218 * exponent1 INTEGER, -- d mod (p-1)
2219 * exponent2 INTEGER, -- d mod (q-1)
2220 * coefficient INTEGER, -- (inverse of q) mod p
2221 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2222 * }
2223 */
2224 if( ( ret = asn1_get_tag( &p, end, &len,
2225 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2226 {
Paul Bakker9d781402011-05-09 16:17:09 +00002227 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228 }
2229
2230 end = p + len;
2231
2232 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2233 {
Paul Bakker9d781402011-05-09 16:17:09 +00002234 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236
2237 if( rsa->ver != 0 )
2238 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002239 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
2241
2242 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2243 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2244 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2245 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2246 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2247 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2248 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2249 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2250 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002252 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
2255 rsa->len = mpi_size( &rsa->N );
2256
2257 if( p != end )
2258 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002260 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002261 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263
2264 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2265 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 rsa_free( rsa );
2267 return( ret );
2268 }
2269
Paul Bakkere2f50402013-06-24 19:00:59 +02002270 return( 0 );
2271}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002272#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002273
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002274#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002275/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002276 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002277 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002278static int x509parse_key_sec1_der( ecp_keypair *eck,
2279 const unsigned char *key,
2280 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002281{
2282 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002283 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002284 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002285 x509_buf params;
2286 unsigned char *p = (unsigned char *) key;
2287 unsigned char *end = p + keylen;
2288 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002289
2290 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002291 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002292 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002293 * ECPrivateKey ::= SEQUENCE {
2294 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2295 * privateKey OCTET STRING,
2296 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2297 * publicKey [1] BIT STRING OPTIONAL
2298 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002299 */
2300 if( ( ret = asn1_get_tag( &p, end, &len,
2301 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2302 {
2303 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2304 }
2305
2306 end = p + len;
2307
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002308 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002309 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2310
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002311 if( version != 1 )
2312 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002313
Paul Bakkere2f50402013-06-24 19:00:59 +02002314 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2315 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2316
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002317 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002318 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002319 ecp_keypair_free( eck );
2320 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2321 }
2322
2323 p += len;
2324
2325 /*
2326 * Is 'parameters' present?
2327 */
2328 if( ( ret = asn1_get_tag( &p, end, &len,
2329 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2330 {
2331 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2332 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2333 {
2334 ecp_keypair_free( eck );
2335 return( ret );
2336 }
2337 }
2338 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2339 {
2340 ecp_keypair_free( eck );
2341 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2342 }
2343
2344 /*
2345 * Is 'publickey' present?
2346 */
2347 if( ( ret = asn1_get_tag( &p, end, &len,
2348 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2349 {
2350 end2 = p + len;
2351
2352 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2353 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2354
2355 if( p + len != end2 )
2356 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2357 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2358
2359 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2360 return( ret );
2361 }
2362 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2363 {
2364 ecp_keypair_free( eck );
2365 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2366 }
2367
2368 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2369 {
2370 ecp_keypair_free( eck );
2371 return( ret );
2372 }
2373
2374 return 0;
2375}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002376#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002377
2378/*
2379 * Parse an unencrypted PKCS#8 encoded private key
2380 */
2381static int x509parse_key_pkcs8_unencrypted_der(
2382 pk_context *pk,
2383 const unsigned char* key,
2384 size_t keylen )
2385{
2386 int ret, version;
2387 size_t len;
2388 x509_buf params;
2389 unsigned char *p = (unsigned char *) key;
2390 unsigned char *end = p + keylen;
2391 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002392 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002393
2394 /*
2395 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2396 *
2397 * PrivateKeyInfo ::= SEQUENCE {
2398 * version Version,
2399 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2400 * privateKey PrivateKey,
2401 * attributes [0] IMPLICIT Attributes OPTIONAL }
2402 *
2403 * Version ::= INTEGER
2404 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2405 * PrivateKey ::= OCTET STRING
2406 *
2407 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2408 */
2409
2410 if( ( ret = asn1_get_tag( &p, end, &len,
2411 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2412 {
2413 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002414 }
2415
2416 end = p + len;
2417
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002418 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2419 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2420
2421 if( version != 0 )
2422 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2423
2424 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2425 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2426
2427 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2428 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2429
2430 if( len < 1 )
2431 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2433
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002434 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2435 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2436
2437 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002438 return( ret );
2439
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002440#if defined(POLARSSL_RSA_C)
2441 if( pk_alg == POLARSSL_PK_RSA )
2442 {
2443 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2444 {
2445 pk_free( pk );
2446 return( ret );
2447 }
2448 } else
2449#endif /* POLARSSL_RSA_C */
2450#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002451 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2452 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002453 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2454 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2455 {
2456 pk_free( pk );
2457 return( ret );
2458 }
2459 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002460#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002461 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2462
2463 return 0;
2464}
2465
2466/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002467 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002468 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002469static int x509parse_key_pkcs8_encrypted_der(
2470 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002471 const unsigned char *key, size_t keylen,
2472 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002473{
2474 int ret;
2475 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002476 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002477 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002478 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002479#if defined(POLARSSL_PKCS12_C)
2480 cipher_type_t cipher_alg;
2481 md_type_t md_alg;
2482#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002484 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002485
2486 p = (unsigned char *) key;
2487 end = p + keylen;
2488
Paul Bakker28144de2013-06-24 19:28:55 +02002489 if( pwdlen == 0 )
2490 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2491
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002492 /*
2493 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2494 *
2495 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2496 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2497 * encryptedData EncryptedData
2498 * }
2499 *
2500 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2501 *
2502 * EncryptedData ::= OCTET STRING
2503 *
2504 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2505 */
2506 if( ( ret = asn1_get_tag( &p, end, &len,
2507 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2508 {
2509 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2510 }
2511
2512 end = p + len;
2513
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002514 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002515 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002516
2517 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2518 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2519
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002520 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002521 return( POLARSSL_ERR_X509_INVALID_INPUT );
2522
2523 /*
2524 * Decrypt EncryptedData with appropriate PDE
2525 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002526#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002527 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002528 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002529 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002530 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002531 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002532 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002533 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2534 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2535
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002536 return( ret );
2537 }
2538 }
2539 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2540 {
2541 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2542 PKCS12_PBE_DECRYPT,
2543 pwd, pwdlen,
2544 p, len, buf ) ) != 0 )
2545 {
2546 return( ret );
2547 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002548
2549 // Best guess for password mismatch when using RC4. If first tag is
2550 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2551 //
2552 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2553 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002554 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002555 else
2556#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002557#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002558 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002559 {
2560 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2561 p, len, buf ) ) != 0 )
2562 {
2563 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2564 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2565
2566 return( ret );
2567 }
2568 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002569 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002570#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002571 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2572
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002573 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002574}
2575
2576/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002577 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002578 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002579int x509parse_key( pk_context *pk,
2580 const unsigned char *key, size_t keylen,
2581 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002582{
2583 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002584 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002585
2586#if defined(POLARSSL_PEM_C)
2587 size_t len;
2588 pem_context pem;
2589
2590 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002591
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002592#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002593 ret = pem_read_buffer( &pem,
2594 "-----BEGIN RSA PRIVATE KEY-----",
2595 "-----END RSA PRIVATE KEY-----",
2596 key, pwd, pwdlen, &len );
2597 if( ret == 0 )
2598 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002599 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2600 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2601
2602 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002603 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2604 pem.buf, pem.buflen ) ) != 0 )
2605 {
2606 pk_free( pk );
2607 }
2608
2609 pem_free( &pem );
2610 return( ret );
2611 }
2612 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2613 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2614 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2615 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2616 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2617 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002618#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002619
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002620#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002621 ret = pem_read_buffer( &pem,
2622 "-----BEGIN EC PRIVATE KEY-----",
2623 "-----END EC PRIVATE KEY-----",
2624 key, pwd, pwdlen, &len );
2625 if( ret == 0 )
2626 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002627 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2628 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2629
2630 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002631 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2632 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002633 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002634 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002635 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002636
2637 pem_free( &pem );
2638 return( ret );
2639 }
2640 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2641 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2642 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2643 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2644 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2645 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002646#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002647
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002648 ret = pem_read_buffer( &pem,
2649 "-----BEGIN PRIVATE KEY-----",
2650 "-----END PRIVATE KEY-----",
2651 key, NULL, 0, &len );
2652 if( ret == 0 )
2653 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002654 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002655 pem.buf, pem.buflen ) ) != 0 )
2656 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002657 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002658 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002659
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002660 pem_free( &pem );
2661 return( ret );
2662 }
2663 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2664 return( ret );
2665
2666 ret = pem_read_buffer( &pem,
2667 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2668 "-----END ENCRYPTED PRIVATE KEY-----",
2669 key, NULL, 0, &len );
2670 if( ret == 0 )
2671 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002672 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002673 pem.buf, pem.buflen,
2674 pwd, pwdlen ) ) != 0 )
2675 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002676 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002677 }
2678
2679 pem_free( &pem );
2680 return( ret );
2681 }
2682 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2683 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002684#else
2685 ((void) pwd);
2686 ((void) pwdlen);
2687#endif /* POLARSSL_PEM_C */
2688
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002689 /*
2690 * At this point we only know it's not a PEM formatted key. Could be any
2691 * of the known DER encoded private key formats
2692 *
2693 * We try the different DER format parsers to see if one passes without
2694 * error
2695 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002696 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2697 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002698 {
2699 return( 0 );
2700 }
2701
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002702 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002703
2704 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2705 {
2706 return( ret );
2707 }
2708
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002709 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002710 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002711
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002712 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002713
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002714#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002715 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2716 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2717
2718 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002719 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2720 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002721 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002722 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002723
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002724 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002725#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002726
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002727#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002728 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2729 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2730
2731 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002732 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2733 {
2734 return( 0 );
2735 }
2736
2737 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002738#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002739
2740 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2741}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002742
2743/*
2744 * Parse a public key
2745 */
2746int x509parse_public_key( pk_context *ctx,
2747 const unsigned char *key, size_t keylen )
2748{
2749 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002750 unsigned char *p;
2751#if defined(POLARSSL_PEM_C)
2752 size_t len;
2753 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002754
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002755 pem_init( &pem );
2756 ret = pem_read_buffer( &pem,
2757 "-----BEGIN PUBLIC KEY-----",
2758 "-----END PUBLIC KEY-----",
2759 key, NULL, 0, &len );
2760
2761 if( ret == 0 )
2762 {
2763 /*
2764 * Was PEM encoded
2765 */
2766 key = pem.buf;
2767 keylen = pem.buflen;
2768 }
2769 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2770 {
2771 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002772 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002773 }
2774#endif
2775 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002776
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002777 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002778
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002779#if defined(POLARSSL_PEM_C)
2780 pem_free( &pem );
2781#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002782
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002783 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002784}
2785
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002786#if defined(POLARSSL_RSA_C)
2787/*
2788 * Parse a private RSA key
2789 */
2790int x509parse_key_rsa( rsa_context *rsa,
2791 const unsigned char *key, size_t keylen,
2792 const unsigned char *pwd, size_t pwdlen )
2793{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002794 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002795 pk_context pk;
2796
2797 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002798
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002799 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
2800
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002801 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2802 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2803
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002804 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002805 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002806 else
2807 rsa_free( rsa );
2808
2809 pk_free( &pk );
2810
2811 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002812}
2813
2814/*
2815 * Parse a public RSA key
2816 */
2817int x509parse_public_key_rsa( rsa_context *rsa,
2818 const unsigned char *key, size_t keylen )
2819{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002820 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002821 pk_context pk;
2822
2823 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002824
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002825 ret = x509parse_public_key( &pk, key, keylen );
2826
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002827 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2828 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2829
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002830 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002831 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002832 else
2833 rsa_free( rsa );
2834
2835 pk_free( &pk );
2836
2837 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002838}
2839#endif /* POLARSSL_RSA_C */
2840
Paul Bakkereaa89f82011-04-04 21:36:15 +00002841#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002842/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002843 * Parse DHM parameters
2844 */
Paul Bakker23986e52011-04-24 08:57:21 +00002845int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002846{
Paul Bakker23986e52011-04-24 08:57:21 +00002847 int ret;
2848 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002849 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002850#if defined(POLARSSL_PEM_C)
2851 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002852
Paul Bakker96743fc2011-02-12 14:30:57 +00002853 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002854
Paul Bakker96743fc2011-02-12 14:30:57 +00002855 ret = pem_read_buffer( &pem,
2856 "-----BEGIN DH PARAMETERS-----",
2857 "-----END DH PARAMETERS-----",
2858 dhmin, NULL, 0, &dhminlen );
2859
2860 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002861 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002862 /*
2863 * Was PEM encoded
2864 */
2865 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002866 }
Paul Bakker00b28602013-06-24 13:02:41 +02002867 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002868 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002869 pem_free( &pem );
2870 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002871 }
2872
Paul Bakker96743fc2011-02-12 14:30:57 +00002873 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2874#else
2875 p = (unsigned char *) dhmin;
2876#endif
2877 end = p + dhminlen;
2878
Paul Bakker1b57b062011-01-06 15:48:19 +00002879 memset( dhm, 0, sizeof( dhm_context ) );
2880
Paul Bakker1b57b062011-01-06 15:48:19 +00002881 /*
2882 * DHParams ::= SEQUENCE {
2883 * prime INTEGER, -- P
2884 * generator INTEGER, -- g
2885 * }
2886 */
2887 if( ( ret = asn1_get_tag( &p, end, &len,
2888 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2889 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002890#if defined(POLARSSL_PEM_C)
2891 pem_free( &pem );
2892#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002893 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002894 }
2895
2896 end = p + len;
2897
2898 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2899 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2900 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002901#if defined(POLARSSL_PEM_C)
2902 pem_free( &pem );
2903#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002904 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002905 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002906 }
2907
2908 if( p != end )
2909 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002910#if defined(POLARSSL_PEM_C)
2911 pem_free( &pem );
2912#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002913 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002914 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002915 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2916 }
2917
Paul Bakker96743fc2011-02-12 14:30:57 +00002918#if defined(POLARSSL_PEM_C)
2919 pem_free( &pem );
2920#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002921
2922 return( 0 );
2923}
2924
Paul Bakker335db3f2011-04-25 15:28:35 +00002925#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002926/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002927 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002928 */
2929int x509parse_dhmfile( dhm_context *dhm, const char *path )
2930{
2931 int ret;
2932 size_t n;
2933 unsigned char *buf;
2934
Paul Bakker69e095c2011-12-10 21:55:01 +00002935 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2936 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002937
Paul Bakker27fdf462011-06-09 13:55:13 +00002938 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002939
2940 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002941 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002942
2943 return( ret );
2944}
Paul Bakker335db3f2011-04-25 15:28:35 +00002945#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002946#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002947
Paul Bakker5121ce52009-01-03 21:22:43 +00002948#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002949#include <stdarg.h>
2950
2951#if !defined vsnprintf
2952#define vsnprintf _vsnprintf
2953#endif // vsnprintf
2954
2955/*
2956 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2957 * Result value is not size of buffer needed, but -1 if no fit is possible.
2958 *
2959 * This fuction tries to 'fix' this by at least suggesting enlarging the
2960 * size by 20.
2961 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002962static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002963{
2964 va_list ap;
2965 int res = -1;
2966
2967 va_start( ap, format );
2968
2969 res = vsnprintf( str, size, format, ap );
2970
2971 va_end( ap );
2972
2973 // No quick fix possible
2974 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002975 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002976
2977 return res;
2978}
2979
2980#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002981#endif
2982
Paul Bakkerd98030e2009-05-02 15:13:40 +00002983#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2984
2985#define SAFE_SNPRINTF() \
2986{ \
2987 if( ret == -1 ) \
2988 return( -1 ); \
2989 \
Paul Bakker23986e52011-04-24 08:57:21 +00002990 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002991 p[n - 1] = '\0'; \
2992 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2993 } \
2994 \
Paul Bakker23986e52011-04-24 08:57:21 +00002995 n -= (unsigned int) ret; \
2996 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002997}
2998
Paul Bakker5121ce52009-01-03 21:22:43 +00002999/*
3000 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003001 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003003int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003004{
Paul Bakker23986e52011-04-24 08:57:21 +00003005 int ret;
3006 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003008 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003009 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 char s[128], *p;
3011
3012 memset( s, 0, sizeof( s ) );
3013
3014 name = dn;
3015 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003016 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
3018 while( name != NULL )
3019 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003020 if( !name->oid.p )
3021 {
3022 name = name->next;
3023 continue;
3024 }
3025
Paul Bakker74111d32011-01-15 16:57:55 +00003026 if( name != dn )
3027 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003028 ret = snprintf( p, n, ", " );
3029 SAFE_SNPRINTF();
3030 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Paul Bakkerc70b9822013-04-07 22:00:46 +02003032 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakkerc70b9822013-04-07 22:00:46 +02003034 if( ret == 0 )
3035 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003037 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003038 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
3040 for( i = 0; i < name->val.len; i++ )
3041 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003042 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 break;
3044
3045 c = name->val.p[i];
3046 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3047 s[i] = '?';
3048 else s[i] = c;
3049 }
3050 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003052 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 name = name->next;
3054 }
3055
Paul Bakker23986e52011-04-24 08:57:21 +00003056 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057}
3058
3059/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003060 * Store the serial in printable form into buf; no more
3061 * than size characters will be written
3062 */
3063int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3064{
Paul Bakker23986e52011-04-24 08:57:21 +00003065 int ret;
3066 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003067 char *p;
3068
3069 p = buf;
3070 n = size;
3071
3072 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003073 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003074
3075 for( i = 0; i < nr; i++ )
3076 {
Paul Bakker93048802011-12-05 14:38:06 +00003077 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003078 continue;
3079
Paul Bakkerdd476992011-01-16 21:34:59 +00003080 ret = snprintf( p, n, "%02X%s",
3081 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3082 SAFE_SNPRINTF();
3083 }
3084
Paul Bakker03c7c252011-11-25 12:37:37 +00003085 if( nr != serial->len )
3086 {
3087 ret = snprintf( p, n, "...." );
3088 SAFE_SNPRINTF();
3089 }
3090
Paul Bakker23986e52011-04-24 08:57:21 +00003091 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003092}
3093
3094/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003095 * Helper for writing "RSA key size", "EC key size", etc
3096 */
3097static int x509_key_size_helper( char *buf, size_t size, const char *name )
3098{
3099 char *p = buf;
3100 size_t n = size;
3101 int ret;
3102
3103 if( strlen( name ) + sizeof( " key size" ) > size )
3104 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3105
3106 ret = snprintf( p, n, "%s key size", name );
3107 SAFE_SNPRINTF();
3108
3109 return( 0 );
3110}
3111
3112/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003113 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003115#define BEFORE_COLON 14
3116#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003117int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3118 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003119{
Paul Bakker23986e52011-04-24 08:57:21 +00003120 int ret;
3121 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003123 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003124 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
3126 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003127 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Paul Bakkerd98030e2009-05-02 15:13:40 +00003129 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003131 SAFE_SNPRINTF();
3132 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003134 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Paul Bakkerdd476992011-01-16 21:34:59 +00003136 ret = x509parse_serial_gets( p, n, &crt->serial);
3137 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakkerd98030e2009-05-02 15:13:40 +00003139 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3140 SAFE_SNPRINTF();
3141 ret = x509parse_dn_gets( p, n, &crt->issuer );
3142 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakkerd98030e2009-05-02 15:13:40 +00003144 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3145 SAFE_SNPRINTF();
3146 ret = x509parse_dn_gets( p, n, &crt->subject );
3147 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Paul Bakkerd98030e2009-05-02 15:13:40 +00003149 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3151 crt->valid_from.year, crt->valid_from.mon,
3152 crt->valid_from.day, crt->valid_from.hour,
3153 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003154 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakkerd98030e2009-05-02 15:13:40 +00003156 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3158 crt->valid_to.year, crt->valid_to.mon,
3159 crt->valid_to.day, crt->valid_to.hour,
3160 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003161 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Paul Bakkerc70b9822013-04-07 22:00:46 +02003163 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003164 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
Paul Bakkerc70b9822013-04-07 22:00:46 +02003166 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3167 if( ret != 0 )
3168 ret = snprintf( p, n, "???" );
3169 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003170 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003171 SAFE_SNPRINTF();
3172
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003173 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003174 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003175 {
3176 return( ret );
3177 }
3178
3179 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003180 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003181 SAFE_SNPRINTF();
3182
Paul Bakker23986e52011-04-24 08:57:21 +00003183 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003184}
3185
Paul Bakker74111d32011-01-15 16:57:55 +00003186/*
3187 * Return an informational string describing the given OID
3188 */
3189const char *x509_oid_get_description( x509_buf *oid )
3190{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003191 const char *desc = NULL;
3192 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003193
Paul Bakkerc70b9822013-04-07 22:00:46 +02003194 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003195
Paul Bakkerc70b9822013-04-07 22:00:46 +02003196 if( ret != 0 )
3197 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003198
Paul Bakkerc70b9822013-04-07 22:00:46 +02003199 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003200}
3201
3202/* Return the x.y.z.... style numeric string for the given OID */
3203int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3204{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003205 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003206}
3207
Paul Bakkerd98030e2009-05-02 15:13:40 +00003208/*
3209 * Return an informational string about the CRL.
3210 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003211int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3212 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003213{
Paul Bakker23986e52011-04-24 08:57:21 +00003214 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003215 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003217 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003218 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003219
3220 p = buf;
3221 n = size;
3222
3223 ret = snprintf( p, n, "%sCRL version : %d",
3224 prefix, crl->version );
3225 SAFE_SNPRINTF();
3226
3227 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3228 SAFE_SNPRINTF();
3229 ret = x509parse_dn_gets( p, n, &crl->issuer );
3230 SAFE_SNPRINTF();
3231
3232 ret = snprintf( p, n, "\n%sthis update : " \
3233 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3234 crl->this_update.year, crl->this_update.mon,
3235 crl->this_update.day, crl->this_update.hour,
3236 crl->this_update.min, crl->this_update.sec );
3237 SAFE_SNPRINTF();
3238
3239 ret = snprintf( p, n, "\n%snext update : " \
3240 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3241 crl->next_update.year, crl->next_update.mon,
3242 crl->next_update.day, crl->next_update.hour,
3243 crl->next_update.min, crl->next_update.sec );
3244 SAFE_SNPRINTF();
3245
3246 entry = &crl->entry;
3247
3248 ret = snprintf( p, n, "\n%sRevoked certificates:",
3249 prefix );
3250 SAFE_SNPRINTF();
3251
Paul Bakker9be19372009-07-27 20:21:53 +00003252 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003253 {
3254 ret = snprintf( p, n, "\n%sserial number: ",
3255 prefix );
3256 SAFE_SNPRINTF();
3257
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003258 ret = x509parse_serial_gets( p, n, &entry->serial);
3259 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003260
Paul Bakkerd98030e2009-05-02 15:13:40 +00003261 ret = snprintf( p, n, " revocation date: " \
3262 "%04d-%02d-%02d %02d:%02d:%02d",
3263 entry->revocation_date.year, entry->revocation_date.mon,
3264 entry->revocation_date.day, entry->revocation_date.hour,
3265 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003266 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003267
3268 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 }
3270
Paul Bakkerc70b9822013-04-07 22:00:46 +02003271 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003272 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003273
Paul Bakkerc70b9822013-04-07 22:00:46 +02003274 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3275 if( ret != 0 )
3276 ret = snprintf( p, n, "???" );
3277 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003278 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003279 SAFE_SNPRINTF();
3280
Paul Bakker1e27bb22009-07-19 20:25:25 +00003281 ret = snprintf( p, n, "\n" );
3282 SAFE_SNPRINTF();
3283
Paul Bakker23986e52011-04-24 08:57:21 +00003284 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285}
3286
3287/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003288 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003290#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003291int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292{
Paul Bakkercce9d772011-11-18 14:26:47 +00003293 int year, mon, day;
3294 int hour, min, sec;
3295
3296#if defined(_WIN32)
3297 SYSTEMTIME st;
3298
3299 GetLocalTime(&st);
3300
3301 year = st.wYear;
3302 mon = st.wMonth;
3303 day = st.wDay;
3304 hour = st.wHour;
3305 min = st.wMinute;
3306 sec = st.wSecond;
3307#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 struct tm *lt;
3309 time_t tt;
3310
3311 tt = time( NULL );
3312 lt = localtime( &tt );
3313
Paul Bakkercce9d772011-11-18 14:26:47 +00003314 year = lt->tm_year + 1900;
3315 mon = lt->tm_mon + 1;
3316 day = lt->tm_mday;
3317 hour = lt->tm_hour;
3318 min = lt->tm_min;
3319 sec = lt->tm_sec;
3320#endif
3321
3322 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003323 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324
Paul Bakkercce9d772011-11-18 14:26:47 +00003325 if( year == to->year &&
3326 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003327 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328
Paul Bakkercce9d772011-11-18 14:26:47 +00003329 if( year == to->year &&
3330 mon == to->mon &&
3331 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003332 return( 1 );
3333
Paul Bakkercce9d772011-11-18 14:26:47 +00003334 if( year == to->year &&
3335 mon == to->mon &&
3336 day == to->day &&
3337 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003338 return( 1 );
3339
Paul Bakkercce9d772011-11-18 14:26:47 +00003340 if( year == to->year &&
3341 mon == to->mon &&
3342 day == to->day &&
3343 hour == to->hour &&
3344 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003345 return( 1 );
3346
Paul Bakkercce9d772011-11-18 14:26:47 +00003347 if( year == to->year &&
3348 mon == to->mon &&
3349 day == to->day &&
3350 hour == to->hour &&
3351 min == to->min &&
3352 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003353 return( 1 );
3354
Paul Bakker40ea7de2009-05-03 10:18:48 +00003355 return( 0 );
3356}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003357#else /* POLARSSL_HAVE_TIME */
3358int x509parse_time_expired( const x509_time *to )
3359{
3360 ((void) to);
3361 return( 0 );
3362}
3363#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003364
3365/*
3366 * Return 1 if the certificate is revoked, or 0 otherwise.
3367 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003368int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003369{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003370 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003371
3372 while( cur != NULL && cur->serial.len != 0 )
3373 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003374 if( crt->serial.len == cur->serial.len &&
3375 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003376 {
3377 if( x509parse_time_expired( &cur->revocation_date ) )
3378 return( 1 );
3379 }
3380
3381 cur = cur->next;
3382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003383
3384 return( 0 );
3385}
3386
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003387/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003388 * Check that the given certificate is valid accoring to the CRL.
3389 */
3390static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3391 x509_crl *crl_list)
3392{
3393 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003394 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3395 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003396
Paul Bakker915275b2012-09-28 07:10:55 +00003397 if( ca == NULL )
3398 return( flags );
3399
Paul Bakker76fd75a2011-01-16 21:12:10 +00003400 /*
3401 * TODO: What happens if no CRL is present?
3402 * Suggestion: Revocation state should be unknown if no CRL is present.
3403 * For backwards compatibility this is not yet implemented.
3404 */
3405
Paul Bakker915275b2012-09-28 07:10:55 +00003406 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003407 {
Paul Bakker915275b2012-09-28 07:10:55 +00003408 if( crl_list->version == 0 ||
3409 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003410 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3411 crl_list->issuer_raw.len ) != 0 )
3412 {
3413 crl_list = crl_list->next;
3414 continue;
3415 }
3416
3417 /*
3418 * Check if CRL is correctly signed by the trusted CA
3419 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003420 md_info = md_info_from_type( crl_list->sig_md );
3421 if( md_info == NULL )
3422 {
3423 /*
3424 * Cannot check 'unknown' hash
3425 */
3426 flags |= BADCRL_NOT_TRUSTED;
3427 break;
3428 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003429
Paul Bakkerc70b9822013-04-07 22:00:46 +02003430 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003431
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003432 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003433 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003434 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003435 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003436 flags |= BADCRL_NOT_TRUSTED;
3437 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003438 }
3439
3440 /*
3441 * Check for validity of CRL (Do not drop out)
3442 */
3443 if( x509parse_time_expired( &crl_list->next_update ) )
3444 flags |= BADCRL_EXPIRED;
3445
3446 /*
3447 * Check if certificate is revoked
3448 */
3449 if( x509parse_revoked(crt, crl_list) )
3450 {
3451 flags |= BADCERT_REVOKED;
3452 break;
3453 }
3454
3455 crl_list = crl_list->next;
3456 }
3457 return flags;
3458}
3459
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003460static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003461{
3462 size_t i;
3463 size_t cn_idx = 0;
3464
Paul Bakker57b12982012-02-11 17:38:38 +00003465 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003466 return( 0 );
3467
3468 for( i = 0; i < strlen( cn ); ++i )
3469 {
3470 if( cn[i] == '.' )
3471 {
3472 cn_idx = i;
3473 break;
3474 }
3475 }
3476
3477 if( cn_idx == 0 )
3478 return( 0 );
3479
Paul Bakker535e97d2012-08-23 10:49:55 +00003480 if( strlen( cn ) - cn_idx == name->len - 1 &&
3481 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003482 {
3483 return( 1 );
3484 }
3485
3486 return( 0 );
3487}
3488
Paul Bakker915275b2012-09-28 07:10:55 +00003489static int x509parse_verify_top(
3490 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003491 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003492 int (*f_vrfy)(void *, x509_cert *, int, int *),
3493 void *p_vrfy )
3494{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003495 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003496 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003497 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3498 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003499
3500 if( x509parse_time_expired( &child->valid_to ) )
3501 *flags |= BADCERT_EXPIRED;
3502
3503 /*
3504 * Child is the top of the chain. Check against the trust_ca list.
3505 */
3506 *flags |= BADCERT_NOT_TRUSTED;
3507
3508 while( trust_ca != NULL )
3509 {
3510 if( trust_ca->version == 0 ||
3511 child->issuer_raw.len != trust_ca->subject_raw.len ||
3512 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3513 child->issuer_raw.len ) != 0 )
3514 {
3515 trust_ca = trust_ca->next;
3516 continue;
3517 }
3518
Paul Bakker9a736322012-11-14 12:39:52 +00003519 /*
3520 * Reduce path_len to check against if top of the chain is
3521 * the same as the trusted CA
3522 */
3523 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3524 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003525 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003526 {
3527 check_path_cnt--;
3528 }
3529
Paul Bakker915275b2012-09-28 07:10:55 +00003530 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003531 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003532 {
3533 trust_ca = trust_ca->next;
3534 continue;
3535 }
3536
Paul Bakkerc70b9822013-04-07 22:00:46 +02003537 md_info = md_info_from_type( child->sig_md );
3538 if( md_info == NULL )
3539 {
3540 /*
3541 * Cannot check 'unknown' hash
3542 */
Paul Bakker3a074a72013-08-20 12:45:03 +02003543 trust_ca = trust_ca->next;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003544 continue;
3545 }
Paul Bakker915275b2012-09-28 07:10:55 +00003546
Paul Bakkerc70b9822013-04-07 22:00:46 +02003547 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003548
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003549 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003550 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003551 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003552 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003553 trust_ca = trust_ca->next;
3554 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003555 }
3556
3557 /*
3558 * Top of chain is signed by a trusted CA
3559 */
3560 *flags &= ~BADCERT_NOT_TRUSTED;
3561 break;
3562 }
3563
Paul Bakker9a736322012-11-14 12:39:52 +00003564 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003565 * If top of chain is not the same as the trusted CA send a verify request
3566 * to the callback for any issues with validity and CRL presence for the
3567 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003568 */
3569 if( trust_ca != NULL &&
3570 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3571 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3572 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003573 {
3574 /* Check trusted CA's CRL for then chain's top crt */
3575 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3576
3577 if( x509parse_time_expired( &trust_ca->valid_to ) )
3578 ca_flags |= BADCERT_EXPIRED;
3579
Paul Bakker915275b2012-09-28 07:10:55 +00003580 if( NULL != f_vrfy )
3581 {
Paul Bakker9a736322012-11-14 12:39:52 +00003582 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003583 return( ret );
3584 }
3585 }
3586
3587 /* Call callback on top cert */
3588 if( NULL != f_vrfy )
3589 {
Paul Bakker9a736322012-11-14 12:39:52 +00003590 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003591 return( ret );
3592 }
3593
Paul Bakker915275b2012-09-28 07:10:55 +00003594 *flags |= ca_flags;
3595
3596 return( 0 );
3597}
3598
3599static int x509parse_verify_child(
3600 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003601 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003602 int (*f_vrfy)(void *, x509_cert *, int, int *),
3603 void *p_vrfy )
3604{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003605 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003606 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003607 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003608 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003609 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003610
3611 if( x509parse_time_expired( &child->valid_to ) )
3612 *flags |= BADCERT_EXPIRED;
3613
Paul Bakkerc70b9822013-04-07 22:00:46 +02003614 md_info = md_info_from_type( child->sig_md );
3615 if( md_info == NULL )
3616 {
3617 /*
3618 * Cannot check 'unknown' hash
3619 */
Paul Bakker915275b2012-09-28 07:10:55 +00003620 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003621 }
3622 else
3623 {
3624 md( md_info, child->tbs.p, child->tbs.len, hash );
3625
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003626 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003627 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003628 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003629 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003630 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003631 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003632 }
3633
Paul Bakker915275b2012-09-28 07:10:55 +00003634 /* Check trusted CA's CRL for the given crt */
3635 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3636
3637 grandparent = parent->next;
3638
3639 while( grandparent != NULL )
3640 {
3641 if( grandparent->version == 0 ||
3642 grandparent->ca_istrue == 0 ||
3643 parent->issuer_raw.len != grandparent->subject_raw.len ||
3644 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3645 parent->issuer_raw.len ) != 0 )
3646 {
3647 grandparent = grandparent->next;
3648 continue;
3649 }
3650 break;
3651 }
3652
Paul Bakker915275b2012-09-28 07:10:55 +00003653 if( grandparent != NULL )
3654 {
3655 /*
3656 * Part of the chain
3657 */
Paul Bakker9a736322012-11-14 12:39:52 +00003658 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 +00003659 if( ret != 0 )
3660 return( ret );
3661 }
3662 else
3663 {
Paul Bakker9a736322012-11-14 12:39:52 +00003664 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 +00003665 if( ret != 0 )
3666 return( ret );
3667 }
3668
3669 /* child is verified to be a child of the parent, call verify callback */
3670 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003671 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003672 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003673
3674 *flags |= parent_flags;
3675
3676 return( 0 );
3677}
3678
Paul Bakker76fd75a2011-01-16 21:12:10 +00003679/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 * Verify the certificate validity
3681 */
3682int x509parse_verify( x509_cert *crt,
3683 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003684 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003685 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003686 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003687 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688{
Paul Bakker23986e52011-04-24 08:57:21 +00003689 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003690 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003691 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003692 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003694 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003695
Paul Bakker40ea7de2009-05-03 10:18:48 +00003696 *flags = 0;
3697
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 if( cn != NULL )
3699 {
3700 name = &crt->subject;
3701 cn_len = strlen( cn );
3702
Paul Bakker4d2c1242012-05-10 14:12:46 +00003703 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003705 cur = &crt->subject_alt_names;
3706
3707 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003708 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003709 if( cur->buf.len == cn_len &&
3710 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003711 break;
3712
Paul Bakker535e97d2012-08-23 10:49:55 +00003713 if( cur->buf.len > 2 &&
3714 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003715 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003716 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
Paul Bakker4d2c1242012-05-10 14:12:46 +00003718 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003719 }
3720
3721 if( cur == NULL )
3722 *flags |= BADCERT_CN_MISMATCH;
3723 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003724 else
3725 {
3726 while( name != NULL )
3727 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003728 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003729 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003730 if( name->val.len == cn_len &&
3731 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003732 break;
3733
Paul Bakker535e97d2012-08-23 10:49:55 +00003734 if( name->val.len > 2 &&
3735 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003736 x509_wildcard_verify( cn, &name->val ) )
3737 break;
3738 }
3739
3740 name = name->next;
3741 }
3742
3743 if( name == NULL )
3744 *flags |= BADCERT_CN_MISMATCH;
3745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
Paul Bakker5121ce52009-01-03 21:22:43 +00003748 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003749 * Iterate upwards in the given cert chain, to find our crt parent.
3750 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003751 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003752 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Paul Bakker76fd75a2011-01-16 21:12:10 +00003754 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003755 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003756 if( parent->ca_istrue == 0 ||
3757 crt->issuer_raw.len != parent->subject_raw.len ||
3758 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 crt->issuer_raw.len ) != 0 )
3760 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003761 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 continue;
3763 }
Paul Bakker915275b2012-09-28 07:10:55 +00003764 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 }
3766
Paul Bakker915275b2012-09-28 07:10:55 +00003767 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 {
Paul Bakker915275b2012-09-28 07:10:55 +00003769 /*
3770 * Part of the chain
3771 */
Paul Bakker9a736322012-11-14 12:39:52 +00003772 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003773 if( ret != 0 )
3774 return( ret );
3775 }
3776 else
Paul Bakker74111d32011-01-15 16:57:55 +00003777 {
Paul Bakker9a736322012-11-14 12:39:52 +00003778 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003779 if( ret != 0 )
3780 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003781 }
Paul Bakker915275b2012-09-28 07:10:55 +00003782
3783 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003784 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003785
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 return( 0 );
3787}
3788
3789/*
3790 * Unallocate all certificate data
3791 */
3792void x509_free( x509_cert *crt )
3793{
3794 x509_cert *cert_cur = crt;
3795 x509_cert *cert_prv;
3796 x509_name *name_cur;
3797 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003798 x509_sequence *seq_cur;
3799 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003800
3801 if( crt == NULL )
3802 return;
3803
3804 do
3805 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003806 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807
3808 name_cur = cert_cur->issuer.next;
3809 while( name_cur != NULL )
3810 {
3811 name_prv = name_cur;
3812 name_cur = name_cur->next;
3813 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003814 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 }
3816
3817 name_cur = cert_cur->subject.next;
3818 while( name_cur != NULL )
3819 {
3820 name_prv = name_cur;
3821 name_cur = name_cur->next;
3822 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003823 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 }
3825
Paul Bakker74111d32011-01-15 16:57:55 +00003826 seq_cur = cert_cur->ext_key_usage.next;
3827 while( seq_cur != NULL )
3828 {
3829 seq_prv = seq_cur;
3830 seq_cur = seq_cur->next;
3831 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003832 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003833 }
3834
Paul Bakker8afa70d2012-02-11 18:42:45 +00003835 seq_cur = cert_cur->subject_alt_names.next;
3836 while( seq_cur != NULL )
3837 {
3838 seq_prv = seq_cur;
3839 seq_cur = seq_cur->next;
3840 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003841 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003842 }
3843
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 if( cert_cur->raw.p != NULL )
3845 {
3846 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003847 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 }
3849
3850 cert_cur = cert_cur->next;
3851 }
3852 while( cert_cur != NULL );
3853
3854 cert_cur = crt;
3855 do
3856 {
3857 cert_prv = cert_cur;
3858 cert_cur = cert_cur->next;
3859
3860 memset( cert_prv, 0, sizeof( x509_cert ) );
3861 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003862 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 }
3864 while( cert_cur != NULL );
3865}
3866
Paul Bakkerd98030e2009-05-02 15:13:40 +00003867/*
3868 * Unallocate all CRL data
3869 */
3870void x509_crl_free( x509_crl *crl )
3871{
3872 x509_crl *crl_cur = crl;
3873 x509_crl *crl_prv;
3874 x509_name *name_cur;
3875 x509_name *name_prv;
3876 x509_crl_entry *entry_cur;
3877 x509_crl_entry *entry_prv;
3878
3879 if( crl == NULL )
3880 return;
3881
3882 do
3883 {
3884 name_cur = crl_cur->issuer.next;
3885 while( name_cur != NULL )
3886 {
3887 name_prv = name_cur;
3888 name_cur = name_cur->next;
3889 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003890 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003891 }
3892
3893 entry_cur = crl_cur->entry.next;
3894 while( entry_cur != NULL )
3895 {
3896 entry_prv = entry_cur;
3897 entry_cur = entry_cur->next;
3898 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003899 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003900 }
3901
3902 if( crl_cur->raw.p != NULL )
3903 {
3904 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003905 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003906 }
3907
3908 crl_cur = crl_cur->next;
3909 }
3910 while( crl_cur != NULL );
3911
3912 crl_cur = crl;
3913 do
3914 {
3915 crl_prv = crl_cur;
3916 crl_cur = crl_cur->next;
3917
3918 memset( crl_prv, 0, sizeof( x509_crl ) );
3919 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003920 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003921 }
3922 while( crl_cur != NULL );
3923}
3924
Paul Bakker40e46942009-01-03 21:51:57 +00003925#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003926
Paul Bakker40e46942009-01-03 21:51:57 +00003927#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003928
3929/*
3930 * Checkup routine
3931 */
3932int x509_self_test( int verbose )
3933{
Paul Bakker5690efc2011-05-26 13:16:06 +00003934#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003935 int ret;
3936 int flags;
3937 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003938 x509_cert cacert;
3939 x509_cert clicert;
3940 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003941#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003942 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003943#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
3945 if( verbose != 0 )
3946 printf( " X.509 certificate load: " );
3947
3948 memset( &clicert, 0, sizeof( x509_cert ) );
3949
Paul Bakker3c2122f2013-06-24 19:03:14 +02003950 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003951 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952 if( ret != 0 )
3953 {
3954 if( verbose != 0 )
3955 printf( "failed\n" );
3956
3957 return( ret );
3958 }
3959
3960 memset( &cacert, 0, sizeof( x509_cert ) );
3961
Paul Bakker3c2122f2013-06-24 19:03:14 +02003962 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003963 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 if( ret != 0 )
3965 {
3966 if( verbose != 0 )
3967 printf( "failed\n" );
3968
3969 return( ret );
3970 }
3971
3972 if( verbose != 0 )
3973 printf( "passed\n X.509 private key load: " );
3974
3975 i = strlen( test_ca_key );
3976 j = strlen( test_ca_pwd );
3977
Paul Bakker66b78b22011-03-25 14:22:50 +00003978 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3979
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003980 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003981 (const unsigned char *) test_ca_key, i,
3982 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 {
3984 if( verbose != 0 )
3985 printf( "failed\n" );
3986
3987 return( ret );
3988 }
3989
3990 if( verbose != 0 )
3991 printf( "passed\n X.509 signature verify: ");
3992
Paul Bakker23986e52011-04-24 08:57:21 +00003993 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 if( ret != 0 )
3995 {
3996 if( verbose != 0 )
3997 printf( "failed\n" );
3998
3999 return( ret );
4000 }
4001
Paul Bakker5690efc2011-05-26 13:16:06 +00004002#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004004 printf( "passed\n X.509 DHM parameter load: " );
4005
4006 i = strlen( test_dhm_params );
4007 j = strlen( test_ca_pwd );
4008
Paul Bakker3c2122f2013-06-24 19:03:14 +02004009 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004010 {
4011 if( verbose != 0 )
4012 printf( "failed\n" );
4013
4014 return( ret );
4015 }
4016
4017 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004019#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
4021 x509_free( &cacert );
4022 x509_free( &clicert );
4023 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004024#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004025 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004027
4028 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004029#else
4030 ((void) verbose);
4031 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004033}
4034
4035#endif
4036
4037#endif