blob: e3c363a39f6dddbda57b808b19ee58bf11cd8493 [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é-Gonnard0a64e8f2013-07-08 18:26:18 +0200163/* Get an algorithm identifier and its parameters
164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
169static int x509_get_algid( unsigned char **p,
170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
172{
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000197/*
198 * AlgorithmIdentifier ::= SEQUENCE {
199 * algorithm OBJECT IDENTIFIER,
200 * parameters ANY DEFINED BY algorithm OPTIONAL }
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200201 *
202 * If params_end is NULL, then parameters must be absent or ANS.1 NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
204static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205 const unsigned char *end,
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200206 x509_buf *alg, const unsigned char **params_end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
Paul Bakker23986e52011-04-24 08:57:21 +0000208 int ret;
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200209 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200211 if( params_end == NULL ) {
212 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
213 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
214
215 return( 0 );
216 }
217
218 /* TODO: use asn1_get_alg */
219 if( ( ret = asn1_get_tag( p, end, &len,
220 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
221 {
222 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
223 }
224
225 end = *p + len;
226 alg->tag = **p;
227
228 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000229 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +0200231 alg->p = *p;
232 *p += alg->len;
233
234 *params_end = end;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 return( 0 );
236}
237
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200238/* Get an EC group id from an ECParameters buffer
239 *
240 * ECParameters ::= CHOICE {
241 * namedCurve OBJECT IDENTIFIER
242 * -- implicitCurve NULL
243 * -- specifiedCurve SpecifiedECDomain
244 * }
245 */
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200246static int x509_ecparams_get_grp_id( const x509_buf *params,
247 ecp_group_id *grp_id )
248{
249 if( oid_get_ec_grp( params, grp_id ) != 0 )
250 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
251
252 return( 0 );
253}
254
255/* Get an EC group id from an ECParameters buffer
256 *
257 * ECParameters ::= CHOICE {
258 * namedCurve OBJECT IDENTIFIER
259 * -- implicitCurve NULL
260 * -- specifiedCurve SpecifiedECDomain
261 * }
262 */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200263static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
264 ecp_group_id *grp_id )
265{
266 int ret;
267 x509_buf curve;
268
269 curve.tag = **p;
270
271 if( ( ret = asn1_get_tag( p, end, &curve.len, ASN1_OID ) ) != 0 )
272 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
273
274 curve.p = *p;
275 *p += curve.len;
276
277 if( *p != end )
278 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
279 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
280
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200281 return( x509_ecparams_get_grp_id( &curve, grp_id ) );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200282}
283
Paul Bakker5121ce52009-01-03 21:22:43 +0000284/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200285 * subjectPublicKey BIT STRING
286 * -- which, in our case, contains
287 * ECPoint ::= octet string (not ASN.1)
288 */
289static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
290 const ecp_group *grp, ecp_point *pt )
291{
292 int ret;
293 size_t len;
294
295 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
296 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
297
298 if( *p + len != end )
299 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
300 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
301
302 /*
303 * First byte in the content of BIT STRING is the nummber of padding bit.
304 * Here it is always 0 since ECPoint is an octet string, so skip it.
305 */
306 ++*p;
307 --len;
308
309 if( ( ret = ecp_point_read_binary( grp, pt,
310 (const unsigned char *) *p, len ) ) != 0 )
311 {
312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
313 }
314
315 return( 0 );
316}
317
318/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * AttributeTypeAndValue ::= SEQUENCE {
320 * type AttributeType,
321 * value AttributeValue }
322 *
323 * AttributeType ::= OBJECT IDENTIFIER
324 *
325 * AttributeValue ::= ANY DEFINED BY AttributeType
326 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000327static int x509_get_attr_type_value( unsigned char **p,
328 const unsigned char *end,
329 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
Paul Bakker23986e52011-04-24 08:57:21 +0000331 int ret;
332 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 x509_buf *oid;
334 x509_buf *val;
335
336 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000338 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 oid = &cur->oid;
341 oid->tag = **p;
342
343 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000344 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 oid->p = *p;
347 *p += oid->len;
348
349 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000351 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
354 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
355 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000357 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359 val = &cur->val;
360 val->tag = *(*p)++;
361
362 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000363 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365 val->p = *p;
366 *p += val->len;
367
368 cur->next = NULL;
369
Paul Bakker400ff6f2011-02-20 10:40:16 +0000370 return( 0 );
371}
372
373/*
374 * RelativeDistinguishedName ::=
375 * SET OF AttributeTypeAndValue
376 *
377 * AttributeTypeAndValue ::= SEQUENCE {
378 * type AttributeType,
379 * value AttributeValue }
380 *
381 * AttributeType ::= OBJECT IDENTIFIER
382 *
383 * AttributeValue ::= ANY DEFINED BY AttributeType
384 */
385static int x509_get_name( unsigned char **p,
386 const unsigned char *end,
387 x509_name *cur )
388{
Paul Bakker23986e52011-04-24 08:57:21 +0000389 int ret;
390 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000391 const unsigned char *end2;
392 x509_name *use;
393
394 if( ( ret = asn1_get_tag( p, end, &len,
395 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000397
398 end2 = end;
399 end = *p + len;
400 use = cur;
401
402 do
403 {
404 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
405 return( ret );
406
407 if( *p != end )
408 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200409 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000410 sizeof( x509_name ) );
411
412 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000413 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000414
415 memset( use->next, 0, sizeof( x509_name ) );
416
417 use = use->next;
418 }
419 }
420 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 /*
423 * recurse until end of SEQUENCE is reached
424 */
425 if( *p == end2 )
426 return( 0 );
427
Paul Bakker6e339b52013-07-03 13:37:05 +0200428 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 sizeof( x509_name ) );
430
431 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000432 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Paul Bakker430ffbe2012-05-01 08:14:20 +0000434 memset( cur->next, 0, sizeof( x509_name ) );
435
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 return( x509_get_name( p, end2, cur->next ) );
437}
438
439/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 * Time ::= CHOICE {
441 * utcTime UTCTime,
442 * generalTime GeneralizedTime }
443 */
Paul Bakker91200182010-02-18 21:26:15 +0000444static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000445 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000446 x509_time *time )
447{
Paul Bakker23986e52011-04-24 08:57:21 +0000448 int ret;
449 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000450 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000451 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000452
Paul Bakker91200182010-02-18 21:26:15 +0000453 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000454 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
455 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000456
Paul Bakker91200182010-02-18 21:26:15 +0000457 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000458
Paul Bakker91200182010-02-18 21:26:15 +0000459 if ( tag == ASN1_UTC_TIME )
460 {
461 (*p)++;
462 ret = asn1_get_len( p, end, &len );
463
464 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000465 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000466
Paul Bakker91200182010-02-18 21:26:15 +0000467 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000468 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
469 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000470
Paul Bakker91200182010-02-18 21:26:15 +0000471 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
472 &time->year, &time->mon, &time->day,
473 &time->hour, &time->min, &time->sec ) < 5 )
474 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000475
Paul Bakker400ff6f2011-02-20 10:40:16 +0000476 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000477 time->year += 1900;
478
479 *p += len;
480
481 return( 0 );
482 }
483 else if ( tag == ASN1_GENERALIZED_TIME )
484 {
485 (*p)++;
486 ret = asn1_get_len( p, end, &len );
487
488 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000490
491 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000492 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
493 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000494
495 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
496 &time->year, &time->mon, &time->day,
497 &time->hour, &time->min, &time->sec ) < 5 )
498 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
499
500 *p += len;
501
502 return( 0 );
503 }
504 else
Paul Bakker9d781402011-05-09 16:17:09 +0000505 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000506}
507
508
509/*
510 * Validity ::= SEQUENCE {
511 * notBefore Time,
512 * notAfter Time }
513 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000514static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 x509_time *from,
517 x509_time *to )
518{
Paul Bakker23986e52011-04-24 08:57:21 +0000519 int ret;
520 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 if( ( ret = asn1_get_tag( p, end, &len,
523 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 end = *p + len;
527
Paul Bakker91200182010-02-18 21:26:15 +0000528 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000529 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Paul Bakker91200182010-02-18 21:26:15 +0000531 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000532 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000535 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000536 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538 return( 0 );
539}
540
541/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200542 * RSAPublicKey ::= SEQUENCE {
543 * modulus INTEGER, -- n
544 * publicExponent INTEGER -- e
545 * }
546 */
547static int x509_get_rsapubkey( unsigned char **p,
548 const unsigned char *end,
549 rsa_context *rsa )
550{
551 int ret;
552 size_t len;
553
554 if( ( ret = asn1_get_tag( p, end, &len,
555 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
556 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
557
558 if( *p + len != end )
559 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
560 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
561
562 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
563 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
564 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
565
566 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
567 return( ret );
568
569 rsa->len = mpi_size( &rsa->N );
570
571 return( 0 );
572}
573
574/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * SubjectPublicKeyInfo ::= SEQUENCE {
576 * algorithm AlgorithmIdentifier,
577 * subjectPublicKey BIT STRING }
578 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200579static int x509_get_pubkey_rsa( unsigned char **p,
580 const unsigned char *end,
581 rsa_context *rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200583 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000584 size_t len;
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200585 x509_buf pk_alg_oid;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200586 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +0200588 if( ( ret = asn1_get_tag( p, end, &len,
589 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
590 {
591 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
592 }
593
594 end = *p + len;
595
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200596 if( ( ret = asn1_get_alg_null( p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200597 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * only RSA public keys handled at this time
601 */
Manuel Pégourié-Gonnard788db112013-07-09 11:26:17 +0200602 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200603 {
Paul Bakkered56b222011-07-13 11:26:43 +0000604 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +0200605 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +0200607 if (pk_alg != POLARSSL_PK_RSA )
608 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
609
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000611 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000614 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000615 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Manuel Pégourié-Gonnardf16ac762013-07-09 12:26:00 +0200617 if( *p + len != end )
618 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
619 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000622 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200624 return( x509_get_rsapubkey( p, end, rsa ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625}
626
627static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000628 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 x509_buf *sig )
630{
Paul Bakker23986e52011-04-24 08:57:21 +0000631 int ret;
632 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Paul Bakker8afa70d2012-02-11 18:42:45 +0000634 if( ( end - *p ) < 1 )
635 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
636 POLARSSL_ERR_ASN1_OUT_OF_DATA );
637
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 sig->tag = **p;
639
640 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000641 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
Paul Bakker74111d32011-01-15 16:57:55 +0000643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000645 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 sig->len = len;
648 sig->p = *p;
649
650 *p += len;
651
652 return( 0 );
653}
654
655/*
656 * X.509 v2/v3 unique identifier (not parsed)
657 */
658static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000659 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 x509_buf *uid, int n )
661{
662 int ret;
663
664 if( *p == end )
665 return( 0 );
666
667 uid->tag = **p;
668
669 if( ( ret = asn1_get_tag( p, end, &uid->len,
670 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
671 {
Paul Bakker40e46942009-01-03 21:51:57 +0000672 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 return( 0 );
674
675 return( ret );
676 }
677
678 uid->p = *p;
679 *p += uid->len;
680
681 return( 0 );
682}
683
684/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000685 * X.509 Extensions (No parsing of extensions, pointer should
686 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 */
688static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000689 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000690 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691{
Paul Bakker23986e52011-04-24 08:57:21 +0000692 int ret;
693 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695 if( *p == end )
696 return( 0 );
697
698 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000699
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000701 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 ext->p = *p;
705 end = *p + ext->len;
706
707 /*
708 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
709 *
710 * Extension ::= SEQUENCE {
711 * extnID OBJECT IDENTIFIER,
712 * critical BOOLEAN DEFAULT FALSE,
713 * extnValue OCTET STRING }
714 */
715 if( ( ret = asn1_get_tag( p, end, &len,
716 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000717 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000721 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Paul Bakkerd98030e2009-05-02 15:13:40 +0000723 return( 0 );
724}
725
726/*
727 * X.509 CRL v2 extensions (no extensions parsed yet.)
728 */
729static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000730 const unsigned char *end,
731 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000732{
Paul Bakker23986e52011-04-24 08:57:21 +0000733 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000734 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000735
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000736 /* Get explicit tag */
737 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000738 {
739 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
740 return( 0 );
741
742 return( ret );
743 }
744
745 while( *p < end )
746 {
747 if( ( ret = asn1_get_tag( p, end, &len,
748 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000750
751 *p += len;
752 }
753
754 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000755 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000756 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
757
758 return( 0 );
759}
760
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000761/*
762 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
763 */
764static int x509_get_crl_entry_ext( unsigned char **p,
765 const unsigned char *end,
766 x509_buf *ext )
767{
768 int ret;
769 size_t len = 0;
770
771 /* OPTIONAL */
772 if (end <= *p)
773 return( 0 );
774
775 ext->tag = **p;
776 ext->p = *p;
777
778 /*
779 * Get CRL-entry extension sequence header
780 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
781 */
782 if( ( ret = asn1_get_tag( p, end, &ext->len,
783 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
784 {
785 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
786 {
787 ext->p = NULL;
788 return( 0 );
789 }
790 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
791 }
792
793 end = *p + ext->len;
794
795 if( end != *p + ext->len )
796 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
797 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
798
799 while( *p < end )
800 {
801 if( ( ret = asn1_get_tag( p, end, &len,
802 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
804
805 *p += len;
806 }
807
808 if( *p != end )
809 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
810 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
811
812 return( 0 );
813}
814
Paul Bakker74111d32011-01-15 16:57:55 +0000815static int x509_get_basic_constraints( unsigned char **p,
816 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000817 int *ca_istrue,
818 int *max_pathlen )
819{
Paul Bakker23986e52011-04-24 08:57:21 +0000820 int ret;
821 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000822
823 /*
824 * BasicConstraints ::= SEQUENCE {
825 * cA BOOLEAN DEFAULT FALSE,
826 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
827 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000828 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000829 *max_pathlen = 0; /* endless */
830
831 if( ( ret = asn1_get_tag( p, end, &len,
832 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000834
835 if( *p == end )
836 return 0;
837
Paul Bakker3cccddb2011-01-16 21:46:31 +0000838 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000839 {
840 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000841 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000842
843 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000844 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000845
Paul Bakker3cccddb2011-01-16 21:46:31 +0000846 if( *ca_istrue != 0 )
847 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000848 }
849
850 if( *p == end )
851 return 0;
852
853 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 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( *p != end )
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_LENGTH_MISMATCH );
859
860 (*max_pathlen)++;
861
Paul Bakker74111d32011-01-15 16:57:55 +0000862 return 0;
863}
864
865static int x509_get_ns_cert_type( unsigned char **p,
866 const unsigned char *end,
867 unsigned char *ns_cert_type)
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
875 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 *ns_cert_type = *bs.p;
881 return 0;
882}
883
884static int x509_get_key_usage( unsigned char **p,
885 const unsigned char *end,
886 unsigned char *key_usage)
887{
888 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000889 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000890
891 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000892 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000893
Paul Bakker94a67962012-08-23 13:03:52 +0000894 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000895 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000896 POLARSSL_ERR_ASN1_INVALID_LENGTH );
897
898 /* Get actual bitstring */
899 *key_usage = *bs.p;
900 return 0;
901}
902
Paul Bakkerd98030e2009-05-02 15:13:40 +0000903/*
Paul Bakker74111d32011-01-15 16:57:55 +0000904 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
905 *
906 * KeyPurposeId ::= OBJECT IDENTIFIER
907 */
908static int x509_get_ext_key_usage( unsigned char **p,
909 const unsigned char *end,
910 x509_sequence *ext_key_usage)
911{
912 int ret;
913
914 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000915 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000916
917 /* Sequence length must be >= 1 */
918 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000919 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000920 POLARSSL_ERR_ASN1_INVALID_LENGTH );
921
922 return 0;
923}
924
925/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000926 * SubjectAltName ::= GeneralNames
927 *
928 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
929 *
930 * GeneralName ::= CHOICE {
931 * otherName [0] OtherName,
932 * rfc822Name [1] IA5String,
933 * dNSName [2] IA5String,
934 * x400Address [3] ORAddress,
935 * directoryName [4] Name,
936 * ediPartyName [5] EDIPartyName,
937 * uniformResourceIdentifier [6] IA5String,
938 * iPAddress [7] OCTET STRING,
939 * registeredID [8] OBJECT IDENTIFIER }
940 *
941 * OtherName ::= SEQUENCE {
942 * type-id OBJECT IDENTIFIER,
943 * value [0] EXPLICIT ANY DEFINED BY type-id }
944 *
945 * EDIPartyName ::= SEQUENCE {
946 * nameAssigner [0] DirectoryString OPTIONAL,
947 * partyName [1] DirectoryString }
948 *
949 * NOTE: PolarSSL only parses and uses dNSName at this point.
950 */
951static int x509_get_subject_alt_name( unsigned char **p,
952 const unsigned char *end,
953 x509_sequence *subject_alt_name )
954{
955 int ret;
956 size_t len, tag_len;
957 asn1_buf *buf;
958 unsigned char tag;
959 asn1_sequence *cur = subject_alt_name;
960
961 /* Get main sequence tag */
962 if( ( ret = asn1_get_tag( p, end, &len,
963 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
964 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
965
966 if( *p + len != end )
967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
968 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
969
970 while( *p < end )
971 {
972 if( ( end - *p ) < 1 )
973 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
974 POLARSSL_ERR_ASN1_OUT_OF_DATA );
975
976 tag = **p;
977 (*p)++;
978 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
979 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
980
981 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
982 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
983 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
984
985 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
986 {
987 *p += tag_len;
988 continue;
989 }
990
991 buf = &(cur->buf);
992 buf->tag = tag;
993 buf->p = *p;
994 buf->len = tag_len;
995 *p += buf->len;
996
997 /* Allocate and assign next pointer */
998 if (*p < end)
999 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001000 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001001 sizeof( asn1_sequence ) );
1002
1003 if( cur->next == NULL )
1004 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1005 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1006
Paul Bakker535e97d2012-08-23 10:49:55 +00001007 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001008 cur = cur->next;
1009 }
1010 }
1011
1012 /* Set final sequence entry's next pointer to NULL */
1013 cur->next = NULL;
1014
1015 if( *p != end )
1016 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1017 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1018
1019 return( 0 );
1020}
1021
1022/*
Paul Bakker74111d32011-01-15 16:57:55 +00001023 * X.509 v3 extensions
1024 *
1025 * TODO: Perform all of the basic constraints tests required by the RFC
1026 * TODO: Set values for undetected extensions to a sane default?
1027 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001028 */
1029static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001030 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001031 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001032{
Paul Bakker23986e52011-04-24 08:57:21 +00001033 int ret;
1034 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001035 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001036
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001037 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001038 {
1039 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1040 return( 0 );
1041
1042 return( ret );
1043 }
1044
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 while( *p < end )
1046 {
Paul Bakker74111d32011-01-15 16:57:55 +00001047 /*
1048 * Extension ::= SEQUENCE {
1049 * extnID OBJECT IDENTIFIER,
1050 * critical BOOLEAN DEFAULT FALSE,
1051 * extnValue OCTET STRING }
1052 */
1053 x509_buf extn_oid = {0, 0, NULL};
1054 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001055 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001056
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 if( ( ret = asn1_get_tag( p, end, &len,
1058 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001059 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001061 end_ext_data = *p + len;
1062
Paul Bakker74111d32011-01-15 16:57:55 +00001063 /* Get extension ID */
1064 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Paul Bakker74111d32011-01-15 16:57:55 +00001066 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001067 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001068
Paul Bakker74111d32011-01-15 16:57:55 +00001069 extn_oid.p = *p;
1070 *p += extn_oid.len;
1071
1072 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001073 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001074 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1075
1076 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001077 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001078 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001079 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001082 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001086 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001087
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001088 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001089 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001090 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Paul Bakker74111d32011-01-15 16:57:55 +00001092 /*
1093 * Detect supported extensions
1094 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1096
1097 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001098 {
1099 /* No parser found, skip extension */
1100 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
Paul Bakker5c721f92011-07-27 16:51:09 +00001102#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001103 if( is_critical )
1104 {
1105 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001106 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001107 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1108 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001109#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001110 continue;
1111 }
1112
1113 crt->ext_types |= ext_type;
1114
1115 switch( ext_type )
1116 {
1117 case EXT_BASIC_CONSTRAINTS:
1118 /* Parse basic constraints */
1119 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1120 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1121 return ( ret );
1122 break;
1123
1124 case EXT_KEY_USAGE:
1125 /* Parse key usage */
1126 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1127 &crt->key_usage ) ) != 0 )
1128 return ( ret );
1129 break;
1130
1131 case EXT_EXTENDED_KEY_USAGE:
1132 /* Parse extended key usage */
1133 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1134 &crt->ext_key_usage ) ) != 0 )
1135 return ( ret );
1136 break;
1137
1138 case EXT_SUBJECT_ALT_NAME:
1139 /* Parse subject alt name */
1140 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1141 &crt->subject_alt_names ) ) != 0 )
1142 return ( ret );
1143 break;
1144
1145 case EXT_NS_CERT_TYPE:
1146 /* Parse netscape certificate type */
1147 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1148 &crt->ns_cert_type ) ) != 0 )
1149 return ( ret );
1150 break;
1151
1152 default:
1153 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 }
1156
1157 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001158 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001159 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 return( 0 );
1162}
1163
1164/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001165 * X.509 CRL Entries
1166 */
1167static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001168 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001169 x509_crl_entry *entry )
1170{
Paul Bakker23986e52011-04-24 08:57:21 +00001171 int ret;
1172 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001173 x509_crl_entry *cur_entry = entry;
1174
1175 if( *p == end )
1176 return( 0 );
1177
Paul Bakker9be19372009-07-27 20:21:53 +00001178 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001179 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1180 {
1181 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1182 return( 0 );
1183
1184 return( ret );
1185 }
1186
Paul Bakker9be19372009-07-27 20:21:53 +00001187 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001188
1189 while( *p < end )
1190 {
Paul Bakker23986e52011-04-24 08:57:21 +00001191 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001192 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001193
1194 if( ( ret = asn1_get_tag( p, end, &len2,
1195 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1196 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001197 return( ret );
1198 }
1199
Paul Bakker9be19372009-07-27 20:21:53 +00001200 cur_entry->raw.tag = **p;
1201 cur_entry->raw.p = *p;
1202 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001203 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001204
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001205 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001206 return( ret );
1207
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001208 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001209 return( ret );
1210
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001211 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001212 return( ret );
1213
Paul Bakker74111d32011-01-15 16:57:55 +00001214 if ( *p < end )
1215 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001216 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001217
1218 if( cur_entry->next == NULL )
1219 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1220
Paul Bakkerd98030e2009-05-02 15:13:40 +00001221 cur_entry = cur_entry->next;
1222 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1223 }
1224 }
1225
1226 return( 0 );
1227}
1228
Paul Bakkerc70b9822013-04-07 22:00:46 +02001229static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1230 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001231{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001232 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001233
Paul Bakkerc70b9822013-04-07 22:00:46 +02001234 if( ret != 0 )
1235 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001236
Paul Bakkerc70b9822013-04-07 22:00:46 +02001237 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001238}
1239
Paul Bakkerd98030e2009-05-02 15:13:40 +00001240/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001241 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001243static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1244 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245{
Paul Bakker23986e52011-04-24 08:57:21 +00001246 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001247 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001248 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakker320a4b52009-03-28 18:52:39 +00001250 /*
1251 * Check for valid input
1252 */
1253 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001254 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001255
Paul Bakker6e339b52013-07-03 13:37:05 +02001256 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001257
1258 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001259 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001260
1261 memcpy( p, buf, buflen );
1262
1263 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
1265 crt->raw.p = p;
1266 crt->raw.len = len;
1267 end = p + len;
1268
1269 /*
1270 * Certificate ::= SEQUENCE {
1271 * tbsCertificate TBSCertificate,
1272 * signatureAlgorithm AlgorithmIdentifier,
1273 * signatureValue BIT STRING }
1274 */
1275 if( ( ret = asn1_get_tag( &p, end, &len,
1276 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1277 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001278 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001279 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 }
1281
Paul Bakkerb00ca422012-09-25 12:10:00 +00001282 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001285 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001286 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001288 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001289
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 /*
1291 * TBSCertificate ::= SEQUENCE {
1292 */
1293 crt->tbs.p = p;
1294
1295 if( ( ret = asn1_get_tag( &p, end, &len,
1296 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1297 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001298 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001299 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 }
1301
1302 end = p + len;
1303 crt->tbs.len = end - crt->tbs.p;
1304
1305 /*
1306 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1307 *
1308 * CertificateSerialNumber ::= INTEGER
1309 *
1310 * signature AlgorithmIdentifier
1311 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001312 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1313 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1314 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319
1320 crt->version++;
1321
1322 if( crt->version > 3 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001325 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
Paul Bakkerc70b9822013-04-07 22:00:46 +02001328 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1329 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001331 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001332 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 }
1334
1335 /*
1336 * issuer Name
1337 */
1338 crt->issuer_raw.p = p;
1339
1340 if( ( ret = asn1_get_tag( &p, end, &len,
1341 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001344 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
1346
1347 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1348 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001349 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 return( ret );
1351 }
1352
1353 crt->issuer_raw.len = p - crt->issuer_raw.p;
1354
1355 /*
1356 * Validity ::= SEQUENCE {
1357 * notBefore Time,
1358 * notAfter Time }
1359 *
1360 */
1361 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1362 &crt->valid_to ) ) != 0 )
1363 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001364 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 return( ret );
1366 }
1367
1368 /*
1369 * subject Name
1370 */
1371 crt->subject_raw.p = p;
1372
1373 if( ( ret = asn1_get_tag( &p, end, &len,
1374 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001377 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Paul Bakkercefb3962012-06-27 11:51:09 +00001380 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 return( ret );
1384 }
1385
1386 crt->subject_raw.len = p - crt->subject_raw.p;
1387
1388 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001389 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02001391 if( ( ret = x509_get_pubkey_rsa( &p, end, &crt->rsa ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001393 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 return( ret );
1395 }
1396
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 /*
1398 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1399 * -- If present, version shall be v2 or v3
1400 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1401 * -- If present, version shall be v2 or v3
1402 * extensions [3] EXPLICIT Extensions OPTIONAL
1403 * -- If present, version shall be v3
1404 */
1405 if( crt->version == 2 || crt->version == 3 )
1406 {
1407 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1408 if( ret != 0 )
1409 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001410 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 return( ret );
1412 }
1413 }
1414
1415 if( crt->version == 2 || crt->version == 3 )
1416 {
1417 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1418 if( ret != 0 )
1419 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001420 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 return( ret );
1422 }
1423 }
1424
1425 if( crt->version == 3 )
1426 {
Paul Bakker74111d32011-01-15 16:57:55 +00001427 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 if( ret != 0 )
1429 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001430 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 return( ret );
1432 }
1433 }
1434
1435 if( p != end )
1436 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001437 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001438 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001439 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 }
1441
Paul Bakkerb00ca422012-09-25 12:10:00 +00001442 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
1444 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001445 * }
1446 * -- end of TBSCertificate
1447 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 * signatureAlgorithm AlgorithmIdentifier,
1449 * signatureValue BIT STRING
1450 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001451 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001453 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 return( ret );
1455 }
1456
Paul Bakker535e97d2012-08-23 10:49:55 +00001457 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1458 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001460 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001461 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
1463
1464 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1465 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001466 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 return( ret );
1468 }
1469
1470 if( p != end )
1471 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001472 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001473 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001474 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 }
1476
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477 return( 0 );
1478}
1479
1480/*
Paul Bakker42c65812013-06-24 19:21:59 +02001481 * Parse one X.509 certificate in DER format from a buffer and add them to a
1482 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001483 */
Paul Bakker42c65812013-06-24 19:21:59 +02001484int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001485{
Paul Bakker42c65812013-06-24 19:21:59 +02001486 int ret;
1487 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001488
1489 /*
1490 * Check for valid input
1491 */
1492 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001493 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494
1495 while( crt->version != 0 && crt->next != NULL )
1496 {
1497 prev = crt;
1498 crt = crt->next;
1499 }
1500
1501 /*
1502 * Add new certificate on the end of the chain if needed.
1503 */
1504 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001505 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001506 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001507
Paul Bakker7d06ad22009-05-02 15:53:56 +00001508 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001509 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001510
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001511 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001512 crt = crt->next;
1513 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001514 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Paul Bakker42c65812013-06-24 19:21:59 +02001516 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1517 {
1518 if( prev )
1519 prev->next = NULL;
1520
1521 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001522 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001523
1524 return( ret );
1525 }
1526
1527 return( 0 );
1528}
1529
1530/*
1531 * Parse one or more PEM certificates from a buffer and add them to the chained list
1532 */
1533int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1534{
1535 int ret, success = 0, first_error = 0, total_failed = 0;
1536 int buf_format = X509_FORMAT_DER;
1537
1538 /*
1539 * Check for valid input
1540 */
1541 if( chain == NULL || buf == NULL )
1542 return( POLARSSL_ERR_X509_INVALID_INPUT );
1543
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544 /*
1545 * Determine buffer content. Buffer contains either one DER certificate or
1546 * one or more PEM certificates.
1547 */
1548#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001549 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001550 buf_format = X509_FORMAT_PEM;
1551#endif
1552
1553 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001554 return x509parse_crt_der( chain, buf, buflen );
1555
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001556#if defined(POLARSSL_PEM_C)
1557 if( buf_format == X509_FORMAT_PEM )
1558 {
1559 pem_context pem;
1560
1561 while( buflen > 0 )
1562 {
1563 size_t use_len;
1564 pem_init( &pem );
1565
1566 ret = pem_read_buffer( &pem,
1567 "-----BEGIN CERTIFICATE-----",
1568 "-----END CERTIFICATE-----",
1569 buf, NULL, 0, &use_len );
1570
1571 if( ret == 0 )
1572 {
1573 /*
1574 * Was PEM encoded
1575 */
1576 buflen -= use_len;
1577 buf += use_len;
1578 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001579 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1580 {
1581 return( ret );
1582 }
Paul Bakker00b28602013-06-24 13:02:41 +02001583 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001584 {
1585 pem_free( &pem );
1586
Paul Bakker5ed3b342013-06-24 19:05:46 +02001587 /*
1588 * PEM header and footer were found
1589 */
1590 buflen -= use_len;
1591 buf += use_len;
1592
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001593 if( first_error == 0 )
1594 first_error = ret;
1595
1596 continue;
1597 }
1598 else
1599 break;
1600
Paul Bakker42c65812013-06-24 19:21:59 +02001601 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001602
1603 pem_free( &pem );
1604
1605 if( ret != 0 )
1606 {
1607 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001608 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001609 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001610 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001611 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001612
1613 if( first_error == 0 )
1614 first_error = ret;
1615
Paul Bakker42c65812013-06-24 19:21:59 +02001616 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001617 continue;
1618 }
1619
1620 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001621 }
1622 }
1623#endif
1624
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001625 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001626 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001627 else if( first_error )
1628 return( first_error );
1629 else
1630 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631}
1632
1633/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 * Parse one or more CRLs and add them to the chained list
1635 */
Paul Bakker23986e52011-04-24 08:57:21 +00001636int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637{
Paul Bakker23986e52011-04-24 08:57:21 +00001638 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001639 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001640 unsigned char *p, *end;
1641 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001642#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001643 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001644 pem_context pem;
1645#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001646
1647 crl = chain;
1648
1649 /*
1650 * Check for valid input
1651 */
1652 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001653 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001654
1655 while( crl->version != 0 && crl->next != NULL )
1656 crl = crl->next;
1657
1658 /*
1659 * Add new CRL on the end of the chain if needed.
1660 */
1661 if ( crl->version != 0 && crl->next == NULL)
1662 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001663 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664
Paul Bakker7d06ad22009-05-02 15:53:56 +00001665 if( crl->next == NULL )
1666 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001667 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001668 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001669 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
Paul Bakker7d06ad22009-05-02 15:53:56 +00001671 crl = crl->next;
1672 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001673 }
1674
Paul Bakker96743fc2011-02-12 14:30:57 +00001675#if defined(POLARSSL_PEM_C)
1676 pem_init( &pem );
1677 ret = pem_read_buffer( &pem,
1678 "-----BEGIN X509 CRL-----",
1679 "-----END X509 CRL-----",
1680 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681
Paul Bakker96743fc2011-02-12 14:30:57 +00001682 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001684 /*
1685 * Was PEM encoded
1686 */
1687 buflen -= use_len;
1688 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001689
1690 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001691 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001693 p = pem.buf;
1694 pem.buf = NULL;
1695 len = pem.buflen;
1696 pem_free( &pem );
1697 }
Paul Bakker00b28602013-06-24 13:02:41 +02001698 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001699 {
1700 pem_free( &pem );
1701 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 }
1703 else
1704 {
1705 /*
1706 * nope, copy the raw DER data
1707 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001708 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709
1710 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001711 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712
1713 memcpy( p, buf, buflen );
1714
1715 buflen = 0;
1716 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001717#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001718 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001719
1720 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001721 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001722
1723 memcpy( p, buf, buflen );
1724
1725 buflen = 0;
1726#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727
1728 crl->raw.p = p;
1729 crl->raw.len = len;
1730 end = p + len;
1731
1732 /*
1733 * CertificateList ::= SEQUENCE {
1734 * tbsCertList TBSCertList,
1735 * signatureAlgorithm AlgorithmIdentifier,
1736 * signatureValue BIT STRING }
1737 */
1738 if( ( ret = asn1_get_tag( &p, end, &len,
1739 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1740 {
1741 x509_crl_free( crl );
1742 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1743 }
1744
Paul Bakker23986e52011-04-24 08:57:21 +00001745 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001746 {
1747 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001748 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001749 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1750 }
1751
1752 /*
1753 * TBSCertList ::= SEQUENCE {
1754 */
1755 crl->tbs.p = p;
1756
1757 if( ( ret = asn1_get_tag( &p, end, &len,
1758 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1759 {
1760 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001761 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001762 }
1763
1764 end = p + len;
1765 crl->tbs.len = end - crl->tbs.p;
1766
1767 /*
1768 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1769 * -- if present, MUST be v2
1770 *
1771 * signature AlgorithmIdentifier
1772 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001773 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001774 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 {
1776 x509_crl_free( crl );
1777 return( ret );
1778 }
1779
1780 crl->version++;
1781
1782 if( crl->version > 2 )
1783 {
1784 x509_crl_free( crl );
1785 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1786 }
1787
Paul Bakkerc70b9822013-04-07 22:00:46 +02001788 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1789 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 {
1791 x509_crl_free( crl );
1792 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1793 }
1794
1795 /*
1796 * issuer Name
1797 */
1798 crl->issuer_raw.p = p;
1799
1800 if( ( ret = asn1_get_tag( &p, end, &len,
1801 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1802 {
1803 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001804 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805 }
1806
1807 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1808 {
1809 x509_crl_free( crl );
1810 return( ret );
1811 }
1812
1813 crl->issuer_raw.len = p - crl->issuer_raw.p;
1814
1815 /*
1816 * thisUpdate Time
1817 * nextUpdate Time OPTIONAL
1818 */
Paul Bakker91200182010-02-18 21:26:15 +00001819 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 {
1821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824
Paul Bakker91200182010-02-18 21:26:15 +00001825 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001826 {
Paul Bakker9d781402011-05-09 16:17:09 +00001827 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001828 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001829 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001830 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001831 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001832 x509_crl_free( crl );
1833 return( ret );
1834 }
1835 }
1836
1837 /*
1838 * revokedCertificates SEQUENCE OF SEQUENCE {
1839 * userCertificate CertificateSerialNumber,
1840 * revocationDate Time,
1841 * crlEntryExtensions Extensions OPTIONAL
1842 * -- if present, MUST be v2
1843 * } OPTIONAL
1844 */
1845 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1846 {
1847 x509_crl_free( crl );
1848 return( ret );
1849 }
1850
1851 /*
1852 * crlExtensions EXPLICIT Extensions OPTIONAL
1853 * -- if present, MUST be v2
1854 */
1855 if( crl->version == 2 )
1856 {
1857 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1858
1859 if( ret != 0 )
1860 {
1861 x509_crl_free( crl );
1862 return( ret );
1863 }
1864 }
1865
1866 if( p != end )
1867 {
1868 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001869 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1871 }
1872
1873 end = crl->raw.p + crl->raw.len;
1874
1875 /*
1876 * signatureAlgorithm AlgorithmIdentifier,
1877 * signatureValue BIT STRING
1878 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001879 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 {
1881 x509_crl_free( crl );
1882 return( ret );
1883 }
1884
Paul Bakker535e97d2012-08-23 10:49:55 +00001885 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1886 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001887 {
1888 x509_crl_free( crl );
1889 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1890 }
1891
1892 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1893 {
1894 x509_crl_free( crl );
1895 return( ret );
1896 }
1897
1898 if( p != end )
1899 {
1900 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001901 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1903 }
1904
1905 if( buflen > 0 )
1906 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001907 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908
Paul Bakker7d06ad22009-05-02 15:53:56 +00001909 if( crl->next == NULL )
1910 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001911 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001912 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001913 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914
Paul Bakker7d06ad22009-05-02 15:53:56 +00001915 crl = crl->next;
1916 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001917
1918 return( x509parse_crl( crl, buf, buflen ) );
1919 }
1920
1921 return( 0 );
1922}
1923
Paul Bakker335db3f2011-04-25 15:28:35 +00001924#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001925/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001926 * Load all data from a file into a given buffer.
1927 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001928static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001929{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001930 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001931
Paul Bakkerd98030e2009-05-02 15:13:40 +00001932 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001933 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001934
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 fseek( f, 0, SEEK_END );
1936 *n = (size_t) ftell( f );
1937 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001938
Paul Bakker6e339b52013-07-03 13:37:05 +02001939 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001940 {
1941 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001942 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001943 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001944
Paul Bakkerd98030e2009-05-02 15:13:40 +00001945 if( fread( *buf, 1, *n, f ) != *n )
1946 {
1947 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001948 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001949 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001950 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001951
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001953
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001955
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001957}
1958
1959/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 * Load one or more certificates and add them to the chained list
1961 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001962int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001963{
1964 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 size_t n;
1966 unsigned char *buf;
1967
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001968 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001969 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Paul Bakker69e095c2011-12-10 21:55:01 +00001971 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001974 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 return( ret );
1977}
1978
Paul Bakker8d914582012-06-04 12:46:42 +00001979int x509parse_crtpath( x509_cert *chain, const char *path )
1980{
1981 int ret = 0;
1982#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001983 int w_ret;
1984 WCHAR szDir[MAX_PATH];
1985 char filename[MAX_PATH];
1986 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001987 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001988
Paul Bakker97872ac2012-11-02 12:53:26 +00001989 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001990 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001991
1992 if( len > MAX_PATH - 3 )
1993 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001994
Paul Bakker3338b792012-10-01 21:13:10 +00001995 memset( szDir, 0, sizeof(szDir) );
1996 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001997 memcpy( filename, path, len );
1998 filename[len++] = '\\';
1999 p = filename + len;
2000 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002001
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002002 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002003
Paul Bakker97872ac2012-11-02 12:53:26 +00002004 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002005 if (hFind == INVALID_HANDLE_VALUE)
2006 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2007
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002008 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002009 do
2010 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002011 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002012
Paul Bakkere4791f32012-06-04 21:29:15 +00002013 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002014 continue;
2015
Paul Bakker3338b792012-10-01 21:13:10 +00002016 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2017 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002018 p, len - 1,
2019 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002020
Paul Bakker3338b792012-10-01 21:13:10 +00002021 w_ret = x509parse_crtfile( chain, filename );
2022 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002023 ret++;
2024 else
2025 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002026 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002027 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002028
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002029 if (GetLastError() != ERROR_NO_MORE_FILES)
2030 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002031
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002032cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002033 FindClose( hFind );
2034#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002035 int t_ret, i;
2036 struct stat sb;
2037 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002038 char entry_name[255];
2039 DIR *dir = opendir( path );
2040
2041 if( dir == NULL)
2042 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2043
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002044 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002045 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002046 if( result == NULL )
2047 break;
2048
2049 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2050
2051 i = stat( entry_name, &sb );
2052
2053 if( i == -1 )
2054 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2055
2056 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002057 continue;
2058
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002059 // Ignore parse errors
2060 //
Paul Bakker8d914582012-06-04 12:46:42 +00002061 t_ret = x509parse_crtfile( chain, entry_name );
2062 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002063 ret++;
2064 else
2065 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002066 }
2067 closedir( dir );
2068#endif
2069
2070 return( ret );
2071}
2072
Paul Bakkerd98030e2009-05-02 15:13:40 +00002073/*
2074 * Load one or more CRLs and add them to the chained list
2075 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002076int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002077{
2078 int ret;
2079 size_t n;
2080 unsigned char *buf;
2081
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002082 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002083 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002084
Paul Bakker27fdf462011-06-09 13:55:13 +00002085 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002086
2087 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002088 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002089
2090 return( ret );
2091}
2092
Paul Bakker5121ce52009-01-03 21:22:43 +00002093/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002094 * Load and parse a private RSA key
2095 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002096int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002097{
2098 int ret;
2099 size_t n;
2100 unsigned char *buf;
2101
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002102 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002103 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002104
2105 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002106 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002107 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002108 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002109 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002110
2111 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002112 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002113
2114 return( ret );
2115}
2116
2117/*
2118 * Load and parse a public RSA key
2119 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002120int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002121{
2122 int ret;
2123 size_t n;
2124 unsigned char *buf;
2125
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002126 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002127 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002128
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002129 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002130
2131 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002132 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002133
2134 return( ret );
2135}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002136
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002137/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002138 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002139 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002140int x509parse_keyfile( pk_context *ctx,
2141 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002142{
2143 int ret;
2144 size_t n;
2145 unsigned char *buf;
2146
2147 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2148 return( ret );
2149
2150 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002151 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002152 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002153 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002154 (const unsigned char *) pwd, strlen( pwd ) );
2155
2156 memset( buf, 0, n + 1 );
2157 free( buf );
2158
2159 return( ret );
2160}
2161
2162/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002163 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002164 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002165int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002166{
2167 int ret;
2168 size_t n;
2169 unsigned char *buf;
2170
2171 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2172 return( ret );
2173
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002174 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002175
2176 memset( buf, 0, n + 1 );
2177 free( buf );
2178
2179 return( ret );
2180}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002181
Paul Bakker335db3f2011-04-25 15:28:35 +00002182#endif /* POLARSSL_FS_IO */
2183
2184/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002185 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002186 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002187static int x509parse_key_pkcs1_der( rsa_context *rsa,
2188 const unsigned char *key,
2189 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002190{
Paul Bakker23986e52011-04-24 08:57:21 +00002191 int ret;
2192 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002194
Paul Bakker96743fc2011-02-12 14:30:57 +00002195 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002196 end = p + keylen;
2197
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002199 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002200 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 * RSAPrivateKey ::= SEQUENCE {
2202 * version Version,
2203 * modulus INTEGER, -- n
2204 * publicExponent INTEGER, -- e
2205 * privateExponent INTEGER, -- d
2206 * prime1 INTEGER, -- p
2207 * prime2 INTEGER, -- q
2208 * exponent1 INTEGER, -- d mod (p-1)
2209 * exponent2 INTEGER, -- d mod (q-1)
2210 * coefficient INTEGER, -- (inverse of q) mod p
2211 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2212 * }
2213 */
2214 if( ( ret = asn1_get_tag( &p, end, &len,
2215 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2216 {
Paul Bakker9d781402011-05-09 16:17:09 +00002217 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
2219
2220 end = p + len;
2221
2222 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2223 {
Paul Bakker9d781402011-05-09 16:17:09 +00002224 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
2227 if( rsa->ver != 0 )
2228 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002229 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 }
2231
2232 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2233 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2234 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2235 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2236 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2237 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2238 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2239 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2240 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002242 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244
2245 rsa->len = mpi_size( &rsa->N );
2246
2247 if( p != end )
2248 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002250 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002251 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
2254 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2255 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 rsa_free( rsa );
2257 return( ret );
2258 }
2259
Paul Bakkere2f50402013-06-24 19:00:59 +02002260 return( 0 );
2261}
2262
2263/*
2264 * Parse an unencrypted PKCS#8 encoded private RSA key
2265 */
2266static int x509parse_key_pkcs8_unencrypted_der(
2267 rsa_context *rsa,
2268 const unsigned char *key,
2269 size_t keylen )
2270{
2271 int ret;
2272 size_t len;
2273 unsigned char *p, *end;
2274 x509_buf pk_alg_oid;
2275 pk_type_t pk_alg = POLARSSL_PK_NONE;
2276
2277 p = (unsigned char *) key;
2278 end = p + keylen;
2279
2280 /*
2281 * This function parses the PrivatKeyInfo object (PKCS#8)
2282 *
2283 * PrivateKeyInfo ::= SEQUENCE {
2284 * version Version,
2285 * algorithm AlgorithmIdentifier,
2286 * PrivateKey BIT STRING
2287 * }
2288 *
2289 * AlgorithmIdentifier ::= SEQUENCE {
2290 * algorithm OBJECT IDENTIFIER,
2291 * parameters ANY DEFINED BY algorithm OPTIONAL
2292 * }
2293 *
2294 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2295 */
2296 if( ( ret = asn1_get_tag( &p, end, &len,
2297 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2298 {
2299 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2300 }
2301
2302 end = p + len;
2303
2304 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2305 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2306
2307 if( rsa->ver != 0 )
2308 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2309
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002310 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002311 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2312
2313 /*
2314 * only RSA keys handled at this time
2315 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002316 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002317 {
Paul Bakkere2f50402013-06-24 19:00:59 +02002318 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002319 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002320
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002321 if (pk_alg != POLARSSL_PK_RSA )
2322 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2323
Paul Bakkere2f50402013-06-24 19:00:59 +02002324 /*
2325 * Get the OCTET STRING and parse the PKCS#1 format inside
2326 */
2327 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2328 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2329
2330 if( ( end - p ) < 1 )
2331 {
2332 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2333 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2334 }
2335
2336 end = p + len;
2337
2338 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2339 return( ret );
2340
2341 return( 0 );
2342}
2343
2344/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002345 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002346 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002347static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2348 size_t *used_len,
2349 const unsigned char *key, size_t keylen,
2350 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002351{
2352 int ret;
2353 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002354 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002355 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002356#if defined(POLARSSL_PKCS12_C)
2357 cipher_type_t cipher_alg;
2358 md_type_t md_alg;
2359#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002360
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002361 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002362
2363 p = (unsigned char *) key;
2364 end = p + keylen;
2365
Paul Bakker28144de2013-06-24 19:28:55 +02002366 if( pwdlen == 0 )
2367 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2368
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002369 /*
2370 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2371 *
2372 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2373 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2374 * encryptedData EncryptedData
2375 * }
2376 *
2377 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2378 *
2379 * EncryptedData ::= OCTET STRING
2380 *
2381 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2382 */
2383 if( ( ret = asn1_get_tag( &p, end, &len,
2384 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2385 {
2386 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2387 }
2388
2389 end = p + len;
2390
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002391 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002392 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002393
2394 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2396
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002397 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002398 return( POLARSSL_ERR_X509_INVALID_INPUT );
2399
2400 /*
2401 * Decrypt EncryptedData with appropriate PDE
2402 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002403#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002404 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002405 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002406 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002407 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002408 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002409 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002410 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2411 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2412
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002413 return( ret );
2414 }
2415 }
2416 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2417 {
2418 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2419 PKCS12_PBE_DECRYPT,
2420 pwd, pwdlen,
2421 p, len, buf ) ) != 0 )
2422 {
2423 return( ret );
2424 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002425
2426 // Best guess for password mismatch when using RC4. If first tag is
2427 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2428 //
2429 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2430 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002431 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002432 else
2433#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002434#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002435 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002436 {
2437 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2438 p, len, buf ) ) != 0 )
2439 {
2440 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2441 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2442
2443 return( ret );
2444 }
2445 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002446 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002447#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002448 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2449
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002450 *used_len = len;
2451 return( 0 );
2452}
2453
2454/*
2455 * Parse an encrypted PKCS#8 encoded private RSA key
2456 */
2457static int x509parse_key_pkcs8_encrypted_der(
2458 rsa_context *rsa,
2459 const unsigned char *key, size_t keylen,
2460 const unsigned char *pwd, size_t pwdlen )
2461{
2462 int ret;
2463 unsigned char buf[2048];
2464 size_t len = 0;
2465
2466 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2467 key, keylen, pwd, pwdlen ) ) != 0 )
2468 {
2469 return( ret );
2470 }
2471
2472 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002473}
2474
2475/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002476 * Parse a private RSA key
2477 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002478int x509parse_key_rsa( rsa_context *rsa,
2479 const unsigned char *key, size_t keylen,
2480 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002481{
2482 int ret;
2483
Paul Bakker96743fc2011-02-12 14:30:57 +00002484#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002485 size_t len;
2486 pem_context pem;
2487
2488 pem_init( &pem );
2489 ret = pem_read_buffer( &pem,
2490 "-----BEGIN RSA PRIVATE KEY-----",
2491 "-----END RSA PRIVATE KEY-----",
2492 key, pwd, pwdlen, &len );
2493 if( ret == 0 )
2494 {
2495 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2496 {
2497 rsa_free( rsa );
2498 }
2499
2500 pem_free( &pem );
2501 return( ret );
2502 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002503 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2504 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2505 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2506 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002507 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002508 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002509
2510 ret = pem_read_buffer( &pem,
2511 "-----BEGIN PRIVATE KEY-----",
2512 "-----END PRIVATE KEY-----",
2513 key, NULL, 0, &len );
2514 if( ret == 0 )
2515 {
2516 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2517 pem.buf, pem.buflen ) ) != 0 )
2518 {
2519 rsa_free( rsa );
2520 }
2521
2522 pem_free( &pem );
2523 return( ret );
2524 }
2525 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002526 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002527
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002528 ret = pem_read_buffer( &pem,
2529 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2530 "-----END ENCRYPTED PRIVATE KEY-----",
2531 key, NULL, 0, &len );
2532 if( ret == 0 )
2533 {
2534 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2535 pem.buf, pem.buflen,
2536 pwd, pwdlen ) ) != 0 )
2537 {
2538 rsa_free( rsa );
2539 }
2540
2541 pem_free( &pem );
2542 return( ret );
2543 }
2544 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002545 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002546#else
2547 ((void) pwd);
2548 ((void) pwdlen);
2549#endif /* POLARSSL_PEM_C */
2550
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002551 /*
2552 * At this point we only know it's not a PEM formatted key. Could be any
2553 * of the known DER encoded private key formats
2554 *
2555 * We try the different DER format parsers to see if one passes without
2556 * error
2557 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002558 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2559 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002560 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002561 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002564 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002565
2566 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2567 {
2568 return( ret );
2569 }
2570
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002571 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2572 return( 0 );
2573
2574 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002575
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002576 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2577 return( 0 );
2578
2579 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002580
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002581 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582}
2583
2584/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002585 * Parse a public RSA key
2586 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002587int x509parse_public_key_rsa( rsa_context *rsa,
2588 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002589{
Paul Bakker23986e52011-04-24 08:57:21 +00002590 int ret;
2591 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002592 unsigned char *p, *end;
Paul Bakker53019ae2011-03-25 13:58:48 +00002593#if defined(POLARSSL_PEM_C)
2594 pem_context pem;
2595
2596 pem_init( &pem );
2597 ret = pem_read_buffer( &pem,
2598 "-----BEGIN PUBLIC KEY-----",
2599 "-----END PUBLIC KEY-----",
2600 key, NULL, 0, &len );
2601
2602 if( ret == 0 )
2603 {
2604 /*
2605 * Was PEM encoded
2606 */
2607 keylen = pem.buflen;
2608 }
Paul Bakker00b28602013-06-24 13:02:41 +02002609 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002610 {
2611 pem_free( &pem );
2612 return( ret );
2613 }
2614
2615 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2616#else
2617 p = (unsigned char *) key;
2618#endif
2619 end = p + keylen;
2620
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02002621 if( ( ret = x509_get_pubkey_rsa( &p, end, rsa ) ) != 0 )
Paul Bakker53019ae2011-03-25 13:58:48 +00002622 {
2623#if defined(POLARSSL_PEM_C)
2624 pem_free( &pem );
2625#endif
2626 rsa_free( rsa );
2627 return( ret );
2628 }
2629
Paul Bakker53019ae2011-03-25 13:58:48 +00002630#if defined(POLARSSL_PEM_C)
2631 pem_free( &pem );
2632#endif
2633
2634 return( 0 );
2635}
2636
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002637#if defined(POLARSSL_ECP_C)
2638/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002639 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002640 */
2641static int x509parse_key_sec1_der( ecp_keypair *eck,
2642 const unsigned char *key,
2643 size_t keylen )
2644{
2645 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002646 int version;
2647 size_t len;
2648 ecp_group_id grp_id;
2649 unsigned char *p = (unsigned char *) key;
2650 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002651
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002652 /*
2653 * RFC 5915, orf SEC1 Appendix C.4
2654 *
2655 * ECPrivateKey ::= SEQUENCE {
2656 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2657 * privateKey OCTET STRING,
2658 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2659 * publicKey [1] BIT STRING OPTIONAL
2660 * }
2661 */
2662 if( ( ret = asn1_get_tag( &p, end, &len,
2663 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2664 {
2665 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2666 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002667
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002668 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002669
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002670 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2671 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002672
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002673 if( version != 1 )
2674 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2675
2676 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2677 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2678
2679 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2680 {
2681 ecp_keypair_free( eck );
2682 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2683 }
2684
2685 p += len;
2686
2687 /*
2688 * Is 'parameters' present?
2689 */
2690 if( ( ret = asn1_get_tag( &p, end, &len,
2691 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2692 {
2693 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2694 return( ret );
2695
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002696 /*
2697 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2698 * defined externally. In this case, make sure both definitions match.
2699 */
2700 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002701 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002702 if( eck->grp.id != grp_id )
2703 {
2704 ecp_keypair_free( eck );
2705 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2706 }
2707 }
2708 else
2709 {
2710 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2711 {
2712 ecp_keypair_free( eck );
2713 return( ret );
2714 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002715 }
2716 }
2717 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2718 {
2719 ecp_keypair_free( eck );
2720 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2721 }
2722
2723 /*
2724 * Is 'publickey' present?
2725 */
2726 if( ( ret = asn1_get_tag( &p, end, &len,
2727 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2728 {
2729 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2730 != 0 )
2731 {
2732 ecp_keypair_free( eck );
2733 return( ret );
2734 }
2735
2736 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2737 {
2738 ecp_keypair_free( eck );
2739 return( ret );
2740 }
2741 }
2742 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2743 {
2744 ecp_keypair_free( eck );
2745 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2746 }
2747
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002748 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002749 {
2750 ecp_keypair_free( eck );
2751 return( ret );
2752 }
2753
2754 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002755}
2756
2757/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002758 * Parse an unencrypted PKCS#8 encoded private EC key
2759 */
2760static int x509parse_key_pkcs8_unencrypted_der_ec(
2761 ecp_keypair *eck,
2762 const unsigned char* key,
2763 size_t keylen )
2764{
2765 int ret, version;
2766 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002767 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002768 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002769 unsigned char *p = (unsigned char *) key;
2770 unsigned char *end = p + keylen;
2771 pk_type_t pk_alg = POLARSSL_PK_NONE;
2772
2773 /*
2774 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2775 *
2776 * PrivateKeyInfo ::= SEQUENCE {
2777 * version Version,
2778 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2779 * privateKey PrivateKey,
2780 * attributes [0] IMPLICIT Attributes OPTIONAL }
2781 *
2782 * Version ::= INTEGER
2783 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2784 * PrivateKey ::= OCTET STRING
2785 *
2786 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2787 */
2788
2789 if( ( ret = asn1_get_tag( &p, end, &len,
2790 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2791 {
2792 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2793 }
2794
2795 end = p + len;
2796
2797 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2798 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2799
2800 if( version != 0 )
2801 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2802
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002803 if( ( ret = x509_get_algid( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002804 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2805
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002806 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002807 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002808
2809 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2810 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2811
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002812 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002813 {
2814 ecp_keypair_free( eck );
2815 return( ret );
2816 }
2817
2818 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2819 {
2820 ecp_keypair_free( eck );
2821 return( ret );
2822 }
2823
2824 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2825 {
2826 ecp_keypair_free( eck );
2827 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2828 }
2829
2830 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2831 {
2832 ecp_keypair_free( eck );
2833 return( ret );
2834 }
2835
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002836 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002837 {
2838 ecp_keypair_free( eck );
2839 return( ret );
2840 }
2841
2842 return 0;
2843}
2844
2845/*
2846 * Parse an encrypted PKCS#8 encoded private EC key
2847 */
2848static int x509parse_key_pkcs8_encrypted_der_ec(
2849 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002850 const unsigned char *key, size_t keylen,
2851 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002852{
2853 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002854 unsigned char buf[2048];
2855 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002856
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002857 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2858 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002859 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002860 return( ret );
2861 }
2862
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002863 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002864}
2865
2866/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002867 * Parse a private EC key
2868 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002869static int x509parse_key_ec( ecp_keypair *eck,
2870 const unsigned char *key, size_t keylen,
2871 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002872{
2873 int ret;
2874
2875#if defined(POLARSSL_PEM_C)
2876 size_t len;
2877 pem_context pem;
2878
2879 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002880 ret = pem_read_buffer( &pem,
2881 "-----BEGIN EC PRIVATE KEY-----",
2882 "-----END EC PRIVATE KEY-----",
2883 key, pwd, pwdlen, &len );
2884 if( ret == 0 )
2885 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002886 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002887 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002888 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002889 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002890
2891 pem_free( &pem );
2892 return( ret );
2893 }
2894 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2895 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2896 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2897 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2898 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2899 return( ret );
2900
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002901 ret = pem_read_buffer( &pem,
2902 "-----BEGIN PRIVATE KEY-----",
2903 "-----END PRIVATE KEY-----",
2904 key, NULL, 0, &len );
2905 if( ret == 0 )
2906 {
2907 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2908 pem.buf, pem.buflen ) ) != 0 )
2909 {
2910 ecp_keypair_free( eck );
2911 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002912
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002913 pem_free( &pem );
2914 return( ret );
2915 }
2916 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2917 return( ret );
2918
2919 ret = pem_read_buffer( &pem,
2920 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2921 "-----END ENCRYPTED PRIVATE KEY-----",
2922 key, NULL, 0, &len );
2923 if( ret == 0 )
2924 {
2925 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2926 pem.buf, pem.buflen,
2927 pwd, pwdlen ) ) != 0 )
2928 {
2929 ecp_keypair_free( eck );
2930 }
2931
2932 pem_free( &pem );
2933 return( ret );
2934 }
2935 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2936 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002937#else
2938 ((void) pwd);
2939 ((void) pwdlen);
2940#endif /* POLARSSL_PEM_C */
2941
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002942 /*
2943 * At this point we only know it's not a PEM formatted key. Could be any
2944 * of the known DER encoded private key formats
2945 *
2946 * We try the different DER format parsers to see if one passes without
2947 * error
2948 */
2949 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2950 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002951 {
2952 return( 0 );
2953 }
2954
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002955 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002956
2957 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2958 {
2959 return( ret );
2960 }
2961
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002962 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2963 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002964 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002965
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002966 ecp_keypair_free( eck );
2967
2968 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2969 return( 0 );
2970
2971 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002972
2973 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2974}
2975
2976/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002977 * Parse a public EC key in RFC 5480 format, der-encoded
2978 */
2979static int x509parse_public_key_ec_der( ecp_keypair *key,
2980 const unsigned char *buf, size_t len )
2981{
2982 int ret;
2983 ecp_group_id grp_id;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002984 x509_buf alg_params;
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002985 pk_type_t alg = POLARSSL_PK_NONE;
2986 unsigned char *p = (unsigned char *) buf;
2987 unsigned char *end = p + len;
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02002988 /*
2989 * SubjectPublicKeyInfo ::= SEQUENCE {
2990 * algorithm AlgorithmIdentifier,
2991 * subjectPublicKey BIT STRING
2992 * }
2993 * -- algorithm parameters are ECParameters
2994 * -- subjectPublicKey is an ECPoint
2995 */
2996 if( ( ret = asn1_get_tag( &p, end, &len,
2997 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2998 {
2999 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
3000 }
3001
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02003002 if( ( ret = x509_get_algid( &p, end, &alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003003 return( ret );
3004
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003005 if( alg != POLARSSL_PK_ECKEY && alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003006 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003007
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02003008 if( alg == POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003009 key->alg = POLARSSL_ECP_KEY_ALG_ECDH;
3010
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02003011 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003012 return( ret );
3013
3014 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
3015 return( ret );
3016
3017 if( ( ret = x509_get_subpubkey_ec( &p, end, &key->grp, &key->Q ) ) != 0 )
3018 {
3019 return( ret );
3020 }
3021
3022 return( 0 );
3023}
3024
3025/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003026 * Parse a public EC key
3027 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003028static int x509parse_public_key_ec( ecp_keypair *eckey,
3029 const unsigned char *key, size_t keylen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003030{
3031 int ret;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003032#if defined(POLARSSL_PEM_C)
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003033 size_t len;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003034 pem_context pem;
3035
3036 pem_init( &pem );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003037 ret = pem_read_buffer( &pem,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003038 "-----BEGIN PUBLIC KEY-----",
3039 "-----END PUBLIC KEY-----",
3040 key, NULL, 0, &len );
3041
3042 if( ret == 0 )
3043 {
3044 /*
3045 * Was PEM encoded
3046 */
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003047 key = pem.buf;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003048 keylen = pem.buflen;
3049 }
3050 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3051 {
3052 pem_free( &pem );
3053 return( ret );
3054 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003055#endif
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003056
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003057 if( ( ret = x509parse_public_key_ec_der ( eckey, key, keylen ) ) != 0 ||
3058 ( ret = ecp_check_pubkey( &eckey->grp, &eckey->Q ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003059 {
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003060 ecp_keypair_free( eckey );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003061 }
3062
3063#if defined(POLARSSL_PEM_C)
3064 pem_free( &pem );
3065#endif
3066
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +02003067 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02003068}
3069#endif /* defined(POLARSSL_ECP_C) */
3070
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003071/*
3072 * Parse a private key
3073 */
3074int x509parse_key( pk_context *ctx,
3075 const unsigned char *key, size_t keylen,
3076 const unsigned char *pwd, size_t pwdlen )
3077{
3078 int ret;
3079
3080 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
3081 return( ret );
3082
3083 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
3084 == 0 )
3085 {
3086 return( 0 );
3087 }
3088
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003089 pk_free( ctx );
3090
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003091 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
3092 return( ret );
3093
3094 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
3095 {
3096 return( 0 );
3097 }
3098
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003099 pk_free( ctx );
3100
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003101 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
3102}
3103
3104/*
3105 * Parse a public key
3106 */
3107int x509parse_public_key( pk_context *ctx,
3108 const unsigned char *key, size_t keylen )
3109{
3110 int ret;
3111
3112 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
3113 return( ret );
3114
3115 if( ( ret = x509parse_public_key_rsa( ctx->data, key, keylen ) ) == 0 )
3116 return( 0 );
3117
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003118 pk_free( ctx );
3119
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003120 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
3121 return( ret );
3122
3123 if( ( ret = x509parse_public_key_ec( ctx->data, key, keylen ) ) == 0 )
3124 return( 0 );
3125
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003126 pk_free( ctx );
3127
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003128 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
3129}
3130
Paul Bakkereaa89f82011-04-04 21:36:15 +00003131#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003132/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003133 * Parse DHM parameters
3134 */
Paul Bakker23986e52011-04-24 08:57:21 +00003135int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003136{
Paul Bakker23986e52011-04-24 08:57:21 +00003137 int ret;
3138 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003139 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003140#if defined(POLARSSL_PEM_C)
3141 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003142
Paul Bakker96743fc2011-02-12 14:30:57 +00003143 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003144
Paul Bakker96743fc2011-02-12 14:30:57 +00003145 ret = pem_read_buffer( &pem,
3146 "-----BEGIN DH PARAMETERS-----",
3147 "-----END DH PARAMETERS-----",
3148 dhmin, NULL, 0, &dhminlen );
3149
3150 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003151 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003152 /*
3153 * Was PEM encoded
3154 */
3155 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003156 }
Paul Bakker00b28602013-06-24 13:02:41 +02003157 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003158 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003159 pem_free( &pem );
3160 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003161 }
3162
Paul Bakker96743fc2011-02-12 14:30:57 +00003163 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3164#else
3165 p = (unsigned char *) dhmin;
3166#endif
3167 end = p + dhminlen;
3168
Paul Bakker1b57b062011-01-06 15:48:19 +00003169 memset( dhm, 0, sizeof( dhm_context ) );
3170
Paul Bakker1b57b062011-01-06 15:48:19 +00003171 /*
3172 * DHParams ::= SEQUENCE {
3173 * prime INTEGER, -- P
3174 * generator INTEGER, -- g
3175 * }
3176 */
3177 if( ( ret = asn1_get_tag( &p, end, &len,
3178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3179 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003180#if defined(POLARSSL_PEM_C)
3181 pem_free( &pem );
3182#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003183 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003184 }
3185
3186 end = p + len;
3187
3188 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3189 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3190 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003191#if defined(POLARSSL_PEM_C)
3192 pem_free( &pem );
3193#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003194 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003195 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003196 }
3197
3198 if( p != end )
3199 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003200#if defined(POLARSSL_PEM_C)
3201 pem_free( &pem );
3202#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003203 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003204 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003205 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3206 }
3207
Paul Bakker96743fc2011-02-12 14:30:57 +00003208#if defined(POLARSSL_PEM_C)
3209 pem_free( &pem );
3210#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003211
3212 return( 0 );
3213}
3214
Paul Bakker335db3f2011-04-25 15:28:35 +00003215#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003216/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003217 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003218 */
3219int x509parse_dhmfile( dhm_context *dhm, const char *path )
3220{
3221 int ret;
3222 size_t n;
3223 unsigned char *buf;
3224
Paul Bakker69e095c2011-12-10 21:55:01 +00003225 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3226 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003227
Paul Bakker27fdf462011-06-09 13:55:13 +00003228 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003229
3230 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003231 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003232
3233 return( ret );
3234}
Paul Bakker335db3f2011-04-25 15:28:35 +00003235#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003236#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003237
Paul Bakker5121ce52009-01-03 21:22:43 +00003238#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003239#include <stdarg.h>
3240
3241#if !defined vsnprintf
3242#define vsnprintf _vsnprintf
3243#endif // vsnprintf
3244
3245/*
3246 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3247 * Result value is not size of buffer needed, but -1 if no fit is possible.
3248 *
3249 * This fuction tries to 'fix' this by at least suggesting enlarging the
3250 * size by 20.
3251 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003252static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003253{
3254 va_list ap;
3255 int res = -1;
3256
3257 va_start( ap, format );
3258
3259 res = vsnprintf( str, size, format, ap );
3260
3261 va_end( ap );
3262
3263 // No quick fix possible
3264 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003265 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003266
3267 return res;
3268}
3269
3270#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003271#endif
3272
Paul Bakkerd98030e2009-05-02 15:13:40 +00003273#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3274
3275#define SAFE_SNPRINTF() \
3276{ \
3277 if( ret == -1 ) \
3278 return( -1 ); \
3279 \
Paul Bakker23986e52011-04-24 08:57:21 +00003280 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003281 p[n - 1] = '\0'; \
3282 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3283 } \
3284 \
Paul Bakker23986e52011-04-24 08:57:21 +00003285 n -= (unsigned int) ret; \
3286 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003287}
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289/*
3290 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003291 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003293int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294{
Paul Bakker23986e52011-04-24 08:57:21 +00003295 int ret;
3296 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003298 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003299 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 char s[128], *p;
3301
3302 memset( s, 0, sizeof( s ) );
3303
3304 name = dn;
3305 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003306 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 while( name != NULL )
3309 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003310 if( !name->oid.p )
3311 {
3312 name = name->next;
3313 continue;
3314 }
3315
Paul Bakker74111d32011-01-15 16:57:55 +00003316 if( name != dn )
3317 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003318 ret = snprintf( p, n, ", " );
3319 SAFE_SNPRINTF();
3320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003321
Paul Bakkerc70b9822013-04-07 22:00:46 +02003322 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Paul Bakkerc70b9822013-04-07 22:00:46 +02003324 if( ret == 0 )
3325 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003327 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003328 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003329
3330 for( i = 0; i < name->val.len; i++ )
3331 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003332 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 break;
3334
3335 c = name->val.p[i];
3336 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3337 s[i] = '?';
3338 else s[i] = c;
3339 }
3340 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003341 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003342 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 name = name->next;
3344 }
3345
Paul Bakker23986e52011-04-24 08:57:21 +00003346 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347}
3348
3349/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003350 * Store the serial in printable form into buf; no more
3351 * than size characters will be written
3352 */
3353int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3354{
Paul Bakker23986e52011-04-24 08:57:21 +00003355 int ret;
3356 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003357 char *p;
3358
3359 p = buf;
3360 n = size;
3361
3362 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003363 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003364
3365 for( i = 0; i < nr; i++ )
3366 {
Paul Bakker93048802011-12-05 14:38:06 +00003367 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003368 continue;
3369
Paul Bakkerdd476992011-01-16 21:34:59 +00003370 ret = snprintf( p, n, "%02X%s",
3371 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3372 SAFE_SNPRINTF();
3373 }
3374
Paul Bakker03c7c252011-11-25 12:37:37 +00003375 if( nr != serial->len )
3376 {
3377 ret = snprintf( p, n, "...." );
3378 SAFE_SNPRINTF();
3379 }
3380
Paul Bakker23986e52011-04-24 08:57:21 +00003381 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003382}
3383
3384/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003385 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003387int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3388 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389{
Paul Bakker23986e52011-04-24 08:57:21 +00003390 int ret;
3391 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003392 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003393 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
3395 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003396 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Paul Bakkerd98030e2009-05-02 15:13:40 +00003398 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003400 SAFE_SNPRINTF();
3401 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003403 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
Paul Bakkerdd476992011-01-16 21:34:59 +00003405 ret = x509parse_serial_gets( p, n, &crt->serial);
3406 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Paul Bakkerd98030e2009-05-02 15:13:40 +00003408 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3409 SAFE_SNPRINTF();
3410 ret = x509parse_dn_gets( p, n, &crt->issuer );
3411 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
Paul Bakkerd98030e2009-05-02 15:13:40 +00003413 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3414 SAFE_SNPRINTF();
3415 ret = x509parse_dn_gets( p, n, &crt->subject );
3416 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003417
Paul Bakkerd98030e2009-05-02 15:13:40 +00003418 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3420 crt->valid_from.year, crt->valid_from.mon,
3421 crt->valid_from.day, crt->valid_from.hour,
3422 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003423 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
Paul Bakkerd98030e2009-05-02 15:13:40 +00003425 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3427 crt->valid_to.year, crt->valid_to.mon,
3428 crt->valid_to.day, crt->valid_to.hour,
3429 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003430 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
Paul Bakkerc70b9822013-04-07 22:00:46 +02003432 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003433 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Paul Bakkerc70b9822013-04-07 22:00:46 +02003435 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3436 if( ret != 0 )
3437 ret = snprintf( p, n, "???" );
3438 else
3439 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003440 SAFE_SNPRINTF();
3441
3442 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003443 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003444 SAFE_SNPRINTF();
3445
Paul Bakker23986e52011-04-24 08:57:21 +00003446 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003447}
3448
Paul Bakker74111d32011-01-15 16:57:55 +00003449/*
3450 * Return an informational string describing the given OID
3451 */
3452const char *x509_oid_get_description( x509_buf *oid )
3453{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003454 const char *desc = NULL;
3455 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003456
Paul Bakkerc70b9822013-04-07 22:00:46 +02003457 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003458
Paul Bakkerc70b9822013-04-07 22:00:46 +02003459 if( ret != 0 )
3460 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003461
Paul Bakkerc70b9822013-04-07 22:00:46 +02003462 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003463}
3464
3465/* Return the x.y.z.... style numeric string for the given OID */
3466int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3467{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003468 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003469}
3470
Paul Bakkerd98030e2009-05-02 15:13:40 +00003471/*
3472 * Return an informational string about the CRL.
3473 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003474int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3475 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003476{
Paul Bakker23986e52011-04-24 08:57:21 +00003477 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003478 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003479 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003480 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003481 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003482
3483 p = buf;
3484 n = size;
3485
3486 ret = snprintf( p, n, "%sCRL version : %d",
3487 prefix, crl->version );
3488 SAFE_SNPRINTF();
3489
3490 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3491 SAFE_SNPRINTF();
3492 ret = x509parse_dn_gets( p, n, &crl->issuer );
3493 SAFE_SNPRINTF();
3494
3495 ret = snprintf( p, n, "\n%sthis update : " \
3496 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3497 crl->this_update.year, crl->this_update.mon,
3498 crl->this_update.day, crl->this_update.hour,
3499 crl->this_update.min, crl->this_update.sec );
3500 SAFE_SNPRINTF();
3501
3502 ret = snprintf( p, n, "\n%snext update : " \
3503 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3504 crl->next_update.year, crl->next_update.mon,
3505 crl->next_update.day, crl->next_update.hour,
3506 crl->next_update.min, crl->next_update.sec );
3507 SAFE_SNPRINTF();
3508
3509 entry = &crl->entry;
3510
3511 ret = snprintf( p, n, "\n%sRevoked certificates:",
3512 prefix );
3513 SAFE_SNPRINTF();
3514
Paul Bakker9be19372009-07-27 20:21:53 +00003515 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003516 {
3517 ret = snprintf( p, n, "\n%sserial number: ",
3518 prefix );
3519 SAFE_SNPRINTF();
3520
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003521 ret = x509parse_serial_gets( p, n, &entry->serial);
3522 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003523
Paul Bakkerd98030e2009-05-02 15:13:40 +00003524 ret = snprintf( p, n, " revocation date: " \
3525 "%04d-%02d-%02d %02d:%02d:%02d",
3526 entry->revocation_date.year, entry->revocation_date.mon,
3527 entry->revocation_date.day, entry->revocation_date.hour,
3528 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003529 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003530
3531 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 }
3533
Paul Bakkerc70b9822013-04-07 22:00:46 +02003534 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003535 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003536
Paul Bakkerc70b9822013-04-07 22:00:46 +02003537 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3538 if( ret != 0 )
3539 ret = snprintf( p, n, "???" );
3540 else
3541 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003542 SAFE_SNPRINTF();
3543
Paul Bakker1e27bb22009-07-19 20:25:25 +00003544 ret = snprintf( p, n, "\n" );
3545 SAFE_SNPRINTF();
3546
Paul Bakker23986e52011-04-24 08:57:21 +00003547 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548}
3549
3550/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003551 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003553#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003554int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555{
Paul Bakkercce9d772011-11-18 14:26:47 +00003556 int year, mon, day;
3557 int hour, min, sec;
3558
3559#if defined(_WIN32)
3560 SYSTEMTIME st;
3561
3562 GetLocalTime(&st);
3563
3564 year = st.wYear;
3565 mon = st.wMonth;
3566 day = st.wDay;
3567 hour = st.wHour;
3568 min = st.wMinute;
3569 sec = st.wSecond;
3570#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 struct tm *lt;
3572 time_t tt;
3573
3574 tt = time( NULL );
3575 lt = localtime( &tt );
3576
Paul Bakkercce9d772011-11-18 14:26:47 +00003577 year = lt->tm_year + 1900;
3578 mon = lt->tm_mon + 1;
3579 day = lt->tm_mday;
3580 hour = lt->tm_hour;
3581 min = lt->tm_min;
3582 sec = lt->tm_sec;
3583#endif
3584
3585 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003586 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Paul Bakkercce9d772011-11-18 14:26:47 +00003588 if( year == to->year &&
3589 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003590 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591
Paul Bakkercce9d772011-11-18 14:26:47 +00003592 if( year == to->year &&
3593 mon == to->mon &&
3594 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003595 return( 1 );
3596
Paul Bakkercce9d772011-11-18 14:26:47 +00003597 if( year == to->year &&
3598 mon == to->mon &&
3599 day == to->day &&
3600 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003601 return( 1 );
3602
Paul Bakkercce9d772011-11-18 14:26:47 +00003603 if( year == to->year &&
3604 mon == to->mon &&
3605 day == to->day &&
3606 hour == to->hour &&
3607 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003608 return( 1 );
3609
Paul Bakkercce9d772011-11-18 14:26:47 +00003610 if( year == to->year &&
3611 mon == to->mon &&
3612 day == to->day &&
3613 hour == to->hour &&
3614 min == to->min &&
3615 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003616 return( 1 );
3617
Paul Bakker40ea7de2009-05-03 10:18:48 +00003618 return( 0 );
3619}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003620#else /* POLARSSL_HAVE_TIME */
3621int x509parse_time_expired( const x509_time *to )
3622{
3623 ((void) to);
3624 return( 0 );
3625}
3626#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003627
3628/*
3629 * Return 1 if the certificate is revoked, or 0 otherwise.
3630 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003631int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003632{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003633 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003634
3635 while( cur != NULL && cur->serial.len != 0 )
3636 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003637 if( crt->serial.len == cur->serial.len &&
3638 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003639 {
3640 if( x509parse_time_expired( &cur->revocation_date ) )
3641 return( 1 );
3642 }
3643
3644 cur = cur->next;
3645 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
3647 return( 0 );
3648}
3649
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003650/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003651 * Check that the given certificate is valid accoring to the CRL.
3652 */
3653static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3654 x509_crl *crl_list)
3655{
3656 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003657 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3658 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003659
Paul Bakker915275b2012-09-28 07:10:55 +00003660 if( ca == NULL )
3661 return( flags );
3662
Paul Bakker76fd75a2011-01-16 21:12:10 +00003663 /*
3664 * TODO: What happens if no CRL is present?
3665 * Suggestion: Revocation state should be unknown if no CRL is present.
3666 * For backwards compatibility this is not yet implemented.
3667 */
3668
Paul Bakker915275b2012-09-28 07:10:55 +00003669 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003670 {
Paul Bakker915275b2012-09-28 07:10:55 +00003671 if( crl_list->version == 0 ||
3672 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003673 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3674 crl_list->issuer_raw.len ) != 0 )
3675 {
3676 crl_list = crl_list->next;
3677 continue;
3678 }
3679
3680 /*
3681 * Check if CRL is correctly signed by the trusted CA
3682 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003683 md_info = md_info_from_type( crl_list->sig_md );
3684 if( md_info == NULL )
3685 {
3686 /*
3687 * Cannot check 'unknown' hash
3688 */
3689 flags |= BADCRL_NOT_TRUSTED;
3690 break;
3691 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003692
Paul Bakkerc70b9822013-04-07 22:00:46 +02003693 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003694
Paul Bakkerc70b9822013-04-07 22:00:46 +02003695 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003696 0, hash, crl_list->sig.p ) == 0 )
3697 {
3698 /*
3699 * CRL is not trusted
3700 */
3701 flags |= BADCRL_NOT_TRUSTED;
3702 break;
3703 }
3704
3705 /*
3706 * Check for validity of CRL (Do not drop out)
3707 */
3708 if( x509parse_time_expired( &crl_list->next_update ) )
3709 flags |= BADCRL_EXPIRED;
3710
3711 /*
3712 * Check if certificate is revoked
3713 */
3714 if( x509parse_revoked(crt, crl_list) )
3715 {
3716 flags |= BADCERT_REVOKED;
3717 break;
3718 }
3719
3720 crl_list = crl_list->next;
3721 }
3722 return flags;
3723}
3724
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003725static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003726{
3727 size_t i;
3728 size_t cn_idx = 0;
3729
Paul Bakker57b12982012-02-11 17:38:38 +00003730 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003731 return( 0 );
3732
3733 for( i = 0; i < strlen( cn ); ++i )
3734 {
3735 if( cn[i] == '.' )
3736 {
3737 cn_idx = i;
3738 break;
3739 }
3740 }
3741
3742 if( cn_idx == 0 )
3743 return( 0 );
3744
Paul Bakker535e97d2012-08-23 10:49:55 +00003745 if( strlen( cn ) - cn_idx == name->len - 1 &&
3746 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003747 {
3748 return( 1 );
3749 }
3750
3751 return( 0 );
3752}
3753
Paul Bakker915275b2012-09-28 07:10:55 +00003754static int x509parse_verify_top(
3755 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003756 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003757 int (*f_vrfy)(void *, x509_cert *, int, int *),
3758 void *p_vrfy )
3759{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003760 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003761 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003762 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3763 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003764
3765 if( x509parse_time_expired( &child->valid_to ) )
3766 *flags |= BADCERT_EXPIRED;
3767
3768 /*
3769 * Child is the top of the chain. Check against the trust_ca list.
3770 */
3771 *flags |= BADCERT_NOT_TRUSTED;
3772
3773 while( trust_ca != NULL )
3774 {
3775 if( trust_ca->version == 0 ||
3776 child->issuer_raw.len != trust_ca->subject_raw.len ||
3777 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3778 child->issuer_raw.len ) != 0 )
3779 {
3780 trust_ca = trust_ca->next;
3781 continue;
3782 }
3783
Paul Bakker9a736322012-11-14 12:39:52 +00003784 /*
3785 * Reduce path_len to check against if top of the chain is
3786 * the same as the trusted CA
3787 */
3788 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3789 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3790 child->issuer_raw.len ) == 0 )
3791 {
3792 check_path_cnt--;
3793 }
3794
Paul Bakker915275b2012-09-28 07:10:55 +00003795 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003796 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003797 {
3798 trust_ca = trust_ca->next;
3799 continue;
3800 }
3801
Paul Bakkerc70b9822013-04-07 22:00:46 +02003802 md_info = md_info_from_type( child->sig_md );
3803 if( md_info == NULL )
3804 {
3805 /*
3806 * Cannot check 'unknown' hash
3807 */
3808 continue;
3809 }
Paul Bakker915275b2012-09-28 07:10:55 +00003810
Paul Bakkerc70b9822013-04-07 22:00:46 +02003811 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003812
Paul Bakkerc70b9822013-04-07 22:00:46 +02003813 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003814 0, hash, child->sig.p ) != 0 )
3815 {
3816 trust_ca = trust_ca->next;
3817 continue;
3818 }
3819
3820 /*
3821 * Top of chain is signed by a trusted CA
3822 */
3823 *flags &= ~BADCERT_NOT_TRUSTED;
3824 break;
3825 }
3826
Paul Bakker9a736322012-11-14 12:39:52 +00003827 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003828 * If top of chain is not the same as the trusted CA send a verify request
3829 * to the callback for any issues with validity and CRL presence for the
3830 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003831 */
3832 if( trust_ca != NULL &&
3833 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3834 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3835 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003836 {
3837 /* Check trusted CA's CRL for then chain's top crt */
3838 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3839
3840 if( x509parse_time_expired( &trust_ca->valid_to ) )
3841 ca_flags |= BADCERT_EXPIRED;
3842
Paul Bakker915275b2012-09-28 07:10:55 +00003843 if( NULL != f_vrfy )
3844 {
Paul Bakker9a736322012-11-14 12:39:52 +00003845 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003846 return( ret );
3847 }
3848 }
3849
3850 /* Call callback on top cert */
3851 if( NULL != f_vrfy )
3852 {
Paul Bakker9a736322012-11-14 12:39:52 +00003853 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003854 return( ret );
3855 }
3856
Paul Bakker915275b2012-09-28 07:10:55 +00003857 *flags |= ca_flags;
3858
3859 return( 0 );
3860}
3861
3862static int x509parse_verify_child(
3863 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003864 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003865 int (*f_vrfy)(void *, x509_cert *, int, int *),
3866 void *p_vrfy )
3867{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003868 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003869 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003870 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003871 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003872 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003873
3874 if( x509parse_time_expired( &child->valid_to ) )
3875 *flags |= BADCERT_EXPIRED;
3876
Paul Bakkerc70b9822013-04-07 22:00:46 +02003877 md_info = md_info_from_type( child->sig_md );
3878 if( md_info == NULL )
3879 {
3880 /*
3881 * Cannot check 'unknown' hash
3882 */
Paul Bakker915275b2012-09-28 07:10:55 +00003883 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003884 }
3885 else
3886 {
3887 md( md_info, child->tbs.p, child->tbs.len, hash );
3888
3889 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3890 child->sig.p ) != 0 )
3891 *flags |= BADCERT_NOT_TRUSTED;
3892 }
3893
Paul Bakker915275b2012-09-28 07:10:55 +00003894 /* Check trusted CA's CRL for the given crt */
3895 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3896
3897 grandparent = parent->next;
3898
3899 while( grandparent != NULL )
3900 {
3901 if( grandparent->version == 0 ||
3902 grandparent->ca_istrue == 0 ||
3903 parent->issuer_raw.len != grandparent->subject_raw.len ||
3904 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3905 parent->issuer_raw.len ) != 0 )
3906 {
3907 grandparent = grandparent->next;
3908 continue;
3909 }
3910 break;
3911 }
3912
Paul Bakker915275b2012-09-28 07:10:55 +00003913 if( grandparent != NULL )
3914 {
3915 /*
3916 * Part of the chain
3917 */
Paul Bakker9a736322012-11-14 12:39:52 +00003918 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 +00003919 if( ret != 0 )
3920 return( ret );
3921 }
3922 else
3923 {
Paul Bakker9a736322012-11-14 12:39:52 +00003924 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 +00003925 if( ret != 0 )
3926 return( ret );
3927 }
3928
3929 /* child is verified to be a child of the parent, call verify callback */
3930 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003931 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003932 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003933
3934 *flags |= parent_flags;
3935
3936 return( 0 );
3937}
3938
Paul Bakker76fd75a2011-01-16 21:12:10 +00003939/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 * Verify the certificate validity
3941 */
3942int x509parse_verify( x509_cert *crt,
3943 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003944 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003945 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003946 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003947 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003948{
Paul Bakker23986e52011-04-24 08:57:21 +00003949 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003950 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003951 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003952 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003954 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Paul Bakker40ea7de2009-05-03 10:18:48 +00003956 *flags = 0;
3957
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 if( cn != NULL )
3959 {
3960 name = &crt->subject;
3961 cn_len = strlen( cn );
3962
Paul Bakker4d2c1242012-05-10 14:12:46 +00003963 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003965 cur = &crt->subject_alt_names;
3966
3967 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003968 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003969 if( cur->buf.len == cn_len &&
3970 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003971 break;
3972
Paul Bakker535e97d2012-08-23 10:49:55 +00003973 if( cur->buf.len > 2 &&
3974 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003975 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003976 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
Paul Bakker4d2c1242012-05-10 14:12:46 +00003978 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003979 }
3980
3981 if( cur == NULL )
3982 *flags |= BADCERT_CN_MISMATCH;
3983 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003984 else
3985 {
3986 while( name != NULL )
3987 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003988 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003989 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003990 if( name->val.len == cn_len &&
3991 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003992 break;
3993
Paul Bakker535e97d2012-08-23 10:49:55 +00003994 if( name->val.len > 2 &&
3995 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003996 x509_wildcard_verify( cn, &name->val ) )
3997 break;
3998 }
3999
4000 name = name->next;
4001 }
4002
4003 if( name == NULL )
4004 *flags |= BADCERT_CN_MISMATCH;
4005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 }
4007
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 /*
Paul Bakker915275b2012-09-28 07:10:55 +00004009 * Iterate upwards in the given cert chain, to find our crt parent.
4010 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00004012 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakker76fd75a2011-01-16 21:12:10 +00004014 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004016 if( parent->ca_istrue == 0 ||
4017 crt->issuer_raw.len != parent->subject_raw.len ||
4018 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00004019 crt->issuer_raw.len ) != 0 )
4020 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004021 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 continue;
4023 }
Paul Bakker915275b2012-09-28 07:10:55 +00004024 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004025 }
4026
Paul Bakker915275b2012-09-28 07:10:55 +00004027 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028 {
Paul Bakker915275b2012-09-28 07:10:55 +00004029 /*
4030 * Part of the chain
4031 */
Paul Bakker9a736322012-11-14 12:39:52 +00004032 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004033 if( ret != 0 )
4034 return( ret );
4035 }
4036 else
Paul Bakker74111d32011-01-15 16:57:55 +00004037 {
Paul Bakker9a736322012-11-14 12:39:52 +00004038 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004039 if( ret != 0 )
4040 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004041 }
Paul Bakker915275b2012-09-28 07:10:55 +00004042
4043 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00004044 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004045
Paul Bakker5121ce52009-01-03 21:22:43 +00004046 return( 0 );
4047}
4048
4049/*
4050 * Unallocate all certificate data
4051 */
4052void x509_free( x509_cert *crt )
4053{
4054 x509_cert *cert_cur = crt;
4055 x509_cert *cert_prv;
4056 x509_name *name_cur;
4057 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00004058 x509_sequence *seq_cur;
4059 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00004060
4061 if( crt == NULL )
4062 return;
4063
4064 do
4065 {
4066 rsa_free( &cert_cur->rsa );
4067
4068 name_cur = cert_cur->issuer.next;
4069 while( name_cur != NULL )
4070 {
4071 name_prv = name_cur;
4072 name_cur = name_cur->next;
4073 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004074 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075 }
4076
4077 name_cur = cert_cur->subject.next;
4078 while( name_cur != NULL )
4079 {
4080 name_prv = name_cur;
4081 name_cur = name_cur->next;
4082 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004083 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004084 }
4085
Paul Bakker74111d32011-01-15 16:57:55 +00004086 seq_cur = cert_cur->ext_key_usage.next;
4087 while( seq_cur != NULL )
4088 {
4089 seq_prv = seq_cur;
4090 seq_cur = seq_cur->next;
4091 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004092 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004093 }
4094
Paul Bakker8afa70d2012-02-11 18:42:45 +00004095 seq_cur = cert_cur->subject_alt_names.next;
4096 while( seq_cur != NULL )
4097 {
4098 seq_prv = seq_cur;
4099 seq_cur = seq_cur->next;
4100 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004101 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004102 }
4103
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 if( cert_cur->raw.p != NULL )
4105 {
4106 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004107 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004108 }
4109
4110 cert_cur = cert_cur->next;
4111 }
4112 while( cert_cur != NULL );
4113
4114 cert_cur = crt;
4115 do
4116 {
4117 cert_prv = cert_cur;
4118 cert_cur = cert_cur->next;
4119
4120 memset( cert_prv, 0, sizeof( x509_cert ) );
4121 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004122 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 }
4124 while( cert_cur != NULL );
4125}
4126
Paul Bakkerd98030e2009-05-02 15:13:40 +00004127/*
4128 * Unallocate all CRL data
4129 */
4130void x509_crl_free( x509_crl *crl )
4131{
4132 x509_crl *crl_cur = crl;
4133 x509_crl *crl_prv;
4134 x509_name *name_cur;
4135 x509_name *name_prv;
4136 x509_crl_entry *entry_cur;
4137 x509_crl_entry *entry_prv;
4138
4139 if( crl == NULL )
4140 return;
4141
4142 do
4143 {
4144 name_cur = crl_cur->issuer.next;
4145 while( name_cur != NULL )
4146 {
4147 name_prv = name_cur;
4148 name_cur = name_cur->next;
4149 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004150 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004151 }
4152
4153 entry_cur = crl_cur->entry.next;
4154 while( entry_cur != NULL )
4155 {
4156 entry_prv = entry_cur;
4157 entry_cur = entry_cur->next;
4158 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004159 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004160 }
4161
4162 if( crl_cur->raw.p != NULL )
4163 {
4164 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004165 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004166 }
4167
4168 crl_cur = crl_cur->next;
4169 }
4170 while( crl_cur != NULL );
4171
4172 crl_cur = crl;
4173 do
4174 {
4175 crl_prv = crl_cur;
4176 crl_cur = crl_cur->next;
4177
4178 memset( crl_prv, 0, sizeof( x509_crl ) );
4179 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004180 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004181 }
4182 while( crl_cur != NULL );
4183}
4184
Paul Bakker40e46942009-01-03 21:51:57 +00004185#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004186
Paul Bakker40e46942009-01-03 21:51:57 +00004187#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004188
4189/*
4190 * Checkup routine
4191 */
4192int x509_self_test( int verbose )
4193{
Paul Bakker5690efc2011-05-26 13:16:06 +00004194#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004195 int ret;
4196 int flags;
4197 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004198 x509_cert cacert;
4199 x509_cert clicert;
4200 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004201#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004202 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004204
4205 if( verbose != 0 )
4206 printf( " X.509 certificate load: " );
4207
4208 memset( &clicert, 0, sizeof( x509_cert ) );
4209
Paul Bakker3c2122f2013-06-24 19:03:14 +02004210 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004211 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 if( ret != 0 )
4213 {
4214 if( verbose != 0 )
4215 printf( "failed\n" );
4216
4217 return( ret );
4218 }
4219
4220 memset( &cacert, 0, sizeof( x509_cert ) );
4221
Paul Bakker3c2122f2013-06-24 19:03:14 +02004222 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004223 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004224 if( ret != 0 )
4225 {
4226 if( verbose != 0 )
4227 printf( "failed\n" );
4228
4229 return( ret );
4230 }
4231
4232 if( verbose != 0 )
4233 printf( "passed\n X.509 private key load: " );
4234
4235 i = strlen( test_ca_key );
4236 j = strlen( test_ca_pwd );
4237
Paul Bakker66b78b22011-03-25 14:22:50 +00004238 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4239
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004240 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004241 (const unsigned char *) test_ca_key, i,
4242 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 {
4244 if( verbose != 0 )
4245 printf( "failed\n" );
4246
4247 return( ret );
4248 }
4249
4250 if( verbose != 0 )
4251 printf( "passed\n X.509 signature verify: ");
4252
Paul Bakker23986e52011-04-24 08:57:21 +00004253 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 if( ret != 0 )
4255 {
Paul Bakker23986e52011-04-24 08:57:21 +00004256 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 if( verbose != 0 )
4258 printf( "failed\n" );
4259
4260 return( ret );
4261 }
4262
Paul Bakker5690efc2011-05-26 13:16:06 +00004263#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004265 printf( "passed\n X.509 DHM parameter load: " );
4266
4267 i = strlen( test_dhm_params );
4268 j = strlen( test_ca_pwd );
4269
Paul Bakker3c2122f2013-06-24 19:03:14 +02004270 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004271 {
4272 if( verbose != 0 )
4273 printf( "failed\n" );
4274
4275 return( ret );
4276 }
4277
4278 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
4282 x509_free( &cacert );
4283 x509_free( &clicert );
4284 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004285#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004286 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004287#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004288
4289 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004290#else
4291 ((void) verbose);
4292 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004294}
4295
4296#endif
4297
4298#endif