blob: 535b1807559f40d5c535d15cce3fbae395248996 [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;
573
574 if( ( ret = asn1_get_tag( p, end, &len,
575 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576 {
577 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
578 }
579
580 end = *p + len;
581
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200582 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200583 return( ret );
584
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200585 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200586 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
587
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200588 if( *p + len != end )
589 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
590 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
591
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200592 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
593 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200594
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200595#if defined(POLARSSL_RSA_C)
596 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200597 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200598 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
599 } else
600#endif /* POLARSSL_RSA_C */
601#if defined(POLARSSL_ECP_C)
602 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
603 {
604 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
605 x509_get_ecpubkey( p, end, pk_ec( *pk ) );
606 } else
607#endif /* POLARSSL_ECP_C */
608 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200609
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200610 if( ret == 0 && *p != end )
611 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
612 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
613
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200614 if( ret != 0 )
615 pk_free( pk );
616
617 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200618}
619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000621 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 x509_buf *sig )
623{
Paul Bakker23986e52011-04-24 08:57:21 +0000624 int ret;
625 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker8afa70d2012-02-11 18:42:45 +0000627 if( ( end - *p ) < 1 )
628 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
629 POLARSSL_ERR_ASN1_OUT_OF_DATA );
630
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 sig->tag = **p;
632
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200633 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 sig->len = len;
637 sig->p = *p;
638
639 *p += len;
640
641 return( 0 );
642}
643
644/*
645 * X.509 v2/v3 unique identifier (not parsed)
646 */
647static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000648 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 x509_buf *uid, int n )
650{
651 int ret;
652
653 if( *p == end )
654 return( 0 );
655
656 uid->tag = **p;
657
658 if( ( ret = asn1_get_tag( p, end, &uid->len,
659 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
660 {
Paul Bakker40e46942009-01-03 21:51:57 +0000661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 return( 0 );
663
664 return( ret );
665 }
666
667 uid->p = *p;
668 *p += uid->len;
669
670 return( 0 );
671}
672
673/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000674 * X.509 Extensions (No parsing of extensions, pointer should
675 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
677static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000678 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000679 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680{
Paul Bakker23986e52011-04-24 08:57:21 +0000681 int ret;
682 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 if( *p == end )
685 return( 0 );
686
687 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000688
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000690 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 ext->p = *p;
694 end = *p + ext->len;
695
696 /*
697 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
698 *
699 * Extension ::= SEQUENCE {
700 * extnID OBJECT IDENTIFIER,
701 * critical BOOLEAN DEFAULT FALSE,
702 * extnValue OCTET STRING }
703 */
704 if( ( ret = asn1_get_tag( p, end, &len,
705 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000710 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Paul Bakkerd98030e2009-05-02 15:13:40 +0000712 return( 0 );
713}
714
715/*
716 * X.509 CRL v2 extensions (no extensions parsed yet.)
717 */
718static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000719 const unsigned char *end,
720 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000721{
Paul Bakker23986e52011-04-24 08:57:21 +0000722 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000723 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000724
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000725 /* Get explicit tag */
726 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000727 {
728 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
729 return( 0 );
730
731 return( ret );
732 }
733
734 while( *p < end )
735 {
736 if( ( ret = asn1_get_tag( p, end, &len,
737 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000739
740 *p += len;
741 }
742
743 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000744 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000745 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
746
747 return( 0 );
748}
749
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000750/*
751 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
752 */
753static int x509_get_crl_entry_ext( unsigned char **p,
754 const unsigned char *end,
755 x509_buf *ext )
756{
757 int ret;
758 size_t len = 0;
759
760 /* OPTIONAL */
761 if (end <= *p)
762 return( 0 );
763
764 ext->tag = **p;
765 ext->p = *p;
766
767 /*
768 * Get CRL-entry extension sequence header
769 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
770 */
771 if( ( ret = asn1_get_tag( p, end, &ext->len,
772 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
773 {
774 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
775 {
776 ext->p = NULL;
777 return( 0 );
778 }
779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
780 }
781
782 end = *p + ext->len;
783
784 if( end != *p + ext->len )
785 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
786 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
787
788 while( *p < end )
789 {
790 if( ( ret = asn1_get_tag( p, end, &len,
791 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
792 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
793
794 *p += len;
795 }
796
797 if( *p != end )
798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
799 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
800
801 return( 0 );
802}
803
Paul Bakker74111d32011-01-15 16:57:55 +0000804static int x509_get_basic_constraints( unsigned char **p,
805 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000806 int *ca_istrue,
807 int *max_pathlen )
808{
Paul Bakker23986e52011-04-24 08:57:21 +0000809 int ret;
810 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000811
812 /*
813 * BasicConstraints ::= SEQUENCE {
814 * cA BOOLEAN DEFAULT FALSE,
815 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
816 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000817 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000818 *max_pathlen = 0; /* endless */
819
820 if( ( ret = asn1_get_tag( p, end, &len,
821 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
824 if( *p == end )
825 return 0;
826
Paul Bakker3cccddb2011-01-16 21:46:31 +0000827 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000828 {
829 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000830 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000831
832 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000834
Paul Bakker3cccddb2011-01-16 21:46:31 +0000835 if( *ca_istrue != 0 )
836 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000837 }
838
839 if( *p == end )
840 return 0;
841
842 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000844
845 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 (*max_pathlen)++;
850
Paul Bakker74111d32011-01-15 16:57:55 +0000851 return 0;
852}
853
854static int x509_get_ns_cert_type( unsigned char **p,
855 const unsigned char *end,
856 unsigned char *ns_cert_type)
857{
858 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000859 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000860
861 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000863
864 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000865 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000866 POLARSSL_ERR_ASN1_INVALID_LENGTH );
867
868 /* Get actual bitstring */
869 *ns_cert_type = *bs.p;
870 return 0;
871}
872
873static int x509_get_key_usage( unsigned char **p,
874 const unsigned char *end,
875 unsigned char *key_usage)
876{
877 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000878 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000879
880 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000881 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000882
Paul Bakker94a67962012-08-23 13:03:52 +0000883 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000884 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000885 POLARSSL_ERR_ASN1_INVALID_LENGTH );
886
887 /* Get actual bitstring */
888 *key_usage = *bs.p;
889 return 0;
890}
891
Paul Bakkerd98030e2009-05-02 15:13:40 +0000892/*
Paul Bakker74111d32011-01-15 16:57:55 +0000893 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
894 *
895 * KeyPurposeId ::= OBJECT IDENTIFIER
896 */
897static int x509_get_ext_key_usage( unsigned char **p,
898 const unsigned char *end,
899 x509_sequence *ext_key_usage)
900{
901 int ret;
902
903 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000905
906 /* Sequence length must be >= 1 */
907 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000909 POLARSSL_ERR_ASN1_INVALID_LENGTH );
910
911 return 0;
912}
913
914/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000915 * SubjectAltName ::= GeneralNames
916 *
917 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
918 *
919 * GeneralName ::= CHOICE {
920 * otherName [0] OtherName,
921 * rfc822Name [1] IA5String,
922 * dNSName [2] IA5String,
923 * x400Address [3] ORAddress,
924 * directoryName [4] Name,
925 * ediPartyName [5] EDIPartyName,
926 * uniformResourceIdentifier [6] IA5String,
927 * iPAddress [7] OCTET STRING,
928 * registeredID [8] OBJECT IDENTIFIER }
929 *
930 * OtherName ::= SEQUENCE {
931 * type-id OBJECT IDENTIFIER,
932 * value [0] EXPLICIT ANY DEFINED BY type-id }
933 *
934 * EDIPartyName ::= SEQUENCE {
935 * nameAssigner [0] DirectoryString OPTIONAL,
936 * partyName [1] DirectoryString }
937 *
938 * NOTE: PolarSSL only parses and uses dNSName at this point.
939 */
940static int x509_get_subject_alt_name( unsigned char **p,
941 const unsigned char *end,
942 x509_sequence *subject_alt_name )
943{
944 int ret;
945 size_t len, tag_len;
946 asn1_buf *buf;
947 unsigned char tag;
948 asn1_sequence *cur = subject_alt_name;
949
950 /* Get main sequence tag */
951 if( ( ret = asn1_get_tag( p, end, &len,
952 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
954
955 if( *p + len != end )
956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
957 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
958
959 while( *p < end )
960 {
961 if( ( end - *p ) < 1 )
962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
963 POLARSSL_ERR_ASN1_OUT_OF_DATA );
964
965 tag = **p;
966 (*p)++;
967 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
968 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
969
970 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
971 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
972 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
973
974 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
975 {
976 *p += tag_len;
977 continue;
978 }
979
980 buf = &(cur->buf);
981 buf->tag = tag;
982 buf->p = *p;
983 buf->len = tag_len;
984 *p += buf->len;
985
986 /* Allocate and assign next pointer */
987 if (*p < end)
988 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200989 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000990 sizeof( asn1_sequence ) );
991
992 if( cur->next == NULL )
993 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
994 POLARSSL_ERR_ASN1_MALLOC_FAILED );
995
Paul Bakker535e97d2012-08-23 10:49:55 +0000996 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000997 cur = cur->next;
998 }
999 }
1000
1001 /* Set final sequence entry's next pointer to NULL */
1002 cur->next = NULL;
1003
1004 if( *p != end )
1005 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1006 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1007
1008 return( 0 );
1009}
1010
1011/*
Paul Bakker74111d32011-01-15 16:57:55 +00001012 * X.509 v3 extensions
1013 *
1014 * TODO: Perform all of the basic constraints tests required by the RFC
1015 * TODO: Set values for undetected extensions to a sane default?
1016 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001017 */
1018static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001019 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001020 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001021{
Paul Bakker23986e52011-04-24 08:57:21 +00001022 int ret;
1023 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001024 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001026 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001027 {
1028 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1029 return( 0 );
1030
1031 return( ret );
1032 }
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 while( *p < end )
1035 {
Paul Bakker74111d32011-01-15 16:57:55 +00001036 /*
1037 * Extension ::= SEQUENCE {
1038 * extnID OBJECT IDENTIFIER,
1039 * critical BOOLEAN DEFAULT FALSE,
1040 * extnValue OCTET STRING }
1041 */
1042 x509_buf extn_oid = {0, 0, NULL};
1043 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001045
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 if( ( ret = asn1_get_tag( p, end, &len,
1047 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001048 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001050 end_ext_data = *p + len;
1051
Paul Bakker74111d32011-01-15 16:57:55 +00001052 /* Get extension ID */
1053 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Paul Bakker74111d32011-01-15 16:57:55 +00001055 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001056 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Paul Bakker74111d32011-01-15 16:57:55 +00001058 extn_oid.p = *p;
1059 *p += extn_oid.len;
1060
1061 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001062 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001063 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1064
1065 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001066 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001067 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001068 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Paul Bakker74111d32011-01-15 16:57:55 +00001070 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001073 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001075 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001076
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001077 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001079 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 /*
1082 * Detect supported extensions
1083 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1085
1086 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001087 {
1088 /* No parser found, skip extension */
1089 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker5c721f92011-07-27 16:51:09 +00001091#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001092 if( is_critical )
1093 {
1094 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001095 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001096 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1097 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001098#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001099 continue;
1100 }
1101
1102 crt->ext_types |= ext_type;
1103
1104 switch( ext_type )
1105 {
1106 case EXT_BASIC_CONSTRAINTS:
1107 /* Parse basic constraints */
1108 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1109 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1110 return ( ret );
1111 break;
1112
1113 case EXT_KEY_USAGE:
1114 /* Parse key usage */
1115 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1116 &crt->key_usage ) ) != 0 )
1117 return ( ret );
1118 break;
1119
1120 case EXT_EXTENDED_KEY_USAGE:
1121 /* Parse extended key usage */
1122 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1123 &crt->ext_key_usage ) ) != 0 )
1124 return ( ret );
1125 break;
1126
1127 case EXT_SUBJECT_ALT_NAME:
1128 /* Parse subject alt name */
1129 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1130 &crt->subject_alt_names ) ) != 0 )
1131 return ( ret );
1132 break;
1133
1134 case EXT_NS_CERT_TYPE:
1135 /* Parse netscape certificate type */
1136 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1137 &crt->ns_cert_type ) ) != 0 )
1138 return ( ret );
1139 break;
1140
1141 default:
1142 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
1145
1146 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001147 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 return( 0 );
1151}
1152
1153/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154 * X.509 CRL Entries
1155 */
1156static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001157 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 x509_crl_entry *entry )
1159{
Paul Bakker23986e52011-04-24 08:57:21 +00001160 int ret;
1161 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001162 x509_crl_entry *cur_entry = entry;
1163
1164 if( *p == end )
1165 return( 0 );
1166
Paul Bakker9be19372009-07-27 20:21:53 +00001167 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001168 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1169 {
1170 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1171 return( 0 );
1172
1173 return( ret );
1174 }
1175
Paul Bakker9be19372009-07-27 20:21:53 +00001176 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001177
1178 while( *p < end )
1179 {
Paul Bakker23986e52011-04-24 08:57:21 +00001180 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001181 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182
1183 if( ( ret = asn1_get_tag( p, end, &len2,
1184 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1185 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186 return( ret );
1187 }
1188
Paul Bakker9be19372009-07-27 20:21:53 +00001189 cur_entry->raw.tag = **p;
1190 cur_entry->raw.p = *p;
1191 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001192 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001193
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001194 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001195 return( ret );
1196
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001197 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001198 return( ret );
1199
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001200 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001201 return( ret );
1202
Paul Bakker74111d32011-01-15 16:57:55 +00001203 if ( *p < end )
1204 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001205 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001206
1207 if( cur_entry->next == NULL )
1208 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1209
Paul Bakkerd98030e2009-05-02 15:13:40 +00001210 cur_entry = cur_entry->next;
1211 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1212 }
1213 }
1214
1215 return( 0 );
1216}
1217
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1219 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001220{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001221 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001222
Paul Bakkerc70b9822013-04-07 22:00:46 +02001223 if( ret != 0 )
1224 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001225
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001227}
1228
Paul Bakkerd98030e2009-05-02 15:13:40 +00001229/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001230 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001232static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1233 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234{
Paul Bakker23986e52011-04-24 08:57:21 +00001235 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001236 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001237 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Paul Bakker320a4b52009-03-28 18:52:39 +00001239 /*
1240 * Check for valid input
1241 */
1242 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001243 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001244
Paul Bakker6e339b52013-07-03 13:37:05 +02001245 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001246
1247 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001248 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001249
1250 memcpy( p, buf, buflen );
1251
1252 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 crt->raw.p = p;
1255 crt->raw.len = len;
1256 end = p + len;
1257
1258 /*
1259 * Certificate ::= SEQUENCE {
1260 * tbsCertificate TBSCertificate,
1261 * signatureAlgorithm AlgorithmIdentifier,
1262 * signatureValue BIT STRING }
1263 */
1264 if( ( ret = asn1_get_tag( &p, end, &len,
1265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1266 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001267 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001268 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
1270
Paul Bakkerb00ca422012-09-25 12:10:00 +00001271 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001275 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001277 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 /*
1280 * TBSCertificate ::= SEQUENCE {
1281 */
1282 crt->tbs.p = p;
1283
1284 if( ( ret = asn1_get_tag( &p, end, &len,
1285 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1286 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001287 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001288 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
1290
1291 end = p + len;
1292 crt->tbs.len = end - crt->tbs.p;
1293
1294 /*
1295 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1296 *
1297 * CertificateSerialNumber ::= INTEGER
1298 *
1299 * signature AlgorithmIdentifier
1300 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001301 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1302 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1303 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308
1309 crt->version++;
1310
1311 if( crt->version > 3 )
1312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001314 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
1316
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1318 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001320 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001321 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323
1324 /*
1325 * issuer Name
1326 */
1327 crt->issuer_raw.p = p;
1328
1329 if( ( ret = asn1_get_tag( &p, end, &len,
1330 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1331 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001332 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001333 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 }
1335
1336 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1337 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001338 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 return( ret );
1340 }
1341
1342 crt->issuer_raw.len = p - crt->issuer_raw.p;
1343
1344 /*
1345 * Validity ::= SEQUENCE {
1346 * notBefore Time,
1347 * notAfter Time }
1348 *
1349 */
1350 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1351 &crt->valid_to ) ) != 0 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 return( ret );
1355 }
1356
1357 /*
1358 * subject Name
1359 */
1360 crt->subject_raw.p = p;
1361
1362 if( ( ret = asn1_get_tag( &p, end, &len,
1363 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1364 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001365 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001366 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Paul Bakkercefb3962012-06-27 11:51:09 +00001369 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001371 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 return( ret );
1373 }
1374
1375 crt->subject_raw.len = p - crt->subject_raw.p;
1376
1377 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001378 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001380 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 return( ret );
1384 }
1385
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 /*
1387 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1388 * -- If present, version shall be v2 or v3
1389 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1390 * -- If present, version shall be v2 or v3
1391 * extensions [3] EXPLICIT Extensions OPTIONAL
1392 * -- If present, version shall be v3
1393 */
1394 if( crt->version == 2 || crt->version == 3 )
1395 {
1396 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1397 if( ret != 0 )
1398 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001399 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 return( ret );
1401 }
1402 }
1403
1404 if( crt->version == 2 || crt->version == 3 )
1405 {
1406 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1407 if( ret != 0 )
1408 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001409 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 return( ret );
1411 }
1412 }
1413
1414 if( crt->version == 3 )
1415 {
Paul Bakker74111d32011-01-15 16:57:55 +00001416 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 if( ret != 0 )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 return( ret );
1421 }
1422 }
1423
1424 if( p != end )
1425 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001426 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001427 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001428 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430
Paul Bakkerb00ca422012-09-25 12:10:00 +00001431 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001434 * }
1435 * -- end of TBSCertificate
1436 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 * signatureAlgorithm AlgorithmIdentifier,
1438 * signatureValue BIT STRING
1439 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001440 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001442 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 return( ret );
1444 }
1445
Paul Bakker535e97d2012-08-23 10:49:55 +00001446 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1447 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001450 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452
1453 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1454 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 return( ret );
1457 }
1458
1459 if( p != end )
1460 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001462 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001463 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466 return( 0 );
1467}
1468
1469/*
Paul Bakker42c65812013-06-24 19:21:59 +02001470 * Parse one X.509 certificate in DER format from a buffer and add them to a
1471 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472 */
Paul Bakker42c65812013-06-24 19:21:59 +02001473int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001474{
Paul Bakker42c65812013-06-24 19:21:59 +02001475 int ret;
1476 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477
1478 /*
1479 * Check for valid input
1480 */
1481 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001482 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001483
1484 while( crt->version != 0 && crt->next != NULL )
1485 {
1486 prev = crt;
1487 crt = crt->next;
1488 }
1489
1490 /*
1491 * Add new certificate on the end of the chain if needed.
1492 */
1493 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001494 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001495 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001496
Paul Bakker7d06ad22009-05-02 15:53:56 +00001497 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001498 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001499
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001501 crt = crt->next;
1502 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakker42c65812013-06-24 19:21:59 +02001505 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1506 {
1507 if( prev )
1508 prev->next = NULL;
1509
1510 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001511 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001512
1513 return( ret );
1514 }
1515
1516 return( 0 );
1517}
1518
1519/*
1520 * Parse one or more PEM certificates from a buffer and add them to the chained list
1521 */
1522int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1523{
1524 int ret, success = 0, first_error = 0, total_failed = 0;
1525 int buf_format = X509_FORMAT_DER;
1526
1527 /*
1528 * Check for valid input
1529 */
1530 if( chain == NULL || buf == NULL )
1531 return( POLARSSL_ERR_X509_INVALID_INPUT );
1532
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001533 /*
1534 * Determine buffer content. Buffer contains either one DER certificate or
1535 * one or more PEM certificates.
1536 */
1537#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001538 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001539 buf_format = X509_FORMAT_PEM;
1540#endif
1541
1542 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001543 return x509parse_crt_der( chain, buf, buflen );
1544
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001545#if defined(POLARSSL_PEM_C)
1546 if( buf_format == X509_FORMAT_PEM )
1547 {
1548 pem_context pem;
1549
1550 while( buflen > 0 )
1551 {
1552 size_t use_len;
1553 pem_init( &pem );
1554
1555 ret = pem_read_buffer( &pem,
1556 "-----BEGIN CERTIFICATE-----",
1557 "-----END CERTIFICATE-----",
1558 buf, NULL, 0, &use_len );
1559
1560 if( ret == 0 )
1561 {
1562 /*
1563 * Was PEM encoded
1564 */
1565 buflen -= use_len;
1566 buf += use_len;
1567 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001568 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1569 {
1570 return( ret );
1571 }
Paul Bakker00b28602013-06-24 13:02:41 +02001572 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 {
1574 pem_free( &pem );
1575
Paul Bakker5ed3b342013-06-24 19:05:46 +02001576 /*
1577 * PEM header and footer were found
1578 */
1579 buflen -= use_len;
1580 buf += use_len;
1581
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001582 if( first_error == 0 )
1583 first_error = ret;
1584
1585 continue;
1586 }
1587 else
1588 break;
1589
Paul Bakker42c65812013-06-24 19:21:59 +02001590 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001591
1592 pem_free( &pem );
1593
1594 if( ret != 0 )
1595 {
1596 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001597 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001598 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001599 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001600 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001601
1602 if( first_error == 0 )
1603 first_error = ret;
1604
Paul Bakker42c65812013-06-24 19:21:59 +02001605 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606 continue;
1607 }
1608
1609 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 }
1611 }
1612#endif
1613
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001614 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001616 else if( first_error )
1617 return( first_error );
1618 else
1619 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
1622/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623 * Parse one or more CRLs and add them to the chained list
1624 */
Paul Bakker23986e52011-04-24 08:57:21 +00001625int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626{
Paul Bakker23986e52011-04-24 08:57:21 +00001627 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001628 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 unsigned char *p, *end;
1630 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001631#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001632 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 pem_context pem;
1634#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635
1636 crl = chain;
1637
1638 /*
1639 * Check for valid input
1640 */
1641 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001642 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643
1644 while( crl->version != 0 && crl->next != NULL )
1645 crl = crl->next;
1646
1647 /*
1648 * Add new CRL on the end of the chain if needed.
1649 */
1650 if ( crl->version != 0 && crl->next == NULL)
1651 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001652 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653
Paul Bakker7d06ad22009-05-02 15:53:56 +00001654 if( crl->next == NULL )
1655 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001657 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001658 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659
Paul Bakker7d06ad22009-05-02 15:53:56 +00001660 crl = crl->next;
1661 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 }
1663
Paul Bakker96743fc2011-02-12 14:30:57 +00001664#if defined(POLARSSL_PEM_C)
1665 pem_init( &pem );
1666 ret = pem_read_buffer( &pem,
1667 "-----BEGIN X509 CRL-----",
1668 "-----END X509 CRL-----",
1669 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
Paul Bakker96743fc2011-02-12 14:30:57 +00001671 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001673 /*
1674 * Was PEM encoded
1675 */
1676 buflen -= use_len;
1677 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678
1679 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001680 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001682 p = pem.buf;
1683 pem.buf = NULL;
1684 len = pem.buflen;
1685 pem_free( &pem );
1686 }
Paul Bakker00b28602013-06-24 13:02:41 +02001687 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001688 {
1689 pem_free( &pem );
1690 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 }
1692 else
1693 {
1694 /*
1695 * nope, copy the raw DER data
1696 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001697 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698
1699 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001700 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701
1702 memcpy( p, buf, buflen );
1703
1704 buflen = 0;
1705 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001706#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001707 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001708
1709 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001710 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001711
1712 memcpy( p, buf, buflen );
1713
1714 buflen = 0;
1715#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716
1717 crl->raw.p = p;
1718 crl->raw.len = len;
1719 end = p + len;
1720
1721 /*
1722 * CertificateList ::= SEQUENCE {
1723 * tbsCertList TBSCertList,
1724 * signatureAlgorithm AlgorithmIdentifier,
1725 * signatureValue BIT STRING }
1726 */
1727 if( ( ret = asn1_get_tag( &p, end, &len,
1728 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1729 {
1730 x509_crl_free( crl );
1731 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1732 }
1733
Paul Bakker23986e52011-04-24 08:57:21 +00001734 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
1736 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001737 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1739 }
1740
1741 /*
1742 * TBSCertList ::= SEQUENCE {
1743 */
1744 crl->tbs.p = p;
1745
1746 if( ( ret = asn1_get_tag( &p, end, &len,
1747 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001750 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 }
1752
1753 end = p + len;
1754 crl->tbs.len = end - crl->tbs.p;
1755
1756 /*
1757 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1758 * -- if present, MUST be v2
1759 *
1760 * signature AlgorithmIdentifier
1761 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001762 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001763 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 {
1765 x509_crl_free( crl );
1766 return( ret );
1767 }
1768
1769 crl->version++;
1770
1771 if( crl->version > 2 )
1772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1775 }
1776
Paul Bakkerc70b9822013-04-07 22:00:46 +02001777 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1778 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779 {
1780 x509_crl_free( crl );
1781 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1782 }
1783
1784 /*
1785 * issuer Name
1786 */
1787 crl->issuer_raw.p = p;
1788
1789 if( ( ret = asn1_get_tag( &p, end, &len,
1790 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1791 {
1792 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001793 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 }
1795
1796 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1797 {
1798 x509_crl_free( crl );
1799 return( ret );
1800 }
1801
1802 crl->issuer_raw.len = p - crl->issuer_raw.p;
1803
1804 /*
1805 * thisUpdate Time
1806 * nextUpdate Time OPTIONAL
1807 */
Paul Bakker91200182010-02-18 21:26:15 +00001808 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001809 {
1810 x509_crl_free( crl );
1811 return( ret );
1812 }
1813
Paul Bakker91200182010-02-18 21:26:15 +00001814 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 {
Paul Bakker9d781402011-05-09 16:17:09 +00001816 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001817 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001818 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001819 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001820 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824 }
1825
1826 /*
1827 * revokedCertificates SEQUENCE OF SEQUENCE {
1828 * userCertificate CertificateSerialNumber,
1829 * revocationDate Time,
1830 * crlEntryExtensions Extensions OPTIONAL
1831 * -- if present, MUST be v2
1832 * } OPTIONAL
1833 */
1834 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 /*
1841 * crlExtensions EXPLICIT Extensions OPTIONAL
1842 * -- if present, MUST be v2
1843 */
1844 if( crl->version == 2 )
1845 {
1846 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1847
1848 if( ret != 0 )
1849 {
1850 x509_crl_free( crl );
1851 return( ret );
1852 }
1853 }
1854
1855 if( p != end )
1856 {
1857 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001858 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1860 }
1861
1862 end = crl->raw.p + crl->raw.len;
1863
1864 /*
1865 * signatureAlgorithm AlgorithmIdentifier,
1866 * signatureValue BIT STRING
1867 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001868 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869 {
1870 x509_crl_free( crl );
1871 return( ret );
1872 }
1873
Paul Bakker535e97d2012-08-23 10:49:55 +00001874 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1875 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 {
1877 x509_crl_free( crl );
1878 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1879 }
1880
1881 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1882 {
1883 x509_crl_free( crl );
1884 return( ret );
1885 }
1886
1887 if( p != end )
1888 {
1889 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001890 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1892 }
1893
1894 if( buflen > 0 )
1895 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001896 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897
Paul Bakker7d06ad22009-05-02 15:53:56 +00001898 if( crl->next == NULL )
1899 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001901 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001902 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903
Paul Bakker7d06ad22009-05-02 15:53:56 +00001904 crl = crl->next;
1905 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906
1907 return( x509parse_crl( crl, buf, buflen ) );
1908 }
1909
1910 return( 0 );
1911}
1912
Paul Bakker335db3f2011-04-25 15:28:35 +00001913#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915 * Load all data from a file into a given buffer.
1916 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001917static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001918{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001919 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001920
Paul Bakkerd98030e2009-05-02 15:13:40 +00001921 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001922 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001923
Paul Bakkerd98030e2009-05-02 15:13:40 +00001924 fseek( f, 0, SEEK_END );
1925 *n = (size_t) ftell( f );
1926 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001927
Paul Bakker694d3ae2013-08-19 14:23:38 +02001928 if( *n + 1 == 0 ||
1929 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001930 {
1931 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001932 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001933 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001934
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 if( fread( *buf, 1, *n, f ) != *n )
1936 {
1937 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001938 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001939 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001940 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001941
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001943
Paul Bakkerd98030e2009-05-02 15:13:40 +00001944 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001945
Paul Bakkerd98030e2009-05-02 15:13:40 +00001946 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001947}
1948
1949/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 * Load one or more certificates and add them to the chained list
1951 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001952int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953{
1954 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 size_t n;
1956 unsigned char *buf;
1957
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001958 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001959 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
Paul Bakker69e095c2011-12-10 21:55:01 +00001961 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
1963 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001964 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
1966 return( ret );
1967}
1968
Paul Bakker8d914582012-06-04 12:46:42 +00001969int x509parse_crtpath( x509_cert *chain, const char *path )
1970{
1971 int ret = 0;
1972#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001973 int w_ret;
1974 WCHAR szDir[MAX_PATH];
1975 char filename[MAX_PATH];
1976 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001977 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001978
Paul Bakker97872ac2012-11-02 12:53:26 +00001979 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001980 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001981
1982 if( len > MAX_PATH - 3 )
1983 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001984
Paul Bakker3338b792012-10-01 21:13:10 +00001985 memset( szDir, 0, sizeof(szDir) );
1986 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001987 memcpy( filename, path, len );
1988 filename[len++] = '\\';
1989 p = filename + len;
1990 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001991
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001992 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001993
Paul Bakker97872ac2012-11-02 12:53:26 +00001994 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001995 if (hFind == INVALID_HANDLE_VALUE)
1996 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1997
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001998 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001999 do
2000 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002001 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002002
Paul Bakkere4791f32012-06-04 21:29:15 +00002003 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002004 continue;
2005
Paul Bakker3338b792012-10-01 21:13:10 +00002006 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2007 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002008 p, len - 1,
2009 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002010
Paul Bakker3338b792012-10-01 21:13:10 +00002011 w_ret = x509parse_crtfile( chain, filename );
2012 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002013 ret++;
2014 else
2015 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002016 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002017 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002018
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002019 if (GetLastError() != ERROR_NO_MORE_FILES)
2020 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002021
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002022cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002023 FindClose( hFind );
2024#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002025 int t_ret, i;
2026 struct stat sb;
2027 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002028 char entry_name[255];
2029 DIR *dir = opendir( path );
2030
2031 if( dir == NULL)
2032 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2033
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002034 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002035 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002036 if( result == NULL )
2037 break;
2038
2039 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2040
2041 i = stat( entry_name, &sb );
2042
2043 if( i == -1 )
2044 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2045
2046 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002047 continue;
2048
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002049 // Ignore parse errors
2050 //
Paul Bakker8d914582012-06-04 12:46:42 +00002051 t_ret = x509parse_crtfile( chain, entry_name );
2052 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002053 ret++;
2054 else
2055 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002056 }
2057 closedir( dir );
2058#endif
2059
2060 return( ret );
2061}
2062
Paul Bakkerd98030e2009-05-02 15:13:40 +00002063/*
2064 * Load one or more CRLs and add them to the chained list
2065 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002066int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002067{
2068 int ret;
2069 size_t n;
2070 unsigned char *buf;
2071
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002072 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002073 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002074
Paul Bakker27fdf462011-06-09 13:55:13 +00002075 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002076
2077 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002078 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002079
2080 return( ret );
2081}
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002084 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002085 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002086int x509parse_keyfile( pk_context *ctx,
2087 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002088{
2089 int ret;
2090 size_t n;
2091 unsigned char *buf;
2092
2093 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2094 return( ret );
2095
2096 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002097 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002098 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002099 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002100 (const unsigned char *) pwd, strlen( pwd ) );
2101
2102 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002103 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002104
2105 return( ret );
2106}
2107
2108/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002109 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002110 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002111int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002112{
2113 int ret;
2114 size_t n;
2115 unsigned char *buf;
2116
2117 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2118 return( ret );
2119
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002120 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002121
2122 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002123 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002124
2125 return( ret );
2126}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002127
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002128#if defined(POLARSSL_RSA_C)
2129/*
2130 * Load and parse a private RSA key
2131 */
2132int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2133{
2134 pk_context pk;
2135
2136 pk_init( &pk );
2137 pk_wrap_rsa( &pk, rsa );
2138
2139 return( x509parse_keyfile( &pk, path, pwd ) );
2140}
2141
2142/*
2143 * Load and parse a public RSA key
2144 */
2145int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2146{
2147 pk_context pk;
2148
2149 pk_init( &pk );
2150 pk_wrap_rsa( &pk, rsa );
2151
2152 return( x509parse_public_keyfile( &pk, path ) );
2153}
2154#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002155#endif /* POLARSSL_FS_IO */
2156
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002157#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002158/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002159 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002161static int x509parse_key_pkcs1_der( rsa_context *rsa,
2162 const unsigned char *key,
2163 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002164{
Paul Bakker23986e52011-04-24 08:57:21 +00002165 int ret;
2166 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002168
Paul Bakker96743fc2011-02-12 14:30:57 +00002169 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002170 end = p + keylen;
2171
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002173 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002174 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 * RSAPrivateKey ::= SEQUENCE {
2176 * version Version,
2177 * modulus INTEGER, -- n
2178 * publicExponent INTEGER, -- e
2179 * privateExponent INTEGER, -- d
2180 * prime1 INTEGER, -- p
2181 * prime2 INTEGER, -- q
2182 * exponent1 INTEGER, -- d mod (p-1)
2183 * exponent2 INTEGER, -- d mod (q-1)
2184 * coefficient INTEGER, -- (inverse of q) mod p
2185 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2186 * }
2187 */
2188 if( ( ret = asn1_get_tag( &p, end, &len,
2189 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2190 {
Paul Bakker9d781402011-05-09 16:17:09 +00002191 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
2193
2194 end = p + len;
2195
2196 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2197 {
Paul Bakker9d781402011-05-09 16:17:09 +00002198 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
2201 if( rsa->ver != 0 )
2202 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002203 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205
2206 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2207 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2208 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2209 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2210 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2211 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2213 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2214 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002216 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 }
2218
2219 rsa->len = mpi_size( &rsa->N );
2220
2221 if( p != end )
2222 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002224 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002225 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
2227
2228 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2229 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 rsa_free( rsa );
2231 return( ret );
2232 }
2233
Paul Bakkere2f50402013-06-24 19:00:59 +02002234 return( 0 );
2235}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002236#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002237
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002238#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002239/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002240 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002241 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002242static int x509parse_key_sec1_der( ecp_keypair *eck,
2243 const unsigned char *key,
2244 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002245{
2246 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002247 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002248 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002249 x509_buf params;
2250 unsigned char *p = (unsigned char *) key;
2251 unsigned char *end = p + keylen;
2252 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002253
2254 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002255 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002256 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002257 * ECPrivateKey ::= SEQUENCE {
2258 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2259 * privateKey OCTET STRING,
2260 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2261 * publicKey [1] BIT STRING OPTIONAL
2262 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002263 */
2264 if( ( ret = asn1_get_tag( &p, end, &len,
2265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2266 {
2267 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2268 }
2269
2270 end = p + len;
2271
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002272 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002273 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2274
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002275 if( version != 1 )
2276 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002277
Paul Bakkere2f50402013-06-24 19:00:59 +02002278 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2279 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2280
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002281 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002282 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002283 ecp_keypair_free( eck );
2284 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2285 }
2286
2287 p += len;
2288
2289 /*
2290 * Is 'parameters' present?
2291 */
2292 if( ( ret = asn1_get_tag( &p, end, &len,
2293 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2294 {
2295 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2296 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2297 {
2298 ecp_keypair_free( eck );
2299 return( ret );
2300 }
2301 }
2302 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2303 {
2304 ecp_keypair_free( eck );
2305 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2306 }
2307
2308 /*
2309 * Is 'publickey' present?
2310 */
2311 if( ( ret = asn1_get_tag( &p, end, &len,
2312 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2313 {
2314 end2 = p + len;
2315
2316 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2317 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2318
2319 if( p + len != end2 )
2320 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2321 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2322
2323 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2324 return( ret );
2325 }
2326 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2327 {
2328 ecp_keypair_free( eck );
2329 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2330 }
2331
2332 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2333 {
2334 ecp_keypair_free( eck );
2335 return( ret );
2336 }
2337
2338 return 0;
2339}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002340#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002341
2342/*
2343 * Parse an unencrypted PKCS#8 encoded private key
2344 */
2345static int x509parse_key_pkcs8_unencrypted_der(
2346 pk_context *pk,
2347 const unsigned char* key,
2348 size_t keylen )
2349{
2350 int ret, version;
2351 size_t len;
2352 x509_buf params;
2353 unsigned char *p = (unsigned char *) key;
2354 unsigned char *end = p + keylen;
2355 pk_type_t pk_alg = POLARSSL_PK_NONE;
2356
2357 /*
2358 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2359 *
2360 * PrivateKeyInfo ::= SEQUENCE {
2361 * version Version,
2362 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2363 * privateKey PrivateKey,
2364 * attributes [0] IMPLICIT Attributes OPTIONAL }
2365 *
2366 * Version ::= INTEGER
2367 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2368 * PrivateKey ::= OCTET STRING
2369 *
2370 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2371 */
2372
2373 if( ( ret = asn1_get_tag( &p, end, &len,
2374 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2375 {
2376 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002377 }
2378
2379 end = p + len;
2380
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002381 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2382 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2383
2384 if( version != 0 )
2385 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2386
2387 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2388 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2389
2390 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2391 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2392
2393 if( len < 1 )
2394 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2395 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2396
2397 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002398 return( ret );
2399
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002400#if defined(POLARSSL_RSA_C)
2401 if( pk_alg == POLARSSL_PK_RSA )
2402 {
2403 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2404 {
2405 pk_free( pk );
2406 return( ret );
2407 }
2408 } else
2409#endif /* POLARSSL_RSA_C */
2410#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002411 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2412 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002413 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2414 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2415 {
2416 pk_free( pk );
2417 return( ret );
2418 }
2419 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002420#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002421 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2422
2423 return 0;
2424}
2425
2426/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002427 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002428 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002429static int x509parse_key_pkcs8_encrypted_der(
2430 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002431 const unsigned char *key, size_t keylen,
2432 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002433{
2434 int ret;
2435 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002436 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002437 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002438 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002439#if defined(POLARSSL_PKCS12_C)
2440 cipher_type_t cipher_alg;
2441 md_type_t md_alg;
2442#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002443
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002444 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002445
2446 p = (unsigned char *) key;
2447 end = p + keylen;
2448
Paul Bakker28144de2013-06-24 19:28:55 +02002449 if( pwdlen == 0 )
2450 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2451
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002452 /*
2453 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2454 *
2455 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2456 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2457 * encryptedData EncryptedData
2458 * }
2459 *
2460 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2461 *
2462 * EncryptedData ::= OCTET STRING
2463 *
2464 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2465 */
2466 if( ( ret = asn1_get_tag( &p, end, &len,
2467 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2468 {
2469 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2470 }
2471
2472 end = p + len;
2473
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002474 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002475 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002476
2477 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2478 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2479
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002480 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002481 return( POLARSSL_ERR_X509_INVALID_INPUT );
2482
2483 /*
2484 * Decrypt EncryptedData with appropriate PDE
2485 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002486#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002487 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002488 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002489 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002490 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002491 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002492 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002493 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2494 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2495
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002496 return( ret );
2497 }
2498 }
2499 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2500 {
2501 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2502 PKCS12_PBE_DECRYPT,
2503 pwd, pwdlen,
2504 p, len, buf ) ) != 0 )
2505 {
2506 return( ret );
2507 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002508
2509 // Best guess for password mismatch when using RC4. If first tag is
2510 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2511 //
2512 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2513 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002514 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002515 else
2516#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002517#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002518 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002519 {
2520 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2521 p, len, buf ) ) != 0 )
2522 {
2523 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2524 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2525
2526 return( ret );
2527 }
2528 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002529 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002530#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002531 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2532
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002533 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002534}
2535
2536/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002537 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002538 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002539int x509parse_key( pk_context *pk,
2540 const unsigned char *key, size_t keylen,
2541 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002542{
2543 int ret;
2544
2545#if defined(POLARSSL_PEM_C)
2546 size_t len;
2547 pem_context pem;
2548
2549 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002550
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002551#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002552 ret = pem_read_buffer( &pem,
2553 "-----BEGIN RSA PRIVATE KEY-----",
2554 "-----END RSA PRIVATE KEY-----",
2555 key, pwd, pwdlen, &len );
2556 if( ret == 0 )
2557 {
2558 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2559 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2560 pem.buf, pem.buflen ) ) != 0 )
2561 {
2562 pk_free( pk );
2563 }
2564
2565 pem_free( &pem );
2566 return( ret );
2567 }
2568 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2569 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2570 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2571 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2572 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2573 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002574#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002575
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002576#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002577 ret = pem_read_buffer( &pem,
2578 "-----BEGIN EC PRIVATE KEY-----",
2579 "-----END EC PRIVATE KEY-----",
2580 key, pwd, pwdlen, &len );
2581 if( ret == 0 )
2582 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002583 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2584 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2585 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002586 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002587 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002588 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002589
2590 pem_free( &pem );
2591 return( ret );
2592 }
2593 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2594 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2595 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2596 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2597 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2598 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002599#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002600
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002601 ret = pem_read_buffer( &pem,
2602 "-----BEGIN PRIVATE KEY-----",
2603 "-----END PRIVATE KEY-----",
2604 key, NULL, 0, &len );
2605 if( ret == 0 )
2606 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002607 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002608 pem.buf, pem.buflen ) ) != 0 )
2609 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002610 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002611 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002612
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002613 pem_free( &pem );
2614 return( ret );
2615 }
2616 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2617 return( ret );
2618
2619 ret = pem_read_buffer( &pem,
2620 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2621 "-----END ENCRYPTED PRIVATE KEY-----",
2622 key, NULL, 0, &len );
2623 if( ret == 0 )
2624 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002625 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002626 pem.buf, pem.buflen,
2627 pwd, pwdlen ) ) != 0 )
2628 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002629 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002630 }
2631
2632 pem_free( &pem );
2633 return( ret );
2634 }
2635 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2636 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002637#else
2638 ((void) pwd);
2639 ((void) pwdlen);
2640#endif /* POLARSSL_PEM_C */
2641
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002642 /*
2643 * At this point we only know it's not a PEM formatted key. Could be any
2644 * of the known DER encoded private key formats
2645 *
2646 * We try the different DER format parsers to see if one passes without
2647 * error
2648 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002649 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2650 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002651 {
2652 return( 0 );
2653 }
2654
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002655 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002656
2657 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2658 {
2659 return( ret );
2660 }
2661
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002662 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002663 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002664
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002665 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002666
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002667#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002668 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2669 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2670 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002671 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002672 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002673
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002674 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002675#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002676
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002677#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002678 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2679 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2680 {
2681 return( 0 );
2682 }
2683
2684 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002685#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002686
2687 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2688}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002689
2690/*
2691 * Parse a public key
2692 */
2693int x509parse_public_key( pk_context *ctx,
2694 const unsigned char *key, size_t keylen )
2695{
2696 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002697 unsigned char *p;
2698#if defined(POLARSSL_PEM_C)
2699 size_t len;
2700 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002701
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002702 pem_init( &pem );
2703 ret = pem_read_buffer( &pem,
2704 "-----BEGIN PUBLIC KEY-----",
2705 "-----END PUBLIC KEY-----",
2706 key, NULL, 0, &len );
2707
2708 if( ret == 0 )
2709 {
2710 /*
2711 * Was PEM encoded
2712 */
2713 key = pem.buf;
2714 keylen = pem.buflen;
2715 }
2716 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2717 {
2718 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002719 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002720 }
2721#endif
2722 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002723
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002724 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002725
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002726#if defined(POLARSSL_PEM_C)
2727 pem_free( &pem );
2728#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002729
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002730 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002731}
2732
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002733#if defined(POLARSSL_RSA_C)
2734/*
2735 * Parse a private RSA key
2736 */
2737int x509parse_key_rsa( rsa_context *rsa,
2738 const unsigned char *key, size_t keylen,
2739 const unsigned char *pwd, size_t pwdlen )
2740{
2741 pk_context pk;
2742
2743 pk_init( &pk );
2744 pk_wrap_rsa( &pk, rsa );
2745
2746 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
2747}
2748
2749/*
2750 * Parse a public RSA key
2751 */
2752int x509parse_public_key_rsa( rsa_context *rsa,
2753 const unsigned char *key, size_t keylen )
2754{
2755 pk_context pk;
2756
2757 pk_init( &pk );
2758 pk_wrap_rsa( &pk, rsa );
2759
2760 return( x509parse_public_key( &pk, key, keylen ) );
2761}
2762#endif /* POLARSSL_RSA_C */
2763
Paul Bakkereaa89f82011-04-04 21:36:15 +00002764#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002765/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002766 * Parse DHM parameters
2767 */
Paul Bakker23986e52011-04-24 08:57:21 +00002768int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002769{
Paul Bakker23986e52011-04-24 08:57:21 +00002770 int ret;
2771 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002772 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002773#if defined(POLARSSL_PEM_C)
2774 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002775
Paul Bakker96743fc2011-02-12 14:30:57 +00002776 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002777
Paul Bakker96743fc2011-02-12 14:30:57 +00002778 ret = pem_read_buffer( &pem,
2779 "-----BEGIN DH PARAMETERS-----",
2780 "-----END DH PARAMETERS-----",
2781 dhmin, NULL, 0, &dhminlen );
2782
2783 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002784 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002785 /*
2786 * Was PEM encoded
2787 */
2788 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002789 }
Paul Bakker00b28602013-06-24 13:02:41 +02002790 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002791 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002792 pem_free( &pem );
2793 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002794 }
2795
Paul Bakker96743fc2011-02-12 14:30:57 +00002796 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2797#else
2798 p = (unsigned char *) dhmin;
2799#endif
2800 end = p + dhminlen;
2801
Paul Bakker1b57b062011-01-06 15:48:19 +00002802 memset( dhm, 0, sizeof( dhm_context ) );
2803
Paul Bakker1b57b062011-01-06 15:48:19 +00002804 /*
2805 * DHParams ::= SEQUENCE {
2806 * prime INTEGER, -- P
2807 * generator INTEGER, -- g
2808 * }
2809 */
2810 if( ( ret = asn1_get_tag( &p, end, &len,
2811 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2812 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002813#if defined(POLARSSL_PEM_C)
2814 pem_free( &pem );
2815#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002816 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002817 }
2818
2819 end = p + len;
2820
2821 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2822 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2823 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002824#if defined(POLARSSL_PEM_C)
2825 pem_free( &pem );
2826#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002827 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002828 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002829 }
2830
2831 if( p != end )
2832 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002833#if defined(POLARSSL_PEM_C)
2834 pem_free( &pem );
2835#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002836 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002837 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002838 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2839 }
2840
Paul Bakker96743fc2011-02-12 14:30:57 +00002841#if defined(POLARSSL_PEM_C)
2842 pem_free( &pem );
2843#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002844
2845 return( 0 );
2846}
2847
Paul Bakker335db3f2011-04-25 15:28:35 +00002848#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002849/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002850 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002851 */
2852int x509parse_dhmfile( dhm_context *dhm, const char *path )
2853{
2854 int ret;
2855 size_t n;
2856 unsigned char *buf;
2857
Paul Bakker69e095c2011-12-10 21:55:01 +00002858 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2859 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002860
Paul Bakker27fdf462011-06-09 13:55:13 +00002861 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002862
2863 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002864 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002865
2866 return( ret );
2867}
Paul Bakker335db3f2011-04-25 15:28:35 +00002868#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002869#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002872#include <stdarg.h>
2873
2874#if !defined vsnprintf
2875#define vsnprintf _vsnprintf
2876#endif // vsnprintf
2877
2878/*
2879 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2880 * Result value is not size of buffer needed, but -1 if no fit is possible.
2881 *
2882 * This fuction tries to 'fix' this by at least suggesting enlarging the
2883 * size by 20.
2884 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002885static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002886{
2887 va_list ap;
2888 int res = -1;
2889
2890 va_start( ap, format );
2891
2892 res = vsnprintf( str, size, format, ap );
2893
2894 va_end( ap );
2895
2896 // No quick fix possible
2897 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002898 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002899
2900 return res;
2901}
2902
2903#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002904#endif
2905
Paul Bakkerd98030e2009-05-02 15:13:40 +00002906#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2907
2908#define SAFE_SNPRINTF() \
2909{ \
2910 if( ret == -1 ) \
2911 return( -1 ); \
2912 \
Paul Bakker23986e52011-04-24 08:57:21 +00002913 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002914 p[n - 1] = '\0'; \
2915 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2916 } \
2917 \
Paul Bakker23986e52011-04-24 08:57:21 +00002918 n -= (unsigned int) ret; \
2919 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002920}
2921
Paul Bakker5121ce52009-01-03 21:22:43 +00002922/*
2923 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002924 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002926int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927{
Paul Bakker23986e52011-04-24 08:57:21 +00002928 int ret;
2929 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002931 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002932 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 char s[128], *p;
2934
2935 memset( s, 0, sizeof( s ) );
2936
2937 name = dn;
2938 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002939 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
2941 while( name != NULL )
2942 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002943 if( !name->oid.p )
2944 {
2945 name = name->next;
2946 continue;
2947 }
2948
Paul Bakker74111d32011-01-15 16:57:55 +00002949 if( name != dn )
2950 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002951 ret = snprintf( p, n, ", " );
2952 SAFE_SNPRINTF();
2953 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
Paul Bakkerc70b9822013-04-07 22:00:46 +02002955 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
Paul Bakkerc70b9822013-04-07 22:00:46 +02002957 if( ret == 0 )
2958 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002960 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002961 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
2963 for( i = 0; i < name->val.len; i++ )
2964 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002965 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 break;
2967
2968 c = name->val.p[i];
2969 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2970 s[i] = '?';
2971 else s[i] = c;
2972 }
2973 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002974 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002975 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 name = name->next;
2977 }
2978
Paul Bakker23986e52011-04-24 08:57:21 +00002979 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980}
2981
2982/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002983 * Store the serial in printable form into buf; no more
2984 * than size characters will be written
2985 */
2986int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2987{
Paul Bakker23986e52011-04-24 08:57:21 +00002988 int ret;
2989 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002990 char *p;
2991
2992 p = buf;
2993 n = size;
2994
2995 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002996 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002997
2998 for( i = 0; i < nr; i++ )
2999 {
Paul Bakker93048802011-12-05 14:38:06 +00003000 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003001 continue;
3002
Paul Bakkerdd476992011-01-16 21:34:59 +00003003 ret = snprintf( p, n, "%02X%s",
3004 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3005 SAFE_SNPRINTF();
3006 }
3007
Paul Bakker03c7c252011-11-25 12:37:37 +00003008 if( nr != serial->len )
3009 {
3010 ret = snprintf( p, n, "...." );
3011 SAFE_SNPRINTF();
3012 }
3013
Paul Bakker23986e52011-04-24 08:57:21 +00003014 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003015}
3016
3017/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003018 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003019 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003020int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3021 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003022{
Paul Bakker23986e52011-04-24 08:57:21 +00003023 int ret;
3024 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003025 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003026 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
3028 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003029 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003033 SAFE_SNPRINTF();
3034 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Paul Bakkerdd476992011-01-16 21:34:59 +00003038 ret = x509parse_serial_gets( p, n, &crt->serial);
3039 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3042 SAFE_SNPRINTF();
3043 ret = x509parse_dn_gets( p, n, &crt->issuer );
3044 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakkerd98030e2009-05-02 15:13:40 +00003046 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3047 SAFE_SNPRINTF();
3048 ret = x509parse_dn_gets( p, n, &crt->subject );
3049 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3053 crt->valid_from.year, crt->valid_from.mon,
3054 crt->valid_from.day, crt->valid_from.hour,
3055 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003056 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakkerd98030e2009-05-02 15:13:40 +00003058 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3060 crt->valid_to.year, crt->valid_to.mon,
3061 crt->valid_to.day, crt->valid_to.hour,
3062 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003063 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003064
Paul Bakkerc70b9822013-04-07 22:00:46 +02003065 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003066 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Paul Bakkerc70b9822013-04-07 22:00:46 +02003068 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3069 if( ret != 0 )
3070 ret = snprintf( p, n, "???" );
3071 else
3072 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003073 SAFE_SNPRINTF();
3074
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003075#if defined(POLARSSL_RSA_C)
3076 if( crt->pk.type == POLARSSL_PK_RSA )
3077 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3078 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3079 else
3080#endif /* POLARSSL_RSA_C */
3081#if defined(POLARSSL_ECP_C)
3082 if( crt->pk.type == POLARSSL_PK_ECKEY ||
3083 crt->pk.type == POLARSSL_PK_ECKEY_DH )
3084 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3085 (int) pk_ec( crt->pk )->grp.pbits );
3086 else
3087#endif /* POLARSSL_ECP_C */
3088 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
Paul Bakkerd98030e2009-05-02 15:13:40 +00003089 SAFE_SNPRINTF();
3090
Paul Bakker23986e52011-04-24 08:57:21 +00003091 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003092}
3093
Paul Bakker74111d32011-01-15 16:57:55 +00003094/*
3095 * Return an informational string describing the given OID
3096 */
3097const char *x509_oid_get_description( x509_buf *oid )
3098{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003099 const char *desc = NULL;
3100 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003101
Paul Bakkerc70b9822013-04-07 22:00:46 +02003102 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003103
Paul Bakkerc70b9822013-04-07 22:00:46 +02003104 if( ret != 0 )
3105 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003106
Paul Bakkerc70b9822013-04-07 22:00:46 +02003107 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003108}
3109
3110/* Return the x.y.z.... style numeric string for the given OID */
3111int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3112{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003113 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003114}
3115
Paul Bakkerd98030e2009-05-02 15:13:40 +00003116/*
3117 * Return an informational string about the CRL.
3118 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003119int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3120 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003121{
Paul Bakker23986e52011-04-24 08:57:21 +00003122 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003123 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003124 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003125 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003126 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003127
3128 p = buf;
3129 n = size;
3130
3131 ret = snprintf( p, n, "%sCRL version : %d",
3132 prefix, crl->version );
3133 SAFE_SNPRINTF();
3134
3135 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3136 SAFE_SNPRINTF();
3137 ret = x509parse_dn_gets( p, n, &crl->issuer );
3138 SAFE_SNPRINTF();
3139
3140 ret = snprintf( p, n, "\n%sthis update : " \
3141 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3142 crl->this_update.year, crl->this_update.mon,
3143 crl->this_update.day, crl->this_update.hour,
3144 crl->this_update.min, crl->this_update.sec );
3145 SAFE_SNPRINTF();
3146
3147 ret = snprintf( p, n, "\n%snext update : " \
3148 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3149 crl->next_update.year, crl->next_update.mon,
3150 crl->next_update.day, crl->next_update.hour,
3151 crl->next_update.min, crl->next_update.sec );
3152 SAFE_SNPRINTF();
3153
3154 entry = &crl->entry;
3155
3156 ret = snprintf( p, n, "\n%sRevoked certificates:",
3157 prefix );
3158 SAFE_SNPRINTF();
3159
Paul Bakker9be19372009-07-27 20:21:53 +00003160 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003161 {
3162 ret = snprintf( p, n, "\n%sserial number: ",
3163 prefix );
3164 SAFE_SNPRINTF();
3165
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003166 ret = x509parse_serial_gets( p, n, &entry->serial);
3167 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003168
Paul Bakkerd98030e2009-05-02 15:13:40 +00003169 ret = snprintf( p, n, " revocation date: " \
3170 "%04d-%02d-%02d %02d:%02d:%02d",
3171 entry->revocation_date.year, entry->revocation_date.mon,
3172 entry->revocation_date.day, entry->revocation_date.hour,
3173 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003174 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003175
3176 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 }
3178
Paul Bakkerc70b9822013-04-07 22:00:46 +02003179 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003180 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakkerc70b9822013-04-07 22:00:46 +02003182 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3183 if( ret != 0 )
3184 ret = snprintf( p, n, "???" );
3185 else
3186 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003187 SAFE_SNPRINTF();
3188
Paul Bakker1e27bb22009-07-19 20:25:25 +00003189 ret = snprintf( p, n, "\n" );
3190 SAFE_SNPRINTF();
3191
Paul Bakker23986e52011-04-24 08:57:21 +00003192 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003193}
3194
3195/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003196 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003198#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003199int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200{
Paul Bakkercce9d772011-11-18 14:26:47 +00003201 int year, mon, day;
3202 int hour, min, sec;
3203
3204#if defined(_WIN32)
3205 SYSTEMTIME st;
3206
3207 GetLocalTime(&st);
3208
3209 year = st.wYear;
3210 mon = st.wMonth;
3211 day = st.wDay;
3212 hour = st.wHour;
3213 min = st.wMinute;
3214 sec = st.wSecond;
3215#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003216 struct tm *lt;
3217 time_t tt;
3218
3219 tt = time( NULL );
3220 lt = localtime( &tt );
3221
Paul Bakkercce9d772011-11-18 14:26:47 +00003222 year = lt->tm_year + 1900;
3223 mon = lt->tm_mon + 1;
3224 day = lt->tm_mday;
3225 hour = lt->tm_hour;
3226 min = lt->tm_min;
3227 sec = lt->tm_sec;
3228#endif
3229
3230 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003231 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232
Paul Bakkercce9d772011-11-18 14:26:47 +00003233 if( year == to->year &&
3234 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003235 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Paul Bakkercce9d772011-11-18 14:26:47 +00003237 if( year == to->year &&
3238 mon == to->mon &&
3239 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003240 return( 1 );
3241
Paul Bakkercce9d772011-11-18 14:26:47 +00003242 if( year == to->year &&
3243 mon == to->mon &&
3244 day == to->day &&
3245 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003246 return( 1 );
3247
Paul Bakkercce9d772011-11-18 14:26:47 +00003248 if( year == to->year &&
3249 mon == to->mon &&
3250 day == to->day &&
3251 hour == to->hour &&
3252 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003253 return( 1 );
3254
Paul Bakkercce9d772011-11-18 14:26:47 +00003255 if( year == to->year &&
3256 mon == to->mon &&
3257 day == to->day &&
3258 hour == to->hour &&
3259 min == to->min &&
3260 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003261 return( 1 );
3262
Paul Bakker40ea7de2009-05-03 10:18:48 +00003263 return( 0 );
3264}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003265#else /* POLARSSL_HAVE_TIME */
3266int x509parse_time_expired( const x509_time *to )
3267{
3268 ((void) to);
3269 return( 0 );
3270}
3271#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003272
3273/*
3274 * Return 1 if the certificate is revoked, or 0 otherwise.
3275 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003276int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003277{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003278 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003279
3280 while( cur != NULL && cur->serial.len != 0 )
3281 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003282 if( crt->serial.len == cur->serial.len &&
3283 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003284 {
3285 if( x509parse_time_expired( &cur->revocation_date ) )
3286 return( 1 );
3287 }
3288
3289 cur = cur->next;
3290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
3292 return( 0 );
3293}
3294
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003295/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003296 * Check that the given certificate is valid accoring to the CRL.
3297 */
3298static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3299 x509_crl *crl_list)
3300{
3301 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003302 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3303 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003304
Paul Bakker915275b2012-09-28 07:10:55 +00003305 if( ca == NULL )
3306 return( flags );
3307
Paul Bakker76fd75a2011-01-16 21:12:10 +00003308 /*
3309 * TODO: What happens if no CRL is present?
3310 * Suggestion: Revocation state should be unknown if no CRL is present.
3311 * For backwards compatibility this is not yet implemented.
3312 */
3313
Paul Bakker915275b2012-09-28 07:10:55 +00003314 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003315 {
Paul Bakker915275b2012-09-28 07:10:55 +00003316 if( crl_list->version == 0 ||
3317 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003318 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3319 crl_list->issuer_raw.len ) != 0 )
3320 {
3321 crl_list = crl_list->next;
3322 continue;
3323 }
3324
3325 /*
3326 * Check if CRL is correctly signed by the trusted CA
3327 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003328 md_info = md_info_from_type( crl_list->sig_md );
3329 if( md_info == NULL )
3330 {
3331 /*
3332 * Cannot check 'unknown' hash
3333 */
3334 flags |= BADCRL_NOT_TRUSTED;
3335 break;
3336 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003337
Paul Bakkerc70b9822013-04-07 22:00:46 +02003338 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003339
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003340 /* EC NOT IMPLEMENTED YET */
3341 if( ca->pk.type != POLARSSL_PK_RSA )
3342 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3343
3344 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003345 0, hash, crl_list->sig.p ) == 0 )
3346 {
3347 /*
3348 * CRL is not trusted
3349 */
3350 flags |= BADCRL_NOT_TRUSTED;
3351 break;
3352 }
3353
3354 /*
3355 * Check for validity of CRL (Do not drop out)
3356 */
3357 if( x509parse_time_expired( &crl_list->next_update ) )
3358 flags |= BADCRL_EXPIRED;
3359
3360 /*
3361 * Check if certificate is revoked
3362 */
3363 if( x509parse_revoked(crt, crl_list) )
3364 {
3365 flags |= BADCERT_REVOKED;
3366 break;
3367 }
3368
3369 crl_list = crl_list->next;
3370 }
3371 return flags;
3372}
3373
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003374static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003375{
3376 size_t i;
3377 size_t cn_idx = 0;
3378
Paul Bakker57b12982012-02-11 17:38:38 +00003379 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003380 return( 0 );
3381
3382 for( i = 0; i < strlen( cn ); ++i )
3383 {
3384 if( cn[i] == '.' )
3385 {
3386 cn_idx = i;
3387 break;
3388 }
3389 }
3390
3391 if( cn_idx == 0 )
3392 return( 0 );
3393
Paul Bakker535e97d2012-08-23 10:49:55 +00003394 if( strlen( cn ) - cn_idx == name->len - 1 &&
3395 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003396 {
3397 return( 1 );
3398 }
3399
3400 return( 0 );
3401}
3402
Paul Bakker915275b2012-09-28 07:10:55 +00003403static int x509parse_verify_top(
3404 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003405 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003406 int (*f_vrfy)(void *, x509_cert *, int, int *),
3407 void *p_vrfy )
3408{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003409 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003410 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003411 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3412 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003413
3414 if( x509parse_time_expired( &child->valid_to ) )
3415 *flags |= BADCERT_EXPIRED;
3416
3417 /*
3418 * Child is the top of the chain. Check against the trust_ca list.
3419 */
3420 *flags |= BADCERT_NOT_TRUSTED;
3421
3422 while( trust_ca != NULL )
3423 {
3424 if( trust_ca->version == 0 ||
3425 child->issuer_raw.len != trust_ca->subject_raw.len ||
3426 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3427 child->issuer_raw.len ) != 0 )
3428 {
3429 trust_ca = trust_ca->next;
3430 continue;
3431 }
3432
Paul Bakker9a736322012-11-14 12:39:52 +00003433 /*
3434 * Reduce path_len to check against if top of the chain is
3435 * the same as the trusted CA
3436 */
3437 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3438 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3439 child->issuer_raw.len ) == 0 )
3440 {
3441 check_path_cnt--;
3442 }
3443
Paul Bakker915275b2012-09-28 07:10:55 +00003444 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003445 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003446 {
3447 trust_ca = trust_ca->next;
3448 continue;
3449 }
3450
Paul Bakkerc70b9822013-04-07 22:00:46 +02003451 md_info = md_info_from_type( child->sig_md );
3452 if( md_info == NULL )
3453 {
3454 /*
3455 * Cannot check 'unknown' hash
3456 */
3457 continue;
3458 }
Paul Bakker915275b2012-09-28 07:10:55 +00003459
Paul Bakkerc70b9822013-04-07 22:00:46 +02003460 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003461
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003462 /* EC NOT IMPLEMENTED YET */
3463 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3464 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3465
3466 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003467 0, hash, child->sig.p ) != 0 )
3468 {
3469 trust_ca = trust_ca->next;
3470 continue;
3471 }
3472
3473 /*
3474 * Top of chain is signed by a trusted CA
3475 */
3476 *flags &= ~BADCERT_NOT_TRUSTED;
3477 break;
3478 }
3479
Paul Bakker9a736322012-11-14 12:39:52 +00003480 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003481 * If top of chain is not the same as the trusted CA send a verify request
3482 * to the callback for any issues with validity and CRL presence for the
3483 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003484 */
3485 if( trust_ca != NULL &&
3486 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3487 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3488 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003489 {
3490 /* Check trusted CA's CRL for then chain's top crt */
3491 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3492
3493 if( x509parse_time_expired( &trust_ca->valid_to ) )
3494 ca_flags |= BADCERT_EXPIRED;
3495
Paul Bakker915275b2012-09-28 07:10:55 +00003496 if( NULL != f_vrfy )
3497 {
Paul Bakker9a736322012-11-14 12:39:52 +00003498 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003499 return( ret );
3500 }
3501 }
3502
3503 /* Call callback on top cert */
3504 if( NULL != f_vrfy )
3505 {
Paul Bakker9a736322012-11-14 12:39:52 +00003506 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003507 return( ret );
3508 }
3509
Paul Bakker915275b2012-09-28 07:10:55 +00003510 *flags |= ca_flags;
3511
3512 return( 0 );
3513}
3514
3515static int x509parse_verify_child(
3516 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003517 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003518 int (*f_vrfy)(void *, x509_cert *, int, int *),
3519 void *p_vrfy )
3520{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003521 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003522 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003523 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003524 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003525 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003526
3527 if( x509parse_time_expired( &child->valid_to ) )
3528 *flags |= BADCERT_EXPIRED;
3529
Paul Bakkerc70b9822013-04-07 22:00:46 +02003530 md_info = md_info_from_type( child->sig_md );
3531 if( md_info == NULL )
3532 {
3533 /*
3534 * Cannot check 'unknown' hash
3535 */
Paul Bakker915275b2012-09-28 07:10:55 +00003536 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003537 }
3538 else
3539 {
3540 md( md_info, child->tbs.p, child->tbs.len, hash );
3541
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003542 /* EC NOT IMPLEMENTED YET */
3543 if( parent->pk.type != POLARSSL_PK_RSA )
3544 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3545
3546 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3547 0, hash, child->sig.p ) != 0 )
3548 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003549 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003550 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003551 }
3552
Paul Bakker915275b2012-09-28 07:10:55 +00003553 /* Check trusted CA's CRL for the given crt */
3554 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3555
3556 grandparent = parent->next;
3557
3558 while( grandparent != NULL )
3559 {
3560 if( grandparent->version == 0 ||
3561 grandparent->ca_istrue == 0 ||
3562 parent->issuer_raw.len != grandparent->subject_raw.len ||
3563 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3564 parent->issuer_raw.len ) != 0 )
3565 {
3566 grandparent = grandparent->next;
3567 continue;
3568 }
3569 break;
3570 }
3571
Paul Bakker915275b2012-09-28 07:10:55 +00003572 if( grandparent != NULL )
3573 {
3574 /*
3575 * Part of the chain
3576 */
Paul Bakker9a736322012-11-14 12:39:52 +00003577 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 +00003578 if( ret != 0 )
3579 return( ret );
3580 }
3581 else
3582 {
Paul Bakker9a736322012-11-14 12:39:52 +00003583 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 +00003584 if( ret != 0 )
3585 return( ret );
3586 }
3587
3588 /* child is verified to be a child of the parent, call verify callback */
3589 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003590 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003591 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003592
3593 *flags |= parent_flags;
3594
3595 return( 0 );
3596}
3597
Paul Bakker76fd75a2011-01-16 21:12:10 +00003598/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 * Verify the certificate validity
3600 */
3601int x509parse_verify( x509_cert *crt,
3602 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003603 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003604 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003605 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003606 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003607{
Paul Bakker23986e52011-04-24 08:57:21 +00003608 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003609 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003610 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003611 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003613 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Paul Bakker40ea7de2009-05-03 10:18:48 +00003615 *flags = 0;
3616
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 if( cn != NULL )
3618 {
3619 name = &crt->subject;
3620 cn_len = strlen( cn );
3621
Paul Bakker4d2c1242012-05-10 14:12:46 +00003622 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003624 cur = &crt->subject_alt_names;
3625
3626 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003627 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003628 if( cur->buf.len == cn_len &&
3629 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003630 break;
3631
Paul Bakker535e97d2012-08-23 10:49:55 +00003632 if( cur->buf.len > 2 &&
3633 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003634 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003635 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Paul Bakker4d2c1242012-05-10 14:12:46 +00003637 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003638 }
3639
3640 if( cur == NULL )
3641 *flags |= BADCERT_CN_MISMATCH;
3642 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003643 else
3644 {
3645 while( name != NULL )
3646 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003647 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003648 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003649 if( name->val.len == cn_len &&
3650 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003651 break;
3652
Paul Bakker535e97d2012-08-23 10:49:55 +00003653 if( name->val.len > 2 &&
3654 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003655 x509_wildcard_verify( cn, &name->val ) )
3656 break;
3657 }
3658
3659 name = name->next;
3660 }
3661
3662 if( name == NULL )
3663 *flags |= BADCERT_CN_MISMATCH;
3664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 }
3666
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003668 * Iterate upwards in the given cert chain, to find our crt parent.
3669 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003671 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003672
Paul Bakker76fd75a2011-01-16 21:12:10 +00003673 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003675 if( parent->ca_istrue == 0 ||
3676 crt->issuer_raw.len != parent->subject_raw.len ||
3677 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 crt->issuer_raw.len ) != 0 )
3679 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003680 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 continue;
3682 }
Paul Bakker915275b2012-09-28 07:10:55 +00003683 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 }
3685
Paul Bakker915275b2012-09-28 07:10:55 +00003686 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 {
Paul Bakker915275b2012-09-28 07:10:55 +00003688 /*
3689 * Part of the chain
3690 */
Paul Bakker9a736322012-11-14 12:39:52 +00003691 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003692 if( ret != 0 )
3693 return( ret );
3694 }
3695 else
Paul Bakker74111d32011-01-15 16:57:55 +00003696 {
Paul Bakker9a736322012-11-14 12:39:52 +00003697 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003698 if( ret != 0 )
3699 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003700 }
Paul Bakker915275b2012-09-28 07:10:55 +00003701
3702 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003703 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003704
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 return( 0 );
3706}
3707
3708/*
3709 * Unallocate all certificate data
3710 */
3711void x509_free( x509_cert *crt )
3712{
3713 x509_cert *cert_cur = crt;
3714 x509_cert *cert_prv;
3715 x509_name *name_cur;
3716 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003717 x509_sequence *seq_cur;
3718 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
3720 if( crt == NULL )
3721 return;
3722
3723 do
3724 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003725 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
3727 name_cur = cert_cur->issuer.next;
3728 while( name_cur != NULL )
3729 {
3730 name_prv = name_cur;
3731 name_cur = name_cur->next;
3732 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003733 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 }
3735
3736 name_cur = cert_cur->subject.next;
3737 while( name_cur != NULL )
3738 {
3739 name_prv = name_cur;
3740 name_cur = name_cur->next;
3741 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003742 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 }
3744
Paul Bakker74111d32011-01-15 16:57:55 +00003745 seq_cur = cert_cur->ext_key_usage.next;
3746 while( seq_cur != NULL )
3747 {
3748 seq_prv = seq_cur;
3749 seq_cur = seq_cur->next;
3750 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003751 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003752 }
3753
Paul Bakker8afa70d2012-02-11 18:42:45 +00003754 seq_cur = cert_cur->subject_alt_names.next;
3755 while( seq_cur != NULL )
3756 {
3757 seq_prv = seq_cur;
3758 seq_cur = seq_cur->next;
3759 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003760 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003761 }
3762
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 if( cert_cur->raw.p != NULL )
3764 {
3765 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003766 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
3769 cert_cur = cert_cur->next;
3770 }
3771 while( cert_cur != NULL );
3772
3773 cert_cur = crt;
3774 do
3775 {
3776 cert_prv = cert_cur;
3777 cert_cur = cert_cur->next;
3778
3779 memset( cert_prv, 0, sizeof( x509_cert ) );
3780 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003781 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 }
3783 while( cert_cur != NULL );
3784}
3785
Paul Bakkerd98030e2009-05-02 15:13:40 +00003786/*
3787 * Unallocate all CRL data
3788 */
3789void x509_crl_free( x509_crl *crl )
3790{
3791 x509_crl *crl_cur = crl;
3792 x509_crl *crl_prv;
3793 x509_name *name_cur;
3794 x509_name *name_prv;
3795 x509_crl_entry *entry_cur;
3796 x509_crl_entry *entry_prv;
3797
3798 if( crl == NULL )
3799 return;
3800
3801 do
3802 {
3803 name_cur = crl_cur->issuer.next;
3804 while( name_cur != NULL )
3805 {
3806 name_prv = name_cur;
3807 name_cur = name_cur->next;
3808 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003809 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003810 }
3811
3812 entry_cur = crl_cur->entry.next;
3813 while( entry_cur != NULL )
3814 {
3815 entry_prv = entry_cur;
3816 entry_cur = entry_cur->next;
3817 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003818 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003819 }
3820
3821 if( crl_cur->raw.p != NULL )
3822 {
3823 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003824 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003825 }
3826
3827 crl_cur = crl_cur->next;
3828 }
3829 while( crl_cur != NULL );
3830
3831 crl_cur = crl;
3832 do
3833 {
3834 crl_prv = crl_cur;
3835 crl_cur = crl_cur->next;
3836
3837 memset( crl_prv, 0, sizeof( x509_crl ) );
3838 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003839 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003840 }
3841 while( crl_cur != NULL );
3842}
3843
Paul Bakker40e46942009-01-03 21:51:57 +00003844#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
Paul Bakker40e46942009-01-03 21:51:57 +00003846#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003847
3848/*
3849 * Checkup routine
3850 */
3851int x509_self_test( int verbose )
3852{
Paul Bakker5690efc2011-05-26 13:16:06 +00003853#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003854 int ret;
3855 int flags;
3856 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 x509_cert cacert;
3858 x509_cert clicert;
3859 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003860#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003861 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003862#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003863
3864 if( verbose != 0 )
3865 printf( " X.509 certificate load: " );
3866
3867 memset( &clicert, 0, sizeof( x509_cert ) );
3868
Paul Bakker3c2122f2013-06-24 19:03:14 +02003869 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003870 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 if( ret != 0 )
3872 {
3873 if( verbose != 0 )
3874 printf( "failed\n" );
3875
3876 return( ret );
3877 }
3878
3879 memset( &cacert, 0, sizeof( x509_cert ) );
3880
Paul Bakker3c2122f2013-06-24 19:03:14 +02003881 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003882 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003883 if( ret != 0 )
3884 {
3885 if( verbose != 0 )
3886 printf( "failed\n" );
3887
3888 return( ret );
3889 }
3890
3891 if( verbose != 0 )
3892 printf( "passed\n X.509 private key load: " );
3893
3894 i = strlen( test_ca_key );
3895 j = strlen( test_ca_pwd );
3896
Paul Bakker66b78b22011-03-25 14:22:50 +00003897 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3898
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003899 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003900 (const unsigned char *) test_ca_key, i,
3901 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 {
3903 if( verbose != 0 )
3904 printf( "failed\n" );
3905
3906 return( ret );
3907 }
3908
3909 if( verbose != 0 )
3910 printf( "passed\n X.509 signature verify: ");
3911
Paul Bakker23986e52011-04-24 08:57:21 +00003912 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 if( ret != 0 )
3914 {
Paul Bakker23986e52011-04-24 08:57:21 +00003915 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 if( verbose != 0 )
3917 printf( "failed\n" );
3918
3919 return( ret );
3920 }
3921
Paul Bakker5690efc2011-05-26 13:16:06 +00003922#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003924 printf( "passed\n X.509 DHM parameter load: " );
3925
3926 i = strlen( test_dhm_params );
3927 j = strlen( test_ca_pwd );
3928
Paul Bakker3c2122f2013-06-24 19:03:14 +02003929 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003930 {
3931 if( verbose != 0 )
3932 printf( "failed\n" );
3933
3934 return( ret );
3935 }
3936
3937 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003939#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
3941 x509_free( &cacert );
3942 x509_free( &clicert );
3943 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003944#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003945 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003946#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003947
3948 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003949#else
3950 ((void) verbose);
3951 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3952#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003953}
3954
3955#endif
3956
3957#endif