blob: 3fb970344ff7d43db12e20a41488730fb9aa5822 [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é-Gonnardf838eed2013-07-02 14:56:43 +0200214/* Get an EC group id from an ECParameters buffer
215 *
216 * ECParameters ::= CHOICE {
217 * namedCurve OBJECT IDENTIFIER
218 * -- implicitCurve NULL
219 * -- specifiedCurve SpecifiedECDomain
220 * }
221 */
222static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200223 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200224{
225 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200227 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
231
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200232 params->p = *p;
233 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200234
235 if( *p != end )
236 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
237 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
238
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200239 return( 0 );
240}
241
242/*
243 * Use EC parameters to initialise an EC group
244 */
245static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
246{
247 int ret;
248 ecp_group_id grp_id;
249
250 if( oid_get_ec_grp( params, &grp_id ) != 0 )
251 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
252
253 /*
254 * grp may already be initilialized; if so, make sure IDs match
255 */
256 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
257 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
258
259 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
260 return( ret );
261
262 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200263}
264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 * AttributeTypeAndValue ::= SEQUENCE {
267 * type AttributeType,
268 * value AttributeValue }
269 *
270 * AttributeType ::= OBJECT IDENTIFIER
271 *
272 * AttributeValue ::= ANY DEFINED BY AttributeType
273 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000274static int x509_get_attr_type_value( unsigned char **p,
275 const unsigned char *end,
276 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000277{
Paul Bakker23986e52011-04-24 08:57:21 +0000278 int ret;
279 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 x509_buf *oid;
281 x509_buf *val;
282
283 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000285 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 oid = &cur->oid;
288 oid->tag = **p;
289
290 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000291 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293 oid->p = *p;
294 *p += oid->len;
295
296 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000297 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000298 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
301 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
302 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000303 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000304 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306 val = &cur->val;
307 val->tag = *(*p)++;
308
309 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 val->p = *p;
313 *p += val->len;
314
315 cur->next = NULL;
316
Paul Bakker400ff6f2011-02-20 10:40:16 +0000317 return( 0 );
318}
319
320/*
321 * RelativeDistinguishedName ::=
322 * SET OF AttributeTypeAndValue
323 *
324 * AttributeTypeAndValue ::= SEQUENCE {
325 * type AttributeType,
326 * value AttributeValue }
327 *
328 * AttributeType ::= OBJECT IDENTIFIER
329 *
330 * AttributeValue ::= ANY DEFINED BY AttributeType
331 */
332static int x509_get_name( unsigned char **p,
333 const unsigned char *end,
334 x509_name *cur )
335{
Paul Bakker23986e52011-04-24 08:57:21 +0000336 int ret;
337 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000338 const unsigned char *end2;
339 x509_name *use;
340
341 if( ( ret = asn1_get_tag( p, end, &len,
342 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000343 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000344
345 end2 = end;
346 end = *p + len;
347 use = cur;
348
349 do
350 {
351 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
352 return( ret );
353
354 if( *p != end )
355 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200356 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000357 sizeof( x509_name ) );
358
359 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000360 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000361
362 memset( use->next, 0, sizeof( x509_name ) );
363
364 use = use->next;
365 }
366 }
367 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
369 /*
370 * recurse until end of SEQUENCE is reached
371 */
372 if( *p == end2 )
373 return( 0 );
374
Paul Bakker6e339b52013-07-03 13:37:05 +0200375 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 sizeof( x509_name ) );
377
378 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000379 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
Paul Bakker430ffbe2012-05-01 08:14:20 +0000381 memset( cur->next, 0, sizeof( x509_name ) );
382
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 return( x509_get_name( p, end2, cur->next ) );
384}
385
386/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 * Time ::= CHOICE {
388 * utcTime UTCTime,
389 * generalTime GeneralizedTime }
390 */
Paul Bakker91200182010-02-18 21:26:15 +0000391static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000392 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000393 x509_time *time )
394{
Paul Bakker23986e52011-04-24 08:57:21 +0000395 int ret;
396 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000397 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000398 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000399
Paul Bakker91200182010-02-18 21:26:15 +0000400 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000401 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
402 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403
Paul Bakker91200182010-02-18 21:26:15 +0000404 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000405
Paul Bakker91200182010-02-18 21:26:15 +0000406 if ( tag == ASN1_UTC_TIME )
407 {
408 (*p)++;
409 ret = asn1_get_len( p, end, &len );
410
411 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000412 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000413
Paul Bakker91200182010-02-18 21:26:15 +0000414 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000415 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
416 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000417
Paul Bakker91200182010-02-18 21:26:15 +0000418 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
419 &time->year, &time->mon, &time->day,
420 &time->hour, &time->min, &time->sec ) < 5 )
421 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422
Paul Bakker400ff6f2011-02-20 10:40:16 +0000423 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000424 time->year += 1900;
425
426 *p += len;
427
428 return( 0 );
429 }
430 else if ( tag == ASN1_GENERALIZED_TIME )
431 {
432 (*p)++;
433 ret = asn1_get_len( p, end, &len );
434
435 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000436 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000437
438 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000439 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
440 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000441
442 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
443 &time->year, &time->mon, &time->day,
444 &time->hour, &time->min, &time->sec ) < 5 )
445 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
446
447 *p += len;
448
449 return( 0 );
450 }
451 else
Paul Bakker9d781402011-05-09 16:17:09 +0000452 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000453}
454
455
456/*
457 * Validity ::= SEQUENCE {
458 * notBefore Time,
459 * notAfter Time }
460 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000461static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000462 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 x509_time *from,
464 x509_time *to )
465{
Paul Bakker23986e52011-04-24 08:57:21 +0000466 int ret;
467 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 if( ( ret = asn1_get_tag( p, end, &len,
470 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000471 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 end = *p + len;
474
Paul Bakker91200182010-02-18 21:26:15 +0000475 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000476 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
Paul Bakker91200182010-02-18 21:26:15 +0000478 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000483 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 return( 0 );
486}
487
488/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200489 * RSAPublicKey ::= SEQUENCE {
490 * modulus INTEGER, -- n
491 * publicExponent INTEGER -- e
492 * }
493 */
494static int x509_get_rsapubkey( unsigned char **p,
495 const unsigned char *end,
496 rsa_context *rsa )
497{
498 int ret;
499 size_t len;
500
501 if( ( ret = asn1_get_tag( p, end, &len,
502 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
503 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
504
505 if( *p + len != end )
506 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
507 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
508
509 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
510 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
511 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
512
513 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
514 return( ret );
515
516 rsa->len = mpi_size( &rsa->N );
517
518 return( 0 );
519}
520
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200521static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200522 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200523{
524 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200525
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200526 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200527 (const unsigned char *) *p, end - *p ) ) != 0 ||
528 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200529 {
530 ecp_keypair_free( key );
531 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200532 }
533
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200534 /*
535 * We know ecp_point_read_binary consumed all bytes
536 */
537 *p = (unsigned char *) end;
538
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200539 return( 0 );
540}
541
542/*
543 * SubjectPublicKeyInfo ::= SEQUENCE {
544 * algorithm AlgorithmIdentifier,
545 * subjectPublicKey BIT STRING }
546 */
547static int x509_get_pubkey( unsigned char **p,
548 const unsigned char *end,
549 pk_context *pk )
550{
551 int ret;
552 size_t len;
553 x509_buf alg_params;
554 pk_type_t pk_alg = POLARSSL_PK_NONE;
555
556 if( ( ret = asn1_get_tag( p, end, &len,
557 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
558 {
559 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
560 }
561
562 end = *p + len;
563
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200564 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200565 return( ret );
566
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200567 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200568 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
569
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200570 if( *p + len != end )
571 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
572 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
573
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200574 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
575 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200576
577 switch( pk_alg )
578 {
579 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200580 case POLARSSL_PK_ECDSA:
581 /* Should never happen */
582 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200583 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200584
585 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200586 ret = x509_get_rsapubkey( p, end, pk->data );
587 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200588
589 case POLARSSL_PK_ECKEY_DH:
590 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
591 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200592
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200593 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200594 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
595 x509_get_ecpubkey( p, end, pk->data );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200596 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200597 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200598
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200599 if( ret == 0 && *p != end )
600 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
601 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
602
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200603 if( ret != 0 )
604 pk_free( pk );
605
606 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200607}
608
Paul Bakker5121ce52009-01-03 21:22:43 +0000609static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000610 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 x509_buf *sig )
612{
Paul Bakker23986e52011-04-24 08:57:21 +0000613 int ret;
614 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Paul Bakker8afa70d2012-02-11 18:42:45 +0000616 if( ( end - *p ) < 1 )
617 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
618 POLARSSL_ERR_ASN1_OUT_OF_DATA );
619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 sig->tag = **p;
621
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200622 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000623 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 sig->len = len;
626 sig->p = *p;
627
628 *p += len;
629
630 return( 0 );
631}
632
633/*
634 * X.509 v2/v3 unique identifier (not parsed)
635 */
636static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000637 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 x509_buf *uid, int n )
639{
640 int ret;
641
642 if( *p == end )
643 return( 0 );
644
645 uid->tag = **p;
646
647 if( ( ret = asn1_get_tag( p, end, &uid->len,
648 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
649 {
Paul Bakker40e46942009-01-03 21:51:57 +0000650 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 return( 0 );
652
653 return( ret );
654 }
655
656 uid->p = *p;
657 *p += uid->len;
658
659 return( 0 );
660}
661
662/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000663 * X.509 Extensions (No parsing of extensions, pointer should
664 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 */
666static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000667 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000668 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000669{
Paul Bakker23986e52011-04-24 08:57:21 +0000670 int ret;
671 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 if( *p == end )
674 return( 0 );
675
676 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000677
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000679 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682 ext->p = *p;
683 end = *p + ext->len;
684
685 /*
686 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
687 *
688 * Extension ::= SEQUENCE {
689 * extnID OBJECT IDENTIFIER,
690 * critical BOOLEAN DEFAULT FALSE,
691 * extnValue OCTET STRING }
692 */
693 if( ( ret = asn1_get_tag( p, end, &len,
694 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000695 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000698 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000699 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Paul Bakkerd98030e2009-05-02 15:13:40 +0000701 return( 0 );
702}
703
704/*
705 * X.509 CRL v2 extensions (no extensions parsed yet.)
706 */
707static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000708 const unsigned char *end,
709 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000710{
Paul Bakker23986e52011-04-24 08:57:21 +0000711 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000712 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000713
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000714 /* Get explicit tag */
715 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000716 {
717 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
718 return( 0 );
719
720 return( ret );
721 }
722
723 while( *p < end )
724 {
725 if( ( ret = asn1_get_tag( p, end, &len,
726 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000727 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000728
729 *p += len;
730 }
731
732 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
735
736 return( 0 );
737}
738
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000739/*
740 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
741 */
742static int x509_get_crl_entry_ext( unsigned char **p,
743 const unsigned char *end,
744 x509_buf *ext )
745{
746 int ret;
747 size_t len = 0;
748
749 /* OPTIONAL */
750 if (end <= *p)
751 return( 0 );
752
753 ext->tag = **p;
754 ext->p = *p;
755
756 /*
757 * Get CRL-entry extension sequence header
758 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
759 */
760 if( ( ret = asn1_get_tag( p, end, &ext->len,
761 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
762 {
763 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
764 {
765 ext->p = NULL;
766 return( 0 );
767 }
768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
769 }
770
771 end = *p + ext->len;
772
773 if( end != *p + ext->len )
774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
775 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
776
777 while( *p < end )
778 {
779 if( ( ret = asn1_get_tag( p, end, &len,
780 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
782
783 *p += len;
784 }
785
786 if( *p != end )
787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
788 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
789
790 return( 0 );
791}
792
Paul Bakker74111d32011-01-15 16:57:55 +0000793static int x509_get_basic_constraints( unsigned char **p,
794 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000795 int *ca_istrue,
796 int *max_pathlen )
797{
Paul Bakker23986e52011-04-24 08:57:21 +0000798 int ret;
799 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000800
801 /*
802 * BasicConstraints ::= SEQUENCE {
803 * cA BOOLEAN DEFAULT FALSE,
804 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
805 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000806 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000807 *max_pathlen = 0; /* endless */
808
809 if( ( ret = asn1_get_tag( p, end, &len,
810 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000811 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000812
813 if( *p == end )
814 return 0;
815
Paul Bakker3cccddb2011-01-16 21:46:31 +0000816 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000817 {
818 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000819 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000820
821 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
Paul Bakker3cccddb2011-01-16 21:46:31 +0000824 if( *ca_istrue != 0 )
825 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000826 }
827
828 if( *p == end )
829 return 0;
830
831 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000832 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000833
834 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000836 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
837
838 (*max_pathlen)++;
839
Paul Bakker74111d32011-01-15 16:57:55 +0000840 return 0;
841}
842
843static int x509_get_ns_cert_type( unsigned char **p,
844 const unsigned char *end,
845 unsigned char *ns_cert_type)
846{
847 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000848 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000849
850 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000852
853 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000855 POLARSSL_ERR_ASN1_INVALID_LENGTH );
856
857 /* Get actual bitstring */
858 *ns_cert_type = *bs.p;
859 return 0;
860}
861
862static int x509_get_key_usage( unsigned char **p,
863 const unsigned char *end,
864 unsigned char *key_usage)
865{
866 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000867 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000868
869 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000871
Paul Bakker94a67962012-08-23 13:03:52 +0000872 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000874 POLARSSL_ERR_ASN1_INVALID_LENGTH );
875
876 /* Get actual bitstring */
877 *key_usage = *bs.p;
878 return 0;
879}
880
Paul Bakkerd98030e2009-05-02 15:13:40 +0000881/*
Paul Bakker74111d32011-01-15 16:57:55 +0000882 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
883 *
884 * KeyPurposeId ::= OBJECT IDENTIFIER
885 */
886static int x509_get_ext_key_usage( unsigned char **p,
887 const unsigned char *end,
888 x509_sequence *ext_key_usage)
889{
890 int ret;
891
892 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000893 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000894
895 /* Sequence length must be >= 1 */
896 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000898 POLARSSL_ERR_ASN1_INVALID_LENGTH );
899
900 return 0;
901}
902
903/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000904 * SubjectAltName ::= GeneralNames
905 *
906 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
907 *
908 * GeneralName ::= CHOICE {
909 * otherName [0] OtherName,
910 * rfc822Name [1] IA5String,
911 * dNSName [2] IA5String,
912 * x400Address [3] ORAddress,
913 * directoryName [4] Name,
914 * ediPartyName [5] EDIPartyName,
915 * uniformResourceIdentifier [6] IA5String,
916 * iPAddress [7] OCTET STRING,
917 * registeredID [8] OBJECT IDENTIFIER }
918 *
919 * OtherName ::= SEQUENCE {
920 * type-id OBJECT IDENTIFIER,
921 * value [0] EXPLICIT ANY DEFINED BY type-id }
922 *
923 * EDIPartyName ::= SEQUENCE {
924 * nameAssigner [0] DirectoryString OPTIONAL,
925 * partyName [1] DirectoryString }
926 *
927 * NOTE: PolarSSL only parses and uses dNSName at this point.
928 */
929static int x509_get_subject_alt_name( unsigned char **p,
930 const unsigned char *end,
931 x509_sequence *subject_alt_name )
932{
933 int ret;
934 size_t len, tag_len;
935 asn1_buf *buf;
936 unsigned char tag;
937 asn1_sequence *cur = subject_alt_name;
938
939 /* Get main sequence tag */
940 if( ( ret = asn1_get_tag( p, end, &len,
941 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
942 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
943
944 if( *p + len != end )
945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
946 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
947
948 while( *p < end )
949 {
950 if( ( end - *p ) < 1 )
951 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
952 POLARSSL_ERR_ASN1_OUT_OF_DATA );
953
954 tag = **p;
955 (*p)++;
956 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
958
959 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
960 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
961 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
962
963 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
964 {
965 *p += tag_len;
966 continue;
967 }
968
969 buf = &(cur->buf);
970 buf->tag = tag;
971 buf->p = *p;
972 buf->len = tag_len;
973 *p += buf->len;
974
975 /* Allocate and assign next pointer */
976 if (*p < end)
977 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200978 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000979 sizeof( asn1_sequence ) );
980
981 if( cur->next == NULL )
982 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
983 POLARSSL_ERR_ASN1_MALLOC_FAILED );
984
Paul Bakker535e97d2012-08-23 10:49:55 +0000985 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000986 cur = cur->next;
987 }
988 }
989
990 /* Set final sequence entry's next pointer to NULL */
991 cur->next = NULL;
992
993 if( *p != end )
994 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
995 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
996
997 return( 0 );
998}
999
1000/*
Paul Bakker74111d32011-01-15 16:57:55 +00001001 * X.509 v3 extensions
1002 *
1003 * TODO: Perform all of the basic constraints tests required by the RFC
1004 * TODO: Set values for undetected extensions to a sane default?
1005 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001006 */
1007static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001008 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001009 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001010{
Paul Bakker23986e52011-04-24 08:57:21 +00001011 int ret;
1012 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001013 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001014
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001015 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001016 {
1017 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1018 return( 0 );
1019
1020 return( ret );
1021 }
1022
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 while( *p < end )
1024 {
Paul Bakker74111d32011-01-15 16:57:55 +00001025 /*
1026 * Extension ::= SEQUENCE {
1027 * extnID OBJECT IDENTIFIER,
1028 * critical BOOLEAN DEFAULT FALSE,
1029 * extnValue OCTET STRING }
1030 */
1031 x509_buf extn_oid = {0, 0, NULL};
1032 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001033 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001034
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 if( ( ret = asn1_get_tag( p, end, &len,
1036 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001037 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001038
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001039 end_ext_data = *p + len;
1040
Paul Bakker74111d32011-01-15 16:57:55 +00001041 /* Get extension ID */
1042 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Paul Bakker74111d32011-01-15 16:57:55 +00001044 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001045 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Paul Bakker74111d32011-01-15 16:57:55 +00001047 extn_oid.p = *p;
1048 *p += extn_oid.len;
1049
1050 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001051 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001052 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1053
1054 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001055 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001056 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001057 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Paul Bakker74111d32011-01-15 16:57:55 +00001059 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001060 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001062 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001064 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001065
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001066 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001067 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001068 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Paul Bakker74111d32011-01-15 16:57:55 +00001070 /*
1071 * Detect supported extensions
1072 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001073 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1074
1075 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001076 {
1077 /* No parser found, skip extension */
1078 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Paul Bakker5c721f92011-07-27 16:51:09 +00001080#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001081 if( is_critical )
1082 {
1083 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001085 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1086 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001087#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001088 continue;
1089 }
1090
1091 crt->ext_types |= ext_type;
1092
1093 switch( ext_type )
1094 {
1095 case EXT_BASIC_CONSTRAINTS:
1096 /* Parse basic constraints */
1097 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1098 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1099 return ( ret );
1100 break;
1101
1102 case EXT_KEY_USAGE:
1103 /* Parse key usage */
1104 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1105 &crt->key_usage ) ) != 0 )
1106 return ( ret );
1107 break;
1108
1109 case EXT_EXTENDED_KEY_USAGE:
1110 /* Parse extended key usage */
1111 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1112 &crt->ext_key_usage ) ) != 0 )
1113 return ( ret );
1114 break;
1115
1116 case EXT_SUBJECT_ALT_NAME:
1117 /* Parse subject alt name */
1118 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1119 &crt->subject_alt_names ) ) != 0 )
1120 return ( ret );
1121 break;
1122
1123 case EXT_NS_CERT_TYPE:
1124 /* Parse netscape certificate type */
1125 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1126 &crt->ns_cert_type ) ) != 0 )
1127 return ( ret );
1128 break;
1129
1130 default:
1131 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001132 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 }
1134
1135 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001136 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001137 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 return( 0 );
1140}
1141
1142/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001143 * X.509 CRL Entries
1144 */
1145static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001146 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001147 x509_crl_entry *entry )
1148{
Paul Bakker23986e52011-04-24 08:57:21 +00001149 int ret;
1150 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001151 x509_crl_entry *cur_entry = entry;
1152
1153 if( *p == end )
1154 return( 0 );
1155
Paul Bakker9be19372009-07-27 20:21:53 +00001156 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001157 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1158 {
1159 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1160 return( 0 );
1161
1162 return( ret );
1163 }
1164
Paul Bakker9be19372009-07-27 20:21:53 +00001165 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001166
1167 while( *p < end )
1168 {
Paul Bakker23986e52011-04-24 08:57:21 +00001169 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001170 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001171
1172 if( ( ret = asn1_get_tag( p, end, &len2,
1173 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1174 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001175 return( ret );
1176 }
1177
Paul Bakker9be19372009-07-27 20:21:53 +00001178 cur_entry->raw.tag = **p;
1179 cur_entry->raw.p = *p;
1180 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001181 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001182
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001183 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001184 return( ret );
1185
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001186 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001187 return( ret );
1188
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001189 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001190 return( ret );
1191
Paul Bakker74111d32011-01-15 16:57:55 +00001192 if ( *p < end )
1193 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001194 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001195
1196 if( cur_entry->next == NULL )
1197 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1198
Paul Bakkerd98030e2009-05-02 15:13:40 +00001199 cur_entry = cur_entry->next;
1200 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1201 }
1202 }
1203
1204 return( 0 );
1205}
1206
Paul Bakkerc70b9822013-04-07 22:00:46 +02001207static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1208 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001209{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001210 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001211
Paul Bakkerc70b9822013-04-07 22:00:46 +02001212 if( ret != 0 )
1213 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001214
Paul Bakkerc70b9822013-04-07 22:00:46 +02001215 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001216}
1217
Paul Bakkerd98030e2009-05-02 15:13:40 +00001218/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001219 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001221static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1222 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001223{
Paul Bakker23986e52011-04-24 08:57:21 +00001224 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001225 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001226 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001227
Paul Bakker320a4b52009-03-28 18:52:39 +00001228 /*
1229 * Check for valid input
1230 */
1231 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001232 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001233
Paul Bakker6e339b52013-07-03 13:37:05 +02001234 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001235
1236 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001237 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001238
1239 memcpy( p, buf, buflen );
1240
1241 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
1243 crt->raw.p = p;
1244 crt->raw.len = len;
1245 end = p + len;
1246
1247 /*
1248 * Certificate ::= SEQUENCE {
1249 * tbsCertificate TBSCertificate,
1250 * signatureAlgorithm AlgorithmIdentifier,
1251 * signatureValue BIT STRING }
1252 */
1253 if( ( ret = asn1_get_tag( &p, end, &len,
1254 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1255 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001256 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001257 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259
Paul Bakkerb00ca422012-09-25 12:10:00 +00001260 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001263 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001264 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001266 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001267
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 /*
1269 * TBSCertificate ::= SEQUENCE {
1270 */
1271 crt->tbs.p = p;
1272
1273 if( ( ret = asn1_get_tag( &p, end, &len,
1274 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001277 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 }
1279
1280 end = p + len;
1281 crt->tbs.len = end - crt->tbs.p;
1282
1283 /*
1284 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1285 *
1286 * CertificateSerialNumber ::= INTEGER
1287 *
1288 * signature AlgorithmIdentifier
1289 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001290 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1291 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1292 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001294 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 return( ret );
1296 }
1297
1298 crt->version++;
1299
1300 if( crt->version > 3 )
1301 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001302 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001303 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 }
1305
Paul Bakkerc70b9822013-04-07 22:00:46 +02001306 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1307 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001310 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
1312
1313 /*
1314 * issuer Name
1315 */
1316 crt->issuer_raw.p = p;
1317
1318 if( ( ret = asn1_get_tag( &p, end, &len,
1319 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1320 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001321 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001322 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
1324
1325 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1326 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001327 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 return( ret );
1329 }
1330
1331 crt->issuer_raw.len = p - crt->issuer_raw.p;
1332
1333 /*
1334 * Validity ::= SEQUENCE {
1335 * notBefore Time,
1336 * notAfter Time }
1337 *
1338 */
1339 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1340 &crt->valid_to ) ) != 0 )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 return( ret );
1344 }
1345
1346 /*
1347 * subject Name
1348 */
1349 crt->subject_raw.p = p;
1350
1351 if( ( ret = asn1_get_tag( &p, end, &len,
1352 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001355 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
1357
Paul Bakkercefb3962012-06-27 11:51:09 +00001358 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 return( ret );
1362 }
1363
1364 crt->subject_raw.len = p - crt->subject_raw.p;
1365
1366 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001367 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001369 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001371 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 return( ret );
1373 }
1374
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 /*
1376 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1377 * -- If present, version shall be v2 or v3
1378 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1379 * -- If present, version shall be v2 or v3
1380 * extensions [3] EXPLICIT Extensions OPTIONAL
1381 * -- If present, version shall be v3
1382 */
1383 if( crt->version == 2 || crt->version == 3 )
1384 {
1385 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1386 if( ret != 0 )
1387 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001388 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 return( ret );
1390 }
1391 }
1392
1393 if( crt->version == 2 || crt->version == 3 )
1394 {
1395 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1396 if( ret != 0 )
1397 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001398 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 return( ret );
1400 }
1401 }
1402
1403 if( crt->version == 3 )
1404 {
Paul Bakker74111d32011-01-15 16:57:55 +00001405 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 if( ret != 0 )
1407 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001408 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 return( ret );
1410 }
1411 }
1412
1413 if( p != end )
1414 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001416 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001417 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 }
1419
Paul Bakkerb00ca422012-09-25 12:10:00 +00001420 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001423 * }
1424 * -- end of TBSCertificate
1425 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 * signatureAlgorithm AlgorithmIdentifier,
1427 * signatureValue BIT STRING
1428 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001429 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001431 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 return( ret );
1433 }
1434
Paul Bakker535e97d2012-08-23 10:49:55 +00001435 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1436 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001438 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001439 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
1442 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1443 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001444 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 return( ret );
1446 }
1447
1448 if( p != end )
1449 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001450 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001451 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001452 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001455 return( 0 );
1456}
1457
1458/*
Paul Bakker42c65812013-06-24 19:21:59 +02001459 * Parse one X.509 certificate in DER format from a buffer and add them to a
1460 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001461 */
Paul Bakker42c65812013-06-24 19:21:59 +02001462int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463{
Paul Bakker42c65812013-06-24 19:21:59 +02001464 int ret;
1465 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466
1467 /*
1468 * Check for valid input
1469 */
1470 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001471 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472
1473 while( crt->version != 0 && crt->next != NULL )
1474 {
1475 prev = crt;
1476 crt = crt->next;
1477 }
1478
1479 /*
1480 * Add new certificate on the end of the chain if needed.
1481 */
1482 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001483 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001484 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001485
Paul Bakker7d06ad22009-05-02 15:53:56 +00001486 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001487 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001488
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001489 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001490 crt = crt->next;
1491 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Paul Bakker42c65812013-06-24 19:21:59 +02001494 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1495 {
1496 if( prev )
1497 prev->next = NULL;
1498
1499 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001500 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001501
1502 return( ret );
1503 }
1504
1505 return( 0 );
1506}
1507
1508/*
1509 * Parse one or more PEM certificates from a buffer and add them to the chained list
1510 */
1511int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1512{
1513 int ret, success = 0, first_error = 0, total_failed = 0;
1514 int buf_format = X509_FORMAT_DER;
1515
1516 /*
1517 * Check for valid input
1518 */
1519 if( chain == NULL || buf == NULL )
1520 return( POLARSSL_ERR_X509_INVALID_INPUT );
1521
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001522 /*
1523 * Determine buffer content. Buffer contains either one DER certificate or
1524 * one or more PEM certificates.
1525 */
1526#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001527 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001528 buf_format = X509_FORMAT_PEM;
1529#endif
1530
1531 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001532 return x509parse_crt_der( chain, buf, buflen );
1533
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001534#if defined(POLARSSL_PEM_C)
1535 if( buf_format == X509_FORMAT_PEM )
1536 {
1537 pem_context pem;
1538
1539 while( buflen > 0 )
1540 {
1541 size_t use_len;
1542 pem_init( &pem );
1543
1544 ret = pem_read_buffer( &pem,
1545 "-----BEGIN CERTIFICATE-----",
1546 "-----END CERTIFICATE-----",
1547 buf, NULL, 0, &use_len );
1548
1549 if( ret == 0 )
1550 {
1551 /*
1552 * Was PEM encoded
1553 */
1554 buflen -= use_len;
1555 buf += use_len;
1556 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001557 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1558 {
1559 return( ret );
1560 }
Paul Bakker00b28602013-06-24 13:02:41 +02001561 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001562 {
1563 pem_free( &pem );
1564
Paul Bakker5ed3b342013-06-24 19:05:46 +02001565 /*
1566 * PEM header and footer were found
1567 */
1568 buflen -= use_len;
1569 buf += use_len;
1570
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001571 if( first_error == 0 )
1572 first_error = ret;
1573
1574 continue;
1575 }
1576 else
1577 break;
1578
Paul Bakker42c65812013-06-24 19:21:59 +02001579 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001580
1581 pem_free( &pem );
1582
1583 if( ret != 0 )
1584 {
1585 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001586 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001587 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001588 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001589 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001590
1591 if( first_error == 0 )
1592 first_error = ret;
1593
Paul Bakker42c65812013-06-24 19:21:59 +02001594 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001595 continue;
1596 }
1597
1598 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001599 }
1600 }
1601#endif
1602
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001603 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001604 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001605 else if( first_error )
1606 return( first_error );
1607 else
1608 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001609}
1610
1611/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 * Parse one or more CRLs and add them to the chained list
1613 */
Paul Bakker23986e52011-04-24 08:57:21 +00001614int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615{
Paul Bakker23986e52011-04-24 08:57:21 +00001616 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001617 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001618 unsigned char *p, *end;
1619 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001620#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001621 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001622 pem_context pem;
1623#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624
1625 crl = chain;
1626
1627 /*
1628 * Check for valid input
1629 */
1630 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001631 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632
1633 while( crl->version != 0 && crl->next != NULL )
1634 crl = crl->next;
1635
1636 /*
1637 * Add new CRL on the end of the chain if needed.
1638 */
1639 if ( crl->version != 0 && crl->next == NULL)
1640 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001641 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642
Paul Bakker7d06ad22009-05-02 15:53:56 +00001643 if( crl->next == NULL )
1644 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001646 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001647 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648
Paul Bakker7d06ad22009-05-02 15:53:56 +00001649 crl = crl->next;
1650 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001651 }
1652
Paul Bakker96743fc2011-02-12 14:30:57 +00001653#if defined(POLARSSL_PEM_C)
1654 pem_init( &pem );
1655 ret = pem_read_buffer( &pem,
1656 "-----BEGIN X509 CRL-----",
1657 "-----END X509 CRL-----",
1658 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659
Paul Bakker96743fc2011-02-12 14:30:57 +00001660 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001661 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001662 /*
1663 * Was PEM encoded
1664 */
1665 buflen -= use_len;
1666 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001667
1668 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001669 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001671 p = pem.buf;
1672 pem.buf = NULL;
1673 len = pem.buflen;
1674 pem_free( &pem );
1675 }
Paul Bakker00b28602013-06-24 13:02:41 +02001676 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001677 {
1678 pem_free( &pem );
1679 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 }
1681 else
1682 {
1683 /*
1684 * nope, copy the raw DER data
1685 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001686 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687
1688 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001689 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690
1691 memcpy( p, buf, buflen );
1692
1693 buflen = 0;
1694 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001695#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001696 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001697
1698 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001699 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001700
1701 memcpy( p, buf, buflen );
1702
1703 buflen = 0;
1704#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705
1706 crl->raw.p = p;
1707 crl->raw.len = len;
1708 end = p + len;
1709
1710 /*
1711 * CertificateList ::= SEQUENCE {
1712 * tbsCertList TBSCertList,
1713 * signatureAlgorithm AlgorithmIdentifier,
1714 * signatureValue BIT STRING }
1715 */
1716 if( ( ret = asn1_get_tag( &p, end, &len,
1717 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1718 {
1719 x509_crl_free( crl );
1720 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1721 }
1722
Paul Bakker23986e52011-04-24 08:57:21 +00001723 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001724 {
1725 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001726 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1728 }
1729
1730 /*
1731 * TBSCertList ::= SEQUENCE {
1732 */
1733 crl->tbs.p = p;
1734
1735 if( ( ret = asn1_get_tag( &p, end, &len,
1736 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1737 {
1738 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001739 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740 }
1741
1742 end = p + len;
1743 crl->tbs.len = end - crl->tbs.p;
1744
1745 /*
1746 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1747 * -- if present, MUST be v2
1748 *
1749 * signature AlgorithmIdentifier
1750 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001751 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001752 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001753 {
1754 x509_crl_free( crl );
1755 return( ret );
1756 }
1757
1758 crl->version++;
1759
1760 if( crl->version > 2 )
1761 {
1762 x509_crl_free( crl );
1763 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1764 }
1765
Paul Bakkerc70b9822013-04-07 22:00:46 +02001766 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1767 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001768 {
1769 x509_crl_free( crl );
1770 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1771 }
1772
1773 /*
1774 * issuer Name
1775 */
1776 crl->issuer_raw.p = p;
1777
1778 if( ( ret = asn1_get_tag( &p, end, &len,
1779 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1780 {
1781 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001782 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783 }
1784
1785 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1786 {
1787 x509_crl_free( crl );
1788 return( ret );
1789 }
1790
1791 crl->issuer_raw.len = p - crl->issuer_raw.p;
1792
1793 /*
1794 * thisUpdate Time
1795 * nextUpdate Time OPTIONAL
1796 */
Paul Bakker91200182010-02-18 21:26:15 +00001797 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798 {
1799 x509_crl_free( crl );
1800 return( ret );
1801 }
1802
Paul Bakker91200182010-02-18 21:26:15 +00001803 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 {
Paul Bakker9d781402011-05-09 16:17:09 +00001805 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001806 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001807 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001808 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001809 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 x509_crl_free( crl );
1811 return( ret );
1812 }
1813 }
1814
1815 /*
1816 * revokedCertificates SEQUENCE OF SEQUENCE {
1817 * userCertificate CertificateSerialNumber,
1818 * revocationDate Time,
1819 * crlEntryExtensions Extensions OPTIONAL
1820 * -- if present, MUST be v2
1821 * } OPTIONAL
1822 */
1823 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1824 {
1825 x509_crl_free( crl );
1826 return( ret );
1827 }
1828
1829 /*
1830 * crlExtensions EXPLICIT Extensions OPTIONAL
1831 * -- if present, MUST be v2
1832 */
1833 if( crl->version == 2 )
1834 {
1835 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1836
1837 if( ret != 0 )
1838 {
1839 x509_crl_free( crl );
1840 return( ret );
1841 }
1842 }
1843
1844 if( p != end )
1845 {
1846 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001847 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1849 }
1850
1851 end = crl->raw.p + crl->raw.len;
1852
1853 /*
1854 * signatureAlgorithm AlgorithmIdentifier,
1855 * signatureValue BIT STRING
1856 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001857 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001858 {
1859 x509_crl_free( crl );
1860 return( ret );
1861 }
1862
Paul Bakker535e97d2012-08-23 10:49:55 +00001863 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1864 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865 {
1866 x509_crl_free( crl );
1867 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1868 }
1869
1870 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1871 {
1872 x509_crl_free( crl );
1873 return( ret );
1874 }
1875
1876 if( p != end )
1877 {
1878 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001879 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1881 }
1882
1883 if( buflen > 0 )
1884 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001885 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001886
Paul Bakker7d06ad22009-05-02 15:53:56 +00001887 if( crl->next == NULL )
1888 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001889 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001890 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001891 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892
Paul Bakker7d06ad22009-05-02 15:53:56 +00001893 crl = crl->next;
1894 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001895
1896 return( x509parse_crl( crl, buf, buflen ) );
1897 }
1898
1899 return( 0 );
1900}
1901
Paul Bakker335db3f2011-04-25 15:28:35 +00001902#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001904 * Load all data from a file into a given buffer.
1905 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001906static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001907{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001909
Paul Bakkerd98030e2009-05-02 15:13:40 +00001910 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001911 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001912
Paul Bakkerd98030e2009-05-02 15:13:40 +00001913 fseek( f, 0, SEEK_END );
1914 *n = (size_t) ftell( f );
1915 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001916
Paul Bakker6e339b52013-07-03 13:37:05 +02001917 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001918 {
1919 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001920 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001921 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001922
Paul Bakkerd98030e2009-05-02 15:13:40 +00001923 if( fread( *buf, 1, *n, f ) != *n )
1924 {
1925 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001926 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001927 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001928 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001929
Paul Bakkerd98030e2009-05-02 15:13:40 +00001930 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001931
Paul Bakkerd98030e2009-05-02 15:13:40 +00001932 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001933
Paul Bakkerd98030e2009-05-02 15:13:40 +00001934 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001935}
1936
1937/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 * Load one or more certificates and add them to the chained list
1939 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001940int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001941{
1942 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 size_t n;
1944 unsigned char *buf;
1945
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001946 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001947 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Paul Bakker69e095c2011-12-10 21:55:01 +00001949 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001950
1951 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001952 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
1954 return( ret );
1955}
1956
Paul Bakker8d914582012-06-04 12:46:42 +00001957int x509parse_crtpath( x509_cert *chain, const char *path )
1958{
1959 int ret = 0;
1960#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001961 int w_ret;
1962 WCHAR szDir[MAX_PATH];
1963 char filename[MAX_PATH];
1964 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001965 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001966
Paul Bakker97872ac2012-11-02 12:53:26 +00001967 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001968 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001969
1970 if( len > MAX_PATH - 3 )
1971 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001972
Paul Bakker3338b792012-10-01 21:13:10 +00001973 memset( szDir, 0, sizeof(szDir) );
1974 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001975 memcpy( filename, path, len );
1976 filename[len++] = '\\';
1977 p = filename + len;
1978 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001979
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001980 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001981
Paul Bakker97872ac2012-11-02 12:53:26 +00001982 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001983 if (hFind == INVALID_HANDLE_VALUE)
1984 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1985
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001986 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001987 do
1988 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001989 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001990
Paul Bakkere4791f32012-06-04 21:29:15 +00001991 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001992 continue;
1993
Paul Bakker3338b792012-10-01 21:13:10 +00001994 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1995 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001996 p, len - 1,
1997 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001998
Paul Bakker3338b792012-10-01 21:13:10 +00001999 w_ret = x509parse_crtfile( chain, filename );
2000 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002001 ret++;
2002 else
2003 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002004 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002005 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002006
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002007 if (GetLastError() != ERROR_NO_MORE_FILES)
2008 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002009
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002010cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002011 FindClose( hFind );
2012#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002013 int t_ret, i;
2014 struct stat sb;
2015 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002016 char entry_name[255];
2017 DIR *dir = opendir( path );
2018
2019 if( dir == NULL)
2020 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2021
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002022 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002023 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002024 if( result == NULL )
2025 break;
2026
2027 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2028
2029 i = stat( entry_name, &sb );
2030
2031 if( i == -1 )
2032 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2033
2034 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002035 continue;
2036
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002037 // Ignore parse errors
2038 //
Paul Bakker8d914582012-06-04 12:46:42 +00002039 t_ret = x509parse_crtfile( chain, entry_name );
2040 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002041 ret++;
2042 else
2043 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002044 }
2045 closedir( dir );
2046#endif
2047
2048 return( ret );
2049}
2050
Paul Bakkerd98030e2009-05-02 15:13:40 +00002051/*
2052 * Load one or more CRLs and add them to the chained list
2053 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002054int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002055{
2056 int ret;
2057 size_t n;
2058 unsigned char *buf;
2059
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002060 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002061 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002062
Paul Bakker27fdf462011-06-09 13:55:13 +00002063 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002064
2065 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002066 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002067
2068 return( ret );
2069}
2070
Paul Bakker5121ce52009-01-03 21:22:43 +00002071/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002072 * Load and parse a private RSA key
2073 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002074int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002075{
2076 int ret;
2077 size_t n;
2078 unsigned char *buf;
2079
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002080 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002081 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002082
2083 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002084 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002085 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002086 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002087 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002088
2089 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002090 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002091
2092 return( ret );
2093}
2094
2095/*
2096 * Load and parse a public RSA key
2097 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002098int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002099{
2100 int ret;
2101 size_t n;
2102 unsigned char *buf;
2103
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002104 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002105 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002106
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002107 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002108
2109 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002110 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002111
2112 return( ret );
2113}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002114
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002115/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002116 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002117 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002118int x509parse_keyfile( pk_context *ctx,
2119 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002120{
2121 int ret;
2122 size_t n;
2123 unsigned char *buf;
2124
2125 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2126 return( ret );
2127
2128 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002129 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002130 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002131 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002132 (const unsigned char *) pwd, strlen( pwd ) );
2133
2134 memset( buf, 0, n + 1 );
2135 free( buf );
2136
2137 return( ret );
2138}
2139
2140/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002141 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002142 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002143int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002144{
2145 int ret;
2146 size_t n;
2147 unsigned char *buf;
2148
2149 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2150 return( ret );
2151
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002152 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002153
2154 memset( buf, 0, n + 1 );
2155 free( buf );
2156
2157 return( ret );
2158}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002159
Paul Bakker335db3f2011-04-25 15:28:35 +00002160#endif /* POLARSSL_FS_IO */
2161
2162/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002163 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002165static int x509parse_key_pkcs1_der( rsa_context *rsa,
2166 const unsigned char *key,
2167 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002168{
Paul Bakker23986e52011-04-24 08:57:21 +00002169 int ret;
2170 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002172
Paul Bakker96743fc2011-02-12 14:30:57 +00002173 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002174 end = p + keylen;
2175
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002177 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002178 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 * RSAPrivateKey ::= SEQUENCE {
2180 * version Version,
2181 * modulus INTEGER, -- n
2182 * publicExponent INTEGER, -- e
2183 * privateExponent INTEGER, -- d
2184 * prime1 INTEGER, -- p
2185 * prime2 INTEGER, -- q
2186 * exponent1 INTEGER, -- d mod (p-1)
2187 * exponent2 INTEGER, -- d mod (q-1)
2188 * coefficient INTEGER, -- (inverse of q) mod p
2189 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2190 * }
2191 */
2192 if( ( ret = asn1_get_tag( &p, end, &len,
2193 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2194 {
Paul Bakker9d781402011-05-09 16:17:09 +00002195 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
2198 end = p + len;
2199
2200 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2201 {
Paul Bakker9d781402011-05-09 16:17:09 +00002202 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
2205 if( rsa->ver != 0 )
2206 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002207 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 }
2209
2210 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2211 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2216 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2217 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2218 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002220 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222
2223 rsa->len = mpi_size( &rsa->N );
2224
2225 if( p != end )
2226 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002228 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002229 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
2232 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2233 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 rsa_free( rsa );
2235 return( ret );
2236 }
2237
Paul Bakkere2f50402013-06-24 19:00:59 +02002238 return( 0 );
2239}
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002240
Paul Bakkere2f50402013-06-24 19:00:59 +02002241/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002242 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002243 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002244static int x509parse_key_sec1_der( ecp_keypair *eck,
2245 const unsigned char *key,
2246 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002247{
2248 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002249 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002250 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002251 x509_buf params;
2252 unsigned char *p = (unsigned char *) key;
2253 unsigned char *end = p + keylen;
2254 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002255
2256 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002257 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002258 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002259 * ECPrivateKey ::= SEQUENCE {
2260 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2261 * privateKey OCTET STRING,
2262 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2263 * publicKey [1] BIT STRING OPTIONAL
2264 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002265 */
2266 if( ( ret = asn1_get_tag( &p, end, &len,
2267 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2268 {
2269 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2270 }
2271
2272 end = p + len;
2273
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002274 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002275 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2276
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002277 if( version != 1 )
2278 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002279
Paul Bakkere2f50402013-06-24 19:00:59 +02002280 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2281 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2282
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002283 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002284 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002285 ecp_keypair_free( eck );
2286 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2287 }
2288
2289 p += len;
2290
2291 /*
2292 * Is 'parameters' present?
2293 */
2294 if( ( ret = asn1_get_tag( &p, end, &len,
2295 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2296 {
2297 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2298 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2299 {
2300 ecp_keypair_free( eck );
2301 return( ret );
2302 }
2303 }
2304 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2305 {
2306 ecp_keypair_free( eck );
2307 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2308 }
2309
2310 /*
2311 * Is 'publickey' present?
2312 */
2313 if( ( ret = asn1_get_tag( &p, end, &len,
2314 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2315 {
2316 end2 = p + len;
2317
2318 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2319 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2320
2321 if( p + len != end2 )
2322 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2323 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2324
2325 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2326 return( ret );
2327 }
2328 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2329 {
2330 ecp_keypair_free( eck );
2331 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2332 }
2333
2334 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2335 {
2336 ecp_keypair_free( eck );
2337 return( ret );
2338 }
2339
2340 return 0;
2341}
2342
2343/*
2344 * Parse an unencrypted PKCS#8 encoded private key
2345 */
2346static int x509parse_key_pkcs8_unencrypted_der(
2347 pk_context *pk,
2348 const unsigned char* key,
2349 size_t keylen )
2350{
2351 int ret, version;
2352 size_t len;
2353 x509_buf params;
2354 unsigned char *p = (unsigned char *) key;
2355 unsigned char *end = p + keylen;
2356 pk_type_t pk_alg = POLARSSL_PK_NONE;
2357
2358 /*
2359 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2360 *
2361 * PrivateKeyInfo ::= SEQUENCE {
2362 * version Version,
2363 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2364 * privateKey PrivateKey,
2365 * attributes [0] IMPLICIT Attributes OPTIONAL }
2366 *
2367 * Version ::= INTEGER
2368 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2369 * PrivateKey ::= OCTET STRING
2370 *
2371 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2372 */
2373
2374 if( ( ret = asn1_get_tag( &p, end, &len,
2375 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2376 {
2377 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002378 }
2379
2380 end = p + len;
2381
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002382 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2383 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2384
2385 if( version != 0 )
2386 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2387
2388 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2389 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2390
2391 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2392 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2393
2394 if( len < 1 )
2395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2396 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2397
2398 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002399 return( ret );
2400
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002401 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2402 {
2403 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2404 pk_ec( *pk )->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2405
2406 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2407 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2408 {
2409 pk_free( pk );
2410 return( ret );
2411 }
2412 } else
2413 if( pk_alg == POLARSSL_PK_RSA )
2414 {
2415 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2416 {
2417 pk_free( pk );
2418 return( ret );
2419 }
2420 } else
2421 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2422
2423 return 0;
2424}
2425
2426/*
2427 * Parse an unencrypted PKCS#8 encoded private RSA key
2428 */
2429static int x509parse_key_pkcs8_unencrypted_der_rsa(
2430 rsa_context *rsa,
2431 const unsigned char *key,
2432 size_t keylen )
2433{
2434 pk_context pk;
2435
2436 pk_init( &pk );
2437 pk_wrap_rsa( &pk, rsa );
2438
2439 return( x509parse_key_pkcs8_unencrypted_der( &pk, key, keylen ) );
Paul Bakkere2f50402013-06-24 19:00:59 +02002440}
2441
2442/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002443 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002444 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002445static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2446 size_t *used_len,
2447 const unsigned char *key, size_t keylen,
2448 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002449{
2450 int ret;
2451 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002452 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002453 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002454#if defined(POLARSSL_PKCS12_C)
2455 cipher_type_t cipher_alg;
2456 md_type_t md_alg;
2457#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002458
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002459 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002460
2461 p = (unsigned char *) key;
2462 end = p + keylen;
2463
Paul Bakker28144de2013-06-24 19:28:55 +02002464 if( pwdlen == 0 )
2465 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2466
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002467 /*
2468 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2469 *
2470 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2471 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2472 * encryptedData EncryptedData
2473 * }
2474 *
2475 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2476 *
2477 * EncryptedData ::= OCTET STRING
2478 *
2479 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2480 */
2481 if( ( ret = asn1_get_tag( &p, end, &len,
2482 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2483 {
2484 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2485 }
2486
2487 end = p + len;
2488
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002489 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002490 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491
2492 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2493 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2494
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002495 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496 return( POLARSSL_ERR_X509_INVALID_INPUT );
2497
2498 /*
2499 * Decrypt EncryptedData with appropriate PDE
2500 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002501#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002502 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002503 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002504 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002505 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002506 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002507 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002508 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2509 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2510
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002511 return( ret );
2512 }
2513 }
2514 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2515 {
2516 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2517 PKCS12_PBE_DECRYPT,
2518 pwd, pwdlen,
2519 p, len, buf ) ) != 0 )
2520 {
2521 return( ret );
2522 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002523
2524 // Best guess for password mismatch when using RC4. If first tag is
2525 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2526 //
2527 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2528 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002529 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002530 else
2531#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002532#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002533 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002534 {
2535 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2536 p, len, buf ) ) != 0 )
2537 {
2538 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2539 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2540
2541 return( ret );
2542 }
2543 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002544 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002545#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002546 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2547
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002548 *used_len = len;
2549 return( 0 );
2550}
2551
2552/*
2553 * Parse an encrypted PKCS#8 encoded private RSA key
2554 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002555static int x509parse_key_pkcs8_encrypted_der_rsa(
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002556 rsa_context *rsa,
2557 const unsigned char *key, size_t keylen,
2558 const unsigned char *pwd, size_t pwdlen )
2559{
2560 int ret;
2561 unsigned char buf[2048];
2562 size_t len = 0;
2563
2564 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2565 key, keylen, pwd, pwdlen ) ) != 0 )
2566 {
2567 return( ret );
2568 }
2569
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002570 return( x509parse_key_pkcs8_unencrypted_der_rsa( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002571}
2572
2573/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002574 * Parse a private RSA key
2575 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002576int x509parse_key_rsa( rsa_context *rsa,
2577 const unsigned char *key, size_t keylen,
2578 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002579{
2580 int ret;
2581
Paul Bakker96743fc2011-02-12 14:30:57 +00002582#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002583 size_t len;
2584 pem_context pem;
2585
2586 pem_init( &pem );
2587 ret = pem_read_buffer( &pem,
2588 "-----BEGIN RSA PRIVATE KEY-----",
2589 "-----END RSA PRIVATE KEY-----",
2590 key, pwd, pwdlen, &len );
2591 if( ret == 0 )
2592 {
2593 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2594 {
2595 rsa_free( rsa );
2596 }
2597
2598 pem_free( &pem );
2599 return( ret );
2600 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002601 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2602 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2603 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2604 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002605 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002606 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002607
2608 ret = pem_read_buffer( &pem,
2609 "-----BEGIN PRIVATE KEY-----",
2610 "-----END PRIVATE KEY-----",
2611 key, NULL, 0, &len );
2612 if( ret == 0 )
2613 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002614 if( ( ret = x509parse_key_pkcs8_unencrypted_der_rsa( rsa,
Paul Bakkere2f50402013-06-24 19:00:59 +02002615 pem.buf, pem.buflen ) ) != 0 )
2616 {
2617 rsa_free( rsa );
2618 }
2619
2620 pem_free( &pem );
2621 return( ret );
2622 }
2623 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002624 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002625
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002626 ret = pem_read_buffer( &pem,
2627 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2628 "-----END ENCRYPTED PRIVATE KEY-----",
2629 key, NULL, 0, &len );
2630 if( ret == 0 )
2631 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002632 if( ( ret = x509parse_key_pkcs8_encrypted_der_rsa( rsa,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002633 pem.buf, pem.buflen,
2634 pwd, pwdlen ) ) != 0 )
2635 {
2636 rsa_free( rsa );
2637 }
2638
2639 pem_free( &pem );
2640 return( ret );
2641 }
2642 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002643 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002644#else
2645 ((void) pwd);
2646 ((void) pwdlen);
2647#endif /* POLARSSL_PEM_C */
2648
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002649 /*
2650 * At this point we only know it's not a PEM formatted key. Could be any
2651 * of the known DER encoded private key formats
2652 *
2653 * We try the different DER format parsers to see if one passes without
2654 * error
2655 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002656 if( ( ret = x509parse_key_pkcs8_encrypted_der_rsa( rsa, key, keylen,
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002657 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002658 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002659 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002661
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002662 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002663
2664 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2665 {
2666 return( ret );
2667 }
2668
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002669 if( ( ret = x509parse_key_pkcs8_unencrypted_der_rsa( rsa, key, keylen ) ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002670 return( 0 );
2671
2672 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002673
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002674 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2675 return( 0 );
2676
2677 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002678
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002679 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680}
2681
2682/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002683 * Parse a public RSA key
2684 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002685int x509parse_public_key_rsa( rsa_context *rsa,
2686 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002687{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002688 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002689
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002690 pk_init( &pk_ctx );
2691 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002692
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002693 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002694}
2695
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002696#if defined(POLARSSL_ECP_C)
2697/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002698 * Parse an unencrypted PKCS#8 encoded private EC key
2699 */
2700static int x509parse_key_pkcs8_unencrypted_der_ec(
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002701 ecp_keypair *ec,
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002702 const unsigned char* key,
2703 size_t keylen )
2704{
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002705 pk_context pk;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002706
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002707 pk.type = POLARSSL_PK_ECKEY;
2708 pk.data = ec;
2709 pk.dont_free = 1;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002710
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002711 return( x509parse_key_pkcs8_unencrypted_der( &pk, key, keylen ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002712}
2713
2714/*
2715 * Parse an encrypted PKCS#8 encoded private EC key
2716 */
2717static int x509parse_key_pkcs8_encrypted_der_ec(
2718 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002719 const unsigned char *key, size_t keylen,
2720 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002721{
2722 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002723 unsigned char buf[2048];
2724 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002725
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002726 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2727 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002728 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002729 return( ret );
2730 }
2731
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002732 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002733}
2734
2735/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002736 * Parse a private EC key
2737 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002738static int x509parse_key_ec( ecp_keypair *eck,
2739 const unsigned char *key, size_t keylen,
2740 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002741{
2742 int ret;
2743
2744#if defined(POLARSSL_PEM_C)
2745 size_t len;
2746 pem_context pem;
2747
2748 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002749 ret = pem_read_buffer( &pem,
2750 "-----BEGIN EC PRIVATE KEY-----",
2751 "-----END EC PRIVATE KEY-----",
2752 key, pwd, pwdlen, &len );
2753 if( ret == 0 )
2754 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002755 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002756 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002757 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002758 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002759
2760 pem_free( &pem );
2761 return( ret );
2762 }
2763 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2764 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2765 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2766 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2767 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2768 return( ret );
2769
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002770 ret = pem_read_buffer( &pem,
2771 "-----BEGIN PRIVATE KEY-----",
2772 "-----END PRIVATE KEY-----",
2773 key, NULL, 0, &len );
2774 if( ret == 0 )
2775 {
2776 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2777 pem.buf, pem.buflen ) ) != 0 )
2778 {
2779 ecp_keypair_free( eck );
2780 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002781
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002782 pem_free( &pem );
2783 return( ret );
2784 }
2785 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2786 return( ret );
2787
2788 ret = pem_read_buffer( &pem,
2789 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2790 "-----END ENCRYPTED PRIVATE KEY-----",
2791 key, NULL, 0, &len );
2792 if( ret == 0 )
2793 {
2794 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2795 pem.buf, pem.buflen,
2796 pwd, pwdlen ) ) != 0 )
2797 {
2798 ecp_keypair_free( eck );
2799 }
2800
2801 pem_free( &pem );
2802 return( ret );
2803 }
2804 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2805 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002806#else
2807 ((void) pwd);
2808 ((void) pwdlen);
2809#endif /* POLARSSL_PEM_C */
2810
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002811 /*
2812 * At this point we only know it's not a PEM formatted key. Could be any
2813 * of the known DER encoded private key formats
2814 *
2815 * We try the different DER format parsers to see if one passes without
2816 * error
2817 */
2818 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2819 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002820 {
2821 return( 0 );
2822 }
2823
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002824 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002825
2826 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2827 {
2828 return( ret );
2829 }
2830
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002831 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2832 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002833 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002834
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002835 ecp_keypair_free( eck );
2836
2837 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2838 return( 0 );
2839
2840 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002841
2842 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2843}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002844#endif /* defined(POLARSSL_ECP_C) */
2845
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002846/*
2847 * Parse a private key
2848 */
2849int x509parse_key( pk_context *ctx,
2850 const unsigned char *key, size_t keylen,
2851 const unsigned char *pwd, size_t pwdlen )
2852{
2853 int ret;
2854
2855 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2856 return( ret );
2857
2858 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2859 == 0 )
2860 {
2861 return( 0 );
2862 }
2863
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002864 pk_free( ctx );
2865
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002866 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2867 return( ret );
2868
2869 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2870 {
2871 return( 0 );
2872 }
2873
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002874 pk_free( ctx );
2875
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002876 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2877}
2878
2879/*
2880 * Parse a public key
2881 */
2882int x509parse_public_key( pk_context *ctx,
2883 const unsigned char *key, size_t keylen )
2884{
2885 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002886 unsigned char *p;
2887#if defined(POLARSSL_PEM_C)
2888 size_t len;
2889 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002890
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002891 pem_init( &pem );
2892 ret = pem_read_buffer( &pem,
2893 "-----BEGIN PUBLIC KEY-----",
2894 "-----END PUBLIC KEY-----",
2895 key, NULL, 0, &len );
2896
2897 if( ret == 0 )
2898 {
2899 /*
2900 * Was PEM encoded
2901 */
2902 key = pem.buf;
2903 keylen = pem.buflen;
2904 }
2905 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2906 {
2907 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002908 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002909 }
2910#endif
2911 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002912
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002913 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002914
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002915#if defined(POLARSSL_PEM_C)
2916 pem_free( &pem );
2917#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002918
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002919 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002920}
2921
Paul Bakkereaa89f82011-04-04 21:36:15 +00002922#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002923/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002924 * Parse DHM parameters
2925 */
Paul Bakker23986e52011-04-24 08:57:21 +00002926int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002927{
Paul Bakker23986e52011-04-24 08:57:21 +00002928 int ret;
2929 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002930 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002931#if defined(POLARSSL_PEM_C)
2932 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002933
Paul Bakker96743fc2011-02-12 14:30:57 +00002934 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002935
Paul Bakker96743fc2011-02-12 14:30:57 +00002936 ret = pem_read_buffer( &pem,
2937 "-----BEGIN DH PARAMETERS-----",
2938 "-----END DH PARAMETERS-----",
2939 dhmin, NULL, 0, &dhminlen );
2940
2941 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002942 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002943 /*
2944 * Was PEM encoded
2945 */
2946 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002947 }
Paul Bakker00b28602013-06-24 13:02:41 +02002948 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002949 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002950 pem_free( &pem );
2951 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002952 }
2953
Paul Bakker96743fc2011-02-12 14:30:57 +00002954 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2955#else
2956 p = (unsigned char *) dhmin;
2957#endif
2958 end = p + dhminlen;
2959
Paul Bakker1b57b062011-01-06 15:48:19 +00002960 memset( dhm, 0, sizeof( dhm_context ) );
2961
Paul Bakker1b57b062011-01-06 15:48:19 +00002962 /*
2963 * DHParams ::= SEQUENCE {
2964 * prime INTEGER, -- P
2965 * generator INTEGER, -- g
2966 * }
2967 */
2968 if( ( ret = asn1_get_tag( &p, end, &len,
2969 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2970 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002971#if defined(POLARSSL_PEM_C)
2972 pem_free( &pem );
2973#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002974 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002975 }
2976
2977 end = p + len;
2978
2979 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2980 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2981 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002982#if defined(POLARSSL_PEM_C)
2983 pem_free( &pem );
2984#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002985 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002986 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002987 }
2988
2989 if( p != end )
2990 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002991#if defined(POLARSSL_PEM_C)
2992 pem_free( &pem );
2993#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002994 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002995 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002996 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2997 }
2998
Paul Bakker96743fc2011-02-12 14:30:57 +00002999#if defined(POLARSSL_PEM_C)
3000 pem_free( &pem );
3001#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003002
3003 return( 0 );
3004}
3005
Paul Bakker335db3f2011-04-25 15:28:35 +00003006#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003007/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003008 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003009 */
3010int x509parse_dhmfile( dhm_context *dhm, const char *path )
3011{
3012 int ret;
3013 size_t n;
3014 unsigned char *buf;
3015
Paul Bakker69e095c2011-12-10 21:55:01 +00003016 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3017 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003018
Paul Bakker27fdf462011-06-09 13:55:13 +00003019 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003020
3021 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003022 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003023
3024 return( ret );
3025}
Paul Bakker335db3f2011-04-25 15:28:35 +00003026#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003027#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003028
Paul Bakker5121ce52009-01-03 21:22:43 +00003029#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003030#include <stdarg.h>
3031
3032#if !defined vsnprintf
3033#define vsnprintf _vsnprintf
3034#endif // vsnprintf
3035
3036/*
3037 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3038 * Result value is not size of buffer needed, but -1 if no fit is possible.
3039 *
3040 * This fuction tries to 'fix' this by at least suggesting enlarging the
3041 * size by 20.
3042 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003043static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003044{
3045 va_list ap;
3046 int res = -1;
3047
3048 va_start( ap, format );
3049
3050 res = vsnprintf( str, size, format, ap );
3051
3052 va_end( ap );
3053
3054 // No quick fix possible
3055 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003056 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003057
3058 return res;
3059}
3060
3061#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003062#endif
3063
Paul Bakkerd98030e2009-05-02 15:13:40 +00003064#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3065
3066#define SAFE_SNPRINTF() \
3067{ \
3068 if( ret == -1 ) \
3069 return( -1 ); \
3070 \
Paul Bakker23986e52011-04-24 08:57:21 +00003071 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003072 p[n - 1] = '\0'; \
3073 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3074 } \
3075 \
Paul Bakker23986e52011-04-24 08:57:21 +00003076 n -= (unsigned int) ret; \
3077 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003078}
3079
Paul Bakker5121ce52009-01-03 21:22:43 +00003080/*
3081 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003084int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003085{
Paul Bakker23986e52011-04-24 08:57:21 +00003086 int ret;
3087 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003089 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003090 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 char s[128], *p;
3092
3093 memset( s, 0, sizeof( s ) );
3094
3095 name = dn;
3096 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003097 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
3099 while( name != NULL )
3100 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003101 if( !name->oid.p )
3102 {
3103 name = name->next;
3104 continue;
3105 }
3106
Paul Bakker74111d32011-01-15 16:57:55 +00003107 if( name != dn )
3108 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003109 ret = snprintf( p, n, ", " );
3110 SAFE_SNPRINTF();
3111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003112
Paul Bakkerc70b9822013-04-07 22:00:46 +02003113 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakkerc70b9822013-04-07 22:00:46 +02003115 if( ret == 0 )
3116 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003118 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003119 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
3121 for( i = 0; i < name->val.len; i++ )
3122 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003123 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 break;
3125
3126 c = name->val.p[i];
3127 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3128 s[i] = '?';
3129 else s[i] = c;
3130 }
3131 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003132 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003133 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 name = name->next;
3135 }
3136
Paul Bakker23986e52011-04-24 08:57:21 +00003137 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138}
3139
3140/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003141 * Store the serial in printable form into buf; no more
3142 * than size characters will be written
3143 */
3144int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3145{
Paul Bakker23986e52011-04-24 08:57:21 +00003146 int ret;
3147 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003148 char *p;
3149
3150 p = buf;
3151 n = size;
3152
3153 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003154 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003155
3156 for( i = 0; i < nr; i++ )
3157 {
Paul Bakker93048802011-12-05 14:38:06 +00003158 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003159 continue;
3160
Paul Bakkerdd476992011-01-16 21:34:59 +00003161 ret = snprintf( p, n, "%02X%s",
3162 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3163 SAFE_SNPRINTF();
3164 }
3165
Paul Bakker03c7c252011-11-25 12:37:37 +00003166 if( nr != serial->len )
3167 {
3168 ret = snprintf( p, n, "...." );
3169 SAFE_SNPRINTF();
3170 }
3171
Paul Bakker23986e52011-04-24 08:57:21 +00003172 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003173}
3174
3175/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003176 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003178int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3179 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003180{
Paul Bakker23986e52011-04-24 08:57:21 +00003181 int ret;
3182 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003183 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003184 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003185
3186 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003187 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Paul Bakkerd98030e2009-05-02 15:13:40 +00003189 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003191 SAFE_SNPRINTF();
3192 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003194 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003195
Paul Bakkerdd476992011-01-16 21:34:59 +00003196 ret = x509parse_serial_gets( p, n, &crt->serial);
3197 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003198
Paul Bakkerd98030e2009-05-02 15:13:40 +00003199 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3200 SAFE_SNPRINTF();
3201 ret = x509parse_dn_gets( p, n, &crt->issuer );
3202 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Paul Bakkerd98030e2009-05-02 15:13:40 +00003204 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3205 SAFE_SNPRINTF();
3206 ret = x509parse_dn_gets( p, n, &crt->subject );
3207 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Paul Bakkerd98030e2009-05-02 15:13:40 +00003209 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3211 crt->valid_from.year, crt->valid_from.mon,
3212 crt->valid_from.day, crt->valid_from.hour,
3213 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003214 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3218 crt->valid_to.year, crt->valid_to.mon,
3219 crt->valid_to.day, crt->valid_to.hour,
3220 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003221 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Paul Bakkerc70b9822013-04-07 22:00:46 +02003223 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003224 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Paul Bakkerc70b9822013-04-07 22:00:46 +02003226 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3227 if( ret != 0 )
3228 ret = snprintf( p, n, "???" );
3229 else
3230 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003231 SAFE_SNPRINTF();
3232
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003233 switch( crt->pk.type )
3234 {
3235 case POLARSSL_PK_NONE:
3236 case POLARSSL_PK_ECDSA:
3237 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3238 break;
3239
3240 case POLARSSL_PK_RSA:
3241 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3242 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3243 break;
3244
3245 case POLARSSL_PK_ECKEY:
3246 case POLARSSL_PK_ECKEY_DH:
3247 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3248 (int) pk_ec( crt->pk )->grp.pbits );
3249 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003250 SAFE_SNPRINTF();
3251
Paul Bakker23986e52011-04-24 08:57:21 +00003252 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003253}
3254
Paul Bakker74111d32011-01-15 16:57:55 +00003255/*
3256 * Return an informational string describing the given OID
3257 */
3258const char *x509_oid_get_description( x509_buf *oid )
3259{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003260 const char *desc = NULL;
3261 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003262
Paul Bakkerc70b9822013-04-07 22:00:46 +02003263 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003264
Paul Bakkerc70b9822013-04-07 22:00:46 +02003265 if( ret != 0 )
3266 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003267
Paul Bakkerc70b9822013-04-07 22:00:46 +02003268 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003269}
3270
3271/* Return the x.y.z.... style numeric string for the given OID */
3272int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3273{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003274 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003275}
3276
Paul Bakkerd98030e2009-05-02 15:13:40 +00003277/*
3278 * Return an informational string about the CRL.
3279 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003280int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3281 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003282{
Paul Bakker23986e52011-04-24 08:57:21 +00003283 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003284 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003285 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003286 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003287 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003288
3289 p = buf;
3290 n = size;
3291
3292 ret = snprintf( p, n, "%sCRL version : %d",
3293 prefix, crl->version );
3294 SAFE_SNPRINTF();
3295
3296 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3297 SAFE_SNPRINTF();
3298 ret = x509parse_dn_gets( p, n, &crl->issuer );
3299 SAFE_SNPRINTF();
3300
3301 ret = snprintf( p, n, "\n%sthis update : " \
3302 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3303 crl->this_update.year, crl->this_update.mon,
3304 crl->this_update.day, crl->this_update.hour,
3305 crl->this_update.min, crl->this_update.sec );
3306 SAFE_SNPRINTF();
3307
3308 ret = snprintf( p, n, "\n%snext update : " \
3309 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3310 crl->next_update.year, crl->next_update.mon,
3311 crl->next_update.day, crl->next_update.hour,
3312 crl->next_update.min, crl->next_update.sec );
3313 SAFE_SNPRINTF();
3314
3315 entry = &crl->entry;
3316
3317 ret = snprintf( p, n, "\n%sRevoked certificates:",
3318 prefix );
3319 SAFE_SNPRINTF();
3320
Paul Bakker9be19372009-07-27 20:21:53 +00003321 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003322 {
3323 ret = snprintf( p, n, "\n%sserial number: ",
3324 prefix );
3325 SAFE_SNPRINTF();
3326
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003327 ret = x509parse_serial_gets( p, n, &entry->serial);
3328 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003329
Paul Bakkerd98030e2009-05-02 15:13:40 +00003330 ret = snprintf( p, n, " revocation date: " \
3331 "%04d-%02d-%02d %02d:%02d:%02d",
3332 entry->revocation_date.year, entry->revocation_date.mon,
3333 entry->revocation_date.day, entry->revocation_date.hour,
3334 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003335 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003336
3337 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 }
3339
Paul Bakkerc70b9822013-04-07 22:00:46 +02003340 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003341 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakkerc70b9822013-04-07 22:00:46 +02003343 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3344 if( ret != 0 )
3345 ret = snprintf( p, n, "???" );
3346 else
3347 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003348 SAFE_SNPRINTF();
3349
Paul Bakker1e27bb22009-07-19 20:25:25 +00003350 ret = snprintf( p, n, "\n" );
3351 SAFE_SNPRINTF();
3352
Paul Bakker23986e52011-04-24 08:57:21 +00003353 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003354}
3355
3356/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003357 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003359#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003360int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003361{
Paul Bakkercce9d772011-11-18 14:26:47 +00003362 int year, mon, day;
3363 int hour, min, sec;
3364
3365#if defined(_WIN32)
3366 SYSTEMTIME st;
3367
3368 GetLocalTime(&st);
3369
3370 year = st.wYear;
3371 mon = st.wMonth;
3372 day = st.wDay;
3373 hour = st.wHour;
3374 min = st.wMinute;
3375 sec = st.wSecond;
3376#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 struct tm *lt;
3378 time_t tt;
3379
3380 tt = time( NULL );
3381 lt = localtime( &tt );
3382
Paul Bakkercce9d772011-11-18 14:26:47 +00003383 year = lt->tm_year + 1900;
3384 mon = lt->tm_mon + 1;
3385 day = lt->tm_mday;
3386 hour = lt->tm_hour;
3387 min = lt->tm_min;
3388 sec = lt->tm_sec;
3389#endif
3390
3391 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003392 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003393
Paul Bakkercce9d772011-11-18 14:26:47 +00003394 if( year == to->year &&
3395 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003396 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Paul Bakkercce9d772011-11-18 14:26:47 +00003398 if( year == to->year &&
3399 mon == to->mon &&
3400 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003401 return( 1 );
3402
Paul Bakkercce9d772011-11-18 14:26:47 +00003403 if( year == to->year &&
3404 mon == to->mon &&
3405 day == to->day &&
3406 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003407 return( 1 );
3408
Paul Bakkercce9d772011-11-18 14:26:47 +00003409 if( year == to->year &&
3410 mon == to->mon &&
3411 day == to->day &&
3412 hour == to->hour &&
3413 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003414 return( 1 );
3415
Paul Bakkercce9d772011-11-18 14:26:47 +00003416 if( year == to->year &&
3417 mon == to->mon &&
3418 day == to->day &&
3419 hour == to->hour &&
3420 min == to->min &&
3421 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003422 return( 1 );
3423
Paul Bakker40ea7de2009-05-03 10:18:48 +00003424 return( 0 );
3425}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003426#else /* POLARSSL_HAVE_TIME */
3427int x509parse_time_expired( const x509_time *to )
3428{
3429 ((void) to);
3430 return( 0 );
3431}
3432#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003433
3434/*
3435 * Return 1 if the certificate is revoked, or 0 otherwise.
3436 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003437int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003438{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003439 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003440
3441 while( cur != NULL && cur->serial.len != 0 )
3442 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003443 if( crt->serial.len == cur->serial.len &&
3444 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003445 {
3446 if( x509parse_time_expired( &cur->revocation_date ) )
3447 return( 1 );
3448 }
3449
3450 cur = cur->next;
3451 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
3453 return( 0 );
3454}
3455
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003456/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003457 * Check that the given certificate is valid accoring to the CRL.
3458 */
3459static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3460 x509_crl *crl_list)
3461{
3462 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003463 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3464 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003465
Paul Bakker915275b2012-09-28 07:10:55 +00003466 if( ca == NULL )
3467 return( flags );
3468
Paul Bakker76fd75a2011-01-16 21:12:10 +00003469 /*
3470 * TODO: What happens if no CRL is present?
3471 * Suggestion: Revocation state should be unknown if no CRL is present.
3472 * For backwards compatibility this is not yet implemented.
3473 */
3474
Paul Bakker915275b2012-09-28 07:10:55 +00003475 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003476 {
Paul Bakker915275b2012-09-28 07:10:55 +00003477 if( crl_list->version == 0 ||
3478 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003479 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3480 crl_list->issuer_raw.len ) != 0 )
3481 {
3482 crl_list = crl_list->next;
3483 continue;
3484 }
3485
3486 /*
3487 * Check if CRL is correctly signed by the trusted CA
3488 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003489 md_info = md_info_from_type( crl_list->sig_md );
3490 if( md_info == NULL )
3491 {
3492 /*
3493 * Cannot check 'unknown' hash
3494 */
3495 flags |= BADCRL_NOT_TRUSTED;
3496 break;
3497 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003498
Paul Bakkerc70b9822013-04-07 22:00:46 +02003499 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003500
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003501 /* EC NOT IMPLEMENTED YET */
3502 if( ca->pk.type != POLARSSL_PK_RSA )
3503 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3504
3505 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003506 0, hash, crl_list->sig.p ) == 0 )
3507 {
3508 /*
3509 * CRL is not trusted
3510 */
3511 flags |= BADCRL_NOT_TRUSTED;
3512 break;
3513 }
3514
3515 /*
3516 * Check for validity of CRL (Do not drop out)
3517 */
3518 if( x509parse_time_expired( &crl_list->next_update ) )
3519 flags |= BADCRL_EXPIRED;
3520
3521 /*
3522 * Check if certificate is revoked
3523 */
3524 if( x509parse_revoked(crt, crl_list) )
3525 {
3526 flags |= BADCERT_REVOKED;
3527 break;
3528 }
3529
3530 crl_list = crl_list->next;
3531 }
3532 return flags;
3533}
3534
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003535static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003536{
3537 size_t i;
3538 size_t cn_idx = 0;
3539
Paul Bakker57b12982012-02-11 17:38:38 +00003540 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003541 return( 0 );
3542
3543 for( i = 0; i < strlen( cn ); ++i )
3544 {
3545 if( cn[i] == '.' )
3546 {
3547 cn_idx = i;
3548 break;
3549 }
3550 }
3551
3552 if( cn_idx == 0 )
3553 return( 0 );
3554
Paul Bakker535e97d2012-08-23 10:49:55 +00003555 if( strlen( cn ) - cn_idx == name->len - 1 &&
3556 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003557 {
3558 return( 1 );
3559 }
3560
3561 return( 0 );
3562}
3563
Paul Bakker915275b2012-09-28 07:10:55 +00003564static int x509parse_verify_top(
3565 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003566 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003567 int (*f_vrfy)(void *, x509_cert *, int, int *),
3568 void *p_vrfy )
3569{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003570 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003571 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003572 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3573 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003574
3575 if( x509parse_time_expired( &child->valid_to ) )
3576 *flags |= BADCERT_EXPIRED;
3577
3578 /*
3579 * Child is the top of the chain. Check against the trust_ca list.
3580 */
3581 *flags |= BADCERT_NOT_TRUSTED;
3582
3583 while( trust_ca != NULL )
3584 {
3585 if( trust_ca->version == 0 ||
3586 child->issuer_raw.len != trust_ca->subject_raw.len ||
3587 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3588 child->issuer_raw.len ) != 0 )
3589 {
3590 trust_ca = trust_ca->next;
3591 continue;
3592 }
3593
Paul Bakker9a736322012-11-14 12:39:52 +00003594 /*
3595 * Reduce path_len to check against if top of the chain is
3596 * the same as the trusted CA
3597 */
3598 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3599 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3600 child->issuer_raw.len ) == 0 )
3601 {
3602 check_path_cnt--;
3603 }
3604
Paul Bakker915275b2012-09-28 07:10:55 +00003605 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003606 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003607 {
3608 trust_ca = trust_ca->next;
3609 continue;
3610 }
3611
Paul Bakkerc70b9822013-04-07 22:00:46 +02003612 md_info = md_info_from_type( child->sig_md );
3613 if( md_info == NULL )
3614 {
3615 /*
3616 * Cannot check 'unknown' hash
3617 */
3618 continue;
3619 }
Paul Bakker915275b2012-09-28 07:10:55 +00003620
Paul Bakkerc70b9822013-04-07 22:00:46 +02003621 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003622
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003623 /* EC NOT IMPLEMENTED YET */
3624 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3625 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3626
3627 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003628 0, hash, child->sig.p ) != 0 )
3629 {
3630 trust_ca = trust_ca->next;
3631 continue;
3632 }
3633
3634 /*
3635 * Top of chain is signed by a trusted CA
3636 */
3637 *flags &= ~BADCERT_NOT_TRUSTED;
3638 break;
3639 }
3640
Paul Bakker9a736322012-11-14 12:39:52 +00003641 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003642 * If top of chain is not the same as the trusted CA send a verify request
3643 * to the callback for any issues with validity and CRL presence for the
3644 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003645 */
3646 if( trust_ca != NULL &&
3647 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3648 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3649 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003650 {
3651 /* Check trusted CA's CRL for then chain's top crt */
3652 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3653
3654 if( x509parse_time_expired( &trust_ca->valid_to ) )
3655 ca_flags |= BADCERT_EXPIRED;
3656
Paul Bakker915275b2012-09-28 07:10:55 +00003657 if( NULL != f_vrfy )
3658 {
Paul Bakker9a736322012-11-14 12:39:52 +00003659 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003660 return( ret );
3661 }
3662 }
3663
3664 /* Call callback on top cert */
3665 if( NULL != f_vrfy )
3666 {
Paul Bakker9a736322012-11-14 12:39:52 +00003667 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003668 return( ret );
3669 }
3670
Paul Bakker915275b2012-09-28 07:10:55 +00003671 *flags |= ca_flags;
3672
3673 return( 0 );
3674}
3675
3676static int x509parse_verify_child(
3677 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003678 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003679 int (*f_vrfy)(void *, x509_cert *, int, int *),
3680 void *p_vrfy )
3681{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003682 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003683 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003684 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003685 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003686 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003687
3688 if( x509parse_time_expired( &child->valid_to ) )
3689 *flags |= BADCERT_EXPIRED;
3690
Paul Bakkerc70b9822013-04-07 22:00:46 +02003691 md_info = md_info_from_type( child->sig_md );
3692 if( md_info == NULL )
3693 {
3694 /*
3695 * Cannot check 'unknown' hash
3696 */
Paul Bakker915275b2012-09-28 07:10:55 +00003697 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003698 }
3699 else
3700 {
3701 md( md_info, child->tbs.p, child->tbs.len, hash );
3702
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003703 /* EC NOT IMPLEMENTED YET */
3704 if( parent->pk.type != POLARSSL_PK_RSA )
3705 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3706
3707 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3708 0, hash, child->sig.p ) != 0 )
3709 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003710 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003711 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003712 }
3713
Paul Bakker915275b2012-09-28 07:10:55 +00003714 /* Check trusted CA's CRL for the given crt */
3715 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3716
3717 grandparent = parent->next;
3718
3719 while( grandparent != NULL )
3720 {
3721 if( grandparent->version == 0 ||
3722 grandparent->ca_istrue == 0 ||
3723 parent->issuer_raw.len != grandparent->subject_raw.len ||
3724 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3725 parent->issuer_raw.len ) != 0 )
3726 {
3727 grandparent = grandparent->next;
3728 continue;
3729 }
3730 break;
3731 }
3732
Paul Bakker915275b2012-09-28 07:10:55 +00003733 if( grandparent != NULL )
3734 {
3735 /*
3736 * Part of the chain
3737 */
Paul Bakker9a736322012-11-14 12:39:52 +00003738 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 +00003739 if( ret != 0 )
3740 return( ret );
3741 }
3742 else
3743 {
Paul Bakker9a736322012-11-14 12:39:52 +00003744 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 +00003745 if( ret != 0 )
3746 return( ret );
3747 }
3748
3749 /* child is verified to be a child of the parent, call verify callback */
3750 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003751 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003752 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003753
3754 *flags |= parent_flags;
3755
3756 return( 0 );
3757}
3758
Paul Bakker76fd75a2011-01-16 21:12:10 +00003759/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003760 * Verify the certificate validity
3761 */
3762int x509parse_verify( x509_cert *crt,
3763 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003764 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003765 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003766 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003767 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768{
Paul Bakker23986e52011-04-24 08:57:21 +00003769 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003770 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003771 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003772 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003774 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003775
Paul Bakker40ea7de2009-05-03 10:18:48 +00003776 *flags = 0;
3777
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 if( cn != NULL )
3779 {
3780 name = &crt->subject;
3781 cn_len = strlen( cn );
3782
Paul Bakker4d2c1242012-05-10 14:12:46 +00003783 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003785 cur = &crt->subject_alt_names;
3786
3787 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003788 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003789 if( cur->buf.len == cn_len &&
3790 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003791 break;
3792
Paul Bakker535e97d2012-08-23 10:49:55 +00003793 if( cur->buf.len > 2 &&
3794 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003795 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003796 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003797
Paul Bakker4d2c1242012-05-10 14:12:46 +00003798 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003799 }
3800
3801 if( cur == NULL )
3802 *flags |= BADCERT_CN_MISMATCH;
3803 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003804 else
3805 {
3806 while( name != NULL )
3807 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003808 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003809 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003810 if( name->val.len == cn_len &&
3811 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003812 break;
3813
Paul Bakker535e97d2012-08-23 10:49:55 +00003814 if( name->val.len > 2 &&
3815 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003816 x509_wildcard_verify( cn, &name->val ) )
3817 break;
3818 }
3819
3820 name = name->next;
3821 }
3822
3823 if( name == NULL )
3824 *flags |= BADCERT_CN_MISMATCH;
3825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 }
3827
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003829 * Iterate upwards in the given cert chain, to find our crt parent.
3830 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003832 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003833
Paul Bakker76fd75a2011-01-16 21:12:10 +00003834 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003836 if( parent->ca_istrue == 0 ||
3837 crt->issuer_raw.len != parent->subject_raw.len ||
3838 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 crt->issuer_raw.len ) != 0 )
3840 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003841 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003842 continue;
3843 }
Paul Bakker915275b2012-09-28 07:10:55 +00003844 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 }
3846
Paul Bakker915275b2012-09-28 07:10:55 +00003847 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 {
Paul Bakker915275b2012-09-28 07:10:55 +00003849 /*
3850 * Part of the chain
3851 */
Paul Bakker9a736322012-11-14 12:39:52 +00003852 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003853 if( ret != 0 )
3854 return( ret );
3855 }
3856 else
Paul Bakker74111d32011-01-15 16:57:55 +00003857 {
Paul Bakker9a736322012-11-14 12:39:52 +00003858 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003859 if( ret != 0 )
3860 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003861 }
Paul Bakker915275b2012-09-28 07:10:55 +00003862
3863 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003864 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003865
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 return( 0 );
3867}
3868
3869/*
3870 * Unallocate all certificate data
3871 */
3872void x509_free( x509_cert *crt )
3873{
3874 x509_cert *cert_cur = crt;
3875 x509_cert *cert_prv;
3876 x509_name *name_cur;
3877 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003878 x509_sequence *seq_cur;
3879 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
3881 if( crt == NULL )
3882 return;
3883
3884 do
3885 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003886 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
3888 name_cur = cert_cur->issuer.next;
3889 while( name_cur != NULL )
3890 {
3891 name_prv = name_cur;
3892 name_cur = name_cur->next;
3893 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003894 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 }
3896
3897 name_cur = cert_cur->subject.next;
3898 while( name_cur != NULL )
3899 {
3900 name_prv = name_cur;
3901 name_cur = name_cur->next;
3902 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003903 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 }
3905
Paul Bakker74111d32011-01-15 16:57:55 +00003906 seq_cur = cert_cur->ext_key_usage.next;
3907 while( seq_cur != NULL )
3908 {
3909 seq_prv = seq_cur;
3910 seq_cur = seq_cur->next;
3911 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003912 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003913 }
3914
Paul Bakker8afa70d2012-02-11 18:42:45 +00003915 seq_cur = cert_cur->subject_alt_names.next;
3916 while( seq_cur != NULL )
3917 {
3918 seq_prv = seq_cur;
3919 seq_cur = seq_cur->next;
3920 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003921 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003922 }
3923
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 if( cert_cur->raw.p != NULL )
3925 {
3926 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003927 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928 }
3929
3930 cert_cur = cert_cur->next;
3931 }
3932 while( cert_cur != NULL );
3933
3934 cert_cur = crt;
3935 do
3936 {
3937 cert_prv = cert_cur;
3938 cert_cur = cert_cur->next;
3939
3940 memset( cert_prv, 0, sizeof( x509_cert ) );
3941 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003942 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 }
3944 while( cert_cur != NULL );
3945}
3946
Paul Bakkerd98030e2009-05-02 15:13:40 +00003947/*
3948 * Unallocate all CRL data
3949 */
3950void x509_crl_free( x509_crl *crl )
3951{
3952 x509_crl *crl_cur = crl;
3953 x509_crl *crl_prv;
3954 x509_name *name_cur;
3955 x509_name *name_prv;
3956 x509_crl_entry *entry_cur;
3957 x509_crl_entry *entry_prv;
3958
3959 if( crl == NULL )
3960 return;
3961
3962 do
3963 {
3964 name_cur = crl_cur->issuer.next;
3965 while( name_cur != NULL )
3966 {
3967 name_prv = name_cur;
3968 name_cur = name_cur->next;
3969 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003970 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003971 }
3972
3973 entry_cur = crl_cur->entry.next;
3974 while( entry_cur != NULL )
3975 {
3976 entry_prv = entry_cur;
3977 entry_cur = entry_cur->next;
3978 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003979 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003980 }
3981
3982 if( crl_cur->raw.p != NULL )
3983 {
3984 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003985 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003986 }
3987
3988 crl_cur = crl_cur->next;
3989 }
3990 while( crl_cur != NULL );
3991
3992 crl_cur = crl;
3993 do
3994 {
3995 crl_prv = crl_cur;
3996 crl_cur = crl_cur->next;
3997
3998 memset( crl_prv, 0, sizeof( x509_crl ) );
3999 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004000 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004001 }
4002 while( crl_cur != NULL );
4003}
4004
Paul Bakker40e46942009-01-03 21:51:57 +00004005#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
Paul Bakker40e46942009-01-03 21:51:57 +00004007#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
4009/*
4010 * Checkup routine
4011 */
4012int x509_self_test( int verbose )
4013{
Paul Bakker5690efc2011-05-26 13:16:06 +00004014#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004015 int ret;
4016 int flags;
4017 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 x509_cert cacert;
4019 x509_cert clicert;
4020 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004021#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004022 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004023#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
4025 if( verbose != 0 )
4026 printf( " X.509 certificate load: " );
4027
4028 memset( &clicert, 0, sizeof( x509_cert ) );
4029
Paul Bakker3c2122f2013-06-24 19:03:14 +02004030 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004031 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 if( ret != 0 )
4033 {
4034 if( verbose != 0 )
4035 printf( "failed\n" );
4036
4037 return( ret );
4038 }
4039
4040 memset( &cacert, 0, sizeof( x509_cert ) );
4041
Paul Bakker3c2122f2013-06-24 19:03:14 +02004042 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004043 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 if( ret != 0 )
4045 {
4046 if( verbose != 0 )
4047 printf( "failed\n" );
4048
4049 return( ret );
4050 }
4051
4052 if( verbose != 0 )
4053 printf( "passed\n X.509 private key load: " );
4054
4055 i = strlen( test_ca_key );
4056 j = strlen( test_ca_pwd );
4057
Paul Bakker66b78b22011-03-25 14:22:50 +00004058 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4059
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004060 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004061 (const unsigned char *) test_ca_key, i,
4062 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004063 {
4064 if( verbose != 0 )
4065 printf( "failed\n" );
4066
4067 return( ret );
4068 }
4069
4070 if( verbose != 0 )
4071 printf( "passed\n X.509 signature verify: ");
4072
Paul Bakker23986e52011-04-24 08:57:21 +00004073 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074 if( ret != 0 )
4075 {
Paul Bakker23986e52011-04-24 08:57:21 +00004076 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004077 if( verbose != 0 )
4078 printf( "failed\n" );
4079
4080 return( ret );
4081 }
4082
Paul Bakker5690efc2011-05-26 13:16:06 +00004083#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004084 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004085 printf( "passed\n X.509 DHM parameter load: " );
4086
4087 i = strlen( test_dhm_params );
4088 j = strlen( test_ca_pwd );
4089
Paul Bakker3c2122f2013-06-24 19:03:14 +02004090 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004091 {
4092 if( verbose != 0 )
4093 printf( "failed\n" );
4094
4095 return( ret );
4096 }
4097
4098 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004101
4102 x509_free( &cacert );
4103 x509_free( &clicert );
4104 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004105#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004106 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004108
4109 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004110#else
4111 ((void) verbose);
4112 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004114}
4115
4116#endif
4117
4118#endif