blob: 1dca19d4285e4406e08b856a2d62ade3224744ac [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:
593 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
594 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200595
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200596 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200597 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
598 x509_get_ecpubkey( p, end, pk->data );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200599 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200600 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200601
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200602 if( ret == 0 && *p != end )
603 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
604 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
605
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200606 if( ret != 0 )
607 pk_free( pk );
608
609 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200610}
611
Paul Bakker5121ce52009-01-03 21:22:43 +0000612static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000613 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 x509_buf *sig )
615{
Paul Bakker23986e52011-04-24 08:57:21 +0000616 int ret;
617 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Paul Bakker8afa70d2012-02-11 18:42:45 +0000619 if( ( end - *p ) < 1 )
620 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
621 POLARSSL_ERR_ASN1_OUT_OF_DATA );
622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 sig->tag = **p;
624
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200625 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000626 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 sig->len = len;
629 sig->p = *p;
630
631 *p += len;
632
633 return( 0 );
634}
635
636/*
637 * X.509 v2/v3 unique identifier (not parsed)
638 */
639static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000640 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 x509_buf *uid, int n )
642{
643 int ret;
644
645 if( *p == end )
646 return( 0 );
647
648 uid->tag = **p;
649
650 if( ( ret = asn1_get_tag( p, end, &uid->len,
651 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
652 {
Paul Bakker40e46942009-01-03 21:51:57 +0000653 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 return( 0 );
655
656 return( ret );
657 }
658
659 uid->p = *p;
660 *p += uid->len;
661
662 return( 0 );
663}
664
665/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000666 * X.509 Extensions (No parsing of extensions, pointer should
667 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
669static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000670 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000671 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000672{
Paul Bakker23986e52011-04-24 08:57:21 +0000673 int ret;
674 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 if( *p == end )
677 return( 0 );
678
679 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000680
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000682 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685 ext->p = *p;
686 end = *p + ext->len;
687
688 /*
689 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
690 *
691 * Extension ::= SEQUENCE {
692 * extnID OBJECT IDENTIFIER,
693 * critical BOOLEAN DEFAULT FALSE,
694 * extnValue OCTET STRING }
695 */
696 if( ( ret = asn1_get_tag( p, end, &len,
697 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000698 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000701 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000702 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakkerd98030e2009-05-02 15:13:40 +0000704 return( 0 );
705}
706
707/*
708 * X.509 CRL v2 extensions (no extensions parsed yet.)
709 */
710static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000711 const unsigned char *end,
712 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000713{
Paul Bakker23986e52011-04-24 08:57:21 +0000714 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000715 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000716
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000717 /* Get explicit tag */
718 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000719 {
720 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
721 return( 0 );
722
723 return( ret );
724 }
725
726 while( *p < end )
727 {
728 if( ( ret = asn1_get_tag( p, end, &len,
729 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000731
732 *p += len;
733 }
734
735 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 return( 0 );
740}
741
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000742/*
743 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
744 */
745static int x509_get_crl_entry_ext( unsigned char **p,
746 const unsigned char *end,
747 x509_buf *ext )
748{
749 int ret;
750 size_t len = 0;
751
752 /* OPTIONAL */
753 if (end <= *p)
754 return( 0 );
755
756 ext->tag = **p;
757 ext->p = *p;
758
759 /*
760 * Get CRL-entry extension sequence header
761 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
762 */
763 if( ( ret = asn1_get_tag( p, end, &ext->len,
764 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
765 {
766 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
767 {
768 ext->p = NULL;
769 return( 0 );
770 }
771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
772 }
773
774 end = *p + ext->len;
775
776 if( end != *p + ext->len )
777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
778 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
779
780 while( *p < end )
781 {
782 if( ( ret = asn1_get_tag( p, end, &len,
783 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
785
786 *p += len;
787 }
788
789 if( *p != end )
790 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
791 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
792
793 return( 0 );
794}
795
Paul Bakker74111d32011-01-15 16:57:55 +0000796static int x509_get_basic_constraints( unsigned char **p,
797 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000798 int *ca_istrue,
799 int *max_pathlen )
800{
Paul Bakker23986e52011-04-24 08:57:21 +0000801 int ret;
802 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000803
804 /*
805 * BasicConstraints ::= SEQUENCE {
806 * cA BOOLEAN DEFAULT FALSE,
807 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
808 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000809 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000810 *max_pathlen = 0; /* endless */
811
812 if( ( ret = asn1_get_tag( p, end, &len,
813 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000814 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000815
816 if( *p == end )
817 return 0;
818
Paul Bakker3cccddb2011-01-16 21:46:31 +0000819 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000820 {
821 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000822 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
824 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000825 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000826
Paul Bakker3cccddb2011-01-16 21:46:31 +0000827 if( *ca_istrue != 0 )
828 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000829 }
830
831 if( *p == end )
832 return 0;
833
834 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000835 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000836
837 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000839 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
840
841 (*max_pathlen)++;
842
Paul Bakker74111d32011-01-15 16:57:55 +0000843 return 0;
844}
845
846static int x509_get_ns_cert_type( unsigned char **p,
847 const unsigned char *end,
848 unsigned char *ns_cert_type)
849{
850 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000851 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000852
853 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000855
856 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000858 POLARSSL_ERR_ASN1_INVALID_LENGTH );
859
860 /* Get actual bitstring */
861 *ns_cert_type = *bs.p;
862 return 0;
863}
864
865static int x509_get_key_usage( unsigned char **p,
866 const unsigned char *end,
867 unsigned char *key_usage)
868{
869 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000870 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000871
872 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000874
Paul Bakker94a67962012-08-23 13:03:52 +0000875 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000876 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000877 POLARSSL_ERR_ASN1_INVALID_LENGTH );
878
879 /* Get actual bitstring */
880 *key_usage = *bs.p;
881 return 0;
882}
883
Paul Bakkerd98030e2009-05-02 15:13:40 +0000884/*
Paul Bakker74111d32011-01-15 16:57:55 +0000885 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
886 *
887 * KeyPurposeId ::= OBJECT IDENTIFIER
888 */
889static int x509_get_ext_key_usage( unsigned char **p,
890 const unsigned char *end,
891 x509_sequence *ext_key_usage)
892{
893 int ret;
894
895 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000896 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000897
898 /* Sequence length must be >= 1 */
899 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000900 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000901 POLARSSL_ERR_ASN1_INVALID_LENGTH );
902
903 return 0;
904}
905
906/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000907 * SubjectAltName ::= GeneralNames
908 *
909 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
910 *
911 * GeneralName ::= CHOICE {
912 * otherName [0] OtherName,
913 * rfc822Name [1] IA5String,
914 * dNSName [2] IA5String,
915 * x400Address [3] ORAddress,
916 * directoryName [4] Name,
917 * ediPartyName [5] EDIPartyName,
918 * uniformResourceIdentifier [6] IA5String,
919 * iPAddress [7] OCTET STRING,
920 * registeredID [8] OBJECT IDENTIFIER }
921 *
922 * OtherName ::= SEQUENCE {
923 * type-id OBJECT IDENTIFIER,
924 * value [0] EXPLICIT ANY DEFINED BY type-id }
925 *
926 * EDIPartyName ::= SEQUENCE {
927 * nameAssigner [0] DirectoryString OPTIONAL,
928 * partyName [1] DirectoryString }
929 *
930 * NOTE: PolarSSL only parses and uses dNSName at this point.
931 */
932static int x509_get_subject_alt_name( unsigned char **p,
933 const unsigned char *end,
934 x509_sequence *subject_alt_name )
935{
936 int ret;
937 size_t len, tag_len;
938 asn1_buf *buf;
939 unsigned char tag;
940 asn1_sequence *cur = subject_alt_name;
941
942 /* Get main sequence tag */
943 if( ( ret = asn1_get_tag( p, end, &len,
944 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
946
947 if( *p + len != end )
948 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
949 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
950
951 while( *p < end )
952 {
953 if( ( end - *p ) < 1 )
954 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
955 POLARSSL_ERR_ASN1_OUT_OF_DATA );
956
957 tag = **p;
958 (*p)++;
959 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
960 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
961
962 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
963 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
964 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
965
966 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
967 {
968 *p += tag_len;
969 continue;
970 }
971
972 buf = &(cur->buf);
973 buf->tag = tag;
974 buf->p = *p;
975 buf->len = tag_len;
976 *p += buf->len;
977
978 /* Allocate and assign next pointer */
979 if (*p < end)
980 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200981 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000982 sizeof( asn1_sequence ) );
983
984 if( cur->next == NULL )
985 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
986 POLARSSL_ERR_ASN1_MALLOC_FAILED );
987
Paul Bakker535e97d2012-08-23 10:49:55 +0000988 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000989 cur = cur->next;
990 }
991 }
992
993 /* Set final sequence entry's next pointer to NULL */
994 cur->next = NULL;
995
996 if( *p != end )
997 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
998 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
999
1000 return( 0 );
1001}
1002
1003/*
Paul Bakker74111d32011-01-15 16:57:55 +00001004 * X.509 v3 extensions
1005 *
1006 * TODO: Perform all of the basic constraints tests required by the RFC
1007 * TODO: Set values for undetected extensions to a sane default?
1008 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001009 */
1010static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001011 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001012 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001013{
Paul Bakker23986e52011-04-24 08:57:21 +00001014 int ret;
1015 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001016 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001017
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001018 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001019 {
1020 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1021 return( 0 );
1022
1023 return( ret );
1024 }
1025
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 while( *p < end )
1027 {
Paul Bakker74111d32011-01-15 16:57:55 +00001028 /*
1029 * Extension ::= SEQUENCE {
1030 * extnID OBJECT IDENTIFIER,
1031 * critical BOOLEAN DEFAULT FALSE,
1032 * extnValue OCTET STRING }
1033 */
1034 x509_buf extn_oid = {0, 0, NULL};
1035 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001036 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 if( ( ret = asn1_get_tag( p, end, &len,
1039 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001040 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001041
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001042 end_ext_data = *p + len;
1043
Paul Bakker74111d32011-01-15 16:57:55 +00001044 /* Get extension ID */
1045 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Paul Bakker74111d32011-01-15 16:57:55 +00001047 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001048 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Paul Bakker74111d32011-01-15 16:57:55 +00001050 extn_oid.p = *p;
1051 *p += extn_oid.len;
1052
1053 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001054 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001055 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1056
1057 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001058 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001059 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001060 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
Paul Bakker74111d32011-01-15 16:57:55 +00001062 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001063 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001065 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001066
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001067 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001068
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001069 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001070 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Paul Bakker74111d32011-01-15 16:57:55 +00001073 /*
1074 * Detect supported extensions
1075 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001076 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1077
1078 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001079 {
1080 /* No parser found, skip extension */
1081 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Paul Bakker5c721f92011-07-27 16:51:09 +00001083#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001084 if( is_critical )
1085 {
1086 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001087 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001088 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1089 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001090#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001091 continue;
1092 }
1093
1094 crt->ext_types |= ext_type;
1095
1096 switch( ext_type )
1097 {
1098 case EXT_BASIC_CONSTRAINTS:
1099 /* Parse basic constraints */
1100 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1101 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1102 return ( ret );
1103 break;
1104
1105 case EXT_KEY_USAGE:
1106 /* Parse key usage */
1107 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1108 &crt->key_usage ) ) != 0 )
1109 return ( ret );
1110 break;
1111
1112 case EXT_EXTENDED_KEY_USAGE:
1113 /* Parse extended key usage */
1114 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1115 &crt->ext_key_usage ) ) != 0 )
1116 return ( ret );
1117 break;
1118
1119 case EXT_SUBJECT_ALT_NAME:
1120 /* Parse subject alt name */
1121 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1122 &crt->subject_alt_names ) ) != 0 )
1123 return ( ret );
1124 break;
1125
1126 case EXT_NS_CERT_TYPE:
1127 /* Parse netscape certificate type */
1128 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1129 &crt->ns_cert_type ) ) != 0 )
1130 return ( ret );
1131 break;
1132
1133 default:
1134 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001135 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 }
1137
1138 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001139 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001140 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001141
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 return( 0 );
1143}
1144
1145/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001146 * X.509 CRL Entries
1147 */
1148static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001149 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001150 x509_crl_entry *entry )
1151{
Paul Bakker23986e52011-04-24 08:57:21 +00001152 int ret;
1153 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154 x509_crl_entry *cur_entry = entry;
1155
1156 if( *p == end )
1157 return( 0 );
1158
Paul Bakker9be19372009-07-27 20:21:53 +00001159 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001160 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1161 {
1162 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1163 return( 0 );
1164
1165 return( ret );
1166 }
1167
Paul Bakker9be19372009-07-27 20:21:53 +00001168 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001169
1170 while( *p < end )
1171 {
Paul Bakker23986e52011-04-24 08:57:21 +00001172 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001173 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001174
1175 if( ( ret = asn1_get_tag( p, end, &len2,
1176 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1177 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001178 return( ret );
1179 }
1180
Paul Bakker9be19372009-07-27 20:21:53 +00001181 cur_entry->raw.tag = **p;
1182 cur_entry->raw.p = *p;
1183 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001184 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001185
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001186 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001187 return( ret );
1188
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001189 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001190 return( ret );
1191
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001192 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001193 return( ret );
1194
Paul Bakker74111d32011-01-15 16:57:55 +00001195 if ( *p < end )
1196 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001197 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001198
1199 if( cur_entry->next == NULL )
1200 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1201
Paul Bakkerd98030e2009-05-02 15:13:40 +00001202 cur_entry = cur_entry->next;
1203 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1204 }
1205 }
1206
1207 return( 0 );
1208}
1209
Paul Bakkerc70b9822013-04-07 22:00:46 +02001210static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1211 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001212{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001213 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001214
Paul Bakkerc70b9822013-04-07 22:00:46 +02001215 if( ret != 0 )
1216 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001217
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001219}
1220
Paul Bakkerd98030e2009-05-02 15:13:40 +00001221/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001222 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001224static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1225 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226{
Paul Bakker23986e52011-04-24 08:57:21 +00001227 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001228 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001229 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001230
Paul Bakker320a4b52009-03-28 18:52:39 +00001231 /*
1232 * Check for valid input
1233 */
1234 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001235 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001236
Paul Bakker6e339b52013-07-03 13:37:05 +02001237 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001238
1239 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001240 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001241
1242 memcpy( p, buf, buflen );
1243
1244 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 crt->raw.p = p;
1247 crt->raw.len = len;
1248 end = p + len;
1249
1250 /*
1251 * Certificate ::= SEQUENCE {
1252 * tbsCertificate TBSCertificate,
1253 * signatureAlgorithm AlgorithmIdentifier,
1254 * signatureValue BIT STRING }
1255 */
1256 if( ( ret = asn1_get_tag( &p, end, &len,
1257 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1258 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001259 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001260 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
Paul Bakkerb00ca422012-09-25 12:10:00 +00001263 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001266 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001267 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001269 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 /*
1272 * TBSCertificate ::= SEQUENCE {
1273 */
1274 crt->tbs.p = p;
1275
1276 if( ( ret = asn1_get_tag( &p, end, &len,
1277 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1278 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001279 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001280 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 }
1282
1283 end = p + len;
1284 crt->tbs.len = end - crt->tbs.p;
1285
1286 /*
1287 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1288 *
1289 * CertificateSerialNumber ::= INTEGER
1290 *
1291 * signature AlgorithmIdentifier
1292 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001293 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1294 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1295 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->version++;
1302
1303 if( crt->version > 3 )
1304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001306 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 }
1308
Paul Bakkerc70b9822013-04-07 22:00:46 +02001309 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1310 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001313 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
1316 /*
1317 * issuer Name
1318 */
1319 crt->issuer_raw.p = p;
1320
1321 if( ( ret = asn1_get_tag( &p, end, &len,
1322 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
1328 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 return( ret );
1332 }
1333
1334 crt->issuer_raw.len = p - crt->issuer_raw.p;
1335
1336 /*
1337 * Validity ::= SEQUENCE {
1338 * notBefore Time,
1339 * notAfter Time }
1340 *
1341 */
1342 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1343 &crt->valid_to ) ) != 0 )
1344 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001345 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 return( ret );
1347 }
1348
1349 /*
1350 * subject Name
1351 */
1352 crt->subject_raw.p = p;
1353
1354 if( ( ret = asn1_get_tag( &p, end, &len,
1355 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001358 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
1360
Paul Bakkercefb3962012-06-27 11:51:09 +00001361 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 return( ret );
1365 }
1366
1367 crt->subject_raw.len = p - crt->subject_raw.p;
1368
1369 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001370 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001372 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001374 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 return( ret );
1376 }
1377
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 /*
1379 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1380 * -- If present, version shall be v2 or v3
1381 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1382 * -- If present, version shall be v2 or v3
1383 * extensions [3] EXPLICIT Extensions OPTIONAL
1384 * -- If present, version shall be v3
1385 */
1386 if( crt->version == 2 || crt->version == 3 )
1387 {
1388 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1389 if( ret != 0 )
1390 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001391 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 return( ret );
1393 }
1394 }
1395
1396 if( crt->version == 2 || crt->version == 3 )
1397 {
1398 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1399 if( ret != 0 )
1400 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001401 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 return( ret );
1403 }
1404 }
1405
1406 if( crt->version == 3 )
1407 {
Paul Bakker74111d32011-01-15 16:57:55 +00001408 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 if( ret != 0 )
1410 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001411 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 return( ret );
1413 }
1414 }
1415
1416 if( p != end )
1417 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001418 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001419 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001420 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Paul Bakkerb00ca422012-09-25 12:10:00 +00001423 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
1425 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001426 * }
1427 * -- end of TBSCertificate
1428 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 * signatureAlgorithm AlgorithmIdentifier,
1430 * signatureValue BIT STRING
1431 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001432 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 return( ret );
1436 }
1437
Paul Bakker535e97d2012-08-23 10:49:55 +00001438 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1439 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001441 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001442 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444
1445 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1446 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001447 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 return( ret );
1449 }
1450
1451 if( p != end )
1452 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001453 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001454 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001455 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
1457
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001458 return( 0 );
1459}
1460
1461/*
Paul Bakker42c65812013-06-24 19:21:59 +02001462 * Parse one X.509 certificate in DER format from a buffer and add them to a
1463 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001464 */
Paul Bakker42c65812013-06-24 19:21:59 +02001465int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466{
Paul Bakker42c65812013-06-24 19:21:59 +02001467 int ret;
1468 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001469
1470 /*
1471 * Check for valid input
1472 */
1473 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001474 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001475
1476 while( crt->version != 0 && crt->next != NULL )
1477 {
1478 prev = crt;
1479 crt = crt->next;
1480 }
1481
1482 /*
1483 * Add new certificate on the end of the chain if needed.
1484 */
1485 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001486 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001487 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001488
Paul Bakker7d06ad22009-05-02 15:53:56 +00001489 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001490 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001491
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001492 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001493 crt = crt->next;
1494 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001496
Paul Bakker42c65812013-06-24 19:21:59 +02001497 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1498 {
1499 if( prev )
1500 prev->next = NULL;
1501
1502 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001503 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001504
1505 return( ret );
1506 }
1507
1508 return( 0 );
1509}
1510
1511/*
1512 * Parse one or more PEM certificates from a buffer and add them to the chained list
1513 */
1514int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1515{
1516 int ret, success = 0, first_error = 0, total_failed = 0;
1517 int buf_format = X509_FORMAT_DER;
1518
1519 /*
1520 * Check for valid input
1521 */
1522 if( chain == NULL || buf == NULL )
1523 return( POLARSSL_ERR_X509_INVALID_INPUT );
1524
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001525 /*
1526 * Determine buffer content. Buffer contains either one DER certificate or
1527 * one or more PEM certificates.
1528 */
1529#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001530 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001531 buf_format = X509_FORMAT_PEM;
1532#endif
1533
1534 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001535 return x509parse_crt_der( chain, buf, buflen );
1536
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001537#if defined(POLARSSL_PEM_C)
1538 if( buf_format == X509_FORMAT_PEM )
1539 {
1540 pem_context pem;
1541
1542 while( buflen > 0 )
1543 {
1544 size_t use_len;
1545 pem_init( &pem );
1546
1547 ret = pem_read_buffer( &pem,
1548 "-----BEGIN CERTIFICATE-----",
1549 "-----END CERTIFICATE-----",
1550 buf, NULL, 0, &use_len );
1551
1552 if( ret == 0 )
1553 {
1554 /*
1555 * Was PEM encoded
1556 */
1557 buflen -= use_len;
1558 buf += use_len;
1559 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001560 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1561 {
1562 return( ret );
1563 }
Paul Bakker00b28602013-06-24 13:02:41 +02001564 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001565 {
1566 pem_free( &pem );
1567
Paul Bakker5ed3b342013-06-24 19:05:46 +02001568 /*
1569 * PEM header and footer were found
1570 */
1571 buflen -= use_len;
1572 buf += use_len;
1573
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001574 if( first_error == 0 )
1575 first_error = ret;
1576
1577 continue;
1578 }
1579 else
1580 break;
1581
Paul Bakker42c65812013-06-24 19:21:59 +02001582 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001583
1584 pem_free( &pem );
1585
1586 if( ret != 0 )
1587 {
1588 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001589 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001590 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001591 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001592 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001593
1594 if( first_error == 0 )
1595 first_error = ret;
1596
Paul Bakker42c65812013-06-24 19:21:59 +02001597 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001598 continue;
1599 }
1600
1601 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001602 }
1603 }
1604#endif
1605
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001607 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001608 else if( first_error )
1609 return( first_error );
1610 else
1611 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612}
1613
1614/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615 * Parse one or more CRLs and add them to the chained list
1616 */
Paul Bakker23986e52011-04-24 08:57:21 +00001617int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001618{
Paul Bakker23986e52011-04-24 08:57:21 +00001619 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001620 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001621 unsigned char *p, *end;
1622 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001623#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001624 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001625 pem_context pem;
1626#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001627
1628 crl = chain;
1629
1630 /*
1631 * Check for valid input
1632 */
1633 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001634 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635
1636 while( crl->version != 0 && crl->next != NULL )
1637 crl = crl->next;
1638
1639 /*
1640 * Add new CRL on the end of the chain if needed.
1641 */
1642 if ( crl->version != 0 && crl->next == NULL)
1643 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001644 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645
Paul Bakker7d06ad22009-05-02 15:53:56 +00001646 if( crl->next == NULL )
1647 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001649 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001650 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001651
Paul Bakker7d06ad22009-05-02 15:53:56 +00001652 crl = crl->next;
1653 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654 }
1655
Paul Bakker96743fc2011-02-12 14:30:57 +00001656#if defined(POLARSSL_PEM_C)
1657 pem_init( &pem );
1658 ret = pem_read_buffer( &pem,
1659 "-----BEGIN X509 CRL-----",
1660 "-----END X509 CRL-----",
1661 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662
Paul Bakker96743fc2011-02-12 14:30:57 +00001663 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001665 /*
1666 * Was PEM encoded
1667 */
1668 buflen -= use_len;
1669 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
1671 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001672 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001673 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001674 p = pem.buf;
1675 pem.buf = NULL;
1676 len = pem.buflen;
1677 pem_free( &pem );
1678 }
Paul Bakker00b28602013-06-24 13:02:41 +02001679 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001680 {
1681 pem_free( &pem );
1682 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683 }
1684 else
1685 {
1686 /*
1687 * nope, copy the raw DER data
1688 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001689 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690
1691 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001692 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693
1694 memcpy( p, buf, buflen );
1695
1696 buflen = 0;
1697 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001698#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001699 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001700
1701 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001702 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001703
1704 memcpy( p, buf, buflen );
1705
1706 buflen = 0;
1707#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708
1709 crl->raw.p = p;
1710 crl->raw.len = len;
1711 end = p + len;
1712
1713 /*
1714 * CertificateList ::= SEQUENCE {
1715 * tbsCertList TBSCertList,
1716 * signatureAlgorithm AlgorithmIdentifier,
1717 * signatureValue BIT STRING }
1718 */
1719 if( ( ret = asn1_get_tag( &p, end, &len,
1720 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1721 {
1722 x509_crl_free( crl );
1723 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1724 }
1725
Paul Bakker23986e52011-04-24 08:57:21 +00001726 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 {
1728 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001729 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001730 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1731 }
1732
1733 /*
1734 * TBSCertList ::= SEQUENCE {
1735 */
1736 crl->tbs.p = p;
1737
1738 if( ( ret = asn1_get_tag( &p, end, &len,
1739 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1740 {
1741 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001742 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001743 }
1744
1745 end = p + len;
1746 crl->tbs.len = end - crl->tbs.p;
1747
1748 /*
1749 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1750 * -- if present, MUST be v2
1751 *
1752 * signature AlgorithmIdentifier
1753 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001754 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001755 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756 {
1757 x509_crl_free( crl );
1758 return( ret );
1759 }
1760
1761 crl->version++;
1762
1763 if( crl->version > 2 )
1764 {
1765 x509_crl_free( crl );
1766 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1767 }
1768
Paul Bakkerc70b9822013-04-07 22:00:46 +02001769 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1770 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 {
1772 x509_crl_free( crl );
1773 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1774 }
1775
1776 /*
1777 * issuer Name
1778 */
1779 crl->issuer_raw.p = p;
1780
1781 if( ( ret = asn1_get_tag( &p, end, &len,
1782 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1783 {
1784 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001785 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001786 }
1787
1788 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1789 {
1790 x509_crl_free( crl );
1791 return( ret );
1792 }
1793
1794 crl->issuer_raw.len = p - crl->issuer_raw.p;
1795
1796 /*
1797 * thisUpdate Time
1798 * nextUpdate Time OPTIONAL
1799 */
Paul Bakker91200182010-02-18 21:26:15 +00001800 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001801 {
1802 x509_crl_free( crl );
1803 return( ret );
1804 }
1805
Paul Bakker91200182010-02-18 21:26:15 +00001806 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 {
Paul Bakker9d781402011-05-09 16:17:09 +00001808 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001809 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001810 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001811 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001812 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813 x509_crl_free( crl );
1814 return( ret );
1815 }
1816 }
1817
1818 /*
1819 * revokedCertificates SEQUENCE OF SEQUENCE {
1820 * userCertificate CertificateSerialNumber,
1821 * revocationDate Time,
1822 * crlEntryExtensions Extensions OPTIONAL
1823 * -- if present, MUST be v2
1824 * } OPTIONAL
1825 */
1826 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1827 {
1828 x509_crl_free( crl );
1829 return( ret );
1830 }
1831
1832 /*
1833 * crlExtensions EXPLICIT Extensions OPTIONAL
1834 * -- if present, MUST be v2
1835 */
1836 if( crl->version == 2 )
1837 {
1838 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1839
1840 if( ret != 0 )
1841 {
1842 x509_crl_free( crl );
1843 return( ret );
1844 }
1845 }
1846
1847 if( p != end )
1848 {
1849 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001850 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001851 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1852 }
1853
1854 end = crl->raw.p + crl->raw.len;
1855
1856 /*
1857 * signatureAlgorithm AlgorithmIdentifier,
1858 * signatureValue BIT STRING
1859 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001860 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861 {
1862 x509_crl_free( crl );
1863 return( ret );
1864 }
1865
Paul Bakker535e97d2012-08-23 10:49:55 +00001866 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1867 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001868 {
1869 x509_crl_free( crl );
1870 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1871 }
1872
1873 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1874 {
1875 x509_crl_free( crl );
1876 return( ret );
1877 }
1878
1879 if( p != end )
1880 {
1881 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001882 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001883 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1884 }
1885
1886 if( buflen > 0 )
1887 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001888 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001889
Paul Bakker7d06ad22009-05-02 15:53:56 +00001890 if( crl->next == NULL )
1891 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001893 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001894 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001895
Paul Bakker7d06ad22009-05-02 15:53:56 +00001896 crl = crl->next;
1897 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898
1899 return( x509parse_crl( crl, buf, buflen ) );
1900 }
1901
1902 return( 0 );
1903}
1904
Paul Bakker335db3f2011-04-25 15:28:35 +00001905#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001907 * Load all data from a file into a given buffer.
1908 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001909static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001910{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001911 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001912
Paul Bakkerd98030e2009-05-02 15:13:40 +00001913 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001914 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915
Paul Bakkerd98030e2009-05-02 15:13:40 +00001916 fseek( f, 0, SEEK_END );
1917 *n = (size_t) ftell( f );
1918 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001919
Paul Bakker6e339b52013-07-03 13:37:05 +02001920 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001921 {
1922 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001923 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001924 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001925
Paul Bakkerd98030e2009-05-02 15:13:40 +00001926 if( fread( *buf, 1, *n, f ) != *n )
1927 {
1928 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001929 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001930 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001931 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001932
Paul Bakkerd98030e2009-05-02 15:13:40 +00001933 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001934
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001936
Paul Bakkerd98030e2009-05-02 15:13:40 +00001937 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001938}
1939
1940/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 * Load one or more certificates and add them to the chained list
1942 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001943int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001944{
1945 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001946 size_t n;
1947 unsigned char *buf;
1948
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001949 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001950 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
Paul Bakker69e095c2011-12-10 21:55:01 +00001952 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001953
1954 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001955 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
1957 return( ret );
1958}
1959
Paul Bakker8d914582012-06-04 12:46:42 +00001960int x509parse_crtpath( x509_cert *chain, const char *path )
1961{
1962 int ret = 0;
1963#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001964 int w_ret;
1965 WCHAR szDir[MAX_PATH];
1966 char filename[MAX_PATH];
1967 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001968 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001969
Paul Bakker97872ac2012-11-02 12:53:26 +00001970 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001971 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001972
1973 if( len > MAX_PATH - 3 )
1974 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001975
Paul Bakker3338b792012-10-01 21:13:10 +00001976 memset( szDir, 0, sizeof(szDir) );
1977 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001978 memcpy( filename, path, len );
1979 filename[len++] = '\\';
1980 p = filename + len;
1981 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001982
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001983 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001984
Paul Bakker97872ac2012-11-02 12:53:26 +00001985 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001986 if (hFind == INVALID_HANDLE_VALUE)
1987 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1988
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001989 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001990 do
1991 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001992 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001993
Paul Bakkere4791f32012-06-04 21:29:15 +00001994 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001995 continue;
1996
Paul Bakker3338b792012-10-01 21:13:10 +00001997 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1998 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001999 p, len - 1,
2000 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002001
Paul Bakker3338b792012-10-01 21:13:10 +00002002 w_ret = x509parse_crtfile( chain, filename );
2003 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002004 ret++;
2005 else
2006 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002007 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002008 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002009
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002010 if (GetLastError() != ERROR_NO_MORE_FILES)
2011 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002012
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002013cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002014 FindClose( hFind );
2015#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002016 int t_ret, i;
2017 struct stat sb;
2018 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002019 char entry_name[255];
2020 DIR *dir = opendir( path );
2021
2022 if( dir == NULL)
2023 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2024
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002025 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002026 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002027 if( result == NULL )
2028 break;
2029
2030 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2031
2032 i = stat( entry_name, &sb );
2033
2034 if( i == -1 )
2035 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2036
2037 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002038 continue;
2039
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002040 // Ignore parse errors
2041 //
Paul Bakker8d914582012-06-04 12:46:42 +00002042 t_ret = x509parse_crtfile( chain, entry_name );
2043 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002044 ret++;
2045 else
2046 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002047 }
2048 closedir( dir );
2049#endif
2050
2051 return( ret );
2052}
2053
Paul Bakkerd98030e2009-05-02 15:13:40 +00002054/*
2055 * Load one or more CRLs and add them to the chained list
2056 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002057int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002058{
2059 int ret;
2060 size_t n;
2061 unsigned char *buf;
2062
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002063 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002064 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002065
Paul Bakker27fdf462011-06-09 13:55:13 +00002066 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002067
2068 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002069 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002070
2071 return( ret );
2072}
2073
Paul Bakker5121ce52009-01-03 21:22:43 +00002074/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002075 * Load and parse a private RSA key
2076 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002077int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002078{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002079 pk_context pk;
Paul Bakker335db3f2011-04-25 15:28:35 +00002080
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002081 pk_init( &pk );
2082 pk_wrap_rsa( &pk, rsa );
Paul Bakker335db3f2011-04-25 15:28:35 +00002083
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002084 return( x509parse_keyfile( &pk, path, pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002085}
2086
2087/*
2088 * Load and parse a public RSA key
2089 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002090int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002091{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002092 pk_context pk;
Paul Bakker335db3f2011-04-25 15:28:35 +00002093
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002094 pk_init( &pk );
2095 pk_wrap_rsa( &pk, rsa );
Paul Bakker335db3f2011-04-25 15:28:35 +00002096
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002097 return( x509parse_public_keyfile( &pk, path ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002098}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002099
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002100/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002101 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002102 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002103int x509parse_keyfile( pk_context *ctx,
2104 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002105{
2106 int ret;
2107 size_t n;
2108 unsigned char *buf;
2109
2110 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2111 return( ret );
2112
2113 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002114 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002115 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002116 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002117 (const unsigned char *) pwd, strlen( pwd ) );
2118
2119 memset( buf, 0, n + 1 );
2120 free( buf );
2121
2122 return( ret );
2123}
2124
2125/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002126 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002127 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002128int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002129{
2130 int ret;
2131 size_t n;
2132 unsigned char *buf;
2133
2134 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2135 return( ret );
2136
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002137 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002138
2139 memset( buf, 0, n + 1 );
2140 free( buf );
2141
2142 return( ret );
2143}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002144
Paul Bakker335db3f2011-04-25 15:28:35 +00002145#endif /* POLARSSL_FS_IO */
2146
2147/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002148 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002150static int x509parse_key_pkcs1_der( rsa_context *rsa,
2151 const unsigned char *key,
2152 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002153{
Paul Bakker23986e52011-04-24 08:57:21 +00002154 int ret;
2155 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002157
Paul Bakker96743fc2011-02-12 14:30:57 +00002158 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002159 end = p + keylen;
2160
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002162 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002163 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 * RSAPrivateKey ::= SEQUENCE {
2165 * version Version,
2166 * modulus INTEGER, -- n
2167 * publicExponent INTEGER, -- e
2168 * privateExponent INTEGER, -- d
2169 * prime1 INTEGER, -- p
2170 * prime2 INTEGER, -- q
2171 * exponent1 INTEGER, -- d mod (p-1)
2172 * exponent2 INTEGER, -- d mod (q-1)
2173 * coefficient INTEGER, -- (inverse of q) mod p
2174 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2175 * }
2176 */
2177 if( ( ret = asn1_get_tag( &p, end, &len,
2178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2179 {
Paul Bakker9d781402011-05-09 16:17:09 +00002180 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182
2183 end = p + len;
2184
2185 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2186 {
Paul Bakker9d781402011-05-09 16:17:09 +00002187 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 }
2189
2190 if( rsa->ver != 0 )
2191 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002192 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 }
2194
2195 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2196 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2197 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2198 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2199 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2200 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2201 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2202 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2203 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002205 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
2208 rsa->len = mpi_size( &rsa->N );
2209
2210 if( p != end )
2211 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002213 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002214 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
2216
2217 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2218 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 rsa_free( rsa );
2220 return( ret );
2221 }
2222
Paul Bakkere2f50402013-06-24 19:00:59 +02002223 return( 0 );
2224}
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002225
Paul Bakkere2f50402013-06-24 19:00:59 +02002226/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002227 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002228 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002229static int x509parse_key_sec1_der( ecp_keypair *eck,
2230 const unsigned char *key,
2231 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002232{
2233 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002234 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002235 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002236 x509_buf params;
2237 unsigned char *p = (unsigned char *) key;
2238 unsigned char *end = p + keylen;
2239 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002240
2241 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002242 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002243 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002244 * ECPrivateKey ::= SEQUENCE {
2245 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2246 * privateKey OCTET STRING,
2247 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2248 * publicKey [1] BIT STRING OPTIONAL
2249 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002250 */
2251 if( ( ret = asn1_get_tag( &p, end, &len,
2252 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2253 {
2254 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2255 }
2256
2257 end = p + len;
2258
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002259 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002260 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2261
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002262 if( version != 1 )
2263 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002264
Paul Bakkere2f50402013-06-24 19:00:59 +02002265 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2267
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002268 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002269 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002270 ecp_keypair_free( eck );
2271 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2272 }
2273
2274 p += len;
2275
2276 /*
2277 * Is 'parameters' present?
2278 */
2279 if( ( ret = asn1_get_tag( &p, end, &len,
2280 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2281 {
2282 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2283 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2284 {
2285 ecp_keypair_free( eck );
2286 return( ret );
2287 }
2288 }
2289 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2290 {
2291 ecp_keypair_free( eck );
2292 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2293 }
2294
2295 /*
2296 * Is 'publickey' present?
2297 */
2298 if( ( ret = asn1_get_tag( &p, end, &len,
2299 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2300 {
2301 end2 = p + len;
2302
2303 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2304 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2305
2306 if( p + len != end2 )
2307 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2308 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2309
2310 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2311 return( ret );
2312 }
2313 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2314 {
2315 ecp_keypair_free( eck );
2316 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2317 }
2318
2319 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2320 {
2321 ecp_keypair_free( eck );
2322 return( ret );
2323 }
2324
2325 return 0;
2326}
2327
2328/*
2329 * Parse an unencrypted PKCS#8 encoded private key
2330 */
2331static int x509parse_key_pkcs8_unencrypted_der(
2332 pk_context *pk,
2333 const unsigned char* key,
2334 size_t keylen )
2335{
2336 int ret, version;
2337 size_t len;
2338 x509_buf params;
2339 unsigned char *p = (unsigned char *) key;
2340 unsigned char *end = p + keylen;
2341 pk_type_t pk_alg = POLARSSL_PK_NONE;
2342
2343 /*
2344 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2345 *
2346 * PrivateKeyInfo ::= SEQUENCE {
2347 * version Version,
2348 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2349 * privateKey PrivateKey,
2350 * attributes [0] IMPLICIT Attributes OPTIONAL }
2351 *
2352 * Version ::= INTEGER
2353 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2354 * PrivateKey ::= OCTET STRING
2355 *
2356 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2357 */
2358
2359 if( ( ret = asn1_get_tag( &p, end, &len,
2360 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2361 {
2362 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002363 }
2364
2365 end = p + len;
2366
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002367 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2368 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2369
2370 if( version != 0 )
2371 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2372
2373 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2374 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2375
2376 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2377 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2378
2379 if( len < 1 )
2380 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2381 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2382
2383 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002384 return( ret );
2385
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002386 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2387 {
2388 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2389 pk_ec( *pk )->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2390
2391 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2392 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2393 {
2394 pk_free( pk );
2395 return( ret );
2396 }
2397 } else
2398 if( pk_alg == POLARSSL_PK_RSA )
2399 {
2400 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2401 {
2402 pk_free( pk );
2403 return( ret );
2404 }
2405 } else
2406 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2407
2408 return 0;
2409}
2410
2411/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002412 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002413 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002414static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2415 size_t *used_len,
2416 const unsigned char *key, size_t keylen,
2417 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002418{
2419 int ret;
2420 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002421 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002422 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002423#if defined(POLARSSL_PKCS12_C)
2424 cipher_type_t cipher_alg;
2425 md_type_t md_alg;
2426#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002427
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002428 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002429
2430 p = (unsigned char *) key;
2431 end = p + keylen;
2432
Paul Bakker28144de2013-06-24 19:28:55 +02002433 if( pwdlen == 0 )
2434 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2435
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002436 /*
2437 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2438 *
2439 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2440 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2441 * encryptedData EncryptedData
2442 * }
2443 *
2444 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2445 *
2446 * EncryptedData ::= OCTET STRING
2447 *
2448 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2449 */
2450 if( ( ret = asn1_get_tag( &p, end, &len,
2451 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2452 {
2453 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2454 }
2455
2456 end = p + len;
2457
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002458 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002459 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002460
2461 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2462 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2463
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002464 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 return( POLARSSL_ERR_X509_INVALID_INPUT );
2466
2467 /*
2468 * Decrypt EncryptedData with appropriate PDE
2469 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002470#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002471 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002472 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002473 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002474 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002475 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002476 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002477 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2478 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2479
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002480 return( ret );
2481 }
2482 }
2483 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2484 {
2485 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2486 PKCS12_PBE_DECRYPT,
2487 pwd, pwdlen,
2488 p, len, buf ) ) != 0 )
2489 {
2490 return( ret );
2491 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002492
2493 // Best guess for password mismatch when using RC4. If first tag is
2494 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2495 //
2496 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2497 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002498 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002499 else
2500#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002501#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002502 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002503 {
2504 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2505 p, len, buf ) ) != 0 )
2506 {
2507 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2508 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2509
2510 return( ret );
2511 }
2512 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002513 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002514#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002515 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2516
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002517 *used_len = len;
2518 return( 0 );
2519}
2520
2521/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002522 * Parse a private RSA key
2523 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002524int x509parse_key_rsa( rsa_context *rsa,
2525 const unsigned char *key, size_t keylen,
2526 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002527{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002528 pk_context pk;
Paul Bakkere2f50402013-06-24 19:00:59 +02002529
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002530 pk_init( &pk );
2531 pk_wrap_rsa( &pk, rsa );
Paul Bakkere2f50402013-06-24 19:00:59 +02002532
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002533 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002534}
2535
2536/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002537 * Parse a public RSA key
2538 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002539int x509parse_public_key_rsa( rsa_context *rsa,
2540 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002541{
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002542 pk_context pk;
Paul Bakker53019ae2011-03-25 13:58:48 +00002543
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002544 pk_init( &pk );
2545 pk_wrap_rsa( &pk, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002546
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002547 return( x509parse_public_key( &pk, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002548}
2549
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002550#if defined(POLARSSL_ECP_C)
2551/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002552 * Parse an encrypted PKCS#8 encoded private key
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002553 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002554static int x509parse_key_pkcs8_encrypted_der(
2555 pk_context *pk,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002556 const unsigned char *key, size_t keylen,
2557 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002558{
2559 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002560 unsigned char buf[2048];
2561 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002562
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002563 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2564 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002565 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002566 return( ret );
2567 }
2568
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002569 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002570}
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002571#endif /* defined(POLARSSL_ECP_C) */
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002572
2573/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002574 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002575 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002576int x509parse_key( pk_context *pk,
2577 const unsigned char *key, size_t keylen,
2578 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002579{
2580 int ret;
2581
2582#if defined(POLARSSL_PEM_C)
2583 size_t len;
2584 pem_context pem;
2585
2586 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002587
2588 ret = pem_read_buffer( &pem,
2589 "-----BEGIN RSA PRIVATE KEY-----",
2590 "-----END RSA PRIVATE KEY-----",
2591 key, pwd, pwdlen, &len );
2592 if( ret == 0 )
2593 {
2594 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2595 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2596 pem.buf, pem.buflen ) ) != 0 )
2597 {
2598 pk_free( pk );
2599 }
2600
2601 pem_free( &pem );
2602 return( ret );
2603 }
2604 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2605 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2606 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2607 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2608 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2609 return( ret );
2610
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002611 ret = pem_read_buffer( &pem,
2612 "-----BEGIN EC PRIVATE KEY-----",
2613 "-----END EC PRIVATE KEY-----",
2614 key, pwd, pwdlen, &len );
2615 if( ret == 0 )
2616 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002617 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2618 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2619 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002620 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002621 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002622 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002623
2624 pem_free( &pem );
2625 return( ret );
2626 }
2627 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2628 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2629 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2630 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2631 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2632 return( ret );
2633
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002634 ret = pem_read_buffer( &pem,
2635 "-----BEGIN PRIVATE KEY-----",
2636 "-----END PRIVATE KEY-----",
2637 key, NULL, 0, &len );
2638 if( ret == 0 )
2639 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002640 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002641 pem.buf, pem.buflen ) ) != 0 )
2642 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002643 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002644 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002645
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002646 pem_free( &pem );
2647 return( ret );
2648 }
2649 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2650 return( ret );
2651
2652 ret = pem_read_buffer( &pem,
2653 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2654 "-----END ENCRYPTED PRIVATE KEY-----",
2655 key, NULL, 0, &len );
2656 if( ret == 0 )
2657 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002658 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002659 pem.buf, pem.buflen,
2660 pwd, pwdlen ) ) != 0 )
2661 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002662 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002663 }
2664
2665 pem_free( &pem );
2666 return( ret );
2667 }
2668 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2669 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002670#else
2671 ((void) pwd);
2672 ((void) pwdlen);
2673#endif /* POLARSSL_PEM_C */
2674
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002675 /*
2676 * At this point we only know it's not a PEM formatted key. Could be any
2677 * of the known DER encoded private key formats
2678 *
2679 * We try the different DER format parsers to see if one passes without
2680 * error
2681 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002682 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2683 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002684 {
2685 return( 0 );
2686 }
2687
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002688 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002689
2690 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2691 {
2692 return( ret );
2693 }
2694
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002695 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002696 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002697
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002698 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002699
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002700 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2701 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2702 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002703 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002704 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002705
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002706 pk_free( pk );
2707
2708 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2709 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2710 {
2711 return( 0 );
2712 }
2713
2714 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002715
2716 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2717}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002718
2719/*
2720 * Parse a public key
2721 */
2722int x509parse_public_key( pk_context *ctx,
2723 const unsigned char *key, size_t keylen )
2724{
2725 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002726 unsigned char *p;
2727#if defined(POLARSSL_PEM_C)
2728 size_t len;
2729 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002730
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002731 pem_init( &pem );
2732 ret = pem_read_buffer( &pem,
2733 "-----BEGIN PUBLIC KEY-----",
2734 "-----END PUBLIC KEY-----",
2735 key, NULL, 0, &len );
2736
2737 if( ret == 0 )
2738 {
2739 /*
2740 * Was PEM encoded
2741 */
2742 key = pem.buf;
2743 keylen = pem.buflen;
2744 }
2745 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2746 {
2747 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002748 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002749 }
2750#endif
2751 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002752
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002753 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002754
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002755#if defined(POLARSSL_PEM_C)
2756 pem_free( &pem );
2757#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002758
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002759 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002760}
2761
Paul Bakkereaa89f82011-04-04 21:36:15 +00002762#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002763/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002764 * Parse DHM parameters
2765 */
Paul Bakker23986e52011-04-24 08:57:21 +00002766int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002767{
Paul Bakker23986e52011-04-24 08:57:21 +00002768 int ret;
2769 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002770 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002771#if defined(POLARSSL_PEM_C)
2772 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002773
Paul Bakker96743fc2011-02-12 14:30:57 +00002774 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002775
Paul Bakker96743fc2011-02-12 14:30:57 +00002776 ret = pem_read_buffer( &pem,
2777 "-----BEGIN DH PARAMETERS-----",
2778 "-----END DH PARAMETERS-----",
2779 dhmin, NULL, 0, &dhminlen );
2780
2781 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002782 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002783 /*
2784 * Was PEM encoded
2785 */
2786 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002787 }
Paul Bakker00b28602013-06-24 13:02:41 +02002788 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002789 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002790 pem_free( &pem );
2791 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002792 }
2793
Paul Bakker96743fc2011-02-12 14:30:57 +00002794 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2795#else
2796 p = (unsigned char *) dhmin;
2797#endif
2798 end = p + dhminlen;
2799
Paul Bakker1b57b062011-01-06 15:48:19 +00002800 memset( dhm, 0, sizeof( dhm_context ) );
2801
Paul Bakker1b57b062011-01-06 15:48:19 +00002802 /*
2803 * DHParams ::= SEQUENCE {
2804 * prime INTEGER, -- P
2805 * generator INTEGER, -- g
2806 * }
2807 */
2808 if( ( ret = asn1_get_tag( &p, end, &len,
2809 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2810 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002811#if defined(POLARSSL_PEM_C)
2812 pem_free( &pem );
2813#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002814 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002815 }
2816
2817 end = p + len;
2818
2819 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2820 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2821 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002822#if defined(POLARSSL_PEM_C)
2823 pem_free( &pem );
2824#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002825 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002826 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002827 }
2828
2829 if( p != end )
2830 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002831#if defined(POLARSSL_PEM_C)
2832 pem_free( &pem );
2833#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002834 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002835 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002836 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2837 }
2838
Paul Bakker96743fc2011-02-12 14:30:57 +00002839#if defined(POLARSSL_PEM_C)
2840 pem_free( &pem );
2841#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002842
2843 return( 0 );
2844}
2845
Paul Bakker335db3f2011-04-25 15:28:35 +00002846#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002847/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002848 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002849 */
2850int x509parse_dhmfile( dhm_context *dhm, const char *path )
2851{
2852 int ret;
2853 size_t n;
2854 unsigned char *buf;
2855
Paul Bakker69e095c2011-12-10 21:55:01 +00002856 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2857 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002858
Paul Bakker27fdf462011-06-09 13:55:13 +00002859 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002860
2861 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002862 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002863
2864 return( ret );
2865}
Paul Bakker335db3f2011-04-25 15:28:35 +00002866#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002867#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002868
Paul Bakker5121ce52009-01-03 21:22:43 +00002869#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002870#include <stdarg.h>
2871
2872#if !defined vsnprintf
2873#define vsnprintf _vsnprintf
2874#endif // vsnprintf
2875
2876/*
2877 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2878 * Result value is not size of buffer needed, but -1 if no fit is possible.
2879 *
2880 * This fuction tries to 'fix' this by at least suggesting enlarging the
2881 * size by 20.
2882 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002883static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002884{
2885 va_list ap;
2886 int res = -1;
2887
2888 va_start( ap, format );
2889
2890 res = vsnprintf( str, size, format, ap );
2891
2892 va_end( ap );
2893
2894 // No quick fix possible
2895 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002896 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002897
2898 return res;
2899}
2900
2901#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002902#endif
2903
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2905
2906#define SAFE_SNPRINTF() \
2907{ \
2908 if( ret == -1 ) \
2909 return( -1 ); \
2910 \
Paul Bakker23986e52011-04-24 08:57:21 +00002911 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912 p[n - 1] = '\0'; \
2913 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2914 } \
2915 \
Paul Bakker23986e52011-04-24 08:57:21 +00002916 n -= (unsigned int) ret; \
2917 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002918}
2919
Paul Bakker5121ce52009-01-03 21:22:43 +00002920/*
2921 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002924int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002925{
Paul Bakker23986e52011-04-24 08:57:21 +00002926 int ret;
2927 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002929 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002930 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 char s[128], *p;
2932
2933 memset( s, 0, sizeof( s ) );
2934
2935 name = dn;
2936 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002937 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
2939 while( name != NULL )
2940 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002941 if( !name->oid.p )
2942 {
2943 name = name->next;
2944 continue;
2945 }
2946
Paul Bakker74111d32011-01-15 16:57:55 +00002947 if( name != dn )
2948 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002949 ret = snprintf( p, n, ", " );
2950 SAFE_SNPRINTF();
2951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Paul Bakkerc70b9822013-04-07 22:00:46 +02002953 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Paul Bakkerc70b9822013-04-07 22:00:46 +02002955 if( ret == 0 )
2956 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002958 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002959 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
2961 for( i = 0; i < name->val.len; i++ )
2962 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002963 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 break;
2965
2966 c = name->val.p[i];
2967 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2968 s[i] = '?';
2969 else s[i] = c;
2970 }
2971 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002972 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002973 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 name = name->next;
2975 }
2976
Paul Bakker23986e52011-04-24 08:57:21 +00002977 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002978}
2979
2980/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002981 * Store the serial in printable form into buf; no more
2982 * than size characters will be written
2983 */
2984int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2985{
Paul Bakker23986e52011-04-24 08:57:21 +00002986 int ret;
2987 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002988 char *p;
2989
2990 p = buf;
2991 n = size;
2992
2993 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002994 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002995
2996 for( i = 0; i < nr; i++ )
2997 {
Paul Bakker93048802011-12-05 14:38:06 +00002998 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002999 continue;
3000
Paul Bakkerdd476992011-01-16 21:34:59 +00003001 ret = snprintf( p, n, "%02X%s",
3002 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3003 SAFE_SNPRINTF();
3004 }
3005
Paul Bakker03c7c252011-11-25 12:37:37 +00003006 if( nr != serial->len )
3007 {
3008 ret = snprintf( p, n, "...." );
3009 SAFE_SNPRINTF();
3010 }
3011
Paul Bakker23986e52011-04-24 08:57:21 +00003012 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003013}
3014
3015/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003016 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003018int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3019 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003020{
Paul Bakker23986e52011-04-24 08:57:21 +00003021 int ret;
3022 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003023 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003024 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
3026 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003027 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakkerd98030e2009-05-02 15:13:40 +00003029 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031 SAFE_SNPRINTF();
3032 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003034 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakkerdd476992011-01-16 21:34:59 +00003036 ret = x509parse_serial_gets( p, n, &crt->serial);
3037 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Paul Bakkerd98030e2009-05-02 15:13:40 +00003039 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3040 SAFE_SNPRINTF();
3041 ret = x509parse_dn_gets( p, n, &crt->issuer );
3042 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Paul Bakkerd98030e2009-05-02 15:13:40 +00003044 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3045 SAFE_SNPRINTF();
3046 ret = x509parse_dn_gets( p, n, &crt->subject );
3047 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakkerd98030e2009-05-02 15:13:40 +00003049 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3051 crt->valid_from.year, crt->valid_from.mon,
3052 crt->valid_from.day, crt->valid_from.hour,
3053 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003054 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakkerd98030e2009-05-02 15:13:40 +00003056 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003057 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3058 crt->valid_to.year, crt->valid_to.mon,
3059 crt->valid_to.day, crt->valid_to.hour,
3060 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003061 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003064 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakkerc70b9822013-04-07 22:00:46 +02003066 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3067 if( ret != 0 )
3068 ret = snprintf( p, n, "???" );
3069 else
3070 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003071 SAFE_SNPRINTF();
3072
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003073 switch( crt->pk.type )
3074 {
3075 case POLARSSL_PK_NONE:
3076 case POLARSSL_PK_ECDSA:
3077 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3078 break;
3079
3080 case POLARSSL_PK_RSA:
3081 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3082 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3083 break;
3084
3085 case POLARSSL_PK_ECKEY:
3086 case POLARSSL_PK_ECKEY_DH:
3087 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3088 (int) pk_ec( crt->pk )->grp.pbits );
3089 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003090 SAFE_SNPRINTF();
3091
Paul Bakker23986e52011-04-24 08:57:21 +00003092 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003093}
3094
Paul Bakker74111d32011-01-15 16:57:55 +00003095/*
3096 * Return an informational string describing the given OID
3097 */
3098const char *x509_oid_get_description( x509_buf *oid )
3099{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003100 const char *desc = NULL;
3101 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003102
Paul Bakkerc70b9822013-04-07 22:00:46 +02003103 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003104
Paul Bakkerc70b9822013-04-07 22:00:46 +02003105 if( ret != 0 )
3106 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003107
Paul Bakkerc70b9822013-04-07 22:00:46 +02003108 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003109}
3110
3111/* Return the x.y.z.... style numeric string for the given OID */
3112int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3113{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003114 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003115}
3116
Paul Bakkerd98030e2009-05-02 15:13:40 +00003117/*
3118 * Return an informational string about the CRL.
3119 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003120int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3121 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122{
Paul Bakker23986e52011-04-24 08:57:21 +00003123 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003124 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003125 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003126 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003127 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003128
3129 p = buf;
3130 n = size;
3131
3132 ret = snprintf( p, n, "%sCRL version : %d",
3133 prefix, crl->version );
3134 SAFE_SNPRINTF();
3135
3136 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3137 SAFE_SNPRINTF();
3138 ret = x509parse_dn_gets( p, n, &crl->issuer );
3139 SAFE_SNPRINTF();
3140
3141 ret = snprintf( p, n, "\n%sthis update : " \
3142 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3143 crl->this_update.year, crl->this_update.mon,
3144 crl->this_update.day, crl->this_update.hour,
3145 crl->this_update.min, crl->this_update.sec );
3146 SAFE_SNPRINTF();
3147
3148 ret = snprintf( p, n, "\n%snext update : " \
3149 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3150 crl->next_update.year, crl->next_update.mon,
3151 crl->next_update.day, crl->next_update.hour,
3152 crl->next_update.min, crl->next_update.sec );
3153 SAFE_SNPRINTF();
3154
3155 entry = &crl->entry;
3156
3157 ret = snprintf( p, n, "\n%sRevoked certificates:",
3158 prefix );
3159 SAFE_SNPRINTF();
3160
Paul Bakker9be19372009-07-27 20:21:53 +00003161 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003162 {
3163 ret = snprintf( p, n, "\n%sserial number: ",
3164 prefix );
3165 SAFE_SNPRINTF();
3166
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003167 ret = x509parse_serial_gets( p, n, &entry->serial);
3168 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003169
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170 ret = snprintf( p, n, " revocation date: " \
3171 "%04d-%02d-%02d %02d:%02d:%02d",
3172 entry->revocation_date.year, entry->revocation_date.mon,
3173 entry->revocation_date.day, entry->revocation_date.hour,
3174 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003175 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003176
3177 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 }
3179
Paul Bakkerc70b9822013-04-07 22:00:46 +02003180 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003181 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003182
Paul Bakkerc70b9822013-04-07 22:00:46 +02003183 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3184 if( ret != 0 )
3185 ret = snprintf( p, n, "???" );
3186 else
3187 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003188 SAFE_SNPRINTF();
3189
Paul Bakker1e27bb22009-07-19 20:25:25 +00003190 ret = snprintf( p, n, "\n" );
3191 SAFE_SNPRINTF();
3192
Paul Bakker23986e52011-04-24 08:57:21 +00003193 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194}
3195
3196/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003197 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003199#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003200int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003201{
Paul Bakkercce9d772011-11-18 14:26:47 +00003202 int year, mon, day;
3203 int hour, min, sec;
3204
3205#if defined(_WIN32)
3206 SYSTEMTIME st;
3207
3208 GetLocalTime(&st);
3209
3210 year = st.wYear;
3211 mon = st.wMonth;
3212 day = st.wDay;
3213 hour = st.wHour;
3214 min = st.wMinute;
3215 sec = st.wSecond;
3216#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 struct tm *lt;
3218 time_t tt;
3219
3220 tt = time( NULL );
3221 lt = localtime( &tt );
3222
Paul Bakkercce9d772011-11-18 14:26:47 +00003223 year = lt->tm_year + 1900;
3224 mon = lt->tm_mon + 1;
3225 day = lt->tm_mday;
3226 hour = lt->tm_hour;
3227 min = lt->tm_min;
3228 sec = lt->tm_sec;
3229#endif
3230
3231 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003232 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakkercce9d772011-11-18 14:26:47 +00003234 if( year == to->year &&
3235 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003236 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Paul Bakkercce9d772011-11-18 14:26:47 +00003238 if( year == to->year &&
3239 mon == to->mon &&
3240 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +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 )
Paul Bakkerb6194992011-01-16 21:40:22 +00003247 return( 1 );
3248
Paul Bakkercce9d772011-11-18 14:26:47 +00003249 if( year == to->year &&
3250 mon == to->mon &&
3251 day == to->day &&
3252 hour == to->hour &&
3253 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003254 return( 1 );
3255
Paul Bakkercce9d772011-11-18 14:26:47 +00003256 if( year == to->year &&
3257 mon == to->mon &&
3258 day == to->day &&
3259 hour == to->hour &&
3260 min == to->min &&
3261 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003262 return( 1 );
3263
Paul Bakker40ea7de2009-05-03 10:18:48 +00003264 return( 0 );
3265}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003266#else /* POLARSSL_HAVE_TIME */
3267int x509parse_time_expired( const x509_time *to )
3268{
3269 ((void) to);
3270 return( 0 );
3271}
3272#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003273
3274/*
3275 * Return 1 if the certificate is revoked, or 0 otherwise.
3276 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003277int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003278{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003279 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003280
3281 while( cur != NULL && cur->serial.len != 0 )
3282 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003283 if( crt->serial.len == cur->serial.len &&
3284 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003285 {
3286 if( x509parse_time_expired( &cur->revocation_date ) )
3287 return( 1 );
3288 }
3289
3290 cur = cur->next;
3291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
3293 return( 0 );
3294}
3295
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003296/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003297 * Check that the given certificate is valid accoring to the CRL.
3298 */
3299static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3300 x509_crl *crl_list)
3301{
3302 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003303 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3304 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003305
Paul Bakker915275b2012-09-28 07:10:55 +00003306 if( ca == NULL )
3307 return( flags );
3308
Paul Bakker76fd75a2011-01-16 21:12:10 +00003309 /*
3310 * TODO: What happens if no CRL is present?
3311 * Suggestion: Revocation state should be unknown if no CRL is present.
3312 * For backwards compatibility this is not yet implemented.
3313 */
3314
Paul Bakker915275b2012-09-28 07:10:55 +00003315 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003316 {
Paul Bakker915275b2012-09-28 07:10:55 +00003317 if( crl_list->version == 0 ||
3318 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003319 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3320 crl_list->issuer_raw.len ) != 0 )
3321 {
3322 crl_list = crl_list->next;
3323 continue;
3324 }
3325
3326 /*
3327 * Check if CRL is correctly signed by the trusted CA
3328 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003329 md_info = md_info_from_type( crl_list->sig_md );
3330 if( md_info == NULL )
3331 {
3332 /*
3333 * Cannot check 'unknown' hash
3334 */
3335 flags |= BADCRL_NOT_TRUSTED;
3336 break;
3337 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003338
Paul Bakkerc70b9822013-04-07 22:00:46 +02003339 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003340
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003341 /* EC NOT IMPLEMENTED YET */
3342 if( ca->pk.type != POLARSSL_PK_RSA )
3343 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3344
3345 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003346 0, hash, crl_list->sig.p ) == 0 )
3347 {
3348 /*
3349 * CRL is not trusted
3350 */
3351 flags |= BADCRL_NOT_TRUSTED;
3352 break;
3353 }
3354
3355 /*
3356 * Check for validity of CRL (Do not drop out)
3357 */
3358 if( x509parse_time_expired( &crl_list->next_update ) )
3359 flags |= BADCRL_EXPIRED;
3360
3361 /*
3362 * Check if certificate is revoked
3363 */
3364 if( x509parse_revoked(crt, crl_list) )
3365 {
3366 flags |= BADCERT_REVOKED;
3367 break;
3368 }
3369
3370 crl_list = crl_list->next;
3371 }
3372 return flags;
3373}
3374
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003375static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003376{
3377 size_t i;
3378 size_t cn_idx = 0;
3379
Paul Bakker57b12982012-02-11 17:38:38 +00003380 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003381 return( 0 );
3382
3383 for( i = 0; i < strlen( cn ); ++i )
3384 {
3385 if( cn[i] == '.' )
3386 {
3387 cn_idx = i;
3388 break;
3389 }
3390 }
3391
3392 if( cn_idx == 0 )
3393 return( 0 );
3394
Paul Bakker535e97d2012-08-23 10:49:55 +00003395 if( strlen( cn ) - cn_idx == name->len - 1 &&
3396 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003397 {
3398 return( 1 );
3399 }
3400
3401 return( 0 );
3402}
3403
Paul Bakker915275b2012-09-28 07:10:55 +00003404static int x509parse_verify_top(
3405 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003406 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003407 int (*f_vrfy)(void *, x509_cert *, int, int *),
3408 void *p_vrfy )
3409{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003410 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003411 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003412 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3413 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003414
3415 if( x509parse_time_expired( &child->valid_to ) )
3416 *flags |= BADCERT_EXPIRED;
3417
3418 /*
3419 * Child is the top of the chain. Check against the trust_ca list.
3420 */
3421 *flags |= BADCERT_NOT_TRUSTED;
3422
3423 while( trust_ca != NULL )
3424 {
3425 if( trust_ca->version == 0 ||
3426 child->issuer_raw.len != trust_ca->subject_raw.len ||
3427 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3428 child->issuer_raw.len ) != 0 )
3429 {
3430 trust_ca = trust_ca->next;
3431 continue;
3432 }
3433
Paul Bakker9a736322012-11-14 12:39:52 +00003434 /*
3435 * Reduce path_len to check against if top of the chain is
3436 * the same as the trusted CA
3437 */
3438 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3439 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3440 child->issuer_raw.len ) == 0 )
3441 {
3442 check_path_cnt--;
3443 }
3444
Paul Bakker915275b2012-09-28 07:10:55 +00003445 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003446 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003447 {
3448 trust_ca = trust_ca->next;
3449 continue;
3450 }
3451
Paul Bakkerc70b9822013-04-07 22:00:46 +02003452 md_info = md_info_from_type( child->sig_md );
3453 if( md_info == NULL )
3454 {
3455 /*
3456 * Cannot check 'unknown' hash
3457 */
3458 continue;
3459 }
Paul Bakker915275b2012-09-28 07:10:55 +00003460
Paul Bakkerc70b9822013-04-07 22:00:46 +02003461 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003462
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003463 /* EC NOT IMPLEMENTED YET */
3464 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3465 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3466
3467 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003468 0, hash, child->sig.p ) != 0 )
3469 {
3470 trust_ca = trust_ca->next;
3471 continue;
3472 }
3473
3474 /*
3475 * Top of chain is signed by a trusted CA
3476 */
3477 *flags &= ~BADCERT_NOT_TRUSTED;
3478 break;
3479 }
3480
Paul Bakker9a736322012-11-14 12:39:52 +00003481 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003482 * If top of chain is not the same as the trusted CA send a verify request
3483 * to the callback for any issues with validity and CRL presence for the
3484 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003485 */
3486 if( trust_ca != NULL &&
3487 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3488 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3489 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003490 {
3491 /* Check trusted CA's CRL for then chain's top crt */
3492 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3493
3494 if( x509parse_time_expired( &trust_ca->valid_to ) )
3495 ca_flags |= BADCERT_EXPIRED;
3496
Paul Bakker915275b2012-09-28 07:10:55 +00003497 if( NULL != f_vrfy )
3498 {
Paul Bakker9a736322012-11-14 12:39:52 +00003499 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003500 return( ret );
3501 }
3502 }
3503
3504 /* Call callback on top cert */
3505 if( NULL != f_vrfy )
3506 {
Paul Bakker9a736322012-11-14 12:39:52 +00003507 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003508 return( ret );
3509 }
3510
Paul Bakker915275b2012-09-28 07:10:55 +00003511 *flags |= ca_flags;
3512
3513 return( 0 );
3514}
3515
3516static int x509parse_verify_child(
3517 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003518 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003519 int (*f_vrfy)(void *, x509_cert *, int, int *),
3520 void *p_vrfy )
3521{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003522 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003523 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003524 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003525 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003526 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003527
3528 if( x509parse_time_expired( &child->valid_to ) )
3529 *flags |= BADCERT_EXPIRED;
3530
Paul Bakkerc70b9822013-04-07 22:00:46 +02003531 md_info = md_info_from_type( child->sig_md );
3532 if( md_info == NULL )
3533 {
3534 /*
3535 * Cannot check 'unknown' hash
3536 */
Paul Bakker915275b2012-09-28 07:10:55 +00003537 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003538 }
3539 else
3540 {
3541 md( md_info, child->tbs.p, child->tbs.len, hash );
3542
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003543 /* EC NOT IMPLEMENTED YET */
3544 if( parent->pk.type != POLARSSL_PK_RSA )
3545 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3546
3547 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3548 0, hash, child->sig.p ) != 0 )
3549 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003550 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003551 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003552 }
3553
Paul Bakker915275b2012-09-28 07:10:55 +00003554 /* Check trusted CA's CRL for the given crt */
3555 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3556
3557 grandparent = parent->next;
3558
3559 while( grandparent != NULL )
3560 {
3561 if( grandparent->version == 0 ||
3562 grandparent->ca_istrue == 0 ||
3563 parent->issuer_raw.len != grandparent->subject_raw.len ||
3564 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3565 parent->issuer_raw.len ) != 0 )
3566 {
3567 grandparent = grandparent->next;
3568 continue;
3569 }
3570 break;
3571 }
3572
Paul Bakker915275b2012-09-28 07:10:55 +00003573 if( grandparent != NULL )
3574 {
3575 /*
3576 * Part of the chain
3577 */
Paul Bakker9a736322012-11-14 12:39:52 +00003578 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 +00003579 if( ret != 0 )
3580 return( ret );
3581 }
3582 else
3583 {
Paul Bakker9a736322012-11-14 12:39:52 +00003584 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 +00003585 if( ret != 0 )
3586 return( ret );
3587 }
3588
3589 /* child is verified to be a child of the parent, call verify callback */
3590 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003591 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003592 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003593
3594 *flags |= parent_flags;
3595
3596 return( 0 );
3597}
3598
Paul Bakker76fd75a2011-01-16 21:12:10 +00003599/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 * Verify the certificate validity
3601 */
3602int x509parse_verify( x509_cert *crt,
3603 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003604 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003605 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003606 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003607 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608{
Paul Bakker23986e52011-04-24 08:57:21 +00003609 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003610 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003611 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003612 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003614 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Paul Bakker40ea7de2009-05-03 10:18:48 +00003616 *flags = 0;
3617
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 if( cn != NULL )
3619 {
3620 name = &crt->subject;
3621 cn_len = strlen( cn );
3622
Paul Bakker4d2c1242012-05-10 14:12:46 +00003623 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003625 cur = &crt->subject_alt_names;
3626
3627 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003628 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003629 if( cur->buf.len == cn_len &&
3630 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003631 break;
3632
Paul Bakker535e97d2012-08-23 10:49:55 +00003633 if( cur->buf.len > 2 &&
3634 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003635 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003636 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
Paul Bakker4d2c1242012-05-10 14:12:46 +00003638 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003639 }
3640
3641 if( cur == NULL )
3642 *flags |= BADCERT_CN_MISMATCH;
3643 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003644 else
3645 {
3646 while( name != NULL )
3647 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003648 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003649 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003650 if( name->val.len == cn_len &&
3651 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003652 break;
3653
Paul Bakker535e97d2012-08-23 10:49:55 +00003654 if( name->val.len > 2 &&
3655 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003656 x509_wildcard_verify( cn, &name->val ) )
3657 break;
3658 }
3659
3660 name = name->next;
3661 }
3662
3663 if( name == NULL )
3664 *flags |= BADCERT_CN_MISMATCH;
3665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 }
3667
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003669 * Iterate upwards in the given cert chain, to find our crt parent.
3670 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003672 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Paul Bakker76fd75a2011-01-16 21:12:10 +00003674 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003676 if( parent->ca_istrue == 0 ||
3677 crt->issuer_raw.len != parent->subject_raw.len ||
3678 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 crt->issuer_raw.len ) != 0 )
3680 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003681 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 continue;
3683 }
Paul Bakker915275b2012-09-28 07:10:55 +00003684 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 }
3686
Paul Bakker915275b2012-09-28 07:10:55 +00003687 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 {
Paul Bakker915275b2012-09-28 07:10:55 +00003689 /*
3690 * Part of the chain
3691 */
Paul Bakker9a736322012-11-14 12:39:52 +00003692 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003693 if( ret != 0 )
3694 return( ret );
3695 }
3696 else
Paul Bakker74111d32011-01-15 16:57:55 +00003697 {
Paul Bakker9a736322012-11-14 12:39:52 +00003698 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003699 if( ret != 0 )
3700 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003701 }
Paul Bakker915275b2012-09-28 07:10:55 +00003702
3703 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003704 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003705
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 return( 0 );
3707}
3708
3709/*
3710 * Unallocate all certificate data
3711 */
3712void x509_free( x509_cert *crt )
3713{
3714 x509_cert *cert_cur = crt;
3715 x509_cert *cert_prv;
3716 x509_name *name_cur;
3717 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003718 x509_sequence *seq_cur;
3719 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
3721 if( crt == NULL )
3722 return;
3723
3724 do
3725 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003726 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
3728 name_cur = cert_cur->issuer.next;
3729 while( name_cur != NULL )
3730 {
3731 name_prv = name_cur;
3732 name_cur = name_cur->next;
3733 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003734 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 }
3736
3737 name_cur = cert_cur->subject.next;
3738 while( name_cur != NULL )
3739 {
3740 name_prv = name_cur;
3741 name_cur = name_cur->next;
3742 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003743 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 }
3745
Paul Bakker74111d32011-01-15 16:57:55 +00003746 seq_cur = cert_cur->ext_key_usage.next;
3747 while( seq_cur != NULL )
3748 {
3749 seq_prv = seq_cur;
3750 seq_cur = seq_cur->next;
3751 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003752 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003753 }
3754
Paul Bakker8afa70d2012-02-11 18:42:45 +00003755 seq_cur = cert_cur->subject_alt_names.next;
3756 while( seq_cur != NULL )
3757 {
3758 seq_prv = seq_cur;
3759 seq_cur = seq_cur->next;
3760 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003761 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003762 }
3763
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 if( cert_cur->raw.p != NULL )
3765 {
3766 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003767 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 }
3769
3770 cert_cur = cert_cur->next;
3771 }
3772 while( cert_cur != NULL );
3773
3774 cert_cur = crt;
3775 do
3776 {
3777 cert_prv = cert_cur;
3778 cert_cur = cert_cur->next;
3779
3780 memset( cert_prv, 0, sizeof( x509_cert ) );
3781 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003782 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 }
3784 while( cert_cur != NULL );
3785}
3786
Paul Bakkerd98030e2009-05-02 15:13:40 +00003787/*
3788 * Unallocate all CRL data
3789 */
3790void x509_crl_free( x509_crl *crl )
3791{
3792 x509_crl *crl_cur = crl;
3793 x509_crl *crl_prv;
3794 x509_name *name_cur;
3795 x509_name *name_prv;
3796 x509_crl_entry *entry_cur;
3797 x509_crl_entry *entry_prv;
3798
3799 if( crl == NULL )
3800 return;
3801
3802 do
3803 {
3804 name_cur = crl_cur->issuer.next;
3805 while( name_cur != NULL )
3806 {
3807 name_prv = name_cur;
3808 name_cur = name_cur->next;
3809 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003810 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003811 }
3812
3813 entry_cur = crl_cur->entry.next;
3814 while( entry_cur != NULL )
3815 {
3816 entry_prv = entry_cur;
3817 entry_cur = entry_cur->next;
3818 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003819 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003820 }
3821
3822 if( crl_cur->raw.p != NULL )
3823 {
3824 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003825 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003826 }
3827
3828 crl_cur = crl_cur->next;
3829 }
3830 while( crl_cur != NULL );
3831
3832 crl_cur = crl;
3833 do
3834 {
3835 crl_prv = crl_cur;
3836 crl_cur = crl_cur->next;
3837
3838 memset( crl_prv, 0, sizeof( x509_crl ) );
3839 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003840 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003841 }
3842 while( crl_cur != NULL );
3843}
3844
Paul Bakker40e46942009-01-03 21:51:57 +00003845#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Paul Bakker40e46942009-01-03 21:51:57 +00003847#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
3849/*
3850 * Checkup routine
3851 */
3852int x509_self_test( int verbose )
3853{
Paul Bakker5690efc2011-05-26 13:16:06 +00003854#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003855 int ret;
3856 int flags;
3857 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 x509_cert cacert;
3859 x509_cert clicert;
3860 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003861#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003862 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003863#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003864
3865 if( verbose != 0 )
3866 printf( " X.509 certificate load: " );
3867
3868 memset( &clicert, 0, sizeof( x509_cert ) );
3869
Paul Bakker3c2122f2013-06-24 19:03:14 +02003870 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003871 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 if( ret != 0 )
3873 {
3874 if( verbose != 0 )
3875 printf( "failed\n" );
3876
3877 return( ret );
3878 }
3879
3880 memset( &cacert, 0, sizeof( x509_cert ) );
3881
Paul Bakker3c2122f2013-06-24 19:03:14 +02003882 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003883 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 if( ret != 0 )
3885 {
3886 if( verbose != 0 )
3887 printf( "failed\n" );
3888
3889 return( ret );
3890 }
3891
3892 if( verbose != 0 )
3893 printf( "passed\n X.509 private key load: " );
3894
3895 i = strlen( test_ca_key );
3896 j = strlen( test_ca_pwd );
3897
Paul Bakker66b78b22011-03-25 14:22:50 +00003898 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3899
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003900 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003901 (const unsigned char *) test_ca_key, i,
3902 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003903 {
3904 if( verbose != 0 )
3905 printf( "failed\n" );
3906
3907 return( ret );
3908 }
3909
3910 if( verbose != 0 )
3911 printf( "passed\n X.509 signature verify: ");
3912
Paul Bakker23986e52011-04-24 08:57:21 +00003913 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 if( ret != 0 )
3915 {
Paul Bakker23986e52011-04-24 08:57:21 +00003916 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 if( verbose != 0 )
3918 printf( "failed\n" );
3919
3920 return( ret );
3921 }
3922
Paul Bakker5690efc2011-05-26 13:16:06 +00003923#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003925 printf( "passed\n X.509 DHM parameter load: " );
3926
3927 i = strlen( test_dhm_params );
3928 j = strlen( test_ca_pwd );
3929
Paul Bakker3c2122f2013-06-24 19:03:14 +02003930 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003931 {
3932 if( verbose != 0 )
3933 printf( "failed\n" );
3934
3935 return( ret );
3936 }
3937
3938 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
3942 x509_free( &cacert );
3943 x509_free( &clicert );
3944 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003945#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003946 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
3949 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003950#else
3951 ((void) verbose);
3952 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3953#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003954}
3955
3956#endif
3957
3958#endif