blob: b3725a7c271eb1384772ee60d9efeb2118fa2122 [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 )
2269 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2270
2271 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002272 continue;
2273
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002274 // Ignore parse errors
2275 //
Paul Bakker8d914582012-06-04 12:46:42 +00002276 t_ret = x509parse_crtfile( chain, entry_name );
2277 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002278 ret++;
2279 else
2280 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002281 }
2282 closedir( dir );
2283#endif
2284
2285 return( ret );
2286}
2287
Paul Bakkerd98030e2009-05-02 15:13:40 +00002288/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002289 * Load a CSR into the structure
2290 */
2291int x509parse_csrfile( x509_csr *csr, const char *path )
2292{
2293 int ret;
2294 size_t n;
2295 unsigned char *buf;
2296
2297 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2298 return( ret );
2299
2300 ret = x509parse_csr( csr, buf, n );
2301
2302 memset( buf, 0, n + 1 );
2303 polarssl_free( buf );
2304
2305 return( ret );
2306}
2307
2308/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002309 * Load one or more CRLs and add them to the chained list
2310 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002311int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002312{
2313 int ret;
2314 size_t n;
2315 unsigned char *buf;
2316
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002317 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002318 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002319
Paul Bakker27fdf462011-06-09 13:55:13 +00002320 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002321
2322 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002323 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002324
2325 return( ret );
2326}
2327
Paul Bakker5121ce52009-01-03 21:22:43 +00002328/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002329 * Load and parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002330 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002331int x509parse_keyfile( pk_context *ctx,
2332 const char *path, const char *pwd )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002333{
2334 int ret;
2335 size_t n;
2336 unsigned char *buf;
2337
2338 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2339 return( ret );
2340
2341 if( pwd == NULL )
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002342 ret = x509parse_key( ctx, buf, n, NULL, 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002343 else
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002344 ret = x509parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002345 (const unsigned char *) pwd, strlen( pwd ) );
2346
2347 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002348 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002349
2350 return( ret );
2351}
2352
2353/*
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002354 * Load and parse a public key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002355 */
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002356int x509parse_public_keyfile( pk_context *ctx, const char *path )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002357{
2358 int ret;
2359 size_t n;
2360 unsigned char *buf;
2361
2362 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2363 return( ret );
2364
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002365 ret = x509parse_public_key( ctx, buf, n );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002366
2367 memset( buf, 0, n + 1 );
Paul Bakkerd9ca94a2013-07-25 11:25:09 +02002368 polarssl_free( buf );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002369
2370 return( ret );
2371}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002372
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002373#if defined(POLARSSL_RSA_C)
2374/*
2375 * Load and parse a private RSA key
2376 */
2377int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2378{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002379 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002380 pk_context pk;
2381
2382 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002383
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002384 ret = x509parse_keyfile( &pk, path, pwd );
2385
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002386 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2387 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2388
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002389 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002390 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002391 else
2392 rsa_free( rsa );
2393
2394 pk_free( &pk );
2395
2396 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002397}
2398
2399/*
2400 * Load and parse a public RSA key
2401 */
2402int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2403{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002404 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002405 pk_context pk;
2406
2407 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002408
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002409 ret = x509parse_public_keyfile( &pk, path );
2410
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002411 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2412 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2413
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002414 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002415 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002416 else
2417 rsa_free( rsa );
2418
2419 pk_free( &pk );
2420
2421 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002422}
2423#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002424#endif /* POLARSSL_FS_IO */
2425
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002426#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002427/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002428 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002430static int x509parse_key_pkcs1_der( rsa_context *rsa,
2431 const unsigned char *key,
2432 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002433{
Paul Bakker23986e52011-04-24 08:57:21 +00002434 int ret;
2435 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002437
Paul Bakker96743fc2011-02-12 14:30:57 +00002438 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002439 end = p + keylen;
2440
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002442 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002443 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 * RSAPrivateKey ::= SEQUENCE {
2445 * version Version,
2446 * modulus INTEGER, -- n
2447 * publicExponent INTEGER, -- e
2448 * privateExponent INTEGER, -- d
2449 * prime1 INTEGER, -- p
2450 * prime2 INTEGER, -- q
2451 * exponent1 INTEGER, -- d mod (p-1)
2452 * exponent2 INTEGER, -- d mod (q-1)
2453 * coefficient INTEGER, -- (inverse of q) mod p
2454 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2455 * }
2456 */
2457 if( ( ret = asn1_get_tag( &p, end, &len,
2458 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2459 {
Paul Bakker9d781402011-05-09 16:17:09 +00002460 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462
2463 end = p + len;
2464
2465 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2466 {
Paul Bakker9d781402011-05-09 16:17:09 +00002467 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
2469
2470 if( rsa->ver != 0 )
2471 {
Manuel Pégourié-Gonnarde3663422013-07-03 18:56:37 +02002472 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
2474
2475 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2476 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2477 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2478 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2479 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2480 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2481 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2482 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2483 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002485 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
2487
2488 rsa->len = mpi_size( &rsa->N );
2489
2490 if( p != end )
2491 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002492 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002493 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496
2497 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2498 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 rsa_free( rsa );
2500 return( ret );
2501 }
2502
Paul Bakkere2f50402013-06-24 19:00:59 +02002503 return( 0 );
2504}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002505#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002506
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002507#if defined(POLARSSL_ECP_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002508/*
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002509 * Parse a SEC1 encoded private EC key
Paul Bakkere2f50402013-06-24 19:00:59 +02002510 */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002511static int x509parse_key_sec1_der( ecp_keypair *eck,
2512 const unsigned char *key,
2513 size_t keylen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002514{
2515 int ret;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002516 int version;
Paul Bakkere2f50402013-06-24 19:00:59 +02002517 size_t len;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002518 x509_buf params;
2519 unsigned char *p = (unsigned char *) key;
2520 unsigned char *end = p + keylen;
2521 unsigned char *end2;
Paul Bakkere2f50402013-06-24 19:00:59 +02002522
2523 /*
Manuel Pégourié-Gonnard6de63e42013-09-12 04:59:34 +02002524 * RFC 5915, or SEC1 Appendix C.4
Paul Bakkere2f50402013-06-24 19:00:59 +02002525 *
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002526 * ECPrivateKey ::= SEQUENCE {
2527 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
2528 * privateKey OCTET STRING,
2529 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
2530 * publicKey [1] BIT STRING OPTIONAL
2531 * }
Paul Bakkere2f50402013-06-24 19:00:59 +02002532 */
2533 if( ( ret = asn1_get_tag( &p, end, &len,
2534 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2535 {
2536 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2537 }
2538
2539 end = p + len;
2540
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002541 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002542 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2543
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002544 if( version != 1 )
2545 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION );
Paul Bakkere2f50402013-06-24 19:00:59 +02002546
Paul Bakkere2f50402013-06-24 19:00:59 +02002547 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2548 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2549
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002550 if( ( ret = mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002551 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002552 ecp_keypair_free( eck );
2553 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2554 }
2555
2556 p += len;
2557
2558 /*
2559 * Is 'parameters' present?
2560 */
2561 if( ( ret = asn1_get_tag( &p, end, &len,
2562 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
2563 {
2564 if( ( ret = x509_get_ecparams( &p, p + len, &params) ) != 0 ||
2565 ( ret = x509_use_ecparams( &params, &eck->grp ) ) != 0 )
2566 {
2567 ecp_keypair_free( eck );
2568 return( ret );
2569 }
2570 }
2571 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2572 {
2573 ecp_keypair_free( eck );
2574 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2575 }
2576
2577 /*
2578 * Is 'publickey' present?
2579 */
2580 if( ( ret = asn1_get_tag( &p, end, &len,
2581 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
2582 {
2583 end2 = p + len;
2584
2585 if( ( ret = asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
2586 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2587
2588 if( p + len != end2 )
2589 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2590 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2591
2592 if( ( ret = x509_get_ecpubkey( &p, end2, eck ) ) != 0 )
2593 return( ret );
2594 }
2595 else if ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2596 {
2597 ecp_keypair_free( eck );
2598 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2599 }
2600
2601 if( ( ret = ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
2602 {
2603 ecp_keypair_free( eck );
2604 return( ret );
2605 }
2606
2607 return 0;
2608}
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002609#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002610
2611/*
2612 * Parse an unencrypted PKCS#8 encoded private key
2613 */
2614static int x509parse_key_pkcs8_unencrypted_der(
2615 pk_context *pk,
2616 const unsigned char* key,
2617 size_t keylen )
2618{
2619 int ret, version;
2620 size_t len;
2621 x509_buf params;
2622 unsigned char *p = (unsigned char *) key;
2623 unsigned char *end = p + keylen;
2624 pk_type_t pk_alg = POLARSSL_PK_NONE;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002625 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002626
2627 /*
2628 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
2629 *
2630 * PrivateKeyInfo ::= SEQUENCE {
2631 * version Version,
2632 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
2633 * privateKey PrivateKey,
2634 * attributes [0] IMPLICIT Attributes OPTIONAL }
2635 *
2636 * Version ::= INTEGER
2637 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
2638 * PrivateKey ::= OCTET STRING
2639 *
2640 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
2641 */
2642
2643 if( ( ret = asn1_get_tag( &p, end, &len,
2644 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2645 {
2646 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002647 }
2648
2649 end = p + len;
2650
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002651 if( ( ret = asn1_get_int( &p, end, &version ) ) != 0 )
2652 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2653
2654 if( version != 0 )
2655 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2656
2657 if( ( ret = x509_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
2658 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2659
2660 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2661 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2662
2663 if( len < 1 )
2664 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2665 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2666
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002667 if( ( pk_info = pk_info_from_type( pk_alg ) ) == NULL )
2668 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2669
2670 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002671 return( ret );
2672
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002673#if defined(POLARSSL_RSA_C)
2674 if( pk_alg == POLARSSL_PK_RSA )
2675 {
2676 if( ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), p, len ) ) != 0 )
2677 {
2678 pk_free( pk );
2679 return( ret );
2680 }
2681 } else
2682#endif /* POLARSSL_RSA_C */
2683#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002684 if( pk_alg == POLARSSL_PK_ECKEY || pk_alg == POLARSSL_PK_ECKEY_DH )
2685 {
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002686 if( ( ret = x509_use_ecparams( &params, &pk_ec( *pk )->grp ) ) != 0 ||
2687 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), p, len ) ) != 0 )
2688 {
2689 pk_free( pk );
2690 return( ret );
2691 }
2692 } else
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002693#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard6e882022013-07-11 14:55:43 +02002694 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2695
2696 return 0;
2697}
2698
2699/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002700 * Parse an encrypted PKCS#8 encoded private key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002701 */
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002702static int x509parse_key_pkcs8_encrypted_der(
2703 pk_context *pk,
Manuel Pégourié-Gonnarda5d99742013-07-04 11:08:31 +02002704 const unsigned char *key, size_t keylen,
2705 const unsigned char *pwd, size_t pwdlen )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002706{
2707 int ret;
2708 size_t len;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002709 unsigned char buf[2048];
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002710 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002711 x509_buf pbe_alg_oid, pbe_params;
Paul Bakker7749a222013-06-28 17:28:20 +02002712#if defined(POLARSSL_PKCS12_C)
2713 cipher_type_t cipher_alg;
2714 md_type_t md_alg;
2715#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002716
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002717 memset( buf, 0, sizeof( buf ) );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002718
2719 p = (unsigned char *) key;
2720 end = p + keylen;
2721
Paul Bakker28144de2013-06-24 19:28:55 +02002722 if( pwdlen == 0 )
2723 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2724
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002725 /*
2726 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2727 *
2728 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2729 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2730 * encryptedData EncryptedData
2731 * }
2732 *
2733 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2734 *
2735 * EncryptedData ::= OCTET STRING
2736 *
2737 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2738 */
2739 if( ( ret = asn1_get_tag( &p, end, &len,
2740 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2741 {
2742 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2743 }
2744
2745 end = p + len;
2746
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002747 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002748 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002749
2750 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2751 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2752
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002753 if( len > sizeof( buf ) )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002754 return( POLARSSL_ERR_X509_INVALID_INPUT );
2755
2756 /*
2757 * Decrypt EncryptedData with appropriate PDE
2758 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002759#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002760 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002761 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002762 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002763 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002764 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002765 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002766 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2767 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2768
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002769 return( ret );
2770 }
2771 }
2772 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2773 {
2774 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2775 PKCS12_PBE_DECRYPT,
2776 pwd, pwdlen,
2777 p, len, buf ) ) != 0 )
2778 {
2779 return( ret );
2780 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002781
2782 // Best guess for password mismatch when using RC4. If first tag is
2783 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2784 //
2785 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2786 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002787 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002788 else
2789#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002790#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002791 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002792 {
2793 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2794 p, len, buf ) ) != 0 )
2795 {
2796 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2797 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2798
2799 return( ret );
2800 }
2801 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002802 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002803#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002804 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2805
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002806 return( x509parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
Manuel Pégourié-Gonnard416fa8f2013-07-04 10:46:23 +02002807}
2808
2809/*
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002810 * Parse a private key
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002811 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002812int x509parse_key( pk_context *pk,
2813 const unsigned char *key, size_t keylen,
2814 const unsigned char *pwd, size_t pwdlen )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002815{
2816 int ret;
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002817 const pk_info_t *pk_info;
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002818
2819#if defined(POLARSSL_PEM_C)
2820 size_t len;
2821 pem_context pem;
2822
2823 pem_init( &pem );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002824
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002825#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002826 ret = pem_read_buffer( &pem,
2827 "-----BEGIN RSA PRIVATE KEY-----",
2828 "-----END RSA PRIVATE KEY-----",
2829 key, pwd, pwdlen, &len );
2830 if( ret == 0 )
2831 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002832 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2833 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2834
2835 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002836 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ),
2837 pem.buf, pem.buflen ) ) != 0 )
2838 {
2839 pk_free( pk );
2840 }
2841
2842 pem_free( &pem );
2843 return( ret );
2844 }
2845 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2846 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2847 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2848 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2849 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2850 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002851#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002852
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002853#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002854 ret = pem_read_buffer( &pem,
2855 "-----BEGIN EC PRIVATE KEY-----",
2856 "-----END EC PRIVATE KEY-----",
2857 key, pwd, pwdlen, &len );
2858 if( ret == 0 )
2859 {
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002860 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2861 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2862
2863 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002864 ( ret = x509parse_key_sec1_der( pk_ec( *pk ),
2865 pem.buf, pem.buflen ) ) != 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002866 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002867 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002868 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002869
2870 pem_free( &pem );
2871 return( ret );
2872 }
2873 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2874 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2875 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2876 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2877 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2878 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002879#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002880
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002881 ret = pem_read_buffer( &pem,
2882 "-----BEGIN PRIVATE KEY-----",
2883 "-----END PRIVATE KEY-----",
2884 key, NULL, 0, &len );
2885 if( ret == 0 )
2886 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002887 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002888 pem.buf, pem.buflen ) ) != 0 )
2889 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002890 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002891 }
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002892
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002893 pem_free( &pem );
2894 return( ret );
2895 }
2896 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2897 return( ret );
2898
2899 ret = pem_read_buffer( &pem,
2900 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2901 "-----END ENCRYPTED PRIVATE KEY-----",
2902 key, NULL, 0, &len );
2903 if( ret == 0 )
2904 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002905 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk,
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002906 pem.buf, pem.buflen,
2907 pwd, pwdlen ) ) != 0 )
2908 {
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002909 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002910 }
2911
2912 pem_free( &pem );
2913 return( ret );
2914 }
2915 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2916 return( ret );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002917#else
2918 ((void) pwd);
2919 ((void) pwdlen);
2920#endif /* POLARSSL_PEM_C */
2921
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002922 /*
2923 * At this point we only know it's not a PEM formatted key. Could be any
2924 * of the known DER encoded private key formats
2925 *
2926 * We try the different DER format parsers to see if one passes without
2927 * error
2928 */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002929 if( ( ret = x509parse_key_pkcs8_encrypted_der( pk, key, keylen,
2930 pwd, pwdlen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002931 {
2932 return( 0 );
2933 }
2934
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002935 pk_free( pk );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002936
2937 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2938 {
2939 return( ret );
2940 }
2941
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002942 if( ( ret = x509parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002943 return( 0 );
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002944
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002945 pk_free( pk );
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002946
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002947#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002948 if( ( pk_info = pk_info_from_type( POLARSSL_PK_RSA ) ) == NULL )
2949 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2950
2951 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002952 ( ret = x509parse_key_pkcs1_der( pk_rsa( *pk ), key, keylen ) ) == 0 )
2953 {
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002954 return( 0 );
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002955 }
Manuel Pégourié-Gonnard15e8b822013-07-03 11:56:37 +02002956
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002957 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002958#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002959
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002960#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002961 if( ( pk_info = pk_info_from_type( POLARSSL_PK_ECKEY ) ) == NULL )
2962 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2963
2964 if( ( ret = pk_init_ctx( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard8b863cd2013-07-11 15:32:03 +02002965 ( ret = x509parse_key_sec1_der( pk_ec( *pk ), key, keylen ) ) == 0 )
2966 {
2967 return( 0 );
2968 }
2969
2970 pk_free( pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002971#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard26833c22013-06-27 11:27:58 +02002972
2973 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2974}
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002975
2976/*
2977 * Parse a public key
2978 */
2979int x509parse_public_key( pk_context *ctx,
2980 const unsigned char *key, size_t keylen )
2981{
2982 int ret;
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002983 unsigned char *p;
2984#if defined(POLARSSL_PEM_C)
2985 size_t len;
2986 pem_context pem;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02002987
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02002988 pem_init( &pem );
2989 ret = pem_read_buffer( &pem,
2990 "-----BEGIN PUBLIC KEY-----",
2991 "-----END PUBLIC KEY-----",
2992 key, NULL, 0, &len );
2993
2994 if( ret == 0 )
2995 {
2996 /*
2997 * Was PEM encoded
2998 */
2999 key = pem.buf;
3000 keylen = pem.buflen;
3001 }
3002 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
3003 {
3004 pem_free( &pem );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003005 return( ret );
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003006 }
3007#endif
3008 p = (unsigned char *) key;
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003009
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003010 ret = x509_get_pubkey( &p, p + keylen, ctx );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003011
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003012#if defined(POLARSSL_PEM_C)
3013 pem_free( &pem );
3014#endif
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +02003015
Manuel Pégourié-Gonnard4fa04762013-07-09 13:10:49 +02003016 return( ret );
Manuel Pégourié-Gonnard88380992013-07-04 14:09:57 +02003017}
3018
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003019#if defined(POLARSSL_RSA_C)
3020/*
3021 * Parse a private RSA key
3022 */
3023int x509parse_key_rsa( rsa_context *rsa,
3024 const unsigned char *key, size_t keylen,
3025 const unsigned char *pwd, size_t pwdlen )
3026{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003027 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003028 pk_context pk;
3029
3030 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003031
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003032 ret = x509parse_key( &pk, key, keylen, pwd, pwdlen );
3033
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003034 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3035 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3036
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003037 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003038 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003039 else
3040 rsa_free( rsa );
3041
3042 pk_free( &pk );
3043
3044 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003045}
3046
3047/*
3048 * Parse a public RSA key
3049 */
3050int x509parse_public_key_rsa( rsa_context *rsa,
3051 const unsigned char *key, size_t keylen )
3052{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003053 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003054 pk_context pk;
3055
3056 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003057
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003058 ret = x509parse_public_key( &pk, key, keylen );
3059
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02003060 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
3061 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
3062
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003063 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003064 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02003065 else
3066 rsa_free( rsa );
3067
3068 pk_free( &pk );
3069
3070 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02003071}
3072#endif /* POLARSSL_RSA_C */
3073
Paul Bakkereaa89f82011-04-04 21:36:15 +00003074#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00003075/*
Paul Bakker1b57b062011-01-06 15:48:19 +00003076 * Parse DHM parameters
3077 */
Paul Bakker23986e52011-04-24 08:57:21 +00003078int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00003079{
Paul Bakker23986e52011-04-24 08:57:21 +00003080 int ret;
3081 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00003082 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00003083#if defined(POLARSSL_PEM_C)
3084 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00003085
Paul Bakker96743fc2011-02-12 14:30:57 +00003086 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00003087
Paul Bakker96743fc2011-02-12 14:30:57 +00003088 ret = pem_read_buffer( &pem,
3089 "-----BEGIN DH PARAMETERS-----",
3090 "-----END DH PARAMETERS-----",
3091 dhmin, NULL, 0, &dhminlen );
3092
3093 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003094 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003095 /*
3096 * Was PEM encoded
3097 */
3098 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00003099 }
Paul Bakker00b28602013-06-24 13:02:41 +02003100 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00003101 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003102 pem_free( &pem );
3103 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003104 }
3105
Paul Bakker96743fc2011-02-12 14:30:57 +00003106 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
3107#else
3108 p = (unsigned char *) dhmin;
3109#endif
3110 end = p + dhminlen;
3111
Paul Bakker1b57b062011-01-06 15:48:19 +00003112 memset( dhm, 0, sizeof( dhm_context ) );
3113
Paul Bakker1b57b062011-01-06 15:48:19 +00003114 /*
3115 * DHParams ::= SEQUENCE {
3116 * prime INTEGER, -- P
3117 * generator INTEGER, -- g
3118 * }
3119 */
3120 if( ( ret = asn1_get_tag( &p, end, &len,
3121 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
3122 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003123#if defined(POLARSSL_PEM_C)
3124 pem_free( &pem );
3125#endif
Paul Bakker9d781402011-05-09 16:17:09 +00003126 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003127 }
3128
3129 end = p + len;
3130
3131 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
3132 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
3133 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003134#if defined(POLARSSL_PEM_C)
3135 pem_free( &pem );
3136#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003137 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003138 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003139 }
3140
3141 if( p != end )
3142 {
Paul Bakker96743fc2011-02-12 14:30:57 +00003143#if defined(POLARSSL_PEM_C)
3144 pem_free( &pem );
3145#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003146 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00003147 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00003148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
3149 }
3150
Paul Bakker96743fc2011-02-12 14:30:57 +00003151#if defined(POLARSSL_PEM_C)
3152 pem_free( &pem );
3153#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00003154
3155 return( 0 );
3156}
3157
Paul Bakker335db3f2011-04-25 15:28:35 +00003158#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00003159/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02003160 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00003161 */
3162int x509parse_dhmfile( dhm_context *dhm, const char *path )
3163{
3164 int ret;
3165 size_t n;
3166 unsigned char *buf;
3167
Paul Bakker69e095c2011-12-10 21:55:01 +00003168 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
3169 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00003170
Paul Bakker27fdf462011-06-09 13:55:13 +00003171 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00003172
3173 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02003174 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00003175
3176 return( ret );
3177}
Paul Bakker335db3f2011-04-25 15:28:35 +00003178#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00003179#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003180
Paul Bakker5121ce52009-01-03 21:22:43 +00003181#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00003182#include <stdarg.h>
3183
3184#if !defined vsnprintf
3185#define vsnprintf _vsnprintf
3186#endif // vsnprintf
3187
3188/*
3189 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
3190 * Result value is not size of buffer needed, but -1 if no fit is possible.
3191 *
3192 * This fuction tries to 'fix' this by at least suggesting enlarging the
3193 * size by 20.
3194 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003195static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00003196{
3197 va_list ap;
3198 int res = -1;
3199
3200 va_start( ap, format );
3201
3202 res = vsnprintf( str, size, format, ap );
3203
3204 va_end( ap );
3205
3206 // No quick fix possible
3207 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00003208 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003209
3210 return res;
3211}
3212
3213#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00003214#endif
3215
Paul Bakkerd98030e2009-05-02 15:13:40 +00003216#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
3217
3218#define SAFE_SNPRINTF() \
3219{ \
3220 if( ret == -1 ) \
3221 return( -1 ); \
3222 \
Paul Bakker23986e52011-04-24 08:57:21 +00003223 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003224 p[n - 1] = '\0'; \
3225 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
3226 } \
3227 \
Paul Bakker23986e52011-04-24 08:57:21 +00003228 n -= (unsigned int) ret; \
3229 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00003230}
3231
Paul Bakker5121ce52009-01-03 21:22:43 +00003232/*
3233 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00003234 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003236int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003237{
Paul Bakker23986e52011-04-24 08:57:21 +00003238 int ret;
3239 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003240 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003241 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003242 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 char s[128], *p;
3244
3245 memset( s, 0, sizeof( s ) );
3246
3247 name = dn;
3248 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
3251 while( name != NULL )
3252 {
Paul Bakkercefb3962012-06-27 11:51:09 +00003253 if( !name->oid.p )
3254 {
3255 name = name->next;
3256 continue;
3257 }
3258
Paul Bakker74111d32011-01-15 16:57:55 +00003259 if( name != dn )
3260 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00003261 ret = snprintf( p, n, ", " );
3262 SAFE_SNPRINTF();
3263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
Paul Bakkerc70b9822013-04-07 22:00:46 +02003265 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266
Paul Bakkerc70b9822013-04-07 22:00:46 +02003267 if( ret == 0 )
3268 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00003270 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003271 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
3273 for( i = 0; i < name->val.len; i++ )
3274 {
Paul Bakker27fdf462011-06-09 13:55:13 +00003275 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 break;
3277
3278 c = name->val.p[i];
3279 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
3280 s[i] = '?';
3281 else s[i] = c;
3282 }
3283 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00003284 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003285 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 name = name->next;
3287 }
3288
Paul Bakker23986e52011-04-24 08:57:21 +00003289 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290}
3291
3292/*
Paul Bakkerdd476992011-01-16 21:34:59 +00003293 * Store the serial in printable form into buf; no more
3294 * than size characters will be written
3295 */
3296int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
3297{
Paul Bakker23986e52011-04-24 08:57:21 +00003298 int ret;
3299 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00003300 char *p;
3301
3302 p = buf;
3303 n = size;
3304
3305 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00003306 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00003307
3308 for( i = 0; i < nr; i++ )
3309 {
Paul Bakker93048802011-12-05 14:38:06 +00003310 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003311 continue;
3312
Paul Bakkerdd476992011-01-16 21:34:59 +00003313 ret = snprintf( p, n, "%02X%s",
3314 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
3315 SAFE_SNPRINTF();
3316 }
3317
Paul Bakker03c7c252011-11-25 12:37:37 +00003318 if( nr != serial->len )
3319 {
3320 ret = snprintf( p, n, "...." );
3321 SAFE_SNPRINTF();
3322 }
3323
Paul Bakker23986e52011-04-24 08:57:21 +00003324 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00003325}
3326
3327/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003328 * Helper for writing "RSA key size", "EC key size", etc
3329 */
3330static int x509_key_size_helper( char *buf, size_t size, const char *name )
3331{
3332 char *p = buf;
3333 size_t n = size;
3334 int ret;
3335
3336 if( strlen( name ) + sizeof( " key size" ) > size )
3337 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
3338
3339 ret = snprintf( p, n, "%s key size", name );
3340 SAFE_SNPRINTF();
3341
3342 return( 0 );
3343}
3344
3345/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00003346 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003348#define BEFORE_COLON 14
3349#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00003350int x509parse_cert_info( char *buf, size_t size, const char *prefix,
3351 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352{
Paul Bakker23986e52011-04-24 08:57:21 +00003353 int ret;
3354 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003355 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003356 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003357 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00003358
3359 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003360 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
Paul Bakkerd98030e2009-05-02 15:13:40 +00003362 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003364 SAFE_SNPRINTF();
3365 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003367 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Paul Bakkerdd476992011-01-16 21:34:59 +00003369 ret = x509parse_serial_gets( p, n, &crt->serial);
3370 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003371
Paul Bakkerd98030e2009-05-02 15:13:40 +00003372 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3373 SAFE_SNPRINTF();
3374 ret = x509parse_dn_gets( p, n, &crt->issuer );
3375 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
Paul Bakkerd98030e2009-05-02 15:13:40 +00003377 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3378 SAFE_SNPRINTF();
3379 ret = x509parse_dn_gets( p, n, &crt->subject );
3380 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Paul Bakkerd98030e2009-05-02 15:13:40 +00003382 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3384 crt->valid_from.year, crt->valid_from.mon,
3385 crt->valid_from.day, crt->valid_from.hour,
3386 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003387 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
Paul Bakkerd98030e2009-05-02 15:13:40 +00003389 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3391 crt->valid_to.year, crt->valid_to.mon,
3392 crt->valid_to.day, crt->valid_to.hour,
3393 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003394 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
Paul Bakkerc70b9822013-04-07 22:00:46 +02003396 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
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 = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
3400 if( ret != 0 )
3401 ret = snprintf( p, n, "???" );
3402 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003403 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003404 SAFE_SNPRINTF();
3405
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003406 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02003407 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02003408 {
3409 return( ret );
3410 }
3411
3412 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003413 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003414 SAFE_SNPRINTF();
3415
Paul Bakker23986e52011-04-24 08:57:21 +00003416 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003417}
3418
Paul Bakker74111d32011-01-15 16:57:55 +00003419/*
3420 * Return an informational string describing the given OID
3421 */
3422const char *x509_oid_get_description( x509_buf *oid )
3423{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003424 const char *desc = NULL;
3425 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00003426
Paul Bakkerc70b9822013-04-07 22:00:46 +02003427 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003428
Paul Bakkerc70b9822013-04-07 22:00:46 +02003429 if( ret != 0 )
3430 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00003431
Paul Bakkerc70b9822013-04-07 22:00:46 +02003432 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00003433}
3434
3435/* Return the x.y.z.... style numeric string for the given OID */
3436int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3437{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003438 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00003439}
3440
Paul Bakkerd98030e2009-05-02 15:13:40 +00003441/*
3442 * Return an informational string about the CRL.
3443 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003444int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3445 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003446{
Paul Bakker23986e52011-04-24 08:57:21 +00003447 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003448 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003449 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003450 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003451 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003452
3453 p = buf;
3454 n = size;
3455
3456 ret = snprintf( p, n, "%sCRL version : %d",
3457 prefix, crl->version );
3458 SAFE_SNPRINTF();
3459
3460 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3461 SAFE_SNPRINTF();
3462 ret = x509parse_dn_gets( p, n, &crl->issuer );
3463 SAFE_SNPRINTF();
3464
3465 ret = snprintf( p, n, "\n%sthis update : " \
3466 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3467 crl->this_update.year, crl->this_update.mon,
3468 crl->this_update.day, crl->this_update.hour,
3469 crl->this_update.min, crl->this_update.sec );
3470 SAFE_SNPRINTF();
3471
3472 ret = snprintf( p, n, "\n%snext update : " \
3473 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3474 crl->next_update.year, crl->next_update.mon,
3475 crl->next_update.day, crl->next_update.hour,
3476 crl->next_update.min, crl->next_update.sec );
3477 SAFE_SNPRINTF();
3478
3479 entry = &crl->entry;
3480
3481 ret = snprintf( p, n, "\n%sRevoked certificates:",
3482 prefix );
3483 SAFE_SNPRINTF();
3484
Paul Bakker9be19372009-07-27 20:21:53 +00003485 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003486 {
3487 ret = snprintf( p, n, "\n%sserial number: ",
3488 prefix );
3489 SAFE_SNPRINTF();
3490
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003491 ret = x509parse_serial_gets( p, n, &entry->serial);
3492 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003493
Paul Bakkerd98030e2009-05-02 15:13:40 +00003494 ret = snprintf( p, n, " revocation date: " \
3495 "%04d-%02d-%02d %02d:%02d:%02d",
3496 entry->revocation_date.year, entry->revocation_date.mon,
3497 entry->revocation_date.day, entry->revocation_date.hour,
3498 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003499 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003500
3501 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 }
3503
Paul Bakkerc70b9822013-04-07 22:00:46 +02003504 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003505 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003506
Paul Bakkerc70b9822013-04-07 22:00:46 +02003507 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
3508 if( ret != 0 )
3509 ret = snprintf( p, n, "???" );
3510 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02003511 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003512 SAFE_SNPRINTF();
3513
Paul Bakker1e27bb22009-07-19 20:25:25 +00003514 ret = snprintf( p, n, "\n" );
3515 SAFE_SNPRINTF();
3516
Paul Bakker23986e52011-04-24 08:57:21 +00003517 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518}
3519
3520/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003521 * Return an informational string about the CSR.
3522 */
3523int x509parse_csr_info( char *buf, size_t size, const char *prefix,
3524 const x509_csr *csr )
3525{
3526 int ret;
3527 size_t n;
3528 char *p;
3529 const char *desc;
3530 char key_size_str[BEFORE_COLON];
3531
3532 p = buf;
3533 n = size;
3534
3535 ret = snprintf( p, n, "%sCSR version : %d",
3536 prefix, csr->version );
3537 SAFE_SNPRINTF();
3538
3539 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
3540 SAFE_SNPRINTF();
3541 ret = x509parse_dn_gets( p, n, &csr->subject );
3542 SAFE_SNPRINTF();
3543
3544 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
3545 SAFE_SNPRINTF();
3546
3547 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
3548 if( ret != 0 )
3549 ret = snprintf( p, n, "???" );
3550 else
3551 ret = snprintf( p, n, "%s", desc );
3552 SAFE_SNPRINTF();
3553
3554 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
3555 pk_get_name( &csr->pk ) ) ) != 0 )
3556 {
3557 return( ret );
3558 }
3559
3560 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
3561 (int) pk_get_size( &csr->pk ) );
3562 SAFE_SNPRINTF();
3563
3564 return( (int) ( size - n ) );
3565}
3566
3567/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003568 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003570#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003571int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572{
Paul Bakkercce9d772011-11-18 14:26:47 +00003573 int year, mon, day;
3574 int hour, min, sec;
3575
3576#if defined(_WIN32)
3577 SYSTEMTIME st;
3578
3579 GetLocalTime(&st);
3580
3581 year = st.wYear;
3582 mon = st.wMonth;
3583 day = st.wDay;
3584 hour = st.wHour;
3585 min = st.wMinute;
3586 sec = st.wSecond;
3587#else
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 struct tm *lt;
3589 time_t tt;
3590
3591 tt = time( NULL );
3592 lt = localtime( &tt );
3593
Paul Bakkercce9d772011-11-18 14:26:47 +00003594 year = lt->tm_year + 1900;
3595 mon = lt->tm_mon + 1;
3596 day = lt->tm_mday;
3597 hour = lt->tm_hour;
3598 min = lt->tm_min;
3599 sec = lt->tm_sec;
3600#endif
3601
3602 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003603 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604
Paul Bakkercce9d772011-11-18 14:26:47 +00003605 if( year == to->year &&
3606 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003607 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Paul Bakkercce9d772011-11-18 14:26:47 +00003609 if( year == to->year &&
3610 mon == to->mon &&
3611 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003612 return( 1 );
3613
Paul Bakkercce9d772011-11-18 14:26:47 +00003614 if( year == to->year &&
3615 mon == to->mon &&
3616 day == to->day &&
3617 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003618 return( 1 );
3619
Paul Bakkercce9d772011-11-18 14:26:47 +00003620 if( year == to->year &&
3621 mon == to->mon &&
3622 day == to->day &&
3623 hour == to->hour &&
3624 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003625 return( 1 );
3626
Paul Bakkercce9d772011-11-18 14:26:47 +00003627 if( year == to->year &&
3628 mon == to->mon &&
3629 day == to->day &&
3630 hour == to->hour &&
3631 min == to->min &&
3632 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003633 return( 1 );
3634
Paul Bakker40ea7de2009-05-03 10:18:48 +00003635 return( 0 );
3636}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02003637#else /* POLARSSL_HAVE_TIME */
3638int x509parse_time_expired( const x509_time *to )
3639{
3640 ((void) to);
3641 return( 0 );
3642}
3643#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00003644
3645/*
3646 * Return 1 if the certificate is revoked, or 0 otherwise.
3647 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003648int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003649{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003650 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003651
3652 while( cur != NULL && cur->serial.len != 0 )
3653 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003654 if( crt->serial.len == cur->serial.len &&
3655 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003656 {
3657 if( x509parse_time_expired( &cur->revocation_date ) )
3658 return( 1 );
3659 }
3660
3661 cur = cur->next;
3662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003663
3664 return( 0 );
3665}
3666
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003667/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003668 * Check that the given certificate is valid accoring to the CRL.
3669 */
3670static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3671 x509_crl *crl_list)
3672{
3673 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003674 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3675 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003676
Paul Bakker915275b2012-09-28 07:10:55 +00003677 if( ca == NULL )
3678 return( flags );
3679
Paul Bakker76fd75a2011-01-16 21:12:10 +00003680 /*
3681 * TODO: What happens if no CRL is present?
3682 * Suggestion: Revocation state should be unknown if no CRL is present.
3683 * For backwards compatibility this is not yet implemented.
3684 */
3685
Paul Bakker915275b2012-09-28 07:10:55 +00003686 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003687 {
Paul Bakker915275b2012-09-28 07:10:55 +00003688 if( crl_list->version == 0 ||
3689 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003690 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3691 crl_list->issuer_raw.len ) != 0 )
3692 {
3693 crl_list = crl_list->next;
3694 continue;
3695 }
3696
3697 /*
3698 * Check if CRL is correctly signed by the trusted CA
3699 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003700 md_info = md_info_from_type( crl_list->sig_md );
3701 if( md_info == NULL )
3702 {
3703 /*
3704 * Cannot check 'unknown' hash
3705 */
3706 flags |= BADCRL_NOT_TRUSTED;
3707 break;
3708 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003709
Paul Bakkerc70b9822013-04-07 22:00:46 +02003710 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003711
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003712 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003713 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003714 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003715 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003716 flags |= BADCRL_NOT_TRUSTED;
3717 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003718 }
3719
3720 /*
3721 * Check for validity of CRL (Do not drop out)
3722 */
3723 if( x509parse_time_expired( &crl_list->next_update ) )
3724 flags |= BADCRL_EXPIRED;
3725
3726 /*
3727 * Check if certificate is revoked
3728 */
3729 if( x509parse_revoked(crt, crl_list) )
3730 {
3731 flags |= BADCERT_REVOKED;
3732 break;
3733 }
3734
3735 crl_list = crl_list->next;
3736 }
3737 return flags;
3738}
3739
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003740static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003741{
3742 size_t i;
3743 size_t cn_idx = 0;
3744
Paul Bakker57b12982012-02-11 17:38:38 +00003745 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003746 return( 0 );
3747
3748 for( i = 0; i < strlen( cn ); ++i )
3749 {
3750 if( cn[i] == '.' )
3751 {
3752 cn_idx = i;
3753 break;
3754 }
3755 }
3756
3757 if( cn_idx == 0 )
3758 return( 0 );
3759
Paul Bakker535e97d2012-08-23 10:49:55 +00003760 if( strlen( cn ) - cn_idx == name->len - 1 &&
3761 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003762 {
3763 return( 1 );
3764 }
3765
3766 return( 0 );
3767}
3768
Paul Bakker915275b2012-09-28 07:10:55 +00003769static int x509parse_verify_top(
3770 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003771 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003772 int (*f_vrfy)(void *, x509_cert *, int, int *),
3773 void *p_vrfy )
3774{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003775 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003776 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003777 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3778 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003779
3780 if( x509parse_time_expired( &child->valid_to ) )
3781 *flags |= BADCERT_EXPIRED;
3782
3783 /*
3784 * Child is the top of the chain. Check against the trust_ca list.
3785 */
3786 *flags |= BADCERT_NOT_TRUSTED;
3787
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003788 md_info = md_info_from_type( child->sig_md );
3789 if( md_info == NULL )
3790 {
3791 /*
3792 * Cannot check 'unknown', no need to try any CA
3793 */
3794 trust_ca = NULL;
3795 }
3796 else
3797 md( md_info, child->tbs.p, child->tbs.len, hash );
3798
Paul Bakker915275b2012-09-28 07:10:55 +00003799 while( trust_ca != NULL )
3800 {
3801 if( trust_ca->version == 0 ||
3802 child->issuer_raw.len != trust_ca->subject_raw.len ||
3803 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3804 child->issuer_raw.len ) != 0 )
3805 {
3806 trust_ca = trust_ca->next;
3807 continue;
3808 }
3809
Paul Bakker9a736322012-11-14 12:39:52 +00003810 /*
3811 * Reduce path_len to check against if top of the chain is
3812 * the same as the trusted CA
3813 */
3814 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3815 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003816 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00003817 {
3818 check_path_cnt--;
3819 }
3820
Paul Bakker915275b2012-09-28 07:10:55 +00003821 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003822 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003823 {
3824 trust_ca = trust_ca->next;
3825 continue;
3826 }
3827
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003828 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003829 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003830 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003831 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003832 trust_ca = trust_ca->next;
3833 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003834 }
3835
3836 /*
3837 * Top of chain is signed by a trusted CA
3838 */
3839 *flags &= ~BADCERT_NOT_TRUSTED;
3840 break;
3841 }
3842
Paul Bakker9a736322012-11-14 12:39:52 +00003843 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003844 * If top of chain is not the same as the trusted CA send a verify request
3845 * to the callback for any issues with validity and CRL presence for the
3846 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003847 */
3848 if( trust_ca != NULL &&
3849 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3850 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3851 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003852 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003853 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003854 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3855
3856 if( x509parse_time_expired( &trust_ca->valid_to ) )
3857 ca_flags |= BADCERT_EXPIRED;
3858
Paul Bakker915275b2012-09-28 07:10:55 +00003859 if( NULL != f_vrfy )
3860 {
Paul Bakker9a736322012-11-14 12:39:52 +00003861 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003862 return( ret );
3863 }
3864 }
3865
3866 /* Call callback on top cert */
3867 if( NULL != f_vrfy )
3868 {
Paul Bakker9a736322012-11-14 12:39:52 +00003869 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003870 return( ret );
3871 }
3872
Paul Bakker915275b2012-09-28 07:10:55 +00003873 *flags |= ca_flags;
3874
3875 return( 0 );
3876}
3877
3878static int x509parse_verify_child(
3879 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003880 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003881 int (*f_vrfy)(void *, x509_cert *, int, int *),
3882 void *p_vrfy )
3883{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003884 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003885 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003886 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003887 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003888 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003889
3890 if( x509parse_time_expired( &child->valid_to ) )
3891 *flags |= BADCERT_EXPIRED;
3892
Paul Bakkerc70b9822013-04-07 22:00:46 +02003893 md_info = md_info_from_type( child->sig_md );
3894 if( md_info == NULL )
3895 {
3896 /*
3897 * Cannot check 'unknown' hash
3898 */
Paul Bakker915275b2012-09-28 07:10:55 +00003899 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003900 }
3901 else
3902 {
3903 md( md_info, child->tbs.p, child->tbs.len, hash );
3904
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003905 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003906 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003907 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003908 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003909 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003910 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003911 }
3912
Paul Bakker915275b2012-09-28 07:10:55 +00003913 /* Check trusted CA's CRL for the given crt */
3914 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3915
3916 grandparent = parent->next;
3917
3918 while( grandparent != NULL )
3919 {
3920 if( grandparent->version == 0 ||
3921 grandparent->ca_istrue == 0 ||
3922 parent->issuer_raw.len != grandparent->subject_raw.len ||
3923 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3924 parent->issuer_raw.len ) != 0 )
3925 {
3926 grandparent = grandparent->next;
3927 continue;
3928 }
3929 break;
3930 }
3931
Paul Bakker915275b2012-09-28 07:10:55 +00003932 if( grandparent != NULL )
3933 {
3934 /*
3935 * Part of the chain
3936 */
Paul Bakker9a736322012-11-14 12:39:52 +00003937 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 +00003938 if( ret != 0 )
3939 return( ret );
3940 }
3941 else
3942 {
Paul Bakker9a736322012-11-14 12:39:52 +00003943 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 +00003944 if( ret != 0 )
3945 return( ret );
3946 }
3947
3948 /* child is verified to be a child of the parent, call verify callback */
3949 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003950 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003951 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003952
3953 *flags |= parent_flags;
3954
3955 return( 0 );
3956}
3957
Paul Bakker76fd75a2011-01-16 21:12:10 +00003958/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 * Verify the certificate validity
3960 */
3961int x509parse_verify( x509_cert *crt,
3962 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003963 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003964 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003965 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003966 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003967{
Paul Bakker23986e52011-04-24 08:57:21 +00003968 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003969 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003970 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003971 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003972 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003973 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Paul Bakker40ea7de2009-05-03 10:18:48 +00003975 *flags = 0;
3976
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 if( cn != NULL )
3978 {
3979 name = &crt->subject;
3980 cn_len = strlen( cn );
3981
Paul Bakker4d2c1242012-05-10 14:12:46 +00003982 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003983 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003984 cur = &crt->subject_alt_names;
3985
3986 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003987 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003988 if( cur->buf.len == cn_len &&
3989 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003990 break;
3991
Paul Bakker535e97d2012-08-23 10:49:55 +00003992 if( cur->buf.len > 2 &&
3993 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003994 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003995 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Paul Bakker4d2c1242012-05-10 14:12:46 +00003997 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003998 }
3999
4000 if( cur == NULL )
4001 *flags |= BADCERT_CN_MISMATCH;
4002 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00004003 else
4004 {
4005 while( name != NULL )
4006 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02004007 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004008 {
Paul Bakker535e97d2012-08-23 10:49:55 +00004009 if( name->val.len == cn_len &&
4010 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00004011 break;
4012
Paul Bakker535e97d2012-08-23 10:49:55 +00004013 if( name->val.len > 2 &&
4014 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00004015 x509_wildcard_verify( cn, &name->val ) )
4016 break;
4017 }
4018
4019 name = name->next;
4020 }
4021
4022 if( name == NULL )
4023 *flags |= BADCERT_CN_MISMATCH;
4024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004025 }
4026
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 /*
Paul Bakker915275b2012-09-28 07:10:55 +00004028 * Iterate upwards in the given cert chain, to find our crt parent.
4029 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00004030 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00004031 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakker76fd75a2011-01-16 21:12:10 +00004033 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004035 if( parent->ca_istrue == 0 ||
4036 crt->issuer_raw.len != parent->subject_raw.len ||
4037 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00004038 crt->issuer_raw.len ) != 0 )
4039 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00004040 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00004041 continue;
4042 }
Paul Bakker915275b2012-09-28 07:10:55 +00004043 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 }
4045
Paul Bakker915275b2012-09-28 07:10:55 +00004046 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004047 {
Paul Bakker915275b2012-09-28 07:10:55 +00004048 /*
4049 * Part of the chain
4050 */
Paul Bakker9a736322012-11-14 12:39:52 +00004051 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004052 if( ret != 0 )
4053 return( ret );
4054 }
4055 else
Paul Bakker74111d32011-01-15 16:57:55 +00004056 {
Paul Bakker9a736322012-11-14 12:39:52 +00004057 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00004058 if( ret != 0 )
4059 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004060 }
Paul Bakker915275b2012-09-28 07:10:55 +00004061
4062 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00004063 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004064
Paul Bakker5121ce52009-01-03 21:22:43 +00004065 return( 0 );
4066}
4067
4068/*
4069 * Unallocate all certificate data
4070 */
4071void x509_free( x509_cert *crt )
4072{
4073 x509_cert *cert_cur = crt;
4074 x509_cert *cert_prv;
4075 x509_name *name_cur;
4076 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00004077 x509_sequence *seq_cur;
4078 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00004079
4080 if( crt == NULL )
4081 return;
4082
4083 do
4084 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02004085 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00004086
4087 name_cur = cert_cur->issuer.next;
4088 while( name_cur != NULL )
4089 {
4090 name_prv = name_cur;
4091 name_cur = name_cur->next;
4092 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004093 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004094 }
4095
4096 name_cur = cert_cur->subject.next;
4097 while( name_cur != NULL )
4098 {
4099 name_prv = name_cur;
4100 name_cur = name_cur->next;
4101 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004102 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004103 }
4104
Paul Bakker74111d32011-01-15 16:57:55 +00004105 seq_cur = cert_cur->ext_key_usage.next;
4106 while( seq_cur != NULL )
4107 {
4108 seq_prv = seq_cur;
4109 seq_cur = seq_cur->next;
4110 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004111 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00004112 }
4113
Paul Bakker8afa70d2012-02-11 18:42:45 +00004114 seq_cur = cert_cur->subject_alt_names.next;
4115 while( seq_cur != NULL )
4116 {
4117 seq_prv = seq_cur;
4118 seq_cur = seq_cur->next;
4119 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004120 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00004121 }
4122
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 if( cert_cur->raw.p != NULL )
4124 {
4125 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004126 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 }
4128
4129 cert_cur = cert_cur->next;
4130 }
4131 while( cert_cur != NULL );
4132
4133 cert_cur = crt;
4134 do
4135 {
4136 cert_prv = cert_cur;
4137 cert_cur = cert_cur->next;
4138
4139 memset( cert_prv, 0, sizeof( x509_cert ) );
4140 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02004141 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00004142 }
4143 while( cert_cur != NULL );
4144}
4145
Paul Bakkerd98030e2009-05-02 15:13:40 +00004146/*
4147 * Unallocate all CRL data
4148 */
4149void x509_crl_free( x509_crl *crl )
4150{
4151 x509_crl *crl_cur = crl;
4152 x509_crl *crl_prv;
4153 x509_name *name_cur;
4154 x509_name *name_prv;
4155 x509_crl_entry *entry_cur;
4156 x509_crl_entry *entry_prv;
4157
4158 if( crl == NULL )
4159 return;
4160
4161 do
4162 {
4163 name_cur = crl_cur->issuer.next;
4164 while( name_cur != NULL )
4165 {
4166 name_prv = name_cur;
4167 name_cur = name_cur->next;
4168 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004169 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004170 }
4171
4172 entry_cur = crl_cur->entry.next;
4173 while( entry_cur != NULL )
4174 {
4175 entry_prv = entry_cur;
4176 entry_cur = entry_cur->next;
4177 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02004178 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004179 }
4180
4181 if( crl_cur->raw.p != NULL )
4182 {
4183 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004184 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004185 }
4186
4187 crl_cur = crl_cur->next;
4188 }
4189 while( crl_cur != NULL );
4190
4191 crl_cur = crl;
4192 do
4193 {
4194 crl_prv = crl_cur;
4195 crl_cur = crl_cur->next;
4196
4197 memset( crl_prv, 0, sizeof( x509_crl ) );
4198 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02004199 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00004200 }
4201 while( crl_cur != NULL );
4202}
4203
Paul Bakkerf9f377e2013-09-09 15:35:10 +02004204/*
4205 * Unallocate all CSR data
4206 */
4207void x509_csr_free( x509_csr *csr )
4208{
4209 x509_name *name_cur;
4210 x509_name *name_prv;
4211
4212 if( csr == NULL )
4213 return;
4214
4215 pk_free( &csr->pk );
4216
4217 name_cur = csr->subject.next;
4218 while( name_cur != NULL )
4219 {
4220 name_prv = name_cur;
4221 name_cur = name_cur->next;
4222 memset( name_prv, 0, sizeof( x509_name ) );
4223 polarssl_free( name_prv );
4224 }
4225
4226 if( csr->raw.p != NULL )
4227 {
4228 memset( csr->raw.p, 0, csr->raw.len );
4229 polarssl_free( csr->raw.p );
4230 }
4231
4232 memset( csr, 0, sizeof( x509_csr ) );
4233}
4234
Paul Bakker40e46942009-01-03 21:51:57 +00004235#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00004236
Paul Bakker40e46942009-01-03 21:51:57 +00004237#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00004238
4239/*
4240 * Checkup routine
4241 */
4242int x509_self_test( int verbose )
4243{
Paul Bakker5690efc2011-05-26 13:16:06 +00004244#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00004245 int ret;
4246 int flags;
4247 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00004248 x509_cert cacert;
4249 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004250 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00004251#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004252 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00004253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004254
4255 if( verbose != 0 )
4256 printf( " X.509 certificate load: " );
4257
4258 memset( &clicert, 0, sizeof( x509_cert ) );
4259
Paul Bakker3c2122f2013-06-24 19:03:14 +02004260 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004261 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262 if( ret != 0 )
4263 {
4264 if( verbose != 0 )
4265 printf( "failed\n" );
4266
4267 return( ret );
4268 }
4269
4270 memset( &cacert, 0, sizeof( x509_cert ) );
4271
Paul Bakker3c2122f2013-06-24 19:03:14 +02004272 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00004273 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 if( ret != 0 )
4275 {
4276 if( verbose != 0 )
4277 printf( "failed\n" );
4278
4279 return( ret );
4280 }
4281
4282 if( verbose != 0 )
4283 printf( "passed\n X.509 private key load: " );
4284
4285 i = strlen( test_ca_key );
4286 j = strlen( test_ca_pwd );
4287
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004288 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00004289
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004290 if( ( ret = x509parse_key( &pkey,
Paul Bakker3c2122f2013-06-24 19:03:14 +02004291 (const unsigned char *) test_ca_key, i,
4292 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 {
4294 if( verbose != 0 )
4295 printf( "failed\n" );
4296
4297 return( ret );
4298 }
4299
4300 if( verbose != 0 )
4301 printf( "passed\n X.509 signature verify: ");
4302
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004303 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304 if( ret != 0 )
4305 {
4306 if( verbose != 0 )
4307 printf( "failed\n" );
4308
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004309 printf("ret = %d, &flags = %04x\n", ret, flags);
4310
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 return( ret );
4312 }
4313
Paul Bakker5690efc2011-05-26 13:16:06 +00004314#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004316 printf( "passed\n X.509 DHM parameter load: " );
4317
4318 i = strlen( test_dhm_params );
4319 j = strlen( test_ca_pwd );
4320
Paul Bakker3c2122f2013-06-24 19:03:14 +02004321 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004322 {
4323 if( verbose != 0 )
4324 printf( "failed\n" );
4325
4326 return( ret );
4327 }
4328
4329 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004330 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00004331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
4333 x509_free( &cacert );
4334 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02004335 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00004336#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00004337 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00004338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
4340 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00004341#else
4342 ((void) verbose);
4343 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
4344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004345}
4346
4347#endif
4348
4349#endif