blob: f5e8688b9908ed79705225525cc24b8f4086193a [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
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001700#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001701 {
1702 /*
1703 * nope, copy the raw DER data
1704 */
1705 p = (unsigned char *) polarssl_malloc( len = buflen );
1706
1707 if( p == NULL )
1708 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1709
1710 memcpy( p, buf, buflen );
1711
1712 buflen = 0;
1713 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001714
1715 csr->raw.p = p;
1716 csr->raw.len = len;
1717 end = p + len;
1718
1719 /*
1720 * CertificationRequest ::= SEQUENCE {
1721 * certificationRequestInfo CertificationRequestInfo,
1722 * signatureAlgorithm AlgorithmIdentifier,
1723 * signature BIT STRING
1724 * }
1725 */
1726 if( ( ret = asn1_get_tag( &p, end, &len,
1727 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1728 {
1729 x509_csr_free( csr );
1730 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1731 }
1732
1733 if( len != (size_t) ( end - p ) )
1734 {
1735 x509_csr_free( csr );
1736 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1738 }
1739
1740 /*
1741 * CertificationRequestInfo ::= SEQUENCE {
1742 */
1743 csr->cri.p = p;
1744
1745 if( ( ret = asn1_get_tag( &p, end, &len,
1746 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1747 {
1748 x509_csr_free( csr );
1749 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1750 }
1751
1752 end = p + len;
1753 csr->cri.len = end - csr->cri.p;
1754
1755 /*
1756 * Version ::= INTEGER { v1(0) }
1757 */
1758 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
1759 {
1760 x509_csr_free( csr );
1761 return( ret );
1762 }
1763
1764 csr->version++;
1765
1766 if( csr->version != 1 )
1767 {
1768 x509_csr_free( csr );
1769 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1770 }
1771
1772 /*
1773 * subject Name
1774 */
1775 csr->subject_raw.p = p;
1776
1777 if( ( ret = asn1_get_tag( &p, end, &len,
1778 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1779 {
1780 x509_csr_free( csr );
1781 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1782 }
1783
1784 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
1785 {
1786 x509_csr_free( csr );
1787 return( ret );
1788 }
1789
1790 csr->subject_raw.len = p - csr->subject_raw.p;
1791
1792 /*
1793 * subjectPKInfo SubjectPublicKeyInfo
1794 */
1795 if( ( ret = x509_get_pubkey( &p, end, &csr->pk ) ) != 0 )
1796 {
1797 x509_csr_free( csr );
1798 return( ret );
1799 }
1800
1801 /*
1802 * attributes [0] Attributes
1803 */
1804 if( ( ret = asn1_get_tag( &p, end, &len,
1805 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1806 {
1807 x509_csr_free( csr );
1808 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1809 }
1810 // TODO Parse Attributes / extension requests
1811
1812 p += len;
1813
1814 end = csr->raw.p + csr->raw.len;
1815
1816 /*
1817 * signatureAlgorithm AlgorithmIdentifier,
1818 * signature BIT STRING
1819 */
1820 if( ( ret = x509_get_alg_null( &p, end, &csr->sig_oid ) ) != 0 )
1821 {
1822 x509_csr_free( csr );
1823 return( ret );
1824 }
1825
1826 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &csr->sig_md,
1827 &csr->sig_pk ) ) != 0 )
1828 {
1829 x509_csr_free( csr );
1830 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1831 }
1832
1833 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
1834 {
1835 x509_csr_free( csr );
1836 return( ret );
1837 }
1838
1839 if( p != end )
1840 {
1841 x509_csr_free( csr );
1842 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1843 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1844 }
1845
1846 return( 0 );
1847}
1848
1849/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001850 * Parse one or more CRLs and add them to the chained list
1851 */
Paul Bakker23986e52011-04-24 08:57:21 +00001852int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001853{
Paul Bakker23986e52011-04-24 08:57:21 +00001854 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001855 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 unsigned char *p, *end;
1857 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001858#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001859 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001860 pem_context pem;
1861#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001862
1863 crl = chain;
1864
1865 /*
1866 * Check for valid input
1867 */
1868 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001869 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870
1871 while( crl->version != 0 && crl->next != NULL )
1872 crl = crl->next;
1873
1874 /*
1875 * Add new CRL on the end of the chain if needed.
1876 */
1877 if ( crl->version != 0 && crl->next == NULL)
1878 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001879 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880
Paul Bakker7d06ad22009-05-02 15:53:56 +00001881 if( crl->next == NULL )
1882 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001883 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001884 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001885 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001886
Paul Bakker7d06ad22009-05-02 15:53:56 +00001887 crl = crl->next;
1888 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001889 }
1890
Paul Bakker96743fc2011-02-12 14:30:57 +00001891#if defined(POLARSSL_PEM_C)
1892 pem_init( &pem );
1893 ret = pem_read_buffer( &pem,
1894 "-----BEGIN X509 CRL-----",
1895 "-----END X509 CRL-----",
1896 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897
Paul Bakker96743fc2011-02-12 14:30:57 +00001898 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001900 /*
1901 * Was PEM encoded
1902 */
1903 buflen -= use_len;
1904 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001905
1906 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001907 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001909 p = pem.buf;
1910 pem.buf = NULL;
1911 len = pem.buflen;
1912 pem_free( &pem );
1913 }
Paul Bakker00b28602013-06-24 13:02:41 +02001914 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001915 {
1916 pem_free( &pem );
1917 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001918 }
1919 else
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001920#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001921 {
1922 /*
1923 * nope, copy the raw DER data
1924 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001925 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001926
1927 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001928 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001929
1930 memcpy( p, buf, buflen );
1931
1932 buflen = 0;
1933 }
1934
1935 crl->raw.p = p;
1936 crl->raw.len = len;
1937 end = p + len;
1938
1939 /*
1940 * CertificateList ::= SEQUENCE {
1941 * tbsCertList TBSCertList,
1942 * signatureAlgorithm AlgorithmIdentifier,
1943 * signatureValue BIT STRING }
1944 */
1945 if( ( ret = asn1_get_tag( &p, end, &len,
1946 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1947 {
1948 x509_crl_free( crl );
1949 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1950 }
1951
Paul Bakker23986e52011-04-24 08:57:21 +00001952 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953 {
1954 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001955 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1957 }
1958
1959 /*
1960 * TBSCertList ::= SEQUENCE {
1961 */
1962 crl->tbs.p = p;
1963
1964 if( ( ret = asn1_get_tag( &p, end, &len,
1965 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1966 {
1967 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001968 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001969 }
1970
1971 end = p + len;
1972 crl->tbs.len = end - crl->tbs.p;
1973
1974 /*
1975 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1976 * -- if present, MUST be v2
1977 *
1978 * signature AlgorithmIdentifier
1979 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001980 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001981 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001982 {
1983 x509_crl_free( crl );
1984 return( ret );
1985 }
1986
1987 crl->version++;
1988
1989 if( crl->version > 2 )
1990 {
1991 x509_crl_free( crl );
1992 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1993 }
1994
Paul Bakkerc70b9822013-04-07 22:00:46 +02001995 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1996 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001997 {
1998 x509_crl_free( crl );
1999 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
2000 }
2001
2002 /*
2003 * issuer Name
2004 */
2005 crl->issuer_raw.p = p;
2006
2007 if( ( ret = asn1_get_tag( &p, end, &len,
2008 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2009 {
2010 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002011 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002012 }
2013
2014 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
2015 {
2016 x509_crl_free( crl );
2017 return( ret );
2018 }
2019
2020 crl->issuer_raw.len = p - crl->issuer_raw.p;
2021
2022 /*
2023 * thisUpdate Time
2024 * nextUpdate Time OPTIONAL
2025 */
Paul Bakker91200182010-02-18 21:26:15 +00002026 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002027 {
2028 x509_crl_free( crl );
2029 return( ret );
2030 }
2031
Paul Bakker91200182010-02-18 21:26:15 +00002032 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002033 {
Paul Bakker9d781402011-05-09 16:17:09 +00002034 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00002035 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00002036 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00002037 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00002038 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002039 x509_crl_free( crl );
2040 return( ret );
2041 }
2042 }
2043
2044 /*
2045 * revokedCertificates SEQUENCE OF SEQUENCE {
2046 * userCertificate CertificateSerialNumber,
2047 * revocationDate Time,
2048 * crlEntryExtensions Extensions OPTIONAL
2049 * -- if present, MUST be v2
2050 * } OPTIONAL
2051 */
2052 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
2053 {
2054 x509_crl_free( crl );
2055 return( ret );
2056 }
2057
2058 /*
2059 * crlExtensions EXPLICIT Extensions OPTIONAL
2060 * -- if present, MUST be v2
2061 */
2062 if( crl->version == 2 )
2063 {
2064 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
2065
2066 if( ret != 0 )
2067 {
2068 x509_crl_free( crl );
2069 return( ret );
2070 }
2071 }
2072
2073 if( p != end )
2074 {
2075 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002076 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00002077 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2078 }
2079
2080 end = crl->raw.p + crl->raw.len;
2081
2082 /*
2083 * signatureAlgorithm AlgorithmIdentifier,
2084 * signatureValue BIT STRING
2085 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02002086 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002087 {
2088 x509_crl_free( crl );
2089 return( ret );
2090 }
2091
Paul Bakker535e97d2012-08-23 10:49:55 +00002092 if( crl->sig_oid1.len != crl->sig_oid2.len ||
2093 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002094 {
2095 x509_crl_free( crl );
2096 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
2097 }
2098
2099 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
2100 {
2101 x509_crl_free( crl );
2102 return( ret );
2103 }
2104
2105 if( p != end )
2106 {
2107 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00002108 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00002109 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2110 }
2111
2112 if( buflen > 0 )
2113 {
Paul Bakker6e339b52013-07-03 13:37:05 +02002114 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002115
Paul Bakker7d06ad22009-05-02 15:53:56 +00002116 if( crl->next == NULL )
2117 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002118 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00002119 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00002120 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002121
Paul Bakker7d06ad22009-05-02 15:53:56 +00002122 crl = crl->next;
2123 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002124
2125 return( x509parse_crl( crl, buf, buflen ) );
2126 }
2127
2128 return( 0 );
2129}
2130
Paul Bakker335db3f2011-04-25 15:28:35 +00002131#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002132/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00002133 * Load all data from a file into a given buffer.
2134 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002135static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00002136{
Paul Bakkerd98030e2009-05-02 15:13:40 +00002137 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002138 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00002139
Paul Bakkerd98030e2009-05-02 15:13:40 +00002140 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00002141 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002142
Paul Bakkerd98030e2009-05-02 15:13:40 +00002143 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002144 if( ( size = ftell( f ) ) == -1 )
2145 {
2146 fclose( f );
2147 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2148 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002149 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002150
Paul Bakker42c3ccf2013-08-19 14:29:31 +02002151 *n = (size_t) size;
2152
Paul Bakker694d3ae2013-08-19 14:23:38 +02002153 if( *n + 1 == 0 ||
2154 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002155 {
2156 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00002157 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02002158 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002159
Paul Bakkerd98030e2009-05-02 15:13:40 +00002160 if( fread( *buf, 1, *n, f ) != *n )
2161 {
2162 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02002163 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00002164 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002165 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00002166
Paul Bakkerd98030e2009-05-02 15:13:40 +00002167 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002168
Paul Bakkerd98030e2009-05-02 15:13:40 +00002169 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00002170
Paul Bakkerd98030e2009-05-02 15:13:40 +00002171 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00002172}
2173
2174/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002175 * Load one or more certificates and add them to the chained list
2176 */
Paul Bakker69e095c2011-12-10 21:55:01 +00002177int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00002178{
2179 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 size_t n;
2181 unsigned char *buf;
2182
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002183 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002184 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
Paul Bakker69e095c2011-12-10 21:55:01 +00002186 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
2188 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002189 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
2191 return( ret );
2192}
2193
Paul Bakker8d914582012-06-04 12:46:42 +00002194int x509parse_crtpath( x509_cert *chain, const char *path )
2195{
2196 int ret = 0;
2197#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00002198 int w_ret;
2199 WCHAR szDir[MAX_PATH];
2200 char filename[MAX_PATH];
2201 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002202 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00002203
Paul Bakker97872ac2012-11-02 12:53:26 +00002204 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00002205 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002206
2207 if( len > MAX_PATH - 3 )
2208 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00002209
Paul Bakker3338b792012-10-01 21:13:10 +00002210 memset( szDir, 0, sizeof(szDir) );
2211 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002212 memcpy( filename, path, len );
2213 filename[len++] = '\\';
2214 p = filename + len;
2215 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00002216
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002217 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002218
Paul Bakker97872ac2012-11-02 12:53:26 +00002219 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002220 if (hFind == INVALID_HANDLE_VALUE)
2221 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2222
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002223 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002224 do
2225 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002226 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002227
Paul Bakkere4791f32012-06-04 21:29:15 +00002228 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002229 continue;
2230
Paul Bakker3338b792012-10-01 21:13:10 +00002231 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2232 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002233 p, len - 1,
2234 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002235
Paul Bakker3338b792012-10-01 21:13:10 +00002236 w_ret = x509parse_crtfile( chain, filename );
2237 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002238 ret++;
2239 else
2240 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002241 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002242 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002243
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002244 if (GetLastError() != ERROR_NO_MORE_FILES)
2245 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002246
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002247cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002248 FindClose( hFind );
2249#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002250 int t_ret, i;
2251 struct stat sb;
2252 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002253 char entry_name[255];
2254 DIR *dir = opendir( path );
2255
2256 if( dir == NULL)
2257 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2258
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002259 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002260 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002261 if( result == NULL )
2262 break;
2263
2264 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2265
2266 i = stat( entry_name, &sb );
2267
2268 if( i == -1 )
Paul Bakker003dbad2013-09-09 17:26:14 +02002269 {
2270 closedir( dir );
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002271 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker003dbad2013-09-09 17:26:14 +02002272 }
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002273
2274 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002275 continue;
2276
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002277 // Ignore parse errors
2278 //
Paul Bakker8d914582012-06-04 12:46:42 +00002279 t_ret = x509parse_crtfile( chain, entry_name );
2280 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002281 ret++;
2282 else
2283 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002284 }
2285 closedir( dir );
2286#endif
2287
2288 return( ret );
2289}
2290
Paul Bakkerd98030e2009-05-02 15:13:40 +00002291/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002292 * Load a CSR into the structure
2293 */
2294int x509parse_csrfile( x509_csr *csr, const char *path )
2295{
2296 int ret;
2297 size_t n;
2298 unsigned char *buf;
2299
2300 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2301 return( ret );
2302
2303 ret = x509parse_csr( csr, buf, n );
2304
2305 memset( buf, 0, n + 1 );
2306 polarssl_free( buf );
2307
2308 return( ret );
2309}
2310
2311/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002312 * Load one or more CRLs and add them to the chained list
2313 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002314int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002315{
2316 int ret;
2317 size_t n;
2318 unsigned char *buf;
2319
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002320 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002321 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002322
Paul Bakker27fdf462011-06-09 13:55:13 +00002323 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002324
2325 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002326 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002327
2328 return( ret );
2329}
2330
Paul Bakker5121ce52009-01-03 21:22:43 +00002331/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002332 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002333 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002334int x509parse_keyfile( pk_context *ctx,
2335 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002336{
2337 int ret;
2338 size_t n;
2339 unsigned char *buf;
2340
2341 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2342 return( ret );
2343
2344 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002345 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002346 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002347 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002348 (const unsigned char *) pwd, strlen( pwd ) );
2349
2350 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002351 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002352
2353 return( ret );
2354}
2355
2356/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002357 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002358 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002359int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002360{
2361 int ret;
2362 size_t n;
2363 unsigned char *buf;
2364
2365 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2366 return( ret );
2367
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002368 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002369
2370 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002371 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002372
2373 return( ret );
2374}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002375
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002376#if defined(POLARSSL_RSA_C)
2377/*
2378 * Load and parse a private RSA key
2379 */
2380int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2381{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002382 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002383 pk_context pk;
2384
2385 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002386
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002387 ret = x509parse_keyfile( &pk, path, pwd );
2388
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002389 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2390 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2391
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002392 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002393 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002394 else
2395 rsa_free( rsa );
2396
2397 pk_free( &pk );
2398
2399 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002400}
2401
2402/*
2403 * Load and parse a public RSA key
2404 */
2405int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2406{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002407 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002408 pk_context pk;
2409
2410 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002411
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002412 ret = x509parse_public_keyfile( &pk, path );
2413
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002414 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2415 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2416
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002417 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002418 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002419 else
2420 rsa_free( rsa );
2421
2422 pk_free( &pk );
2423
2424 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002425}
2426#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002427#endif /* POLARSSL_FS_IO */
2428
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002429#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002430/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002431 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002433static int x509parse_key_pkcs1_der( rsa_context *rsa,
2434 const unsigned char *key,
2435 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002436{
Paul Bakker23986e52011-04-24 08:57:21 +00002437 int ret;
2438 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002440
Paul Bakker96743fc2011-02-12 14:30:57 +00002441 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002442 end = p + keylen;
2443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002445 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002446 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 * RSAPrivateKey ::= SEQUENCE {
2448 * version Version,
2449 * modulus INTEGER, -- n
2450 * publicExponent INTEGER, -- e
2451 * privateExponent INTEGER, -- d
2452 * prime1 INTEGER, -- p
2453 * prime2 INTEGER, -- q
2454 * exponent1 INTEGER, -- d mod (p-1)
2455 * exponent2 INTEGER, -- d mod (q-1)
2456 * coefficient INTEGER, -- (inverse of q) mod p
2457 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2458 * }
2459 */
2460 if( ( ret = asn1_get_tag( &p, end, &len,
2461 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2462 {
Paul Bakker9d781402011-05-09 16:17:09 +00002463 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 }
2465
2466 end = p + len;
2467
2468 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2469 {
Paul Bakker9d781402011-05-09 16:17:09 +00002470 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 }
2472
2473 if( rsa->ver != 0 )
2474 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002475 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 }
2477
2478 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2479 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2480 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2481 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2482 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2483 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2484 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2485 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2486 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002488 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 }
2490
2491 rsa->len = mpi_size( &rsa->N );
2492
2493 if( p != end )
2494 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002496 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002497 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
2499
2500 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2501 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 rsa_free( rsa );
2503 return( ret );
2504 }
2505
Paul Bakkere2f50402013-06-24 19:00:59 +02002506 return( 0 );
2507}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002508#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002509
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002510#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002511/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002512 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002513 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002514static int x509parse_key_sec1_der( ecp_keypair *eck,
2515 const unsigned char *key,
2516 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002517{
2518 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002519 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002520 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002521 x509_buf params;
2522 unsigned char *p = (unsigned char *) key;
2523 unsigned char *end = p + keylen;
2524 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002525
2526 /*
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02002527 * RFC 5915, or SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002528 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002529 * ECPrivateKey ::= SEQUENCE {
2530 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2531 * privateKey OCTET STRING,
2532 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2533 * publicKey [1] BIT STRING OPTIONAL
2534 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002535 */
2536 if( ( ret = asn1_get_tag( &p, end, &len,
2537 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2538 {
2539 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2540 }
2541
2542 end = p + len;
2543
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002544 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002545 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2546
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002547 if( version != 1 )
2548 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002549
Paul Bakkere2f50402013-06-24 19:00:59 +02002550 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2551 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2552
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002553 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002554 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002555 ecp_keypair_free( eck );
2556 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2557 }
2558
2559 p += len;
2560
2561 /*
2562 * Is 'parameters' present?
2563 */
2564 if( ( ret = asn1_get_tag( &p, end, &len,
2565 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2566 {
2567 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2568 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2569 {
2570 ecp_keypair_free( eck );
2571 return( ret );
2572 }
2573 }
2574 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2575 {
2576 ecp_keypair_free( eck );
2577 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2578 }
2579
2580 /*
2581 * Is 'publickey' present?
2582 */
2583 if( ( ret = asn1_get_tag( &p, end, &len,
2584 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2585 {
2586 end2 = p + len;
2587
2588 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2589 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2590
2591 if( p + len != end2 )
2592 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2593 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2594
2595 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2596 return( ret );
2597 }
2598 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2599 {
2600 ecp_keypair_free( eck );
2601 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2602 }
2603
2604 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2605 {
2606 ecp_keypair_free( eck );
2607 return( ret );
2608 }
2609
2610 return 0;
2611}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002612#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002613
2614/*
2615 * Parse an unencrypted PKCS#8 encoded private key
2616 */
2617static int x509parse_key_pkcs8_unencrypted_der(
2618 pk_context *pk,
2619 const unsigned char* key,
2620 size_t keylen )
2621{
2622 int ret, version;
2623 size_t len;
2624 x509_buf params;
2625 unsigned char *p = (unsigned char *) key;
2626 unsigned char *end = p + keylen;
2627 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002628 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002629
2630 /*
2631 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2632 *
2633 * PrivateKeyInfo ::= SEQUENCE {
2634 * version Version,
2635 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2636 * privateKey PrivateKey,
2637 * attributes [0] IMPLICIT Attributes OPTIONAL }
2638 *
2639 * Version ::= INTEGER
2640 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2641 * PrivateKey ::= OCTET STRING
2642 *
2643 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2644 */
2645
2646 if( ( ret = asn1_get_tag( &p, end, &len,
2647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2648 {
2649 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002650 }
2651
2652 end = p + len;
2653
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002654 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2655 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2656
2657 if( version != 0 )
2658 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2659
2660 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2661 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2662
2663 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2664 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2665
2666 if( len < 1 )
2667 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2668 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2669
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002670 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2671 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2672
2673 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002674 return( ret );
2675
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002676#if defined(POLARSSL_RSA_C)
2677 if( pk_alg == POLARSSL_PK_RSA )
2678 {
2679 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2680 {
2681 pk_free( pk );
2682 return( ret );
2683 }
2684 } else
2685#endif /* POLARSSL_RSA_C */
2686#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002687 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2688 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002689 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2690 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2691 {
2692 pk_free( pk );
2693 return( ret );
2694 }
2695 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002696#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002697 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2698
2699 return 0;
2700}
2701
2702/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002703 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002704 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002705static int x509parse_key_pkcs8_encrypted_der(
2706 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002707 const unsigned char *key, size_t keylen,
2708 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002709{
2710 int ret;
2711 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002712 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002713 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002714 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002715#if defined(POLARSSL_PKCS12_C)
2716 cipher_type_t cipher_alg;
2717 md_type_t md_alg;
2718#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002719
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002720 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002721
2722 p = (unsigned char *) key;
2723 end = p + keylen;
2724
Paul Bakker28144de2013-06-24 19:28:55 +02002725 if( pwdlen == 0 )
2726 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2727
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002728 /*
2729 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2730 *
2731 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2732 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2733 * encryptedData EncryptedData
2734 * }
2735 *
2736 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2737 *
2738 * EncryptedData ::= OCTET STRING
2739 *
2740 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2741 */
2742 if( ( ret = asn1_get_tag( &p, end, &len,
2743 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2744 {
2745 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2746 }
2747
2748 end = p + len;
2749
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002750 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002751 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002752
2753 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2754 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2755
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002756 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002757 return( POLARSSL_ERR_X509_INVALID_INPUT );
2758
2759 /*
2760 * Decrypt EncryptedData with appropriate PDE
2761 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002762#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002763 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002764 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002765 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002766 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002767 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002768 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002769 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2770 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2771
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002772 return( ret );
2773 }
2774 }
2775 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2776 {
2777 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2778 PKCS12_PBE_DECRYPT,
2779 pwd, pwdlen,
2780 p, len, buf ) ) != 0 )
2781 {
2782 return( ret );
2783 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002784
2785 // Best guess for password mismatch when using RC4. If first tag is
2786 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2787 //
2788 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2789 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002790 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002791 else
2792#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002793#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002794 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002795 {
2796 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2797 p, len, buf ) ) != 0 )
2798 {
2799 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2800 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2801
2802 return( ret );
2803 }
2804 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002805 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002806#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002807 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2808
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002809 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002810}
2811
2812/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002813 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002814 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002815int x509parse_key( pk_context *pk,
2816 const unsigned char *key, size_t keylen,
2817 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002818{
2819 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002820 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002821
2822#if defined(POLARSSL_PEM_C)
2823 size_t len;
2824 pem_context pem;
2825
2826 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002827
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002828#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002829 ret = pem_read_buffer( &pem,
2830 "-----BEGIN RSA PRIVATE KEY-----",
2831 "-----END RSA PRIVATE KEY-----",
2832 key, pwd, pwdlen, &len );
2833 if( ret == 0 )
2834 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002835 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2836 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2837
2838 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002839 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2840 pem.buf, pem.buflen ) ) != 0 )
2841 {
2842 pk_free( pk );
2843 }
2844
2845 pem_free( &pem );
2846 return( ret );
2847 }
2848 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2849 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2850 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2851 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2852 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2853 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002854#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002855
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002856#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002857 ret = pem_read_buffer( &pem,
2858 "-----BEGIN EC PRIVATE KEY-----",
2859 "-----END EC PRIVATE KEY-----",
2860 key, pwd, pwdlen, &len );
2861 if( ret == 0 )
2862 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002863 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2864 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2865
2866 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002867 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2868 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002869 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002870 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002871 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002872
2873 pem_free( &pem );
2874 return( ret );
2875 }
2876 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2877 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2878 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2879 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2880 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2881 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002882#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002883
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002884 ret = pem_read_buffer( &pem,
2885 "-----BEGIN PRIVATE KEY-----",
2886 "-----END PRIVATE KEY-----",
2887 key, NULL, 0, &len );
2888 if( ret == 0 )
2889 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002890 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002891 pem.buf, pem.buflen ) ) != 0 )
2892 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002893 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002894 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002895
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002896 pem_free( &pem );
2897 return( ret );
2898 }
2899 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2900 return( ret );
2901
2902 ret = pem_read_buffer( &pem,
2903 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2904 "-----END ENCRYPTED PRIVATE KEY-----",
2905 key, NULL, 0, &len );
2906 if( ret == 0 )
2907 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002908 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002909 pem.buf, pem.buflen,
2910 pwd, pwdlen ) ) != 0 )
2911 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002912 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002913 }
2914
2915 pem_free( &pem );
2916 return( ret );
2917 }
2918 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2919 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002920#else
2921 ((void) pwd);
2922 ((void) pwdlen);
2923#endif /* POLARSSL_PEM_C */
2924
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002925 /*
2926 * At this point we only know it's not a PEM formatted key. Could be any
2927 * of the known DER encoded private key formats
2928 *
2929 * We try the different DER format parsers to see if one passes without
2930 * error
2931 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002932 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2933 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002934 {
2935 return( 0 );
2936 }
2937
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002938 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002939
2940 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2941 {
2942 return( ret );
2943 }
2944
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002945 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002946 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002947
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002948 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002949
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002950#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002951 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2952 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2953
2954 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002955 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2956 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002957 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002958 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002959
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002960 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002961#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002962
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002963#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002964 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2965 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2966
2967 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002968 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2969 {
2970 return( 0 );
2971 }
2972
2973 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002974#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002975
2976 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2977}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002978
2979/*
2980 * Parse a public key
2981 */
2982int x509parse_public_key( pk_context *ctx,
2983 const unsigned char *key, size_t keylen )
2984{
2985 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002986 unsigned char *p;
2987#if defined(POLARSSL_PEM_C)
2988 size_t len;
2989 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002990
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002991 pem_init( &pem );
2992 ret = pem_read_buffer( &pem,
2993 "-----BEGIN PUBLIC KEY-----",
2994 "-----END PUBLIC KEY-----",
2995 key, NULL, 0, &len );
2996
2997 if( ret == 0 )
2998 {
2999 /*
3000 * Was PEM encoded
3001 */
3002 key = pem.buf;
3003 keylen = pem.buflen;
3004 }
3005 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3006 {
3007 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003008 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003009 }
3010#endif
3011 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003012
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003013 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003014
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003015#if defined(POLARSSL_PEM_C)
3016 pem_free( &pem );
3017#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003018
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003019 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003020}
3021
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003022#if defined(POLARSSL_RSA_C)
3023/*
3024 * Parse a private RSA key
3025 */
3026int x509parse_key_rsa( rsa_context *rsa,
3027 const unsigned char *key, size_t keylen,
3028 const unsigned char *pwd, size_t pwdlen )
3029{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003030 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003031 pk_context pk;
3032
3033 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003034
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003035 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
3036
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003037 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3038 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3039
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003040 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003041 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003042 else
3043 rsa_free( rsa );
3044
3045 pk_free( &pk );
3046
3047 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003048}
3049
3050/*
3051 * Parse a public RSA key
3052 */
3053int x509parse_public_key_rsa( rsa_context *rsa,
3054 const unsigned char *key, size_t keylen )
3055{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003056 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003057 pk_context pk;
3058
3059 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003060
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003061 ret = x509parse_public_key( &pk, key, keylen );
3062
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003063 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3064 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3065
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003066 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003067 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003068 else
3069 rsa_free( rsa );
3070
3071 pk_free( &pk );
3072
3073 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003074}
3075#endif /* POLARSSL_RSA_C */
3076
Paul Bakkereaa89f82011-04-04 21:36:15 +00003077#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003078/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003079 * Parse DHM parameters
3080 */
Paul Bakker23986e52011-04-24 08:57:21 +00003081int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003082{
Paul Bakker23986e52011-04-24 08:57:21 +00003083 int ret;
3084 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003085 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003086#if defined(POLARSSL_PEM_C)
3087 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003088
Paul Bakker96743fc2011-02-12 14:30:57 +00003089 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003090
Paul Bakker96743fc2011-02-12 14:30:57 +00003091 ret = pem_read_buffer( &pem,
3092 "-----BEGIN DH PARAMETERS-----",
3093 "-----END DH PARAMETERS-----",
3094 dhmin, NULL, 0, &dhminlen );
3095
3096 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003097 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003098 /*
3099 * Was PEM encoded
3100 */
3101 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003102 }
Paul Bakker00b28602013-06-24 13:02:41 +02003103 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003104 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003105 pem_free( &pem );
3106 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003107 }
3108
Paul Bakker96743fc2011-02-12 14:30:57 +00003109 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3110#else
3111 p = (unsigned char *) dhmin;
3112#endif
3113 end = p + dhminlen;
3114
Paul Bakker1b57b062011-01-06 15:48:19 +00003115 memset( dhm, 0, sizeof( dhm_context ) );
3116
Paul Bakker1b57b062011-01-06 15:48:19 +00003117 /*
3118 * DHParams ::= SEQUENCE {
3119 * prime INTEGER, -- P
3120 * generator INTEGER, -- g
3121 * }
3122 */
3123 if( ( ret = asn1_get_tag( &p, end, &len,
3124 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3125 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003126#if defined(POLARSSL_PEM_C)
3127 pem_free( &pem );
3128#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003129 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003130 }
3131
3132 end = p + len;
3133
3134 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3135 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3136 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003137#if defined(POLARSSL_PEM_C)
3138 pem_free( &pem );
3139#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003140 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003141 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003142 }
3143
3144 if( p != end )
3145 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003146#if defined(POLARSSL_PEM_C)
3147 pem_free( &pem );
3148#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003149 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003150 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003151 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3152 }
3153
Paul Bakker96743fc2011-02-12 14:30:57 +00003154#if defined(POLARSSL_PEM_C)
3155 pem_free( &pem );
3156#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003157
3158 return( 0 );
3159}
3160
Paul Bakker335db3f2011-04-25 15:28:35 +00003161#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003162/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003163 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003164 */
3165int x509parse_dhmfile( dhm_context *dhm, const char *path )
3166{
3167 int ret;
3168 size_t n;
3169 unsigned char *buf;
3170
Paul Bakker69e095c2011-12-10 21:55:01 +00003171 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3172 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003173
Paul Bakker27fdf462011-06-09 13:55:13 +00003174 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003175
3176 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003177 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003178
3179 return( ret );
3180}
Paul Bakker335db3f2011-04-25 15:28:35 +00003181#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003182#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003183
Paul Bakker5121ce52009-01-03 21:22:43 +00003184#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003185#include <stdarg.h>
3186
3187#if !defined vsnprintf
3188#define vsnprintf _vsnprintf
3189#endif // vsnprintf
3190
3191/*
3192 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3193 * Result value is not size of buffer needed, but -1 if no fit is possible.
3194 *
3195 * This fuction tries to 'fix' this by at least suggesting enlarging the
3196 * size by 20.
3197 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003198static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003199{
3200 va_list ap;
3201 int res = -1;
3202
3203 va_start( ap, format );
3204
3205 res = vsnprintf( str, size, format, ap );
3206
3207 va_end( ap );
3208
3209 // No quick fix possible
3210 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003211 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003212
3213 return res;
3214}
3215
3216#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003217#endif
3218
Paul Bakkerd98030e2009-05-02 15:13:40 +00003219#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3220
3221#define SAFE_SNPRINTF() \
3222{ \
3223 if( ret == -1 ) \
3224 return( -1 ); \
3225 \
Paul Bakker23986e52011-04-24 08:57:21 +00003226 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003227 p[n - 1] = '\0'; \
3228 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3229 } \
3230 \
Paul Bakker23986e52011-04-24 08:57:21 +00003231 n -= (unsigned int) ret; \
3232 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003233}
3234
Paul Bakker5121ce52009-01-03 21:22:43 +00003235/*
3236 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003237 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003239int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003240{
Paul Bakker23986e52011-04-24 08:57:21 +00003241 int ret;
3242 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003244 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003245 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 char s[128], *p;
3247
3248 memset( s, 0, sizeof( s ) );
3249
3250 name = dn;
3251 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003252 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
3254 while( name != NULL )
3255 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003256 if( !name->oid.p )
3257 {
3258 name = name->next;
3259 continue;
3260 }
3261
Paul Bakker74111d32011-01-15 16:57:55 +00003262 if( name != dn )
3263 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003264 ret = snprintf( p, n, ", " );
3265 SAFE_SNPRINTF();
3266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
Paul Bakkerc70b9822013-04-07 22:00:46 +02003268 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
Paul Bakkerc70b9822013-04-07 22:00:46 +02003270 if( ret == 0 )
3271 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003273 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003274 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003275
3276 for( i = 0; i < name->val.len; i++ )
3277 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003278 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 break;
3280
3281 c = name->val.p[i];
3282 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3283 s[i] = '?';
3284 else s[i] = c;
3285 }
3286 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003287 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003288 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 name = name->next;
3290 }
3291
Paul Bakker23986e52011-04-24 08:57:21 +00003292 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003293}
3294
3295/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003296 * Store the serial in printable form into buf; no more
3297 * than size characters will be written
3298 */
3299int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3300{
Paul Bakker23986e52011-04-24 08:57:21 +00003301 int ret;
3302 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003303 char *p;
3304
3305 p = buf;
3306 n = size;
3307
3308 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003309 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003310
3311 for( i = 0; i < nr; i++ )
3312 {
Paul Bakker93048802011-12-05 14:38:06 +00003313 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003314 continue;
3315
Paul Bakkerdd476992011-01-16 21:34:59 +00003316 ret = snprintf( p, n, "%02X%s",
3317 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3318 SAFE_SNPRINTF();
3319 }
3320
Paul Bakker03c7c252011-11-25 12:37:37 +00003321 if( nr != serial->len )
3322 {
3323 ret = snprintf( p, n, "...." );
3324 SAFE_SNPRINTF();
3325 }
3326
Paul Bakker23986e52011-04-24 08:57:21 +00003327 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003328}
3329
3330/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003331 * Helper for writing "RSA key size", "EC key size", etc
3332 */
3333static int x509_key_size_helper( char *buf, size_t size, const char *name )
3334{
3335 char *p = buf;
3336 size_t n = size;
3337 int ret;
3338
3339 if( strlen( name ) + sizeof( " key size" ) > size )
3340 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3341
3342 ret = snprintf( p, n, "%s key size", name );
3343 SAFE_SNPRINTF();
3344
3345 return( 0 );
3346}
3347
3348/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003349 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003351#define BEFORE_COLON 14
3352#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003353int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3354 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355{
Paul Bakker23986e52011-04-24 08:57:21 +00003356 int ret;
3357 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003358 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003359 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003360 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
3362 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003363 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Paul Bakkerd98030e2009-05-02 15:13:40 +00003365 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003367 SAFE_SNPRINTF();
3368 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003370 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Paul Bakkerdd476992011-01-16 21:34:59 +00003372 ret = x509parse_serial_gets( p, n, &crt->serial);
3373 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
Paul Bakkerd98030e2009-05-02 15:13:40 +00003375 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3376 SAFE_SNPRINTF();
3377 ret = x509parse_dn_gets( p, n, &crt->issuer );
3378 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Paul Bakkerd98030e2009-05-02 15:13:40 +00003380 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3381 SAFE_SNPRINTF();
3382 ret = x509parse_dn_gets( p, n, &crt->subject );
3383 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003384
Paul Bakkerd98030e2009-05-02 15:13:40 +00003385 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3387 crt->valid_from.year, crt->valid_from.mon,
3388 crt->valid_from.day, crt->valid_from.hour,
3389 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003390 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
Paul Bakkerd98030e2009-05-02 15:13:40 +00003392 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3394 crt->valid_to.year, crt->valid_to.mon,
3395 crt->valid_to.day, crt->valid_to.hour,
3396 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003397 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
Paul Bakkerc70b9822013-04-07 22:00:46 +02003399 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003400 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Paul Bakkerc70b9822013-04-07 22:00:46 +02003402 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3403 if( ret != 0 )
3404 ret = snprintf( p, n, "???" );
3405 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003406 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003407 SAFE_SNPRINTF();
3408
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003409 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003410 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003411 {
3412 return( ret );
3413 }
3414
3415 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003416 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003417 SAFE_SNPRINTF();
3418
Paul Bakker23986e52011-04-24 08:57:21 +00003419 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003420}
3421
Paul Bakker74111d32011-01-15 16:57:55 +00003422/*
3423 * Return an informational string describing the given OID
3424 */
3425const char *x509_oid_get_description( x509_buf *oid )
3426{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003427 const char *desc = NULL;
3428 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003429
Paul Bakkerc70b9822013-04-07 22:00:46 +02003430 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003431
Paul Bakkerc70b9822013-04-07 22:00:46 +02003432 if( ret != 0 )
3433 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003434
Paul Bakkerc70b9822013-04-07 22:00:46 +02003435 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003436}
3437
3438/* Return the x.y.z.... style numeric string for the given OID */
3439int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3440{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003441 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003442}
3443
Paul Bakkerd98030e2009-05-02 15:13:40 +00003444/*
3445 * Return an informational string about the CRL.
3446 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003447int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3448 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003449{
Paul Bakker23986e52011-04-24 08:57:21 +00003450 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003451 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003452 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003453 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003454 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003455
3456 p = buf;
3457 n = size;
3458
3459 ret = snprintf( p, n, "%sCRL version : %d",
3460 prefix, crl->version );
3461 SAFE_SNPRINTF();
3462
3463 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3464 SAFE_SNPRINTF();
3465 ret = x509parse_dn_gets( p, n, &crl->issuer );
3466 SAFE_SNPRINTF();
3467
3468 ret = snprintf( p, n, "\n%sthis update : " \
3469 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3470 crl->this_update.year, crl->this_update.mon,
3471 crl->this_update.day, crl->this_update.hour,
3472 crl->this_update.min, crl->this_update.sec );
3473 SAFE_SNPRINTF();
3474
3475 ret = snprintf( p, n, "\n%snext update : " \
3476 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3477 crl->next_update.year, crl->next_update.mon,
3478 crl->next_update.day, crl->next_update.hour,
3479 crl->next_update.min, crl->next_update.sec );
3480 SAFE_SNPRINTF();
3481
3482 entry = &crl->entry;
3483
3484 ret = snprintf( p, n, "\n%sRevoked certificates:",
3485 prefix );
3486 SAFE_SNPRINTF();
3487
Paul Bakker9be19372009-07-27 20:21:53 +00003488 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003489 {
3490 ret = snprintf( p, n, "\n%sserial number: ",
3491 prefix );
3492 SAFE_SNPRINTF();
3493
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003494 ret = x509parse_serial_gets( p, n, &entry->serial);
3495 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003496
Paul Bakkerd98030e2009-05-02 15:13:40 +00003497 ret = snprintf( p, n, " revocation date: " \
3498 "%04d-%02d-%02d %02d:%02d:%02d",
3499 entry->revocation_date.year, entry->revocation_date.mon,
3500 entry->revocation_date.day, entry->revocation_date.hour,
3501 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003502 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003503
3504 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003505 }
3506
Paul Bakkerc70b9822013-04-07 22:00:46 +02003507 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003508 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Paul Bakkerc70b9822013-04-07 22:00:46 +02003510 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3511 if( ret != 0 )
3512 ret = snprintf( p, n, "???" );
3513 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003514 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003515 SAFE_SNPRINTF();
3516
Paul Bakker1e27bb22009-07-19 20:25:25 +00003517 ret = snprintf( p, n, "\n" );
3518 SAFE_SNPRINTF();
3519
Paul Bakker23986e52011-04-24 08:57:21 +00003520 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003521}
3522
3523/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003524 * Return an informational string about the CSR.
3525 */
3526int x509parse_csr_info( char *buf, size_t size, const char *prefix,
3527 const x509_csr *csr )
3528{
3529 int ret;
3530 size_t n;
3531 char *p;
3532 const char *desc;
3533 char key_size_str[BEFORE_COLON];
3534
3535 p = buf;
3536 n = size;
3537
3538 ret = snprintf( p, n, "%sCSR version : %d",
3539 prefix, csr->version );
3540 SAFE_SNPRINTF();
3541
3542 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3543 SAFE_SNPRINTF();
3544 ret = x509parse_dn_gets( p, n, &csr->subject );
3545 SAFE_SNPRINTF();
3546
3547 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
3548 SAFE_SNPRINTF();
3549
3550 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
3551 if( ret != 0 )
3552 ret = snprintf( p, n, "???" );
3553 else
3554 ret = snprintf( p, n, "%s", desc );
3555 SAFE_SNPRINTF();
3556
3557 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
3558 pk_get_name( &csr->pk ) ) ) != 0 )
3559 {
3560 return( ret );
3561 }
3562
3563 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
3564 (int) pk_get_size( &csr->pk ) );
3565 SAFE_SNPRINTF();
3566
3567 return( (int) ( size - n ) );
3568}
3569
3570/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003571 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003573#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003574int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003575{
Paul Bakkercce9d772011-11-18 14:26:47 +00003576 int year, mon, day;
3577 int hour, min, sec;
3578
3579#if defined(_WIN32)
3580 SYSTEMTIME st;
3581
3582 GetLocalTime(&st);
3583
3584 year = st.wYear;
3585 mon = st.wMonth;
3586 day = st.wDay;
3587 hour = st.wHour;
3588 min = st.wMinute;
3589 sec = st.wSecond;
3590#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 struct tm *lt;
3592 time_t tt;
3593
3594 tt = time( NULL );
3595 lt = localtime( &tt );
3596
Paul Bakkercce9d772011-11-18 14:26:47 +00003597 year = lt->tm_year + 1900;
3598 mon = lt->tm_mon + 1;
3599 day = lt->tm_mday;
3600 hour = lt->tm_hour;
3601 min = lt->tm_min;
3602 sec = lt->tm_sec;
3603#endif
3604
3605 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003606 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Paul Bakkercce9d772011-11-18 14:26:47 +00003608 if( year == to->year &&
3609 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003610 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Paul Bakkercce9d772011-11-18 14:26:47 +00003612 if( year == to->year &&
3613 mon == to->mon &&
3614 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003615 return( 1 );
3616
Paul Bakkercce9d772011-11-18 14:26:47 +00003617 if( year == to->year &&
3618 mon == to->mon &&
3619 day == to->day &&
3620 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003621 return( 1 );
3622
Paul Bakkercce9d772011-11-18 14:26:47 +00003623 if( year == to->year &&
3624 mon == to->mon &&
3625 day == to->day &&
3626 hour == to->hour &&
3627 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003628 return( 1 );
3629
Paul Bakkercce9d772011-11-18 14:26:47 +00003630 if( year == to->year &&
3631 mon == to->mon &&
3632 day == to->day &&
3633 hour == to->hour &&
3634 min == to->min &&
3635 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003636 return( 1 );
3637
Paul Bakker40ea7de2009-05-03 10:18:48 +00003638 return( 0 );
3639}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003640#else /* POLARSSL_HAVE_TIME */
3641int x509parse_time_expired( const x509_time *to )
3642{
3643 ((void) to);
3644 return( 0 );
3645}
3646#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003647
3648/*
3649 * Return 1 if the certificate is revoked, or 0 otherwise.
3650 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003651int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003652{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003653 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003654
3655 while( cur != NULL && cur->serial.len != 0 )
3656 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003657 if( crt->serial.len == cur->serial.len &&
3658 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003659 {
3660 if( x509parse_time_expired( &cur->revocation_date ) )
3661 return( 1 );
3662 }
3663
3664 cur = cur->next;
3665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
3667 return( 0 );
3668}
3669
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003670/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003671 * Check that the given certificate is valid accoring to the CRL.
3672 */
3673static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3674 x509_crl *crl_list)
3675{
3676 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003677 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3678 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003679
Paul Bakker915275b2012-09-28 07:10:55 +00003680 if( ca == NULL )
3681 return( flags );
3682
Paul Bakker76fd75a2011-01-16 21:12:10 +00003683 /*
3684 * TODO: What happens if no CRL is present?
3685 * Suggestion: Revocation state should be unknown if no CRL is present.
3686 * For backwards compatibility this is not yet implemented.
3687 */
3688
Paul Bakker915275b2012-09-28 07:10:55 +00003689 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003690 {
Paul Bakker915275b2012-09-28 07:10:55 +00003691 if( crl_list->version == 0 ||
3692 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003693 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3694 crl_list->issuer_raw.len ) != 0 )
3695 {
3696 crl_list = crl_list->next;
3697 continue;
3698 }
3699
3700 /*
3701 * Check if CRL is correctly signed by the trusted CA
3702 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003703 md_info = md_info_from_type( crl_list->sig_md );
3704 if( md_info == NULL )
3705 {
3706 /*
3707 * Cannot check 'unknown' hash
3708 */
3709 flags |= BADCRL_NOT_TRUSTED;
3710 break;
3711 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003712
Paul Bakkerc70b9822013-04-07 22:00:46 +02003713 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003714
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003715 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003716 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003717 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003718 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003719 flags |= BADCRL_NOT_TRUSTED;
3720 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003721 }
3722
3723 /*
3724 * Check for validity of CRL (Do not drop out)
3725 */
3726 if( x509parse_time_expired( &crl_list->next_update ) )
3727 flags |= BADCRL_EXPIRED;
3728
3729 /*
3730 * Check if certificate is revoked
3731 */
3732 if( x509parse_revoked(crt, crl_list) )
3733 {
3734 flags |= BADCERT_REVOKED;
3735 break;
3736 }
3737
3738 crl_list = crl_list->next;
3739 }
3740 return flags;
3741}
3742
Paul Bakkera5943852013-09-09 17:21:45 +02003743// Equal == 0, inequal == 1
3744static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3745{
3746 size_t i;
3747 unsigned char diff;
3748 const unsigned char *n1 = s1, *n2 = s2;
3749
3750 for( i = 0; i < len; i++ )
3751 {
3752 diff = n1[i] ^ n2[i];
3753
3754 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
3755 continue;
3756
3757 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
3758 continue;
3759
3760 return( 1 );
3761 }
3762
3763 return( 0 );
3764}
3765
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003766static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003767{
3768 size_t i;
3769 size_t cn_idx = 0;
3770
Paul Bakker57b12982012-02-11 17:38:38 +00003771 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003772 return( 0 );
3773
3774 for( i = 0; i < strlen( cn ); ++i )
3775 {
3776 if( cn[i] == '.' )
3777 {
3778 cn_idx = i;
3779 break;
3780 }
3781 }
3782
3783 if( cn_idx == 0 )
3784 return( 0 );
3785
Paul Bakker535e97d2012-08-23 10:49:55 +00003786 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkera5943852013-09-09 17:21:45 +02003787 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003788 {
3789 return( 1 );
3790 }
3791
3792 return( 0 );
3793}
3794
Paul Bakker915275b2012-09-28 07:10:55 +00003795static int x509parse_verify_top(
3796 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003797 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003798 int (*f_vrfy)(void *, x509_cert *, int, int *),
3799 void *p_vrfy )
3800{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003801 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003802 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003803 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3804 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003805
3806 if( x509parse_time_expired( &child->valid_to ) )
3807 *flags |= BADCERT_EXPIRED;
3808
3809 /*
3810 * Child is the top of the chain. Check against the trust_ca list.
3811 */
3812 *flags |= BADCERT_NOT_TRUSTED;
3813
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003814 md_info = md_info_from_type( child->sig_md );
3815 if( md_info == NULL )
3816 {
3817 /*
3818 * Cannot check 'unknown', no need to try any CA
3819 */
3820 trust_ca = NULL;
3821 }
3822 else
3823 md( md_info, child->tbs.p, child->tbs.len, hash );
3824
Paul Bakker915275b2012-09-28 07:10:55 +00003825 while( trust_ca != NULL )
3826 {
3827 if( trust_ca->version == 0 ||
3828 child->issuer_raw.len != trust_ca->subject_raw.len ||
3829 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3830 child->issuer_raw.len ) != 0 )
3831 {
3832 trust_ca = trust_ca->next;
3833 continue;
3834 }
3835
Paul Bakker9a736322012-11-14 12:39:52 +00003836 /*
3837 * Reduce path_len to check against if top of the chain is
3838 * the same as the trusted CA
3839 */
3840 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3841 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003842 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003843 {
3844 check_path_cnt--;
3845 }
3846
Paul Bakker915275b2012-09-28 07:10:55 +00003847 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003848 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003849 {
3850 trust_ca = trust_ca->next;
3851 continue;
3852 }
3853
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003854 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003855 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003856 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003857 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003858 trust_ca = trust_ca->next;
3859 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003860 }
3861
3862 /*
3863 * Top of chain is signed by a trusted CA
3864 */
3865 *flags &= ~BADCERT_NOT_TRUSTED;
3866 break;
3867 }
3868
Paul Bakker9a736322012-11-14 12:39:52 +00003869 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003870 * If top of chain is not the same as the trusted CA send a verify request
3871 * to the callback for any issues with validity and CRL presence for the
3872 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003873 */
3874 if( trust_ca != NULL &&
3875 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3876 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3877 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003878 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003879 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003880 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3881
3882 if( x509parse_time_expired( &trust_ca->valid_to ) )
3883 ca_flags |= BADCERT_EXPIRED;
3884
Paul Bakker915275b2012-09-28 07:10:55 +00003885 if( NULL != f_vrfy )
3886 {
Paul Bakker9a736322012-11-14 12:39:52 +00003887 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003888 return( ret );
3889 }
3890 }
3891
3892 /* Call callback on top cert */
3893 if( NULL != f_vrfy )
3894 {
Paul Bakker9a736322012-11-14 12:39:52 +00003895 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003896 return( ret );
3897 }
3898
Paul Bakker915275b2012-09-28 07:10:55 +00003899 *flags |= ca_flags;
3900
3901 return( 0 );
3902}
3903
3904static int x509parse_verify_child(
3905 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003906 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003907 int (*f_vrfy)(void *, x509_cert *, int, int *),
3908 void *p_vrfy )
3909{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003910 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003911 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003912 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003913 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003914 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003915
3916 if( x509parse_time_expired( &child->valid_to ) )
3917 *flags |= BADCERT_EXPIRED;
3918
Paul Bakkerc70b9822013-04-07 22:00:46 +02003919 md_info = md_info_from_type( child->sig_md );
3920 if( md_info == NULL )
3921 {
3922 /*
3923 * Cannot check 'unknown' hash
3924 */
Paul Bakker915275b2012-09-28 07:10:55 +00003925 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003926 }
3927 else
3928 {
3929 md( md_info, child->tbs.p, child->tbs.len, hash );
3930
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003931 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003932 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003933 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003934 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003935 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003936 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003937 }
3938
Paul Bakker915275b2012-09-28 07:10:55 +00003939 /* Check trusted CA's CRL for the given crt */
3940 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3941
3942 grandparent = parent->next;
3943
3944 while( grandparent != NULL )
3945 {
3946 if( grandparent->version == 0 ||
3947 grandparent->ca_istrue == 0 ||
3948 parent->issuer_raw.len != grandparent->subject_raw.len ||
3949 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3950 parent->issuer_raw.len ) != 0 )
3951 {
3952 grandparent = grandparent->next;
3953 continue;
3954 }
3955 break;
3956 }
3957
Paul Bakker915275b2012-09-28 07:10:55 +00003958 if( grandparent != NULL )
3959 {
3960 /*
3961 * Part of the chain
3962 */
Paul Bakker9a736322012-11-14 12:39:52 +00003963 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 +00003964 if( ret != 0 )
3965 return( ret );
Paul Bakkera5943852013-09-09 17:21:45 +02003966 }
Paul Bakker915275b2012-09-28 07:10:55 +00003967 else
3968 {
Paul Bakker9a736322012-11-14 12:39:52 +00003969 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 +00003970 if( ret != 0 )
3971 return( ret );
3972 }
3973
3974 /* child is verified to be a child of the parent, call verify callback */
3975 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003976 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003977 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003978
3979 *flags |= parent_flags;
3980
3981 return( 0 );
3982}
3983
Paul Bakker76fd75a2011-01-16 21:12:10 +00003984/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 * Verify the certificate validity
3986 */
3987int x509parse_verify( x509_cert *crt,
3988 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003989 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003990 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003991 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003992 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003993{
Paul Bakker23986e52011-04-24 08:57:21 +00003994 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003995 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003996 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003997 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003999 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakker40ea7de2009-05-03 10:18:48 +00004001 *flags = 0;
4002
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 if( cn != NULL )
4004 {
4005 name = &crt->subject;
4006 cn_len = strlen( cn );
4007
Paul Bakker4d2c1242012-05-10 14:12:46 +00004008 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00004009 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00004010 cur = &crt->subject_alt_names;
4011
4012 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004013 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004014 if( cur->buf.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02004015 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004016 break;
4017
Paul Bakker535e97d2012-08-23 10:49:55 +00004018 if( cur->buf.len > 2 &&
4019 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004020 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00004021 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Paul Bakker4d2c1242012-05-10 14:12:46 +00004023 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00004024 }
4025
4026 if( cur == NULL )
4027 *flags |= BADCERT_CN_MISMATCH;
4028 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00004029 else
4030 {
4031 while( name != NULL )
4032 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02004033 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004034 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004035 if( name->val.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02004036 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004037 break;
4038
Paul Bakker535e97d2012-08-23 10:49:55 +00004039 if( name->val.len > 2 &&
4040 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004041 x509_wildcard_verify( cn, &name->val ) )
4042 break;
4043 }
4044
4045 name = name->next;
4046 }
4047
4048 if( name == NULL )
4049 *flags |= BADCERT_CN_MISMATCH;
4050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004051 }
4052
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 /*
Paul Bakker915275b2012-09-28 07:10:55 +00004054 * Iterate upwards in the given cert chain, to find our crt parent.
4055 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00004057 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
Paul Bakker76fd75a2011-01-16 21:12:10 +00004059 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004060 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004061 if( parent->ca_istrue == 0 ||
4062 crt->issuer_raw.len != parent->subject_raw.len ||
4063 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 crt->issuer_raw.len ) != 0 )
4065 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004066 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004067 continue;
4068 }
Paul Bakker915275b2012-09-28 07:10:55 +00004069 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004070 }
4071
Paul Bakker915275b2012-09-28 07:10:55 +00004072 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004073 {
Paul Bakker915275b2012-09-28 07:10:55 +00004074 /*
4075 * Part of the chain
4076 */
Paul Bakker9a736322012-11-14 12:39:52 +00004077 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004078 if( ret != 0 )
4079 return( ret );
4080 }
4081 else
Paul Bakker74111d32011-01-15 16:57:55 +00004082 {
Paul Bakker9a736322012-11-14 12:39:52 +00004083 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004084 if( ret != 0 )
4085 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004086 }
Paul Bakker915275b2012-09-28 07:10:55 +00004087
4088 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00004089 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004090
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 return( 0 );
4092}
4093
4094/*
4095 * Unallocate all certificate data
4096 */
4097void x509_free( x509_cert *crt )
4098{
4099 x509_cert *cert_cur = crt;
4100 x509_cert *cert_prv;
4101 x509_name *name_cur;
4102 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00004103 x509_sequence *seq_cur;
4104 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00004105
4106 if( crt == NULL )
4107 return;
4108
4109 do
4110 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02004111 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00004112
4113 name_cur = cert_cur->issuer.next;
4114 while( name_cur != NULL )
4115 {
4116 name_prv = name_cur;
4117 name_cur = name_cur->next;
4118 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004119 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 }
4121
4122 name_cur = cert_cur->subject.next;
4123 while( name_cur != NULL )
4124 {
4125 name_prv = name_cur;
4126 name_cur = name_cur->next;
4127 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004128 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004129 }
4130
Paul Bakker74111d32011-01-15 16:57:55 +00004131 seq_cur = cert_cur->ext_key_usage.next;
4132 while( seq_cur != NULL )
4133 {
4134 seq_prv = seq_cur;
4135 seq_cur = seq_cur->next;
4136 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004137 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004138 }
4139
Paul Bakker8afa70d2012-02-11 18:42:45 +00004140 seq_cur = cert_cur->subject_alt_names.next;
4141 while( seq_cur != NULL )
4142 {
4143 seq_prv = seq_cur;
4144 seq_cur = seq_cur->next;
4145 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004146 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004147 }
4148
Paul Bakker5121ce52009-01-03 21:22:43 +00004149 if( cert_cur->raw.p != NULL )
4150 {
4151 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004152 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004153 }
4154
4155 cert_cur = cert_cur->next;
4156 }
4157 while( cert_cur != NULL );
4158
4159 cert_cur = crt;
4160 do
4161 {
4162 cert_prv = cert_cur;
4163 cert_cur = cert_cur->next;
4164
4165 memset( cert_prv, 0, sizeof( x509_cert ) );
4166 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004167 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004168 }
4169 while( cert_cur != NULL );
4170}
4171
Paul Bakkerd98030e2009-05-02 15:13:40 +00004172/*
4173 * Unallocate all CRL data
4174 */
4175void x509_crl_free( x509_crl *crl )
4176{
4177 x509_crl *crl_cur = crl;
4178 x509_crl *crl_prv;
4179 x509_name *name_cur;
4180 x509_name *name_prv;
4181 x509_crl_entry *entry_cur;
4182 x509_crl_entry *entry_prv;
4183
4184 if( crl == NULL )
4185 return;
4186
4187 do
4188 {
4189 name_cur = crl_cur->issuer.next;
4190 while( name_cur != NULL )
4191 {
4192 name_prv = name_cur;
4193 name_cur = name_cur->next;
4194 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004195 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004196 }
4197
4198 entry_cur = crl_cur->entry.next;
4199 while( entry_cur != NULL )
4200 {
4201 entry_prv = entry_cur;
4202 entry_cur = entry_cur->next;
4203 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004204 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004205 }
4206
4207 if( crl_cur->raw.p != NULL )
4208 {
4209 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004210 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004211 }
4212
4213 crl_cur = crl_cur->next;
4214 }
4215 while( crl_cur != NULL );
4216
4217 crl_cur = crl;
4218 do
4219 {
4220 crl_prv = crl_cur;
4221 crl_cur = crl_cur->next;
4222
4223 memset( crl_prv, 0, sizeof( x509_crl ) );
4224 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004225 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004226 }
4227 while( crl_cur != NULL );
4228}
4229
Paul Bakkerf9f377e2013-09-09 15:35:10 +02004230/*
4231 * Unallocate all CSR data
4232 */
4233void x509_csr_free( x509_csr *csr )
4234{
4235 x509_name *name_cur;
4236 x509_name *name_prv;
4237
4238 if( csr == NULL )
4239 return;
4240
4241 pk_free( &csr->pk );
4242
4243 name_cur = csr->subject.next;
4244 while( name_cur != NULL )
4245 {
4246 name_prv = name_cur;
4247 name_cur = name_cur->next;
4248 memset( name_prv, 0, sizeof( x509_name ) );
4249 polarssl_free( name_prv );
4250 }
4251
4252 if( csr->raw.p != NULL )
4253 {
4254 memset( csr->raw.p, 0, csr->raw.len );
4255 polarssl_free( csr->raw.p );
4256 }
4257
4258 memset( csr, 0, sizeof( x509_csr ) );
4259}
4260
Paul Bakker40e46942009-01-03 21:51:57 +00004261#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004262
Paul Bakker40e46942009-01-03 21:51:57 +00004263#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004264
4265/*
4266 * Checkup routine
4267 */
4268int x509_self_test( int verbose )
4269{
Paul Bakker5690efc2011-05-26 13:16:06 +00004270#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004271 int ret;
4272 int flags;
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 x509_cert cacert;
4274 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004275 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00004276#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004277 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
4280 if( verbose != 0 )
4281 printf( " X.509 certificate load: " );
4282
4283 memset( &clicert, 0, sizeof( x509_cert ) );
4284
Paul Bakker3c2122f2013-06-24 19:03:14 +02004285 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004286 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004287 if( ret != 0 )
4288 {
4289 if( verbose != 0 )
4290 printf( "failed\n" );
4291
4292 return( ret );
4293 }
4294
4295 memset( &cacert, 0, sizeof( x509_cert ) );
4296
Paul Bakker3c2122f2013-06-24 19:03:14 +02004297 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004298 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 if( ret != 0 )
4300 {
4301 if( verbose != 0 )
4302 printf( "failed\n" );
4303
4304 return( ret );
4305 }
4306
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02004307#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
4308 defined(POLARSSL_DES_C) && defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 if( verbose != 0 )
4310 printf( "passed\n X.509 private key load: " );
4311
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004312 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00004313
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004314 if( ( ret = x509parse_key( &pkey,
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02004315 (const unsigned char *) test_ca_key,
4316 strlen( test_ca_key ),
4317 (const unsigned char *) test_ca_pwd,
4318 strlen( test_ca_pwd ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 {
4320 if( verbose != 0 )
4321 printf( "failed\n" );
4322
4323 return( ret );
4324 }
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02004325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004326
4327 if( verbose != 0 )
4328 printf( "passed\n X.509 signature verify: ");
4329
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004330 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 if( ret != 0 )
4332 {
4333 if( verbose != 0 )
4334 printf( "failed\n" );
4335
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004336 printf("ret = %d, &flags = %04x\n", ret, flags);
4337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 return( ret );
4339 }
4340
Paul Bakker5690efc2011-05-26 13:16:06 +00004341#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004343 printf( "passed\n X.509 DHM parameter load: " );
4344
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02004345 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
4346 strlen( test_dhm_params ) ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004347 {
4348 if( verbose != 0 )
4349 printf( "failed\n" );
4350
4351 return( ret );
4352 }
4353
4354 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004357
4358 x509_free( &cacert );
4359 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004360 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00004361#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004362 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004363#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004364
4365 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004366#else
4367 ((void) verbose);
4368 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004370}
4371
4372#endif
4373
4374#endif