blob: a44cf11c0901a7fcd5c96937af4f51bbb089dc0c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200163/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200172{
173 int ret;
174 x509_buf alg_oid;
175
176 memset( params, 0, sizeof(asn1_buf) );
177
178 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
182 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
183
184 /*
185 * No parameters with RSA (only for EC)
186 */
187 if( *pk_alg == POLARSSL_PK_RSA &&
188 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
189 params->len != 0 ) )
190 {
191 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
192 }
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197/* Get an algorithm identifier without parameters (eg for signatures)
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AlgorithmIdentifier ::= SEQUENCE {
200 * algorithm OBJECT IDENTIFIER,
201 * parameters ANY DEFINED BY algorithm OPTIONAL }
202 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200203static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
204 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200208 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( 0 );
212}
213
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200214
215#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200216/* Get an EC group id from an ECParameters buffer
217 *
218 * ECParameters ::= CHOICE {
219 * namedCurve OBJECT IDENTIFIER
220 * -- implicitCurve NULL
221 * -- specifiedCurve SpecifiedECDomain
222 * }
223 */
224static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200225 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226{
227 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200231 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200232 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
233
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200234 params->p = *p;
235 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200236
237 if( *p != end )
238 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
239 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
240
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200241 return( 0 );
242}
243
244/*
245 * Use EC parameters to initialise an EC group
246 */
247static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
248{
249 int ret;
250 ecp_group_id grp_id;
251
252 if( oid_get_ec_grp( params, &grp_id ) != 0 )
253 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
254
255 /*
256 * grp may already be initilialized; if so, make sure IDs match
257 */
258 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
259 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
260
261 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
262 return( ret );
263
264 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200265}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200266#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * AttributeTypeAndValue ::= SEQUENCE {
270 * type AttributeType,
271 * value AttributeValue }
272 *
273 * AttributeType ::= OBJECT IDENTIFIER
274 *
275 * AttributeValue ::= ANY DEFINED BY AttributeType
276 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277static int x509_get_attr_type_value( unsigned char **p,
278 const unsigned char *end,
279 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 x509_buf *oid;
284 x509_buf *val;
285
286 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200290 if( ( end - *p ) < 1 )
291 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
292 POLARSSL_ERR_ASN1_OUT_OF_DATA );
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 oid = &cur->oid;
295 oid->tag = **p;
296
297 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 oid->p = *p;
301 *p += oid->len;
302
303 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
308 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
309 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000311 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 val = &cur->val;
314 val->tag = *(*p)++;
315
316 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319 val->p = *p;
320 *p += val->len;
321
322 cur->next = NULL;
323
Paul Bakker400ff6f2011-02-20 10:40:16 +0000324 return( 0 );
325}
326
327/*
328 * RelativeDistinguishedName ::=
329 * SET OF AttributeTypeAndValue
330 *
331 * AttributeTypeAndValue ::= SEQUENCE {
332 * type AttributeType,
333 * value AttributeValue }
334 *
335 * AttributeType ::= OBJECT IDENTIFIER
336 *
337 * AttributeValue ::= ANY DEFINED BY AttributeType
338 */
339static int x509_get_name( unsigned char **p,
340 const unsigned char *end,
341 x509_name *cur )
342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret;
344 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000345 const unsigned char *end2;
346 x509_name *use;
347
348 if( ( ret = asn1_get_tag( p, end, &len,
349 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000351
352 end2 = end;
353 end = *p + len;
354 use = cur;
355
356 do
357 {
358 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
359 return( ret );
360
361 if( *p != end )
362 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200363 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000364 sizeof( x509_name ) );
365
366 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000367 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368
369 memset( use->next, 0, sizeof( x509_name ) );
370
371 use = use->next;
372 }
373 }
374 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 /*
377 * recurse until end of SEQUENCE is reached
378 */
379 if( *p == end2 )
380 return( 0 );
381
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 sizeof( x509_name ) );
384
385 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000386 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Paul Bakker430ffbe2012-05-01 08:14:20 +0000388 memset( cur->next, 0, sizeof( x509_name ) );
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 return( x509_get_name( p, end2, cur->next ) );
391}
392
393/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * Time ::= CHOICE {
395 * utcTime UTCTime,
396 * generalTime GeneralizedTime }
397 */
Paul Bakker91200182010-02-18 21:26:15 +0000398static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400 x509_time *time )
401{
Paul Bakker23986e52011-04-24 08:57:21 +0000402 int ret;
403 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000405 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406
Paul Bakker91200182010-02-18 21:26:15 +0000407 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000408 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
409 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412
Paul Bakker91200182010-02-18 21:26:15 +0000413 if ( tag == ASN1_UTC_TIME )
414 {
415 (*p)++;
416 ret = asn1_get_len( p, end, &len );
417
418 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420
Paul Bakker91200182010-02-18 21:26:15 +0000421 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000422 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
423 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424
Paul Bakker91200182010-02-18 21:26:15 +0000425 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
426 &time->year, &time->mon, &time->day,
427 &time->hour, &time->min, &time->sec ) < 5 )
428 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429
Paul Bakker400ff6f2011-02-20 10:40:16 +0000430 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000431 time->year += 1900;
432
433 *p += len;
434
435 return( 0 );
436 }
437 else if ( tag == ASN1_GENERALIZED_TIME )
438 {
439 (*p)++;
440 ret = asn1_get_len( p, end, &len );
441
442 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000443 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000444
445 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000446 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
447 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000448
449 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
450 &time->year, &time->mon, &time->day,
451 &time->hour, &time->min, &time->sec ) < 5 )
452 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
453
454 *p += len;
455
456 return( 0 );
457 }
458 else
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000460}
461
462
463/*
464 * Validity ::= SEQUENCE {
465 * notBefore Time,
466 * notAfter Time }
467 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000469 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 x509_time *from,
471 x509_time *to )
472{
Paul Bakker23986e52011-04-24 08:57:21 +0000473 int ret;
474 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 if( ( ret = asn1_get_tag( p, end, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 end = *p + len;
481
Paul Bakker91200182010-02-18 21:26:15 +0000482 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker91200182010-02-18 21:26:15 +0000485 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200495#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200497 * RSAPublicKey ::= SEQUENCE {
498 * modulus INTEGER, -- n
499 * publicExponent INTEGER -- e
500 * }
501 */
502static int x509_get_rsapubkey( unsigned char **p,
503 const unsigned char *end,
504 rsa_context *rsa )
505{
506 int ret;
507 size_t len;
508
509 if( ( ret = asn1_get_tag( p, end, &len,
510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
511 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
512
513 if( *p + len != end )
514 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
515 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
516
517 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
518 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
519 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
520
Manuel Pégourié-Gonnardc13c0d42013-08-15 13:58:01 +0200521 if( *p != end )
522 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200525 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
526 return( ret );
527
528 rsa->len = mpi_size( &rsa->N );
529
530 return( 0 );
531}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200532#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200533
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200534#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200535/*
536 * EC public key is an EC point
537 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200538static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200539 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200540{
541 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200542
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200543 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200544 (const unsigned char *) *p, end - *p ) ) != 0 ||
545 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200546 {
547 ecp_keypair_free( key );
548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200549 }
550
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200551 /*
552 * We know ecp_point_read_binary consumed all bytes
553 */
554 *p = (unsigned char *) end;
555
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200556 return( 0 );
557}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200558#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200559
560/*
561 * SubjectPublicKeyInfo ::= SEQUENCE {
562 * algorithm AlgorithmIdentifier,
563 * subjectPublicKey BIT STRING }
564 */
565static int x509_get_pubkey( unsigned char **p,
566 const unsigned char *end,
567 pk_context *pk )
568{
569 int ret;
570 size_t len;
571 x509_buf alg_params;
572 pk_type_t pk_alg = POLARSSL_PK_NONE;
573
574 if( ( ret = asn1_get_tag( p, end, &len,
575 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576 {
577 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
578 }
579
580 end = *p + len;
581
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200582 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200583 return( ret );
584
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200585 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200586 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
587
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200588 if( *p + len != end )
589 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
590 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
591
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200592 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
593 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200594
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200595#if defined(POLARSSL_RSA_C)
596 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200597 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200598 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
599 } else
600#endif /* POLARSSL_RSA_C */
601#if defined(POLARSSL_ECP_C)
602 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
603 {
604 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
605 x509_get_ecpubkey( p, end, pk_ec( *pk ) );
606 } else
607#endif /* POLARSSL_ECP_C */
608 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200609
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200610 if( ret == 0 && *p != end )
611 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
612 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
613
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200614 if( ret != 0 )
615 pk_free( pk );
616
617 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200618}
619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000621 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 x509_buf *sig )
623{
Paul Bakker23986e52011-04-24 08:57:21 +0000624 int ret;
625 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker8afa70d2012-02-11 18:42:45 +0000627 if( ( end - *p ) < 1 )
628 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
629 POLARSSL_ERR_ASN1_OUT_OF_DATA );
630
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 sig->tag = **p;
632
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200633 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 sig->len = len;
637 sig->p = *p;
638
639 *p += len;
640
641 return( 0 );
642}
643
644/*
645 * X.509 v2/v3 unique identifier (not parsed)
646 */
647static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000648 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 x509_buf *uid, int n )
650{
651 int ret;
652
653 if( *p == end )
654 return( 0 );
655
656 uid->tag = **p;
657
658 if( ( ret = asn1_get_tag( p, end, &uid->len,
659 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
660 {
Paul Bakker40e46942009-01-03 21:51:57 +0000661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 return( 0 );
663
664 return( ret );
665 }
666
667 uid->p = *p;
668 *p += uid->len;
669
670 return( 0 );
671}
672
673/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000674 * X.509 Extensions (No parsing of extensions, pointer should
675 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
677static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000678 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000679 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680{
Paul Bakker23986e52011-04-24 08:57:21 +0000681 int ret;
682 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 if( *p == end )
685 return( 0 );
686
687 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000688
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000690 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 ext->p = *p;
694 end = *p + ext->len;
695
696 /*
697 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
698 *
699 * Extension ::= SEQUENCE {
700 * extnID OBJECT IDENTIFIER,
701 * critical BOOLEAN DEFAULT FALSE,
702 * extnValue OCTET STRING }
703 */
704 if( ( ret = asn1_get_tag( p, end, &len,
705 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000710 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Paul Bakkerd98030e2009-05-02 15:13:40 +0000712 return( 0 );
713}
714
715/*
716 * X.509 CRL v2 extensions (no extensions parsed yet.)
717 */
718static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000719 const unsigned char *end,
720 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000721{
Paul Bakker23986e52011-04-24 08:57:21 +0000722 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000723 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000724
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000725 /* Get explicit tag */
726 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000727 {
728 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
729 return( 0 );
730
731 return( ret );
732 }
733
734 while( *p < end )
735 {
736 if( ( ret = asn1_get_tag( p, end, &len,
737 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000739
740 *p += len;
741 }
742
743 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000744 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000745 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
746
747 return( 0 );
748}
749
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000750/*
751 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
752 */
753static int x509_get_crl_entry_ext( unsigned char **p,
754 const unsigned char *end,
755 x509_buf *ext )
756{
757 int ret;
758 size_t len = 0;
759
760 /* OPTIONAL */
761 if (end <= *p)
762 return( 0 );
763
764 ext->tag = **p;
765 ext->p = *p;
766
767 /*
768 * Get CRL-entry extension sequence header
769 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
770 */
771 if( ( ret = asn1_get_tag( p, end, &ext->len,
772 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
773 {
774 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
775 {
776 ext->p = NULL;
777 return( 0 );
778 }
779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
780 }
781
782 end = *p + ext->len;
783
784 if( end != *p + ext->len )
785 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
786 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
787
788 while( *p < end )
789 {
790 if( ( ret = asn1_get_tag( p, end, &len,
791 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
792 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
793
794 *p += len;
795 }
796
797 if( *p != end )
798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
799 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
800
801 return( 0 );
802}
803
Paul Bakker74111d32011-01-15 16:57:55 +0000804static int x509_get_basic_constraints( unsigned char **p,
805 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000806 int *ca_istrue,
807 int *max_pathlen )
808{
Paul Bakker23986e52011-04-24 08:57:21 +0000809 int ret;
810 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000811
812 /*
813 * BasicConstraints ::= SEQUENCE {
814 * cA BOOLEAN DEFAULT FALSE,
815 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
816 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000817 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000818 *max_pathlen = 0; /* endless */
819
820 if( ( ret = asn1_get_tag( p, end, &len,
821 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
824 if( *p == end )
825 return 0;
826
Paul Bakker3cccddb2011-01-16 21:46:31 +0000827 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000828 {
829 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000830 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000831
832 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000834
Paul Bakker3cccddb2011-01-16 21:46:31 +0000835 if( *ca_istrue != 0 )
836 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000837 }
838
839 if( *p == end )
840 return 0;
841
842 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000844
845 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 (*max_pathlen)++;
850
Paul Bakker74111d32011-01-15 16:57:55 +0000851 return 0;
852}
853
854static int x509_get_ns_cert_type( unsigned char **p,
855 const unsigned char *end,
856 unsigned char *ns_cert_type)
857{
858 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000859 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000860
861 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000863
864 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000865 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000866 POLARSSL_ERR_ASN1_INVALID_LENGTH );
867
868 /* Get actual bitstring */
869 *ns_cert_type = *bs.p;
870 return 0;
871}
872
873static int x509_get_key_usage( unsigned char **p,
874 const unsigned char *end,
875 unsigned char *key_usage)
876{
877 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000878 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000879
880 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000881 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000882
Paul Bakker94a67962012-08-23 13:03:52 +0000883 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000884 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000885 POLARSSL_ERR_ASN1_INVALID_LENGTH );
886
887 /* Get actual bitstring */
888 *key_usage = *bs.p;
889 return 0;
890}
891
Paul Bakkerd98030e2009-05-02 15:13:40 +0000892/*
Paul Bakker74111d32011-01-15 16:57:55 +0000893 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
894 *
895 * KeyPurposeId ::= OBJECT IDENTIFIER
896 */
897static int x509_get_ext_key_usage( unsigned char **p,
898 const unsigned char *end,
899 x509_sequence *ext_key_usage)
900{
901 int ret;
902
903 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000905
906 /* Sequence length must be >= 1 */
907 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000909 POLARSSL_ERR_ASN1_INVALID_LENGTH );
910
911 return 0;
912}
913
914/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000915 * SubjectAltName ::= GeneralNames
916 *
917 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
918 *
919 * GeneralName ::= CHOICE {
920 * otherName [0] OtherName,
921 * rfc822Name [1] IA5String,
922 * dNSName [2] IA5String,
923 * x400Address [3] ORAddress,
924 * directoryName [4] Name,
925 * ediPartyName [5] EDIPartyName,
926 * uniformResourceIdentifier [6] IA5String,
927 * iPAddress [7] OCTET STRING,
928 * registeredID [8] OBJECT IDENTIFIER }
929 *
930 * OtherName ::= SEQUENCE {
931 * type-id OBJECT IDENTIFIER,
932 * value [0] EXPLICIT ANY DEFINED BY type-id }
933 *
934 * EDIPartyName ::= SEQUENCE {
935 * nameAssigner [0] DirectoryString OPTIONAL,
936 * partyName [1] DirectoryString }
937 *
938 * NOTE: PolarSSL only parses and uses dNSName at this point.
939 */
940static int x509_get_subject_alt_name( unsigned char **p,
941 const unsigned char *end,
942 x509_sequence *subject_alt_name )
943{
944 int ret;
945 size_t len, tag_len;
946 asn1_buf *buf;
947 unsigned char tag;
948 asn1_sequence *cur = subject_alt_name;
949
950 /* Get main sequence tag */
951 if( ( ret = asn1_get_tag( p, end, &len,
952 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
954
955 if( *p + len != end )
956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
957 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
958
959 while( *p < end )
960 {
961 if( ( end - *p ) < 1 )
962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
963 POLARSSL_ERR_ASN1_OUT_OF_DATA );
964
965 tag = **p;
966 (*p)++;
967 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
968 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
969
970 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
971 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
972 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
973
974 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
975 {
976 *p += tag_len;
977 continue;
978 }
979
980 buf = &(cur->buf);
981 buf->tag = tag;
982 buf->p = *p;
983 buf->len = tag_len;
984 *p += buf->len;
985
986 /* Allocate and assign next pointer */
987 if (*p < end)
988 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200989 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000990 sizeof( asn1_sequence ) );
991
992 if( cur->next == NULL )
993 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
994 POLARSSL_ERR_ASN1_MALLOC_FAILED );
995
Paul Bakker535e97d2012-08-23 10:49:55 +0000996 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000997 cur = cur->next;
998 }
999 }
1000
1001 /* Set final sequence entry's next pointer to NULL */
1002 cur->next = NULL;
1003
1004 if( *p != end )
1005 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1006 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1007
1008 return( 0 );
1009}
1010
1011/*
Paul Bakker74111d32011-01-15 16:57:55 +00001012 * X.509 v3 extensions
1013 *
1014 * TODO: Perform all of the basic constraints tests required by the RFC
1015 * TODO: Set values for undetected extensions to a sane default?
1016 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001017 */
1018static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001019 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001020 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001021{
Paul Bakker23986e52011-04-24 08:57:21 +00001022 int ret;
1023 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001024 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001026 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001027 {
1028 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1029 return( 0 );
1030
1031 return( ret );
1032 }
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 while( *p < end )
1035 {
Paul Bakker74111d32011-01-15 16:57:55 +00001036 /*
1037 * Extension ::= SEQUENCE {
1038 * extnID OBJECT IDENTIFIER,
1039 * critical BOOLEAN DEFAULT FALSE,
1040 * extnValue OCTET STRING }
1041 */
1042 x509_buf extn_oid = {0, 0, NULL};
1043 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001045
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 if( ( ret = asn1_get_tag( p, end, &len,
1047 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001048 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001050 end_ext_data = *p + len;
1051
Paul Bakker74111d32011-01-15 16:57:55 +00001052 /* Get extension ID */
1053 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Paul Bakker74111d32011-01-15 16:57:55 +00001055 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001056 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Paul Bakker74111d32011-01-15 16:57:55 +00001058 extn_oid.p = *p;
1059 *p += extn_oid.len;
1060
1061 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001062 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001063 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1064
1065 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001066 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001067 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001068 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Paul Bakker74111d32011-01-15 16:57:55 +00001070 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001073 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001075 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001076
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001077 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001079 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 /*
1082 * Detect supported extensions
1083 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1085
1086 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001087 {
1088 /* No parser found, skip extension */
1089 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker5c721f92011-07-27 16:51:09 +00001091#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001092 if( is_critical )
1093 {
1094 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001095 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001096 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1097 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001098#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001099 continue;
1100 }
1101
1102 crt->ext_types |= ext_type;
1103
1104 switch( ext_type )
1105 {
1106 case EXT_BASIC_CONSTRAINTS:
1107 /* Parse basic constraints */
1108 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1109 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1110 return ( ret );
1111 break;
1112
1113 case EXT_KEY_USAGE:
1114 /* Parse key usage */
1115 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1116 &crt->key_usage ) ) != 0 )
1117 return ( ret );
1118 break;
1119
1120 case EXT_EXTENDED_KEY_USAGE:
1121 /* Parse extended key usage */
1122 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1123 &crt->ext_key_usage ) ) != 0 )
1124 return ( ret );
1125 break;
1126
1127 case EXT_SUBJECT_ALT_NAME:
1128 /* Parse subject alt name */
1129 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1130 &crt->subject_alt_names ) ) != 0 )
1131 return ( ret );
1132 break;
1133
1134 case EXT_NS_CERT_TYPE:
1135 /* Parse netscape certificate type */
1136 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1137 &crt->ns_cert_type ) ) != 0 )
1138 return ( ret );
1139 break;
1140
1141 default:
1142 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
1145
1146 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001147 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 return( 0 );
1151}
1152
1153/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154 * X.509 CRL Entries
1155 */
1156static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001157 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 x509_crl_entry *entry )
1159{
Paul Bakker23986e52011-04-24 08:57:21 +00001160 int ret;
1161 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001162 x509_crl_entry *cur_entry = entry;
1163
1164 if( *p == end )
1165 return( 0 );
1166
Paul Bakker9be19372009-07-27 20:21:53 +00001167 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001168 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1169 {
1170 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1171 return( 0 );
1172
1173 return( ret );
1174 }
1175
Paul Bakker9be19372009-07-27 20:21:53 +00001176 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001177
1178 while( *p < end )
1179 {
Paul Bakker23986e52011-04-24 08:57:21 +00001180 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001181 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182
1183 if( ( ret = asn1_get_tag( p, end, &len2,
1184 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1185 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186 return( ret );
1187 }
1188
Paul Bakker9be19372009-07-27 20:21:53 +00001189 cur_entry->raw.tag = **p;
1190 cur_entry->raw.p = *p;
1191 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001192 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001193
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001194 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001195 return( ret );
1196
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001197 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001198 return( ret );
1199
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001200 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001201 return( ret );
1202
Paul Bakker74111d32011-01-15 16:57:55 +00001203 if ( *p < end )
1204 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001205 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001206
1207 if( cur_entry->next == NULL )
1208 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1209
Paul Bakkerd98030e2009-05-02 15:13:40 +00001210 cur_entry = cur_entry->next;
1211 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1212 }
1213 }
1214
1215 return( 0 );
1216}
1217
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1219 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001220{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001221 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001222
Paul Bakkerc70b9822013-04-07 22:00:46 +02001223 if( ret != 0 )
1224 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001225
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001227}
1228
Paul Bakkerd98030e2009-05-02 15:13:40 +00001229/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001230 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001232static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1233 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234{
Paul Bakker23986e52011-04-24 08:57:21 +00001235 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001236 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001237 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Paul Bakker320a4b52009-03-28 18:52:39 +00001239 /*
1240 * Check for valid input
1241 */
1242 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001243 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001244
Paul Bakker6e339b52013-07-03 13:37:05 +02001245 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001246
1247 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001248 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001249
1250 memcpy( p, buf, buflen );
1251
1252 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 crt->raw.p = p;
1255 crt->raw.len = len;
1256 end = p + len;
1257
1258 /*
1259 * Certificate ::= SEQUENCE {
1260 * tbsCertificate TBSCertificate,
1261 * signatureAlgorithm AlgorithmIdentifier,
1262 * signatureValue BIT STRING }
1263 */
1264 if( ( ret = asn1_get_tag( &p, end, &len,
1265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1266 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001267 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001268 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
1270
Paul Bakkerb00ca422012-09-25 12:10:00 +00001271 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001275 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001277 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 /*
1280 * TBSCertificate ::= SEQUENCE {
1281 */
1282 crt->tbs.p = p;
1283
1284 if( ( ret = asn1_get_tag( &p, end, &len,
1285 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1286 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001287 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001288 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
1290
1291 end = p + len;
1292 crt->tbs.len = end - crt->tbs.p;
1293
1294 /*
1295 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1296 *
1297 * CertificateSerialNumber ::= INTEGER
1298 *
1299 * signature AlgorithmIdentifier
1300 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001301 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1302 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1303 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308
1309 crt->version++;
1310
1311 if( crt->version > 3 )
1312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001314 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
1316
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1318 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001320 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001321 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323
1324 /*
1325 * issuer Name
1326 */
1327 crt->issuer_raw.p = p;
1328
1329 if( ( ret = asn1_get_tag( &p, end, &len,
1330 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1331 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001332 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001333 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 }
1335
1336 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1337 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001338 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 return( ret );
1340 }
1341
1342 crt->issuer_raw.len = p - crt->issuer_raw.p;
1343
1344 /*
1345 * Validity ::= SEQUENCE {
1346 * notBefore Time,
1347 * notAfter Time }
1348 *
1349 */
1350 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1351 &crt->valid_to ) ) != 0 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 return( ret );
1355 }
1356
1357 /*
1358 * subject Name
1359 */
1360 crt->subject_raw.p = p;
1361
1362 if( ( ret = asn1_get_tag( &p, end, &len,
1363 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1364 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001365 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001366 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Paul Bakkercefb3962012-06-27 11:51:09 +00001369 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001371 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 return( ret );
1373 }
1374
1375 crt->subject_raw.len = p - crt->subject_raw.p;
1376
1377 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001378 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001380 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 return( ret );
1384 }
1385
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 /*
1387 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1388 * -- If present, version shall be v2 or v3
1389 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1390 * -- If present, version shall be v2 or v3
1391 * extensions [3] EXPLICIT Extensions OPTIONAL
1392 * -- If present, version shall be v3
1393 */
1394 if( crt->version == 2 || crt->version == 3 )
1395 {
1396 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1397 if( ret != 0 )
1398 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001399 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 return( ret );
1401 }
1402 }
1403
1404 if( crt->version == 2 || crt->version == 3 )
1405 {
1406 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1407 if( ret != 0 )
1408 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001409 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 return( ret );
1411 }
1412 }
1413
1414 if( crt->version == 3 )
1415 {
Paul Bakker74111d32011-01-15 16:57:55 +00001416 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 if( ret != 0 )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 return( ret );
1421 }
1422 }
1423
1424 if( p != end )
1425 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001426 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001427 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001428 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430
Paul Bakkerb00ca422012-09-25 12:10:00 +00001431 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001434 * }
1435 * -- end of TBSCertificate
1436 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 * signatureAlgorithm AlgorithmIdentifier,
1438 * signatureValue BIT STRING
1439 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001440 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001442 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 return( ret );
1444 }
1445
Paul Bakker535e97d2012-08-23 10:49:55 +00001446 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1447 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001450 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452
1453 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1454 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 return( ret );
1457 }
1458
1459 if( p != end )
1460 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001462 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001463 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466 return( 0 );
1467}
1468
1469/*
Paul Bakker42c65812013-06-24 19:21:59 +02001470 * Parse one X.509 certificate in DER format from a buffer and add them to a
1471 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472 */
Paul Bakker42c65812013-06-24 19:21:59 +02001473int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001474{
Paul Bakker42c65812013-06-24 19:21:59 +02001475 int ret;
1476 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477
1478 /*
1479 * Check for valid input
1480 */
1481 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001482 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001483
1484 while( crt->version != 0 && crt->next != NULL )
1485 {
1486 prev = crt;
1487 crt = crt->next;
1488 }
1489
1490 /*
1491 * Add new certificate on the end of the chain if needed.
1492 */
1493 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001494 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001495 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001496
Paul Bakker7d06ad22009-05-02 15:53:56 +00001497 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001498 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001499
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001501 crt = crt->next;
1502 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakker42c65812013-06-24 19:21:59 +02001505 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1506 {
1507 if( prev )
1508 prev->next = NULL;
1509
1510 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001511 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001512
1513 return( ret );
1514 }
1515
1516 return( 0 );
1517}
1518
1519/*
1520 * Parse one or more PEM certificates from a buffer and add them to the chained list
1521 */
1522int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1523{
1524 int ret, success = 0, first_error = 0, total_failed = 0;
1525 int buf_format = X509_FORMAT_DER;
1526
1527 /*
1528 * Check for valid input
1529 */
1530 if( chain == NULL || buf == NULL )
1531 return( POLARSSL_ERR_X509_INVALID_INPUT );
1532
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001533 /*
1534 * Determine buffer content. Buffer contains either one DER certificate or
1535 * one or more PEM certificates.
1536 */
1537#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001538 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001539 buf_format = X509_FORMAT_PEM;
1540#endif
1541
1542 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001543 return x509parse_crt_der( chain, buf, buflen );
1544
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001545#if defined(POLARSSL_PEM_C)
1546 if( buf_format == X509_FORMAT_PEM )
1547 {
1548 pem_context pem;
1549
1550 while( buflen > 0 )
1551 {
1552 size_t use_len;
1553 pem_init( &pem );
1554
1555 ret = pem_read_buffer( &pem,
1556 "-----BEGIN CERTIFICATE-----",
1557 "-----END CERTIFICATE-----",
1558 buf, NULL, 0, &use_len );
1559
1560 if( ret == 0 )
1561 {
1562 /*
1563 * Was PEM encoded
1564 */
1565 buflen -= use_len;
1566 buf += use_len;
1567 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001568 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1569 {
1570 return( ret );
1571 }
Paul Bakker00b28602013-06-24 13:02:41 +02001572 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 {
1574 pem_free( &pem );
1575
Paul Bakker5ed3b342013-06-24 19:05:46 +02001576 /*
1577 * PEM header and footer were found
1578 */
1579 buflen -= use_len;
1580 buf += use_len;
1581
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001582 if( first_error == 0 )
1583 first_error = ret;
1584
1585 continue;
1586 }
1587 else
1588 break;
1589
Paul Bakker42c65812013-06-24 19:21:59 +02001590 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001591
1592 pem_free( &pem );
1593
1594 if( ret != 0 )
1595 {
1596 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001597 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001598 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001599 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001600 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001601
1602 if( first_error == 0 )
1603 first_error = ret;
1604
Paul Bakker42c65812013-06-24 19:21:59 +02001605 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606 continue;
1607 }
1608
1609 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 }
1611 }
1612#endif
1613
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001614 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001616 else if( first_error )
1617 return( first_error );
1618 else
1619 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
1622/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623 * Parse one or more CRLs and add them to the chained list
1624 */
Paul Bakker23986e52011-04-24 08:57:21 +00001625int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626{
Paul Bakker23986e52011-04-24 08:57:21 +00001627 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001628 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 unsigned char *p, *end;
1630 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001631#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001632 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 pem_context pem;
1634#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635
1636 crl = chain;
1637
1638 /*
1639 * Check for valid input
1640 */
1641 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001642 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643
1644 while( crl->version != 0 && crl->next != NULL )
1645 crl = crl->next;
1646
1647 /*
1648 * Add new CRL on the end of the chain if needed.
1649 */
1650 if ( crl->version != 0 && crl->next == NULL)
1651 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001652 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653
Paul Bakker7d06ad22009-05-02 15:53:56 +00001654 if( crl->next == NULL )
1655 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001657 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001658 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659
Paul Bakker7d06ad22009-05-02 15:53:56 +00001660 crl = crl->next;
1661 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 }
1663
Paul Bakker96743fc2011-02-12 14:30:57 +00001664#if defined(POLARSSL_PEM_C)
1665 pem_init( &pem );
1666 ret = pem_read_buffer( &pem,
1667 "-----BEGIN X509 CRL-----",
1668 "-----END X509 CRL-----",
1669 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
Paul Bakker96743fc2011-02-12 14:30:57 +00001671 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001673 /*
1674 * Was PEM encoded
1675 */
1676 buflen -= use_len;
1677 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678
1679 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001680 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001682 p = pem.buf;
1683 pem.buf = NULL;
1684 len = pem.buflen;
1685 pem_free( &pem );
1686 }
Paul Bakker00b28602013-06-24 13:02:41 +02001687 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001688 {
1689 pem_free( &pem );
1690 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 }
1692 else
1693 {
1694 /*
1695 * nope, copy the raw DER data
1696 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001697 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698
1699 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001700 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701
1702 memcpy( p, buf, buflen );
1703
1704 buflen = 0;
1705 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001706#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001707 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001708
1709 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001710 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001711
1712 memcpy( p, buf, buflen );
1713
1714 buflen = 0;
1715#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716
1717 crl->raw.p = p;
1718 crl->raw.len = len;
1719 end = p + len;
1720
1721 /*
1722 * CertificateList ::= SEQUENCE {
1723 * tbsCertList TBSCertList,
1724 * signatureAlgorithm AlgorithmIdentifier,
1725 * signatureValue BIT STRING }
1726 */
1727 if( ( ret = asn1_get_tag( &p, end, &len,
1728 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1729 {
1730 x509_crl_free( crl );
1731 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1732 }
1733
Paul Bakker23986e52011-04-24 08:57:21 +00001734 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
1736 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001737 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1739 }
1740
1741 /*
1742 * TBSCertList ::= SEQUENCE {
1743 */
1744 crl->tbs.p = p;
1745
1746 if( ( ret = asn1_get_tag( &p, end, &len,
1747 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001750 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 }
1752
1753 end = p + len;
1754 crl->tbs.len = end - crl->tbs.p;
1755
1756 /*
1757 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1758 * -- if present, MUST be v2
1759 *
1760 * signature AlgorithmIdentifier
1761 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001762 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001763 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 {
1765 x509_crl_free( crl );
1766 return( ret );
1767 }
1768
1769 crl->version++;
1770
1771 if( crl->version > 2 )
1772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1775 }
1776
Paul Bakkerc70b9822013-04-07 22:00:46 +02001777 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1778 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779 {
1780 x509_crl_free( crl );
1781 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1782 }
1783
1784 /*
1785 * issuer Name
1786 */
1787 crl->issuer_raw.p = p;
1788
1789 if( ( ret = asn1_get_tag( &p, end, &len,
1790 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1791 {
1792 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001793 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 }
1795
1796 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1797 {
1798 x509_crl_free( crl );
1799 return( ret );
1800 }
1801
1802 crl->issuer_raw.len = p - crl->issuer_raw.p;
1803
1804 /*
1805 * thisUpdate Time
1806 * nextUpdate Time OPTIONAL
1807 */
Paul Bakker91200182010-02-18 21:26:15 +00001808 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001809 {
1810 x509_crl_free( crl );
1811 return( ret );
1812 }
1813
Paul Bakker91200182010-02-18 21:26:15 +00001814 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 {
Paul Bakker9d781402011-05-09 16:17:09 +00001816 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001817 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001818 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001819 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001820 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824 }
1825
1826 /*
1827 * revokedCertificates SEQUENCE OF SEQUENCE {
1828 * userCertificate CertificateSerialNumber,
1829 * revocationDate Time,
1830 * crlEntryExtensions Extensions OPTIONAL
1831 * -- if present, MUST be v2
1832 * } OPTIONAL
1833 */
1834 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 /*
1841 * crlExtensions EXPLICIT Extensions OPTIONAL
1842 * -- if present, MUST be v2
1843 */
1844 if( crl->version == 2 )
1845 {
1846 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1847
1848 if( ret != 0 )
1849 {
1850 x509_crl_free( crl );
1851 return( ret );
1852 }
1853 }
1854
1855 if( p != end )
1856 {
1857 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001858 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1860 }
1861
1862 end = crl->raw.p + crl->raw.len;
1863
1864 /*
1865 * signatureAlgorithm AlgorithmIdentifier,
1866 * signatureValue BIT STRING
1867 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001868 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869 {
1870 x509_crl_free( crl );
1871 return( ret );
1872 }
1873
Paul Bakker535e97d2012-08-23 10:49:55 +00001874 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1875 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 {
1877 x509_crl_free( crl );
1878 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1879 }
1880
1881 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1882 {
1883 x509_crl_free( crl );
1884 return( ret );
1885 }
1886
1887 if( p != end )
1888 {
1889 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001890 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1892 }
1893
1894 if( buflen > 0 )
1895 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001896 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897
Paul Bakker7d06ad22009-05-02 15:53:56 +00001898 if( crl->next == NULL )
1899 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001901 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001902 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903
Paul Bakker7d06ad22009-05-02 15:53:56 +00001904 crl = crl->next;
1905 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906
1907 return( x509parse_crl( crl, buf, buflen ) );
1908 }
1909
1910 return( 0 );
1911}
1912
Paul Bakker335db3f2011-04-25 15:28:35 +00001913#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915 * Load all data from a file into a given buffer.
1916 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001917static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001918{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001919 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001920 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001921
Paul Bakkerd98030e2009-05-02 15:13:40 +00001922 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001923 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001924
Paul Bakkerd98030e2009-05-02 15:13:40 +00001925 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001926 if( ( size = ftell( f ) ) == -1 )
1927 {
1928 fclose( f );
1929 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1930 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001931 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001932
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001933 *n = (size_t) size;
1934
Paul Bakker694d3ae2013-08-19 14:23:38 +02001935 if( *n + 1 == 0 ||
1936 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001937 {
1938 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001939 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001940 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001941
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942 if( fread( *buf, 1, *n, f ) != *n )
1943 {
1944 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001945 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001946 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001947 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001948
Paul Bakkerd98030e2009-05-02 15:13:40 +00001949 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001950
Paul Bakkerd98030e2009-05-02 15:13:40 +00001951 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001952
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001954}
1955
1956/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 * Load one or more certificates and add them to the chained list
1958 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001959int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960{
1961 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 size_t n;
1963 unsigned char *buf;
1964
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001965 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001966 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Paul Bakker69e095c2011-12-10 21:55:01 +00001968 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
1970 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001971 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 return( ret );
1974}
1975
Paul Bakker8d914582012-06-04 12:46:42 +00001976int x509parse_crtpath( x509_cert *chain, const char *path )
1977{
1978 int ret = 0;
1979#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001980 int w_ret;
1981 WCHAR szDir[MAX_PATH];
1982 char filename[MAX_PATH];
1983 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001984 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001985
Paul Bakker97872ac2012-11-02 12:53:26 +00001986 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001987 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001988
1989 if( len > MAX_PATH - 3 )
1990 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001991
Paul Bakker3338b792012-10-01 21:13:10 +00001992 memset( szDir, 0, sizeof(szDir) );
1993 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001994 memcpy( filename, path, len );
1995 filename[len++] = '\\';
1996 p = filename + len;
1997 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001998
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001999 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002000
Paul Bakker97872ac2012-11-02 12:53:26 +00002001 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002002 if (hFind == INVALID_HANDLE_VALUE)
2003 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2004
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002005 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002006 do
2007 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002008 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002009
Paul Bakkere4791f32012-06-04 21:29:15 +00002010 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002011 continue;
2012
Paul Bakker3338b792012-10-01 21:13:10 +00002013 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2014 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002015 p, len - 1,
2016 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002017
Paul Bakker3338b792012-10-01 21:13:10 +00002018 w_ret = x509parse_crtfile( chain, filename );
2019 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002020 ret++;
2021 else
2022 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002023 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002024 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002025
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002026 if (GetLastError() != ERROR_NO_MORE_FILES)
2027 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002028
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002029cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002030 FindClose( hFind );
2031#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002032 int t_ret, i;
2033 struct stat sb;
2034 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002035 char entry_name[255];
2036 DIR *dir = opendir( path );
2037
2038 if( dir == NULL)
2039 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2040
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002041 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002042 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002043 if( result == NULL )
2044 break;
2045
2046 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2047
2048 i = stat( entry_name, &sb );
2049
2050 if( i == -1 )
2051 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2052
2053 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002054 continue;
2055
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002056 // Ignore parse errors
2057 //
Paul Bakker8d914582012-06-04 12:46:42 +00002058 t_ret = x509parse_crtfile( chain, entry_name );
2059 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002060 ret++;
2061 else
2062 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002063 }
2064 closedir( dir );
2065#endif
2066
2067 return( ret );
2068}
2069
Paul Bakkerd98030e2009-05-02 15:13:40 +00002070/*
2071 * Load one or more CRLs and add them to the chained list
2072 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002073int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002074{
2075 int ret;
2076 size_t n;
2077 unsigned char *buf;
2078
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002079 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002080 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002081
Paul Bakker27fdf462011-06-09 13:55:13 +00002082 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002083
2084 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002085 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002086
2087 return( ret );
2088}
2089
Paul Bakker5121ce52009-01-03 21:22:43 +00002090/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002091 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002092 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002093int x509parse_keyfile( pk_context *ctx,
2094 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002095{
2096 int ret;
2097 size_t n;
2098 unsigned char *buf;
2099
2100 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2101 return( ret );
2102
2103 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002104 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002105 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002106 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002107 (const unsigned char *) pwd, strlen( pwd ) );
2108
2109 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002110 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002111
2112 return( ret );
2113}
2114
2115/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002116 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002117 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002118int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002119{
2120 int ret;
2121 size_t n;
2122 unsigned char *buf;
2123
2124 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2125 return( ret );
2126
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002127 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002128
2129 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002130 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002131
2132 return( ret );
2133}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002134
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002135#if defined(POLARSSL_RSA_C)
2136/*
2137 * Load and parse a private RSA key
2138 */
2139int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2140{
2141 pk_context pk;
2142
2143 pk_init( &pk );
2144 pk_wrap_rsa( &pk, rsa );
2145
2146 return( x509parse_keyfile( &pk, path, pwd ) );
2147}
2148
2149/*
2150 * Load and parse a public RSA key
2151 */
2152int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2153{
2154 pk_context pk;
2155
2156 pk_init( &pk );
2157 pk_wrap_rsa( &pk, rsa );
2158
2159 return( x509parse_public_keyfile( &pk, path ) );
2160}
2161#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002162#endif /* POLARSSL_FS_IO */
2163
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002164#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002165/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002166 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002168static int x509parse_key_pkcs1_der( rsa_context *rsa,
2169 const unsigned char *key,
2170 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002171{
Paul Bakker23986e52011-04-24 08:57:21 +00002172 int ret;
2173 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002175
Paul Bakker96743fc2011-02-12 14:30:57 +00002176 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002177 end = p + keylen;
2178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002180 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002181 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 * RSAPrivateKey ::= SEQUENCE {
2183 * version Version,
2184 * modulus INTEGER, -- n
2185 * publicExponent INTEGER, -- e
2186 * privateExponent INTEGER, -- d
2187 * prime1 INTEGER, -- p
2188 * prime2 INTEGER, -- q
2189 * exponent1 INTEGER, -- d mod (p-1)
2190 * exponent2 INTEGER, -- d mod (q-1)
2191 * coefficient INTEGER, -- (inverse of q) mod p
2192 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2193 * }
2194 */
2195 if( ( ret = asn1_get_tag( &p, end, &len,
2196 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2197 {
Paul Bakker9d781402011-05-09 16:17:09 +00002198 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 }
2200
2201 end = p + len;
2202
2203 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2204 {
Paul Bakker9d781402011-05-09 16:17:09 +00002205 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
2207
2208 if( rsa->ver != 0 )
2209 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002210 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 }
2212
2213 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2214 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2215 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2216 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2217 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2218 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2219 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2220 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2221 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002223 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
2226 rsa->len = mpi_size( &rsa->N );
2227
2228 if( p != end )
2229 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002231 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002232 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 }
2234
2235 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2236 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 rsa_free( rsa );
2238 return( ret );
2239 }
2240
Paul Bakkere2f50402013-06-24 19:00:59 +02002241 return( 0 );
2242}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002243#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002244
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002245#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002246/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002247 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002248 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002249static int x509parse_key_sec1_der( ecp_keypair *eck,
2250 const unsigned char *key,
2251 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002252{
2253 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002254 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002255 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002256 x509_buf params;
2257 unsigned char *p = (unsigned char *) key;
2258 unsigned char *end = p + keylen;
2259 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002260
2261 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002262 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002263 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002264 * ECPrivateKey ::= SEQUENCE {
2265 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2266 * privateKey OCTET STRING,
2267 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2268 * publicKey [1] BIT STRING OPTIONAL
2269 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002270 */
2271 if( ( ret = asn1_get_tag( &p, end, &len,
2272 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2273 {
2274 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2275 }
2276
2277 end = p + len;
2278
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002279 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002280 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2281
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002282 if( version != 1 )
2283 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002284
Paul Bakkere2f50402013-06-24 19:00:59 +02002285 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2286 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2287
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002288 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002289 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002290 ecp_keypair_free( eck );
2291 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2292 }
2293
2294 p += len;
2295
2296 /*
2297 * Is 'parameters' present?
2298 */
2299 if( ( ret = asn1_get_tag( &p, end, &len,
2300 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2301 {
2302 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2303 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2304 {
2305 ecp_keypair_free( eck );
2306 return( ret );
2307 }
2308 }
2309 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2310 {
2311 ecp_keypair_free( eck );
2312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2313 }
2314
2315 /*
2316 * Is 'publickey' present?
2317 */
2318 if( ( ret = asn1_get_tag( &p, end, &len,
2319 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2320 {
2321 end2 = p + len;
2322
2323 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2324 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2325
2326 if( p + len != end2 )
2327 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2328 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2329
2330 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2331 return( ret );
2332 }
2333 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2334 {
2335 ecp_keypair_free( eck );
2336 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2337 }
2338
2339 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2340 {
2341 ecp_keypair_free( eck );
2342 return( ret );
2343 }
2344
2345 return 0;
2346}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002347#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002348
2349/*
2350 * Parse an unencrypted PKCS#8 encoded private key
2351 */
2352static int x509parse_key_pkcs8_unencrypted_der(
2353 pk_context *pk,
2354 const unsigned char* key,
2355 size_t keylen )
2356{
2357 int ret, version;
2358 size_t len;
2359 x509_buf params;
2360 unsigned char *p = (unsigned char *) key;
2361 unsigned char *end = p + keylen;
2362 pk_type_t pk_alg = POLARSSL_PK_NONE;
2363
2364 /*
2365 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2366 *
2367 * PrivateKeyInfo ::= SEQUENCE {
2368 * version Version,
2369 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2370 * privateKey PrivateKey,
2371 * attributes [0] IMPLICIT Attributes OPTIONAL }
2372 *
2373 * Version ::= INTEGER
2374 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2375 * PrivateKey ::= OCTET STRING
2376 *
2377 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2378 */
2379
2380 if( ( ret = asn1_get_tag( &p, end, &len,
2381 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2382 {
2383 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002384 }
2385
2386 end = p + len;
2387
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002388 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2389 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2390
2391 if( version != 0 )
2392 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2393
2394 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2396
2397 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2398 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2399
2400 if( len < 1 )
2401 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2402 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2403
2404 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002405 return( ret );
2406
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002407#if defined(POLARSSL_RSA_C)
2408 if( pk_alg == POLARSSL_PK_RSA )
2409 {
2410 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2411 {
2412 pk_free( pk );
2413 return( ret );
2414 }
2415 } else
2416#endif /* POLARSSL_RSA_C */
2417#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002418 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2419 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002420 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2421 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2422 {
2423 pk_free( pk );
2424 return( ret );
2425 }
2426 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002427#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002428 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2429
2430 return 0;
2431}
2432
2433/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002434 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002435 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002436static int x509parse_key_pkcs8_encrypted_der(
2437 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002438 const unsigned char *key, size_t keylen,
2439 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002440{
2441 int ret;
2442 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002443 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002444 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002445 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002446#if defined(POLARSSL_PKCS12_C)
2447 cipher_type_t cipher_alg;
2448 md_type_t md_alg;
2449#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002450
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002451 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002452
2453 p = (unsigned char *) key;
2454 end = p + keylen;
2455
Paul Bakker28144de2013-06-24 19:28:55 +02002456 if( pwdlen == 0 )
2457 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2458
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002459 /*
2460 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2461 *
2462 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2463 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2464 * encryptedData EncryptedData
2465 * }
2466 *
2467 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2468 *
2469 * EncryptedData ::= OCTET STRING
2470 *
2471 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2472 */
2473 if( ( ret = asn1_get_tag( &p, end, &len,
2474 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2475 {
2476 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2477 }
2478
2479 end = p + len;
2480
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002481 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002482 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002483
2484 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2485 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2486
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002487 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002488 return( POLARSSL_ERR_X509_INVALID_INPUT );
2489
2490 /*
2491 * Decrypt EncryptedData with appropriate PDE
2492 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002493#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002494 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002495 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002496 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002497 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002498 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002499 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002500 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2501 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2502
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002503 return( ret );
2504 }
2505 }
2506 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2507 {
2508 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2509 PKCS12_PBE_DECRYPT,
2510 pwd, pwdlen,
2511 p, len, buf ) ) != 0 )
2512 {
2513 return( ret );
2514 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002515
2516 // Best guess for password mismatch when using RC4. If first tag is
2517 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2518 //
2519 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2520 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002521 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002522 else
2523#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002524#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002525 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002526 {
2527 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2528 p, len, buf ) ) != 0 )
2529 {
2530 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2531 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2532
2533 return( ret );
2534 }
2535 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002536 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002537#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002538 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2539
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002540 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002541}
2542
2543/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002544 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002545 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002546int x509parse_key( pk_context *pk,
2547 const unsigned char *key, size_t keylen,
2548 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002549{
2550 int ret;
2551
2552#if defined(POLARSSL_PEM_C)
2553 size_t len;
2554 pem_context pem;
2555
2556 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002557
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002558#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002559 ret = pem_read_buffer( &pem,
2560 "-----BEGIN RSA PRIVATE KEY-----",
2561 "-----END RSA PRIVATE KEY-----",
2562 key, pwd, pwdlen, &len );
2563 if( ret == 0 )
2564 {
2565 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2566 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2567 pem.buf, pem.buflen ) ) != 0 )
2568 {
2569 pk_free( pk );
2570 }
2571
2572 pem_free( &pem );
2573 return( ret );
2574 }
2575 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2576 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2577 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2578 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2579 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2580 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002581#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002582
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002583#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002584 ret = pem_read_buffer( &pem,
2585 "-----BEGIN EC PRIVATE KEY-----",
2586 "-----END EC PRIVATE KEY-----",
2587 key, pwd, pwdlen, &len );
2588 if( ret == 0 )
2589 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002590 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2591 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2592 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002593 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002594 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002595 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002596
2597 pem_free( &pem );
2598 return( ret );
2599 }
2600 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2601 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2602 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2603 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2604 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2605 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002606#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002607
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002608 ret = pem_read_buffer( &pem,
2609 "-----BEGIN PRIVATE KEY-----",
2610 "-----END PRIVATE KEY-----",
2611 key, NULL, 0, &len );
2612 if( ret == 0 )
2613 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002614 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002615 pem.buf, pem.buflen ) ) != 0 )
2616 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002617 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002618 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002619
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002620 pem_free( &pem );
2621 return( ret );
2622 }
2623 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2624 return( ret );
2625
2626 ret = pem_read_buffer( &pem,
2627 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2628 "-----END ENCRYPTED PRIVATE KEY-----",
2629 key, NULL, 0, &len );
2630 if( ret == 0 )
2631 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002632 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002633 pem.buf, pem.buflen,
2634 pwd, pwdlen ) ) != 0 )
2635 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002636 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002637 }
2638
2639 pem_free( &pem );
2640 return( ret );
2641 }
2642 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2643 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002644#else
2645 ((void) pwd);
2646 ((void) pwdlen);
2647#endif /* POLARSSL_PEM_C */
2648
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002649 /*
2650 * At this point we only know it's not a PEM formatted key. Could be any
2651 * of the known DER encoded private key formats
2652 *
2653 * We try the different DER format parsers to see if one passes without
2654 * error
2655 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002656 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2657 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002658 {
2659 return( 0 );
2660 }
2661
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002662 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002663
2664 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2665 {
2666 return( ret );
2667 }
2668
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002669 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002670 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002671
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002672 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002673
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002674#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002675 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2676 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2677 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002678 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002679 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002680
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002681 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002682#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002683
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002684#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002685 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2686 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2687 {
2688 return( 0 );
2689 }
2690
2691 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002692#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002693
2694 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2695}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002696
2697/*
2698 * Parse a public key
2699 */
2700int x509parse_public_key( pk_context *ctx,
2701 const unsigned char *key, size_t keylen )
2702{
2703 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002704 unsigned char *p;
2705#if defined(POLARSSL_PEM_C)
2706 size_t len;
2707 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002708
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002709 pem_init( &pem );
2710 ret = pem_read_buffer( &pem,
2711 "-----BEGIN PUBLIC KEY-----",
2712 "-----END PUBLIC KEY-----",
2713 key, NULL, 0, &len );
2714
2715 if( ret == 0 )
2716 {
2717 /*
2718 * Was PEM encoded
2719 */
2720 key = pem.buf;
2721 keylen = pem.buflen;
2722 }
2723 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2724 {
2725 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002726 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002727 }
2728#endif
2729 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002730
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002731 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002732
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002733#if defined(POLARSSL_PEM_C)
2734 pem_free( &pem );
2735#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002736
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002737 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002738}
2739
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002740#if defined(POLARSSL_RSA_C)
2741/*
2742 * Parse a private RSA key
2743 */
2744int x509parse_key_rsa( rsa_context *rsa,
2745 const unsigned char *key, size_t keylen,
2746 const unsigned char *pwd, size_t pwdlen )
2747{
2748 pk_context pk;
2749
2750 pk_init( &pk );
2751 pk_wrap_rsa( &pk, rsa );
2752
2753 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
2754}
2755
2756/*
2757 * Parse a public RSA key
2758 */
2759int x509parse_public_key_rsa( rsa_context *rsa,
2760 const unsigned char *key, size_t keylen )
2761{
2762 pk_context pk;
2763
2764 pk_init( &pk );
2765 pk_wrap_rsa( &pk, rsa );
2766
2767 return( x509parse_public_key( &pk, key, keylen ) );
2768}
2769#endif /* POLARSSL_RSA_C */
2770
Paul Bakkereaa89f82011-04-04 21:36:15 +00002771#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002772/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002773 * Parse DHM parameters
2774 */
Paul Bakker23986e52011-04-24 08:57:21 +00002775int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002776{
Paul Bakker23986e52011-04-24 08:57:21 +00002777 int ret;
2778 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002779 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002780#if defined(POLARSSL_PEM_C)
2781 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002782
Paul Bakker96743fc2011-02-12 14:30:57 +00002783 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002784
Paul Bakker96743fc2011-02-12 14:30:57 +00002785 ret = pem_read_buffer( &pem,
2786 "-----BEGIN DH PARAMETERS-----",
2787 "-----END DH PARAMETERS-----",
2788 dhmin, NULL, 0, &dhminlen );
2789
2790 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002791 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002792 /*
2793 * Was PEM encoded
2794 */
2795 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002796 }
Paul Bakker00b28602013-06-24 13:02:41 +02002797 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002798 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002799 pem_free( &pem );
2800 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002801 }
2802
Paul Bakker96743fc2011-02-12 14:30:57 +00002803 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2804#else
2805 p = (unsigned char *) dhmin;
2806#endif
2807 end = p + dhminlen;
2808
Paul Bakker1b57b062011-01-06 15:48:19 +00002809 memset( dhm, 0, sizeof( dhm_context ) );
2810
Paul Bakker1b57b062011-01-06 15:48:19 +00002811 /*
2812 * DHParams ::= SEQUENCE {
2813 * prime INTEGER, -- P
2814 * generator INTEGER, -- g
2815 * }
2816 */
2817 if( ( ret = asn1_get_tag( &p, end, &len,
2818 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2819 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002820#if defined(POLARSSL_PEM_C)
2821 pem_free( &pem );
2822#endif
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 end = p + len;
2827
2828 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2829 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2830 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002831#if defined(POLARSSL_PEM_C)
2832 pem_free( &pem );
2833#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002834 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002835 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002836 }
2837
2838 if( p != end )
2839 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002840#if defined(POLARSSL_PEM_C)
2841 pem_free( &pem );
2842#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002843 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002844 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002845 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2846 }
2847
Paul Bakker96743fc2011-02-12 14:30:57 +00002848#if defined(POLARSSL_PEM_C)
2849 pem_free( &pem );
2850#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002851
2852 return( 0 );
2853}
2854
Paul Bakker335db3f2011-04-25 15:28:35 +00002855#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002856/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002857 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002858 */
2859int x509parse_dhmfile( dhm_context *dhm, const char *path )
2860{
2861 int ret;
2862 size_t n;
2863 unsigned char *buf;
2864
Paul Bakker69e095c2011-12-10 21:55:01 +00002865 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2866 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002867
Paul Bakker27fdf462011-06-09 13:55:13 +00002868 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002869
2870 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002871 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002872
2873 return( ret );
2874}
Paul Bakker335db3f2011-04-25 15:28:35 +00002875#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002876#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002877
Paul Bakker5121ce52009-01-03 21:22:43 +00002878#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002879#include <stdarg.h>
2880
2881#if !defined vsnprintf
2882#define vsnprintf _vsnprintf
2883#endif // vsnprintf
2884
2885/*
2886 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2887 * Result value is not size of buffer needed, but -1 if no fit is possible.
2888 *
2889 * This fuction tries to 'fix' this by at least suggesting enlarging the
2890 * size by 20.
2891 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002892static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002893{
2894 va_list ap;
2895 int res = -1;
2896
2897 va_start( ap, format );
2898
2899 res = vsnprintf( str, size, format, ap );
2900
2901 va_end( ap );
2902
2903 // No quick fix possible
2904 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002905 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002906
2907 return res;
2908}
2909
2910#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002911#endif
2912
Paul Bakkerd98030e2009-05-02 15:13:40 +00002913#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2914
2915#define SAFE_SNPRINTF() \
2916{ \
2917 if( ret == -1 ) \
2918 return( -1 ); \
2919 \
Paul Bakker23986e52011-04-24 08:57:21 +00002920 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002921 p[n - 1] = '\0'; \
2922 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2923 } \
2924 \
Paul Bakker23986e52011-04-24 08:57:21 +00002925 n -= (unsigned int) ret; \
2926 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927}
2928
Paul Bakker5121ce52009-01-03 21:22:43 +00002929/*
2930 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002931 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002933int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002934{
Paul Bakker23986e52011-04-24 08:57:21 +00002935 int ret;
2936 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002938 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002939 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 char s[128], *p;
2941
2942 memset( s, 0, sizeof( s ) );
2943
2944 name = dn;
2945 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
2948 while( name != NULL )
2949 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002950 if( !name->oid.p )
2951 {
2952 name = name->next;
2953 continue;
2954 }
2955
Paul Bakker74111d32011-01-15 16:57:55 +00002956 if( name != dn )
2957 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002958 ret = snprintf( p, n, ", " );
2959 SAFE_SNPRINTF();
2960 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakkerc70b9822013-04-07 22:00:46 +02002962 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakkerc70b9822013-04-07 22:00:46 +02002964 if( ret == 0 )
2965 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002967 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002968 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
2970 for( i = 0; i < name->val.len; i++ )
2971 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002972 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002973 break;
2974
2975 c = name->val.p[i];
2976 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2977 s[i] = '?';
2978 else s[i] = c;
2979 }
2980 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002981 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002982 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 name = name->next;
2984 }
2985
Paul Bakker23986e52011-04-24 08:57:21 +00002986 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987}
2988
2989/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002990 * Store the serial in printable form into buf; no more
2991 * than size characters will be written
2992 */
2993int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2994{
Paul Bakker23986e52011-04-24 08:57:21 +00002995 int ret;
2996 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002997 char *p;
2998
2999 p = buf;
3000 n = size;
3001
3002 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003003 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003004
3005 for( i = 0; i < nr; i++ )
3006 {
Paul Bakker93048802011-12-05 14:38:06 +00003007 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003008 continue;
3009
Paul Bakkerdd476992011-01-16 21:34:59 +00003010 ret = snprintf( p, n, "%02X%s",
3011 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3012 SAFE_SNPRINTF();
3013 }
3014
Paul Bakker03c7c252011-11-25 12:37:37 +00003015 if( nr != serial->len )
3016 {
3017 ret = snprintf( p, n, "...." );
3018 SAFE_SNPRINTF();
3019 }
3020
Paul Bakker23986e52011-04-24 08:57:21 +00003021 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003022}
3023
3024/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003025 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003027int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3028 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029{
Paul Bakker23986e52011-04-24 08:57:21 +00003030 int ret;
3031 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003032 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003033 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
3035 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Paul Bakkerd98030e2009-05-02 15:13:40 +00003038 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003039 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040 SAFE_SNPRINTF();
3041 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003043 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Paul Bakkerdd476992011-01-16 21:34:59 +00003045 ret = x509parse_serial_gets( p, n, &crt->serial);
3046 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Paul Bakkerd98030e2009-05-02 15:13:40 +00003048 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3049 SAFE_SNPRINTF();
3050 ret = x509parse_dn_gets( p, n, &crt->issuer );
3051 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Paul Bakkerd98030e2009-05-02 15:13:40 +00003053 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3054 SAFE_SNPRINTF();
3055 ret = x509parse_dn_gets( p, n, &crt->subject );
3056 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakkerd98030e2009-05-02 15:13:40 +00003058 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3060 crt->valid_from.year, crt->valid_from.mon,
3061 crt->valid_from.day, crt->valid_from.hour,
3062 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003063 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003064
Paul Bakkerd98030e2009-05-02 15:13:40 +00003065 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003066 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3067 crt->valid_to.year, crt->valid_to.mon,
3068 crt->valid_to.day, crt->valid_to.hour,
3069 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003070 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003073 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003074
Paul Bakkerc70b9822013-04-07 22:00:46 +02003075 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3076 if( ret != 0 )
3077 ret = snprintf( p, n, "???" );
3078 else
3079 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003080 SAFE_SNPRINTF();
3081
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003082#if defined(POLARSSL_RSA_C)
3083 if( crt->pk.type == POLARSSL_PK_RSA )
3084 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3085 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3086 else
3087#endif /* POLARSSL_RSA_C */
3088#if defined(POLARSSL_ECP_C)
3089 if( crt->pk.type == POLARSSL_PK_ECKEY ||
3090 crt->pk.type == POLARSSL_PK_ECKEY_DH )
3091 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3092 (int) pk_ec( crt->pk )->grp.pbits );
3093 else
3094#endif /* POLARSSL_ECP_C */
3095 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
Paul Bakkerd98030e2009-05-02 15:13:40 +00003096 SAFE_SNPRINTF();
3097
Paul Bakker23986e52011-04-24 08:57:21 +00003098 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003099}
3100
Paul Bakker74111d32011-01-15 16:57:55 +00003101/*
3102 * Return an informational string describing the given OID
3103 */
3104const char *x509_oid_get_description( x509_buf *oid )
3105{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003106 const char *desc = NULL;
3107 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003108
Paul Bakkerc70b9822013-04-07 22:00:46 +02003109 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003110
Paul Bakkerc70b9822013-04-07 22:00:46 +02003111 if( ret != 0 )
3112 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003113
Paul Bakkerc70b9822013-04-07 22:00:46 +02003114 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003115}
3116
3117/* Return the x.y.z.... style numeric string for the given OID */
3118int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3119{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003120 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003121}
3122
Paul Bakkerd98030e2009-05-02 15:13:40 +00003123/*
3124 * Return an informational string about the CRL.
3125 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003126int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3127 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003128{
Paul Bakker23986e52011-04-24 08:57:21 +00003129 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003130 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003131 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003132 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003133 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003134
3135 p = buf;
3136 n = size;
3137
3138 ret = snprintf( p, n, "%sCRL version : %d",
3139 prefix, crl->version );
3140 SAFE_SNPRINTF();
3141
3142 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3143 SAFE_SNPRINTF();
3144 ret = x509parse_dn_gets( p, n, &crl->issuer );
3145 SAFE_SNPRINTF();
3146
3147 ret = snprintf( p, n, "\n%sthis update : " \
3148 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3149 crl->this_update.year, crl->this_update.mon,
3150 crl->this_update.day, crl->this_update.hour,
3151 crl->this_update.min, crl->this_update.sec );
3152 SAFE_SNPRINTF();
3153
3154 ret = snprintf( p, n, "\n%snext update : " \
3155 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3156 crl->next_update.year, crl->next_update.mon,
3157 crl->next_update.day, crl->next_update.hour,
3158 crl->next_update.min, crl->next_update.sec );
3159 SAFE_SNPRINTF();
3160
3161 entry = &crl->entry;
3162
3163 ret = snprintf( p, n, "\n%sRevoked certificates:",
3164 prefix );
3165 SAFE_SNPRINTF();
3166
Paul Bakker9be19372009-07-27 20:21:53 +00003167 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003168 {
3169 ret = snprintf( p, n, "\n%sserial number: ",
3170 prefix );
3171 SAFE_SNPRINTF();
3172
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003173 ret = x509parse_serial_gets( p, n, &entry->serial);
3174 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003175
Paul Bakkerd98030e2009-05-02 15:13:40 +00003176 ret = snprintf( p, n, " revocation date: " \
3177 "%04d-%02d-%02d %02d:%02d:%02d",
3178 entry->revocation_date.year, entry->revocation_date.mon,
3179 entry->revocation_date.day, entry->revocation_date.hour,
3180 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003181 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003182
3183 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 }
3185
Paul Bakkerc70b9822013-04-07 22:00:46 +02003186 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003187 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
Paul Bakkerc70b9822013-04-07 22:00:46 +02003189 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3190 if( ret != 0 )
3191 ret = snprintf( p, n, "???" );
3192 else
3193 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003194 SAFE_SNPRINTF();
3195
Paul Bakker1e27bb22009-07-19 20:25:25 +00003196 ret = snprintf( p, n, "\n" );
3197 SAFE_SNPRINTF();
3198
Paul Bakker23986e52011-04-24 08:57:21 +00003199 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003200}
3201
3202/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003203 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003205#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003206int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003207{
Paul Bakkercce9d772011-11-18 14:26:47 +00003208 int year, mon, day;
3209 int hour, min, sec;
3210
3211#if defined(_WIN32)
3212 SYSTEMTIME st;
3213
3214 GetLocalTime(&st);
3215
3216 year = st.wYear;
3217 mon = st.wMonth;
3218 day = st.wDay;
3219 hour = st.wHour;
3220 min = st.wMinute;
3221 sec = st.wSecond;
3222#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 struct tm *lt;
3224 time_t tt;
3225
3226 tt = time( NULL );
3227 lt = localtime( &tt );
3228
Paul Bakkercce9d772011-11-18 14:26:47 +00003229 year = lt->tm_year + 1900;
3230 mon = lt->tm_mon + 1;
3231 day = lt->tm_mday;
3232 hour = lt->tm_hour;
3233 min = lt->tm_min;
3234 sec = lt->tm_sec;
3235#endif
3236
3237 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003238 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
Paul Bakkercce9d772011-11-18 14:26:47 +00003240 if( year == to->year &&
3241 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003242 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Paul Bakkercce9d772011-11-18 14:26:47 +00003244 if( year == to->year &&
3245 mon == to->mon &&
3246 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003247 return( 1 );
3248
Paul Bakkercce9d772011-11-18 14:26:47 +00003249 if( year == to->year &&
3250 mon == to->mon &&
3251 day == to->day &&
3252 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003253 return( 1 );
3254
Paul Bakkercce9d772011-11-18 14:26:47 +00003255 if( year == to->year &&
3256 mon == to->mon &&
3257 day == to->day &&
3258 hour == to->hour &&
3259 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003260 return( 1 );
3261
Paul Bakkercce9d772011-11-18 14:26:47 +00003262 if( year == to->year &&
3263 mon == to->mon &&
3264 day == to->day &&
3265 hour == to->hour &&
3266 min == to->min &&
3267 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003268 return( 1 );
3269
Paul Bakker40ea7de2009-05-03 10:18:48 +00003270 return( 0 );
3271}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003272#else /* POLARSSL_HAVE_TIME */
3273int x509parse_time_expired( const x509_time *to )
3274{
3275 ((void) to);
3276 return( 0 );
3277}
3278#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003279
3280/*
3281 * Return 1 if the certificate is revoked, or 0 otherwise.
3282 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003283int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003284{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003285 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003286
3287 while( cur != NULL && cur->serial.len != 0 )
3288 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003289 if( crt->serial.len == cur->serial.len &&
3290 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003291 {
3292 if( x509parse_time_expired( &cur->revocation_date ) )
3293 return( 1 );
3294 }
3295
3296 cur = cur->next;
3297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
3299 return( 0 );
3300}
3301
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003302/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003303 * Check that the given certificate is valid accoring to the CRL.
3304 */
3305static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3306 x509_crl *crl_list)
3307{
Manuel Pégourié-Gonnard96d59122013-08-09 15:12:46 +02003308 int ret;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003309 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003310 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3311 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003312
Paul Bakker915275b2012-09-28 07:10:55 +00003313 if( ca == NULL )
3314 return( flags );
3315
Paul Bakker76fd75a2011-01-16 21:12:10 +00003316 /*
3317 * TODO: What happens if no CRL is present?
3318 * Suggestion: Revocation state should be unknown if no CRL is present.
3319 * For backwards compatibility this is not yet implemented.
3320 */
3321
Paul Bakker915275b2012-09-28 07:10:55 +00003322 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003323 {
Paul Bakker915275b2012-09-28 07:10:55 +00003324 if( crl_list->version == 0 ||
3325 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003326 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3327 crl_list->issuer_raw.len ) != 0 )
3328 {
3329 crl_list = crl_list->next;
3330 continue;
3331 }
3332
3333 /*
3334 * Check if CRL is correctly signed by the trusted CA
3335 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003336 md_info = md_info_from_type( crl_list->sig_md );
3337 if( md_info == NULL )
3338 {
3339 /*
3340 * Cannot check 'unknown' hash
3341 */
3342 flags |= BADCRL_NOT_TRUSTED;
3343 break;
3344 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003345
Paul Bakkerc70b9822013-04-07 22:00:46 +02003346 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003347
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003348#if defined(POLARSSL_RSA_C)
3349 if( ca->pk.type == POLARSSL_PK_RSA )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003350 {
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003351 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC,
3352 crl_list->sig_md, 0, hash, crl_list->sig.p ) == 0 )
3353 {
3354 /*
3355 * CRL is not trusted
3356 */
3357 flags |= BADCRL_NOT_TRUSTED;
3358 break;
3359 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003360 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003361 else
3362#endif /* POLARSSL_RSA_C */
3363#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard96d59122013-08-09 15:12:46 +02003364 if( pk_can_ecdsa( ca->pk ) ) {
3365 if( ( ret = pk_ec_to_ecdsa( &ca->pk ) ) != 0 )
3366 return( ret );
3367
3368 if( ecdsa_read_signature( (ecdsa_context *) ca->pk.data,
3369 hash, md_info->size,
3370 crl_list->sig.p, crl_list->sig.len ) != 0 )
3371 {
3372 /*
3373 * CRL is not trusted
3374 */
3375 flags |= BADCRL_NOT_TRUSTED;
3376 break;
3377 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003378 }
3379 else
3380#endif /* POLARSSL_ECDSA_C */
3381 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003382
3383 /*
3384 * Check for validity of CRL (Do not drop out)
3385 */
3386 if( x509parse_time_expired( &crl_list->next_update ) )
3387 flags |= BADCRL_EXPIRED;
3388
3389 /*
3390 * Check if certificate is revoked
3391 */
3392 if( x509parse_revoked(crt, crl_list) )
3393 {
3394 flags |= BADCERT_REVOKED;
3395 break;
3396 }
3397
3398 crl_list = crl_list->next;
3399 }
3400 return flags;
3401}
3402
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003403static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003404{
3405 size_t i;
3406 size_t cn_idx = 0;
3407
Paul Bakker57b12982012-02-11 17:38:38 +00003408 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003409 return( 0 );
3410
3411 for( i = 0; i < strlen( cn ); ++i )
3412 {
3413 if( cn[i] == '.' )
3414 {
3415 cn_idx = i;
3416 break;
3417 }
3418 }
3419
3420 if( cn_idx == 0 )
3421 return( 0 );
3422
Paul Bakker535e97d2012-08-23 10:49:55 +00003423 if( strlen( cn ) - cn_idx == name->len - 1 &&
3424 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003425 {
3426 return( 1 );
3427 }
3428
3429 return( 0 );
3430}
3431
Paul Bakker915275b2012-09-28 07:10:55 +00003432static int x509parse_verify_top(
3433 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003434 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003435 int (*f_vrfy)(void *, x509_cert *, int, int *),
3436 void *p_vrfy )
3437{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003438 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003439 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003440 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3441 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003442
3443 if( x509parse_time_expired( &child->valid_to ) )
3444 *flags |= BADCERT_EXPIRED;
3445
3446 /*
3447 * Child is the top of the chain. Check against the trust_ca list.
3448 */
3449 *flags |= BADCERT_NOT_TRUSTED;
3450
3451 while( trust_ca != NULL )
3452 {
3453 if( trust_ca->version == 0 ||
3454 child->issuer_raw.len != trust_ca->subject_raw.len ||
3455 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3456 child->issuer_raw.len ) != 0 )
3457 {
3458 trust_ca = trust_ca->next;
3459 continue;
3460 }
3461
Paul Bakker9a736322012-11-14 12:39:52 +00003462 /*
3463 * Reduce path_len to check against if top of the chain is
3464 * the same as the trusted CA
3465 */
3466 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3467 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3468 child->issuer_raw.len ) == 0 )
3469 {
3470 check_path_cnt--;
3471 }
3472
Paul Bakker915275b2012-09-28 07:10:55 +00003473 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003474 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003475 {
3476 trust_ca = trust_ca->next;
3477 continue;
3478 }
3479
Paul Bakkerc70b9822013-04-07 22:00:46 +02003480 md_info = md_info_from_type( child->sig_md );
3481 if( md_info == NULL )
3482 {
3483 /*
3484 * Cannot check 'unknown' hash
3485 */
Paul Bakker3a074a72013-08-20 12:45:03 +02003486 trust_ca = trust_ca->next;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003487 continue;
3488 }
Paul Bakker915275b2012-09-28 07:10:55 +00003489
Paul Bakkerc70b9822013-04-07 22:00:46 +02003490 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003491
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003492#if defined(POLARSSL_RSA_C)
3493 if( trust_ca->pk.type == POLARSSL_PK_RSA )
Paul Bakker915275b2012-09-28 07:10:55 +00003494 {
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003495 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC,
3496 child->sig_md, 0, hash, child->sig.p ) != 0 )
3497 {
3498 trust_ca = trust_ca->next;
3499 continue;
3500 }
Paul Bakker915275b2012-09-28 07:10:55 +00003501 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003502 else
3503#endif /* POLARSSL_RSA_C */
3504#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard96d59122013-08-09 15:12:46 +02003505 if( pk_can_ecdsa( trust_ca->pk ) ) {
3506 if( ( ret = pk_ec_to_ecdsa( &trust_ca->pk ) ) != 0 )
3507 return( ret );
3508
3509 if( ecdsa_read_signature( (ecdsa_context *) trust_ca->pk.data,
3510 hash, md_info->size,
3511 child->sig.p, child->sig.len ) != 0 )
3512 {
3513 trust_ca = trust_ca->next;
3514 continue;
3515 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003516 }
3517 else
3518#endif /* POLARSSL_ECDSA_C */
3519 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker915275b2012-09-28 07:10:55 +00003520
3521 /*
3522 * Top of chain is signed by a trusted CA
3523 */
3524 *flags &= ~BADCERT_NOT_TRUSTED;
3525 break;
3526 }
3527
Paul Bakker9a736322012-11-14 12:39:52 +00003528 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003529 * If top of chain is not the same as the trusted CA send a verify request
3530 * to the callback for any issues with validity and CRL presence for the
3531 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003532 */
3533 if( trust_ca != NULL &&
3534 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3535 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3536 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003537 {
3538 /* Check trusted CA's CRL for then chain's top crt */
3539 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3540
3541 if( x509parse_time_expired( &trust_ca->valid_to ) )
3542 ca_flags |= BADCERT_EXPIRED;
3543
Paul Bakker915275b2012-09-28 07:10:55 +00003544 if( NULL != f_vrfy )
3545 {
Paul Bakker9a736322012-11-14 12:39:52 +00003546 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003547 return( ret );
3548 }
3549 }
3550
3551 /* Call callback on top cert */
3552 if( NULL != f_vrfy )
3553 {
Paul Bakker9a736322012-11-14 12:39:52 +00003554 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003555 return( ret );
3556 }
3557
Paul Bakker915275b2012-09-28 07:10:55 +00003558 *flags |= ca_flags;
3559
3560 return( 0 );
3561}
3562
3563static int x509parse_verify_child(
3564 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003565 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003566 int (*f_vrfy)(void *, x509_cert *, int, int *),
3567 void *p_vrfy )
3568{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003569 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003570 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003571 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003572 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003573 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003574
3575 if( x509parse_time_expired( &child->valid_to ) )
3576 *flags |= BADCERT_EXPIRED;
3577
Paul Bakkerc70b9822013-04-07 22:00:46 +02003578 md_info = md_info_from_type( child->sig_md );
3579 if( md_info == NULL )
3580 {
3581 /*
3582 * Cannot check 'unknown' hash
3583 */
Paul Bakker915275b2012-09-28 07:10:55 +00003584 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003585 }
3586 else
3587 {
3588 md( md_info, child->tbs.p, child->tbs.len, hash );
3589
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003590#if defined(POLARSSL_RSA_C)
3591 if( parent->pk.type == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003592 {
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003593 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC,
3594 child->sig_md, 0, hash, child->sig.p ) != 0 )
3595 {
3596 *flags |= BADCERT_NOT_TRUSTED;
3597 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003598 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003599 else
3600#endif /* POLARSSL_RSA_C */
3601#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard96d59122013-08-09 15:12:46 +02003602 if( pk_can_ecdsa( parent->pk ) ) {
3603 if( ( ret = pk_ec_to_ecdsa( &parent->pk ) ) != 0 )
3604 return( ret );
3605
3606 if( ecdsa_read_signature( (ecdsa_context *) parent->pk.data,
3607 hash, md_info->size,
3608 child->sig.p, child->sig.len ) != 0 )
3609 {
3610 *flags |= BADCERT_NOT_TRUSTED;
3611 }
Manuel Pégourié-Gonnardb4d69c42013-08-09 12:30:45 +02003612 }
3613 else
3614#endif /* POLARSSL_ECDSA_C */
3615 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003616 }
3617
Paul Bakker915275b2012-09-28 07:10:55 +00003618 /* Check trusted CA's CRL for the given crt */
3619 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3620
3621 grandparent = parent->next;
3622
3623 while( grandparent != NULL )
3624 {
3625 if( grandparent->version == 0 ||
3626 grandparent->ca_istrue == 0 ||
3627 parent->issuer_raw.len != grandparent->subject_raw.len ||
3628 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3629 parent->issuer_raw.len ) != 0 )
3630 {
3631 grandparent = grandparent->next;
3632 continue;
3633 }
3634 break;
3635 }
3636
Paul Bakker915275b2012-09-28 07:10:55 +00003637 if( grandparent != NULL )
3638 {
3639 /*
3640 * Part of the chain
3641 */
Paul Bakker9a736322012-11-14 12:39:52 +00003642 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 +00003643 if( ret != 0 )
3644 return( ret );
3645 }
3646 else
3647 {
Paul Bakker9a736322012-11-14 12:39:52 +00003648 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 +00003649 if( ret != 0 )
3650 return( ret );
3651 }
3652
3653 /* child is verified to be a child of the parent, call verify callback */
3654 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003655 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003656 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003657
3658 *flags |= parent_flags;
3659
3660 return( 0 );
3661}
3662
Paul Bakker76fd75a2011-01-16 21:12:10 +00003663/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 * Verify the certificate validity
3665 */
3666int x509parse_verify( x509_cert *crt,
3667 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003668 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003669 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003670 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003671 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672{
Paul Bakker23986e52011-04-24 08:57:21 +00003673 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003674 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003675 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003676 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003678 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
Paul Bakker40ea7de2009-05-03 10:18:48 +00003680 *flags = 0;
3681
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 if( cn != NULL )
3683 {
3684 name = &crt->subject;
3685 cn_len = strlen( cn );
3686
Paul Bakker4d2c1242012-05-10 14:12:46 +00003687 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003689 cur = &crt->subject_alt_names;
3690
3691 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003692 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003693 if( cur->buf.len == cn_len &&
3694 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003695 break;
3696
Paul Bakker535e97d2012-08-23 10:49:55 +00003697 if( cur->buf.len > 2 &&
3698 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003699 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003700 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Paul Bakker4d2c1242012-05-10 14:12:46 +00003702 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003703 }
3704
3705 if( cur == NULL )
3706 *flags |= BADCERT_CN_MISMATCH;
3707 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003708 else
3709 {
3710 while( name != NULL )
3711 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003712 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003713 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003714 if( name->val.len == cn_len &&
3715 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003716 break;
3717
Paul Bakker535e97d2012-08-23 10:49:55 +00003718 if( name->val.len > 2 &&
3719 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003720 x509_wildcard_verify( cn, &name->val ) )
3721 break;
3722 }
3723
3724 name = name->next;
3725 }
3726
3727 if( name == NULL )
3728 *flags |= BADCERT_CN_MISMATCH;
3729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 }
3731
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003733 * Iterate upwards in the given cert chain, to find our crt parent.
3734 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003736 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Paul Bakker76fd75a2011-01-16 21:12:10 +00003738 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003740 if( parent->ca_istrue == 0 ||
3741 crt->issuer_raw.len != parent->subject_raw.len ||
3742 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 crt->issuer_raw.len ) != 0 )
3744 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003745 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 continue;
3747 }
Paul Bakker915275b2012-09-28 07:10:55 +00003748 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 }
3750
Paul Bakker915275b2012-09-28 07:10:55 +00003751 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
Paul Bakker915275b2012-09-28 07:10:55 +00003753 /*
3754 * Part of the chain
3755 */
Paul Bakker9a736322012-11-14 12:39:52 +00003756 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003757 if( ret != 0 )
3758 return( ret );
3759 }
3760 else
Paul Bakker74111d32011-01-15 16:57:55 +00003761 {
Paul Bakker9a736322012-11-14 12:39:52 +00003762 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003763 if( ret != 0 )
3764 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003765 }
Paul Bakker915275b2012-09-28 07:10:55 +00003766
3767 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003768 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003769
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 return( 0 );
3771}
3772
3773/*
3774 * Unallocate all certificate data
3775 */
3776void x509_free( x509_cert *crt )
3777{
3778 x509_cert *cert_cur = crt;
3779 x509_cert *cert_prv;
3780 x509_name *name_cur;
3781 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003782 x509_sequence *seq_cur;
3783 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
3785 if( crt == NULL )
3786 return;
3787
3788 do
3789 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003790 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003791
3792 name_cur = cert_cur->issuer.next;
3793 while( name_cur != NULL )
3794 {
3795 name_prv = name_cur;
3796 name_cur = name_cur->next;
3797 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003798 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 }
3800
3801 name_cur = cert_cur->subject.next;
3802 while( name_cur != NULL )
3803 {
3804 name_prv = name_cur;
3805 name_cur = name_cur->next;
3806 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003807 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003808 }
3809
Paul Bakker74111d32011-01-15 16:57:55 +00003810 seq_cur = cert_cur->ext_key_usage.next;
3811 while( seq_cur != NULL )
3812 {
3813 seq_prv = seq_cur;
3814 seq_cur = seq_cur->next;
3815 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003816 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003817 }
3818
Paul Bakker8afa70d2012-02-11 18:42:45 +00003819 seq_cur = cert_cur->subject_alt_names.next;
3820 while( seq_cur != NULL )
3821 {
3822 seq_prv = seq_cur;
3823 seq_cur = seq_cur->next;
3824 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003825 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003826 }
3827
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 if( cert_cur->raw.p != NULL )
3829 {
3830 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003831 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 }
3833
3834 cert_cur = cert_cur->next;
3835 }
3836 while( cert_cur != NULL );
3837
3838 cert_cur = crt;
3839 do
3840 {
3841 cert_prv = cert_cur;
3842 cert_cur = cert_cur->next;
3843
3844 memset( cert_prv, 0, sizeof( x509_cert ) );
3845 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003846 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 }
3848 while( cert_cur != NULL );
3849}
3850
Paul Bakkerd98030e2009-05-02 15:13:40 +00003851/*
3852 * Unallocate all CRL data
3853 */
3854void x509_crl_free( x509_crl *crl )
3855{
3856 x509_crl *crl_cur = crl;
3857 x509_crl *crl_prv;
3858 x509_name *name_cur;
3859 x509_name *name_prv;
3860 x509_crl_entry *entry_cur;
3861 x509_crl_entry *entry_prv;
3862
3863 if( crl == NULL )
3864 return;
3865
3866 do
3867 {
3868 name_cur = crl_cur->issuer.next;
3869 while( name_cur != NULL )
3870 {
3871 name_prv = name_cur;
3872 name_cur = name_cur->next;
3873 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003874 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003875 }
3876
3877 entry_cur = crl_cur->entry.next;
3878 while( entry_cur != NULL )
3879 {
3880 entry_prv = entry_cur;
3881 entry_cur = entry_cur->next;
3882 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003883 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003884 }
3885
3886 if( crl_cur->raw.p != NULL )
3887 {
3888 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003889 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003890 }
3891
3892 crl_cur = crl_cur->next;
3893 }
3894 while( crl_cur != NULL );
3895
3896 crl_cur = crl;
3897 do
3898 {
3899 crl_prv = crl_cur;
3900 crl_cur = crl_cur->next;
3901
3902 memset( crl_prv, 0, sizeof( x509_crl ) );
3903 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003904 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003905 }
3906 while( crl_cur != NULL );
3907}
3908
Paul Bakker40e46942009-01-03 21:51:57 +00003909#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
Paul Bakker40e46942009-01-03 21:51:57 +00003911#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003912
3913/*
3914 * Checkup routine
3915 */
3916int x509_self_test( int verbose )
3917{
Paul Bakker5690efc2011-05-26 13:16:06 +00003918#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003919 int ret;
3920 int flags;
3921 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 x509_cert cacert;
3923 x509_cert clicert;
3924 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003925#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003926 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003928
3929 if( verbose != 0 )
3930 printf( " X.509 certificate load: " );
3931
3932 memset( &clicert, 0, sizeof( x509_cert ) );
3933
Paul Bakker3c2122f2013-06-24 19:03:14 +02003934 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003935 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 if( ret != 0 )
3937 {
3938 if( verbose != 0 )
3939 printf( "failed\n" );
3940
3941 return( ret );
3942 }
3943
3944 memset( &cacert, 0, sizeof( x509_cert ) );
3945
Paul Bakker3c2122f2013-06-24 19:03:14 +02003946 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003947 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003948 if( ret != 0 )
3949 {
3950 if( verbose != 0 )
3951 printf( "failed\n" );
3952
3953 return( ret );
3954 }
3955
3956 if( verbose != 0 )
3957 printf( "passed\n X.509 private key load: " );
3958
3959 i = strlen( test_ca_key );
3960 j = strlen( test_ca_pwd );
3961
Paul Bakker66b78b22011-03-25 14:22:50 +00003962 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3963
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003964 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003965 (const unsigned char *) test_ca_key, i,
3966 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003967 {
3968 if( verbose != 0 )
3969 printf( "failed\n" );
3970
3971 return( ret );
3972 }
3973
3974 if( verbose != 0 )
3975 printf( "passed\n X.509 signature verify: ");
3976
Paul Bakker23986e52011-04-24 08:57:21 +00003977 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003978 if( ret != 0 )
3979 {
3980 if( verbose != 0 )
3981 printf( "failed\n" );
3982
3983 return( ret );
3984 }
3985
Paul Bakker5690efc2011-05-26 13:16:06 +00003986#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003988 printf( "passed\n X.509 DHM parameter load: " );
3989
3990 i = strlen( test_dhm_params );
3991 j = strlen( test_ca_pwd );
3992
Paul Bakker3c2122f2013-06-24 19:03:14 +02003993 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003994 {
3995 if( verbose != 0 )
3996 printf( "failed\n" );
3997
3998 return( ret );
3999 }
4000
4001 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004003#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004004
4005 x509_free( &cacert );
4006 x509_free( &clicert );
4007 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00004008#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004009 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004010#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
4012 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004013#else
4014 ((void) verbose);
4015 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4016#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004017}
4018
4019#endif
4020
4021#endif