blob: 55cc9e37808d11f0b7dc2a774f1e7199a83108cd [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 )
Paul Bakker003dbad2013-09-09 17:26:14 +02002056 {
2057 closedir( dir );
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002058 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker003dbad2013-09-09 17:26:14 +02002059 }
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002060
2061 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002062 continue;
2063
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002064 // Ignore parse errors
2065 //
Paul Bakker8d914582012-06-04 12:46:42 +00002066 t_ret = x509parse_crtfile( chain, entry_name );
2067 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002068 ret++;
2069 else
2070 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002071 }
2072 closedir( dir );
2073#endif
2074
2075 return( ret );
2076}
2077
Paul Bakkerd98030e2009-05-02 15:13:40 +00002078/*
2079 * Load one or more CRLs and add them to the chained list
2080 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002081int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002082{
2083 int ret;
2084 size_t n;
2085 unsigned char *buf;
2086
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002087 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002088 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002089
Paul Bakker27fdf462011-06-09 13:55:13 +00002090 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002091
2092 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002093 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002094
2095 return( ret );
2096}
2097
Paul Bakker5121ce52009-01-03 21:22:43 +00002098/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002099 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002100 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002101int x509parse_keyfile( pk_context *ctx,
2102 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002103{
2104 int ret;
2105 size_t n;
2106 unsigned char *buf;
2107
2108 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2109 return( ret );
2110
2111 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002112 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002113 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002114 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002115 (const unsigned char *) pwd, strlen( pwd ) );
2116
2117 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002118 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002119
2120 return( ret );
2121}
2122
2123/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002124 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002125 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002126int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002127{
2128 int ret;
2129 size_t n;
2130 unsigned char *buf;
2131
2132 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2133 return( ret );
2134
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002135 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002136
2137 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002138 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002139
2140 return( ret );
2141}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002142
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002143#if defined(POLARSSL_RSA_C)
2144/*
2145 * Load and parse a private RSA key
2146 */
2147int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2148{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002149 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002150 pk_context pk;
2151
2152 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002153
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002154 ret = x509parse_keyfile( &pk, path, pwd );
2155
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002156 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2157 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2158
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002159 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002160 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002161 else
2162 rsa_free( rsa );
2163
2164 pk_free( &pk );
2165
2166 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002167}
2168
2169/*
2170 * Load and parse a public RSA key
2171 */
2172int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2173{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002174 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002175 pk_context pk;
2176
2177 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002178
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002179 ret = x509parse_public_keyfile( &pk, path );
2180
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002181 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2182 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2183
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002184 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002185 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002186 else
2187 rsa_free( rsa );
2188
2189 pk_free( &pk );
2190
2191 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002192}
2193#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002194#endif /* POLARSSL_FS_IO */
2195
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002196#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002197/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002198 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002200static int x509parse_key_pkcs1_der( rsa_context *rsa,
2201 const unsigned char *key,
2202 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203{
Paul Bakker23986e52011-04-24 08:57:21 +00002204 int ret;
2205 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002207
Paul Bakker96743fc2011-02-12 14:30:57 +00002208 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002209 end = p + keylen;
2210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002212 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002213 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 * RSAPrivateKey ::= SEQUENCE {
2215 * version Version,
2216 * modulus INTEGER, -- n
2217 * publicExponent INTEGER, -- e
2218 * privateExponent INTEGER, -- d
2219 * prime1 INTEGER, -- p
2220 * prime2 INTEGER, -- q
2221 * exponent1 INTEGER, -- d mod (p-1)
2222 * exponent2 INTEGER, -- d mod (q-1)
2223 * coefficient INTEGER, -- (inverse of q) mod p
2224 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2225 * }
2226 */
2227 if( ( ret = asn1_get_tag( &p, end, &len,
2228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2229 {
Paul Bakker9d781402011-05-09 16:17:09 +00002230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 }
2232
2233 end = p + len;
2234
2235 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2236 {
Paul Bakker9d781402011-05-09 16:17:09 +00002237 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
2240 if( rsa->ver != 0 )
2241 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002242 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244
2245 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2246 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2247 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2248 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2249 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2250 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2251 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2252 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2253 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002255 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
2257
2258 rsa->len = mpi_size( &rsa->N );
2259
2260 if( p != end )
2261 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002263 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002264 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 }
2266
2267 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2268 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 rsa_free( rsa );
2270 return( ret );
2271 }
2272
Paul Bakkere2f50402013-06-24 19:00:59 +02002273 return( 0 );
2274}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002275#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002276
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002277#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002278/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002279 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002280 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002281static int x509parse_key_sec1_der( ecp_keypair *eck,
2282 const unsigned char *key,
2283 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002284{
2285 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002286 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002287 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002288 x509_buf params;
2289 unsigned char *p = (unsigned char *) key;
2290 unsigned char *end = p + keylen;
2291 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002292
2293 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002294 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002295 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002296 * ECPrivateKey ::= SEQUENCE {
2297 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2298 * privateKey OCTET STRING,
2299 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2300 * publicKey [1] BIT STRING OPTIONAL
2301 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002302 */
2303 if( ( ret = asn1_get_tag( &p, end, &len,
2304 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2305 {
2306 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2307 }
2308
2309 end = p + len;
2310
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002311 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2313
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002314 if( version != 1 )
2315 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002316
Paul Bakkere2f50402013-06-24 19:00:59 +02002317 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2318 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2319
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002320 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002321 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002322 ecp_keypair_free( eck );
2323 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2324 }
2325
2326 p += len;
2327
2328 /*
2329 * Is 'parameters' present?
2330 */
2331 if( ( ret = asn1_get_tag( &p, end, &len,
2332 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2333 {
2334 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2335 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2336 {
2337 ecp_keypair_free( eck );
2338 return( ret );
2339 }
2340 }
2341 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2342 {
2343 ecp_keypair_free( eck );
2344 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2345 }
2346
2347 /*
2348 * Is 'publickey' present?
2349 */
2350 if( ( ret = asn1_get_tag( &p, end, &len,
2351 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2352 {
2353 end2 = p + len;
2354
2355 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2356 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2357
2358 if( p + len != end2 )
2359 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2360 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2361
2362 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2363 return( ret );
2364 }
2365 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2366 {
2367 ecp_keypair_free( eck );
2368 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2369 }
2370
2371 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2372 {
2373 ecp_keypair_free( eck );
2374 return( ret );
2375 }
2376
2377 return 0;
2378}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002379#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002380
2381/*
2382 * Parse an unencrypted PKCS#8 encoded private key
2383 */
2384static int x509parse_key_pkcs8_unencrypted_der(
2385 pk_context *pk,
2386 const unsigned char* key,
2387 size_t keylen )
2388{
2389 int ret, version;
2390 size_t len;
2391 x509_buf params;
2392 unsigned char *p = (unsigned char *) key;
2393 unsigned char *end = p + keylen;
2394 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002395 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002396
2397 /*
2398 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2399 *
2400 * PrivateKeyInfo ::= SEQUENCE {
2401 * version Version,
2402 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2403 * privateKey PrivateKey,
2404 * attributes [0] IMPLICIT Attributes OPTIONAL }
2405 *
2406 * Version ::= INTEGER
2407 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2408 * PrivateKey ::= OCTET STRING
2409 *
2410 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2411 */
2412
2413 if( ( ret = asn1_get_tag( &p, end, &len,
2414 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2415 {
2416 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002417 }
2418
2419 end = p + len;
2420
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002421 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2422 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2423
2424 if( version != 0 )
2425 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2426
2427 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2428 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2429
2430 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2431 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2432
2433 if( len < 1 )
2434 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2435 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2436
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002437 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2438 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2439
2440 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002441 return( ret );
2442
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002443#if defined(POLARSSL_RSA_C)
2444 if( pk_alg == POLARSSL_PK_RSA )
2445 {
2446 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2447 {
2448 pk_free( pk );
2449 return( ret );
2450 }
2451 } else
2452#endif /* POLARSSL_RSA_C */
2453#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002454 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2455 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002456 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2457 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2458 {
2459 pk_free( pk );
2460 return( ret );
2461 }
2462 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002463#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002464 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2465
2466 return 0;
2467}
2468
2469/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002470 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002471 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002472static int x509parse_key_pkcs8_encrypted_der(
2473 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002474 const unsigned char *key, size_t keylen,
2475 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002476{
2477 int ret;
2478 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002479 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002480 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002481 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002482#if defined(POLARSSL_PKCS12_C)
2483 cipher_type_t cipher_alg;
2484 md_type_t md_alg;
2485#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002486
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002487 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002488
2489 p = (unsigned char *) key;
2490 end = p + keylen;
2491
Paul Bakker28144de2013-06-24 19:28:55 +02002492 if( pwdlen == 0 )
2493 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2494
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002495 /*
2496 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2497 *
2498 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2499 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2500 * encryptedData EncryptedData
2501 * }
2502 *
2503 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2504 *
2505 * EncryptedData ::= OCTET STRING
2506 *
2507 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2508 */
2509 if( ( ret = asn1_get_tag( &p, end, &len,
2510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2511 {
2512 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2513 }
2514
2515 end = p + len;
2516
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002517 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002518 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002519
2520 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2521 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2522
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002523 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002524 return( POLARSSL_ERR_X509_INVALID_INPUT );
2525
2526 /*
2527 * Decrypt EncryptedData with appropriate PDE
2528 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002529#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002530 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002531 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002532 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002533 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002534 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002535 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002536 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2537 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2538
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002539 return( ret );
2540 }
2541 }
2542 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2543 {
2544 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2545 PKCS12_PBE_DECRYPT,
2546 pwd, pwdlen,
2547 p, len, buf ) ) != 0 )
2548 {
2549 return( ret );
2550 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002551
2552 // Best guess for password mismatch when using RC4. If first tag is
2553 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2554 //
2555 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2556 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002557 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002558 else
2559#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002560#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002561 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002562 {
2563 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2564 p, len, buf ) ) != 0 )
2565 {
2566 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2567 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2568
2569 return( ret );
2570 }
2571 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002572 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002573#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002574 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2575
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002576 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002577}
2578
2579/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002580 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002581 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002582int x509parse_key( pk_context *pk,
2583 const unsigned char *key, size_t keylen,
2584 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002585{
2586 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002587 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002588
2589#if defined(POLARSSL_PEM_C)
2590 size_t len;
2591 pem_context pem;
2592
2593 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002594
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002595#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002596 ret = pem_read_buffer( &pem,
2597 "-----BEGIN RSA PRIVATE KEY-----",
2598 "-----END RSA PRIVATE KEY-----",
2599 key, pwd, pwdlen, &len );
2600 if( ret == 0 )
2601 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002602 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2603 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2604
2605 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002606 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2607 pem.buf, pem.buflen ) ) != 0 )
2608 {
2609 pk_free( pk );
2610 }
2611
2612 pem_free( &pem );
2613 return( ret );
2614 }
2615 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2616 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2617 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2618 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2619 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2620 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002621#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002622
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002623#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002624 ret = pem_read_buffer( &pem,
2625 "-----BEGIN EC PRIVATE KEY-----",
2626 "-----END EC PRIVATE KEY-----",
2627 key, pwd, pwdlen, &len );
2628 if( ret == 0 )
2629 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002630 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2631 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2632
2633 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002634 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2635 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002636 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002637 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002638 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002639
2640 pem_free( &pem );
2641 return( ret );
2642 }
2643 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2644 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2645 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2646 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2647 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2648 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002649#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002650
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002651 ret = pem_read_buffer( &pem,
2652 "-----BEGIN PRIVATE KEY-----",
2653 "-----END PRIVATE KEY-----",
2654 key, NULL, 0, &len );
2655 if( ret == 0 )
2656 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002657 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002658 pem.buf, pem.buflen ) ) != 0 )
2659 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002660 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002661 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002662
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002663 pem_free( &pem );
2664 return( ret );
2665 }
2666 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2667 return( ret );
2668
2669 ret = pem_read_buffer( &pem,
2670 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2671 "-----END ENCRYPTED PRIVATE KEY-----",
2672 key, NULL, 0, &len );
2673 if( ret == 0 )
2674 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002675 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002676 pem.buf, pem.buflen,
2677 pwd, pwdlen ) ) != 0 )
2678 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002679 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002680 }
2681
2682 pem_free( &pem );
2683 return( ret );
2684 }
2685 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2686 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002687#else
2688 ((void) pwd);
2689 ((void) pwdlen);
2690#endif /* POLARSSL_PEM_C */
2691
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002692 /*
2693 * At this point we only know it's not a PEM formatted key. Could be any
2694 * of the known DER encoded private key formats
2695 *
2696 * We try the different DER format parsers to see if one passes without
2697 * error
2698 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002699 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2700 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002701 {
2702 return( 0 );
2703 }
2704
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002705 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002706
2707 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2708 {
2709 return( ret );
2710 }
2711
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002712 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002713 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002714
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002715 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002716
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002717#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002718 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2719 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2720
2721 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002722 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2723 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002724 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002725 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002726
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002727 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002728#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002729
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002730#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002731 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2732 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2733
2734 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002735 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2736 {
2737 return( 0 );
2738 }
2739
2740 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002741#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002742
2743 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2744}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002745
2746/*
2747 * Parse a public key
2748 */
2749int x509parse_public_key( pk_context *ctx,
2750 const unsigned char *key, size_t keylen )
2751{
2752 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002753 unsigned char *p;
2754#if defined(POLARSSL_PEM_C)
2755 size_t len;
2756 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002757
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002758 pem_init( &pem );
2759 ret = pem_read_buffer( &pem,
2760 "-----BEGIN PUBLIC KEY-----",
2761 "-----END PUBLIC KEY-----",
2762 key, NULL, 0, &len );
2763
2764 if( ret == 0 )
2765 {
2766 /*
2767 * Was PEM encoded
2768 */
2769 key = pem.buf;
2770 keylen = pem.buflen;
2771 }
2772 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2773 {
2774 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002775 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002776 }
2777#endif
2778 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002779
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002780 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002781
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002782#if defined(POLARSSL_PEM_C)
2783 pem_free( &pem );
2784#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002785
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002786 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002787}
2788
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002789#if defined(POLARSSL_RSA_C)
2790/*
2791 * Parse a private RSA key
2792 */
2793int x509parse_key_rsa( rsa_context *rsa,
2794 const unsigned char *key, size_t keylen,
2795 const unsigned char *pwd, size_t pwdlen )
2796{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002797 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002798 pk_context pk;
2799
2800 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002801
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002802 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
2803
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002804 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2805 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2806
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002807 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002808 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002809 else
2810 rsa_free( rsa );
2811
2812 pk_free( &pk );
2813
2814 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002815}
2816
2817/*
2818 * Parse a public RSA key
2819 */
2820int x509parse_public_key_rsa( rsa_context *rsa,
2821 const unsigned char *key, size_t keylen )
2822{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002823 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002824 pk_context pk;
2825
2826 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002827
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002828 ret = x509parse_public_key( &pk, key, keylen );
2829
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002830 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2831 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2832
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002833 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002834 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002835 else
2836 rsa_free( rsa );
2837
2838 pk_free( &pk );
2839
2840 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002841}
2842#endif /* POLARSSL_RSA_C */
2843
Paul Bakkereaa89f82011-04-04 21:36:15 +00002844#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002845/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002846 * Parse DHM parameters
2847 */
Paul Bakker23986e52011-04-24 08:57:21 +00002848int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002849{
Paul Bakker23986e52011-04-24 08:57:21 +00002850 int ret;
2851 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002852 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002853#if defined(POLARSSL_PEM_C)
2854 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002855
Paul Bakker96743fc2011-02-12 14:30:57 +00002856 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002857
Paul Bakker96743fc2011-02-12 14:30:57 +00002858 ret = pem_read_buffer( &pem,
2859 "-----BEGIN DH PARAMETERS-----",
2860 "-----END DH PARAMETERS-----",
2861 dhmin, NULL, 0, &dhminlen );
2862
2863 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002864 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002865 /*
2866 * Was PEM encoded
2867 */
2868 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002869 }
Paul Bakker00b28602013-06-24 13:02:41 +02002870 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002871 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002872 pem_free( &pem );
2873 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002874 }
2875
Paul Bakker96743fc2011-02-12 14:30:57 +00002876 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2877#else
2878 p = (unsigned char *) dhmin;
2879#endif
2880 end = p + dhminlen;
2881
Paul Bakker1b57b062011-01-06 15:48:19 +00002882 memset( dhm, 0, sizeof( dhm_context ) );
2883
Paul Bakker1b57b062011-01-06 15:48:19 +00002884 /*
2885 * DHParams ::= SEQUENCE {
2886 * prime INTEGER, -- P
2887 * generator INTEGER, -- g
2888 * }
2889 */
2890 if( ( ret = asn1_get_tag( &p, end, &len,
2891 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2892 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002893#if defined(POLARSSL_PEM_C)
2894 pem_free( &pem );
2895#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002896 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002897 }
2898
2899 end = p + len;
2900
2901 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2902 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2903 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002904#if defined(POLARSSL_PEM_C)
2905 pem_free( &pem );
2906#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002907 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002908 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002909 }
2910
2911 if( p != end )
2912 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002913#if defined(POLARSSL_PEM_C)
2914 pem_free( &pem );
2915#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002916 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002917 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002918 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2919 }
2920
Paul Bakker96743fc2011-02-12 14:30:57 +00002921#if defined(POLARSSL_PEM_C)
2922 pem_free( &pem );
2923#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002924
2925 return( 0 );
2926}
2927
Paul Bakker335db3f2011-04-25 15:28:35 +00002928#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002929/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002930 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002931 */
2932int x509parse_dhmfile( dhm_context *dhm, const char *path )
2933{
2934 int ret;
2935 size_t n;
2936 unsigned char *buf;
2937
Paul Bakker69e095c2011-12-10 21:55:01 +00002938 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2939 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002940
Paul Bakker27fdf462011-06-09 13:55:13 +00002941 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002942
2943 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002944 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002945
2946 return( ret );
2947}
Paul Bakker335db3f2011-04-25 15:28:35 +00002948#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002949#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002950
Paul Bakker5121ce52009-01-03 21:22:43 +00002951#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002952#include <stdarg.h>
2953
2954#if !defined vsnprintf
2955#define vsnprintf _vsnprintf
2956#endif // vsnprintf
2957
2958/*
2959 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2960 * Result value is not size of buffer needed, but -1 if no fit is possible.
2961 *
2962 * This fuction tries to 'fix' this by at least suggesting enlarging the
2963 * size by 20.
2964 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002965static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002966{
2967 va_list ap;
2968 int res = -1;
2969
2970 va_start( ap, format );
2971
2972 res = vsnprintf( str, size, format, ap );
2973
2974 va_end( ap );
2975
2976 // No quick fix possible
2977 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002978 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002979
2980 return res;
2981}
2982
2983#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002984#endif
2985
Paul Bakkerd98030e2009-05-02 15:13:40 +00002986#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2987
2988#define SAFE_SNPRINTF() \
2989{ \
2990 if( ret == -1 ) \
2991 return( -1 ); \
2992 \
Paul Bakker23986e52011-04-24 08:57:21 +00002993 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002994 p[n - 1] = '\0'; \
2995 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2996 } \
2997 \
Paul Bakker23986e52011-04-24 08:57:21 +00002998 n -= (unsigned int) ret; \
2999 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003000}
3001
Paul Bakker5121ce52009-01-03 21:22:43 +00003002/*
3003 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003004 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003006int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003007{
Paul Bakker23986e52011-04-24 08:57:21 +00003008 int ret;
3009 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003011 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003012 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 char s[128], *p;
3014
3015 memset( s, 0, sizeof( s ) );
3016
3017 name = dn;
3018 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003019 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020
3021 while( name != NULL )
3022 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003023 if( !name->oid.p )
3024 {
3025 name = name->next;
3026 continue;
3027 }
3028
Paul Bakker74111d32011-01-15 16:57:55 +00003029 if( name != dn )
3030 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031 ret = snprintf( p, n, ", " );
3032 SAFE_SNPRINTF();
3033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Paul Bakkerc70b9822013-04-07 22:00:46 +02003035 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Paul Bakkerc70b9822013-04-07 22:00:46 +02003037 if( ret == 0 )
3038 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003041 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
3043 for( i = 0; i < name->val.len; i++ )
3044 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003045 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 break;
3047
3048 c = name->val.p[i];
3049 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3050 s[i] = '?';
3051 else s[i] = c;
3052 }
3053 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003054 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003055 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 name = name->next;
3057 }
3058
Paul Bakker23986e52011-04-24 08:57:21 +00003059 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060}
3061
3062/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003063 * Store the serial in printable form into buf; no more
3064 * than size characters will be written
3065 */
3066int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3067{
Paul Bakker23986e52011-04-24 08:57:21 +00003068 int ret;
3069 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003070 char *p;
3071
3072 p = buf;
3073 n = size;
3074
3075 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003076 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003077
3078 for( i = 0; i < nr; i++ )
3079 {
Paul Bakker93048802011-12-05 14:38:06 +00003080 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003081 continue;
3082
Paul Bakkerdd476992011-01-16 21:34:59 +00003083 ret = snprintf( p, n, "%02X%s",
3084 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3085 SAFE_SNPRINTF();
3086 }
3087
Paul Bakker03c7c252011-11-25 12:37:37 +00003088 if( nr != serial->len )
3089 {
3090 ret = snprintf( p, n, "...." );
3091 SAFE_SNPRINTF();
3092 }
3093
Paul Bakker23986e52011-04-24 08:57:21 +00003094 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003095}
3096
3097/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003098 * Helper for writing "RSA key size", "EC key size", etc
3099 */
3100static int x509_key_size_helper( char *buf, size_t size, const char *name )
3101{
3102 char *p = buf;
3103 size_t n = size;
3104 int ret;
3105
3106 if( strlen( name ) + sizeof( " key size" ) > size )
3107 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3108
3109 ret = snprintf( p, n, "%s key size", name );
3110 SAFE_SNPRINTF();
3111
3112 return( 0 );
3113}
3114
3115/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003116 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003118#define BEFORE_COLON 14
3119#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003120int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3121 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003122{
Paul Bakker23986e52011-04-24 08:57:21 +00003123 int ret;
3124 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003125 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003126 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003127 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
3129 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003130 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Paul Bakkerd98030e2009-05-02 15:13:40 +00003132 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003134 SAFE_SNPRINTF();
3135 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003136 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003137 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakkerdd476992011-01-16 21:34:59 +00003139 ret = x509parse_serial_gets( p, n, &crt->serial);
3140 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Paul Bakkerd98030e2009-05-02 15:13:40 +00003142 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3143 SAFE_SNPRINTF();
3144 ret = x509parse_dn_gets( p, n, &crt->issuer );
3145 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003146
Paul Bakkerd98030e2009-05-02 15:13:40 +00003147 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3148 SAFE_SNPRINTF();
3149 ret = x509parse_dn_gets( p, n, &crt->subject );
3150 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Paul Bakkerd98030e2009-05-02 15:13:40 +00003152 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3154 crt->valid_from.year, crt->valid_from.mon,
3155 crt->valid_from.day, crt->valid_from.hour,
3156 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003157 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakkerd98030e2009-05-02 15:13:40 +00003159 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3161 crt->valid_to.year, crt->valid_to.mon,
3162 crt->valid_to.day, crt->valid_to.hour,
3163 crt->valid_to.min, crt->valid_to.sec );
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 = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003167 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakkerc70b9822013-04-07 22:00:46 +02003169 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3170 if( ret != 0 )
3171 ret = snprintf( p, n, "???" );
3172 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003173 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003174 SAFE_SNPRINTF();
3175
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003176 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003177 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003178 {
3179 return( ret );
3180 }
3181
3182 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003183 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003184 SAFE_SNPRINTF();
3185
Paul Bakker23986e52011-04-24 08:57:21 +00003186 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003187}
3188
Paul Bakker74111d32011-01-15 16:57:55 +00003189/*
3190 * Return an informational string describing the given OID
3191 */
3192const char *x509_oid_get_description( x509_buf *oid )
3193{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003194 const char *desc = NULL;
3195 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003196
Paul Bakkerc70b9822013-04-07 22:00:46 +02003197 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003198
Paul Bakkerc70b9822013-04-07 22:00:46 +02003199 if( ret != 0 )
3200 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003201
Paul Bakkerc70b9822013-04-07 22:00:46 +02003202 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003203}
3204
3205/* Return the x.y.z.... style numeric string for the given OID */
3206int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3207{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003208 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003209}
3210
Paul Bakkerd98030e2009-05-02 15:13:40 +00003211/*
3212 * Return an informational string about the CRL.
3213 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003214int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3215 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216{
Paul Bakker23986e52011-04-24 08:57:21 +00003217 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003218 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003219 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003220 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003221 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003222
3223 p = buf;
3224 n = size;
3225
3226 ret = snprintf( p, n, "%sCRL version : %d",
3227 prefix, crl->version );
3228 SAFE_SNPRINTF();
3229
3230 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3231 SAFE_SNPRINTF();
3232 ret = x509parse_dn_gets( p, n, &crl->issuer );
3233 SAFE_SNPRINTF();
3234
3235 ret = snprintf( p, n, "\n%sthis update : " \
3236 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3237 crl->this_update.year, crl->this_update.mon,
3238 crl->this_update.day, crl->this_update.hour,
3239 crl->this_update.min, crl->this_update.sec );
3240 SAFE_SNPRINTF();
3241
3242 ret = snprintf( p, n, "\n%snext update : " \
3243 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3244 crl->next_update.year, crl->next_update.mon,
3245 crl->next_update.day, crl->next_update.hour,
3246 crl->next_update.min, crl->next_update.sec );
3247 SAFE_SNPRINTF();
3248
3249 entry = &crl->entry;
3250
3251 ret = snprintf( p, n, "\n%sRevoked certificates:",
3252 prefix );
3253 SAFE_SNPRINTF();
3254
Paul Bakker9be19372009-07-27 20:21:53 +00003255 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003256 {
3257 ret = snprintf( p, n, "\n%sserial number: ",
3258 prefix );
3259 SAFE_SNPRINTF();
3260
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003261 ret = x509parse_serial_gets( p, n, &entry->serial);
3262 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003263
Paul Bakkerd98030e2009-05-02 15:13:40 +00003264 ret = snprintf( p, n, " revocation date: " \
3265 "%04d-%02d-%02d %02d:%02d:%02d",
3266 entry->revocation_date.year, entry->revocation_date.mon,
3267 entry->revocation_date.day, entry->revocation_date.hour,
3268 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003269 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003270
3271 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 }
3273
Paul Bakkerc70b9822013-04-07 22:00:46 +02003274 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003275 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
Paul Bakkerc70b9822013-04-07 22:00:46 +02003277 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3278 if( ret != 0 )
3279 ret = snprintf( p, n, "???" );
3280 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003281 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003282 SAFE_SNPRINTF();
3283
Paul Bakker1e27bb22009-07-19 20:25:25 +00003284 ret = snprintf( p, n, "\n" );
3285 SAFE_SNPRINTF();
3286
Paul Bakker23986e52011-04-24 08:57:21 +00003287 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288}
3289
3290/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003291 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003293#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003294int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295{
Paul Bakkercce9d772011-11-18 14:26:47 +00003296 int year, mon, day;
3297 int hour, min, sec;
3298
3299#if defined(_WIN32)
3300 SYSTEMTIME st;
3301
3302 GetLocalTime(&st);
3303
3304 year = st.wYear;
3305 mon = st.wMonth;
3306 day = st.wDay;
3307 hour = st.wHour;
3308 min = st.wMinute;
3309 sec = st.wSecond;
3310#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 struct tm *lt;
3312 time_t tt;
3313
3314 tt = time( NULL );
3315 lt = localtime( &tt );
3316
Paul Bakkercce9d772011-11-18 14:26:47 +00003317 year = lt->tm_year + 1900;
3318 mon = lt->tm_mon + 1;
3319 day = lt->tm_mday;
3320 hour = lt->tm_hour;
3321 min = lt->tm_min;
3322 sec = lt->tm_sec;
3323#endif
3324
3325 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003326 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003327
Paul Bakkercce9d772011-11-18 14:26:47 +00003328 if( year == to->year &&
3329 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003330 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
Paul Bakkercce9d772011-11-18 14:26:47 +00003332 if( year == to->year &&
3333 mon == to->mon &&
3334 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003335 return( 1 );
3336
Paul Bakkercce9d772011-11-18 14:26:47 +00003337 if( year == to->year &&
3338 mon == to->mon &&
3339 day == to->day &&
3340 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003341 return( 1 );
3342
Paul Bakkercce9d772011-11-18 14:26:47 +00003343 if( year == to->year &&
3344 mon == to->mon &&
3345 day == to->day &&
3346 hour == to->hour &&
3347 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003348 return( 1 );
3349
Paul Bakkercce9d772011-11-18 14:26:47 +00003350 if( year == to->year &&
3351 mon == to->mon &&
3352 day == to->day &&
3353 hour == to->hour &&
3354 min == to->min &&
3355 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003356 return( 1 );
3357
Paul Bakker40ea7de2009-05-03 10:18:48 +00003358 return( 0 );
3359}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003360#else /* POLARSSL_HAVE_TIME */
3361int x509parse_time_expired( const x509_time *to )
3362{
3363 ((void) to);
3364 return( 0 );
3365}
3366#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003367
3368/*
3369 * Return 1 if the certificate is revoked, or 0 otherwise.
3370 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003371int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003372{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003373 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003374
3375 while( cur != NULL && cur->serial.len != 0 )
3376 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003377 if( crt->serial.len == cur->serial.len &&
3378 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003379 {
3380 if( x509parse_time_expired( &cur->revocation_date ) )
3381 return( 1 );
3382 }
3383
3384 cur = cur->next;
3385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003386
3387 return( 0 );
3388}
3389
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003390/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003391 * Check that the given certificate is valid accoring to the CRL.
3392 */
3393static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3394 x509_crl *crl_list)
3395{
3396 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003397 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3398 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003399
Paul Bakker915275b2012-09-28 07:10:55 +00003400 if( ca == NULL )
3401 return( flags );
3402
Paul Bakker76fd75a2011-01-16 21:12:10 +00003403 /*
3404 * TODO: What happens if no CRL is present?
3405 * Suggestion: Revocation state should be unknown if no CRL is present.
3406 * For backwards compatibility this is not yet implemented.
3407 */
3408
Paul Bakker915275b2012-09-28 07:10:55 +00003409 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003410 {
Paul Bakker915275b2012-09-28 07:10:55 +00003411 if( crl_list->version == 0 ||
3412 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003413 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3414 crl_list->issuer_raw.len ) != 0 )
3415 {
3416 crl_list = crl_list->next;
3417 continue;
3418 }
3419
3420 /*
3421 * Check if CRL is correctly signed by the trusted CA
3422 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003423 md_info = md_info_from_type( crl_list->sig_md );
3424 if( md_info == NULL )
3425 {
3426 /*
3427 * Cannot check 'unknown' hash
3428 */
3429 flags |= BADCRL_NOT_TRUSTED;
3430 break;
3431 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003432
Paul Bakkerc70b9822013-04-07 22:00:46 +02003433 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003434
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003435 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003436 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003437 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003438 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003439 flags |= BADCRL_NOT_TRUSTED;
3440 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003441 }
3442
3443 /*
3444 * Check for validity of CRL (Do not drop out)
3445 */
3446 if( x509parse_time_expired( &crl_list->next_update ) )
3447 flags |= BADCRL_EXPIRED;
3448
3449 /*
3450 * Check if certificate is revoked
3451 */
3452 if( x509parse_revoked(crt, crl_list) )
3453 {
3454 flags |= BADCERT_REVOKED;
3455 break;
3456 }
3457
3458 crl_list = crl_list->next;
3459 }
3460 return flags;
3461}
3462
Paul Bakkera5943852013-09-09 17:21:45 +02003463// Equal == 0, inequal == 1
3464static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3465{
3466 size_t i;
3467 unsigned char diff;
3468 const unsigned char *n1 = s1, *n2 = s2;
3469
3470 for( i = 0; i < len; i++ )
3471 {
3472 diff = n1[i] ^ n2[i];
3473
3474 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
3475 continue;
3476
3477 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
3478 continue;
3479
3480 return( 1 );
3481 }
3482
3483 return( 0 );
3484}
3485
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003486static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003487{
3488 size_t i;
3489 size_t cn_idx = 0;
3490
Paul Bakker57b12982012-02-11 17:38:38 +00003491 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003492 return( 0 );
3493
3494 for( i = 0; i < strlen( cn ); ++i )
3495 {
3496 if( cn[i] == '.' )
3497 {
3498 cn_idx = i;
3499 break;
3500 }
3501 }
3502
3503 if( cn_idx == 0 )
3504 return( 0 );
3505
Paul Bakker535e97d2012-08-23 10:49:55 +00003506 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkera5943852013-09-09 17:21:45 +02003507 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003508 {
3509 return( 1 );
3510 }
3511
3512 return( 0 );
3513}
3514
Paul Bakker915275b2012-09-28 07:10:55 +00003515static int x509parse_verify_top(
3516 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003517 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003518 int (*f_vrfy)(void *, x509_cert *, int, int *),
3519 void *p_vrfy )
3520{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003521 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003522 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003523 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3524 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003525
3526 if( x509parse_time_expired( &child->valid_to ) )
3527 *flags |= BADCERT_EXPIRED;
3528
3529 /*
3530 * Child is the top of the chain. Check against the trust_ca list.
3531 */
3532 *flags |= BADCERT_NOT_TRUSTED;
3533
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003534 md_info = md_info_from_type( child->sig_md );
3535 if( md_info == NULL )
3536 {
3537 /*
3538 * Cannot check 'unknown', no need to try any CA
3539 */
3540 trust_ca = NULL;
3541 }
3542 else
3543 md( md_info, child->tbs.p, child->tbs.len, hash );
3544
Paul Bakker915275b2012-09-28 07:10:55 +00003545 while( trust_ca != NULL )
3546 {
3547 if( trust_ca->version == 0 ||
3548 child->issuer_raw.len != trust_ca->subject_raw.len ||
3549 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3550 child->issuer_raw.len ) != 0 )
3551 {
3552 trust_ca = trust_ca->next;
3553 continue;
3554 }
3555
Paul Bakker9a736322012-11-14 12:39:52 +00003556 /*
3557 * Reduce path_len to check against if top of the chain is
3558 * the same as the trusted CA
3559 */
3560 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3561 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003562 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003563 {
3564 check_path_cnt--;
3565 }
3566
Paul Bakker915275b2012-09-28 07:10:55 +00003567 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003568 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003569 {
3570 trust_ca = trust_ca->next;
3571 continue;
3572 }
3573
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003574 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003575 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003576 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003577 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003578 trust_ca = trust_ca->next;
3579 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003580 }
3581
3582 /*
3583 * Top of chain is signed by a trusted CA
3584 */
3585 *flags &= ~BADCERT_NOT_TRUSTED;
3586 break;
3587 }
3588
Paul Bakker9a736322012-11-14 12:39:52 +00003589 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003590 * If top of chain is not the same as the trusted CA send a verify request
3591 * to the callback for any issues with validity and CRL presence for the
3592 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003593 */
3594 if( trust_ca != NULL &&
3595 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3596 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3597 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003598 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003599 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003600 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3601
3602 if( x509parse_time_expired( &trust_ca->valid_to ) )
3603 ca_flags |= BADCERT_EXPIRED;
3604
Paul Bakker915275b2012-09-28 07:10:55 +00003605 if( NULL != f_vrfy )
3606 {
Paul Bakker9a736322012-11-14 12:39:52 +00003607 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003608 return( ret );
3609 }
3610 }
3611
3612 /* Call callback on top cert */
3613 if( NULL != f_vrfy )
3614 {
Paul Bakker9a736322012-11-14 12:39:52 +00003615 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003616 return( ret );
3617 }
3618
Paul Bakker915275b2012-09-28 07:10:55 +00003619 *flags |= ca_flags;
3620
3621 return( 0 );
3622}
3623
3624static int x509parse_verify_child(
3625 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003626 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003627 int (*f_vrfy)(void *, x509_cert *, int, int *),
3628 void *p_vrfy )
3629{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003630 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003631 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003632 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003633 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003634 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003635
3636 if( x509parse_time_expired( &child->valid_to ) )
3637 *flags |= BADCERT_EXPIRED;
3638
Paul Bakkerc70b9822013-04-07 22:00:46 +02003639 md_info = md_info_from_type( child->sig_md );
3640 if( md_info == NULL )
3641 {
3642 /*
3643 * Cannot check 'unknown' hash
3644 */
Paul Bakker915275b2012-09-28 07:10:55 +00003645 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003646 }
3647 else
3648 {
3649 md( md_info, child->tbs.p, child->tbs.len, hash );
3650
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003651 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003652 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003653 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003654 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003655 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003656 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003657 }
3658
Paul Bakker915275b2012-09-28 07:10:55 +00003659 /* Check trusted CA's CRL for the given crt */
3660 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3661
3662 grandparent = parent->next;
3663
3664 while( grandparent != NULL )
3665 {
3666 if( grandparent->version == 0 ||
3667 grandparent->ca_istrue == 0 ||
3668 parent->issuer_raw.len != grandparent->subject_raw.len ||
3669 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3670 parent->issuer_raw.len ) != 0 )
3671 {
3672 grandparent = grandparent->next;
3673 continue;
3674 }
3675 break;
3676 }
3677
Paul Bakker915275b2012-09-28 07:10:55 +00003678 if( grandparent != NULL )
3679 {
3680 /*
3681 * Part of the chain
3682 */
Paul Bakker9a736322012-11-14 12:39:52 +00003683 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 +00003684 if( ret != 0 )
3685 return( ret );
Paul Bakkera5943852013-09-09 17:21:45 +02003686 }
Paul Bakker915275b2012-09-28 07:10:55 +00003687 else
3688 {
Paul Bakker9a736322012-11-14 12:39:52 +00003689 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 +00003690 if( ret != 0 )
3691 return( ret );
3692 }
3693
3694 /* child is verified to be a child of the parent, call verify callback */
3695 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003696 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003697 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003698
3699 *flags |= parent_flags;
3700
3701 return( 0 );
3702}
3703
Paul Bakker76fd75a2011-01-16 21:12:10 +00003704/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 * Verify the certificate validity
3706 */
3707int x509parse_verify( x509_cert *crt,
3708 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003709 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003710 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003711 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003712 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003713{
Paul Bakker23986e52011-04-24 08:57:21 +00003714 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003715 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003716 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003717 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003719 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Paul Bakker40ea7de2009-05-03 10:18:48 +00003721 *flags = 0;
3722
Paul Bakker5121ce52009-01-03 21:22:43 +00003723 if( cn != NULL )
3724 {
3725 name = &crt->subject;
3726 cn_len = strlen( cn );
3727
Paul Bakker4d2c1242012-05-10 14:12:46 +00003728 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003730 cur = &crt->subject_alt_names;
3731
3732 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003733 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003734 if( cur->buf.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003735 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003736 break;
3737
Paul Bakker535e97d2012-08-23 10:49:55 +00003738 if( cur->buf.len > 2 &&
3739 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003740 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003741 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
Paul Bakker4d2c1242012-05-10 14:12:46 +00003743 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003744 }
3745
3746 if( cur == NULL )
3747 *flags |= BADCERT_CN_MISMATCH;
3748 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003749 else
3750 {
3751 while( name != NULL )
3752 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003753 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003754 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003755 if( name->val.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003756 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003757 break;
3758
Paul Bakker535e97d2012-08-23 10:49:55 +00003759 if( name->val.len > 2 &&
3760 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003761 x509_wildcard_verify( cn, &name->val ) )
3762 break;
3763 }
3764
3765 name = name->next;
3766 }
3767
3768 if( name == NULL )
3769 *flags |= BADCERT_CN_MISMATCH;
3770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 }
3772
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003774 * Iterate upwards in the given cert chain, to find our crt parent.
3775 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003777 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
Paul Bakker76fd75a2011-01-16 21:12:10 +00003779 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003780 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003781 if( parent->ca_istrue == 0 ||
3782 crt->issuer_raw.len != parent->subject_raw.len ||
3783 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 crt->issuer_raw.len ) != 0 )
3785 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003786 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003787 continue;
3788 }
Paul Bakker915275b2012-09-28 07:10:55 +00003789 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 }
3791
Paul Bakker915275b2012-09-28 07:10:55 +00003792 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 {
Paul Bakker915275b2012-09-28 07:10:55 +00003794 /*
3795 * Part of the chain
3796 */
Paul Bakker9a736322012-11-14 12:39:52 +00003797 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003798 if( ret != 0 )
3799 return( ret );
3800 }
3801 else
Paul Bakker74111d32011-01-15 16:57:55 +00003802 {
Paul Bakker9a736322012-11-14 12:39:52 +00003803 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003804 if( ret != 0 )
3805 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003806 }
Paul Bakker915275b2012-09-28 07:10:55 +00003807
3808 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003809 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003810
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 return( 0 );
3812}
3813
3814/*
3815 * Unallocate all certificate data
3816 */
3817void x509_free( x509_cert *crt )
3818{
3819 x509_cert *cert_cur = crt;
3820 x509_cert *cert_prv;
3821 x509_name *name_cur;
3822 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003823 x509_sequence *seq_cur;
3824 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003825
3826 if( crt == NULL )
3827 return;
3828
3829 do
3830 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003831 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003832
3833 name_cur = cert_cur->issuer.next;
3834 while( name_cur != NULL )
3835 {
3836 name_prv = name_cur;
3837 name_cur = name_cur->next;
3838 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003839 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 }
3841
3842 name_cur = cert_cur->subject.next;
3843 while( name_cur != NULL )
3844 {
3845 name_prv = name_cur;
3846 name_cur = name_cur->next;
3847 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003848 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003849 }
3850
Paul Bakker74111d32011-01-15 16:57:55 +00003851 seq_cur = cert_cur->ext_key_usage.next;
3852 while( seq_cur != NULL )
3853 {
3854 seq_prv = seq_cur;
3855 seq_cur = seq_cur->next;
3856 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003857 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003858 }
3859
Paul Bakker8afa70d2012-02-11 18:42:45 +00003860 seq_cur = cert_cur->subject_alt_names.next;
3861 while( seq_cur != NULL )
3862 {
3863 seq_prv = seq_cur;
3864 seq_cur = seq_cur->next;
3865 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003866 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003867 }
3868
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 if( cert_cur->raw.p != NULL )
3870 {
3871 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003872 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 }
3874
3875 cert_cur = cert_cur->next;
3876 }
3877 while( cert_cur != NULL );
3878
3879 cert_cur = crt;
3880 do
3881 {
3882 cert_prv = cert_cur;
3883 cert_cur = cert_cur->next;
3884
3885 memset( cert_prv, 0, sizeof( x509_cert ) );
3886 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003887 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 }
3889 while( cert_cur != NULL );
3890}
3891
Paul Bakkerd98030e2009-05-02 15:13:40 +00003892/*
3893 * Unallocate all CRL data
3894 */
3895void x509_crl_free( x509_crl *crl )
3896{
3897 x509_crl *crl_cur = crl;
3898 x509_crl *crl_prv;
3899 x509_name *name_cur;
3900 x509_name *name_prv;
3901 x509_crl_entry *entry_cur;
3902 x509_crl_entry *entry_prv;
3903
3904 if( crl == NULL )
3905 return;
3906
3907 do
3908 {
3909 name_cur = crl_cur->issuer.next;
3910 while( name_cur != NULL )
3911 {
3912 name_prv = name_cur;
3913 name_cur = name_cur->next;
3914 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003915 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003916 }
3917
3918 entry_cur = crl_cur->entry.next;
3919 while( entry_cur != NULL )
3920 {
3921 entry_prv = entry_cur;
3922 entry_cur = entry_cur->next;
3923 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003924 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003925 }
3926
3927 if( crl_cur->raw.p != NULL )
3928 {
3929 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003930 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003931 }
3932
3933 crl_cur = crl_cur->next;
3934 }
3935 while( crl_cur != NULL );
3936
3937 crl_cur = crl;
3938 do
3939 {
3940 crl_prv = crl_cur;
3941 crl_cur = crl_cur->next;
3942
3943 memset( crl_prv, 0, sizeof( x509_crl ) );
3944 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003945 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003946 }
3947 while( crl_cur != NULL );
3948}
3949
Paul Bakker40e46942009-01-03 21:51:57 +00003950#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Paul Bakker40e46942009-01-03 21:51:57 +00003952#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
3954/*
3955 * Checkup routine
3956 */
3957int x509_self_test( int verbose )
3958{
Paul Bakker5690efc2011-05-26 13:16:06 +00003959#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003960 int ret;
3961 int flags;
3962 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 x509_cert cacert;
3964 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003965 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00003966#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003967 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003968#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
3970 if( verbose != 0 )
3971 printf( " X.509 certificate load: " );
3972
3973 memset( &clicert, 0, sizeof( x509_cert ) );
3974
Paul Bakker3c2122f2013-06-24 19:03:14 +02003975 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003976 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 if( ret != 0 )
3978 {
3979 if( verbose != 0 )
3980 printf( "failed\n" );
3981
3982 return( ret );
3983 }
3984
3985 memset( &cacert, 0, sizeof( x509_cert ) );
3986
Paul Bakker3c2122f2013-06-24 19:03:14 +02003987 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003988 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 if( ret != 0 )
3990 {
3991 if( verbose != 0 )
3992 printf( "failed\n" );
3993
3994 return( ret );
3995 }
3996
3997 if( verbose != 0 )
3998 printf( "passed\n X.509 private key load: " );
3999
4000 i = strlen( test_ca_key );
4001 j = strlen( test_ca_pwd );
4002
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004003 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00004004
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004005 if( ( ret = x509parse_key( &pkey,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004006 (const unsigned char *) test_ca_key, i,
4007 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 {
4009 if( verbose != 0 )
4010 printf( "failed\n" );
4011
4012 return( ret );
4013 }
4014
4015 if( verbose != 0 )
4016 printf( "passed\n X.509 signature verify: ");
4017
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004018 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019 if( ret != 0 )
4020 {
4021 if( verbose != 0 )
4022 printf( "failed\n" );
4023
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004024 printf("ret = %d, &flags = %04x\n", ret, flags);
4025
Paul Bakker5121ce52009-01-03 21:22:43 +00004026 return( ret );
4027 }
4028
Paul Bakker5690efc2011-05-26 13:16:06 +00004029#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004030 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004031 printf( "passed\n X.509 DHM parameter load: " );
4032
4033 i = strlen( test_dhm_params );
4034 j = strlen( test_ca_pwd );
4035
Paul Bakker3c2122f2013-06-24 19:03:14 +02004036 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004037 {
4038 if( verbose != 0 )
4039 printf( "failed\n" );
4040
4041 return( ret );
4042 }
4043
4044 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
4048 x509_free( &cacert );
4049 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004050 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00004051#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004052 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004054
4055 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004056#else
4057 ((void) verbose);
4058 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004060}
4061
4062#endif
4063
4064#endif