blob: bbaca8ea44cc5a80edac8bdf81eb2a66bf555d71 [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é-Gonnardab2d9832013-07-11 16:17:23 +0200214
215#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200216/* Get an EC group id from an ECParameters buffer
217 *
218 * ECParameters ::= CHOICE {
219 * namedCurve OBJECT IDENTIFIER
220 * -- implicitCurve NULL
221 * -- specifiedCurve SpecifiedECDomain
222 * }
223 */
224static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200225 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226{
227 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200231 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200232 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
233
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200234 params->p = *p;
235 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200236
237 if( *p != end )
238 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
239 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
240
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200241 return( 0 );
242}
243
244/*
245 * Use EC parameters to initialise an EC group
246 */
247static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
248{
249 int ret;
250 ecp_group_id grp_id;
251
252 if( oid_get_ec_grp( params, &grp_id ) != 0 )
253 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
254
255 /*
256 * grp may already be initilialized; if so, make sure IDs match
257 */
258 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
259 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
260
261 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
262 return( ret );
263
264 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200265}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200266#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * AttributeTypeAndValue ::= SEQUENCE {
270 * type AttributeType,
271 * value AttributeValue }
272 *
273 * AttributeType ::= OBJECT IDENTIFIER
274 *
275 * AttributeValue ::= ANY DEFINED BY AttributeType
276 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277static int x509_get_attr_type_value( unsigned char **p,
278 const unsigned char *end,
279 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 x509_buf *oid;
284 x509_buf *val;
285
286 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200290 if( ( end - *p ) < 1 )
291 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
292 POLARSSL_ERR_ASN1_OUT_OF_DATA );
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 oid = &cur->oid;
295 oid->tag = **p;
296
297 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 oid->p = *p;
301 *p += oid->len;
302
303 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
308 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
309 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000311 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 val = &cur->val;
314 val->tag = *(*p)++;
315
316 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319 val->p = *p;
320 *p += val->len;
321
322 cur->next = NULL;
323
Paul Bakker400ff6f2011-02-20 10:40:16 +0000324 return( 0 );
325}
326
327/*
328 * RelativeDistinguishedName ::=
329 * SET OF AttributeTypeAndValue
330 *
331 * AttributeTypeAndValue ::= SEQUENCE {
332 * type AttributeType,
333 * value AttributeValue }
334 *
335 * AttributeType ::= OBJECT IDENTIFIER
336 *
337 * AttributeValue ::= ANY DEFINED BY AttributeType
338 */
339static int x509_get_name( unsigned char **p,
340 const unsigned char *end,
341 x509_name *cur )
342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret;
344 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000345 const unsigned char *end2;
346 x509_name *use;
347
348 if( ( ret = asn1_get_tag( p, end, &len,
349 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000351
352 end2 = end;
353 end = *p + len;
354 use = cur;
355
356 do
357 {
358 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
359 return( ret );
360
361 if( *p != end )
362 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200363 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000364 sizeof( x509_name ) );
365
366 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000367 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368
369 memset( use->next, 0, sizeof( x509_name ) );
370
371 use = use->next;
372 }
373 }
374 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 /*
377 * recurse until end of SEQUENCE is reached
378 */
379 if( *p == end2 )
380 return( 0 );
381
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 sizeof( x509_name ) );
384
385 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000386 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Paul Bakker430ffbe2012-05-01 08:14:20 +0000388 memset( cur->next, 0, sizeof( x509_name ) );
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 return( x509_get_name( p, end2, cur->next ) );
391}
392
393/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * Time ::= CHOICE {
395 * utcTime UTCTime,
396 * generalTime GeneralizedTime }
397 */
Paul Bakker91200182010-02-18 21:26:15 +0000398static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400 x509_time *time )
401{
Paul Bakker23986e52011-04-24 08:57:21 +0000402 int ret;
403 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000405 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406
Paul Bakker91200182010-02-18 21:26:15 +0000407 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000408 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
409 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412
Paul Bakker91200182010-02-18 21:26:15 +0000413 if ( tag == ASN1_UTC_TIME )
414 {
415 (*p)++;
416 ret = asn1_get_len( p, end, &len );
417
418 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420
Paul Bakker91200182010-02-18 21:26:15 +0000421 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000422 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
423 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424
Paul Bakker91200182010-02-18 21:26:15 +0000425 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
426 &time->year, &time->mon, &time->day,
427 &time->hour, &time->min, &time->sec ) < 5 )
428 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429
Paul Bakker400ff6f2011-02-20 10:40:16 +0000430 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000431 time->year += 1900;
432
433 *p += len;
434
435 return( 0 );
436 }
437 else if ( tag == ASN1_GENERALIZED_TIME )
438 {
439 (*p)++;
440 ret = asn1_get_len( p, end, &len );
441
442 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000443 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000444
445 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000446 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
447 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000448
449 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
450 &time->year, &time->mon, &time->day,
451 &time->hour, &time->min, &time->sec ) < 5 )
452 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
453
454 *p += len;
455
456 return( 0 );
457 }
458 else
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000460}
461
462
463/*
464 * Validity ::= SEQUENCE {
465 * notBefore Time,
466 * notAfter Time }
467 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000469 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 x509_time *from,
471 x509_time *to )
472{
Paul Bakker23986e52011-04-24 08:57:21 +0000473 int ret;
474 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 if( ( ret = asn1_get_tag( p, end, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 end = *p + len;
481
Paul Bakker91200182010-02-18 21:26:15 +0000482 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker91200182010-02-18 21:26:15 +0000485 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200495#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200497 * RSAPublicKey ::= SEQUENCE {
498 * modulus INTEGER, -- n
499 * publicExponent INTEGER -- e
500 * }
501 */
502static int x509_get_rsapubkey( unsigned char **p,
503 const unsigned char *end,
504 rsa_context *rsa )
505{
506 int ret;
507 size_t len;
508
509 if( ( ret = asn1_get_tag( p, end, &len,
510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
511 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
512
513 if( *p + len != end )
514 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
515 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
516
517 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
518 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
519 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
520
Manuel Pégourié-Gonnardc13c0d42013-08-15 13:58:01 +0200521 if( *p != end )
522 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200525 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
526 return( ret );
527
528 rsa->len = mpi_size( &rsa->N );
529
530 return( 0 );
531}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200532#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200533
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200534#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200535/*
536 * EC public key is an EC point
537 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200538static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200539 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200540{
541 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200542
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200543 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200544 (const unsigned char *) *p, end - *p ) ) != 0 ||
545 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200546 {
547 ecp_keypair_free( key );
548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200549 }
550
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200551 /*
552 * We know ecp_point_read_binary consumed all bytes
553 */
554 *p = (unsigned char *) end;
555
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200556 return( 0 );
557}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200558#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200559
560/*
561 * SubjectPublicKeyInfo ::= SEQUENCE {
562 * algorithm AlgorithmIdentifier,
563 * subjectPublicKey BIT STRING }
564 */
565static int x509_get_pubkey( unsigned char **p,
566 const unsigned char *end,
567 pk_context *pk )
568{
569 int ret;
570 size_t len;
571 x509_buf alg_params;
572 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200573 const pk_info_t *pk_info;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200574
575 if( ( ret = asn1_get_tag( p, end, &len,
576 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
577 {
578 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
579 }
580
581 end = *p + len;
582
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200583 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200584 return( ret );
585
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200586 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200587 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
588
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200589 if( *p + len != end )
590 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
591 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
592
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200593 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
594 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
595
596 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200597 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200598
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200599#if defined(POLARSSL_RSA_C)
600 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200601 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200602 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
603 } else
604#endif /* POLARSSL_RSA_C */
605#if defined(POLARSSL_ECP_C)
606 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
607 {
608 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
609 x509_get_ecpubkey( p, end, pk_ec( *pk ) );
610 } else
611#endif /* POLARSSL_ECP_C */
612 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200613
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200614 if( ret == 0 && *p != end )
615 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
616 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
617
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200618 if( ret != 0 )
619 pk_free( pk );
620
621 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200622}
623
Paul Bakker5121ce52009-01-03 21:22:43 +0000624static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000625 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 x509_buf *sig )
627{
Paul Bakker23986e52011-04-24 08:57:21 +0000628 int ret;
629 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Paul Bakker8afa70d2012-02-11 18:42:45 +0000631 if( ( end - *p ) < 1 )
632 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
633 POLARSSL_ERR_ASN1_OUT_OF_DATA );
634
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 sig->tag = **p;
636
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200637 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000638 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 sig->len = len;
641 sig->p = *p;
642
643 *p += len;
644
645 return( 0 );
646}
647
648/*
649 * X.509 v2/v3 unique identifier (not parsed)
650 */
651static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000652 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 x509_buf *uid, int n )
654{
655 int ret;
656
657 if( *p == end )
658 return( 0 );
659
660 uid->tag = **p;
661
662 if( ( ret = asn1_get_tag( p, end, &uid->len,
663 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
664 {
Paul Bakker40e46942009-01-03 21:51:57 +0000665 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 return( 0 );
667
668 return( ret );
669 }
670
671 uid->p = *p;
672 *p += uid->len;
673
674 return( 0 );
675}
676
677/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000678 * X.509 Extensions (No parsing of extensions, pointer should
679 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 */
681static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000682 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000683 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000684{
Paul Bakker23986e52011-04-24 08:57:21 +0000685 int ret;
686 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
688 if( *p == end )
689 return( 0 );
690
691 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000692
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000694 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 ext->p = *p;
698 end = *p + ext->len;
699
700 /*
701 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
702 *
703 * Extension ::= SEQUENCE {
704 * extnID OBJECT IDENTIFIER,
705 * critical BOOLEAN DEFAULT FALSE,
706 * extnValue OCTET STRING }
707 */
708 if( ( ret = asn1_get_tag( p, end, &len,
709 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000713 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000714 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000715
Paul Bakkerd98030e2009-05-02 15:13:40 +0000716 return( 0 );
717}
718
719/*
720 * X.509 CRL v2 extensions (no extensions parsed yet.)
721 */
722static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000723 const unsigned char *end,
724 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000725{
Paul Bakker23986e52011-04-24 08:57:21 +0000726 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000727 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000728
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000729 /* Get explicit tag */
730 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000731 {
732 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
733 return( 0 );
734
735 return( ret );
736 }
737
738 while( *p < end )
739 {
740 if( ( ret = asn1_get_tag( p, end, &len,
741 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000742 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000743
744 *p += len;
745 }
746
747 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000748 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000749 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
750
751 return( 0 );
752}
753
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000754/*
755 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
756 */
757static int x509_get_crl_entry_ext( unsigned char **p,
758 const unsigned char *end,
759 x509_buf *ext )
760{
761 int ret;
762 size_t len = 0;
763
764 /* OPTIONAL */
765 if (end <= *p)
766 return( 0 );
767
768 ext->tag = **p;
769 ext->p = *p;
770
771 /*
772 * Get CRL-entry extension sequence header
773 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
774 */
775 if( ( ret = asn1_get_tag( p, end, &ext->len,
776 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
777 {
778 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
779 {
780 ext->p = NULL;
781 return( 0 );
782 }
783 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
784 }
785
786 end = *p + ext->len;
787
788 if( end != *p + ext->len )
789 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
790 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
791
792 while( *p < end )
793 {
794 if( ( ret = asn1_get_tag( p, end, &len,
795 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
796 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
797
798 *p += len;
799 }
800
801 if( *p != end )
802 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
803 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
804
805 return( 0 );
806}
807
Paul Bakker74111d32011-01-15 16:57:55 +0000808static int x509_get_basic_constraints( unsigned char **p,
809 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000810 int *ca_istrue,
811 int *max_pathlen )
812{
Paul Bakker23986e52011-04-24 08:57:21 +0000813 int ret;
814 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000815
816 /*
817 * BasicConstraints ::= SEQUENCE {
818 * cA BOOLEAN DEFAULT FALSE,
819 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
820 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000821 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000822 *max_pathlen = 0; /* endless */
823
824 if( ( ret = asn1_get_tag( p, end, &len,
825 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000826 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000827
828 if( *p == end )
829 return 0;
830
Paul Bakker3cccddb2011-01-16 21:46:31 +0000831 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000832 {
833 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000834 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000835
836 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000838
Paul Bakker3cccddb2011-01-16 21:46:31 +0000839 if( *ca_istrue != 0 )
840 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000841 }
842
843 if( *p == end )
844 return 0;
845
846 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000847 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000848
849 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000850 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000851 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
852
853 (*max_pathlen)++;
854
Paul Bakker74111d32011-01-15 16:57:55 +0000855 return 0;
856}
857
858static int x509_get_ns_cert_type( unsigned char **p,
859 const unsigned char *end,
860 unsigned char *ns_cert_type)
861{
862 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000863 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000864
865 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000866 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000867
868 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000869 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000870 POLARSSL_ERR_ASN1_INVALID_LENGTH );
871
872 /* Get actual bitstring */
873 *ns_cert_type = *bs.p;
874 return 0;
875}
876
877static int x509_get_key_usage( unsigned char **p,
878 const unsigned char *end,
879 unsigned char *key_usage)
880{
881 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000882 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000883
884 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000885 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000886
Paul Bakker94a67962012-08-23 13:03:52 +0000887 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000888 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000889 POLARSSL_ERR_ASN1_INVALID_LENGTH );
890
891 /* Get actual bitstring */
892 *key_usage = *bs.p;
893 return 0;
894}
895
Paul Bakkerd98030e2009-05-02 15:13:40 +0000896/*
Paul Bakker74111d32011-01-15 16:57:55 +0000897 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
898 *
899 * KeyPurposeId ::= OBJECT IDENTIFIER
900 */
901static int x509_get_ext_key_usage( unsigned char **p,
902 const unsigned char *end,
903 x509_sequence *ext_key_usage)
904{
905 int ret;
906
907 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000909
910 /* Sequence length must be >= 1 */
911 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000912 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000913 POLARSSL_ERR_ASN1_INVALID_LENGTH );
914
915 return 0;
916}
917
918/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000919 * SubjectAltName ::= GeneralNames
920 *
921 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
922 *
923 * GeneralName ::= CHOICE {
924 * otherName [0] OtherName,
925 * rfc822Name [1] IA5String,
926 * dNSName [2] IA5String,
927 * x400Address [3] ORAddress,
928 * directoryName [4] Name,
929 * ediPartyName [5] EDIPartyName,
930 * uniformResourceIdentifier [6] IA5String,
931 * iPAddress [7] OCTET STRING,
932 * registeredID [8] OBJECT IDENTIFIER }
933 *
934 * OtherName ::= SEQUENCE {
935 * type-id OBJECT IDENTIFIER,
936 * value [0] EXPLICIT ANY DEFINED BY type-id }
937 *
938 * EDIPartyName ::= SEQUENCE {
939 * nameAssigner [0] DirectoryString OPTIONAL,
940 * partyName [1] DirectoryString }
941 *
942 * NOTE: PolarSSL only parses and uses dNSName at this point.
943 */
944static int x509_get_subject_alt_name( unsigned char **p,
945 const unsigned char *end,
946 x509_sequence *subject_alt_name )
947{
948 int ret;
949 size_t len, tag_len;
950 asn1_buf *buf;
951 unsigned char tag;
952 asn1_sequence *cur = subject_alt_name;
953
954 /* Get main sequence tag */
955 if( ( ret = asn1_get_tag( p, end, &len,
956 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
958
959 if( *p + len != end )
960 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
961 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
962
963 while( *p < end )
964 {
965 if( ( end - *p ) < 1 )
966 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
967 POLARSSL_ERR_ASN1_OUT_OF_DATA );
968
969 tag = **p;
970 (*p)++;
971 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
972 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
973
974 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
975 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
976 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
977
978 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
979 {
980 *p += tag_len;
981 continue;
982 }
983
984 buf = &(cur->buf);
985 buf->tag = tag;
986 buf->p = *p;
987 buf->len = tag_len;
988 *p += buf->len;
989
990 /* Allocate and assign next pointer */
991 if (*p < end)
992 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200993 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000994 sizeof( asn1_sequence ) );
995
996 if( cur->next == NULL )
997 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
998 POLARSSL_ERR_ASN1_MALLOC_FAILED );
999
Paul Bakker535e97d2012-08-23 10:49:55 +00001000 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001001 cur = cur->next;
1002 }
1003 }
1004
1005 /* Set final sequence entry's next pointer to NULL */
1006 cur->next = NULL;
1007
1008 if( *p != end )
1009 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1010 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1011
1012 return( 0 );
1013}
1014
1015/*
Paul Bakker74111d32011-01-15 16:57:55 +00001016 * X.509 v3 extensions
1017 *
1018 * TODO: Perform all of the basic constraints tests required by the RFC
1019 * TODO: Set values for undetected extensions to a sane default?
1020 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001021 */
1022static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001023 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001024 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025{
Paul Bakker23986e52011-04-24 08:57:21 +00001026 int ret;
1027 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001028 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001029
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001030 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001031 {
1032 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1033 return( 0 );
1034
1035 return( ret );
1036 }
1037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 while( *p < end )
1039 {
Paul Bakker74111d32011-01-15 16:57:55 +00001040 /*
1041 * Extension ::= SEQUENCE {
1042 * extnID OBJECT IDENTIFIER,
1043 * critical BOOLEAN DEFAULT FALSE,
1044 * extnValue OCTET STRING }
1045 */
1046 x509_buf extn_oid = {0, 0, NULL};
1047 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001048 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 if( ( ret = asn1_get_tag( p, end, &len,
1051 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001052 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001054 end_ext_data = *p + len;
1055
Paul Bakker74111d32011-01-15 16:57:55 +00001056 /* Get extension ID */
1057 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Paul Bakker74111d32011-01-15 16:57:55 +00001059 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001060 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
Paul Bakker74111d32011-01-15 16:57:55 +00001062 extn_oid.p = *p;
1063 *p += extn_oid.len;
1064
1065 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001066 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001067 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1068
1069 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001070 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001071 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001072 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073
Paul Bakker74111d32011-01-15 16:57:55 +00001074 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001075 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001077 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001079 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001080
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001081 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001082 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001083 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Paul Bakker74111d32011-01-15 16:57:55 +00001085 /*
1086 * Detect supported extensions
1087 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001088 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1089
1090 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001091 {
1092 /* No parser found, skip extension */
1093 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Paul Bakker5c721f92011-07-27 16:51:09 +00001095#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001096 if( is_critical )
1097 {
1098 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001099 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001100 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1101 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001102#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001103 continue;
1104 }
1105
1106 crt->ext_types |= ext_type;
1107
1108 switch( ext_type )
1109 {
1110 case EXT_BASIC_CONSTRAINTS:
1111 /* Parse basic constraints */
1112 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1113 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1114 return ( ret );
1115 break;
1116
1117 case EXT_KEY_USAGE:
1118 /* Parse key usage */
1119 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1120 &crt->key_usage ) ) != 0 )
1121 return ( ret );
1122 break;
1123
1124 case EXT_EXTENDED_KEY_USAGE:
1125 /* Parse extended key usage */
1126 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1127 &crt->ext_key_usage ) ) != 0 )
1128 return ( ret );
1129 break;
1130
1131 case EXT_SUBJECT_ALT_NAME:
1132 /* Parse subject alt name */
1133 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1134 &crt->subject_alt_names ) ) != 0 )
1135 return ( ret );
1136 break;
1137
1138 case EXT_NS_CERT_TYPE:
1139 /* Parse netscape certificate type */
1140 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1141 &crt->ns_cert_type ) ) != 0 )
1142 return ( ret );
1143 break;
1144
1145 default:
1146 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 }
1149
1150 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001151 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001152 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( 0 );
1155}
1156
1157/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 * X.509 CRL Entries
1159 */
1160static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001161 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001162 x509_crl_entry *entry )
1163{
Paul Bakker23986e52011-04-24 08:57:21 +00001164 int ret;
1165 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001166 x509_crl_entry *cur_entry = entry;
1167
1168 if( *p == end )
1169 return( 0 );
1170
Paul Bakker9be19372009-07-27 20:21:53 +00001171 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001172 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1173 {
1174 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1175 return( 0 );
1176
1177 return( ret );
1178 }
1179
Paul Bakker9be19372009-07-27 20:21:53 +00001180 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001181
1182 while( *p < end )
1183 {
Paul Bakker23986e52011-04-24 08:57:21 +00001184 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001185 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186
1187 if( ( ret = asn1_get_tag( p, end, &len2,
1188 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1189 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001190 return( ret );
1191 }
1192
Paul Bakker9be19372009-07-27 20:21:53 +00001193 cur_entry->raw.tag = **p;
1194 cur_entry->raw.p = *p;
1195 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001196 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001197
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001198 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001199 return( ret );
1200
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001201 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001202 return( ret );
1203
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001204 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001205 return( ret );
1206
Paul Bakker74111d32011-01-15 16:57:55 +00001207 if ( *p < end )
1208 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001209 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001210
1211 if( cur_entry->next == NULL )
1212 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1213
Paul Bakkerd98030e2009-05-02 15:13:40 +00001214 cur_entry = cur_entry->next;
1215 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1216 }
1217 }
1218
1219 return( 0 );
1220}
1221
Paul Bakkerc70b9822013-04-07 22:00:46 +02001222static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1223 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001224{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001225 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001226
Paul Bakkerc70b9822013-04-07 22:00:46 +02001227 if( ret != 0 )
1228 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001229
Paul Bakkerc70b9822013-04-07 22:00:46 +02001230 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001231}
1232
Paul Bakkerd98030e2009-05-02 15:13:40 +00001233/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001234 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001236static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1237 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238{
Paul Bakker23986e52011-04-24 08:57:21 +00001239 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001240 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001241 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
Paul Bakker320a4b52009-03-28 18:52:39 +00001243 /*
1244 * Check for valid input
1245 */
1246 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001247 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001248
Paul Bakker6e339b52013-07-03 13:37:05 +02001249 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001250
1251 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001252 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001253
1254 memcpy( p, buf, buflen );
1255
1256 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
1258 crt->raw.p = p;
1259 crt->raw.len = len;
1260 end = p + len;
1261
1262 /*
1263 * Certificate ::= SEQUENCE {
1264 * tbsCertificate TBSCertificate,
1265 * signatureAlgorithm AlgorithmIdentifier,
1266 * signatureValue BIT STRING }
1267 */
1268 if( ( ret = asn1_get_tag( &p, end, &len,
1269 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001272 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 }
1274
Paul Bakkerb00ca422012-09-25 12:10:00 +00001275 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001277 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001278 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001279 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001281 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001282
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 /*
1284 * TBSCertificate ::= SEQUENCE {
1285 */
1286 crt->tbs.p = p;
1287
1288 if( ( ret = asn1_get_tag( &p, end, &len,
1289 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
1294
1295 end = p + len;
1296 crt->tbs.len = end - crt->tbs.p;
1297
1298 /*
1299 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1300 *
1301 * CertificateSerialNumber ::= INTEGER
1302 *
1303 * signature AlgorithmIdentifier
1304 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001305 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1306 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1307 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 return( ret );
1311 }
1312
1313 crt->version++;
1314
1315 if( crt->version > 3 )
1316 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001317 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001318 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 }
1320
Paul Bakkerc70b9822013-04-07 22:00:46 +02001321 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1322 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001325 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
1327
1328 /*
1329 * issuer Name
1330 */
1331 crt->issuer_raw.p = p;
1332
1333 if( ( ret = asn1_get_tag( &p, end, &len,
1334 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001337 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 }
1339
1340 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1341 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001342 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 return( ret );
1344 }
1345
1346 crt->issuer_raw.len = p - crt->issuer_raw.p;
1347
1348 /*
1349 * Validity ::= SEQUENCE {
1350 * notBefore Time,
1351 * notAfter Time }
1352 *
1353 */
1354 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1355 &crt->valid_to ) ) != 0 )
1356 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001357 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 return( ret );
1359 }
1360
1361 /*
1362 * subject Name
1363 */
1364 crt->subject_raw.p = p;
1365
1366 if( ( ret = asn1_get_tag( &p, end, &len,
1367 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001370 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
1372
Paul Bakkercefb3962012-06-27 11:51:09 +00001373 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 return( ret );
1377 }
1378
1379 crt->subject_raw.len = p - crt->subject_raw.p;
1380
1381 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001382 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001384 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 return( ret );
1388 }
1389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 /*
1391 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1392 * -- If present, version shall be v2 or v3
1393 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1394 * -- If present, version shall be v2 or v3
1395 * extensions [3] EXPLICIT Extensions OPTIONAL
1396 * -- If present, version shall be v3
1397 */
1398 if( crt->version == 2 || crt->version == 3 )
1399 {
1400 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1401 if( ret != 0 )
1402 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001403 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 return( ret );
1405 }
1406 }
1407
1408 if( crt->version == 2 || crt->version == 3 )
1409 {
1410 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1411 if( ret != 0 )
1412 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001413 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 return( ret );
1415 }
1416 }
1417
1418 if( crt->version == 3 )
1419 {
Paul Bakker74111d32011-01-15 16:57:55 +00001420 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 if( ret != 0 )
1422 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001423 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 return( ret );
1425 }
1426 }
1427
1428 if( p != end )
1429 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001430 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001431 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001432 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 }
1434
Paul Bakkerb00ca422012-09-25 12:10:00 +00001435 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
1437 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001438 * }
1439 * -- end of TBSCertificate
1440 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 * signatureAlgorithm AlgorithmIdentifier,
1442 * signatureValue BIT STRING
1443 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001444 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 return( ret );
1448 }
1449
Paul Bakker535e97d2012-08-23 10:49:55 +00001450 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1451 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001453 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001454 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 }
1456
1457 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 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 if( p != end )
1464 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001465 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001466 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001467 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
1469
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001470 return( 0 );
1471}
1472
1473/*
Paul Bakker42c65812013-06-24 19:21:59 +02001474 * Parse one X.509 certificate in DER format from a buffer and add them to a
1475 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001476 */
Paul Bakker42c65812013-06-24 19:21:59 +02001477int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001478{
Paul Bakker42c65812013-06-24 19:21:59 +02001479 int ret;
1480 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001481
1482 /*
1483 * Check for valid input
1484 */
1485 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001486 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001487
1488 while( crt->version != 0 && crt->next != NULL )
1489 {
1490 prev = crt;
1491 crt = crt->next;
1492 }
1493
1494 /*
1495 * Add new certificate on the end of the chain if needed.
1496 */
1497 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001498 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001499 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001500
Paul Bakker7d06ad22009-05-02 15:53:56 +00001501 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001502 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001503
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001504 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001505 crt = crt->next;
1506 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Paul Bakker42c65812013-06-24 19:21:59 +02001509 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1510 {
1511 if( prev )
1512 prev->next = NULL;
1513
1514 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001515 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001516
1517 return( ret );
1518 }
1519
1520 return( 0 );
1521}
1522
1523/*
1524 * Parse one or more PEM certificates from a buffer and add them to the chained list
1525 */
1526int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1527{
1528 int ret, success = 0, first_error = 0, total_failed = 0;
1529 int buf_format = X509_FORMAT_DER;
1530
1531 /*
1532 * Check for valid input
1533 */
1534 if( chain == NULL || buf == NULL )
1535 return( POLARSSL_ERR_X509_INVALID_INPUT );
1536
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001537 /*
1538 * Determine buffer content. Buffer contains either one DER certificate or
1539 * one or more PEM certificates.
1540 */
1541#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001542 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001543 buf_format = X509_FORMAT_PEM;
1544#endif
1545
1546 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001547 return x509parse_crt_der( chain, buf, buflen );
1548
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001549#if defined(POLARSSL_PEM_C)
1550 if( buf_format == X509_FORMAT_PEM )
1551 {
1552 pem_context pem;
1553
1554 while( buflen > 0 )
1555 {
1556 size_t use_len;
1557 pem_init( &pem );
1558
1559 ret = pem_read_buffer( &pem,
1560 "-----BEGIN CERTIFICATE-----",
1561 "-----END CERTIFICATE-----",
1562 buf, NULL, 0, &use_len );
1563
1564 if( ret == 0 )
1565 {
1566 /*
1567 * Was PEM encoded
1568 */
1569 buflen -= use_len;
1570 buf += use_len;
1571 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001572 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1573 {
1574 return( ret );
1575 }
Paul Bakker00b28602013-06-24 13:02:41 +02001576 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001577 {
1578 pem_free( &pem );
1579
Paul Bakker5ed3b342013-06-24 19:05:46 +02001580 /*
1581 * PEM header and footer were found
1582 */
1583 buflen -= use_len;
1584 buf += use_len;
1585
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001586 if( first_error == 0 )
1587 first_error = ret;
1588
1589 continue;
1590 }
1591 else
1592 break;
1593
Paul Bakker42c65812013-06-24 19:21:59 +02001594 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001595
1596 pem_free( &pem );
1597
1598 if( ret != 0 )
1599 {
1600 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001601 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001602 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001603 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001604 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001605
1606 if( first_error == 0 )
1607 first_error = ret;
1608
Paul Bakker42c65812013-06-24 19:21:59 +02001609 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 continue;
1611 }
1612
1613 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001614 }
1615 }
1616#endif
1617
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001618 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001619 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001620 else if( first_error )
1621 return( first_error );
1622 else
1623 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624}
1625
1626/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001627 * Parse one or more CRLs and add them to the chained list
1628 */
Paul Bakker23986e52011-04-24 08:57:21 +00001629int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001630{
Paul Bakker23986e52011-04-24 08:57:21 +00001631 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001632 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001633 unsigned char *p, *end;
1634 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001635#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001636 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001637 pem_context pem;
1638#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001639
1640 crl = chain;
1641
1642 /*
1643 * Check for valid input
1644 */
1645 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001646 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647
1648 while( crl->version != 0 && crl->next != NULL )
1649 crl = crl->next;
1650
1651 /*
1652 * Add new CRL on the end of the chain if needed.
1653 */
1654 if ( crl->version != 0 && crl->next == NULL)
1655 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001656 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657
Paul Bakker7d06ad22009-05-02 15:53:56 +00001658 if( crl->next == NULL )
1659 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001661 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001662 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001663
Paul Bakker7d06ad22009-05-02 15:53:56 +00001664 crl = crl->next;
1665 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666 }
1667
Paul Bakker96743fc2011-02-12 14:30:57 +00001668#if defined(POLARSSL_PEM_C)
1669 pem_init( &pem );
1670 ret = pem_read_buffer( &pem,
1671 "-----BEGIN X509 CRL-----",
1672 "-----END X509 CRL-----",
1673 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001674
Paul Bakker96743fc2011-02-12 14:30:57 +00001675 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001676 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001677 /*
1678 * Was PEM encoded
1679 */
1680 buflen -= use_len;
1681 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001682
1683 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001684 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001685 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001686 p = pem.buf;
1687 pem.buf = NULL;
1688 len = pem.buflen;
1689 pem_free( &pem );
1690 }
Paul Bakker00b28602013-06-24 13:02:41 +02001691 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001692 {
1693 pem_free( &pem );
1694 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 }
1696 else
1697 {
1698 /*
1699 * nope, copy the raw DER data
1700 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001701 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001702
1703 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001704 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705
1706 memcpy( p, buf, buflen );
1707
1708 buflen = 0;
1709 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001710#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001711 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001712
1713 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001714 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001715
1716 memcpy( p, buf, buflen );
1717
1718 buflen = 0;
1719#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720
1721 crl->raw.p = p;
1722 crl->raw.len = len;
1723 end = p + len;
1724
1725 /*
1726 * CertificateList ::= SEQUENCE {
1727 * tbsCertList TBSCertList,
1728 * signatureAlgorithm AlgorithmIdentifier,
1729 * signatureValue BIT STRING }
1730 */
1731 if( ( ret = asn1_get_tag( &p, end, &len,
1732 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1733 {
1734 x509_crl_free( crl );
1735 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1736 }
1737
Paul Bakker23986e52011-04-24 08:57:21 +00001738 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739 {
1740 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001741 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001742 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1743 }
1744
1745 /*
1746 * TBSCertList ::= SEQUENCE {
1747 */
1748 crl->tbs.p = p;
1749
1750 if( ( ret = asn1_get_tag( &p, end, &len,
1751 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1752 {
1753 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001754 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 }
1756
1757 end = p + len;
1758 crl->tbs.len = end - crl->tbs.p;
1759
1760 /*
1761 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1762 * -- if present, MUST be v2
1763 *
1764 * signature AlgorithmIdentifier
1765 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001766 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001767 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001768 {
1769 x509_crl_free( crl );
1770 return( ret );
1771 }
1772
1773 crl->version++;
1774
1775 if( crl->version > 2 )
1776 {
1777 x509_crl_free( crl );
1778 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1779 }
1780
Paul Bakkerc70b9822013-04-07 22:00:46 +02001781 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1782 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783 {
1784 x509_crl_free( crl );
1785 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1786 }
1787
1788 /*
1789 * issuer Name
1790 */
1791 crl->issuer_raw.p = p;
1792
1793 if( ( ret = asn1_get_tag( &p, end, &len,
1794 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1795 {
1796 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001797 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001798 }
1799
1800 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1801 {
1802 x509_crl_free( crl );
1803 return( ret );
1804 }
1805
1806 crl->issuer_raw.len = p - crl->issuer_raw.p;
1807
1808 /*
1809 * thisUpdate Time
1810 * nextUpdate Time OPTIONAL
1811 */
Paul Bakker91200182010-02-18 21:26:15 +00001812 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813 {
1814 x509_crl_free( crl );
1815 return( ret );
1816 }
1817
Paul Bakker91200182010-02-18 21:26:15 +00001818 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001819 {
Paul Bakker9d781402011-05-09 16:17:09 +00001820 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001821 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001822 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001823 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001824 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 x509_crl_free( crl );
1826 return( ret );
1827 }
1828 }
1829
1830 /*
1831 * revokedCertificates SEQUENCE OF SEQUENCE {
1832 * userCertificate CertificateSerialNumber,
1833 * revocationDate Time,
1834 * crlEntryExtensions Extensions OPTIONAL
1835 * -- if present, MUST be v2
1836 * } OPTIONAL
1837 */
1838 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1839 {
1840 x509_crl_free( crl );
1841 return( ret );
1842 }
1843
1844 /*
1845 * crlExtensions EXPLICIT Extensions OPTIONAL
1846 * -- if present, MUST be v2
1847 */
1848 if( crl->version == 2 )
1849 {
1850 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1851
1852 if( ret != 0 )
1853 {
1854 x509_crl_free( crl );
1855 return( ret );
1856 }
1857 }
1858
1859 if( p != end )
1860 {
1861 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001862 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001863 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1864 }
1865
1866 end = crl->raw.p + crl->raw.len;
1867
1868 /*
1869 * signatureAlgorithm AlgorithmIdentifier,
1870 * signatureValue BIT STRING
1871 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001872 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001873 {
1874 x509_crl_free( crl );
1875 return( ret );
1876 }
1877
Paul Bakker535e97d2012-08-23 10:49:55 +00001878 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1879 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 {
1881 x509_crl_free( crl );
1882 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1883 }
1884
1885 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1886 {
1887 x509_crl_free( crl );
1888 return( ret );
1889 }
1890
1891 if( p != end )
1892 {
1893 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001894 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001895 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1896 }
1897
1898 if( buflen > 0 )
1899 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001900 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001901
Paul Bakker7d06ad22009-05-02 15:53:56 +00001902 if( crl->next == NULL )
1903 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001905 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001906 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001907
Paul Bakker7d06ad22009-05-02 15:53:56 +00001908 crl = crl->next;
1909 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001910
1911 return( x509parse_crl( crl, buf, buflen ) );
1912 }
1913
1914 return( 0 );
1915}
1916
Paul Bakker335db3f2011-04-25 15:28:35 +00001917#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001918/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001919 * Load all data from a file into a given buffer.
1920 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001921static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001922{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001923 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001924 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001925
Paul Bakkerd98030e2009-05-02 15:13:40 +00001926 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001927 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001928
Paul Bakkerd98030e2009-05-02 15:13:40 +00001929 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001930 if( ( size = ftell( f ) ) == -1 )
1931 {
1932 fclose( f );
1933 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1934 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001936
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001937 *n = (size_t) size;
1938
Paul Bakker694d3ae2013-08-19 14:23:38 +02001939 if( *n + 1 == 0 ||
1940 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001941 {
1942 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001943 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001944 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001945
Paul Bakkerd98030e2009-05-02 15:13:40 +00001946 if( fread( *buf, 1, *n, f ) != *n )
1947 {
1948 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001949 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001950 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001951 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001952
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001954
Paul Bakkerd98030e2009-05-02 15:13:40 +00001955 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001956
Paul Bakkerd98030e2009-05-02 15:13:40 +00001957 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001958}
1959
1960/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 * Load one or more certificates and add them to the chained list
1962 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001963int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001964{
1965 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 size_t n;
1967 unsigned char *buf;
1968
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001969 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001970 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Paul Bakker69e095c2011-12-10 21:55:01 +00001972 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
1974 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001975 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001976
1977 return( ret );
1978}
1979
Paul Bakker8d914582012-06-04 12:46:42 +00001980int x509parse_crtpath( x509_cert *chain, const char *path )
1981{
1982 int ret = 0;
1983#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001984 int w_ret;
1985 WCHAR szDir[MAX_PATH];
1986 char filename[MAX_PATH];
1987 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001988 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001989
Paul Bakker97872ac2012-11-02 12:53:26 +00001990 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001991 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001992
1993 if( len > MAX_PATH - 3 )
1994 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001995
Paul Bakker3338b792012-10-01 21:13:10 +00001996 memset( szDir, 0, sizeof(szDir) );
1997 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001998 memcpy( filename, path, len );
1999 filename[len++] = '\\';
2000 p = filename + len;
2001 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002002
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002003 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002004
Paul Bakker97872ac2012-11-02 12:53:26 +00002005 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002006 if (hFind == INVALID_HANDLE_VALUE)
2007 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2008
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002009 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002010 do
2011 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002012 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002013
Paul Bakkere4791f32012-06-04 21:29:15 +00002014 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002015 continue;
2016
Paul Bakker3338b792012-10-01 21:13:10 +00002017 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2018 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002019 p, len - 1,
2020 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002021
Paul Bakker3338b792012-10-01 21:13:10 +00002022 w_ret = x509parse_crtfile( chain, filename );
2023 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002024 ret++;
2025 else
2026 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002027 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002028 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002029
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002030 if (GetLastError() != ERROR_NO_MORE_FILES)
2031 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002032
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002033cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002034 FindClose( hFind );
2035#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002036 int t_ret, i;
2037 struct stat sb;
2038 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002039 char entry_name[255];
2040 DIR *dir = opendir( path );
2041
2042 if( dir == NULL)
2043 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2044
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002045 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002046 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002047 if( result == NULL )
2048 break;
2049
2050 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2051
2052 i = stat( entry_name, &sb );
2053
2054 if( i == -1 )
2055 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2056
2057 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002058 continue;
2059
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002060 // Ignore parse errors
2061 //
Paul Bakker8d914582012-06-04 12:46:42 +00002062 t_ret = x509parse_crtfile( chain, entry_name );
2063 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002064 ret++;
2065 else
2066 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002067 }
2068 closedir( dir );
2069#endif
2070
2071 return( ret );
2072}
2073
Paul Bakkerd98030e2009-05-02 15:13:40 +00002074/*
2075 * Load one or more CRLs and add them to the chained list
2076 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002077int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002078{
2079 int ret;
2080 size_t n;
2081 unsigned char *buf;
2082
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002083 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002084 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002085
Paul Bakker27fdf462011-06-09 13:55:13 +00002086 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002087
2088 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002089 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002090
2091 return( ret );
2092}
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002095 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002096 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002097int x509parse_keyfile( pk_context *ctx,
2098 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002099{
2100 int ret;
2101 size_t n;
2102 unsigned char *buf;
2103
2104 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2105 return( ret );
2106
2107 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002108 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002109 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002110 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002111 (const unsigned char *) pwd, strlen( pwd ) );
2112
2113 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002114 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002115
2116 return( ret );
2117}
2118
2119/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002120 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002121 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002122int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002123{
2124 int ret;
2125 size_t n;
2126 unsigned char *buf;
2127
2128 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2129 return( ret );
2130
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002131 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002132
2133 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002134 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002135
2136 return( ret );
2137}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002138
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002139#if defined(POLARSSL_RSA_C)
2140/*
2141 * Load and parse a private RSA key
2142 */
2143int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2144{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002145 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002146 pk_context pk;
2147
2148 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002149
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002150 ret = x509parse_keyfile( &pk, path, pwd );
2151
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002152 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2153 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2154
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002155 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002156 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002157 else
2158 rsa_free( rsa );
2159
2160 pk_free( &pk );
2161
2162 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002163}
2164
2165/*
2166 * Load and parse a public RSA key
2167 */
2168int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2169{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002170 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002171 pk_context pk;
2172
2173 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002174
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002175 ret = x509parse_public_keyfile( &pk, path );
2176
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002177 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2178 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2179
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002180 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002181 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002182 else
2183 rsa_free( rsa );
2184
2185 pk_free( &pk );
2186
2187 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002188}
2189#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002190#endif /* POLARSSL_FS_IO */
2191
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002192#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002193/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002194 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002196static int x509parse_key_pkcs1_der( rsa_context *rsa,
2197 const unsigned char *key,
2198 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199{
Paul Bakker23986e52011-04-24 08:57:21 +00002200 int ret;
2201 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002203
Paul Bakker96743fc2011-02-12 14:30:57 +00002204 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002205 end = p + keylen;
2206
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002208 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002209 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 * RSAPrivateKey ::= SEQUENCE {
2211 * version Version,
2212 * modulus INTEGER, -- n
2213 * publicExponent INTEGER, -- e
2214 * privateExponent INTEGER, -- d
2215 * prime1 INTEGER, -- p
2216 * prime2 INTEGER, -- q
2217 * exponent1 INTEGER, -- d mod (p-1)
2218 * exponent2 INTEGER, -- d mod (q-1)
2219 * coefficient INTEGER, -- (inverse of q) mod p
2220 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2221 * }
2222 */
2223 if( ( ret = asn1_get_tag( &p, end, &len,
2224 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2225 {
Paul Bakker9d781402011-05-09 16:17:09 +00002226 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 }
2228
2229 end = p + len;
2230
2231 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2232 {
Paul Bakker9d781402011-05-09 16:17:09 +00002233 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
2235
2236 if( rsa->ver != 0 )
2237 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002238 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
2241 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2242 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2243 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2244 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2245 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2246 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2247 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2248 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2249 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
2254 rsa->len = mpi_size( &rsa->N );
2255
2256 if( p != end )
2257 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002259 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002260 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 }
2262
2263 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2264 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 rsa_free( rsa );
2266 return( ret );
2267 }
2268
Paul Bakkere2f50402013-06-24 19:00:59 +02002269 return( 0 );
2270}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002271#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002272
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002273#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002274/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002275 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002276 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002277static int x509parse_key_sec1_der( ecp_keypair *eck,
2278 const unsigned char *key,
2279 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002280{
2281 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002282 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002283 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002284 x509_buf params;
2285 unsigned char *p = (unsigned char *) key;
2286 unsigned char *end = p + keylen;
2287 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002288
2289 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002290 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002291 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002292 * ECPrivateKey ::= SEQUENCE {
2293 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2294 * privateKey OCTET STRING,
2295 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2296 * publicKey [1] BIT STRING OPTIONAL
2297 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002298 */
2299 if( ( ret = asn1_get_tag( &p, end, &len,
2300 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2301 {
2302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2303 }
2304
2305 end = p + len;
2306
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002307 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002308 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2309
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002310 if( version != 1 )
2311 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002312
Paul Bakkere2f50402013-06-24 19:00:59 +02002313 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2314 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2315
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002316 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002317 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002318 ecp_keypair_free( eck );
2319 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2320 }
2321
2322 p += len;
2323
2324 /*
2325 * Is 'parameters' present?
2326 */
2327 if( ( ret = asn1_get_tag( &p, end, &len,
2328 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2329 {
2330 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2331 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2332 {
2333 ecp_keypair_free( eck );
2334 return( ret );
2335 }
2336 }
2337 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2338 {
2339 ecp_keypair_free( eck );
2340 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2341 }
2342
2343 /*
2344 * Is 'publickey' present?
2345 */
2346 if( ( ret = asn1_get_tag( &p, end, &len,
2347 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2348 {
2349 end2 = p + len;
2350
2351 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2352 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2353
2354 if( p + len != end2 )
2355 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2356 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2357
2358 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2359 return( ret );
2360 }
2361 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2362 {
2363 ecp_keypair_free( eck );
2364 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2365 }
2366
2367 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2368 {
2369 ecp_keypair_free( eck );
2370 return( ret );
2371 }
2372
2373 return 0;
2374}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002375#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002376
2377/*
2378 * Parse an unencrypted PKCS#8 encoded private key
2379 */
2380static int x509parse_key_pkcs8_unencrypted_der(
2381 pk_context *pk,
2382 const unsigned char* key,
2383 size_t keylen )
2384{
2385 int ret, version;
2386 size_t len;
2387 x509_buf params;
2388 unsigned char *p = (unsigned char *) key;
2389 unsigned char *end = p + keylen;
2390 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002391 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002392
2393 /*
2394 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2395 *
2396 * PrivateKeyInfo ::= SEQUENCE {
2397 * version Version,
2398 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2399 * privateKey PrivateKey,
2400 * attributes [0] IMPLICIT Attributes OPTIONAL }
2401 *
2402 * Version ::= INTEGER
2403 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2404 * PrivateKey ::= OCTET STRING
2405 *
2406 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2407 */
2408
2409 if( ( ret = asn1_get_tag( &p, end, &len,
2410 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2411 {
2412 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002413 }
2414
2415 end = p + len;
2416
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002417 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2418 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2419
2420 if( version != 0 )
2421 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2422
2423 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2424 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2425
2426 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2427 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2428
2429 if( len < 1 )
2430 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2431 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2432
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002433 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2434 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2435
2436 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002437 return( ret );
2438
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002439#if defined(POLARSSL_RSA_C)
2440 if( pk_alg == POLARSSL_PK_RSA )
2441 {
2442 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2443 {
2444 pk_free( pk );
2445 return( ret );
2446 }
2447 } else
2448#endif /* POLARSSL_RSA_C */
2449#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002450 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2451 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002452 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2453 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2454 {
2455 pk_free( pk );
2456 return( ret );
2457 }
2458 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002459#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002460 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2461
2462 return 0;
2463}
2464
2465/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002466 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002467 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002468static int x509parse_key_pkcs8_encrypted_der(
2469 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002470 const unsigned char *key, size_t keylen,
2471 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002472{
2473 int ret;
2474 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002475 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002476 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002477 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002478#if defined(POLARSSL_PKCS12_C)
2479 cipher_type_t cipher_alg;
2480 md_type_t md_alg;
2481#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002482
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002483 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002484
2485 p = (unsigned char *) key;
2486 end = p + keylen;
2487
Paul Bakker28144de2013-06-24 19:28:55 +02002488 if( pwdlen == 0 )
2489 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2490
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491 /*
2492 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2493 *
2494 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2495 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2496 * encryptedData EncryptedData
2497 * }
2498 *
2499 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2500 *
2501 * EncryptedData ::= OCTET STRING
2502 *
2503 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2504 */
2505 if( ( ret = asn1_get_tag( &p, end, &len,
2506 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2507 {
2508 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2509 }
2510
2511 end = p + len;
2512
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002513 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002514 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002515
2516 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2517 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2518
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002519 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002520 return( POLARSSL_ERR_X509_INVALID_INPUT );
2521
2522 /*
2523 * Decrypt EncryptedData with appropriate PDE
2524 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002525#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002526 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002527 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002528 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002529 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002530 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002531 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002532 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2533 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2534
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002535 return( ret );
2536 }
2537 }
2538 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2539 {
2540 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2541 PKCS12_PBE_DECRYPT,
2542 pwd, pwdlen,
2543 p, len, buf ) ) != 0 )
2544 {
2545 return( ret );
2546 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002547
2548 // Best guess for password mismatch when using RC4. If first tag is
2549 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2550 //
2551 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2552 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002553 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002554 else
2555#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002556#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002557 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002558 {
2559 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2560 p, len, buf ) ) != 0 )
2561 {
2562 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2563 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2564
2565 return( ret );
2566 }
2567 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002568 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002569#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002570 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2571
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002572 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002573}
2574
2575/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002576 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002577 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002578int x509parse_key( pk_context *pk,
2579 const unsigned char *key, size_t keylen,
2580 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002581{
2582 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002583 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002584
2585#if defined(POLARSSL_PEM_C)
2586 size_t len;
2587 pem_context pem;
2588
2589 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002590
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002591#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002592 ret = pem_read_buffer( &pem,
2593 "-----BEGIN RSA PRIVATE KEY-----",
2594 "-----END RSA PRIVATE KEY-----",
2595 key, pwd, pwdlen, &len );
2596 if( ret == 0 )
2597 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002598 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2599 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2600
2601 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002602 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2603 pem.buf, pem.buflen ) ) != 0 )
2604 {
2605 pk_free( pk );
2606 }
2607
2608 pem_free( &pem );
2609 return( ret );
2610 }
2611 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2612 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2613 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2614 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2615 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2616 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002617#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002618
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002619#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002620 ret = pem_read_buffer( &pem,
2621 "-----BEGIN EC PRIVATE KEY-----",
2622 "-----END EC PRIVATE KEY-----",
2623 key, pwd, pwdlen, &len );
2624 if( ret == 0 )
2625 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002626 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2627 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2628
2629 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002630 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2631 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002632 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002633 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002634 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002635
2636 pem_free( &pem );
2637 return( ret );
2638 }
2639 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2640 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2641 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2642 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2643 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2644 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002645#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002646
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002647 ret = pem_read_buffer( &pem,
2648 "-----BEGIN PRIVATE KEY-----",
2649 "-----END PRIVATE KEY-----",
2650 key, NULL, 0, &len );
2651 if( ret == 0 )
2652 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002653 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002654 pem.buf, pem.buflen ) ) != 0 )
2655 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002656 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002657 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002658
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002659 pem_free( &pem );
2660 return( ret );
2661 }
2662 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2663 return( ret );
2664
2665 ret = pem_read_buffer( &pem,
2666 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2667 "-----END ENCRYPTED PRIVATE KEY-----",
2668 key, NULL, 0, &len );
2669 if( ret == 0 )
2670 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002671 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002672 pem.buf, pem.buflen,
2673 pwd, pwdlen ) ) != 0 )
2674 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002675 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002676 }
2677
2678 pem_free( &pem );
2679 return( ret );
2680 }
2681 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2682 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002683#else
2684 ((void) pwd);
2685 ((void) pwdlen);
2686#endif /* POLARSSL_PEM_C */
2687
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002688 /*
2689 * At this point we only know it's not a PEM formatted key. Could be any
2690 * of the known DER encoded private key formats
2691 *
2692 * We try the different DER format parsers to see if one passes without
2693 * error
2694 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002695 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2696 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002697 {
2698 return( 0 );
2699 }
2700
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002701 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002702
2703 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2704 {
2705 return( ret );
2706 }
2707
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002708 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002709 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002710
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002711 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002712
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002713#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002714 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2715 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2716
2717 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002718 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2719 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002720 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002721 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002722
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002723 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002724#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002725
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002726#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002727 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2728 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2729
2730 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002731 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2732 {
2733 return( 0 );
2734 }
2735
2736 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002737#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002738
2739 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2740}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002741
2742/*
2743 * Parse a public key
2744 */
2745int x509parse_public_key( pk_context *ctx,
2746 const unsigned char *key, size_t keylen )
2747{
2748 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002749 unsigned char *p;
2750#if defined(POLARSSL_PEM_C)
2751 size_t len;
2752 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002753
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002754 pem_init( &pem );
2755 ret = pem_read_buffer( &pem,
2756 "-----BEGIN PUBLIC KEY-----",
2757 "-----END PUBLIC KEY-----",
2758 key, NULL, 0, &len );
2759
2760 if( ret == 0 )
2761 {
2762 /*
2763 * Was PEM encoded
2764 */
2765 key = pem.buf;
2766 keylen = pem.buflen;
2767 }
2768 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2769 {
2770 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002771 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002772 }
2773#endif
2774 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002775
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002776 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002777
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002778#if defined(POLARSSL_PEM_C)
2779 pem_free( &pem );
2780#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002781
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002782 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002783}
2784
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002785#if defined(POLARSSL_RSA_C)
2786/*
2787 * Parse a private RSA key
2788 */
2789int x509parse_key_rsa( rsa_context *rsa,
2790 const unsigned char *key, size_t keylen,
2791 const unsigned char *pwd, size_t pwdlen )
2792{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002793 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002794 pk_context pk;
2795
2796 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002797
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002798 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
2799
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002800 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2801 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2802
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002803 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002804 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002805 else
2806 rsa_free( rsa );
2807
2808 pk_free( &pk );
2809
2810 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002811}
2812
2813/*
2814 * Parse a public RSA key
2815 */
2816int x509parse_public_key_rsa( rsa_context *rsa,
2817 const unsigned char *key, size_t keylen )
2818{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002819 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002820 pk_context pk;
2821
2822 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002823
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002824 ret = x509parse_public_key( &pk, key, keylen );
2825
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002826 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2827 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2828
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002829 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002830 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002831 else
2832 rsa_free( rsa );
2833
2834 pk_free( &pk );
2835
2836 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002837}
2838#endif /* POLARSSL_RSA_C */
2839
Paul Bakkereaa89f82011-04-04 21:36:15 +00002840#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002841/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002842 * Parse DHM parameters
2843 */
Paul Bakker23986e52011-04-24 08:57:21 +00002844int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002845{
Paul Bakker23986e52011-04-24 08:57:21 +00002846 int ret;
2847 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002848 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002849#if defined(POLARSSL_PEM_C)
2850 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002851
Paul Bakker96743fc2011-02-12 14:30:57 +00002852 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002853
Paul Bakker96743fc2011-02-12 14:30:57 +00002854 ret = pem_read_buffer( &pem,
2855 "-----BEGIN DH PARAMETERS-----",
2856 "-----END DH PARAMETERS-----",
2857 dhmin, NULL, 0, &dhminlen );
2858
2859 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002860 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002861 /*
2862 * Was PEM encoded
2863 */
2864 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002865 }
Paul Bakker00b28602013-06-24 13:02:41 +02002866 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002867 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002868 pem_free( &pem );
2869 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002870 }
2871
Paul Bakker96743fc2011-02-12 14:30:57 +00002872 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2873#else
2874 p = (unsigned char *) dhmin;
2875#endif
2876 end = p + dhminlen;
2877
Paul Bakker1b57b062011-01-06 15:48:19 +00002878 memset( dhm, 0, sizeof( dhm_context ) );
2879
Paul Bakker1b57b062011-01-06 15:48:19 +00002880 /*
2881 * DHParams ::= SEQUENCE {
2882 * prime INTEGER, -- P
2883 * generator INTEGER, -- g
2884 * }
2885 */
2886 if( ( ret = asn1_get_tag( &p, end, &len,
2887 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2888 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002889#if defined(POLARSSL_PEM_C)
2890 pem_free( &pem );
2891#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002892 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002893 }
2894
2895 end = p + len;
2896
2897 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2898 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2899 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002900#if defined(POLARSSL_PEM_C)
2901 pem_free( &pem );
2902#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002903 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002904 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002905 }
2906
2907 if( p != end )
2908 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002909#if defined(POLARSSL_PEM_C)
2910 pem_free( &pem );
2911#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002912 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002913 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002914 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2915 }
2916
Paul Bakker96743fc2011-02-12 14:30:57 +00002917#if defined(POLARSSL_PEM_C)
2918 pem_free( &pem );
2919#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002920
2921 return( 0 );
2922}
2923
Paul Bakker335db3f2011-04-25 15:28:35 +00002924#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002925/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002926 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002927 */
2928int x509parse_dhmfile( dhm_context *dhm, const char *path )
2929{
2930 int ret;
2931 size_t n;
2932 unsigned char *buf;
2933
Paul Bakker69e095c2011-12-10 21:55:01 +00002934 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2935 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002936
Paul Bakker27fdf462011-06-09 13:55:13 +00002937 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002938
2939 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002940 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002941
2942 return( ret );
2943}
Paul Bakker335db3f2011-04-25 15:28:35 +00002944#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002945#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002946
Paul Bakker5121ce52009-01-03 21:22:43 +00002947#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002948#include <stdarg.h>
2949
2950#if !defined vsnprintf
2951#define vsnprintf _vsnprintf
2952#endif // vsnprintf
2953
2954/*
2955 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2956 * Result value is not size of buffer needed, but -1 if no fit is possible.
2957 *
2958 * This fuction tries to 'fix' this by at least suggesting enlarging the
2959 * size by 20.
2960 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002961static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002962{
2963 va_list ap;
2964 int res = -1;
2965
2966 va_start( ap, format );
2967
2968 res = vsnprintf( str, size, format, ap );
2969
2970 va_end( ap );
2971
2972 // No quick fix possible
2973 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002974 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002975
2976 return res;
2977}
2978
2979#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002980#endif
2981
Paul Bakkerd98030e2009-05-02 15:13:40 +00002982#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2983
2984#define SAFE_SNPRINTF() \
2985{ \
2986 if( ret == -1 ) \
2987 return( -1 ); \
2988 \
Paul Bakker23986e52011-04-24 08:57:21 +00002989 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002990 p[n - 1] = '\0'; \
2991 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2992 } \
2993 \
Paul Bakker23986e52011-04-24 08:57:21 +00002994 n -= (unsigned int) ret; \
2995 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002996}
2997
Paul Bakker5121ce52009-01-03 21:22:43 +00002998/*
2999 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003000 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003002int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003003{
Paul Bakker23986e52011-04-24 08:57:21 +00003004 int ret;
3005 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003007 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003008 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 char s[128], *p;
3010
3011 memset( s, 0, sizeof( s ) );
3012
3013 name = dn;
3014 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003015 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
3017 while( name != NULL )
3018 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003019 if( !name->oid.p )
3020 {
3021 name = name->next;
3022 continue;
3023 }
3024
Paul Bakker74111d32011-01-15 16:57:55 +00003025 if( name != dn )
3026 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003027 ret = snprintf( p, n, ", " );
3028 SAFE_SNPRINTF();
3029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Paul Bakkerc70b9822013-04-07 22:00:46 +02003031 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakkerc70b9822013-04-07 22:00:46 +02003033 if( ret == 0 )
3034 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003037 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
3039 for( i = 0; i < name->val.len; i++ )
3040 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003041 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 break;
3043
3044 c = name->val.p[i];
3045 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3046 s[i] = '?';
3047 else s[i] = c;
3048 }
3049 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003050 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003051 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 name = name->next;
3053 }
3054
Paul Bakker23986e52011-04-24 08:57:21 +00003055 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056}
3057
3058/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003059 * Store the serial in printable form into buf; no more
3060 * than size characters will be written
3061 */
3062int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3063{
Paul Bakker23986e52011-04-24 08:57:21 +00003064 int ret;
3065 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003066 char *p;
3067
3068 p = buf;
3069 n = size;
3070
3071 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003072 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003073
3074 for( i = 0; i < nr; i++ )
3075 {
Paul Bakker93048802011-12-05 14:38:06 +00003076 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003077 continue;
3078
Paul Bakkerdd476992011-01-16 21:34:59 +00003079 ret = snprintf( p, n, "%02X%s",
3080 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3081 SAFE_SNPRINTF();
3082 }
3083
Paul Bakker03c7c252011-11-25 12:37:37 +00003084 if( nr != serial->len )
3085 {
3086 ret = snprintf( p, n, "...." );
3087 SAFE_SNPRINTF();
3088 }
3089
Paul Bakker23986e52011-04-24 08:57:21 +00003090 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003091}
3092
3093/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003094 * Helper for writing "RSA key size", "EC key size", etc
3095 */
3096static int x509_key_size_helper( char *buf, size_t size, const char *name )
3097{
3098 char *p = buf;
3099 size_t n = size;
3100 int ret;
3101
3102 if( strlen( name ) + sizeof( " key size" ) > size )
3103 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3104
3105 ret = snprintf( p, n, "%s key size", name );
3106 SAFE_SNPRINTF();
3107
3108 return( 0 );
3109}
3110
3111/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003112 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003114#define BEFORE_COLON 14
3115#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003116int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3117 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003118{
Paul Bakker23986e52011-04-24 08:57:21 +00003119 int ret;
3120 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003121 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003122 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003123 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
3125 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003126 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003127
Paul Bakkerd98030e2009-05-02 15:13:40 +00003128 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003130 SAFE_SNPRINTF();
3131 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003133 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Paul Bakkerdd476992011-01-16 21:34:59 +00003135 ret = x509parse_serial_gets( p, n, &crt->serial);
3136 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
Paul Bakkerd98030e2009-05-02 15:13:40 +00003138 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3139 SAFE_SNPRINTF();
3140 ret = x509parse_dn_gets( p, n, &crt->issuer );
3141 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Paul Bakkerd98030e2009-05-02 15:13:40 +00003143 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3144 SAFE_SNPRINTF();
3145 ret = x509parse_dn_gets( p, n, &crt->subject );
3146 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Paul Bakkerd98030e2009-05-02 15:13:40 +00003148 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003149 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3150 crt->valid_from.year, crt->valid_from.mon,
3151 crt->valid_from.day, crt->valid_from.hour,
3152 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003153 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakkerd98030e2009-05-02 15:13:40 +00003155 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3157 crt->valid_to.year, crt->valid_to.mon,
3158 crt->valid_to.day, crt->valid_to.hour,
3159 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003160 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Paul Bakkerc70b9822013-04-07 22:00:46 +02003162 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003163 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakkerc70b9822013-04-07 22:00:46 +02003165 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3166 if( ret != 0 )
3167 ret = snprintf( p, n, "???" );
3168 else
3169 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170 SAFE_SNPRINTF();
3171
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003172 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003173 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003174 {
3175 return( ret );
3176 }
3177
3178 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003179 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003180 SAFE_SNPRINTF();
3181
Paul Bakker23986e52011-04-24 08:57:21 +00003182 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003183}
3184
Paul Bakker74111d32011-01-15 16:57:55 +00003185/*
3186 * Return an informational string describing the given OID
3187 */
3188const char *x509_oid_get_description( x509_buf *oid )
3189{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003190 const char *desc = NULL;
3191 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003192
Paul Bakkerc70b9822013-04-07 22:00:46 +02003193 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003194
Paul Bakkerc70b9822013-04-07 22:00:46 +02003195 if( ret != 0 )
3196 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003197
Paul Bakkerc70b9822013-04-07 22:00:46 +02003198 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003199}
3200
3201/* Return the x.y.z.... style numeric string for the given OID */
3202int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3203{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003204 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003205}
3206
Paul Bakkerd98030e2009-05-02 15:13:40 +00003207/*
3208 * Return an informational string about the CRL.
3209 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003210int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3211 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003212{
Paul Bakker23986e52011-04-24 08:57:21 +00003213 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003214 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003215 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003216 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003217 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003218
3219 p = buf;
3220 n = size;
3221
3222 ret = snprintf( p, n, "%sCRL version : %d",
3223 prefix, crl->version );
3224 SAFE_SNPRINTF();
3225
3226 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3227 SAFE_SNPRINTF();
3228 ret = x509parse_dn_gets( p, n, &crl->issuer );
3229 SAFE_SNPRINTF();
3230
3231 ret = snprintf( p, n, "\n%sthis update : " \
3232 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3233 crl->this_update.year, crl->this_update.mon,
3234 crl->this_update.day, crl->this_update.hour,
3235 crl->this_update.min, crl->this_update.sec );
3236 SAFE_SNPRINTF();
3237
3238 ret = snprintf( p, n, "\n%snext update : " \
3239 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3240 crl->next_update.year, crl->next_update.mon,
3241 crl->next_update.day, crl->next_update.hour,
3242 crl->next_update.min, crl->next_update.sec );
3243 SAFE_SNPRINTF();
3244
3245 entry = &crl->entry;
3246
3247 ret = snprintf( p, n, "\n%sRevoked certificates:",
3248 prefix );
3249 SAFE_SNPRINTF();
3250
Paul Bakker9be19372009-07-27 20:21:53 +00003251 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003252 {
3253 ret = snprintf( p, n, "\n%sserial number: ",
3254 prefix );
3255 SAFE_SNPRINTF();
3256
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003257 ret = x509parse_serial_gets( p, n, &entry->serial);
3258 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003259
Paul Bakkerd98030e2009-05-02 15:13:40 +00003260 ret = snprintf( p, n, " revocation date: " \
3261 "%04d-%02d-%02d %02d:%02d:%02d",
3262 entry->revocation_date.year, entry->revocation_date.mon,
3263 entry->revocation_date.day, entry->revocation_date.hour,
3264 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003265 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003266
3267 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 }
3269
Paul Bakkerc70b9822013-04-07 22:00:46 +02003270 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003271 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Paul Bakkerc70b9822013-04-07 22:00:46 +02003273 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3274 if( ret != 0 )
3275 ret = snprintf( p, n, "???" );
3276 else
3277 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003278 SAFE_SNPRINTF();
3279
Paul Bakker1e27bb22009-07-19 20:25:25 +00003280 ret = snprintf( p, n, "\n" );
3281 SAFE_SNPRINTF();
3282
Paul Bakker23986e52011-04-24 08:57:21 +00003283 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284}
3285
3286/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003287 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003289#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003290int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291{
Paul Bakkercce9d772011-11-18 14:26:47 +00003292 int year, mon, day;
3293 int hour, min, sec;
3294
3295#if defined(_WIN32)
3296 SYSTEMTIME st;
3297
3298 GetLocalTime(&st);
3299
3300 year = st.wYear;
3301 mon = st.wMonth;
3302 day = st.wDay;
3303 hour = st.wHour;
3304 min = st.wMinute;
3305 sec = st.wSecond;
3306#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 struct tm *lt;
3308 time_t tt;
3309
3310 tt = time( NULL );
3311 lt = localtime( &tt );
3312
Paul Bakkercce9d772011-11-18 14:26:47 +00003313 year = lt->tm_year + 1900;
3314 mon = lt->tm_mon + 1;
3315 day = lt->tm_mday;
3316 hour = lt->tm_hour;
3317 min = lt->tm_min;
3318 sec = lt->tm_sec;
3319#endif
3320
3321 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003322 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003323
Paul Bakkercce9d772011-11-18 14:26:47 +00003324 if( year == to->year &&
3325 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003326 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003327
Paul Bakkercce9d772011-11-18 14:26:47 +00003328 if( year == to->year &&
3329 mon == to->mon &&
3330 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003331 return( 1 );
3332
Paul Bakkercce9d772011-11-18 14:26:47 +00003333 if( year == to->year &&
3334 mon == to->mon &&
3335 day == to->day &&
3336 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003337 return( 1 );
3338
Paul Bakkercce9d772011-11-18 14:26:47 +00003339 if( year == to->year &&
3340 mon == to->mon &&
3341 day == to->day &&
3342 hour == to->hour &&
3343 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003344 return( 1 );
3345
Paul Bakkercce9d772011-11-18 14:26:47 +00003346 if( year == to->year &&
3347 mon == to->mon &&
3348 day == to->day &&
3349 hour == to->hour &&
3350 min == to->min &&
3351 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003352 return( 1 );
3353
Paul Bakker40ea7de2009-05-03 10:18:48 +00003354 return( 0 );
3355}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003356#else /* POLARSSL_HAVE_TIME */
3357int x509parse_time_expired( const x509_time *to )
3358{
3359 ((void) to);
3360 return( 0 );
3361}
3362#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003363
3364/*
3365 * Return 1 if the certificate is revoked, or 0 otherwise.
3366 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003367int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003368{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003369 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003370
3371 while( cur != NULL && cur->serial.len != 0 )
3372 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003373 if( crt->serial.len == cur->serial.len &&
3374 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003375 {
3376 if( x509parse_time_expired( &cur->revocation_date ) )
3377 return( 1 );
3378 }
3379
3380 cur = cur->next;
3381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
3383 return( 0 );
3384}
3385
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003386/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003387 * Check that the given certificate is valid accoring to the CRL.
3388 */
3389static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3390 x509_crl *crl_list)
3391{
3392 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003393 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3394 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003395
Paul Bakker915275b2012-09-28 07:10:55 +00003396 if( ca == NULL )
3397 return( flags );
3398
Paul Bakker76fd75a2011-01-16 21:12:10 +00003399 /*
3400 * TODO: What happens if no CRL is present?
3401 * Suggestion: Revocation state should be unknown if no CRL is present.
3402 * For backwards compatibility this is not yet implemented.
3403 */
3404
Paul Bakker915275b2012-09-28 07:10:55 +00003405 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003406 {
Paul Bakker915275b2012-09-28 07:10:55 +00003407 if( crl_list->version == 0 ||
3408 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003409 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3410 crl_list->issuer_raw.len ) != 0 )
3411 {
3412 crl_list = crl_list->next;
3413 continue;
3414 }
3415
3416 /*
3417 * Check if CRL is correctly signed by the trusted CA
3418 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003419 md_info = md_info_from_type( crl_list->sig_md );
3420 if( md_info == NULL )
3421 {
3422 /*
3423 * Cannot check 'unknown' hash
3424 */
3425 flags |= BADCRL_NOT_TRUSTED;
3426 break;
3427 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003428
Paul Bakkerc70b9822013-04-07 22:00:46 +02003429 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003430
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003431 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003432 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003433 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003434 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003435 flags |= BADCRL_NOT_TRUSTED;
3436 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003437 }
3438
3439 /*
3440 * Check for validity of CRL (Do not drop out)
3441 */
3442 if( x509parse_time_expired( &crl_list->next_update ) )
3443 flags |= BADCRL_EXPIRED;
3444
3445 /*
3446 * Check if certificate is revoked
3447 */
3448 if( x509parse_revoked(crt, crl_list) )
3449 {
3450 flags |= BADCERT_REVOKED;
3451 break;
3452 }
3453
3454 crl_list = crl_list->next;
3455 }
3456 return flags;
3457}
3458
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003459static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003460{
3461 size_t i;
3462 size_t cn_idx = 0;
3463
Paul Bakker57b12982012-02-11 17:38:38 +00003464 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003465 return( 0 );
3466
3467 for( i = 0; i < strlen( cn ); ++i )
3468 {
3469 if( cn[i] == '.' )
3470 {
3471 cn_idx = i;
3472 break;
3473 }
3474 }
3475
3476 if( cn_idx == 0 )
3477 return( 0 );
3478
Paul Bakker535e97d2012-08-23 10:49:55 +00003479 if( strlen( cn ) - cn_idx == name->len - 1 &&
3480 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003481 {
3482 return( 1 );
3483 }
3484
3485 return( 0 );
3486}
3487
Paul Bakker915275b2012-09-28 07:10:55 +00003488static int x509parse_verify_top(
3489 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003490 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003491 int (*f_vrfy)(void *, x509_cert *, int, int *),
3492 void *p_vrfy )
3493{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003494 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003495 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003496 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3497 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003498
3499 if( x509parse_time_expired( &child->valid_to ) )
3500 *flags |= BADCERT_EXPIRED;
3501
3502 /*
3503 * Child is the top of the chain. Check against the trust_ca list.
3504 */
3505 *flags |= BADCERT_NOT_TRUSTED;
3506
3507 while( trust_ca != NULL )
3508 {
3509 if( trust_ca->version == 0 ||
3510 child->issuer_raw.len != trust_ca->subject_raw.len ||
3511 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3512 child->issuer_raw.len ) != 0 )
3513 {
3514 trust_ca = trust_ca->next;
3515 continue;
3516 }
3517
Paul Bakker9a736322012-11-14 12:39:52 +00003518 /*
3519 * Reduce path_len to check against if top of the chain is
3520 * the same as the trusted CA
3521 */
3522 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3523 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003524 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003525 {
3526 check_path_cnt--;
3527 }
3528
Paul Bakker915275b2012-09-28 07:10:55 +00003529 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003530 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003531 {
3532 trust_ca = trust_ca->next;
3533 continue;
3534 }
3535
Paul Bakkerc70b9822013-04-07 22:00:46 +02003536 md_info = md_info_from_type( child->sig_md );
3537 if( md_info == NULL )
3538 {
3539 /*
3540 * Cannot check 'unknown' hash
3541 */
Paul Bakker3a074a72013-08-20 12:45:03 +02003542 trust_ca = trust_ca->next;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003543 continue;
3544 }
Paul Bakker915275b2012-09-28 07:10:55 +00003545
Paul Bakkerc70b9822013-04-07 22:00:46 +02003546 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003547
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003548 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003549 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003550 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003551 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003552 trust_ca = trust_ca->next;
3553 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003554 }
3555
3556 /*
3557 * Top of chain is signed by a trusted CA
3558 */
3559 *flags &= ~BADCERT_NOT_TRUSTED;
3560 break;
3561 }
3562
Paul Bakker9a736322012-11-14 12:39:52 +00003563 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003564 * If top of chain is not the same as the trusted CA send a verify request
3565 * to the callback for any issues with validity and CRL presence for the
3566 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003567 */
3568 if( trust_ca != NULL &&
3569 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3570 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3571 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003572 {
3573 /* Check trusted CA's CRL for then chain's top crt */
3574 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3575
3576 if( x509parse_time_expired( &trust_ca->valid_to ) )
3577 ca_flags |= BADCERT_EXPIRED;
3578
Paul Bakker915275b2012-09-28 07:10:55 +00003579 if( NULL != f_vrfy )
3580 {
Paul Bakker9a736322012-11-14 12:39:52 +00003581 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003582 return( ret );
3583 }
3584 }
3585
3586 /* Call callback on top cert */
3587 if( NULL != f_vrfy )
3588 {
Paul Bakker9a736322012-11-14 12:39:52 +00003589 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003590 return( ret );
3591 }
3592
Paul Bakker915275b2012-09-28 07:10:55 +00003593 *flags |= ca_flags;
3594
3595 return( 0 );
3596}
3597
3598static int x509parse_verify_child(
3599 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003600 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003601 int (*f_vrfy)(void *, x509_cert *, int, int *),
3602 void *p_vrfy )
3603{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003604 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003605 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003606 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003607 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003608 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003609
3610 if( x509parse_time_expired( &child->valid_to ) )
3611 *flags |= BADCERT_EXPIRED;
3612
Paul Bakkerc70b9822013-04-07 22:00:46 +02003613 md_info = md_info_from_type( child->sig_md );
3614 if( md_info == NULL )
3615 {
3616 /*
3617 * Cannot check 'unknown' hash
3618 */
Paul Bakker915275b2012-09-28 07:10:55 +00003619 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003620 }
3621 else
3622 {
3623 md( md_info, child->tbs.p, child->tbs.len, hash );
3624
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003625 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003626 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003627 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003628 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003629 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003630 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003631 }
3632
Paul Bakker915275b2012-09-28 07:10:55 +00003633 /* Check trusted CA's CRL for the given crt */
3634 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3635
3636 grandparent = parent->next;
3637
3638 while( grandparent != NULL )
3639 {
3640 if( grandparent->version == 0 ||
3641 grandparent->ca_istrue == 0 ||
3642 parent->issuer_raw.len != grandparent->subject_raw.len ||
3643 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3644 parent->issuer_raw.len ) != 0 )
3645 {
3646 grandparent = grandparent->next;
3647 continue;
3648 }
3649 break;
3650 }
3651
Paul Bakker915275b2012-09-28 07:10:55 +00003652 if( grandparent != NULL )
3653 {
3654 /*
3655 * Part of the chain
3656 */
Paul Bakker9a736322012-11-14 12:39:52 +00003657 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 +00003658 if( ret != 0 )
3659 return( ret );
3660 }
3661 else
3662 {
Paul Bakker9a736322012-11-14 12:39:52 +00003663 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 +00003664 if( ret != 0 )
3665 return( ret );
3666 }
3667
3668 /* child is verified to be a child of the parent, call verify callback */
3669 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003670 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003671 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003672
3673 *flags |= parent_flags;
3674
3675 return( 0 );
3676}
3677
Paul Bakker76fd75a2011-01-16 21:12:10 +00003678/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 * Verify the certificate validity
3680 */
3681int x509parse_verify( x509_cert *crt,
3682 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003683 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003684 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003685 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003686 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687{
Paul Bakker23986e52011-04-24 08:57:21 +00003688 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003689 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003690 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003691 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003693 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
Paul Bakker40ea7de2009-05-03 10:18:48 +00003695 *flags = 0;
3696
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 if( cn != NULL )
3698 {
3699 name = &crt->subject;
3700 cn_len = strlen( cn );
3701
Paul Bakker4d2c1242012-05-10 14:12:46 +00003702 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003704 cur = &crt->subject_alt_names;
3705
3706 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003707 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003708 if( cur->buf.len == cn_len &&
3709 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003710 break;
3711
Paul Bakker535e97d2012-08-23 10:49:55 +00003712 if( cur->buf.len > 2 &&
3713 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003714 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003715 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003716
Paul Bakker4d2c1242012-05-10 14:12:46 +00003717 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003718 }
3719
3720 if( cur == NULL )
3721 *flags |= BADCERT_CN_MISMATCH;
3722 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003723 else
3724 {
3725 while( name != NULL )
3726 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003727 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003728 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003729 if( name->val.len == cn_len &&
3730 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003731 break;
3732
Paul Bakker535e97d2012-08-23 10:49:55 +00003733 if( name->val.len > 2 &&
3734 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003735 x509_wildcard_verify( cn, &name->val ) )
3736 break;
3737 }
3738
3739 name = name->next;
3740 }
3741
3742 if( name == NULL )
3743 *flags |= BADCERT_CN_MISMATCH;
3744 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 }
3746
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003748 * Iterate upwards in the given cert chain, to find our crt parent.
3749 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003751 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003752
Paul Bakker76fd75a2011-01-16 21:12:10 +00003753 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003754 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003755 if( parent->ca_istrue == 0 ||
3756 crt->issuer_raw.len != parent->subject_raw.len ||
3757 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 crt->issuer_raw.len ) != 0 )
3759 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003760 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 continue;
3762 }
Paul Bakker915275b2012-09-28 07:10:55 +00003763 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 }
3765
Paul Bakker915275b2012-09-28 07:10:55 +00003766 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 {
Paul Bakker915275b2012-09-28 07:10:55 +00003768 /*
3769 * Part of the chain
3770 */
Paul Bakker9a736322012-11-14 12:39:52 +00003771 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003772 if( ret != 0 )
3773 return( ret );
3774 }
3775 else
Paul Bakker74111d32011-01-15 16:57:55 +00003776 {
Paul Bakker9a736322012-11-14 12:39:52 +00003777 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003778 if( ret != 0 )
3779 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003780 }
Paul Bakker915275b2012-09-28 07:10:55 +00003781
3782 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003783 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003784
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 return( 0 );
3786}
3787
3788/*
3789 * Unallocate all certificate data
3790 */
3791void x509_free( x509_cert *crt )
3792{
3793 x509_cert *cert_cur = crt;
3794 x509_cert *cert_prv;
3795 x509_name *name_cur;
3796 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003797 x509_sequence *seq_cur;
3798 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003799
3800 if( crt == NULL )
3801 return;
3802
3803 do
3804 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003805 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 name_cur = cert_cur->issuer.next;
3808 while( name_cur != NULL )
3809 {
3810 name_prv = name_cur;
3811 name_cur = name_cur->next;
3812 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003813 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 }
3815
3816 name_cur = cert_cur->subject.next;
3817 while( name_cur != NULL )
3818 {
3819 name_prv = name_cur;
3820 name_cur = name_cur->next;
3821 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003822 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 }
3824
Paul Bakker74111d32011-01-15 16:57:55 +00003825 seq_cur = cert_cur->ext_key_usage.next;
3826 while( seq_cur != NULL )
3827 {
3828 seq_prv = seq_cur;
3829 seq_cur = seq_cur->next;
3830 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003831 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003832 }
3833
Paul Bakker8afa70d2012-02-11 18:42:45 +00003834 seq_cur = cert_cur->subject_alt_names.next;
3835 while( seq_cur != NULL )
3836 {
3837 seq_prv = seq_cur;
3838 seq_cur = seq_cur->next;
3839 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003840 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003841 }
3842
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 if( cert_cur->raw.p != NULL )
3844 {
3845 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003846 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 }
3848
3849 cert_cur = cert_cur->next;
3850 }
3851 while( cert_cur != NULL );
3852
3853 cert_cur = crt;
3854 do
3855 {
3856 cert_prv = cert_cur;
3857 cert_cur = cert_cur->next;
3858
3859 memset( cert_prv, 0, sizeof( x509_cert ) );
3860 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003861 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 }
3863 while( cert_cur != NULL );
3864}
3865
Paul Bakkerd98030e2009-05-02 15:13:40 +00003866/*
3867 * Unallocate all CRL data
3868 */
3869void x509_crl_free( x509_crl *crl )
3870{
3871 x509_crl *crl_cur = crl;
3872 x509_crl *crl_prv;
3873 x509_name *name_cur;
3874 x509_name *name_prv;
3875 x509_crl_entry *entry_cur;
3876 x509_crl_entry *entry_prv;
3877
3878 if( crl == NULL )
3879 return;
3880
3881 do
3882 {
3883 name_cur = crl_cur->issuer.next;
3884 while( name_cur != NULL )
3885 {
3886 name_prv = name_cur;
3887 name_cur = name_cur->next;
3888 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003889 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003890 }
3891
3892 entry_cur = crl_cur->entry.next;
3893 while( entry_cur != NULL )
3894 {
3895 entry_prv = entry_cur;
3896 entry_cur = entry_cur->next;
3897 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003898 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003899 }
3900
3901 if( crl_cur->raw.p != NULL )
3902 {
3903 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003904 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003905 }
3906
3907 crl_cur = crl_cur->next;
3908 }
3909 while( crl_cur != NULL );
3910
3911 crl_cur = crl;
3912 do
3913 {
3914 crl_prv = crl_cur;
3915 crl_cur = crl_cur->next;
3916
3917 memset( crl_prv, 0, sizeof( x509_crl ) );
3918 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003919 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003920 }
3921 while( crl_cur != NULL );
3922}
3923
Paul Bakker40e46942009-01-03 21:51:57 +00003924#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003925
Paul Bakker40e46942009-01-03 21:51:57 +00003926#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
3928/*
3929 * Checkup routine
3930 */
3931int x509_self_test( int verbose )
3932{
Paul Bakker5690efc2011-05-26 13:16:06 +00003933#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003934 int ret;
3935 int flags;
3936 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 x509_cert cacert;
3938 x509_cert clicert;
3939 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003940#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003941 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003942#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003943
3944 if( verbose != 0 )
3945 printf( " X.509 certificate load: " );
3946
3947 memset( &clicert, 0, sizeof( x509_cert ) );
3948
Paul Bakker3c2122f2013-06-24 19:03:14 +02003949 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003950 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 if( ret != 0 )
3952 {
3953 if( verbose != 0 )
3954 printf( "failed\n" );
3955
3956 return( ret );
3957 }
3958
3959 memset( &cacert, 0, sizeof( x509_cert ) );
3960
Paul Bakker3c2122f2013-06-24 19:03:14 +02003961 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003962 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 if( ret != 0 )
3964 {
3965 if( verbose != 0 )
3966 printf( "failed\n" );
3967
3968 return( ret );
3969 }
3970
3971 if( verbose != 0 )
3972 printf( "passed\n X.509 private key load: " );
3973
3974 i = strlen( test_ca_key );
3975 j = strlen( test_ca_pwd );
3976
Paul Bakker66b78b22011-03-25 14:22:50 +00003977 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3978
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003979 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003980 (const unsigned char *) test_ca_key, i,
3981 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 {
3983 if( verbose != 0 )
3984 printf( "failed\n" );
3985
3986 return( ret );
3987 }
3988
3989 if( verbose != 0 )
3990 printf( "passed\n X.509 signature verify: ");
3991
Paul Bakker23986e52011-04-24 08:57:21 +00003992 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993 if( ret != 0 )
3994 {
3995 if( verbose != 0 )
3996 printf( "failed\n" );
3997
3998 return( ret );
3999 }
4000
Paul Bakker5690efc2011-05-26 13:16:06 +00004001#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004003 printf( "passed\n X.509 DHM parameter load: " );
4004
4005 i = strlen( test_dhm_params );
4006 j = strlen( test_ca_pwd );
4007
Paul Bakker3c2122f2013-06-24 19:03:14 +02004008 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004009 {
4010 if( verbose != 0 )
4011 printf( "failed\n" );
4012
4013 return( ret );
4014 }
4015
4016 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004018#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
4020 x509_free( &cacert );
4021 x509_free( &clicert );
4022 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004023#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004024 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004025#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
4027 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004028#else
4029 ((void) verbose);
4030 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004032}
4033
4034#endif
4035
4036#endif