blob: 0335db4b7894dfff15d699758142d93ba031c3b3 [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 Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010046#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010048#endif
49#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010051#endif
52#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010054#endif
55#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010057#endif
58#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000059#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010060#endif
61#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000062#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010063#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000064#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020065#if defined(POLARSSL_PKCS5_C)
66#include "polarssl/pkcs5.h"
67#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020068#if defined(POLARSSL_PKCS12_C)
69#include "polarssl/pkcs12.h"
70#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72#include <string.h>
73#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000074#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000075#include <windows.h>
76#else
Paul Bakker5121ce52009-01-03 21:22:43 +000077#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Paul Bakker335db3f2011-04-25 15:28:35 +000080#if defined(POLARSSL_FS_IO)
81#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000082#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000083#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020084#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000085#include <dirent.h>
86#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000087#endif
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089/*
Paul Bakker5121ce52009-01-03 21:22:43 +000090 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
91 */
92static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000093 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000094 int *ver )
95{
Paul Bakker23986e52011-04-24 08:57:21 +000096 int ret;
97 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99 if( ( ret = asn1_get_tag( p, end, &len,
100 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
101 {
Paul Bakker40e46942009-01-03 21:51:57 +0000102 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000103 {
104 *ver = 0;
105 return( 0 );
106 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( ret );
109 }
110
111 end = *p + len;
112
113 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000114 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000117 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000118 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120 return( 0 );
121}
122
123/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000124 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000125 */
126static int x509_crl_get_version( unsigned char **p,
127 const unsigned char *end,
128 int *ver )
129{
130 int ret;
131
132 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
133 {
134 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000135 {
136 *ver = 0;
137 return( 0 );
138 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000139
140 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
141 }
142
143 return( 0 );
144}
145
146/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 * CertificateSerialNumber ::= INTEGER
148 */
149static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000150 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 x509_buf *serial )
152{
153 int ret;
154
155 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000156 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000157 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
160 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000161 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000162 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 serial->tag = *(*p)++;
165
166 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000167 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 serial->p = *p;
170 *p += serial->len;
171
172 return( 0 );
173}
174
175/*
176 * AlgorithmIdentifier ::= SEQUENCE {
177 * algorithm OBJECT IDENTIFIER,
178 * parameters ANY DEFINED BY algorithm OPTIONAL }
179 */
180static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000181 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 x509_buf *alg )
183{
Paul Bakker23986e52011-04-24 08:57:21 +0000184 int ret;
185 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187 if( ( ret = asn1_get_tag( p, end, &len,
188 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000189 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 end = *p + len;
192 alg->tag = **p;
193
194 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 alg->p = *p;
198 *p += alg->len;
199
200 if( *p == end )
201 return( 0 );
202
203 /*
204 * assume the algorithm parameters must be NULL
205 */
206 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000207 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000210 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000211 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 return( 0 );
214}
215
216/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 * AttributeTypeAndValue ::= SEQUENCE {
218 * type AttributeType,
219 * value AttributeValue }
220 *
221 * AttributeType ::= OBJECT IDENTIFIER
222 *
223 * AttributeValue ::= ANY DEFINED BY AttributeType
224 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000225static int x509_get_attr_type_value( unsigned char **p,
226 const unsigned char *end,
227 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228{
Paul Bakker23986e52011-04-24 08:57:21 +0000229 int ret;
230 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 x509_buf *oid;
232 x509_buf *val;
233
234 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 oid = &cur->oid;
239 oid->tag = **p;
240
241 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 oid->p = *p;
245 *p += oid->len;
246
247 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000248 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000249 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
252 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
253 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000254 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000255 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257 val = &cur->val;
258 val->tag = *(*p)++;
259
260 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000261 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263 val->p = *p;
264 *p += val->len;
265
266 cur->next = NULL;
267
Paul Bakker400ff6f2011-02-20 10:40:16 +0000268 return( 0 );
269}
270
271/*
272 * RelativeDistinguishedName ::=
273 * SET OF AttributeTypeAndValue
274 *
275 * AttributeTypeAndValue ::= SEQUENCE {
276 * type AttributeType,
277 * value AttributeValue }
278 *
279 * AttributeType ::= OBJECT IDENTIFIER
280 *
281 * AttributeValue ::= ANY DEFINED BY AttributeType
282 */
283static int x509_get_name( unsigned char **p,
284 const unsigned char *end,
285 x509_name *cur )
286{
Paul Bakker23986e52011-04-24 08:57:21 +0000287 int ret;
288 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000289 const unsigned char *end2;
290 x509_name *use;
291
292 if( ( ret = asn1_get_tag( p, end, &len,
293 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000294 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000295
296 end2 = end;
297 end = *p + len;
298 use = cur;
299
300 do
301 {
302 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
303 return( ret );
304
305 if( *p != end )
306 {
307 use->next = (x509_name *) malloc(
308 sizeof( x509_name ) );
309
310 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000311 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000312
313 memset( use->next, 0, sizeof( x509_name ) );
314
315 use = use->next;
316 }
317 }
318 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 /*
321 * recurse until end of SEQUENCE is reached
322 */
323 if( *p == end2 )
324 return( 0 );
325
326 cur->next = (x509_name *) malloc(
327 sizeof( x509_name ) );
328
329 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000330 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Paul Bakker430ffbe2012-05-01 08:14:20 +0000332 memset( cur->next, 0, sizeof( x509_name ) );
333
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 return( x509_get_name( p, end2, cur->next ) );
335}
336
337/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * Time ::= CHOICE {
339 * utcTime UTCTime,
340 * generalTime GeneralizedTime }
341 */
Paul Bakker91200182010-02-18 21:26:15 +0000342static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000343 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344 x509_time *time )
345{
Paul Bakker23986e52011-04-24 08:57:21 +0000346 int ret;
347 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000349 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000350
Paul Bakker91200182010-02-18 21:26:15 +0000351 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000352 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
353 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000354
Paul Bakker91200182010-02-18 21:26:15 +0000355 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356
Paul Bakker91200182010-02-18 21:26:15 +0000357 if ( tag == ASN1_UTC_TIME )
358 {
359 (*p)++;
360 ret = asn1_get_len( p, end, &len );
361
362 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000363 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000364
Paul Bakker91200182010-02-18 21:26:15 +0000365 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000366 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
367 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368
Paul Bakker91200182010-02-18 21:26:15 +0000369 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
370 &time->year, &time->mon, &time->day,
371 &time->hour, &time->min, &time->sec ) < 5 )
372 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000373
Paul Bakker400ff6f2011-02-20 10:40:16 +0000374 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000375 time->year += 1900;
376
377 *p += len;
378
379 return( 0 );
380 }
381 else if ( tag == ASN1_GENERALIZED_TIME )
382 {
383 (*p)++;
384 ret = asn1_get_len( p, end, &len );
385
386 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000387 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000388
389 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000390 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
391 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000392
393 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
394 &time->year, &time->mon, &time->day,
395 &time->hour, &time->min, &time->sec ) < 5 )
396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
397
398 *p += len;
399
400 return( 0 );
401 }
402 else
Paul Bakker9d781402011-05-09 16:17:09 +0000403 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404}
405
406
407/*
408 * Validity ::= SEQUENCE {
409 * notBefore Time,
410 * notAfter Time }
411 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000412static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000413 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 x509_time *from,
415 x509_time *to )
416{
Paul Bakker23986e52011-04-24 08:57:21 +0000417 int ret;
418 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( ( ret = asn1_get_tag( p, end, &len,
421 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000422 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 end = *p + len;
425
Paul Bakker91200182010-02-18 21:26:15 +0000426 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
Paul Bakker91200182010-02-18 21:26:15 +0000429 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000433 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000434 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 return( 0 );
437}
438
439/*
440 * SubjectPublicKeyInfo ::= SEQUENCE {
441 * algorithm AlgorithmIdentifier,
442 * subjectPublicKey BIT STRING }
443 */
444static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000445 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 x509_buf *pk_alg_oid,
447 mpi *N, mpi *E )
448{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200449 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000450 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200452 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
455 return( ret );
456
457 /*
458 * only RSA public keys handled at this time
459 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200460 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000461 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000464 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
466 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000467 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000468 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 end2 = *p + len;
471
472 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000473 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 /*
476 * RSAPublicKey ::= SEQUENCE {
477 * modulus INTEGER, -- n
478 * publicExponent INTEGER -- e
479 * }
480 */
481 if( ( ret = asn1_get_tag( p, end2, &len,
482 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000483 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000486 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000487 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
490 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000491 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000494 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000495 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497 return( 0 );
498}
499
500static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000501 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 x509_buf *sig )
503{
Paul Bakker23986e52011-04-24 08:57:21 +0000504 int ret;
505 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker8afa70d2012-02-11 18:42:45 +0000507 if( ( end - *p ) < 1 )
508 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
509 POLARSSL_ERR_ASN1_OUT_OF_DATA );
510
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 sig->tag = **p;
512
513 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000514 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker74111d32011-01-15 16:57:55 +0000516
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000518 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 sig->len = len;
521 sig->p = *p;
522
523 *p += len;
524
525 return( 0 );
526}
527
528/*
529 * X.509 v2/v3 unique identifier (not parsed)
530 */
531static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000532 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 x509_buf *uid, int n )
534{
535 int ret;
536
537 if( *p == end )
538 return( 0 );
539
540 uid->tag = **p;
541
542 if( ( ret = asn1_get_tag( p, end, &uid->len,
543 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
544 {
Paul Bakker40e46942009-01-03 21:51:57 +0000545 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 return( 0 );
547
548 return( ret );
549 }
550
551 uid->p = *p;
552 *p += uid->len;
553
554 return( 0 );
555}
556
557/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000558 * X.509 Extensions (No parsing of extensions, pointer should
559 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 */
561static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000562 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000563 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564{
Paul Bakker23986e52011-04-24 08:57:21 +0000565 int ret;
566 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 if( *p == end )
569 return( 0 );
570
571 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000572
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000574 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 ext->p = *p;
578 end = *p + ext->len;
579
580 /*
581 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
582 *
583 * Extension ::= SEQUENCE {
584 * extnID OBJECT IDENTIFIER,
585 * critical BOOLEAN DEFAULT FALSE,
586 * extnValue OCTET STRING }
587 */
588 if( ( ret = asn1_get_tag( p, end, &len,
589 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000590 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000593 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000594 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Paul Bakkerd98030e2009-05-02 15:13:40 +0000596 return( 0 );
597}
598
599/*
600 * X.509 CRL v2 extensions (no extensions parsed yet.)
601 */
602static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000603 const unsigned char *end,
604 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000605{
Paul Bakker23986e52011-04-24 08:57:21 +0000606 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000607 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000609 /* Get explicit tag */
610 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611 {
612 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
613 return( 0 );
614
615 return( ret );
616 }
617
618 while( *p < end )
619 {
620 if( ( ret = asn1_get_tag( p, end, &len,
621 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000622 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000623
624 *p += len;
625 }
626
627 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000629 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
630
631 return( 0 );
632}
633
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000634/*
635 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
636 */
637static int x509_get_crl_entry_ext( unsigned char **p,
638 const unsigned char *end,
639 x509_buf *ext )
640{
641 int ret;
642 size_t len = 0;
643
644 /* OPTIONAL */
645 if (end <= *p)
646 return( 0 );
647
648 ext->tag = **p;
649 ext->p = *p;
650
651 /*
652 * Get CRL-entry extension sequence header
653 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
654 */
655 if( ( ret = asn1_get_tag( p, end, &ext->len,
656 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
657 {
658 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
659 {
660 ext->p = NULL;
661 return( 0 );
662 }
663 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
664 }
665
666 end = *p + ext->len;
667
668 if( end != *p + ext->len )
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
670 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
671
672 while( *p < end )
673 {
674 if( ( ret = asn1_get_tag( p, end, &len,
675 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
676 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
677
678 *p += len;
679 }
680
681 if( *p != end )
682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
683 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
684
685 return( 0 );
686}
687
Paul Bakker74111d32011-01-15 16:57:55 +0000688static int x509_get_basic_constraints( unsigned char **p,
689 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000690 int *ca_istrue,
691 int *max_pathlen )
692{
Paul Bakker23986e52011-04-24 08:57:21 +0000693 int ret;
694 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000695
696 /*
697 * BasicConstraints ::= SEQUENCE {
698 * cA BOOLEAN DEFAULT FALSE,
699 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
700 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000701 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000702 *max_pathlen = 0; /* endless */
703
704 if( ( ret = asn1_get_tag( p, end, &len,
705 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000707
708 if( *p == end )
709 return 0;
710
Paul Bakker3cccddb2011-01-16 21:46:31 +0000711 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000712 {
713 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000714 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000715
716 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000717 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000718
Paul Bakker3cccddb2011-01-16 21:46:31 +0000719 if( *ca_istrue != 0 )
720 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000721 }
722
723 if( *p == end )
724 return 0;
725
726 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000727 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000728
729 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000731 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
732
733 (*max_pathlen)++;
734
Paul Bakker74111d32011-01-15 16:57:55 +0000735 return 0;
736}
737
738static int x509_get_ns_cert_type( unsigned char **p,
739 const unsigned char *end,
740 unsigned char *ns_cert_type)
741{
742 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000743 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000744
745 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000746 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000747
748 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000750 POLARSSL_ERR_ASN1_INVALID_LENGTH );
751
752 /* Get actual bitstring */
753 *ns_cert_type = *bs.p;
754 return 0;
755}
756
757static int x509_get_key_usage( unsigned char **p,
758 const unsigned char *end,
759 unsigned char *key_usage)
760{
761 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000762 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000763
764 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000765 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000766
Paul Bakker94a67962012-08-23 13:03:52 +0000767 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000769 POLARSSL_ERR_ASN1_INVALID_LENGTH );
770
771 /* Get actual bitstring */
772 *key_usage = *bs.p;
773 return 0;
774}
775
Paul Bakkerd98030e2009-05-02 15:13:40 +0000776/*
Paul Bakker74111d32011-01-15 16:57:55 +0000777 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
778 *
779 * KeyPurposeId ::= OBJECT IDENTIFIER
780 */
781static int x509_get_ext_key_usage( unsigned char **p,
782 const unsigned char *end,
783 x509_sequence *ext_key_usage)
784{
785 int ret;
786
787 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000788 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000789
790 /* Sequence length must be >= 1 */
791 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000792 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000793 POLARSSL_ERR_ASN1_INVALID_LENGTH );
794
795 return 0;
796}
797
798/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000799 * SubjectAltName ::= GeneralNames
800 *
801 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
802 *
803 * GeneralName ::= CHOICE {
804 * otherName [0] OtherName,
805 * rfc822Name [1] IA5String,
806 * dNSName [2] IA5String,
807 * x400Address [3] ORAddress,
808 * directoryName [4] Name,
809 * ediPartyName [5] EDIPartyName,
810 * uniformResourceIdentifier [6] IA5String,
811 * iPAddress [7] OCTET STRING,
812 * registeredID [8] OBJECT IDENTIFIER }
813 *
814 * OtherName ::= SEQUENCE {
815 * type-id OBJECT IDENTIFIER,
816 * value [0] EXPLICIT ANY DEFINED BY type-id }
817 *
818 * EDIPartyName ::= SEQUENCE {
819 * nameAssigner [0] DirectoryString OPTIONAL,
820 * partyName [1] DirectoryString }
821 *
822 * NOTE: PolarSSL only parses and uses dNSName at this point.
823 */
824static int x509_get_subject_alt_name( unsigned char **p,
825 const unsigned char *end,
826 x509_sequence *subject_alt_name )
827{
828 int ret;
829 size_t len, tag_len;
830 asn1_buf *buf;
831 unsigned char tag;
832 asn1_sequence *cur = subject_alt_name;
833
834 /* Get main sequence tag */
835 if( ( ret = asn1_get_tag( p, end, &len,
836 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
838
839 if( *p + len != end )
840 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
841 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
842
843 while( *p < end )
844 {
845 if( ( end - *p ) < 1 )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
847 POLARSSL_ERR_ASN1_OUT_OF_DATA );
848
849 tag = **p;
850 (*p)++;
851 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
852 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
853
854 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
856 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
857
858 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
859 {
860 *p += tag_len;
861 continue;
862 }
863
864 buf = &(cur->buf);
865 buf->tag = tag;
866 buf->p = *p;
867 buf->len = tag_len;
868 *p += buf->len;
869
870 /* Allocate and assign next pointer */
871 if (*p < end)
872 {
873 cur->next = (asn1_sequence *) malloc(
874 sizeof( asn1_sequence ) );
875
876 if( cur->next == NULL )
877 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
878 POLARSSL_ERR_ASN1_MALLOC_FAILED );
879
Paul Bakker535e97d2012-08-23 10:49:55 +0000880 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000881 cur = cur->next;
882 }
883 }
884
885 /* Set final sequence entry's next pointer to NULL */
886 cur->next = NULL;
887
888 if( *p != end )
889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
890 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
891
892 return( 0 );
893}
894
895/*
Paul Bakker74111d32011-01-15 16:57:55 +0000896 * X.509 v3 extensions
897 *
898 * TODO: Perform all of the basic constraints tests required by the RFC
899 * TODO: Set values for undetected extensions to a sane default?
900 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000901 */
902static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000903 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000904 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000905{
Paul Bakker23986e52011-04-24 08:57:21 +0000906 int ret;
907 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000908 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000909
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000910 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000911 {
912 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
913 return( 0 );
914
915 return( ret );
916 }
917
Paul Bakker5121ce52009-01-03 21:22:43 +0000918 while( *p < end )
919 {
Paul Bakker74111d32011-01-15 16:57:55 +0000920 /*
921 * Extension ::= SEQUENCE {
922 * extnID OBJECT IDENTIFIER,
923 * critical BOOLEAN DEFAULT FALSE,
924 * extnValue OCTET STRING }
925 */
926 x509_buf extn_oid = {0, 0, NULL};
927 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200928 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000929
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 if( ( ret = asn1_get_tag( p, end, &len,
931 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000932 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000934 end_ext_data = *p + len;
935
Paul Bakker74111d32011-01-15 16:57:55 +0000936 /* Get extension ID */
937 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Paul Bakker74111d32011-01-15 16:57:55 +0000939 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000940 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Paul Bakker74111d32011-01-15 16:57:55 +0000942 extn_oid.p = *p;
943 *p += extn_oid.len;
944
945 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000946 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000947 POLARSSL_ERR_ASN1_OUT_OF_DATA );
948
949 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000950 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000951 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000952 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Paul Bakker74111d32011-01-15 16:57:55 +0000954 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000955 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000959 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000960
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000961 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000963 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000964
Paul Bakker74111d32011-01-15 16:57:55 +0000965 /*
966 * Detect supported extensions
967 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200968 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
969
970 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000971 {
972 /* No parser found, skip extension */
973 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
Paul Bakker5c721f92011-07-27 16:51:09 +0000975#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000976 if( is_critical )
977 {
978 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000979 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000980 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
981 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000982#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200983 continue;
984 }
985
986 crt->ext_types |= ext_type;
987
988 switch( ext_type )
989 {
990 case EXT_BASIC_CONSTRAINTS:
991 /* Parse basic constraints */
992 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
993 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
994 return ( ret );
995 break;
996
997 case EXT_KEY_USAGE:
998 /* Parse key usage */
999 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1000 &crt->key_usage ) ) != 0 )
1001 return ( ret );
1002 break;
1003
1004 case EXT_EXTENDED_KEY_USAGE:
1005 /* Parse extended key usage */
1006 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1007 &crt->ext_key_usage ) ) != 0 )
1008 return ( ret );
1009 break;
1010
1011 case EXT_SUBJECT_ALT_NAME:
1012 /* Parse subject alt name */
1013 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1014 &crt->subject_alt_names ) ) != 0 )
1015 return ( ret );
1016 break;
1017
1018 case EXT_NS_CERT_TYPE:
1019 /* Parse netscape certificate type */
1020 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1021 &crt->ns_cert_type ) ) != 0 )
1022 return ( ret );
1023 break;
1024
1025 default:
1026 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 }
1029
1030 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001031 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001032 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034 return( 0 );
1035}
1036
1037/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001038 * X.509 CRL Entries
1039 */
1040static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001041 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001042 x509_crl_entry *entry )
1043{
Paul Bakker23986e52011-04-24 08:57:21 +00001044 int ret;
1045 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001046 x509_crl_entry *cur_entry = entry;
1047
1048 if( *p == end )
1049 return( 0 );
1050
Paul Bakker9be19372009-07-27 20:21:53 +00001051 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001052 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1053 {
1054 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1055 return( 0 );
1056
1057 return( ret );
1058 }
1059
Paul Bakker9be19372009-07-27 20:21:53 +00001060 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001061
1062 while( *p < end )
1063 {
Paul Bakker23986e52011-04-24 08:57:21 +00001064 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001065 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001066
1067 if( ( ret = asn1_get_tag( p, end, &len2,
1068 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1069 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001070 return( ret );
1071 }
1072
Paul Bakker9be19372009-07-27 20:21:53 +00001073 cur_entry->raw.tag = **p;
1074 cur_entry->raw.p = *p;
1075 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001076 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001077
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001078 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001079 return( ret );
1080
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001081 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 return( ret );
1083
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001084 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001085 return( ret );
1086
Paul Bakker74111d32011-01-15 16:57:55 +00001087 if ( *p < end )
1088 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001090
1091 if( cur_entry->next == NULL )
1092 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1093
Paul Bakkerd98030e2009-05-02 15:13:40 +00001094 cur_entry = cur_entry->next;
1095 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1096 }
1097 }
1098
1099 return( 0 );
1100}
1101
Paul Bakkerc70b9822013-04-07 22:00:46 +02001102static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1103 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001104{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001105 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001106
Paul Bakkerc70b9822013-04-07 22:00:46 +02001107 if( ret != 0 )
1108 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001109
Paul Bakkerc70b9822013-04-07 22:00:46 +02001110 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001111}
1112
Paul Bakkerd98030e2009-05-02 15:13:40 +00001113/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001114 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001116static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1117 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118{
Paul Bakker23986e52011-04-24 08:57:21 +00001119 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001120 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001121 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
Paul Bakker320a4b52009-03-28 18:52:39 +00001123 /*
1124 * Check for valid input
1125 */
1126 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001127 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001128
Paul Bakker96743fc2011-02-12 14:30:57 +00001129 p = (unsigned char *) malloc( len = buflen );
1130
1131 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001132 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001133
1134 memcpy( p, buf, buflen );
1135
1136 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
1138 crt->raw.p = p;
1139 crt->raw.len = len;
1140 end = p + len;
1141
1142 /*
1143 * Certificate ::= SEQUENCE {
1144 * tbsCertificate TBSCertificate,
1145 * signatureAlgorithm AlgorithmIdentifier,
1146 * signatureValue BIT STRING }
1147 */
1148 if( ( ret = asn1_get_tag( &p, end, &len,
1149 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1150 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001151 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001152 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
1154
Paul Bakkerb00ca422012-09-25 12:10:00 +00001155 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001156 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001157 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001158 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001159 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001161 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001162
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 /*
1164 * TBSCertificate ::= SEQUENCE {
1165 */
1166 crt->tbs.p = p;
1167
1168 if( ( ret = asn1_get_tag( &p, end, &len,
1169 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1170 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001171 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001172 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 }
1174
1175 end = p + len;
1176 crt->tbs.len = end - crt->tbs.p;
1177
1178 /*
1179 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1180 *
1181 * CertificateSerialNumber ::= INTEGER
1182 *
1183 * signature AlgorithmIdentifier
1184 */
1185 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1186 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1187 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1188 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001189 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001190 return( ret );
1191 }
1192
1193 crt->version++;
1194
1195 if( crt->version > 3 )
1196 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001197 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001198 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
Paul Bakkerc70b9822013-04-07 22:00:46 +02001201 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1202 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001204 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001205 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 }
1207
1208 /*
1209 * issuer Name
1210 */
1211 crt->issuer_raw.p = p;
1212
1213 if( ( ret = asn1_get_tag( &p, end, &len,
1214 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1215 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001216 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001217 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 }
1219
1220 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1221 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001222 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 return( ret );
1224 }
1225
1226 crt->issuer_raw.len = p - crt->issuer_raw.p;
1227
1228 /*
1229 * Validity ::= SEQUENCE {
1230 * notBefore Time,
1231 * notAfter Time }
1232 *
1233 */
1234 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1235 &crt->valid_to ) ) != 0 )
1236 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001237 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 return( ret );
1239 }
1240
1241 /*
1242 * subject Name
1243 */
1244 crt->subject_raw.p = p;
1245
1246 if( ( ret = asn1_get_tag( &p, end, &len,
1247 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001250 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 }
1252
Paul Bakkercefb3962012-06-27 11:51:09 +00001253 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001254 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001255 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 return( ret );
1257 }
1258
1259 crt->subject_raw.len = p - crt->subject_raw.p;
1260
1261 /*
1262 * SubjectPublicKeyInfo ::= SEQUENCE
1263 * algorithm AlgorithmIdentifier,
1264 * subjectPublicKey BIT STRING }
1265 */
1266 if( ( ret = asn1_get_tag( &p, end, &len,
1267 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1268 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001269 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001270 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
1272
1273 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1274 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1275 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001276 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 return( ret );
1278 }
1279
1280 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1281 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001282 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 return( ret );
1284 }
1285
1286 crt->rsa.len = mpi_size( &crt->rsa.N );
1287
1288 /*
1289 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1290 * -- If present, version shall be v2 or v3
1291 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1292 * -- If present, version shall be v2 or v3
1293 * extensions [3] EXPLICIT Extensions OPTIONAL
1294 * -- If present, version shall be v3
1295 */
1296 if( crt->version == 2 || crt->version == 3 )
1297 {
1298 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1299 if( ret != 0 )
1300 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001301 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 return( ret );
1303 }
1304 }
1305
1306 if( crt->version == 2 || crt->version == 3 )
1307 {
1308 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1309 if( ret != 0 )
1310 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001311 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 return( ret );
1313 }
1314 }
1315
1316 if( crt->version == 3 )
1317 {
Paul Bakker74111d32011-01-15 16:57:55 +00001318 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 if( ret != 0 )
1320 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001321 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 return( ret );
1323 }
1324 }
1325
1326 if( p != end )
1327 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001328 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001329 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001330 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 }
1332
Paul Bakkerb00ca422012-09-25 12:10:00 +00001333 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
1335 /*
1336 * signatureAlgorithm AlgorithmIdentifier,
1337 * signatureValue BIT STRING
1338 */
1339 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 return( ret );
1343 }
1344
Paul Bakker535e97d2012-08-23 10:49:55 +00001345 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1346 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001348 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001349 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
1351
1352 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 return( ret );
1356 }
1357
1358 if( p != end )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001361 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001362 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001365 return( 0 );
1366}
1367
1368/*
Paul Bakker42c65812013-06-24 19:21:59 +02001369 * Parse one X.509 certificate in DER format from a buffer and add them to a
1370 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001371 */
Paul Bakker42c65812013-06-24 19:21:59 +02001372int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001373{
Paul Bakker42c65812013-06-24 19:21:59 +02001374 int ret;
1375 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001376
1377 /*
1378 * Check for valid input
1379 */
1380 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001381 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001382
1383 while( crt->version != 0 && crt->next != NULL )
1384 {
1385 prev = crt;
1386 crt = crt->next;
1387 }
1388
1389 /*
1390 * Add new certificate on the end of the chain if needed.
1391 */
1392 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001393 {
1394 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1395
Paul Bakker7d06ad22009-05-02 15:53:56 +00001396 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001397 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001398
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001399 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001400 crt = crt->next;
1401 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Paul Bakker42c65812013-06-24 19:21:59 +02001404 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1405 {
1406 if( prev )
1407 prev->next = NULL;
1408
1409 if( crt != chain )
1410 free( crt );
1411
1412 return( ret );
1413 }
1414
1415 return( 0 );
1416}
1417
1418/*
1419 * Parse one or more PEM certificates from a buffer and add them to the chained list
1420 */
1421int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1422{
1423 int ret, success = 0, first_error = 0, total_failed = 0;
1424 int buf_format = X509_FORMAT_DER;
1425
1426 /*
1427 * Check for valid input
1428 */
1429 if( chain == NULL || buf == NULL )
1430 return( POLARSSL_ERR_X509_INVALID_INPUT );
1431
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001432 /*
1433 * Determine buffer content. Buffer contains either one DER certificate or
1434 * one or more PEM certificates.
1435 */
1436#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001437 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001438 buf_format = X509_FORMAT_PEM;
1439#endif
1440
1441 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001442 return x509parse_crt_der( chain, buf, buflen );
1443
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001444#if defined(POLARSSL_PEM_C)
1445 if( buf_format == X509_FORMAT_PEM )
1446 {
1447 pem_context pem;
1448
1449 while( buflen > 0 )
1450 {
1451 size_t use_len;
1452 pem_init( &pem );
1453
1454 ret = pem_read_buffer( &pem,
1455 "-----BEGIN CERTIFICATE-----",
1456 "-----END CERTIFICATE-----",
1457 buf, NULL, 0, &use_len );
1458
1459 if( ret == 0 )
1460 {
1461 /*
1462 * Was PEM encoded
1463 */
1464 buflen -= use_len;
1465 buf += use_len;
1466 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001467 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1468 {
1469 return( ret );
1470 }
Paul Bakker00b28602013-06-24 13:02:41 +02001471 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472 {
1473 pem_free( &pem );
1474
Paul Bakker5ed3b342013-06-24 19:05:46 +02001475 /*
1476 * PEM header and footer were found
1477 */
1478 buflen -= use_len;
1479 buf += use_len;
1480
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001481 if( first_error == 0 )
1482 first_error = ret;
1483
1484 continue;
1485 }
1486 else
1487 break;
1488
Paul Bakker42c65812013-06-24 19:21:59 +02001489 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001490
1491 pem_free( &pem );
1492
1493 if( ret != 0 )
1494 {
1495 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001496 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001497 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001498 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001499 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500
1501 if( first_error == 0 )
1502 first_error = ret;
1503
Paul Bakker42c65812013-06-24 19:21:59 +02001504 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001505 continue;
1506 }
1507
1508 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001509 }
1510 }
1511#endif
1512
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001513 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001514 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001515 else if( first_error )
1516 return( first_error );
1517 else
1518 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519}
1520
1521/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522 * Parse one or more CRLs and add them to the chained list
1523 */
Paul Bakker23986e52011-04-24 08:57:21 +00001524int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525{
Paul Bakker23986e52011-04-24 08:57:21 +00001526 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001527 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001528 unsigned char *p, *end;
1529 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001530#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001531 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001532 pem_context pem;
1533#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001534
1535 crl = chain;
1536
1537 /*
1538 * Check for valid input
1539 */
1540 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001541 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001542
1543 while( crl->version != 0 && crl->next != NULL )
1544 crl = crl->next;
1545
1546 /*
1547 * Add new CRL on the end of the chain if needed.
1548 */
1549 if ( crl->version != 0 && crl->next == NULL)
1550 {
1551 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1552
Paul Bakker7d06ad22009-05-02 15:53:56 +00001553 if( crl->next == NULL )
1554 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001556 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001557 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558
Paul Bakker7d06ad22009-05-02 15:53:56 +00001559 crl = crl->next;
1560 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001561 }
1562
Paul Bakker96743fc2011-02-12 14:30:57 +00001563#if defined(POLARSSL_PEM_C)
1564 pem_init( &pem );
1565 ret = pem_read_buffer( &pem,
1566 "-----BEGIN X509 CRL-----",
1567 "-----END X509 CRL-----",
1568 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001569
Paul Bakker96743fc2011-02-12 14:30:57 +00001570 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001572 /*
1573 * Was PEM encoded
1574 */
1575 buflen -= use_len;
1576 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577
1578 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001579 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001580 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001581 p = pem.buf;
1582 pem.buf = NULL;
1583 len = pem.buflen;
1584 pem_free( &pem );
1585 }
Paul Bakker00b28602013-06-24 13:02:41 +02001586 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001587 {
1588 pem_free( &pem );
1589 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001590 }
1591 else
1592 {
1593 /*
1594 * nope, copy the raw DER data
1595 */
1596 p = (unsigned char *) malloc( len = buflen );
1597
1598 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001599 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001600
1601 memcpy( p, buf, buflen );
1602
1603 buflen = 0;
1604 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001605#else
1606 p = (unsigned char *) malloc( len = buflen );
1607
1608 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001609 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001610
1611 memcpy( p, buf, buflen );
1612
1613 buflen = 0;
1614#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001615
1616 crl->raw.p = p;
1617 crl->raw.len = len;
1618 end = p + len;
1619
1620 /*
1621 * CertificateList ::= SEQUENCE {
1622 * tbsCertList TBSCertList,
1623 * signatureAlgorithm AlgorithmIdentifier,
1624 * signatureValue BIT STRING }
1625 */
1626 if( ( ret = asn1_get_tag( &p, end, &len,
1627 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1628 {
1629 x509_crl_free( crl );
1630 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1631 }
1632
Paul Bakker23986e52011-04-24 08:57:21 +00001633 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 {
1635 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001636 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1638 }
1639
1640 /*
1641 * TBSCertList ::= SEQUENCE {
1642 */
1643 crl->tbs.p = p;
1644
1645 if( ( ret = asn1_get_tag( &p, end, &len,
1646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1647 {
1648 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001649 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 }
1651
1652 end = p + len;
1653 crl->tbs.len = end - crl->tbs.p;
1654
1655 /*
1656 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1657 * -- if present, MUST be v2
1658 *
1659 * signature AlgorithmIdentifier
1660 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001661 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1663 {
1664 x509_crl_free( crl );
1665 return( ret );
1666 }
1667
1668 crl->version++;
1669
1670 if( crl->version > 2 )
1671 {
1672 x509_crl_free( crl );
1673 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1674 }
1675
Paul Bakkerc70b9822013-04-07 22:00:46 +02001676 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1677 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 {
1679 x509_crl_free( crl );
1680 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1681 }
1682
1683 /*
1684 * issuer Name
1685 */
1686 crl->issuer_raw.p = p;
1687
1688 if( ( ret = asn1_get_tag( &p, end, &len,
1689 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1690 {
1691 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001692 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693 }
1694
1695 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1696 {
1697 x509_crl_free( crl );
1698 return( ret );
1699 }
1700
1701 crl->issuer_raw.len = p - crl->issuer_raw.p;
1702
1703 /*
1704 * thisUpdate Time
1705 * nextUpdate Time OPTIONAL
1706 */
Paul Bakker91200182010-02-18 21:26:15 +00001707 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 {
1709 x509_crl_free( crl );
1710 return( ret );
1711 }
1712
Paul Bakker91200182010-02-18 21:26:15 +00001713 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 {
Paul Bakker9d781402011-05-09 16:17:09 +00001715 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001716 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001717 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001718 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001719 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720 x509_crl_free( crl );
1721 return( ret );
1722 }
1723 }
1724
1725 /*
1726 * revokedCertificates SEQUENCE OF SEQUENCE {
1727 * userCertificate CertificateSerialNumber,
1728 * revocationDate Time,
1729 * crlEntryExtensions Extensions OPTIONAL
1730 * -- if present, MUST be v2
1731 * } OPTIONAL
1732 */
1733 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1734 {
1735 x509_crl_free( crl );
1736 return( ret );
1737 }
1738
1739 /*
1740 * crlExtensions EXPLICIT Extensions OPTIONAL
1741 * -- if present, MUST be v2
1742 */
1743 if( crl->version == 2 )
1744 {
1745 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1746
1747 if( ret != 0 )
1748 {
1749 x509_crl_free( crl );
1750 return( ret );
1751 }
1752 }
1753
1754 if( p != end )
1755 {
1756 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001757 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001758 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1759 }
1760
1761 end = crl->raw.p + crl->raw.len;
1762
1763 /*
1764 * signatureAlgorithm AlgorithmIdentifier,
1765 * signatureValue BIT STRING
1766 */
1767 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1768 {
1769 x509_crl_free( crl );
1770 return( ret );
1771 }
1772
Paul Bakker535e97d2012-08-23 10:49:55 +00001773 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1774 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 {
1776 x509_crl_free( crl );
1777 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1778 }
1779
1780 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1781 {
1782 x509_crl_free( crl );
1783 return( ret );
1784 }
1785
1786 if( p != end )
1787 {
1788 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001789 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1791 }
1792
1793 if( buflen > 0 )
1794 {
1795 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1796
Paul Bakker7d06ad22009-05-02 15:53:56 +00001797 if( crl->next == NULL )
1798 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001800 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001801 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802
Paul Bakker7d06ad22009-05-02 15:53:56 +00001803 crl = crl->next;
1804 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805
1806 return( x509parse_crl( crl, buf, buflen ) );
1807 }
1808
1809 return( 0 );
1810}
1811
Paul Bakker335db3f2011-04-25 15:28:35 +00001812#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001813/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001814 * Load all data from a file into a given buffer.
1815 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001816static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001817{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001818 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001821 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001822
Paul Bakkerd98030e2009-05-02 15:13:40 +00001823 fseek( f, 0, SEEK_END );
1824 *n = (size_t) ftell( f );
1825 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001828 {
1829 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001830 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001831 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001832
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833 if( fread( *buf, 1, *n, f ) != *n )
1834 {
1835 fclose( f );
1836 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001837 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001838 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001839
Paul Bakkerd98030e2009-05-02 15:13:40 +00001840 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001841
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001845}
1846
1847/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 * Load one or more certificates and add them to the chained list
1849 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001850int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851{
1852 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 size_t n;
1854 unsigned char *buf;
1855
Paul Bakker69e095c2011-12-10 21:55:01 +00001856 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1857 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Paul Bakker69e095c2011-12-10 21:55:01 +00001859 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
1861 memset( buf, 0, n + 1 );
1862 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
1864 return( ret );
1865}
1866
Paul Bakker8d914582012-06-04 12:46:42 +00001867int x509parse_crtpath( x509_cert *chain, const char *path )
1868{
1869 int ret = 0;
1870#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001871 int w_ret;
1872 WCHAR szDir[MAX_PATH];
1873 char filename[MAX_PATH];
1874 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001875 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001876
Paul Bakker97872ac2012-11-02 12:53:26 +00001877 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001878 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001879
1880 if( len > MAX_PATH - 3 )
1881 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001882
Paul Bakker3338b792012-10-01 21:13:10 +00001883 memset( szDir, 0, sizeof(szDir) );
1884 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001885 memcpy( filename, path, len );
1886 filename[len++] = '\\';
1887 p = filename + len;
1888 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001889
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001890 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001891
Paul Bakker97872ac2012-11-02 12:53:26 +00001892 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001893 if (hFind == INVALID_HANDLE_VALUE)
1894 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1895
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001896 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001897 do
1898 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001899 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001900
Paul Bakkere4791f32012-06-04 21:29:15 +00001901 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001902 continue;
1903
Paul Bakker3338b792012-10-01 21:13:10 +00001904 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1905 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001906 p, len - 1,
1907 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001908
Paul Bakker3338b792012-10-01 21:13:10 +00001909 w_ret = x509parse_crtfile( chain, filename );
1910 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001911 ret++;
1912 else
1913 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001914 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001915 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001916
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001917 if (GetLastError() != ERROR_NO_MORE_FILES)
1918 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001919
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001920cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001921 FindClose( hFind );
1922#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001923 int t_ret, i;
1924 struct stat sb;
1925 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001926 char entry_name[255];
1927 DIR *dir = opendir( path );
1928
1929 if( dir == NULL)
1930 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1931
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001932 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001933 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001934 if( result == NULL )
1935 break;
1936
1937 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1938
1939 i = stat( entry_name, &sb );
1940
1941 if( i == -1 )
1942 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1943
1944 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001945 continue;
1946
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001947 // Ignore parse errors
1948 //
Paul Bakker8d914582012-06-04 12:46:42 +00001949 t_ret = x509parse_crtfile( chain, entry_name );
1950 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02001951 ret++;
1952 else
1953 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001954 }
1955 closedir( dir );
1956#endif
1957
1958 return( ret );
1959}
1960
Paul Bakkerd98030e2009-05-02 15:13:40 +00001961/*
1962 * Load one or more CRLs and add them to the chained list
1963 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001964int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001965{
1966 int ret;
1967 size_t n;
1968 unsigned char *buf;
1969
Paul Bakker69e095c2011-12-10 21:55:01 +00001970 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1971 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001972
Paul Bakker27fdf462011-06-09 13:55:13 +00001973 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001974
1975 memset( buf, 0, n + 1 );
1976 free( buf );
1977
1978 return( ret );
1979}
1980
Paul Bakker5121ce52009-01-03 21:22:43 +00001981/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001982 * Load and parse a private RSA key
1983 */
1984int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1985{
1986 int ret;
1987 size_t n;
1988 unsigned char *buf;
1989
Paul Bakker69e095c2011-12-10 21:55:01 +00001990 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1991 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001992
1993 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001994 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001995 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001996 ret = x509parse_key( rsa, buf, n,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001997 (const unsigned char *) pwd, strlen( pwd ) );
Paul Bakker335db3f2011-04-25 15:28:35 +00001998
1999 memset( buf, 0, n + 1 );
2000 free( buf );
2001
2002 return( ret );
2003}
2004
2005/*
2006 * Load and parse a public RSA key
2007 */
2008int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2009{
2010 int ret;
2011 size_t n;
2012 unsigned char *buf;
2013
Paul Bakker69e095c2011-12-10 21:55:01 +00002014 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2015 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002016
Paul Bakker27fdf462011-06-09 13:55:13 +00002017 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002018
2019 memset( buf, 0, n + 1 );
2020 free( buf );
2021
2022 return( ret );
2023}
2024#endif /* POLARSSL_FS_IO */
2025
2026/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002027 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002029static int x509parse_key_pkcs1_der( rsa_context *rsa,
2030 const unsigned char *key,
2031 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032{
Paul Bakker23986e52011-04-24 08:57:21 +00002033 int ret;
2034 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002036
Paul Bakker96743fc2011-02-12 14:30:57 +00002037 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002038 end = p + keylen;
2039
Paul Bakker5121ce52009-01-03 21:22:43 +00002040 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002041 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002042 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 * RSAPrivateKey ::= SEQUENCE {
2044 * version Version,
2045 * modulus INTEGER, -- n
2046 * publicExponent INTEGER, -- e
2047 * privateExponent INTEGER, -- d
2048 * prime1 INTEGER, -- p
2049 * prime2 INTEGER, -- q
2050 * exponent1 INTEGER, -- d mod (p-1)
2051 * exponent2 INTEGER, -- d mod (q-1)
2052 * coefficient INTEGER, -- (inverse of q) mod p
2053 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2054 * }
2055 */
2056 if( ( ret = asn1_get_tag( &p, end, &len,
2057 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2058 {
Paul Bakker9d781402011-05-09 16:17:09 +00002059 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
2062 end = p + len;
2063
2064 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2065 {
Paul Bakker9d781402011-05-09 16:17:09 +00002066 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 }
2068
2069 if( rsa->ver != 0 )
2070 {
Paul Bakker9d781402011-05-09 16:17:09 +00002071 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 }
2073
2074 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2075 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2076 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2077 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2078 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2079 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2080 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2081 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2082 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002084 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 rsa->len = mpi_size( &rsa->N );
2088
2089 if( p != end )
2090 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002092 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002093 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095
2096 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2097 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 rsa_free( rsa );
2099 return( ret );
2100 }
2101
Paul Bakkere2f50402013-06-24 19:00:59 +02002102 return( 0 );
2103}
2104
2105/*
2106 * Parse an unencrypted PKCS#8 encoded private RSA key
2107 */
2108static int x509parse_key_pkcs8_unencrypted_der(
2109 rsa_context *rsa,
2110 const unsigned char *key,
2111 size_t keylen )
2112{
2113 int ret;
2114 size_t len;
2115 unsigned char *p, *end;
2116 x509_buf pk_alg_oid;
2117 pk_type_t pk_alg = POLARSSL_PK_NONE;
2118
2119 p = (unsigned char *) key;
2120 end = p + keylen;
2121
2122 /*
2123 * This function parses the PrivatKeyInfo object (PKCS#8)
2124 *
2125 * PrivateKeyInfo ::= SEQUENCE {
2126 * version Version,
2127 * algorithm AlgorithmIdentifier,
2128 * PrivateKey BIT STRING
2129 * }
2130 *
2131 * AlgorithmIdentifier ::= SEQUENCE {
2132 * algorithm OBJECT IDENTIFIER,
2133 * parameters ANY DEFINED BY algorithm OPTIONAL
2134 * }
2135 *
2136 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2137 */
2138 if( ( ret = asn1_get_tag( &p, end, &len,
2139 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2140 {
2141 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2142 }
2143
2144 end = p + len;
2145
2146 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2147 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2148
2149 if( rsa->ver != 0 )
2150 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2151
2152 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2153 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2154
2155 /*
2156 * only RSA keys handled at this time
2157 */
2158 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2159 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2160
2161 /*
2162 * Get the OCTET STRING and parse the PKCS#1 format inside
2163 */
2164 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2165 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2166
2167 if( ( end - p ) < 1 )
2168 {
2169 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2170 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2171 }
2172
2173 end = p + len;
2174
2175 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2176 return( ret );
2177
2178 return( 0 );
2179}
2180
2181/*
Paul Bakkerbda7cb72013-06-24 19:34:25 +02002182 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002183 */
2184static int x509parse_key_pkcs8_encrypted_der(
2185 rsa_context *rsa,
2186 const unsigned char *key,
2187 size_t keylen,
2188 const unsigned char *pwd,
2189 size_t pwdlen )
2190{
2191 int ret;
2192 size_t len;
2193 unsigned char *p, *end, *end2;
2194 x509_buf pbe_alg_oid, pbe_params;
2195 unsigned char buf[2048];
Paul Bakker7749a222013-06-28 17:28:20 +02002196#if defined(POLARSSL_PKCS12_C)
2197 cipher_type_t cipher_alg;
2198 md_type_t md_alg;
2199#endif
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002200
2201 memset(buf, 0, 2048);
2202
2203 p = (unsigned char *) key;
2204 end = p + keylen;
2205
Paul Bakker28144de2013-06-24 19:28:55 +02002206 if( pwdlen == 0 )
2207 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2208
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002209 /*
2210 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2211 *
2212 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2213 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2214 * encryptedData EncryptedData
2215 * }
2216 *
2217 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2218 *
2219 * EncryptedData ::= OCTET STRING
2220 *
2221 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2222 */
2223 if( ( ret = asn1_get_tag( &p, end, &len,
2224 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2225 {
2226 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2227 }
2228
2229 end = p + len;
2230
2231 if( ( ret = asn1_get_tag( &p, end, &len,
2232 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2233 {
2234 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2235 }
2236
2237 end2 = p + len;
2238
2239 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2240 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2241
2242 pbe_alg_oid.p = p;
2243 p += pbe_alg_oid.len;
2244
2245 /*
2246 * Store the algorithm parameters
2247 */
2248 pbe_params.p = p;
2249 pbe_params.len = end2 - p;
2250 p += pbe_params.len;
2251
2252 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2253 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2254
2255 // buf has been sized to 2048 bytes
2256 if( len > 2048 )
2257 return( POLARSSL_ERR_X509_INVALID_INPUT );
2258
2259 /*
2260 * Decrypt EncryptedData with appropriate PDE
2261 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002262#if defined(POLARSSL_PKCS12_C)
Paul Bakker7749a222013-06-28 17:28:20 +02002263 if( oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002264 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002265 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
Paul Bakker7749a222013-06-28 17:28:20 +02002266 cipher_alg, md_alg,
Paul Bakker38b50d72013-06-24 19:33:27 +02002267 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002268 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002269 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2270 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2271
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002272 return( ret );
2273 }
2274 }
2275 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2276 {
2277 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2278 PKCS12_PBE_DECRYPT,
2279 pwd, pwdlen,
2280 p, len, buf ) ) != 0 )
2281 {
2282 return( ret );
2283 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002284
2285 // Best guess for password mismatch when using RC4. If first tag is
2286 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2287 //
2288 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2289 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002290 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002291 else
2292#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002293#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002294 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002295 {
2296 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2297 p, len, buf ) ) != 0 )
2298 {
2299 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2300 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2301
2302 return( ret );
2303 }
2304 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002305 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002306#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002307 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2308
2309 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2310}
2311
2312/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002313 * Parse a private RSA key
2314 */
2315int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2316 const unsigned char *pwd, size_t pwdlen )
2317{
2318 int ret;
2319
Paul Bakker96743fc2011-02-12 14:30:57 +00002320#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002321 size_t len;
2322 pem_context pem;
2323
2324 pem_init( &pem );
2325 ret = pem_read_buffer( &pem,
2326 "-----BEGIN RSA PRIVATE KEY-----",
2327 "-----END RSA PRIVATE KEY-----",
2328 key, pwd, pwdlen, &len );
2329 if( ret == 0 )
2330 {
2331 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2332 {
2333 rsa_free( rsa );
2334 }
2335
2336 pem_free( &pem );
2337 return( ret );
2338 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002339 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2340 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2341 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2342 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002343 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002344 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002345
2346 ret = pem_read_buffer( &pem,
2347 "-----BEGIN PRIVATE KEY-----",
2348 "-----END PRIVATE KEY-----",
2349 key, NULL, 0, &len );
2350 if( ret == 0 )
2351 {
2352 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2353 pem.buf, pem.buflen ) ) != 0 )
2354 {
2355 rsa_free( rsa );
2356 }
2357
2358 pem_free( &pem );
2359 return( ret );
2360 }
2361 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002362 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002363
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002364 ret = pem_read_buffer( &pem,
2365 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2366 "-----END ENCRYPTED PRIVATE KEY-----",
2367 key, NULL, 0, &len );
2368 if( ret == 0 )
2369 {
2370 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2371 pem.buf, pem.buflen,
2372 pwd, pwdlen ) ) != 0 )
2373 {
2374 rsa_free( rsa );
2375 }
2376
2377 pem_free( &pem );
2378 return( ret );
2379 }
2380 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002381 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002382#else
2383 ((void) pwd);
2384 ((void) pwdlen);
2385#endif /* POLARSSL_PEM_C */
2386
2387 // At this point we only know it's not a PEM formatted key. Could be any
2388 // of the known DER encoded private key formats
2389 //
2390 // We try the different DER format parsers to see if one passes without
2391 // error
2392 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002393 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2394 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002395 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002396 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002399 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002400
2401 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2402 {
2403 return( ret );
2404 }
2405
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002406 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2407 return( 0 );
2408
2409 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002410
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002411 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2412 return( 0 );
2413
2414 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002415
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002416 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417}
2418
2419/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002420 * Parse a public RSA key
2421 */
Paul Bakker23986e52011-04-24 08:57:21 +00002422int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002423{
Paul Bakker23986e52011-04-24 08:57:21 +00002424 int ret;
2425 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002426 unsigned char *p, *end;
2427 x509_buf alg_oid;
2428#if defined(POLARSSL_PEM_C)
2429 pem_context pem;
2430
2431 pem_init( &pem );
2432 ret = pem_read_buffer( &pem,
2433 "-----BEGIN PUBLIC KEY-----",
2434 "-----END PUBLIC KEY-----",
2435 key, NULL, 0, &len );
2436
2437 if( ret == 0 )
2438 {
2439 /*
2440 * Was PEM encoded
2441 */
2442 keylen = pem.buflen;
2443 }
Paul Bakker00b28602013-06-24 13:02:41 +02002444 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002445 {
2446 pem_free( &pem );
2447 return( ret );
2448 }
2449
2450 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2451#else
2452 p = (unsigned char *) key;
2453#endif
2454 end = p + keylen;
2455
2456 /*
2457 * PublicKeyInfo ::= SEQUENCE {
2458 * algorithm AlgorithmIdentifier,
2459 * PublicKey BIT STRING
2460 * }
2461 *
2462 * AlgorithmIdentifier ::= SEQUENCE {
2463 * algorithm OBJECT IDENTIFIER,
2464 * parameters ANY DEFINED BY algorithm OPTIONAL
2465 * }
2466 *
2467 * RSAPublicKey ::= SEQUENCE {
2468 * modulus INTEGER, -- n
2469 * publicExponent INTEGER -- e
2470 * }
2471 */
2472
2473 if( ( ret = asn1_get_tag( &p, end, &len,
2474 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2475 {
2476#if defined(POLARSSL_PEM_C)
2477 pem_free( &pem );
2478#endif
2479 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002480 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002481 }
2482
2483 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2484 {
2485#if defined(POLARSSL_PEM_C)
2486 pem_free( &pem );
2487#endif
2488 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002489 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002490 }
2491
2492 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2493 {
2494#if defined(POLARSSL_PEM_C)
2495 pem_free( &pem );
2496#endif
2497 rsa_free( rsa );
2498 return( ret );
2499 }
2500
2501 rsa->len = mpi_size( &rsa->N );
2502
2503#if defined(POLARSSL_PEM_C)
2504 pem_free( &pem );
2505#endif
2506
2507 return( 0 );
2508}
2509
Paul Bakkereaa89f82011-04-04 21:36:15 +00002510#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002511/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002512 * Parse DHM parameters
2513 */
Paul Bakker23986e52011-04-24 08:57:21 +00002514int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002515{
Paul Bakker23986e52011-04-24 08:57:21 +00002516 int ret;
2517 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002518 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002519#if defined(POLARSSL_PEM_C)
2520 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002521
Paul Bakker96743fc2011-02-12 14:30:57 +00002522 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002523
Paul Bakker96743fc2011-02-12 14:30:57 +00002524 ret = pem_read_buffer( &pem,
2525 "-----BEGIN DH PARAMETERS-----",
2526 "-----END DH PARAMETERS-----",
2527 dhmin, NULL, 0, &dhminlen );
2528
2529 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002530 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002531 /*
2532 * Was PEM encoded
2533 */
2534 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002535 }
Paul Bakker00b28602013-06-24 13:02:41 +02002536 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002537 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002538 pem_free( &pem );
2539 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002540 }
2541
Paul Bakker96743fc2011-02-12 14:30:57 +00002542 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2543#else
2544 p = (unsigned char *) dhmin;
2545#endif
2546 end = p + dhminlen;
2547
Paul Bakker1b57b062011-01-06 15:48:19 +00002548 memset( dhm, 0, sizeof( dhm_context ) );
2549
Paul Bakker1b57b062011-01-06 15:48:19 +00002550 /*
2551 * DHParams ::= SEQUENCE {
2552 * prime INTEGER, -- P
2553 * generator INTEGER, -- g
2554 * }
2555 */
2556 if( ( ret = asn1_get_tag( &p, end, &len,
2557 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2558 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002559#if defined(POLARSSL_PEM_C)
2560 pem_free( &pem );
2561#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002562 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002563 }
2564
2565 end = p + len;
2566
2567 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2568 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2569 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002570#if defined(POLARSSL_PEM_C)
2571 pem_free( &pem );
2572#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002573 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002574 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002575 }
2576
2577 if( p != end )
2578 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002579#if defined(POLARSSL_PEM_C)
2580 pem_free( &pem );
2581#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002582 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002583 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002584 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2585 }
2586
Paul Bakker96743fc2011-02-12 14:30:57 +00002587#if defined(POLARSSL_PEM_C)
2588 pem_free( &pem );
2589#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002590
2591 return( 0 );
2592}
2593
Paul Bakker335db3f2011-04-25 15:28:35 +00002594#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002595/*
2596 * Load and parse a private RSA key
2597 */
2598int x509parse_dhmfile( dhm_context *dhm, const char *path )
2599{
2600 int ret;
2601 size_t n;
2602 unsigned char *buf;
2603
Paul Bakker69e095c2011-12-10 21:55:01 +00002604 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2605 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002606
Paul Bakker27fdf462011-06-09 13:55:13 +00002607 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002608
2609 memset( buf, 0, n + 1 );
2610 free( buf );
2611
2612 return( ret );
2613}
Paul Bakker335db3f2011-04-25 15:28:35 +00002614#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002615#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002616
Paul Bakker5121ce52009-01-03 21:22:43 +00002617#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002618#include <stdarg.h>
2619
2620#if !defined vsnprintf
2621#define vsnprintf _vsnprintf
2622#endif // vsnprintf
2623
2624/*
2625 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2626 * Result value is not size of buffer needed, but -1 if no fit is possible.
2627 *
2628 * This fuction tries to 'fix' this by at least suggesting enlarging the
2629 * size by 20.
2630 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002631static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002632{
2633 va_list ap;
2634 int res = -1;
2635
2636 va_start( ap, format );
2637
2638 res = vsnprintf( str, size, format, ap );
2639
2640 va_end( ap );
2641
2642 // No quick fix possible
2643 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002644 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645
2646 return res;
2647}
2648
2649#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002650#endif
2651
Paul Bakkerd98030e2009-05-02 15:13:40 +00002652#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2653
2654#define SAFE_SNPRINTF() \
2655{ \
2656 if( ret == -1 ) \
2657 return( -1 ); \
2658 \
Paul Bakker23986e52011-04-24 08:57:21 +00002659 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002660 p[n - 1] = '\0'; \
2661 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2662 } \
2663 \
Paul Bakker23986e52011-04-24 08:57:21 +00002664 n -= (unsigned int) ret; \
2665 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002666}
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668/*
2669 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002670 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002672int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002673{
Paul Bakker23986e52011-04-24 08:57:21 +00002674 int ret;
2675 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002677 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002678 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 char s[128], *p;
2680
2681 memset( s, 0, sizeof( s ) );
2682
2683 name = dn;
2684 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002685 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
2687 while( name != NULL )
2688 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002689 if( !name->oid.p )
2690 {
2691 name = name->next;
2692 continue;
2693 }
2694
Paul Bakker74111d32011-01-15 16:57:55 +00002695 if( name != dn )
2696 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002697 ret = snprintf( p, n, ", " );
2698 SAFE_SNPRINTF();
2699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Paul Bakkerc70b9822013-04-07 22:00:46 +02002701 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702
Paul Bakkerc70b9822013-04-07 22:00:46 +02002703 if( ret == 0 )
2704 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002707 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 for( i = 0; i < name->val.len; i++ )
2710 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002711 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 break;
2713
2714 c = name->val.p[i];
2715 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2716 s[i] = '?';
2717 else s[i] = c;
2718 }
2719 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002720 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002721 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 name = name->next;
2723 }
2724
Paul Bakker23986e52011-04-24 08:57:21 +00002725 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726}
2727
2728/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002729 * Store the serial in printable form into buf; no more
2730 * than size characters will be written
2731 */
2732int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2733{
Paul Bakker23986e52011-04-24 08:57:21 +00002734 int ret;
2735 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002736 char *p;
2737
2738 p = buf;
2739 n = size;
2740
2741 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002742 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002743
2744 for( i = 0; i < nr; i++ )
2745 {
Paul Bakker93048802011-12-05 14:38:06 +00002746 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002747 continue;
2748
Paul Bakkerdd476992011-01-16 21:34:59 +00002749 ret = snprintf( p, n, "%02X%s",
2750 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2751 SAFE_SNPRINTF();
2752 }
2753
Paul Bakker03c7c252011-11-25 12:37:37 +00002754 if( nr != serial->len )
2755 {
2756 ret = snprintf( p, n, "...." );
2757 SAFE_SNPRINTF();
2758 }
2759
Paul Bakker23986e52011-04-24 08:57:21 +00002760 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002761}
2762
2763/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002764 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002766int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2767 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Paul Bakker23986e52011-04-24 08:57:21 +00002769 int ret;
2770 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002771 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002772 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
2774 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002775 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002776
Paul Bakkerd98030e2009-05-02 15:13:40 +00002777 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002779 SAFE_SNPRINTF();
2780 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002782 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Paul Bakkerdd476992011-01-16 21:34:59 +00002784 ret = x509parse_serial_gets( p, n, &crt->serial);
2785 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Paul Bakkerd98030e2009-05-02 15:13:40 +00002787 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2788 SAFE_SNPRINTF();
2789 ret = x509parse_dn_gets( p, n, &crt->issuer );
2790 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakkerd98030e2009-05-02 15:13:40 +00002792 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2793 SAFE_SNPRINTF();
2794 ret = x509parse_dn_gets( p, n, &crt->subject );
2795 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
Paul Bakkerd98030e2009-05-02 15:13:40 +00002797 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002798 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2799 crt->valid_from.year, crt->valid_from.mon,
2800 crt->valid_from.day, crt->valid_from.hour,
2801 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002802 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Paul Bakkerd98030e2009-05-02 15:13:40 +00002804 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2806 crt->valid_to.year, crt->valid_to.mon,
2807 crt->valid_to.day, crt->valid_to.hour,
2808 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
Paul Bakkerc70b9822013-04-07 22:00:46 +02002811 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Paul Bakkerc70b9822013-04-07 22:00:46 +02002814 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2815 if( ret != 0 )
2816 ret = snprintf( p, n, "???" );
2817 else
2818 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002819 SAFE_SNPRINTF();
2820
2821 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002822 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002823 SAFE_SNPRINTF();
2824
Paul Bakker23986e52011-04-24 08:57:21 +00002825 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002826}
2827
Paul Bakker74111d32011-01-15 16:57:55 +00002828/*
2829 * Return an informational string describing the given OID
2830 */
2831const char *x509_oid_get_description( x509_buf *oid )
2832{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002833 const char *desc = NULL;
2834 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002835
Paul Bakkerc70b9822013-04-07 22:00:46 +02002836 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002837
Paul Bakkerc70b9822013-04-07 22:00:46 +02002838 if( ret != 0 )
2839 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002840
Paul Bakkerc70b9822013-04-07 22:00:46 +02002841 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002842}
2843
2844/* Return the x.y.z.... style numeric string for the given OID */
2845int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2846{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002847 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002848}
2849
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850/*
2851 * Return an informational string about the CRL.
2852 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002853int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2854 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002855{
Paul Bakker23986e52011-04-24 08:57:21 +00002856 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002857 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002858 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002859 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002860 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002861
2862 p = buf;
2863 n = size;
2864
2865 ret = snprintf( p, n, "%sCRL version : %d",
2866 prefix, crl->version );
2867 SAFE_SNPRINTF();
2868
2869 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2870 SAFE_SNPRINTF();
2871 ret = x509parse_dn_gets( p, n, &crl->issuer );
2872 SAFE_SNPRINTF();
2873
2874 ret = snprintf( p, n, "\n%sthis update : " \
2875 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2876 crl->this_update.year, crl->this_update.mon,
2877 crl->this_update.day, crl->this_update.hour,
2878 crl->this_update.min, crl->this_update.sec );
2879 SAFE_SNPRINTF();
2880
2881 ret = snprintf( p, n, "\n%snext update : " \
2882 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2883 crl->next_update.year, crl->next_update.mon,
2884 crl->next_update.day, crl->next_update.hour,
2885 crl->next_update.min, crl->next_update.sec );
2886 SAFE_SNPRINTF();
2887
2888 entry = &crl->entry;
2889
2890 ret = snprintf( p, n, "\n%sRevoked certificates:",
2891 prefix );
2892 SAFE_SNPRINTF();
2893
Paul Bakker9be19372009-07-27 20:21:53 +00002894 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002895 {
2896 ret = snprintf( p, n, "\n%sserial number: ",
2897 prefix );
2898 SAFE_SNPRINTF();
2899
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002900 ret = x509parse_serial_gets( p, n, &entry->serial);
2901 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002902
Paul Bakkerd98030e2009-05-02 15:13:40 +00002903 ret = snprintf( p, n, " revocation date: " \
2904 "%04d-%02d-%02d %02d:%02d:%02d",
2905 entry->revocation_date.year, entry->revocation_date.mon,
2906 entry->revocation_date.day, entry->revocation_date.hour,
2907 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002908 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002909
2910 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
2912
Paul Bakkerc70b9822013-04-07 22:00:46 +02002913 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002914 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakkerc70b9822013-04-07 22:00:46 +02002916 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2917 if( ret != 0 )
2918 ret = snprintf( p, n, "???" );
2919 else
2920 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002921 SAFE_SNPRINTF();
2922
Paul Bakker1e27bb22009-07-19 20:25:25 +00002923 ret = snprintf( p, n, "\n" );
2924 SAFE_SNPRINTF();
2925
Paul Bakker23986e52011-04-24 08:57:21 +00002926 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002927}
2928
2929/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002930 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002932int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933{
Paul Bakkercce9d772011-11-18 14:26:47 +00002934 int year, mon, day;
2935 int hour, min, sec;
2936
2937#if defined(_WIN32)
2938 SYSTEMTIME st;
2939
2940 GetLocalTime(&st);
2941
2942 year = st.wYear;
2943 mon = st.wMonth;
2944 day = st.wDay;
2945 hour = st.wHour;
2946 min = st.wMinute;
2947 sec = st.wSecond;
2948#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 struct tm *lt;
2950 time_t tt;
2951
2952 tt = time( NULL );
2953 lt = localtime( &tt );
2954
Paul Bakkercce9d772011-11-18 14:26:47 +00002955 year = lt->tm_year + 1900;
2956 mon = lt->tm_mon + 1;
2957 day = lt->tm_mday;
2958 hour = lt->tm_hour;
2959 min = lt->tm_min;
2960 sec = lt->tm_sec;
2961#endif
2962
2963 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002964 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Paul Bakkercce9d772011-11-18 14:26:47 +00002966 if( year == to->year &&
2967 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002968 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakkercce9d772011-11-18 14:26:47 +00002970 if( year == to->year &&
2971 mon == to->mon &&
2972 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002973 return( 1 );
2974
Paul Bakkercce9d772011-11-18 14:26:47 +00002975 if( year == to->year &&
2976 mon == to->mon &&
2977 day == to->day &&
2978 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002979 return( 1 );
2980
Paul Bakkercce9d772011-11-18 14:26:47 +00002981 if( year == to->year &&
2982 mon == to->mon &&
2983 day == to->day &&
2984 hour == to->hour &&
2985 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002986 return( 1 );
2987
Paul Bakkercce9d772011-11-18 14:26:47 +00002988 if( year == to->year &&
2989 mon == to->mon &&
2990 day == to->day &&
2991 hour == to->hour &&
2992 min == to->min &&
2993 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002994 return( 1 );
2995
Paul Bakker40ea7de2009-05-03 10:18:48 +00002996 return( 0 );
2997}
2998
2999/*
3000 * Return 1 if the certificate is revoked, or 0 otherwise.
3001 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003002int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003003{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003004 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003005
3006 while( cur != NULL && cur->serial.len != 0 )
3007 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003008 if( crt->serial.len == cur->serial.len &&
3009 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003010 {
3011 if( x509parse_time_expired( &cur->revocation_date ) )
3012 return( 1 );
3013 }
3014
3015 cur = cur->next;
3016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
3018 return( 0 );
3019}
3020
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003021/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003022 * Check that the given certificate is valid accoring to the CRL.
3023 */
3024static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3025 x509_crl *crl_list)
3026{
3027 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003028 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3029 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003030
Paul Bakker915275b2012-09-28 07:10:55 +00003031 if( ca == NULL )
3032 return( flags );
3033
Paul Bakker76fd75a2011-01-16 21:12:10 +00003034 /*
3035 * TODO: What happens if no CRL is present?
3036 * Suggestion: Revocation state should be unknown if no CRL is present.
3037 * For backwards compatibility this is not yet implemented.
3038 */
3039
Paul Bakker915275b2012-09-28 07:10:55 +00003040 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003041 {
Paul Bakker915275b2012-09-28 07:10:55 +00003042 if( crl_list->version == 0 ||
3043 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003044 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3045 crl_list->issuer_raw.len ) != 0 )
3046 {
3047 crl_list = crl_list->next;
3048 continue;
3049 }
3050
3051 /*
3052 * Check if CRL is correctly signed by the trusted CA
3053 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003054 md_info = md_info_from_type( crl_list->sig_md );
3055 if( md_info == NULL )
3056 {
3057 /*
3058 * Cannot check 'unknown' hash
3059 */
3060 flags |= BADCRL_NOT_TRUSTED;
3061 break;
3062 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003063
Paul Bakkerc70b9822013-04-07 22:00:46 +02003064 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003065
Paul Bakkerc70b9822013-04-07 22:00:46 +02003066 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003067 0, hash, crl_list->sig.p ) == 0 )
3068 {
3069 /*
3070 * CRL is not trusted
3071 */
3072 flags |= BADCRL_NOT_TRUSTED;
3073 break;
3074 }
3075
3076 /*
3077 * Check for validity of CRL (Do not drop out)
3078 */
3079 if( x509parse_time_expired( &crl_list->next_update ) )
3080 flags |= BADCRL_EXPIRED;
3081
3082 /*
3083 * Check if certificate is revoked
3084 */
3085 if( x509parse_revoked(crt, crl_list) )
3086 {
3087 flags |= BADCERT_REVOKED;
3088 break;
3089 }
3090
3091 crl_list = crl_list->next;
3092 }
3093 return flags;
3094}
3095
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003096static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003097{
3098 size_t i;
3099 size_t cn_idx = 0;
3100
Paul Bakker57b12982012-02-11 17:38:38 +00003101 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003102 return( 0 );
3103
3104 for( i = 0; i < strlen( cn ); ++i )
3105 {
3106 if( cn[i] == '.' )
3107 {
3108 cn_idx = i;
3109 break;
3110 }
3111 }
3112
3113 if( cn_idx == 0 )
3114 return( 0 );
3115
Paul Bakker535e97d2012-08-23 10:49:55 +00003116 if( strlen( cn ) - cn_idx == name->len - 1 &&
3117 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003118 {
3119 return( 1 );
3120 }
3121
3122 return( 0 );
3123}
3124
Paul Bakker915275b2012-09-28 07:10:55 +00003125static int x509parse_verify_top(
3126 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003127 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003128 int (*f_vrfy)(void *, x509_cert *, int, int *),
3129 void *p_vrfy )
3130{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003131 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003132 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003133 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3134 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003135
3136 if( x509parse_time_expired( &child->valid_to ) )
3137 *flags |= BADCERT_EXPIRED;
3138
3139 /*
3140 * Child is the top of the chain. Check against the trust_ca list.
3141 */
3142 *flags |= BADCERT_NOT_TRUSTED;
3143
3144 while( trust_ca != NULL )
3145 {
3146 if( trust_ca->version == 0 ||
3147 child->issuer_raw.len != trust_ca->subject_raw.len ||
3148 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3149 child->issuer_raw.len ) != 0 )
3150 {
3151 trust_ca = trust_ca->next;
3152 continue;
3153 }
3154
Paul Bakker9a736322012-11-14 12:39:52 +00003155 /*
3156 * Reduce path_len to check against if top of the chain is
3157 * the same as the trusted CA
3158 */
3159 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3160 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3161 child->issuer_raw.len ) == 0 )
3162 {
3163 check_path_cnt--;
3164 }
3165
Paul Bakker915275b2012-09-28 07:10:55 +00003166 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003167 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003168 {
3169 trust_ca = trust_ca->next;
3170 continue;
3171 }
3172
Paul Bakkerc70b9822013-04-07 22:00:46 +02003173 md_info = md_info_from_type( child->sig_md );
3174 if( md_info == NULL )
3175 {
3176 /*
3177 * Cannot check 'unknown' hash
3178 */
3179 continue;
3180 }
Paul Bakker915275b2012-09-28 07:10:55 +00003181
Paul Bakkerc70b9822013-04-07 22:00:46 +02003182 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003183
Paul Bakkerc70b9822013-04-07 22:00:46 +02003184 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003185 0, hash, child->sig.p ) != 0 )
3186 {
3187 trust_ca = trust_ca->next;
3188 continue;
3189 }
3190
3191 /*
3192 * Top of chain is signed by a trusted CA
3193 */
3194 *flags &= ~BADCERT_NOT_TRUSTED;
3195 break;
3196 }
3197
Paul Bakker9a736322012-11-14 12:39:52 +00003198 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003199 * If top of chain is not the same as the trusted CA send a verify request
3200 * to the callback for any issues with validity and CRL presence for the
3201 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003202 */
3203 if( trust_ca != NULL &&
3204 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3205 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3206 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003207 {
3208 /* Check trusted CA's CRL for then chain's top crt */
3209 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3210
3211 if( x509parse_time_expired( &trust_ca->valid_to ) )
3212 ca_flags |= BADCERT_EXPIRED;
3213
Paul Bakker915275b2012-09-28 07:10:55 +00003214 if( NULL != f_vrfy )
3215 {
Paul Bakker9a736322012-11-14 12:39:52 +00003216 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003217 return( ret );
3218 }
3219 }
3220
3221 /* Call callback on top cert */
3222 if( NULL != f_vrfy )
3223 {
Paul Bakker9a736322012-11-14 12:39:52 +00003224 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003225 return( ret );
3226 }
3227
Paul Bakker915275b2012-09-28 07:10:55 +00003228 *flags |= ca_flags;
3229
3230 return( 0 );
3231}
3232
3233static int x509parse_verify_child(
3234 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003235 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003236 int (*f_vrfy)(void *, x509_cert *, int, int *),
3237 void *p_vrfy )
3238{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003239 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003240 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003241 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003242 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003243 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003244
3245 if( x509parse_time_expired( &child->valid_to ) )
3246 *flags |= BADCERT_EXPIRED;
3247
Paul Bakkerc70b9822013-04-07 22:00:46 +02003248 md_info = md_info_from_type( child->sig_md );
3249 if( md_info == NULL )
3250 {
3251 /*
3252 * Cannot check 'unknown' hash
3253 */
Paul Bakker915275b2012-09-28 07:10:55 +00003254 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003255 }
3256 else
3257 {
3258 md( md_info, child->tbs.p, child->tbs.len, hash );
3259
3260 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3261 child->sig.p ) != 0 )
3262 *flags |= BADCERT_NOT_TRUSTED;
3263 }
3264
Paul Bakker915275b2012-09-28 07:10:55 +00003265 /* Check trusted CA's CRL for the given crt */
3266 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3267
3268 grandparent = parent->next;
3269
3270 while( grandparent != NULL )
3271 {
3272 if( grandparent->version == 0 ||
3273 grandparent->ca_istrue == 0 ||
3274 parent->issuer_raw.len != grandparent->subject_raw.len ||
3275 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3276 parent->issuer_raw.len ) != 0 )
3277 {
3278 grandparent = grandparent->next;
3279 continue;
3280 }
3281 break;
3282 }
3283
Paul Bakker915275b2012-09-28 07:10:55 +00003284 if( grandparent != NULL )
3285 {
3286 /*
3287 * Part of the chain
3288 */
Paul Bakker9a736322012-11-14 12:39:52 +00003289 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 +00003290 if( ret != 0 )
3291 return( ret );
3292 }
3293 else
3294 {
Paul Bakker9a736322012-11-14 12:39:52 +00003295 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 +00003296 if( ret != 0 )
3297 return( ret );
3298 }
3299
3300 /* child is verified to be a child of the parent, call verify callback */
3301 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003302 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003303 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003304
3305 *flags |= parent_flags;
3306
3307 return( 0 );
3308}
3309
Paul Bakker76fd75a2011-01-16 21:12:10 +00003310/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 * Verify the certificate validity
3312 */
3313int x509parse_verify( x509_cert *crt,
3314 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003315 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003316 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003317 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003318 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003319{
Paul Bakker23986e52011-04-24 08:57:21 +00003320 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003321 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003322 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003323 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003325 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003326
Paul Bakker40ea7de2009-05-03 10:18:48 +00003327 *flags = 0;
3328
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 if( cn != NULL )
3330 {
3331 name = &crt->subject;
3332 cn_len = strlen( cn );
3333
Paul Bakker4d2c1242012-05-10 14:12:46 +00003334 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003336 cur = &crt->subject_alt_names;
3337
3338 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003339 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003340 if( cur->buf.len == cn_len &&
3341 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003342 break;
3343
Paul Bakker535e97d2012-08-23 10:49:55 +00003344 if( cur->buf.len > 2 &&
3345 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003346 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003347 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003348
Paul Bakker4d2c1242012-05-10 14:12:46 +00003349 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003350 }
3351
3352 if( cur == NULL )
3353 *flags |= BADCERT_CN_MISMATCH;
3354 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003355 else
3356 {
3357 while( name != NULL )
3358 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003359 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003360 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003361 if( name->val.len == cn_len &&
3362 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003363 break;
3364
Paul Bakker535e97d2012-08-23 10:49:55 +00003365 if( name->val.len > 2 &&
3366 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003367 x509_wildcard_verify( cn, &name->val ) )
3368 break;
3369 }
3370
3371 name = name->next;
3372 }
3373
3374 if( name == NULL )
3375 *flags |= BADCERT_CN_MISMATCH;
3376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 }
3378
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003380 * Iterate upwards in the given cert chain, to find our crt parent.
3381 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003382 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003383 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003384
Paul Bakker76fd75a2011-01-16 21:12:10 +00003385 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003387 if( parent->ca_istrue == 0 ||
3388 crt->issuer_raw.len != parent->subject_raw.len ||
3389 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 crt->issuer_raw.len ) != 0 )
3391 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003392 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 continue;
3394 }
Paul Bakker915275b2012-09-28 07:10:55 +00003395 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 }
3397
Paul Bakker915275b2012-09-28 07:10:55 +00003398 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 {
Paul Bakker915275b2012-09-28 07:10:55 +00003400 /*
3401 * Part of the chain
3402 */
Paul Bakker9a736322012-11-14 12:39:52 +00003403 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003404 if( ret != 0 )
3405 return( ret );
3406 }
3407 else
Paul Bakker74111d32011-01-15 16:57:55 +00003408 {
Paul Bakker9a736322012-11-14 12:39:52 +00003409 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003410 if( ret != 0 )
3411 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003412 }
Paul Bakker915275b2012-09-28 07:10:55 +00003413
3414 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003415 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003416
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 return( 0 );
3418}
3419
3420/*
3421 * Unallocate all certificate data
3422 */
3423void x509_free( x509_cert *crt )
3424{
3425 x509_cert *cert_cur = crt;
3426 x509_cert *cert_prv;
3427 x509_name *name_cur;
3428 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003429 x509_sequence *seq_cur;
3430 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
3432 if( crt == NULL )
3433 return;
3434
3435 do
3436 {
3437 rsa_free( &cert_cur->rsa );
3438
3439 name_cur = cert_cur->issuer.next;
3440 while( name_cur != NULL )
3441 {
3442 name_prv = name_cur;
3443 name_cur = name_cur->next;
3444 memset( name_prv, 0, sizeof( x509_name ) );
3445 free( name_prv );
3446 }
3447
3448 name_cur = cert_cur->subject.next;
3449 while( name_cur != NULL )
3450 {
3451 name_prv = name_cur;
3452 name_cur = name_cur->next;
3453 memset( name_prv, 0, sizeof( x509_name ) );
3454 free( name_prv );
3455 }
3456
Paul Bakker74111d32011-01-15 16:57:55 +00003457 seq_cur = cert_cur->ext_key_usage.next;
3458 while( seq_cur != NULL )
3459 {
3460 seq_prv = seq_cur;
3461 seq_cur = seq_cur->next;
3462 memset( seq_prv, 0, sizeof( x509_sequence ) );
3463 free( seq_prv );
3464 }
3465
Paul Bakker8afa70d2012-02-11 18:42:45 +00003466 seq_cur = cert_cur->subject_alt_names.next;
3467 while( seq_cur != NULL )
3468 {
3469 seq_prv = seq_cur;
3470 seq_cur = seq_cur->next;
3471 memset( seq_prv, 0, sizeof( x509_sequence ) );
3472 free( seq_prv );
3473 }
3474
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 if( cert_cur->raw.p != NULL )
3476 {
3477 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3478 free( cert_cur->raw.p );
3479 }
3480
3481 cert_cur = cert_cur->next;
3482 }
3483 while( cert_cur != NULL );
3484
3485 cert_cur = crt;
3486 do
3487 {
3488 cert_prv = cert_cur;
3489 cert_cur = cert_cur->next;
3490
3491 memset( cert_prv, 0, sizeof( x509_cert ) );
3492 if( cert_prv != crt )
3493 free( cert_prv );
3494 }
3495 while( cert_cur != NULL );
3496}
3497
Paul Bakkerd98030e2009-05-02 15:13:40 +00003498/*
3499 * Unallocate all CRL data
3500 */
3501void x509_crl_free( x509_crl *crl )
3502{
3503 x509_crl *crl_cur = crl;
3504 x509_crl *crl_prv;
3505 x509_name *name_cur;
3506 x509_name *name_prv;
3507 x509_crl_entry *entry_cur;
3508 x509_crl_entry *entry_prv;
3509
3510 if( crl == NULL )
3511 return;
3512
3513 do
3514 {
3515 name_cur = crl_cur->issuer.next;
3516 while( name_cur != NULL )
3517 {
3518 name_prv = name_cur;
3519 name_cur = name_cur->next;
3520 memset( name_prv, 0, sizeof( x509_name ) );
3521 free( name_prv );
3522 }
3523
3524 entry_cur = crl_cur->entry.next;
3525 while( entry_cur != NULL )
3526 {
3527 entry_prv = entry_cur;
3528 entry_cur = entry_cur->next;
3529 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3530 free( entry_prv );
3531 }
3532
3533 if( crl_cur->raw.p != NULL )
3534 {
3535 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3536 free( crl_cur->raw.p );
3537 }
3538
3539 crl_cur = crl_cur->next;
3540 }
3541 while( crl_cur != NULL );
3542
3543 crl_cur = crl;
3544 do
3545 {
3546 crl_prv = crl_cur;
3547 crl_cur = crl_cur->next;
3548
3549 memset( crl_prv, 0, sizeof( x509_crl ) );
3550 if( crl_prv != crl )
3551 free( crl_prv );
3552 }
3553 while( crl_cur != NULL );
3554}
3555
Paul Bakker40e46942009-01-03 21:51:57 +00003556#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003557
Paul Bakker40e46942009-01-03 21:51:57 +00003558#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
3560/*
3561 * Checkup routine
3562 */
3563int x509_self_test( int verbose )
3564{
Paul Bakker5690efc2011-05-26 13:16:06 +00003565#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003566 int ret;
3567 int flags;
3568 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 x509_cert cacert;
3570 x509_cert clicert;
3571 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003572#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003573 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003574#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
3576 if( verbose != 0 )
3577 printf( " X.509 certificate load: " );
3578
3579 memset( &clicert, 0, sizeof( x509_cert ) );
3580
Paul Bakker3c2122f2013-06-24 19:03:14 +02003581 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003582 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 if( ret != 0 )
3584 {
3585 if( verbose != 0 )
3586 printf( "failed\n" );
3587
3588 return( ret );
3589 }
3590
3591 memset( &cacert, 0, sizeof( x509_cert ) );
3592
Paul Bakker3c2122f2013-06-24 19:03:14 +02003593 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003594 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 if( ret != 0 )
3596 {
3597 if( verbose != 0 )
3598 printf( "failed\n" );
3599
3600 return( ret );
3601 }
3602
3603 if( verbose != 0 )
3604 printf( "passed\n X.509 private key load: " );
3605
3606 i = strlen( test_ca_key );
3607 j = strlen( test_ca_pwd );
3608
Paul Bakker66b78b22011-03-25 14:22:50 +00003609 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3610
Paul Bakker5121ce52009-01-03 21:22:43 +00003611 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003612 (const unsigned char *) test_ca_key, i,
3613 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 {
3615 if( verbose != 0 )
3616 printf( "failed\n" );
3617
3618 return( ret );
3619 }
3620
3621 if( verbose != 0 )
3622 printf( "passed\n X.509 signature verify: ");
3623
Paul Bakker23986e52011-04-24 08:57:21 +00003624 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 if( ret != 0 )
3626 {
Paul Bakker23986e52011-04-24 08:57:21 +00003627 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 if( verbose != 0 )
3629 printf( "failed\n" );
3630
3631 return( ret );
3632 }
3633
Paul Bakker5690efc2011-05-26 13:16:06 +00003634#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003636 printf( "passed\n X.509 DHM parameter load: " );
3637
3638 i = strlen( test_dhm_params );
3639 j = strlen( test_ca_pwd );
3640
Paul Bakker3c2122f2013-06-24 19:03:14 +02003641 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003642 {
3643 if( verbose != 0 )
3644 printf( "failed\n" );
3645
3646 return( ret );
3647 }
3648
3649 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003652
3653 x509_free( &cacert );
3654 x509_free( &clicert );
3655 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003656#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003657 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003658#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
3660 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003661#else
3662 ((void) verbose);
3663 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003665}
3666
3667#endif
3668
3669#endif