blob: 4eafd7edb49ee817ca8b35bdd5d0d284413693c5 [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}
Paul Bakkere2f50402013-06-24 19:00:59 +02002240/*
2241 * Parse an unencrypted PKCS#8 encoded private RSA key
2242 */
2243static int x509parse_key_pkcs8_unencrypted_der(
2244 rsa_context *rsa,
2245 const unsigned char *key,
2246 size_t keylen )
2247{
2248 int ret;
2249 size_t len;
2250 unsigned char *p, *end;
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002251 x509_buf alg_params;
Paul Bakkere2f50402013-06-24 19:00:59 +02002252 pk_type_t pk_alg = POLARSSL_PK_NONE;
2253
2254 p = (unsigned char *) key;
2255 end = p + keylen;
2256
2257 /*
2258 * This function parses the PrivatKeyInfo object (PKCS#8)
2259 *
2260 * PrivateKeyInfo ::= SEQUENCE {
2261 * version Version,
2262 * algorithm AlgorithmIdentifier,
2263 * PrivateKey BIT STRING
2264 * }
2265 *
2266 * AlgorithmIdentifier ::= SEQUENCE {
2267 * algorithm OBJECT IDENTIFIER,
2268 * parameters ANY DEFINED BY algorithm OPTIONAL
2269 * }
2270 *
2271 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2272 */
2273 if( ( ret = asn1_get_tag( &p, end, &len,
2274 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2275 {
2276 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2277 }
2278
2279 end = p + len;
2280
2281 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2282 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2283
2284 if( rsa->ver != 0 )
2285 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2286
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002287 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002288 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2289
2290 /*
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002291 * We explicitly want RSA keys only
Paul Bakkere2f50402013-06-24 19:00:59 +02002292 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002293 if (pk_alg != POLARSSL_PK_RSA )
2294 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2295
Paul Bakkere2f50402013-06-24 19:00:59 +02002296 /*
2297 * Get the OCTET STRING and parse the PKCS#1 format inside
2298 */
2299 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2300 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2301
2302 if( ( end - p ) < 1 )
2303 {
2304 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2306 }
2307
2308 end = p + len;
2309
2310 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2311 return( ret );
2312
2313 return( 0 );
2314}
2315
2316/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002317 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002318 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002319static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2320 size_t *used_len,
2321 const unsigned char *key, size_t keylen,
2322 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002323{
2324 int ret;
2325 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002326 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002327 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002328#if defined(POLARSSL_PKCS12_C)
2329 cipher_type_t cipher_alg;
2330 md_type_t md_alg;
2331#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002332
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002333 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002334
2335 p = (unsigned char *) key;
2336 end = p + keylen;
2337
Paul Bakker28144de2013-06-24 19:28:55 +02002338 if( pwdlen == 0 )
2339 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2340
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002341 /*
2342 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2343 *
2344 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2345 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2346 * encryptedData EncryptedData
2347 * }
2348 *
2349 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2350 *
2351 * EncryptedData ::= OCTET STRING
2352 *
2353 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2354 */
2355 if( ( ret = asn1_get_tag( &p, end, &len,
2356 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2357 {
2358 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2359 }
2360
2361 end = p + len;
2362
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002363 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002364 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002365
2366 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2367 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2368
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002369 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002370 return( POLARSSL_ERR_X509_INVALID_INPUT );
2371
2372 /*
2373 * Decrypt EncryptedData with appropriate PDE
2374 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002375#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002376 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002377 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002378 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002379 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002380 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002381 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002382 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2383 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2384
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002385 return( ret );
2386 }
2387 }
2388 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2389 {
2390 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2391 PKCS12_PBE_DECRYPT,
2392 pwd, pwdlen,
2393 p, len, buf ) ) != 0 )
2394 {
2395 return( ret );
2396 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002397
2398 // Best guess for password mismatch when using RC4. If first tag is
2399 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2400 //
2401 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2402 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002403 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002404 else
2405#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002406#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002407 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002408 {
2409 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2410 p, len, buf ) ) != 0 )
2411 {
2412 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2413 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2414
2415 return( ret );
2416 }
2417 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002418 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002419#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002420 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2421
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002422 *used_len = len;
2423 return( 0 );
2424}
2425
2426/*
2427 * Parse an encrypted PKCS#8 encoded private RSA key
2428 */
2429static int x509parse_key_pkcs8_encrypted_der(
2430 rsa_context *rsa,
2431 const unsigned char *key, size_t keylen,
2432 const unsigned char *pwd, size_t pwdlen )
2433{
2434 int ret;
2435 unsigned char buf[2048];
2436 size_t len = 0;
2437
2438 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2439 key, keylen, pwd, pwdlen ) ) != 0 )
2440 {
2441 return( ret );
2442 }
2443
2444 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002445}
2446
2447/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002448 * Parse a private RSA key
2449 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002450int x509parse_key_rsa( rsa_context *rsa,
2451 const unsigned char *key, size_t keylen,
2452 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002453{
2454 int ret;
2455
Paul Bakker96743fc2011-02-12 14:30:57 +00002456#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002457 size_t len;
2458 pem_context pem;
2459
2460 pem_init( &pem );
2461 ret = pem_read_buffer( &pem,
2462 "-----BEGIN RSA PRIVATE KEY-----",
2463 "-----END RSA PRIVATE KEY-----",
2464 key, pwd, pwdlen, &len );
2465 if( ret == 0 )
2466 {
2467 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2468 {
2469 rsa_free( rsa );
2470 }
2471
2472 pem_free( &pem );
2473 return( ret );
2474 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002475 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2476 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2477 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2478 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002479 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002480 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002481
2482 ret = pem_read_buffer( &pem,
2483 "-----BEGIN PRIVATE KEY-----",
2484 "-----END PRIVATE KEY-----",
2485 key, NULL, 0, &len );
2486 if( ret == 0 )
2487 {
2488 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2489 pem.buf, pem.buflen ) ) != 0 )
2490 {
2491 rsa_free( rsa );
2492 }
2493
2494 pem_free( &pem );
2495 return( ret );
2496 }
2497 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002498 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002499
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002500 ret = pem_read_buffer( &pem,
2501 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2502 "-----END ENCRYPTED PRIVATE KEY-----",
2503 key, NULL, 0, &len );
2504 if( ret == 0 )
2505 {
2506 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2507 pem.buf, pem.buflen,
2508 pwd, pwdlen ) ) != 0 )
2509 {
2510 rsa_free( rsa );
2511 }
2512
2513 pem_free( &pem );
2514 return( ret );
2515 }
2516 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002517 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002518#else
2519 ((void) pwd);
2520 ((void) pwdlen);
2521#endif /* POLARSSL_PEM_C */
2522
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002523 /*
2524 * At this point we only know it's not a PEM formatted key. Could be any
2525 * of the known DER encoded private key formats
2526 *
2527 * We try the different DER format parsers to see if one passes without
2528 * error
2529 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002530 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2531 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002532 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002533 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002536 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002537
2538 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2539 {
2540 return( ret );
2541 }
2542
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002543 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2544 return( 0 );
2545
2546 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002547
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002548 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2549 return( 0 );
2550
2551 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002552
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002553 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554}
2555
2556/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002557 * Parse a public RSA key
2558 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002559int x509parse_public_key_rsa( rsa_context *rsa,
2560 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002561{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002562 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002563
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002564 pk_init( &pk_ctx );
2565 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002566
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002567 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002568}
2569
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002570#if defined(POLARSSL_ECP_C)
2571/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002572 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002573 */
2574static int x509parse_key_sec1_der( ecp_keypair *eck,
2575 const unsigned char *key,
2576 size_t keylen )
2577{
2578 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002579 int version;
2580 size_t len;
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002581 x509_buf params;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002582 unsigned char *p = (unsigned char *) key;
2583 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002584 unsigned char *end2;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002585
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002586 /*
2587 * RFC 5915, orf SEC1 Appendix C.4
2588 *
2589 * ECPrivateKey ::= SEQUENCE {
2590 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2591 * privateKey OCTET STRING,
2592 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2593 * publicKey [1] BIT STRING OPTIONAL
2594 * }
2595 */
2596 if( ( ret = asn1_get_tag( &p, end, &len,
2597 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2598 {
2599 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2600 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002601
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002602 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002603
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002604 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2605 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002606
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002607 if( version != 1 )
2608 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2609
2610 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2611 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2612
2613 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2614 {
2615 ecp_keypair_free( eck );
2616 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2617 }
2618
2619 p += len;
2620
2621 /*
2622 * Is 'parameters' present?
2623 */
2624 if( ( ret = asn1_get_tag( &p, end, &len,
2625 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2626 {
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002627 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2628 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2629 {
2630 ecp_keypair_free( eck );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002631 return( ret );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002632 }
2633 }
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002634 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002635 {
2636 ecp_keypair_free( eck );
2637 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2638 }
2639
2640 /*
2641 * Is 'publickey' present?
2642 */
2643 if( ( ret = asn1_get_tag( &p, end, &len,
2644 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2645 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002646 end2 = p + len;
2647
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +02002648 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2649 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002650
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +02002651 if( p + len != end2 )
2652 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002653 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +02002654
2655 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2656 return( ret );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002657 }
2658 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2659 {
2660 ecp_keypair_free( eck );
2661 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2662 }
2663
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002664 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002665 {
2666 ecp_keypair_free( eck );
2667 return( ret );
2668 }
2669
2670 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002671}
2672
2673/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002674 * Parse an unencrypted PKCS#8 encoded private EC key
2675 */
2676static int x509parse_key_pkcs8_unencrypted_der_ec(
2677 ecp_keypair *eck,
2678 const unsigned char* key,
2679 size_t keylen )
2680{
2681 int ret, version;
2682 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002683 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002684 unsigned char *p = (unsigned char *) key;
2685 unsigned char *end = p + keylen;
2686 pk_type_t pk_alg = POLARSSL_PK_NONE;
2687
2688 /*
2689 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2690 *
2691 * PrivateKeyInfo ::= SEQUENCE {
2692 * version Version,
2693 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2694 * privateKey PrivateKey,
2695 * attributes [0] IMPLICIT Attributes OPTIONAL }
2696 *
2697 * Version ::= INTEGER
2698 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2699 * PrivateKey ::= OCTET STRING
2700 *
2701 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2702 */
2703
2704 if( ( ret = asn1_get_tag( &p, end, &len,
2705 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2706 {
2707 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2708 }
2709
2710 end = p + len;
2711
2712 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2713 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2714
2715 if( version != 0 )
2716 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2717
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002718 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002719 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2720
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002721 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002722 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002723
2724 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2725 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2726
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002727 if( ( ret = x509_use_ecparams( &alg_params, &eck->grp ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002728 {
2729 ecp_keypair_free( eck );
2730 return( ret );
2731 }
2732
2733 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2734 {
2735 ecp_keypair_free( eck );
2736 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2737 }
2738
2739 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2740 {
2741 ecp_keypair_free( eck );
2742 return( ret );
2743 }
2744
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002745 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002746 {
2747 ecp_keypair_free( eck );
2748 return( ret );
2749 }
2750
2751 return 0;
2752}
2753
2754/*
2755 * Parse an encrypted PKCS#8 encoded private EC key
2756 */
2757static int x509parse_key_pkcs8_encrypted_der_ec(
2758 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002759 const unsigned char *key, size_t keylen,
2760 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002761{
2762 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002763 unsigned char buf[2048];
2764 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002765
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002766 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2767 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002768 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002769 return( ret );
2770 }
2771
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002772 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002773}
2774
2775/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002776 * Parse a private EC key
2777 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002778static int x509parse_key_ec( ecp_keypair *eck,
2779 const unsigned char *key, size_t keylen,
2780 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002781{
2782 int ret;
2783
2784#if defined(POLARSSL_PEM_C)
2785 size_t len;
2786 pem_context pem;
2787
2788 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002789 ret = pem_read_buffer( &pem,
2790 "-----BEGIN EC PRIVATE KEY-----",
2791 "-----END EC PRIVATE KEY-----",
2792 key, pwd, pwdlen, &len );
2793 if( ret == 0 )
2794 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002795 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002796 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002797 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002798 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002799
2800 pem_free( &pem );
2801 return( ret );
2802 }
2803 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2804 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2805 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2806 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2807 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2808 return( ret );
2809
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002810 ret = pem_read_buffer( &pem,
2811 "-----BEGIN PRIVATE KEY-----",
2812 "-----END PRIVATE KEY-----",
2813 key, NULL, 0, &len );
2814 if( ret == 0 )
2815 {
2816 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2817 pem.buf, pem.buflen ) ) != 0 )
2818 {
2819 ecp_keypair_free( eck );
2820 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002821
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002822 pem_free( &pem );
2823 return( ret );
2824 }
2825 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2826 return( ret );
2827
2828 ret = pem_read_buffer( &pem,
2829 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2830 "-----END ENCRYPTED PRIVATE KEY-----",
2831 key, NULL, 0, &len );
2832 if( ret == 0 )
2833 {
2834 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2835 pem.buf, pem.buflen,
2836 pwd, pwdlen ) ) != 0 )
2837 {
2838 ecp_keypair_free( eck );
2839 }
2840
2841 pem_free( &pem );
2842 return( ret );
2843 }
2844 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2845 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002846#else
2847 ((void) pwd);
2848 ((void) pwdlen);
2849#endif /* POLARSSL_PEM_C */
2850
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002851 /*
2852 * At this point we only know it's not a PEM formatted key. Could be any
2853 * of the known DER encoded private key formats
2854 *
2855 * We try the different DER format parsers to see if one passes without
2856 * error
2857 */
2858 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2859 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002860 {
2861 return( 0 );
2862 }
2863
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002864 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002865
2866 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2867 {
2868 return( ret );
2869 }
2870
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002871 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2872 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002873 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002874
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002875 ecp_keypair_free( eck );
2876
2877 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2878 return( 0 );
2879
2880 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002881
2882 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2883}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002884#endif /* defined(POLARSSL_ECP_C) */
2885
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002886/*
2887 * Parse a private key
2888 */
2889int x509parse_key( pk_context *ctx,
2890 const unsigned char *key, size_t keylen,
2891 const unsigned char *pwd, size_t pwdlen )
2892{
2893 int ret;
2894
2895 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2896 return( ret );
2897
2898 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2899 == 0 )
2900 {
2901 return( 0 );
2902 }
2903
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002904 pk_free( ctx );
2905
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002906 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2907 return( ret );
2908
2909 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2910 {
2911 return( 0 );
2912 }
2913
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002914 pk_free( ctx );
2915
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002916 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2917}
2918
2919/*
2920 * Parse a public key
2921 */
2922int x509parse_public_key( pk_context *ctx,
2923 const unsigned char *key, size_t keylen )
2924{
2925 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002926 unsigned char *p;
2927#if defined(POLARSSL_PEM_C)
2928 size_t len;
2929 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002930
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002931 pem_init( &pem );
2932 ret = pem_read_buffer( &pem,
2933 "-----BEGIN PUBLIC KEY-----",
2934 "-----END PUBLIC KEY-----",
2935 key, NULL, 0, &len );
2936
2937 if( ret == 0 )
2938 {
2939 /*
2940 * Was PEM encoded
2941 */
2942 key = pem.buf;
2943 keylen = pem.buflen;
2944 }
2945 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2946 {
2947 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002948 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002949 }
2950#endif
2951 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002952
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002953 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002954
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002955#if defined(POLARSSL_PEM_C)
2956 pem_free( &pem );
2957#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002958
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002959 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002960}
2961
Paul Bakkereaa89f82011-04-04 21:36:15 +00002962#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002963/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002964 * Parse DHM parameters
2965 */
Paul Bakker23986e52011-04-24 08:57:21 +00002966int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002967{
Paul Bakker23986e52011-04-24 08:57:21 +00002968 int ret;
2969 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002970 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002971#if defined(POLARSSL_PEM_C)
2972 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002973
Paul Bakker96743fc2011-02-12 14:30:57 +00002974 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002975
Paul Bakker96743fc2011-02-12 14:30:57 +00002976 ret = pem_read_buffer( &pem,
2977 "-----BEGIN DH PARAMETERS-----",
2978 "-----END DH PARAMETERS-----",
2979 dhmin, NULL, 0, &dhminlen );
2980
2981 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002982 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002983 /*
2984 * Was PEM encoded
2985 */
2986 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002987 }
Paul Bakker00b28602013-06-24 13:02:41 +02002988 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002989 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002990 pem_free( &pem );
2991 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002992 }
2993
Paul Bakker96743fc2011-02-12 14:30:57 +00002994 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2995#else
2996 p = (unsigned char *) dhmin;
2997#endif
2998 end = p + dhminlen;
2999
Paul Bakker1b57b062011-01-06 15:48:19 +00003000 memset( dhm, 0, sizeof( dhm_context ) );
3001
Paul Bakker1b57b062011-01-06 15:48:19 +00003002 /*
3003 * DHParams ::= SEQUENCE {
3004 * prime INTEGER, -- P
3005 * generator INTEGER, -- g
3006 * }
3007 */
3008 if( ( ret = asn1_get_tag( &p, end, &len,
3009 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3010 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003011#if defined(POLARSSL_PEM_C)
3012 pem_free( &pem );
3013#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003014 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003015 }
3016
3017 end = p + len;
3018
3019 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3020 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3021 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003022#if defined(POLARSSL_PEM_C)
3023 pem_free( &pem );
3024#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003025 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003026 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003027 }
3028
3029 if( p != end )
3030 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003031#if defined(POLARSSL_PEM_C)
3032 pem_free( &pem );
3033#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003034 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003035 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003036 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3037 }
3038
Paul Bakker96743fc2011-02-12 14:30:57 +00003039#if defined(POLARSSL_PEM_C)
3040 pem_free( &pem );
3041#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003042
3043 return( 0 );
3044}
3045
Paul Bakker335db3f2011-04-25 15:28:35 +00003046#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003047/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003048 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003049 */
3050int x509parse_dhmfile( dhm_context *dhm, const char *path )
3051{
3052 int ret;
3053 size_t n;
3054 unsigned char *buf;
3055
Paul Bakker69e095c2011-12-10 21:55:01 +00003056 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3057 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003058
Paul Bakker27fdf462011-06-09 13:55:13 +00003059 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003060
3061 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003062 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003063
3064 return( ret );
3065}
Paul Bakker335db3f2011-04-25 15:28:35 +00003066#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003067#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003068
Paul Bakker5121ce52009-01-03 21:22:43 +00003069#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003070#include <stdarg.h>
3071
3072#if !defined vsnprintf
3073#define vsnprintf _vsnprintf
3074#endif // vsnprintf
3075
3076/*
3077 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3078 * Result value is not size of buffer needed, but -1 if no fit is possible.
3079 *
3080 * This fuction tries to 'fix' this by at least suggesting enlarging the
3081 * size by 20.
3082 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003083static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003084{
3085 va_list ap;
3086 int res = -1;
3087
3088 va_start( ap, format );
3089
3090 res = vsnprintf( str, size, format, ap );
3091
3092 va_end( ap );
3093
3094 // No quick fix possible
3095 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003096 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003097
3098 return res;
3099}
3100
3101#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003102#endif
3103
Paul Bakkerd98030e2009-05-02 15:13:40 +00003104#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3105
3106#define SAFE_SNPRINTF() \
3107{ \
3108 if( ret == -1 ) \
3109 return( -1 ); \
3110 \
Paul Bakker23986e52011-04-24 08:57:21 +00003111 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003112 p[n - 1] = '\0'; \
3113 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3114 } \
3115 \
Paul Bakker23986e52011-04-24 08:57:21 +00003116 n -= (unsigned int) ret; \
3117 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003118}
3119
Paul Bakker5121ce52009-01-03 21:22:43 +00003120/*
3121 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003124int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003125{
Paul Bakker23986e52011-04-24 08:57:21 +00003126 int ret;
3127 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003128 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003129 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003130 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 char s[128], *p;
3132
3133 memset( s, 0, sizeof( s ) );
3134
3135 name = dn;
3136 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003137 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 while( name != NULL )
3140 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003141 if( !name->oid.p )
3142 {
3143 name = name->next;
3144 continue;
3145 }
3146
Paul Bakker74111d32011-01-15 16:57:55 +00003147 if( name != dn )
3148 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003149 ret = snprintf( p, n, ", " );
3150 SAFE_SNPRINTF();
3151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003152
Paul Bakkerc70b9822013-04-07 22:00:46 +02003153 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakkerc70b9822013-04-07 22:00:46 +02003155 if( ret == 0 )
3156 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003158 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003159 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
3161 for( i = 0; i < name->val.len; i++ )
3162 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003163 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 break;
3165
3166 c = name->val.p[i];
3167 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3168 s[i] = '?';
3169 else s[i] = c;
3170 }
3171 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003172 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003173 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 name = name->next;
3175 }
3176
Paul Bakker23986e52011-04-24 08:57:21 +00003177 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003178}
3179
3180/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003181 * Store the serial in printable form into buf; no more
3182 * than size characters will be written
3183 */
3184int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3185{
Paul Bakker23986e52011-04-24 08:57:21 +00003186 int ret;
3187 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003188 char *p;
3189
3190 p = buf;
3191 n = size;
3192
3193 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003194 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003195
3196 for( i = 0; i < nr; i++ )
3197 {
Paul Bakker93048802011-12-05 14:38:06 +00003198 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003199 continue;
3200
Paul Bakkerdd476992011-01-16 21:34:59 +00003201 ret = snprintf( p, n, "%02X%s",
3202 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3203 SAFE_SNPRINTF();
3204 }
3205
Paul Bakker03c7c252011-11-25 12:37:37 +00003206 if( nr != serial->len )
3207 {
3208 ret = snprintf( p, n, "...." );
3209 SAFE_SNPRINTF();
3210 }
3211
Paul Bakker23986e52011-04-24 08:57:21 +00003212 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003213}
3214
3215/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003218int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3219 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003220{
Paul Bakker23986e52011-04-24 08:57:21 +00003221 int ret;
3222 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003223 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003224 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
3226 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003227 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228
Paul Bakkerd98030e2009-05-02 15:13:40 +00003229 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003231 SAFE_SNPRINTF();
3232 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003234 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Paul Bakkerdd476992011-01-16 21:34:59 +00003236 ret = x509parse_serial_gets( p, n, &crt->serial);
3237 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Paul Bakkerd98030e2009-05-02 15:13:40 +00003239 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3240 SAFE_SNPRINTF();
3241 ret = x509parse_dn_gets( p, n, &crt->issuer );
3242 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Paul Bakkerd98030e2009-05-02 15:13:40 +00003244 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3245 SAFE_SNPRINTF();
3246 ret = x509parse_dn_gets( p, n, &crt->subject );
3247 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3251 crt->valid_from.year, crt->valid_from.mon,
3252 crt->valid_from.day, crt->valid_from.hour,
3253 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003254 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
Paul Bakkerd98030e2009-05-02 15:13:40 +00003256 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3258 crt->valid_to.year, crt->valid_to.mon,
3259 crt->valid_to.day, crt->valid_to.hour,
3260 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003261 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Paul Bakkerc70b9822013-04-07 22:00:46 +02003263 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003264 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003265
Paul Bakkerc70b9822013-04-07 22:00:46 +02003266 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3267 if( ret != 0 )
3268 ret = snprintf( p, n, "???" );
3269 else
3270 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003271 SAFE_SNPRINTF();
3272
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003273 switch( crt->pk.type )
3274 {
3275 case POLARSSL_PK_NONE:
3276 case POLARSSL_PK_ECDSA:
3277 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3278 break;
3279
3280 case POLARSSL_PK_RSA:
3281 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3282 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3283 break;
3284
3285 case POLARSSL_PK_ECKEY:
3286 case POLARSSL_PK_ECKEY_DH:
3287 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3288 (int) pk_ec( crt->pk )->grp.pbits );
3289 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003290 SAFE_SNPRINTF();
3291
Paul Bakker23986e52011-04-24 08:57:21 +00003292 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003293}
3294
Paul Bakker74111d32011-01-15 16:57:55 +00003295/*
3296 * Return an informational string describing the given OID
3297 */
3298const char *x509_oid_get_description( x509_buf *oid )
3299{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003300 const char *desc = NULL;
3301 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003302
Paul Bakkerc70b9822013-04-07 22:00:46 +02003303 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003304
Paul Bakkerc70b9822013-04-07 22:00:46 +02003305 if( ret != 0 )
3306 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003307
Paul Bakkerc70b9822013-04-07 22:00:46 +02003308 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003309}
3310
3311/* Return the x.y.z.... style numeric string for the given OID */
3312int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3313{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003314 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003315}
3316
Paul Bakkerd98030e2009-05-02 15:13:40 +00003317/*
3318 * Return an informational string about the CRL.
3319 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003320int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3321 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003322{
Paul Bakker23986e52011-04-24 08:57:21 +00003323 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003324 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003325 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003326 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003327 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003328
3329 p = buf;
3330 n = size;
3331
3332 ret = snprintf( p, n, "%sCRL version : %d",
3333 prefix, crl->version );
3334 SAFE_SNPRINTF();
3335
3336 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3337 SAFE_SNPRINTF();
3338 ret = x509parse_dn_gets( p, n, &crl->issuer );
3339 SAFE_SNPRINTF();
3340
3341 ret = snprintf( p, n, "\n%sthis update : " \
3342 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3343 crl->this_update.year, crl->this_update.mon,
3344 crl->this_update.day, crl->this_update.hour,
3345 crl->this_update.min, crl->this_update.sec );
3346 SAFE_SNPRINTF();
3347
3348 ret = snprintf( p, n, "\n%snext update : " \
3349 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3350 crl->next_update.year, crl->next_update.mon,
3351 crl->next_update.day, crl->next_update.hour,
3352 crl->next_update.min, crl->next_update.sec );
3353 SAFE_SNPRINTF();
3354
3355 entry = &crl->entry;
3356
3357 ret = snprintf( p, n, "\n%sRevoked certificates:",
3358 prefix );
3359 SAFE_SNPRINTF();
3360
Paul Bakker9be19372009-07-27 20:21:53 +00003361 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003362 {
3363 ret = snprintf( p, n, "\n%sserial number: ",
3364 prefix );
3365 SAFE_SNPRINTF();
3366
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003367 ret = x509parse_serial_gets( p, n, &entry->serial);
3368 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003369
Paul Bakkerd98030e2009-05-02 15:13:40 +00003370 ret = snprintf( p, n, " revocation date: " \
3371 "%04d-%02d-%02d %02d:%02d:%02d",
3372 entry->revocation_date.year, entry->revocation_date.mon,
3373 entry->revocation_date.day, entry->revocation_date.hour,
3374 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003375 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003376
3377 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 }
3379
Paul Bakkerc70b9822013-04-07 22:00:46 +02003380 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003381 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
Paul Bakkerc70b9822013-04-07 22:00:46 +02003383 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3384 if( ret != 0 )
3385 ret = snprintf( p, n, "???" );
3386 else
3387 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003388 SAFE_SNPRINTF();
3389
Paul Bakker1e27bb22009-07-19 20:25:25 +00003390 ret = snprintf( p, n, "\n" );
3391 SAFE_SNPRINTF();
3392
Paul Bakker23986e52011-04-24 08:57:21 +00003393 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394}
3395
3396/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003397 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003399#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003400int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003401{
Paul Bakkercce9d772011-11-18 14:26:47 +00003402 int year, mon, day;
3403 int hour, min, sec;
3404
3405#if defined(_WIN32)
3406 SYSTEMTIME st;
3407
3408 GetLocalTime(&st);
3409
3410 year = st.wYear;
3411 mon = st.wMonth;
3412 day = st.wDay;
3413 hour = st.wHour;
3414 min = st.wMinute;
3415 sec = st.wSecond;
3416#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 struct tm *lt;
3418 time_t tt;
3419
3420 tt = time( NULL );
3421 lt = localtime( &tt );
3422
Paul Bakkercce9d772011-11-18 14:26:47 +00003423 year = lt->tm_year + 1900;
3424 mon = lt->tm_mon + 1;
3425 day = lt->tm_mday;
3426 hour = lt->tm_hour;
3427 min = lt->tm_min;
3428 sec = lt->tm_sec;
3429#endif
3430
3431 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003432 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003433
Paul Bakkercce9d772011-11-18 14:26:47 +00003434 if( year == to->year &&
3435 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003436 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003437
Paul Bakkercce9d772011-11-18 14:26:47 +00003438 if( year == to->year &&
3439 mon == to->mon &&
3440 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003441 return( 1 );
3442
Paul Bakkercce9d772011-11-18 14:26:47 +00003443 if( year == to->year &&
3444 mon == to->mon &&
3445 day == to->day &&
3446 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003447 return( 1 );
3448
Paul Bakkercce9d772011-11-18 14:26:47 +00003449 if( year == to->year &&
3450 mon == to->mon &&
3451 day == to->day &&
3452 hour == to->hour &&
3453 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003454 return( 1 );
3455
Paul Bakkercce9d772011-11-18 14:26:47 +00003456 if( year == to->year &&
3457 mon == to->mon &&
3458 day == to->day &&
3459 hour == to->hour &&
3460 min == to->min &&
3461 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003462 return( 1 );
3463
Paul Bakker40ea7de2009-05-03 10:18:48 +00003464 return( 0 );
3465}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003466#else /* POLARSSL_HAVE_TIME */
3467int x509parse_time_expired( const x509_time *to )
3468{
3469 ((void) to);
3470 return( 0 );
3471}
3472#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003473
3474/*
3475 * Return 1 if the certificate is revoked, or 0 otherwise.
3476 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003477int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003478{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003479 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003480
3481 while( cur != NULL && cur->serial.len != 0 )
3482 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003483 if( crt->serial.len == cur->serial.len &&
3484 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003485 {
3486 if( x509parse_time_expired( &cur->revocation_date ) )
3487 return( 1 );
3488 }
3489
3490 cur = cur->next;
3491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
3493 return( 0 );
3494}
3495
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003496/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003497 * Check that the given certificate is valid accoring to the CRL.
3498 */
3499static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3500 x509_crl *crl_list)
3501{
3502 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003503 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3504 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003505
Paul Bakker915275b2012-09-28 07:10:55 +00003506 if( ca == NULL )
3507 return( flags );
3508
Paul Bakker76fd75a2011-01-16 21:12:10 +00003509 /*
3510 * TODO: What happens if no CRL is present?
3511 * Suggestion: Revocation state should be unknown if no CRL is present.
3512 * For backwards compatibility this is not yet implemented.
3513 */
3514
Paul Bakker915275b2012-09-28 07:10:55 +00003515 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003516 {
Paul Bakker915275b2012-09-28 07:10:55 +00003517 if( crl_list->version == 0 ||
3518 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003519 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3520 crl_list->issuer_raw.len ) != 0 )
3521 {
3522 crl_list = crl_list->next;
3523 continue;
3524 }
3525
3526 /*
3527 * Check if CRL is correctly signed by the trusted CA
3528 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003529 md_info = md_info_from_type( crl_list->sig_md );
3530 if( md_info == NULL )
3531 {
3532 /*
3533 * Cannot check 'unknown' hash
3534 */
3535 flags |= BADCRL_NOT_TRUSTED;
3536 break;
3537 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003538
Paul Bakkerc70b9822013-04-07 22:00:46 +02003539 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003540
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003541 /* EC NOT IMPLEMENTED YET */
3542 if( ca->pk.type != POLARSSL_PK_RSA )
3543 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3544
3545 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003546 0, hash, crl_list->sig.p ) == 0 )
3547 {
3548 /*
3549 * CRL is not trusted
3550 */
3551 flags |= BADCRL_NOT_TRUSTED;
3552 break;
3553 }
3554
3555 /*
3556 * Check for validity of CRL (Do not drop out)
3557 */
3558 if( x509parse_time_expired( &crl_list->next_update ) )
3559 flags |= BADCRL_EXPIRED;
3560
3561 /*
3562 * Check if certificate is revoked
3563 */
3564 if( x509parse_revoked(crt, crl_list) )
3565 {
3566 flags |= BADCERT_REVOKED;
3567 break;
3568 }
3569
3570 crl_list = crl_list->next;
3571 }
3572 return flags;
3573}
3574
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003575static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003576{
3577 size_t i;
3578 size_t cn_idx = 0;
3579
Paul Bakker57b12982012-02-11 17:38:38 +00003580 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003581 return( 0 );
3582
3583 for( i = 0; i < strlen( cn ); ++i )
3584 {
3585 if( cn[i] == '.' )
3586 {
3587 cn_idx = i;
3588 break;
3589 }
3590 }
3591
3592 if( cn_idx == 0 )
3593 return( 0 );
3594
Paul Bakker535e97d2012-08-23 10:49:55 +00003595 if( strlen( cn ) - cn_idx == name->len - 1 &&
3596 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003597 {
3598 return( 1 );
3599 }
3600
3601 return( 0 );
3602}
3603
Paul Bakker915275b2012-09-28 07:10:55 +00003604static int x509parse_verify_top(
3605 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003606 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003607 int (*f_vrfy)(void *, x509_cert *, int, int *),
3608 void *p_vrfy )
3609{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003610 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003611 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003612 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3613 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003614
3615 if( x509parse_time_expired( &child->valid_to ) )
3616 *flags |= BADCERT_EXPIRED;
3617
3618 /*
3619 * Child is the top of the chain. Check against the trust_ca list.
3620 */
3621 *flags |= BADCERT_NOT_TRUSTED;
3622
3623 while( trust_ca != NULL )
3624 {
3625 if( trust_ca->version == 0 ||
3626 child->issuer_raw.len != trust_ca->subject_raw.len ||
3627 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3628 child->issuer_raw.len ) != 0 )
3629 {
3630 trust_ca = trust_ca->next;
3631 continue;
3632 }
3633
Paul Bakker9a736322012-11-14 12:39:52 +00003634 /*
3635 * Reduce path_len to check against if top of the chain is
3636 * the same as the trusted CA
3637 */
3638 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3639 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3640 child->issuer_raw.len ) == 0 )
3641 {
3642 check_path_cnt--;
3643 }
3644
Paul Bakker915275b2012-09-28 07:10:55 +00003645 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003646 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003647 {
3648 trust_ca = trust_ca->next;
3649 continue;
3650 }
3651
Paul Bakkerc70b9822013-04-07 22:00:46 +02003652 md_info = md_info_from_type( child->sig_md );
3653 if( md_info == NULL )
3654 {
3655 /*
3656 * Cannot check 'unknown' hash
3657 */
3658 continue;
3659 }
Paul Bakker915275b2012-09-28 07:10:55 +00003660
Paul Bakkerc70b9822013-04-07 22:00:46 +02003661 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003662
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003663 /* EC NOT IMPLEMENTED YET */
3664 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3665 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3666
3667 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003668 0, hash, child->sig.p ) != 0 )
3669 {
3670 trust_ca = trust_ca->next;
3671 continue;
3672 }
3673
3674 /*
3675 * Top of chain is signed by a trusted CA
3676 */
3677 *flags &= ~BADCERT_NOT_TRUSTED;
3678 break;
3679 }
3680
Paul Bakker9a736322012-11-14 12:39:52 +00003681 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003682 * If top of chain is not the same as the trusted CA send a verify request
3683 * to the callback for any issues with validity and CRL presence for the
3684 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003685 */
3686 if( trust_ca != NULL &&
3687 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3688 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3689 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003690 {
3691 /* Check trusted CA's CRL for then chain's top crt */
3692 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3693
3694 if( x509parse_time_expired( &trust_ca->valid_to ) )
3695 ca_flags |= BADCERT_EXPIRED;
3696
Paul Bakker915275b2012-09-28 07:10:55 +00003697 if( NULL != f_vrfy )
3698 {
Paul Bakker9a736322012-11-14 12:39:52 +00003699 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003700 return( ret );
3701 }
3702 }
3703
3704 /* Call callback on top cert */
3705 if( NULL != f_vrfy )
3706 {
Paul Bakker9a736322012-11-14 12:39:52 +00003707 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003708 return( ret );
3709 }
3710
Paul Bakker915275b2012-09-28 07:10:55 +00003711 *flags |= ca_flags;
3712
3713 return( 0 );
3714}
3715
3716static int x509parse_verify_child(
3717 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003718 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003719 int (*f_vrfy)(void *, x509_cert *, int, int *),
3720 void *p_vrfy )
3721{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003722 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003723 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003724 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003725 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003726 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003727
3728 if( x509parse_time_expired( &child->valid_to ) )
3729 *flags |= BADCERT_EXPIRED;
3730
Paul Bakkerc70b9822013-04-07 22:00:46 +02003731 md_info = md_info_from_type( child->sig_md );
3732 if( md_info == NULL )
3733 {
3734 /*
3735 * Cannot check 'unknown' hash
3736 */
Paul Bakker915275b2012-09-28 07:10:55 +00003737 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003738 }
3739 else
3740 {
3741 md( md_info, child->tbs.p, child->tbs.len, hash );
3742
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003743 /* EC NOT IMPLEMENTED YET */
3744 if( parent->pk.type != POLARSSL_PK_RSA )
3745 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3746
3747 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3748 0, hash, child->sig.p ) != 0 )
3749 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003750 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003751 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003752 }
3753
Paul Bakker915275b2012-09-28 07:10:55 +00003754 /* Check trusted CA's CRL for the given crt */
3755 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3756
3757 grandparent = parent->next;
3758
3759 while( grandparent != NULL )
3760 {
3761 if( grandparent->version == 0 ||
3762 grandparent->ca_istrue == 0 ||
3763 parent->issuer_raw.len != grandparent->subject_raw.len ||
3764 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3765 parent->issuer_raw.len ) != 0 )
3766 {
3767 grandparent = grandparent->next;
3768 continue;
3769 }
3770 break;
3771 }
3772
Paul Bakker915275b2012-09-28 07:10:55 +00003773 if( grandparent != NULL )
3774 {
3775 /*
3776 * Part of the chain
3777 */
Paul Bakker9a736322012-11-14 12:39:52 +00003778 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 +00003779 if( ret != 0 )
3780 return( ret );
3781 }
3782 else
3783 {
Paul Bakker9a736322012-11-14 12:39:52 +00003784 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 +00003785 if( ret != 0 )
3786 return( ret );
3787 }
3788
3789 /* child is verified to be a child of the parent, call verify callback */
3790 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003791 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003792 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003793
3794 *flags |= parent_flags;
3795
3796 return( 0 );
3797}
3798
Paul Bakker76fd75a2011-01-16 21:12:10 +00003799/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 * Verify the certificate validity
3801 */
3802int x509parse_verify( x509_cert *crt,
3803 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003804 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003805 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003806 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003807 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003808{
Paul Bakker23986e52011-04-24 08:57:21 +00003809 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003810 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003811 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003812 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003814 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003815
Paul Bakker40ea7de2009-05-03 10:18:48 +00003816 *flags = 0;
3817
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 if( cn != NULL )
3819 {
3820 name = &crt->subject;
3821 cn_len = strlen( cn );
3822
Paul Bakker4d2c1242012-05-10 14:12:46 +00003823 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003825 cur = &crt->subject_alt_names;
3826
3827 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003828 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003829 if( cur->buf.len == cn_len &&
3830 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003831 break;
3832
Paul Bakker535e97d2012-08-23 10:49:55 +00003833 if( cur->buf.len > 2 &&
3834 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003835 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003836 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Paul Bakker4d2c1242012-05-10 14:12:46 +00003838 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003839 }
3840
3841 if( cur == NULL )
3842 *flags |= BADCERT_CN_MISMATCH;
3843 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003844 else
3845 {
3846 while( name != NULL )
3847 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003848 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003849 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003850 if( name->val.len == cn_len &&
3851 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003852 break;
3853
Paul Bakker535e97d2012-08-23 10:49:55 +00003854 if( name->val.len > 2 &&
3855 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003856 x509_wildcard_verify( cn, &name->val ) )
3857 break;
3858 }
3859
3860 name = name->next;
3861 }
3862
3863 if( name == NULL )
3864 *flags |= BADCERT_CN_MISMATCH;
3865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 }
3867
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003869 * Iterate upwards in the given cert chain, to find our crt parent.
3870 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003872 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003873
Paul Bakker76fd75a2011-01-16 21:12:10 +00003874 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003876 if( parent->ca_istrue == 0 ||
3877 crt->issuer_raw.len != parent->subject_raw.len ||
3878 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 crt->issuer_raw.len ) != 0 )
3880 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003881 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 continue;
3883 }
Paul Bakker915275b2012-09-28 07:10:55 +00003884 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 }
3886
Paul Bakker915275b2012-09-28 07:10:55 +00003887 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 {
Paul Bakker915275b2012-09-28 07:10:55 +00003889 /*
3890 * Part of the chain
3891 */
Paul Bakker9a736322012-11-14 12:39:52 +00003892 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003893 if( ret != 0 )
3894 return( ret );
3895 }
3896 else
Paul Bakker74111d32011-01-15 16:57:55 +00003897 {
Paul Bakker9a736322012-11-14 12:39:52 +00003898 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003899 if( ret != 0 )
3900 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003901 }
Paul Bakker915275b2012-09-28 07:10:55 +00003902
3903 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003904 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003905
Paul Bakker5121ce52009-01-03 21:22:43 +00003906 return( 0 );
3907}
3908
3909/*
3910 * Unallocate all certificate data
3911 */
3912void x509_free( x509_cert *crt )
3913{
3914 x509_cert *cert_cur = crt;
3915 x509_cert *cert_prv;
3916 x509_name *name_cur;
3917 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003918 x509_sequence *seq_cur;
3919 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003920
3921 if( crt == NULL )
3922 return;
3923
3924 do
3925 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003926 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
3928 name_cur = cert_cur->issuer.next;
3929 while( name_cur != NULL )
3930 {
3931 name_prv = name_cur;
3932 name_cur = name_cur->next;
3933 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003934 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003935 }
3936
3937 name_cur = cert_cur->subject.next;
3938 while( name_cur != NULL )
3939 {
3940 name_prv = name_cur;
3941 name_cur = name_cur->next;
3942 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003943 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003944 }
3945
Paul Bakker74111d32011-01-15 16:57:55 +00003946 seq_cur = cert_cur->ext_key_usage.next;
3947 while( seq_cur != NULL )
3948 {
3949 seq_prv = seq_cur;
3950 seq_cur = seq_cur->next;
3951 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003952 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003953 }
3954
Paul Bakker8afa70d2012-02-11 18:42:45 +00003955 seq_cur = cert_cur->subject_alt_names.next;
3956 while( seq_cur != NULL )
3957 {
3958 seq_prv = seq_cur;
3959 seq_cur = seq_cur->next;
3960 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003961 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003962 }
3963
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 if( cert_cur->raw.p != NULL )
3965 {
3966 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003967 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 }
3969
3970 cert_cur = cert_cur->next;
3971 }
3972 while( cert_cur != NULL );
3973
3974 cert_cur = crt;
3975 do
3976 {
3977 cert_prv = cert_cur;
3978 cert_cur = cert_cur->next;
3979
3980 memset( cert_prv, 0, sizeof( x509_cert ) );
3981 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003982 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 }
3984 while( cert_cur != NULL );
3985}
3986
Paul Bakkerd98030e2009-05-02 15:13:40 +00003987/*
3988 * Unallocate all CRL data
3989 */
3990void x509_crl_free( x509_crl *crl )
3991{
3992 x509_crl *crl_cur = crl;
3993 x509_crl *crl_prv;
3994 x509_name *name_cur;
3995 x509_name *name_prv;
3996 x509_crl_entry *entry_cur;
3997 x509_crl_entry *entry_prv;
3998
3999 if( crl == NULL )
4000 return;
4001
4002 do
4003 {
4004 name_cur = crl_cur->issuer.next;
4005 while( name_cur != NULL )
4006 {
4007 name_prv = name_cur;
4008 name_cur = name_cur->next;
4009 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004010 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004011 }
4012
4013 entry_cur = crl_cur->entry.next;
4014 while( entry_cur != NULL )
4015 {
4016 entry_prv = entry_cur;
4017 entry_cur = entry_cur->next;
4018 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004020 }
4021
4022 if( crl_cur->raw.p != NULL )
4023 {
4024 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004025 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004026 }
4027
4028 crl_cur = crl_cur->next;
4029 }
4030 while( crl_cur != NULL );
4031
4032 crl_cur = crl;
4033 do
4034 {
4035 crl_prv = crl_cur;
4036 crl_cur = crl_cur->next;
4037
4038 memset( crl_prv, 0, sizeof( x509_crl ) );
4039 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004040 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004041 }
4042 while( crl_cur != NULL );
4043}
4044
Paul Bakker40e46942009-01-03 21:51:57 +00004045#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004046
Paul Bakker40e46942009-01-03 21:51:57 +00004047#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
4049/*
4050 * Checkup routine
4051 */
4052int x509_self_test( int verbose )
4053{
Paul Bakker5690efc2011-05-26 13:16:06 +00004054#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004055 int ret;
4056 int flags;
4057 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 x509_cert cacert;
4059 x509_cert clicert;
4060 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004061#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004062 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
4065 if( verbose != 0 )
4066 printf( " X.509 certificate load: " );
4067
4068 memset( &clicert, 0, sizeof( x509_cert ) );
4069
Paul Bakker3c2122f2013-06-24 19:03:14 +02004070 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004071 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 if( ret != 0 )
4073 {
4074 if( verbose != 0 )
4075 printf( "failed\n" );
4076
4077 return( ret );
4078 }
4079
4080 memset( &cacert, 0, sizeof( x509_cert ) );
4081
Paul Bakker3c2122f2013-06-24 19:03:14 +02004082 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004083 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004084 if( ret != 0 )
4085 {
4086 if( verbose != 0 )
4087 printf( "failed\n" );
4088
4089 return( ret );
4090 }
4091
4092 if( verbose != 0 )
4093 printf( "passed\n X.509 private key load: " );
4094
4095 i = strlen( test_ca_key );
4096 j = strlen( test_ca_pwd );
4097
Paul Bakker66b78b22011-03-25 14:22:50 +00004098 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4099
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004100 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004101 (const unsigned char *) test_ca_key, i,
4102 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 {
4104 if( verbose != 0 )
4105 printf( "failed\n" );
4106
4107 return( ret );
4108 }
4109
4110 if( verbose != 0 )
4111 printf( "passed\n X.509 signature verify: ");
4112
Paul Bakker23986e52011-04-24 08:57:21 +00004113 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004114 if( ret != 0 )
4115 {
Paul Bakker23986e52011-04-24 08:57:21 +00004116 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004117 if( verbose != 0 )
4118 printf( "failed\n" );
4119
4120 return( ret );
4121 }
4122
Paul Bakker5690efc2011-05-26 13:16:06 +00004123#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004125 printf( "passed\n X.509 DHM parameter load: " );
4126
4127 i = strlen( test_dhm_params );
4128 j = strlen( test_ca_pwd );
4129
Paul Bakker3c2122f2013-06-24 19:03:14 +02004130 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004131 {
4132 if( verbose != 0 )
4133 printf( "failed\n" );
4134
4135 return( ret );
4136 }
4137
4138 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004139 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004140#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004141
4142 x509_free( &cacert );
4143 x509_free( &clicert );
4144 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004145#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004146 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004148
4149 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004150#else
4151 ((void) verbose);
4152 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004154}
4155
4156#endif
4157
4158#endif