blob: e70cee98edaa4a2ba2dbc76f756e3bec375a78a7 [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 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200170 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
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200574static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
575 x509_buf *alg_params, ecp_keypair *key )
576{
577 int ret;
578 ecp_group_id grp_id;
579
580 if( ( ret = x509_ecparams_get_grp_id( alg_params, &grp_id ) ) != 0 )
581 return( ret );
582
583 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
584 return( ret );
585
586 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
587 (const unsigned char *) *p, end - *p ) ) != 0 )
588 {
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200589 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200590 }
591
592 return( 0 );
593}
594
595/*
596 * SubjectPublicKeyInfo ::= SEQUENCE {
597 * algorithm AlgorithmIdentifier,
598 * subjectPublicKey BIT STRING }
599 */
600static int x509_get_pubkey( unsigned char **p,
601 const unsigned char *end,
602 pk_context *pk )
603{
604 int ret;
605 size_t len;
606 x509_buf alg_params;
607 pk_type_t pk_alg = POLARSSL_PK_NONE;
608
609 if( ( ret = asn1_get_tag( p, end, &len,
610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
611 {
612 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
613 }
614
615 end = *p + len;
616
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200617 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200618 return( ret );
619
620 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
621 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
622
623 if( ( end - *p ) < 1 )
624 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
625 POLARSSL_ERR_ASN1_OUT_OF_DATA );
626
627 if( *p + len != end )
628 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
629 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
630
631 if( *(*p)++ != 0 )
632 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
633
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200634 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
635 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200636
637 switch( pk_alg )
638 {
639 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200640 case POLARSSL_PK_ECDSA:
641 /* Should never happen */
642 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200643 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200644
645 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200646 ret = x509_get_rsapubkey( p, end, pk->data );
647 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200648
649 case POLARSSL_PK_ECKEY_DH:
650 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
651 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200652
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200653 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200654 ret = x509_get_ecpubkey( p, end, &alg_params, pk->data );
655 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200656 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200657
658 if( ret != 0 )
659 pk_free( pk );
660
661 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200662}
663
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200664/*
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200665 * Get an RSA public key (compatibility wrapper)
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200667static int x509_get_pubkey_rsa( unsigned char **p,
668 const unsigned char *end,
669 rsa_context *rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200671 pk_context pk_ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200673 pk_init( &pk_ctx );
674 pk_wrap_rsa( &pk_ctx, rsa );
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +0200675
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200676 return( x509_get_pubkey( p, end, &pk_ctx ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677}
678
679static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000680 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 x509_buf *sig )
682{
Paul Bakker23986e52011-04-24 08:57:21 +0000683 int ret;
684 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
Paul Bakker8afa70d2012-02-11 18:42:45 +0000686 if( ( end - *p ) < 1 )
687 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
688 POLARSSL_ERR_ASN1_OUT_OF_DATA );
689
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 sig->tag = **p;
691
692 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000693 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Paul Bakker74111d32011-01-15 16:57:55 +0000695
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000697 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
699 sig->len = len;
700 sig->p = *p;
701
702 *p += len;
703
704 return( 0 );
705}
706
707/*
708 * X.509 v2/v3 unique identifier (not parsed)
709 */
710static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000711 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 x509_buf *uid, int n )
713{
714 int ret;
715
716 if( *p == end )
717 return( 0 );
718
719 uid->tag = **p;
720
721 if( ( ret = asn1_get_tag( p, end, &uid->len,
722 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
723 {
Paul Bakker40e46942009-01-03 21:51:57 +0000724 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 return( 0 );
726
727 return( ret );
728 }
729
730 uid->p = *p;
731 *p += uid->len;
732
733 return( 0 );
734}
735
736/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000737 * X.509 Extensions (No parsing of extensions, pointer should
738 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 */
740static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000741 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000742 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743{
Paul Bakker23986e52011-04-24 08:57:21 +0000744 int ret;
745 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
747 if( *p == end )
748 return( 0 );
749
750 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000751
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000753 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 ext->p = *p;
757 end = *p + ext->len;
758
759 /*
760 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
761 *
762 * Extension ::= SEQUENCE {
763 * extnID OBJECT IDENTIFIER,
764 * critical BOOLEAN DEFAULT FALSE,
765 * extnValue OCTET STRING }
766 */
767 if( ( ret = asn1_get_tag( p, end, &len,
768 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000769 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
771 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000772 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000773 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
Paul Bakkerd98030e2009-05-02 15:13:40 +0000775 return( 0 );
776}
777
778/*
779 * X.509 CRL v2 extensions (no extensions parsed yet.)
780 */
781static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000782 const unsigned char *end,
783 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000784{
Paul Bakker23986e52011-04-24 08:57:21 +0000785 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000786 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000787
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000788 /* Get explicit tag */
789 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000790 {
791 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
792 return( 0 );
793
794 return( ret );
795 }
796
797 while( *p < end )
798 {
799 if( ( ret = asn1_get_tag( p, end, &len,
800 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000801 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000802
803 *p += len;
804 }
805
806 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000807 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000808 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
809
810 return( 0 );
811}
812
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000813/*
814 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
815 */
816static int x509_get_crl_entry_ext( unsigned char **p,
817 const unsigned char *end,
818 x509_buf *ext )
819{
820 int ret;
821 size_t len = 0;
822
823 /* OPTIONAL */
824 if (end <= *p)
825 return( 0 );
826
827 ext->tag = **p;
828 ext->p = *p;
829
830 /*
831 * Get CRL-entry extension sequence header
832 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
833 */
834 if( ( ret = asn1_get_tag( p, end, &ext->len,
835 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
836 {
837 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
838 {
839 ext->p = NULL;
840 return( 0 );
841 }
842 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
843 }
844
845 end = *p + ext->len;
846
847 if( end != *p + ext->len )
848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
849 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
850
851 while( *p < end )
852 {
853 if( ( ret = asn1_get_tag( p, end, &len,
854 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
856
857 *p += len;
858 }
859
860 if( *p != end )
861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
862 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
863
864 return( 0 );
865}
866
Paul Bakker74111d32011-01-15 16:57:55 +0000867static int x509_get_basic_constraints( unsigned char **p,
868 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000869 int *ca_istrue,
870 int *max_pathlen )
871{
Paul Bakker23986e52011-04-24 08:57:21 +0000872 int ret;
873 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000874
875 /*
876 * BasicConstraints ::= SEQUENCE {
877 * cA BOOLEAN DEFAULT FALSE,
878 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
879 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000880 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000881 *max_pathlen = 0; /* endless */
882
883 if( ( ret = asn1_get_tag( p, end, &len,
884 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000885 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000886
887 if( *p == end )
888 return 0;
889
Paul Bakker3cccddb2011-01-16 21:46:31 +0000890 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000891 {
892 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000893 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000894
895 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000896 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000897
Paul Bakker3cccddb2011-01-16 21:46:31 +0000898 if( *ca_istrue != 0 )
899 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000900 }
901
902 if( *p == end )
903 return 0;
904
905 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000906 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000907
908 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000910 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
911
912 (*max_pathlen)++;
913
Paul Bakker74111d32011-01-15 16:57:55 +0000914 return 0;
915}
916
917static int x509_get_ns_cert_type( unsigned char **p,
918 const unsigned char *end,
919 unsigned char *ns_cert_type)
920{
921 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000922 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000923
924 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000925 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000926
927 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000928 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000929 POLARSSL_ERR_ASN1_INVALID_LENGTH );
930
931 /* Get actual bitstring */
932 *ns_cert_type = *bs.p;
933 return 0;
934}
935
936static int x509_get_key_usage( unsigned char **p,
937 const unsigned char *end,
938 unsigned char *key_usage)
939{
940 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000941 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000942
943 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000944 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000945
Paul Bakker94a67962012-08-23 13:03:52 +0000946 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000947 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000948 POLARSSL_ERR_ASN1_INVALID_LENGTH );
949
950 /* Get actual bitstring */
951 *key_usage = *bs.p;
952 return 0;
953}
954
Paul Bakkerd98030e2009-05-02 15:13:40 +0000955/*
Paul Bakker74111d32011-01-15 16:57:55 +0000956 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
957 *
958 * KeyPurposeId ::= OBJECT IDENTIFIER
959 */
960static int x509_get_ext_key_usage( unsigned char **p,
961 const unsigned char *end,
962 x509_sequence *ext_key_usage)
963{
964 int ret;
965
966 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000968
969 /* Sequence length must be >= 1 */
970 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000971 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000972 POLARSSL_ERR_ASN1_INVALID_LENGTH );
973
974 return 0;
975}
976
977/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000978 * SubjectAltName ::= GeneralNames
979 *
980 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
981 *
982 * GeneralName ::= CHOICE {
983 * otherName [0] OtherName,
984 * rfc822Name [1] IA5String,
985 * dNSName [2] IA5String,
986 * x400Address [3] ORAddress,
987 * directoryName [4] Name,
988 * ediPartyName [5] EDIPartyName,
989 * uniformResourceIdentifier [6] IA5String,
990 * iPAddress [7] OCTET STRING,
991 * registeredID [8] OBJECT IDENTIFIER }
992 *
993 * OtherName ::= SEQUENCE {
994 * type-id OBJECT IDENTIFIER,
995 * value [0] EXPLICIT ANY DEFINED BY type-id }
996 *
997 * EDIPartyName ::= SEQUENCE {
998 * nameAssigner [0] DirectoryString OPTIONAL,
999 * partyName [1] DirectoryString }
1000 *
1001 * NOTE: PolarSSL only parses and uses dNSName at this point.
1002 */
1003static int x509_get_subject_alt_name( unsigned char **p,
1004 const unsigned char *end,
1005 x509_sequence *subject_alt_name )
1006{
1007 int ret;
1008 size_t len, tag_len;
1009 asn1_buf *buf;
1010 unsigned char tag;
1011 asn1_sequence *cur = subject_alt_name;
1012
1013 /* Get main sequence tag */
1014 if( ( ret = asn1_get_tag( p, end, &len,
1015 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1016 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1017
1018 if( *p + len != end )
1019 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1020 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1021
1022 while( *p < end )
1023 {
1024 if( ( end - *p ) < 1 )
1025 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1026 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1027
1028 tag = **p;
1029 (*p)++;
1030 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
1031 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1032
1033 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
1034 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1035 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1036
1037 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
1038 {
1039 *p += tag_len;
1040 continue;
1041 }
1042
1043 buf = &(cur->buf);
1044 buf->tag = tag;
1045 buf->p = *p;
1046 buf->len = tag_len;
1047 *p += buf->len;
1048
1049 /* Allocate and assign next pointer */
1050 if (*p < end)
1051 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001052 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001053 sizeof( asn1_sequence ) );
1054
1055 if( cur->next == NULL )
1056 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1057 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1058
Paul Bakker535e97d2012-08-23 10:49:55 +00001059 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001060 cur = cur->next;
1061 }
1062 }
1063
1064 /* Set final sequence entry's next pointer to NULL */
1065 cur->next = NULL;
1066
1067 if( *p != end )
1068 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1069 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1070
1071 return( 0 );
1072}
1073
1074/*
Paul Bakker74111d32011-01-15 16:57:55 +00001075 * X.509 v3 extensions
1076 *
1077 * TODO: Perform all of the basic constraints tests required by the RFC
1078 * TODO: Set values for undetected extensions to a sane default?
1079 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001080 */
1081static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001082 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001083 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001084{
Paul Bakker23986e52011-04-24 08:57:21 +00001085 int ret;
1086 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001087 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001089 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001090 {
1091 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1092 return( 0 );
1093
1094 return( ret );
1095 }
1096
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 while( *p < end )
1098 {
Paul Bakker74111d32011-01-15 16:57:55 +00001099 /*
1100 * Extension ::= SEQUENCE {
1101 * extnID OBJECT IDENTIFIER,
1102 * critical BOOLEAN DEFAULT FALSE,
1103 * extnValue OCTET STRING }
1104 */
1105 x509_buf extn_oid = {0, 0, NULL};
1106 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001107 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001108
Paul Bakker5121ce52009-01-03 21:22:43 +00001109 if( ( ret = asn1_get_tag( p, end, &len,
1110 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001111 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001113 end_ext_data = *p + len;
1114
Paul Bakker74111d32011-01-15 16:57:55 +00001115 /* Get extension ID */
1116 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker74111d32011-01-15 16:57:55 +00001118 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001119 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakker74111d32011-01-15 16:57:55 +00001121 extn_oid.p = *p;
1122 *p += extn_oid.len;
1123
1124 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001125 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001126 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1127
1128 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001129 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001130 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001131 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
Paul Bakker74111d32011-01-15 16:57:55 +00001133 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001134 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001136 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001138 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001139
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001140 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001141 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001142 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Paul Bakker74111d32011-01-15 16:57:55 +00001144 /*
1145 * Detect supported extensions
1146 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001147 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1148
1149 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001150 {
1151 /* No parser found, skip extension */
1152 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Paul Bakker5c721f92011-07-27 16:51:09 +00001154#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001155 if( is_critical )
1156 {
1157 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001158 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001159 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1160 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001161#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001162 continue;
1163 }
1164
1165 crt->ext_types |= ext_type;
1166
1167 switch( ext_type )
1168 {
1169 case EXT_BASIC_CONSTRAINTS:
1170 /* Parse basic constraints */
1171 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1172 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1173 return ( ret );
1174 break;
1175
1176 case EXT_KEY_USAGE:
1177 /* Parse key usage */
1178 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1179 &crt->key_usage ) ) != 0 )
1180 return ( ret );
1181 break;
1182
1183 case EXT_EXTENDED_KEY_USAGE:
1184 /* Parse extended key usage */
1185 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1186 &crt->ext_key_usage ) ) != 0 )
1187 return ( ret );
1188 break;
1189
1190 case EXT_SUBJECT_ALT_NAME:
1191 /* Parse subject alt name */
1192 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1193 &crt->subject_alt_names ) ) != 0 )
1194 return ( ret );
1195 break;
1196
1197 case EXT_NS_CERT_TYPE:
1198 /* Parse netscape certificate type */
1199 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1200 &crt->ns_cert_type ) ) != 0 )
1201 return ( ret );
1202 break;
1203
1204 default:
1205 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
1208
1209 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001210 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001211 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 return( 0 );
1214}
1215
1216/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001217 * X.509 CRL Entries
1218 */
1219static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001220 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001221 x509_crl_entry *entry )
1222{
Paul Bakker23986e52011-04-24 08:57:21 +00001223 int ret;
1224 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001225 x509_crl_entry *cur_entry = entry;
1226
1227 if( *p == end )
1228 return( 0 );
1229
Paul Bakker9be19372009-07-27 20:21:53 +00001230 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001231 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1232 {
1233 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1234 return( 0 );
1235
1236 return( ret );
1237 }
1238
Paul Bakker9be19372009-07-27 20:21:53 +00001239 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001240
1241 while( *p < end )
1242 {
Paul Bakker23986e52011-04-24 08:57:21 +00001243 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001244 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001245
1246 if( ( ret = asn1_get_tag( p, end, &len2,
1247 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1248 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001249 return( ret );
1250 }
1251
Paul Bakker9be19372009-07-27 20:21:53 +00001252 cur_entry->raw.tag = **p;
1253 cur_entry->raw.p = *p;
1254 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001255 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001256
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001257 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001258 return( ret );
1259
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001260 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001261 return( ret );
1262
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001263 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001264 return( ret );
1265
Paul Bakker74111d32011-01-15 16:57:55 +00001266 if ( *p < end )
1267 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001268 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001269
1270 if( cur_entry->next == NULL )
1271 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1272
Paul Bakkerd98030e2009-05-02 15:13:40 +00001273 cur_entry = cur_entry->next;
1274 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1275 }
1276 }
1277
1278 return( 0 );
1279}
1280
Paul Bakkerc70b9822013-04-07 22:00:46 +02001281static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1282 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001283{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001284 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001285
Paul Bakkerc70b9822013-04-07 22:00:46 +02001286 if( ret != 0 )
1287 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001288
Paul Bakkerc70b9822013-04-07 22:00:46 +02001289 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001290}
1291
Paul Bakkerd98030e2009-05-02 15:13:40 +00001292/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001293 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001295static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1296 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001297{
Paul Bakker23986e52011-04-24 08:57:21 +00001298 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001299 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001300 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Paul Bakker320a4b52009-03-28 18:52:39 +00001302 /*
1303 * Check for valid input
1304 */
1305 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001306 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001307
Paul Bakker6e339b52013-07-03 13:37:05 +02001308 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001309
1310 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001311 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001312
1313 memcpy( p, buf, buflen );
1314
1315 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 crt->raw.p = p;
1318 crt->raw.len = len;
1319 end = p + len;
1320
1321 /*
1322 * Certificate ::= SEQUENCE {
1323 * tbsCertificate TBSCertificate,
1324 * signatureAlgorithm AlgorithmIdentifier,
1325 * signatureValue BIT STRING }
1326 */
1327 if( ( ret = asn1_get_tag( &p, end, &len,
1328 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1329 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001330 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001331 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
Paul Bakkerb00ca422012-09-25 12:10:00 +00001334 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001337 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001338 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001340 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * TBSCertificate ::= SEQUENCE {
1344 */
1345 crt->tbs.p = p;
1346
1347 if( ( ret = asn1_get_tag( &p, end, &len,
1348 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1349 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001350 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001351 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 }
1353
1354 end = p + len;
1355 crt->tbs.len = end - crt->tbs.p;
1356
1357 /*
1358 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1359 *
1360 * CertificateSerialNumber ::= INTEGER
1361 *
1362 * signature AlgorithmIdentifier
1363 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001364 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1365 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1366 ( ret = x509_get_alg( &p, end, &crt->sig_oid1, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001368 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 return( ret );
1370 }
1371
1372 crt->version++;
1373
1374 if( crt->version > 3 )
1375 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001376 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001377 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Paul Bakkerc70b9822013-04-07 22:00:46 +02001380 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1381 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001383 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001384 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
1386
1387 /*
1388 * issuer Name
1389 */
1390 crt->issuer_raw.p = p;
1391
1392 if( ( ret = asn1_get_tag( &p, end, &len,
1393 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001396 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 }
1398
1399 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1400 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001401 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 return( ret );
1403 }
1404
1405 crt->issuer_raw.len = p - crt->issuer_raw.p;
1406
1407 /*
1408 * Validity ::= SEQUENCE {
1409 * notBefore Time,
1410 * notAfter Time }
1411 *
1412 */
1413 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1414 &crt->valid_to ) ) != 0 )
1415 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 return( ret );
1418 }
1419
1420 /*
1421 * subject Name
1422 */
1423 crt->subject_raw.p = p;
1424
1425 if( ( ret = asn1_get_tag( &p, end, &len,
1426 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1427 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001428 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001429 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430 }
1431
Paul Bakkercefb3962012-06-27 11:51:09 +00001432 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001434 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 return( ret );
1436 }
1437
1438 crt->subject_raw.len = p - crt->subject_raw.p;
1439
1440 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001441 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +02001443 if( ( ret = x509_get_pubkey_rsa( &p, end, &crt->rsa ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 return( ret );
1447 }
1448
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 /*
1450 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1451 * -- If present, version shall be v2 or v3
1452 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1453 * -- If present, version shall be v2 or v3
1454 * extensions [3] EXPLICIT Extensions OPTIONAL
1455 * -- If present, version shall be v3
1456 */
1457 if( crt->version == 2 || crt->version == 3 )
1458 {
1459 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1460 if( ret != 0 )
1461 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001462 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 return( ret );
1464 }
1465 }
1466
1467 if( crt->version == 2 || crt->version == 3 )
1468 {
1469 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1470 if( ret != 0 )
1471 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001472 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473 return( ret );
1474 }
1475 }
1476
1477 if( crt->version == 3 )
1478 {
Paul Bakker74111d32011-01-15 16:57:55 +00001479 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 if( ret != 0 )
1481 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001482 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 return( ret );
1484 }
1485 }
1486
1487 if( p != end )
1488 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001489 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001490 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001491 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
1493
Paul Bakkerb00ca422012-09-25 12:10:00 +00001494 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001495
1496 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001497 * }
1498 * -- end of TBSCertificate
1499 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 * signatureAlgorithm AlgorithmIdentifier,
1501 * signatureValue BIT STRING
1502 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001503 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001505 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 return( ret );
1507 }
1508
Paul Bakker535e97d2012-08-23 10:49:55 +00001509 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1510 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001512 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001513 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
1515
1516 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1517 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001518 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519 return( ret );
1520 }
1521
1522 if( p != end )
1523 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001524 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001525 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001526 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 }
1528
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001529 return( 0 );
1530}
1531
1532/*
Paul Bakker42c65812013-06-24 19:21:59 +02001533 * Parse one X.509 certificate in DER format from a buffer and add them to a
1534 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001535 */
Paul Bakker42c65812013-06-24 19:21:59 +02001536int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001537{
Paul Bakker42c65812013-06-24 19:21:59 +02001538 int ret;
1539 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001540
1541 /*
1542 * Check for valid input
1543 */
1544 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001545 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001546
1547 while( crt->version != 0 && crt->next != NULL )
1548 {
1549 prev = crt;
1550 crt = crt->next;
1551 }
1552
1553 /*
1554 * Add new certificate on the end of the chain if needed.
1555 */
1556 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001557 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001558 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001559
Paul Bakker7d06ad22009-05-02 15:53:56 +00001560 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001561 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001562
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001563 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001564 crt = crt->next;
1565 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001566 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001567
Paul Bakker42c65812013-06-24 19:21:59 +02001568 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1569 {
1570 if( prev )
1571 prev->next = NULL;
1572
1573 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001574 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001575
1576 return( ret );
1577 }
1578
1579 return( 0 );
1580}
1581
1582/*
1583 * Parse one or more PEM certificates from a buffer and add them to the chained list
1584 */
1585int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1586{
1587 int ret, success = 0, first_error = 0, total_failed = 0;
1588 int buf_format = X509_FORMAT_DER;
1589
1590 /*
1591 * Check for valid input
1592 */
1593 if( chain == NULL || buf == NULL )
1594 return( POLARSSL_ERR_X509_INVALID_INPUT );
1595
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001596 /*
1597 * Determine buffer content. Buffer contains either one DER certificate or
1598 * one or more PEM certificates.
1599 */
1600#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001601 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001602 buf_format = X509_FORMAT_PEM;
1603#endif
1604
1605 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001606 return x509parse_crt_der( chain, buf, buflen );
1607
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001608#if defined(POLARSSL_PEM_C)
1609 if( buf_format == X509_FORMAT_PEM )
1610 {
1611 pem_context pem;
1612
1613 while( buflen > 0 )
1614 {
1615 size_t use_len;
1616 pem_init( &pem );
1617
1618 ret = pem_read_buffer( &pem,
1619 "-----BEGIN CERTIFICATE-----",
1620 "-----END CERTIFICATE-----",
1621 buf, NULL, 0, &use_len );
1622
1623 if( ret == 0 )
1624 {
1625 /*
1626 * Was PEM encoded
1627 */
1628 buflen -= use_len;
1629 buf += use_len;
1630 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001631 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1632 {
1633 return( ret );
1634 }
Paul Bakker00b28602013-06-24 13:02:41 +02001635 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001636 {
1637 pem_free( &pem );
1638
Paul Bakker5ed3b342013-06-24 19:05:46 +02001639 /*
1640 * PEM header and footer were found
1641 */
1642 buflen -= use_len;
1643 buf += use_len;
1644
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001645 if( first_error == 0 )
1646 first_error = ret;
1647
1648 continue;
1649 }
1650 else
1651 break;
1652
Paul Bakker42c65812013-06-24 19:21:59 +02001653 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001654
1655 pem_free( &pem );
1656
1657 if( ret != 0 )
1658 {
1659 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001660 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001661 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001662 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001663 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001664
1665 if( first_error == 0 )
1666 first_error = ret;
1667
Paul Bakker42c65812013-06-24 19:21:59 +02001668 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001669 continue;
1670 }
1671
1672 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001673 }
1674 }
1675#endif
1676
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001677 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001678 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001679 else if( first_error )
1680 return( first_error );
1681 else
1682 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
1684
1685/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001686 * Parse one or more CRLs and add them to the chained list
1687 */
Paul Bakker23986e52011-04-24 08:57:21 +00001688int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001689{
Paul Bakker23986e52011-04-24 08:57:21 +00001690 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001691 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692 unsigned char *p, *end;
1693 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001694#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001695 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001696 pem_context pem;
1697#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698
1699 crl = chain;
1700
1701 /*
1702 * Check for valid input
1703 */
1704 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001705 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001706
1707 while( crl->version != 0 && crl->next != NULL )
1708 crl = crl->next;
1709
1710 /*
1711 * Add new CRL on the end of the chain if needed.
1712 */
1713 if ( crl->version != 0 && crl->next == NULL)
1714 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001715 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716
Paul Bakker7d06ad22009-05-02 15:53:56 +00001717 if( crl->next == NULL )
1718 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001719 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001720 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001721 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722
Paul Bakker7d06ad22009-05-02 15:53:56 +00001723 crl = crl->next;
1724 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 }
1726
Paul Bakker96743fc2011-02-12 14:30:57 +00001727#if defined(POLARSSL_PEM_C)
1728 pem_init( &pem );
1729 ret = pem_read_buffer( &pem,
1730 "-----BEGIN X509 CRL-----",
1731 "-----END X509 CRL-----",
1732 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733
Paul Bakker96743fc2011-02-12 14:30:57 +00001734 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001736 /*
1737 * Was PEM encoded
1738 */
1739 buflen -= use_len;
1740 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001741
1742 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001743 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001744 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001745 p = pem.buf;
1746 pem.buf = NULL;
1747 len = pem.buflen;
1748 pem_free( &pem );
1749 }
Paul Bakker00b28602013-06-24 13:02:41 +02001750 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001751 {
1752 pem_free( &pem );
1753 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754 }
1755 else
1756 {
1757 /*
1758 * nope, copy the raw DER data
1759 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001760 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001761
1762 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001763 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764
1765 memcpy( p, buf, buflen );
1766
1767 buflen = 0;
1768 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001769#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001770 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001771
1772 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001773 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001774
1775 memcpy( p, buf, buflen );
1776
1777 buflen = 0;
1778#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779
1780 crl->raw.p = p;
1781 crl->raw.len = len;
1782 end = p + len;
1783
1784 /*
1785 * CertificateList ::= SEQUENCE {
1786 * tbsCertList TBSCertList,
1787 * signatureAlgorithm AlgorithmIdentifier,
1788 * signatureValue BIT STRING }
1789 */
1790 if( ( ret = asn1_get_tag( &p, end, &len,
1791 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1792 {
1793 x509_crl_free( crl );
1794 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1795 }
1796
Paul Bakker23986e52011-04-24 08:57:21 +00001797 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798 {
1799 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001800 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001801 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1802 }
1803
1804 /*
1805 * TBSCertList ::= SEQUENCE {
1806 */
1807 crl->tbs.p = p;
1808
1809 if( ( ret = asn1_get_tag( &p, end, &len,
1810 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1811 {
1812 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001813 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 }
1815
1816 end = p + len;
1817 crl->tbs.len = end - crl->tbs.p;
1818
1819 /*
1820 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1821 * -- if present, MUST be v2
1822 *
1823 * signature AlgorithmIdentifier
1824 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001825 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001826 ( ret = x509_get_alg( &p, end, &crl->sig_oid1, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 {
1828 x509_crl_free( crl );
1829 return( ret );
1830 }
1831
1832 crl->version++;
1833
1834 if( crl->version > 2 )
1835 {
1836 x509_crl_free( crl );
1837 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1838 }
1839
Paul Bakkerc70b9822013-04-07 22:00:46 +02001840 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1841 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 {
1843 x509_crl_free( crl );
1844 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1845 }
1846
1847 /*
1848 * issuer Name
1849 */
1850 crl->issuer_raw.p = p;
1851
1852 if( ( ret = asn1_get_tag( &p, end, &len,
1853 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1854 {
1855 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001856 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001857 }
1858
1859 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1860 {
1861 x509_crl_free( crl );
1862 return( ret );
1863 }
1864
1865 crl->issuer_raw.len = p - crl->issuer_raw.p;
1866
1867 /*
1868 * thisUpdate Time
1869 * nextUpdate Time OPTIONAL
1870 */
Paul Bakker91200182010-02-18 21:26:15 +00001871 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001872 {
1873 x509_crl_free( crl );
1874 return( ret );
1875 }
1876
Paul Bakker91200182010-02-18 21:26:15 +00001877 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001878 {
Paul Bakker9d781402011-05-09 16:17:09 +00001879 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001880 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001881 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001882 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001883 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001884 x509_crl_free( crl );
1885 return( ret );
1886 }
1887 }
1888
1889 /*
1890 * revokedCertificates SEQUENCE OF SEQUENCE {
1891 * userCertificate CertificateSerialNumber,
1892 * revocationDate Time,
1893 * crlEntryExtensions Extensions OPTIONAL
1894 * -- if present, MUST be v2
1895 * } OPTIONAL
1896 */
1897 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1898 {
1899 x509_crl_free( crl );
1900 return( ret );
1901 }
1902
1903 /*
1904 * crlExtensions EXPLICIT Extensions OPTIONAL
1905 * -- if present, MUST be v2
1906 */
1907 if( crl->version == 2 )
1908 {
1909 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1910
1911 if( ret != 0 )
1912 {
1913 x509_crl_free( crl );
1914 return( ret );
1915 }
1916 }
1917
1918 if( p != end )
1919 {
1920 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001921 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001922 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1923 }
1924
1925 end = crl->raw.p + crl->raw.len;
1926
1927 /*
1928 * signatureAlgorithm AlgorithmIdentifier,
1929 * signatureValue BIT STRING
1930 */
Manuel Pégourié-Gonnard444b4272013-07-01 15:27:48 +02001931 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2, NULL ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001932 {
1933 x509_crl_free( crl );
1934 return( ret );
1935 }
1936
Paul Bakker535e97d2012-08-23 10:49:55 +00001937 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1938 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939 {
1940 x509_crl_free( crl );
1941 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1942 }
1943
1944 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1945 {
1946 x509_crl_free( crl );
1947 return( ret );
1948 }
1949
1950 if( p != end )
1951 {
1952 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001953 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1955 }
1956
1957 if( buflen > 0 )
1958 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001959 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001960
Paul Bakker7d06ad22009-05-02 15:53:56 +00001961 if( crl->next == NULL )
1962 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001963 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001964 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001965 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001966
Paul Bakker7d06ad22009-05-02 15:53:56 +00001967 crl = crl->next;
1968 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001969
1970 return( x509parse_crl( crl, buf, buflen ) );
1971 }
1972
1973 return( 0 );
1974}
1975
Paul Bakker335db3f2011-04-25 15:28:35 +00001976#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001977/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001978 * Load all data from a file into a given buffer.
1979 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001980static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001981{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001982 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001983
Paul Bakkerd98030e2009-05-02 15:13:40 +00001984 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001985 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001986
Paul Bakkerd98030e2009-05-02 15:13:40 +00001987 fseek( f, 0, SEEK_END );
1988 *n = (size_t) ftell( f );
1989 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001990
Paul Bakker6e339b52013-07-03 13:37:05 +02001991 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001992 {
1993 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001994 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001995 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001996
Paul Bakkerd98030e2009-05-02 15:13:40 +00001997 if( fread( *buf, 1, *n, f ) != *n )
1998 {
1999 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02002000 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00002001 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002002 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002003
Paul Bakkerd98030e2009-05-02 15:13:40 +00002004 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002005
Paul Bakkerd98030e2009-05-02 15:13:40 +00002006 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00002007
Paul Bakkerd98030e2009-05-02 15:13:40 +00002008 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002009}
2010
2011/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 * Load one or more certificates and add them to the chained list
2013 */
Paul Bakker69e095c2011-12-10 21:55:01 +00002014int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00002015{
2016 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 size_t n;
2018 unsigned char *buf;
2019
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002020 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002021 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Paul Bakker69e095c2011-12-10 21:55:01 +00002023 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
2025 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002026 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002027
2028 return( ret );
2029}
2030
Paul Bakker8d914582012-06-04 12:46:42 +00002031int x509parse_crtpath( x509_cert *chain, const char *path )
2032{
2033 int ret = 0;
2034#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002035 int w_ret;
2036 WCHAR szDir[MAX_PATH];
2037 char filename[MAX_PATH];
2038 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002039 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002040
Paul Bakker97872ac2012-11-02 12:53:26 +00002041 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002042 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002043
2044 if( len > MAX_PATH - 3 )
2045 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002046
Paul Bakker3338b792012-10-01 21:13:10 +00002047 memset( szDir, 0, sizeof(szDir) );
2048 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002049 memcpy( filename, path, len );
2050 filename[len++] = '\\';
2051 p = filename + len;
2052 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002053
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002054 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002055
Paul Bakker97872ac2012-11-02 12:53:26 +00002056 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002057 if (hFind == INVALID_HANDLE_VALUE)
2058 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2059
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002060 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002061 do
2062 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002063 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002064
Paul Bakkere4791f32012-06-04 21:29:15 +00002065 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002066 continue;
2067
Paul Bakker3338b792012-10-01 21:13:10 +00002068 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2069 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002070 p, len - 1,
2071 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002072
Paul Bakker3338b792012-10-01 21:13:10 +00002073 w_ret = x509parse_crtfile( chain, filename );
2074 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002075 ret++;
2076 else
2077 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002078 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002079 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002080
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002081 if (GetLastError() != ERROR_NO_MORE_FILES)
2082 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002083
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002084cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002085 FindClose( hFind );
2086#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002087 int t_ret, i;
2088 struct stat sb;
2089 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002090 char entry_name[255];
2091 DIR *dir = opendir( path );
2092
2093 if( dir == NULL)
2094 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2095
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002096 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002097 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002098 if( result == NULL )
2099 break;
2100
2101 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2102
2103 i = stat( entry_name, &sb );
2104
2105 if( i == -1 )
2106 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2107
2108 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002109 continue;
2110
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002111 // Ignore parse errors
2112 //
Paul Bakker8d914582012-06-04 12:46:42 +00002113 t_ret = x509parse_crtfile( chain, entry_name );
2114 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002115 ret++;
2116 else
2117 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002118 }
2119 closedir( dir );
2120#endif
2121
2122 return( ret );
2123}
2124
Paul Bakkerd98030e2009-05-02 15:13:40 +00002125/*
2126 * Load one or more CRLs and add them to the chained list
2127 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002128int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002129{
2130 int ret;
2131 size_t n;
2132 unsigned char *buf;
2133
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002134 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002135 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002136
Paul Bakker27fdf462011-06-09 13:55:13 +00002137 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002138
2139 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002140 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002141
2142 return( ret );
2143}
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002146 * Load and parse a private RSA key
2147 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002148int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002149{
2150 int ret;
2151 size_t n;
2152 unsigned char *buf;
2153
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002154 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002155 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002156
2157 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002158 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002159 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002160 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002161 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002162
2163 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002164 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002165
2166 return( ret );
2167}
2168
2169/*
2170 * Load and parse a public RSA key
2171 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002172int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002173{
2174 int ret;
2175 size_t n;
2176 unsigned char *buf;
2177
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002178 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002179 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002180
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002181 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002182
2183 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002184 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002185
2186 return( ret );
2187}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002188
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002189/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002190 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002191 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002192int x509parse_keyfile( pk_context *ctx,
2193 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002194{
2195 int ret;
2196 size_t n;
2197 unsigned char *buf;
2198
2199 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2200 return( ret );
2201
2202 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002203 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002204 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002205 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002206 (const unsigned char *) pwd, strlen( pwd ) );
2207
2208 memset( buf, 0, n + 1 );
2209 free( buf );
2210
2211 return( ret );
2212}
2213
2214/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002215 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002216 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002217int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002218{
2219 int ret;
2220 size_t n;
2221 unsigned char *buf;
2222
2223 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2224 return( ret );
2225
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002226 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002227
2228 memset( buf, 0, n + 1 );
2229 free( buf );
2230
2231 return( ret );
2232}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002233
Paul Bakker335db3f2011-04-25 15:28:35 +00002234#endif /* POLARSSL_FS_IO */
2235
2236/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002237 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002239static int x509parse_key_pkcs1_der( rsa_context *rsa,
2240 const unsigned char *key,
2241 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002242{
Paul Bakker23986e52011-04-24 08:57:21 +00002243 int ret;
2244 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002246
Paul Bakker96743fc2011-02-12 14:30:57 +00002247 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002248 end = p + keylen;
2249
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002251 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002252 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 * RSAPrivateKey ::= SEQUENCE {
2254 * version Version,
2255 * modulus INTEGER, -- n
2256 * publicExponent INTEGER, -- e
2257 * privateExponent INTEGER, -- d
2258 * prime1 INTEGER, -- p
2259 * prime2 INTEGER, -- q
2260 * exponent1 INTEGER, -- d mod (p-1)
2261 * exponent2 INTEGER, -- d mod (q-1)
2262 * coefficient INTEGER, -- (inverse of q) mod p
2263 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2264 * }
2265 */
2266 if( ( ret = asn1_get_tag( &p, end, &len,
2267 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2268 {
Paul Bakker9d781402011-05-09 16:17:09 +00002269 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 }
2271
2272 end = p + len;
2273
2274 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2275 {
Paul Bakker9d781402011-05-09 16:17:09 +00002276 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
2279 if( rsa->ver != 0 )
2280 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002281 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282 }
2283
2284 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2285 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2286 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2287 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2288 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2289 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2290 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2291 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2292 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002294 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
2296
2297 rsa->len = mpi_size( &rsa->N );
2298
2299 if( p != end )
2300 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002303 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305
2306 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2307 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 rsa_free( rsa );
2309 return( ret );
2310 }
2311
Paul Bakkere2f50402013-06-24 19:00:59 +02002312 return( 0 );
2313}
2314
2315/*
2316 * Parse an unencrypted PKCS#8 encoded private RSA key
2317 */
2318static int x509parse_key_pkcs8_unencrypted_der(
2319 rsa_context *rsa,
2320 const unsigned char *key,
2321 size_t keylen )
2322{
2323 int ret;
2324 size_t len;
2325 unsigned char *p, *end;
2326 x509_buf pk_alg_oid;
2327 pk_type_t pk_alg = POLARSSL_PK_NONE;
2328
2329 p = (unsigned char *) key;
2330 end = p + keylen;
2331
2332 /*
2333 * This function parses the PrivatKeyInfo object (PKCS#8)
2334 *
2335 * PrivateKeyInfo ::= SEQUENCE {
2336 * version Version,
2337 * algorithm AlgorithmIdentifier,
2338 * PrivateKey BIT STRING
2339 * }
2340 *
2341 * AlgorithmIdentifier ::= SEQUENCE {
2342 * algorithm OBJECT IDENTIFIER,
2343 * parameters ANY DEFINED BY algorithm OPTIONAL
2344 * }
2345 *
2346 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2347 */
2348 if( ( ret = asn1_get_tag( &p, end, &len,
2349 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2350 {
2351 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2352 }
2353
2354 end = p + len;
2355
2356 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2357 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2358
2359 if( rsa->ver != 0 )
2360 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2361
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002362 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002363 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2364
2365 /*
2366 * only RSA keys handled at this time
2367 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002368 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002369 {
Paul Bakkere2f50402013-06-24 19:00:59 +02002370 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Manuel Pégourié-Gonnard80300ad2013-07-04 11:57:13 +02002371 }
Paul Bakkere2f50402013-06-24 19:00:59 +02002372
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002373 if (pk_alg != POLARSSL_PK_RSA )
2374 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2375
Paul Bakkere2f50402013-06-24 19:00:59 +02002376 /*
2377 * Get the OCTET STRING and parse the PKCS#1 format inside
2378 */
2379 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2380 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2381
2382 if( ( end - p ) < 1 )
2383 {
2384 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2385 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2386 }
2387
2388 end = p + len;
2389
2390 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2391 return( ret );
2392
2393 return( 0 );
2394}
2395
2396/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002397 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002398 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002399static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2400 size_t *used_len,
2401 const unsigned char *key, size_t keylen,
2402 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002403{
2404 int ret;
2405 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002406 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002407 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002408#if defined(POLARSSL_PKCS12_C)
2409 cipher_type_t cipher_alg;
2410 md_type_t md_alg;
2411#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002412
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002413 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002414
2415 p = (unsigned char *) key;
2416 end = p + keylen;
2417
Paul Bakker28144de2013-06-24 19:28:55 +02002418 if( pwdlen == 0 )
2419 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2420
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002421 /*
2422 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2423 *
2424 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2425 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2426 * encryptedData EncryptedData
2427 * }
2428 *
2429 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2430 *
2431 * EncryptedData ::= OCTET STRING
2432 *
2433 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2434 */
2435 if( ( ret = asn1_get_tag( &p, end, &len,
2436 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2437 {
2438 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2439 }
2440
2441 end = p + len;
2442
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002443 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002444 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002445
2446 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2447 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2448
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002449 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002450 return( POLARSSL_ERR_X509_INVALID_INPUT );
2451
2452 /*
2453 * Decrypt EncryptedData with appropriate PDE
2454 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002455#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002456 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002457 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002458 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002459 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002460 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002461 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002462 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2463 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2464
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 return( ret );
2466 }
2467 }
2468 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2469 {
2470 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2471 PKCS12_PBE_DECRYPT,
2472 pwd, pwdlen,
2473 p, len, buf ) ) != 0 )
2474 {
2475 return( ret );
2476 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002477
2478 // Best guess for password mismatch when using RC4. If first tag is
2479 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2480 //
2481 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2482 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002484 else
2485#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002486#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002487 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002488 {
2489 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2490 p, len, buf ) ) != 0 )
2491 {
2492 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2493 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2494
2495 return( ret );
2496 }
2497 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002498 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002499#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002500 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2501
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002502 *used_len = len;
2503 return( 0 );
2504}
2505
2506/*
2507 * Parse an encrypted PKCS#8 encoded private RSA key
2508 */
2509static int x509parse_key_pkcs8_encrypted_der(
2510 rsa_context *rsa,
2511 const unsigned char *key, size_t keylen,
2512 const unsigned char *pwd, size_t pwdlen )
2513{
2514 int ret;
2515 unsigned char buf[2048];
2516 size_t len = 0;
2517
2518 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2519 key, keylen, pwd, pwdlen ) ) != 0 )
2520 {
2521 return( ret );
2522 }
2523
2524 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002525}
2526
2527/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002528 * Parse a private RSA key
2529 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002530int x509parse_key_rsa( rsa_context *rsa,
2531 const unsigned char *key, size_t keylen,
2532 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002533{
2534 int ret;
2535
Paul Bakker96743fc2011-02-12 14:30:57 +00002536#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002537 size_t len;
2538 pem_context pem;
2539
2540 pem_init( &pem );
2541 ret = pem_read_buffer( &pem,
2542 "-----BEGIN RSA PRIVATE KEY-----",
2543 "-----END RSA PRIVATE KEY-----",
2544 key, pwd, pwdlen, &len );
2545 if( ret == 0 )
2546 {
2547 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2548 {
2549 rsa_free( rsa );
2550 }
2551
2552 pem_free( &pem );
2553 return( ret );
2554 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002555 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2556 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2557 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2558 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002559 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002560 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002561
2562 ret = pem_read_buffer( &pem,
2563 "-----BEGIN PRIVATE KEY-----",
2564 "-----END PRIVATE KEY-----",
2565 key, NULL, 0, &len );
2566 if( ret == 0 )
2567 {
2568 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2569 pem.buf, pem.buflen ) ) != 0 )
2570 {
2571 rsa_free( rsa );
2572 }
2573
2574 pem_free( &pem );
2575 return( ret );
2576 }
2577 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002578 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002579
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002580 ret = pem_read_buffer( &pem,
2581 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2582 "-----END ENCRYPTED PRIVATE KEY-----",
2583 key, NULL, 0, &len );
2584 if( ret == 0 )
2585 {
2586 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2587 pem.buf, pem.buflen,
2588 pwd, pwdlen ) ) != 0 )
2589 {
2590 rsa_free( rsa );
2591 }
2592
2593 pem_free( &pem );
2594 return( ret );
2595 }
2596 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002597 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002598#else
2599 ((void) pwd);
2600 ((void) pwdlen);
2601#endif /* POLARSSL_PEM_C */
2602
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002603 /*
2604 * At this point we only know it's not a PEM formatted key. Could be any
2605 * of the known DER encoded private key formats
2606 *
2607 * We try the different DER format parsers to see if one passes without
2608 * error
2609 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002610 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2611 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002612 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002613 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002616 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002617
2618 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2619 {
2620 return( ret );
2621 }
2622
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002623 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2624 return( 0 );
2625
2626 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002627
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002628 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2629 return( 0 );
2630
2631 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002632
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002633 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634}
2635
2636/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002637 * Parse a public RSA key
2638 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002639int x509parse_public_key_rsa( rsa_context *rsa,
2640 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002641{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002642 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002643
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002644 pk_init( &pk_ctx );
2645 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002646
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002647 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002648}
2649
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002650#if defined(POLARSSL_ECP_C)
2651/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002652 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002653 */
2654static int x509parse_key_sec1_der( ecp_keypair *eck,
2655 const unsigned char *key,
2656 size_t keylen )
2657{
2658 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002659 int version;
2660 size_t len;
2661 ecp_group_id grp_id;
2662 unsigned char *p = (unsigned char *) key;
2663 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002664
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002665 /*
2666 * RFC 5915, orf SEC1 Appendix C.4
2667 *
2668 * ECPrivateKey ::= SEQUENCE {
2669 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2670 * privateKey OCTET STRING,
2671 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2672 * publicKey [1] BIT STRING OPTIONAL
2673 * }
2674 */
2675 if( ( ret = asn1_get_tag( &p, end, &len,
2676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2677 {
2678 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2679 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002680
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002681 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002682
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002683 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2684 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002685
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002686 if( version != 1 )
2687 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2688
2689 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2690 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2691
2692 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2693 {
2694 ecp_keypair_free( eck );
2695 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2696 }
2697
2698 p += len;
2699
2700 /*
2701 * Is 'parameters' present?
2702 */
2703 if( ( ret = asn1_get_tag( &p, end, &len,
2704 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2705 {
2706 if( ( ret = x509_get_ecparams( &p, p + len, &grp_id) ) != 0 )
2707 return( ret );
2708
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002709 /*
2710 * If we're wrapped in a bigger structure (eg PKCS#8), grp may have been
2711 * defined externally. In this case, make sure both definitions match.
2712 */
2713 if( eck->grp.id != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002714 {
Manuel Pégourié-Gonnardd4ec21d2013-07-04 12:04:57 +02002715 if( eck->grp.id != grp_id )
2716 {
2717 ecp_keypair_free( eck );
2718 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2719 }
2720 }
2721 else
2722 {
2723 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2724 {
2725 ecp_keypair_free( eck );
2726 return( ret );
2727 }
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002728 }
2729 }
2730 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2731 {
2732 ecp_keypair_free( eck );
2733 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2734 }
2735
2736 /*
2737 * Is 'publickey' present?
2738 */
2739 if( ( ret = asn1_get_tag( &p, end, &len,
2740 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2741 {
2742 if( ( ret = x509_get_subpubkey_ec( &p, p + len, &eck->grp, &eck->Q ) )
2743 != 0 )
2744 {
2745 ecp_keypair_free( eck );
2746 return( ret );
2747 }
2748
2749 if( ( ret = ecp_check_pubkey( &eck->grp, &eck->Q ) ) != 0 )
2750 {
2751 ecp_keypair_free( eck );
2752 return( ret );
2753 }
2754 }
2755 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2756 {
2757 ecp_keypair_free( eck );
2758 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2759 }
2760
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002761 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002762 {
2763 ecp_keypair_free( eck );
2764 return( ret );
2765 }
2766
2767 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002768}
2769
2770/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002771 * Parse an unencrypted PKCS#8 encoded private EC key
2772 */
2773static int x509parse_key_pkcs8_unencrypted_der_ec(
2774 ecp_keypair *eck,
2775 const unsigned char* key,
2776 size_t keylen )
2777{
2778 int ret, version;
2779 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002780 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002781 ecp_group_id grp_id;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002782 unsigned char *p = (unsigned char *) key;
2783 unsigned char *end = p + keylen;
2784 pk_type_t pk_alg = POLARSSL_PK_NONE;
2785
2786 /*
2787 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2788 *
2789 * PrivateKeyInfo ::= SEQUENCE {
2790 * version Version,
2791 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2792 * privateKey PrivateKey,
2793 * attributes [0] IMPLICIT Attributes OPTIONAL }
2794 *
2795 * Version ::= INTEGER
2796 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2797 * PrivateKey ::= OCTET STRING
2798 *
2799 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2800 */
2801
2802 if( ( ret = asn1_get_tag( &p, end, &len,
2803 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2804 {
2805 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2806 }
2807
2808 end = p + len;
2809
2810 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2811 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2812
2813 if( version != 0 )
2814 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2815
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002816 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002817 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2818
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002819 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002820 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002821
2822 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2823 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2824
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002825 if( ( ret = x509_ecparams_get_grp_id( &alg_params, &grp_id ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002826 {
2827 ecp_keypair_free( eck );
2828 return( ret );
2829 }
2830
2831 if( ( ret = ecp_use_known_dp( &eck->grp, grp_id ) ) != 0 )
2832 {
2833 ecp_keypair_free( eck );
2834 return( ret );
2835 }
2836
2837 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2838 {
2839 ecp_keypair_free( eck );
2840 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2841 }
2842
2843 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2844 {
2845 ecp_keypair_free( eck );
2846 return( ret );
2847 }
2848
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002849 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002850 {
2851 ecp_keypair_free( eck );
2852 return( ret );
2853 }
2854
2855 return 0;
2856}
2857
2858/*
2859 * Parse an encrypted PKCS#8 encoded private EC key
2860 */
2861static int x509parse_key_pkcs8_encrypted_der_ec(
2862 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002863 const unsigned char *key, size_t keylen,
2864 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002865{
2866 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002867 unsigned char buf[2048];
2868 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002869
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002870 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2871 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002872 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002873 return( ret );
2874 }
2875
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002876 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002877}
2878
2879/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002880 * Parse a private EC key
2881 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002882static int x509parse_key_ec( ecp_keypair *eck,
2883 const unsigned char *key, size_t keylen,
2884 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002885{
2886 int ret;
2887
2888#if defined(POLARSSL_PEM_C)
2889 size_t len;
2890 pem_context pem;
2891
2892 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002893 ret = pem_read_buffer( &pem,
2894 "-----BEGIN EC PRIVATE KEY-----",
2895 "-----END EC PRIVATE KEY-----",
2896 key, pwd, pwdlen, &len );
2897 if( ret == 0 )
2898 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002899 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002900 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002901 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002902 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002903
2904 pem_free( &pem );
2905 return( ret );
2906 }
2907 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2908 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2909 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2910 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2911 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2912 return( ret );
2913
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002914 ret = pem_read_buffer( &pem,
2915 "-----BEGIN PRIVATE KEY-----",
2916 "-----END PRIVATE KEY-----",
2917 key, NULL, 0, &len );
2918 if( ret == 0 )
2919 {
2920 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2921 pem.buf, pem.buflen ) ) != 0 )
2922 {
2923 ecp_keypair_free( eck );
2924 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002925
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002926 pem_free( &pem );
2927 return( ret );
2928 }
2929 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2930 return( ret );
2931
2932 ret = pem_read_buffer( &pem,
2933 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2934 "-----END ENCRYPTED PRIVATE KEY-----",
2935 key, NULL, 0, &len );
2936 if( ret == 0 )
2937 {
2938 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2939 pem.buf, pem.buflen,
2940 pwd, pwdlen ) ) != 0 )
2941 {
2942 ecp_keypair_free( eck );
2943 }
2944
2945 pem_free( &pem );
2946 return( ret );
2947 }
2948 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2949 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002950#else
2951 ((void) pwd);
2952 ((void) pwdlen);
2953#endif /* POLARSSL_PEM_C */
2954
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002955 /*
2956 * At this point we only know it's not a PEM formatted key. Could be any
2957 * of the known DER encoded private key formats
2958 *
2959 * We try the different DER format parsers to see if one passes without
2960 * error
2961 */
2962 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2963 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002964 {
2965 return( 0 );
2966 }
2967
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002968 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002969
2970 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2971 {
2972 return( ret );
2973 }
2974
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002975 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2976 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002977 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002978
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002979 ecp_keypair_free( eck );
2980
2981 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2982 return( 0 );
2983
2984 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002985
2986 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2987}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002988#endif /* defined(POLARSSL_ECP_C) */
2989
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002990/*
2991 * Parse a private key
2992 */
2993int x509parse_key( pk_context *ctx,
2994 const unsigned char *key, size_t keylen,
2995 const unsigned char *pwd, size_t pwdlen )
2996{
2997 int ret;
2998
2999 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
3000 return( ret );
3001
3002 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
3003 == 0 )
3004 {
3005 return( 0 );
3006 }
3007
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003008 pk_free( ctx );
3009
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003010 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
3011 return( ret );
3012
3013 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
3014 {
3015 return( 0 );
3016 }
3017
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003018 pk_free( ctx );
3019
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003020 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
3021}
3022
3023/*
3024 * Parse a public key
3025 */
3026int x509parse_public_key( pk_context *ctx,
3027 const unsigned char *key, size_t keylen )
3028{
3029 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003030 unsigned char *p;
3031#if defined(POLARSSL_PEM_C)
3032 size_t len;
3033 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003034
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003035 pem_init( &pem );
3036 ret = pem_read_buffer( &pem,
3037 "-----BEGIN PUBLIC KEY-----",
3038 "-----END PUBLIC KEY-----",
3039 key, NULL, 0, &len );
3040
3041 if( ret == 0 )
3042 {
3043 /*
3044 * Was PEM encoded
3045 */
3046 key = pem.buf;
3047 keylen = pem.buflen;
3048 }
3049 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3050 {
3051 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003052 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003053 }
3054#endif
3055 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003056
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003057 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003058
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003059#if defined(POLARSSL_PEM_C)
3060 pem_free( &pem );
3061#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003062
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003063 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003064}
3065
Paul Bakkereaa89f82011-04-04 21:36:15 +00003066#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003067/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003068 * Parse DHM parameters
3069 */
Paul Bakker23986e52011-04-24 08:57:21 +00003070int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003071{
Paul Bakker23986e52011-04-24 08:57:21 +00003072 int ret;
3073 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003074 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003075#if defined(POLARSSL_PEM_C)
3076 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003077
Paul Bakker96743fc2011-02-12 14:30:57 +00003078 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003079
Paul Bakker96743fc2011-02-12 14:30:57 +00003080 ret = pem_read_buffer( &pem,
3081 "-----BEGIN DH PARAMETERS-----",
3082 "-----END DH PARAMETERS-----",
3083 dhmin, NULL, 0, &dhminlen );
3084
3085 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003086 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003087 /*
3088 * Was PEM encoded
3089 */
3090 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003091 }
Paul Bakker00b28602013-06-24 13:02:41 +02003092 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003093 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003094 pem_free( &pem );
3095 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003096 }
3097
Paul Bakker96743fc2011-02-12 14:30:57 +00003098 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3099#else
3100 p = (unsigned char *) dhmin;
3101#endif
3102 end = p + dhminlen;
3103
Paul Bakker1b57b062011-01-06 15:48:19 +00003104 memset( dhm, 0, sizeof( dhm_context ) );
3105
Paul Bakker1b57b062011-01-06 15:48:19 +00003106 /*
3107 * DHParams ::= SEQUENCE {
3108 * prime INTEGER, -- P
3109 * generator INTEGER, -- g
3110 * }
3111 */
3112 if( ( ret = asn1_get_tag( &p, end, &len,
3113 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3114 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003115#if defined(POLARSSL_PEM_C)
3116 pem_free( &pem );
3117#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003118 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003119 }
3120
3121 end = p + len;
3122
3123 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3124 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3125 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003126#if defined(POLARSSL_PEM_C)
3127 pem_free( &pem );
3128#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003129 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003130 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003131 }
3132
3133 if( p != end )
3134 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003135#if defined(POLARSSL_PEM_C)
3136 pem_free( &pem );
3137#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003138 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003140 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3141 }
3142
Paul Bakker96743fc2011-02-12 14:30:57 +00003143#if defined(POLARSSL_PEM_C)
3144 pem_free( &pem );
3145#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003146
3147 return( 0 );
3148}
3149
Paul Bakker335db3f2011-04-25 15:28:35 +00003150#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003151/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003152 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003153 */
3154int x509parse_dhmfile( dhm_context *dhm, const char *path )
3155{
3156 int ret;
3157 size_t n;
3158 unsigned char *buf;
3159
Paul Bakker69e095c2011-12-10 21:55:01 +00003160 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3161 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003162
Paul Bakker27fdf462011-06-09 13:55:13 +00003163 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003164
3165 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003166 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003167
3168 return( ret );
3169}
Paul Bakker335db3f2011-04-25 15:28:35 +00003170#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003171#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003174#include <stdarg.h>
3175
3176#if !defined vsnprintf
3177#define vsnprintf _vsnprintf
3178#endif // vsnprintf
3179
3180/*
3181 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3182 * Result value is not size of buffer needed, but -1 if no fit is possible.
3183 *
3184 * This fuction tries to 'fix' this by at least suggesting enlarging the
3185 * size by 20.
3186 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003187static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003188{
3189 va_list ap;
3190 int res = -1;
3191
3192 va_start( ap, format );
3193
3194 res = vsnprintf( str, size, format, ap );
3195
3196 va_end( ap );
3197
3198 // No quick fix possible
3199 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003200 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003201
3202 return res;
3203}
3204
3205#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003206#endif
3207
Paul Bakkerd98030e2009-05-02 15:13:40 +00003208#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3209
3210#define SAFE_SNPRINTF() \
3211{ \
3212 if( ret == -1 ) \
3213 return( -1 ); \
3214 \
Paul Bakker23986e52011-04-24 08:57:21 +00003215 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216 p[n - 1] = '\0'; \
3217 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3218 } \
3219 \
Paul Bakker23986e52011-04-24 08:57:21 +00003220 n -= (unsigned int) ret; \
3221 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003222}
3223
Paul Bakker5121ce52009-01-03 21:22:43 +00003224/*
3225 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003226 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003228int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229{
Paul Bakker23986e52011-04-24 08:57:21 +00003230 int ret;
3231 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003233 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003234 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 char s[128], *p;
3236
3237 memset( s, 0, sizeof( s ) );
3238
3239 name = dn;
3240 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003241 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
3243 while( name != NULL )
3244 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003245 if( !name->oid.p )
3246 {
3247 name = name->next;
3248 continue;
3249 }
3250
Paul Bakker74111d32011-01-15 16:57:55 +00003251 if( name != dn )
3252 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003253 ret = snprintf( p, n, ", " );
3254 SAFE_SNPRINTF();
3255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Paul Bakkerc70b9822013-04-07 22:00:46 +02003257 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Paul Bakkerc70b9822013-04-07 22:00:46 +02003259 if( ret == 0 )
3260 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003262 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003263 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 for( i = 0; i < name->val.len; i++ )
3266 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003267 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 break;
3269
3270 c = name->val.p[i];
3271 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3272 s[i] = '?';
3273 else s[i] = c;
3274 }
3275 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003276 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003277 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 name = name->next;
3279 }
3280
Paul Bakker23986e52011-04-24 08:57:21 +00003281 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282}
3283
3284/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003285 * Store the serial in printable form into buf; no more
3286 * than size characters will be written
3287 */
3288int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3289{
Paul Bakker23986e52011-04-24 08:57:21 +00003290 int ret;
3291 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003292 char *p;
3293
3294 p = buf;
3295 n = size;
3296
3297 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003298 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003299
3300 for( i = 0; i < nr; i++ )
3301 {
Paul Bakker93048802011-12-05 14:38:06 +00003302 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003303 continue;
3304
Paul Bakkerdd476992011-01-16 21:34:59 +00003305 ret = snprintf( p, n, "%02X%s",
3306 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3307 SAFE_SNPRINTF();
3308 }
3309
Paul Bakker03c7c252011-11-25 12:37:37 +00003310 if( nr != serial->len )
3311 {
3312 ret = snprintf( p, n, "...." );
3313 SAFE_SNPRINTF();
3314 }
3315
Paul Bakker23986e52011-04-24 08:57:21 +00003316 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003317}
3318
3319/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003320 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003322int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3323 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003324{
Paul Bakker23986e52011-04-24 08:57:21 +00003325 int ret;
3326 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003327 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003328 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003329
3330 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003331 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003332
Paul Bakkerd98030e2009-05-02 15:13:40 +00003333 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003335 SAFE_SNPRINTF();
3336 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003338 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
Paul Bakkerdd476992011-01-16 21:34:59 +00003340 ret = x509parse_serial_gets( p, n, &crt->serial);
3341 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakkerd98030e2009-05-02 15:13:40 +00003343 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3344 SAFE_SNPRINTF();
3345 ret = x509parse_dn_gets( p, n, &crt->issuer );
3346 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003347
Paul Bakkerd98030e2009-05-02 15:13:40 +00003348 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3349 SAFE_SNPRINTF();
3350 ret = x509parse_dn_gets( p, n, &crt->subject );
3351 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003352
Paul Bakkerd98030e2009-05-02 15:13:40 +00003353 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3355 crt->valid_from.year, crt->valid_from.mon,
3356 crt->valid_from.day, crt->valid_from.hour,
3357 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003358 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003359
Paul Bakkerd98030e2009-05-02 15:13:40 +00003360 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3362 crt->valid_to.year, crt->valid_to.mon,
3363 crt->valid_to.day, crt->valid_to.hour,
3364 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003365 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Paul Bakkerc70b9822013-04-07 22:00:46 +02003367 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003368 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Paul Bakkerc70b9822013-04-07 22:00:46 +02003370 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3371 if( ret != 0 )
3372 ret = snprintf( p, n, "???" );
3373 else
3374 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003375 SAFE_SNPRINTF();
3376
3377 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00003378 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003379 SAFE_SNPRINTF();
3380
Paul Bakker23986e52011-04-24 08:57:21 +00003381 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003382}
3383
Paul Bakker74111d32011-01-15 16:57:55 +00003384/*
3385 * Return an informational string describing the given OID
3386 */
3387const char *x509_oid_get_description( x509_buf *oid )
3388{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003389 const char *desc = NULL;
3390 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003391
Paul Bakkerc70b9822013-04-07 22:00:46 +02003392 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003393
Paul Bakkerc70b9822013-04-07 22:00:46 +02003394 if( ret != 0 )
3395 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003396
Paul Bakkerc70b9822013-04-07 22:00:46 +02003397 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003398}
3399
3400/* Return the x.y.z.... style numeric string for the given OID */
3401int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3402{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003403 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003404}
3405
Paul Bakkerd98030e2009-05-02 15:13:40 +00003406/*
3407 * Return an informational string about the CRL.
3408 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003409int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3410 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003411{
Paul Bakker23986e52011-04-24 08:57:21 +00003412 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003413 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003414 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003415 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003416 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003417
3418 p = buf;
3419 n = size;
3420
3421 ret = snprintf( p, n, "%sCRL version : %d",
3422 prefix, crl->version );
3423 SAFE_SNPRINTF();
3424
3425 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3426 SAFE_SNPRINTF();
3427 ret = x509parse_dn_gets( p, n, &crl->issuer );
3428 SAFE_SNPRINTF();
3429
3430 ret = snprintf( p, n, "\n%sthis update : " \
3431 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3432 crl->this_update.year, crl->this_update.mon,
3433 crl->this_update.day, crl->this_update.hour,
3434 crl->this_update.min, crl->this_update.sec );
3435 SAFE_SNPRINTF();
3436
3437 ret = snprintf( p, n, "\n%snext update : " \
3438 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3439 crl->next_update.year, crl->next_update.mon,
3440 crl->next_update.day, crl->next_update.hour,
3441 crl->next_update.min, crl->next_update.sec );
3442 SAFE_SNPRINTF();
3443
3444 entry = &crl->entry;
3445
3446 ret = snprintf( p, n, "\n%sRevoked certificates:",
3447 prefix );
3448 SAFE_SNPRINTF();
3449
Paul Bakker9be19372009-07-27 20:21:53 +00003450 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003451 {
3452 ret = snprintf( p, n, "\n%sserial number: ",
3453 prefix );
3454 SAFE_SNPRINTF();
3455
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003456 ret = x509parse_serial_gets( p, n, &entry->serial);
3457 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003458
Paul Bakkerd98030e2009-05-02 15:13:40 +00003459 ret = snprintf( p, n, " revocation date: " \
3460 "%04d-%02d-%02d %02d:%02d:%02d",
3461 entry->revocation_date.year, entry->revocation_date.mon,
3462 entry->revocation_date.day, entry->revocation_date.hour,
3463 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003464 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003465
3466 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 }
3468
Paul Bakkerc70b9822013-04-07 22:00:46 +02003469 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003470 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Paul Bakkerc70b9822013-04-07 22:00:46 +02003472 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3473 if( ret != 0 )
3474 ret = snprintf( p, n, "???" );
3475 else
3476 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003477 SAFE_SNPRINTF();
3478
Paul Bakker1e27bb22009-07-19 20:25:25 +00003479 ret = snprintf( p, n, "\n" );
3480 SAFE_SNPRINTF();
3481
Paul Bakker23986e52011-04-24 08:57:21 +00003482 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483}
3484
3485/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003486 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003488#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003489int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003490{
Paul Bakkercce9d772011-11-18 14:26:47 +00003491 int year, mon, day;
3492 int hour, min, sec;
3493
3494#if defined(_WIN32)
3495 SYSTEMTIME st;
3496
3497 GetLocalTime(&st);
3498
3499 year = st.wYear;
3500 mon = st.wMonth;
3501 day = st.wDay;
3502 hour = st.wHour;
3503 min = st.wMinute;
3504 sec = st.wSecond;
3505#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 struct tm *lt;
3507 time_t tt;
3508
3509 tt = time( NULL );
3510 lt = localtime( &tt );
3511
Paul Bakkercce9d772011-11-18 14:26:47 +00003512 year = lt->tm_year + 1900;
3513 mon = lt->tm_mon + 1;
3514 day = lt->tm_mday;
3515 hour = lt->tm_hour;
3516 min = lt->tm_min;
3517 sec = lt->tm_sec;
3518#endif
3519
3520 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003521 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Paul Bakkercce9d772011-11-18 14:26:47 +00003523 if( year == to->year &&
3524 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003525 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
Paul Bakkercce9d772011-11-18 14:26:47 +00003527 if( year == to->year &&
3528 mon == to->mon &&
3529 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003530 return( 1 );
3531
Paul Bakkercce9d772011-11-18 14:26:47 +00003532 if( year == to->year &&
3533 mon == to->mon &&
3534 day == to->day &&
3535 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003536 return( 1 );
3537
Paul Bakkercce9d772011-11-18 14:26:47 +00003538 if( year == to->year &&
3539 mon == to->mon &&
3540 day == to->day &&
3541 hour == to->hour &&
3542 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003543 return( 1 );
3544
Paul Bakkercce9d772011-11-18 14:26:47 +00003545 if( year == to->year &&
3546 mon == to->mon &&
3547 day == to->day &&
3548 hour == to->hour &&
3549 min == to->min &&
3550 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003551 return( 1 );
3552
Paul Bakker40ea7de2009-05-03 10:18:48 +00003553 return( 0 );
3554}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003555#else /* POLARSSL_HAVE_TIME */
3556int x509parse_time_expired( const x509_time *to )
3557{
3558 ((void) to);
3559 return( 0 );
3560}
3561#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003562
3563/*
3564 * Return 1 if the certificate is revoked, or 0 otherwise.
3565 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003566int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003567{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003568 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003569
3570 while( cur != NULL && cur->serial.len != 0 )
3571 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003572 if( crt->serial.len == cur->serial.len &&
3573 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003574 {
3575 if( x509parse_time_expired( &cur->revocation_date ) )
3576 return( 1 );
3577 }
3578
3579 cur = cur->next;
3580 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
3582 return( 0 );
3583}
3584
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003585/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003586 * Check that the given certificate is valid accoring to the CRL.
3587 */
3588static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3589 x509_crl *crl_list)
3590{
3591 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003592 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3593 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003594
Paul Bakker915275b2012-09-28 07:10:55 +00003595 if( ca == NULL )
3596 return( flags );
3597
Paul Bakker76fd75a2011-01-16 21:12:10 +00003598 /*
3599 * TODO: What happens if no CRL is present?
3600 * Suggestion: Revocation state should be unknown if no CRL is present.
3601 * For backwards compatibility this is not yet implemented.
3602 */
3603
Paul Bakker915275b2012-09-28 07:10:55 +00003604 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003605 {
Paul Bakker915275b2012-09-28 07:10:55 +00003606 if( crl_list->version == 0 ||
3607 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003608 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3609 crl_list->issuer_raw.len ) != 0 )
3610 {
3611 crl_list = crl_list->next;
3612 continue;
3613 }
3614
3615 /*
3616 * Check if CRL is correctly signed by the trusted CA
3617 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003618 md_info = md_info_from_type( crl_list->sig_md );
3619 if( md_info == NULL )
3620 {
3621 /*
3622 * Cannot check 'unknown' hash
3623 */
3624 flags |= BADCRL_NOT_TRUSTED;
3625 break;
3626 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003627
Paul Bakkerc70b9822013-04-07 22:00:46 +02003628 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003629
Paul Bakkerc70b9822013-04-07 22:00:46 +02003630 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003631 0, hash, crl_list->sig.p ) == 0 )
3632 {
3633 /*
3634 * CRL is not trusted
3635 */
3636 flags |= BADCRL_NOT_TRUSTED;
3637 break;
3638 }
3639
3640 /*
3641 * Check for validity of CRL (Do not drop out)
3642 */
3643 if( x509parse_time_expired( &crl_list->next_update ) )
3644 flags |= BADCRL_EXPIRED;
3645
3646 /*
3647 * Check if certificate is revoked
3648 */
3649 if( x509parse_revoked(crt, crl_list) )
3650 {
3651 flags |= BADCERT_REVOKED;
3652 break;
3653 }
3654
3655 crl_list = crl_list->next;
3656 }
3657 return flags;
3658}
3659
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003660static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003661{
3662 size_t i;
3663 size_t cn_idx = 0;
3664
Paul Bakker57b12982012-02-11 17:38:38 +00003665 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003666 return( 0 );
3667
3668 for( i = 0; i < strlen( cn ); ++i )
3669 {
3670 if( cn[i] == '.' )
3671 {
3672 cn_idx = i;
3673 break;
3674 }
3675 }
3676
3677 if( cn_idx == 0 )
3678 return( 0 );
3679
Paul Bakker535e97d2012-08-23 10:49:55 +00003680 if( strlen( cn ) - cn_idx == name->len - 1 &&
3681 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003682 {
3683 return( 1 );
3684 }
3685
3686 return( 0 );
3687}
3688
Paul Bakker915275b2012-09-28 07:10:55 +00003689static int x509parse_verify_top(
3690 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003691 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003692 int (*f_vrfy)(void *, x509_cert *, int, int *),
3693 void *p_vrfy )
3694{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003695 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003696 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003697 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3698 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003699
3700 if( x509parse_time_expired( &child->valid_to ) )
3701 *flags |= BADCERT_EXPIRED;
3702
3703 /*
3704 * Child is the top of the chain. Check against the trust_ca list.
3705 */
3706 *flags |= BADCERT_NOT_TRUSTED;
3707
3708 while( trust_ca != NULL )
3709 {
3710 if( trust_ca->version == 0 ||
3711 child->issuer_raw.len != trust_ca->subject_raw.len ||
3712 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3713 child->issuer_raw.len ) != 0 )
3714 {
3715 trust_ca = trust_ca->next;
3716 continue;
3717 }
3718
Paul Bakker9a736322012-11-14 12:39:52 +00003719 /*
3720 * Reduce path_len to check against if top of the chain is
3721 * the same as the trusted CA
3722 */
3723 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3724 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3725 child->issuer_raw.len ) == 0 )
3726 {
3727 check_path_cnt--;
3728 }
3729
Paul Bakker915275b2012-09-28 07:10:55 +00003730 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003731 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003732 {
3733 trust_ca = trust_ca->next;
3734 continue;
3735 }
3736
Paul Bakkerc70b9822013-04-07 22:00:46 +02003737 md_info = md_info_from_type( child->sig_md );
3738 if( md_info == NULL )
3739 {
3740 /*
3741 * Cannot check 'unknown' hash
3742 */
3743 continue;
3744 }
Paul Bakker915275b2012-09-28 07:10:55 +00003745
Paul Bakkerc70b9822013-04-07 22:00:46 +02003746 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003747
Paul Bakkerc70b9822013-04-07 22:00:46 +02003748 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003749 0, hash, child->sig.p ) != 0 )
3750 {
3751 trust_ca = trust_ca->next;
3752 continue;
3753 }
3754
3755 /*
3756 * Top of chain is signed by a trusted CA
3757 */
3758 *flags &= ~BADCERT_NOT_TRUSTED;
3759 break;
3760 }
3761
Paul Bakker9a736322012-11-14 12:39:52 +00003762 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003763 * If top of chain is not the same as the trusted CA send a verify request
3764 * to the callback for any issues with validity and CRL presence for the
3765 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003766 */
3767 if( trust_ca != NULL &&
3768 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3769 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3770 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003771 {
3772 /* Check trusted CA's CRL for then chain's top crt */
3773 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3774
3775 if( x509parse_time_expired( &trust_ca->valid_to ) )
3776 ca_flags |= BADCERT_EXPIRED;
3777
Paul Bakker915275b2012-09-28 07:10:55 +00003778 if( NULL != f_vrfy )
3779 {
Paul Bakker9a736322012-11-14 12:39:52 +00003780 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003781 return( ret );
3782 }
3783 }
3784
3785 /* Call callback on top cert */
3786 if( NULL != f_vrfy )
3787 {
Paul Bakker9a736322012-11-14 12:39:52 +00003788 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003789 return( ret );
3790 }
3791
Paul Bakker915275b2012-09-28 07:10:55 +00003792 *flags |= ca_flags;
3793
3794 return( 0 );
3795}
3796
3797static int x509parse_verify_child(
3798 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003799 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003800 int (*f_vrfy)(void *, x509_cert *, int, int *),
3801 void *p_vrfy )
3802{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003803 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003804 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003805 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003806 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003807 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003808
3809 if( x509parse_time_expired( &child->valid_to ) )
3810 *flags |= BADCERT_EXPIRED;
3811
Paul Bakkerc70b9822013-04-07 22:00:46 +02003812 md_info = md_info_from_type( child->sig_md );
3813 if( md_info == NULL )
3814 {
3815 /*
3816 * Cannot check 'unknown' hash
3817 */
Paul Bakker915275b2012-09-28 07:10:55 +00003818 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003819 }
3820 else
3821 {
3822 md( md_info, child->tbs.p, child->tbs.len, hash );
3823
3824 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3825 child->sig.p ) != 0 )
3826 *flags |= BADCERT_NOT_TRUSTED;
3827 }
3828
Paul Bakker915275b2012-09-28 07:10:55 +00003829 /* Check trusted CA's CRL for the given crt */
3830 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3831
3832 grandparent = parent->next;
3833
3834 while( grandparent != NULL )
3835 {
3836 if( grandparent->version == 0 ||
3837 grandparent->ca_istrue == 0 ||
3838 parent->issuer_raw.len != grandparent->subject_raw.len ||
3839 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3840 parent->issuer_raw.len ) != 0 )
3841 {
3842 grandparent = grandparent->next;
3843 continue;
3844 }
3845 break;
3846 }
3847
Paul Bakker915275b2012-09-28 07:10:55 +00003848 if( grandparent != NULL )
3849 {
3850 /*
3851 * Part of the chain
3852 */
Paul Bakker9a736322012-11-14 12:39:52 +00003853 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 +00003854 if( ret != 0 )
3855 return( ret );
3856 }
3857 else
3858 {
Paul Bakker9a736322012-11-14 12:39:52 +00003859 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 +00003860 if( ret != 0 )
3861 return( ret );
3862 }
3863
3864 /* child is verified to be a child of the parent, call verify callback */
3865 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003866 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003867 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003868
3869 *flags |= parent_flags;
3870
3871 return( 0 );
3872}
3873
Paul Bakker76fd75a2011-01-16 21:12:10 +00003874/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 * Verify the certificate validity
3876 */
3877int x509parse_verify( x509_cert *crt,
3878 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003879 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003880 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003881 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003882 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
Paul Bakker23986e52011-04-24 08:57:21 +00003884 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003885 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003886 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003887 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003889 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003890
Paul Bakker40ea7de2009-05-03 10:18:48 +00003891 *flags = 0;
3892
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 if( cn != NULL )
3894 {
3895 name = &crt->subject;
3896 cn_len = strlen( cn );
3897
Paul Bakker4d2c1242012-05-10 14:12:46 +00003898 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003900 cur = &crt->subject_alt_names;
3901
3902 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003903 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003904 if( cur->buf.len == cn_len &&
3905 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003906 break;
3907
Paul Bakker535e97d2012-08-23 10:49:55 +00003908 if( cur->buf.len > 2 &&
3909 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003910 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003911 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003912
Paul Bakker4d2c1242012-05-10 14:12:46 +00003913 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003914 }
3915
3916 if( cur == NULL )
3917 *flags |= BADCERT_CN_MISMATCH;
3918 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003919 else
3920 {
3921 while( name != NULL )
3922 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003923 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003924 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003925 if( name->val.len == cn_len &&
3926 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003927 break;
3928
Paul Bakker535e97d2012-08-23 10:49:55 +00003929 if( name->val.len > 2 &&
3930 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003931 x509_wildcard_verify( cn, &name->val ) )
3932 break;
3933 }
3934
3935 name = name->next;
3936 }
3937
3938 if( name == NULL )
3939 *flags |= BADCERT_CN_MISMATCH;
3940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003941 }
3942
Paul Bakker5121ce52009-01-03 21:22:43 +00003943 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003944 * Iterate upwards in the given cert chain, to find our crt parent.
3945 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003947 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
Paul Bakker76fd75a2011-01-16 21:12:10 +00003949 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003950 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003951 if( parent->ca_istrue == 0 ||
3952 crt->issuer_raw.len != parent->subject_raw.len ||
3953 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003954 crt->issuer_raw.len ) != 0 )
3955 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003956 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003957 continue;
3958 }
Paul Bakker915275b2012-09-28 07:10:55 +00003959 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 }
3961
Paul Bakker915275b2012-09-28 07:10:55 +00003962 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 {
Paul Bakker915275b2012-09-28 07:10:55 +00003964 /*
3965 * Part of the chain
3966 */
Paul Bakker9a736322012-11-14 12:39:52 +00003967 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003968 if( ret != 0 )
3969 return( ret );
3970 }
3971 else
Paul Bakker74111d32011-01-15 16:57:55 +00003972 {
Paul Bakker9a736322012-11-14 12:39:52 +00003973 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003974 if( ret != 0 )
3975 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003976 }
Paul Bakker915275b2012-09-28 07:10:55 +00003977
3978 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003979 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003980
Paul Bakker5121ce52009-01-03 21:22:43 +00003981 return( 0 );
3982}
3983
3984/*
3985 * Unallocate all certificate data
3986 */
3987void x509_free( x509_cert *crt )
3988{
3989 x509_cert *cert_cur = crt;
3990 x509_cert *cert_prv;
3991 x509_name *name_cur;
3992 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003993 x509_sequence *seq_cur;
3994 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
3996 if( crt == NULL )
3997 return;
3998
3999 do
4000 {
4001 rsa_free( &cert_cur->rsa );
4002
4003 name_cur = cert_cur->issuer.next;
4004 while( name_cur != NULL )
4005 {
4006 name_prv = name_cur;
4007 name_cur = name_cur->next;
4008 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004009 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 }
4011
4012 name_cur = cert_cur->subject.next;
4013 while( name_cur != NULL )
4014 {
4015 name_prv = name_cur;
4016 name_cur = name_cur->next;
4017 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004018 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019 }
4020
Paul Bakker74111d32011-01-15 16:57:55 +00004021 seq_cur = cert_cur->ext_key_usage.next;
4022 while( seq_cur != NULL )
4023 {
4024 seq_prv = seq_cur;
4025 seq_cur = seq_cur->next;
4026 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004027 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004028 }
4029
Paul Bakker8afa70d2012-02-11 18:42:45 +00004030 seq_cur = cert_cur->subject_alt_names.next;
4031 while( seq_cur != NULL )
4032 {
4033 seq_prv = seq_cur;
4034 seq_cur = seq_cur->next;
4035 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004036 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004037 }
4038
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 if( cert_cur->raw.p != NULL )
4040 {
4041 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004042 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 }
4044
4045 cert_cur = cert_cur->next;
4046 }
4047 while( cert_cur != NULL );
4048
4049 cert_cur = crt;
4050 do
4051 {
4052 cert_prv = cert_cur;
4053 cert_cur = cert_cur->next;
4054
4055 memset( cert_prv, 0, sizeof( x509_cert ) );
4056 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004057 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 }
4059 while( cert_cur != NULL );
4060}
4061
Paul Bakkerd98030e2009-05-02 15:13:40 +00004062/*
4063 * Unallocate all CRL data
4064 */
4065void x509_crl_free( x509_crl *crl )
4066{
4067 x509_crl *crl_cur = crl;
4068 x509_crl *crl_prv;
4069 x509_name *name_cur;
4070 x509_name *name_prv;
4071 x509_crl_entry *entry_cur;
4072 x509_crl_entry *entry_prv;
4073
4074 if( crl == NULL )
4075 return;
4076
4077 do
4078 {
4079 name_cur = crl_cur->issuer.next;
4080 while( name_cur != NULL )
4081 {
4082 name_prv = name_cur;
4083 name_cur = name_cur->next;
4084 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004085 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004086 }
4087
4088 entry_cur = crl_cur->entry.next;
4089 while( entry_cur != NULL )
4090 {
4091 entry_prv = entry_cur;
4092 entry_cur = entry_cur->next;
4093 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004094 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004095 }
4096
4097 if( crl_cur->raw.p != NULL )
4098 {
4099 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004100 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004101 }
4102
4103 crl_cur = crl_cur->next;
4104 }
4105 while( crl_cur != NULL );
4106
4107 crl_cur = crl;
4108 do
4109 {
4110 crl_prv = crl_cur;
4111 crl_cur = crl_cur->next;
4112
4113 memset( crl_prv, 0, sizeof( x509_crl ) );
4114 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004115 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004116 }
4117 while( crl_cur != NULL );
4118}
4119
Paul Bakker40e46942009-01-03 21:51:57 +00004120#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004121
Paul Bakker40e46942009-01-03 21:51:57 +00004122#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004123
4124/*
4125 * Checkup routine
4126 */
4127int x509_self_test( int verbose )
4128{
Paul Bakker5690efc2011-05-26 13:16:06 +00004129#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004130 int ret;
4131 int flags;
4132 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004133 x509_cert cacert;
4134 x509_cert clicert;
4135 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004136#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004137 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004139
4140 if( verbose != 0 )
4141 printf( " X.509 certificate load: " );
4142
4143 memset( &clicert, 0, sizeof( x509_cert ) );
4144
Paul Bakker3c2122f2013-06-24 19:03:14 +02004145 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004146 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147 if( ret != 0 )
4148 {
4149 if( verbose != 0 )
4150 printf( "failed\n" );
4151
4152 return( ret );
4153 }
4154
4155 memset( &cacert, 0, sizeof( x509_cert ) );
4156
Paul Bakker3c2122f2013-06-24 19:03:14 +02004157 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004158 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004159 if( ret != 0 )
4160 {
4161 if( verbose != 0 )
4162 printf( "failed\n" );
4163
4164 return( ret );
4165 }
4166
4167 if( verbose != 0 )
4168 printf( "passed\n X.509 private key load: " );
4169
4170 i = strlen( test_ca_key );
4171 j = strlen( test_ca_pwd );
4172
Paul Bakker66b78b22011-03-25 14:22:50 +00004173 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4174
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004175 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004176 (const unsigned char *) test_ca_key, i,
4177 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004178 {
4179 if( verbose != 0 )
4180 printf( "failed\n" );
4181
4182 return( ret );
4183 }
4184
4185 if( verbose != 0 )
4186 printf( "passed\n X.509 signature verify: ");
4187
Paul Bakker23986e52011-04-24 08:57:21 +00004188 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004189 if( ret != 0 )
4190 {
Paul Bakker23986e52011-04-24 08:57:21 +00004191 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 if( verbose != 0 )
4193 printf( "failed\n" );
4194
4195 return( ret );
4196 }
4197
Paul Bakker5690efc2011-05-26 13:16:06 +00004198#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004199 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004200 printf( "passed\n X.509 DHM parameter load: " );
4201
4202 i = strlen( test_dhm_params );
4203 j = strlen( test_ca_pwd );
4204
Paul Bakker3c2122f2013-06-24 19:03:14 +02004205 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004206 {
4207 if( verbose != 0 )
4208 printf( "failed\n" );
4209
4210 return( ret );
4211 }
4212
4213 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004214 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004216
4217 x509_free( &cacert );
4218 x509_free( &clicert );
4219 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004220#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004221 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004223
4224 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004225#else
4226 ((void) verbose);
4227 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4228#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004229}
4230
4231#endif
4232
4233#endif