blob: e080174e8071924f691e0633fc793a271ec0492e [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{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002141 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002142 pk_context pk;
2143
2144 pk_init( &pk );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002145 pk_set_type( &pk, POLARSSL_PK_RSA );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002146
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002147 ret = x509parse_keyfile( &pk, path, pwd );
2148
2149 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002150 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002151 else
2152 rsa_free( rsa );
2153
2154 pk_free( &pk );
2155
2156 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002157}
2158
2159/*
2160 * Load and parse a public RSA key
2161 */
2162int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2163{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002164 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002165 pk_context pk;
2166
2167 pk_init( &pk );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002168 pk_set_type( &pk, POLARSSL_PK_RSA );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002169
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002170 ret = x509parse_public_keyfile( &pk, path );
2171
2172 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002173 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002174 else
2175 rsa_free( rsa );
2176
2177 pk_free( &pk );
2178
2179 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002180}
2181#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002182#endif /* POLARSSL_FS_IO */
2183
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002184#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002185/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002186 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002188static int x509parse_key_pkcs1_der( rsa_context *rsa,
2189 const unsigned char *key,
2190 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002191{
Paul Bakker23986e52011-04-24 08:57:21 +00002192 int ret;
2193 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002194 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002195
Paul Bakker96743fc2011-02-12 14:30:57 +00002196 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002197 end = p + keylen;
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002200 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002201 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 * RSAPrivateKey ::= SEQUENCE {
2203 * version Version,
2204 * modulus INTEGER, -- n
2205 * publicExponent INTEGER, -- e
2206 * privateExponent INTEGER, -- d
2207 * prime1 INTEGER, -- p
2208 * prime2 INTEGER, -- q
2209 * exponent1 INTEGER, -- d mod (p-1)
2210 * exponent2 INTEGER, -- d mod (q-1)
2211 * coefficient INTEGER, -- (inverse of q) mod p
2212 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2213 * }
2214 */
2215 if( ( ret = asn1_get_tag( &p, end, &len,
2216 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2217 {
Paul Bakker9d781402011-05-09 16:17:09 +00002218 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220
2221 end = p + len;
2222
2223 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2224 {
Paul Bakker9d781402011-05-09 16:17:09 +00002225 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
2227
2228 if( rsa->ver != 0 )
2229 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002230 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 }
2232
2233 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2234 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2235 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2236 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2237 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2238 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2239 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2240 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2241 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002243 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245
2246 rsa->len = mpi_size( &rsa->N );
2247
2248 if( p != end )
2249 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002252 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
2255 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2256 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 rsa_free( rsa );
2258 return( ret );
2259 }
2260
Paul Bakkere2f50402013-06-24 19:00:59 +02002261 return( 0 );
2262}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002263#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002264
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002265#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002266/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002267 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002268 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002269static int x509parse_key_sec1_der( ecp_keypair *eck,
2270 const unsigned char *key,
2271 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002272{
2273 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002274 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002275 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002276 x509_buf params;
2277 unsigned char *p = (unsigned char *) key;
2278 unsigned char *end = p + keylen;
2279 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002280
2281 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002282 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002283 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002284 * ECPrivateKey ::= SEQUENCE {
2285 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2286 * privateKey OCTET STRING,
2287 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2288 * publicKey [1] BIT STRING OPTIONAL
2289 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002290 */
2291 if( ( ret = asn1_get_tag( &p, end, &len,
2292 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2293 {
2294 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2295 }
2296
2297 end = p + len;
2298
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002299 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002300 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2301
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002302 if( version != 1 )
2303 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002304
Paul Bakkere2f50402013-06-24 19:00:59 +02002305 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2306 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2307
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002308 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002309 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002310 ecp_keypair_free( eck );
2311 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2312 }
2313
2314 p += len;
2315
2316 /*
2317 * Is 'parameters' present?
2318 */
2319 if( ( ret = asn1_get_tag( &p, end, &len,
2320 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2321 {
2322 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2323 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2324 {
2325 ecp_keypair_free( eck );
2326 return( ret );
2327 }
2328 }
2329 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2330 {
2331 ecp_keypair_free( eck );
2332 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2333 }
2334
2335 /*
2336 * Is 'publickey' present?
2337 */
2338 if( ( ret = asn1_get_tag( &p, end, &len,
2339 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2340 {
2341 end2 = p + len;
2342
2343 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2344 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2345
2346 if( p + len != end2 )
2347 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2348 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2349
2350 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2351 return( ret );
2352 }
2353 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2354 {
2355 ecp_keypair_free( eck );
2356 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2357 }
2358
2359 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2360 {
2361 ecp_keypair_free( eck );
2362 return( ret );
2363 }
2364
2365 return 0;
2366}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002367#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002368
2369/*
2370 * Parse an unencrypted PKCS#8 encoded private key
2371 */
2372static int x509parse_key_pkcs8_unencrypted_der(
2373 pk_context *pk,
2374 const unsigned char* key,
2375 size_t keylen )
2376{
2377 int ret, version;
2378 size_t len;
2379 x509_buf params;
2380 unsigned char *p = (unsigned char *) key;
2381 unsigned char *end = p + keylen;
2382 pk_type_t pk_alg = POLARSSL_PK_NONE;
2383
2384 /*
2385 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2386 *
2387 * PrivateKeyInfo ::= SEQUENCE {
2388 * version Version,
2389 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2390 * privateKey PrivateKey,
2391 * attributes [0] IMPLICIT Attributes OPTIONAL }
2392 *
2393 * Version ::= INTEGER
2394 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2395 * PrivateKey ::= OCTET STRING
2396 *
2397 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2398 */
2399
2400 if( ( ret = asn1_get_tag( &p, end, &len,
2401 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2402 {
2403 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002404 }
2405
2406 end = p + len;
2407
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002408 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2409 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2410
2411 if( version != 0 )
2412 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2413
2414 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2415 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2416
2417 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2418 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2419
2420 if( len < 1 )
2421 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2422 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2423
2424 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002425 return( ret );
2426
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002427#if defined(POLARSSL_RSA_C)
2428 if( pk_alg == POLARSSL_PK_RSA )
2429 {
2430 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2431 {
2432 pk_free( pk );
2433 return( ret );
2434 }
2435 } else
2436#endif /* POLARSSL_RSA_C */
2437#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002438 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2439 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002440 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2441 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2442 {
2443 pk_free( pk );
2444 return( ret );
2445 }
2446 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002447#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002448 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2449
2450 return 0;
2451}
2452
2453/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002454 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002455 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002456static int x509parse_key_pkcs8_encrypted_der(
2457 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002458 const unsigned char *key, size_t keylen,
2459 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002460{
2461 int ret;
2462 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002463 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002464 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002465 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002466#if defined(POLARSSL_PKCS12_C)
2467 cipher_type_t cipher_alg;
2468 md_type_t md_alg;
2469#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002470
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002471 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002472
2473 p = (unsigned char *) key;
2474 end = p + keylen;
2475
Paul Bakker28144de2013-06-24 19:28:55 +02002476 if( pwdlen == 0 )
2477 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2478
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002479 /*
2480 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2481 *
2482 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2483 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2484 * encryptedData EncryptedData
2485 * }
2486 *
2487 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2488 *
2489 * EncryptedData ::= OCTET STRING
2490 *
2491 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2492 */
2493 if( ( ret = asn1_get_tag( &p, end, &len,
2494 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2495 {
2496 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2497 }
2498
2499 end = p + len;
2500
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002501 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002502 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002503
2504 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2505 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2506
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002507 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002508 return( POLARSSL_ERR_X509_INVALID_INPUT );
2509
2510 /*
2511 * Decrypt EncryptedData with appropriate PDE
2512 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002513#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002514 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002515 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002516 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002517 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002518 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002519 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002520 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2521 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2522
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002523 return( ret );
2524 }
2525 }
2526 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2527 {
2528 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2529 PKCS12_PBE_DECRYPT,
2530 pwd, pwdlen,
2531 p, len, buf ) ) != 0 )
2532 {
2533 return( ret );
2534 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002535
2536 // Best guess for password mismatch when using RC4. If first tag is
2537 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2538 //
2539 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2540 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002541 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002542 else
2543#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002544#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002545 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002546 {
2547 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2548 p, len, buf ) ) != 0 )
2549 {
2550 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2551 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2552
2553 return( ret );
2554 }
2555 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002556 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002557#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002558 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2559
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002560 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002561}
2562
2563/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002564 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002565 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002566int x509parse_key( pk_context *pk,
2567 const unsigned char *key, size_t keylen,
2568 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002569{
2570 int ret;
2571
2572#if defined(POLARSSL_PEM_C)
2573 size_t len;
2574 pem_context pem;
2575
2576 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002577
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002578#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002579 ret = pem_read_buffer( &pem,
2580 "-----BEGIN RSA PRIVATE KEY-----",
2581 "-----END RSA PRIVATE KEY-----",
2582 key, pwd, pwdlen, &len );
2583 if( ret == 0 )
2584 {
2585 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2586 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2587 pem.buf, pem.buflen ) ) != 0 )
2588 {
2589 pk_free( pk );
2590 }
2591
2592 pem_free( &pem );
2593 return( ret );
2594 }
2595 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2596 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2597 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2598 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2599 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2600 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002601#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002602
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002603#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002604 ret = pem_read_buffer( &pem,
2605 "-----BEGIN EC PRIVATE KEY-----",
2606 "-----END EC PRIVATE KEY-----",
2607 key, pwd, pwdlen, &len );
2608 if( ret == 0 )
2609 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002610 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2611 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2612 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002613 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002614 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002615 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002616
2617 pem_free( &pem );
2618 return( ret );
2619 }
2620 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2621 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2622 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2623 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2624 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2625 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002626#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002627
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002628 ret = pem_read_buffer( &pem,
2629 "-----BEGIN PRIVATE KEY-----",
2630 "-----END PRIVATE KEY-----",
2631 key, NULL, 0, &len );
2632 if( ret == 0 )
2633 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002634 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002635 pem.buf, pem.buflen ) ) != 0 )
2636 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002637 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002638 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002639
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002640 pem_free( &pem );
2641 return( ret );
2642 }
2643 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2644 return( ret );
2645
2646 ret = pem_read_buffer( &pem,
2647 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2648 "-----END ENCRYPTED PRIVATE KEY-----",
2649 key, NULL, 0, &len );
2650 if( ret == 0 )
2651 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002652 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002653 pem.buf, pem.buflen,
2654 pwd, pwdlen ) ) != 0 )
2655 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002656 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002657 }
2658
2659 pem_free( &pem );
2660 return( ret );
2661 }
2662 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2663 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002664#else
2665 ((void) pwd);
2666 ((void) pwdlen);
2667#endif /* POLARSSL_PEM_C */
2668
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002669 /*
2670 * At this point we only know it's not a PEM formatted key. Could be any
2671 * of the known DER encoded private key formats
2672 *
2673 * We try the different DER format parsers to see if one passes without
2674 * error
2675 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002676 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2677 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002678 {
2679 return( 0 );
2680 }
2681
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002682 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002683
2684 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2685 {
2686 return( ret );
2687 }
2688
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002689 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002690 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002691
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002692 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002693
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002694#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002695 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2696 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2697 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002698 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002699 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002700
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002701 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002702#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002703
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002704#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002705 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2706 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2707 {
2708 return( 0 );
2709 }
2710
2711 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002712#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002713
2714 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2715}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002716
2717/*
2718 * Parse a public key
2719 */
2720int x509parse_public_key( pk_context *ctx,
2721 const unsigned char *key, size_t keylen )
2722{
2723 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002724 unsigned char *p;
2725#if defined(POLARSSL_PEM_C)
2726 size_t len;
2727 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002728
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002729 pem_init( &pem );
2730 ret = pem_read_buffer( &pem,
2731 "-----BEGIN PUBLIC KEY-----",
2732 "-----END PUBLIC KEY-----",
2733 key, NULL, 0, &len );
2734
2735 if( ret == 0 )
2736 {
2737 /*
2738 * Was PEM encoded
2739 */
2740 key = pem.buf;
2741 keylen = pem.buflen;
2742 }
2743 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2744 {
2745 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002746 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002747 }
2748#endif
2749 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002750
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002751 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002752
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002753#if defined(POLARSSL_PEM_C)
2754 pem_free( &pem );
2755#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002756
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002757 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002758}
2759
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002760#if defined(POLARSSL_RSA_C)
2761/*
2762 * Parse a private RSA key
2763 */
2764int x509parse_key_rsa( rsa_context *rsa,
2765 const unsigned char *key, size_t keylen,
2766 const unsigned char *pwd, size_t pwdlen )
2767{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002768 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002769 pk_context pk;
2770
2771 pk_init( &pk );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002772 pk_set_type( &pk, POLARSSL_PK_RSA );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002773
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002774 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
2775
2776 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002777 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002778 else
2779 rsa_free( rsa );
2780
2781 pk_free( &pk );
2782
2783 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002784}
2785
2786/*
2787 * Parse a public RSA key
2788 */
2789int x509parse_public_key_rsa( rsa_context *rsa,
2790 const unsigned char *key, size_t keylen )
2791{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002792 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002793 pk_context pk;
2794
2795 pk_init( &pk );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002796 pk_set_type( &pk, POLARSSL_PK_RSA );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002797
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002798 ret = x509parse_public_key( &pk, key, keylen );
2799
2800 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002801 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002802 else
2803 rsa_free( rsa );
2804
2805 pk_free( &pk );
2806
2807 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002808}
2809#endif /* POLARSSL_RSA_C */
2810
Paul Bakkereaa89f82011-04-04 21:36:15 +00002811#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002812/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002813 * Parse DHM parameters
2814 */
Paul Bakker23986e52011-04-24 08:57:21 +00002815int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002816{
Paul Bakker23986e52011-04-24 08:57:21 +00002817 int ret;
2818 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002819 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002820#if defined(POLARSSL_PEM_C)
2821 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002822
Paul Bakker96743fc2011-02-12 14:30:57 +00002823 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002824
Paul Bakker96743fc2011-02-12 14:30:57 +00002825 ret = pem_read_buffer( &pem,
2826 "-----BEGIN DH PARAMETERS-----",
2827 "-----END DH PARAMETERS-----",
2828 dhmin, NULL, 0, &dhminlen );
2829
2830 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002831 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002832 /*
2833 * Was PEM encoded
2834 */
2835 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002836 }
Paul Bakker00b28602013-06-24 13:02:41 +02002837 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002838 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002839 pem_free( &pem );
2840 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002841 }
2842
Paul Bakker96743fc2011-02-12 14:30:57 +00002843 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2844#else
2845 p = (unsigned char *) dhmin;
2846#endif
2847 end = p + dhminlen;
2848
Paul Bakker1b57b062011-01-06 15:48:19 +00002849 memset( dhm, 0, sizeof( dhm_context ) );
2850
Paul Bakker1b57b062011-01-06 15:48:19 +00002851 /*
2852 * DHParams ::= SEQUENCE {
2853 * prime INTEGER, -- P
2854 * generator INTEGER, -- g
2855 * }
2856 */
2857 if( ( ret = asn1_get_tag( &p, end, &len,
2858 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2859 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002860#if defined(POLARSSL_PEM_C)
2861 pem_free( &pem );
2862#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002863 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002864 }
2865
2866 end = p + len;
2867
2868 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2869 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2870 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002871#if defined(POLARSSL_PEM_C)
2872 pem_free( &pem );
2873#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002874 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002875 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002876 }
2877
2878 if( p != end )
2879 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002880#if defined(POLARSSL_PEM_C)
2881 pem_free( &pem );
2882#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002883 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002884 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002885 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2886 }
2887
Paul Bakker96743fc2011-02-12 14:30:57 +00002888#if defined(POLARSSL_PEM_C)
2889 pem_free( &pem );
2890#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002891
2892 return( 0 );
2893}
2894
Paul Bakker335db3f2011-04-25 15:28:35 +00002895#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002896/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002897 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002898 */
2899int x509parse_dhmfile( dhm_context *dhm, const char *path )
2900{
2901 int ret;
2902 size_t n;
2903 unsigned char *buf;
2904
Paul Bakker69e095c2011-12-10 21:55:01 +00002905 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2906 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002907
Paul Bakker27fdf462011-06-09 13:55:13 +00002908 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002909
2910 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002911 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002912
2913 return( ret );
2914}
Paul Bakker335db3f2011-04-25 15:28:35 +00002915#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002916#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002917
Paul Bakker5121ce52009-01-03 21:22:43 +00002918#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002919#include <stdarg.h>
2920
2921#if !defined vsnprintf
2922#define vsnprintf _vsnprintf
2923#endif // vsnprintf
2924
2925/*
2926 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2927 * Result value is not size of buffer needed, but -1 if no fit is possible.
2928 *
2929 * This fuction tries to 'fix' this by at least suggesting enlarging the
2930 * size by 20.
2931 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002932static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002933{
2934 va_list ap;
2935 int res = -1;
2936
2937 va_start( ap, format );
2938
2939 res = vsnprintf( str, size, format, ap );
2940
2941 va_end( ap );
2942
2943 // No quick fix possible
2944 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002945 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946
2947 return res;
2948}
2949
2950#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002951#endif
2952
Paul Bakkerd98030e2009-05-02 15:13:40 +00002953#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2954
2955#define SAFE_SNPRINTF() \
2956{ \
2957 if( ret == -1 ) \
2958 return( -1 ); \
2959 \
Paul Bakker23986e52011-04-24 08:57:21 +00002960 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002961 p[n - 1] = '\0'; \
2962 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2963 } \
2964 \
Paul Bakker23986e52011-04-24 08:57:21 +00002965 n -= (unsigned int) ret; \
2966 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002967}
2968
Paul Bakker5121ce52009-01-03 21:22:43 +00002969/*
2970 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002971 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002973int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002974{
Paul Bakker23986e52011-04-24 08:57:21 +00002975 int ret;
2976 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002978 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002979 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 char s[128], *p;
2981
2982 memset( s, 0, sizeof( s ) );
2983
2984 name = dn;
2985 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002986 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
2988 while( name != NULL )
2989 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002990 if( !name->oid.p )
2991 {
2992 name = name->next;
2993 continue;
2994 }
2995
Paul Bakker74111d32011-01-15 16:57:55 +00002996 if( name != dn )
2997 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002998 ret = snprintf( p, n, ", " );
2999 SAFE_SNPRINTF();
3000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Paul Bakkerc70b9822013-04-07 22:00:46 +02003002 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Paul Bakkerc70b9822013-04-07 22:00:46 +02003004 if( ret == 0 )
3005 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003007 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003008 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
3010 for( i = 0; i < name->val.len; i++ )
3011 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003012 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 break;
3014
3015 c = name->val.p[i];
3016 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3017 s[i] = '?';
3018 else s[i] = c;
3019 }
3020 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003021 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003022 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003023 name = name->next;
3024 }
3025
Paul Bakker23986e52011-04-24 08:57:21 +00003026 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003027}
3028
3029/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003030 * Store the serial in printable form into buf; no more
3031 * than size characters will be written
3032 */
3033int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3034{
Paul Bakker23986e52011-04-24 08:57:21 +00003035 int ret;
3036 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003037 char *p;
3038
3039 p = buf;
3040 n = size;
3041
3042 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003043 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003044
3045 for( i = 0; i < nr; i++ )
3046 {
Paul Bakker93048802011-12-05 14:38:06 +00003047 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003048 continue;
3049
Paul Bakkerdd476992011-01-16 21:34:59 +00003050 ret = snprintf( p, n, "%02X%s",
3051 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3052 SAFE_SNPRINTF();
3053 }
3054
Paul Bakker03c7c252011-11-25 12:37:37 +00003055 if( nr != serial->len )
3056 {
3057 ret = snprintf( p, n, "...." );
3058 SAFE_SNPRINTF();
3059 }
3060
Paul Bakker23986e52011-04-24 08:57:21 +00003061 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003062}
3063
3064/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003065 * Helper for writing "RSA key size", "EC key size", etc
3066 */
3067static int x509_key_size_helper( char *buf, size_t size, const char *name )
3068{
3069 char *p = buf;
3070 size_t n = size;
3071 int ret;
3072
3073 if( strlen( name ) + sizeof( " key size" ) > size )
3074 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3075
3076 ret = snprintf( p, n, "%s key size", name );
3077 SAFE_SNPRINTF();
3078
3079 return( 0 );
3080}
3081
3082/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003083 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003085#define BEFORE_COLON 14
3086#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003087int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3088 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089{
Paul Bakker23986e52011-04-24 08:57:21 +00003090 int ret;
3091 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003092 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003093 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003094 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
3096 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003097 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098
Paul Bakkerd98030e2009-05-02 15:13:40 +00003099 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003101 SAFE_SNPRINTF();
3102 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003104 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Paul Bakkerdd476992011-01-16 21:34:59 +00003106 ret = x509parse_serial_gets( p, n, &crt->serial);
3107 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Paul Bakkerd98030e2009-05-02 15:13:40 +00003109 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3110 SAFE_SNPRINTF();
3111 ret = x509parse_dn_gets( p, n, &crt->issuer );
3112 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakkerd98030e2009-05-02 15:13:40 +00003114 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3115 SAFE_SNPRINTF();
3116 ret = x509parse_dn_gets( p, n, &crt->subject );
3117 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Paul Bakkerd98030e2009-05-02 15:13:40 +00003119 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3121 crt->valid_from.year, crt->valid_from.mon,
3122 crt->valid_from.day, crt->valid_from.hour,
3123 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003124 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
Paul Bakkerd98030e2009-05-02 15:13:40 +00003126 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3128 crt->valid_to.year, crt->valid_to.mon,
3129 crt->valid_to.day, crt->valid_to.hour,
3130 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003131 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
Paul Bakkerc70b9822013-04-07 22:00:46 +02003133 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003134 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Paul Bakkerc70b9822013-04-07 22:00:46 +02003136 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3137 if( ret != 0 )
3138 ret = snprintf( p, n, "???" );
3139 else
3140 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003141 SAFE_SNPRINTF();
3142
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003143 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003144 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003145 {
3146 return( ret );
3147 }
3148
3149 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003150 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003151 SAFE_SNPRINTF();
3152
Paul Bakker23986e52011-04-24 08:57:21 +00003153 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003154}
3155
Paul Bakker74111d32011-01-15 16:57:55 +00003156/*
3157 * Return an informational string describing the given OID
3158 */
3159const char *x509_oid_get_description( x509_buf *oid )
3160{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003161 const char *desc = NULL;
3162 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003163
Paul Bakkerc70b9822013-04-07 22:00:46 +02003164 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003165
Paul Bakkerc70b9822013-04-07 22:00:46 +02003166 if( ret != 0 )
3167 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003168
Paul Bakkerc70b9822013-04-07 22:00:46 +02003169 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003170}
3171
3172/* Return the x.y.z.... style numeric string for the given OID */
3173int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3174{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003175 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003176}
3177
Paul Bakkerd98030e2009-05-02 15:13:40 +00003178/*
3179 * Return an informational string about the CRL.
3180 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003181int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3182 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003183{
Paul Bakker23986e52011-04-24 08:57:21 +00003184 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003185 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003186 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003187 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003188 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003189
3190 p = buf;
3191 n = size;
3192
3193 ret = snprintf( p, n, "%sCRL version : %d",
3194 prefix, crl->version );
3195 SAFE_SNPRINTF();
3196
3197 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3198 SAFE_SNPRINTF();
3199 ret = x509parse_dn_gets( p, n, &crl->issuer );
3200 SAFE_SNPRINTF();
3201
3202 ret = snprintf( p, n, "\n%sthis update : " \
3203 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3204 crl->this_update.year, crl->this_update.mon,
3205 crl->this_update.day, crl->this_update.hour,
3206 crl->this_update.min, crl->this_update.sec );
3207 SAFE_SNPRINTF();
3208
3209 ret = snprintf( p, n, "\n%snext update : " \
3210 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3211 crl->next_update.year, crl->next_update.mon,
3212 crl->next_update.day, crl->next_update.hour,
3213 crl->next_update.min, crl->next_update.sec );
3214 SAFE_SNPRINTF();
3215
3216 entry = &crl->entry;
3217
3218 ret = snprintf( p, n, "\n%sRevoked certificates:",
3219 prefix );
3220 SAFE_SNPRINTF();
3221
Paul Bakker9be19372009-07-27 20:21:53 +00003222 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003223 {
3224 ret = snprintf( p, n, "\n%sserial number: ",
3225 prefix );
3226 SAFE_SNPRINTF();
3227
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003228 ret = x509parse_serial_gets( p, n, &entry->serial);
3229 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003230
Paul Bakkerd98030e2009-05-02 15:13:40 +00003231 ret = snprintf( p, n, " revocation date: " \
3232 "%04d-%02d-%02d %02d:%02d:%02d",
3233 entry->revocation_date.year, entry->revocation_date.mon,
3234 entry->revocation_date.day, entry->revocation_date.hour,
3235 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003236 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003237
3238 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 }
3240
Paul Bakkerc70b9822013-04-07 22:00:46 +02003241 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003242 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
Paul Bakkerc70b9822013-04-07 22:00:46 +02003244 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3245 if( ret != 0 )
3246 ret = snprintf( p, n, "???" );
3247 else
3248 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249 SAFE_SNPRINTF();
3250
Paul Bakker1e27bb22009-07-19 20:25:25 +00003251 ret = snprintf( p, n, "\n" );
3252 SAFE_SNPRINTF();
3253
Paul Bakker23986e52011-04-24 08:57:21 +00003254 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003255}
3256
3257/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003258 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003260#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003261int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003262{
Paul Bakkercce9d772011-11-18 14:26:47 +00003263 int year, mon, day;
3264 int hour, min, sec;
3265
3266#if defined(_WIN32)
3267 SYSTEMTIME st;
3268
3269 GetLocalTime(&st);
3270
3271 year = st.wYear;
3272 mon = st.wMonth;
3273 day = st.wDay;
3274 hour = st.wHour;
3275 min = st.wMinute;
3276 sec = st.wSecond;
3277#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 struct tm *lt;
3279 time_t tt;
3280
3281 tt = time( NULL );
3282 lt = localtime( &tt );
3283
Paul Bakkercce9d772011-11-18 14:26:47 +00003284 year = lt->tm_year + 1900;
3285 mon = lt->tm_mon + 1;
3286 day = lt->tm_mday;
3287 hour = lt->tm_hour;
3288 min = lt->tm_min;
3289 sec = lt->tm_sec;
3290#endif
3291
3292 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003293 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Paul Bakkercce9d772011-11-18 14:26:47 +00003295 if( year == to->year &&
3296 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003297 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
Paul Bakkercce9d772011-11-18 14:26:47 +00003299 if( year == to->year &&
3300 mon == to->mon &&
3301 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003302 return( 1 );
3303
Paul Bakkercce9d772011-11-18 14:26:47 +00003304 if( year == to->year &&
3305 mon == to->mon &&
3306 day == to->day &&
3307 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003308 return( 1 );
3309
Paul Bakkercce9d772011-11-18 14:26:47 +00003310 if( year == to->year &&
3311 mon == to->mon &&
3312 day == to->day &&
3313 hour == to->hour &&
3314 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003315 return( 1 );
3316
Paul Bakkercce9d772011-11-18 14:26:47 +00003317 if( year == to->year &&
3318 mon == to->mon &&
3319 day == to->day &&
3320 hour == to->hour &&
3321 min == to->min &&
3322 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003323 return( 1 );
3324
Paul Bakker40ea7de2009-05-03 10:18:48 +00003325 return( 0 );
3326}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003327#else /* POLARSSL_HAVE_TIME */
3328int x509parse_time_expired( const x509_time *to )
3329{
3330 ((void) to);
3331 return( 0 );
3332}
3333#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003334
3335/*
3336 * Return 1 if the certificate is revoked, or 0 otherwise.
3337 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003338int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003339{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003340 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003341
3342 while( cur != NULL && cur->serial.len != 0 )
3343 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003344 if( crt->serial.len == cur->serial.len &&
3345 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003346 {
3347 if( x509parse_time_expired( &cur->revocation_date ) )
3348 return( 1 );
3349 }
3350
3351 cur = cur->next;
3352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
3354 return( 0 );
3355}
3356
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003357/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003358 * Check that the given certificate is valid accoring to the CRL.
3359 */
3360static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3361 x509_crl *crl_list)
3362{
3363 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003364 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3365 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003366
Paul Bakker915275b2012-09-28 07:10:55 +00003367 if( ca == NULL )
3368 return( flags );
3369
Paul Bakker76fd75a2011-01-16 21:12:10 +00003370 /*
3371 * TODO: What happens if no CRL is present?
3372 * Suggestion: Revocation state should be unknown if no CRL is present.
3373 * For backwards compatibility this is not yet implemented.
3374 */
3375
Paul Bakker915275b2012-09-28 07:10:55 +00003376 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003377 {
Paul Bakker915275b2012-09-28 07:10:55 +00003378 if( crl_list->version == 0 ||
3379 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003380 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3381 crl_list->issuer_raw.len ) != 0 )
3382 {
3383 crl_list = crl_list->next;
3384 continue;
3385 }
3386
3387 /*
3388 * Check if CRL is correctly signed by the trusted CA
3389 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003390 md_info = md_info_from_type( crl_list->sig_md );
3391 if( md_info == NULL )
3392 {
3393 /*
3394 * Cannot check 'unknown' hash
3395 */
3396 flags |= BADCRL_NOT_TRUSTED;
3397 break;
3398 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003399
Paul Bakkerc70b9822013-04-07 22:00:46 +02003400 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003401
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003402 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
3403 pk_verify( &ca->pk, hash, md_info,
3404 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003405 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003406 flags |= BADCRL_NOT_TRUSTED;
3407 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003408 }
3409
3410 /*
3411 * Check for validity of CRL (Do not drop out)
3412 */
3413 if( x509parse_time_expired( &crl_list->next_update ) )
3414 flags |= BADCRL_EXPIRED;
3415
3416 /*
3417 * Check if certificate is revoked
3418 */
3419 if( x509parse_revoked(crt, crl_list) )
3420 {
3421 flags |= BADCERT_REVOKED;
3422 break;
3423 }
3424
3425 crl_list = crl_list->next;
3426 }
3427 return flags;
3428}
3429
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003430static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003431{
3432 size_t i;
3433 size_t cn_idx = 0;
3434
Paul Bakker57b12982012-02-11 17:38:38 +00003435 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003436 return( 0 );
3437
3438 for( i = 0; i < strlen( cn ); ++i )
3439 {
3440 if( cn[i] == '.' )
3441 {
3442 cn_idx = i;
3443 break;
3444 }
3445 }
3446
3447 if( cn_idx == 0 )
3448 return( 0 );
3449
Paul Bakker535e97d2012-08-23 10:49:55 +00003450 if( strlen( cn ) - cn_idx == name->len - 1 &&
3451 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003452 {
3453 return( 1 );
3454 }
3455
3456 return( 0 );
3457}
3458
Paul Bakker915275b2012-09-28 07:10:55 +00003459static int x509parse_verify_top(
3460 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003461 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003462 int (*f_vrfy)(void *, x509_cert *, int, int *),
3463 void *p_vrfy )
3464{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003465 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003466 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003467 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3468 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003469
3470 if( x509parse_time_expired( &child->valid_to ) )
3471 *flags |= BADCERT_EXPIRED;
3472
3473 /*
3474 * Child is the top of the chain. Check against the trust_ca list.
3475 */
3476 *flags |= BADCERT_NOT_TRUSTED;
3477
3478 while( trust_ca != NULL )
3479 {
3480 if( trust_ca->version == 0 ||
3481 child->issuer_raw.len != trust_ca->subject_raw.len ||
3482 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3483 child->issuer_raw.len ) != 0 )
3484 {
3485 trust_ca = trust_ca->next;
3486 continue;
3487 }
3488
Paul Bakker9a736322012-11-14 12:39:52 +00003489 /*
3490 * Reduce path_len to check against if top of the chain is
3491 * the same as the trusted CA
3492 */
3493 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3494 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003495 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003496 {
3497 check_path_cnt--;
3498 }
3499
Paul Bakker915275b2012-09-28 07:10:55 +00003500 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003501 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003502 {
3503 trust_ca = trust_ca->next;
3504 continue;
3505 }
3506
Paul Bakkerc70b9822013-04-07 22:00:46 +02003507 md_info = md_info_from_type( child->sig_md );
3508 if( md_info == NULL )
3509 {
3510 /*
3511 * Cannot check 'unknown' hash
3512 */
Paul Bakker3a074a72013-08-20 12:45:03 +02003513 trust_ca = trust_ca->next;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003514 continue;
3515 }
Paul Bakker915275b2012-09-28 07:10:55 +00003516
Paul Bakkerc70b9822013-04-07 22:00:46 +02003517 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003518
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003519 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
3520 pk_verify( &trust_ca->pk, hash, md_info,
3521 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003522 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003523 trust_ca = trust_ca->next;
3524 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003525 }
3526
3527 /*
3528 * Top of chain is signed by a trusted CA
3529 */
3530 *flags &= ~BADCERT_NOT_TRUSTED;
3531 break;
3532 }
3533
Paul Bakker9a736322012-11-14 12:39:52 +00003534 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003535 * If top of chain is not the same as the trusted CA send a verify request
3536 * to the callback for any issues with validity and CRL presence for the
3537 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003538 */
3539 if( trust_ca != NULL &&
3540 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3541 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3542 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003543 {
3544 /* Check trusted CA's CRL for then chain's top crt */
3545 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3546
3547 if( x509parse_time_expired( &trust_ca->valid_to ) )
3548 ca_flags |= BADCERT_EXPIRED;
3549
Paul Bakker915275b2012-09-28 07:10:55 +00003550 if( NULL != f_vrfy )
3551 {
Paul Bakker9a736322012-11-14 12:39:52 +00003552 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003553 return( ret );
3554 }
3555 }
3556
3557 /* Call callback on top cert */
3558 if( NULL != f_vrfy )
3559 {
Paul Bakker9a736322012-11-14 12:39:52 +00003560 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003561 return( ret );
3562 }
3563
Paul Bakker915275b2012-09-28 07:10:55 +00003564 *flags |= ca_flags;
3565
3566 return( 0 );
3567}
3568
3569static int x509parse_verify_child(
3570 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003571 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003572 int (*f_vrfy)(void *, x509_cert *, int, int *),
3573 void *p_vrfy )
3574{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003575 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003576 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003577 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003578 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003579 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003580
3581 if( x509parse_time_expired( &child->valid_to ) )
3582 *flags |= BADCERT_EXPIRED;
3583
Paul Bakkerc70b9822013-04-07 22:00:46 +02003584 md_info = md_info_from_type( child->sig_md );
3585 if( md_info == NULL )
3586 {
3587 /*
3588 * Cannot check 'unknown' hash
3589 */
Paul Bakker915275b2012-09-28 07:10:55 +00003590 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003591 }
3592 else
3593 {
3594 md( md_info, child->tbs.p, child->tbs.len, hash );
3595
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003596 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
3597 pk_verify( &parent->pk, hash, md_info,
3598 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003599 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003600 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003601 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003602 }
3603
Paul Bakker915275b2012-09-28 07:10:55 +00003604 /* Check trusted CA's CRL for the given crt */
3605 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3606
3607 grandparent = parent->next;
3608
3609 while( grandparent != NULL )
3610 {
3611 if( grandparent->version == 0 ||
3612 grandparent->ca_istrue == 0 ||
3613 parent->issuer_raw.len != grandparent->subject_raw.len ||
3614 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3615 parent->issuer_raw.len ) != 0 )
3616 {
3617 grandparent = grandparent->next;
3618 continue;
3619 }
3620 break;
3621 }
3622
Paul Bakker915275b2012-09-28 07:10:55 +00003623 if( grandparent != NULL )
3624 {
3625 /*
3626 * Part of the chain
3627 */
Paul Bakker9a736322012-11-14 12:39:52 +00003628 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 +00003629 if( ret != 0 )
3630 return( ret );
3631 }
3632 else
3633 {
Paul Bakker9a736322012-11-14 12:39:52 +00003634 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 +00003635 if( ret != 0 )
3636 return( ret );
3637 }
3638
3639 /* child is verified to be a child of the parent, call verify callback */
3640 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003641 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003642 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003643
3644 *flags |= parent_flags;
3645
3646 return( 0 );
3647}
3648
Paul Bakker76fd75a2011-01-16 21:12:10 +00003649/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 * Verify the certificate validity
3651 */
3652int x509parse_verify( x509_cert *crt,
3653 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003654 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003655 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003656 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003657 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658{
Paul Bakker23986e52011-04-24 08:57:21 +00003659 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003660 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003661 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003662 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003664 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
Paul Bakker40ea7de2009-05-03 10:18:48 +00003666 *flags = 0;
3667
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 if( cn != NULL )
3669 {
3670 name = &crt->subject;
3671 cn_len = strlen( cn );
3672
Paul Bakker4d2c1242012-05-10 14:12:46 +00003673 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003675 cur = &crt->subject_alt_names;
3676
3677 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003678 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003679 if( cur->buf.len == cn_len &&
3680 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003681 break;
3682
Paul Bakker535e97d2012-08-23 10:49:55 +00003683 if( cur->buf.len > 2 &&
3684 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003685 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003686 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Paul Bakker4d2c1242012-05-10 14:12:46 +00003688 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003689 }
3690
3691 if( cur == NULL )
3692 *flags |= BADCERT_CN_MISMATCH;
3693 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003694 else
3695 {
3696 while( name != NULL )
3697 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003698 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003699 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003700 if( name->val.len == cn_len &&
3701 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003702 break;
3703
Paul Bakker535e97d2012-08-23 10:49:55 +00003704 if( name->val.len > 2 &&
3705 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003706 x509_wildcard_verify( cn, &name->val ) )
3707 break;
3708 }
3709
3710 name = name->next;
3711 }
3712
3713 if( name == NULL )
3714 *flags |= BADCERT_CN_MISMATCH;
3715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003719 * Iterate upwards in the given cert chain, to find our crt parent.
3720 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003722 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Paul Bakker76fd75a2011-01-16 21:12:10 +00003724 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003726 if( parent->ca_istrue == 0 ||
3727 crt->issuer_raw.len != parent->subject_raw.len ||
3728 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 crt->issuer_raw.len ) != 0 )
3730 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003731 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 continue;
3733 }
Paul Bakker915275b2012-09-28 07:10:55 +00003734 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 }
3736
Paul Bakker915275b2012-09-28 07:10:55 +00003737 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 {
Paul Bakker915275b2012-09-28 07:10:55 +00003739 /*
3740 * Part of the chain
3741 */
Paul Bakker9a736322012-11-14 12:39:52 +00003742 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003743 if( ret != 0 )
3744 return( ret );
3745 }
3746 else
Paul Bakker74111d32011-01-15 16:57:55 +00003747 {
Paul Bakker9a736322012-11-14 12:39:52 +00003748 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003749 if( ret != 0 )
3750 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003751 }
Paul Bakker915275b2012-09-28 07:10:55 +00003752
3753 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003754 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003755
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 return( 0 );
3757}
3758
3759/*
3760 * Unallocate all certificate data
3761 */
3762void x509_free( x509_cert *crt )
3763{
3764 x509_cert *cert_cur = crt;
3765 x509_cert *cert_prv;
3766 x509_name *name_cur;
3767 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003768 x509_sequence *seq_cur;
3769 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003770
3771 if( crt == NULL )
3772 return;
3773
3774 do
3775 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003776 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777
3778 name_cur = cert_cur->issuer.next;
3779 while( name_cur != NULL )
3780 {
3781 name_prv = name_cur;
3782 name_cur = name_cur->next;
3783 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003784 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 }
3786
3787 name_cur = cert_cur->subject.next;
3788 while( name_cur != NULL )
3789 {
3790 name_prv = name_cur;
3791 name_cur = name_cur->next;
3792 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003793 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Paul Bakker74111d32011-01-15 16:57:55 +00003796 seq_cur = cert_cur->ext_key_usage.next;
3797 while( seq_cur != NULL )
3798 {
3799 seq_prv = seq_cur;
3800 seq_cur = seq_cur->next;
3801 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003802 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003803 }
3804
Paul Bakker8afa70d2012-02-11 18:42:45 +00003805 seq_cur = cert_cur->subject_alt_names.next;
3806 while( seq_cur != NULL )
3807 {
3808 seq_prv = seq_cur;
3809 seq_cur = seq_cur->next;
3810 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003811 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003812 }
3813
Paul Bakker5121ce52009-01-03 21:22:43 +00003814 if( cert_cur->raw.p != NULL )
3815 {
3816 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003817 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 }
3819
3820 cert_cur = cert_cur->next;
3821 }
3822 while( cert_cur != NULL );
3823
3824 cert_cur = crt;
3825 do
3826 {
3827 cert_prv = cert_cur;
3828 cert_cur = cert_cur->next;
3829
3830 memset( cert_prv, 0, sizeof( x509_cert ) );
3831 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003832 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 }
3834 while( cert_cur != NULL );
3835}
3836
Paul Bakkerd98030e2009-05-02 15:13:40 +00003837/*
3838 * Unallocate all CRL data
3839 */
3840void x509_crl_free( x509_crl *crl )
3841{
3842 x509_crl *crl_cur = crl;
3843 x509_crl *crl_prv;
3844 x509_name *name_cur;
3845 x509_name *name_prv;
3846 x509_crl_entry *entry_cur;
3847 x509_crl_entry *entry_prv;
3848
3849 if( crl == NULL )
3850 return;
3851
3852 do
3853 {
3854 name_cur = crl_cur->issuer.next;
3855 while( name_cur != NULL )
3856 {
3857 name_prv = name_cur;
3858 name_cur = name_cur->next;
3859 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003860 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003861 }
3862
3863 entry_cur = crl_cur->entry.next;
3864 while( entry_cur != NULL )
3865 {
3866 entry_prv = entry_cur;
3867 entry_cur = entry_cur->next;
3868 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003869 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003870 }
3871
3872 if( crl_cur->raw.p != NULL )
3873 {
3874 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003875 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003876 }
3877
3878 crl_cur = crl_cur->next;
3879 }
3880 while( crl_cur != NULL );
3881
3882 crl_cur = crl;
3883 do
3884 {
3885 crl_prv = crl_cur;
3886 crl_cur = crl_cur->next;
3887
3888 memset( crl_prv, 0, sizeof( x509_crl ) );
3889 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003890 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003891 }
3892 while( crl_cur != NULL );
3893}
3894
Paul Bakker40e46942009-01-03 21:51:57 +00003895#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003896
Paul Bakker40e46942009-01-03 21:51:57 +00003897#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003898
3899/*
3900 * Checkup routine
3901 */
3902int x509_self_test( int verbose )
3903{
Paul Bakker5690efc2011-05-26 13:16:06 +00003904#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003905 int ret;
3906 int flags;
3907 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 x509_cert cacert;
3909 x509_cert clicert;
3910 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003911#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003912 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
3915 if( verbose != 0 )
3916 printf( " X.509 certificate load: " );
3917
3918 memset( &clicert, 0, sizeof( x509_cert ) );
3919
Paul Bakker3c2122f2013-06-24 19:03:14 +02003920 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003921 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 if( ret != 0 )
3923 {
3924 if( verbose != 0 )
3925 printf( "failed\n" );
3926
3927 return( ret );
3928 }
3929
3930 memset( &cacert, 0, sizeof( x509_cert ) );
3931
Paul Bakker3c2122f2013-06-24 19:03:14 +02003932 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003933 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 if( ret != 0 )
3935 {
3936 if( verbose != 0 )
3937 printf( "failed\n" );
3938
3939 return( ret );
3940 }
3941
3942 if( verbose != 0 )
3943 printf( "passed\n X.509 private key load: " );
3944
3945 i = strlen( test_ca_key );
3946 j = strlen( test_ca_pwd );
3947
Paul Bakker66b78b22011-03-25 14:22:50 +00003948 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3949
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003950 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003951 (const unsigned char *) test_ca_key, i,
3952 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 {
3954 if( verbose != 0 )
3955 printf( "failed\n" );
3956
3957 return( ret );
3958 }
3959
3960 if( verbose != 0 )
3961 printf( "passed\n X.509 signature verify: ");
3962
Paul Bakker23986e52011-04-24 08:57:21 +00003963 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 if( ret != 0 )
3965 {
3966 if( verbose != 0 )
3967 printf( "failed\n" );
3968
3969 return( ret );
3970 }
3971
Paul Bakker5690efc2011-05-26 13:16:06 +00003972#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003974 printf( "passed\n X.509 DHM parameter load: " );
3975
3976 i = strlen( test_dhm_params );
3977 j = strlen( test_ca_pwd );
3978
Paul Bakker3c2122f2013-06-24 19:03:14 +02003979 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003980 {
3981 if( verbose != 0 )
3982 printf( "failed\n" );
3983
3984 return( ret );
3985 }
3986
3987 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003990
3991 x509_free( &cacert );
3992 x509_free( &clicert );
3993 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003994#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003995 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003996#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
3998 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003999#else
4000 ((void) verbose);
4001 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4002#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004003}
4004
4005#endif
4006
4007#endif