blob: 4fba038d5bcdb6299fba9559699b282c79bfaea0 [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é-Gonnard8b863cd2013-07-11 15:32:03 +0200521/*
522 * EC public key is an EC point
523 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200524static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200525 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200526{
527 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200528
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200529 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200530 (const unsigned char *) *p, end - *p ) ) != 0 ||
531 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200532 {
533 ecp_keypair_free( key );
534 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200535 }
536
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200537 /*
538 * We know ecp_point_read_binary consumed all bytes
539 */
540 *p = (unsigned char *) end;
541
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200542 return( 0 );
543}
544
545/*
546 * SubjectPublicKeyInfo ::= SEQUENCE {
547 * algorithm AlgorithmIdentifier,
548 * subjectPublicKey BIT STRING }
549 */
550static int x509_get_pubkey( unsigned char **p,
551 const unsigned char *end,
552 pk_context *pk )
553{
554 int ret;
555 size_t len;
556 x509_buf alg_params;
557 pk_type_t pk_alg = POLARSSL_PK_NONE;
558
559 if( ( ret = asn1_get_tag( p, end, &len,
560 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
561 {
562 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
563 }
564
565 end = *p + len;
566
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200567 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200568 return( ret );
569
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200570 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200571 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
572
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200573 if( *p + len != end )
574 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
575 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
576
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200577 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
578 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200579
580 switch( pk_alg )
581 {
582 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200583 case POLARSSL_PK_ECDSA:
584 /* Should never happen */
585 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200586 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200587
588 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200589 ret = x509_get_rsapubkey( p, end, pk->data );
590 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200591
592 case POLARSSL_PK_ECKEY_DH:
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{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002076 pk_context pk;
Paul Bakker335db3f2011-04-25 15:28:35 +00002077
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002078 pk_init( &pk );
2079 pk_wrap_rsa( &pk, rsa );
Paul Bakker335db3f2011-04-25 15:28:35 +00002080
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002081 return( x509parse_keyfile( &pk, path, pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002082}
2083
2084/*
2085 * Load and parse a public RSA key
2086 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002087int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002088{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002089 pk_context pk;
Paul Bakker335db3f2011-04-25 15:28:35 +00002090
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002091 pk_init( &pk );
2092 pk_wrap_rsa( &pk, rsa );
Paul Bakker335db3f2011-04-25 15:28:35 +00002093
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002094 return( x509parse_public_keyfile( &pk, path ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002095}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002096
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002097/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002098 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002099 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002100int x509parse_keyfile( pk_context *ctx,
2101 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002102{
2103 int ret;
2104 size_t n;
2105 unsigned char *buf;
2106
2107 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2108 return( ret );
2109
2110 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002111 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002112 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002113 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002114 (const unsigned char *) pwd, strlen( pwd ) );
2115
2116 memset( buf, 0, n + 1 );
2117 free( buf );
2118
2119 return( ret );
2120}
2121
2122/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002123 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002124 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002125int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002126{
2127 int ret;
2128 size_t n;
2129 unsigned char *buf;
2130
2131 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2132 return( ret );
2133
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002134 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002135
2136 memset( buf, 0, n + 1 );
2137 free( buf );
2138
2139 return( ret );
2140}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002141
Paul Bakker335db3f2011-04-25 15:28:35 +00002142#endif /* POLARSSL_FS_IO */
2143
2144/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002145 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002147static int x509parse_key_pkcs1_der( rsa_context *rsa,
2148 const unsigned char *key,
2149 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150{
Paul Bakker23986e52011-04-24 08:57:21 +00002151 int ret;
2152 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002154
Paul Bakker96743fc2011-02-12 14:30:57 +00002155 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002156 end = p + keylen;
2157
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002159 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002160 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 * RSAPrivateKey ::= SEQUENCE {
2162 * version Version,
2163 * modulus INTEGER, -- n
2164 * publicExponent INTEGER, -- e
2165 * privateExponent INTEGER, -- d
2166 * prime1 INTEGER, -- p
2167 * prime2 INTEGER, -- q
2168 * exponent1 INTEGER, -- d mod (p-1)
2169 * exponent2 INTEGER, -- d mod (q-1)
2170 * coefficient INTEGER, -- (inverse of q) mod p
2171 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2172 * }
2173 */
2174 if( ( ret = asn1_get_tag( &p, end, &len,
2175 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2176 {
Paul Bakker9d781402011-05-09 16:17:09 +00002177 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 }
2179
2180 end = p + len;
2181
2182 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2183 {
Paul Bakker9d781402011-05-09 16:17:09 +00002184 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
2186
2187 if( rsa->ver != 0 )
2188 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002189 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
2191
2192 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2193 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2194 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2195 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2196 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2197 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2198 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2199 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2200 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 rsa_free( rsa );
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 rsa->len = mpi_size( &rsa->N );
2206
2207 if( p != end )
2208 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002210 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002211 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 }
2213
2214 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2215 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 rsa_free( rsa );
2217 return( ret );
2218 }
2219
Paul Bakkere2f50402013-06-24 19:00:59 +02002220 return( 0 );
2221}
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002222
Paul Bakkere2f50402013-06-24 19:00:59 +02002223/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002224 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002225 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002226static int x509parse_key_sec1_der( ecp_keypair *eck,
2227 const unsigned char *key,
2228 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002229{
2230 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002231 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002232 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002233 x509_buf params;
2234 unsigned char *p = (unsigned char *) key;
2235 unsigned char *end = p + keylen;
2236 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002237
2238 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002239 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002240 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002241 * ECPrivateKey ::= SEQUENCE {
2242 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2243 * privateKey OCTET STRING,
2244 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2245 * publicKey [1] BIT STRING OPTIONAL
2246 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002247 */
2248 if( ( ret = asn1_get_tag( &p, end, &len,
2249 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2250 {
2251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2252 }
2253
2254 end = p + len;
2255
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002256 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002257 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2258
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002259 if( version != 1 )
2260 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002261
Paul Bakkere2f50402013-06-24 19:00:59 +02002262 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2263 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2264
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002265 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002266 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002267 ecp_keypair_free( eck );
2268 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2269 }
2270
2271 p += len;
2272
2273 /*
2274 * Is 'parameters' present?
2275 */
2276 if( ( ret = asn1_get_tag( &p, end, &len,
2277 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2278 {
2279 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2280 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2281 {
2282 ecp_keypair_free( eck );
2283 return( ret );
2284 }
2285 }
2286 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2287 {
2288 ecp_keypair_free( eck );
2289 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2290 }
2291
2292 /*
2293 * Is 'publickey' present?
2294 */
2295 if( ( ret = asn1_get_tag( &p, end, &len,
2296 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2297 {
2298 end2 = p + len;
2299
2300 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2301 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2302
2303 if( p + len != end2 )
2304 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2305 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2306
2307 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2308 return( ret );
2309 }
2310 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2311 {
2312 ecp_keypair_free( eck );
2313 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2314 }
2315
2316 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2317 {
2318 ecp_keypair_free( eck );
2319 return( ret );
2320 }
2321
2322 return 0;
2323}
2324
2325/*
2326 * Parse an unencrypted PKCS#8 encoded private key
2327 */
2328static int x509parse_key_pkcs8_unencrypted_der(
2329 pk_context *pk,
2330 const unsigned char* key,
2331 size_t keylen )
2332{
2333 int ret, version;
2334 size_t len;
2335 x509_buf params;
2336 unsigned char *p = (unsigned char *) key;
2337 unsigned char *end = p + keylen;
2338 pk_type_t pk_alg = POLARSSL_PK_NONE;
2339
2340 /*
2341 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2342 *
2343 * PrivateKeyInfo ::= SEQUENCE {
2344 * version Version,
2345 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2346 * privateKey PrivateKey,
2347 * attributes [0] IMPLICIT Attributes OPTIONAL }
2348 *
2349 * Version ::= INTEGER
2350 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2351 * PrivateKey ::= OCTET STRING
2352 *
2353 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2354 */
2355
2356 if( ( ret = asn1_get_tag( &p, end, &len,
2357 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2358 {
2359 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002360 }
2361
2362 end = p + len;
2363
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002364 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2365 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2366
2367 if( version != 0 )
2368 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2369
2370 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2371 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2372
2373 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2374 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2375
2376 if( len < 1 )
2377 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2378 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2379
2380 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002381 return( ret );
2382
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002383 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2384 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002385 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2386 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2387 {
2388 pk_free( pk );
2389 return( ret );
2390 }
2391 } else
2392 if( pk_alg == POLARSSL_PK_RSA )
2393 {
2394 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2395 {
2396 pk_free( pk );
2397 return( ret );
2398 }
2399 } else
2400 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2401
2402 return 0;
2403}
2404
2405/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002406 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002407 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002408static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2409 size_t *used_len,
2410 const unsigned char *key, size_t keylen,
2411 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002412{
2413 int ret;
2414 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002415 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002416 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002417#if defined(POLARSSL_PKCS12_C)
2418 cipher_type_t cipher_alg;
2419 md_type_t md_alg;
2420#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002421
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002422 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002423
2424 p = (unsigned char *) key;
2425 end = p + keylen;
2426
Paul Bakker28144de2013-06-24 19:28:55 +02002427 if( pwdlen == 0 )
2428 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2429
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002430 /*
2431 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2432 *
2433 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2434 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2435 * encryptedData EncryptedData
2436 * }
2437 *
2438 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2439 *
2440 * EncryptedData ::= OCTET STRING
2441 *
2442 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2443 */
2444 if( ( ret = asn1_get_tag( &p, end, &len,
2445 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2446 {
2447 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2448 }
2449
2450 end = p + len;
2451
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002452 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002453 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002454
2455 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2456 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2457
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002458 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002459 return( POLARSSL_ERR_X509_INVALID_INPUT );
2460
2461 /*
2462 * Decrypt EncryptedData with appropriate PDE
2463 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002464#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002465 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002466 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002467 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002468 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002469 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002470 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002471 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2472 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2473
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002474 return( ret );
2475 }
2476 }
2477 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2478 {
2479 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2480 PKCS12_PBE_DECRYPT,
2481 pwd, pwdlen,
2482 p, len, buf ) ) != 0 )
2483 {
2484 return( ret );
2485 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002486
2487 // Best guess for password mismatch when using RC4. If first tag is
2488 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2489 //
2490 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2491 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002492 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002493 else
2494#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002495#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002496 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002497 {
2498 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2499 p, len, buf ) ) != 0 )
2500 {
2501 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2502 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2503
2504 return( ret );
2505 }
2506 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002507 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002508#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002509 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2510
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002511 *used_len = len;
2512 return( 0 );
2513}
2514
2515/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002516 * Parse a private RSA key
2517 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002518int x509parse_key_rsa( rsa_context *rsa,
2519 const unsigned char *key, size_t keylen,
2520 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002521{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002522 pk_context pk;
Paul Bakkere2f50402013-06-24 19:00:59 +02002523
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002524 pk_init( &pk );
2525 pk_wrap_rsa( &pk, rsa );
Paul Bakkere2f50402013-06-24 19:00:59 +02002526
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002527 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002528}
2529
2530/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002531 * Parse a public RSA key
2532 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002533int x509parse_public_key_rsa( rsa_context *rsa,
2534 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002535{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002536 pk_context pk;
Paul Bakker53019ae2011-03-25 13:58:48 +00002537
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002538 pk_init( &pk );
2539 pk_wrap_rsa( &pk, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002540
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002541 return( x509parse_public_key( &pk, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002542}
2543
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002544#if defined(POLARSSL_ECP_C)
2545/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002546 * Parse an encrypted PKCS#8 encoded private key
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002547 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002548static int x509parse_key_pkcs8_encrypted_der(
2549 pk_context *pk,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002550 const unsigned char *key, size_t keylen,
2551 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002552{
2553 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002554 unsigned char buf[2048];
2555 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002556
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002557 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2558 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002559 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002560 return( ret );
2561 }
2562
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002563 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002564}
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002565#endif /* defined(POLARSSL_ECP_C) */
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002566
2567/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002568 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002569 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002570int x509parse_key( pk_context *pk,
2571 const unsigned char *key, size_t keylen,
2572 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002573{
2574 int ret;
2575
2576#if defined(POLARSSL_PEM_C)
2577 size_t len;
2578 pem_context pem;
2579
2580 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002581
2582 ret = pem_read_buffer( &pem,
2583 "-----BEGIN RSA PRIVATE KEY-----",
2584 "-----END RSA PRIVATE KEY-----",
2585 key, pwd, pwdlen, &len );
2586 if( ret == 0 )
2587 {
2588 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2589 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2590 pem.buf, pem.buflen ) ) != 0 )
2591 {
2592 pk_free( pk );
2593 }
2594
2595 pem_free( &pem );
2596 return( ret );
2597 }
2598 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2599 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2600 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2601 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2602 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2603 return( ret );
2604
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002605 ret = pem_read_buffer( &pem,
2606 "-----BEGIN EC PRIVATE KEY-----",
2607 "-----END EC PRIVATE KEY-----",
2608 key, pwd, pwdlen, &len );
2609 if( ret == 0 )
2610 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002611 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2612 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2613 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002614 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002615 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002616 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002617
2618 pem_free( &pem );
2619 return( ret );
2620 }
2621 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2622 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2623 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2624 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2625 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2626 return( ret );
2627
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002628 ret = pem_read_buffer( &pem,
2629 "-----BEGIN PRIVATE KEY-----",
2630 "-----END PRIVATE KEY-----",
2631 key, NULL, 0, &len );
2632 if( ret == 0 )
2633 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002634 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002635 pem.buf, pem.buflen ) ) != 0 )
2636 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002637 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002638 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002639
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002640 pem_free( &pem );
2641 return( ret );
2642 }
2643 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2644 return( ret );
2645
2646 ret = pem_read_buffer( &pem,
2647 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2648 "-----END ENCRYPTED PRIVATE KEY-----",
2649 key, NULL, 0, &len );
2650 if( ret == 0 )
2651 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002652 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002653 pem.buf, pem.buflen,
2654 pwd, pwdlen ) ) != 0 )
2655 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002656 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002657 }
2658
2659 pem_free( &pem );
2660 return( ret );
2661 }
2662 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2663 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002664#else
2665 ((void) pwd);
2666 ((void) pwdlen);
2667#endif /* POLARSSL_PEM_C */
2668
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002669 /*
2670 * At this point we only know it's not a PEM formatted key. Could be any
2671 * of the known DER encoded private key formats
2672 *
2673 * We try the different DER format parsers to see if one passes without
2674 * error
2675 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002676 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2677 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002678 {
2679 return( 0 );
2680 }
2681
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002682 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002683
2684 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2685 {
2686 return( ret );
2687 }
2688
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002689 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002690 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002691
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002692 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002693
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002694 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2695 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2696 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002697 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002698 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002699
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002700 pk_free( pk );
2701
2702 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2703 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2704 {
2705 return( 0 );
2706 }
2707
2708 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002709
2710 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2711}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002712
2713/*
2714 * Parse a public key
2715 */
2716int x509parse_public_key( pk_context *ctx,
2717 const unsigned char *key, size_t keylen )
2718{
2719 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002720 unsigned char *p;
2721#if defined(POLARSSL_PEM_C)
2722 size_t len;
2723 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002724
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002725 pem_init( &pem );
2726 ret = pem_read_buffer( &pem,
2727 "-----BEGIN PUBLIC KEY-----",
2728 "-----END PUBLIC KEY-----",
2729 key, NULL, 0, &len );
2730
2731 if( ret == 0 )
2732 {
2733 /*
2734 * Was PEM encoded
2735 */
2736 key = pem.buf;
2737 keylen = pem.buflen;
2738 }
2739 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2740 {
2741 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002742 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002743 }
2744#endif
2745 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002746
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002747 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002748
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002749#if defined(POLARSSL_PEM_C)
2750 pem_free( &pem );
2751#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002752
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002753 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002754}
2755
Paul Bakkereaa89f82011-04-04 21:36:15 +00002756#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002757/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002758 * Parse DHM parameters
2759 */
Paul Bakker23986e52011-04-24 08:57:21 +00002760int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002761{
Paul Bakker23986e52011-04-24 08:57:21 +00002762 int ret;
2763 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002764 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002765#if defined(POLARSSL_PEM_C)
2766 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002767
Paul Bakker96743fc2011-02-12 14:30:57 +00002768 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002769
Paul Bakker96743fc2011-02-12 14:30:57 +00002770 ret = pem_read_buffer( &pem,
2771 "-----BEGIN DH PARAMETERS-----",
2772 "-----END DH PARAMETERS-----",
2773 dhmin, NULL, 0, &dhminlen );
2774
2775 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002776 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002777 /*
2778 * Was PEM encoded
2779 */
2780 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002781 }
Paul Bakker00b28602013-06-24 13:02:41 +02002782 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002783 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002784 pem_free( &pem );
2785 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002786 }
2787
Paul Bakker96743fc2011-02-12 14:30:57 +00002788 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2789#else
2790 p = (unsigned char *) dhmin;
2791#endif
2792 end = p + dhminlen;
2793
Paul Bakker1b57b062011-01-06 15:48:19 +00002794 memset( dhm, 0, sizeof( dhm_context ) );
2795
Paul Bakker1b57b062011-01-06 15:48:19 +00002796 /*
2797 * DHParams ::= SEQUENCE {
2798 * prime INTEGER, -- P
2799 * generator INTEGER, -- g
2800 * }
2801 */
2802 if( ( ret = asn1_get_tag( &p, end, &len,
2803 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2804 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002805#if defined(POLARSSL_PEM_C)
2806 pem_free( &pem );
2807#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002808 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002809 }
2810
2811 end = p + len;
2812
2813 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2814 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2815 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002816#if defined(POLARSSL_PEM_C)
2817 pem_free( &pem );
2818#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002819 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002820 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002821 }
2822
2823 if( p != end )
2824 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002825#if defined(POLARSSL_PEM_C)
2826 pem_free( &pem );
2827#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002828 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002829 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002830 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2831 }
2832
Paul Bakker96743fc2011-02-12 14:30:57 +00002833#if defined(POLARSSL_PEM_C)
2834 pem_free( &pem );
2835#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002836
2837 return( 0 );
2838}
2839
Paul Bakker335db3f2011-04-25 15:28:35 +00002840#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002841/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002842 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002843 */
2844int x509parse_dhmfile( dhm_context *dhm, const char *path )
2845{
2846 int ret;
2847 size_t n;
2848 unsigned char *buf;
2849
Paul Bakker69e095c2011-12-10 21:55:01 +00002850 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2851 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002852
Paul Bakker27fdf462011-06-09 13:55:13 +00002853 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002854
2855 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002856 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002857
2858 return( ret );
2859}
Paul Bakker335db3f2011-04-25 15:28:35 +00002860#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002861#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002862
Paul Bakker5121ce52009-01-03 21:22:43 +00002863#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002864#include <stdarg.h>
2865
2866#if !defined vsnprintf
2867#define vsnprintf _vsnprintf
2868#endif // vsnprintf
2869
2870/*
2871 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2872 * Result value is not size of buffer needed, but -1 if no fit is possible.
2873 *
2874 * This fuction tries to 'fix' this by at least suggesting enlarging the
2875 * size by 20.
2876 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002877static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002878{
2879 va_list ap;
2880 int res = -1;
2881
2882 va_start( ap, format );
2883
2884 res = vsnprintf( str, size, format, ap );
2885
2886 va_end( ap );
2887
2888 // No quick fix possible
2889 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002891
2892 return res;
2893}
2894
2895#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002896#endif
2897
Paul Bakkerd98030e2009-05-02 15:13:40 +00002898#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2899
2900#define SAFE_SNPRINTF() \
2901{ \
2902 if( ret == -1 ) \
2903 return( -1 ); \
2904 \
Paul Bakker23986e52011-04-24 08:57:21 +00002905 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002906 p[n - 1] = '\0'; \
2907 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2908 } \
2909 \
Paul Bakker23986e52011-04-24 08:57:21 +00002910 n -= (unsigned int) ret; \
2911 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912}
2913
Paul Bakker5121ce52009-01-03 21:22:43 +00002914/*
2915 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002916 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002918int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919{
Paul Bakker23986e52011-04-24 08:57:21 +00002920 int ret;
2921 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002923 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002924 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 char s[128], *p;
2926
2927 memset( s, 0, sizeof( s ) );
2928
2929 name = dn;
2930 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002931 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
2933 while( name != NULL )
2934 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002935 if( !name->oid.p )
2936 {
2937 name = name->next;
2938 continue;
2939 }
2940
Paul Bakker74111d32011-01-15 16:57:55 +00002941 if( name != dn )
2942 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002943 ret = snprintf( p, n, ", " );
2944 SAFE_SNPRINTF();
2945 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Paul Bakkerc70b9822013-04-07 22:00:46 +02002947 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakkerc70b9822013-04-07 22:00:46 +02002949 if( ret == 0 )
2950 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002952 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002953 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
2955 for( i = 0; i < name->val.len; i++ )
2956 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002957 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 break;
2959
2960 c = name->val.p[i];
2961 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2962 s[i] = '?';
2963 else s[i] = c;
2964 }
2965 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002966 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002967 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002968 name = name->next;
2969 }
2970
Paul Bakker23986e52011-04-24 08:57:21 +00002971 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972}
2973
2974/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002975 * Store the serial in printable form into buf; no more
2976 * than size characters will be written
2977 */
2978int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2979{
Paul Bakker23986e52011-04-24 08:57:21 +00002980 int ret;
2981 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002982 char *p;
2983
2984 p = buf;
2985 n = size;
2986
2987 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002988 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002989
2990 for( i = 0; i < nr; i++ )
2991 {
Paul Bakker93048802011-12-05 14:38:06 +00002992 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002993 continue;
2994
Paul Bakkerdd476992011-01-16 21:34:59 +00002995 ret = snprintf( p, n, "%02X%s",
2996 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2997 SAFE_SNPRINTF();
2998 }
2999
Paul Bakker03c7c252011-11-25 12:37:37 +00003000 if( nr != serial->len )
3001 {
3002 ret = snprintf( p, n, "...." );
3003 SAFE_SNPRINTF();
3004 }
3005
Paul Bakker23986e52011-04-24 08:57:21 +00003006 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003007}
3008
3009/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003010 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003012int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3013 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003014{
Paul Bakker23986e52011-04-24 08:57:21 +00003015 int ret;
3016 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003017 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003018 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
3020 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003021 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
Paul Bakkerd98030e2009-05-02 15:13:40 +00003023 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003025 SAFE_SNPRINTF();
3026 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003028 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakkerdd476992011-01-16 21:34:59 +00003030 ret = x509parse_serial_gets( p, n, &crt->serial);
3031 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakkerd98030e2009-05-02 15:13:40 +00003033 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3034 SAFE_SNPRINTF();
3035 ret = x509parse_dn_gets( p, n, &crt->issuer );
3036 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Paul Bakkerd98030e2009-05-02 15:13:40 +00003038 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3039 SAFE_SNPRINTF();
3040 ret = x509parse_dn_gets( p, n, &crt->subject );
3041 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Paul Bakkerd98030e2009-05-02 15:13:40 +00003043 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3045 crt->valid_from.year, crt->valid_from.mon,
3046 crt->valid_from.day, crt->valid_from.hour,
3047 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003048 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Paul Bakkerd98030e2009-05-02 15:13:40 +00003050 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3052 crt->valid_to.year, crt->valid_to.mon,
3053 crt->valid_to.day, crt->valid_to.hour,
3054 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003055 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakkerc70b9822013-04-07 22:00:46 +02003057 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003058 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakkerc70b9822013-04-07 22:00:46 +02003060 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3061 if( ret != 0 )
3062 ret = snprintf( p, n, "???" );
3063 else
3064 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003065 SAFE_SNPRINTF();
3066
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003067 switch( crt->pk.type )
3068 {
3069 case POLARSSL_PK_NONE:
3070 case POLARSSL_PK_ECDSA:
3071 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3072 break;
3073
3074 case POLARSSL_PK_RSA:
3075 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3076 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3077 break;
3078
3079 case POLARSSL_PK_ECKEY:
3080 case POLARSSL_PK_ECKEY_DH:
3081 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3082 (int) pk_ec( crt->pk )->grp.pbits );
3083 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003084 SAFE_SNPRINTF();
3085
Paul Bakker23986e52011-04-24 08:57:21 +00003086 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003087}
3088
Paul Bakker74111d32011-01-15 16:57:55 +00003089/*
3090 * Return an informational string describing the given OID
3091 */
3092const char *x509_oid_get_description( x509_buf *oid )
3093{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003094 const char *desc = NULL;
3095 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003096
Paul Bakkerc70b9822013-04-07 22:00:46 +02003097 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003098
Paul Bakkerc70b9822013-04-07 22:00:46 +02003099 if( ret != 0 )
3100 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003101
Paul Bakkerc70b9822013-04-07 22:00:46 +02003102 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003103}
3104
3105/* Return the x.y.z.... style numeric string for the given OID */
3106int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3107{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003108 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003109}
3110
Paul Bakkerd98030e2009-05-02 15:13:40 +00003111/*
3112 * Return an informational string about the CRL.
3113 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003114int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3115 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003116{
Paul Bakker23986e52011-04-24 08:57:21 +00003117 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003118 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003119 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003120 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003121 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122
3123 p = buf;
3124 n = size;
3125
3126 ret = snprintf( p, n, "%sCRL version : %d",
3127 prefix, crl->version );
3128 SAFE_SNPRINTF();
3129
3130 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3131 SAFE_SNPRINTF();
3132 ret = x509parse_dn_gets( p, n, &crl->issuer );
3133 SAFE_SNPRINTF();
3134
3135 ret = snprintf( p, n, "\n%sthis update : " \
3136 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3137 crl->this_update.year, crl->this_update.mon,
3138 crl->this_update.day, crl->this_update.hour,
3139 crl->this_update.min, crl->this_update.sec );
3140 SAFE_SNPRINTF();
3141
3142 ret = snprintf( p, n, "\n%snext update : " \
3143 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3144 crl->next_update.year, crl->next_update.mon,
3145 crl->next_update.day, crl->next_update.hour,
3146 crl->next_update.min, crl->next_update.sec );
3147 SAFE_SNPRINTF();
3148
3149 entry = &crl->entry;
3150
3151 ret = snprintf( p, n, "\n%sRevoked certificates:",
3152 prefix );
3153 SAFE_SNPRINTF();
3154
Paul Bakker9be19372009-07-27 20:21:53 +00003155 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003156 {
3157 ret = snprintf( p, n, "\n%sserial number: ",
3158 prefix );
3159 SAFE_SNPRINTF();
3160
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003161 ret = x509parse_serial_gets( p, n, &entry->serial);
3162 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003163
Paul Bakkerd98030e2009-05-02 15:13:40 +00003164 ret = snprintf( p, n, " revocation date: " \
3165 "%04d-%02d-%02d %02d:%02d:%02d",
3166 entry->revocation_date.year, entry->revocation_date.mon,
3167 entry->revocation_date.day, entry->revocation_date.hour,
3168 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003169 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170
3171 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 }
3173
Paul Bakkerc70b9822013-04-07 22:00:46 +02003174 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003175 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Paul Bakkerc70b9822013-04-07 22:00:46 +02003177 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3178 if( ret != 0 )
3179 ret = snprintf( p, n, "???" );
3180 else
3181 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003182 SAFE_SNPRINTF();
3183
Paul Bakker1e27bb22009-07-19 20:25:25 +00003184 ret = snprintf( p, n, "\n" );
3185 SAFE_SNPRINTF();
3186
Paul Bakker23986e52011-04-24 08:57:21 +00003187 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188}
3189
3190/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003191 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003193#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003194int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195{
Paul Bakkercce9d772011-11-18 14:26:47 +00003196 int year, mon, day;
3197 int hour, min, sec;
3198
3199#if defined(_WIN32)
3200 SYSTEMTIME st;
3201
3202 GetLocalTime(&st);
3203
3204 year = st.wYear;
3205 mon = st.wMonth;
3206 day = st.wDay;
3207 hour = st.wHour;
3208 min = st.wMinute;
3209 sec = st.wSecond;
3210#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 struct tm *lt;
3212 time_t tt;
3213
3214 tt = time( NULL );
3215 lt = localtime( &tt );
3216
Paul Bakkercce9d772011-11-18 14:26:47 +00003217 year = lt->tm_year + 1900;
3218 mon = lt->tm_mon + 1;
3219 day = lt->tm_mday;
3220 hour = lt->tm_hour;
3221 min = lt->tm_min;
3222 sec = lt->tm_sec;
3223#endif
3224
3225 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003226 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Paul Bakkercce9d772011-11-18 14:26:47 +00003228 if( year == to->year &&
3229 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003230 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231
Paul Bakkercce9d772011-11-18 14:26:47 +00003232 if( year == to->year &&
3233 mon == to->mon &&
3234 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003235 return( 1 );
3236
Paul Bakkercce9d772011-11-18 14:26:47 +00003237 if( year == to->year &&
3238 mon == to->mon &&
3239 day == to->day &&
3240 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003241 return( 1 );
3242
Paul Bakkercce9d772011-11-18 14:26:47 +00003243 if( year == to->year &&
3244 mon == to->mon &&
3245 day == to->day &&
3246 hour == to->hour &&
3247 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003248 return( 1 );
3249
Paul Bakkercce9d772011-11-18 14:26:47 +00003250 if( year == to->year &&
3251 mon == to->mon &&
3252 day == to->day &&
3253 hour == to->hour &&
3254 min == to->min &&
3255 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003256 return( 1 );
3257
Paul Bakker40ea7de2009-05-03 10:18:48 +00003258 return( 0 );
3259}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003260#else /* POLARSSL_HAVE_TIME */
3261int x509parse_time_expired( const x509_time *to )
3262{
3263 ((void) to);
3264 return( 0 );
3265}
3266#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003267
3268/*
3269 * Return 1 if the certificate is revoked, or 0 otherwise.
3270 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003271int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003272{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003273 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003274
3275 while( cur != NULL && cur->serial.len != 0 )
3276 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003277 if( crt->serial.len == cur->serial.len &&
3278 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003279 {
3280 if( x509parse_time_expired( &cur->revocation_date ) )
3281 return( 1 );
3282 }
3283
3284 cur = cur->next;
3285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
3287 return( 0 );
3288}
3289
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003290/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003291 * Check that the given certificate is valid accoring to the CRL.
3292 */
3293static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3294 x509_crl *crl_list)
3295{
3296 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003297 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3298 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003299
Paul Bakker915275b2012-09-28 07:10:55 +00003300 if( ca == NULL )
3301 return( flags );
3302
Paul Bakker76fd75a2011-01-16 21:12:10 +00003303 /*
3304 * TODO: What happens if no CRL is present?
3305 * Suggestion: Revocation state should be unknown if no CRL is present.
3306 * For backwards compatibility this is not yet implemented.
3307 */
3308
Paul Bakker915275b2012-09-28 07:10:55 +00003309 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003310 {
Paul Bakker915275b2012-09-28 07:10:55 +00003311 if( crl_list->version == 0 ||
3312 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003313 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3314 crl_list->issuer_raw.len ) != 0 )
3315 {
3316 crl_list = crl_list->next;
3317 continue;
3318 }
3319
3320 /*
3321 * Check if CRL is correctly signed by the trusted CA
3322 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003323 md_info = md_info_from_type( crl_list->sig_md );
3324 if( md_info == NULL )
3325 {
3326 /*
3327 * Cannot check 'unknown' hash
3328 */
3329 flags |= BADCRL_NOT_TRUSTED;
3330 break;
3331 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003332
Paul Bakkerc70b9822013-04-07 22:00:46 +02003333 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003334
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003335 /* EC NOT IMPLEMENTED YET */
3336 if( ca->pk.type != POLARSSL_PK_RSA )
3337 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3338
3339 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003340 0, hash, crl_list->sig.p ) == 0 )
3341 {
3342 /*
3343 * CRL is not trusted
3344 */
3345 flags |= BADCRL_NOT_TRUSTED;
3346 break;
3347 }
3348
3349 /*
3350 * Check for validity of CRL (Do not drop out)
3351 */
3352 if( x509parse_time_expired( &crl_list->next_update ) )
3353 flags |= BADCRL_EXPIRED;
3354
3355 /*
3356 * Check if certificate is revoked
3357 */
3358 if( x509parse_revoked(crt, crl_list) )
3359 {
3360 flags |= BADCERT_REVOKED;
3361 break;
3362 }
3363
3364 crl_list = crl_list->next;
3365 }
3366 return flags;
3367}
3368
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003369static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003370{
3371 size_t i;
3372 size_t cn_idx = 0;
3373
Paul Bakker57b12982012-02-11 17:38:38 +00003374 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003375 return( 0 );
3376
3377 for( i = 0; i < strlen( cn ); ++i )
3378 {
3379 if( cn[i] == '.' )
3380 {
3381 cn_idx = i;
3382 break;
3383 }
3384 }
3385
3386 if( cn_idx == 0 )
3387 return( 0 );
3388
Paul Bakker535e97d2012-08-23 10:49:55 +00003389 if( strlen( cn ) - cn_idx == name->len - 1 &&
3390 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003391 {
3392 return( 1 );
3393 }
3394
3395 return( 0 );
3396}
3397
Paul Bakker915275b2012-09-28 07:10:55 +00003398static int x509parse_verify_top(
3399 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003400 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003401 int (*f_vrfy)(void *, x509_cert *, int, int *),
3402 void *p_vrfy )
3403{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003404 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003405 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003406 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3407 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003408
3409 if( x509parse_time_expired( &child->valid_to ) )
3410 *flags |= BADCERT_EXPIRED;
3411
3412 /*
3413 * Child is the top of the chain. Check against the trust_ca list.
3414 */
3415 *flags |= BADCERT_NOT_TRUSTED;
3416
3417 while( trust_ca != NULL )
3418 {
3419 if( trust_ca->version == 0 ||
3420 child->issuer_raw.len != trust_ca->subject_raw.len ||
3421 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3422 child->issuer_raw.len ) != 0 )
3423 {
3424 trust_ca = trust_ca->next;
3425 continue;
3426 }
3427
Paul Bakker9a736322012-11-14 12:39:52 +00003428 /*
3429 * Reduce path_len to check against if top of the chain is
3430 * the same as the trusted CA
3431 */
3432 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3433 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3434 child->issuer_raw.len ) == 0 )
3435 {
3436 check_path_cnt--;
3437 }
3438
Paul Bakker915275b2012-09-28 07:10:55 +00003439 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003440 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003441 {
3442 trust_ca = trust_ca->next;
3443 continue;
3444 }
3445
Paul Bakkerc70b9822013-04-07 22:00:46 +02003446 md_info = md_info_from_type( child->sig_md );
3447 if( md_info == NULL )
3448 {
3449 /*
3450 * Cannot check 'unknown' hash
3451 */
3452 continue;
3453 }
Paul Bakker915275b2012-09-28 07:10:55 +00003454
Paul Bakkerc70b9822013-04-07 22:00:46 +02003455 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003456
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003457 /* EC NOT IMPLEMENTED YET */
3458 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3459 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3460
3461 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003462 0, hash, child->sig.p ) != 0 )
3463 {
3464 trust_ca = trust_ca->next;
3465 continue;
3466 }
3467
3468 /*
3469 * Top of chain is signed by a trusted CA
3470 */
3471 *flags &= ~BADCERT_NOT_TRUSTED;
3472 break;
3473 }
3474
Paul Bakker9a736322012-11-14 12:39:52 +00003475 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003476 * If top of chain is not the same as the trusted CA send a verify request
3477 * to the callback for any issues with validity and CRL presence for the
3478 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003479 */
3480 if( trust_ca != NULL &&
3481 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3482 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3483 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003484 {
3485 /* Check trusted CA's CRL for then chain's top crt */
3486 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3487
3488 if( x509parse_time_expired( &trust_ca->valid_to ) )
3489 ca_flags |= BADCERT_EXPIRED;
3490
Paul Bakker915275b2012-09-28 07:10:55 +00003491 if( NULL != f_vrfy )
3492 {
Paul Bakker9a736322012-11-14 12:39:52 +00003493 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003494 return( ret );
3495 }
3496 }
3497
3498 /* Call callback on top cert */
3499 if( NULL != f_vrfy )
3500 {
Paul Bakker9a736322012-11-14 12:39:52 +00003501 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003502 return( ret );
3503 }
3504
Paul Bakker915275b2012-09-28 07:10:55 +00003505 *flags |= ca_flags;
3506
3507 return( 0 );
3508}
3509
3510static int x509parse_verify_child(
3511 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003512 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003513 int (*f_vrfy)(void *, x509_cert *, int, int *),
3514 void *p_vrfy )
3515{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003516 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003517 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003518 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003519 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003520 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003521
3522 if( x509parse_time_expired( &child->valid_to ) )
3523 *flags |= BADCERT_EXPIRED;
3524
Paul Bakkerc70b9822013-04-07 22:00:46 +02003525 md_info = md_info_from_type( child->sig_md );
3526 if( md_info == NULL )
3527 {
3528 /*
3529 * Cannot check 'unknown' hash
3530 */
Paul Bakker915275b2012-09-28 07:10:55 +00003531 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003532 }
3533 else
3534 {
3535 md( md_info, child->tbs.p, child->tbs.len, hash );
3536
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003537 /* EC NOT IMPLEMENTED YET */
3538 if( parent->pk.type != POLARSSL_PK_RSA )
3539 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3540
3541 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3542 0, hash, child->sig.p ) != 0 )
3543 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003544 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003545 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003546 }
3547
Paul Bakker915275b2012-09-28 07:10:55 +00003548 /* Check trusted CA's CRL for the given crt */
3549 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3550
3551 grandparent = parent->next;
3552
3553 while( grandparent != NULL )
3554 {
3555 if( grandparent->version == 0 ||
3556 grandparent->ca_istrue == 0 ||
3557 parent->issuer_raw.len != grandparent->subject_raw.len ||
3558 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3559 parent->issuer_raw.len ) != 0 )
3560 {
3561 grandparent = grandparent->next;
3562 continue;
3563 }
3564 break;
3565 }
3566
Paul Bakker915275b2012-09-28 07:10:55 +00003567 if( grandparent != NULL )
3568 {
3569 /*
3570 * Part of the chain
3571 */
Paul Bakker9a736322012-11-14 12:39:52 +00003572 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 +00003573 if( ret != 0 )
3574 return( ret );
3575 }
3576 else
3577 {
Paul Bakker9a736322012-11-14 12:39:52 +00003578 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 +00003579 if( ret != 0 )
3580 return( ret );
3581 }
3582
3583 /* child is verified to be a child of the parent, call verify callback */
3584 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003585 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003586 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003587
3588 *flags |= parent_flags;
3589
3590 return( 0 );
3591}
3592
Paul Bakker76fd75a2011-01-16 21:12:10 +00003593/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 * Verify the certificate validity
3595 */
3596int x509parse_verify( x509_cert *crt,
3597 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003598 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003599 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003600 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003601 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602{
Paul Bakker23986e52011-04-24 08:57:21 +00003603 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003604 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003605 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003606 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003608 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Paul Bakker40ea7de2009-05-03 10:18:48 +00003610 *flags = 0;
3611
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 if( cn != NULL )
3613 {
3614 name = &crt->subject;
3615 cn_len = strlen( cn );
3616
Paul Bakker4d2c1242012-05-10 14:12:46 +00003617 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003619 cur = &crt->subject_alt_names;
3620
3621 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003622 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003623 if( cur->buf.len == cn_len &&
3624 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003625 break;
3626
Paul Bakker535e97d2012-08-23 10:49:55 +00003627 if( cur->buf.len > 2 &&
3628 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003629 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Paul Bakker4d2c1242012-05-10 14:12:46 +00003632 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003633 }
3634
3635 if( cur == NULL )
3636 *flags |= BADCERT_CN_MISMATCH;
3637 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003638 else
3639 {
3640 while( name != NULL )
3641 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003642 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003643 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003644 if( name->val.len == cn_len &&
3645 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003646 break;
3647
Paul Bakker535e97d2012-08-23 10:49:55 +00003648 if( name->val.len > 2 &&
3649 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003650 x509_wildcard_verify( cn, &name->val ) )
3651 break;
3652 }
3653
3654 name = name->next;
3655 }
3656
3657 if( name == NULL )
3658 *flags |= BADCERT_CN_MISMATCH;
3659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 }
3661
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003663 * Iterate upwards in the given cert chain, to find our crt parent.
3664 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003666 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Paul Bakker76fd75a2011-01-16 21:12:10 +00003668 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003670 if( parent->ca_istrue == 0 ||
3671 crt->issuer_raw.len != parent->subject_raw.len ||
3672 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 crt->issuer_raw.len ) != 0 )
3674 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003675 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 continue;
3677 }
Paul Bakker915275b2012-09-28 07:10:55 +00003678 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Paul Bakker915275b2012-09-28 07:10:55 +00003681 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 {
Paul Bakker915275b2012-09-28 07:10:55 +00003683 /*
3684 * Part of the chain
3685 */
Paul Bakker9a736322012-11-14 12:39:52 +00003686 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003687 if( ret != 0 )
3688 return( ret );
3689 }
3690 else
Paul Bakker74111d32011-01-15 16:57:55 +00003691 {
Paul Bakker9a736322012-11-14 12:39:52 +00003692 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003693 if( ret != 0 )
3694 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003695 }
Paul Bakker915275b2012-09-28 07:10:55 +00003696
3697 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003698 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003699
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 return( 0 );
3701}
3702
3703/*
3704 * Unallocate all certificate data
3705 */
3706void x509_free( x509_cert *crt )
3707{
3708 x509_cert *cert_cur = crt;
3709 x509_cert *cert_prv;
3710 x509_name *name_cur;
3711 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003712 x509_sequence *seq_cur;
3713 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
3715 if( crt == NULL )
3716 return;
3717
3718 do
3719 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003720 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
3722 name_cur = cert_cur->issuer.next;
3723 while( name_cur != NULL )
3724 {
3725 name_prv = name_cur;
3726 name_cur = name_cur->next;
3727 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003728 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 }
3730
3731 name_cur = cert_cur->subject.next;
3732 while( name_cur != NULL )
3733 {
3734 name_prv = name_cur;
3735 name_cur = name_cur->next;
3736 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003737 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 }
3739
Paul Bakker74111d32011-01-15 16:57:55 +00003740 seq_cur = cert_cur->ext_key_usage.next;
3741 while( seq_cur != NULL )
3742 {
3743 seq_prv = seq_cur;
3744 seq_cur = seq_cur->next;
3745 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003746 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003747 }
3748
Paul Bakker8afa70d2012-02-11 18:42:45 +00003749 seq_cur = cert_cur->subject_alt_names.next;
3750 while( seq_cur != NULL )
3751 {
3752 seq_prv = seq_cur;
3753 seq_cur = seq_cur->next;
3754 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003755 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003756 }
3757
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 if( cert_cur->raw.p != NULL )
3759 {
3760 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003761 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 }
3763
3764 cert_cur = cert_cur->next;
3765 }
3766 while( cert_cur != NULL );
3767
3768 cert_cur = crt;
3769 do
3770 {
3771 cert_prv = cert_cur;
3772 cert_cur = cert_cur->next;
3773
3774 memset( cert_prv, 0, sizeof( x509_cert ) );
3775 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003776 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 }
3778 while( cert_cur != NULL );
3779}
3780
Paul Bakkerd98030e2009-05-02 15:13:40 +00003781/*
3782 * Unallocate all CRL data
3783 */
3784void x509_crl_free( x509_crl *crl )
3785{
3786 x509_crl *crl_cur = crl;
3787 x509_crl *crl_prv;
3788 x509_name *name_cur;
3789 x509_name *name_prv;
3790 x509_crl_entry *entry_cur;
3791 x509_crl_entry *entry_prv;
3792
3793 if( crl == NULL )
3794 return;
3795
3796 do
3797 {
3798 name_cur = crl_cur->issuer.next;
3799 while( name_cur != NULL )
3800 {
3801 name_prv = name_cur;
3802 name_cur = name_cur->next;
3803 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003804 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003805 }
3806
3807 entry_cur = crl_cur->entry.next;
3808 while( entry_cur != NULL )
3809 {
3810 entry_prv = entry_cur;
3811 entry_cur = entry_cur->next;
3812 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003813 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003814 }
3815
3816 if( crl_cur->raw.p != NULL )
3817 {
3818 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003819 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003820 }
3821
3822 crl_cur = crl_cur->next;
3823 }
3824 while( crl_cur != NULL );
3825
3826 crl_cur = crl;
3827 do
3828 {
3829 crl_prv = crl_cur;
3830 crl_cur = crl_cur->next;
3831
3832 memset( crl_prv, 0, sizeof( x509_crl ) );
3833 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003834 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003835 }
3836 while( crl_cur != NULL );
3837}
3838
Paul Bakker40e46942009-01-03 21:51:57 +00003839#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Paul Bakker40e46942009-01-03 21:51:57 +00003841#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843/*
3844 * Checkup routine
3845 */
3846int x509_self_test( int verbose )
3847{
Paul Bakker5690efc2011-05-26 13:16:06 +00003848#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003849 int ret;
3850 int flags;
3851 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 x509_cert cacert;
3853 x509_cert clicert;
3854 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003855#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003856 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
3859 if( verbose != 0 )
3860 printf( " X.509 certificate load: " );
3861
3862 memset( &clicert, 0, sizeof( x509_cert ) );
3863
Paul Bakker3c2122f2013-06-24 19:03:14 +02003864 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003865 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 if( ret != 0 )
3867 {
3868 if( verbose != 0 )
3869 printf( "failed\n" );
3870
3871 return( ret );
3872 }
3873
3874 memset( &cacert, 0, sizeof( x509_cert ) );
3875
Paul Bakker3c2122f2013-06-24 19:03:14 +02003876 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003877 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 if( ret != 0 )
3879 {
3880 if( verbose != 0 )
3881 printf( "failed\n" );
3882
3883 return( ret );
3884 }
3885
3886 if( verbose != 0 )
3887 printf( "passed\n X.509 private key load: " );
3888
3889 i = strlen( test_ca_key );
3890 j = strlen( test_ca_pwd );
3891
Paul Bakker66b78b22011-03-25 14:22:50 +00003892 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3893
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003894 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003895 (const unsigned char *) test_ca_key, i,
3896 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 {
3898 if( verbose != 0 )
3899 printf( "failed\n" );
3900
3901 return( ret );
3902 }
3903
3904 if( verbose != 0 )
3905 printf( "passed\n X.509 signature verify: ");
3906
Paul Bakker23986e52011-04-24 08:57:21 +00003907 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 if( ret != 0 )
3909 {
Paul Bakker23986e52011-04-24 08:57:21 +00003910 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 if( verbose != 0 )
3912 printf( "failed\n" );
3913
3914 return( ret );
3915 }
3916
Paul Bakker5690efc2011-05-26 13:16:06 +00003917#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003919 printf( "passed\n X.509 DHM parameter load: " );
3920
3921 i = strlen( test_dhm_params );
3922 j = strlen( test_ca_pwd );
3923
Paul Bakker3c2122f2013-06-24 19:03:14 +02003924 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003925 {
3926 if( verbose != 0 )
3927 printf( "failed\n" );
3928
3929 return( ret );
3930 }
3931
3932 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003935
3936 x509_free( &cacert );
3937 x509_free( &clicert );
3938 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003939#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003940 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
3943 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003944#else
3945 ((void) verbose);
3946 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003948}
3949
3950#endif
3951
3952#endif