blob: ee9fe24b4f5ca3ac51c224744f7bc8ad28ad4d39 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200163/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200172{
173 int ret;
174 x509_buf alg_oid;
175
176 memset( params, 0, sizeof(asn1_buf) );
177
178 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
182 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
183
184 /*
185 * No parameters with RSA (only for EC)
186 */
187 if( *pk_alg == POLARSSL_PK_RSA &&
188 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
189 params->len != 0 ) )
190 {
191 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
192 }
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197/* Get an algorithm identifier without parameters (eg for signatures)
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AlgorithmIdentifier ::= SEQUENCE {
200 * algorithm OBJECT IDENTIFIER,
201 * parameters ANY DEFINED BY algorithm OPTIONAL }
202 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200203static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
204 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200208 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( 0 );
212}
213
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200214/* Get an EC group id from an ECParameters buffer
215 *
216 * ECParameters ::= CHOICE {
217 * namedCurve OBJECT IDENTIFIER
218 * -- implicitCurve NULL
219 * -- specifiedCurve SpecifiedECDomain
220 * }
221 */
222static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200223 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200224{
225 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200227 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
231
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200232 params->p = *p;
233 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200234
235 if( *p != end )
236 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
237 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
238
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200239 return( 0 );
240}
241
242/*
243 * Use EC parameters to initialise an EC group
244 */
245static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
246{
247 int ret;
248 ecp_group_id grp_id;
249
250 if( oid_get_ec_grp( params, &grp_id ) != 0 )
251 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
252
253 /*
254 * grp may already be initilialized; if so, make sure IDs match
255 */
256 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
257 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
258
259 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
260 return( ret );
261
262 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200263}
264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265/*
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200266 * subjectPublicKey BIT STRING
267 * -- which, in our case, contains
268 * ECPoint ::= octet string (not ASN.1)
269 */
270static int x509_get_subpubkey_ec( unsigned char **p, const unsigned char *end,
271 const ecp_group *grp, ecp_point *pt )
272{
273 int ret;
274 size_t len;
275
276 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
277 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
278
279 if( *p + len != end )
280 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
281 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
282
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200283 if( --len < 1 || *(*p)++ != 0 )
284 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200285
286 if( ( ret = ecp_point_read_binary( grp, pt,
287 (const unsigned char *) *p, len ) ) != 0 )
288 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200289 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
290 }
291
292 *p += len;
293
294 if( ( ret = ecp_check_pubkey( grp, pt ) ) != 0 )
295 {
296 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnard73c0cda2013-07-01 19:45:45 +0200297 }
298
299 return( 0 );
300}
301
302/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 * AttributeTypeAndValue ::= SEQUENCE {
304 * type AttributeType,
305 * value AttributeValue }
306 *
307 * AttributeType ::= OBJECT IDENTIFIER
308 *
309 * AttributeValue ::= ANY DEFINED BY AttributeType
310 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000311static int x509_get_attr_type_value( unsigned char **p,
312 const unsigned char *end,
313 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000314{
Paul Bakker23986e52011-04-24 08:57:21 +0000315 int ret;
316 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 x509_buf *oid;
318 x509_buf *val;
319
320 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000322 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 oid = &cur->oid;
325 oid->tag = **p;
326
327 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000328 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 oid->p = *p;
331 *p += oid->len;
332
333 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000334 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000335 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
338 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
339 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000340 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000341 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343 val = &cur->val;
344 val->tag = *(*p)++;
345
346 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000347 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
349 val->p = *p;
350 *p += val->len;
351
352 cur->next = NULL;
353
Paul Bakker400ff6f2011-02-20 10:40:16 +0000354 return( 0 );
355}
356
357/*
358 * RelativeDistinguishedName ::=
359 * SET OF AttributeTypeAndValue
360 *
361 * AttributeTypeAndValue ::= SEQUENCE {
362 * type AttributeType,
363 * value AttributeValue }
364 *
365 * AttributeType ::= OBJECT IDENTIFIER
366 *
367 * AttributeValue ::= ANY DEFINED BY AttributeType
368 */
369static int x509_get_name( unsigned char **p,
370 const unsigned char *end,
371 x509_name *cur )
372{
Paul Bakker23986e52011-04-24 08:57:21 +0000373 int ret;
374 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000375 const unsigned char *end2;
376 x509_name *use;
377
378 if( ( ret = asn1_get_tag( p, end, &len,
379 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000380 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000381
382 end2 = end;
383 end = *p + len;
384 use = cur;
385
386 do
387 {
388 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
389 return( ret );
390
391 if( *p != end )
392 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200393 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000394 sizeof( x509_name ) );
395
396 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000397 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000398
399 memset( use->next, 0, sizeof( x509_name ) );
400
401 use = use->next;
402 }
403 }
404 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 /*
407 * recurse until end of SEQUENCE is reached
408 */
409 if( *p == end2 )
410 return( 0 );
411
Paul Bakker6e339b52013-07-03 13:37:05 +0200412 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 sizeof( x509_name ) );
414
415 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000416 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Paul Bakker430ffbe2012-05-01 08:14:20 +0000418 memset( cur->next, 0, sizeof( x509_name ) );
419
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 return( x509_get_name( p, end2, cur->next ) );
421}
422
423/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 * Time ::= CHOICE {
425 * utcTime UTCTime,
426 * generalTime GeneralizedTime }
427 */
Paul Bakker91200182010-02-18 21:26:15 +0000428static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000429 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000430 x509_time *time )
431{
Paul Bakker23986e52011-04-24 08:57:21 +0000432 int ret;
433 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000434 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000435 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000436
Paul Bakker91200182010-02-18 21:26:15 +0000437 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000438 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
439 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000440
Paul Bakker91200182010-02-18 21:26:15 +0000441 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000442
Paul Bakker91200182010-02-18 21:26:15 +0000443 if ( tag == ASN1_UTC_TIME )
444 {
445 (*p)++;
446 ret = asn1_get_len( p, end, &len );
447
448 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000449 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000450
Paul Bakker91200182010-02-18 21:26:15 +0000451 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000452 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
453 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000454
Paul Bakker91200182010-02-18 21:26:15 +0000455 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
456 &time->year, &time->mon, &time->day,
457 &time->hour, &time->min, &time->sec ) < 5 )
458 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000459
Paul Bakker400ff6f2011-02-20 10:40:16 +0000460 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000461 time->year += 1900;
462
463 *p += len;
464
465 return( 0 );
466 }
467 else if ( tag == ASN1_GENERALIZED_TIME )
468 {
469 (*p)++;
470 ret = asn1_get_len( p, end, &len );
471
472 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000473 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000474
475 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000476 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
477 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000478
479 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
480 &time->year, &time->mon, &time->day,
481 &time->hour, &time->min, &time->sec ) < 5 )
482 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
483
484 *p += len;
485
486 return( 0 );
487 }
488 else
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000490}
491
492
493/*
494 * Validity ::= SEQUENCE {
495 * notBefore Time,
496 * notAfter Time }
497 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000498static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000499 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 x509_time *from,
501 x509_time *to )
502{
Paul Bakker23986e52011-04-24 08:57:21 +0000503 int ret;
504 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 if( ( ret = asn1_get_tag( p, end, &len,
507 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000508 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 end = *p + len;
511
Paul Bakker91200182010-02-18 21:26:15 +0000512 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000513 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker91200182010-02-18 21:26:15 +0000515 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000520 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 return( 0 );
523}
524
525/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200526 * RSAPublicKey ::= SEQUENCE {
527 * modulus INTEGER, -- n
528 * publicExponent INTEGER -- e
529 * }
530 */
531static int x509_get_rsapubkey( unsigned char **p,
532 const unsigned char *end,
533 rsa_context *rsa )
534{
535 int ret;
536 size_t len;
537
538 if( ( ret = asn1_get_tag( p, end, &len,
539 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
540 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
541
542 if( *p + len != end )
543 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
544 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
545
546 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
547 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
549
550 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
551 return( ret );
552
553 rsa->len = mpi_size( &rsa->N );
554
555 return( 0 );
556}
557
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200558static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
559 x509_buf *alg_params, ecp_keypair *key )
560{
561 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200562
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200563 if( ( ret = x509_use_ecparams( alg_params, &key->grp ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200564 return( ret );
565
566 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
567 (const unsigned char *) *p, end - *p ) ) != 0 )
568 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200569 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
570 }
571
572 *p = (unsigned char *) end;
573
574 if( ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
575 {
576 ecp_keypair_free( key );
577 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200578 }
579
580 return( 0 );
581}
582
583/*
584 * SubjectPublicKeyInfo ::= SEQUENCE {
585 * algorithm AlgorithmIdentifier,
586 * subjectPublicKey BIT STRING }
587 */
588static int x509_get_pubkey( unsigned char **p,
589 const unsigned char *end,
590 pk_context *pk )
591{
592 int ret;
593 size_t len;
594 x509_buf alg_params;
595 pk_type_t pk_alg = POLARSSL_PK_NONE;
596
597 if( ( ret = asn1_get_tag( p, end, &len,
598 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
599 {
600 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
601 }
602
603 end = *p + len;
604
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200605 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200606 return( ret );
607
608 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
609 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
610
611 if( ( end - *p ) < 1 )
612 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
613 POLARSSL_ERR_ASN1_OUT_OF_DATA );
614
615 if( *p + len != end )
616 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
617 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
618
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200619 if( --len < 1 || *(*p)++ != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200620 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
621
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200622 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
623 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200624
625 switch( pk_alg )
626 {
627 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200628 case POLARSSL_PK_ECDSA:
629 /* Should never happen */
630 ret = POLARSSL_ERR_X509_CERT_INVALID_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200631 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200632
633 case POLARSSL_PK_RSA:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200634 ret = x509_get_rsapubkey( p, end, pk->data );
635 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200636
637 case POLARSSL_PK_ECKEY_DH:
638 ((ecp_keypair *) pk->data)->alg = POLARSSL_ECP_KEY_ALG_ECDH;
639 /* FALLTHROUGH */
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200640
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200641 case POLARSSL_PK_ECKEY:
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200642 ret = x509_get_ecpubkey( p, end, &alg_params, pk->data );
643 break;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200644 }
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200645
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200646 if( ret == 0 && *p != end )
647 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
648 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
649
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200650 if( ret != 0 )
651 pk_free( pk );
652
653 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200654}
655
Paul Bakker5121ce52009-01-03 21:22:43 +0000656static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000657 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 x509_buf *sig )
659{
Paul Bakker23986e52011-04-24 08:57:21 +0000660 int ret;
661 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
Paul Bakker8afa70d2012-02-11 18:42:45 +0000663 if( ( end - *p ) < 1 )
664 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
665 POLARSSL_ERR_ASN1_OUT_OF_DATA );
666
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 sig->tag = **p;
668
669 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000670 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker74111d32011-01-15 16:57:55 +0000672
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000674 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 sig->len = len;
677 sig->p = *p;
678
679 *p += len;
680
681 return( 0 );
682}
683
684/*
685 * X.509 v2/v3 unique identifier (not parsed)
686 */
687static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000688 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 x509_buf *uid, int n )
690{
691 int ret;
692
693 if( *p == end )
694 return( 0 );
695
696 uid->tag = **p;
697
698 if( ( ret = asn1_get_tag( p, end, &uid->len,
699 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
700 {
Paul Bakker40e46942009-01-03 21:51:57 +0000701 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 return( 0 );
703
704 return( ret );
705 }
706
707 uid->p = *p;
708 *p += uid->len;
709
710 return( 0 );
711}
712
713/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000714 * X.509 Extensions (No parsing of extensions, pointer should
715 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 */
717static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000718 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000719 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000720{
Paul Bakker23986e52011-04-24 08:57:21 +0000721 int ret;
722 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724 if( *p == end )
725 return( 0 );
726
727 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000728
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000730 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 ext->p = *p;
734 end = *p + ext->len;
735
736 /*
737 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
738 *
739 * Extension ::= SEQUENCE {
740 * extnID OBJECT IDENTIFIER,
741 * critical BOOLEAN DEFAULT FALSE,
742 * extnValue OCTET STRING }
743 */
744 if( ( ret = asn1_get_tag( p, end, &len,
745 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000746 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
748 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000750 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Paul Bakkerd98030e2009-05-02 15:13:40 +0000752 return( 0 );
753}
754
755/*
756 * X.509 CRL v2 extensions (no extensions parsed yet.)
757 */
758static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000759 const unsigned char *end,
760 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000761{
Paul Bakker23986e52011-04-24 08:57:21 +0000762 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000763 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000764
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000765 /* Get explicit tag */
766 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000767 {
768 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
769 return( 0 );
770
771 return( ret );
772 }
773
774 while( *p < end )
775 {
776 if( ( ret = asn1_get_tag( p, end, &len,
777 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000778 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000779
780 *p += len;
781 }
782
783 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000784 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000785 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
786
787 return( 0 );
788}
789
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000790/*
791 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
792 */
793static int x509_get_crl_entry_ext( unsigned char **p,
794 const unsigned char *end,
795 x509_buf *ext )
796{
797 int ret;
798 size_t len = 0;
799
800 /* OPTIONAL */
801 if (end <= *p)
802 return( 0 );
803
804 ext->tag = **p;
805 ext->p = *p;
806
807 /*
808 * Get CRL-entry extension sequence header
809 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
810 */
811 if( ( ret = asn1_get_tag( p, end, &ext->len,
812 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
813 {
814 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
815 {
816 ext->p = NULL;
817 return( 0 );
818 }
819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
820 }
821
822 end = *p + ext->len;
823
824 if( end != *p + ext->len )
825 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
826 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
827
828 while( *p < end )
829 {
830 if( ( ret = asn1_get_tag( p, end, &len,
831 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
832 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
833
834 *p += len;
835 }
836
837 if( *p != end )
838 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
839 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
840
841 return( 0 );
842}
843
Paul Bakker74111d32011-01-15 16:57:55 +0000844static int x509_get_basic_constraints( unsigned char **p,
845 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000846 int *ca_istrue,
847 int *max_pathlen )
848{
Paul Bakker23986e52011-04-24 08:57:21 +0000849 int ret;
850 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000851
852 /*
853 * BasicConstraints ::= SEQUENCE {
854 * cA BOOLEAN DEFAULT FALSE,
855 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
856 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000857 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000858 *max_pathlen = 0; /* endless */
859
860 if( ( ret = asn1_get_tag( p, end, &len,
861 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000863
864 if( *p == end )
865 return 0;
866
Paul Bakker3cccddb2011-01-16 21:46:31 +0000867 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000868 {
869 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000870 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000871
872 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000874
Paul Bakker3cccddb2011-01-16 21:46:31 +0000875 if( *ca_istrue != 0 )
876 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000877 }
878
879 if( *p == end )
880 return 0;
881
882 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000883 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000884
885 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000887 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
888
889 (*max_pathlen)++;
890
Paul Bakker74111d32011-01-15 16:57:55 +0000891 return 0;
892}
893
894static int x509_get_ns_cert_type( unsigned char **p,
895 const unsigned char *end,
896 unsigned char *ns_cert_type)
897{
898 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000899 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000900
901 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000902 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000903
904 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000905 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000906 POLARSSL_ERR_ASN1_INVALID_LENGTH );
907
908 /* Get actual bitstring */
909 *ns_cert_type = *bs.p;
910 return 0;
911}
912
913static int x509_get_key_usage( unsigned char **p,
914 const unsigned char *end,
915 unsigned char *key_usage)
916{
917 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000918 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000919
920 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000921 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000922
Paul Bakker94a67962012-08-23 13:03:52 +0000923 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000924 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000925 POLARSSL_ERR_ASN1_INVALID_LENGTH );
926
927 /* Get actual bitstring */
928 *key_usage = *bs.p;
929 return 0;
930}
931
Paul Bakkerd98030e2009-05-02 15:13:40 +0000932/*
Paul Bakker74111d32011-01-15 16:57:55 +0000933 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
934 *
935 * KeyPurposeId ::= OBJECT IDENTIFIER
936 */
937static int x509_get_ext_key_usage( unsigned char **p,
938 const unsigned char *end,
939 x509_sequence *ext_key_usage)
940{
941 int ret;
942
943 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 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
946 /* Sequence length must be >= 1 */
947 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000948 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000949 POLARSSL_ERR_ASN1_INVALID_LENGTH );
950
951 return 0;
952}
953
954/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000955 * SubjectAltName ::= GeneralNames
956 *
957 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
958 *
959 * GeneralName ::= CHOICE {
960 * otherName [0] OtherName,
961 * rfc822Name [1] IA5String,
962 * dNSName [2] IA5String,
963 * x400Address [3] ORAddress,
964 * directoryName [4] Name,
965 * ediPartyName [5] EDIPartyName,
966 * uniformResourceIdentifier [6] IA5String,
967 * iPAddress [7] OCTET STRING,
968 * registeredID [8] OBJECT IDENTIFIER }
969 *
970 * OtherName ::= SEQUENCE {
971 * type-id OBJECT IDENTIFIER,
972 * value [0] EXPLICIT ANY DEFINED BY type-id }
973 *
974 * EDIPartyName ::= SEQUENCE {
975 * nameAssigner [0] DirectoryString OPTIONAL,
976 * partyName [1] DirectoryString }
977 *
978 * NOTE: PolarSSL only parses and uses dNSName at this point.
979 */
980static int x509_get_subject_alt_name( unsigned char **p,
981 const unsigned char *end,
982 x509_sequence *subject_alt_name )
983{
984 int ret;
985 size_t len, tag_len;
986 asn1_buf *buf;
987 unsigned char tag;
988 asn1_sequence *cur = subject_alt_name;
989
990 /* Get main sequence tag */
991 if( ( ret = asn1_get_tag( p, end, &len,
992 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
993 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
994
995 if( *p + len != end )
996 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
997 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
998
999 while( *p < end )
1000 {
1001 if( ( end - *p ) < 1 )
1002 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1003 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1004
1005 tag = **p;
1006 (*p)++;
1007 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
1008 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
1009
1010 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
1011 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1012 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1013
1014 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
1015 {
1016 *p += tag_len;
1017 continue;
1018 }
1019
1020 buf = &(cur->buf);
1021 buf->tag = tag;
1022 buf->p = *p;
1023 buf->len = tag_len;
1024 *p += buf->len;
1025
1026 /* Allocate and assign next pointer */
1027 if (*p < end)
1028 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001029 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001030 sizeof( asn1_sequence ) );
1031
1032 if( cur->next == NULL )
1033 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1034 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1035
Paul Bakker535e97d2012-08-23 10:49:55 +00001036 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001037 cur = cur->next;
1038 }
1039 }
1040
1041 /* Set final sequence entry's next pointer to NULL */
1042 cur->next = NULL;
1043
1044 if( *p != end )
1045 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1046 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1047
1048 return( 0 );
1049}
1050
1051/*
Paul Bakker74111d32011-01-15 16:57:55 +00001052 * X.509 v3 extensions
1053 *
1054 * TODO: Perform all of the basic constraints tests required by the RFC
1055 * TODO: Set values for undetected extensions to a sane default?
1056 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001057 */
1058static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001059 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001060 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001061{
Paul Bakker23986e52011-04-24 08:57:21 +00001062 int ret;
1063 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001064 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001065
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001066 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001067 {
1068 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1069 return( 0 );
1070
1071 return( ret );
1072 }
1073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 while( *p < end )
1075 {
Paul Bakker74111d32011-01-15 16:57:55 +00001076 /*
1077 * Extension ::= SEQUENCE {
1078 * extnID OBJECT IDENTIFIER,
1079 * critical BOOLEAN DEFAULT FALSE,
1080 * extnValue OCTET STRING }
1081 */
1082 x509_buf extn_oid = {0, 0, NULL};
1083 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001085
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 if( ( ret = asn1_get_tag( p, end, &len,
1087 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001088 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001090 end_ext_data = *p + len;
1091
Paul Bakker74111d32011-01-15 16:57:55 +00001092 /* Get extension ID */
1093 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Paul Bakker74111d32011-01-15 16:57:55 +00001095 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001096 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Paul Bakker74111d32011-01-15 16:57:55 +00001098 extn_oid.p = *p;
1099 *p += extn_oid.len;
1100
1101 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001102 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001103 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1104
1105 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001106 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001107 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001108 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001109
Paul Bakker74111d32011-01-15 16:57:55 +00001110 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001111 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001113 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001115 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001116
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001117 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001118 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001119 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120
Paul Bakker74111d32011-01-15 16:57:55 +00001121 /*
1122 * Detect supported extensions
1123 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001124 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1125
1126 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001127 {
1128 /* No parser found, skip extension */
1129 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Paul Bakker5c721f92011-07-27 16:51:09 +00001131#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001132 if( is_critical )
1133 {
1134 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001135 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001136 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1137 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001138#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001139 continue;
1140 }
1141
1142 crt->ext_types |= ext_type;
1143
1144 switch( ext_type )
1145 {
1146 case EXT_BASIC_CONSTRAINTS:
1147 /* Parse basic constraints */
1148 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1149 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1150 return ( ret );
1151 break;
1152
1153 case EXT_KEY_USAGE:
1154 /* Parse key usage */
1155 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1156 &crt->key_usage ) ) != 0 )
1157 return ( ret );
1158 break;
1159
1160 case EXT_EXTENDED_KEY_USAGE:
1161 /* Parse extended key usage */
1162 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1163 &crt->ext_key_usage ) ) != 0 )
1164 return ( ret );
1165 break;
1166
1167 case EXT_SUBJECT_ALT_NAME:
1168 /* Parse subject alt name */
1169 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1170 &crt->subject_alt_names ) ) != 0 )
1171 return ( ret );
1172 break;
1173
1174 case EXT_NS_CERT_TYPE:
1175 /* Parse netscape certificate type */
1176 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1177 &crt->ns_cert_type ) ) != 0 )
1178 return ( ret );
1179 break;
1180
1181 default:
1182 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
1185
1186 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001187 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001188 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 return( 0 );
1191}
1192
1193/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001194 * X.509 CRL Entries
1195 */
1196static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001197 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001198 x509_crl_entry *entry )
1199{
Paul Bakker23986e52011-04-24 08:57:21 +00001200 int ret;
1201 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001202 x509_crl_entry *cur_entry = entry;
1203
1204 if( *p == end )
1205 return( 0 );
1206
Paul Bakker9be19372009-07-27 20:21:53 +00001207 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001208 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1209 {
1210 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1211 return( 0 );
1212
1213 return( ret );
1214 }
1215
Paul Bakker9be19372009-07-27 20:21:53 +00001216 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001217
1218 while( *p < end )
1219 {
Paul Bakker23986e52011-04-24 08:57:21 +00001220 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001221 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001222
1223 if( ( ret = asn1_get_tag( p, end, &len2,
1224 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1225 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001226 return( ret );
1227 }
1228
Paul Bakker9be19372009-07-27 20:21:53 +00001229 cur_entry->raw.tag = **p;
1230 cur_entry->raw.p = *p;
1231 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001232 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001233
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001234 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001235 return( ret );
1236
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001237 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001238 return( ret );
1239
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001240 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001241 return( ret );
1242
Paul Bakker74111d32011-01-15 16:57:55 +00001243 if ( *p < end )
1244 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001245 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001246
1247 if( cur_entry->next == NULL )
1248 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1249
Paul Bakkerd98030e2009-05-02 15:13:40 +00001250 cur_entry = cur_entry->next;
1251 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1252 }
1253 }
1254
1255 return( 0 );
1256}
1257
Paul Bakkerc70b9822013-04-07 22:00:46 +02001258static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1259 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001260{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001261 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001262
Paul Bakkerc70b9822013-04-07 22:00:46 +02001263 if( ret != 0 )
1264 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001265
Paul Bakkerc70b9822013-04-07 22:00:46 +02001266 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001267}
1268
Paul Bakkerd98030e2009-05-02 15:13:40 +00001269/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001270 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001272static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1273 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
Paul Bakker23986e52011-04-24 08:57:21 +00001275 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001276 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001277 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Paul Bakker320a4b52009-03-28 18:52:39 +00001279 /*
1280 * Check for valid input
1281 */
1282 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001283 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001284
Paul Bakker6e339b52013-07-03 13:37:05 +02001285 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001286
1287 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001288 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001289
1290 memcpy( p, buf, buflen );
1291
1292 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
1294 crt->raw.p = p;
1295 crt->raw.len = len;
1296 end = p + len;
1297
1298 /*
1299 * Certificate ::= SEQUENCE {
1300 * tbsCertificate TBSCertificate,
1301 * signatureAlgorithm AlgorithmIdentifier,
1302 * signatureValue BIT STRING }
1303 */
1304 if( ( ret = asn1_get_tag( &p, end, &len,
1305 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1306 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001307 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001308 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310
Paul Bakkerb00ca422012-09-25 12:10:00 +00001311 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001314 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001315 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001317 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001318
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 /*
1320 * TBSCertificate ::= SEQUENCE {
1321 */
1322 crt->tbs.p = p;
1323
1324 if( ( ret = asn1_get_tag( &p, end, &len,
1325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1326 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001327 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001328 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330
1331 end = p + len;
1332 crt->tbs.len = end - crt->tbs.p;
1333
1334 /*
1335 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1336 *
1337 * CertificateSerialNumber ::= INTEGER
1338 *
1339 * signature AlgorithmIdentifier
1340 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001341 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1342 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1343 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001345 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 return( ret );
1347 }
1348
1349 crt->version++;
1350
1351 if( crt->version > 3 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001354 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
1356
Paul Bakkerc70b9822013-04-07 22:00:46 +02001357 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1358 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001361 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
1363
1364 /*
1365 * issuer Name
1366 */
1367 crt->issuer_raw.p = p;
1368
1369 if( ( ret = asn1_get_tag( &p, end, &len,
1370 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1371 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001372 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001373 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
1375
1376 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1377 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001378 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 return( ret );
1380 }
1381
1382 crt->issuer_raw.len = p - crt->issuer_raw.p;
1383
1384 /*
1385 * Validity ::= SEQUENCE {
1386 * notBefore Time,
1387 * notAfter Time }
1388 *
1389 */
1390 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1391 &crt->valid_to ) ) != 0 )
1392 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001393 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 return( ret );
1395 }
1396
1397 /*
1398 * subject Name
1399 */
1400 crt->subject_raw.p = p;
1401
1402 if( ( ret = asn1_get_tag( &p, end, &len,
1403 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1404 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001405 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001406 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Paul Bakkercefb3962012-06-27 11:51:09 +00001409 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001411 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 return( ret );
1413 }
1414
1415 crt->subject_raw.len = p - crt->subject_raw.p;
1416
1417 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001418 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001420 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 return( ret );
1424 }
1425
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 /*
1427 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1428 * -- If present, version shall be v2 or v3
1429 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1430 * -- If present, version shall be v2 or v3
1431 * extensions [3] EXPLICIT Extensions OPTIONAL
1432 * -- If present, version shall be v3
1433 */
1434 if( crt->version == 2 || crt->version == 3 )
1435 {
1436 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1437 if( ret != 0 )
1438 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001439 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 return( ret );
1441 }
1442 }
1443
1444 if( crt->version == 2 || crt->version == 3 )
1445 {
1446 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1447 if( ret != 0 )
1448 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 return( ret );
1451 }
1452 }
1453
1454 if( crt->version == 3 )
1455 {
Paul Bakker74111d32011-01-15 16:57:55 +00001456 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 if( ret != 0 )
1458 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001459 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 return( ret );
1461 }
1462 }
1463
1464 if( p != end )
1465 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001466 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001467 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001468 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 }
1470
Paul Bakkerb00ca422012-09-25 12:10:00 +00001471 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001472
1473 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001474 * }
1475 * -- end of TBSCertificate
1476 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001477 * signatureAlgorithm AlgorithmIdentifier,
1478 * signatureValue BIT STRING
1479 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001480 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001482 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 return( ret );
1484 }
1485
Paul Bakker535e97d2012-08-23 10:49:55 +00001486 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1487 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001489 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001490 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 }
1492
1493 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1494 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001495 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 return( ret );
1497 }
1498
1499 if( p != end )
1500 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001501 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001502 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001503 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
1505
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506 return( 0 );
1507}
1508
1509/*
Paul Bakker42c65812013-06-24 19:21:59 +02001510 * Parse one X.509 certificate in DER format from a buffer and add them to a
1511 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001512 */
Paul Bakker42c65812013-06-24 19:21:59 +02001513int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001514{
Paul Bakker42c65812013-06-24 19:21:59 +02001515 int ret;
1516 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001517
1518 /*
1519 * Check for valid input
1520 */
1521 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001522 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001523
1524 while( crt->version != 0 && crt->next != NULL )
1525 {
1526 prev = crt;
1527 crt = crt->next;
1528 }
1529
1530 /*
1531 * Add new certificate on the end of the chain if needed.
1532 */
1533 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001534 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001535 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001536
Paul Bakker7d06ad22009-05-02 15:53:56 +00001537 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001538 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001539
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001540 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001541 crt = crt->next;
1542 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Paul Bakker42c65812013-06-24 19:21:59 +02001545 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1546 {
1547 if( prev )
1548 prev->next = NULL;
1549
1550 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001551 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001552
1553 return( ret );
1554 }
1555
1556 return( 0 );
1557}
1558
1559/*
1560 * Parse one or more PEM certificates from a buffer and add them to the chained list
1561 */
1562int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1563{
1564 int ret, success = 0, first_error = 0, total_failed = 0;
1565 int buf_format = X509_FORMAT_DER;
1566
1567 /*
1568 * Check for valid input
1569 */
1570 if( chain == NULL || buf == NULL )
1571 return( POLARSSL_ERR_X509_INVALID_INPUT );
1572
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 /*
1574 * Determine buffer content. Buffer contains either one DER certificate or
1575 * one or more PEM certificates.
1576 */
1577#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001578 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001579 buf_format = X509_FORMAT_PEM;
1580#endif
1581
1582 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001583 return x509parse_crt_der( chain, buf, buflen );
1584
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001585#if defined(POLARSSL_PEM_C)
1586 if( buf_format == X509_FORMAT_PEM )
1587 {
1588 pem_context pem;
1589
1590 while( buflen > 0 )
1591 {
1592 size_t use_len;
1593 pem_init( &pem );
1594
1595 ret = pem_read_buffer( &pem,
1596 "-----BEGIN CERTIFICATE-----",
1597 "-----END CERTIFICATE-----",
1598 buf, NULL, 0, &use_len );
1599
1600 if( ret == 0 )
1601 {
1602 /*
1603 * Was PEM encoded
1604 */
1605 buflen -= use_len;
1606 buf += use_len;
1607 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001608 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1609 {
1610 return( ret );
1611 }
Paul Bakker00b28602013-06-24 13:02:41 +02001612 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001613 {
1614 pem_free( &pem );
1615
Paul Bakker5ed3b342013-06-24 19:05:46 +02001616 /*
1617 * PEM header and footer were found
1618 */
1619 buflen -= use_len;
1620 buf += use_len;
1621
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001622 if( first_error == 0 )
1623 first_error = ret;
1624
1625 continue;
1626 }
1627 else
1628 break;
1629
Paul Bakker42c65812013-06-24 19:21:59 +02001630 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001631
1632 pem_free( &pem );
1633
1634 if( ret != 0 )
1635 {
1636 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001637 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001638 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001639 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001640 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001641
1642 if( first_error == 0 )
1643 first_error = ret;
1644
Paul Bakker42c65812013-06-24 19:21:59 +02001645 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001646 continue;
1647 }
1648
1649 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001650 }
1651 }
1652#endif
1653
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001654 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001655 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001656 else if( first_error )
1657 return( first_error );
1658 else
1659 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
1661
1662/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001663 * Parse one or more CRLs and add them to the chained list
1664 */
Paul Bakker23986e52011-04-24 08:57:21 +00001665int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666{
Paul Bakker23986e52011-04-24 08:57:21 +00001667 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001668 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669 unsigned char *p, *end;
1670 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001671#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001672 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001673 pem_context pem;
1674#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001675
1676 crl = chain;
1677
1678 /*
1679 * Check for valid input
1680 */
1681 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001682 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001683
1684 while( crl->version != 0 && crl->next != NULL )
1685 crl = crl->next;
1686
1687 /*
1688 * Add new CRL on the end of the chain if needed.
1689 */
1690 if ( crl->version != 0 && crl->next == NULL)
1691 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001692 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693
Paul Bakker7d06ad22009-05-02 15:53:56 +00001694 if( crl->next == NULL )
1695 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001696 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001697 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001698 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001699
Paul Bakker7d06ad22009-05-02 15:53:56 +00001700 crl = crl->next;
1701 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702 }
1703
Paul Bakker96743fc2011-02-12 14:30:57 +00001704#if defined(POLARSSL_PEM_C)
1705 pem_init( &pem );
1706 ret = pem_read_buffer( &pem,
1707 "-----BEGIN X509 CRL-----",
1708 "-----END X509 CRL-----",
1709 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001710
Paul Bakker96743fc2011-02-12 14:30:57 +00001711 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001713 /*
1714 * Was PEM encoded
1715 */
1716 buflen -= use_len;
1717 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001718
1719 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001720 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001722 p = pem.buf;
1723 pem.buf = NULL;
1724 len = pem.buflen;
1725 pem_free( &pem );
1726 }
Paul Bakker00b28602013-06-24 13:02:41 +02001727 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001728 {
1729 pem_free( &pem );
1730 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001731 }
1732 else
1733 {
1734 /*
1735 * nope, copy the raw DER data
1736 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001737 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738
1739 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001740 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001741
1742 memcpy( p, buf, buflen );
1743
1744 buflen = 0;
1745 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001746#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001747 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001748
1749 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001750 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001751
1752 memcpy( p, buf, buflen );
1753
1754 buflen = 0;
1755#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756
1757 crl->raw.p = p;
1758 crl->raw.len = len;
1759 end = p + len;
1760
1761 /*
1762 * CertificateList ::= SEQUENCE {
1763 * tbsCertList TBSCertList,
1764 * signatureAlgorithm AlgorithmIdentifier,
1765 * signatureValue BIT STRING }
1766 */
1767 if( ( ret = asn1_get_tag( &p, end, &len,
1768 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1769 {
1770 x509_crl_free( crl );
1771 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1772 }
1773
Paul Bakker23986e52011-04-24 08:57:21 +00001774 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 {
1776 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001777 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001778 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1779 }
1780
1781 /*
1782 * TBSCertList ::= SEQUENCE {
1783 */
1784 crl->tbs.p = p;
1785
1786 if( ( ret = asn1_get_tag( &p, end, &len,
1787 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1788 {
1789 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001790 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001791 }
1792
1793 end = p + len;
1794 crl->tbs.len = end - crl->tbs.p;
1795
1796 /*
1797 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1798 * -- if present, MUST be v2
1799 *
1800 * signature AlgorithmIdentifier
1801 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001802 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001803 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 {
1805 x509_crl_free( crl );
1806 return( ret );
1807 }
1808
1809 crl->version++;
1810
1811 if( crl->version > 2 )
1812 {
1813 x509_crl_free( crl );
1814 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1815 }
1816
Paul Bakkerc70b9822013-04-07 22:00:46 +02001817 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1818 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001819 {
1820 x509_crl_free( crl );
1821 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1822 }
1823
1824 /*
1825 * issuer Name
1826 */
1827 crl->issuer_raw.p = p;
1828
1829 if( ( ret = asn1_get_tag( &p, end, &len,
1830 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1831 {
1832 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001833 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834 }
1835
1836 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1837 {
1838 x509_crl_free( crl );
1839 return( ret );
1840 }
1841
1842 crl->issuer_raw.len = p - crl->issuer_raw.p;
1843
1844 /*
1845 * thisUpdate Time
1846 * nextUpdate Time OPTIONAL
1847 */
Paul Bakker91200182010-02-18 21:26:15 +00001848 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001849 {
1850 x509_crl_free( crl );
1851 return( ret );
1852 }
1853
Paul Bakker91200182010-02-18 21:26:15 +00001854 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001855 {
Paul Bakker9d781402011-05-09 16:17:09 +00001856 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001857 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001858 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001859 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001860 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861 x509_crl_free( crl );
1862 return( ret );
1863 }
1864 }
1865
1866 /*
1867 * revokedCertificates SEQUENCE OF SEQUENCE {
1868 * userCertificate CertificateSerialNumber,
1869 * revocationDate Time,
1870 * crlEntryExtensions Extensions OPTIONAL
1871 * -- if present, MUST be v2
1872 * } OPTIONAL
1873 */
1874 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1875 {
1876 x509_crl_free( crl );
1877 return( ret );
1878 }
1879
1880 /*
1881 * crlExtensions EXPLICIT Extensions OPTIONAL
1882 * -- if present, MUST be v2
1883 */
1884 if( crl->version == 2 )
1885 {
1886 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1887
1888 if( ret != 0 )
1889 {
1890 x509_crl_free( crl );
1891 return( ret );
1892 }
1893 }
1894
1895 if( p != end )
1896 {
1897 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001898 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1900 }
1901
1902 end = crl->raw.p + crl->raw.len;
1903
1904 /*
1905 * signatureAlgorithm AlgorithmIdentifier,
1906 * signatureValue BIT STRING
1907 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001908 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001909 {
1910 x509_crl_free( crl );
1911 return( ret );
1912 }
1913
Paul Bakker535e97d2012-08-23 10:49:55 +00001914 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1915 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001916 {
1917 x509_crl_free( crl );
1918 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1919 }
1920
1921 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1922 {
1923 x509_crl_free( crl );
1924 return( ret );
1925 }
1926
1927 if( p != end )
1928 {
1929 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001930 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001931 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1932 }
1933
1934 if( buflen > 0 )
1935 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001936 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001937
Paul Bakker7d06ad22009-05-02 15:53:56 +00001938 if( crl->next == NULL )
1939 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001940 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001941 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001942 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001943
Paul Bakker7d06ad22009-05-02 15:53:56 +00001944 crl = crl->next;
1945 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001946
1947 return( x509parse_crl( crl, buf, buflen ) );
1948 }
1949
1950 return( 0 );
1951}
1952
Paul Bakker335db3f2011-04-25 15:28:35 +00001953#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001955 * Load all data from a file into a given buffer.
1956 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001957static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001958{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001959 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001960
Paul Bakkerd98030e2009-05-02 15:13:40 +00001961 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001962 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001963
Paul Bakkerd98030e2009-05-02 15:13:40 +00001964 fseek( f, 0, SEEK_END );
1965 *n = (size_t) ftell( f );
1966 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001967
Paul Bakker6e339b52013-07-03 13:37:05 +02001968 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001969 {
1970 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001971 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001972 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001973
Paul Bakkerd98030e2009-05-02 15:13:40 +00001974 if( fread( *buf, 1, *n, f ) != *n )
1975 {
1976 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001977 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001978 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001979 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001980
Paul Bakkerd98030e2009-05-02 15:13:40 +00001981 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001982
Paul Bakkerd98030e2009-05-02 15:13:40 +00001983 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001984
Paul Bakkerd98030e2009-05-02 15:13:40 +00001985 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001986}
1987
1988/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 * Load one or more certificates and add them to the chained list
1990 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001991int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992{
1993 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001994 size_t n;
1995 unsigned char *buf;
1996
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001997 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001998 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
Paul Bakker69e095c2011-12-10 21:55:01 +00002000 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
2002 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002003 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
2005 return( ret );
2006}
2007
Paul Bakker8d914582012-06-04 12:46:42 +00002008int x509parse_crtpath( x509_cert *chain, const char *path )
2009{
2010 int ret = 0;
2011#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002012 int w_ret;
2013 WCHAR szDir[MAX_PATH];
2014 char filename[MAX_PATH];
2015 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002016 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002017
Paul Bakker97872ac2012-11-02 12:53:26 +00002018 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002019 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002020
2021 if( len > MAX_PATH - 3 )
2022 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002023
Paul Bakker3338b792012-10-01 21:13:10 +00002024 memset( szDir, 0, sizeof(szDir) );
2025 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002026 memcpy( filename, path, len );
2027 filename[len++] = '\\';
2028 p = filename + len;
2029 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002030
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002031 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002032
Paul Bakker97872ac2012-11-02 12:53:26 +00002033 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002034 if (hFind == INVALID_HANDLE_VALUE)
2035 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2036
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002037 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002038 do
2039 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002040 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002041
Paul Bakkere4791f32012-06-04 21:29:15 +00002042 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002043 continue;
2044
Paul Bakker3338b792012-10-01 21:13:10 +00002045 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2046 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002047 p, len - 1,
2048 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002049
Paul Bakker3338b792012-10-01 21:13:10 +00002050 w_ret = x509parse_crtfile( chain, filename );
2051 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002052 ret++;
2053 else
2054 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002055 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002056 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002057
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002058 if (GetLastError() != ERROR_NO_MORE_FILES)
2059 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002060
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002061cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002062 FindClose( hFind );
2063#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002064 int t_ret, i;
2065 struct stat sb;
2066 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002067 char entry_name[255];
2068 DIR *dir = opendir( path );
2069
2070 if( dir == NULL)
2071 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2072
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002073 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002074 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002075 if( result == NULL )
2076 break;
2077
2078 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2079
2080 i = stat( entry_name, &sb );
2081
2082 if( i == -1 )
2083 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2084
2085 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002086 continue;
2087
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002088 // Ignore parse errors
2089 //
Paul Bakker8d914582012-06-04 12:46:42 +00002090 t_ret = x509parse_crtfile( chain, entry_name );
2091 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002092 ret++;
2093 else
2094 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002095 }
2096 closedir( dir );
2097#endif
2098
2099 return( ret );
2100}
2101
Paul Bakkerd98030e2009-05-02 15:13:40 +00002102/*
2103 * Load one or more CRLs and add them to the chained list
2104 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002105int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002106{
2107 int ret;
2108 size_t n;
2109 unsigned char *buf;
2110
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002111 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002112 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002113
Paul Bakker27fdf462011-06-09 13:55:13 +00002114 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002115
2116 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002117 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002118
2119 return( ret );
2120}
2121
Paul Bakker5121ce52009-01-03 21:22:43 +00002122/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002123 * Load and parse a private RSA key
2124 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002125int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00002126{
2127 int ret;
2128 size_t n;
2129 unsigned char *buf;
2130
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002131 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002132 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002133
2134 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002135 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002136 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002137 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002138 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00002139
2140 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002141 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002142
2143 return( ret );
2144}
2145
2146/*
2147 * Load and parse a public RSA key
2148 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002149int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00002150{
2151 int ret;
2152 size_t n;
2153 unsigned char *buf;
2154
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002155 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002156 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002157
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002158 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002159
2160 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002161 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00002162
2163 return( ret );
2164}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002165
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002166/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002167 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002168 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002169int x509parse_keyfile( pk_context *ctx,
2170 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002171{
2172 int ret;
2173 size_t n;
2174 unsigned char *buf;
2175
2176 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2177 return( ret );
2178
2179 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002180 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002181 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002182 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002183 (const unsigned char *) pwd, strlen( pwd ) );
2184
2185 memset( buf, 0, n + 1 );
2186 free( buf );
2187
2188 return( ret );
2189}
2190
2191/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002192 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002193 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002194int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002195{
2196 int ret;
2197 size_t n;
2198 unsigned char *buf;
2199
2200 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2201 return( ret );
2202
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002203 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002204
2205 memset( buf, 0, n + 1 );
2206 free( buf );
2207
2208 return( ret );
2209}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002210
Paul Bakker335db3f2011-04-25 15:28:35 +00002211#endif /* POLARSSL_FS_IO */
2212
2213/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002214 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002216static int x509parse_key_pkcs1_der( rsa_context *rsa,
2217 const unsigned char *key,
2218 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219{
Paul Bakker23986e52011-04-24 08:57:21 +00002220 int ret;
2221 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002223
Paul Bakker96743fc2011-02-12 14:30:57 +00002224 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002225 end = p + keylen;
2226
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002228 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002229 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 * RSAPrivateKey ::= SEQUENCE {
2231 * version Version,
2232 * modulus INTEGER, -- n
2233 * publicExponent INTEGER, -- e
2234 * privateExponent INTEGER, -- d
2235 * prime1 INTEGER, -- p
2236 * prime2 INTEGER, -- q
2237 * exponent1 INTEGER, -- d mod (p-1)
2238 * exponent2 INTEGER, -- d mod (q-1)
2239 * coefficient INTEGER, -- (inverse of q) mod p
2240 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2241 * }
2242 */
2243 if( ( ret = asn1_get_tag( &p, end, &len,
2244 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2245 {
Paul Bakker9d781402011-05-09 16:17:09 +00002246 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 }
2248
2249 end = p + len;
2250
2251 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2252 {
Paul Bakker9d781402011-05-09 16:17:09 +00002253 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 }
2255
2256 if( rsa->ver != 0 )
2257 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002258 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
2260
2261 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2262 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2263 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2264 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2265 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2266 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2267 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2268 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2269 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002271 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
2274 rsa->len = mpi_size( &rsa->N );
2275
2276 if( p != end )
2277 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002279 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002280 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
2282
2283 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2284 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 rsa_free( rsa );
2286 return( ret );
2287 }
2288
Paul Bakkere2f50402013-06-24 19:00:59 +02002289 return( 0 );
2290}
Paul Bakkere2f50402013-06-24 19:00:59 +02002291/*
2292 * Parse an unencrypted PKCS#8 encoded private RSA key
2293 */
2294static int x509parse_key_pkcs8_unencrypted_der(
2295 rsa_context *rsa,
2296 const unsigned char *key,
2297 size_t keylen )
2298{
2299 int ret;
2300 size_t len;
2301 unsigned char *p, *end;
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002302 x509_buf alg_params;
Paul Bakkere2f50402013-06-24 19:00:59 +02002303 pk_type_t pk_alg = POLARSSL_PK_NONE;
2304
2305 p = (unsigned char *) key;
2306 end = p + keylen;
2307
2308 /*
2309 * This function parses the PrivatKeyInfo object (PKCS#8)
2310 *
2311 * PrivateKeyInfo ::= SEQUENCE {
2312 * version Version,
2313 * algorithm AlgorithmIdentifier,
2314 * PrivateKey BIT STRING
2315 * }
2316 *
2317 * AlgorithmIdentifier ::= SEQUENCE {
2318 * algorithm OBJECT IDENTIFIER,
2319 * parameters ANY DEFINED BY algorithm OPTIONAL
2320 * }
2321 *
2322 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2323 */
2324 if( ( ret = asn1_get_tag( &p, end, &len,
2325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2326 {
2327 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2328 }
2329
2330 end = p + len;
2331
2332 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2333 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2334
2335 if( rsa->ver != 0 )
2336 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2337
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002338 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002339 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2340
2341 /*
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002342 * We explicitly want RSA keys only
Paul Bakkere2f50402013-06-24 19:00:59 +02002343 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002344 if (pk_alg != POLARSSL_PK_RSA )
2345 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
2346
Paul Bakkere2f50402013-06-24 19:00:59 +02002347 /*
2348 * Get the OCTET STRING and parse the PKCS#1 format inside
2349 */
2350 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2351 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2352
2353 if( ( end - p ) < 1 )
2354 {
2355 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2356 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2357 }
2358
2359 end = p + len;
2360
2361 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2362 return( ret );
2363
2364 return( 0 );
2365}
2366
2367/*
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002368 * Decrypt the content of a PKCS#8 EncryptedPrivateKeyInfo
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002369 */
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002370static int x509parse_pkcs8_decrypt( unsigned char *buf, size_t buflen,
2371 size_t *used_len,
2372 const unsigned char *key, size_t keylen,
2373 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002374{
2375 int ret;
2376 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002377 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002378 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002379#if defined(POLARSSL_PKCS12_C)
2380 cipher_type_t cipher_alg;
2381 md_type_t md_alg;
2382#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002383
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002384 memset(buf, 0, buflen);
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002385
2386 p = (unsigned char *) key;
2387 end = p + keylen;
2388
Paul Bakker28144de2013-06-24 19:28:55 +02002389 if( pwdlen == 0 )
2390 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2391
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002392 /*
2393 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2394 *
2395 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2396 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2397 * encryptedData EncryptedData
2398 * }
2399 *
2400 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2401 *
2402 * EncryptedData ::= OCTET STRING
2403 *
2404 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2405 */
2406 if( ( ret = asn1_get_tag( &p, end, &len,
2407 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2408 {
2409 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2410 }
2411
2412 end = p + len;
2413
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002414 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002415 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002416
2417 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2418 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2419
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002420 if( len > buflen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002421 return( POLARSSL_ERR_X509_INVALID_INPUT );
2422
2423 /*
2424 * Decrypt EncryptedData with appropriate PDE
2425 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002426#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002427 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002428 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002429 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002430 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002431 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002432 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002433 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2434 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2435
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002436 return( ret );
2437 }
2438 }
2439 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2440 {
2441 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2442 PKCS12_PBE_DECRYPT,
2443 pwd, pwdlen,
2444 p, len, buf ) ) != 0 )
2445 {
2446 return( ret );
2447 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002448
2449 // Best guess for password mismatch when using RC4. If first tag is
2450 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2451 //
2452 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2453 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002454 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002455 else
2456#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002457#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002458 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002459 {
2460 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2461 p, len, buf ) ) != 0 )
2462 {
2463 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2464 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2465
2466 return( ret );
2467 }
2468 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002469 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002470#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002471 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2472
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002473 *used_len = len;
2474 return( 0 );
2475}
2476
2477/*
2478 * Parse an encrypted PKCS#8 encoded private RSA key
2479 */
2480static int x509parse_key_pkcs8_encrypted_der(
2481 rsa_context *rsa,
2482 const unsigned char *key, size_t keylen,
2483 const unsigned char *pwd, size_t pwdlen )
2484{
2485 int ret;
2486 unsigned char buf[2048];
2487 size_t len = 0;
2488
2489 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2490 key, keylen, pwd, pwdlen ) ) != 0 )
2491 {
2492 return( ret );
2493 }
2494
2495 return( x509parse_key_pkcs8_unencrypted_der( rsa, buf, len ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496}
2497
2498/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002499 * Parse a private RSA key
2500 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002501int x509parse_key_rsa( rsa_context *rsa,
2502 const unsigned char *key, size_t keylen,
2503 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002504{
2505 int ret;
2506
Paul Bakker96743fc2011-02-12 14:30:57 +00002507#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002508 size_t len;
2509 pem_context pem;
2510
2511 pem_init( &pem );
2512 ret = pem_read_buffer( &pem,
2513 "-----BEGIN RSA PRIVATE KEY-----",
2514 "-----END RSA PRIVATE KEY-----",
2515 key, pwd, pwdlen, &len );
2516 if( ret == 0 )
2517 {
2518 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2519 {
2520 rsa_free( rsa );
2521 }
2522
2523 pem_free( &pem );
2524 return( ret );
2525 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002526 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2527 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2528 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2529 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002531 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002532
2533 ret = pem_read_buffer( &pem,
2534 "-----BEGIN PRIVATE KEY-----",
2535 "-----END PRIVATE KEY-----",
2536 key, NULL, 0, &len );
2537 if( ret == 0 )
2538 {
2539 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2540 pem.buf, pem.buflen ) ) != 0 )
2541 {
2542 rsa_free( rsa );
2543 }
2544
2545 pem_free( &pem );
2546 return( ret );
2547 }
2548 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002549 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002550
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002551 ret = pem_read_buffer( &pem,
2552 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2553 "-----END ENCRYPTED PRIVATE KEY-----",
2554 key, NULL, 0, &len );
2555 if( ret == 0 )
2556 {
2557 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2558 pem.buf, pem.buflen,
2559 pwd, pwdlen ) ) != 0 )
2560 {
2561 rsa_free( rsa );
2562 }
2563
2564 pem_free( &pem );
2565 return( ret );
2566 }
2567 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002568 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002569#else
2570 ((void) pwd);
2571 ((void) pwdlen);
2572#endif /* POLARSSL_PEM_C */
2573
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002574 /*
2575 * At this point we only know it's not a PEM formatted key. Could be any
2576 * of the known DER encoded private key formats
2577 *
2578 * We try the different DER format parsers to see if one passes without
2579 * error
2580 */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002581 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2582 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002583 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002584 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002587 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002588
2589 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2590 {
2591 return( ret );
2592 }
2593
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002594 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2595 return( 0 );
2596
2597 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002598
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002599 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2600 return( 0 );
2601
2602 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002603
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002604 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002605}
2606
2607/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002608 * Parse a public RSA key
2609 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002610int x509parse_public_key_rsa( rsa_context *rsa,
2611 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002612{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002613 pk_context pk_ctx;
Paul Bakker53019ae2011-03-25 13:58:48 +00002614
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002615 pk_init( &pk_ctx );
2616 pk_wrap_rsa( &pk_ctx, rsa );
Paul Bakker53019ae2011-03-25 13:58:48 +00002617
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +02002618 return( x509parse_public_key( &pk_ctx, key, keylen ) );
Paul Bakker53019ae2011-03-25 13:58:48 +00002619}
2620
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002621#if defined(POLARSSL_ECP_C)
2622/*
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002623 * Parse a SEC1 encoded private EC key
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002624 */
2625static int x509parse_key_sec1_der( ecp_keypair *eck,
2626 const unsigned char *key,
2627 size_t keylen )
2628{
2629 int ret;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002630 int version;
2631 size_t len;
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002632 x509_buf params;
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002633 unsigned char *p = (unsigned char *) key;
2634 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002635 unsigned char *end2;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002636
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002637 /*
2638 * RFC 5915, orf SEC1 Appendix C.4
2639 *
2640 * ECPrivateKey ::= SEQUENCE {
2641 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2642 * privateKey OCTET STRING,
2643 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2644 * publicKey [1] BIT STRING OPTIONAL
2645 * }
2646 */
2647 if( ( ret = asn1_get_tag( &p, end, &len,
2648 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2649 {
2650 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2651 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002652
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002653 end = p + len;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002654
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002655 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2656 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002657
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002658 if( version != 1 )
2659 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
2660
2661 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2663
2664 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
2665 {
2666 ecp_keypair_free( eck );
2667 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2668 }
2669
2670 p += len;
2671
2672 /*
2673 * Is 'parameters' present?
2674 */
2675 if( ( ret = asn1_get_tag( &p, end, &len,
2676 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2677 {
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002678 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2679 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2680 {
2681 ecp_keypair_free( eck );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002682 return( ret );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002683 }
2684 }
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002685 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002686 {
2687 ecp_keypair_free( eck );
2688 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2689 }
2690
2691 /*
2692 * Is 'publickey' present?
2693 */
2694 if( ( ret = asn1_get_tag( &p, end, &len,
2695 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2696 {
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002697 end2 = p + len;
2698
2699 if( ( ret = x509_get_subpubkey_ec( &p, end2, &eck->grp, &eck->Q ) )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002700 != 0 )
2701 {
2702 ecp_keypair_free( eck );
2703 return( ret );
2704 }
2705
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +02002706 if( p != end2 )
2707 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
2708 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002709 }
2710 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2711 {
2712 ecp_keypair_free( eck );
2713 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2714 }
2715
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002716 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002717 {
2718 ecp_keypair_free( eck );
2719 return( ret );
2720 }
2721
2722 return 0;
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002723}
2724
2725/*
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002726 * Parse an unencrypted PKCS#8 encoded private EC key
2727 */
2728static int x509parse_key_pkcs8_unencrypted_der_ec(
2729 ecp_keypair *eck,
2730 const unsigned char* key,
2731 size_t keylen )
2732{
2733 int ret, version;
2734 size_t len;
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +02002735 x509_buf alg_params;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002736 unsigned char *p = (unsigned char *) key;
2737 unsigned char *end = p + keylen;
2738 pk_type_t pk_alg = POLARSSL_PK_NONE;
2739
2740 /*
2741 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2742 *
2743 * PrivateKeyInfo ::= SEQUENCE {
2744 * version Version,
2745 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2746 * privateKey PrivateKey,
2747 * attributes [0] IMPLICIT Attributes OPTIONAL }
2748 *
2749 * Version ::= INTEGER
2750 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2751 * PrivateKey ::= OCTET STRING
2752 *
2753 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2754 */
2755
2756 if( ( ret = asn1_get_tag( &p, end, &len,
2757 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2758 {
2759 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2760 }
2761
2762 end = p + len;
2763
2764 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2765 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2766
2767 if( version != 0 )
2768 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2769
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +02002770 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002771 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2772
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002773 if( pk_alg != POLARSSL_PK_ECKEY && pk_alg != POLARSSL_PK_ECKEY_DH )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002774 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002775
2776 if( pk_alg == POLARSSL_PK_ECKEY_DH )
2777 eck->alg = POLARSSL_ECP_KEY_ALG_ECDH;
2778
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +02002779 if( ( ret = x509_use_ecparams( &alg_params, &eck->grp ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002780 {
2781 ecp_keypair_free( eck );
2782 return( ret );
2783 }
2784
2785 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2786 {
2787 ecp_keypair_free( eck );
2788 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2789 }
2790
2791 if( ( ret = x509parse_key_sec1_der( eck, p, len ) ) != 0 )
2792 {
2793 ecp_keypair_free( eck );
2794 return( ret );
2795 }
2796
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02002797 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002798 {
2799 ecp_keypair_free( eck );
2800 return( ret );
2801 }
2802
2803 return 0;
2804}
2805
2806/*
2807 * Parse an encrypted PKCS#8 encoded private EC key
2808 */
2809static int x509parse_key_pkcs8_encrypted_der_ec(
2810 ecp_keypair *eck,
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002811 const unsigned char *key, size_t keylen,
2812 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002813{
2814 int ret;
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002815 unsigned char buf[2048];
2816 size_t len = 0;
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002817
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002818 if( ( ret = x509parse_pkcs8_decrypt( buf, sizeof( buf ), &len,
2819 key, keylen, pwd, pwdlen ) ) != 0 )
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002820 {
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002821 return( ret );
2822 }
2823
Manuel Pégourié-Gonnard9c1cf452013-07-04 11:20:24 +02002824 return( x509parse_key_pkcs8_unencrypted_der_ec( eck, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002825}
2826
2827/*
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002828 * Parse a private EC key
2829 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002830static int x509parse_key_ec( ecp_keypair *eck,
2831 const unsigned char *key, size_t keylen,
2832 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002833{
2834 int ret;
2835
2836#if defined(POLARSSL_PEM_C)
2837 size_t len;
2838 pem_context pem;
2839
2840 pem_init( &pem );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002841 ret = pem_read_buffer( &pem,
2842 "-----BEGIN EC PRIVATE KEY-----",
2843 "-----END EC PRIVATE KEY-----",
2844 key, pwd, pwdlen, &len );
2845 if( ret == 0 )
2846 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002847 if( ( ret = x509parse_key_sec1_der( eck, pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002848 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002849 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002850 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002851
2852 pem_free( &pem );
2853 return( ret );
2854 }
2855 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2856 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2857 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2858 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2859 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2860 return( ret );
2861
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002862 ret = pem_read_buffer( &pem,
2863 "-----BEGIN PRIVATE KEY-----",
2864 "-----END PRIVATE KEY-----",
2865 key, NULL, 0, &len );
2866 if( ret == 0 )
2867 {
2868 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2869 pem.buf, pem.buflen ) ) != 0 )
2870 {
2871 ecp_keypair_free( eck );
2872 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002873
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002874 pem_free( &pem );
2875 return( ret );
2876 }
2877 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2878 return( ret );
2879
2880 ret = pem_read_buffer( &pem,
2881 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2882 "-----END ENCRYPTED PRIVATE KEY-----",
2883 key, NULL, 0, &len );
2884 if( ret == 0 )
2885 {
2886 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck,
2887 pem.buf, pem.buflen,
2888 pwd, pwdlen ) ) != 0 )
2889 {
2890 ecp_keypair_free( eck );
2891 }
2892
2893 pem_free( &pem );
2894 return( ret );
2895 }
2896 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2897 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002898#else
2899 ((void) pwd);
2900 ((void) pwdlen);
2901#endif /* POLARSSL_PEM_C */
2902
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002903 /*
2904 * At this point we only know it's not a PEM formatted key. Could be any
2905 * of the known DER encoded private key formats
2906 *
2907 * We try the different DER format parsers to see if one passes without
2908 * error
2909 */
2910 if( ( ret = x509parse_key_pkcs8_encrypted_der_ec( eck, key, keylen,
2911 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002912 {
2913 return( 0 );
2914 }
2915
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002916 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002917
2918 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2919 {
2920 return( ret );
2921 }
2922
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002923 if( ( ret = x509parse_key_pkcs8_unencrypted_der_ec( eck,
2924 key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002925 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002926
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002927 ecp_keypair_free( eck );
2928
2929 if( ( ret = x509parse_key_sec1_der( eck, key, keylen ) ) == 0 )
2930 return( 0 );
2931
2932 ecp_keypair_free( eck );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002933
2934 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2935}
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002936#endif /* defined(POLARSSL_ECP_C) */
2937
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002938/*
2939 * Parse a private key
2940 */
2941int x509parse_key( pk_context *ctx,
2942 const unsigned char *key, size_t keylen,
2943 const unsigned char *pwd, size_t pwdlen )
2944{
2945 int ret;
2946
2947 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_RSA ) ) != 0 )
2948 return( ret );
2949
2950 if( ( ret = x509parse_key_rsa( ctx->data, key, keylen, pwd, pwdlen ) )
2951 == 0 )
2952 {
2953 return( 0 );
2954 }
2955
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002956 pk_free( ctx );
2957
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002958 if ( ( ret = pk_set_type( ctx, POLARSSL_PK_ECKEY ) ) != 0 )
2959 return( ret );
2960
2961 if( ( ret = x509parse_key_ec( ctx->data, key, keylen, pwd, pwdlen ) ) == 0 )
2962 {
2963 return( 0 );
2964 }
2965
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002966 pk_free( ctx );
2967
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002968 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
2969}
2970
2971/*
2972 * Parse a public key
2973 */
2974int x509parse_public_key( pk_context *ctx,
2975 const unsigned char *key, size_t keylen )
2976{
2977 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002978 unsigned char *p;
2979#if defined(POLARSSL_PEM_C)
2980 size_t len;
2981 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002982
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002983 pem_init( &pem );
2984 ret = pem_read_buffer( &pem,
2985 "-----BEGIN PUBLIC KEY-----",
2986 "-----END PUBLIC KEY-----",
2987 key, NULL, 0, &len );
2988
2989 if( ret == 0 )
2990 {
2991 /*
2992 * Was PEM encoded
2993 */
2994 key = pem.buf;
2995 keylen = pem.buflen;
2996 }
2997 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2998 {
2999 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003000 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003001 }
3002#endif
3003 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003004
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003005 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003006
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003007#if defined(POLARSSL_PEM_C)
3008 pem_free( &pem );
3009#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003010
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003011 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003012}
3013
Paul Bakkereaa89f82011-04-04 21:36:15 +00003014#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003015/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003016 * Parse DHM parameters
3017 */
Paul Bakker23986e52011-04-24 08:57:21 +00003018int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003019{
Paul Bakker23986e52011-04-24 08:57:21 +00003020 int ret;
3021 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003022 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003023#if defined(POLARSSL_PEM_C)
3024 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003025
Paul Bakker96743fc2011-02-12 14:30:57 +00003026 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003027
Paul Bakker96743fc2011-02-12 14:30:57 +00003028 ret = pem_read_buffer( &pem,
3029 "-----BEGIN DH PARAMETERS-----",
3030 "-----END DH PARAMETERS-----",
3031 dhmin, NULL, 0, &dhminlen );
3032
3033 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003034 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003035 /*
3036 * Was PEM encoded
3037 */
3038 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003039 }
Paul Bakker00b28602013-06-24 13:02:41 +02003040 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003041 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003042 pem_free( &pem );
3043 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003044 }
3045
Paul Bakker96743fc2011-02-12 14:30:57 +00003046 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3047#else
3048 p = (unsigned char *) dhmin;
3049#endif
3050 end = p + dhminlen;
3051
Paul Bakker1b57b062011-01-06 15:48:19 +00003052 memset( dhm, 0, sizeof( dhm_context ) );
3053
Paul Bakker1b57b062011-01-06 15:48:19 +00003054 /*
3055 * DHParams ::= SEQUENCE {
3056 * prime INTEGER, -- P
3057 * generator INTEGER, -- g
3058 * }
3059 */
3060 if( ( ret = asn1_get_tag( &p, end, &len,
3061 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3062 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003063#if defined(POLARSSL_PEM_C)
3064 pem_free( &pem );
3065#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003066 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003067 }
3068
3069 end = p + len;
3070
3071 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3072 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3073 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003074#if defined(POLARSSL_PEM_C)
3075 pem_free( &pem );
3076#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003077 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003078 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003079 }
3080
3081 if( p != end )
3082 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003083#if defined(POLARSSL_PEM_C)
3084 pem_free( &pem );
3085#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003086 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003087 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003088 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3089 }
3090
Paul Bakker96743fc2011-02-12 14:30:57 +00003091#if defined(POLARSSL_PEM_C)
3092 pem_free( &pem );
3093#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003094
3095 return( 0 );
3096}
3097
Paul Bakker335db3f2011-04-25 15:28:35 +00003098#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003099/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003100 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003101 */
3102int x509parse_dhmfile( dhm_context *dhm, const char *path )
3103{
3104 int ret;
3105 size_t n;
3106 unsigned char *buf;
3107
Paul Bakker69e095c2011-12-10 21:55:01 +00003108 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3109 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003110
Paul Bakker27fdf462011-06-09 13:55:13 +00003111 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003112
3113 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003114 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003115
3116 return( ret );
3117}
Paul Bakker335db3f2011-04-25 15:28:35 +00003118#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003119#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003120
Paul Bakker5121ce52009-01-03 21:22:43 +00003121#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122#include <stdarg.h>
3123
3124#if !defined vsnprintf
3125#define vsnprintf _vsnprintf
3126#endif // vsnprintf
3127
3128/*
3129 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3130 * Result value is not size of buffer needed, but -1 if no fit is possible.
3131 *
3132 * This fuction tries to 'fix' this by at least suggesting enlarging the
3133 * size by 20.
3134 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003135static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003136{
3137 va_list ap;
3138 int res = -1;
3139
3140 va_start( ap, format );
3141
3142 res = vsnprintf( str, size, format, ap );
3143
3144 va_end( ap );
3145
3146 // No quick fix possible
3147 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003148 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003149
3150 return res;
3151}
3152
3153#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003154#endif
3155
Paul Bakkerd98030e2009-05-02 15:13:40 +00003156#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3157
3158#define SAFE_SNPRINTF() \
3159{ \
3160 if( ret == -1 ) \
3161 return( -1 ); \
3162 \
Paul Bakker23986e52011-04-24 08:57:21 +00003163 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003164 p[n - 1] = '\0'; \
3165 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3166 } \
3167 \
Paul Bakker23986e52011-04-24 08:57:21 +00003168 n -= (unsigned int) ret; \
3169 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170}
3171
Paul Bakker5121ce52009-01-03 21:22:43 +00003172/*
3173 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003174 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003176int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003177{
Paul Bakker23986e52011-04-24 08:57:21 +00003178 int ret;
3179 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003181 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003182 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 char s[128], *p;
3184
3185 memset( s, 0, sizeof( s ) );
3186
3187 name = dn;
3188 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003189 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
3191 while( name != NULL )
3192 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003193 if( !name->oid.p )
3194 {
3195 name = name->next;
3196 continue;
3197 }
3198
Paul Bakker74111d32011-01-15 16:57:55 +00003199 if( name != dn )
3200 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003201 ret = snprintf( p, n, ", " );
3202 SAFE_SNPRINTF();
3203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003204
Paul Bakkerc70b9822013-04-07 22:00:46 +02003205 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Paul Bakkerc70b9822013-04-07 22:00:46 +02003207 if( ret == 0 )
3208 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003210 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003211 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003212
3213 for( i = 0; i < name->val.len; i++ )
3214 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003215 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 break;
3217
3218 c = name->val.p[i];
3219 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3220 s[i] = '?';
3221 else s[i] = c;
3222 }
3223 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003224 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003225 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 name = name->next;
3227 }
3228
Paul Bakker23986e52011-04-24 08:57:21 +00003229 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003230}
3231
3232/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003233 * Store the serial in printable form into buf; no more
3234 * than size characters will be written
3235 */
3236int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3237{
Paul Bakker23986e52011-04-24 08:57:21 +00003238 int ret;
3239 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003240 char *p;
3241
3242 p = buf;
3243 n = size;
3244
3245 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003246 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003247
3248 for( i = 0; i < nr; i++ )
3249 {
Paul Bakker93048802011-12-05 14:38:06 +00003250 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003251 continue;
3252
Paul Bakkerdd476992011-01-16 21:34:59 +00003253 ret = snprintf( p, n, "%02X%s",
3254 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3255 SAFE_SNPRINTF();
3256 }
3257
Paul Bakker03c7c252011-11-25 12:37:37 +00003258 if( nr != serial->len )
3259 {
3260 ret = snprintf( p, n, "...." );
3261 SAFE_SNPRINTF();
3262 }
3263
Paul Bakker23986e52011-04-24 08:57:21 +00003264 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003265}
3266
3267/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003268 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003270int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3271 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003272{
Paul Bakker23986e52011-04-24 08:57:21 +00003273 int ret;
3274 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003275 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003276 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
3278 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003279 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Paul Bakkerd98030e2009-05-02 15:13:40 +00003281 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003283 SAFE_SNPRINTF();
3284 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003286 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Paul Bakkerdd476992011-01-16 21:34:59 +00003288 ret = x509parse_serial_gets( p, n, &crt->serial);
3289 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Paul Bakkerd98030e2009-05-02 15:13:40 +00003291 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3292 SAFE_SNPRINTF();
3293 ret = x509parse_dn_gets( p, n, &crt->issuer );
3294 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Paul Bakkerd98030e2009-05-02 15:13:40 +00003296 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3297 SAFE_SNPRINTF();
3298 ret = x509parse_dn_gets( p, n, &crt->subject );
3299 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Paul Bakkerd98030e2009-05-02 15:13:40 +00003301 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3303 crt->valid_from.year, crt->valid_from.mon,
3304 crt->valid_from.day, crt->valid_from.hour,
3305 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003306 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
Paul Bakkerd98030e2009-05-02 15:13:40 +00003308 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3310 crt->valid_to.year, crt->valid_to.mon,
3311 crt->valid_to.day, crt->valid_to.hour,
3312 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003313 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Paul Bakkerc70b9822013-04-07 22:00:46 +02003315 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003316 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Paul Bakkerc70b9822013-04-07 22:00:46 +02003318 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3319 if( ret != 0 )
3320 ret = snprintf( p, n, "???" );
3321 else
3322 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003323 SAFE_SNPRINTF();
3324
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +02003325 switch( crt->pk.type )
3326 {
3327 case POLARSSL_PK_NONE:
3328 case POLARSSL_PK_ECDSA:
3329 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
3330 break;
3331
3332 case POLARSSL_PK_RSA:
3333 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3334 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3335 break;
3336
3337 case POLARSSL_PK_ECKEY:
3338 case POLARSSL_PK_ECKEY_DH:
3339 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3340 (int) pk_ec( crt->pk )->grp.pbits );
3341 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00003342 SAFE_SNPRINTF();
3343
Paul Bakker23986e52011-04-24 08:57:21 +00003344 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003345}
3346
Paul Bakker74111d32011-01-15 16:57:55 +00003347/*
3348 * Return an informational string describing the given OID
3349 */
3350const char *x509_oid_get_description( x509_buf *oid )
3351{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003352 const char *desc = NULL;
3353 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003354
Paul Bakkerc70b9822013-04-07 22:00:46 +02003355 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003356
Paul Bakkerc70b9822013-04-07 22:00:46 +02003357 if( ret != 0 )
3358 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003359
Paul Bakkerc70b9822013-04-07 22:00:46 +02003360 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003361}
3362
3363/* Return the x.y.z.... style numeric string for the given OID */
3364int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3365{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003366 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003367}
3368
Paul Bakkerd98030e2009-05-02 15:13:40 +00003369/*
3370 * Return an informational string about the CRL.
3371 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003372int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3373 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003374{
Paul Bakker23986e52011-04-24 08:57:21 +00003375 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003376 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003377 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003378 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003379 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003380
3381 p = buf;
3382 n = size;
3383
3384 ret = snprintf( p, n, "%sCRL version : %d",
3385 prefix, crl->version );
3386 SAFE_SNPRINTF();
3387
3388 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3389 SAFE_SNPRINTF();
3390 ret = x509parse_dn_gets( p, n, &crl->issuer );
3391 SAFE_SNPRINTF();
3392
3393 ret = snprintf( p, n, "\n%sthis update : " \
3394 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3395 crl->this_update.year, crl->this_update.mon,
3396 crl->this_update.day, crl->this_update.hour,
3397 crl->this_update.min, crl->this_update.sec );
3398 SAFE_SNPRINTF();
3399
3400 ret = snprintf( p, n, "\n%snext update : " \
3401 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3402 crl->next_update.year, crl->next_update.mon,
3403 crl->next_update.day, crl->next_update.hour,
3404 crl->next_update.min, crl->next_update.sec );
3405 SAFE_SNPRINTF();
3406
3407 entry = &crl->entry;
3408
3409 ret = snprintf( p, n, "\n%sRevoked certificates:",
3410 prefix );
3411 SAFE_SNPRINTF();
3412
Paul Bakker9be19372009-07-27 20:21:53 +00003413 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003414 {
3415 ret = snprintf( p, n, "\n%sserial number: ",
3416 prefix );
3417 SAFE_SNPRINTF();
3418
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003419 ret = x509parse_serial_gets( p, n, &entry->serial);
3420 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003421
Paul Bakkerd98030e2009-05-02 15:13:40 +00003422 ret = snprintf( p, n, " revocation date: " \
3423 "%04d-%02d-%02d %02d:%02d:%02d",
3424 entry->revocation_date.year, entry->revocation_date.mon,
3425 entry->revocation_date.day, entry->revocation_date.hour,
3426 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003427 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003428
3429 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 }
3431
Paul Bakkerc70b9822013-04-07 22:00:46 +02003432 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003433 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Paul Bakkerc70b9822013-04-07 22:00:46 +02003435 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3436 if( ret != 0 )
3437 ret = snprintf( p, n, "???" );
3438 else
3439 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003440 SAFE_SNPRINTF();
3441
Paul Bakker1e27bb22009-07-19 20:25:25 +00003442 ret = snprintf( p, n, "\n" );
3443 SAFE_SNPRINTF();
3444
Paul Bakker23986e52011-04-24 08:57:21 +00003445 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003446}
3447
3448/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003449 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003451#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003452int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003453{
Paul Bakkercce9d772011-11-18 14:26:47 +00003454 int year, mon, day;
3455 int hour, min, sec;
3456
3457#if defined(_WIN32)
3458 SYSTEMTIME st;
3459
3460 GetLocalTime(&st);
3461
3462 year = st.wYear;
3463 mon = st.wMonth;
3464 day = st.wDay;
3465 hour = st.wHour;
3466 min = st.wMinute;
3467 sec = st.wSecond;
3468#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 struct tm *lt;
3470 time_t tt;
3471
3472 tt = time( NULL );
3473 lt = localtime( &tt );
3474
Paul Bakkercce9d772011-11-18 14:26:47 +00003475 year = lt->tm_year + 1900;
3476 mon = lt->tm_mon + 1;
3477 day = lt->tm_mday;
3478 hour = lt->tm_hour;
3479 min = lt->tm_min;
3480 sec = lt->tm_sec;
3481#endif
3482
3483 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003484 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Paul Bakkercce9d772011-11-18 14:26:47 +00003486 if( year == to->year &&
3487 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003488 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Paul Bakkercce9d772011-11-18 14:26:47 +00003490 if( year == to->year &&
3491 mon == to->mon &&
3492 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003493 return( 1 );
3494
Paul Bakkercce9d772011-11-18 14:26:47 +00003495 if( year == to->year &&
3496 mon == to->mon &&
3497 day == to->day &&
3498 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003499 return( 1 );
3500
Paul Bakkercce9d772011-11-18 14:26:47 +00003501 if( year == to->year &&
3502 mon == to->mon &&
3503 day == to->day &&
3504 hour == to->hour &&
3505 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003506 return( 1 );
3507
Paul Bakkercce9d772011-11-18 14:26:47 +00003508 if( year == to->year &&
3509 mon == to->mon &&
3510 day == to->day &&
3511 hour == to->hour &&
3512 min == to->min &&
3513 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003514 return( 1 );
3515
Paul Bakker40ea7de2009-05-03 10:18:48 +00003516 return( 0 );
3517}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003518#else /* POLARSSL_HAVE_TIME */
3519int x509parse_time_expired( const x509_time *to )
3520{
3521 ((void) to);
3522 return( 0 );
3523}
3524#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003525
3526/*
3527 * Return 1 if the certificate is revoked, or 0 otherwise.
3528 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003529int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003530{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003531 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003532
3533 while( cur != NULL && cur->serial.len != 0 )
3534 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003535 if( crt->serial.len == cur->serial.len &&
3536 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003537 {
3538 if( x509parse_time_expired( &cur->revocation_date ) )
3539 return( 1 );
3540 }
3541
3542 cur = cur->next;
3543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
3545 return( 0 );
3546}
3547
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003548/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003549 * Check that the given certificate is valid accoring to the CRL.
3550 */
3551static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3552 x509_crl *crl_list)
3553{
3554 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003555 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3556 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003557
Paul Bakker915275b2012-09-28 07:10:55 +00003558 if( ca == NULL )
3559 return( flags );
3560
Paul Bakker76fd75a2011-01-16 21:12:10 +00003561 /*
3562 * TODO: What happens if no CRL is present?
3563 * Suggestion: Revocation state should be unknown if no CRL is present.
3564 * For backwards compatibility this is not yet implemented.
3565 */
3566
Paul Bakker915275b2012-09-28 07:10:55 +00003567 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003568 {
Paul Bakker915275b2012-09-28 07:10:55 +00003569 if( crl_list->version == 0 ||
3570 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003571 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3572 crl_list->issuer_raw.len ) != 0 )
3573 {
3574 crl_list = crl_list->next;
3575 continue;
3576 }
3577
3578 /*
3579 * Check if CRL is correctly signed by the trusted CA
3580 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003581 md_info = md_info_from_type( crl_list->sig_md );
3582 if( md_info == NULL )
3583 {
3584 /*
3585 * Cannot check 'unknown' hash
3586 */
3587 flags |= BADCRL_NOT_TRUSTED;
3588 break;
3589 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003590
Paul Bakkerc70b9822013-04-07 22:00:46 +02003591 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003592
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003593 /* EC NOT IMPLEMENTED YET */
3594 if( ca->pk.type != POLARSSL_PK_RSA )
3595 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3596
3597 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003598 0, hash, crl_list->sig.p ) == 0 )
3599 {
3600 /*
3601 * CRL is not trusted
3602 */
3603 flags |= BADCRL_NOT_TRUSTED;
3604 break;
3605 }
3606
3607 /*
3608 * Check for validity of CRL (Do not drop out)
3609 */
3610 if( x509parse_time_expired( &crl_list->next_update ) )
3611 flags |= BADCRL_EXPIRED;
3612
3613 /*
3614 * Check if certificate is revoked
3615 */
3616 if( x509parse_revoked(crt, crl_list) )
3617 {
3618 flags |= BADCERT_REVOKED;
3619 break;
3620 }
3621
3622 crl_list = crl_list->next;
3623 }
3624 return flags;
3625}
3626
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003627static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003628{
3629 size_t i;
3630 size_t cn_idx = 0;
3631
Paul Bakker57b12982012-02-11 17:38:38 +00003632 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003633 return( 0 );
3634
3635 for( i = 0; i < strlen( cn ); ++i )
3636 {
3637 if( cn[i] == '.' )
3638 {
3639 cn_idx = i;
3640 break;
3641 }
3642 }
3643
3644 if( cn_idx == 0 )
3645 return( 0 );
3646
Paul Bakker535e97d2012-08-23 10:49:55 +00003647 if( strlen( cn ) - cn_idx == name->len - 1 &&
3648 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003649 {
3650 return( 1 );
3651 }
3652
3653 return( 0 );
3654}
3655
Paul Bakker915275b2012-09-28 07:10:55 +00003656static int x509parse_verify_top(
3657 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003658 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003659 int (*f_vrfy)(void *, x509_cert *, int, int *),
3660 void *p_vrfy )
3661{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003662 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003663 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003664 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3665 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003666
3667 if( x509parse_time_expired( &child->valid_to ) )
3668 *flags |= BADCERT_EXPIRED;
3669
3670 /*
3671 * Child is the top of the chain. Check against the trust_ca list.
3672 */
3673 *flags |= BADCERT_NOT_TRUSTED;
3674
3675 while( trust_ca != NULL )
3676 {
3677 if( trust_ca->version == 0 ||
3678 child->issuer_raw.len != trust_ca->subject_raw.len ||
3679 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3680 child->issuer_raw.len ) != 0 )
3681 {
3682 trust_ca = trust_ca->next;
3683 continue;
3684 }
3685
Paul Bakker9a736322012-11-14 12:39:52 +00003686 /*
3687 * Reduce path_len to check against if top of the chain is
3688 * the same as the trusted CA
3689 */
3690 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3691 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3692 child->issuer_raw.len ) == 0 )
3693 {
3694 check_path_cnt--;
3695 }
3696
Paul Bakker915275b2012-09-28 07:10:55 +00003697 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003698 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003699 {
3700 trust_ca = trust_ca->next;
3701 continue;
3702 }
3703
Paul Bakkerc70b9822013-04-07 22:00:46 +02003704 md_info = md_info_from_type( child->sig_md );
3705 if( md_info == NULL )
3706 {
3707 /*
3708 * Cannot check 'unknown' hash
3709 */
3710 continue;
3711 }
Paul Bakker915275b2012-09-28 07:10:55 +00003712
Paul Bakkerc70b9822013-04-07 22:00:46 +02003713 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003714
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003715 /* EC NOT IMPLEMENTED YET */
3716 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3717 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3718
3719 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003720 0, hash, child->sig.p ) != 0 )
3721 {
3722 trust_ca = trust_ca->next;
3723 continue;
3724 }
3725
3726 /*
3727 * Top of chain is signed by a trusted CA
3728 */
3729 *flags &= ~BADCERT_NOT_TRUSTED;
3730 break;
3731 }
3732
Paul Bakker9a736322012-11-14 12:39:52 +00003733 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003734 * If top of chain is not the same as the trusted CA send a verify request
3735 * to the callback for any issues with validity and CRL presence for the
3736 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003737 */
3738 if( trust_ca != NULL &&
3739 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3740 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3741 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003742 {
3743 /* Check trusted CA's CRL for then chain's top crt */
3744 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3745
3746 if( x509parse_time_expired( &trust_ca->valid_to ) )
3747 ca_flags |= BADCERT_EXPIRED;
3748
Paul Bakker915275b2012-09-28 07:10:55 +00003749 if( NULL != f_vrfy )
3750 {
Paul Bakker9a736322012-11-14 12:39:52 +00003751 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003752 return( ret );
3753 }
3754 }
3755
3756 /* Call callback on top cert */
3757 if( NULL != f_vrfy )
3758 {
Paul Bakker9a736322012-11-14 12:39:52 +00003759 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003760 return( ret );
3761 }
3762
Paul Bakker915275b2012-09-28 07:10:55 +00003763 *flags |= ca_flags;
3764
3765 return( 0 );
3766}
3767
3768static int x509parse_verify_child(
3769 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003770 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003771 int (*f_vrfy)(void *, x509_cert *, int, int *),
3772 void *p_vrfy )
3773{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003774 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003775 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003776 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003777 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003778 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003779
3780 if( x509parse_time_expired( &child->valid_to ) )
3781 *flags |= BADCERT_EXPIRED;
3782
Paul Bakkerc70b9822013-04-07 22:00:46 +02003783 md_info = md_info_from_type( child->sig_md );
3784 if( md_info == NULL )
3785 {
3786 /*
3787 * Cannot check 'unknown' hash
3788 */
Paul Bakker915275b2012-09-28 07:10:55 +00003789 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003790 }
3791 else
3792 {
3793 md( md_info, child->tbs.p, child->tbs.len, hash );
3794
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003795 /* EC NOT IMPLEMENTED YET */
3796 if( parent->pk.type != POLARSSL_PK_RSA )
3797 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3798
3799 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3800 0, hash, child->sig.p ) != 0 )
3801 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003802 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003803 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003804 }
3805
Paul Bakker915275b2012-09-28 07:10:55 +00003806 /* Check trusted CA's CRL for the given crt */
3807 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3808
3809 grandparent = parent->next;
3810
3811 while( grandparent != NULL )
3812 {
3813 if( grandparent->version == 0 ||
3814 grandparent->ca_istrue == 0 ||
3815 parent->issuer_raw.len != grandparent->subject_raw.len ||
3816 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3817 parent->issuer_raw.len ) != 0 )
3818 {
3819 grandparent = grandparent->next;
3820 continue;
3821 }
3822 break;
3823 }
3824
Paul Bakker915275b2012-09-28 07:10:55 +00003825 if( grandparent != NULL )
3826 {
3827 /*
3828 * Part of the chain
3829 */
Paul Bakker9a736322012-11-14 12:39:52 +00003830 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 +00003831 if( ret != 0 )
3832 return( ret );
3833 }
3834 else
3835 {
Paul Bakker9a736322012-11-14 12:39:52 +00003836 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 +00003837 if( ret != 0 )
3838 return( ret );
3839 }
3840
3841 /* child is verified to be a child of the parent, call verify callback */
3842 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003843 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003844 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003845
3846 *flags |= parent_flags;
3847
3848 return( 0 );
3849}
3850
Paul Bakker76fd75a2011-01-16 21:12:10 +00003851/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 * Verify the certificate validity
3853 */
3854int x509parse_verify( x509_cert *crt,
3855 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003856 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003857 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003858 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003859 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860{
Paul Bakker23986e52011-04-24 08:57:21 +00003861 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003862 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003863 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003864 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003866 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
Paul Bakker40ea7de2009-05-03 10:18:48 +00003868 *flags = 0;
3869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( cn != NULL )
3871 {
3872 name = &crt->subject;
3873 cn_len = strlen( cn );
3874
Paul Bakker4d2c1242012-05-10 14:12:46 +00003875 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003877 cur = &crt->subject_alt_names;
3878
3879 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003880 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003881 if( cur->buf.len == cn_len &&
3882 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003883 break;
3884
Paul Bakker535e97d2012-08-23 10:49:55 +00003885 if( cur->buf.len > 2 &&
3886 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003887 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003888 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003889
Paul Bakker4d2c1242012-05-10 14:12:46 +00003890 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003891 }
3892
3893 if( cur == NULL )
3894 *flags |= BADCERT_CN_MISMATCH;
3895 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003896 else
3897 {
3898 while( name != NULL )
3899 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003900 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003901 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003902 if( name->val.len == cn_len &&
3903 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003904 break;
3905
Paul Bakker535e97d2012-08-23 10:49:55 +00003906 if( name->val.len > 2 &&
3907 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003908 x509_wildcard_verify( cn, &name->val ) )
3909 break;
3910 }
3911
3912 name = name->next;
3913 }
3914
3915 if( name == NULL )
3916 *flags |= BADCERT_CN_MISMATCH;
3917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 }
3919
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003921 * Iterate upwards in the given cert chain, to find our crt parent.
3922 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003924 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
Paul Bakker76fd75a2011-01-16 21:12:10 +00003926 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003928 if( parent->ca_istrue == 0 ||
3929 crt->issuer_raw.len != parent->subject_raw.len ||
3930 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 crt->issuer_raw.len ) != 0 )
3932 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003933 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 continue;
3935 }
Paul Bakker915275b2012-09-28 07:10:55 +00003936 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 }
3938
Paul Bakker915275b2012-09-28 07:10:55 +00003939 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 {
Paul Bakker915275b2012-09-28 07:10:55 +00003941 /*
3942 * Part of the chain
3943 */
Paul Bakker9a736322012-11-14 12:39:52 +00003944 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003945 if( ret != 0 )
3946 return( ret );
3947 }
3948 else
Paul Bakker74111d32011-01-15 16:57:55 +00003949 {
Paul Bakker9a736322012-11-14 12:39:52 +00003950 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003951 if( ret != 0 )
3952 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003953 }
Paul Bakker915275b2012-09-28 07:10:55 +00003954
3955 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003956 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003957
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 return( 0 );
3959}
3960
3961/*
3962 * Unallocate all certificate data
3963 */
3964void x509_free( x509_cert *crt )
3965{
3966 x509_cert *cert_cur = crt;
3967 x509_cert *cert_prv;
3968 x509_name *name_cur;
3969 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003970 x509_sequence *seq_cur;
3971 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
3973 if( crt == NULL )
3974 return;
3975
3976 do
3977 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003978 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
3980 name_cur = cert_cur->issuer.next;
3981 while( name_cur != NULL )
3982 {
3983 name_prv = name_cur;
3984 name_cur = name_cur->next;
3985 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003986 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 }
3988
3989 name_cur = cert_cur->subject.next;
3990 while( name_cur != NULL )
3991 {
3992 name_prv = name_cur;
3993 name_cur = name_cur->next;
3994 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003995 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003996 }
3997
Paul Bakker74111d32011-01-15 16:57:55 +00003998 seq_cur = cert_cur->ext_key_usage.next;
3999 while( seq_cur != NULL )
4000 {
4001 seq_prv = seq_cur;
4002 seq_cur = seq_cur->next;
4003 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004004 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004005 }
4006
Paul Bakker8afa70d2012-02-11 18:42:45 +00004007 seq_cur = cert_cur->subject_alt_names.next;
4008 while( seq_cur != NULL )
4009 {
4010 seq_prv = seq_cur;
4011 seq_cur = seq_cur->next;
4012 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004013 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004014 }
4015
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 if( cert_cur->raw.p != NULL )
4017 {
4018 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004019 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 }
4021
4022 cert_cur = cert_cur->next;
4023 }
4024 while( cert_cur != NULL );
4025
4026 cert_cur = crt;
4027 do
4028 {
4029 cert_prv = cert_cur;
4030 cert_cur = cert_cur->next;
4031
4032 memset( cert_prv, 0, sizeof( x509_cert ) );
4033 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004034 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 }
4036 while( cert_cur != NULL );
4037}
4038
Paul Bakkerd98030e2009-05-02 15:13:40 +00004039/*
4040 * Unallocate all CRL data
4041 */
4042void x509_crl_free( x509_crl *crl )
4043{
4044 x509_crl *crl_cur = crl;
4045 x509_crl *crl_prv;
4046 x509_name *name_cur;
4047 x509_name *name_prv;
4048 x509_crl_entry *entry_cur;
4049 x509_crl_entry *entry_prv;
4050
4051 if( crl == NULL )
4052 return;
4053
4054 do
4055 {
4056 name_cur = crl_cur->issuer.next;
4057 while( name_cur != NULL )
4058 {
4059 name_prv = name_cur;
4060 name_cur = name_cur->next;
4061 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004062 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004063 }
4064
4065 entry_cur = crl_cur->entry.next;
4066 while( entry_cur != NULL )
4067 {
4068 entry_prv = entry_cur;
4069 entry_cur = entry_cur->next;
4070 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004071 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004072 }
4073
4074 if( crl_cur->raw.p != NULL )
4075 {
4076 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004077 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004078 }
4079
4080 crl_cur = crl_cur->next;
4081 }
4082 while( crl_cur != NULL );
4083
4084 crl_cur = crl;
4085 do
4086 {
4087 crl_prv = crl_cur;
4088 crl_cur = crl_cur->next;
4089
4090 memset( crl_prv, 0, sizeof( x509_crl ) );
4091 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004092 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004093 }
4094 while( crl_cur != NULL );
4095}
4096
Paul Bakker40e46942009-01-03 21:51:57 +00004097#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004098
Paul Bakker40e46942009-01-03 21:51:57 +00004099#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004100
4101/*
4102 * Checkup routine
4103 */
4104int x509_self_test( int verbose )
4105{
Paul Bakker5690efc2011-05-26 13:16:06 +00004106#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004107 int ret;
4108 int flags;
4109 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004110 x509_cert cacert;
4111 x509_cert clicert;
4112 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00004113#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004114 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004115#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004116
4117 if( verbose != 0 )
4118 printf( " X.509 certificate load: " );
4119
4120 memset( &clicert, 0, sizeof( x509_cert ) );
4121
Paul Bakker3c2122f2013-06-24 19:03:14 +02004122 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004123 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 if( ret != 0 )
4125 {
4126 if( verbose != 0 )
4127 printf( "failed\n" );
4128
4129 return( ret );
4130 }
4131
4132 memset( &cacert, 0, sizeof( x509_cert ) );
4133
Paul Bakker3c2122f2013-06-24 19:03:14 +02004134 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004135 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004136 if( ret != 0 )
4137 {
4138 if( verbose != 0 )
4139 printf( "failed\n" );
4140
4141 return( ret );
4142 }
4143
4144 if( verbose != 0 )
4145 printf( "passed\n X.509 private key load: " );
4146
4147 i = strlen( test_ca_key );
4148 j = strlen( test_ca_pwd );
4149
Paul Bakker66b78b22011-03-25 14:22:50 +00004150 rsa_init( &rsa, RSA_PKCS_V15, 0 );
4151
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02004152 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004153 (const unsigned char *) test_ca_key, i,
4154 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155 {
4156 if( verbose != 0 )
4157 printf( "failed\n" );
4158
4159 return( ret );
4160 }
4161
4162 if( verbose != 0 )
4163 printf( "passed\n X.509 signature verify: ");
4164
Paul Bakker23986e52011-04-24 08:57:21 +00004165 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 if( ret != 0 )
4167 {
Paul Bakker23986e52011-04-24 08:57:21 +00004168 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00004169 if( verbose != 0 )
4170 printf( "failed\n" );
4171
4172 return( ret );
4173 }
4174
Paul Bakker5690efc2011-05-26 13:16:06 +00004175#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004176 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004177 printf( "passed\n X.509 DHM parameter load: " );
4178
4179 i = strlen( test_dhm_params );
4180 j = strlen( test_ca_pwd );
4181
Paul Bakker3c2122f2013-06-24 19:03:14 +02004182 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004183 {
4184 if( verbose != 0 )
4185 printf( "failed\n" );
4186
4187 return( ret );
4188 }
4189
4190 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004192#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004193
4194 x509_free( &cacert );
4195 x509_free( &clicert );
4196 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004197#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004198 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004200
4201 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004202#else
4203 ((void) verbose);
4204 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4205#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004206}
4207
4208#endif
4209
4210#endif