blob: 7c7ee29fb9a3b5e58061bd5e6f20ff01c6223c9b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
167 */
168static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 x509_buf *alg )
171{
Paul Bakker23986e52011-04-24 08:57:21 +0000172 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200174 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000175 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 return( 0 );
178}
179
180/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * AttributeTypeAndValue ::= SEQUENCE {
182 * type AttributeType,
183 * value AttributeValue }
184 *
185 * AttributeType ::= OBJECT IDENTIFIER
186 *
187 * AttributeValue ::= ANY DEFINED BY AttributeType
188 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000189static int x509_get_attr_type_value( unsigned char **p,
190 const unsigned char *end,
191 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000192{
Paul Bakker23986e52011-04-24 08:57:21 +0000193 int ret;
194 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 x509_buf *oid;
196 x509_buf *val;
197
198 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000200 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 oid = &cur->oid;
203 oid->tag = **p;
204
205 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000206 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 oid->p = *p;
209 *p += oid->len;
210
211 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000213 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
216 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
217 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000219 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 val = &cur->val;
222 val->tag = *(*p)++;
223
224 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000225 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 val->p = *p;
228 *p += val->len;
229
230 cur->next = NULL;
231
Paul Bakker400ff6f2011-02-20 10:40:16 +0000232 return( 0 );
233}
234
235/*
236 * RelativeDistinguishedName ::=
237 * SET OF AttributeTypeAndValue
238 *
239 * AttributeTypeAndValue ::= SEQUENCE {
240 * type AttributeType,
241 * value AttributeValue }
242 *
243 * AttributeType ::= OBJECT IDENTIFIER
244 *
245 * AttributeValue ::= ANY DEFINED BY AttributeType
246 */
247static int x509_get_name( unsigned char **p,
248 const unsigned char *end,
249 x509_name *cur )
250{
Paul Bakker23986e52011-04-24 08:57:21 +0000251 int ret;
252 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000253 const unsigned char *end2;
254 x509_name *use;
255
256 if( ( ret = asn1_get_tag( p, end, &len,
257 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000258 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000259
260 end2 = end;
261 end = *p + len;
262 use = cur;
263
264 do
265 {
266 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
267 return( ret );
268
269 if( *p != end )
270 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200271 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000272 sizeof( x509_name ) );
273
274 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000275 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000276
277 memset( use->next, 0, sizeof( x509_name ) );
278
279 use = use->next;
280 }
281 }
282 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 /*
285 * recurse until end of SEQUENCE is reached
286 */
287 if( *p == end2 )
288 return( 0 );
289
Paul Bakker6e339b52013-07-03 13:37:05 +0200290 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 sizeof( x509_name ) );
292
293 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000294 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Paul Bakker430ffbe2012-05-01 08:14:20 +0000296 memset( cur->next, 0, sizeof( x509_name ) );
297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 return( x509_get_name( p, end2, cur->next ) );
299}
300
301/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * Time ::= CHOICE {
303 * utcTime UTCTime,
304 * generalTime GeneralizedTime }
305 */
Paul Bakker91200182010-02-18 21:26:15 +0000306static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000308 x509_time *time )
309{
Paul Bakker23986e52011-04-24 08:57:21 +0000310 int ret;
311 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000312 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000313 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000314
Paul Bakker91200182010-02-18 21:26:15 +0000315 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000316 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
317 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318
Paul Bakker91200182010-02-18 21:26:15 +0000319 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320
Paul Bakker91200182010-02-18 21:26:15 +0000321 if ( tag == ASN1_UTC_TIME )
322 {
323 (*p)++;
324 ret = asn1_get_len( p, end, &len );
325
326 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000327 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328
Paul Bakker91200182010-02-18 21:26:15 +0000329 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000330 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
331 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
334 &time->year, &time->mon, &time->day,
335 &time->hour, &time->min, &time->sec ) < 5 )
336 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337
Paul Bakker400ff6f2011-02-20 10:40:16 +0000338 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000339 time->year += 1900;
340
341 *p += len;
342
343 return( 0 );
344 }
345 else if ( tag == ASN1_GENERALIZED_TIME )
346 {
347 (*p)++;
348 ret = asn1_get_len( p, end, &len );
349
350 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000352
353 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000354 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
355 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000356
357 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
358 &time->year, &time->mon, &time->day,
359 &time->hour, &time->min, &time->sec ) < 5 )
360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
361
362 *p += len;
363
364 return( 0 );
365 }
366 else
Paul Bakker9d781402011-05-09 16:17:09 +0000367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368}
369
370
371/*
372 * Validity ::= SEQUENCE {
373 * notBefore Time,
374 * notAfter Time }
375 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000376static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 x509_time *from,
379 x509_time *to )
380{
Paul Bakker23986e52011-04-24 08:57:21 +0000381 int ret;
382 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
384 if( ( ret = asn1_get_tag( p, end, &len,
385 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000386 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388 end = *p + len;
389
Paul Bakker91200182010-02-18 21:26:15 +0000390 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000391 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
Paul Bakker91200182010-02-18 21:26:15 +0000393 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000394 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000397 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000398 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 return( 0 );
401}
402
403/*
404 * SubjectPublicKeyInfo ::= SEQUENCE {
405 * algorithm AlgorithmIdentifier,
406 * subjectPublicKey BIT STRING }
407 */
408static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000409 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 x509_buf *pk_alg_oid,
411 mpi *N, mpi *E )
412{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200413 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000414 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200416 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Paul Bakkerf8d018a2013-06-29 12:16:17 +0200418 if( ( ret = asn1_get_alg_null( p, end, pk_alg_oid ) ) != 0 )
419 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421 /*
422 * only RSA public keys handled at this time
423 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200424 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000425 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000428 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000431 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000432 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 end2 = *p + len;
435
436 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 /*
440 * RSAPublicKey ::= SEQUENCE {
441 * modulus INTEGER, -- n
442 * publicExponent INTEGER -- e
443 * }
444 */
445 if( ( ret = asn1_get_tag( p, end2, &len,
446 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000447 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000450 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000451 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
454 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000455 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000458 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000459 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 return( 0 );
462}
463
464static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000465 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 x509_buf *sig )
467{
Paul Bakker23986e52011-04-24 08:57:21 +0000468 int ret;
469 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Paul Bakker8afa70d2012-02-11 18:42:45 +0000471 if( ( end - *p ) < 1 )
472 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
473 POLARSSL_ERR_ASN1_OUT_OF_DATA );
474
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 sig->tag = **p;
476
477 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Paul Bakker74111d32011-01-15 16:57:55 +0000480
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 sig->len = len;
485 sig->p = *p;
486
487 *p += len;
488
489 return( 0 );
490}
491
492/*
493 * X.509 v2/v3 unique identifier (not parsed)
494 */
495static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 x509_buf *uid, int n )
498{
499 int ret;
500
501 if( *p == end )
502 return( 0 );
503
504 uid->tag = **p;
505
506 if( ( ret = asn1_get_tag( p, end, &uid->len,
507 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
508 {
Paul Bakker40e46942009-01-03 21:51:57 +0000509 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 return( 0 );
511
512 return( ret );
513 }
514
515 uid->p = *p;
516 *p += uid->len;
517
518 return( 0 );
519}
520
521/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000522 * X.509 Extensions (No parsing of extensions, pointer should
523 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 */
525static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000526 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000527 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528{
Paul Bakker23986e52011-04-24 08:57:21 +0000529 int ret;
530 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
532 if( *p == end )
533 return( 0 );
534
535 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000538 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541 ext->p = *p;
542 end = *p + ext->len;
543
544 /*
545 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
546 *
547 * Extension ::= SEQUENCE {
548 * extnID OBJECT IDENTIFIER,
549 * critical BOOLEAN DEFAULT FALSE,
550 * extnValue OCTET STRING }
551 */
552 if( ( ret = asn1_get_tag( p, end, &len,
553 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000554 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000557 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000558 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Paul Bakkerd98030e2009-05-02 15:13:40 +0000560 return( 0 );
561}
562
563/*
564 * X.509 CRL v2 extensions (no extensions parsed yet.)
565 */
566static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567 const unsigned char *end,
568 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000569{
Paul Bakker23986e52011-04-24 08:57:21 +0000570 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000571 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000572
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000573 /* Get explicit tag */
574 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000575 {
576 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
577 return( 0 );
578
579 return( ret );
580 }
581
582 while( *p < end )
583 {
584 if( ( ret = asn1_get_tag( p, end, &len,
585 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000586 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000587
588 *p += len;
589 }
590
591 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000592 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000593 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
594
595 return( 0 );
596}
597
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000598/*
599 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
600 */
601static int x509_get_crl_entry_ext( unsigned char **p,
602 const unsigned char *end,
603 x509_buf *ext )
604{
605 int ret;
606 size_t len = 0;
607
608 /* OPTIONAL */
609 if (end <= *p)
610 return( 0 );
611
612 ext->tag = **p;
613 ext->p = *p;
614
615 /*
616 * Get CRL-entry extension sequence header
617 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
618 */
619 if( ( ret = asn1_get_tag( p, end, &ext->len,
620 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
621 {
622 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
623 {
624 ext->p = NULL;
625 return( 0 );
626 }
627 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
628 }
629
630 end = *p + ext->len;
631
632 if( end != *p + ext->len )
633 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
635
636 while( *p < end )
637 {
638 if( ( ret = asn1_get_tag( p, end, &len,
639 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
640 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
641
642 *p += len;
643 }
644
645 if( *p != end )
646 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
647 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
648
649 return( 0 );
650}
651
Paul Bakker74111d32011-01-15 16:57:55 +0000652static int x509_get_basic_constraints( unsigned char **p,
653 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000654 int *ca_istrue,
655 int *max_pathlen )
656{
Paul Bakker23986e52011-04-24 08:57:21 +0000657 int ret;
658 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000659
660 /*
661 * BasicConstraints ::= SEQUENCE {
662 * cA BOOLEAN DEFAULT FALSE,
663 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
664 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000665 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000666 *max_pathlen = 0; /* endless */
667
668 if( ( ret = asn1_get_tag( p, end, &len,
669 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000670 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000671
672 if( *p == end )
673 return 0;
674
Paul Bakker3cccddb2011-01-16 21:46:31 +0000675 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000676 {
677 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000678 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000679
680 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000681 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000682
Paul Bakker3cccddb2011-01-16 21:46:31 +0000683 if( *ca_istrue != 0 )
684 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000685 }
686
687 if( *p == end )
688 return 0;
689
690 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000692
693 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000695 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
696
697 (*max_pathlen)++;
698
Paul Bakker74111d32011-01-15 16:57:55 +0000699 return 0;
700}
701
702static int x509_get_ns_cert_type( unsigned char **p,
703 const unsigned char *end,
704 unsigned char *ns_cert_type)
705{
706 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000707 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000711
712 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000713 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000714 POLARSSL_ERR_ASN1_INVALID_LENGTH );
715
716 /* Get actual bitstring */
717 *ns_cert_type = *bs.p;
718 return 0;
719}
720
721static int x509_get_key_usage( unsigned char **p,
722 const unsigned char *end,
723 unsigned char *key_usage)
724{
725 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000726 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000727
728 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000729 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000730
Paul Bakker94a67962012-08-23 13:03:52 +0000731 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000732 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000733 POLARSSL_ERR_ASN1_INVALID_LENGTH );
734
735 /* Get actual bitstring */
736 *key_usage = *bs.p;
737 return 0;
738}
739
Paul Bakkerd98030e2009-05-02 15:13:40 +0000740/*
Paul Bakker74111d32011-01-15 16:57:55 +0000741 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
742 *
743 * KeyPurposeId ::= OBJECT IDENTIFIER
744 */
745static int x509_get_ext_key_usage( unsigned char **p,
746 const unsigned char *end,
747 x509_sequence *ext_key_usage)
748{
749 int ret;
750
751 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 /* Sequence length must be >= 1 */
755 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000756 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000757 POLARSSL_ERR_ASN1_INVALID_LENGTH );
758
759 return 0;
760}
761
762/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000763 * SubjectAltName ::= GeneralNames
764 *
765 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
766 *
767 * GeneralName ::= CHOICE {
768 * otherName [0] OtherName,
769 * rfc822Name [1] IA5String,
770 * dNSName [2] IA5String,
771 * x400Address [3] ORAddress,
772 * directoryName [4] Name,
773 * ediPartyName [5] EDIPartyName,
774 * uniformResourceIdentifier [6] IA5String,
775 * iPAddress [7] OCTET STRING,
776 * registeredID [8] OBJECT IDENTIFIER }
777 *
778 * OtherName ::= SEQUENCE {
779 * type-id OBJECT IDENTIFIER,
780 * value [0] EXPLICIT ANY DEFINED BY type-id }
781 *
782 * EDIPartyName ::= SEQUENCE {
783 * nameAssigner [0] DirectoryString OPTIONAL,
784 * partyName [1] DirectoryString }
785 *
786 * NOTE: PolarSSL only parses and uses dNSName at this point.
787 */
788static int x509_get_subject_alt_name( unsigned char **p,
789 const unsigned char *end,
790 x509_sequence *subject_alt_name )
791{
792 int ret;
793 size_t len, tag_len;
794 asn1_buf *buf;
795 unsigned char tag;
796 asn1_sequence *cur = subject_alt_name;
797
798 /* Get main sequence tag */
799 if( ( ret = asn1_get_tag( p, end, &len,
800 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
801 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
802
803 if( *p + len != end )
804 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
805 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
806
807 while( *p < end )
808 {
809 if( ( end - *p ) < 1 )
810 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
811 POLARSSL_ERR_ASN1_OUT_OF_DATA );
812
813 tag = **p;
814 (*p)++;
815 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
816 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
817
818 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
819 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
820 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
821
822 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
823 {
824 *p += tag_len;
825 continue;
826 }
827
828 buf = &(cur->buf);
829 buf->tag = tag;
830 buf->p = *p;
831 buf->len = tag_len;
832 *p += buf->len;
833
834 /* Allocate and assign next pointer */
835 if (*p < end)
836 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200837 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000838 sizeof( asn1_sequence ) );
839
840 if( cur->next == NULL )
841 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
842 POLARSSL_ERR_ASN1_MALLOC_FAILED );
843
Paul Bakker535e97d2012-08-23 10:49:55 +0000844 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000845 cur = cur->next;
846 }
847 }
848
849 /* Set final sequence entry's next pointer to NULL */
850 cur->next = NULL;
851
852 if( *p != end )
853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
854 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
855
856 return( 0 );
857}
858
859/*
Paul Bakker74111d32011-01-15 16:57:55 +0000860 * X.509 v3 extensions
861 *
862 * TODO: Perform all of the basic constraints tests required by the RFC
863 * TODO: Set values for undetected extensions to a sane default?
864 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000865 */
866static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000867 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000868 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000869{
Paul Bakker23986e52011-04-24 08:57:21 +0000870 int ret;
871 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000872 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000873
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000874 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000875 {
876 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
877 return( 0 );
878
879 return( ret );
880 }
881
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 while( *p < end )
883 {
Paul Bakker74111d32011-01-15 16:57:55 +0000884 /*
885 * Extension ::= SEQUENCE {
886 * extnID OBJECT IDENTIFIER,
887 * critical BOOLEAN DEFAULT FALSE,
888 * extnValue OCTET STRING }
889 */
890 x509_buf extn_oid = {0, 0, NULL};
891 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200892 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000893
Paul Bakker5121ce52009-01-03 21:22:43 +0000894 if( ( ret = asn1_get_tag( p, end, &len,
895 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000896 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000897
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000898 end_ext_data = *p + len;
899
Paul Bakker74111d32011-01-15 16:57:55 +0000900 /* Get extension ID */
901 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Paul Bakker74111d32011-01-15 16:57:55 +0000903 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Paul Bakker74111d32011-01-15 16:57:55 +0000906 extn_oid.p = *p;
907 *p += extn_oid.len;
908
909 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000910 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000911 POLARSSL_ERR_ASN1_OUT_OF_DATA );
912
913 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000914 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000915 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000916 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Paul Bakker74111d32011-01-15 16:57:55 +0000918 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000919 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000921 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000923 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000924
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000925 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker74111d32011-01-15 16:57:55 +0000929 /*
930 * Detect supported extensions
931 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200932 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
933
934 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000935 {
936 /* No parser found, skip extension */
937 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker5c721f92011-07-27 16:51:09 +0000939#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000940 if( is_critical )
941 {
942 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000943 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000944 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
945 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000946#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200947 continue;
948 }
949
950 crt->ext_types |= ext_type;
951
952 switch( ext_type )
953 {
954 case EXT_BASIC_CONSTRAINTS:
955 /* Parse basic constraints */
956 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
957 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
958 return ( ret );
959 break;
960
961 case EXT_KEY_USAGE:
962 /* Parse key usage */
963 if( ( ret = x509_get_key_usage( p, end_ext_octet,
964 &crt->key_usage ) ) != 0 )
965 return ( ret );
966 break;
967
968 case EXT_EXTENDED_KEY_USAGE:
969 /* Parse extended key usage */
970 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
971 &crt->ext_key_usage ) ) != 0 )
972 return ( ret );
973 break;
974
975 case EXT_SUBJECT_ALT_NAME:
976 /* Parse subject alt name */
977 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
978 &crt->subject_alt_names ) ) != 0 )
979 return ( ret );
980 break;
981
982 case EXT_NS_CERT_TYPE:
983 /* Parse netscape certificate type */
984 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
985 &crt->ns_cert_type ) ) != 0 )
986 return ( ret );
987 break;
988
989 default:
990 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +0000991 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000992 }
993
994 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000996 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 return( 0 );
999}
1000
1001/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001002 * X.509 CRL Entries
1003 */
1004static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001005 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001006 x509_crl_entry *entry )
1007{
Paul Bakker23986e52011-04-24 08:57:21 +00001008 int ret;
1009 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001010 x509_crl_entry *cur_entry = entry;
1011
1012 if( *p == end )
1013 return( 0 );
1014
Paul Bakker9be19372009-07-27 20:21:53 +00001015 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001016 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1017 {
1018 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1019 return( 0 );
1020
1021 return( ret );
1022 }
1023
Paul Bakker9be19372009-07-27 20:21:53 +00001024 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001025
1026 while( *p < end )
1027 {
Paul Bakker23986e52011-04-24 08:57:21 +00001028 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001029 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001030
1031 if( ( ret = asn1_get_tag( p, end, &len2,
1032 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1033 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001034 return( ret );
1035 }
1036
Paul Bakker9be19372009-07-27 20:21:53 +00001037 cur_entry->raw.tag = **p;
1038 cur_entry->raw.p = *p;
1039 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001040 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001041
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001042 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001043 return( ret );
1044
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001045 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001046 return( ret );
1047
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001048 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049 return( ret );
1050
Paul Bakker74111d32011-01-15 16:57:55 +00001051 if ( *p < end )
1052 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001053 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001054
1055 if( cur_entry->next == NULL )
1056 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1057
Paul Bakkerd98030e2009-05-02 15:13:40 +00001058 cur_entry = cur_entry->next;
1059 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1060 }
1061 }
1062
1063 return( 0 );
1064}
1065
Paul Bakkerc70b9822013-04-07 22:00:46 +02001066static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1067 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001068{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001069 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001070
Paul Bakkerc70b9822013-04-07 22:00:46 +02001071 if( ret != 0 )
1072 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001073
Paul Bakkerc70b9822013-04-07 22:00:46 +02001074 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001075}
1076
Paul Bakkerd98030e2009-05-02 15:13:40 +00001077/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001078 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001080static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1081 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082{
Paul Bakker23986e52011-04-24 08:57:21 +00001083 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001084 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001085 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001086
Paul Bakker320a4b52009-03-28 18:52:39 +00001087 /*
1088 * Check for valid input
1089 */
1090 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001091 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001092
Paul Bakker6e339b52013-07-03 13:37:05 +02001093 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001094
1095 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001096 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001097
1098 memcpy( p, buf, buflen );
1099
1100 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001101
1102 crt->raw.p = p;
1103 crt->raw.len = len;
1104 end = p + len;
1105
1106 /*
1107 * Certificate ::= SEQUENCE {
1108 * tbsCertificate TBSCertificate,
1109 * signatureAlgorithm AlgorithmIdentifier,
1110 * signatureValue BIT STRING }
1111 */
1112 if( ( ret = asn1_get_tag( &p, end, &len,
1113 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001116 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
Paul Bakkerb00ca422012-09-25 12:10:00 +00001119 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001121 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001122 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001123 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001125 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001126
Paul Bakker5121ce52009-01-03 21:22:43 +00001127 /*
1128 * TBSCertificate ::= SEQUENCE {
1129 */
1130 crt->tbs.p = p;
1131
1132 if( ( ret = asn1_get_tag( &p, end, &len,
1133 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1134 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001135 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001136 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 }
1138
1139 end = p + len;
1140 crt->tbs.len = end - crt->tbs.p;
1141
1142 /*
1143 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1144 *
1145 * CertificateSerialNumber ::= INTEGER
1146 *
1147 * signature AlgorithmIdentifier
1148 */
1149 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1150 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1151 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1152 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001153 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( ret );
1155 }
1156
1157 crt->version++;
1158
1159 if( crt->version > 3 )
1160 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001161 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
Paul Bakkerc70b9822013-04-07 22:00:46 +02001165 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1166 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001168 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001169 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
1171
1172 /*
1173 * issuer Name
1174 */
1175 crt->issuer_raw.p = p;
1176
1177 if( ( ret = asn1_get_tag( &p, end, &len,
1178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1179 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001180 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001181 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
1183
1184 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1185 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001186 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 return( ret );
1188 }
1189
1190 crt->issuer_raw.len = p - crt->issuer_raw.p;
1191
1192 /*
1193 * Validity ::= SEQUENCE {
1194 * notBefore Time,
1195 * notAfter Time }
1196 *
1197 */
1198 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1199 &crt->valid_to ) ) != 0 )
1200 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001201 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 return( ret );
1203 }
1204
1205 /*
1206 * subject Name
1207 */
1208 crt->subject_raw.p = p;
1209
1210 if( ( ret = asn1_get_tag( &p, end, &len,
1211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001214 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakkercefb3962012-06-27 11:51:09 +00001217 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 return( ret );
1221 }
1222
1223 crt->subject_raw.len = p - crt->subject_raw.p;
1224
1225 /*
1226 * SubjectPublicKeyInfo ::= SEQUENCE
1227 * algorithm AlgorithmIdentifier,
1228 * subjectPublicKey BIT STRING }
1229 */
1230 if( ( ret = asn1_get_tag( &p, end, &len,
1231 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1232 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001233 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001234 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236
1237 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1238 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1239 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001240 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 return( ret );
1242 }
1243
1244 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 return( ret );
1248 }
1249
1250 crt->rsa.len = mpi_size( &crt->rsa.N );
1251
1252 /*
1253 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1254 * -- If present, version shall be v2 or v3
1255 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1256 * -- If present, version shall be v2 or v3
1257 * extensions [3] EXPLICIT Extensions OPTIONAL
1258 * -- If present, version shall be v3
1259 */
1260 if( crt->version == 2 || crt->version == 3 )
1261 {
1262 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1263 if( ret != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268 }
1269
1270 if( crt->version == 2 || crt->version == 3 )
1271 {
1272 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1273 if( ret != 0 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 return( ret );
1277 }
1278 }
1279
1280 if( crt->version == 3 )
1281 {
Paul Bakker74111d32011-01-15 16:57:55 +00001282 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 if( ret != 0 )
1284 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001285 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 return( ret );
1287 }
1288 }
1289
1290 if( p != end )
1291 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001292 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001293 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001294 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
1296
Paul Bakkerb00ca422012-09-25 12:10:00 +00001297 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
1299 /*
1300 * signatureAlgorithm AlgorithmIdentifier,
1301 * signatureValue BIT STRING
1302 */
1303 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1304 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001305 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 return( ret );
1307 }
1308
Paul Bakker535e97d2012-08-23 10:49:55 +00001309 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1310 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001312 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001313 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 }
1315
1316 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1317 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001318 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 return( ret );
1320 }
1321
1322 if( p != end )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001325 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001326 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001329 return( 0 );
1330}
1331
1332/*
Paul Bakker42c65812013-06-24 19:21:59 +02001333 * Parse one X.509 certificate in DER format from a buffer and add them to a
1334 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001335 */
Paul Bakker42c65812013-06-24 19:21:59 +02001336int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001337{
Paul Bakker42c65812013-06-24 19:21:59 +02001338 int ret;
1339 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001340
1341 /*
1342 * Check for valid input
1343 */
1344 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001345 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001346
1347 while( crt->version != 0 && crt->next != NULL )
1348 {
1349 prev = crt;
1350 crt = crt->next;
1351 }
1352
1353 /*
1354 * Add new certificate on the end of the chain if needed.
1355 */
1356 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001357 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001358 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001359
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001361 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001362
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001363 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001364 crt = crt->next;
1365 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Paul Bakker42c65812013-06-24 19:21:59 +02001368 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1369 {
1370 if( prev )
1371 prev->next = NULL;
1372
1373 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001374 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001375
1376 return( ret );
1377 }
1378
1379 return( 0 );
1380}
1381
1382/*
1383 * Parse one or more PEM certificates from a buffer and add them to the chained list
1384 */
1385int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1386{
1387 int ret, success = 0, first_error = 0, total_failed = 0;
1388 int buf_format = X509_FORMAT_DER;
1389
1390 /*
1391 * Check for valid input
1392 */
1393 if( chain == NULL || buf == NULL )
1394 return( POLARSSL_ERR_X509_INVALID_INPUT );
1395
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001396 /*
1397 * Determine buffer content. Buffer contains either one DER certificate or
1398 * one or more PEM certificates.
1399 */
1400#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001401 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001402 buf_format = X509_FORMAT_PEM;
1403#endif
1404
1405 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001406 return x509parse_crt_der( chain, buf, buflen );
1407
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001408#if defined(POLARSSL_PEM_C)
1409 if( buf_format == X509_FORMAT_PEM )
1410 {
1411 pem_context pem;
1412
1413 while( buflen > 0 )
1414 {
1415 size_t use_len;
1416 pem_init( &pem );
1417
1418 ret = pem_read_buffer( &pem,
1419 "-----BEGIN CERTIFICATE-----",
1420 "-----END CERTIFICATE-----",
1421 buf, NULL, 0, &use_len );
1422
1423 if( ret == 0 )
1424 {
1425 /*
1426 * Was PEM encoded
1427 */
1428 buflen -= use_len;
1429 buf += use_len;
1430 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001431 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1432 {
1433 return( ret );
1434 }
Paul Bakker00b28602013-06-24 13:02:41 +02001435 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001436 {
1437 pem_free( &pem );
1438
Paul Bakker5ed3b342013-06-24 19:05:46 +02001439 /*
1440 * PEM header and footer were found
1441 */
1442 buflen -= use_len;
1443 buf += use_len;
1444
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001445 if( first_error == 0 )
1446 first_error = ret;
1447
1448 continue;
1449 }
1450 else
1451 break;
1452
Paul Bakker42c65812013-06-24 19:21:59 +02001453 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001454
1455 pem_free( &pem );
1456
1457 if( ret != 0 )
1458 {
1459 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001460 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001461 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001462 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001464
1465 if( first_error == 0 )
1466 first_error = ret;
1467
Paul Bakker42c65812013-06-24 19:21:59 +02001468 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001469 continue;
1470 }
1471
1472 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001473 }
1474 }
1475#endif
1476
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001477 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001478 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479 else if( first_error )
1480 return( first_error );
1481 else
1482 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001483}
1484
1485/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001486 * Parse one or more CRLs and add them to the chained list
1487 */
Paul Bakker23986e52011-04-24 08:57:21 +00001488int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001489{
Paul Bakker23986e52011-04-24 08:57:21 +00001490 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001491 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001492 unsigned char *p, *end;
1493 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001494#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001495 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001496 pem_context pem;
1497#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001498
1499 crl = chain;
1500
1501 /*
1502 * Check for valid input
1503 */
1504 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001505 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506
1507 while( crl->version != 0 && crl->next != NULL )
1508 crl = crl->next;
1509
1510 /*
1511 * Add new CRL on the end of the chain if needed.
1512 */
1513 if ( crl->version != 0 && crl->next == NULL)
1514 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001515 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516
Paul Bakker7d06ad22009-05-02 15:53:56 +00001517 if( crl->next == NULL )
1518 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001520 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001521 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522
Paul Bakker7d06ad22009-05-02 15:53:56 +00001523 crl = crl->next;
1524 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525 }
1526
Paul Bakker96743fc2011-02-12 14:30:57 +00001527#if defined(POLARSSL_PEM_C)
1528 pem_init( &pem );
1529 ret = pem_read_buffer( &pem,
1530 "-----BEGIN X509 CRL-----",
1531 "-----END X509 CRL-----",
1532 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001533
Paul Bakker96743fc2011-02-12 14:30:57 +00001534 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001535 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001536 /*
1537 * Was PEM encoded
1538 */
1539 buflen -= use_len;
1540 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001541
1542 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001543 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001545 p = pem.buf;
1546 pem.buf = NULL;
1547 len = pem.buflen;
1548 pem_free( &pem );
1549 }
Paul Bakker00b28602013-06-24 13:02:41 +02001550 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001551 {
1552 pem_free( &pem );
1553 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001554 }
1555 else
1556 {
1557 /*
1558 * nope, copy the raw DER data
1559 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001560 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001561
1562 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001563 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
1565 memcpy( p, buf, buflen );
1566
1567 buflen = 0;
1568 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001569#else
Paul Bakker6e339b52013-07-03 13:37:05 +02001570 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001571
1572 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001573 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001574
1575 memcpy( p, buf, buflen );
1576
1577 buflen = 0;
1578#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579
1580 crl->raw.p = p;
1581 crl->raw.len = len;
1582 end = p + len;
1583
1584 /*
1585 * CertificateList ::= SEQUENCE {
1586 * tbsCertList TBSCertList,
1587 * signatureAlgorithm AlgorithmIdentifier,
1588 * signatureValue BIT STRING }
1589 */
1590 if( ( ret = asn1_get_tag( &p, end, &len,
1591 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1592 {
1593 x509_crl_free( crl );
1594 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1595 }
1596
Paul Bakker23986e52011-04-24 08:57:21 +00001597 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598 {
1599 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001600 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1602 }
1603
1604 /*
1605 * TBSCertList ::= SEQUENCE {
1606 */
1607 crl->tbs.p = p;
1608
1609 if( ( ret = asn1_get_tag( &p, end, &len,
1610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1611 {
1612 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001613 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 }
1615
1616 end = p + len;
1617 crl->tbs.len = end - crl->tbs.p;
1618
1619 /*
1620 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1621 * -- if present, MUST be v2
1622 *
1623 * signature AlgorithmIdentifier
1624 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001625 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001626 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1627 {
1628 x509_crl_free( crl );
1629 return( ret );
1630 }
1631
1632 crl->version++;
1633
1634 if( crl->version > 2 )
1635 {
1636 x509_crl_free( crl );
1637 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1638 }
1639
Paul Bakkerc70b9822013-04-07 22:00:46 +02001640 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1641 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642 {
1643 x509_crl_free( crl );
1644 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1645 }
1646
1647 /*
1648 * issuer Name
1649 */
1650 crl->issuer_raw.p = p;
1651
1652 if( ( ret = asn1_get_tag( &p, end, &len,
1653 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1654 {
1655 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001656 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001657 }
1658
1659 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( ret );
1663 }
1664
1665 crl->issuer_raw.len = p - crl->issuer_raw.p;
1666
1667 /*
1668 * thisUpdate Time
1669 * nextUpdate Time OPTIONAL
1670 */
Paul Bakker91200182010-02-18 21:26:15 +00001671 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 {
1673 x509_crl_free( crl );
1674 return( ret );
1675 }
1676
Paul Bakker91200182010-02-18 21:26:15 +00001677 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 {
Paul Bakker9d781402011-05-09 16:17:09 +00001679 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001680 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001681 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001682 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001683 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687 }
1688
1689 /*
1690 * revokedCertificates SEQUENCE OF SEQUENCE {
1691 * userCertificate CertificateSerialNumber,
1692 * revocationDate Time,
1693 * crlEntryExtensions Extensions OPTIONAL
1694 * -- if present, MUST be v2
1695 * } OPTIONAL
1696 */
1697 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1698 {
1699 x509_crl_free( crl );
1700 return( ret );
1701 }
1702
1703 /*
1704 * crlExtensions EXPLICIT Extensions OPTIONAL
1705 * -- if present, MUST be v2
1706 */
1707 if( crl->version == 2 )
1708 {
1709 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1710
1711 if( ret != 0 )
1712 {
1713 x509_crl_free( crl );
1714 return( ret );
1715 }
1716 }
1717
1718 if( p != end )
1719 {
1720 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001721 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1723 }
1724
1725 end = crl->raw.p + crl->raw.len;
1726
1727 /*
1728 * signatureAlgorithm AlgorithmIdentifier,
1729 * signatureValue BIT STRING
1730 */
1731 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1732 {
1733 x509_crl_free( crl );
1734 return( ret );
1735 }
1736
Paul Bakker535e97d2012-08-23 10:49:55 +00001737 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1738 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739 {
1740 x509_crl_free( crl );
1741 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1742 }
1743
1744 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1745 {
1746 x509_crl_free( crl );
1747 return( ret );
1748 }
1749
1750 if( p != end )
1751 {
1752 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001753 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1755 }
1756
1757 if( buflen > 0 )
1758 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001759 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760
Paul Bakker7d06ad22009-05-02 15:53:56 +00001761 if( crl->next == NULL )
1762 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001763 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001764 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001765 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766
Paul Bakker7d06ad22009-05-02 15:53:56 +00001767 crl = crl->next;
1768 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001769
1770 return( x509parse_crl( crl, buf, buflen ) );
1771 }
1772
1773 return( 0 );
1774}
1775
Paul Bakker335db3f2011-04-25 15:28:35 +00001776#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001777/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001778 * Load all data from a file into a given buffer.
1779 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001780static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001781{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001782 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001783
Paul Bakkerd98030e2009-05-02 15:13:40 +00001784 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001785 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001786
Paul Bakkerd98030e2009-05-02 15:13:40 +00001787 fseek( f, 0, SEEK_END );
1788 *n = (size_t) ftell( f );
1789 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001790
Paul Bakker6e339b52013-07-03 13:37:05 +02001791 if( ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001792 {
1793 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001794 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001795 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001796
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797 if( fread( *buf, 1, *n, f ) != *n )
1798 {
1799 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001800 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001801 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001803
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001805
Paul Bakkerd98030e2009-05-02 15:13:40 +00001806 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001807
Paul Bakkerd98030e2009-05-02 15:13:40 +00001808 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001809}
1810
1811/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 * Load one or more certificates and add them to the chained list
1813 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001814int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001815{
1816 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 size_t n;
1818 unsigned char *buf;
1819
Paul Bakker69e095c2011-12-10 21:55:01 +00001820 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1821 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Paul Bakker69e095c2011-12-10 21:55:01 +00001823 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
1825 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001826 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
1828 return( ret );
1829}
1830
Paul Bakker8d914582012-06-04 12:46:42 +00001831int x509parse_crtpath( x509_cert *chain, const char *path )
1832{
1833 int ret = 0;
1834#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001835 int w_ret;
1836 WCHAR szDir[MAX_PATH];
1837 char filename[MAX_PATH];
1838 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001839 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001840
Paul Bakker97872ac2012-11-02 12:53:26 +00001841 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001842 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001843
1844 if( len > MAX_PATH - 3 )
1845 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001846
Paul Bakker3338b792012-10-01 21:13:10 +00001847 memset( szDir, 0, sizeof(szDir) );
1848 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001849 memcpy( filename, path, len );
1850 filename[len++] = '\\';
1851 p = filename + len;
1852 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001853
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001854 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001855
Paul Bakker97872ac2012-11-02 12:53:26 +00001856 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001857 if (hFind == INVALID_HANDLE_VALUE)
1858 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1859
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001860 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001861 do
1862 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001863 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001864
Paul Bakkere4791f32012-06-04 21:29:15 +00001865 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001866 continue;
1867
Paul Bakker3338b792012-10-01 21:13:10 +00001868 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1869 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001870 p, len - 1,
1871 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001872
Paul Bakker3338b792012-10-01 21:13:10 +00001873 w_ret = x509parse_crtfile( chain, filename );
1874 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001875 ret++;
1876 else
1877 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001878 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001879 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001880
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001881 if (GetLastError() != ERROR_NO_MORE_FILES)
1882 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001883
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001884cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001885 FindClose( hFind );
1886#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001887 int t_ret, i;
1888 struct stat sb;
1889 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001890 char entry_name[255];
1891 DIR *dir = opendir( path );
1892
1893 if( dir == NULL)
1894 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1895
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001896 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001897 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001898 if( result == NULL )
1899 break;
1900
1901 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1902
1903 i = stat( entry_name, &sb );
1904
1905 if( i == -1 )
1906 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1907
1908 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001909 continue;
1910
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001911 // Ignore parse errors
1912 //
Paul Bakker8d914582012-06-04 12:46:42 +00001913 t_ret = x509parse_crtfile( chain, entry_name );
1914 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001915 ret++;
1916 else
1917 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001918 }
1919 closedir( dir );
1920#endif
1921
1922 return( ret );
1923}
1924
Paul Bakkerd98030e2009-05-02 15:13:40 +00001925/*
1926 * Load one or more CRLs and add them to the chained list
1927 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001928int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001929{
1930 int ret;
1931 size_t n;
1932 unsigned char *buf;
1933
Paul Bakker69e095c2011-12-10 21:55:01 +00001934 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1935 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001936
Paul Bakker27fdf462011-06-09 13:55:13 +00001937 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001938
1939 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001940 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001941
1942 return( ret );
1943}
1944
Paul Bakker5121ce52009-01-03 21:22:43 +00001945/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001946 * Load and parse a private RSA key
1947 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001948int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
Paul Bakker335db3f2011-04-25 15:28:35 +00001949{
1950 int ret;
1951 size_t n;
1952 unsigned char *buf;
1953
Paul Bakker69e095c2011-12-10 21:55:01 +00001954 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1955 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001956
1957 if( pwd == NULL )
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001958 ret = x509parse_key_rsa( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001959 else
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001960 ret = x509parse_key_rsa( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001961 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00001962
1963 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001964 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00001965
1966 return( ret );
1967}
1968
1969/*
1970 * Load and parse a public RSA key
1971 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001972int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
Paul Bakker335db3f2011-04-25 15:28:35 +00001973{
1974 int ret;
1975 size_t n;
1976 unsigned char *buf;
1977
Paul Bakker69e095c2011-12-10 21:55:01 +00001978 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1979 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001980
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02001981 ret = x509parse_public_key_rsa( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001982
1983 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001984 polarssl_free( buf );
Paul Bakker335db3f2011-04-25 15:28:35 +00001985
1986 return( ret );
1987}
1988#endif /* POLARSSL_FS_IO */
1989
1990/*
Paul Bakkere2f50402013-06-24 19:00:59 +02001991 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 */
Paul Bakkere2f50402013-06-24 19:00:59 +02001993static int x509parse_key_pkcs1_der( rsa_context *rsa,
1994 const unsigned char *key,
1995 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001996{
Paul Bakker23986e52011-04-24 08:57:21 +00001997 int ret;
1998 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001999 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002000
Paul Bakker96743fc2011-02-12 14:30:57 +00002001 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002002 end = p + keylen;
2003
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002005 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002006 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 * RSAPrivateKey ::= SEQUENCE {
2008 * version Version,
2009 * modulus INTEGER, -- n
2010 * publicExponent INTEGER, -- e
2011 * privateExponent INTEGER, -- d
2012 * prime1 INTEGER, -- p
2013 * prime2 INTEGER, -- q
2014 * exponent1 INTEGER, -- d mod (p-1)
2015 * exponent2 INTEGER, -- d mod (q-1)
2016 * coefficient INTEGER, -- (inverse of q) mod p
2017 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2018 * }
2019 */
2020 if( ( ret = asn1_get_tag( &p, end, &len,
2021 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2022 {
Paul Bakker9d781402011-05-09 16:17:09 +00002023 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 }
2025
2026 end = p + len;
2027
2028 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2029 {
Paul Bakker9d781402011-05-09 16:17:09 +00002030 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
2032
2033 if( rsa->ver != 0 )
2034 {
Paul Bakker9d781402011-05-09 16:17:09 +00002035 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
2037
2038 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2039 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2040 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2041 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2042 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2043 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2044 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2045 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2046 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002048 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002049 }
2050
2051 rsa->len = mpi_size( &rsa->N );
2052
2053 if( p != end )
2054 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002056 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002057 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059
2060 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2061 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 rsa_free( rsa );
2063 return( ret );
2064 }
2065
Paul Bakkere2f50402013-06-24 19:00:59 +02002066 return( 0 );
2067}
2068
2069/*
2070 * Parse an unencrypted PKCS#8 encoded private RSA key
2071 */
2072static int x509parse_key_pkcs8_unencrypted_der(
2073 rsa_context *rsa,
2074 const unsigned char *key,
2075 size_t keylen )
2076{
2077 int ret;
2078 size_t len;
2079 unsigned char *p, *end;
2080 x509_buf pk_alg_oid;
2081 pk_type_t pk_alg = POLARSSL_PK_NONE;
2082
2083 p = (unsigned char *) key;
2084 end = p + keylen;
2085
2086 /*
2087 * This function parses the PrivatKeyInfo object (PKCS#8)
2088 *
2089 * PrivateKeyInfo ::= SEQUENCE {
2090 * version Version,
2091 * algorithm AlgorithmIdentifier,
2092 * PrivateKey BIT STRING
2093 * }
2094 *
2095 * AlgorithmIdentifier ::= SEQUENCE {
2096 * algorithm OBJECT IDENTIFIER,
2097 * parameters ANY DEFINED BY algorithm OPTIONAL
2098 * }
2099 *
2100 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2101 */
2102 if( ( ret = asn1_get_tag( &p, end, &len,
2103 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2104 {
2105 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2106 }
2107
2108 end = p + len;
2109
2110 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2111 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2112
2113 if( rsa->ver != 0 )
2114 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2115
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002116 if( ( ret = asn1_get_alg_null( &p, end, &pk_alg_oid ) ) != 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002117 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2118
2119 /*
2120 * only RSA keys handled at this time
2121 */
2122 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2123 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2124
2125 /*
2126 * Get the OCTET STRING and parse the PKCS#1 format inside
2127 */
2128 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2129 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2130
2131 if( ( end - p ) < 1 )
2132 {
2133 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2134 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2135 }
2136
2137 end = p + len;
2138
2139 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2140 return( ret );
2141
2142 return( 0 );
2143}
2144
2145/*
Paul Bakkerbda7cb72013-06-24 19:34:25 +02002146 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002147 */
2148static int x509parse_key_pkcs8_encrypted_der(
2149 rsa_context *rsa,
2150 const unsigned char *key,
2151 size_t keylen,
2152 const unsigned char *pwd,
2153 size_t pwdlen )
2154{
2155 int ret;
2156 size_t len;
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002157 unsigned char *p, *end;
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002158 x509_buf pbe_alg_oid, pbe_params;
2159 unsigned char buf[2048];
Paul Bakker7749a222013-06-28 17:28:20 +02002160#if defined(POLARSSL_PKCS12_C)
2161 cipher_type_t cipher_alg;
2162 md_type_t md_alg;
2163#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002164
2165 memset(buf, 0, 2048);
2166
2167 p = (unsigned char *) key;
2168 end = p + keylen;
2169
Paul Bakker28144de2013-06-24 19:28:55 +02002170 if( pwdlen == 0 )
2171 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2172
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002173 /*
2174 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2175 *
2176 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2177 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2178 * encryptedData EncryptedData
2179 * }
2180 *
2181 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2182 *
2183 * EncryptedData ::= OCTET STRING
2184 *
2185 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2186 */
2187 if( ( ret = asn1_get_tag( &p, end, &len,
2188 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2189 {
2190 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2191 }
2192
2193 end = p + len;
2194
Paul Bakkerf8d018a2013-06-29 12:16:17 +02002195 if( ( ret = asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002196 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002197
2198 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2199 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2200
2201 // buf has been sized to 2048 bytes
2202 if( len > 2048 )
2203 return( POLARSSL_ERR_X509_INVALID_INPUT );
2204
2205 /*
2206 * Decrypt EncryptedData with appropriate PDE
2207 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002208#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002209 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002210 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002211 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002212 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002213 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002214 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002215 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2216 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2217
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002218 return( ret );
2219 }
2220 }
2221 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2222 {
2223 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2224 PKCS12_PBE_DECRYPT,
2225 pwd, pwdlen,
2226 p, len, buf ) ) != 0 )
2227 {
2228 return( ret );
2229 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002230
2231 // Best guess for password mismatch when using RC4. If first tag is
2232 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2233 //
2234 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2235 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002236 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002237 else
2238#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002239#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002240 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002241 {
2242 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2243 p, len, buf ) ) != 0 )
2244 {
2245 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2246 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2247
2248 return( ret );
2249 }
2250 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002251 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002252#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002253 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2254
2255 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2256}
2257
2258/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002259 * Parse a private RSA key
2260 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002261int x509parse_key_rsa( rsa_context *rsa,
2262 const unsigned char *key, size_t keylen,
2263 const unsigned char *pwd, size_t pwdlen )
Paul Bakkere2f50402013-06-24 19:00:59 +02002264{
2265 int ret;
2266
Paul Bakker96743fc2011-02-12 14:30:57 +00002267#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002268 size_t len;
2269 pem_context pem;
2270
2271 pem_init( &pem );
2272 ret = pem_read_buffer( &pem,
2273 "-----BEGIN RSA PRIVATE KEY-----",
2274 "-----END RSA PRIVATE KEY-----",
2275 key, pwd, pwdlen, &len );
2276 if( ret == 0 )
2277 {
2278 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2279 {
2280 rsa_free( rsa );
2281 }
2282
2283 pem_free( &pem );
2284 return( ret );
2285 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002286 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2287 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2288 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2289 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002290 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002291 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002292
2293 ret = pem_read_buffer( &pem,
2294 "-----BEGIN PRIVATE KEY-----",
2295 "-----END PRIVATE KEY-----",
2296 key, NULL, 0, &len );
2297 if( ret == 0 )
2298 {
2299 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2300 pem.buf, pem.buflen ) ) != 0 )
2301 {
2302 rsa_free( rsa );
2303 }
2304
2305 pem_free( &pem );
2306 return( ret );
2307 }
2308 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002309 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002310
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002311 ret = pem_read_buffer( &pem,
2312 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2313 "-----END ENCRYPTED PRIVATE KEY-----",
2314 key, NULL, 0, &len );
2315 if( ret == 0 )
2316 {
2317 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2318 pem.buf, pem.buflen,
2319 pwd, pwdlen ) ) != 0 )
2320 {
2321 rsa_free( rsa );
2322 }
2323
2324 pem_free( &pem );
2325 return( ret );
2326 }
2327 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002328 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002329#else
2330 ((void) pwd);
2331 ((void) pwdlen);
2332#endif /* POLARSSL_PEM_C */
2333
2334 // At this point we only know it's not a PEM formatted key. Could be any
2335 // of the known DER encoded private key formats
2336 //
2337 // We try the different DER format parsers to see if one passes without
2338 // error
2339 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002340 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2341 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002342 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002343 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002346 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002347
2348 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2349 {
2350 return( ret );
2351 }
2352
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002353 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2354 return( 0 );
2355
2356 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002357
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002358 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2359 return( 0 );
2360
2361 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002362
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002363 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002364}
2365
2366/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002367 * Parse a public RSA key
2368 */
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02002369int x509parse_public_key_rsa( rsa_context *rsa,
2370 const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002371{
Paul Bakker23986e52011-04-24 08:57:21 +00002372 int ret;
2373 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002374 unsigned char *p, *end;
2375 x509_buf alg_oid;
2376#if defined(POLARSSL_PEM_C)
2377 pem_context pem;
2378
2379 pem_init( &pem );
2380 ret = pem_read_buffer( &pem,
2381 "-----BEGIN PUBLIC KEY-----",
2382 "-----END PUBLIC KEY-----",
2383 key, NULL, 0, &len );
2384
2385 if( ret == 0 )
2386 {
2387 /*
2388 * Was PEM encoded
2389 */
2390 keylen = pem.buflen;
2391 }
Paul Bakker00b28602013-06-24 13:02:41 +02002392 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002393 {
2394 pem_free( &pem );
2395 return( ret );
2396 }
2397
2398 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2399#else
2400 p = (unsigned char *) key;
2401#endif
2402 end = p + keylen;
2403
2404 /*
2405 * PublicKeyInfo ::= SEQUENCE {
2406 * algorithm AlgorithmIdentifier,
2407 * PublicKey BIT STRING
2408 * }
2409 *
2410 * AlgorithmIdentifier ::= SEQUENCE {
2411 * algorithm OBJECT IDENTIFIER,
2412 * parameters ANY DEFINED BY algorithm OPTIONAL
2413 * }
2414 *
2415 * RSAPublicKey ::= SEQUENCE {
2416 * modulus INTEGER, -- n
2417 * publicExponent INTEGER -- e
2418 * }
2419 */
2420
2421 if( ( ret = asn1_get_tag( &p, end, &len,
2422 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2423 {
2424#if defined(POLARSSL_PEM_C)
2425 pem_free( &pem );
2426#endif
2427 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002428 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002429 }
2430
2431 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2432 {
2433#if defined(POLARSSL_PEM_C)
2434 pem_free( &pem );
2435#endif
2436 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002437 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002438 }
2439
2440 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2441 {
2442#if defined(POLARSSL_PEM_C)
2443 pem_free( &pem );
2444#endif
2445 rsa_free( rsa );
2446 return( ret );
2447 }
2448
2449 rsa->len = mpi_size( &rsa->N );
2450
2451#if defined(POLARSSL_PEM_C)
2452 pem_free( &pem );
2453#endif
2454
2455 return( 0 );
2456}
2457
Paul Bakkereaa89f82011-04-04 21:36:15 +00002458#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002459/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002460 * Parse DHM parameters
2461 */
Paul Bakker23986e52011-04-24 08:57:21 +00002462int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002463{
Paul Bakker23986e52011-04-24 08:57:21 +00002464 int ret;
2465 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002466 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002467#if defined(POLARSSL_PEM_C)
2468 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002469
Paul Bakker96743fc2011-02-12 14:30:57 +00002470 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002471
Paul Bakker96743fc2011-02-12 14:30:57 +00002472 ret = pem_read_buffer( &pem,
2473 "-----BEGIN DH PARAMETERS-----",
2474 "-----END DH PARAMETERS-----",
2475 dhmin, NULL, 0, &dhminlen );
2476
2477 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002478 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002479 /*
2480 * Was PEM encoded
2481 */
2482 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002483 }
Paul Bakker00b28602013-06-24 13:02:41 +02002484 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002485 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002486 pem_free( &pem );
2487 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002488 }
2489
Paul Bakker96743fc2011-02-12 14:30:57 +00002490 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2491#else
2492 p = (unsigned char *) dhmin;
2493#endif
2494 end = p + dhminlen;
2495
Paul Bakker1b57b062011-01-06 15:48:19 +00002496 memset( dhm, 0, sizeof( dhm_context ) );
2497
Paul Bakker1b57b062011-01-06 15:48:19 +00002498 /*
2499 * DHParams ::= SEQUENCE {
2500 * prime INTEGER, -- P
2501 * generator INTEGER, -- g
2502 * }
2503 */
2504 if( ( ret = asn1_get_tag( &p, end, &len,
2505 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2506 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002507#if defined(POLARSSL_PEM_C)
2508 pem_free( &pem );
2509#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002510 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002511 }
2512
2513 end = p + len;
2514
2515 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2516 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2517 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002518#if defined(POLARSSL_PEM_C)
2519 pem_free( &pem );
2520#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002521 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002522 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002523 }
2524
2525 if( p != end )
2526 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002527#if defined(POLARSSL_PEM_C)
2528 pem_free( &pem );
2529#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002530 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002531 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002532 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2533 }
2534
Paul Bakker96743fc2011-02-12 14:30:57 +00002535#if defined(POLARSSL_PEM_C)
2536 pem_free( &pem );
2537#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002538
2539 return( 0 );
2540}
2541
Paul Bakker335db3f2011-04-25 15:28:35 +00002542#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002543/*
2544 * Load and parse a private RSA key
2545 */
2546int x509parse_dhmfile( dhm_context *dhm, const char *path )
2547{
2548 int ret;
2549 size_t n;
2550 unsigned char *buf;
2551
Paul Bakker69e095c2011-12-10 21:55:01 +00002552 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2553 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002554
Paul Bakker27fdf462011-06-09 13:55:13 +00002555 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002556
2557 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002558 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002559
2560 return( ret );
2561}
Paul Bakker335db3f2011-04-25 15:28:35 +00002562#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002563#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002566#include <stdarg.h>
2567
2568#if !defined vsnprintf
2569#define vsnprintf _vsnprintf
2570#endif // vsnprintf
2571
2572/*
2573 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2574 * Result value is not size of buffer needed, but -1 if no fit is possible.
2575 *
2576 * This fuction tries to 'fix' this by at least suggesting enlarging the
2577 * size by 20.
2578 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002579static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002580{
2581 va_list ap;
2582 int res = -1;
2583
2584 va_start( ap, format );
2585
2586 res = vsnprintf( str, size, format, ap );
2587
2588 va_end( ap );
2589
2590 // No quick fix possible
2591 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002592 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002593
2594 return res;
2595}
2596
2597#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002598#endif
2599
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2601
2602#define SAFE_SNPRINTF() \
2603{ \
2604 if( ret == -1 ) \
2605 return( -1 ); \
2606 \
Paul Bakker23986e52011-04-24 08:57:21 +00002607 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002608 p[n - 1] = '\0'; \
2609 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2610 } \
2611 \
Paul Bakker23986e52011-04-24 08:57:21 +00002612 n -= (unsigned int) ret; \
2613 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002614}
2615
Paul Bakker5121ce52009-01-03 21:22:43 +00002616/*
2617 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002618 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002620int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621{
Paul Bakker23986e52011-04-24 08:57:21 +00002622 int ret;
2623 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002625 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002626 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 char s[128], *p;
2628
2629 memset( s, 0, sizeof( s ) );
2630
2631 name = dn;
2632 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002633 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
2635 while( name != NULL )
2636 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002637 if( !name->oid.p )
2638 {
2639 name = name->next;
2640 continue;
2641 }
2642
Paul Bakker74111d32011-01-15 16:57:55 +00002643 if( name != dn )
2644 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645 ret = snprintf( p, n, ", " );
2646 SAFE_SNPRINTF();
2647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Paul Bakkerc70b9822013-04-07 22:00:46 +02002649 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
Paul Bakkerc70b9822013-04-07 22:00:46 +02002651 if( ret == 0 )
2652 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002654 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002655 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002656
2657 for( i = 0; i < name->val.len; i++ )
2658 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002659 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 break;
2661
2662 c = name->val.p[i];
2663 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2664 s[i] = '?';
2665 else s[i] = c;
2666 }
2667 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002668 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002669 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 name = name->next;
2671 }
2672
Paul Bakker23986e52011-04-24 08:57:21 +00002673 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674}
2675
2676/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002677 * Store the serial in printable form into buf; no more
2678 * than size characters will be written
2679 */
2680int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2681{
Paul Bakker23986e52011-04-24 08:57:21 +00002682 int ret;
2683 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002684 char *p;
2685
2686 p = buf;
2687 n = size;
2688
2689 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002690 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002691
2692 for( i = 0; i < nr; i++ )
2693 {
Paul Bakker93048802011-12-05 14:38:06 +00002694 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002695 continue;
2696
Paul Bakkerdd476992011-01-16 21:34:59 +00002697 ret = snprintf( p, n, "%02X%s",
2698 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2699 SAFE_SNPRINTF();
2700 }
2701
Paul Bakker03c7c252011-11-25 12:37:37 +00002702 if( nr != serial->len )
2703 {
2704 ret = snprintf( p, n, "...." );
2705 SAFE_SNPRINTF();
2706 }
2707
Paul Bakker23986e52011-04-24 08:57:21 +00002708 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002709}
2710
2711/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002712 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002714int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2715 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002716{
Paul Bakker23986e52011-04-24 08:57:21 +00002717 int ret;
2718 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002719 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002720 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
2722 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002723 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Paul Bakkerd98030e2009-05-02 15:13:40 +00002725 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002727 SAFE_SNPRINTF();
2728 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002730 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002731
Paul Bakkerdd476992011-01-16 21:34:59 +00002732 ret = x509parse_serial_gets( p, n, &crt->serial);
2733 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2736 SAFE_SNPRINTF();
2737 ret = x509parse_dn_gets( p, n, &crt->issuer );
2738 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Paul Bakkerd98030e2009-05-02 15:13:40 +00002740 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2741 SAFE_SNPRINTF();
2742 ret = x509parse_dn_gets( p, n, &crt->subject );
2743 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Paul Bakkerd98030e2009-05-02 15:13:40 +00002745 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2747 crt->valid_from.year, crt->valid_from.mon,
2748 crt->valid_from.day, crt->valid_from.hour,
2749 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Paul Bakkerd98030e2009-05-02 15:13:40 +00002752 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2754 crt->valid_to.year, crt->valid_to.mon,
2755 crt->valid_to.day, crt->valid_to.hour,
2756 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002757 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Paul Bakkerc70b9822013-04-07 22:00:46 +02002759 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002760 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
Paul Bakkerc70b9822013-04-07 22:00:46 +02002762 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2763 if( ret != 0 )
2764 ret = snprintf( p, n, "???" );
2765 else
2766 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002767 SAFE_SNPRINTF();
2768
2769 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002770 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002771 SAFE_SNPRINTF();
2772
Paul Bakker23986e52011-04-24 08:57:21 +00002773 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002774}
2775
Paul Bakker74111d32011-01-15 16:57:55 +00002776/*
2777 * Return an informational string describing the given OID
2778 */
2779const char *x509_oid_get_description( x509_buf *oid )
2780{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002781 const char *desc = NULL;
2782 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002783
Paul Bakkerc70b9822013-04-07 22:00:46 +02002784 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002785
Paul Bakkerc70b9822013-04-07 22:00:46 +02002786 if( ret != 0 )
2787 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002788
Paul Bakkerc70b9822013-04-07 22:00:46 +02002789 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002790}
2791
2792/* Return the x.y.z.... style numeric string for the given OID */
2793int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2794{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002795 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002796}
2797
Paul Bakkerd98030e2009-05-02 15:13:40 +00002798/*
2799 * Return an informational string about the CRL.
2800 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002801int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2802 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002803{
Paul Bakker23986e52011-04-24 08:57:21 +00002804 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002805 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002806 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002807 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002808 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809
2810 p = buf;
2811 n = size;
2812
2813 ret = snprintf( p, n, "%sCRL version : %d",
2814 prefix, crl->version );
2815 SAFE_SNPRINTF();
2816
2817 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2818 SAFE_SNPRINTF();
2819 ret = x509parse_dn_gets( p, n, &crl->issuer );
2820 SAFE_SNPRINTF();
2821
2822 ret = snprintf( p, n, "\n%sthis update : " \
2823 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2824 crl->this_update.year, crl->this_update.mon,
2825 crl->this_update.day, crl->this_update.hour,
2826 crl->this_update.min, crl->this_update.sec );
2827 SAFE_SNPRINTF();
2828
2829 ret = snprintf( p, n, "\n%snext update : " \
2830 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2831 crl->next_update.year, crl->next_update.mon,
2832 crl->next_update.day, crl->next_update.hour,
2833 crl->next_update.min, crl->next_update.sec );
2834 SAFE_SNPRINTF();
2835
2836 entry = &crl->entry;
2837
2838 ret = snprintf( p, n, "\n%sRevoked certificates:",
2839 prefix );
2840 SAFE_SNPRINTF();
2841
Paul Bakker9be19372009-07-27 20:21:53 +00002842 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002843 {
2844 ret = snprintf( p, n, "\n%sserial number: ",
2845 prefix );
2846 SAFE_SNPRINTF();
2847
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002848 ret = x509parse_serial_gets( p, n, &entry->serial);
2849 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850
Paul Bakkerd98030e2009-05-02 15:13:40 +00002851 ret = snprintf( p, n, " revocation date: " \
2852 "%04d-%02d-%02d %02d:%02d:%02d",
2853 entry->revocation_date.year, entry->revocation_date.mon,
2854 entry->revocation_date.day, entry->revocation_date.hour,
2855 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002856 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002857
2858 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 }
2860
Paul Bakkerc70b9822013-04-07 22:00:46 +02002861 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002862 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakkerc70b9822013-04-07 22:00:46 +02002864 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2865 if( ret != 0 )
2866 ret = snprintf( p, n, "???" );
2867 else
2868 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002869 SAFE_SNPRINTF();
2870
Paul Bakker1e27bb22009-07-19 20:25:25 +00002871 ret = snprintf( p, n, "\n" );
2872 SAFE_SNPRINTF();
2873
Paul Bakker23986e52011-04-24 08:57:21 +00002874 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875}
2876
2877/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002878 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002880#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00002881int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002882{
Paul Bakkercce9d772011-11-18 14:26:47 +00002883 int year, mon, day;
2884 int hour, min, sec;
2885
2886#if defined(_WIN32)
2887 SYSTEMTIME st;
2888
2889 GetLocalTime(&st);
2890
2891 year = st.wYear;
2892 mon = st.wMonth;
2893 day = st.wDay;
2894 hour = st.wHour;
2895 min = st.wMinute;
2896 sec = st.wSecond;
2897#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 struct tm *lt;
2899 time_t tt;
2900
2901 tt = time( NULL );
2902 lt = localtime( &tt );
2903
Paul Bakkercce9d772011-11-18 14:26:47 +00002904 year = lt->tm_year + 1900;
2905 mon = lt->tm_mon + 1;
2906 day = lt->tm_mday;
2907 hour = lt->tm_hour;
2908 min = lt->tm_min;
2909 sec = lt->tm_sec;
2910#endif
2911
2912 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002913 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Paul Bakkercce9d772011-11-18 14:26:47 +00002915 if( year == to->year &&
2916 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002917 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakkercce9d772011-11-18 14:26:47 +00002919 if( year == to->year &&
2920 mon == to->mon &&
2921 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002922 return( 1 );
2923
Paul Bakkercce9d772011-11-18 14:26:47 +00002924 if( year == to->year &&
2925 mon == to->mon &&
2926 day == to->day &&
2927 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002928 return( 1 );
2929
Paul Bakkercce9d772011-11-18 14:26:47 +00002930 if( year == to->year &&
2931 mon == to->mon &&
2932 day == to->day &&
2933 hour == to->hour &&
2934 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002935 return( 1 );
2936
Paul Bakkercce9d772011-11-18 14:26:47 +00002937 if( year == to->year &&
2938 mon == to->mon &&
2939 day == to->day &&
2940 hour == to->hour &&
2941 min == to->min &&
2942 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002943 return( 1 );
2944
Paul Bakker40ea7de2009-05-03 10:18:48 +00002945 return( 0 );
2946}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002947#else /* POLARSSL_HAVE_TIME */
2948int x509parse_time_expired( const x509_time *to )
2949{
2950 ((void) to);
2951 return( 0 );
2952}
2953#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00002954
2955/*
2956 * Return 1 if the certificate is revoked, or 0 otherwise.
2957 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002958int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002959{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002960 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002961
2962 while( cur != NULL && cur->serial.len != 0 )
2963 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002964 if( crt->serial.len == cur->serial.len &&
2965 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002966 {
2967 if( x509parse_time_expired( &cur->revocation_date ) )
2968 return( 1 );
2969 }
2970
2971 cur = cur->next;
2972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
2974 return( 0 );
2975}
2976
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002977/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002978 * Check that the given certificate is valid accoring to the CRL.
2979 */
2980static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2981 x509_crl *crl_list)
2982{
2983 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002984 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2985 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002986
Paul Bakker915275b2012-09-28 07:10:55 +00002987 if( ca == NULL )
2988 return( flags );
2989
Paul Bakker76fd75a2011-01-16 21:12:10 +00002990 /*
2991 * TODO: What happens if no CRL is present?
2992 * Suggestion: Revocation state should be unknown if no CRL is present.
2993 * For backwards compatibility this is not yet implemented.
2994 */
2995
Paul Bakker915275b2012-09-28 07:10:55 +00002996 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002997 {
Paul Bakker915275b2012-09-28 07:10:55 +00002998 if( crl_list->version == 0 ||
2999 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003000 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3001 crl_list->issuer_raw.len ) != 0 )
3002 {
3003 crl_list = crl_list->next;
3004 continue;
3005 }
3006
3007 /*
3008 * Check if CRL is correctly signed by the trusted CA
3009 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003010 md_info = md_info_from_type( crl_list->sig_md );
3011 if( md_info == NULL )
3012 {
3013 /*
3014 * Cannot check 'unknown' hash
3015 */
3016 flags |= BADCRL_NOT_TRUSTED;
3017 break;
3018 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003019
Paul Bakkerc70b9822013-04-07 22:00:46 +02003020 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003021
Paul Bakkerc70b9822013-04-07 22:00:46 +02003022 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003023 0, hash, crl_list->sig.p ) == 0 )
3024 {
3025 /*
3026 * CRL is not trusted
3027 */
3028 flags |= BADCRL_NOT_TRUSTED;
3029 break;
3030 }
3031
3032 /*
3033 * Check for validity of CRL (Do not drop out)
3034 */
3035 if( x509parse_time_expired( &crl_list->next_update ) )
3036 flags |= BADCRL_EXPIRED;
3037
3038 /*
3039 * Check if certificate is revoked
3040 */
3041 if( x509parse_revoked(crt, crl_list) )
3042 {
3043 flags |= BADCERT_REVOKED;
3044 break;
3045 }
3046
3047 crl_list = crl_list->next;
3048 }
3049 return flags;
3050}
3051
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003052static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003053{
3054 size_t i;
3055 size_t cn_idx = 0;
3056
Paul Bakker57b12982012-02-11 17:38:38 +00003057 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003058 return( 0 );
3059
3060 for( i = 0; i < strlen( cn ); ++i )
3061 {
3062 if( cn[i] == '.' )
3063 {
3064 cn_idx = i;
3065 break;
3066 }
3067 }
3068
3069 if( cn_idx == 0 )
3070 return( 0 );
3071
Paul Bakker535e97d2012-08-23 10:49:55 +00003072 if( strlen( cn ) - cn_idx == name->len - 1 &&
3073 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003074 {
3075 return( 1 );
3076 }
3077
3078 return( 0 );
3079}
3080
Paul Bakker915275b2012-09-28 07:10:55 +00003081static int x509parse_verify_top(
3082 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003083 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003084 int (*f_vrfy)(void *, x509_cert *, int, int *),
3085 void *p_vrfy )
3086{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003087 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003088 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003089 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3090 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003091
3092 if( x509parse_time_expired( &child->valid_to ) )
3093 *flags |= BADCERT_EXPIRED;
3094
3095 /*
3096 * Child is the top of the chain. Check against the trust_ca list.
3097 */
3098 *flags |= BADCERT_NOT_TRUSTED;
3099
3100 while( trust_ca != NULL )
3101 {
3102 if( trust_ca->version == 0 ||
3103 child->issuer_raw.len != trust_ca->subject_raw.len ||
3104 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3105 child->issuer_raw.len ) != 0 )
3106 {
3107 trust_ca = trust_ca->next;
3108 continue;
3109 }
3110
Paul Bakker9a736322012-11-14 12:39:52 +00003111 /*
3112 * Reduce path_len to check against if top of the chain is
3113 * the same as the trusted CA
3114 */
3115 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3116 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3117 child->issuer_raw.len ) == 0 )
3118 {
3119 check_path_cnt--;
3120 }
3121
Paul Bakker915275b2012-09-28 07:10:55 +00003122 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003123 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003124 {
3125 trust_ca = trust_ca->next;
3126 continue;
3127 }
3128
Paul Bakkerc70b9822013-04-07 22:00:46 +02003129 md_info = md_info_from_type( child->sig_md );
3130 if( md_info == NULL )
3131 {
3132 /*
3133 * Cannot check 'unknown' hash
3134 */
3135 continue;
3136 }
Paul Bakker915275b2012-09-28 07:10:55 +00003137
Paul Bakkerc70b9822013-04-07 22:00:46 +02003138 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003139
Paul Bakkerc70b9822013-04-07 22:00:46 +02003140 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003141 0, hash, child->sig.p ) != 0 )
3142 {
3143 trust_ca = trust_ca->next;
3144 continue;
3145 }
3146
3147 /*
3148 * Top of chain is signed by a trusted CA
3149 */
3150 *flags &= ~BADCERT_NOT_TRUSTED;
3151 break;
3152 }
3153
Paul Bakker9a736322012-11-14 12:39:52 +00003154 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003155 * If top of chain is not the same as the trusted CA send a verify request
3156 * to the callback for any issues with validity and CRL presence for the
3157 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003158 */
3159 if( trust_ca != NULL &&
3160 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3161 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3162 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003163 {
3164 /* Check trusted CA's CRL for then chain's top crt */
3165 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3166
3167 if( x509parse_time_expired( &trust_ca->valid_to ) )
3168 ca_flags |= BADCERT_EXPIRED;
3169
Paul Bakker915275b2012-09-28 07:10:55 +00003170 if( NULL != f_vrfy )
3171 {
Paul Bakker9a736322012-11-14 12:39:52 +00003172 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003173 return( ret );
3174 }
3175 }
3176
3177 /* Call callback on top cert */
3178 if( NULL != f_vrfy )
3179 {
Paul Bakker9a736322012-11-14 12:39:52 +00003180 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003181 return( ret );
3182 }
3183
Paul Bakker915275b2012-09-28 07:10:55 +00003184 *flags |= ca_flags;
3185
3186 return( 0 );
3187}
3188
3189static int x509parse_verify_child(
3190 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003191 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003192 int (*f_vrfy)(void *, x509_cert *, int, int *),
3193 void *p_vrfy )
3194{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003195 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003196 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003197 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003198 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003199 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003200
3201 if( x509parse_time_expired( &child->valid_to ) )
3202 *flags |= BADCERT_EXPIRED;
3203
Paul Bakkerc70b9822013-04-07 22:00:46 +02003204 md_info = md_info_from_type( child->sig_md );
3205 if( md_info == NULL )
3206 {
3207 /*
3208 * Cannot check 'unknown' hash
3209 */
Paul Bakker915275b2012-09-28 07:10:55 +00003210 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003211 }
3212 else
3213 {
3214 md( md_info, child->tbs.p, child->tbs.len, hash );
3215
3216 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3217 child->sig.p ) != 0 )
3218 *flags |= BADCERT_NOT_TRUSTED;
3219 }
3220
Paul Bakker915275b2012-09-28 07:10:55 +00003221 /* Check trusted CA's CRL for the given crt */
3222 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3223
3224 grandparent = parent->next;
3225
3226 while( grandparent != NULL )
3227 {
3228 if( grandparent->version == 0 ||
3229 grandparent->ca_istrue == 0 ||
3230 parent->issuer_raw.len != grandparent->subject_raw.len ||
3231 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3232 parent->issuer_raw.len ) != 0 )
3233 {
3234 grandparent = grandparent->next;
3235 continue;
3236 }
3237 break;
3238 }
3239
Paul Bakker915275b2012-09-28 07:10:55 +00003240 if( grandparent != NULL )
3241 {
3242 /*
3243 * Part of the chain
3244 */
Paul Bakker9a736322012-11-14 12:39:52 +00003245 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 +00003246 if( ret != 0 )
3247 return( ret );
3248 }
3249 else
3250 {
Paul Bakker9a736322012-11-14 12:39:52 +00003251 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 +00003252 if( ret != 0 )
3253 return( ret );
3254 }
3255
3256 /* child is verified to be a child of the parent, call verify callback */
3257 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003258 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003259 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003260
3261 *flags |= parent_flags;
3262
3263 return( 0 );
3264}
3265
Paul Bakker76fd75a2011-01-16 21:12:10 +00003266/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 * Verify the certificate validity
3268 */
3269int x509parse_verify( x509_cert *crt,
3270 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003271 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003272 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003273 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003274 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003275{
Paul Bakker23986e52011-04-24 08:57:21 +00003276 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003277 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003278 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003279 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003281 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Paul Bakker40ea7de2009-05-03 10:18:48 +00003283 *flags = 0;
3284
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 if( cn != NULL )
3286 {
3287 name = &crt->subject;
3288 cn_len = strlen( cn );
3289
Paul Bakker4d2c1242012-05-10 14:12:46 +00003290 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003292 cur = &crt->subject_alt_names;
3293
3294 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003295 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003296 if( cur->buf.len == cn_len &&
3297 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003298 break;
3299
Paul Bakker535e97d2012-08-23 10:49:55 +00003300 if( cur->buf.len > 2 &&
3301 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003302 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003303 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003304
Paul Bakker4d2c1242012-05-10 14:12:46 +00003305 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003306 }
3307
3308 if( cur == NULL )
3309 *flags |= BADCERT_CN_MISMATCH;
3310 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003311 else
3312 {
3313 while( name != NULL )
3314 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003315 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003316 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003317 if( name->val.len == cn_len &&
3318 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003319 break;
3320
Paul Bakker535e97d2012-08-23 10:49:55 +00003321 if( name->val.len > 2 &&
3322 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003323 x509_wildcard_verify( cn, &name->val ) )
3324 break;
3325 }
3326
3327 name = name->next;
3328 }
3329
3330 if( name == NULL )
3331 *flags |= BADCERT_CN_MISMATCH;
3332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003336 * Iterate upwards in the given cert chain, to find our crt parent.
3337 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003339 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003340
Paul Bakker76fd75a2011-01-16 21:12:10 +00003341 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003343 if( parent->ca_istrue == 0 ||
3344 crt->issuer_raw.len != parent->subject_raw.len ||
3345 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 crt->issuer_raw.len ) != 0 )
3347 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003348 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 continue;
3350 }
Paul Bakker915275b2012-09-28 07:10:55 +00003351 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
3353
Paul Bakker915275b2012-09-28 07:10:55 +00003354 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 {
Paul Bakker915275b2012-09-28 07:10:55 +00003356 /*
3357 * Part of the chain
3358 */
Paul Bakker9a736322012-11-14 12:39:52 +00003359 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003360 if( ret != 0 )
3361 return( ret );
3362 }
3363 else
Paul Bakker74111d32011-01-15 16:57:55 +00003364 {
Paul Bakker9a736322012-11-14 12:39:52 +00003365 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003366 if( ret != 0 )
3367 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003368 }
Paul Bakker915275b2012-09-28 07:10:55 +00003369
3370 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003371 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003372
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 return( 0 );
3374}
3375
3376/*
3377 * Unallocate all certificate data
3378 */
3379void x509_free( x509_cert *crt )
3380{
3381 x509_cert *cert_cur = crt;
3382 x509_cert *cert_prv;
3383 x509_name *name_cur;
3384 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003385 x509_sequence *seq_cur;
3386 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003387
3388 if( crt == NULL )
3389 return;
3390
3391 do
3392 {
3393 rsa_free( &cert_cur->rsa );
3394
3395 name_cur = cert_cur->issuer.next;
3396 while( name_cur != NULL )
3397 {
3398 name_prv = name_cur;
3399 name_cur = name_cur->next;
3400 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003401 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 }
3403
3404 name_cur = cert_cur->subject.next;
3405 while( name_cur != NULL )
3406 {
3407 name_prv = name_cur;
3408 name_cur = name_cur->next;
3409 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003410 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 }
3412
Paul Bakker74111d32011-01-15 16:57:55 +00003413 seq_cur = cert_cur->ext_key_usage.next;
3414 while( seq_cur != NULL )
3415 {
3416 seq_prv = seq_cur;
3417 seq_cur = seq_cur->next;
3418 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003419 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003420 }
3421
Paul Bakker8afa70d2012-02-11 18:42:45 +00003422 seq_cur = cert_cur->subject_alt_names.next;
3423 while( seq_cur != NULL )
3424 {
3425 seq_prv = seq_cur;
3426 seq_cur = seq_cur->next;
3427 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003428 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003429 }
3430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( cert_cur->raw.p != NULL )
3432 {
3433 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003434 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 }
3436
3437 cert_cur = cert_cur->next;
3438 }
3439 while( cert_cur != NULL );
3440
3441 cert_cur = crt;
3442 do
3443 {
3444 cert_prv = cert_cur;
3445 cert_cur = cert_cur->next;
3446
3447 memset( cert_prv, 0, sizeof( x509_cert ) );
3448 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003449 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 }
3451 while( cert_cur != NULL );
3452}
3453
Paul Bakkerd98030e2009-05-02 15:13:40 +00003454/*
3455 * Unallocate all CRL data
3456 */
3457void x509_crl_free( x509_crl *crl )
3458{
3459 x509_crl *crl_cur = crl;
3460 x509_crl *crl_prv;
3461 x509_name *name_cur;
3462 x509_name *name_prv;
3463 x509_crl_entry *entry_cur;
3464 x509_crl_entry *entry_prv;
3465
3466 if( crl == NULL )
3467 return;
3468
3469 do
3470 {
3471 name_cur = crl_cur->issuer.next;
3472 while( name_cur != NULL )
3473 {
3474 name_prv = name_cur;
3475 name_cur = name_cur->next;
3476 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003477 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003478 }
3479
3480 entry_cur = crl_cur->entry.next;
3481 while( entry_cur != NULL )
3482 {
3483 entry_prv = entry_cur;
3484 entry_cur = entry_cur->next;
3485 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003486 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003487 }
3488
3489 if( crl_cur->raw.p != NULL )
3490 {
3491 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003492 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003493 }
3494
3495 crl_cur = crl_cur->next;
3496 }
3497 while( crl_cur != NULL );
3498
3499 crl_cur = crl;
3500 do
3501 {
3502 crl_prv = crl_cur;
3503 crl_cur = crl_cur->next;
3504
3505 memset( crl_prv, 0, sizeof( x509_crl ) );
3506 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003507 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003508 }
3509 while( crl_cur != NULL );
3510}
3511
Paul Bakker40e46942009-01-03 21:51:57 +00003512#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Paul Bakker40e46942009-01-03 21:51:57 +00003514#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
3516/*
3517 * Checkup routine
3518 */
3519int x509_self_test( int verbose )
3520{
Paul Bakker5690efc2011-05-26 13:16:06 +00003521#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003522 int ret;
3523 int flags;
3524 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 x509_cert cacert;
3526 x509_cert clicert;
3527 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003528#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003529 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
3532 if( verbose != 0 )
3533 printf( " X.509 certificate load: " );
3534
3535 memset( &clicert, 0, sizeof( x509_cert ) );
3536
Paul Bakker3c2122f2013-06-24 19:03:14 +02003537 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003538 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 if( ret != 0 )
3540 {
3541 if( verbose != 0 )
3542 printf( "failed\n" );
3543
3544 return( ret );
3545 }
3546
3547 memset( &cacert, 0, sizeof( x509_cert ) );
3548
Paul Bakker3c2122f2013-06-24 19:03:14 +02003549 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003550 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 if( ret != 0 )
3552 {
3553 if( verbose != 0 )
3554 printf( "failed\n" );
3555
3556 return( ret );
3557 }
3558
3559 if( verbose != 0 )
3560 printf( "passed\n X.509 private key load: " );
3561
3562 i = strlen( test_ca_key );
3563 j = strlen( test_ca_pwd );
3564
Paul Bakker66b78b22011-03-25 14:22:50 +00003565 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3566
Manuel Pégourié-Gonnardba4878a2013-06-27 10:51:01 +02003567 if( ( ret = x509parse_key_rsa( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003568 (const unsigned char *) test_ca_key, i,
3569 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 {
3571 if( verbose != 0 )
3572 printf( "failed\n" );
3573
3574 return( ret );
3575 }
3576
3577 if( verbose != 0 )
3578 printf( "passed\n X.509 signature verify: ");
3579
Paul Bakker23986e52011-04-24 08:57:21 +00003580 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 if( ret != 0 )
3582 {
Paul Bakker23986e52011-04-24 08:57:21 +00003583 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 if( verbose != 0 )
3585 printf( "failed\n" );
3586
3587 return( ret );
3588 }
3589
Paul Bakker5690efc2011-05-26 13:16:06 +00003590#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003592 printf( "passed\n X.509 DHM parameter load: " );
3593
3594 i = strlen( test_dhm_params );
3595 j = strlen( test_ca_pwd );
3596
Paul Bakker3c2122f2013-06-24 19:03:14 +02003597 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003598 {
3599 if( verbose != 0 )
3600 printf( "failed\n" );
3601
3602 return( ret );
3603 }
3604
3605 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
3609 x509_free( &cacert );
3610 x509_free( &clicert );
3611 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003612#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003613 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003614#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
3616 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003617#else
3618 ((void) verbose);
3619 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3620#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003621}
3622
3623#endif
3624
3625#endif