blob: b27faf9a4636d9daf85a21e6c237a1eeedaf0b2d [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 */
1948int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1949{
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 )
Paul Bakker27fdf462011-06-09 13:55:13 +00001958 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001959 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001960 ret = x509parse_key( 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 */
1972int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1973{
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
Paul Bakker27fdf462011-06-09 13:55:13 +00001981 ret = x509parse_public_key( 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 */
2261int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2262 const unsigned char *pwd, size_t pwdlen )
2263{
2264 int ret;
2265
Paul Bakker96743fc2011-02-12 14:30:57 +00002266#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002267 size_t len;
2268 pem_context pem;
2269
2270 pem_init( &pem );
2271 ret = pem_read_buffer( &pem,
2272 "-----BEGIN RSA PRIVATE KEY-----",
2273 "-----END RSA PRIVATE KEY-----",
2274 key, pwd, pwdlen, &len );
2275 if( ret == 0 )
2276 {
2277 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2278 {
2279 rsa_free( rsa );
2280 }
2281
2282 pem_free( &pem );
2283 return( ret );
2284 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002285 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2286 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2287 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2288 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002289 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002290 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002291
2292 ret = pem_read_buffer( &pem,
2293 "-----BEGIN PRIVATE KEY-----",
2294 "-----END PRIVATE KEY-----",
2295 key, NULL, 0, &len );
2296 if( ret == 0 )
2297 {
2298 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2299 pem.buf, pem.buflen ) ) != 0 )
2300 {
2301 rsa_free( rsa );
2302 }
2303
2304 pem_free( &pem );
2305 return( ret );
2306 }
2307 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002308 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002309
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002310 ret = pem_read_buffer( &pem,
2311 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2312 "-----END ENCRYPTED PRIVATE KEY-----",
2313 key, NULL, 0, &len );
2314 if( ret == 0 )
2315 {
2316 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2317 pem.buf, pem.buflen,
2318 pwd, pwdlen ) ) != 0 )
2319 {
2320 rsa_free( rsa );
2321 }
2322
2323 pem_free( &pem );
2324 return( ret );
2325 }
2326 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002327 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002328#else
2329 ((void) pwd);
2330 ((void) pwdlen);
2331#endif /* POLARSSL_PEM_C */
2332
2333 // At this point we only know it's not a PEM formatted key. Could be any
2334 // of the known DER encoded private key formats
2335 //
2336 // We try the different DER format parsers to see if one passes without
2337 // error
2338 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002339 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2340 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002341 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002342 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002345 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002346
2347 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2348 {
2349 return( ret );
2350 }
2351
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002352 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2353 return( 0 );
2354
2355 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002356
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002357 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2358 return( 0 );
2359
2360 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002361
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002362 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363}
2364
2365/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002366 * Parse a public RSA key
2367 */
Paul Bakker23986e52011-04-24 08:57:21 +00002368int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002369{
Paul Bakker23986e52011-04-24 08:57:21 +00002370 int ret;
2371 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002372 unsigned char *p, *end;
2373 x509_buf alg_oid;
2374#if defined(POLARSSL_PEM_C)
2375 pem_context pem;
2376
2377 pem_init( &pem );
2378 ret = pem_read_buffer( &pem,
2379 "-----BEGIN PUBLIC KEY-----",
2380 "-----END PUBLIC KEY-----",
2381 key, NULL, 0, &len );
2382
2383 if( ret == 0 )
2384 {
2385 /*
2386 * Was PEM encoded
2387 */
2388 keylen = pem.buflen;
2389 }
Paul Bakker00b28602013-06-24 13:02:41 +02002390 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002391 {
2392 pem_free( &pem );
2393 return( ret );
2394 }
2395
2396 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2397#else
2398 p = (unsigned char *) key;
2399#endif
2400 end = p + keylen;
2401
2402 /*
2403 * PublicKeyInfo ::= SEQUENCE {
2404 * algorithm AlgorithmIdentifier,
2405 * PublicKey BIT STRING
2406 * }
2407 *
2408 * AlgorithmIdentifier ::= SEQUENCE {
2409 * algorithm OBJECT IDENTIFIER,
2410 * parameters ANY DEFINED BY algorithm OPTIONAL
2411 * }
2412 *
2413 * RSAPublicKey ::= SEQUENCE {
2414 * modulus INTEGER, -- n
2415 * publicExponent INTEGER -- e
2416 * }
2417 */
2418
2419 if( ( ret = asn1_get_tag( &p, end, &len,
2420 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2421 {
2422#if defined(POLARSSL_PEM_C)
2423 pem_free( &pem );
2424#endif
2425 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002426 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002427 }
2428
2429 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2430 {
2431#if defined(POLARSSL_PEM_C)
2432 pem_free( &pem );
2433#endif
2434 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002435 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002436 }
2437
2438 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2439 {
2440#if defined(POLARSSL_PEM_C)
2441 pem_free( &pem );
2442#endif
2443 rsa_free( rsa );
2444 return( ret );
2445 }
2446
2447 rsa->len = mpi_size( &rsa->N );
2448
2449#if defined(POLARSSL_PEM_C)
2450 pem_free( &pem );
2451#endif
2452
2453 return( 0 );
2454}
2455
Paul Bakkereaa89f82011-04-04 21:36:15 +00002456#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002457/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002458 * Parse DHM parameters
2459 */
Paul Bakker23986e52011-04-24 08:57:21 +00002460int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002461{
Paul Bakker23986e52011-04-24 08:57:21 +00002462 int ret;
2463 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002464 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002465#if defined(POLARSSL_PEM_C)
2466 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002467
Paul Bakker96743fc2011-02-12 14:30:57 +00002468 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002469
Paul Bakker96743fc2011-02-12 14:30:57 +00002470 ret = pem_read_buffer( &pem,
2471 "-----BEGIN DH PARAMETERS-----",
2472 "-----END DH PARAMETERS-----",
2473 dhmin, NULL, 0, &dhminlen );
2474
2475 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002476 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002477 /*
2478 * Was PEM encoded
2479 */
2480 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002481 }
Paul Bakker00b28602013-06-24 13:02:41 +02002482 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002483 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002484 pem_free( &pem );
2485 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002486 }
2487
Paul Bakker96743fc2011-02-12 14:30:57 +00002488 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2489#else
2490 p = (unsigned char *) dhmin;
2491#endif
2492 end = p + dhminlen;
2493
Paul Bakker1b57b062011-01-06 15:48:19 +00002494 memset( dhm, 0, sizeof( dhm_context ) );
2495
Paul Bakker1b57b062011-01-06 15:48:19 +00002496 /*
2497 * DHParams ::= SEQUENCE {
2498 * prime INTEGER, -- P
2499 * generator INTEGER, -- g
2500 * }
2501 */
2502 if( ( ret = asn1_get_tag( &p, end, &len,
2503 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2504 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002505#if defined(POLARSSL_PEM_C)
2506 pem_free( &pem );
2507#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002508 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002509 }
2510
2511 end = p + len;
2512
2513 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2514 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2515 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002516#if defined(POLARSSL_PEM_C)
2517 pem_free( &pem );
2518#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002519 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002520 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002521 }
2522
2523 if( p != end )
2524 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002525#if defined(POLARSSL_PEM_C)
2526 pem_free( &pem );
2527#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002528 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002529 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002530 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2531 }
2532
Paul Bakker96743fc2011-02-12 14:30:57 +00002533#if defined(POLARSSL_PEM_C)
2534 pem_free( &pem );
2535#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002536
2537 return( 0 );
2538}
2539
Paul Bakker335db3f2011-04-25 15:28:35 +00002540#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002541/*
2542 * Load and parse a private RSA key
2543 */
2544int x509parse_dhmfile( dhm_context *dhm, const char *path )
2545{
2546 int ret;
2547 size_t n;
2548 unsigned char *buf;
2549
Paul Bakker69e095c2011-12-10 21:55:01 +00002550 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2551 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002552
Paul Bakker27fdf462011-06-09 13:55:13 +00002553 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002554
2555 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002556 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002557
2558 return( ret );
2559}
Paul Bakker335db3f2011-04-25 15:28:35 +00002560#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002561#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002562
Paul Bakker5121ce52009-01-03 21:22:43 +00002563#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564#include <stdarg.h>
2565
2566#if !defined vsnprintf
2567#define vsnprintf _vsnprintf
2568#endif // vsnprintf
2569
2570/*
2571 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2572 * Result value is not size of buffer needed, but -1 if no fit is possible.
2573 *
2574 * This fuction tries to 'fix' this by at least suggesting enlarging the
2575 * size by 20.
2576 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002577static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002578{
2579 va_list ap;
2580 int res = -1;
2581
2582 va_start( ap, format );
2583
2584 res = vsnprintf( str, size, format, ap );
2585
2586 va_end( ap );
2587
2588 // No quick fix possible
2589 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002590 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002591
2592 return res;
2593}
2594
2595#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002596#endif
2597
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2599
2600#define SAFE_SNPRINTF() \
2601{ \
2602 if( ret == -1 ) \
2603 return( -1 ); \
2604 \
Paul Bakker23986e52011-04-24 08:57:21 +00002605 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002606 p[n - 1] = '\0'; \
2607 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2608 } \
2609 \
Paul Bakker23986e52011-04-24 08:57:21 +00002610 n -= (unsigned int) ret; \
2611 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002612}
2613
Paul Bakker5121ce52009-01-03 21:22:43 +00002614/*
2615 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002616 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002618int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619{
Paul Bakker23986e52011-04-24 08:57:21 +00002620 int ret;
2621 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002623 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002624 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 char s[128], *p;
2626
2627 memset( s, 0, sizeof( s ) );
2628
2629 name = dn;
2630 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002631 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002632
2633 while( name != NULL )
2634 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002635 if( !name->oid.p )
2636 {
2637 name = name->next;
2638 continue;
2639 }
2640
Paul Bakker74111d32011-01-15 16:57:55 +00002641 if( name != dn )
2642 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002643 ret = snprintf( p, n, ", " );
2644 SAFE_SNPRINTF();
2645 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Paul Bakkerc70b9822013-04-07 22:00:46 +02002647 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Paul Bakkerc70b9822013-04-07 22:00:46 +02002649 if( ret == 0 )
2650 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002652 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002653 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
2655 for( i = 0; i < name->val.len; i++ )
2656 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002657 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 break;
2659
2660 c = name->val.p[i];
2661 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2662 s[i] = '?';
2663 else s[i] = c;
2664 }
2665 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002666 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002667 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 name = name->next;
2669 }
2670
Paul Bakker23986e52011-04-24 08:57:21 +00002671 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672}
2673
2674/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002675 * Store the serial in printable form into buf; no more
2676 * than size characters will be written
2677 */
2678int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2679{
Paul Bakker23986e52011-04-24 08:57:21 +00002680 int ret;
2681 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002682 char *p;
2683
2684 p = buf;
2685 n = size;
2686
2687 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002688 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002689
2690 for( i = 0; i < nr; i++ )
2691 {
Paul Bakker93048802011-12-05 14:38:06 +00002692 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002693 continue;
2694
Paul Bakkerdd476992011-01-16 21:34:59 +00002695 ret = snprintf( p, n, "%02X%s",
2696 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2697 SAFE_SNPRINTF();
2698 }
2699
Paul Bakker03c7c252011-11-25 12:37:37 +00002700 if( nr != serial->len )
2701 {
2702 ret = snprintf( p, n, "...." );
2703 SAFE_SNPRINTF();
2704 }
2705
Paul Bakker23986e52011-04-24 08:57:21 +00002706 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002707}
2708
2709/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002710 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002712int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2713 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002714{
Paul Bakker23986e52011-04-24 08:57:21 +00002715 int ret;
2716 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002717 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002718 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002721 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Paul Bakkerd98030e2009-05-02 15:13:40 +00002723 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002725 SAFE_SNPRINTF();
2726 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002728 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Paul Bakkerdd476992011-01-16 21:34:59 +00002730 ret = x509parse_serial_gets( p, n, &crt->serial);
2731 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Paul Bakkerd98030e2009-05-02 15:13:40 +00002733 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2734 SAFE_SNPRINTF();
2735 ret = x509parse_dn_gets( p, n, &crt->issuer );
2736 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Paul Bakkerd98030e2009-05-02 15:13:40 +00002738 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2739 SAFE_SNPRINTF();
2740 ret = x509parse_dn_gets( p, n, &crt->subject );
2741 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2745 crt->valid_from.year, crt->valid_from.mon,
2746 crt->valid_from.day, crt->valid_from.hour,
2747 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2752 crt->valid_to.year, crt->valid_to.mon,
2753 crt->valid_to.day, crt->valid_to.hour,
2754 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002755 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Paul Bakkerc70b9822013-04-07 22:00:46 +02002757 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002758 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002759
Paul Bakkerc70b9822013-04-07 22:00:46 +02002760 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2761 if( ret != 0 )
2762 ret = snprintf( p, n, "???" );
2763 else
2764 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002765 SAFE_SNPRINTF();
2766
2767 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002768 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002769 SAFE_SNPRINTF();
2770
Paul Bakker23986e52011-04-24 08:57:21 +00002771 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772}
2773
Paul Bakker74111d32011-01-15 16:57:55 +00002774/*
2775 * Return an informational string describing the given OID
2776 */
2777const char *x509_oid_get_description( x509_buf *oid )
2778{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002779 const char *desc = NULL;
2780 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002781
Paul Bakkerc70b9822013-04-07 22:00:46 +02002782 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002783
Paul Bakkerc70b9822013-04-07 22:00:46 +02002784 if( ret != 0 )
2785 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002786
Paul Bakkerc70b9822013-04-07 22:00:46 +02002787 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002788}
2789
2790/* Return the x.y.z.... style numeric string for the given OID */
2791int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2792{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002793 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002794}
2795
Paul Bakkerd98030e2009-05-02 15:13:40 +00002796/*
2797 * Return an informational string about the CRL.
2798 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002799int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2800 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002801{
Paul Bakker23986e52011-04-24 08:57:21 +00002802 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002803 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002804 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002805 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002806 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002807
2808 p = buf;
2809 n = size;
2810
2811 ret = snprintf( p, n, "%sCRL version : %d",
2812 prefix, crl->version );
2813 SAFE_SNPRINTF();
2814
2815 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2816 SAFE_SNPRINTF();
2817 ret = x509parse_dn_gets( p, n, &crl->issuer );
2818 SAFE_SNPRINTF();
2819
2820 ret = snprintf( p, n, "\n%sthis update : " \
2821 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2822 crl->this_update.year, crl->this_update.mon,
2823 crl->this_update.day, crl->this_update.hour,
2824 crl->this_update.min, crl->this_update.sec );
2825 SAFE_SNPRINTF();
2826
2827 ret = snprintf( p, n, "\n%snext update : " \
2828 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2829 crl->next_update.year, crl->next_update.mon,
2830 crl->next_update.day, crl->next_update.hour,
2831 crl->next_update.min, crl->next_update.sec );
2832 SAFE_SNPRINTF();
2833
2834 entry = &crl->entry;
2835
2836 ret = snprintf( p, n, "\n%sRevoked certificates:",
2837 prefix );
2838 SAFE_SNPRINTF();
2839
Paul Bakker9be19372009-07-27 20:21:53 +00002840 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002841 {
2842 ret = snprintf( p, n, "\n%sserial number: ",
2843 prefix );
2844 SAFE_SNPRINTF();
2845
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002846 ret = x509parse_serial_gets( p, n, &entry->serial);
2847 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002848
Paul Bakkerd98030e2009-05-02 15:13:40 +00002849 ret = snprintf( p, n, " revocation date: " \
2850 "%04d-%02d-%02d %02d:%02d:%02d",
2851 entry->revocation_date.year, entry->revocation_date.mon,
2852 entry->revocation_date.day, entry->revocation_date.hour,
2853 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002854 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002855
2856 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 }
2858
Paul Bakkerc70b9822013-04-07 22:00:46 +02002859 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002860 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakkerc70b9822013-04-07 22:00:46 +02002862 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2863 if( ret != 0 )
2864 ret = snprintf( p, n, "???" );
2865 else
2866 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002867 SAFE_SNPRINTF();
2868
Paul Bakker1e27bb22009-07-19 20:25:25 +00002869 ret = snprintf( p, n, "\n" );
2870 SAFE_SNPRINTF();
2871
Paul Bakker23986e52011-04-24 08:57:21 +00002872 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873}
2874
2875/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002876 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002878int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879{
Paul Bakkercce9d772011-11-18 14:26:47 +00002880 int year, mon, day;
2881 int hour, min, sec;
2882
2883#if defined(_WIN32)
2884 SYSTEMTIME st;
2885
2886 GetLocalTime(&st);
2887
2888 year = st.wYear;
2889 mon = st.wMonth;
2890 day = st.wDay;
2891 hour = st.wHour;
2892 min = st.wMinute;
2893 sec = st.wSecond;
2894#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 struct tm *lt;
2896 time_t tt;
2897
2898 tt = time( NULL );
2899 lt = localtime( &tt );
2900
Paul Bakkercce9d772011-11-18 14:26:47 +00002901 year = lt->tm_year + 1900;
2902 mon = lt->tm_mon + 1;
2903 day = lt->tm_mday;
2904 hour = lt->tm_hour;
2905 min = lt->tm_min;
2906 sec = lt->tm_sec;
2907#endif
2908
2909 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002910 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakkercce9d772011-11-18 14:26:47 +00002912 if( year == to->year &&
2913 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002914 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakkercce9d772011-11-18 14:26:47 +00002916 if( year == to->year &&
2917 mon == to->mon &&
2918 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002919 return( 1 );
2920
Paul Bakkercce9d772011-11-18 14:26:47 +00002921 if( year == to->year &&
2922 mon == to->mon &&
2923 day == to->day &&
2924 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002925 return( 1 );
2926
Paul Bakkercce9d772011-11-18 14:26:47 +00002927 if( year == to->year &&
2928 mon == to->mon &&
2929 day == to->day &&
2930 hour == to->hour &&
2931 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002932 return( 1 );
2933
Paul Bakkercce9d772011-11-18 14:26:47 +00002934 if( year == to->year &&
2935 mon == to->mon &&
2936 day == to->day &&
2937 hour == to->hour &&
2938 min == to->min &&
2939 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002940 return( 1 );
2941
Paul Bakker40ea7de2009-05-03 10:18:48 +00002942 return( 0 );
2943}
2944
2945/*
2946 * Return 1 if the certificate is revoked, or 0 otherwise.
2947 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002948int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002949{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002950 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002951
2952 while( cur != NULL && cur->serial.len != 0 )
2953 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002954 if( crt->serial.len == cur->serial.len &&
2955 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002956 {
2957 if( x509parse_time_expired( &cur->revocation_date ) )
2958 return( 1 );
2959 }
2960
2961 cur = cur->next;
2962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
2964 return( 0 );
2965}
2966
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002967/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002968 * Check that the given certificate is valid accoring to the CRL.
2969 */
2970static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2971 x509_crl *crl_list)
2972{
2973 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002974 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2975 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002976
Paul Bakker915275b2012-09-28 07:10:55 +00002977 if( ca == NULL )
2978 return( flags );
2979
Paul Bakker76fd75a2011-01-16 21:12:10 +00002980 /*
2981 * TODO: What happens if no CRL is present?
2982 * Suggestion: Revocation state should be unknown if no CRL is present.
2983 * For backwards compatibility this is not yet implemented.
2984 */
2985
Paul Bakker915275b2012-09-28 07:10:55 +00002986 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002987 {
Paul Bakker915275b2012-09-28 07:10:55 +00002988 if( crl_list->version == 0 ||
2989 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002990 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2991 crl_list->issuer_raw.len ) != 0 )
2992 {
2993 crl_list = crl_list->next;
2994 continue;
2995 }
2996
2997 /*
2998 * Check if CRL is correctly signed by the trusted CA
2999 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003000 md_info = md_info_from_type( crl_list->sig_md );
3001 if( md_info == NULL )
3002 {
3003 /*
3004 * Cannot check 'unknown' hash
3005 */
3006 flags |= BADCRL_NOT_TRUSTED;
3007 break;
3008 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003009
Paul Bakkerc70b9822013-04-07 22:00:46 +02003010 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003011
Paul Bakkerc70b9822013-04-07 22:00:46 +02003012 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003013 0, hash, crl_list->sig.p ) == 0 )
3014 {
3015 /*
3016 * CRL is not trusted
3017 */
3018 flags |= BADCRL_NOT_TRUSTED;
3019 break;
3020 }
3021
3022 /*
3023 * Check for validity of CRL (Do not drop out)
3024 */
3025 if( x509parse_time_expired( &crl_list->next_update ) )
3026 flags |= BADCRL_EXPIRED;
3027
3028 /*
3029 * Check if certificate is revoked
3030 */
3031 if( x509parse_revoked(crt, crl_list) )
3032 {
3033 flags |= BADCERT_REVOKED;
3034 break;
3035 }
3036
3037 crl_list = crl_list->next;
3038 }
3039 return flags;
3040}
3041
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003042static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003043{
3044 size_t i;
3045 size_t cn_idx = 0;
3046
Paul Bakker57b12982012-02-11 17:38:38 +00003047 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003048 return( 0 );
3049
3050 for( i = 0; i < strlen( cn ); ++i )
3051 {
3052 if( cn[i] == '.' )
3053 {
3054 cn_idx = i;
3055 break;
3056 }
3057 }
3058
3059 if( cn_idx == 0 )
3060 return( 0 );
3061
Paul Bakker535e97d2012-08-23 10:49:55 +00003062 if( strlen( cn ) - cn_idx == name->len - 1 &&
3063 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003064 {
3065 return( 1 );
3066 }
3067
3068 return( 0 );
3069}
3070
Paul Bakker915275b2012-09-28 07:10:55 +00003071static int x509parse_verify_top(
3072 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003073 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003074 int (*f_vrfy)(void *, x509_cert *, int, int *),
3075 void *p_vrfy )
3076{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003077 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003078 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003079 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3080 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003081
3082 if( x509parse_time_expired( &child->valid_to ) )
3083 *flags |= BADCERT_EXPIRED;
3084
3085 /*
3086 * Child is the top of the chain. Check against the trust_ca list.
3087 */
3088 *flags |= BADCERT_NOT_TRUSTED;
3089
3090 while( trust_ca != NULL )
3091 {
3092 if( trust_ca->version == 0 ||
3093 child->issuer_raw.len != trust_ca->subject_raw.len ||
3094 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3095 child->issuer_raw.len ) != 0 )
3096 {
3097 trust_ca = trust_ca->next;
3098 continue;
3099 }
3100
Paul Bakker9a736322012-11-14 12:39:52 +00003101 /*
3102 * Reduce path_len to check against if top of the chain is
3103 * the same as the trusted CA
3104 */
3105 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3106 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3107 child->issuer_raw.len ) == 0 )
3108 {
3109 check_path_cnt--;
3110 }
3111
Paul Bakker915275b2012-09-28 07:10:55 +00003112 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003113 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003114 {
3115 trust_ca = trust_ca->next;
3116 continue;
3117 }
3118
Paul Bakkerc70b9822013-04-07 22:00:46 +02003119 md_info = md_info_from_type( child->sig_md );
3120 if( md_info == NULL )
3121 {
3122 /*
3123 * Cannot check 'unknown' hash
3124 */
3125 continue;
3126 }
Paul Bakker915275b2012-09-28 07:10:55 +00003127
Paul Bakkerc70b9822013-04-07 22:00:46 +02003128 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003129
Paul Bakkerc70b9822013-04-07 22:00:46 +02003130 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003131 0, hash, child->sig.p ) != 0 )
3132 {
3133 trust_ca = trust_ca->next;
3134 continue;
3135 }
3136
3137 /*
3138 * Top of chain is signed by a trusted CA
3139 */
3140 *flags &= ~BADCERT_NOT_TRUSTED;
3141 break;
3142 }
3143
Paul Bakker9a736322012-11-14 12:39:52 +00003144 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003145 * If top of chain is not the same as the trusted CA send a verify request
3146 * to the callback for any issues with validity and CRL presence for the
3147 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003148 */
3149 if( trust_ca != NULL &&
3150 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3151 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3152 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003153 {
3154 /* Check trusted CA's CRL for then chain's top crt */
3155 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3156
3157 if( x509parse_time_expired( &trust_ca->valid_to ) )
3158 ca_flags |= BADCERT_EXPIRED;
3159
Paul Bakker915275b2012-09-28 07:10:55 +00003160 if( NULL != f_vrfy )
3161 {
Paul Bakker9a736322012-11-14 12:39:52 +00003162 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003163 return( ret );
3164 }
3165 }
3166
3167 /* Call callback on top cert */
3168 if( NULL != f_vrfy )
3169 {
Paul Bakker9a736322012-11-14 12:39:52 +00003170 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003171 return( ret );
3172 }
3173
Paul Bakker915275b2012-09-28 07:10:55 +00003174 *flags |= ca_flags;
3175
3176 return( 0 );
3177}
3178
3179static int x509parse_verify_child(
3180 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003181 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003182 int (*f_vrfy)(void *, x509_cert *, int, int *),
3183 void *p_vrfy )
3184{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003185 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003186 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003187 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003188 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003189 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003190
3191 if( x509parse_time_expired( &child->valid_to ) )
3192 *flags |= BADCERT_EXPIRED;
3193
Paul Bakkerc70b9822013-04-07 22:00:46 +02003194 md_info = md_info_from_type( child->sig_md );
3195 if( md_info == NULL )
3196 {
3197 /*
3198 * Cannot check 'unknown' hash
3199 */
Paul Bakker915275b2012-09-28 07:10:55 +00003200 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003201 }
3202 else
3203 {
3204 md( md_info, child->tbs.p, child->tbs.len, hash );
3205
3206 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3207 child->sig.p ) != 0 )
3208 *flags |= BADCERT_NOT_TRUSTED;
3209 }
3210
Paul Bakker915275b2012-09-28 07:10:55 +00003211 /* Check trusted CA's CRL for the given crt */
3212 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3213
3214 grandparent = parent->next;
3215
3216 while( grandparent != NULL )
3217 {
3218 if( grandparent->version == 0 ||
3219 grandparent->ca_istrue == 0 ||
3220 parent->issuer_raw.len != grandparent->subject_raw.len ||
3221 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3222 parent->issuer_raw.len ) != 0 )
3223 {
3224 grandparent = grandparent->next;
3225 continue;
3226 }
3227 break;
3228 }
3229
Paul Bakker915275b2012-09-28 07:10:55 +00003230 if( grandparent != NULL )
3231 {
3232 /*
3233 * Part of the chain
3234 */
Paul Bakker9a736322012-11-14 12:39:52 +00003235 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 +00003236 if( ret != 0 )
3237 return( ret );
3238 }
3239 else
3240 {
Paul Bakker9a736322012-11-14 12:39:52 +00003241 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 +00003242 if( ret != 0 )
3243 return( ret );
3244 }
3245
3246 /* child is verified to be a child of the parent, call verify callback */
3247 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003248 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003249 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003250
3251 *flags |= parent_flags;
3252
3253 return( 0 );
3254}
3255
Paul Bakker76fd75a2011-01-16 21:12:10 +00003256/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 * Verify the certificate validity
3258 */
3259int x509parse_verify( x509_cert *crt,
3260 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003261 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003262 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003263 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003264 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003265{
Paul Bakker23986e52011-04-24 08:57:21 +00003266 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003267 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003268 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003269 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003271 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Paul Bakker40ea7de2009-05-03 10:18:48 +00003273 *flags = 0;
3274
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 if( cn != NULL )
3276 {
3277 name = &crt->subject;
3278 cn_len = strlen( cn );
3279
Paul Bakker4d2c1242012-05-10 14:12:46 +00003280 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003282 cur = &crt->subject_alt_names;
3283
3284 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003285 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003286 if( cur->buf.len == cn_len &&
3287 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003288 break;
3289
Paul Bakker535e97d2012-08-23 10:49:55 +00003290 if( cur->buf.len > 2 &&
3291 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003292 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003293 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Paul Bakker4d2c1242012-05-10 14:12:46 +00003295 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003296 }
3297
3298 if( cur == NULL )
3299 *flags |= BADCERT_CN_MISMATCH;
3300 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003301 else
3302 {
3303 while( name != NULL )
3304 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003305 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003306 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003307 if( name->val.len == cn_len &&
3308 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003309 break;
3310
Paul Bakker535e97d2012-08-23 10:49:55 +00003311 if( name->val.len > 2 &&
3312 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003313 x509_wildcard_verify( cn, &name->val ) )
3314 break;
3315 }
3316
3317 name = name->next;
3318 }
3319
3320 if( name == NULL )
3321 *flags |= BADCERT_CN_MISMATCH;
3322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 }
3324
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003326 * Iterate upwards in the given cert chain, to find our crt parent.
3327 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003329 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
Paul Bakker76fd75a2011-01-16 21:12:10 +00003331 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003333 if( parent->ca_istrue == 0 ||
3334 crt->issuer_raw.len != parent->subject_raw.len ||
3335 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 crt->issuer_raw.len ) != 0 )
3337 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003338 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 continue;
3340 }
Paul Bakker915275b2012-09-28 07:10:55 +00003341 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
3343
Paul Bakker915275b2012-09-28 07:10:55 +00003344 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 {
Paul Bakker915275b2012-09-28 07:10:55 +00003346 /*
3347 * Part of the chain
3348 */
Paul Bakker9a736322012-11-14 12:39:52 +00003349 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003350 if( ret != 0 )
3351 return( ret );
3352 }
3353 else
Paul Bakker74111d32011-01-15 16:57:55 +00003354 {
Paul Bakker9a736322012-11-14 12:39:52 +00003355 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003356 if( ret != 0 )
3357 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003358 }
Paul Bakker915275b2012-09-28 07:10:55 +00003359
3360 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003361 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003362
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 return( 0 );
3364}
3365
3366/*
3367 * Unallocate all certificate data
3368 */
3369void x509_free( x509_cert *crt )
3370{
3371 x509_cert *cert_cur = crt;
3372 x509_cert *cert_prv;
3373 x509_name *name_cur;
3374 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003375 x509_sequence *seq_cur;
3376 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
3378 if( crt == NULL )
3379 return;
3380
3381 do
3382 {
3383 rsa_free( &cert_cur->rsa );
3384
3385 name_cur = cert_cur->issuer.next;
3386 while( name_cur != NULL )
3387 {
3388 name_prv = name_cur;
3389 name_cur = name_cur->next;
3390 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003391 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003392 }
3393
3394 name_cur = cert_cur->subject.next;
3395 while( name_cur != NULL )
3396 {
3397 name_prv = name_cur;
3398 name_cur = name_cur->next;
3399 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003400 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 }
3402
Paul Bakker74111d32011-01-15 16:57:55 +00003403 seq_cur = cert_cur->ext_key_usage.next;
3404 while( seq_cur != NULL )
3405 {
3406 seq_prv = seq_cur;
3407 seq_cur = seq_cur->next;
3408 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003409 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003410 }
3411
Paul Bakker8afa70d2012-02-11 18:42:45 +00003412 seq_cur = cert_cur->subject_alt_names.next;
3413 while( seq_cur != NULL )
3414 {
3415 seq_prv = seq_cur;
3416 seq_cur = seq_cur->next;
3417 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003418 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003419 }
3420
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 if( cert_cur->raw.p != NULL )
3422 {
3423 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003424 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 }
3426
3427 cert_cur = cert_cur->next;
3428 }
3429 while( cert_cur != NULL );
3430
3431 cert_cur = crt;
3432 do
3433 {
3434 cert_prv = cert_cur;
3435 cert_cur = cert_cur->next;
3436
3437 memset( cert_prv, 0, sizeof( x509_cert ) );
3438 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003439 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 }
3441 while( cert_cur != NULL );
3442}
3443
Paul Bakkerd98030e2009-05-02 15:13:40 +00003444/*
3445 * Unallocate all CRL data
3446 */
3447void x509_crl_free( x509_crl *crl )
3448{
3449 x509_crl *crl_cur = crl;
3450 x509_crl *crl_prv;
3451 x509_name *name_cur;
3452 x509_name *name_prv;
3453 x509_crl_entry *entry_cur;
3454 x509_crl_entry *entry_prv;
3455
3456 if( crl == NULL )
3457 return;
3458
3459 do
3460 {
3461 name_cur = crl_cur->issuer.next;
3462 while( name_cur != NULL )
3463 {
3464 name_prv = name_cur;
3465 name_cur = name_cur->next;
3466 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003467 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003468 }
3469
3470 entry_cur = crl_cur->entry.next;
3471 while( entry_cur != NULL )
3472 {
3473 entry_prv = entry_cur;
3474 entry_cur = entry_cur->next;
3475 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003476 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003477 }
3478
3479 if( crl_cur->raw.p != NULL )
3480 {
3481 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003482 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003483 }
3484
3485 crl_cur = crl_cur->next;
3486 }
3487 while( crl_cur != NULL );
3488
3489 crl_cur = crl;
3490 do
3491 {
3492 crl_prv = crl_cur;
3493 crl_cur = crl_cur->next;
3494
3495 memset( crl_prv, 0, sizeof( x509_crl ) );
3496 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003497 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003498 }
3499 while( crl_cur != NULL );
3500}
3501
Paul Bakker40e46942009-01-03 21:51:57 +00003502#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
Paul Bakker40e46942009-01-03 21:51:57 +00003504#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
3506/*
3507 * Checkup routine
3508 */
3509int x509_self_test( int verbose )
3510{
Paul Bakker5690efc2011-05-26 13:16:06 +00003511#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003512 int ret;
3513 int flags;
3514 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 x509_cert cacert;
3516 x509_cert clicert;
3517 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003518#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003519 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003520#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
3522 if( verbose != 0 )
3523 printf( " X.509 certificate load: " );
3524
3525 memset( &clicert, 0, sizeof( x509_cert ) );
3526
Paul Bakker3c2122f2013-06-24 19:03:14 +02003527 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003528 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529 if( ret != 0 )
3530 {
3531 if( verbose != 0 )
3532 printf( "failed\n" );
3533
3534 return( ret );
3535 }
3536
3537 memset( &cacert, 0, sizeof( x509_cert ) );
3538
Paul Bakker3c2122f2013-06-24 19:03:14 +02003539 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003540 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 if( ret != 0 )
3542 {
3543 if( verbose != 0 )
3544 printf( "failed\n" );
3545
3546 return( ret );
3547 }
3548
3549 if( verbose != 0 )
3550 printf( "passed\n X.509 private key load: " );
3551
3552 i = strlen( test_ca_key );
3553 j = strlen( test_ca_pwd );
3554
Paul Bakker66b78b22011-03-25 14:22:50 +00003555 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3556
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003558 (const unsigned char *) test_ca_key, i,
3559 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003560 {
3561 if( verbose != 0 )
3562 printf( "failed\n" );
3563
3564 return( ret );
3565 }
3566
3567 if( verbose != 0 )
3568 printf( "passed\n X.509 signature verify: ");
3569
Paul Bakker23986e52011-04-24 08:57:21 +00003570 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 if( ret != 0 )
3572 {
Paul Bakker23986e52011-04-24 08:57:21 +00003573 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 if( verbose != 0 )
3575 printf( "failed\n" );
3576
3577 return( ret );
3578 }
3579
Paul Bakker5690efc2011-05-26 13:16:06 +00003580#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003582 printf( "passed\n X.509 DHM parameter load: " );
3583
3584 i = strlen( test_dhm_params );
3585 j = strlen( test_ca_pwd );
3586
Paul Bakker3c2122f2013-06-24 19:03:14 +02003587 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003588 {
3589 if( verbose != 0 )
3590 printf( "failed\n" );
3591
3592 return( ret );
3593 }
3594
3595 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
3599 x509_free( &cacert );
3600 x509_free( &clicert );
3601 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003602#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003603 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003605
3606 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003607#else
3608 ((void) verbose);
3609 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003611}
3612
3613#endif
3614
3615#endif