blob: 6d7d08df746fe5f78e8213a4e6354758e7ddb4d0 [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
521 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
522 return( ret );
523
524 rsa->len = mpi_size( &rsa->N );
525
526 return( 0 );
527}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200528#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200529
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200530#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200531/*
532 * EC public key is an EC point
533 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200534static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200535 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200536{
537 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200538
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200539 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200540 (const unsigned char *) *p, end - *p ) ) != 0 ||
541 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200542 {
543 ecp_keypair_free( key );
544 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200545 }
546
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200547 /*
548 * We know ecp_point_read_binary consumed all bytes
549 */
550 *p = (unsigned char *) end;
551
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200552 return( 0 );
553}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200554#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200555
556/*
557 * SubjectPublicKeyInfo ::= SEQUENCE {
558 * algorithm AlgorithmIdentifier,
559 * subjectPublicKey BIT STRING }
560 */
561static int x509_get_pubkey( unsigned char **p,
562 const unsigned char *end,
563 pk_context *pk )
564{
565 int ret;
566 size_t len;
567 x509_buf alg_params;
568 pk_type_t pk_alg = POLARSSL_PK_NONE;
569
570 if( ( ret = asn1_get_tag( p, end, &len,
571 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
572 {
573 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
574 }
575
576 end = *p + len;
577
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200578 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200579 return( ret );
580
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200581 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200582 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
583
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200584 if( *p + len != end )
585 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
586 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
587
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200588 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
589 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200590
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200591#if defined(POLARSSL_RSA_C)
592 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200593 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200594 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
595 } else
596#endif /* POLARSSL_RSA_C */
597#if defined(POLARSSL_ECP_C)
598 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
599 {
600 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
601 x509_get_ecpubkey( p, end, pk_ec( *pk ) );
602 } else
603#endif /* POLARSSL_ECP_C */
604 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200605
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200606 if( ret == 0 && *p != end )
607 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
608 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
609
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200610 if( ret != 0 )
611 pk_free( pk );
612
613 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200614}
615
Paul Bakker5121ce52009-01-03 21:22:43 +0000616static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000617 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 x509_buf *sig )
619{
Paul Bakker23986e52011-04-24 08:57:21 +0000620 int ret;
621 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Paul Bakker8afa70d2012-02-11 18:42:45 +0000623 if( ( end - *p ) < 1 )
624 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
625 POLARSSL_ERR_ASN1_OUT_OF_DATA );
626
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 sig->tag = **p;
628
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200629 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000630 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 sig->len = len;
633 sig->p = *p;
634
635 *p += len;
636
637 return( 0 );
638}
639
640/*
641 * X.509 v2/v3 unique identifier (not parsed)
642 */
643static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000644 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 x509_buf *uid, int n )
646{
647 int ret;
648
649 if( *p == end )
650 return( 0 );
651
652 uid->tag = **p;
653
654 if( ( ret = asn1_get_tag( p, end, &uid->len,
655 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
656 {
Paul Bakker40e46942009-01-03 21:51:57 +0000657 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 return( 0 );
659
660 return( ret );
661 }
662
663 uid->p = *p;
664 *p += uid->len;
665
666 return( 0 );
667}
668
669/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000670 * X.509 Extensions (No parsing of extensions, pointer should
671 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
673static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000674 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000675 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676{
Paul Bakker23986e52011-04-24 08:57:21 +0000677 int ret;
678 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680 if( *p == end )
681 return( 0 );
682
683 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000684
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000686 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689 ext->p = *p;
690 end = *p + ext->len;
691
692 /*
693 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
694 *
695 * Extension ::= SEQUENCE {
696 * extnID OBJECT IDENTIFIER,
697 * critical BOOLEAN DEFAULT FALSE,
698 * extnValue OCTET STRING }
699 */
700 if( ( ret = asn1_get_tag( p, end, &len,
701 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000702 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000705 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000706 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
Paul Bakkerd98030e2009-05-02 15:13:40 +0000708 return( 0 );
709}
710
711/*
712 * X.509 CRL v2 extensions (no extensions parsed yet.)
713 */
714static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000715 const unsigned char *end,
716 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000717{
Paul Bakker23986e52011-04-24 08:57:21 +0000718 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000719 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000720
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000721 /* Get explicit tag */
722 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000723 {
724 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
725 return( 0 );
726
727 return( ret );
728 }
729
730 while( *p < end )
731 {
732 if( ( ret = asn1_get_tag( p, end, &len,
733 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000734 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000735
736 *p += len;
737 }
738
739 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000740 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000741 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
742
743 return( 0 );
744}
745
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000746/*
747 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
748 */
749static int x509_get_crl_entry_ext( unsigned char **p,
750 const unsigned char *end,
751 x509_buf *ext )
752{
753 int ret;
754 size_t len = 0;
755
756 /* OPTIONAL */
757 if (end <= *p)
758 return( 0 );
759
760 ext->tag = **p;
761 ext->p = *p;
762
763 /*
764 * Get CRL-entry extension sequence header
765 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
766 */
767 if( ( ret = asn1_get_tag( p, end, &ext->len,
768 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
769 {
770 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
771 {
772 ext->p = NULL;
773 return( 0 );
774 }
775 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
776 }
777
778 end = *p + ext->len;
779
780 if( end != *p + ext->len )
781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
782 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
783
784 while( *p < end )
785 {
786 if( ( ret = asn1_get_tag( p, end, &len,
787 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
788 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
789
790 *p += len;
791 }
792
793 if( *p != end )
794 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
795 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
796
797 return( 0 );
798}
799
Paul Bakker74111d32011-01-15 16:57:55 +0000800static int x509_get_basic_constraints( unsigned char **p,
801 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000802 int *ca_istrue,
803 int *max_pathlen )
804{
Paul Bakker23986e52011-04-24 08:57:21 +0000805 int ret;
806 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000807
808 /*
809 * BasicConstraints ::= SEQUENCE {
810 * cA BOOLEAN DEFAULT FALSE,
811 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
812 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000813 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000814 *max_pathlen = 0; /* endless */
815
816 if( ( ret = asn1_get_tag( p, end, &len,
817 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000818 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000819
820 if( *p == end )
821 return 0;
822
Paul Bakker3cccddb2011-01-16 21:46:31 +0000823 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000824 {
825 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000826 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000827
828 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000829 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000830
Paul Bakker3cccddb2011-01-16 21:46:31 +0000831 if( *ca_istrue != 0 )
832 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000833 }
834
835 if( *p == end )
836 return 0;
837
838 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000840
841 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000842 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000843 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
844
845 (*max_pathlen)++;
846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 return 0;
848}
849
850static int x509_get_ns_cert_type( unsigned char **p,
851 const unsigned char *end,
852 unsigned char *ns_cert_type)
853{
854 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000855 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000856
857 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000858 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000859
860 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000862 POLARSSL_ERR_ASN1_INVALID_LENGTH );
863
864 /* Get actual bitstring */
865 *ns_cert_type = *bs.p;
866 return 0;
867}
868
869static int x509_get_key_usage( unsigned char **p,
870 const unsigned char *end,
871 unsigned char *key_usage)
872{
873 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000874 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000875
876 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000877 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000878
Paul Bakker94a67962012-08-23 13:03:52 +0000879 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000880 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000881 POLARSSL_ERR_ASN1_INVALID_LENGTH );
882
883 /* Get actual bitstring */
884 *key_usage = *bs.p;
885 return 0;
886}
887
Paul Bakkerd98030e2009-05-02 15:13:40 +0000888/*
Paul Bakker74111d32011-01-15 16:57:55 +0000889 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
890 *
891 * KeyPurposeId ::= OBJECT IDENTIFIER
892 */
893static int x509_get_ext_key_usage( unsigned char **p,
894 const unsigned char *end,
895 x509_sequence *ext_key_usage)
896{
897 int ret;
898
899 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000900 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000901
902 /* Sequence length must be >= 1 */
903 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000905 POLARSSL_ERR_ASN1_INVALID_LENGTH );
906
907 return 0;
908}
909
910/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000911 * SubjectAltName ::= GeneralNames
912 *
913 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
914 *
915 * GeneralName ::= CHOICE {
916 * otherName [0] OtherName,
917 * rfc822Name [1] IA5String,
918 * dNSName [2] IA5String,
919 * x400Address [3] ORAddress,
920 * directoryName [4] Name,
921 * ediPartyName [5] EDIPartyName,
922 * uniformResourceIdentifier [6] IA5String,
923 * iPAddress [7] OCTET STRING,
924 * registeredID [8] OBJECT IDENTIFIER }
925 *
926 * OtherName ::= SEQUENCE {
927 * type-id OBJECT IDENTIFIER,
928 * value [0] EXPLICIT ANY DEFINED BY type-id }
929 *
930 * EDIPartyName ::= SEQUENCE {
931 * nameAssigner [0] DirectoryString OPTIONAL,
932 * partyName [1] DirectoryString }
933 *
934 * NOTE: PolarSSL only parses and uses dNSName at this point.
935 */
936static int x509_get_subject_alt_name( unsigned char **p,
937 const unsigned char *end,
938 x509_sequence *subject_alt_name )
939{
940 int ret;
941 size_t len, tag_len;
942 asn1_buf *buf;
943 unsigned char tag;
944 asn1_sequence *cur = subject_alt_name;
945
946 /* Get main sequence tag */
947 if( ( ret = asn1_get_tag( p, end, &len,
948 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
949 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
950
951 if( *p + len != end )
952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
953 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
954
955 while( *p < end )
956 {
957 if( ( end - *p ) < 1 )
958 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
959 POLARSSL_ERR_ASN1_OUT_OF_DATA );
960
961 tag = **p;
962 (*p)++;
963 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
964 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
965
966 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
968 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
969
970 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
971 {
972 *p += tag_len;
973 continue;
974 }
975
976 buf = &(cur->buf);
977 buf->tag = tag;
978 buf->p = *p;
979 buf->len = tag_len;
980 *p += buf->len;
981
982 /* Allocate and assign next pointer */
983 if (*p < end)
984 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200985 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000986 sizeof( asn1_sequence ) );
987
988 if( cur->next == NULL )
989 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
990 POLARSSL_ERR_ASN1_MALLOC_FAILED );
991
Paul Bakker535e97d2012-08-23 10:49:55 +0000992 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000993 cur = cur->next;
994 }
995 }
996
997 /* Set final sequence entry's next pointer to NULL */
998 cur->next = NULL;
999
1000 if( *p != end )
1001 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1002 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1003
1004 return( 0 );
1005}
1006
1007/*
Paul Bakker74111d32011-01-15 16:57:55 +00001008 * X.509 v3 extensions
1009 *
1010 * TODO: Perform all of the basic constraints tests required by the RFC
1011 * TODO: Set values for undetected extensions to a sane default?
1012 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001013 */
1014static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001015 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001016 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001017{
Paul Bakker23986e52011-04-24 08:57:21 +00001018 int ret;
1019 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001020 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001021
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001022 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001023 {
1024 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1025 return( 0 );
1026
1027 return( ret );
1028 }
1029
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 while( *p < end )
1031 {
Paul Bakker74111d32011-01-15 16:57:55 +00001032 /*
1033 * Extension ::= SEQUENCE {
1034 * extnID OBJECT IDENTIFIER,
1035 * critical BOOLEAN DEFAULT FALSE,
1036 * extnValue OCTET STRING }
1037 */
1038 x509_buf extn_oid = {0, 0, NULL};
1039 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001040 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001041
Paul Bakker5121ce52009-01-03 21:22:43 +00001042 if( ( ret = asn1_get_tag( p, end, &len,
1043 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001044 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001046 end_ext_data = *p + len;
1047
Paul Bakker74111d32011-01-15 16:57:55 +00001048 /* Get extension ID */
1049 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
Paul Bakker74111d32011-01-15 16:57:55 +00001051 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001052 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakker74111d32011-01-15 16:57:55 +00001054 extn_oid.p = *p;
1055 *p += extn_oid.len;
1056
1057 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001058 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001059 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1060
1061 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001062 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001063 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001064 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Paul Bakker74111d32011-01-15 16:57:55 +00001066 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001067 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001069 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001072
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001073 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001074 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001075 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Paul Bakker74111d32011-01-15 16:57:55 +00001077 /*
1078 * Detect supported extensions
1079 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001080 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1081
1082 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001083 {
1084 /* No parser found, skip extension */
1085 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Paul Bakker5c721f92011-07-27 16:51:09 +00001087#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001088 if( is_critical )
1089 {
1090 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001091 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001092 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1093 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001094#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095 continue;
1096 }
1097
1098 crt->ext_types |= ext_type;
1099
1100 switch( ext_type )
1101 {
1102 case EXT_BASIC_CONSTRAINTS:
1103 /* Parse basic constraints */
1104 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1105 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1106 return ( ret );
1107 break;
1108
1109 case EXT_KEY_USAGE:
1110 /* Parse key usage */
1111 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1112 &crt->key_usage ) ) != 0 )
1113 return ( ret );
1114 break;
1115
1116 case EXT_EXTENDED_KEY_USAGE:
1117 /* Parse extended key usage */
1118 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1119 &crt->ext_key_usage ) ) != 0 )
1120 return ( ret );
1121 break;
1122
1123 case EXT_SUBJECT_ALT_NAME:
1124 /* Parse subject alt name */
1125 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1126 &crt->subject_alt_names ) ) != 0 )
1127 return ( ret );
1128 break;
1129
1130 case EXT_NS_CERT_TYPE:
1131 /* Parse netscape certificate type */
1132 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1133 &crt->ns_cert_type ) ) != 0 )
1134 return ( ret );
1135 break;
1136
1137 default:
1138 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001139 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 }
1141
1142 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001143 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001144 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 return( 0 );
1147}
1148
1149/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001150 * X.509 CRL Entries
1151 */
1152static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001153 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154 x509_crl_entry *entry )
1155{
Paul Bakker23986e52011-04-24 08:57:21 +00001156 int ret;
1157 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 x509_crl_entry *cur_entry = entry;
1159
1160 if( *p == end )
1161 return( 0 );
1162
Paul Bakker9be19372009-07-27 20:21:53 +00001163 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001164 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1165 {
1166 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1167 return( 0 );
1168
1169 return( ret );
1170 }
1171
Paul Bakker9be19372009-07-27 20:21:53 +00001172 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001173
1174 while( *p < end )
1175 {
Paul Bakker23986e52011-04-24 08:57:21 +00001176 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001177 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001178
1179 if( ( ret = asn1_get_tag( p, end, &len2,
1180 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1181 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182 return( ret );
1183 }
1184
Paul Bakker9be19372009-07-27 20:21:53 +00001185 cur_entry->raw.tag = **p;
1186 cur_entry->raw.p = *p;
1187 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001188 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001189
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001190 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001191 return( ret );
1192
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001193 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001194 return( ret );
1195
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001196 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001197 return( ret );
1198
Paul Bakker74111d32011-01-15 16:57:55 +00001199 if ( *p < end )
1200 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001201 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001202
1203 if( cur_entry->next == NULL )
1204 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1205
Paul Bakkerd98030e2009-05-02 15:13:40 +00001206 cur_entry = cur_entry->next;
1207 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1208 }
1209 }
1210
1211 return( 0 );
1212}
1213
Paul Bakkerc70b9822013-04-07 22:00:46 +02001214static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1215 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001216{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001217 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001218
Paul Bakkerc70b9822013-04-07 22:00:46 +02001219 if( ret != 0 )
1220 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001221
Paul Bakkerc70b9822013-04-07 22:00:46 +02001222 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001223}
1224
Paul Bakkerd98030e2009-05-02 15:13:40 +00001225/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001226 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001228static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1229 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001230{
Paul Bakker23986e52011-04-24 08:57:21 +00001231 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001232 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001233 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Paul Bakker320a4b52009-03-28 18:52:39 +00001235 /*
1236 * Check for valid input
1237 */
1238 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001239 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001240
Paul Bakker6e339b52013-07-03 13:37:05 +02001241 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001242
1243 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001244 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001245
1246 memcpy( p, buf, buflen );
1247
1248 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
1250 crt->raw.p = p;
1251 crt->raw.len = len;
1252 end = p + len;
1253
1254 /*
1255 * Certificate ::= SEQUENCE {
1256 * tbsCertificate TBSCertificate,
1257 * signatureAlgorithm AlgorithmIdentifier,
1258 * signatureValue BIT STRING }
1259 */
1260 if( ( ret = asn1_get_tag( &p, end, &len,
1261 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1262 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001263 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001264 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
1266
Paul Bakkerb00ca422012-09-25 12:10:00 +00001267 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001269 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001270 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001271 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001273 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001274
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 /*
1276 * TBSCertificate ::= SEQUENCE {
1277 */
1278 crt->tbs.p = p;
1279
1280 if( ( ret = asn1_get_tag( &p, end, &len,
1281 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1282 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001283 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001284 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 }
1286
1287 end = p + len;
1288 crt->tbs.len = end - crt->tbs.p;
1289
1290 /*
1291 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1292 *
1293 * CertificateSerialNumber ::= INTEGER
1294 *
1295 * signature AlgorithmIdentifier
1296 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001297 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1298 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1299 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001301 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 return( ret );
1303 }
1304
1305 crt->version++;
1306
1307 if( crt->version > 3 )
1308 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001310 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 }
1312
Paul Bakkerc70b9822013-04-07 22:00:46 +02001313 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1314 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001317 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 }
1319
1320 /*
1321 * issuer Name
1322 */
1323 crt->issuer_raw.p = p;
1324
1325 if( ( ret = asn1_get_tag( &p, end, &len,
1326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1327 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001328 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001329 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 }
1331
1332 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337
1338 crt->issuer_raw.len = p - crt->issuer_raw.p;
1339
1340 /*
1341 * Validity ::= SEQUENCE {
1342 * notBefore Time,
1343 * notAfter Time }
1344 *
1345 */
1346 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1347 &crt->valid_to ) ) != 0 )
1348 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001349 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 return( ret );
1351 }
1352
1353 /*
1354 * subject Name
1355 */
1356 crt->subject_raw.p = p;
1357
1358 if( ( ret = asn1_get_tag( &p, end, &len,
1359 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1360 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001361 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001362 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
Paul Bakkercefb3962012-06-27 11:51:09 +00001365 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001367 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 return( ret );
1369 }
1370
1371 crt->subject_raw.len = p - crt->subject_raw.p;
1372
1373 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001374 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001376 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001378 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 return( ret );
1380 }
1381
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 /*
1383 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1384 * -- If present, version shall be v2 or v3
1385 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1386 * -- If present, version shall be v2 or v3
1387 * extensions [3] EXPLICIT Extensions OPTIONAL
1388 * -- If present, version shall be v3
1389 */
1390 if( crt->version == 2 || crt->version == 3 )
1391 {
1392 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1393 if( ret != 0 )
1394 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001395 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 return( ret );
1397 }
1398 }
1399
1400 if( crt->version == 2 || crt->version == 3 )
1401 {
1402 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1403 if( ret != 0 )
1404 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001405 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 return( ret );
1407 }
1408 }
1409
1410 if( crt->version == 3 )
1411 {
Paul Bakker74111d32011-01-15 16:57:55 +00001412 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 if( ret != 0 )
1414 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 return( ret );
1417 }
1418 }
1419
1420 if( p != end )
1421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001423 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001424 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426
Paul Bakkerb00ca422012-09-25 12:10:00 +00001427 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
1429 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001430 * }
1431 * -- end of TBSCertificate
1432 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 * signatureAlgorithm AlgorithmIdentifier,
1434 * signatureValue BIT STRING
1435 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001436 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001438 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 return( ret );
1440 }
1441
Paul Bakker535e97d2012-08-23 10:49:55 +00001442 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1443 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001445 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001446 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
1449 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1450 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001451 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 return( ret );
1453 }
1454
1455 if( p != end )
1456 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001457 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001458 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001459 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 }
1461
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001462 return( 0 );
1463}
1464
1465/*
Paul Bakker42c65812013-06-24 19:21:59 +02001466 * Parse one X.509 certificate in DER format from a buffer and add them to a
1467 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001468 */
Paul Bakker42c65812013-06-24 19:21:59 +02001469int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001470{
Paul Bakker42c65812013-06-24 19:21:59 +02001471 int ret;
1472 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001473
1474 /*
1475 * Check for valid input
1476 */
1477 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001478 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479
1480 while( crt->version != 0 && crt->next != NULL )
1481 {
1482 prev = crt;
1483 crt = crt->next;
1484 }
1485
1486 /*
1487 * Add new certificate on the end of the chain if needed.
1488 */
1489 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001490 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001491 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001492
Paul Bakker7d06ad22009-05-02 15:53:56 +00001493 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001494 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001495
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001496 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001497 crt = crt->next;
1498 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001500
Paul Bakker42c65812013-06-24 19:21:59 +02001501 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1502 {
1503 if( prev )
1504 prev->next = NULL;
1505
1506 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001507 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001508
1509 return( ret );
1510 }
1511
1512 return( 0 );
1513}
1514
1515/*
1516 * Parse one or more PEM certificates from a buffer and add them to the chained list
1517 */
1518int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1519{
1520 int ret, success = 0, first_error = 0, total_failed = 0;
1521 int buf_format = X509_FORMAT_DER;
1522
1523 /*
1524 * Check for valid input
1525 */
1526 if( chain == NULL || buf == NULL )
1527 return( POLARSSL_ERR_X509_INVALID_INPUT );
1528
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001529 /*
1530 * Determine buffer content. Buffer contains either one DER certificate or
1531 * one or more PEM certificates.
1532 */
1533#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001534 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001535 buf_format = X509_FORMAT_PEM;
1536#endif
1537
1538 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001539 return x509parse_crt_der( chain, buf, buflen );
1540
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001541#if defined(POLARSSL_PEM_C)
1542 if( buf_format == X509_FORMAT_PEM )
1543 {
1544 pem_context pem;
1545
1546 while( buflen > 0 )
1547 {
1548 size_t use_len;
1549 pem_init( &pem );
1550
1551 ret = pem_read_buffer( &pem,
1552 "-----BEGIN CERTIFICATE-----",
1553 "-----END CERTIFICATE-----",
1554 buf, NULL, 0, &use_len );
1555
1556 if( ret == 0 )
1557 {
1558 /*
1559 * Was PEM encoded
1560 */
1561 buflen -= use_len;
1562 buf += use_len;
1563 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001564 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1565 {
1566 return( ret );
1567 }
Paul Bakker00b28602013-06-24 13:02:41 +02001568 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001569 {
1570 pem_free( &pem );
1571
Paul Bakker5ed3b342013-06-24 19:05:46 +02001572 /*
1573 * PEM header and footer were found
1574 */
1575 buflen -= use_len;
1576 buf += use_len;
1577
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001578 if( first_error == 0 )
1579 first_error = ret;
1580
1581 continue;
1582 }
1583 else
1584 break;
1585
Paul Bakker42c65812013-06-24 19:21:59 +02001586 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001587
1588 pem_free( &pem );
1589
1590 if( ret != 0 )
1591 {
1592 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001593 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001594 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001595 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001596 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001597
1598 if( first_error == 0 )
1599 first_error = ret;
1600
Paul Bakker42c65812013-06-24 19:21:59 +02001601 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001602 continue;
1603 }
1604
1605 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606 }
1607 }
1608#endif
1609
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001611 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001612 else if( first_error )
1613 return( first_error );
1614 else
1615 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616}
1617
1618/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001619 * Parse one or more CRLs and add them to the chained list
1620 */
Paul Bakker23986e52011-04-24 08:57:21 +00001621int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001622{
Paul Bakker23986e52011-04-24 08:57:21 +00001623 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001624 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625 unsigned char *p, *end;
1626 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001627#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001628 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001629 pem_context pem;
1630#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631
1632 crl = chain;
1633
1634 /*
1635 * Check for valid input
1636 */
1637 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001638 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001639
1640 while( crl->version != 0 && crl->next != NULL )
1641 crl = crl->next;
1642
1643 /*
1644 * Add new CRL on the end of the chain if needed.
1645 */
1646 if ( crl->version != 0 && crl->next == NULL)
1647 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001648 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649
Paul Bakker7d06ad22009-05-02 15:53:56 +00001650 if( crl->next == NULL )
1651 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001653 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001654 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001655
Paul Bakker7d06ad22009-05-02 15:53:56 +00001656 crl = crl->next;
1657 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 }
1659
Paul Bakker96743fc2011-02-12 14:30:57 +00001660#if defined(POLARSSL_PEM_C)
1661 pem_init( &pem );
1662 ret = pem_read_buffer( &pem,
1663 "-----BEGIN X509 CRL-----",
1664 "-----END X509 CRL-----",
1665 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666
Paul Bakker96743fc2011-02-12 14:30:57 +00001667 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001669 /*
1670 * Was PEM encoded
1671 */
1672 buflen -= use_len;
1673 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001674
1675 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001676 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001677 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001678 p = pem.buf;
1679 pem.buf = NULL;
1680 len = pem.buflen;
1681 pem_free( &pem );
1682 }
Paul Bakker00b28602013-06-24 13:02:41 +02001683 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001684 {
1685 pem_free( &pem );
1686 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 }
1688 else
1689 {
1690 /*
1691 * nope, copy the raw DER data
1692 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001693 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001694
1695 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001696 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001697
1698 memcpy( p, buf, buflen );
1699
1700 buflen = 0;
1701 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001702#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001703 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001704
1705 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001706 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001707
1708 memcpy( p, buf, buflen );
1709
1710 buflen = 0;
1711#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712
1713 crl->raw.p = p;
1714 crl->raw.len = len;
1715 end = p + len;
1716
1717 /*
1718 * CertificateList ::= SEQUENCE {
1719 * tbsCertList TBSCertList,
1720 * signatureAlgorithm AlgorithmIdentifier,
1721 * signatureValue BIT STRING }
1722 */
1723 if( ( ret = asn1_get_tag( &p, end, &len,
1724 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1725 {
1726 x509_crl_free( crl );
1727 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1728 }
1729
Paul Bakker23986e52011-04-24 08:57:21 +00001730 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001731 {
1732 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001733 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1735 }
1736
1737 /*
1738 * TBSCertList ::= SEQUENCE {
1739 */
1740 crl->tbs.p = p;
1741
1742 if( ( ret = asn1_get_tag( &p, end, &len,
1743 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1744 {
1745 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001746 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001747 }
1748
1749 end = p + len;
1750 crl->tbs.len = end - crl->tbs.p;
1751
1752 /*
1753 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1754 * -- if present, MUST be v2
1755 *
1756 * signature AlgorithmIdentifier
1757 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001758 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001759 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760 {
1761 x509_crl_free( crl );
1762 return( ret );
1763 }
1764
1765 crl->version++;
1766
1767 if( crl->version > 2 )
1768 {
1769 x509_crl_free( crl );
1770 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1771 }
1772
Paul Bakkerc70b9822013-04-07 22:00:46 +02001773 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1774 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 {
1776 x509_crl_free( crl );
1777 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1778 }
1779
1780 /*
1781 * issuer Name
1782 */
1783 crl->issuer_raw.p = p;
1784
1785 if( ( ret = asn1_get_tag( &p, end, &len,
1786 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1787 {
1788 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001789 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 }
1791
1792 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1793 {
1794 x509_crl_free( crl );
1795 return( ret );
1796 }
1797
1798 crl->issuer_raw.len = p - crl->issuer_raw.p;
1799
1800 /*
1801 * thisUpdate Time
1802 * nextUpdate Time OPTIONAL
1803 */
Paul Bakker91200182010-02-18 21:26:15 +00001804 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805 {
1806 x509_crl_free( crl );
1807 return( ret );
1808 }
1809
Paul Bakker91200182010-02-18 21:26:15 +00001810 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001811 {
Paul Bakker9d781402011-05-09 16:17:09 +00001812 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001813 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001814 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001815 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001816 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817 x509_crl_free( crl );
1818 return( ret );
1819 }
1820 }
1821
1822 /*
1823 * revokedCertificates SEQUENCE OF SEQUENCE {
1824 * userCertificate CertificateSerialNumber,
1825 * revocationDate Time,
1826 * crlEntryExtensions Extensions OPTIONAL
1827 * -- if present, MUST be v2
1828 * } OPTIONAL
1829 */
1830 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1831 {
1832 x509_crl_free( crl );
1833 return( ret );
1834 }
1835
1836 /*
1837 * crlExtensions EXPLICIT Extensions OPTIONAL
1838 * -- if present, MUST be v2
1839 */
1840 if( crl->version == 2 )
1841 {
1842 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1843
1844 if( ret != 0 )
1845 {
1846 x509_crl_free( crl );
1847 return( ret );
1848 }
1849 }
1850
1851 if( p != end )
1852 {
1853 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001854 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001855 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1856 }
1857
1858 end = crl->raw.p + crl->raw.len;
1859
1860 /*
1861 * signatureAlgorithm AlgorithmIdentifier,
1862 * signatureValue BIT STRING
1863 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001864 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865 {
1866 x509_crl_free( crl );
1867 return( ret );
1868 }
1869
Paul Bakker535e97d2012-08-23 10:49:55 +00001870 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1871 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001872 {
1873 x509_crl_free( crl );
1874 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1875 }
1876
1877 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1878 {
1879 x509_crl_free( crl );
1880 return( ret );
1881 }
1882
1883 if( p != end )
1884 {
1885 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001886 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001887 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1888 }
1889
1890 if( buflen > 0 )
1891 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001892 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001893
Paul Bakker7d06ad22009-05-02 15:53:56 +00001894 if( crl->next == NULL )
1895 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001896 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001897 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001898 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899
Paul Bakker7d06ad22009-05-02 15:53:56 +00001900 crl = crl->next;
1901 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902
1903 return( x509parse_crl( crl, buf, buflen ) );
1904 }
1905
1906 return( 0 );
1907}
1908
Paul Bakker335db3f2011-04-25 15:28:35 +00001909#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001910/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001911 * Load all data from a file into a given buffer.
1912 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001913static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001914{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001915 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001916
Paul Bakkerd98030e2009-05-02 15:13:40 +00001917 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001918 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001919
Paul Bakkerd98030e2009-05-02 15:13:40 +00001920 fseek( f, 0, SEEK_END );
1921 *n = (size_t) ftell( f );
1922 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001923
Paul Bakker6e339b52013-07-03 13:37:05 +02001924 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001925 {
1926 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001927 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001928 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001929
Paul Bakkerd98030e2009-05-02 15:13:40 +00001930 if( fread( *buf, 1, *n, f ) != *n )
1931 {
1932 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001933 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001934 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001935 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001936
Paul Bakkerd98030e2009-05-02 15:13:40 +00001937 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001938
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001940
Paul Bakkerd98030e2009-05-02 15:13:40 +00001941 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001942}
1943
1944/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 * Load one or more certificates and add them to the chained list
1946 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001947int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001948{
1949 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 size_t n;
1951 unsigned char *buf;
1952
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001953 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001954 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Paul Bakker69e095c2011-12-10 21:55:01 +00001956 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
1958 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001959 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001960
1961 return( ret );
1962}
1963
Paul Bakker8d914582012-06-04 12:46:42 +00001964int x509parse_crtpath( x509_cert *chain, const char *path )
1965{
1966 int ret = 0;
1967#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001968 int w_ret;
1969 WCHAR szDir[MAX_PATH];
1970 char filename[MAX_PATH];
1971 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001972 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001973
Paul Bakker97872ac2012-11-02 12:53:26 +00001974 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001975 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001976
1977 if( len > MAX_PATH - 3 )
1978 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001979
Paul Bakker3338b792012-10-01 21:13:10 +00001980 memset( szDir, 0, sizeof(szDir) );
1981 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001982 memcpy( filename, path, len );
1983 filename[len++] = '\\';
1984 p = filename + len;
1985 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001986
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001987 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001988
Paul Bakker97872ac2012-11-02 12:53:26 +00001989 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001990 if (hFind == INVALID_HANDLE_VALUE)
1991 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1992
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001993 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001994 do
1995 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001996 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001997
Paul Bakkere4791f32012-06-04 21:29:15 +00001998 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001999 continue;
2000
Paul Bakker3338b792012-10-01 21:13:10 +00002001 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2002 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002003 p, len - 1,
2004 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002005
Paul Bakker3338b792012-10-01 21:13:10 +00002006 w_ret = x509parse_crtfile( chain, filename );
2007 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002008 ret++;
2009 else
2010 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002011 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002012 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002013
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002014 if (GetLastError() != ERROR_NO_MORE_FILES)
2015 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002016
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002017cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002018 FindClose( hFind );
2019#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002020 int t_ret, i;
2021 struct stat sb;
2022 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002023 char entry_name[255];
2024 DIR *dir = opendir( path );
2025
2026 if( dir == NULL)
2027 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2028
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002029 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002030 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002031 if( result == NULL )
2032 break;
2033
2034 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2035
2036 i = stat( entry_name, &sb );
2037
2038 if( i == -1 )
2039 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2040
2041 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002042 continue;
2043
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002044 // Ignore parse errors
2045 //
Paul Bakker8d914582012-06-04 12:46:42 +00002046 t_ret = x509parse_crtfile( chain, entry_name );
2047 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002048 ret++;
2049 else
2050 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002051 }
2052 closedir( dir );
2053#endif
2054
2055 return( ret );
2056}
2057
Paul Bakkerd98030e2009-05-02 15:13:40 +00002058/*
2059 * Load one or more CRLs and add them to the chained list
2060 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002061int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002062{
2063 int ret;
2064 size_t n;
2065 unsigned char *buf;
2066
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002067 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002068 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002069
Paul Bakker27fdf462011-06-09 13:55:13 +00002070 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002071
2072 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002073 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002074
2075 return( ret );
2076}
2077
Paul Bakker5121ce52009-01-03 21:22:43 +00002078/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002079 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002080 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002081int x509parse_keyfile( pk_context *ctx,
2082 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002083{
2084 int ret;
2085 size_t n;
2086 unsigned char *buf;
2087
2088 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2089 return( ret );
2090
2091 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002092 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002093 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002094 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002095 (const unsigned char *) pwd, strlen( pwd ) );
2096
2097 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002098 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002099
2100 return( ret );
2101}
2102
2103/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002104 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002105 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002106int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002107{
2108 int ret;
2109 size_t n;
2110 unsigned char *buf;
2111
2112 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2113 return( ret );
2114
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002115 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002116
2117 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002118 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002119
2120 return( ret );
2121}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002122
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002123#if defined(POLARSSL_RSA_C)
2124/*
2125 * Load and parse a private RSA key
2126 */
2127int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2128{
2129 pk_context pk;
2130
2131 pk_init( &pk );
2132 pk_wrap_rsa( &pk, rsa );
2133
2134 return( x509parse_keyfile( &pk, path, pwd ) );
2135}
2136
2137/*
2138 * Load and parse a public RSA key
2139 */
2140int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2141{
2142 pk_context pk;
2143
2144 pk_init( &pk );
2145 pk_wrap_rsa( &pk, rsa );
2146
2147 return( x509parse_public_keyfile( &pk, path ) );
2148}
2149#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002150#endif /* POLARSSL_FS_IO */
2151
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002152#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002153/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002154 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002156static int x509parse_key_pkcs1_der( rsa_context *rsa,
2157 const unsigned char *key,
2158 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159{
Paul Bakker23986e52011-04-24 08:57:21 +00002160 int ret;
2161 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002163
Paul Bakker96743fc2011-02-12 14:30:57 +00002164 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002165 end = p + keylen;
2166
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002168 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002169 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 * RSAPrivateKey ::= SEQUENCE {
2171 * version Version,
2172 * modulus INTEGER, -- n
2173 * publicExponent INTEGER, -- e
2174 * privateExponent INTEGER, -- d
2175 * prime1 INTEGER, -- p
2176 * prime2 INTEGER, -- q
2177 * exponent1 INTEGER, -- d mod (p-1)
2178 * exponent2 INTEGER, -- d mod (q-1)
2179 * coefficient INTEGER, -- (inverse of q) mod p
2180 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2181 * }
2182 */
2183 if( ( ret = asn1_get_tag( &p, end, &len,
2184 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2185 {
Paul Bakker9d781402011-05-09 16:17:09 +00002186 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
2188
2189 end = p + len;
2190
2191 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2192 {
Paul Bakker9d781402011-05-09 16:17:09 +00002193 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 }
2195
2196 if( rsa->ver != 0 )
2197 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002198 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
2201 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2202 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2203 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2204 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2205 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2206 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2207 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2208 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2209 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002211 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 }
2213
2214 rsa->len = mpi_size( &rsa->N );
2215
2216 if( p != end )
2217 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002219 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002220 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222
2223 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2224 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 rsa_free( rsa );
2226 return( ret );
2227 }
2228
Paul Bakkere2f50402013-06-24 19:00:59 +02002229 return( 0 );
2230}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002231#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002232
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002233#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002234/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002235 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002236 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002237static int x509parse_key_sec1_der( ecp_keypair *eck,
2238 const unsigned char *key,
2239 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002240{
2241 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002242 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002243 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002244 x509_buf params;
2245 unsigned char *p = (unsigned char *) key;
2246 unsigned char *end = p + keylen;
2247 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002248
2249 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002250 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002251 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002252 * ECPrivateKey ::= SEQUENCE {
2253 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2254 * privateKey OCTET STRING,
2255 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2256 * publicKey [1] BIT STRING OPTIONAL
2257 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002258 */
2259 if( ( ret = asn1_get_tag( &p, end, &len,
2260 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2261 {
2262 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2263 }
2264
2265 end = p + len;
2266
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002267 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002268 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2269
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002270 if( version != 1 )
2271 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002272
Paul Bakkere2f50402013-06-24 19:00:59 +02002273 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2274 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2275
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002276 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002277 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002278 ecp_keypair_free( eck );
2279 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2280 }
2281
2282 p += len;
2283
2284 /*
2285 * Is 'parameters' present?
2286 */
2287 if( ( ret = asn1_get_tag( &p, end, &len,
2288 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2289 {
2290 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2291 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2292 {
2293 ecp_keypair_free( eck );
2294 return( ret );
2295 }
2296 }
2297 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2298 {
2299 ecp_keypair_free( eck );
2300 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2301 }
2302
2303 /*
2304 * Is 'publickey' present?
2305 */
2306 if( ( ret = asn1_get_tag( &p, end, &len,
2307 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2308 {
2309 end2 = p + len;
2310
2311 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2313
2314 if( p + len != end2 )
2315 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2316 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2317
2318 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2319 return( ret );
2320 }
2321 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2322 {
2323 ecp_keypair_free( eck );
2324 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2325 }
2326
2327 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2328 {
2329 ecp_keypair_free( eck );
2330 return( ret );
2331 }
2332
2333 return 0;
2334}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002335#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002336
2337/*
2338 * Parse an unencrypted PKCS#8 encoded private key
2339 */
2340static int x509parse_key_pkcs8_unencrypted_der(
2341 pk_context *pk,
2342 const unsigned char* key,
2343 size_t keylen )
2344{
2345 int ret, version;
2346 size_t len;
2347 x509_buf params;
2348 unsigned char *p = (unsigned char *) key;
2349 unsigned char *end = p + keylen;
2350 pk_type_t pk_alg = POLARSSL_PK_NONE;
2351
2352 /*
2353 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2354 *
2355 * PrivateKeyInfo ::= SEQUENCE {
2356 * version Version,
2357 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2358 * privateKey PrivateKey,
2359 * attributes [0] IMPLICIT Attributes OPTIONAL }
2360 *
2361 * Version ::= INTEGER
2362 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2363 * PrivateKey ::= OCTET STRING
2364 *
2365 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2366 */
2367
2368 if( ( ret = asn1_get_tag( &p, end, &len,
2369 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2370 {
2371 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002372 }
2373
2374 end = p + len;
2375
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002376 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2377 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2378
2379 if( version != 0 )
2380 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2381
2382 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2383 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2384
2385 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2386 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2387
2388 if( len < 1 )
2389 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2390 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2391
2392 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002393 return( ret );
2394
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002395#if defined(POLARSSL_RSA_C)
2396 if( pk_alg == POLARSSL_PK_RSA )
2397 {
2398 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2399 {
2400 pk_free( pk );
2401 return( ret );
2402 }
2403 } else
2404#endif /* POLARSSL_RSA_C */
2405#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002406 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2407 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002408 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2409 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2410 {
2411 pk_free( pk );
2412 return( ret );
2413 }
2414 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002415#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002416 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2417
2418 return 0;
2419}
2420
2421/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002422 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002423 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002424static int x509parse_key_pkcs8_encrypted_der(
2425 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002426 const unsigned char *key, size_t keylen,
2427 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002428{
2429 int ret;
2430 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002431 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002432 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002433 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002434#if defined(POLARSSL_PKCS12_C)
2435 cipher_type_t cipher_alg;
2436 md_type_t md_alg;
2437#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002438
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002439 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002440
2441 p = (unsigned char *) key;
2442 end = p + keylen;
2443
Paul Bakker28144de2013-06-24 19:28:55 +02002444 if( pwdlen == 0 )
2445 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2446
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002447 /*
2448 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2449 *
2450 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2451 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2452 * encryptedData EncryptedData
2453 * }
2454 *
2455 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2456 *
2457 * EncryptedData ::= OCTET STRING
2458 *
2459 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2460 */
2461 if( ( ret = asn1_get_tag( &p, end, &len,
2462 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2463 {
2464 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2465 }
2466
2467 end = p + len;
2468
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002469 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002470 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002471
2472 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2473 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2474
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002475 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002476 return( POLARSSL_ERR_X509_INVALID_INPUT );
2477
2478 /*
2479 * Decrypt EncryptedData with appropriate PDE
2480 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002481#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002482 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002484 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002485 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002486 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002487 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002488 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2489 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2490
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491 return( ret );
2492 }
2493 }
2494 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2495 {
2496 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2497 PKCS12_PBE_DECRYPT,
2498 pwd, pwdlen,
2499 p, len, buf ) ) != 0 )
2500 {
2501 return( ret );
2502 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002503
2504 // Best guess for password mismatch when using RC4. If first tag is
2505 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2506 //
2507 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2508 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002509 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002510 else
2511#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002512#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002513 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002514 {
2515 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2516 p, len, buf ) ) != 0 )
2517 {
2518 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2519 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2520
2521 return( ret );
2522 }
2523 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002524 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002525#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002526 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2527
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002528 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002529}
2530
2531/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002532 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002533 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002534int x509parse_key( pk_context *pk,
2535 const unsigned char *key, size_t keylen,
2536 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002537{
2538 int ret;
2539
2540#if defined(POLARSSL_PEM_C)
2541 size_t len;
2542 pem_context pem;
2543
2544 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002545
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002546#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002547 ret = pem_read_buffer( &pem,
2548 "-----BEGIN RSA PRIVATE KEY-----",
2549 "-----END RSA PRIVATE KEY-----",
2550 key, pwd, pwdlen, &len );
2551 if( ret == 0 )
2552 {
2553 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2554 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2555 pem.buf, pem.buflen ) ) != 0 )
2556 {
2557 pk_free( pk );
2558 }
2559
2560 pem_free( &pem );
2561 return( ret );
2562 }
2563 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2564 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2565 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2566 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2567 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2568 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002569#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002570
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002571#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002572 ret = pem_read_buffer( &pem,
2573 "-----BEGIN EC PRIVATE KEY-----",
2574 "-----END EC PRIVATE KEY-----",
2575 key, pwd, pwdlen, &len );
2576 if( ret == 0 )
2577 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002578 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2579 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2580 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002581 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002582 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002583 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002584
2585 pem_free( &pem );
2586 return( ret );
2587 }
2588 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2589 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2590 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2591 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2592 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2593 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002594#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002595
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002596 ret = pem_read_buffer( &pem,
2597 "-----BEGIN PRIVATE KEY-----",
2598 "-----END PRIVATE KEY-----",
2599 key, NULL, 0, &len );
2600 if( ret == 0 )
2601 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002602 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002603 pem.buf, pem.buflen ) ) != 0 )
2604 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002605 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002606 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002607
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002608 pem_free( &pem );
2609 return( ret );
2610 }
2611 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2612 return( ret );
2613
2614 ret = pem_read_buffer( &pem,
2615 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2616 "-----END ENCRYPTED PRIVATE KEY-----",
2617 key, NULL, 0, &len );
2618 if( ret == 0 )
2619 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002620 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002621 pem.buf, pem.buflen,
2622 pwd, pwdlen ) ) != 0 )
2623 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002624 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002625 }
2626
2627 pem_free( &pem );
2628 return( ret );
2629 }
2630 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2631 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002632#else
2633 ((void) pwd);
2634 ((void) pwdlen);
2635#endif /* POLARSSL_PEM_C */
2636
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002637 /*
2638 * At this point we only know it's not a PEM formatted key. Could be any
2639 * of the known DER encoded private key formats
2640 *
2641 * We try the different DER format parsers to see if one passes without
2642 * error
2643 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002644 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2645 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002646 {
2647 return( 0 );
2648 }
2649
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002650 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002651
2652 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2653 {
2654 return( ret );
2655 }
2656
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002657 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002658 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002659
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002660 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002661
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002662#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002663 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2664 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2665 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002666 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002667 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002668
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002669 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002670#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002671
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002672#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002673 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2674 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2675 {
2676 return( 0 );
2677 }
2678
2679 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002680#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002681
2682 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2683}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002684
2685/*
2686 * Parse a public key
2687 */
2688int x509parse_public_key( pk_context *ctx,
2689 const unsigned char *key, size_t keylen )
2690{
2691 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002692 unsigned char *p;
2693#if defined(POLARSSL_PEM_C)
2694 size_t len;
2695 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002696
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002697 pem_init( &pem );
2698 ret = pem_read_buffer( &pem,
2699 "-----BEGIN PUBLIC KEY-----",
2700 "-----END PUBLIC KEY-----",
2701 key, NULL, 0, &len );
2702
2703 if( ret == 0 )
2704 {
2705 /*
2706 * Was PEM encoded
2707 */
2708 key = pem.buf;
2709 keylen = pem.buflen;
2710 }
2711 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2712 {
2713 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002714 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002715 }
2716#endif
2717 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002718
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002719 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002720
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002721#if defined(POLARSSL_PEM_C)
2722 pem_free( &pem );
2723#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002724
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002725 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002726}
2727
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002728#if defined(POLARSSL_RSA_C)
2729/*
2730 * Parse a private RSA key
2731 */
2732int x509parse_key_rsa( rsa_context *rsa,
2733 const unsigned char *key, size_t keylen,
2734 const unsigned char *pwd, size_t pwdlen )
2735{
2736 pk_context pk;
2737
2738 pk_init( &pk );
2739 pk_wrap_rsa( &pk, rsa );
2740
2741 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
2742}
2743
2744/*
2745 * Parse a public RSA key
2746 */
2747int x509parse_public_key_rsa( rsa_context *rsa,
2748 const unsigned char *key, size_t keylen )
2749{
2750 pk_context pk;
2751
2752 pk_init( &pk );
2753 pk_wrap_rsa( &pk, rsa );
2754
2755 return( x509parse_public_key( &pk, key, keylen ) );
2756}
2757#endif /* POLARSSL_RSA_C */
2758
Paul Bakkereaa89f82011-04-04 21:36:15 +00002759#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002760/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002761 * Parse DHM parameters
2762 */
Paul Bakker23986e52011-04-24 08:57:21 +00002763int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002764{
Paul Bakker23986e52011-04-24 08:57:21 +00002765 int ret;
2766 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002767 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002768#if defined(POLARSSL_PEM_C)
2769 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002770
Paul Bakker96743fc2011-02-12 14:30:57 +00002771 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002772
Paul Bakker96743fc2011-02-12 14:30:57 +00002773 ret = pem_read_buffer( &pem,
2774 "-----BEGIN DH PARAMETERS-----",
2775 "-----END DH PARAMETERS-----",
2776 dhmin, NULL, 0, &dhminlen );
2777
2778 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002779 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002780 /*
2781 * Was PEM encoded
2782 */
2783 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002784 }
Paul Bakker00b28602013-06-24 13:02:41 +02002785 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002786 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002787 pem_free( &pem );
2788 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002789 }
2790
Paul Bakker96743fc2011-02-12 14:30:57 +00002791 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2792#else
2793 p = (unsigned char *) dhmin;
2794#endif
2795 end = p + dhminlen;
2796
Paul Bakker1b57b062011-01-06 15:48:19 +00002797 memset( dhm, 0, sizeof( dhm_context ) );
2798
Paul Bakker1b57b062011-01-06 15:48:19 +00002799 /*
2800 * DHParams ::= SEQUENCE {
2801 * prime INTEGER, -- P
2802 * generator INTEGER, -- g
2803 * }
2804 */
2805 if( ( ret = asn1_get_tag( &p, end, &len,
2806 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2807 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002808#if defined(POLARSSL_PEM_C)
2809 pem_free( &pem );
2810#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002811 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002812 }
2813
2814 end = p + len;
2815
2816 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2817 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2818 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002819#if defined(POLARSSL_PEM_C)
2820 pem_free( &pem );
2821#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002822 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002823 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002824 }
2825
2826 if( p != end )
2827 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002828#if defined(POLARSSL_PEM_C)
2829 pem_free( &pem );
2830#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002831 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002832 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002833 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2834 }
2835
Paul Bakker96743fc2011-02-12 14:30:57 +00002836#if defined(POLARSSL_PEM_C)
2837 pem_free( &pem );
2838#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002839
2840 return( 0 );
2841}
2842
Paul Bakker335db3f2011-04-25 15:28:35 +00002843#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002844/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002845 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002846 */
2847int x509parse_dhmfile( dhm_context *dhm, const char *path )
2848{
2849 int ret;
2850 size_t n;
2851 unsigned char *buf;
2852
Paul Bakker69e095c2011-12-10 21:55:01 +00002853 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2854 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002855
Paul Bakker27fdf462011-06-09 13:55:13 +00002856 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002857
2858 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002859 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002860
2861 return( ret );
2862}
Paul Bakker335db3f2011-04-25 15:28:35 +00002863#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002864#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002865
Paul Bakker5121ce52009-01-03 21:22:43 +00002866#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002867#include <stdarg.h>
2868
2869#if !defined vsnprintf
2870#define vsnprintf _vsnprintf
2871#endif // vsnprintf
2872
2873/*
2874 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2875 * Result value is not size of buffer needed, but -1 if no fit is possible.
2876 *
2877 * This fuction tries to 'fix' this by at least suggesting enlarging the
2878 * size by 20.
2879 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002880static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002881{
2882 va_list ap;
2883 int res = -1;
2884
2885 va_start( ap, format );
2886
2887 res = vsnprintf( str, size, format, ap );
2888
2889 va_end( ap );
2890
2891 // No quick fix possible
2892 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002893 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002894
2895 return res;
2896}
2897
2898#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002899#endif
2900
Paul Bakkerd98030e2009-05-02 15:13:40 +00002901#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2902
2903#define SAFE_SNPRINTF() \
2904{ \
2905 if( ret == -1 ) \
2906 return( -1 ); \
2907 \
Paul Bakker23986e52011-04-24 08:57:21 +00002908 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002909 p[n - 1] = '\0'; \
2910 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2911 } \
2912 \
Paul Bakker23986e52011-04-24 08:57:21 +00002913 n -= (unsigned int) ret; \
2914 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002915}
2916
Paul Bakker5121ce52009-01-03 21:22:43 +00002917/*
2918 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002919 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002921int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922{
Paul Bakker23986e52011-04-24 08:57:21 +00002923 int ret;
2924 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002926 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002927 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 char s[128], *p;
2929
2930 memset( s, 0, sizeof( s ) );
2931
2932 name = dn;
2933 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002934 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
2936 while( name != NULL )
2937 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002938 if( !name->oid.p )
2939 {
2940 name = name->next;
2941 continue;
2942 }
2943
Paul Bakker74111d32011-01-15 16:57:55 +00002944 if( name != dn )
2945 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946 ret = snprintf( p, n, ", " );
2947 SAFE_SNPRINTF();
2948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakkerc70b9822013-04-07 22:00:46 +02002950 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Paul Bakkerc70b9822013-04-07 22:00:46 +02002952 if( ret == 0 )
2953 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002955 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002956 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
2958 for( i = 0; i < name->val.len; i++ )
2959 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002960 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 break;
2962
2963 c = name->val.p[i];
2964 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2965 s[i] = '?';
2966 else s[i] = c;
2967 }
2968 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002969 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002970 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 name = name->next;
2972 }
2973
Paul Bakker23986e52011-04-24 08:57:21 +00002974 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975}
2976
2977/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002978 * Store the serial in printable form into buf; no more
2979 * than size characters will be written
2980 */
2981int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2982{
Paul Bakker23986e52011-04-24 08:57:21 +00002983 int ret;
2984 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002985 char *p;
2986
2987 p = buf;
2988 n = size;
2989
2990 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002991 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002992
2993 for( i = 0; i < nr; i++ )
2994 {
Paul Bakker93048802011-12-05 14:38:06 +00002995 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002996 continue;
2997
Paul Bakkerdd476992011-01-16 21:34:59 +00002998 ret = snprintf( p, n, "%02X%s",
2999 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3000 SAFE_SNPRINTF();
3001 }
3002
Paul Bakker03c7c252011-11-25 12:37:37 +00003003 if( nr != serial->len )
3004 {
3005 ret = snprintf( p, n, "...." );
3006 SAFE_SNPRINTF();
3007 }
3008
Paul Bakker23986e52011-04-24 08:57:21 +00003009 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003010}
3011
3012/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003013 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003015int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3016 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017{
Paul Bakker23986e52011-04-24 08:57:21 +00003018 int ret;
3019 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003020 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003021 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003022
3023 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003024 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
Paul Bakkerd98030e2009-05-02 15:13:40 +00003026 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003028 SAFE_SNPRINTF();
3029 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Paul Bakkerdd476992011-01-16 21:34:59 +00003033 ret = x509parse_serial_gets( p, n, &crt->serial);
3034 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3037 SAFE_SNPRINTF();
3038 ret = x509parse_dn_gets( p, n, &crt->issuer );
3039 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3042 SAFE_SNPRINTF();
3043 ret = x509parse_dn_gets( p, n, &crt->subject );
3044 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakkerd98030e2009-05-02 15:13:40 +00003046 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3048 crt->valid_from.year, crt->valid_from.mon,
3049 crt->valid_from.day, crt->valid_from.hour,
3050 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Paul Bakkerd98030e2009-05-02 15:13:40 +00003053 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3055 crt->valid_to.year, crt->valid_to.mon,
3056 crt->valid_to.day, crt->valid_to.hour,
3057 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003058 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakkerc70b9822013-04-07 22:00:46 +02003060 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003061 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3064 if( ret != 0 )
3065 ret = snprintf( p, n, "???" );
3066 else
3067 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003068 SAFE_SNPRINTF();
3069
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003070#if defined(POLARSSL_RSA_C)
3071 if( crt->pk.type == POLARSSL_PK_RSA )
3072 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3073 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3074 else
3075#endif /* POLARSSL_RSA_C */
3076#if defined(POLARSSL_ECP_C)
3077 if( crt->pk.type == POLARSSL_PK_ECKEY ||
3078 crt->pk.type == POLARSSL_PK_ECKEY_DH )
3079 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3080 (int) pk_ec( crt->pk )->grp.pbits );
3081 else
3082#endif /* POLARSSL_ECP_C */
3083 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
Paul Bakkerd98030e2009-05-02 15:13:40 +00003084 SAFE_SNPRINTF();
3085
Paul Bakker23986e52011-04-24 08:57:21 +00003086 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003087}
3088
Paul Bakker74111d32011-01-15 16:57:55 +00003089/*
3090 * Return an informational string describing the given OID
3091 */
3092const char *x509_oid_get_description( x509_buf *oid )
3093{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003094 const char *desc = NULL;
3095 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003096
Paul Bakkerc70b9822013-04-07 22:00:46 +02003097 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003098
Paul Bakkerc70b9822013-04-07 22:00:46 +02003099 if( ret != 0 )
3100 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003101
Paul Bakkerc70b9822013-04-07 22:00:46 +02003102 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003103}
3104
3105/* Return the x.y.z.... style numeric string for the given OID */
3106int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3107{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003108 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003109}
3110
Paul Bakkerd98030e2009-05-02 15:13:40 +00003111/*
3112 * Return an informational string about the CRL.
3113 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003114int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3115 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003116{
Paul Bakker23986e52011-04-24 08:57:21 +00003117 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003118 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003119 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003120 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003121 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003122
3123 p = buf;
3124 n = size;
3125
3126 ret = snprintf( p, n, "%sCRL version : %d",
3127 prefix, crl->version );
3128 SAFE_SNPRINTF();
3129
3130 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3131 SAFE_SNPRINTF();
3132 ret = x509parse_dn_gets( p, n, &crl->issuer );
3133 SAFE_SNPRINTF();
3134
3135 ret = snprintf( p, n, "\n%sthis update : " \
3136 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3137 crl->this_update.year, crl->this_update.mon,
3138 crl->this_update.day, crl->this_update.hour,
3139 crl->this_update.min, crl->this_update.sec );
3140 SAFE_SNPRINTF();
3141
3142 ret = snprintf( p, n, "\n%snext update : " \
3143 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3144 crl->next_update.year, crl->next_update.mon,
3145 crl->next_update.day, crl->next_update.hour,
3146 crl->next_update.min, crl->next_update.sec );
3147 SAFE_SNPRINTF();
3148
3149 entry = &crl->entry;
3150
3151 ret = snprintf( p, n, "\n%sRevoked certificates:",
3152 prefix );
3153 SAFE_SNPRINTF();
3154
Paul Bakker9be19372009-07-27 20:21:53 +00003155 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003156 {
3157 ret = snprintf( p, n, "\n%sserial number: ",
3158 prefix );
3159 SAFE_SNPRINTF();
3160
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003161 ret = x509parse_serial_gets( p, n, &entry->serial);
3162 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003163
Paul Bakkerd98030e2009-05-02 15:13:40 +00003164 ret = snprintf( p, n, " revocation date: " \
3165 "%04d-%02d-%02d %02d:%02d:%02d",
3166 entry->revocation_date.year, entry->revocation_date.mon,
3167 entry->revocation_date.day, entry->revocation_date.hour,
3168 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003169 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003170
3171 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 }
3173
Paul Bakkerc70b9822013-04-07 22:00:46 +02003174 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003175 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Paul Bakkerc70b9822013-04-07 22:00:46 +02003177 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3178 if( ret != 0 )
3179 ret = snprintf( p, n, "???" );
3180 else
3181 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003182 SAFE_SNPRINTF();
3183
Paul Bakker1e27bb22009-07-19 20:25:25 +00003184 ret = snprintf( p, n, "\n" );
3185 SAFE_SNPRINTF();
3186
Paul Bakker23986e52011-04-24 08:57:21 +00003187 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003188}
3189
3190/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003191 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003193#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003194int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195{
Paul Bakkercce9d772011-11-18 14:26:47 +00003196 int year, mon, day;
3197 int hour, min, sec;
3198
3199#if defined(_WIN32)
3200 SYSTEMTIME st;
3201
3202 GetLocalTime(&st);
3203
3204 year = st.wYear;
3205 mon = st.wMonth;
3206 day = st.wDay;
3207 hour = st.wHour;
3208 min = st.wMinute;
3209 sec = st.wSecond;
3210#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 struct tm *lt;
3212 time_t tt;
3213
3214 tt = time( NULL );
3215 lt = localtime( &tt );
3216
Paul Bakkercce9d772011-11-18 14:26:47 +00003217 year = lt->tm_year + 1900;
3218 mon = lt->tm_mon + 1;
3219 day = lt->tm_mday;
3220 hour = lt->tm_hour;
3221 min = lt->tm_min;
3222 sec = lt->tm_sec;
3223#endif
3224
3225 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003226 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Paul Bakkercce9d772011-11-18 14:26:47 +00003228 if( year == to->year &&
3229 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003230 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231
Paul Bakkercce9d772011-11-18 14:26:47 +00003232 if( year == to->year &&
3233 mon == to->mon &&
3234 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003235 return( 1 );
3236
Paul Bakkercce9d772011-11-18 14:26:47 +00003237 if( year == to->year &&
3238 mon == to->mon &&
3239 day == to->day &&
3240 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003241 return( 1 );
3242
Paul Bakkercce9d772011-11-18 14:26:47 +00003243 if( year == to->year &&
3244 mon == to->mon &&
3245 day == to->day &&
3246 hour == to->hour &&
3247 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003248 return( 1 );
3249
Paul Bakkercce9d772011-11-18 14:26:47 +00003250 if( year == to->year &&
3251 mon == to->mon &&
3252 day == to->day &&
3253 hour == to->hour &&
3254 min == to->min &&
3255 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003256 return( 1 );
3257
Paul Bakker40ea7de2009-05-03 10:18:48 +00003258 return( 0 );
3259}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003260#else /* POLARSSL_HAVE_TIME */
3261int x509parse_time_expired( const x509_time *to )
3262{
3263 ((void) to);
3264 return( 0 );
3265}
3266#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003267
3268/*
3269 * Return 1 if the certificate is revoked, or 0 otherwise.
3270 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003271int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003272{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003273 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003274
3275 while( cur != NULL && cur->serial.len != 0 )
3276 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003277 if( crt->serial.len == cur->serial.len &&
3278 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003279 {
3280 if( x509parse_time_expired( &cur->revocation_date ) )
3281 return( 1 );
3282 }
3283
3284 cur = cur->next;
3285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003286
3287 return( 0 );
3288}
3289
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003290/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003291 * Check that the given certificate is valid accoring to the CRL.
3292 */
3293static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3294 x509_crl *crl_list)
3295{
3296 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003297 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3298 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003299
Paul Bakker915275b2012-09-28 07:10:55 +00003300 if( ca == NULL )
3301 return( flags );
3302
Paul Bakker76fd75a2011-01-16 21:12:10 +00003303 /*
3304 * TODO: What happens if no CRL is present?
3305 * Suggestion: Revocation state should be unknown if no CRL is present.
3306 * For backwards compatibility this is not yet implemented.
3307 */
3308
Paul Bakker915275b2012-09-28 07:10:55 +00003309 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003310 {
Paul Bakker915275b2012-09-28 07:10:55 +00003311 if( crl_list->version == 0 ||
3312 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003313 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3314 crl_list->issuer_raw.len ) != 0 )
3315 {
3316 crl_list = crl_list->next;
3317 continue;
3318 }
3319
3320 /*
3321 * Check if CRL is correctly signed by the trusted CA
3322 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003323 md_info = md_info_from_type( crl_list->sig_md );
3324 if( md_info == NULL )
3325 {
3326 /*
3327 * Cannot check 'unknown' hash
3328 */
3329 flags |= BADCRL_NOT_TRUSTED;
3330 break;
3331 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003332
Paul Bakkerc70b9822013-04-07 22:00:46 +02003333 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003334
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003335 /* EC NOT IMPLEMENTED YET */
3336 if( ca->pk.type != POLARSSL_PK_RSA )
3337 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3338
3339 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003340 0, hash, crl_list->sig.p ) == 0 )
3341 {
3342 /*
3343 * CRL is not trusted
3344 */
3345 flags |= BADCRL_NOT_TRUSTED;
3346 break;
3347 }
3348
3349 /*
3350 * Check for validity of CRL (Do not drop out)
3351 */
3352 if( x509parse_time_expired( &crl_list->next_update ) )
3353 flags |= BADCRL_EXPIRED;
3354
3355 /*
3356 * Check if certificate is revoked
3357 */
3358 if( x509parse_revoked(crt, crl_list) )
3359 {
3360 flags |= BADCERT_REVOKED;
3361 break;
3362 }
3363
3364 crl_list = crl_list->next;
3365 }
3366 return flags;
3367}
3368
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003369static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003370{
3371 size_t i;
3372 size_t cn_idx = 0;
3373
Paul Bakker57b12982012-02-11 17:38:38 +00003374 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003375 return( 0 );
3376
3377 for( i = 0; i < strlen( cn ); ++i )
3378 {
3379 if( cn[i] == '.' )
3380 {
3381 cn_idx = i;
3382 break;
3383 }
3384 }
3385
3386 if( cn_idx == 0 )
3387 return( 0 );
3388
Paul Bakker535e97d2012-08-23 10:49:55 +00003389 if( strlen( cn ) - cn_idx == name->len - 1 &&
3390 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003391 {
3392 return( 1 );
3393 }
3394
3395 return( 0 );
3396}
3397
Paul Bakker915275b2012-09-28 07:10:55 +00003398static int x509parse_verify_top(
3399 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003400 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003401 int (*f_vrfy)(void *, x509_cert *, int, int *),
3402 void *p_vrfy )
3403{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003404 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003405 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003406 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3407 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003408
3409 if( x509parse_time_expired( &child->valid_to ) )
3410 *flags |= BADCERT_EXPIRED;
3411
3412 /*
3413 * Child is the top of the chain. Check against the trust_ca list.
3414 */
3415 *flags |= BADCERT_NOT_TRUSTED;
3416
3417 while( trust_ca != NULL )
3418 {
3419 if( trust_ca->version == 0 ||
3420 child->issuer_raw.len != trust_ca->subject_raw.len ||
3421 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3422 child->issuer_raw.len ) != 0 )
3423 {
3424 trust_ca = trust_ca->next;
3425 continue;
3426 }
3427
Paul Bakker9a736322012-11-14 12:39:52 +00003428 /*
3429 * Reduce path_len to check against if top of the chain is
3430 * the same as the trusted CA
3431 */
3432 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3433 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3434 child->issuer_raw.len ) == 0 )
3435 {
3436 check_path_cnt--;
3437 }
3438
Paul Bakker915275b2012-09-28 07:10:55 +00003439 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003440 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003441 {
3442 trust_ca = trust_ca->next;
3443 continue;
3444 }
3445
Paul Bakkerc70b9822013-04-07 22:00:46 +02003446 md_info = md_info_from_type( child->sig_md );
3447 if( md_info == NULL )
3448 {
3449 /*
3450 * Cannot check 'unknown' hash
3451 */
3452 continue;
3453 }
Paul Bakker915275b2012-09-28 07:10:55 +00003454
Paul Bakkerc70b9822013-04-07 22:00:46 +02003455 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003456
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003457 /* EC NOT IMPLEMENTED YET */
3458 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3459 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3460
3461 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003462 0, hash, child->sig.p ) != 0 )
3463 {
3464 trust_ca = trust_ca->next;
3465 continue;
3466 }
3467
3468 /*
3469 * Top of chain is signed by a trusted CA
3470 */
3471 *flags &= ~BADCERT_NOT_TRUSTED;
3472 break;
3473 }
3474
Paul Bakker9a736322012-11-14 12:39:52 +00003475 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003476 * If top of chain is not the same as the trusted CA send a verify request
3477 * to the callback for any issues with validity and CRL presence for the
3478 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003479 */
3480 if( trust_ca != NULL &&
3481 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3482 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3483 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003484 {
3485 /* Check trusted CA's CRL for then chain's top crt */
3486 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3487
3488 if( x509parse_time_expired( &trust_ca->valid_to ) )
3489 ca_flags |= BADCERT_EXPIRED;
3490
Paul Bakker915275b2012-09-28 07:10:55 +00003491 if( NULL != f_vrfy )
3492 {
Paul Bakker9a736322012-11-14 12:39:52 +00003493 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003494 return( ret );
3495 }
3496 }
3497
3498 /* Call callback on top cert */
3499 if( NULL != f_vrfy )
3500 {
Paul Bakker9a736322012-11-14 12:39:52 +00003501 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003502 return( ret );
3503 }
3504
Paul Bakker915275b2012-09-28 07:10:55 +00003505 *flags |= ca_flags;
3506
3507 return( 0 );
3508}
3509
3510static int x509parse_verify_child(
3511 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003512 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003513 int (*f_vrfy)(void *, x509_cert *, int, int *),
3514 void *p_vrfy )
3515{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003516 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003517 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003518 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003519 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003520 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003521
3522 if( x509parse_time_expired( &child->valid_to ) )
3523 *flags |= BADCERT_EXPIRED;
3524
Paul Bakkerc70b9822013-04-07 22:00:46 +02003525 md_info = md_info_from_type( child->sig_md );
3526 if( md_info == NULL )
3527 {
3528 /*
3529 * Cannot check 'unknown' hash
3530 */
Paul Bakker915275b2012-09-28 07:10:55 +00003531 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003532 }
3533 else
3534 {
3535 md( md_info, child->tbs.p, child->tbs.len, hash );
3536
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003537 /* EC NOT IMPLEMENTED YET */
3538 if( parent->pk.type != POLARSSL_PK_RSA )
3539 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3540
3541 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3542 0, hash, child->sig.p ) != 0 )
3543 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003544 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003545 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003546 }
3547
Paul Bakker915275b2012-09-28 07:10:55 +00003548 /* Check trusted CA's CRL for the given crt */
3549 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3550
3551 grandparent = parent->next;
3552
3553 while( grandparent != NULL )
3554 {
3555 if( grandparent->version == 0 ||
3556 grandparent->ca_istrue == 0 ||
3557 parent->issuer_raw.len != grandparent->subject_raw.len ||
3558 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3559 parent->issuer_raw.len ) != 0 )
3560 {
3561 grandparent = grandparent->next;
3562 continue;
3563 }
3564 break;
3565 }
3566
Paul Bakker915275b2012-09-28 07:10:55 +00003567 if( grandparent != NULL )
3568 {
3569 /*
3570 * Part of the chain
3571 */
Paul Bakker9a736322012-11-14 12:39:52 +00003572 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 +00003573 if( ret != 0 )
3574 return( ret );
3575 }
3576 else
3577 {
Paul Bakker9a736322012-11-14 12:39:52 +00003578 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 +00003579 if( ret != 0 )
3580 return( ret );
3581 }
3582
3583 /* child is verified to be a child of the parent, call verify callback */
3584 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003585 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003586 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003587
3588 *flags |= parent_flags;
3589
3590 return( 0 );
3591}
3592
Paul Bakker76fd75a2011-01-16 21:12:10 +00003593/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 * Verify the certificate validity
3595 */
3596int x509parse_verify( x509_cert *crt,
3597 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003598 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003599 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003600 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003601 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003602{
Paul Bakker23986e52011-04-24 08:57:21 +00003603 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003604 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003605 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003606 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003608 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Paul Bakker40ea7de2009-05-03 10:18:48 +00003610 *flags = 0;
3611
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 if( cn != NULL )
3613 {
3614 name = &crt->subject;
3615 cn_len = strlen( cn );
3616
Paul Bakker4d2c1242012-05-10 14:12:46 +00003617 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003619 cur = &crt->subject_alt_names;
3620
3621 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003622 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003623 if( cur->buf.len == cn_len &&
3624 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003625 break;
3626
Paul Bakker535e97d2012-08-23 10:49:55 +00003627 if( cur->buf.len > 2 &&
3628 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003629 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003630 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Paul Bakker4d2c1242012-05-10 14:12:46 +00003632 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003633 }
3634
3635 if( cur == NULL )
3636 *flags |= BADCERT_CN_MISMATCH;
3637 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003638 else
3639 {
3640 while( name != NULL )
3641 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003642 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003643 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003644 if( name->val.len == cn_len &&
3645 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003646 break;
3647
Paul Bakker535e97d2012-08-23 10:49:55 +00003648 if( name->val.len > 2 &&
3649 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003650 x509_wildcard_verify( cn, &name->val ) )
3651 break;
3652 }
3653
3654 name = name->next;
3655 }
3656
3657 if( name == NULL )
3658 *flags |= BADCERT_CN_MISMATCH;
3659 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 }
3661
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003663 * Iterate upwards in the given cert chain, to find our crt parent.
3664 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003666 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Paul Bakker76fd75a2011-01-16 21:12:10 +00003668 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003670 if( parent->ca_istrue == 0 ||
3671 crt->issuer_raw.len != parent->subject_raw.len ||
3672 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 crt->issuer_raw.len ) != 0 )
3674 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003675 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 continue;
3677 }
Paul Bakker915275b2012-09-28 07:10:55 +00003678 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Paul Bakker915275b2012-09-28 07:10:55 +00003681 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 {
Paul Bakker915275b2012-09-28 07:10:55 +00003683 /*
3684 * Part of the chain
3685 */
Paul Bakker9a736322012-11-14 12:39:52 +00003686 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003687 if( ret != 0 )
3688 return( ret );
3689 }
3690 else
Paul Bakker74111d32011-01-15 16:57:55 +00003691 {
Paul Bakker9a736322012-11-14 12:39:52 +00003692 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003693 if( ret != 0 )
3694 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003695 }
Paul Bakker915275b2012-09-28 07:10:55 +00003696
3697 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003698 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003699
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 return( 0 );
3701}
3702
3703/*
3704 * Unallocate all certificate data
3705 */
3706void x509_free( x509_cert *crt )
3707{
3708 x509_cert *cert_cur = crt;
3709 x509_cert *cert_prv;
3710 x509_name *name_cur;
3711 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003712 x509_sequence *seq_cur;
3713 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
3715 if( crt == NULL )
3716 return;
3717
3718 do
3719 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003720 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
3722 name_cur = cert_cur->issuer.next;
3723 while( name_cur != NULL )
3724 {
3725 name_prv = name_cur;
3726 name_cur = name_cur->next;
3727 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003728 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 }
3730
3731 name_cur = cert_cur->subject.next;
3732 while( name_cur != NULL )
3733 {
3734 name_prv = name_cur;
3735 name_cur = name_cur->next;
3736 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003737 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 }
3739
Paul Bakker74111d32011-01-15 16:57:55 +00003740 seq_cur = cert_cur->ext_key_usage.next;
3741 while( seq_cur != NULL )
3742 {
3743 seq_prv = seq_cur;
3744 seq_cur = seq_cur->next;
3745 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003746 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003747 }
3748
Paul Bakker8afa70d2012-02-11 18:42:45 +00003749 seq_cur = cert_cur->subject_alt_names.next;
3750 while( seq_cur != NULL )
3751 {
3752 seq_prv = seq_cur;
3753 seq_cur = seq_cur->next;
3754 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003755 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003756 }
3757
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 if( cert_cur->raw.p != NULL )
3759 {
3760 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003761 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 }
3763
3764 cert_cur = cert_cur->next;
3765 }
3766 while( cert_cur != NULL );
3767
3768 cert_cur = crt;
3769 do
3770 {
3771 cert_prv = cert_cur;
3772 cert_cur = cert_cur->next;
3773
3774 memset( cert_prv, 0, sizeof( x509_cert ) );
3775 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003776 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 }
3778 while( cert_cur != NULL );
3779}
3780
Paul Bakkerd98030e2009-05-02 15:13:40 +00003781/*
3782 * Unallocate all CRL data
3783 */
3784void x509_crl_free( x509_crl *crl )
3785{
3786 x509_crl *crl_cur = crl;
3787 x509_crl *crl_prv;
3788 x509_name *name_cur;
3789 x509_name *name_prv;
3790 x509_crl_entry *entry_cur;
3791 x509_crl_entry *entry_prv;
3792
3793 if( crl == NULL )
3794 return;
3795
3796 do
3797 {
3798 name_cur = crl_cur->issuer.next;
3799 while( name_cur != NULL )
3800 {
3801 name_prv = name_cur;
3802 name_cur = name_cur->next;
3803 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003804 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003805 }
3806
3807 entry_cur = crl_cur->entry.next;
3808 while( entry_cur != NULL )
3809 {
3810 entry_prv = entry_cur;
3811 entry_cur = entry_cur->next;
3812 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003813 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003814 }
3815
3816 if( crl_cur->raw.p != NULL )
3817 {
3818 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003819 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003820 }
3821
3822 crl_cur = crl_cur->next;
3823 }
3824 while( crl_cur != NULL );
3825
3826 crl_cur = crl;
3827 do
3828 {
3829 crl_prv = crl_cur;
3830 crl_cur = crl_cur->next;
3831
3832 memset( crl_prv, 0, sizeof( x509_crl ) );
3833 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003834 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003835 }
3836 while( crl_cur != NULL );
3837}
3838
Paul Bakker40e46942009-01-03 21:51:57 +00003839#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Paul Bakker40e46942009-01-03 21:51:57 +00003841#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003842
3843/*
3844 * Checkup routine
3845 */
3846int x509_self_test( int verbose )
3847{
Paul Bakker5690efc2011-05-26 13:16:06 +00003848#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003849 int ret;
3850 int flags;
3851 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 x509_cert cacert;
3853 x509_cert clicert;
3854 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003855#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003856 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
3859 if( verbose != 0 )
3860 printf( " X.509 certificate load: " );
3861
3862 memset( &clicert, 0, sizeof( x509_cert ) );
3863
Paul Bakker3c2122f2013-06-24 19:03:14 +02003864 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003865 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 if( ret != 0 )
3867 {
3868 if( verbose != 0 )
3869 printf( "failed\n" );
3870
3871 return( ret );
3872 }
3873
3874 memset( &cacert, 0, sizeof( x509_cert ) );
3875
Paul Bakker3c2122f2013-06-24 19:03:14 +02003876 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003877 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 if( ret != 0 )
3879 {
3880 if( verbose != 0 )
3881 printf( "failed\n" );
3882
3883 return( ret );
3884 }
3885
3886 if( verbose != 0 )
3887 printf( "passed\n X.509 private key load: " );
3888
3889 i = strlen( test_ca_key );
3890 j = strlen( test_ca_pwd );
3891
Paul Bakker66b78b22011-03-25 14:22:50 +00003892 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3893
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003894 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003895 (const unsigned char *) test_ca_key, i,
3896 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 {
3898 if( verbose != 0 )
3899 printf( "failed\n" );
3900
3901 return( ret );
3902 }
3903
3904 if( verbose != 0 )
3905 printf( "passed\n X.509 signature verify: ");
3906
Paul Bakker23986e52011-04-24 08:57:21 +00003907 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 if( ret != 0 )
3909 {
Paul Bakker23986e52011-04-24 08:57:21 +00003910 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 if( verbose != 0 )
3912 printf( "failed\n" );
3913
3914 return( ret );
3915 }
3916
Paul Bakker5690efc2011-05-26 13:16:06 +00003917#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003918 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003919 printf( "passed\n X.509 DHM parameter load: " );
3920
3921 i = strlen( test_dhm_params );
3922 j = strlen( test_ca_pwd );
3923
Paul Bakker3c2122f2013-06-24 19:03:14 +02003924 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003925 {
3926 if( verbose != 0 )
3927 printf( "failed\n" );
3928
3929 return( ret );
3930 }
3931
3932 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003935
3936 x509_free( &cacert );
3937 x509_free( &clicert );
3938 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003939#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003940 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003941#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
3943 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003944#else
3945 ((void) verbose);
3946 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003948}
3949
3950#endif
3951
3952#endif