blob: a4ee6b5489d7f7796e37cc2198bff418997b8fa0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200163/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200164 *
165 * AlgorithmIdentifier ::= SEQUENCE {
166 * algorithm OBJECT IDENTIFIER,
167 * parameters ANY DEFINED BY algorithm OPTIONAL }
168 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200169static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200170 const unsigned char *end,
171 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200172{
173 int ret;
174 x509_buf alg_oid;
175
176 memset( params, 0, sizeof(asn1_buf) );
177
178 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
179 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
180
181 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
182 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
183
184 /*
185 * No parameters with RSA (only for EC)
186 */
187 if( *pk_alg == POLARSSL_PK_RSA &&
188 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
189 params->len != 0 ) )
190 {
191 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
192 }
193
194 return( 0 );
195}
196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197/* Get an algorithm identifier without parameters (eg for signatures)
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * AlgorithmIdentifier ::= SEQUENCE {
200 * algorithm OBJECT IDENTIFIER,
201 * parameters ANY DEFINED BY algorithm OPTIONAL }
202 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200203static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
204 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200208 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return( 0 );
212}
213
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200214
215#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200216/* Get an EC group id from an ECParameters buffer
217 *
218 * ECParameters ::= CHOICE {
219 * namedCurve OBJECT IDENTIFIER
220 * -- implicitCurve NULL
221 * -- specifiedCurve SpecifiedECDomain
222 * }
223 */
224static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200225 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200226{
227 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200228
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200229 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200230
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200231 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200232 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
233
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200234 params->p = *p;
235 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200236
237 if( *p != end )
238 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
239 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
240
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200241 return( 0 );
242}
243
244/*
245 * Use EC parameters to initialise an EC group
246 */
247static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
248{
249 int ret;
250 ecp_group_id grp_id;
251
252 if( oid_get_ec_grp( params, &grp_id ) != 0 )
253 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
254
255 /*
256 * grp may already be initilialized; if so, make sure IDs match
257 */
258 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
259 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
260
261 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
262 return( ret );
263
264 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200265}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200266#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * AttributeTypeAndValue ::= SEQUENCE {
270 * type AttributeType,
271 * value AttributeValue }
272 *
273 * AttributeType ::= OBJECT IDENTIFIER
274 *
275 * AttributeValue ::= ANY DEFINED BY AttributeType
276 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277static int x509_get_attr_type_value( unsigned char **p,
278 const unsigned char *end,
279 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 x509_buf *oid;
284 x509_buf *val;
285
286 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000288 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200290 if( ( end - *p ) < 1 )
291 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
292 POLARSSL_ERR_ASN1_OUT_OF_DATA );
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 oid = &cur->oid;
295 oid->tag = **p;
296
297 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000298 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 oid->p = *p;
301 *p += oid->len;
302
303 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000304 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000305 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
308 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
309 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000311 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 val = &cur->val;
314 val->tag = *(*p)++;
315
316 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319 val->p = *p;
320 *p += val->len;
321
322 cur->next = NULL;
323
Paul Bakker400ff6f2011-02-20 10:40:16 +0000324 return( 0 );
325}
326
327/*
328 * RelativeDistinguishedName ::=
329 * SET OF AttributeTypeAndValue
330 *
331 * AttributeTypeAndValue ::= SEQUENCE {
332 * type AttributeType,
333 * value AttributeValue }
334 *
335 * AttributeType ::= OBJECT IDENTIFIER
336 *
337 * AttributeValue ::= ANY DEFINED BY AttributeType
338 */
339static int x509_get_name( unsigned char **p,
340 const unsigned char *end,
341 x509_name *cur )
342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret;
344 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000345 const unsigned char *end2;
346 x509_name *use;
347
348 if( ( ret = asn1_get_tag( p, end, &len,
349 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000350 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000351
352 end2 = end;
353 end = *p + len;
354 use = cur;
355
356 do
357 {
358 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
359 return( ret );
360
361 if( *p != end )
362 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200363 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000364 sizeof( x509_name ) );
365
366 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000367 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368
369 memset( use->next, 0, sizeof( x509_name ) );
370
371 use = use->next;
372 }
373 }
374 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 /*
377 * recurse until end of SEQUENCE is reached
378 */
379 if( *p == end2 )
380 return( 0 );
381
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 sizeof( x509_name ) );
384
385 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000386 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Paul Bakker430ffbe2012-05-01 08:14:20 +0000388 memset( cur->next, 0, sizeof( x509_name ) );
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 return( x509_get_name( p, end2, cur->next ) );
391}
392
393/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * Time ::= CHOICE {
395 * utcTime UTCTime,
396 * generalTime GeneralizedTime }
397 */
Paul Bakker91200182010-02-18 21:26:15 +0000398static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000400 x509_time *time )
401{
Paul Bakker23986e52011-04-24 08:57:21 +0000402 int ret;
403 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000405 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406
Paul Bakker91200182010-02-18 21:26:15 +0000407 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000408 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
409 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410
Paul Bakker91200182010-02-18 21:26:15 +0000411 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412
Paul Bakker91200182010-02-18 21:26:15 +0000413 if ( tag == ASN1_UTC_TIME )
414 {
415 (*p)++;
416 ret = asn1_get_len( p, end, &len );
417
418 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420
Paul Bakker91200182010-02-18 21:26:15 +0000421 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000422 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
423 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424
Paul Bakker91200182010-02-18 21:26:15 +0000425 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
426 &time->year, &time->mon, &time->day,
427 &time->hour, &time->min, &time->sec ) < 5 )
428 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429
Paul Bakker400ff6f2011-02-20 10:40:16 +0000430 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000431 time->year += 1900;
432
433 *p += len;
434
435 return( 0 );
436 }
437 else if ( tag == ASN1_GENERALIZED_TIME )
438 {
439 (*p)++;
440 ret = asn1_get_len( p, end, &len );
441
442 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000443 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000444
445 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000446 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
447 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000448
449 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
450 &time->year, &time->mon, &time->day,
451 &time->hour, &time->min, &time->sec ) < 5 )
452 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
453
454 *p += len;
455
456 return( 0 );
457 }
458 else
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000460}
461
462
463/*
464 * Validity ::= SEQUENCE {
465 * notBefore Time,
466 * notAfter Time }
467 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000469 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 x509_time *from,
471 x509_time *to )
472{
Paul Bakker23986e52011-04-24 08:57:21 +0000473 int ret;
474 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 if( ( ret = asn1_get_tag( p, end, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 end = *p + len;
481
Paul Bakker91200182010-02-18 21:26:15 +0000482 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker91200182010-02-18 21:26:15 +0000485 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000486 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200495#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000496/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200497 * RSAPublicKey ::= SEQUENCE {
498 * modulus INTEGER, -- n
499 * publicExponent INTEGER -- e
500 * }
501 */
502static int x509_get_rsapubkey( unsigned char **p,
503 const unsigned char *end,
504 rsa_context *rsa )
505{
506 int ret;
507 size_t len;
508
509 if( ( ret = asn1_get_tag( p, end, &len,
510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
511 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
512
513 if( *p + len != end )
514 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
515 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
516
517 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
518 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
519 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
520
Manuel Pégourié-Gonnardc13c0d42013-08-15 13:58:01 +0200521 if( *p != end )
522 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200525 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
526 return( ret );
527
528 rsa->len = mpi_size( &rsa->N );
529
530 return( 0 );
531}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200532#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200533
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200534#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200535/*
536 * EC public key is an EC point
537 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200538static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200539 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200540{
541 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200542
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200543 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200544 (const unsigned char *) *p, end - *p ) ) != 0 ||
545 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200546 {
547 ecp_keypair_free( key );
548 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200549 }
550
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200551 /*
552 * We know ecp_point_read_binary consumed all bytes
553 */
554 *p = (unsigned char *) end;
555
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200556 return( 0 );
557}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200558#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200559
560/*
561 * SubjectPublicKeyInfo ::= SEQUENCE {
562 * algorithm AlgorithmIdentifier,
563 * subjectPublicKey BIT STRING }
564 */
565static int x509_get_pubkey( unsigned char **p,
566 const unsigned char *end,
567 pk_context *pk )
568{
569 int ret;
570 size_t len;
571 x509_buf alg_params;
572 pk_type_t pk_alg = POLARSSL_PK_NONE;
573
574 if( ( ret = asn1_get_tag( p, end, &len,
575 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576 {
577 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
578 }
579
580 end = *p + len;
581
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200582 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200583 return( ret );
584
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200585 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200586 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
587
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200588 if( *p + len != end )
589 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
590 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
591
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200592 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
593 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200594
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200595#if defined(POLARSSL_RSA_C)
596 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200597 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200598 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
599 } else
600#endif /* POLARSSL_RSA_C */
601#if defined(POLARSSL_ECP_C)
602 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
603 {
604 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp ) ||
605 x509_get_ecpubkey( p, end, pk_ec( *pk ) );
606 } else
607#endif /* POLARSSL_ECP_C */
608 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200609
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200610 if( ret == 0 && *p != end )
611 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
612 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
613
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200614 if( ret != 0 )
615 pk_free( pk );
616
617 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200618}
619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000621 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 x509_buf *sig )
623{
Paul Bakker23986e52011-04-24 08:57:21 +0000624 int ret;
625 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker8afa70d2012-02-11 18:42:45 +0000627 if( ( end - *p ) < 1 )
628 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
629 POLARSSL_ERR_ASN1_OUT_OF_DATA );
630
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 sig->tag = **p;
632
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200633 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 sig->len = len;
637 sig->p = *p;
638
639 *p += len;
640
641 return( 0 );
642}
643
644/*
645 * X.509 v2/v3 unique identifier (not parsed)
646 */
647static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000648 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 x509_buf *uid, int n )
650{
651 int ret;
652
653 if( *p == end )
654 return( 0 );
655
656 uid->tag = **p;
657
658 if( ( ret = asn1_get_tag( p, end, &uid->len,
659 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
660 {
Paul Bakker40e46942009-01-03 21:51:57 +0000661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 return( 0 );
663
664 return( ret );
665 }
666
667 uid->p = *p;
668 *p += uid->len;
669
670 return( 0 );
671}
672
673/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000674 * X.509 Extensions (No parsing of extensions, pointer should
675 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
677static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000678 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000679 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000680{
Paul Bakker23986e52011-04-24 08:57:21 +0000681 int ret;
682 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684 if( *p == end )
685 return( 0 );
686
687 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000688
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000690 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693 ext->p = *p;
694 end = *p + ext->len;
695
696 /*
697 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
698 *
699 * Extension ::= SEQUENCE {
700 * extnID OBJECT IDENTIFIER,
701 * critical BOOLEAN DEFAULT FALSE,
702 * extnValue OCTET STRING }
703 */
704 if( ( ret = asn1_get_tag( p, end, &len,
705 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000710 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
Paul Bakkerd98030e2009-05-02 15:13:40 +0000712 return( 0 );
713}
714
715/*
716 * X.509 CRL v2 extensions (no extensions parsed yet.)
717 */
718static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000719 const unsigned char *end,
720 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000721{
Paul Bakker23986e52011-04-24 08:57:21 +0000722 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000723 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000724
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000725 /* Get explicit tag */
726 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000727 {
728 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
729 return( 0 );
730
731 return( ret );
732 }
733
734 while( *p < end )
735 {
736 if( ( ret = asn1_get_tag( p, end, &len,
737 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000739
740 *p += len;
741 }
742
743 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000744 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000745 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
746
747 return( 0 );
748}
749
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000750/*
751 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
752 */
753static int x509_get_crl_entry_ext( unsigned char **p,
754 const unsigned char *end,
755 x509_buf *ext )
756{
757 int ret;
758 size_t len = 0;
759
760 /* OPTIONAL */
761 if (end <= *p)
762 return( 0 );
763
764 ext->tag = **p;
765 ext->p = *p;
766
767 /*
768 * Get CRL-entry extension sequence header
769 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
770 */
771 if( ( ret = asn1_get_tag( p, end, &ext->len,
772 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
773 {
774 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
775 {
776 ext->p = NULL;
777 return( 0 );
778 }
779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
780 }
781
782 end = *p + ext->len;
783
784 if( end != *p + ext->len )
785 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
786 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
787
788 while( *p < end )
789 {
790 if( ( ret = asn1_get_tag( p, end, &len,
791 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
792 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
793
794 *p += len;
795 }
796
797 if( *p != end )
798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
799 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
800
801 return( 0 );
802}
803
Paul Bakker74111d32011-01-15 16:57:55 +0000804static int x509_get_basic_constraints( unsigned char **p,
805 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000806 int *ca_istrue,
807 int *max_pathlen )
808{
Paul Bakker23986e52011-04-24 08:57:21 +0000809 int ret;
810 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000811
812 /*
813 * BasicConstraints ::= SEQUENCE {
814 * cA BOOLEAN DEFAULT FALSE,
815 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
816 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000817 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000818 *max_pathlen = 0; /* endless */
819
820 if( ( ret = asn1_get_tag( p, end, &len,
821 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000822 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000823
824 if( *p == end )
825 return 0;
826
Paul Bakker3cccddb2011-01-16 21:46:31 +0000827 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000828 {
829 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000830 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000831
832 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000834
Paul Bakker3cccddb2011-01-16 21:46:31 +0000835 if( *ca_istrue != 0 )
836 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000837 }
838
839 if( *p == end )
840 return 0;
841
842 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000844
845 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 (*max_pathlen)++;
850
Paul Bakker74111d32011-01-15 16:57:55 +0000851 return 0;
852}
853
854static int x509_get_ns_cert_type( unsigned char **p,
855 const unsigned char *end,
856 unsigned char *ns_cert_type)
857{
858 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000859 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000860
861 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000863
864 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000865 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000866 POLARSSL_ERR_ASN1_INVALID_LENGTH );
867
868 /* Get actual bitstring */
869 *ns_cert_type = *bs.p;
870 return 0;
871}
872
873static int x509_get_key_usage( unsigned char **p,
874 const unsigned char *end,
875 unsigned char *key_usage)
876{
877 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000878 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000879
880 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000881 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000882
Paul Bakker94a67962012-08-23 13:03:52 +0000883 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000884 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000885 POLARSSL_ERR_ASN1_INVALID_LENGTH );
886
887 /* Get actual bitstring */
888 *key_usage = *bs.p;
889 return 0;
890}
891
Paul Bakkerd98030e2009-05-02 15:13:40 +0000892/*
Paul Bakker74111d32011-01-15 16:57:55 +0000893 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
894 *
895 * KeyPurposeId ::= OBJECT IDENTIFIER
896 */
897static int x509_get_ext_key_usage( unsigned char **p,
898 const unsigned char *end,
899 x509_sequence *ext_key_usage)
900{
901 int ret;
902
903 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000905
906 /* Sequence length must be >= 1 */
907 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000908 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000909 POLARSSL_ERR_ASN1_INVALID_LENGTH );
910
911 return 0;
912}
913
914/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000915 * SubjectAltName ::= GeneralNames
916 *
917 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
918 *
919 * GeneralName ::= CHOICE {
920 * otherName [0] OtherName,
921 * rfc822Name [1] IA5String,
922 * dNSName [2] IA5String,
923 * x400Address [3] ORAddress,
924 * directoryName [4] Name,
925 * ediPartyName [5] EDIPartyName,
926 * uniformResourceIdentifier [6] IA5String,
927 * iPAddress [7] OCTET STRING,
928 * registeredID [8] OBJECT IDENTIFIER }
929 *
930 * OtherName ::= SEQUENCE {
931 * type-id OBJECT IDENTIFIER,
932 * value [0] EXPLICIT ANY DEFINED BY type-id }
933 *
934 * EDIPartyName ::= SEQUENCE {
935 * nameAssigner [0] DirectoryString OPTIONAL,
936 * partyName [1] DirectoryString }
937 *
938 * NOTE: PolarSSL only parses and uses dNSName at this point.
939 */
940static int x509_get_subject_alt_name( unsigned char **p,
941 const unsigned char *end,
942 x509_sequence *subject_alt_name )
943{
944 int ret;
945 size_t len, tag_len;
946 asn1_buf *buf;
947 unsigned char tag;
948 asn1_sequence *cur = subject_alt_name;
949
950 /* Get main sequence tag */
951 if( ( ret = asn1_get_tag( p, end, &len,
952 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
954
955 if( *p + len != end )
956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
957 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
958
959 while( *p < end )
960 {
961 if( ( end - *p ) < 1 )
962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
963 POLARSSL_ERR_ASN1_OUT_OF_DATA );
964
965 tag = **p;
966 (*p)++;
967 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
968 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
969
970 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
971 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
972 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
973
974 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
975 {
976 *p += tag_len;
977 continue;
978 }
979
980 buf = &(cur->buf);
981 buf->tag = tag;
982 buf->p = *p;
983 buf->len = tag_len;
984 *p += buf->len;
985
986 /* Allocate and assign next pointer */
987 if (*p < end)
988 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200989 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000990 sizeof( asn1_sequence ) );
991
992 if( cur->next == NULL )
993 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
994 POLARSSL_ERR_ASN1_MALLOC_FAILED );
995
Paul Bakker535e97d2012-08-23 10:49:55 +0000996 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000997 cur = cur->next;
998 }
999 }
1000
1001 /* Set final sequence entry's next pointer to NULL */
1002 cur->next = NULL;
1003
1004 if( *p != end )
1005 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1006 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1007
1008 return( 0 );
1009}
1010
1011/*
Paul Bakker74111d32011-01-15 16:57:55 +00001012 * X.509 v3 extensions
1013 *
1014 * TODO: Perform all of the basic constraints tests required by the RFC
1015 * TODO: Set values for undetected extensions to a sane default?
1016 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001017 */
1018static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001019 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001020 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001021{
Paul Bakker23986e52011-04-24 08:57:21 +00001022 int ret;
1023 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001024 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001026 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001027 {
1028 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1029 return( 0 );
1030
1031 return( ret );
1032 }
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 while( *p < end )
1035 {
Paul Bakker74111d32011-01-15 16:57:55 +00001036 /*
1037 * Extension ::= SEQUENCE {
1038 * extnID OBJECT IDENTIFIER,
1039 * critical BOOLEAN DEFAULT FALSE,
1040 * extnValue OCTET STRING }
1041 */
1042 x509_buf extn_oid = {0, 0, NULL};
1043 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001045
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 if( ( ret = asn1_get_tag( p, end, &len,
1047 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001048 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001050 end_ext_data = *p + len;
1051
Paul Bakker74111d32011-01-15 16:57:55 +00001052 /* Get extension ID */
1053 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001054
Paul Bakker74111d32011-01-15 16:57:55 +00001055 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001056 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057
Paul Bakker74111d32011-01-15 16:57:55 +00001058 extn_oid.p = *p;
1059 *p += extn_oid.len;
1060
1061 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001062 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001063 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1064
1065 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001066 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001067 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001068 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Paul Bakker74111d32011-01-15 16:57:55 +00001070 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001071 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001073 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001075 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001076
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001077 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001079 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker74111d32011-01-15 16:57:55 +00001081 /*
1082 * Detect supported extensions
1083 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1085
1086 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001087 {
1088 /* No parser found, skip extension */
1089 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker5c721f92011-07-27 16:51:09 +00001091#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001092 if( is_critical )
1093 {
1094 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001095 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001096 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1097 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001098#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001099 continue;
1100 }
1101
1102 crt->ext_types |= ext_type;
1103
1104 switch( ext_type )
1105 {
1106 case EXT_BASIC_CONSTRAINTS:
1107 /* Parse basic constraints */
1108 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1109 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1110 return ( ret );
1111 break;
1112
1113 case EXT_KEY_USAGE:
1114 /* Parse key usage */
1115 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1116 &crt->key_usage ) ) != 0 )
1117 return ( ret );
1118 break;
1119
1120 case EXT_EXTENDED_KEY_USAGE:
1121 /* Parse extended key usage */
1122 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1123 &crt->ext_key_usage ) ) != 0 )
1124 return ( ret );
1125 break;
1126
1127 case EXT_SUBJECT_ALT_NAME:
1128 /* Parse subject alt name */
1129 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1130 &crt->subject_alt_names ) ) != 0 )
1131 return ( ret );
1132 break;
1133
1134 case EXT_NS_CERT_TYPE:
1135 /* Parse netscape certificate type */
1136 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1137 &crt->ns_cert_type ) ) != 0 )
1138 return ( ret );
1139 break;
1140
1141 default:
1142 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001143 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
1145
1146 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001147 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 return( 0 );
1151}
1152
1153/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001154 * X.509 CRL Entries
1155 */
1156static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001157 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001158 x509_crl_entry *entry )
1159{
Paul Bakker23986e52011-04-24 08:57:21 +00001160 int ret;
1161 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001162 x509_crl_entry *cur_entry = entry;
1163
1164 if( *p == end )
1165 return( 0 );
1166
Paul Bakker9be19372009-07-27 20:21:53 +00001167 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001168 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1169 {
1170 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1171 return( 0 );
1172
1173 return( ret );
1174 }
1175
Paul Bakker9be19372009-07-27 20:21:53 +00001176 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001177
1178 while( *p < end )
1179 {
Paul Bakker23986e52011-04-24 08:57:21 +00001180 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001181 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182
1183 if( ( ret = asn1_get_tag( p, end, &len2,
1184 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1185 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186 return( ret );
1187 }
1188
Paul Bakker9be19372009-07-27 20:21:53 +00001189 cur_entry->raw.tag = **p;
1190 cur_entry->raw.p = *p;
1191 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001192 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001193
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001194 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001195 return( ret );
1196
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001197 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001198 return( ret );
1199
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001200 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001201 return( ret );
1202
Paul Bakker74111d32011-01-15 16:57:55 +00001203 if ( *p < end )
1204 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001205 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001206
1207 if( cur_entry->next == NULL )
1208 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1209
Paul Bakkerd98030e2009-05-02 15:13:40 +00001210 cur_entry = cur_entry->next;
1211 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1212 }
1213 }
1214
1215 return( 0 );
1216}
1217
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1219 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001220{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001221 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001222
Paul Bakkerc70b9822013-04-07 22:00:46 +02001223 if( ret != 0 )
1224 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001225
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001227}
1228
Paul Bakkerd98030e2009-05-02 15:13:40 +00001229/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001230 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001232static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1233 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001234{
Paul Bakker23986e52011-04-24 08:57:21 +00001235 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001236 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001237 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001238
Paul Bakker320a4b52009-03-28 18:52:39 +00001239 /*
1240 * Check for valid input
1241 */
1242 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001243 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001244
Paul Bakker6e339b52013-07-03 13:37:05 +02001245 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001246
1247 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001248 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001249
1250 memcpy( p, buf, buflen );
1251
1252 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
1254 crt->raw.p = p;
1255 crt->raw.len = len;
1256 end = p + len;
1257
1258 /*
1259 * Certificate ::= SEQUENCE {
1260 * tbsCertificate TBSCertificate,
1261 * signatureAlgorithm AlgorithmIdentifier,
1262 * signatureValue BIT STRING }
1263 */
1264 if( ( ret = asn1_get_tag( &p, end, &len,
1265 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1266 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001267 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001268 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 }
1270
Paul Bakkerb00ca422012-09-25 12:10:00 +00001271 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001273 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001275 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001277 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 /*
1280 * TBSCertificate ::= SEQUENCE {
1281 */
1282 crt->tbs.p = p;
1283
1284 if( ( ret = asn1_get_tag( &p, end, &len,
1285 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1286 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001287 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001288 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
1290
1291 end = p + len;
1292 crt->tbs.len = end - crt->tbs.p;
1293
1294 /*
1295 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1296 *
1297 * CertificateSerialNumber ::= INTEGER
1298 *
1299 * signature AlgorithmIdentifier
1300 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001301 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1302 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1303 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308
1309 crt->version++;
1310
1311 if( crt->version > 3 )
1312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001314 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
1316
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1318 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001320 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001321 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323
1324 /*
1325 * issuer Name
1326 */
1327 crt->issuer_raw.p = p;
1328
1329 if( ( ret = asn1_get_tag( &p, end, &len,
1330 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1331 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001332 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001333 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 }
1335
1336 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1337 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001338 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 return( ret );
1340 }
1341
1342 crt->issuer_raw.len = p - crt->issuer_raw.p;
1343
1344 /*
1345 * Validity ::= SEQUENCE {
1346 * notBefore Time,
1347 * notAfter Time }
1348 *
1349 */
1350 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1351 &crt->valid_to ) ) != 0 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 return( ret );
1355 }
1356
1357 /*
1358 * subject Name
1359 */
1360 crt->subject_raw.p = p;
1361
1362 if( ( ret = asn1_get_tag( &p, end, &len,
1363 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1364 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001365 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001366 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Paul Bakkercefb3962012-06-27 11:51:09 +00001369 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001371 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 return( ret );
1373 }
1374
1375 crt->subject_raw.len = p - crt->subject_raw.p;
1376
1377 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001378 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001380 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001382 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 return( ret );
1384 }
1385
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 /*
1387 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1388 * -- If present, version shall be v2 or v3
1389 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1390 * -- If present, version shall be v2 or v3
1391 * extensions [3] EXPLICIT Extensions OPTIONAL
1392 * -- If present, version shall be v3
1393 */
1394 if( crt->version == 2 || crt->version == 3 )
1395 {
1396 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1397 if( ret != 0 )
1398 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001399 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 return( ret );
1401 }
1402 }
1403
1404 if( crt->version == 2 || crt->version == 3 )
1405 {
1406 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1407 if( ret != 0 )
1408 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001409 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 return( ret );
1411 }
1412 }
1413
1414 if( crt->version == 3 )
1415 {
Paul Bakker74111d32011-01-15 16:57:55 +00001416 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 if( ret != 0 )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 return( ret );
1421 }
1422 }
1423
1424 if( p != end )
1425 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001426 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001427 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001428 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429 }
1430
Paul Bakkerb00ca422012-09-25 12:10:00 +00001431 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001434 * }
1435 * -- end of TBSCertificate
1436 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 * signatureAlgorithm AlgorithmIdentifier,
1438 * signatureValue BIT STRING
1439 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001440 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001442 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 return( ret );
1444 }
1445
Paul Bakker535e97d2012-08-23 10:49:55 +00001446 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1447 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001449 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001450 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452
1453 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1454 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 return( ret );
1457 }
1458
1459 if( p != end )
1460 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001461 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001462 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001463 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 }
1465
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001466 return( 0 );
1467}
1468
1469/*
Paul Bakker42c65812013-06-24 19:21:59 +02001470 * Parse one X.509 certificate in DER format from a buffer and add them to a
1471 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472 */
Paul Bakker42c65812013-06-24 19:21:59 +02001473int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001474{
Paul Bakker42c65812013-06-24 19:21:59 +02001475 int ret;
1476 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477
1478 /*
1479 * Check for valid input
1480 */
1481 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001482 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001483
1484 while( crt->version != 0 && crt->next != NULL )
1485 {
1486 prev = crt;
1487 crt = crt->next;
1488 }
1489
1490 /*
1491 * Add new certificate on the end of the chain if needed.
1492 */
1493 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001494 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001495 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001496
Paul Bakker7d06ad22009-05-02 15:53:56 +00001497 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001498 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001499
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001501 crt = crt->next;
1502 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001503 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakker42c65812013-06-24 19:21:59 +02001505 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1506 {
1507 if( prev )
1508 prev->next = NULL;
1509
1510 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001511 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001512
1513 return( ret );
1514 }
1515
1516 return( 0 );
1517}
1518
1519/*
1520 * Parse one or more PEM certificates from a buffer and add them to the chained list
1521 */
1522int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1523{
1524 int ret, success = 0, first_error = 0, total_failed = 0;
1525 int buf_format = X509_FORMAT_DER;
1526
1527 /*
1528 * Check for valid input
1529 */
1530 if( chain == NULL || buf == NULL )
1531 return( POLARSSL_ERR_X509_INVALID_INPUT );
1532
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001533 /*
1534 * Determine buffer content. Buffer contains either one DER certificate or
1535 * one or more PEM certificates.
1536 */
1537#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001538 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001539 buf_format = X509_FORMAT_PEM;
1540#endif
1541
1542 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001543 return x509parse_crt_der( chain, buf, buflen );
1544
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001545#if defined(POLARSSL_PEM_C)
1546 if( buf_format == X509_FORMAT_PEM )
1547 {
1548 pem_context pem;
1549
1550 while( buflen > 0 )
1551 {
1552 size_t use_len;
1553 pem_init( &pem );
1554
1555 ret = pem_read_buffer( &pem,
1556 "-----BEGIN CERTIFICATE-----",
1557 "-----END CERTIFICATE-----",
1558 buf, NULL, 0, &use_len );
1559
1560 if( ret == 0 )
1561 {
1562 /*
1563 * Was PEM encoded
1564 */
1565 buflen -= use_len;
1566 buf += use_len;
1567 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001568 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1569 {
1570 return( ret );
1571 }
Paul Bakker00b28602013-06-24 13:02:41 +02001572 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 {
1574 pem_free( &pem );
1575
Paul Bakker5ed3b342013-06-24 19:05:46 +02001576 /*
1577 * PEM header and footer were found
1578 */
1579 buflen -= use_len;
1580 buf += use_len;
1581
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001582 if( first_error == 0 )
1583 first_error = ret;
1584
1585 continue;
1586 }
1587 else
1588 break;
1589
Paul Bakker42c65812013-06-24 19:21:59 +02001590 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001591
1592 pem_free( &pem );
1593
1594 if( ret != 0 )
1595 {
1596 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001597 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001598 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001599 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001600 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001601
1602 if( first_error == 0 )
1603 first_error = ret;
1604
Paul Bakker42c65812013-06-24 19:21:59 +02001605 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001606 continue;
1607 }
1608
1609 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 }
1611 }
1612#endif
1613
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001614 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001616 else if( first_error )
1617 return( first_error );
1618 else
1619 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
1622/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623 * Parse one or more CRLs and add them to the chained list
1624 */
Paul Bakker23986e52011-04-24 08:57:21 +00001625int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626{
Paul Bakker23986e52011-04-24 08:57:21 +00001627 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001628 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 unsigned char *p, *end;
1630 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001631#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001632 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 pem_context pem;
1634#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635
1636 crl = chain;
1637
1638 /*
1639 * Check for valid input
1640 */
1641 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001642 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643
1644 while( crl->version != 0 && crl->next != NULL )
1645 crl = crl->next;
1646
1647 /*
1648 * Add new CRL on the end of the chain if needed.
1649 */
1650 if ( crl->version != 0 && crl->next == NULL)
1651 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001652 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653
Paul Bakker7d06ad22009-05-02 15:53:56 +00001654 if( crl->next == NULL )
1655 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001656 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001657 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001658 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659
Paul Bakker7d06ad22009-05-02 15:53:56 +00001660 crl = crl->next;
1661 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 }
1663
Paul Bakker96743fc2011-02-12 14:30:57 +00001664#if defined(POLARSSL_PEM_C)
1665 pem_init( &pem );
1666 ret = pem_read_buffer( &pem,
1667 "-----BEGIN X509 CRL-----",
1668 "-----END X509 CRL-----",
1669 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670
Paul Bakker96743fc2011-02-12 14:30:57 +00001671 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001673 /*
1674 * Was PEM encoded
1675 */
1676 buflen -= use_len;
1677 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678
1679 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001680 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001682 p = pem.buf;
1683 pem.buf = NULL;
1684 len = pem.buflen;
1685 pem_free( &pem );
1686 }
Paul Bakker00b28602013-06-24 13:02:41 +02001687 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001688 {
1689 pem_free( &pem );
1690 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 }
1692 else
1693 {
1694 /*
1695 * nope, copy the raw DER data
1696 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001697 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001698
1699 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001700 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701
1702 memcpy( p, buf, buflen );
1703
1704 buflen = 0;
1705 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001706#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001707 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001708
1709 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001710 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001711
1712 memcpy( p, buf, buflen );
1713
1714 buflen = 0;
1715#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001716
1717 crl->raw.p = p;
1718 crl->raw.len = len;
1719 end = p + len;
1720
1721 /*
1722 * CertificateList ::= SEQUENCE {
1723 * tbsCertList TBSCertList,
1724 * signatureAlgorithm AlgorithmIdentifier,
1725 * signatureValue BIT STRING }
1726 */
1727 if( ( ret = asn1_get_tag( &p, end, &len,
1728 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1729 {
1730 x509_crl_free( crl );
1731 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1732 }
1733
Paul Bakker23986e52011-04-24 08:57:21 +00001734 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
1736 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001737 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1739 }
1740
1741 /*
1742 * TBSCertList ::= SEQUENCE {
1743 */
1744 crl->tbs.p = p;
1745
1746 if( ( ret = asn1_get_tag( &p, end, &len,
1747 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001750 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 }
1752
1753 end = p + len;
1754 crl->tbs.len = end - crl->tbs.p;
1755
1756 /*
1757 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1758 * -- if present, MUST be v2
1759 *
1760 * signature AlgorithmIdentifier
1761 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001762 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001763 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 {
1765 x509_crl_free( crl );
1766 return( ret );
1767 }
1768
1769 crl->version++;
1770
1771 if( crl->version > 2 )
1772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1775 }
1776
Paul Bakkerc70b9822013-04-07 22:00:46 +02001777 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1778 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779 {
1780 x509_crl_free( crl );
1781 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1782 }
1783
1784 /*
1785 * issuer Name
1786 */
1787 crl->issuer_raw.p = p;
1788
1789 if( ( ret = asn1_get_tag( &p, end, &len,
1790 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1791 {
1792 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001793 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 }
1795
1796 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1797 {
1798 x509_crl_free( crl );
1799 return( ret );
1800 }
1801
1802 crl->issuer_raw.len = p - crl->issuer_raw.p;
1803
1804 /*
1805 * thisUpdate Time
1806 * nextUpdate Time OPTIONAL
1807 */
Paul Bakker91200182010-02-18 21:26:15 +00001808 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001809 {
1810 x509_crl_free( crl );
1811 return( ret );
1812 }
1813
Paul Bakker91200182010-02-18 21:26:15 +00001814 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 {
Paul Bakker9d781402011-05-09 16:17:09 +00001816 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001817 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001818 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001819 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001820 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824 }
1825
1826 /*
1827 * revokedCertificates SEQUENCE OF SEQUENCE {
1828 * userCertificate CertificateSerialNumber,
1829 * revocationDate Time,
1830 * crlEntryExtensions Extensions OPTIONAL
1831 * -- if present, MUST be v2
1832 * } OPTIONAL
1833 */
1834 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 /*
1841 * crlExtensions EXPLICIT Extensions OPTIONAL
1842 * -- if present, MUST be v2
1843 */
1844 if( crl->version == 2 )
1845 {
1846 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1847
1848 if( ret != 0 )
1849 {
1850 x509_crl_free( crl );
1851 return( ret );
1852 }
1853 }
1854
1855 if( p != end )
1856 {
1857 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001858 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1860 }
1861
1862 end = crl->raw.p + crl->raw.len;
1863
1864 /*
1865 * signatureAlgorithm AlgorithmIdentifier,
1866 * signatureValue BIT STRING
1867 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001868 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869 {
1870 x509_crl_free( crl );
1871 return( ret );
1872 }
1873
Paul Bakker535e97d2012-08-23 10:49:55 +00001874 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1875 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 {
1877 x509_crl_free( crl );
1878 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1879 }
1880
1881 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1882 {
1883 x509_crl_free( crl );
1884 return( ret );
1885 }
1886
1887 if( p != end )
1888 {
1889 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001890 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1892 }
1893
1894 if( buflen > 0 )
1895 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001896 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897
Paul Bakker7d06ad22009-05-02 15:53:56 +00001898 if( crl->next == NULL )
1899 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001901 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001902 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903
Paul Bakker7d06ad22009-05-02 15:53:56 +00001904 crl = crl->next;
1905 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906
1907 return( x509parse_crl( crl, buf, buflen ) );
1908 }
1909
1910 return( 0 );
1911}
1912
Paul Bakker335db3f2011-04-25 15:28:35 +00001913#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915 * Load all data from a file into a given buffer.
1916 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001917static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001918{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001919 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001920
Paul Bakkerd98030e2009-05-02 15:13:40 +00001921 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001922 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001923
Paul Bakkerd98030e2009-05-02 15:13:40 +00001924 fseek( f, 0, SEEK_END );
1925 *n = (size_t) ftell( f );
1926 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001927
Paul Bakker6e339b52013-07-03 13:37:05 +02001928 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001929 {
1930 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001931 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001932 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001933
Paul Bakkerd98030e2009-05-02 15:13:40 +00001934 if( fread( *buf, 1, *n, f ) != *n )
1935 {
1936 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001937 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001938 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001939 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001940
Paul Bakkerd98030e2009-05-02 15:13:40 +00001941 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001942
Paul Bakkerd98030e2009-05-02 15:13:40 +00001943 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001944
Paul Bakkerd98030e2009-05-02 15:13:40 +00001945 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001946}
1947
1948/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 * Load one or more certificates and add them to the chained list
1950 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001951int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952{
1953 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 size_t n;
1955 unsigned char *buf;
1956
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001957 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001958 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Paul Bakker69e095c2011-12-10 21:55:01 +00001960 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001961
1962 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001963 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001964
1965 return( ret );
1966}
1967
Paul Bakker8d914582012-06-04 12:46:42 +00001968int x509parse_crtpath( x509_cert *chain, const char *path )
1969{
1970 int ret = 0;
1971#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001972 int w_ret;
1973 WCHAR szDir[MAX_PATH];
1974 char filename[MAX_PATH];
1975 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001976 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001977
Paul Bakker97872ac2012-11-02 12:53:26 +00001978 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001979 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001980
1981 if( len > MAX_PATH - 3 )
1982 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001983
Paul Bakker3338b792012-10-01 21:13:10 +00001984 memset( szDir, 0, sizeof(szDir) );
1985 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001986 memcpy( filename, path, len );
1987 filename[len++] = '\\';
1988 p = filename + len;
1989 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001990
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001991 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001992
Paul Bakker97872ac2012-11-02 12:53:26 +00001993 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001994 if (hFind == INVALID_HANDLE_VALUE)
1995 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1996
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001997 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001998 do
1999 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002000 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002001
Paul Bakkere4791f32012-06-04 21:29:15 +00002002 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002003 continue;
2004
Paul Bakker3338b792012-10-01 21:13:10 +00002005 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2006 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002007 p, len - 1,
2008 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002009
Paul Bakker3338b792012-10-01 21:13:10 +00002010 w_ret = x509parse_crtfile( chain, filename );
2011 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002012 ret++;
2013 else
2014 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002015 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002016 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002017
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002018 if (GetLastError() != ERROR_NO_MORE_FILES)
2019 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002020
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002021cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002022 FindClose( hFind );
2023#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002024 int t_ret, i;
2025 struct stat sb;
2026 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002027 char entry_name[255];
2028 DIR *dir = opendir( path );
2029
2030 if( dir == NULL)
2031 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2032
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002033 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002034 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002035 if( result == NULL )
2036 break;
2037
2038 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2039
2040 i = stat( entry_name, &sb );
2041
2042 if( i == -1 )
2043 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2044
2045 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002046 continue;
2047
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002048 // Ignore parse errors
2049 //
Paul Bakker8d914582012-06-04 12:46:42 +00002050 t_ret = x509parse_crtfile( chain, entry_name );
2051 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002052 ret++;
2053 else
2054 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002055 }
2056 closedir( dir );
2057#endif
2058
2059 return( ret );
2060}
2061
Paul Bakkerd98030e2009-05-02 15:13:40 +00002062/*
2063 * Load one or more CRLs and add them to the chained list
2064 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002065int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002066{
2067 int ret;
2068 size_t n;
2069 unsigned char *buf;
2070
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002071 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002072 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002073
Paul Bakker27fdf462011-06-09 13:55:13 +00002074 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002075
2076 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002077 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002078
2079 return( ret );
2080}
2081
Paul Bakker5121ce52009-01-03 21:22:43 +00002082/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002083 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002084 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002085int x509parse_keyfile( pk_context *ctx,
2086 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002087{
2088 int ret;
2089 size_t n;
2090 unsigned char *buf;
2091
2092 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2093 return( ret );
2094
2095 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002096 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002097 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002098 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002099 (const unsigned char *) pwd, strlen( pwd ) );
2100
2101 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002102 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002103
2104 return( ret );
2105}
2106
2107/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002108 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002109 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002110int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002111{
2112 int ret;
2113 size_t n;
2114 unsigned char *buf;
2115
2116 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2117 return( ret );
2118
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002119 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002120
2121 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002122 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002123
2124 return( ret );
2125}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002126
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002127#if defined(POLARSSL_RSA_C)
2128/*
2129 * Load and parse a private RSA key
2130 */
2131int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2132{
2133 pk_context pk;
2134
2135 pk_init( &pk );
2136 pk_wrap_rsa( &pk, rsa );
2137
2138 return( x509parse_keyfile( &pk, path, pwd ) );
2139}
2140
2141/*
2142 * Load and parse a public RSA key
2143 */
2144int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2145{
2146 pk_context pk;
2147
2148 pk_init( &pk );
2149 pk_wrap_rsa( &pk, rsa );
2150
2151 return( x509parse_public_keyfile( &pk, path ) );
2152}
2153#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002154#endif /* POLARSSL_FS_IO */
2155
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002156#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002157/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002158 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002160static int x509parse_key_pkcs1_der( rsa_context *rsa,
2161 const unsigned char *key,
2162 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002163{
Paul Bakker23986e52011-04-24 08:57:21 +00002164 int ret;
2165 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002167
Paul Bakker96743fc2011-02-12 14:30:57 +00002168 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002169 end = p + keylen;
2170
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002172 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002173 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 * RSAPrivateKey ::= SEQUENCE {
2175 * version Version,
2176 * modulus INTEGER, -- n
2177 * publicExponent INTEGER, -- e
2178 * privateExponent INTEGER, -- d
2179 * prime1 INTEGER, -- p
2180 * prime2 INTEGER, -- q
2181 * exponent1 INTEGER, -- d mod (p-1)
2182 * exponent2 INTEGER, -- d mod (q-1)
2183 * coefficient INTEGER, -- (inverse of q) mod p
2184 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2185 * }
2186 */
2187 if( ( ret = asn1_get_tag( &p, end, &len,
2188 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2189 {
Paul Bakker9d781402011-05-09 16:17:09 +00002190 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
2192
2193 end = p + len;
2194
2195 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2196 {
Paul Bakker9d781402011-05-09 16:17:09 +00002197 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
2200 if( rsa->ver != 0 )
2201 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002202 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
2205 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2206 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2207 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2208 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2209 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2210 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2211 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2212 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2213 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002215 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 }
2217
2218 rsa->len = mpi_size( &rsa->N );
2219
2220 if( p != end )
2221 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002223 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002224 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
2227 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2228 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 rsa_free( rsa );
2230 return( ret );
2231 }
2232
Paul Bakkere2f50402013-06-24 19:00:59 +02002233 return( 0 );
2234}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002235#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002236
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002237#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002238/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002239 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002240 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002241static int x509parse_key_sec1_der( ecp_keypair *eck,
2242 const unsigned char *key,
2243 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002244{
2245 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002246 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002247 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002248 x509_buf params;
2249 unsigned char *p = (unsigned char *) key;
2250 unsigned char *end = p + keylen;
2251 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002252
2253 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002254 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002255 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002256 * ECPrivateKey ::= SEQUENCE {
2257 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2258 * privateKey OCTET STRING,
2259 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2260 * publicKey [1] BIT STRING OPTIONAL
2261 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002262 */
2263 if( ( ret = asn1_get_tag( &p, end, &len,
2264 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2265 {
2266 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2267 }
2268
2269 end = p + len;
2270
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002271 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002272 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2273
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002274 if( version != 1 )
2275 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002276
Paul Bakkere2f50402013-06-24 19:00:59 +02002277 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2278 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2279
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002280 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002281 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002282 ecp_keypair_free( eck );
2283 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2284 }
2285
2286 p += len;
2287
2288 /*
2289 * Is 'parameters' present?
2290 */
2291 if( ( ret = asn1_get_tag( &p, end, &len,
2292 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2293 {
2294 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2295 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2296 {
2297 ecp_keypair_free( eck );
2298 return( ret );
2299 }
2300 }
2301 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2302 {
2303 ecp_keypair_free( eck );
2304 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2305 }
2306
2307 /*
2308 * Is 'publickey' present?
2309 */
2310 if( ( ret = asn1_get_tag( &p, end, &len,
2311 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2312 {
2313 end2 = p + len;
2314
2315 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2316 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2317
2318 if( p + len != end2 )
2319 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2320 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2321
2322 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2323 return( ret );
2324 }
2325 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2326 {
2327 ecp_keypair_free( eck );
2328 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2329 }
2330
2331 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2332 {
2333 ecp_keypair_free( eck );
2334 return( ret );
2335 }
2336
2337 return 0;
2338}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002339#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002340
2341/*
2342 * Parse an unencrypted PKCS#8 encoded private key
2343 */
2344static int x509parse_key_pkcs8_unencrypted_der(
2345 pk_context *pk,
2346 const unsigned char* key,
2347 size_t keylen )
2348{
2349 int ret, version;
2350 size_t len;
2351 x509_buf params;
2352 unsigned char *p = (unsigned char *) key;
2353 unsigned char *end = p + keylen;
2354 pk_type_t pk_alg = POLARSSL_PK_NONE;
2355
2356 /*
2357 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2358 *
2359 * PrivateKeyInfo ::= SEQUENCE {
2360 * version Version,
2361 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2362 * privateKey PrivateKey,
2363 * attributes [0] IMPLICIT Attributes OPTIONAL }
2364 *
2365 * Version ::= INTEGER
2366 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2367 * PrivateKey ::= OCTET STRING
2368 *
2369 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2370 */
2371
2372 if( ( ret = asn1_get_tag( &p, end, &len,
2373 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2374 {
2375 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002376 }
2377
2378 end = p + len;
2379
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002380 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2381 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2382
2383 if( version != 0 )
2384 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2385
2386 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2387 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2388
2389 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2390 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2391
2392 if( len < 1 )
2393 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2394 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2395
2396 if( ( ret = pk_set_type( pk, pk_alg ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002397 return( ret );
2398
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002399#if defined(POLARSSL_RSA_C)
2400 if( pk_alg == POLARSSL_PK_RSA )
2401 {
2402 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2403 {
2404 pk_free( pk );
2405 return( ret );
2406 }
2407 } else
2408#endif /* POLARSSL_RSA_C */
2409#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002410 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2411 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002412 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2413 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2414 {
2415 pk_free( pk );
2416 return( ret );
2417 }
2418 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002419#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002420 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2421
2422 return 0;
2423}
2424
2425/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002426 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002427 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002428static int x509parse_key_pkcs8_encrypted_der(
2429 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002430 const unsigned char *key, size_t keylen,
2431 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002432{
2433 int ret;
2434 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002435 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002436 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002437 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002438#if defined(POLARSSL_PKCS12_C)
2439 cipher_type_t cipher_alg;
2440 md_type_t md_alg;
2441#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002442
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002443 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002444
2445 p = (unsigned char *) key;
2446 end = p + keylen;
2447
Paul Bakker28144de2013-06-24 19:28:55 +02002448 if( pwdlen == 0 )
2449 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2450
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002451 /*
2452 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2453 *
2454 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2455 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2456 * encryptedData EncryptedData
2457 * }
2458 *
2459 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2460 *
2461 * EncryptedData ::= OCTET STRING
2462 *
2463 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2464 */
2465 if( ( ret = asn1_get_tag( &p, end, &len,
2466 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2467 {
2468 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2469 }
2470
2471 end = p + len;
2472
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002473 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002474 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002475
2476 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2477 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2478
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002479 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002480 return( POLARSSL_ERR_X509_INVALID_INPUT );
2481
2482 /*
2483 * Decrypt EncryptedData with appropriate PDE
2484 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002485#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002486 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002487 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002488 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002489 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002490 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002491 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002492 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2493 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2494
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002495 return( ret );
2496 }
2497 }
2498 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2499 {
2500 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2501 PKCS12_PBE_DECRYPT,
2502 pwd, pwdlen,
2503 p, len, buf ) ) != 0 )
2504 {
2505 return( ret );
2506 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002507
2508 // Best guess for password mismatch when using RC4. If first tag is
2509 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2510 //
2511 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2512 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002513 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002514 else
2515#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002516#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002517 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002518 {
2519 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2520 p, len, buf ) ) != 0 )
2521 {
2522 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2523 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2524
2525 return( ret );
2526 }
2527 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002528 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002529#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002530 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2531
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002532 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002533}
2534
2535/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002536 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002537 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002538int x509parse_key( pk_context *pk,
2539 const unsigned char *key, size_t keylen,
2540 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002541{
2542 int ret;
2543
2544#if defined(POLARSSL_PEM_C)
2545 size_t len;
2546 pem_context pem;
2547
2548 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002549
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002550#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002551 ret = pem_read_buffer( &pem,
2552 "-----BEGIN RSA PRIVATE KEY-----",
2553 "-----END RSA PRIVATE KEY-----",
2554 key, pwd, pwdlen, &len );
2555 if( ret == 0 )
2556 {
2557 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) != 0 ||
2558 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2559 pem.buf, pem.buflen ) ) != 0 )
2560 {
2561 pk_free( pk );
2562 }
2563
2564 pem_free( &pem );
2565 return( ret );
2566 }
2567 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2568 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2569 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2570 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2571 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2572 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002573#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002574
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002575#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002576 ret = pem_read_buffer( &pem,
2577 "-----BEGIN EC PRIVATE KEY-----",
2578 "-----END EC PRIVATE KEY-----",
2579 key, pwd, pwdlen, &len );
2580 if( ret == 0 )
2581 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002582 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) != 0 ||
2583 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2584 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002585 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002586 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002587 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002588
2589 pem_free( &pem );
2590 return( ret );
2591 }
2592 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2593 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2594 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2595 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2596 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2597 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002598#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002599
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002600 ret = pem_read_buffer( &pem,
2601 "-----BEGIN PRIVATE KEY-----",
2602 "-----END PRIVATE KEY-----",
2603 key, NULL, 0, &len );
2604 if( ret == 0 )
2605 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002606 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002607 pem.buf, pem.buflen ) ) != 0 )
2608 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002609 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002610 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002611
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002612 pem_free( &pem );
2613 return( ret );
2614 }
2615 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2616 return( ret );
2617
2618 ret = pem_read_buffer( &pem,
2619 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2620 "-----END ENCRYPTED PRIVATE KEY-----",
2621 key, NULL, 0, &len );
2622 if( ret == 0 )
2623 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002624 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002625 pem.buf, pem.buflen,
2626 pwd, pwdlen ) ) != 0 )
2627 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002628 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002629 }
2630
2631 pem_free( &pem );
2632 return( ret );
2633 }
2634 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2635 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002636#else
2637 ((void) pwd);
2638 ((void) pwdlen);
2639#endif /* POLARSSL_PEM_C */
2640
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002641 /*
2642 * At this point we only know it's not a PEM formatted key. Could be any
2643 * of the known DER encoded private key formats
2644 *
2645 * We try the different DER format parsers to see if one passes without
2646 * error
2647 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002648 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2649 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002650 {
2651 return( 0 );
2652 }
2653
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002654 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002655
2656 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2657 {
2658 return( ret );
2659 }
2660
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002661 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002662 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002663
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002664 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002665
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002666#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002667 if( ( ret = pk_set_type( pk, POLARSSL_PK_RSA ) ) == 0 &&
2668 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2669 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002670 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002671 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002672
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002673 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002674#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002675
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002676#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002677 if( ( ret = pk_set_type( pk, POLARSSL_PK_ECKEY ) ) == 0 &&
2678 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2679 {
2680 return( 0 );
2681 }
2682
2683 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002684#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002685
2686 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2687}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002688
2689/*
2690 * Parse a public key
2691 */
2692int x509parse_public_key( pk_context *ctx,
2693 const unsigned char *key, size_t keylen )
2694{
2695 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002696 unsigned char *p;
2697#if defined(POLARSSL_PEM_C)
2698 size_t len;
2699 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002700
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002701 pem_init( &pem );
2702 ret = pem_read_buffer( &pem,
2703 "-----BEGIN PUBLIC KEY-----",
2704 "-----END PUBLIC KEY-----",
2705 key, NULL, 0, &len );
2706
2707 if( ret == 0 )
2708 {
2709 /*
2710 * Was PEM encoded
2711 */
2712 key = pem.buf;
2713 keylen = pem.buflen;
2714 }
2715 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2716 {
2717 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002718 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002719 }
2720#endif
2721 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002722
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002723 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002724
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002725#if defined(POLARSSL_PEM_C)
2726 pem_free( &pem );
2727#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02002728
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002729 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002730}
2731
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002732#if defined(POLARSSL_RSA_C)
2733/*
2734 * Parse a private RSA key
2735 */
2736int x509parse_key_rsa( rsa_context *rsa,
2737 const unsigned char *key, size_t keylen,
2738 const unsigned char *pwd, size_t pwdlen )
2739{
2740 pk_context pk;
2741
2742 pk_init( &pk );
2743 pk_wrap_rsa( &pk, rsa );
2744
2745 return( x509parse_key( &pk, key, keylen, pwd, pwdlen ) );
2746}
2747
2748/*
2749 * Parse a public RSA key
2750 */
2751int x509parse_public_key_rsa( rsa_context *rsa,
2752 const unsigned char *key, size_t keylen )
2753{
2754 pk_context pk;
2755
2756 pk_init( &pk );
2757 pk_wrap_rsa( &pk, rsa );
2758
2759 return( x509parse_public_key( &pk, key, keylen ) );
2760}
2761#endif /* POLARSSL_RSA_C */
2762
Paul Bakkereaa89f82011-04-04 21:36:15 +00002763#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002764/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002765 * Parse DHM parameters
2766 */
Paul Bakker23986e52011-04-24 08:57:21 +00002767int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002768{
Paul Bakker23986e52011-04-24 08:57:21 +00002769 int ret;
2770 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002771 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002772#if defined(POLARSSL_PEM_C)
2773 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002774
Paul Bakker96743fc2011-02-12 14:30:57 +00002775 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002776
Paul Bakker96743fc2011-02-12 14:30:57 +00002777 ret = pem_read_buffer( &pem,
2778 "-----BEGIN DH PARAMETERS-----",
2779 "-----END DH PARAMETERS-----",
2780 dhmin, NULL, 0, &dhminlen );
2781
2782 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002783 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002784 /*
2785 * Was PEM encoded
2786 */
2787 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002788 }
Paul Bakker00b28602013-06-24 13:02:41 +02002789 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002790 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002791 pem_free( &pem );
2792 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002793 }
2794
Paul Bakker96743fc2011-02-12 14:30:57 +00002795 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2796#else
2797 p = (unsigned char *) dhmin;
2798#endif
2799 end = p + dhminlen;
2800
Paul Bakker1b57b062011-01-06 15:48:19 +00002801 memset( dhm, 0, sizeof( dhm_context ) );
2802
Paul Bakker1b57b062011-01-06 15:48:19 +00002803 /*
2804 * DHParams ::= SEQUENCE {
2805 * prime INTEGER, -- P
2806 * generator INTEGER, -- g
2807 * }
2808 */
2809 if( ( ret = asn1_get_tag( &p, end, &len,
2810 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2811 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002812#if defined(POLARSSL_PEM_C)
2813 pem_free( &pem );
2814#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002815 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002816 }
2817
2818 end = p + len;
2819
2820 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2821 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2822 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002823#if defined(POLARSSL_PEM_C)
2824 pem_free( &pem );
2825#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002826 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002827 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002828 }
2829
2830 if( p != end )
2831 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002832#if defined(POLARSSL_PEM_C)
2833 pem_free( &pem );
2834#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002835 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002836 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002837 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2838 }
2839
Paul Bakker96743fc2011-02-12 14:30:57 +00002840#if defined(POLARSSL_PEM_C)
2841 pem_free( &pem );
2842#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002843
2844 return( 0 );
2845}
2846
Paul Bakker335db3f2011-04-25 15:28:35 +00002847#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002848/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002849 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002850 */
2851int x509parse_dhmfile( dhm_context *dhm, const char *path )
2852{
2853 int ret;
2854 size_t n;
2855 unsigned char *buf;
2856
Paul Bakker69e095c2011-12-10 21:55:01 +00002857 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2858 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002859
Paul Bakker27fdf462011-06-09 13:55:13 +00002860 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002861
2862 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002863 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002864
2865 return( ret );
2866}
Paul Bakker335db3f2011-04-25 15:28:35 +00002867#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002868#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002869
Paul Bakker5121ce52009-01-03 21:22:43 +00002870#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002871#include <stdarg.h>
2872
2873#if !defined vsnprintf
2874#define vsnprintf _vsnprintf
2875#endif // vsnprintf
2876
2877/*
2878 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2879 * Result value is not size of buffer needed, but -1 if no fit is possible.
2880 *
2881 * This fuction tries to 'fix' this by at least suggesting enlarging the
2882 * size by 20.
2883 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002884static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002885{
2886 va_list ap;
2887 int res = -1;
2888
2889 va_start( ap, format );
2890
2891 res = vsnprintf( str, size, format, ap );
2892
2893 va_end( ap );
2894
2895 // No quick fix possible
2896 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002897 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002898
2899 return res;
2900}
2901
2902#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002903#endif
2904
Paul Bakkerd98030e2009-05-02 15:13:40 +00002905#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2906
2907#define SAFE_SNPRINTF() \
2908{ \
2909 if( ret == -1 ) \
2910 return( -1 ); \
2911 \
Paul Bakker23986e52011-04-24 08:57:21 +00002912 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002913 p[n - 1] = '\0'; \
2914 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2915 } \
2916 \
Paul Bakker23986e52011-04-24 08:57:21 +00002917 n -= (unsigned int) ret; \
2918 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002919}
2920
Paul Bakker5121ce52009-01-03 21:22:43 +00002921/*
2922 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002923 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002925int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002926{
Paul Bakker23986e52011-04-24 08:57:21 +00002927 int ret;
2928 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002930 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002931 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 char s[128], *p;
2933
2934 memset( s, 0, sizeof( s ) );
2935
2936 name = dn;
2937 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002938 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 while( name != NULL )
2941 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002942 if( !name->oid.p )
2943 {
2944 name = name->next;
2945 continue;
2946 }
2947
Paul Bakker74111d32011-01-15 16:57:55 +00002948 if( name != dn )
2949 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002950 ret = snprintf( p, n, ", " );
2951 SAFE_SNPRINTF();
2952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakkerc70b9822013-04-07 22:00:46 +02002954 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakkerc70b9822013-04-07 22:00:46 +02002956 if( ret == 0 )
2957 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002959 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002960 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
2962 for( i = 0; i < name->val.len; i++ )
2963 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002964 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 break;
2966
2967 c = name->val.p[i];
2968 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2969 s[i] = '?';
2970 else s[i] = c;
2971 }
2972 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002973 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002974 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 name = name->next;
2976 }
2977
Paul Bakker23986e52011-04-24 08:57:21 +00002978 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979}
2980
2981/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002982 * Store the serial in printable form into buf; no more
2983 * than size characters will be written
2984 */
2985int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2986{
Paul Bakker23986e52011-04-24 08:57:21 +00002987 int ret;
2988 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002989 char *p;
2990
2991 p = buf;
2992 n = size;
2993
2994 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002995 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002996
2997 for( i = 0; i < nr; i++ )
2998 {
Paul Bakker93048802011-12-05 14:38:06 +00002999 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003000 continue;
3001
Paul Bakkerdd476992011-01-16 21:34:59 +00003002 ret = snprintf( p, n, "%02X%s",
3003 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3004 SAFE_SNPRINTF();
3005 }
3006
Paul Bakker03c7c252011-11-25 12:37:37 +00003007 if( nr != serial->len )
3008 {
3009 ret = snprintf( p, n, "...." );
3010 SAFE_SNPRINTF();
3011 }
3012
Paul Bakker23986e52011-04-24 08:57:21 +00003013 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003014}
3015
3016/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003017 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003018 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003019int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3020 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021{
Paul Bakker23986e52011-04-24 08:57:21 +00003022 int ret;
3023 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003024 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003025 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
3027 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003028 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakkerd98030e2009-05-02 15:13:40 +00003030 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003032 SAFE_SNPRINTF();
3033 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003034 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003035 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Paul Bakkerdd476992011-01-16 21:34:59 +00003037 ret = x509parse_serial_gets( p, n, &crt->serial);
3038 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003039
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3041 SAFE_SNPRINTF();
3042 ret = x509parse_dn_gets( p, n, &crt->issuer );
3043 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
Paul Bakkerd98030e2009-05-02 15:13:40 +00003045 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3046 SAFE_SNPRINTF();
3047 ret = x509parse_dn_gets( p, n, &crt->subject );
3048 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
Paul Bakkerd98030e2009-05-02 15:13:40 +00003050 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3052 crt->valid_from.year, crt->valid_from.mon,
3053 crt->valid_from.day, crt->valid_from.hour,
3054 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003055 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakkerd98030e2009-05-02 15:13:40 +00003057 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3059 crt->valid_to.year, crt->valid_to.mon,
3060 crt->valid_to.day, crt->valid_to.hour,
3061 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003062 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakkerc70b9822013-04-07 22:00:46 +02003064 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003065 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
Paul Bakkerc70b9822013-04-07 22:00:46 +02003067 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3068 if( ret != 0 )
3069 ret = snprintf( p, n, "???" );
3070 else
3071 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003072 SAFE_SNPRINTF();
3073
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003074#if defined(POLARSSL_RSA_C)
3075 if( crt->pk.type == POLARSSL_PK_RSA )
3076 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
3077 (int) pk_rsa( crt->pk )->N.n * (int) sizeof( t_uint ) * 8 );
3078 else
3079#endif /* POLARSSL_RSA_C */
3080#if defined(POLARSSL_ECP_C)
3081 if( crt->pk.type == POLARSSL_PK_ECKEY ||
3082 crt->pk.type == POLARSSL_PK_ECKEY_DH )
3083 ret = snprintf( p, n, "\n%sEC key size : %d bits\n", prefix,
3084 (int) pk_ec( crt->pk )->grp.pbits );
3085 else
3086#endif /* POLARSSL_ECP_C */
3087 ret = snprintf(p, n, "\n%sPK type looks wrong!", prefix);
Paul Bakkerd98030e2009-05-02 15:13:40 +00003088 SAFE_SNPRINTF();
3089
Paul Bakker23986e52011-04-24 08:57:21 +00003090 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003091}
3092
Paul Bakker74111d32011-01-15 16:57:55 +00003093/*
3094 * Return an informational string describing the given OID
3095 */
3096const char *x509_oid_get_description( x509_buf *oid )
3097{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003098 const char *desc = NULL;
3099 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003100
Paul Bakkerc70b9822013-04-07 22:00:46 +02003101 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003102
Paul Bakkerc70b9822013-04-07 22:00:46 +02003103 if( ret != 0 )
3104 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003105
Paul Bakkerc70b9822013-04-07 22:00:46 +02003106 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003107}
3108
3109/* Return the x.y.z.... style numeric string for the given OID */
3110int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3111{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003112 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003113}
3114
Paul Bakkerd98030e2009-05-02 15:13:40 +00003115/*
3116 * Return an informational string about the CRL.
3117 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003118int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3119 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003120{
Paul Bakker23986e52011-04-24 08:57:21 +00003121 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003122 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003123 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003124 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003125 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003126
3127 p = buf;
3128 n = size;
3129
3130 ret = snprintf( p, n, "%sCRL version : %d",
3131 prefix, crl->version );
3132 SAFE_SNPRINTF();
3133
3134 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3135 SAFE_SNPRINTF();
3136 ret = x509parse_dn_gets( p, n, &crl->issuer );
3137 SAFE_SNPRINTF();
3138
3139 ret = snprintf( p, n, "\n%sthis update : " \
3140 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3141 crl->this_update.year, crl->this_update.mon,
3142 crl->this_update.day, crl->this_update.hour,
3143 crl->this_update.min, crl->this_update.sec );
3144 SAFE_SNPRINTF();
3145
3146 ret = snprintf( p, n, "\n%snext update : " \
3147 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3148 crl->next_update.year, crl->next_update.mon,
3149 crl->next_update.day, crl->next_update.hour,
3150 crl->next_update.min, crl->next_update.sec );
3151 SAFE_SNPRINTF();
3152
3153 entry = &crl->entry;
3154
3155 ret = snprintf( p, n, "\n%sRevoked certificates:",
3156 prefix );
3157 SAFE_SNPRINTF();
3158
Paul Bakker9be19372009-07-27 20:21:53 +00003159 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003160 {
3161 ret = snprintf( p, n, "\n%sserial number: ",
3162 prefix );
3163 SAFE_SNPRINTF();
3164
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003165 ret = x509parse_serial_gets( p, n, &entry->serial);
3166 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003167
Paul Bakkerd98030e2009-05-02 15:13:40 +00003168 ret = snprintf( p, n, " revocation date: " \
3169 "%04d-%02d-%02d %02d:%02d:%02d",
3170 entry->revocation_date.year, entry->revocation_date.mon,
3171 entry->revocation_date.day, entry->revocation_date.hour,
3172 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003173 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003174
3175 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 }
3177
Paul Bakkerc70b9822013-04-07 22:00:46 +02003178 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003179 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
Paul Bakkerc70b9822013-04-07 22:00:46 +02003181 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3182 if( ret != 0 )
3183 ret = snprintf( p, n, "???" );
3184 else
3185 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003186 SAFE_SNPRINTF();
3187
Paul Bakker1e27bb22009-07-19 20:25:25 +00003188 ret = snprintf( p, n, "\n" );
3189 SAFE_SNPRINTF();
3190
Paul Bakker23986e52011-04-24 08:57:21 +00003191 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192}
3193
3194/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003195 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003197#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003198int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003199{
Paul Bakkercce9d772011-11-18 14:26:47 +00003200 int year, mon, day;
3201 int hour, min, sec;
3202
3203#if defined(_WIN32)
3204 SYSTEMTIME st;
3205
3206 GetLocalTime(&st);
3207
3208 year = st.wYear;
3209 mon = st.wMonth;
3210 day = st.wDay;
3211 hour = st.wHour;
3212 min = st.wMinute;
3213 sec = st.wSecond;
3214#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 struct tm *lt;
3216 time_t tt;
3217
3218 tt = time( NULL );
3219 lt = localtime( &tt );
3220
Paul Bakkercce9d772011-11-18 14:26:47 +00003221 year = lt->tm_year + 1900;
3222 mon = lt->tm_mon + 1;
3223 day = lt->tm_mday;
3224 hour = lt->tm_hour;
3225 min = lt->tm_min;
3226 sec = lt->tm_sec;
3227#endif
3228
3229 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003230 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231
Paul Bakkercce9d772011-11-18 14:26:47 +00003232 if( year == to->year &&
3233 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003234 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Paul Bakkercce9d772011-11-18 14:26:47 +00003236 if( year == to->year &&
3237 mon == to->mon &&
3238 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003239 return( 1 );
3240
Paul Bakkercce9d772011-11-18 14:26:47 +00003241 if( year == to->year &&
3242 mon == to->mon &&
3243 day == to->day &&
3244 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003245 return( 1 );
3246
Paul Bakkercce9d772011-11-18 14:26:47 +00003247 if( year == to->year &&
3248 mon == to->mon &&
3249 day == to->day &&
3250 hour == to->hour &&
3251 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003252 return( 1 );
3253
Paul Bakkercce9d772011-11-18 14:26:47 +00003254 if( year == to->year &&
3255 mon == to->mon &&
3256 day == to->day &&
3257 hour == to->hour &&
3258 min == to->min &&
3259 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003260 return( 1 );
3261
Paul Bakker40ea7de2009-05-03 10:18:48 +00003262 return( 0 );
3263}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003264#else /* POLARSSL_HAVE_TIME */
3265int x509parse_time_expired( const x509_time *to )
3266{
3267 ((void) to);
3268 return( 0 );
3269}
3270#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003271
3272/*
3273 * Return 1 if the certificate is revoked, or 0 otherwise.
3274 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003275int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003276{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003277 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003278
3279 while( cur != NULL && cur->serial.len != 0 )
3280 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003281 if( crt->serial.len == cur->serial.len &&
3282 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003283 {
3284 if( x509parse_time_expired( &cur->revocation_date ) )
3285 return( 1 );
3286 }
3287
3288 cur = cur->next;
3289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
3291 return( 0 );
3292}
3293
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003294/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003295 * Check that the given certificate is valid accoring to the CRL.
3296 */
3297static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3298 x509_crl *crl_list)
3299{
3300 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003301 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3302 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003303
Paul Bakker915275b2012-09-28 07:10:55 +00003304 if( ca == NULL )
3305 return( flags );
3306
Paul Bakker76fd75a2011-01-16 21:12:10 +00003307 /*
3308 * TODO: What happens if no CRL is present?
3309 * Suggestion: Revocation state should be unknown if no CRL is present.
3310 * For backwards compatibility this is not yet implemented.
3311 */
3312
Paul Bakker915275b2012-09-28 07:10:55 +00003313 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003314 {
Paul Bakker915275b2012-09-28 07:10:55 +00003315 if( crl_list->version == 0 ||
3316 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003317 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3318 crl_list->issuer_raw.len ) != 0 )
3319 {
3320 crl_list = crl_list->next;
3321 continue;
3322 }
3323
3324 /*
3325 * Check if CRL is correctly signed by the trusted CA
3326 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003327 md_info = md_info_from_type( crl_list->sig_md );
3328 if( md_info == NULL )
3329 {
3330 /*
3331 * Cannot check 'unknown' hash
3332 */
3333 flags |= BADCRL_NOT_TRUSTED;
3334 break;
3335 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003336
Paul Bakkerc70b9822013-04-07 22:00:46 +02003337 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003338
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003339 /* EC NOT IMPLEMENTED YET */
3340 if( ca->pk.type != POLARSSL_PK_RSA )
3341 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3342
3343 if( !rsa_pkcs1_verify( pk_rsa( ca->pk ), RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003344 0, hash, crl_list->sig.p ) == 0 )
3345 {
3346 /*
3347 * CRL is not trusted
3348 */
3349 flags |= BADCRL_NOT_TRUSTED;
3350 break;
3351 }
3352
3353 /*
3354 * Check for validity of CRL (Do not drop out)
3355 */
3356 if( x509parse_time_expired( &crl_list->next_update ) )
3357 flags |= BADCRL_EXPIRED;
3358
3359 /*
3360 * Check if certificate is revoked
3361 */
3362 if( x509parse_revoked(crt, crl_list) )
3363 {
3364 flags |= BADCERT_REVOKED;
3365 break;
3366 }
3367
3368 crl_list = crl_list->next;
3369 }
3370 return flags;
3371}
3372
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003373static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003374{
3375 size_t i;
3376 size_t cn_idx = 0;
3377
Paul Bakker57b12982012-02-11 17:38:38 +00003378 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003379 return( 0 );
3380
3381 for( i = 0; i < strlen( cn ); ++i )
3382 {
3383 if( cn[i] == '.' )
3384 {
3385 cn_idx = i;
3386 break;
3387 }
3388 }
3389
3390 if( cn_idx == 0 )
3391 return( 0 );
3392
Paul Bakker535e97d2012-08-23 10:49:55 +00003393 if( strlen( cn ) - cn_idx == name->len - 1 &&
3394 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003395 {
3396 return( 1 );
3397 }
3398
3399 return( 0 );
3400}
3401
Paul Bakker915275b2012-09-28 07:10:55 +00003402static int x509parse_verify_top(
3403 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003404 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003405 int (*f_vrfy)(void *, x509_cert *, int, int *),
3406 void *p_vrfy )
3407{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003408 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003409 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003410 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3411 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003412
3413 if( x509parse_time_expired( &child->valid_to ) )
3414 *flags |= BADCERT_EXPIRED;
3415
3416 /*
3417 * Child is the top of the chain. Check against the trust_ca list.
3418 */
3419 *flags |= BADCERT_NOT_TRUSTED;
3420
3421 while( trust_ca != NULL )
3422 {
3423 if( trust_ca->version == 0 ||
3424 child->issuer_raw.len != trust_ca->subject_raw.len ||
3425 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3426 child->issuer_raw.len ) != 0 )
3427 {
3428 trust_ca = trust_ca->next;
3429 continue;
3430 }
3431
Paul Bakker9a736322012-11-14 12:39:52 +00003432 /*
3433 * Reduce path_len to check against if top of the chain is
3434 * the same as the trusted CA
3435 */
3436 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3437 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3438 child->issuer_raw.len ) == 0 )
3439 {
3440 check_path_cnt--;
3441 }
3442
Paul Bakker915275b2012-09-28 07:10:55 +00003443 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003444 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003445 {
3446 trust_ca = trust_ca->next;
3447 continue;
3448 }
3449
Paul Bakkerc70b9822013-04-07 22:00:46 +02003450 md_info = md_info_from_type( child->sig_md );
3451 if( md_info == NULL )
3452 {
3453 /*
3454 * Cannot check 'unknown' hash
3455 */
3456 continue;
3457 }
Paul Bakker915275b2012-09-28 07:10:55 +00003458
Paul Bakkerc70b9822013-04-07 22:00:46 +02003459 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003460
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003461 /* EC NOT IMPLEMENTED YET */
3462 if( trust_ca->pk.type != POLARSSL_PK_RSA )
3463 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3464
3465 if( rsa_pkcs1_verify( pk_rsa( trust_ca->pk ), RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003466 0, hash, child->sig.p ) != 0 )
3467 {
3468 trust_ca = trust_ca->next;
3469 continue;
3470 }
3471
3472 /*
3473 * Top of chain is signed by a trusted CA
3474 */
3475 *flags &= ~BADCERT_NOT_TRUSTED;
3476 break;
3477 }
3478
Paul Bakker9a736322012-11-14 12:39:52 +00003479 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003480 * If top of chain is not the same as the trusted CA send a verify request
3481 * to the callback for any issues with validity and CRL presence for the
3482 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003483 */
3484 if( trust_ca != NULL &&
3485 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3486 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3487 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003488 {
3489 /* Check trusted CA's CRL for then chain's top crt */
3490 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3491
3492 if( x509parse_time_expired( &trust_ca->valid_to ) )
3493 ca_flags |= BADCERT_EXPIRED;
3494
Paul Bakker915275b2012-09-28 07:10:55 +00003495 if( NULL != f_vrfy )
3496 {
Paul Bakker9a736322012-11-14 12:39:52 +00003497 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003498 return( ret );
3499 }
3500 }
3501
3502 /* Call callback on top cert */
3503 if( NULL != f_vrfy )
3504 {
Paul Bakker9a736322012-11-14 12:39:52 +00003505 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003506 return( ret );
3507 }
3508
Paul Bakker915275b2012-09-28 07:10:55 +00003509 *flags |= ca_flags;
3510
3511 return( 0 );
3512}
3513
3514static int x509parse_verify_child(
3515 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003516 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003517 int (*f_vrfy)(void *, x509_cert *, int, int *),
3518 void *p_vrfy )
3519{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003520 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003521 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003522 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003523 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003524 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003525
3526 if( x509parse_time_expired( &child->valid_to ) )
3527 *flags |= BADCERT_EXPIRED;
3528
Paul Bakkerc70b9822013-04-07 22:00:46 +02003529 md_info = md_info_from_type( child->sig_md );
3530 if( md_info == NULL )
3531 {
3532 /*
3533 * Cannot check 'unknown' hash
3534 */
Paul Bakker915275b2012-09-28 07:10:55 +00003535 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003536 }
3537 else
3538 {
3539 md( md_info, child->tbs.p, child->tbs.len, hash );
3540
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003541 /* EC NOT IMPLEMENTED YET */
3542 if( parent->pk.type != POLARSSL_PK_RSA )
3543 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3544
3545 if( rsa_pkcs1_verify( pk_rsa( parent->pk ), RSA_PUBLIC, child->sig_md,
3546 0, hash, child->sig.p ) != 0 )
3547 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003548 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003549 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003550 }
3551
Paul Bakker915275b2012-09-28 07:10:55 +00003552 /* Check trusted CA's CRL for the given crt */
3553 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3554
3555 grandparent = parent->next;
3556
3557 while( grandparent != NULL )
3558 {
3559 if( grandparent->version == 0 ||
3560 grandparent->ca_istrue == 0 ||
3561 parent->issuer_raw.len != grandparent->subject_raw.len ||
3562 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3563 parent->issuer_raw.len ) != 0 )
3564 {
3565 grandparent = grandparent->next;
3566 continue;
3567 }
3568 break;
3569 }
3570
Paul Bakker915275b2012-09-28 07:10:55 +00003571 if( grandparent != NULL )
3572 {
3573 /*
3574 * Part of the chain
3575 */
Paul Bakker9a736322012-11-14 12:39:52 +00003576 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 +00003577 if( ret != 0 )
3578 return( ret );
3579 }
3580 else
3581 {
Paul Bakker9a736322012-11-14 12:39:52 +00003582 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 +00003583 if( ret != 0 )
3584 return( ret );
3585 }
3586
3587 /* child is verified to be a child of the parent, call verify callback */
3588 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003589 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003590 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003591
3592 *flags |= parent_flags;
3593
3594 return( 0 );
3595}
3596
Paul Bakker76fd75a2011-01-16 21:12:10 +00003597/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 * Verify the certificate validity
3599 */
3600int x509parse_verify( x509_cert *crt,
3601 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003602 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003603 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003604 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003605 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606{
Paul Bakker23986e52011-04-24 08:57:21 +00003607 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003608 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003609 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003610 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003612 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Paul Bakker40ea7de2009-05-03 10:18:48 +00003614 *flags = 0;
3615
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 if( cn != NULL )
3617 {
3618 name = &crt->subject;
3619 cn_len = strlen( cn );
3620
Paul Bakker4d2c1242012-05-10 14:12:46 +00003621 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003623 cur = &crt->subject_alt_names;
3624
3625 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003626 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003627 if( cur->buf.len == cn_len &&
3628 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003629 break;
3630
Paul Bakker535e97d2012-08-23 10:49:55 +00003631 if( cur->buf.len > 2 &&
3632 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003633 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003634 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003635
Paul Bakker4d2c1242012-05-10 14:12:46 +00003636 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003637 }
3638
3639 if( cur == NULL )
3640 *flags |= BADCERT_CN_MISMATCH;
3641 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003642 else
3643 {
3644 while( name != NULL )
3645 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003646 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003647 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003648 if( name->val.len == cn_len &&
3649 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003650 break;
3651
Paul Bakker535e97d2012-08-23 10:49:55 +00003652 if( name->val.len > 2 &&
3653 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003654 x509_wildcard_verify( cn, &name->val ) )
3655 break;
3656 }
3657
3658 name = name->next;
3659 }
3660
3661 if( name == NULL )
3662 *flags |= BADCERT_CN_MISMATCH;
3663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 }
3665
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003667 * Iterate upwards in the given cert chain, to find our crt parent.
3668 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003670 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
Paul Bakker76fd75a2011-01-16 21:12:10 +00003672 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003674 if( parent->ca_istrue == 0 ||
3675 crt->issuer_raw.len != parent->subject_raw.len ||
3676 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 crt->issuer_raw.len ) != 0 )
3678 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003679 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 continue;
3681 }
Paul Bakker915275b2012-09-28 07:10:55 +00003682 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 }
3684
Paul Bakker915275b2012-09-28 07:10:55 +00003685 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 {
Paul Bakker915275b2012-09-28 07:10:55 +00003687 /*
3688 * Part of the chain
3689 */
Paul Bakker9a736322012-11-14 12:39:52 +00003690 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003691 if( ret != 0 )
3692 return( ret );
3693 }
3694 else
Paul Bakker74111d32011-01-15 16:57:55 +00003695 {
Paul Bakker9a736322012-11-14 12:39:52 +00003696 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003697 if( ret != 0 )
3698 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003699 }
Paul Bakker915275b2012-09-28 07:10:55 +00003700
3701 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003702 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003703
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 return( 0 );
3705}
3706
3707/*
3708 * Unallocate all certificate data
3709 */
3710void x509_free( x509_cert *crt )
3711{
3712 x509_cert *cert_cur = crt;
3713 x509_cert *cert_prv;
3714 x509_name *name_cur;
3715 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003716 x509_sequence *seq_cur;
3717 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
3719 if( crt == NULL )
3720 return;
3721
3722 do
3723 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003724 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
3726 name_cur = cert_cur->issuer.next;
3727 while( name_cur != NULL )
3728 {
3729 name_prv = name_cur;
3730 name_cur = name_cur->next;
3731 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003732 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 }
3734
3735 name_cur = cert_cur->subject.next;
3736 while( name_cur != NULL )
3737 {
3738 name_prv = name_cur;
3739 name_cur = name_cur->next;
3740 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003741 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 }
3743
Paul Bakker74111d32011-01-15 16:57:55 +00003744 seq_cur = cert_cur->ext_key_usage.next;
3745 while( seq_cur != NULL )
3746 {
3747 seq_prv = seq_cur;
3748 seq_cur = seq_cur->next;
3749 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003750 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003751 }
3752
Paul Bakker8afa70d2012-02-11 18:42:45 +00003753 seq_cur = cert_cur->subject_alt_names.next;
3754 while( seq_cur != NULL )
3755 {
3756 seq_prv = seq_cur;
3757 seq_cur = seq_cur->next;
3758 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003759 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003760 }
3761
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 if( cert_cur->raw.p != NULL )
3763 {
3764 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003765 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 }
3767
3768 cert_cur = cert_cur->next;
3769 }
3770 while( cert_cur != NULL );
3771
3772 cert_cur = crt;
3773 do
3774 {
3775 cert_prv = cert_cur;
3776 cert_cur = cert_cur->next;
3777
3778 memset( cert_prv, 0, sizeof( x509_cert ) );
3779 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003780 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 }
3782 while( cert_cur != NULL );
3783}
3784
Paul Bakkerd98030e2009-05-02 15:13:40 +00003785/*
3786 * Unallocate all CRL data
3787 */
3788void x509_crl_free( x509_crl *crl )
3789{
3790 x509_crl *crl_cur = crl;
3791 x509_crl *crl_prv;
3792 x509_name *name_cur;
3793 x509_name *name_prv;
3794 x509_crl_entry *entry_cur;
3795 x509_crl_entry *entry_prv;
3796
3797 if( crl == NULL )
3798 return;
3799
3800 do
3801 {
3802 name_cur = crl_cur->issuer.next;
3803 while( name_cur != NULL )
3804 {
3805 name_prv = name_cur;
3806 name_cur = name_cur->next;
3807 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003808 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003809 }
3810
3811 entry_cur = crl_cur->entry.next;
3812 while( entry_cur != NULL )
3813 {
3814 entry_prv = entry_cur;
3815 entry_cur = entry_cur->next;
3816 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003817 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003818 }
3819
3820 if( crl_cur->raw.p != NULL )
3821 {
3822 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003823 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003824 }
3825
3826 crl_cur = crl_cur->next;
3827 }
3828 while( crl_cur != NULL );
3829
3830 crl_cur = crl;
3831 do
3832 {
3833 crl_prv = crl_cur;
3834 crl_cur = crl_cur->next;
3835
3836 memset( crl_prv, 0, sizeof( x509_crl ) );
3837 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003838 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003839 }
3840 while( crl_cur != NULL );
3841}
3842
Paul Bakker40e46942009-01-03 21:51:57 +00003843#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003844
Paul Bakker40e46942009-01-03 21:51:57 +00003845#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
3847/*
3848 * Checkup routine
3849 */
3850int x509_self_test( int verbose )
3851{
Paul Bakker5690efc2011-05-26 13:16:06 +00003852#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003853 int ret;
3854 int flags;
3855 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 x509_cert cacert;
3857 x509_cert clicert;
3858 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003859#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003860 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
3863 if( verbose != 0 )
3864 printf( " X.509 certificate load: " );
3865
3866 memset( &clicert, 0, sizeof( x509_cert ) );
3867
Paul Bakker3c2122f2013-06-24 19:03:14 +02003868 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003869 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( ret != 0 )
3871 {
3872 if( verbose != 0 )
3873 printf( "failed\n" );
3874
3875 return( ret );
3876 }
3877
3878 memset( &cacert, 0, sizeof( x509_cert ) );
3879
Paul Bakker3c2122f2013-06-24 19:03:14 +02003880 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003881 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 if( ret != 0 )
3883 {
3884 if( verbose != 0 )
3885 printf( "failed\n" );
3886
3887 return( ret );
3888 }
3889
3890 if( verbose != 0 )
3891 printf( "passed\n X.509 private key load: " );
3892
3893 i = strlen( test_ca_key );
3894 j = strlen( test_ca_pwd );
3895
Paul Bakker66b78b22011-03-25 14:22:50 +00003896 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3897
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003898 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003899 (const unsigned char *) test_ca_key, i,
3900 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 {
3902 if( verbose != 0 )
3903 printf( "failed\n" );
3904
3905 return( ret );
3906 }
3907
3908 if( verbose != 0 )
3909 printf( "passed\n X.509 signature verify: ");
3910
Paul Bakker23986e52011-04-24 08:57:21 +00003911 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 if( ret != 0 )
3913 {
Paul Bakker23986e52011-04-24 08:57:21 +00003914 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003915 if( verbose != 0 )
3916 printf( "failed\n" );
3917
3918 return( ret );
3919 }
3920
Paul Bakker5690efc2011-05-26 13:16:06 +00003921#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003922 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003923 printf( "passed\n X.509 DHM parameter load: " );
3924
3925 i = strlen( test_dhm_params );
3926 j = strlen( test_ca_pwd );
3927
Paul Bakker3c2122f2013-06-24 19:03:14 +02003928 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003929 {
3930 if( verbose != 0 )
3931 printf( "failed\n" );
3932
3933 return( ret );
3934 }
3935
3936 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003937 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003939
3940 x509_free( &cacert );
3941 x509_free( &clicert );
3942 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003943#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003944 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
3947 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003948#else
3949 ((void) verbose);
3950 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3951#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003952}
3953
3954#endif
3955
3956#endif