blob: 371c4701f579e98ee09d9054d3e7effc31a1dda9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, 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 Bakker42c65812013-06-24 19:21:59 +02001116int 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 Bakkerff60ee62010-03-16 21:09:09 +00001816int 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 Bakker335db3f2011-04-25 15:28:35 +00001997 (unsigned char *) pwd, strlen( pwd ) );
1998
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 Bakkerf1f21fe2013-06-24 19:17:19 +02002182 * Parse an unencrypted PKCS#8 encoded private RSA key
2183 */
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];
2196
2197 memset(buf, 0, 2048);
2198
2199 p = (unsigned char *) key;
2200 end = p + keylen;
2201
Paul Bakker28144de2013-06-24 19:28:55 +02002202 if( pwdlen == 0 )
2203 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2204
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002205 /*
2206 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2207 *
2208 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2209 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2210 * encryptedData EncryptedData
2211 * }
2212 *
2213 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2214 *
2215 * EncryptedData ::= OCTET STRING
2216 *
2217 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2218 */
2219 if( ( ret = asn1_get_tag( &p, end, &len,
2220 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2221 {
2222 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2223 }
2224
2225 end = p + len;
2226
2227 if( ( ret = asn1_get_tag( &p, end, &len,
2228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2229 {
2230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2231 }
2232
2233 end2 = p + len;
2234
2235 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2236 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2237
2238 pbe_alg_oid.p = p;
2239 p += pbe_alg_oid.len;
2240
2241 /*
2242 * Store the algorithm parameters
2243 */
2244 pbe_params.p = p;
2245 pbe_params.len = end2 - p;
2246 p += pbe_params.len;
2247
2248 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2249 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2250
2251 // buf has been sized to 2048 bytes
2252 if( len > 2048 )
2253 return( POLARSSL_ERR_X509_INVALID_INPUT );
2254
2255 /*
2256 * Decrypt EncryptedData with appropriate PDE
2257 */
Paul Bakker38b50d72013-06-24 19:33:27 +02002258#if defined(POLARSSL_PKCS12_C)
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002259 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2260 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002261 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2262 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2263 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002264 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002265 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2266 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2267
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002268 return( ret );
2269 }
2270 }
2271 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2272 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002273 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2274 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2275 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002276 {
Paul Bakker38b50d72013-06-24 19:33:27 +02002277 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2278 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2279
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002280 return( ret );
2281 }
2282 }
2283 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2284 {
2285 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2286 PKCS12_PBE_DECRYPT,
2287 pwd, pwdlen,
2288 p, len, buf ) ) != 0 )
2289 {
2290 return( ret );
2291 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002292
2293 // Best guess for password mismatch when using RC4. If first tag is
2294 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2295 //
2296 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2297 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002298 }
Paul Bakker38b50d72013-06-24 19:33:27 +02002299 else
2300#endif /* POLARSSL_PKCS12_C */
Paul Bakker28144de2013-06-24 19:28:55 +02002301#if defined(POLARSSL_PKCS5_C)
Paul Bakker38b50d72013-06-24 19:33:27 +02002302 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker28144de2013-06-24 19:28:55 +02002303 {
2304 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2305 p, len, buf ) ) != 0 )
2306 {
2307 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2308 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2309
2310 return( ret );
2311 }
2312 }
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002313 else
Paul Bakker38b50d72013-06-24 19:33:27 +02002314#endif /* POLARSSL_PKCS5_C */
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002315 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2316
2317 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2318}
2319
2320/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002321 * Parse a private RSA key
2322 */
2323int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2324 const unsigned char *pwd, size_t pwdlen )
2325{
2326 int ret;
2327
Paul Bakker96743fc2011-02-12 14:30:57 +00002328#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002329 size_t len;
2330 pem_context pem;
2331
2332 pem_init( &pem );
2333 ret = pem_read_buffer( &pem,
2334 "-----BEGIN RSA PRIVATE KEY-----",
2335 "-----END RSA PRIVATE KEY-----",
2336 key, pwd, pwdlen, &len );
2337 if( ret == 0 )
2338 {
2339 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2340 {
2341 rsa_free( rsa );
2342 }
2343
2344 pem_free( &pem );
2345 return( ret );
2346 }
Paul Bakkera4232a72013-06-24 19:32:25 +02002347 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2348 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2349 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2350 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakkere2f50402013-06-24 19:00:59 +02002351 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002352 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002353
2354 ret = pem_read_buffer( &pem,
2355 "-----BEGIN PRIVATE KEY-----",
2356 "-----END PRIVATE KEY-----",
2357 key, NULL, 0, &len );
2358 if( ret == 0 )
2359 {
2360 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2361 pem.buf, pem.buflen ) ) != 0 )
2362 {
2363 rsa_free( rsa );
2364 }
2365
2366 pem_free( &pem );
2367 return( ret );
2368 }
2369 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkere2f50402013-06-24 19:00:59 +02002370 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002371
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002372 ret = pem_read_buffer( &pem,
2373 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2374 "-----END ENCRYPTED PRIVATE KEY-----",
2375 key, NULL, 0, &len );
2376 if( ret == 0 )
2377 {
2378 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2379 pem.buf, pem.buflen,
2380 pwd, pwdlen ) ) != 0 )
2381 {
2382 rsa_free( rsa );
2383 }
2384
2385 pem_free( &pem );
2386 return( ret );
2387 }
2388 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002389 return( ret );
Paul Bakkere2f50402013-06-24 19:00:59 +02002390#else
2391 ((void) pwd);
2392 ((void) pwdlen);
2393#endif /* POLARSSL_PEM_C */
2394
2395 // At this point we only know it's not a PEM formatted key. Could be any
2396 // of the known DER encoded private key formats
2397 //
2398 // We try the different DER format parsers to see if one passes without
2399 // error
2400 //
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002401 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2402 pwd, pwdlen ) ) == 0 )
Paul Bakkere2f50402013-06-24 19:00:59 +02002403 {
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002404 return( 0 );
Paul Bakkere2f50402013-06-24 19:00:59 +02002405 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002407 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002408
2409 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2410 {
2411 return( ret );
2412 }
2413
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002414 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2415 return( 0 );
2416
2417 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002418
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002419 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2420 return( 0 );
2421
2422 rsa_free( rsa );
Paul Bakker28144de2013-06-24 19:28:55 +02002423
Paul Bakkerf1f21fe2013-06-24 19:17:19 +02002424 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425}
2426
2427/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002428 * Parse a public RSA key
2429 */
Paul Bakker23986e52011-04-24 08:57:21 +00002430int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002431{
Paul Bakker23986e52011-04-24 08:57:21 +00002432 int ret;
2433 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002434 unsigned char *p, *end;
2435 x509_buf alg_oid;
2436#if defined(POLARSSL_PEM_C)
2437 pem_context pem;
2438
2439 pem_init( &pem );
2440 ret = pem_read_buffer( &pem,
2441 "-----BEGIN PUBLIC KEY-----",
2442 "-----END PUBLIC KEY-----",
2443 key, NULL, 0, &len );
2444
2445 if( ret == 0 )
2446 {
2447 /*
2448 * Was PEM encoded
2449 */
2450 keylen = pem.buflen;
2451 }
Paul Bakker00b28602013-06-24 13:02:41 +02002452 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002453 {
2454 pem_free( &pem );
2455 return( ret );
2456 }
2457
2458 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2459#else
2460 p = (unsigned char *) key;
2461#endif
2462 end = p + keylen;
2463
2464 /*
2465 * PublicKeyInfo ::= SEQUENCE {
2466 * algorithm AlgorithmIdentifier,
2467 * PublicKey BIT STRING
2468 * }
2469 *
2470 * AlgorithmIdentifier ::= SEQUENCE {
2471 * algorithm OBJECT IDENTIFIER,
2472 * parameters ANY DEFINED BY algorithm OPTIONAL
2473 * }
2474 *
2475 * RSAPublicKey ::= SEQUENCE {
2476 * modulus INTEGER, -- n
2477 * publicExponent INTEGER -- e
2478 * }
2479 */
2480
2481 if( ( ret = asn1_get_tag( &p, end, &len,
2482 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2483 {
2484#if defined(POLARSSL_PEM_C)
2485 pem_free( &pem );
2486#endif
2487 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002488 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002489 }
2490
2491 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2492 {
2493#if defined(POLARSSL_PEM_C)
2494 pem_free( &pem );
2495#endif
2496 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002497 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002498 }
2499
2500 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2501 {
2502#if defined(POLARSSL_PEM_C)
2503 pem_free( &pem );
2504#endif
2505 rsa_free( rsa );
2506 return( ret );
2507 }
2508
2509 rsa->len = mpi_size( &rsa->N );
2510
2511#if defined(POLARSSL_PEM_C)
2512 pem_free( &pem );
2513#endif
2514
2515 return( 0 );
2516}
2517
Paul Bakkereaa89f82011-04-04 21:36:15 +00002518#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002519/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002520 * Parse DHM parameters
2521 */
Paul Bakker23986e52011-04-24 08:57:21 +00002522int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002523{
Paul Bakker23986e52011-04-24 08:57:21 +00002524 int ret;
2525 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002526 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002527#if defined(POLARSSL_PEM_C)
2528 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002529
Paul Bakker96743fc2011-02-12 14:30:57 +00002530 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002531
Paul Bakker96743fc2011-02-12 14:30:57 +00002532 ret = pem_read_buffer( &pem,
2533 "-----BEGIN DH PARAMETERS-----",
2534 "-----END DH PARAMETERS-----",
2535 dhmin, NULL, 0, &dhminlen );
2536
2537 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002538 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002539 /*
2540 * Was PEM encoded
2541 */
2542 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002543 }
Paul Bakker00b28602013-06-24 13:02:41 +02002544 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002545 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002546 pem_free( &pem );
2547 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002548 }
2549
Paul Bakker96743fc2011-02-12 14:30:57 +00002550 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2551#else
2552 p = (unsigned char *) dhmin;
2553#endif
2554 end = p + dhminlen;
2555
Paul Bakker1b57b062011-01-06 15:48:19 +00002556 memset( dhm, 0, sizeof( dhm_context ) );
2557
Paul Bakker1b57b062011-01-06 15:48:19 +00002558 /*
2559 * DHParams ::= SEQUENCE {
2560 * prime INTEGER, -- P
2561 * generator INTEGER, -- g
2562 * }
2563 */
2564 if( ( ret = asn1_get_tag( &p, end, &len,
2565 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2566 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002567#if defined(POLARSSL_PEM_C)
2568 pem_free( &pem );
2569#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002570 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002571 }
2572
2573 end = p + len;
2574
2575 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2576 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2577 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002578#if defined(POLARSSL_PEM_C)
2579 pem_free( &pem );
2580#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002581 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002582 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002583 }
2584
2585 if( p != end )
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 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002591 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002592 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2593 }
2594
Paul Bakker96743fc2011-02-12 14:30:57 +00002595#if defined(POLARSSL_PEM_C)
2596 pem_free( &pem );
2597#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002598
2599 return( 0 );
2600}
2601
Paul Bakker335db3f2011-04-25 15:28:35 +00002602#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002603/*
2604 * Load and parse a private RSA key
2605 */
2606int x509parse_dhmfile( dhm_context *dhm, const char *path )
2607{
2608 int ret;
2609 size_t n;
2610 unsigned char *buf;
2611
Paul Bakker69e095c2011-12-10 21:55:01 +00002612 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2613 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002614
Paul Bakker27fdf462011-06-09 13:55:13 +00002615 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002616
2617 memset( buf, 0, n + 1 );
2618 free( buf );
2619
2620 return( ret );
2621}
Paul Bakker335db3f2011-04-25 15:28:35 +00002622#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002623#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002626#include <stdarg.h>
2627
2628#if !defined vsnprintf
2629#define vsnprintf _vsnprintf
2630#endif // vsnprintf
2631
2632/*
2633 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2634 * Result value is not size of buffer needed, but -1 if no fit is possible.
2635 *
2636 * This fuction tries to 'fix' this by at least suggesting enlarging the
2637 * size by 20.
2638 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002639static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640{
2641 va_list ap;
2642 int res = -1;
2643
2644 va_start( ap, format );
2645
2646 res = vsnprintf( str, size, format, ap );
2647
2648 va_end( ap );
2649
2650 // No quick fix possible
2651 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002652 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653
2654 return res;
2655}
2656
2657#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002658#endif
2659
Paul Bakkerd98030e2009-05-02 15:13:40 +00002660#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2661
2662#define SAFE_SNPRINTF() \
2663{ \
2664 if( ret == -1 ) \
2665 return( -1 ); \
2666 \
Paul Bakker23986e52011-04-24 08:57:21 +00002667 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002668 p[n - 1] = '\0'; \
2669 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2670 } \
2671 \
Paul Bakker23986e52011-04-24 08:57:21 +00002672 n -= (unsigned int) ret; \
2673 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674}
2675
Paul Bakker5121ce52009-01-03 21:22:43 +00002676/*
2677 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002678 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002680int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002681{
Paul Bakker23986e52011-04-24 08:57:21 +00002682 int ret;
2683 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002685 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002686 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 char s[128], *p;
2688
2689 memset( s, 0, sizeof( s ) );
2690
2691 name = dn;
2692 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002693 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
2695 while( name != NULL )
2696 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002697 if( !name->oid.p )
2698 {
2699 name = name->next;
2700 continue;
2701 }
2702
Paul Bakker74111d32011-01-15 16:57:55 +00002703 if( name != dn )
2704 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002705 ret = snprintf( p, n, ", " );
2706 SAFE_SNPRINTF();
2707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Paul Bakkerc70b9822013-04-07 22:00:46 +02002709 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Paul Bakkerc70b9822013-04-07 22:00:46 +02002711 if( ret == 0 )
2712 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002714 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002715 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
2717 for( i = 0; i < name->val.len; i++ )
2718 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002719 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 break;
2721
2722 c = name->val.p[i];
2723 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2724 s[i] = '?';
2725 else s[i] = c;
2726 }
2727 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002728 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002729 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 name = name->next;
2731 }
2732
Paul Bakker23986e52011-04-24 08:57:21 +00002733 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734}
2735
2736/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002737 * Store the serial in printable form into buf; no more
2738 * than size characters will be written
2739 */
2740int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2741{
Paul Bakker23986e52011-04-24 08:57:21 +00002742 int ret;
2743 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002744 char *p;
2745
2746 p = buf;
2747 n = size;
2748
2749 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002750 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002751
2752 for( i = 0; i < nr; i++ )
2753 {
Paul Bakker93048802011-12-05 14:38:06 +00002754 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002755 continue;
2756
Paul Bakkerdd476992011-01-16 21:34:59 +00002757 ret = snprintf( p, n, "%02X%s",
2758 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2759 SAFE_SNPRINTF();
2760 }
2761
Paul Bakker03c7c252011-11-25 12:37:37 +00002762 if( nr != serial->len )
2763 {
2764 ret = snprintf( p, n, "...." );
2765 SAFE_SNPRINTF();
2766 }
2767
Paul Bakker23986e52011-04-24 08:57:21 +00002768 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002769}
2770
2771/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002774int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2775 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776{
Paul Bakker23986e52011-04-24 08:57:21 +00002777 int ret;
2778 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002779 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002780 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
2782 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Paul Bakkerd98030e2009-05-02 15:13:40 +00002785 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002787 SAFE_SNPRINTF();
2788 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakkerdd476992011-01-16 21:34:59 +00002792 ret = x509parse_serial_gets( p, n, &crt->serial);
2793 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Paul Bakkerd98030e2009-05-02 15:13:40 +00002795 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2796 SAFE_SNPRINTF();
2797 ret = x509parse_dn_gets( p, n, &crt->issuer );
2798 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2801 SAFE_SNPRINTF();
2802 ret = x509parse_dn_gets( p, n, &crt->subject );
2803 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
Paul Bakkerd98030e2009-05-02 15:13:40 +00002805 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2807 crt->valid_from.year, crt->valid_from.mon,
2808 crt->valid_from.day, crt->valid_from.hour,
2809 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002810 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2814 crt->valid_to.year, crt->valid_to.mon,
2815 crt->valid_to.day, crt->valid_to.hour,
2816 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002817 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Paul Bakkerc70b9822013-04-07 22:00:46 +02002819 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002820 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Paul Bakkerc70b9822013-04-07 22:00:46 +02002822 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2823 if( ret != 0 )
2824 ret = snprintf( p, n, "???" );
2825 else
2826 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 SAFE_SNPRINTF();
2828
2829 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002830 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002831 SAFE_SNPRINTF();
2832
Paul Bakker23986e52011-04-24 08:57:21 +00002833 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002834}
2835
Paul Bakker74111d32011-01-15 16:57:55 +00002836/*
2837 * Return an informational string describing the given OID
2838 */
2839const char *x509_oid_get_description( x509_buf *oid )
2840{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002841 const char *desc = NULL;
2842 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002843
Paul Bakkerc70b9822013-04-07 22:00:46 +02002844 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002845
Paul Bakkerc70b9822013-04-07 22:00:46 +02002846 if( ret != 0 )
2847 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002848
Paul Bakkerc70b9822013-04-07 22:00:46 +02002849 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002850}
2851
2852/* Return the x.y.z.... style numeric string for the given OID */
2853int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2854{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002855 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002856}
2857
Paul Bakkerd98030e2009-05-02 15:13:40 +00002858/*
2859 * Return an informational string about the CRL.
2860 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002861int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2862 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002863{
Paul Bakker23986e52011-04-24 08:57:21 +00002864 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002865 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002866 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002867 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002868 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002869
2870 p = buf;
2871 n = size;
2872
2873 ret = snprintf( p, n, "%sCRL version : %d",
2874 prefix, crl->version );
2875 SAFE_SNPRINTF();
2876
2877 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2878 SAFE_SNPRINTF();
2879 ret = x509parse_dn_gets( p, n, &crl->issuer );
2880 SAFE_SNPRINTF();
2881
2882 ret = snprintf( p, n, "\n%sthis update : " \
2883 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2884 crl->this_update.year, crl->this_update.mon,
2885 crl->this_update.day, crl->this_update.hour,
2886 crl->this_update.min, crl->this_update.sec );
2887 SAFE_SNPRINTF();
2888
2889 ret = snprintf( p, n, "\n%snext update : " \
2890 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2891 crl->next_update.year, crl->next_update.mon,
2892 crl->next_update.day, crl->next_update.hour,
2893 crl->next_update.min, crl->next_update.sec );
2894 SAFE_SNPRINTF();
2895
2896 entry = &crl->entry;
2897
2898 ret = snprintf( p, n, "\n%sRevoked certificates:",
2899 prefix );
2900 SAFE_SNPRINTF();
2901
Paul Bakker9be19372009-07-27 20:21:53 +00002902 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002903 {
2904 ret = snprintf( p, n, "\n%sserial number: ",
2905 prefix );
2906 SAFE_SNPRINTF();
2907
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002908 ret = x509parse_serial_gets( p, n, &entry->serial);
2909 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002910
Paul Bakkerd98030e2009-05-02 15:13:40 +00002911 ret = snprintf( p, n, " revocation date: " \
2912 "%04d-%02d-%02d %02d:%02d:%02d",
2913 entry->revocation_date.year, entry->revocation_date.mon,
2914 entry->revocation_date.day, entry->revocation_date.hour,
2915 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002916 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002917
2918 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 }
2920
Paul Bakkerc70b9822013-04-07 22:00:46 +02002921 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakkerc70b9822013-04-07 22:00:46 +02002924 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2925 if( ret != 0 )
2926 ret = snprintf( p, n, "???" );
2927 else
2928 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002929 SAFE_SNPRINTF();
2930
Paul Bakker1e27bb22009-07-19 20:25:25 +00002931 ret = snprintf( p, n, "\n" );
2932 SAFE_SNPRINTF();
2933
Paul Bakker23986e52011-04-24 08:57:21 +00002934 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935}
2936
2937/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002938 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002940int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002941{
Paul Bakkercce9d772011-11-18 14:26:47 +00002942 int year, mon, day;
2943 int hour, min, sec;
2944
2945#if defined(_WIN32)
2946 SYSTEMTIME st;
2947
2948 GetLocalTime(&st);
2949
2950 year = st.wYear;
2951 mon = st.wMonth;
2952 day = st.wDay;
2953 hour = st.wHour;
2954 min = st.wMinute;
2955 sec = st.wSecond;
2956#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002957 struct tm *lt;
2958 time_t tt;
2959
2960 tt = time( NULL );
2961 lt = localtime( &tt );
2962
Paul Bakkercce9d772011-11-18 14:26:47 +00002963 year = lt->tm_year + 1900;
2964 mon = lt->tm_mon + 1;
2965 day = lt->tm_mday;
2966 hour = lt->tm_hour;
2967 min = lt->tm_min;
2968 sec = lt->tm_sec;
2969#endif
2970
2971 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002972 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Paul Bakkercce9d772011-11-18 14:26:47 +00002974 if( year == to->year &&
2975 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002976 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002977
Paul Bakkercce9d772011-11-18 14:26:47 +00002978 if( year == to->year &&
2979 mon == to->mon &&
2980 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002981 return( 1 );
2982
Paul Bakkercce9d772011-11-18 14:26:47 +00002983 if( year == to->year &&
2984 mon == to->mon &&
2985 day == to->day &&
2986 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002987 return( 1 );
2988
Paul Bakkercce9d772011-11-18 14:26:47 +00002989 if( year == to->year &&
2990 mon == to->mon &&
2991 day == to->day &&
2992 hour == to->hour &&
2993 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002994 return( 1 );
2995
Paul Bakkercce9d772011-11-18 14:26:47 +00002996 if( year == to->year &&
2997 mon == to->mon &&
2998 day == to->day &&
2999 hour == to->hour &&
3000 min == to->min &&
3001 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003002 return( 1 );
3003
Paul Bakker40ea7de2009-05-03 10:18:48 +00003004 return( 0 );
3005}
3006
3007/*
3008 * Return 1 if the certificate is revoked, or 0 otherwise.
3009 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003010int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003011{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003012 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003013
3014 while( cur != NULL && cur->serial.len != 0 )
3015 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003016 if( crt->serial.len == cur->serial.len &&
3017 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003018 {
3019 if( x509parse_time_expired( &cur->revocation_date ) )
3020 return( 1 );
3021 }
3022
3023 cur = cur->next;
3024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003025
3026 return( 0 );
3027}
3028
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003029/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003030 * Check that the given certificate is valid accoring to the CRL.
3031 */
3032static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3033 x509_crl *crl_list)
3034{
3035 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003036 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3037 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003038
Paul Bakker915275b2012-09-28 07:10:55 +00003039 if( ca == NULL )
3040 return( flags );
3041
Paul Bakker76fd75a2011-01-16 21:12:10 +00003042 /*
3043 * TODO: What happens if no CRL is present?
3044 * Suggestion: Revocation state should be unknown if no CRL is present.
3045 * For backwards compatibility this is not yet implemented.
3046 */
3047
Paul Bakker915275b2012-09-28 07:10:55 +00003048 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003049 {
Paul Bakker915275b2012-09-28 07:10:55 +00003050 if( crl_list->version == 0 ||
3051 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003052 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3053 crl_list->issuer_raw.len ) != 0 )
3054 {
3055 crl_list = crl_list->next;
3056 continue;
3057 }
3058
3059 /*
3060 * Check if CRL is correctly signed by the trusted CA
3061 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02003062 md_info = md_info_from_type( crl_list->sig_md );
3063 if( md_info == NULL )
3064 {
3065 /*
3066 * Cannot check 'unknown' hash
3067 */
3068 flags |= BADCRL_NOT_TRUSTED;
3069 break;
3070 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003071
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003073
Paul Bakkerc70b9822013-04-07 22:00:46 +02003074 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003075 0, hash, crl_list->sig.p ) == 0 )
3076 {
3077 /*
3078 * CRL is not trusted
3079 */
3080 flags |= BADCRL_NOT_TRUSTED;
3081 break;
3082 }
3083
3084 /*
3085 * Check for validity of CRL (Do not drop out)
3086 */
3087 if( x509parse_time_expired( &crl_list->next_update ) )
3088 flags |= BADCRL_EXPIRED;
3089
3090 /*
3091 * Check if certificate is revoked
3092 */
3093 if( x509parse_revoked(crt, crl_list) )
3094 {
3095 flags |= BADCERT_REVOKED;
3096 break;
3097 }
3098
3099 crl_list = crl_list->next;
3100 }
3101 return flags;
3102}
3103
Paul Bakker57b12982012-02-11 17:38:38 +00003104int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003105{
3106 size_t i;
3107 size_t cn_idx = 0;
3108
Paul Bakker57b12982012-02-11 17:38:38 +00003109 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003110 return( 0 );
3111
3112 for( i = 0; i < strlen( cn ); ++i )
3113 {
3114 if( cn[i] == '.' )
3115 {
3116 cn_idx = i;
3117 break;
3118 }
3119 }
3120
3121 if( cn_idx == 0 )
3122 return( 0 );
3123
Paul Bakker535e97d2012-08-23 10:49:55 +00003124 if( strlen( cn ) - cn_idx == name->len - 1 &&
3125 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003126 {
3127 return( 1 );
3128 }
3129
3130 return( 0 );
3131}
3132
Paul Bakker915275b2012-09-28 07:10:55 +00003133static int x509parse_verify_top(
3134 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003135 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003136 int (*f_vrfy)(void *, x509_cert *, int, int *),
3137 void *p_vrfy )
3138{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003139 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003140 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003141 unsigned char hash[POLARSSL_MD_MAX_SIZE];
3142 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003143
3144 if( x509parse_time_expired( &child->valid_to ) )
3145 *flags |= BADCERT_EXPIRED;
3146
3147 /*
3148 * Child is the top of the chain. Check against the trust_ca list.
3149 */
3150 *flags |= BADCERT_NOT_TRUSTED;
3151
3152 while( trust_ca != NULL )
3153 {
3154 if( trust_ca->version == 0 ||
3155 child->issuer_raw.len != trust_ca->subject_raw.len ||
3156 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3157 child->issuer_raw.len ) != 0 )
3158 {
3159 trust_ca = trust_ca->next;
3160 continue;
3161 }
3162
Paul Bakker9a736322012-11-14 12:39:52 +00003163 /*
3164 * Reduce path_len to check against if top of the chain is
3165 * the same as the trusted CA
3166 */
3167 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3168 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3169 child->issuer_raw.len ) == 0 )
3170 {
3171 check_path_cnt--;
3172 }
3173
Paul Bakker915275b2012-09-28 07:10:55 +00003174 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003175 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003176 {
3177 trust_ca = trust_ca->next;
3178 continue;
3179 }
3180
Paul Bakkerc70b9822013-04-07 22:00:46 +02003181 md_info = md_info_from_type( child->sig_md );
3182 if( md_info == NULL )
3183 {
3184 /*
3185 * Cannot check 'unknown' hash
3186 */
3187 continue;
3188 }
Paul Bakker915275b2012-09-28 07:10:55 +00003189
Paul Bakkerc70b9822013-04-07 22:00:46 +02003190 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003191
Paul Bakkerc70b9822013-04-07 22:00:46 +02003192 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003193 0, hash, child->sig.p ) != 0 )
3194 {
3195 trust_ca = trust_ca->next;
3196 continue;
3197 }
3198
3199 /*
3200 * Top of chain is signed by a trusted CA
3201 */
3202 *flags &= ~BADCERT_NOT_TRUSTED;
3203 break;
3204 }
3205
Paul Bakker9a736322012-11-14 12:39:52 +00003206 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003207 * If top of chain is not the same as the trusted CA send a verify request
3208 * to the callback for any issues with validity and CRL presence for the
3209 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003210 */
3211 if( trust_ca != NULL &&
3212 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3213 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3214 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003215 {
3216 /* Check trusted CA's CRL for then chain's top crt */
3217 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3218
3219 if( x509parse_time_expired( &trust_ca->valid_to ) )
3220 ca_flags |= BADCERT_EXPIRED;
3221
Paul Bakker915275b2012-09-28 07:10:55 +00003222 if( NULL != f_vrfy )
3223 {
Paul Bakker9a736322012-11-14 12:39:52 +00003224 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003225 return( ret );
3226 }
3227 }
3228
3229 /* Call callback on top cert */
3230 if( NULL != f_vrfy )
3231 {
Paul Bakker9a736322012-11-14 12:39:52 +00003232 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003233 return( ret );
3234 }
3235
Paul Bakker915275b2012-09-28 07:10:55 +00003236 *flags |= ca_flags;
3237
3238 return( 0 );
3239}
3240
3241static int x509parse_verify_child(
3242 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003243 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003244 int (*f_vrfy)(void *, x509_cert *, int, int *),
3245 void *p_vrfy )
3246{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003247 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003248 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003249 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003250 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003251 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003252
3253 if( x509parse_time_expired( &child->valid_to ) )
3254 *flags |= BADCERT_EXPIRED;
3255
Paul Bakkerc70b9822013-04-07 22:00:46 +02003256 md_info = md_info_from_type( child->sig_md );
3257 if( md_info == NULL )
3258 {
3259 /*
3260 * Cannot check 'unknown' hash
3261 */
Paul Bakker915275b2012-09-28 07:10:55 +00003262 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003263 }
3264 else
3265 {
3266 md( md_info, child->tbs.p, child->tbs.len, hash );
3267
3268 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3269 child->sig.p ) != 0 )
3270 *flags |= BADCERT_NOT_TRUSTED;
3271 }
3272
Paul Bakker915275b2012-09-28 07:10:55 +00003273 /* Check trusted CA's CRL for the given crt */
3274 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3275
3276 grandparent = parent->next;
3277
3278 while( grandparent != NULL )
3279 {
3280 if( grandparent->version == 0 ||
3281 grandparent->ca_istrue == 0 ||
3282 parent->issuer_raw.len != grandparent->subject_raw.len ||
3283 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3284 parent->issuer_raw.len ) != 0 )
3285 {
3286 grandparent = grandparent->next;
3287 continue;
3288 }
3289 break;
3290 }
3291
Paul Bakker915275b2012-09-28 07:10:55 +00003292 if( grandparent != NULL )
3293 {
3294 /*
3295 * Part of the chain
3296 */
Paul Bakker9a736322012-11-14 12:39:52 +00003297 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 +00003298 if( ret != 0 )
3299 return( ret );
3300 }
3301 else
3302 {
Paul Bakker9a736322012-11-14 12:39:52 +00003303 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 +00003304 if( ret != 0 )
3305 return( ret );
3306 }
3307
3308 /* child is verified to be a child of the parent, call verify callback */
3309 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003310 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003311 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003312
3313 *flags |= parent_flags;
3314
3315 return( 0 );
3316}
3317
Paul Bakker76fd75a2011-01-16 21:12:10 +00003318/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 * Verify the certificate validity
3320 */
3321int x509parse_verify( x509_cert *crt,
3322 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003323 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003324 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003325 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003326 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003327{
Paul Bakker23986e52011-04-24 08:57:21 +00003328 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003329 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003330 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003331 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003333 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Paul Bakker40ea7de2009-05-03 10:18:48 +00003335 *flags = 0;
3336
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 if( cn != NULL )
3338 {
3339 name = &crt->subject;
3340 cn_len = strlen( cn );
3341
Paul Bakker4d2c1242012-05-10 14:12:46 +00003342 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003344 cur = &crt->subject_alt_names;
3345
3346 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003347 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003348 if( cur->buf.len == cn_len &&
3349 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003350 break;
3351
Paul Bakker535e97d2012-08-23 10:49:55 +00003352 if( cur->buf.len > 2 &&
3353 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003354 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003355 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003356
Paul Bakker4d2c1242012-05-10 14:12:46 +00003357 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003358 }
3359
3360 if( cur == NULL )
3361 *flags |= BADCERT_CN_MISMATCH;
3362 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003363 else
3364 {
3365 while( name != NULL )
3366 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003367 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003368 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003369 if( name->val.len == cn_len &&
3370 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003371 break;
3372
Paul Bakker535e97d2012-08-23 10:49:55 +00003373 if( name->val.len > 2 &&
3374 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003375 x509_wildcard_verify( cn, &name->val ) )
3376 break;
3377 }
3378
3379 name = name->next;
3380 }
3381
3382 if( name == NULL )
3383 *flags |= BADCERT_CN_MISMATCH;
3384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 }
3386
Paul Bakker5121ce52009-01-03 21:22:43 +00003387 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003388 * Iterate upwards in the given cert chain, to find our crt parent.
3389 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003391 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
Paul Bakker76fd75a2011-01-16 21:12:10 +00003393 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003395 if( parent->ca_istrue == 0 ||
3396 crt->issuer_raw.len != parent->subject_raw.len ||
3397 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 crt->issuer_raw.len ) != 0 )
3399 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003400 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 continue;
3402 }
Paul Bakker915275b2012-09-28 07:10:55 +00003403 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
Paul Bakker915275b2012-09-28 07:10:55 +00003406 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 {
Paul Bakker915275b2012-09-28 07:10:55 +00003408 /*
3409 * Part of the chain
3410 */
Paul Bakker9a736322012-11-14 12:39:52 +00003411 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003412 if( ret != 0 )
3413 return( ret );
3414 }
3415 else
Paul Bakker74111d32011-01-15 16:57:55 +00003416 {
Paul Bakker9a736322012-11-14 12:39:52 +00003417 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003418 if( ret != 0 )
3419 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003420 }
Paul Bakker915275b2012-09-28 07:10:55 +00003421
3422 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003423 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003424
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 return( 0 );
3426}
3427
3428/*
3429 * Unallocate all certificate data
3430 */
3431void x509_free( x509_cert *crt )
3432{
3433 x509_cert *cert_cur = crt;
3434 x509_cert *cert_prv;
3435 x509_name *name_cur;
3436 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003437 x509_sequence *seq_cur;
3438 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
3440 if( crt == NULL )
3441 return;
3442
3443 do
3444 {
3445 rsa_free( &cert_cur->rsa );
3446
3447 name_cur = cert_cur->issuer.next;
3448 while( name_cur != NULL )
3449 {
3450 name_prv = name_cur;
3451 name_cur = name_cur->next;
3452 memset( name_prv, 0, sizeof( x509_name ) );
3453 free( name_prv );
3454 }
3455
3456 name_cur = cert_cur->subject.next;
3457 while( name_cur != NULL )
3458 {
3459 name_prv = name_cur;
3460 name_cur = name_cur->next;
3461 memset( name_prv, 0, sizeof( x509_name ) );
3462 free( name_prv );
3463 }
3464
Paul Bakker74111d32011-01-15 16:57:55 +00003465 seq_cur = cert_cur->ext_key_usage.next;
3466 while( seq_cur != NULL )
3467 {
3468 seq_prv = seq_cur;
3469 seq_cur = seq_cur->next;
3470 memset( seq_prv, 0, sizeof( x509_sequence ) );
3471 free( seq_prv );
3472 }
3473
Paul Bakker8afa70d2012-02-11 18:42:45 +00003474 seq_cur = cert_cur->subject_alt_names.next;
3475 while( seq_cur != NULL )
3476 {
3477 seq_prv = seq_cur;
3478 seq_cur = seq_cur->next;
3479 memset( seq_prv, 0, sizeof( x509_sequence ) );
3480 free( seq_prv );
3481 }
3482
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 if( cert_cur->raw.p != NULL )
3484 {
3485 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3486 free( cert_cur->raw.p );
3487 }
3488
3489 cert_cur = cert_cur->next;
3490 }
3491 while( cert_cur != NULL );
3492
3493 cert_cur = crt;
3494 do
3495 {
3496 cert_prv = cert_cur;
3497 cert_cur = cert_cur->next;
3498
3499 memset( cert_prv, 0, sizeof( x509_cert ) );
3500 if( cert_prv != crt )
3501 free( cert_prv );
3502 }
3503 while( cert_cur != NULL );
3504}
3505
Paul Bakkerd98030e2009-05-02 15:13:40 +00003506/*
3507 * Unallocate all CRL data
3508 */
3509void x509_crl_free( x509_crl *crl )
3510{
3511 x509_crl *crl_cur = crl;
3512 x509_crl *crl_prv;
3513 x509_name *name_cur;
3514 x509_name *name_prv;
3515 x509_crl_entry *entry_cur;
3516 x509_crl_entry *entry_prv;
3517
3518 if( crl == NULL )
3519 return;
3520
3521 do
3522 {
3523 name_cur = crl_cur->issuer.next;
3524 while( name_cur != NULL )
3525 {
3526 name_prv = name_cur;
3527 name_cur = name_cur->next;
3528 memset( name_prv, 0, sizeof( x509_name ) );
3529 free( name_prv );
3530 }
3531
3532 entry_cur = crl_cur->entry.next;
3533 while( entry_cur != NULL )
3534 {
3535 entry_prv = entry_cur;
3536 entry_cur = entry_cur->next;
3537 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3538 free( entry_prv );
3539 }
3540
3541 if( crl_cur->raw.p != NULL )
3542 {
3543 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3544 free( crl_cur->raw.p );
3545 }
3546
3547 crl_cur = crl_cur->next;
3548 }
3549 while( crl_cur != NULL );
3550
3551 crl_cur = crl;
3552 do
3553 {
3554 crl_prv = crl_cur;
3555 crl_cur = crl_cur->next;
3556
3557 memset( crl_prv, 0, sizeof( x509_crl ) );
3558 if( crl_prv != crl )
3559 free( crl_prv );
3560 }
3561 while( crl_cur != NULL );
3562}
3563
Paul Bakker40e46942009-01-03 21:51:57 +00003564#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003565
Paul Bakker40e46942009-01-03 21:51:57 +00003566#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003567
3568/*
3569 * Checkup routine
3570 */
3571int x509_self_test( int verbose )
3572{
Paul Bakker5690efc2011-05-26 13:16:06 +00003573#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003574 int ret;
3575 int flags;
3576 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003577 x509_cert cacert;
3578 x509_cert clicert;
3579 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003580#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003581 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
3584 if( verbose != 0 )
3585 printf( " X.509 certificate load: " );
3586
3587 memset( &clicert, 0, sizeof( x509_cert ) );
3588
Paul Bakker3c2122f2013-06-24 19:03:14 +02003589 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003590 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 if( ret != 0 )
3592 {
3593 if( verbose != 0 )
3594 printf( "failed\n" );
3595
3596 return( ret );
3597 }
3598
3599 memset( &cacert, 0, sizeof( x509_cert ) );
3600
Paul Bakker3c2122f2013-06-24 19:03:14 +02003601 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003602 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 if( ret != 0 )
3604 {
3605 if( verbose != 0 )
3606 printf( "failed\n" );
3607
3608 return( ret );
3609 }
3610
3611 if( verbose != 0 )
3612 printf( "passed\n X.509 private key load: " );
3613
3614 i = strlen( test_ca_key );
3615 j = strlen( test_ca_pwd );
3616
Paul Bakker66b78b22011-03-25 14:22:50 +00003617 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3618
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003620 (const unsigned char *) test_ca_key, i,
3621 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 {
3623 if( verbose != 0 )
3624 printf( "failed\n" );
3625
3626 return( ret );
3627 }
3628
3629 if( verbose != 0 )
3630 printf( "passed\n X.509 signature verify: ");
3631
Paul Bakker23986e52011-04-24 08:57:21 +00003632 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 if( ret != 0 )
3634 {
Paul Bakker23986e52011-04-24 08:57:21 +00003635 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 if( verbose != 0 )
3637 printf( "failed\n" );
3638
3639 return( ret );
3640 }
3641
Paul Bakker5690efc2011-05-26 13:16:06 +00003642#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003644 printf( "passed\n X.509 DHM parameter load: " );
3645
3646 i = strlen( test_dhm_params );
3647 j = strlen( test_ca_pwd );
3648
Paul Bakker3c2122f2013-06-24 19:03:14 +02003649 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003650 {
3651 if( verbose != 0 )
3652 printf( "failed\n" );
3653
3654 return( ret );
3655 }
3656
3657 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
3661 x509_free( &cacert );
3662 x509_free( &clicert );
3663 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003664#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003665 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
3668 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003669#else
3670 ((void) verbose);
3671 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003673}
3674
3675#endif
3676
3677#endif