blob: 2290b7b4cdc0c6826705f95877b057c6e42cc6d7 [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 Bakkerf9f377e2013-09-09 15:35:10 +0200135 * Version ::= INTEGER { v1(0) }
136 */
137static int x509_csr_get_version( unsigned char **p,
138 const unsigned char *end,
139 int *ver )
140{
141 int ret;
142
143 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
144 {
145 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
146 {
147 *ver = 0;
148 return( 0 );
149 }
150
151 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
152 }
153
154 return( 0 );
155}
156
157/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * CertificateSerialNumber ::= INTEGER
159 */
160static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 x509_buf *serial )
163{
164 int ret;
165
166 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000167 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000168 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
171 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000172 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000173 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175 serial->tag = *(*p)++;
176
177 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000178 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 serial->p = *p;
181 *p += serial->len;
182
183 return( 0 );
184}
185
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200186/* Get a PK algorithm identifier
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200187 *
188 * AlgorithmIdentifier ::= SEQUENCE {
189 * algorithm OBJECT IDENTIFIER,
190 * parameters ANY DEFINED BY algorithm OPTIONAL }
191 */
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200192static int x509_get_pk_alg( unsigned char **p,
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200193 const unsigned char *end,
194 pk_type_t *pk_alg, x509_buf *params )
Manuel Pégourié-Gonnard0a64e8f2013-07-08 18:26:18 +0200195{
196 int ret;
197 x509_buf alg_oid;
198
199 memset( params, 0, sizeof(asn1_buf) );
200
201 if( ( ret = asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
202 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
203
204 if( oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
205 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
206
207 /*
208 * No parameters with RSA (only for EC)
209 */
210 if( *pk_alg == POLARSSL_PK_RSA &&
211 ( ( params->tag != ASN1_NULL && params->tag != 0 ) ||
212 params->len != 0 ) )
213 {
214 return( POLARSSL_ERR_X509_CERT_INVALID_ALG );
215 }
216
217 return( 0 );
218}
219
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200220/* Get an algorithm identifier without parameters (eg for signatures)
221 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 * AlgorithmIdentifier ::= SEQUENCE {
223 * algorithm OBJECT IDENTIFIER,
224 * parameters ANY DEFINED BY algorithm OPTIONAL }
225 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200226static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
227 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228{
Paul Bakker23986e52011-04-24 08:57:21 +0000229 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200231 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000232 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 return( 0 );
235}
236
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200237
238#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200239/* Get an EC group id from an ECParameters buffer
240 *
241 * ECParameters ::= CHOICE {
242 * namedCurve OBJECT IDENTIFIER
243 * -- implicitCurve NULL
244 * -- specifiedCurve SpecifiedECDomain
245 * }
246 */
247static int x509_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200248 x509_buf *params )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200249{
250 int ret;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200251
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200252 params->tag = **p;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200253
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200254 if( ( ret = asn1_get_tag( p, end, &params->len, ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200255 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
256
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200257 params->p = *p;
258 *p += params->len;
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200259
260 if( *p != end )
261 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
262 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
263
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200264 return( 0 );
265}
266
267/*
268 * Use EC parameters to initialise an EC group
269 */
270static int x509_use_ecparams( const x509_buf *params, ecp_group *grp )
271{
272 int ret;
273 ecp_group_id grp_id;
274
275 if( oid_get_ec_grp( params, &grp_id ) != 0 )
276 return( POLARSSL_ERR_X509_UNKNOWN_NAMED_CURVE );
277
278 /*
279 * grp may already be initilialized; if so, make sure IDs match
280 */
281 if( grp->id != POLARSSL_ECP_DP_NONE && grp->id != grp_id )
282 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
283
284 if( ( ret = ecp_use_known_dp( grp, grp_id ) ) != 0 )
285 return( ret );
286
287 return( 0 );
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200288}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200289#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardf838eed2013-07-02 14:56:43 +0200290
Paul Bakker5121ce52009-01-03 21:22:43 +0000291/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * AttributeTypeAndValue ::= SEQUENCE {
293 * type AttributeType,
294 * value AttributeValue }
295 *
296 * AttributeType ::= OBJECT IDENTIFIER
297 *
298 * AttributeValue ::= ANY DEFINED BY AttributeType
299 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000300static int x509_get_attr_type_value( unsigned char **p,
301 const unsigned char *end,
302 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000303{
Paul Bakker23986e52011-04-24 08:57:21 +0000304 int ret;
305 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 x509_buf *oid;
307 x509_buf *val;
308
309 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000311 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200313 if( ( end - *p ) < 1 )
314 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
315 POLARSSL_ERR_ASN1_OUT_OF_DATA );
316
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 oid = &cur->oid;
318 oid->tag = **p;
319
320 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000321 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 oid->p = *p;
324 *p += oid->len;
325
326 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000327 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000328 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
331 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
332 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000333 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000334 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336 val = &cur->val;
337 val->tag = *(*p)++;
338
339 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000340 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 val->p = *p;
343 *p += val->len;
344
345 cur->next = NULL;
346
Paul Bakker400ff6f2011-02-20 10:40:16 +0000347 return( 0 );
348}
349
350/*
351 * RelativeDistinguishedName ::=
352 * SET OF AttributeTypeAndValue
353 *
354 * AttributeTypeAndValue ::= SEQUENCE {
355 * type AttributeType,
356 * value AttributeValue }
357 *
358 * AttributeType ::= OBJECT IDENTIFIER
359 *
360 * AttributeValue ::= ANY DEFINED BY AttributeType
361 */
362static int x509_get_name( unsigned char **p,
363 const unsigned char *end,
364 x509_name *cur )
365{
Paul Bakker23986e52011-04-24 08:57:21 +0000366 int ret;
367 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000368 const unsigned char *end2;
369 x509_name *use;
370
371 if( ( ret = asn1_get_tag( p, end, &len,
372 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000373 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000374
375 end2 = end;
376 end = *p + len;
377 use = cur;
378
379 do
380 {
381 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
382 return( ret );
383
384 if( *p != end )
385 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200386 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000387 sizeof( x509_name ) );
388
389 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000390 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000391
392 memset( use->next, 0, sizeof( x509_name ) );
393
394 use = use->next;
395 }
396 }
397 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399 /*
400 * recurse until end of SEQUENCE is reached
401 */
402 if( *p == end2 )
403 return( 0 );
404
Paul Bakker6e339b52013-07-03 13:37:05 +0200405 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 sizeof( x509_name ) );
407
408 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000409 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Paul Bakker430ffbe2012-05-01 08:14:20 +0000411 memset( cur->next, 0, sizeof( x509_name ) );
412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 return( x509_get_name( p, end2, cur->next ) );
414}
415
416/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 * Time ::= CHOICE {
418 * utcTime UTCTime,
419 * generalTime GeneralizedTime }
420 */
Paul Bakker91200182010-02-18 21:26:15 +0000421static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000422 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000423 x509_time *time )
424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 int ret;
426 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000428 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000429
Paul Bakker91200182010-02-18 21:26:15 +0000430 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000431 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000433
Paul Bakker91200182010-02-18 21:26:15 +0000434 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000435
Paul Bakker91200182010-02-18 21:26:15 +0000436 if ( tag == ASN1_UTC_TIME )
437 {
438 (*p)++;
439 ret = asn1_get_len( p, end, &len );
440
441 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000442 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000443
Paul Bakker91200182010-02-18 21:26:15 +0000444 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000445 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
446 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000447
Paul Bakker91200182010-02-18 21:26:15 +0000448 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
449 &time->year, &time->mon, &time->day,
450 &time->hour, &time->min, &time->sec ) < 5 )
451 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000452
Paul Bakker400ff6f2011-02-20 10:40:16 +0000453 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000454 time->year += 1900;
455
456 *p += len;
457
458 return( 0 );
459 }
460 else if ( tag == ASN1_GENERALIZED_TIME )
461 {
462 (*p)++;
463 ret = asn1_get_len( p, end, &len );
464
465 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000467
468 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000469 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
470 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000471
472 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
473 &time->year, &time->mon, &time->day,
474 &time->hour, &time->min, &time->sec ) < 5 )
475 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
476
477 *p += len;
478
479 return( 0 );
480 }
481 else
Paul Bakker9d781402011-05-09 16:17:09 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000483}
484
485
486/*
487 * Validity ::= SEQUENCE {
488 * notBefore Time,
489 * notAfter Time }
490 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000491static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000492 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 x509_time *from,
494 x509_time *to )
495{
Paul Bakker23986e52011-04-24 08:57:21 +0000496 int ret;
497 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 if( ( ret = asn1_get_tag( p, end, &len,
500 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000501 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 end = *p + len;
504
Paul Bakker91200182010-02-18 21:26:15 +0000505 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000506 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker91200182010-02-18 21:26:15 +0000508 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000509 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000512 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000513 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 return( 0 );
516}
517
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200518#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000519/*
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200520 * RSAPublicKey ::= SEQUENCE {
521 * modulus INTEGER, -- n
522 * publicExponent INTEGER -- e
523 * }
524 */
525static int x509_get_rsapubkey( unsigned char **p,
526 const unsigned char *end,
527 rsa_context *rsa )
528{
529 int ret;
530 size_t len;
531
532 if( ( ret = asn1_get_tag( p, end, &len,
533 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
534 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
535
536 if( *p + len != end )
537 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
538 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
539
540 if( ( ret = asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
541 ( ret = asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
542 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
543
Manuel Pégourié-Gonnardc13c0d42013-08-15 13:58:01 +0200544 if( *p != end )
545 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
546 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
547
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200548 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
549 return( ret );
550
551 rsa->len = mpi_size( &rsa->N );
552
553 return( 0 );
554}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200555#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard094ad9e2013-07-09 12:32:51 +0200556
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200557#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +0200558/*
559 * EC public key is an EC point
560 */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200561static int x509_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200562 ecp_keypair *key )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200563{
564 int ret;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200565
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200566 if( ( ret = ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200567 (const unsigned char *) *p, end - *p ) ) != 0 ||
568 ( ret = ecp_check_pubkey( &key->grp, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200569 {
570 ecp_keypair_free( key );
571 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200572 }
573
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200574 /*
575 * We know ecp_point_read_binary consumed all bytes
576 */
577 *p = (unsigned char *) end;
578
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200579 return( 0 );
580}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200581#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200582
583/*
584 * SubjectPublicKeyInfo ::= SEQUENCE {
585 * algorithm AlgorithmIdentifier,
586 * subjectPublicKey BIT STRING }
587 */
588static int x509_get_pubkey( unsigned char **p,
589 const unsigned char *end,
590 pk_context *pk )
591{
592 int ret;
593 size_t len;
594 x509_buf alg_params;
595 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200596 const pk_info_t *pk_info;
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200597
598 if( ( ret = asn1_get_tag( p, end, &len,
599 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
600 {
601 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
602 }
603
604 end = *p + len;
605
Manuel Pégourié-Gonnard7a287c42013-07-10 12:55:08 +0200606 if( ( ret = x509_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200607 return( ret );
608
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200609 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200610 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
611
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200612 if( *p + len != end )
613 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
614 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
615
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200616 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
617 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
618
619 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200620 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200621
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200622#if defined(POLARSSL_RSA_C)
623 if( pk_alg == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200624 {
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200625 ret = x509_get_rsapubkey( p, end, pk_rsa( *pk ) );
626 } else
627#endif /* POLARSSL_RSA_C */
628#if defined(POLARSSL_ECP_C)
629 if( pk_alg == POLARSSL_PK_ECKEY_DH || pk_alg == POLARSSL_PK_ECKEY )
630 {
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200631 ret = x509_use_ecparams( &alg_params, &pk_ec( *pk )->grp );
632 if( ret == 0 )
633 ret = x509_get_ecpubkey( p, end, pk_ec( *pk ) );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +0200634 } else
635#endif /* POLARSSL_ECP_C */
636 ret = POLARSSL_ERR_X509_UNKNOWN_PK_ALG;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200637
Manuel Pégourié-Gonnard5b18fb02013-07-10 16:07:25 +0200638 if( ret == 0 && *p != end )
639 ret = POLARSSL_ERR_X509_CERT_INVALID_PUBKEY
640 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
641
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +0200642 if( ret != 0 )
643 pk_free( pk );
644
645 return( ret );
Manuel Pégourié-Gonnardc296c592013-07-09 12:54:04 +0200646}
647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000649 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 x509_buf *sig )
651{
Paul Bakker23986e52011-04-24 08:57:21 +0000652 int ret;
653 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker8afa70d2012-02-11 18:42:45 +0000655 if( ( end - *p ) < 1 )
656 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
657 POLARSSL_ERR_ASN1_OUT_OF_DATA );
658
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 sig->tag = **p;
660
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200661 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000662 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 sig->len = len;
665 sig->p = *p;
666
667 *p += len;
668
669 return( 0 );
670}
671
672/*
673 * X.509 v2/v3 unique identifier (not parsed)
674 */
675static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000676 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 x509_buf *uid, int n )
678{
679 int ret;
680
681 if( *p == end )
682 return( 0 );
683
684 uid->tag = **p;
685
686 if( ( ret = asn1_get_tag( p, end, &uid->len,
687 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
688 {
Paul Bakker40e46942009-01-03 21:51:57 +0000689 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 return( 0 );
691
692 return( ret );
693 }
694
695 uid->p = *p;
696 *p += uid->len;
697
698 return( 0 );
699}
700
701/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000702 * X.509 Extensions (No parsing of extensions, pointer should
703 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 */
705static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000706 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000707 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000708{
Paul Bakker23986e52011-04-24 08:57:21 +0000709 int ret;
710 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 if( *p == end )
713 return( 0 );
714
715 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000716
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000718 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000719 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 ext->p = *p;
722 end = *p + ext->len;
723
724 /*
725 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
726 *
727 * Extension ::= SEQUENCE {
728 * extnID OBJECT IDENTIFIER,
729 * critical BOOLEAN DEFAULT FALSE,
730 * extnValue OCTET STRING }
731 */
732 if( ( ret = asn1_get_tag( p, end, &len,
733 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000734 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000737 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
Paul Bakkerd98030e2009-05-02 15:13:40 +0000740 return( 0 );
741}
742
743/*
744 * X.509 CRL v2 extensions (no extensions parsed yet.)
745 */
746static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000747 const unsigned char *end,
748 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000749{
Paul Bakker23986e52011-04-24 08:57:21 +0000750 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000751 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000752
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000753 /* Get explicit tag */
754 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000755 {
756 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
757 return( 0 );
758
759 return( ret );
760 }
761
762 while( *p < end )
763 {
764 if( ( ret = asn1_get_tag( p, end, &len,
765 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000767
768 *p += len;
769 }
770
771 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000772 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000773 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
774
775 return( 0 );
776}
777
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000778/*
779 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
780 */
781static int x509_get_crl_entry_ext( unsigned char **p,
782 const unsigned char *end,
783 x509_buf *ext )
784{
785 int ret;
786 size_t len = 0;
787
788 /* OPTIONAL */
789 if (end <= *p)
790 return( 0 );
791
792 ext->tag = **p;
793 ext->p = *p;
794
795 /*
796 * Get CRL-entry extension sequence header
797 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
798 */
799 if( ( ret = asn1_get_tag( p, end, &ext->len,
800 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
801 {
802 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
803 {
804 ext->p = NULL;
805 return( 0 );
806 }
807 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
808 }
809
810 end = *p + ext->len;
811
812 if( end != *p + ext->len )
813 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
814 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
815
816 while( *p < end )
817 {
818 if( ( ret = asn1_get_tag( p, end, &len,
819 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
820 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
821
822 *p += len;
823 }
824
825 if( *p != end )
826 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
827 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
828
829 return( 0 );
830}
831
Paul Bakker74111d32011-01-15 16:57:55 +0000832static int x509_get_basic_constraints( unsigned char **p,
833 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000834 int *ca_istrue,
835 int *max_pathlen )
836{
Paul Bakker23986e52011-04-24 08:57:21 +0000837 int ret;
838 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000839
840 /*
841 * BasicConstraints ::= SEQUENCE {
842 * cA BOOLEAN DEFAULT FALSE,
843 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
844 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000845 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000846 *max_pathlen = 0; /* endless */
847
848 if( ( ret = asn1_get_tag( p, end, &len,
849 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000850 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000851
852 if( *p == end )
853 return 0;
854
Paul Bakker3cccddb2011-01-16 21:46:31 +0000855 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000856 {
857 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000858 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000859
860 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000862
Paul Bakker3cccddb2011-01-16 21:46:31 +0000863 if( *ca_istrue != 0 )
864 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000865 }
866
867 if( *p == end )
868 return 0;
869
870 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000871 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000872
873 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000874 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000875 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
876
877 (*max_pathlen)++;
878
Paul Bakker74111d32011-01-15 16:57:55 +0000879 return 0;
880}
881
882static int x509_get_ns_cert_type( unsigned char **p,
883 const unsigned char *end,
884 unsigned char *ns_cert_type)
885{
886 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000887 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000888
889 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000890 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000891
892 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000893 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000894 POLARSSL_ERR_ASN1_INVALID_LENGTH );
895
896 /* Get actual bitstring */
897 *ns_cert_type = *bs.p;
898 return 0;
899}
900
901static int x509_get_key_usage( unsigned char **p,
902 const unsigned char *end,
903 unsigned char *key_usage)
904{
905 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000906 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000907
908 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000909 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000910
Paul Bakker94a67962012-08-23 13:03:52 +0000911 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000912 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000913 POLARSSL_ERR_ASN1_INVALID_LENGTH );
914
915 /* Get actual bitstring */
916 *key_usage = *bs.p;
917 return 0;
918}
919
Paul Bakkerd98030e2009-05-02 15:13:40 +0000920/*
Paul Bakker74111d32011-01-15 16:57:55 +0000921 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
922 *
923 * KeyPurposeId ::= OBJECT IDENTIFIER
924 */
925static int x509_get_ext_key_usage( unsigned char **p,
926 const unsigned char *end,
927 x509_sequence *ext_key_usage)
928{
929 int ret;
930
931 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000933
934 /* Sequence length must be >= 1 */
935 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000936 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000937 POLARSSL_ERR_ASN1_INVALID_LENGTH );
938
939 return 0;
940}
941
942/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000943 * SubjectAltName ::= GeneralNames
944 *
945 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
946 *
947 * GeneralName ::= CHOICE {
948 * otherName [0] OtherName,
949 * rfc822Name [1] IA5String,
950 * dNSName [2] IA5String,
951 * x400Address [3] ORAddress,
952 * directoryName [4] Name,
953 * ediPartyName [5] EDIPartyName,
954 * uniformResourceIdentifier [6] IA5String,
955 * iPAddress [7] OCTET STRING,
956 * registeredID [8] OBJECT IDENTIFIER }
957 *
958 * OtherName ::= SEQUENCE {
959 * type-id OBJECT IDENTIFIER,
960 * value [0] EXPLICIT ANY DEFINED BY type-id }
961 *
962 * EDIPartyName ::= SEQUENCE {
963 * nameAssigner [0] DirectoryString OPTIONAL,
964 * partyName [1] DirectoryString }
965 *
966 * NOTE: PolarSSL only parses and uses dNSName at this point.
967 */
968static int x509_get_subject_alt_name( unsigned char **p,
969 const unsigned char *end,
970 x509_sequence *subject_alt_name )
971{
972 int ret;
973 size_t len, tag_len;
974 asn1_buf *buf;
975 unsigned char tag;
976 asn1_sequence *cur = subject_alt_name;
977
978 /* Get main sequence tag */
979 if( ( ret = asn1_get_tag( p, end, &len,
980 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
981 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
982
983 if( *p + len != end )
984 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
985 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
986
987 while( *p < end )
988 {
989 if( ( end - *p ) < 1 )
990 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
991 POLARSSL_ERR_ASN1_OUT_OF_DATA );
992
993 tag = **p;
994 (*p)++;
995 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
996 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
997
998 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
999 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1000 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1001
1002 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
1003 {
1004 *p += tag_len;
1005 continue;
1006 }
1007
1008 buf = &(cur->buf);
1009 buf->tag = tag;
1010 buf->p = *p;
1011 buf->len = tag_len;
1012 *p += buf->len;
1013
1014 /* Allocate and assign next pointer */
1015 if (*p < end)
1016 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001017 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +00001018 sizeof( asn1_sequence ) );
1019
1020 if( cur->next == NULL )
1021 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1022 POLARSSL_ERR_ASN1_MALLOC_FAILED );
1023
Paul Bakker535e97d2012-08-23 10:49:55 +00001024 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +00001025 cur = cur->next;
1026 }
1027 }
1028
1029 /* Set final sequence entry's next pointer to NULL */
1030 cur->next = NULL;
1031
1032 if( *p != end )
1033 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
1034 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1035
1036 return( 0 );
1037}
1038
1039/*
Paul Bakker74111d32011-01-15 16:57:55 +00001040 * X.509 v3 extensions
1041 *
1042 * TODO: Perform all of the basic constraints tests required by the RFC
1043 * TODO: Set values for undetected extensions to a sane default?
1044 *
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 */
1046static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001047 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +00001048 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049{
Paul Bakker23986e52011-04-24 08:57:21 +00001050 int ret;
1051 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001052 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001053
Paul Bakkerfbc09f32011-10-12 09:56:41 +00001054 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001055 {
1056 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1057 return( 0 );
1058
1059 return( ret );
1060 }
1061
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 while( *p < end )
1063 {
Paul Bakker74111d32011-01-15 16:57:55 +00001064 /*
1065 * Extension ::= SEQUENCE {
1066 * extnID OBJECT IDENTIFIER,
1067 * critical BOOLEAN DEFAULT FALSE,
1068 * extnValue OCTET STRING }
1069 */
1070 x509_buf extn_oid = {0, 0, NULL};
1071 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001072 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +00001073
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 if( ( ret = asn1_get_tag( p, end, &len,
1075 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001076 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001078 end_ext_data = *p + len;
1079
Paul Bakker74111d32011-01-15 16:57:55 +00001080 /* Get extension ID */
1081 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Paul Bakker74111d32011-01-15 16:57:55 +00001083 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Paul Bakker74111d32011-01-15 16:57:55 +00001086 extn_oid.p = *p;
1087 *p += extn_oid.len;
1088
1089 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +00001090 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001091 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1092
1093 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001094 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +00001095 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +00001096 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Paul Bakker74111d32011-01-15 16:57:55 +00001098 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001099 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001101 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001103 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001104
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001105 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001106 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001107 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Paul Bakker74111d32011-01-15 16:57:55 +00001109 /*
1110 * Detect supported extensions
1111 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02001112 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
1113
1114 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001115 {
1116 /* No parser found, skip extension */
1117 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker5c721f92011-07-27 16:51:09 +00001119#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001120 if( is_critical )
1121 {
1122 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001123 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001124 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1125 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001126#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +02001127 continue;
1128 }
1129
1130 crt->ext_types |= ext_type;
1131
1132 switch( ext_type )
1133 {
1134 case EXT_BASIC_CONSTRAINTS:
1135 /* Parse basic constraints */
1136 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1137 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1138 return ( ret );
1139 break;
1140
1141 case EXT_KEY_USAGE:
1142 /* Parse key usage */
1143 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1144 &crt->key_usage ) ) != 0 )
1145 return ( ret );
1146 break;
1147
1148 case EXT_EXTENDED_KEY_USAGE:
1149 /* Parse extended key usage */
1150 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1151 &crt->ext_key_usage ) ) != 0 )
1152 return ( ret );
1153 break;
1154
1155 case EXT_SUBJECT_ALT_NAME:
1156 /* Parse subject alt name */
1157 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1158 &crt->subject_alt_names ) ) != 0 )
1159 return ( ret );
1160 break;
1161
1162 case EXT_NS_CERT_TYPE:
1163 /* Parse netscape certificate type */
1164 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1165 &crt->ns_cert_type ) ) != 0 )
1166 return ( ret );
1167 break;
1168
1169 default:
1170 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 }
1173
1174 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001175 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001176 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
1181/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001182 * X.509 CRL Entries
1183 */
1184static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001185 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001186 x509_crl_entry *entry )
1187{
Paul Bakker23986e52011-04-24 08:57:21 +00001188 int ret;
1189 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001190 x509_crl_entry *cur_entry = entry;
1191
1192 if( *p == end )
1193 return( 0 );
1194
Paul Bakker9be19372009-07-27 20:21:53 +00001195 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001196 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1197 {
1198 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1199 return( 0 );
1200
1201 return( ret );
1202 }
1203
Paul Bakker9be19372009-07-27 20:21:53 +00001204 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001205
1206 while( *p < end )
1207 {
Paul Bakker23986e52011-04-24 08:57:21 +00001208 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001209 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001210
1211 if( ( ret = asn1_get_tag( p, end, &len2,
1212 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1213 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001214 return( ret );
1215 }
1216
Paul Bakker9be19372009-07-27 20:21:53 +00001217 cur_entry->raw.tag = **p;
1218 cur_entry->raw.p = *p;
1219 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001220 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001221
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001222 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001223 return( ret );
1224
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001225 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001226 return( ret );
1227
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001228 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001229 return( ret );
1230
Paul Bakker74111d32011-01-15 16:57:55 +00001231 if ( *p < end )
1232 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001233 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001234
1235 if( cur_entry->next == NULL )
1236 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1237
Paul Bakkerd98030e2009-05-02 15:13:40 +00001238 cur_entry = cur_entry->next;
1239 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1240 }
1241 }
1242
1243 return( 0 );
1244}
1245
Paul Bakkerc70b9822013-04-07 22:00:46 +02001246static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1247 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001248{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001249 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001250
Paul Bakkerc70b9822013-04-07 22:00:46 +02001251 if( ret != 0 )
1252 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001253
Paul Bakkerc70b9822013-04-07 22:00:46 +02001254 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001255}
1256
Paul Bakkerd98030e2009-05-02 15:13:40 +00001257/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001258 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001260static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1261 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262{
Paul Bakker23986e52011-04-24 08:57:21 +00001263 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001264 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001265 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Paul Bakker320a4b52009-03-28 18:52:39 +00001267 /*
1268 * Check for valid input
1269 */
1270 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001271 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001272
Paul Bakker6e339b52013-07-03 13:37:05 +02001273 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001274
1275 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001276 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001277
1278 memcpy( p, buf, buflen );
1279
1280 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 crt->raw.p = p;
1283 crt->raw.len = len;
1284 end = p + len;
1285
1286 /*
1287 * Certificate ::= SEQUENCE {
1288 * tbsCertificate TBSCertificate,
1289 * signatureAlgorithm AlgorithmIdentifier,
1290 * signatureValue BIT STRING }
1291 */
1292 if( ( ret = asn1_get_tag( &p, end, &len,
1293 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1294 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001296 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 }
1298
Paul Bakkerb00ca422012-09-25 12:10:00 +00001299 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001301 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001302 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001303 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001305 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001306
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 /*
1308 * TBSCertificate ::= SEQUENCE {
1309 */
1310 crt->tbs.p = p;
1311
1312 if( ( ret = asn1_get_tag( &p, end, &len,
1313 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1314 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001315 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001316 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 }
1318
1319 end = p + len;
1320 crt->tbs.len = end - crt->tbs.p;
1321
1322 /*
1323 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1324 *
1325 * CertificateSerialNumber ::= INTEGER
1326 *
1327 * signature AlgorithmIdentifier
1328 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001329 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1330 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1331 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001333 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 return( ret );
1335 }
1336
1337 crt->version++;
1338
1339 if( crt->version > 3 )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001342 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344
Paul Bakkerc70b9822013-04-07 22:00:46 +02001345 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1346 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001348 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001349 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351
1352 /*
1353 * issuer Name
1354 */
1355 crt->issuer_raw.p = p;
1356
1357 if( ( ret = asn1_get_tag( &p, end, &len,
1358 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001361 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
1363
1364 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1365 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001366 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 return( ret );
1368 }
1369
1370 crt->issuer_raw.len = p - crt->issuer_raw.p;
1371
1372 /*
1373 * Validity ::= SEQUENCE {
1374 * notBefore Time,
1375 * notAfter Time }
1376 *
1377 */
1378 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1379 &crt->valid_to ) ) != 0 )
1380 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001381 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 return( ret );
1383 }
1384
1385 /*
1386 * subject Name
1387 */
1388 crt->subject_raw.p = p;
1389
1390 if( ( ret = asn1_get_tag( &p, end, &len,
1391 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1392 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001393 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001394 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
1396
Paul Bakkercefb3962012-06-27 11:51:09 +00001397 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001399 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 return( ret );
1401 }
1402
1403 crt->subject_raw.len = p - crt->subject_raw.p;
1404
1405 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001406 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 */
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02001408 if( ( ret = x509_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001410 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 return( ret );
1412 }
1413
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 /*
1415 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1416 * -- If present, version shall be v2 or v3
1417 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1418 * -- If present, version shall be v2 or v3
1419 * extensions [3] EXPLICIT Extensions OPTIONAL
1420 * -- If present, version shall be v3
1421 */
1422 if( crt->version == 2 || crt->version == 3 )
1423 {
1424 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1425 if( ret != 0 )
1426 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001427 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 return( ret );
1429 }
1430 }
1431
1432 if( crt->version == 2 || crt->version == 3 )
1433 {
1434 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1435 if( ret != 0 )
1436 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001437 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 return( ret );
1439 }
1440 }
1441
1442 if( crt->version == 3 )
1443 {
Paul Bakker74111d32011-01-15 16:57:55 +00001444 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 if( ret != 0 )
1446 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001447 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 return( ret );
1449 }
1450 }
1451
1452 if( p != end )
1453 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001455 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001456 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 }
1458
Paul Bakkerb00ca422012-09-25 12:10:00 +00001459 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
1461 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001462 * }
1463 * -- end of TBSCertificate
1464 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001465 * signatureAlgorithm AlgorithmIdentifier,
1466 * signatureValue BIT STRING
1467 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001468 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001469 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001470 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 return( ret );
1472 }
1473
Paul Bakker535e97d2012-08-23 10:49:55 +00001474 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1475 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001477 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001478 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 }
1480
1481 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1482 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001483 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 return( ret );
1485 }
1486
1487 if( p != end )
1488 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001489 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001490 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001491 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
1493
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494 return( 0 );
1495}
1496
1497/*
Paul Bakker42c65812013-06-24 19:21:59 +02001498 * Parse one X.509 certificate in DER format from a buffer and add them to a
1499 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 */
Paul Bakker42c65812013-06-24 19:21:59 +02001501int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001502{
Paul Bakker42c65812013-06-24 19:21:59 +02001503 int ret;
1504 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001505
1506 /*
1507 * Check for valid input
1508 */
1509 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001510 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001511
1512 while( crt->version != 0 && crt->next != NULL )
1513 {
1514 prev = crt;
1515 crt = crt->next;
1516 }
1517
1518 /*
1519 * Add new certificate on the end of the chain if needed.
1520 */
1521 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001522 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001523 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001524
Paul Bakker7d06ad22009-05-02 15:53:56 +00001525 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001526 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001527
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001528 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001529 crt = crt->next;
1530 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Paul Bakker42c65812013-06-24 19:21:59 +02001533 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1534 {
1535 if( prev )
1536 prev->next = NULL;
1537
1538 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001539 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001540
1541 return( ret );
1542 }
1543
1544 return( 0 );
1545}
1546
1547/*
1548 * Parse one or more PEM certificates from a buffer and add them to the chained list
1549 */
1550int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1551{
1552 int ret, success = 0, first_error = 0, total_failed = 0;
1553 int buf_format = X509_FORMAT_DER;
1554
1555 /*
1556 * Check for valid input
1557 */
1558 if( chain == NULL || buf == NULL )
1559 return( POLARSSL_ERR_X509_INVALID_INPUT );
1560
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001561 /*
1562 * Determine buffer content. Buffer contains either one DER certificate or
1563 * one or more PEM certificates.
1564 */
1565#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001566 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001567 buf_format = X509_FORMAT_PEM;
1568#endif
1569
1570 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001571 return x509parse_crt_der( chain, buf, buflen );
1572
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573#if defined(POLARSSL_PEM_C)
1574 if( buf_format == X509_FORMAT_PEM )
1575 {
1576 pem_context pem;
1577
1578 while( buflen > 0 )
1579 {
1580 size_t use_len;
1581 pem_init( &pem );
1582
1583 ret = pem_read_buffer( &pem,
1584 "-----BEGIN CERTIFICATE-----",
1585 "-----END CERTIFICATE-----",
1586 buf, NULL, 0, &use_len );
1587
1588 if( ret == 0 )
1589 {
1590 /*
1591 * Was PEM encoded
1592 */
1593 buflen -= use_len;
1594 buf += use_len;
1595 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001596 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1597 {
1598 return( ret );
1599 }
Paul Bakker00b28602013-06-24 13:02:41 +02001600 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001601 {
1602 pem_free( &pem );
1603
Paul Bakker5ed3b342013-06-24 19:05:46 +02001604 /*
1605 * PEM header and footer were found
1606 */
1607 buflen -= use_len;
1608 buf += use_len;
1609
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001610 if( first_error == 0 )
1611 first_error = ret;
1612
1613 continue;
1614 }
1615 else
1616 break;
1617
Paul Bakker42c65812013-06-24 19:21:59 +02001618 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001619
1620 pem_free( &pem );
1621
1622 if( ret != 0 )
1623 {
1624 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001625 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001626 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001627 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001628 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001629
1630 if( first_error == 0 )
1631 first_error = ret;
1632
Paul Bakker42c65812013-06-24 19:21:59 +02001633 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001634 continue;
1635 }
1636
1637 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001638 }
1639 }
1640#endif
1641
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001642 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001643 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001644 else if( first_error )
1645 return( first_error );
1646 else
1647 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001648}
1649
1650/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001651 * Parse a CSR
1652 */
1653int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen )
1654{
1655 int ret;
1656 size_t len;
1657 unsigned char *p, *end;
1658#if defined(POLARSSL_PEM_C)
1659 size_t use_len;
1660 pem_context pem;
1661#endif
1662
1663 /*
1664 * Check for valid input
1665 */
1666 if( csr == NULL || buf == NULL )
1667 return( POLARSSL_ERR_X509_INVALID_INPUT );
1668
1669 memset( csr, 0, sizeof( x509_csr ) );
1670
1671#if defined(POLARSSL_PEM_C)
1672 pem_init( &pem );
1673 ret = pem_read_buffer( &pem,
1674 "-----BEGIN CERTIFICATE REQUEST-----",
1675 "-----END CERTIFICATE REQUEST-----",
1676 buf, NULL, 0, &use_len );
1677
1678 if( ret == 0 )
1679 {
1680 /*
1681 * Was PEM encoded
1682 */
1683 buflen -= use_len;
1684 buf += use_len;
1685
1686 /*
1687 * Steal PEM buffer
1688 */
1689 p = pem.buf;
1690 pem.buf = NULL;
1691 len = pem.buflen;
1692 pem_free( &pem );
1693 }
1694 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1695 {
1696 pem_free( &pem );
1697 return( ret );
1698 }
1699 else
1700 {
1701 /*
1702 * nope, copy the raw DER data
1703 */
1704 p = (unsigned char *) polarssl_malloc( len = buflen );
1705
1706 if( p == NULL )
1707 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1708
1709 memcpy( p, buf, buflen );
1710
1711 buflen = 0;
1712 }
1713#else
1714 p = (unsigned char *) polarssl_malloc( len = buflen );
1715
1716 if( p == NULL )
1717 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1718
1719 memcpy( p, buf, buflen );
1720
1721 buflen = 0;
1722#endif
1723
1724 csr->raw.p = p;
1725 csr->raw.len = len;
1726 end = p + len;
1727
1728 /*
1729 * CertificationRequest ::= SEQUENCE {
1730 * certificationRequestInfo CertificationRequestInfo,
1731 * signatureAlgorithm AlgorithmIdentifier,
1732 * signature BIT STRING
1733 * }
1734 */
1735 if( ( ret = asn1_get_tag( &p, end, &len,
1736 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1737 {
1738 x509_csr_free( csr );
1739 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1740 }
1741
1742 if( len != (size_t) ( end - p ) )
1743 {
1744 x509_csr_free( csr );
1745 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1746 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1747 }
1748
1749 /*
1750 * CertificationRequestInfo ::= SEQUENCE {
1751 */
1752 csr->cri.p = p;
1753
1754 if( ( ret = asn1_get_tag( &p, end, &len,
1755 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1756 {
1757 x509_csr_free( csr );
1758 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1759 }
1760
1761 end = p + len;
1762 csr->cri.len = end - csr->cri.p;
1763
1764 /*
1765 * Version ::= INTEGER { v1(0) }
1766 */
1767 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
1768 {
1769 x509_csr_free( csr );
1770 return( ret );
1771 }
1772
1773 csr->version++;
1774
1775 if( csr->version != 1 )
1776 {
1777 x509_csr_free( csr );
1778 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1779 }
1780
1781 /*
1782 * subject Name
1783 */
1784 csr->subject_raw.p = p;
1785
1786 if( ( ret = asn1_get_tag( &p, end, &len,
1787 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1788 {
1789 x509_csr_free( csr );
1790 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1791 }
1792
1793 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
1794 {
1795 x509_csr_free( csr );
1796 return( ret );
1797 }
1798
1799 csr->subject_raw.len = p - csr->subject_raw.p;
1800
1801 /*
1802 * subjectPKInfo SubjectPublicKeyInfo
1803 */
1804 if( ( ret = x509_get_pubkey( &p, end, &csr->pk ) ) != 0 )
1805 {
1806 x509_csr_free( csr );
1807 return( ret );
1808 }
1809
1810 /*
1811 * attributes [0] Attributes
1812 */
1813 if( ( ret = asn1_get_tag( &p, end, &len,
1814 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1815 {
1816 x509_csr_free( csr );
1817 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1818 }
1819 // TODO Parse Attributes / extension requests
1820
1821 p += len;
1822
1823 end = csr->raw.p + csr->raw.len;
1824
1825 /*
1826 * signatureAlgorithm AlgorithmIdentifier,
1827 * signature BIT STRING
1828 */
1829 if( ( ret = x509_get_alg_null( &p, end, &csr->sig_oid ) ) != 0 )
1830 {
1831 x509_csr_free( csr );
1832 return( ret );
1833 }
1834
1835 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &csr->sig_md,
1836 &csr->sig_pk ) ) != 0 )
1837 {
1838 x509_csr_free( csr );
1839 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1840 }
1841
1842 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
1843 {
1844 x509_csr_free( csr );
1845 return( ret );
1846 }
1847
1848 if( p != end )
1849 {
1850 x509_csr_free( csr );
1851 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1852 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1853 }
1854
1855 return( 0 );
1856}
1857
1858/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 * Parse one or more CRLs and add them to the chained list
1860 */
Paul Bakker23986e52011-04-24 08:57:21 +00001861int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001862{
Paul Bakker23986e52011-04-24 08:57:21 +00001863 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001864 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865 unsigned char *p, *end;
1866 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001867#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001868 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001869 pem_context pem;
1870#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001871
1872 crl = chain;
1873
1874 /*
1875 * Check for valid input
1876 */
1877 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001878 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001879
1880 while( crl->version != 0 && crl->next != NULL )
1881 crl = crl->next;
1882
1883 /*
1884 * Add new CRL on the end of the chain if needed.
1885 */
1886 if ( crl->version != 0 && crl->next == NULL)
1887 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001888 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001889
Paul Bakker7d06ad22009-05-02 15:53:56 +00001890 if( crl->next == NULL )
1891 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001893 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001894 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001895
Paul Bakker7d06ad22009-05-02 15:53:56 +00001896 crl = crl->next;
1897 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 }
1899
Paul Bakker96743fc2011-02-12 14:30:57 +00001900#if defined(POLARSSL_PEM_C)
1901 pem_init( &pem );
1902 ret = pem_read_buffer( &pem,
1903 "-----BEGIN X509 CRL-----",
1904 "-----END X509 CRL-----",
1905 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906
Paul Bakker96743fc2011-02-12 14:30:57 +00001907 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001909 /*
1910 * Was PEM encoded
1911 */
1912 buflen -= use_len;
1913 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914
1915 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001916 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001917 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001918 p = pem.buf;
1919 pem.buf = NULL;
1920 len = pem.buflen;
1921 pem_free( &pem );
1922 }
Paul Bakker00b28602013-06-24 13:02:41 +02001923 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001924 {
1925 pem_free( &pem );
1926 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001927 }
1928 else
1929 {
1930 /*
1931 * nope, copy the raw DER data
1932 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001933 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001934
1935 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001936 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001937
1938 memcpy( p, buf, buflen );
1939
1940 buflen = 0;
1941 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001942#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001943 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001944
1945 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001946 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001947
1948 memcpy( p, buf, buflen );
1949
1950 buflen = 0;
1951#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952
1953 crl->raw.p = p;
1954 crl->raw.len = len;
1955 end = p + len;
1956
1957 /*
1958 * CertificateList ::= SEQUENCE {
1959 * tbsCertList TBSCertList,
1960 * signatureAlgorithm AlgorithmIdentifier,
1961 * signatureValue BIT STRING }
1962 */
1963 if( ( ret = asn1_get_tag( &p, end, &len,
1964 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1965 {
1966 x509_crl_free( crl );
1967 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1968 }
1969
Paul Bakker23986e52011-04-24 08:57:21 +00001970 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001971 {
1972 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001973 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001974 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1975 }
1976
1977 /*
1978 * TBSCertList ::= SEQUENCE {
1979 */
1980 crl->tbs.p = p;
1981
1982 if( ( ret = asn1_get_tag( &p, end, &len,
1983 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1984 {
1985 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001986 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001987 }
1988
1989 end = p + len;
1990 crl->tbs.len = end - crl->tbs.p;
1991
1992 /*
1993 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1994 * -- if present, MUST be v2
1995 *
1996 * signature AlgorithmIdentifier
1997 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001998 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001999 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002000 {
2001 x509_crl_free( crl );
2002 return( ret );
2003 }
2004
2005 crl->version++;
2006
2007 if( crl->version > 2 )
2008 {
2009 x509_crl_free( crl );
2010 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
2011 }
2012
Paul Bakkerc70b9822013-04-07 22:00:46 +02002013 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
2014 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002015 {
2016 x509_crl_free( crl );
2017 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
2018 }
2019
2020 /*
2021 * issuer Name
2022 */
2023 crl->issuer_raw.p = p;
2024
2025 if( ( ret = asn1_get_tag( &p, end, &len,
2026 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2027 {
2028 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002029 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002030 }
2031
2032 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
2033 {
2034 x509_crl_free( crl );
2035 return( ret );
2036 }
2037
2038 crl->issuer_raw.len = p - crl->issuer_raw.p;
2039
2040 /*
2041 * thisUpdate Time
2042 * nextUpdate Time OPTIONAL
2043 */
Paul Bakker91200182010-02-18 21:26:15 +00002044 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002045 {
2046 x509_crl_free( crl );
2047 return( ret );
2048 }
2049
Paul Bakker91200182010-02-18 21:26:15 +00002050 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002051 {
Paul Bakker9d781402011-05-09 16:17:09 +00002052 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00002053 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00002054 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00002055 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00002056 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002057 x509_crl_free( crl );
2058 return( ret );
2059 }
2060 }
2061
2062 /*
2063 * revokedCertificates SEQUENCE OF SEQUENCE {
2064 * userCertificate CertificateSerialNumber,
2065 * revocationDate Time,
2066 * crlEntryExtensions Extensions OPTIONAL
2067 * -- if present, MUST be v2
2068 * } OPTIONAL
2069 */
2070 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
2071 {
2072 x509_crl_free( crl );
2073 return( ret );
2074 }
2075
2076 /*
2077 * crlExtensions EXPLICIT Extensions OPTIONAL
2078 * -- if present, MUST be v2
2079 */
2080 if( crl->version == 2 )
2081 {
2082 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
2083
2084 if( ret != 0 )
2085 {
2086 x509_crl_free( crl );
2087 return( ret );
2088 }
2089 }
2090
2091 if( p != end )
2092 {
2093 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002094 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00002095 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2096 }
2097
2098 end = crl->raw.p + crl->raw.len;
2099
2100 /*
2101 * signatureAlgorithm AlgorithmIdentifier,
2102 * signatureValue BIT STRING
2103 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002104 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002105 {
2106 x509_crl_free( crl );
2107 return( ret );
2108 }
2109
Paul Bakker535e97d2012-08-23 10:49:55 +00002110 if( crl->sig_oid1.len != crl->sig_oid2.len ||
2111 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002112 {
2113 x509_crl_free( crl );
2114 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
2115 }
2116
2117 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
2118 {
2119 x509_crl_free( crl );
2120 return( ret );
2121 }
2122
2123 if( p != end )
2124 {
2125 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002126 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00002127 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2128 }
2129
2130 if( buflen > 0 )
2131 {
Paul Bakker6e339b52013-07-03 13:37:05 +02002132 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002133
Paul Bakker7d06ad22009-05-02 15:53:56 +00002134 if( crl->next == NULL )
2135 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002136 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00002137 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00002138 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002139
Paul Bakker7d06ad22009-05-02 15:53:56 +00002140 crl = crl->next;
2141 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002142
2143 return( x509parse_crl( crl, buf, buflen ) );
2144 }
2145
2146 return( 0 );
2147}
2148
Paul Bakker335db3f2011-04-25 15:28:35 +00002149#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002150/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00002151 * Load all data from a file into a given buffer.
2152 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002153static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00002154{
Paul Bakkerd98030e2009-05-02 15:13:40 +00002155 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002156 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00002157
Paul Bakkerd98030e2009-05-02 15:13:40 +00002158 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00002159 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002160
Paul Bakkerd98030e2009-05-02 15:13:40 +00002161 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002162 if( ( size = ftell( f ) ) == -1 )
2163 {
2164 fclose( f );
2165 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2166 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002167 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002168
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002169 *n = (size_t) size;
2170
Paul Bakker694d3ae2013-08-19 14:23:38 +02002171 if( *n + 1 == 0 ||
2172 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002173 {
2174 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00002175 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002176 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002177
Paul Bakkerd98030e2009-05-02 15:13:40 +00002178 if( fread( *buf, 1, *n, f ) != *n )
2179 {
2180 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02002181 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00002182 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002183 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002184
Paul Bakkerd98030e2009-05-02 15:13:40 +00002185 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002186
Paul Bakkerd98030e2009-05-02 15:13:40 +00002187 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00002188
Paul Bakkerd98030e2009-05-02 15:13:40 +00002189 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002190}
2191
2192/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 * Load one or more certificates and add them to the chained list
2194 */
Paul Bakker69e095c2011-12-10 21:55:01 +00002195int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00002196{
2197 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 size_t n;
2199 unsigned char *buf;
2200
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002201 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002202 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203
Paul Bakker69e095c2011-12-10 21:55:01 +00002204 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
2206 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002207 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
2209 return( ret );
2210}
2211
Paul Bakker8d914582012-06-04 12:46:42 +00002212int x509parse_crtpath( x509_cert *chain, const char *path )
2213{
2214 int ret = 0;
2215#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002216 int w_ret;
2217 WCHAR szDir[MAX_PATH];
2218 char filename[MAX_PATH];
2219 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002220 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002221
Paul Bakker97872ac2012-11-02 12:53:26 +00002222 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002223 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002224
2225 if( len > MAX_PATH - 3 )
2226 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002227
Paul Bakker3338b792012-10-01 21:13:10 +00002228 memset( szDir, 0, sizeof(szDir) );
2229 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002230 memcpy( filename, path, len );
2231 filename[len++] = '\\';
2232 p = filename + len;
2233 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002234
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002235 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002236
Paul Bakker97872ac2012-11-02 12:53:26 +00002237 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002238 if (hFind == INVALID_HANDLE_VALUE)
2239 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2240
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002241 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002242 do
2243 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002244 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002245
Paul Bakkere4791f32012-06-04 21:29:15 +00002246 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002247 continue;
2248
Paul Bakker3338b792012-10-01 21:13:10 +00002249 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2250 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002251 p, len - 1,
2252 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002253
Paul Bakker3338b792012-10-01 21:13:10 +00002254 w_ret = x509parse_crtfile( chain, filename );
2255 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002256 ret++;
2257 else
2258 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002259 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002260 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002261
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002262 if (GetLastError() != ERROR_NO_MORE_FILES)
2263 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002264
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002265cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002266 FindClose( hFind );
2267#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002268 int t_ret, i;
2269 struct stat sb;
2270 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002271 char entry_name[255];
2272 DIR *dir = opendir( path );
2273
2274 if( dir == NULL)
2275 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2276
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002277 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002278 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002279 if( result == NULL )
2280 break;
2281
2282 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2283
2284 i = stat( entry_name, &sb );
2285
2286 if( i == -1 )
2287 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2288
2289 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002290 continue;
2291
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002292 // Ignore parse errors
2293 //
Paul Bakker8d914582012-06-04 12:46:42 +00002294 t_ret = x509parse_crtfile( chain, entry_name );
2295 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002296 ret++;
2297 else
2298 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002299 }
2300 closedir( dir );
2301#endif
2302
2303 return( ret );
2304}
2305
Paul Bakkerd98030e2009-05-02 15:13:40 +00002306/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002307 * Load a CSR into the structure
2308 */
2309int x509parse_csrfile( x509_csr *csr, const char *path )
2310{
2311 int ret;
2312 size_t n;
2313 unsigned char *buf;
2314
2315 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2316 return( ret );
2317
2318 ret = x509parse_csr( csr, buf, n );
2319
2320 memset( buf, 0, n + 1 );
2321 polarssl_free( buf );
2322
2323 return( ret );
2324}
2325
2326/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002327 * Load one or more CRLs and add them to the chained list
2328 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002329int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002330{
2331 int ret;
2332 size_t n;
2333 unsigned char *buf;
2334
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002335 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002336 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002337
Paul Bakker27fdf462011-06-09 13:55:13 +00002338 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002339
2340 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002341 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002342
2343 return( ret );
2344}
2345
Paul Bakker5121ce52009-01-03 21:22:43 +00002346/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002347 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002348 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002349int x509parse_keyfile( pk_context *ctx,
2350 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002351{
2352 int ret;
2353 size_t n;
2354 unsigned char *buf;
2355
2356 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2357 return( ret );
2358
2359 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002360 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002361 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002362 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002363 (const unsigned char *) pwd, strlen( pwd ) );
2364
2365 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002366 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002367
2368 return( ret );
2369}
2370
2371/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002372 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002373 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002374int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002375{
2376 int ret;
2377 size_t n;
2378 unsigned char *buf;
2379
2380 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2381 return( ret );
2382
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002383 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002384
2385 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002386 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002387
2388 return( ret );
2389}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002390
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002391#if defined(POLARSSL_RSA_C)
2392/*
2393 * Load and parse a private RSA key
2394 */
2395int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2396{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002397 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002398 pk_context pk;
2399
2400 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002401
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002402 ret = x509parse_keyfile( &pk, path, pwd );
2403
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002404 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2405 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2406
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002407 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002408 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002409 else
2410 rsa_free( rsa );
2411
2412 pk_free( &pk );
2413
2414 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002415}
2416
2417/*
2418 * Load and parse a public RSA key
2419 */
2420int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2421{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002422 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002423 pk_context pk;
2424
2425 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002426
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002427 ret = x509parse_public_keyfile( &pk, path );
2428
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002429 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2430 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2431
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002432 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002433 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002434 else
2435 rsa_free( rsa );
2436
2437 pk_free( &pk );
2438
2439 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002440}
2441#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002442#endif /* POLARSSL_FS_IO */
2443
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002444#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002445/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002446 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002448static int x509parse_key_pkcs1_der( rsa_context *rsa,
2449 const unsigned char *key,
2450 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451{
Paul Bakker23986e52011-04-24 08:57:21 +00002452 int ret;
2453 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002455
Paul Bakker96743fc2011-02-12 14:30:57 +00002456 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002457 end = p + keylen;
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002460 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002461 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 * RSAPrivateKey ::= SEQUENCE {
2463 * version Version,
2464 * modulus INTEGER, -- n
2465 * publicExponent INTEGER, -- e
2466 * privateExponent INTEGER, -- d
2467 * prime1 INTEGER, -- p
2468 * prime2 INTEGER, -- q
2469 * exponent1 INTEGER, -- d mod (p-1)
2470 * exponent2 INTEGER, -- d mod (q-1)
2471 * coefficient INTEGER, -- (inverse of q) mod p
2472 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2473 * }
2474 */
2475 if( ( ret = asn1_get_tag( &p, end, &len,
2476 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2477 {
Paul Bakker9d781402011-05-09 16:17:09 +00002478 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
2480
2481 end = p + len;
2482
2483 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2484 {
Paul Bakker9d781402011-05-09 16:17:09 +00002485 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
2487
2488 if( rsa->ver != 0 )
2489 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002490 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 }
2492
2493 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2494 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2495 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2496 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2497 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2498 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2499 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2500 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2501 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002503 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505
2506 rsa->len = mpi_size( &rsa->N );
2507
2508 if( p != end )
2509 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002511 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002512 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 }
2514
2515 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2516 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 rsa_free( rsa );
2518 return( ret );
2519 }
2520
Paul Bakkere2f50402013-06-24 19:00:59 +02002521 return( 0 );
2522}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002523#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002524
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002525#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002526/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002527 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002528 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002529static int x509parse_key_sec1_der( ecp_keypair *eck,
2530 const unsigned char *key,
2531 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002532{
2533 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002534 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002535 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002536 x509_buf params;
2537 unsigned char *p = (unsigned char *) key;
2538 unsigned char *end = p + keylen;
2539 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002540
2541 /*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002542 * RFC 5915, orf SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002543 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002544 * ECPrivateKey ::= SEQUENCE {
2545 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2546 * privateKey OCTET STRING,
2547 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2548 * publicKey [1] BIT STRING OPTIONAL
2549 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002550 */
2551 if( ( ret = asn1_get_tag( &p, end, &len,
2552 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2553 {
2554 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2555 }
2556
2557 end = p + len;
2558
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002559 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002560 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2561
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002562 if( version != 1 )
2563 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002564
Paul Bakkere2f50402013-06-24 19:00:59 +02002565 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2566 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2567
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002568 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002569 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002570 ecp_keypair_free( eck );
2571 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2572 }
2573
2574 p += len;
2575
2576 /*
2577 * Is 'parameters' present?
2578 */
2579 if( ( ret = asn1_get_tag( &p, end, &len,
2580 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2581 {
2582 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2583 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2584 {
2585 ecp_keypair_free( eck );
2586 return( ret );
2587 }
2588 }
2589 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2590 {
2591 ecp_keypair_free( eck );
2592 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2593 }
2594
2595 /*
2596 * Is 'publickey' present?
2597 */
2598 if( ( ret = asn1_get_tag( &p, end, &len,
2599 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2600 {
2601 end2 = p + len;
2602
2603 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2604 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2605
2606 if( p + len != end2 )
2607 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2608 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2609
2610 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2611 return( ret );
2612 }
2613 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2614 {
2615 ecp_keypair_free( eck );
2616 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2617 }
2618
2619 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2620 {
2621 ecp_keypair_free( eck );
2622 return( ret );
2623 }
2624
2625 return 0;
2626}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002627#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002628
2629/*
2630 * Parse an unencrypted PKCS#8 encoded private key
2631 */
2632static int x509parse_key_pkcs8_unencrypted_der(
2633 pk_context *pk,
2634 const unsigned char* key,
2635 size_t keylen )
2636{
2637 int ret, version;
2638 size_t len;
2639 x509_buf params;
2640 unsigned char *p = (unsigned char *) key;
2641 unsigned char *end = p + keylen;
2642 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002643 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002644
2645 /*
2646 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2647 *
2648 * PrivateKeyInfo ::= SEQUENCE {
2649 * version Version,
2650 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2651 * privateKey PrivateKey,
2652 * attributes [0] IMPLICIT Attributes OPTIONAL }
2653 *
2654 * Version ::= INTEGER
2655 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2656 * PrivateKey ::= OCTET STRING
2657 *
2658 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2659 */
2660
2661 if( ( ret = asn1_get_tag( &p, end, &len,
2662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2663 {
2664 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002665 }
2666
2667 end = p + len;
2668
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002669 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2670 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2671
2672 if( version != 0 )
2673 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2674
2675 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2676 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2677
2678 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2679 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2680
2681 if( len < 1 )
2682 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2683 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2684
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002685 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2686 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2687
2688 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002689 return( ret );
2690
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002691#if defined(POLARSSL_RSA_C)
2692 if( pk_alg == POLARSSL_PK_RSA )
2693 {
2694 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2695 {
2696 pk_free( pk );
2697 return( ret );
2698 }
2699 } else
2700#endif /* POLARSSL_RSA_C */
2701#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002702 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2703 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002704 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2705 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2706 {
2707 pk_free( pk );
2708 return( ret );
2709 }
2710 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002711#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002712 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2713
2714 return 0;
2715}
2716
2717/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002718 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002719 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002720static int x509parse_key_pkcs8_encrypted_der(
2721 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002722 const unsigned char *key, size_t keylen,
2723 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002724{
2725 int ret;
2726 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002727 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002728 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002729 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002730#if defined(POLARSSL_PKCS12_C)
2731 cipher_type_t cipher_alg;
2732 md_type_t md_alg;
2733#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002734
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002735 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002736
2737 p = (unsigned char *) key;
2738 end = p + keylen;
2739
Paul Bakker28144de2013-06-24 19:28:55 +02002740 if( pwdlen == 0 )
2741 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2742
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002743 /*
2744 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2745 *
2746 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2747 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2748 * encryptedData EncryptedData
2749 * }
2750 *
2751 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2752 *
2753 * EncryptedData ::= OCTET STRING
2754 *
2755 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2756 */
2757 if( ( ret = asn1_get_tag( &p, end, &len,
2758 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2759 {
2760 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2761 }
2762
2763 end = p + len;
2764
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002765 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002766 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002767
2768 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2769 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2770
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002771 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002772 return( POLARSSL_ERR_X509_INVALID_INPUT );
2773
2774 /*
2775 * Decrypt EncryptedData with appropriate PDE
2776 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002777#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002778 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002779 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002780 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002781 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002782 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002783 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002784 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2785 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2786
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002787 return( ret );
2788 }
2789 }
2790 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2791 {
2792 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2793 PKCS12_PBE_DECRYPT,
2794 pwd, pwdlen,
2795 p, len, buf ) ) != 0 )
2796 {
2797 return( ret );
2798 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002799
2800 // Best guess for password mismatch when using RC4. If first tag is
2801 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2802 //
2803 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2804 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002805 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002806 else
2807#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002808#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002809 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002810 {
2811 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2812 p, len, buf ) ) != 0 )
2813 {
2814 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2815 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2816
2817 return( ret );
2818 }
2819 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002820 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002821#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002822 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2823
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002824 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002825}
2826
2827/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002828 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002829 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002830int x509parse_key( pk_context *pk,
2831 const unsigned char *key, size_t keylen,
2832 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002833{
2834 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002835 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002836
2837#if defined(POLARSSL_PEM_C)
2838 size_t len;
2839 pem_context pem;
2840
2841 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002842
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002843#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002844 ret = pem_read_buffer( &pem,
2845 "-----BEGIN RSA PRIVATE KEY-----",
2846 "-----END RSA PRIVATE KEY-----",
2847 key, pwd, pwdlen, &len );
2848 if( ret == 0 )
2849 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002850 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2851 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2852
2853 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002854 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2855 pem.buf, pem.buflen ) ) != 0 )
2856 {
2857 pk_free( pk );
2858 }
2859
2860 pem_free( &pem );
2861 return( ret );
2862 }
2863 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2864 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2865 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2866 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2867 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2868 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002869#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002870
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002871#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002872 ret = pem_read_buffer( &pem,
2873 "-----BEGIN EC PRIVATE KEY-----",
2874 "-----END EC PRIVATE KEY-----",
2875 key, pwd, pwdlen, &len );
2876 if( ret == 0 )
2877 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002878 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2879 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2880
2881 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002882 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2883 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002884 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002885 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002886 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002887
2888 pem_free( &pem );
2889 return( ret );
2890 }
2891 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2892 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2893 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2894 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2895 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2896 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002897#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002898
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002899 ret = pem_read_buffer( &pem,
2900 "-----BEGIN PRIVATE KEY-----",
2901 "-----END PRIVATE KEY-----",
2902 key, NULL, 0, &len );
2903 if( ret == 0 )
2904 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002905 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002906 pem.buf, pem.buflen ) ) != 0 )
2907 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002908 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002909 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002910
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002911 pem_free( &pem );
2912 return( ret );
2913 }
2914 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2915 return( ret );
2916
2917 ret = pem_read_buffer( &pem,
2918 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2919 "-----END ENCRYPTED PRIVATE KEY-----",
2920 key, NULL, 0, &len );
2921 if( ret == 0 )
2922 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002923 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002924 pem.buf, pem.buflen,
2925 pwd, pwdlen ) ) != 0 )
2926 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002927 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002928 }
2929
2930 pem_free( &pem );
2931 return( ret );
2932 }
2933 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2934 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002935#else
2936 ((void) pwd);
2937 ((void) pwdlen);
2938#endif /* POLARSSL_PEM_C */
2939
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002940 /*
2941 * At this point we only know it's not a PEM formatted key. Could be any
2942 * of the known DER encoded private key formats
2943 *
2944 * We try the different DER format parsers to see if one passes without
2945 * error
2946 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002947 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2948 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002949 {
2950 return( 0 );
2951 }
2952
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002953 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002954
2955 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2956 {
2957 return( ret );
2958 }
2959
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002960 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002961 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002962
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002963 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002964
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002965#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002966 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2967 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2968
2969 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002970 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2971 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002972 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002973 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002974
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002975 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002976#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002977
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002978#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002979 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2980 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2981
2982 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002983 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2984 {
2985 return( 0 );
2986 }
2987
2988 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002989#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002990
2991 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2992}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002993
2994/*
2995 * Parse a public key
2996 */
2997int x509parse_public_key( pk_context *ctx,
2998 const unsigned char *key, size_t keylen )
2999{
3000 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003001 unsigned char *p;
3002#if defined(POLARSSL_PEM_C)
3003 size_t len;
3004 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003005
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003006 pem_init( &pem );
3007 ret = pem_read_buffer( &pem,
3008 "-----BEGIN PUBLIC KEY-----",
3009 "-----END PUBLIC KEY-----",
3010 key, NULL, 0, &len );
3011
3012 if( ret == 0 )
3013 {
3014 /*
3015 * Was PEM encoded
3016 */
3017 key = pem.buf;
3018 keylen = pem.buflen;
3019 }
3020 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3021 {
3022 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003023 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003024 }
3025#endif
3026 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003027
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003028 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003029
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003030#if defined(POLARSSL_PEM_C)
3031 pem_free( &pem );
3032#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003033
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003034 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003035}
3036
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003037#if defined(POLARSSL_RSA_C)
3038/*
3039 * Parse a private RSA key
3040 */
3041int x509parse_key_rsa( rsa_context *rsa,
3042 const unsigned char *key, size_t keylen,
3043 const unsigned char *pwd, size_t pwdlen )
3044{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003045 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003046 pk_context pk;
3047
3048 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003049
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003050 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
3051
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003052 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3053 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3054
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003055 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003056 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003057 else
3058 rsa_free( rsa );
3059
3060 pk_free( &pk );
3061
3062 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003063}
3064
3065/*
3066 * Parse a public RSA key
3067 */
3068int x509parse_public_key_rsa( rsa_context *rsa,
3069 const unsigned char *key, size_t keylen )
3070{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003071 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003072 pk_context pk;
3073
3074 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003075
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003076 ret = x509parse_public_key( &pk, key, keylen );
3077
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003078 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3079 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3080
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003081 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003082 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003083 else
3084 rsa_free( rsa );
3085
3086 pk_free( &pk );
3087
3088 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003089}
3090#endif /* POLARSSL_RSA_C */
3091
Paul Bakkereaa89f82011-04-04 21:36:15 +00003092#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003093/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003094 * Parse DHM parameters
3095 */
Paul Bakker23986e52011-04-24 08:57:21 +00003096int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003097{
Paul Bakker23986e52011-04-24 08:57:21 +00003098 int ret;
3099 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003100 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003101#if defined(POLARSSL_PEM_C)
3102 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003103
Paul Bakker96743fc2011-02-12 14:30:57 +00003104 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003105
Paul Bakker96743fc2011-02-12 14:30:57 +00003106 ret = pem_read_buffer( &pem,
3107 "-----BEGIN DH PARAMETERS-----",
3108 "-----END DH PARAMETERS-----",
3109 dhmin, NULL, 0, &dhminlen );
3110
3111 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003112 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003113 /*
3114 * Was PEM encoded
3115 */
3116 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003117 }
Paul Bakker00b28602013-06-24 13:02:41 +02003118 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003119 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003120 pem_free( &pem );
3121 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003122 }
3123
Paul Bakker96743fc2011-02-12 14:30:57 +00003124 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3125#else
3126 p = (unsigned char *) dhmin;
3127#endif
3128 end = p + dhminlen;
3129
Paul Bakker1b57b062011-01-06 15:48:19 +00003130 memset( dhm, 0, sizeof( dhm_context ) );
3131
Paul Bakker1b57b062011-01-06 15:48:19 +00003132 /*
3133 * DHParams ::= SEQUENCE {
3134 * prime INTEGER, -- P
3135 * generator INTEGER, -- g
3136 * }
3137 */
3138 if( ( ret = asn1_get_tag( &p, end, &len,
3139 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3140 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003141#if defined(POLARSSL_PEM_C)
3142 pem_free( &pem );
3143#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003144 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003145 }
3146
3147 end = p + len;
3148
3149 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3150 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3151 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003152#if defined(POLARSSL_PEM_C)
3153 pem_free( &pem );
3154#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003155 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003156 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003157 }
3158
3159 if( p != end )
3160 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003161#if defined(POLARSSL_PEM_C)
3162 pem_free( &pem );
3163#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003164 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003165 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003166 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3167 }
3168
Paul Bakker96743fc2011-02-12 14:30:57 +00003169#if defined(POLARSSL_PEM_C)
3170 pem_free( &pem );
3171#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003172
3173 return( 0 );
3174}
3175
Paul Bakker335db3f2011-04-25 15:28:35 +00003176#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003177/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003178 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003179 */
3180int x509parse_dhmfile( dhm_context *dhm, const char *path )
3181{
3182 int ret;
3183 size_t n;
3184 unsigned char *buf;
3185
Paul Bakker69e095c2011-12-10 21:55:01 +00003186 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3187 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003188
Paul Bakker27fdf462011-06-09 13:55:13 +00003189 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003190
3191 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003192 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003193
3194 return( ret );
3195}
Paul Bakker335db3f2011-04-25 15:28:35 +00003196#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003197#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003198
Paul Bakker5121ce52009-01-03 21:22:43 +00003199#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003200#include <stdarg.h>
3201
3202#if !defined vsnprintf
3203#define vsnprintf _vsnprintf
3204#endif // vsnprintf
3205
3206/*
3207 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3208 * Result value is not size of buffer needed, but -1 if no fit is possible.
3209 *
3210 * This fuction tries to 'fix' this by at least suggesting enlarging the
3211 * size by 20.
3212 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003213static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003214{
3215 va_list ap;
3216 int res = -1;
3217
3218 va_start( ap, format );
3219
3220 res = vsnprintf( str, size, format, ap );
3221
3222 va_end( ap );
3223
3224 // No quick fix possible
3225 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003226 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003227
3228 return res;
3229}
3230
3231#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003232#endif
3233
Paul Bakkerd98030e2009-05-02 15:13:40 +00003234#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3235
3236#define SAFE_SNPRINTF() \
3237{ \
3238 if( ret == -1 ) \
3239 return( -1 ); \
3240 \
Paul Bakker23986e52011-04-24 08:57:21 +00003241 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003242 p[n - 1] = '\0'; \
3243 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3244 } \
3245 \
Paul Bakker23986e52011-04-24 08:57:21 +00003246 n -= (unsigned int) ret; \
3247 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003248}
3249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250/*
3251 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003252 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003254int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255{
Paul Bakker23986e52011-04-24 08:57:21 +00003256 int ret;
3257 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003259 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003260 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 char s[128], *p;
3262
3263 memset( s, 0, sizeof( s ) );
3264
3265 name = dn;
3266 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003267 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
3269 while( name != NULL )
3270 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003271 if( !name->oid.p )
3272 {
3273 name = name->next;
3274 continue;
3275 }
3276
Paul Bakker74111d32011-01-15 16:57:55 +00003277 if( name != dn )
3278 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003279 ret = snprintf( p, n, ", " );
3280 SAFE_SNPRINTF();
3281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Paul Bakkerc70b9822013-04-07 22:00:46 +02003283 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
Paul Bakkerc70b9822013-04-07 22:00:46 +02003285 if( ret == 0 )
3286 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003288 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003289 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
3291 for( i = 0; i < name->val.len; i++ )
3292 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003293 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 break;
3295
3296 c = name->val.p[i];
3297 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3298 s[i] = '?';
3299 else s[i] = c;
3300 }
3301 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003302 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003303 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 name = name->next;
3305 }
3306
Paul Bakker23986e52011-04-24 08:57:21 +00003307 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003308}
3309
3310/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003311 * Store the serial in printable form into buf; no more
3312 * than size characters will be written
3313 */
3314int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3315{
Paul Bakker23986e52011-04-24 08:57:21 +00003316 int ret;
3317 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003318 char *p;
3319
3320 p = buf;
3321 n = size;
3322
3323 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003324 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003325
3326 for( i = 0; i < nr; i++ )
3327 {
Paul Bakker93048802011-12-05 14:38:06 +00003328 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003329 continue;
3330
Paul Bakkerdd476992011-01-16 21:34:59 +00003331 ret = snprintf( p, n, "%02X%s",
3332 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3333 SAFE_SNPRINTF();
3334 }
3335
Paul Bakker03c7c252011-11-25 12:37:37 +00003336 if( nr != serial->len )
3337 {
3338 ret = snprintf( p, n, "...." );
3339 SAFE_SNPRINTF();
3340 }
3341
Paul Bakker23986e52011-04-24 08:57:21 +00003342 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003343}
3344
3345/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003346 * Helper for writing "RSA key size", "EC key size", etc
3347 */
3348static int x509_key_size_helper( char *buf, size_t size, const char *name )
3349{
3350 char *p = buf;
3351 size_t n = size;
3352 int ret;
3353
3354 if( strlen( name ) + sizeof( " key size" ) > size )
3355 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3356
3357 ret = snprintf( p, n, "%s key size", name );
3358 SAFE_SNPRINTF();
3359
3360 return( 0 );
3361}
3362
3363/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003364 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003366#define BEFORE_COLON 14
3367#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003368int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3369 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003370{
Paul Bakker23986e52011-04-24 08:57:21 +00003371 int ret;
3372 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003373 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003374 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003375 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
3377 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003378 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Paul Bakkerd98030e2009-05-02 15:13:40 +00003380 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003382 SAFE_SNPRINTF();
3383 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003385 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003386
Paul Bakkerdd476992011-01-16 21:34:59 +00003387 ret = x509parse_serial_gets( p, n, &crt->serial);
3388 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003389
Paul Bakkerd98030e2009-05-02 15:13:40 +00003390 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3391 SAFE_SNPRINTF();
3392 ret = x509parse_dn_gets( p, n, &crt->issuer );
3393 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003394
Paul Bakkerd98030e2009-05-02 15:13:40 +00003395 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3396 SAFE_SNPRINTF();
3397 ret = x509parse_dn_gets( p, n, &crt->subject );
3398 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
Paul Bakkerd98030e2009-05-02 15:13:40 +00003400 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3402 crt->valid_from.year, crt->valid_from.mon,
3403 crt->valid_from.day, crt->valid_from.hour,
3404 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003405 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003406
Paul Bakkerd98030e2009-05-02 15:13:40 +00003407 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3409 crt->valid_to.year, crt->valid_to.mon,
3410 crt->valid_to.day, crt->valid_to.hour,
3411 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003412 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003413
Paul Bakkerc70b9822013-04-07 22:00:46 +02003414 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003415 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003416
Paul Bakkerc70b9822013-04-07 22:00:46 +02003417 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3418 if( ret != 0 )
3419 ret = snprintf( p, n, "???" );
3420 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003421 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003422 SAFE_SNPRINTF();
3423
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003424 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003425 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003426 {
3427 return( ret );
3428 }
3429
3430 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003431 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003432 SAFE_SNPRINTF();
3433
Paul Bakker23986e52011-04-24 08:57:21 +00003434 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003435}
3436
Paul Bakker74111d32011-01-15 16:57:55 +00003437/*
3438 * Return an informational string describing the given OID
3439 */
3440const char *x509_oid_get_description( x509_buf *oid )
3441{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003442 const char *desc = NULL;
3443 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003444
Paul Bakkerc70b9822013-04-07 22:00:46 +02003445 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003446
Paul Bakkerc70b9822013-04-07 22:00:46 +02003447 if( ret != 0 )
3448 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003449
Paul Bakkerc70b9822013-04-07 22:00:46 +02003450 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003451}
3452
3453/* Return the x.y.z.... style numeric string for the given OID */
3454int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3455{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003456 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003457}
3458
Paul Bakkerd98030e2009-05-02 15:13:40 +00003459/*
3460 * Return an informational string about the CRL.
3461 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003462int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3463 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003464{
Paul Bakker23986e52011-04-24 08:57:21 +00003465 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003466 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003467 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003468 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003469 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003470
3471 p = buf;
3472 n = size;
3473
3474 ret = snprintf( p, n, "%sCRL version : %d",
3475 prefix, crl->version );
3476 SAFE_SNPRINTF();
3477
3478 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3479 SAFE_SNPRINTF();
3480 ret = x509parse_dn_gets( p, n, &crl->issuer );
3481 SAFE_SNPRINTF();
3482
3483 ret = snprintf( p, n, "\n%sthis update : " \
3484 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3485 crl->this_update.year, crl->this_update.mon,
3486 crl->this_update.day, crl->this_update.hour,
3487 crl->this_update.min, crl->this_update.sec );
3488 SAFE_SNPRINTF();
3489
3490 ret = snprintf( p, n, "\n%snext update : " \
3491 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3492 crl->next_update.year, crl->next_update.mon,
3493 crl->next_update.day, crl->next_update.hour,
3494 crl->next_update.min, crl->next_update.sec );
3495 SAFE_SNPRINTF();
3496
3497 entry = &crl->entry;
3498
3499 ret = snprintf( p, n, "\n%sRevoked certificates:",
3500 prefix );
3501 SAFE_SNPRINTF();
3502
Paul Bakker9be19372009-07-27 20:21:53 +00003503 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003504 {
3505 ret = snprintf( p, n, "\n%sserial number: ",
3506 prefix );
3507 SAFE_SNPRINTF();
3508
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003509 ret = x509parse_serial_gets( p, n, &entry->serial);
3510 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003511
Paul Bakkerd98030e2009-05-02 15:13:40 +00003512 ret = snprintf( p, n, " revocation date: " \
3513 "%04d-%02d-%02d %02d:%02d:%02d",
3514 entry->revocation_date.year, entry->revocation_date.mon,
3515 entry->revocation_date.day, entry->revocation_date.hour,
3516 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003517 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003518
3519 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 }
3521
Paul Bakkerc70b9822013-04-07 22:00:46 +02003522 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003523 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Paul Bakkerc70b9822013-04-07 22:00:46 +02003525 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3526 if( ret != 0 )
3527 ret = snprintf( p, n, "???" );
3528 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003529 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003530 SAFE_SNPRINTF();
3531
Paul Bakker1e27bb22009-07-19 20:25:25 +00003532 ret = snprintf( p, n, "\n" );
3533 SAFE_SNPRINTF();
3534
Paul Bakker23986e52011-04-24 08:57:21 +00003535 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003536}
3537
3538/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003539 * Return an informational string about the CSR.
3540 */
3541int x509parse_csr_info( char *buf, size_t size, const char *prefix,
3542 const x509_csr *csr )
3543{
3544 int ret;
3545 size_t n;
3546 char *p;
3547 const char *desc;
3548 char key_size_str[BEFORE_COLON];
3549
3550 p = buf;
3551 n = size;
3552
3553 ret = snprintf( p, n, "%sCSR version : %d",
3554 prefix, csr->version );
3555 SAFE_SNPRINTF();
3556
3557 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3558 SAFE_SNPRINTF();
3559 ret = x509parse_dn_gets( p, n, &csr->subject );
3560 SAFE_SNPRINTF();
3561
3562 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
3563 SAFE_SNPRINTF();
3564
3565 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
3566 if( ret != 0 )
3567 ret = snprintf( p, n, "???" );
3568 else
3569 ret = snprintf( p, n, "%s", desc );
3570 SAFE_SNPRINTF();
3571
3572 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
3573 pk_get_name( &csr->pk ) ) ) != 0 )
3574 {
3575 return( ret );
3576 }
3577
3578 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
3579 (int) pk_get_size( &csr->pk ) );
3580 SAFE_SNPRINTF();
3581
3582 return( (int) ( size - n ) );
3583}
3584
3585/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003586 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003588#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003589int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003590{
Paul Bakkercce9d772011-11-18 14:26:47 +00003591 int year, mon, day;
3592 int hour, min, sec;
3593
3594#if defined(_WIN32)
3595 SYSTEMTIME st;
3596
3597 GetLocalTime(&st);
3598
3599 year = st.wYear;
3600 mon = st.wMonth;
3601 day = st.wDay;
3602 hour = st.wHour;
3603 min = st.wMinute;
3604 sec = st.wSecond;
3605#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 struct tm *lt;
3607 time_t tt;
3608
3609 tt = time( NULL );
3610 lt = localtime( &tt );
3611
Paul Bakkercce9d772011-11-18 14:26:47 +00003612 year = lt->tm_year + 1900;
3613 mon = lt->tm_mon + 1;
3614 day = lt->tm_mday;
3615 hour = lt->tm_hour;
3616 min = lt->tm_min;
3617 sec = lt->tm_sec;
3618#endif
3619
3620 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003621 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003622
Paul Bakkercce9d772011-11-18 14:26:47 +00003623 if( year == to->year &&
3624 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003625 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Paul Bakkercce9d772011-11-18 14:26:47 +00003627 if( year == to->year &&
3628 mon == to->mon &&
3629 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003630 return( 1 );
3631
Paul Bakkercce9d772011-11-18 14:26:47 +00003632 if( year == to->year &&
3633 mon == to->mon &&
3634 day == to->day &&
3635 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003636 return( 1 );
3637
Paul Bakkercce9d772011-11-18 14:26:47 +00003638 if( year == to->year &&
3639 mon == to->mon &&
3640 day == to->day &&
3641 hour == to->hour &&
3642 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003643 return( 1 );
3644
Paul Bakkercce9d772011-11-18 14:26:47 +00003645 if( year == to->year &&
3646 mon == to->mon &&
3647 day == to->day &&
3648 hour == to->hour &&
3649 min == to->min &&
3650 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003651 return( 1 );
3652
Paul Bakker40ea7de2009-05-03 10:18:48 +00003653 return( 0 );
3654}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003655#else /* POLARSSL_HAVE_TIME */
3656int x509parse_time_expired( const x509_time *to )
3657{
3658 ((void) to);
3659 return( 0 );
3660}
3661#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003662
3663/*
3664 * Return 1 if the certificate is revoked, or 0 otherwise.
3665 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003666int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003667{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003668 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003669
3670 while( cur != NULL && cur->serial.len != 0 )
3671 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003672 if( crt->serial.len == cur->serial.len &&
3673 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003674 {
3675 if( x509parse_time_expired( &cur->revocation_date ) )
3676 return( 1 );
3677 }
3678
3679 cur = cur->next;
3680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
3682 return( 0 );
3683}
3684
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003685/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003686 * Check that the given certificate is valid accoring to the CRL.
3687 */
3688static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3689 x509_crl *crl_list)
3690{
3691 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003692 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3693 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003694
Paul Bakker915275b2012-09-28 07:10:55 +00003695 if( ca == NULL )
3696 return( flags );
3697
Paul Bakker76fd75a2011-01-16 21:12:10 +00003698 /*
3699 * TODO: What happens if no CRL is present?
3700 * Suggestion: Revocation state should be unknown if no CRL is present.
3701 * For backwards compatibility this is not yet implemented.
3702 */
3703
Paul Bakker915275b2012-09-28 07:10:55 +00003704 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003705 {
Paul Bakker915275b2012-09-28 07:10:55 +00003706 if( crl_list->version == 0 ||
3707 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003708 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3709 crl_list->issuer_raw.len ) != 0 )
3710 {
3711 crl_list = crl_list->next;
3712 continue;
3713 }
3714
3715 /*
3716 * Check if CRL is correctly signed by the trusted CA
3717 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003718 md_info = md_info_from_type( crl_list->sig_md );
3719 if( md_info == NULL )
3720 {
3721 /*
3722 * Cannot check 'unknown' hash
3723 */
3724 flags |= BADCRL_NOT_TRUSTED;
3725 break;
3726 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003727
Paul Bakkerc70b9822013-04-07 22:00:46 +02003728 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003729
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003730 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003731 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003732 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003733 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003734 flags |= BADCRL_NOT_TRUSTED;
3735 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003736 }
3737
3738 /*
3739 * Check for validity of CRL (Do not drop out)
3740 */
3741 if( x509parse_time_expired( &crl_list->next_update ) )
3742 flags |= BADCRL_EXPIRED;
3743
3744 /*
3745 * Check if certificate is revoked
3746 */
3747 if( x509parse_revoked(crt, crl_list) )
3748 {
3749 flags |= BADCERT_REVOKED;
3750 break;
3751 }
3752
3753 crl_list = crl_list->next;
3754 }
3755 return flags;
3756}
3757
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003758static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003759{
3760 size_t i;
3761 size_t cn_idx = 0;
3762
Paul Bakker57b12982012-02-11 17:38:38 +00003763 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003764 return( 0 );
3765
3766 for( i = 0; i < strlen( cn ); ++i )
3767 {
3768 if( cn[i] == '.' )
3769 {
3770 cn_idx = i;
3771 break;
3772 }
3773 }
3774
3775 if( cn_idx == 0 )
3776 return( 0 );
3777
Paul Bakker535e97d2012-08-23 10:49:55 +00003778 if( strlen( cn ) - cn_idx == name->len - 1 &&
3779 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003780 {
3781 return( 1 );
3782 }
3783
3784 return( 0 );
3785}
3786
Paul Bakker915275b2012-09-28 07:10:55 +00003787static int x509parse_verify_top(
3788 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003789 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003790 int (*f_vrfy)(void *, x509_cert *, int, int *),
3791 void *p_vrfy )
3792{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003793 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003794 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003795 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3796 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003797
3798 if( x509parse_time_expired( &child->valid_to ) )
3799 *flags |= BADCERT_EXPIRED;
3800
3801 /*
3802 * Child is the top of the chain. Check against the trust_ca list.
3803 */
3804 *flags |= BADCERT_NOT_TRUSTED;
3805
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003806 md_info = md_info_from_type( child->sig_md );
3807 if( md_info == NULL )
3808 {
3809 /*
3810 * Cannot check 'unknown', no need to try any CA
3811 */
3812 trust_ca = NULL;
3813 }
3814 else
3815 md( md_info, child->tbs.p, child->tbs.len, hash );
3816
Paul Bakker915275b2012-09-28 07:10:55 +00003817 while( trust_ca != NULL )
3818 {
3819 if( trust_ca->version == 0 ||
3820 child->issuer_raw.len != trust_ca->subject_raw.len ||
3821 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3822 child->issuer_raw.len ) != 0 )
3823 {
3824 trust_ca = trust_ca->next;
3825 continue;
3826 }
3827
Paul Bakker9a736322012-11-14 12:39:52 +00003828 /*
3829 * Reduce path_len to check against if top of the chain is
3830 * the same as the trusted CA
3831 */
3832 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3833 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003834 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003835 {
3836 check_path_cnt--;
3837 }
3838
Paul Bakker915275b2012-09-28 07:10:55 +00003839 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003840 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003841 {
3842 trust_ca = trust_ca->next;
3843 continue;
3844 }
3845
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003846 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003847 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003848 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003849 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003850 trust_ca = trust_ca->next;
3851 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003852 }
3853
3854 /*
3855 * Top of chain is signed by a trusted CA
3856 */
3857 *flags &= ~BADCERT_NOT_TRUSTED;
3858 break;
3859 }
3860
Paul Bakker9a736322012-11-14 12:39:52 +00003861 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003862 * If top of chain is not the same as the trusted CA send a verify request
3863 * to the callback for any issues with validity and CRL presence for the
3864 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003865 */
3866 if( trust_ca != NULL &&
3867 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3868 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3869 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003870 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003871 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003872 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3873
3874 if( x509parse_time_expired( &trust_ca->valid_to ) )
3875 ca_flags |= BADCERT_EXPIRED;
3876
Paul Bakker915275b2012-09-28 07:10:55 +00003877 if( NULL != f_vrfy )
3878 {
Paul Bakker9a736322012-11-14 12:39:52 +00003879 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003880 return( ret );
3881 }
3882 }
3883
3884 /* Call callback on top cert */
3885 if( NULL != f_vrfy )
3886 {
Paul Bakker9a736322012-11-14 12:39:52 +00003887 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003888 return( ret );
3889 }
3890
Paul Bakker915275b2012-09-28 07:10:55 +00003891 *flags |= ca_flags;
3892
3893 return( 0 );
3894}
3895
3896static int x509parse_verify_child(
3897 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003898 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003899 int (*f_vrfy)(void *, x509_cert *, int, int *),
3900 void *p_vrfy )
3901{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003902 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003903 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003904 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003905 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003906 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003907
3908 if( x509parse_time_expired( &child->valid_to ) )
3909 *flags |= BADCERT_EXPIRED;
3910
Paul Bakkerc70b9822013-04-07 22:00:46 +02003911 md_info = md_info_from_type( child->sig_md );
3912 if( md_info == NULL )
3913 {
3914 /*
3915 * Cannot check 'unknown' hash
3916 */
Paul Bakker915275b2012-09-28 07:10:55 +00003917 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003918 }
3919 else
3920 {
3921 md( md_info, child->tbs.p, child->tbs.len, hash );
3922
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003923 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003924 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003925 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003926 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003927 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003928 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003929 }
3930
Paul Bakker915275b2012-09-28 07:10:55 +00003931 /* Check trusted CA's CRL for the given crt */
3932 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3933
3934 grandparent = parent->next;
3935
3936 while( grandparent != NULL )
3937 {
3938 if( grandparent->version == 0 ||
3939 grandparent->ca_istrue == 0 ||
3940 parent->issuer_raw.len != grandparent->subject_raw.len ||
3941 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3942 parent->issuer_raw.len ) != 0 )
3943 {
3944 grandparent = grandparent->next;
3945 continue;
3946 }
3947 break;
3948 }
3949
Paul Bakker915275b2012-09-28 07:10:55 +00003950 if( grandparent != NULL )
3951 {
3952 /*
3953 * Part of the chain
3954 */
Paul Bakker9a736322012-11-14 12:39:52 +00003955 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 +00003956 if( ret != 0 )
3957 return( ret );
3958 }
3959 else
3960 {
Paul Bakker9a736322012-11-14 12:39:52 +00003961 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 +00003962 if( ret != 0 )
3963 return( ret );
3964 }
3965
3966 /* child is verified to be a child of the parent, call verify callback */
3967 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003968 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003969 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003970
3971 *flags |= parent_flags;
3972
3973 return( 0 );
3974}
3975
Paul Bakker76fd75a2011-01-16 21:12:10 +00003976/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 * Verify the certificate validity
3978 */
3979int x509parse_verify( x509_cert *crt,
3980 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003981 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003982 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003983 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003984 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003985{
Paul Bakker23986e52011-04-24 08:57:21 +00003986 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003987 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003988 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003989 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003991 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003992
Paul Bakker40ea7de2009-05-03 10:18:48 +00003993 *flags = 0;
3994
Paul Bakker5121ce52009-01-03 21:22:43 +00003995 if( cn != NULL )
3996 {
3997 name = &crt->subject;
3998 cn_len = strlen( cn );
3999
Paul Bakker4d2c1242012-05-10 14:12:46 +00004000 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00004001 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00004002 cur = &crt->subject_alt_names;
4003
4004 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004005 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004006 if( cur->buf.len == cn_len &&
4007 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004008 break;
4009
Paul Bakker535e97d2012-08-23 10:49:55 +00004010 if( cur->buf.len > 2 &&
4011 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004012 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004013 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004014
Paul Bakker4d2c1242012-05-10 14:12:46 +00004015 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00004016 }
4017
4018 if( cur == NULL )
4019 *flags |= BADCERT_CN_MISMATCH;
4020 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00004021 else
4022 {
4023 while( name != NULL )
4024 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02004025 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004026 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004027 if( name->val.len == cn_len &&
4028 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004029 break;
4030
Paul Bakker535e97d2012-08-23 10:49:55 +00004031 if( name->val.len > 2 &&
4032 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004033 x509_wildcard_verify( cn, &name->val ) )
4034 break;
4035 }
4036
4037 name = name->next;
4038 }
4039
4040 if( name == NULL )
4041 *flags |= BADCERT_CN_MISMATCH;
4042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 }
4044
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 /*
Paul Bakker915275b2012-09-28 07:10:55 +00004046 * Iterate upwards in the given cert chain, to find our crt parent.
4047 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00004049 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004050
Paul Bakker76fd75a2011-01-16 21:12:10 +00004051 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004052 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004053 if( parent->ca_istrue == 0 ||
4054 crt->issuer_raw.len != parent->subject_raw.len ||
4055 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 crt->issuer_raw.len ) != 0 )
4057 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004058 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004059 continue;
4060 }
Paul Bakker915275b2012-09-28 07:10:55 +00004061 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 }
4063
Paul Bakker915275b2012-09-28 07:10:55 +00004064 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004065 {
Paul Bakker915275b2012-09-28 07:10:55 +00004066 /*
4067 * Part of the chain
4068 */
Paul Bakker9a736322012-11-14 12:39:52 +00004069 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004070 if( ret != 0 )
4071 return( ret );
4072 }
4073 else
Paul Bakker74111d32011-01-15 16:57:55 +00004074 {
Paul Bakker9a736322012-11-14 12:39:52 +00004075 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004076 if( ret != 0 )
4077 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004078 }
Paul Bakker915275b2012-09-28 07:10:55 +00004079
4080 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00004081 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004082
Paul Bakker5121ce52009-01-03 21:22:43 +00004083 return( 0 );
4084}
4085
4086/*
4087 * Unallocate all certificate data
4088 */
4089void x509_free( x509_cert *crt )
4090{
4091 x509_cert *cert_cur = crt;
4092 x509_cert *cert_prv;
4093 x509_name *name_cur;
4094 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00004095 x509_sequence *seq_cur;
4096 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00004097
4098 if( crt == NULL )
4099 return;
4100
4101 do
4102 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02004103 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00004104
4105 name_cur = cert_cur->issuer.next;
4106 while( name_cur != NULL )
4107 {
4108 name_prv = name_cur;
4109 name_cur = name_cur->next;
4110 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004111 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004112 }
4113
4114 name_cur = cert_cur->subject.next;
4115 while( name_cur != NULL )
4116 {
4117 name_prv = name_cur;
4118 name_cur = name_cur->next;
4119 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004120 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 }
4122
Paul Bakker74111d32011-01-15 16:57:55 +00004123 seq_cur = cert_cur->ext_key_usage.next;
4124 while( seq_cur != NULL )
4125 {
4126 seq_prv = seq_cur;
4127 seq_cur = seq_cur->next;
4128 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004129 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004130 }
4131
Paul Bakker8afa70d2012-02-11 18:42:45 +00004132 seq_cur = cert_cur->subject_alt_names.next;
4133 while( seq_cur != NULL )
4134 {
4135 seq_prv = seq_cur;
4136 seq_cur = seq_cur->next;
4137 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004138 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004139 }
4140
Paul Bakker5121ce52009-01-03 21:22:43 +00004141 if( cert_cur->raw.p != NULL )
4142 {
4143 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004144 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004145 }
4146
4147 cert_cur = cert_cur->next;
4148 }
4149 while( cert_cur != NULL );
4150
4151 cert_cur = crt;
4152 do
4153 {
4154 cert_prv = cert_cur;
4155 cert_cur = cert_cur->next;
4156
4157 memset( cert_prv, 0, sizeof( x509_cert ) );
4158 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004159 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 }
4161 while( cert_cur != NULL );
4162}
4163
Paul Bakkerd98030e2009-05-02 15:13:40 +00004164/*
4165 * Unallocate all CRL data
4166 */
4167void x509_crl_free( x509_crl *crl )
4168{
4169 x509_crl *crl_cur = crl;
4170 x509_crl *crl_prv;
4171 x509_name *name_cur;
4172 x509_name *name_prv;
4173 x509_crl_entry *entry_cur;
4174 x509_crl_entry *entry_prv;
4175
4176 if( crl == NULL )
4177 return;
4178
4179 do
4180 {
4181 name_cur = crl_cur->issuer.next;
4182 while( name_cur != NULL )
4183 {
4184 name_prv = name_cur;
4185 name_cur = name_cur->next;
4186 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004187 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004188 }
4189
4190 entry_cur = crl_cur->entry.next;
4191 while( entry_cur != NULL )
4192 {
4193 entry_prv = entry_cur;
4194 entry_cur = entry_cur->next;
4195 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004196 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004197 }
4198
4199 if( crl_cur->raw.p != NULL )
4200 {
4201 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004202 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004203 }
4204
4205 crl_cur = crl_cur->next;
4206 }
4207 while( crl_cur != NULL );
4208
4209 crl_cur = crl;
4210 do
4211 {
4212 crl_prv = crl_cur;
4213 crl_cur = crl_cur->next;
4214
4215 memset( crl_prv, 0, sizeof( x509_crl ) );
4216 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004217 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004218 }
4219 while( crl_cur != NULL );
4220}
4221
Paul Bakkerf9f377e2013-09-09 15:35:10 +02004222/*
4223 * Unallocate all CSR data
4224 */
4225void x509_csr_free( x509_csr *csr )
4226{
4227 x509_name *name_cur;
4228 x509_name *name_prv;
4229
4230 if( csr == NULL )
4231 return;
4232
4233 pk_free( &csr->pk );
4234
4235 name_cur = csr->subject.next;
4236 while( name_cur != NULL )
4237 {
4238 name_prv = name_cur;
4239 name_cur = name_cur->next;
4240 memset( name_prv, 0, sizeof( x509_name ) );
4241 polarssl_free( name_prv );
4242 }
4243
4244 if( csr->raw.p != NULL )
4245 {
4246 memset( csr->raw.p, 0, csr->raw.len );
4247 polarssl_free( csr->raw.p );
4248 }
4249
4250 memset( csr, 0, sizeof( x509_csr ) );
4251}
4252
Paul Bakker40e46942009-01-03 21:51:57 +00004253#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004254
Paul Bakker40e46942009-01-03 21:51:57 +00004255#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004256
4257/*
4258 * Checkup routine
4259 */
4260int x509_self_test( int verbose )
4261{
Paul Bakker5690efc2011-05-26 13:16:06 +00004262#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004263 int ret;
4264 int flags;
4265 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 x509_cert cacert;
4267 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004268 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00004269#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004270 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004271#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
4273 if( verbose != 0 )
4274 printf( " X.509 certificate load: " );
4275
4276 memset( &clicert, 0, sizeof( x509_cert ) );
4277
Paul Bakker3c2122f2013-06-24 19:03:14 +02004278 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004279 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280 if( ret != 0 )
4281 {
4282 if( verbose != 0 )
4283 printf( "failed\n" );
4284
4285 return( ret );
4286 }
4287
4288 memset( &cacert, 0, sizeof( x509_cert ) );
4289
Paul Bakker3c2122f2013-06-24 19:03:14 +02004290 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004291 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 if( ret != 0 )
4293 {
4294 if( verbose != 0 )
4295 printf( "failed\n" );
4296
4297 return( ret );
4298 }
4299
4300 if( verbose != 0 )
4301 printf( "passed\n X.509 private key load: " );
4302
4303 i = strlen( test_ca_key );
4304 j = strlen( test_ca_pwd );
4305
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004306 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00004307
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004308 if( ( ret = x509parse_key( &pkey,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004309 (const unsigned char *) test_ca_key, i,
4310 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
4312 if( verbose != 0 )
4313 printf( "failed\n" );
4314
4315 return( ret );
4316 }
4317
4318 if( verbose != 0 )
4319 printf( "passed\n X.509 signature verify: ");
4320
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004321 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 if( ret != 0 )
4323 {
4324 if( verbose != 0 )
4325 printf( "failed\n" );
4326
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004327 printf("ret = %d, &flags = %04x\n", ret, flags);
4328
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 return( ret );
4330 }
4331
Paul Bakker5690efc2011-05-26 13:16:06 +00004332#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004334 printf( "passed\n X.509 DHM parameter load: " );
4335
4336 i = strlen( test_dhm_params );
4337 j = strlen( test_ca_pwd );
4338
Paul Bakker3c2122f2013-06-24 19:03:14 +02004339 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004340 {
4341 if( verbose != 0 )
4342 printf( "failed\n" );
4343
4344 return( ret );
4345 }
4346
4347 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004349#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
4351 x509_free( &cacert );
4352 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004353 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00004354#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004355 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004357
4358 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004359#else
4360 ((void) verbose);
4361 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004363}
4364
4365#endif
4366
4367#endif