blob: 4a9b7c19f1ba32a8fb114a8a9f078f281c219dc9 [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 Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakker1fd43212013-06-17 15:14:42 +020064#if defined(POLARSSL_PKCS5_C)
65#include "polarssl/pkcs5.h"
66#endif
Paul Bakker14a222c2013-06-18 16:35:48 +020067#if defined(POLARSSL_PKCS12_C)
68#include "polarssl/pkcs12.h"
69#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71#include <string.h>
72#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000073#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000074#include <windows.h>
75#else
Paul Bakker5121ce52009-01-03 21:22:43 +000076#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker335db3f2011-04-25 15:28:35 +000079#if defined(POLARSSL_FS_IO)
80#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000081#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000082#include <sys/types.h>
Paul Bakkercbfcaa92013-06-13 09:20:25 +020083#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000084#include <dirent.h>
85#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000086#endif
87
Paul Bakkercf6e95d2013-06-12 13:18:15 +020088/* Compare a given OID string with an OID x509_buf * */
89#define OID_CMP(oid_str, oid_buf) \
90 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
91 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/*
Paul Bakker5121ce52009-01-03 21:22:43 +000094 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
95 */
96static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000097 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000098 int *ver )
99{
Paul Bakker23986e52011-04-24 08:57:21 +0000100 int ret;
101 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103 if( ( ret = asn1_get_tag( p, end, &len,
104 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
105 {
Paul Bakker40e46942009-01-03 21:51:57 +0000106 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000107 {
108 *ver = 0;
109 return( 0 );
110 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112 return( ret );
113 }
114
115 end = *p + len;
116
117 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000118 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000121 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000122 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 return( 0 );
125}
126
127/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000128 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000129 */
130static int x509_crl_get_version( unsigned char **p,
131 const unsigned char *end,
132 int *ver )
133{
134 int ret;
135
136 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
137 {
138 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000139 {
140 *ver = 0;
141 return( 0 );
142 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000143
144 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
145 }
146
147 return( 0 );
148}
149
150/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 * CertificateSerialNumber ::= INTEGER
152 */
153static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 x509_buf *serial )
156{
157 int ret;
158
159 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000160 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000161 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
164 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000166 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 serial->tag = *(*p)++;
169
170 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 serial->p = *p;
174 *p += serial->len;
175
176 return( 0 );
177}
178
179/*
180 * AlgorithmIdentifier ::= SEQUENCE {
181 * algorithm OBJECT IDENTIFIER,
182 * parameters ANY DEFINED BY algorithm OPTIONAL }
183 */
184static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000185 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 x509_buf *alg )
187{
Paul Bakker23986e52011-04-24 08:57:21 +0000188 int ret;
189 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( ( ret = asn1_get_tag( p, end, &len,
192 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000193 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 end = *p + len;
196 alg->tag = **p;
197
198 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000199 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 alg->p = *p;
202 *p += alg->len;
203
204 if( *p == end )
205 return( 0 );
206
207 /*
208 * assume the algorithm parameters must be NULL
209 */
210 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000211 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000214 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000215 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 return( 0 );
218}
219
220/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * AttributeTypeAndValue ::= SEQUENCE {
222 * type AttributeType,
223 * value AttributeValue }
224 *
225 * AttributeType ::= OBJECT IDENTIFIER
226 *
227 * AttributeValue ::= ANY DEFINED BY AttributeType
228 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000229static int x509_get_attr_type_value( unsigned char **p,
230 const unsigned char *end,
231 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000232{
Paul Bakker23986e52011-04-24 08:57:21 +0000233 int ret;
234 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 x509_buf *oid;
236 x509_buf *val;
237
238 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000240 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 oid = &cur->oid;
243 oid->tag = **p;
244
245 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000246 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 oid->p = *p;
249 *p += oid->len;
250
251 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000252 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000253 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
256 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
257 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000258 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000259 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 val = &cur->val;
262 val->tag = *(*p)++;
263
264 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000265 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 val->p = *p;
268 *p += val->len;
269
270 cur->next = NULL;
271
Paul Bakker400ff6f2011-02-20 10:40:16 +0000272 return( 0 );
273}
274
275/*
276 * RelativeDistinguishedName ::=
277 * SET OF AttributeTypeAndValue
278 *
279 * AttributeTypeAndValue ::= SEQUENCE {
280 * type AttributeType,
281 * value AttributeValue }
282 *
283 * AttributeType ::= OBJECT IDENTIFIER
284 *
285 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200286 *
287 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
288 * the most common case, and our x509_name structure currently can't handle
289 * more than that.
Paul Bakker400ff6f2011-02-20 10:40:16 +0000290 */
291static int x509_get_name( unsigned char **p,
292 const unsigned char *end,
293 x509_name *cur )
294{
Paul Bakker23986e52011-04-24 08:57:21 +0000295 int ret;
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200296 size_t set_len;
297 const unsigned char *end_set;
298
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100299 /* don't use recursion, we'd risk stack overflow if not optimized */
300 while( 1 )
301 {
302 /*
303 * parse first SET, restricted to 1 element
304 */
305 if( ( ret = asn1_get_tag( p, end, &set_len,
306 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
307 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000308
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100309 end_set = *p + set_len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000310
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100311 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
312 return( ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000313
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100314 if( *p != end_set )
315 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100317 /*
318 * continue until end of SEQUENCE is reached
319 */
320 if( *p == end )
321 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100323 cur->next = (x509_name *) malloc( sizeof( x509_name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100325 if( cur->next == NULL )
326 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100328 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker430ffbe2012-05-01 08:14:20 +0000329
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100330 cur = cur->next;
331 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000332}
333
334/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 * Time ::= CHOICE {
336 * utcTime UTCTime,
337 * generalTime GeneralizedTime }
338 */
Paul Bakker91200182010-02-18 21:26:15 +0000339static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341 x509_time *time )
342{
Paul Bakker23986e52011-04-24 08:57:21 +0000343 int ret;
344 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000345 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000346 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker91200182010-02-18 21:26:15 +0000348 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000349 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
350 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000351
Paul Bakker91200182010-02-18 21:26:15 +0000352 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000353
Paul Bakker91200182010-02-18 21:26:15 +0000354 if ( tag == ASN1_UTC_TIME )
355 {
356 (*p)++;
357 ret = asn1_get_len( p, end, &len );
358
359 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker91200182010-02-18 21:26:15 +0000362 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000363 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
364 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000365
Paul Bakker243d6182014-07-08 14:40:58 +0200366 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000367 &time->year, &time->mon, &time->day,
368 &time->hour, &time->min, &time->sec ) < 5 )
369 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000370
Paul Bakker400ff6f2011-02-20 10:40:16 +0000371 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000372 time->year += 1900;
373
374 *p += len;
375
376 return( 0 );
377 }
378 else if ( tag == ASN1_GENERALIZED_TIME )
379 {
380 (*p)++;
381 ret = asn1_get_len( p, end, &len );
382
383 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000384 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000385
386 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000387 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
388 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000389
Paul Bakker243d6182014-07-08 14:40:58 +0200390 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000391 &time->year, &time->mon, &time->day,
392 &time->hour, &time->min, &time->sec ) < 5 )
393 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
394
395 *p += len;
396
397 return( 0 );
398 }
399 else
Paul Bakker9d781402011-05-09 16:17:09 +0000400 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000401}
402
403
404/*
405 * Validity ::= SEQUENCE {
406 * notBefore Time,
407 * notAfter Time }
408 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000409static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000410 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 x509_time *from,
412 x509_time *to )
413{
Paul Bakker23986e52011-04-24 08:57:21 +0000414 int ret;
415 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 if( ( ret = asn1_get_tag( p, end, &len,
418 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000419 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421 end = *p + len;
422
Paul Bakker91200182010-02-18 21:26:15 +0000423 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000424 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Paul Bakker91200182010-02-18 21:26:15 +0000426 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000427 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000430 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000431 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 return( 0 );
434}
435
436/*
437 * SubjectPublicKeyInfo ::= SEQUENCE {
438 * algorithm AlgorithmIdentifier,
439 * subjectPublicKey BIT STRING }
440 */
441static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000442 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 x509_buf *pk_alg_oid,
444 mpi *N, mpi *E )
445{
Paul Bakker65a19092013-06-06 21:14:58 +0200446 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000447 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 unsigned char *end2;
449
450 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
451 return( ret );
452
453 /*
454 * only RSA public keys handled at this time
455 */
Paul Bakker65a19092013-06-06 21:14:58 +0200456 if( pk_alg_oid->len != 9 ||
457 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) != 0 )
Paul Bakker400ff6f2011-02-20 10:40:16 +0000458 {
Paul Bakkered56b222011-07-13 11:26:43 +0000459 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker65a19092013-06-06 21:14:58 +0200460 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000463 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000467 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 end2 = *p + len;
470
471 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000472 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 /*
475 * RSAPublicKey ::= SEQUENCE {
476 * modulus INTEGER, -- n
477 * publicExponent INTEGER -- e
478 * }
479 */
480 if( ( ret = asn1_get_tag( p, end2, &len,
481 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000485 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000486 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
489 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000490 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000493 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 return( 0 );
497}
498
499static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000500 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 x509_buf *sig )
502{
Paul Bakker23986e52011-04-24 08:57:21 +0000503 int ret;
504 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Paul Bakker8afa70d2012-02-11 18:42:45 +0000506 if( ( end - *p ) < 1 )
507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
508 POLARSSL_ERR_ASN1_OUT_OF_DATA );
509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 sig->tag = **p;
511
512 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000513 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker74111d32011-01-15 16:57:55 +0000515
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 sig->len = len;
520 sig->p = *p;
521
522 *p += len;
523
524 return( 0 );
525}
526
527/*
528 * X.509 v2/v3 unique identifier (not parsed)
529 */
530static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 x509_buf *uid, int n )
533{
534 int ret;
535
536 if( *p == end )
537 return( 0 );
538
539 uid->tag = **p;
540
541 if( ( ret = asn1_get_tag( p, end, &uid->len,
542 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
543 {
Paul Bakker40e46942009-01-03 21:51:57 +0000544 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 return( 0 );
546
547 return( ret );
548 }
549
550 uid->p = *p;
551 *p += uid->len;
552
553 return( 0 );
554}
555
556/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000557 * X.509 Extensions (No parsing of extensions, pointer should
558 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
560static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000562 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000563{
Paul Bakker23986e52011-04-24 08:57:21 +0000564 int ret;
565 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 if( *p == end )
568 return( 0 );
569
570 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000571
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000573 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 ext->p = *p;
577 end = *p + ext->len;
578
579 /*
580 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
581 *
582 * Extension ::= SEQUENCE {
583 * extnID OBJECT IDENTIFIER,
584 * critical BOOLEAN DEFAULT FALSE,
585 * extnValue OCTET STRING }
586 */
587 if( ( ret = asn1_get_tag( p, end, &len,
588 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000589 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000592 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000593 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakkerd98030e2009-05-02 15:13:40 +0000595 return( 0 );
596}
597
598/*
599 * X.509 CRL v2 extensions (no extensions parsed yet.)
600 */
601static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000602 const unsigned char *end,
603 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000604{
Paul Bakker23986e52011-04-24 08:57:21 +0000605 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000606 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000607
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 /* Get explicit tag */
609 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000610 {
611 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
612 return( 0 );
613
614 return( ret );
615 }
616
617 while( *p < end )
618 {
619 if( ( ret = asn1_get_tag( p, end, &len,
620 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000621 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000622
623 *p += len;
624 }
625
626 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000627 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000628 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
629
630 return( 0 );
631}
632
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000633/*
634 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
635 */
636static int x509_get_crl_entry_ext( unsigned char **p,
637 const unsigned char *end,
638 x509_buf *ext )
639{
640 int ret;
641 size_t len = 0;
642
643 /* OPTIONAL */
644 if (end <= *p)
645 return( 0 );
646
647 ext->tag = **p;
648 ext->p = *p;
649
650 /*
651 * Get CRL-entry extension sequence header
652 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
653 */
654 if( ( ret = asn1_get_tag( p, end, &ext->len,
655 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
656 {
657 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
658 {
659 ext->p = NULL;
660 return( 0 );
661 }
662 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
663 }
664
665 end = *p + ext->len;
666
667 if( end != *p + ext->len )
668 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
669 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
670
671 while( *p < end )
672 {
673 if( ( ret = asn1_get_tag( p, end, &len,
674 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
676
677 *p += len;
678 }
679
680 if( *p != end )
681 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
682 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
683
684 return( 0 );
685}
686
Paul Bakker74111d32011-01-15 16:57:55 +0000687static int x509_get_basic_constraints( unsigned char **p,
688 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000689 int *ca_istrue,
690 int *max_pathlen )
691{
Paul Bakker23986e52011-04-24 08:57:21 +0000692 int ret;
693 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000694
695 /*
696 * BasicConstraints ::= SEQUENCE {
697 * cA BOOLEAN DEFAULT FALSE,
698 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
699 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000700 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000701 *max_pathlen = 0; /* endless */
702
703 if( ( ret = asn1_get_tag( p, end, &len,
704 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000705 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000706
707 if( *p == end )
708 return 0;
709
Paul Bakker3cccddb2011-01-16 21:46:31 +0000710 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000711 {
712 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000713 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000714
715 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000716 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000717
Paul Bakker3cccddb2011-01-16 21:46:31 +0000718 if( *ca_istrue != 0 )
719 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000720 }
721
722 if( *p == end )
723 return 0;
724
725 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000726 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000727
728 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000729 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000730 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
731
732 (*max_pathlen)++;
733
Paul Bakker74111d32011-01-15 16:57:55 +0000734 return 0;
735}
736
737static int x509_get_ns_cert_type( unsigned char **p,
738 const unsigned char *end,
739 unsigned char *ns_cert_type)
740{
741 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000742 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000743
744 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000745 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000746
747 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000748 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000749 POLARSSL_ERR_ASN1_INVALID_LENGTH );
750
751 /* Get actual bitstring */
752 *ns_cert_type = *bs.p;
753 return 0;
754}
755
756static int x509_get_key_usage( unsigned char **p,
757 const unsigned char *end,
758 unsigned char *key_usage)
759{
760 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000761 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000762
763 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000764 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000765
Paul Bakker94a67962012-08-23 13:03:52 +0000766 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000767 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000768 POLARSSL_ERR_ASN1_INVALID_LENGTH );
769
770 /* Get actual bitstring */
771 *key_usage = *bs.p;
772 return 0;
773}
774
Paul Bakkerd98030e2009-05-02 15:13:40 +0000775/*
Paul Bakker74111d32011-01-15 16:57:55 +0000776 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
777 *
778 * KeyPurposeId ::= OBJECT IDENTIFIER
779 */
780static int x509_get_ext_key_usage( unsigned char **p,
781 const unsigned char *end,
782 x509_sequence *ext_key_usage)
783{
784 int ret;
785
786 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000788
789 /* Sequence length must be >= 1 */
790 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000791 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000792 POLARSSL_ERR_ASN1_INVALID_LENGTH );
793
794 return 0;
795}
796
797/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000798 * SubjectAltName ::= GeneralNames
799 *
800 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
801 *
802 * GeneralName ::= CHOICE {
803 * otherName [0] OtherName,
804 * rfc822Name [1] IA5String,
805 * dNSName [2] IA5String,
806 * x400Address [3] ORAddress,
807 * directoryName [4] Name,
808 * ediPartyName [5] EDIPartyName,
809 * uniformResourceIdentifier [6] IA5String,
810 * iPAddress [7] OCTET STRING,
811 * registeredID [8] OBJECT IDENTIFIER }
812 *
813 * OtherName ::= SEQUENCE {
814 * type-id OBJECT IDENTIFIER,
815 * value [0] EXPLICIT ANY DEFINED BY type-id }
816 *
817 * EDIPartyName ::= SEQUENCE {
818 * nameAssigner [0] DirectoryString OPTIONAL,
819 * partyName [1] DirectoryString }
820 *
821 * NOTE: PolarSSL only parses and uses dNSName at this point.
822 */
823static int x509_get_subject_alt_name( unsigned char **p,
824 const unsigned char *end,
825 x509_sequence *subject_alt_name )
826{
827 int ret;
828 size_t len, tag_len;
829 asn1_buf *buf;
830 unsigned char tag;
831 asn1_sequence *cur = subject_alt_name;
832
833 /* Get main sequence tag */
834 if( ( ret = asn1_get_tag( p, end, &len,
835 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
836 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
837
838 if( *p + len != end )
839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
840 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
841
842 while( *p < end )
843 {
844 if( ( end - *p ) < 1 )
845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
846 POLARSSL_ERR_ASN1_OUT_OF_DATA );
847
848 tag = **p;
849 (*p)++;
850 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
852
853 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
855 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
856
857 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
858 {
859 *p += tag_len;
860 continue;
861 }
862
863 buf = &(cur->buf);
864 buf->tag = tag;
865 buf->p = *p;
866 buf->len = tag_len;
867 *p += buf->len;
868
869 /* Allocate and assign next pointer */
870 if (*p < end)
871 {
Manuel Pégourié-Gonnardfdec9572014-11-11 23:11:16 +0100872 if( cur->next != NULL )
873 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
874
Paul Bakkera8cd2392012-02-11 16:09:32 +0000875 cur->next = (asn1_sequence *) malloc(
876 sizeof( asn1_sequence ) );
877
878 if( cur->next == NULL )
879 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
880 POLARSSL_ERR_ASN1_MALLOC_FAILED );
881
Paul Bakker535e97d2012-08-23 10:49:55 +0000882 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000883 cur = cur->next;
884 }
885 }
886
887 /* Set final sequence entry's next pointer to NULL */
888 cur->next = NULL;
889
890 if( *p != end )
891 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
892 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
893
894 return( 0 );
895}
896
897/*
Paul Bakker74111d32011-01-15 16:57:55 +0000898 * X.509 v3 extensions
899 *
900 * TODO: Perform all of the basic constraints tests required by the RFC
901 * TODO: Set values for undetected extensions to a sane default?
902 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000903 */
904static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000905 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000906 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000907{
Paul Bakker23986e52011-04-24 08:57:21 +0000908 int ret;
909 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000910 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000911
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000912 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000913 {
914 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
915 return( 0 );
916
917 return( ret );
918 }
919
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 while( *p < end )
921 {
Paul Bakker74111d32011-01-15 16:57:55 +0000922 /*
923 * Extension ::= SEQUENCE {
924 * extnID OBJECT IDENTIFIER,
925 * critical BOOLEAN DEFAULT FALSE,
926 * extnValue OCTET STRING }
927 */
928 x509_buf extn_oid = {0, 0, NULL};
929 int is_critical = 0; /* DEFAULT FALSE */
930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 if( ( ret = asn1_get_tag( p, end, &len,
932 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000933 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000935 end_ext_data = *p + len;
936
Paul Bakker74111d32011-01-15 16:57:55 +0000937 /* Get extension ID */
938 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Paul Bakker74111d32011-01-15 16:57:55 +0000940 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000941 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000942
Paul Bakker74111d32011-01-15 16:57:55 +0000943 extn_oid.p = *p;
944 *p += extn_oid.len;
945
946 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000947 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000948 POLARSSL_ERR_ASN1_OUT_OF_DATA );
949
950 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000951 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000952 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000953 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Paul Bakker74111d32011-01-15 16:57:55 +0000955 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000956 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000958 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000960 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000961
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000962 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000963 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000964 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Paul Bakker74111d32011-01-15 16:57:55 +0000966 /*
967 * Detect supported extensions
968 */
969 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
970 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971 {
Paul Bakker74111d32011-01-15 16:57:55 +0000972 /* Parse basic constraints */
973 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000974 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000975 return ( ret );
976 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 }
Paul Bakker74111d32011-01-15 16:57:55 +0000978 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
979 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
980 {
981 /* Parse netscape certificate type */
982 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
983 &crt->ns_cert_type ) ) != 0 )
984 return ( ret );
985 crt->ext_types |= EXT_NS_CERT_TYPE;
986 }
987 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
988 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
989 {
990 /* Parse key usage */
991 if( ( ret = x509_get_key_usage( p, end_ext_octet,
992 &crt->key_usage ) ) != 0 )
993 return ( ret );
994 crt->ext_types |= EXT_KEY_USAGE;
995 }
996 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
997 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
998 {
999 /* Parse extended key usage */
1000 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1001 &crt->ext_key_usage ) ) != 0 )
1002 return ( ret );
1003 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1004 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001005 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1006 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1007 {
1008 /* Parse extended key usage */
1009 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1010 &crt->subject_alt_names ) ) != 0 )
1011 return ( ret );
1012 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1013 }
Paul Bakker74111d32011-01-15 16:57:55 +00001014 else
1015 {
1016 /* No parser found, skip extension */
1017 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Paul Bakker5c721f92011-07-27 16:51:09 +00001019#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001020 if( is_critical )
1021 {
1022 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001023 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001024 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1025 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001026#endif
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 Bakker27d66162010-03-17 06:56:01 +00001102static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1103{
1104 if( sig_oid->len == 9 &&
1105 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1106 {
1107 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1108 {
1109 *sig_alg = sig_oid->p[8];
1110 return( 0 );
1111 }
1112
1113 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1114 {
1115 *sig_alg = sig_oid->p[8];
1116 return( 0 );
1117 }
1118
1119 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1120 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001121 if( sig_oid->len == 5 &&
1122 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1123 {
1124 *sig_alg = SIG_RSA_SHA1;
1125 return( 0 );
1126 }
Paul Bakker27d66162010-03-17 06:56:01 +00001127
1128 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1129}
1130
Paul Bakkerd98030e2009-05-02 15:13:40 +00001131/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001132 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001134static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1135 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001136{
Paul Bakker23986e52011-04-24 08:57:21 +00001137 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001138 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001139 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Paul Bakker320a4b52009-03-28 18:52:39 +00001141 /*
1142 * Check for valid input
1143 */
1144 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001145 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001146
Paul Bakker96743fc2011-02-12 14:30:57 +00001147 p = (unsigned char *) malloc( len = buflen );
1148
1149 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001150 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001151
1152 memcpy( p, buf, buflen );
1153
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 crt->raw.p = p;
1155 crt->raw.len = len;
1156 end = p + len;
1157
1158 /*
1159 * Certificate ::= SEQUENCE {
1160 * tbsCertificate TBSCertificate,
1161 * signatureAlgorithm AlgorithmIdentifier,
1162 * signatureValue BIT STRING }
1163 */
1164 if( ( ret = asn1_get_tag( &p, end, &len,
1165 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001168 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
1170
Paul Bakkerb00ca422012-09-25 12:10:00 +00001171 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001173 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001174 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001175 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001177 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001178
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 /*
1180 * TBSCertificate ::= SEQUENCE {
1181 */
1182 crt->tbs.p = p;
1183
1184 if( ( ret = asn1_get_tag( &p, end, &len,
1185 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1186 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001187 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001188 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 }
1190
1191 end = p + len;
1192 crt->tbs.len = end - crt->tbs.p;
1193
1194 /*
1195 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1196 *
1197 * CertificateSerialNumber ::= INTEGER
1198 *
1199 * signature AlgorithmIdentifier
1200 */
1201 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1202 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1203 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1204 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001205 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 return( ret );
1207 }
1208
1209 crt->version++;
1210
1211 if( crt->version > 3 )
1212 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001213 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001214 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakker27d66162010-03-17 06:56:01 +00001217 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
1223 /*
1224 * issuer Name
1225 */
1226 crt->issuer_raw.p = p;
1227
1228 if( ( ret = asn1_get_tag( &p, end, &len,
1229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001232 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
1235 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 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 crt->issuer_raw.len = p - crt->issuer_raw.p;
1242
1243 /*
1244 * Validity ::= SEQUENCE {
1245 * notBefore Time,
1246 * notAfter Time }
1247 *
1248 */
1249 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1250 &crt->valid_to ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
1256 /*
1257 * subject Name
1258 */
1259 crt->subject_raw.p = p;
1260
1261 if( ( ret = asn1_get_tag( &p, end, &len,
1262 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001265 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 }
1267
Paul Bakkercefb3962012-06-27 11:51:09 +00001268 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001270 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 return( ret );
1272 }
1273
1274 crt->subject_raw.len = p - crt->subject_raw.p;
1275
1276 /*
1277 * SubjectPublicKeyInfo ::= SEQUENCE
1278 * algorithm AlgorithmIdentifier,
1279 * subjectPublicKey BIT STRING }
1280 */
1281 if( ( ret = asn1_get_tag( &p, end, &len,
1282 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001285 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
1287
1288 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1289 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 return( ret );
1293 }
1294
1295 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1296 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001297 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 return( ret );
1299 }
1300
1301 crt->rsa.len = mpi_size( &crt->rsa.N );
1302
1303 /*
1304 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1307 * -- If present, version shall be v2 or v3
1308 * extensions [3] EXPLICIT Extensions OPTIONAL
1309 * -- If present, version shall be v3
1310 */
1311 if( crt->version == 2 || crt->version == 3 )
1312 {
1313 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1314 if( ret != 0 )
1315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319 }
1320
1321 if( crt->version == 2 || crt->version == 3 )
1322 {
1323 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1324 if( ret != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 return( ret );
1328 }
1329 }
1330
1331 if( crt->version == 3 )
1332 {
Paul Bakker74111d32011-01-15 16:57:55 +00001333 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 if( ret != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339 }
1340
1341 if( p != end )
1342 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001343 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001344 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001345 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 }
1347
Paul Bakkerb00ca422012-09-25 12:10:00 +00001348 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
1350 /*
1351 * signatureAlgorithm AlgorithmIdentifier,
1352 * signatureValue BIT STRING
1353 */
1354 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1355 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001356 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 return( ret );
1358 }
1359
Paul Bakker535e97d2012-08-23 10:49:55 +00001360 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1361 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001364 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
1366
1367 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 return( ret );
1371 }
1372
1373 if( p != end )
1374 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001375 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001376 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001377 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 }
1379
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001380 return( 0 );
1381}
1382
1383/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001384 * Parse one X.509 certificate in DER format from a buffer and add them to a
1385 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001386 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001387int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001388{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001389 int ret;
1390 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001391
1392 /*
1393 * Check for valid input
1394 */
1395 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001396 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001397
1398 while( crt->version != 0 && crt->next != NULL )
1399 {
1400 prev = crt;
1401 crt = crt->next;
1402 }
1403
1404 /*
1405 * Add new certificate on the end of the chain if needed.
1406 */
1407 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001408 {
1409 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1410
Paul Bakker7d06ad22009-05-02 15:53:56 +00001411 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001412 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001413
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001414 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 crt = crt->next;
1416 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakkerd6d41092013-06-13 09:00:25 +02001419 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1420 {
1421 if( prev )
1422 prev->next = NULL;
1423
1424 if( crt != chain )
1425 free( crt );
1426
1427 return( ret );
1428 }
1429
1430 return( 0 );
1431}
1432
1433/*
1434 * Parse one or more PEM certificates from a buffer and add them to the chained list
1435 */
1436int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1437{
1438 int ret, success = 0, first_error = 0, total_failed = 0;
1439 int buf_format = X509_FORMAT_DER;
1440
1441 /*
1442 * Check for valid input
1443 */
1444 if( chain == NULL || buf == NULL )
1445 return( POLARSSL_ERR_X509_INVALID_INPUT );
1446
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001447 /*
1448 * Determine buffer content. Buffer contains either one DER certificate or
1449 * one or more PEM certificates.
1450 */
1451#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001452 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001453 buf_format = X509_FORMAT_PEM;
1454#endif
1455
1456 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001457 return x509parse_crt_der( chain, buf, buflen );
1458
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001459#if defined(POLARSSL_PEM_C)
1460 if( buf_format == X509_FORMAT_PEM )
1461 {
1462 pem_context pem;
1463
1464 while( buflen > 0 )
1465 {
1466 size_t use_len;
1467 pem_init( &pem );
1468
1469 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001470 (char *) "-----BEGIN CERTIFICATE-----",
1471 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001472 buf, NULL, 0, &use_len );
1473
1474 if( ret == 0 )
1475 {
1476 /*
1477 * Was PEM encoded
1478 */
1479 buflen -= use_len;
1480 buf += use_len;
1481 }
Paul Bakker64171862013-06-06 15:01:18 +02001482 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1483 {
1484 return( ret );
1485 }
Paul Bakker9255e832013-06-06 14:58:28 +02001486 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001487 {
1488 pem_free( &pem );
1489
Paul Bakker64171862013-06-06 15:01:18 +02001490 /*
1491 * PEM header and footer were found
1492 */
1493 buflen -= use_len;
1494 buf += use_len;
1495
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001496 if( first_error == 0 )
1497 first_error = ret;
1498
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001499 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 continue;
1501 }
1502 else
1503 break;
1504
Paul Bakkerd6d41092013-06-13 09:00:25 +02001505 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506
1507 pem_free( &pem );
1508
1509 if( ret != 0 )
1510 {
1511 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001512 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001513 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001514 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001515 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001516
1517 if( first_error == 0 )
1518 first_error = ret;
1519
Paul Bakkerd6d41092013-06-13 09:00:25 +02001520 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001521 continue;
1522 }
1523
1524 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001525 }
1526 }
1527#endif
1528
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001529 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001530 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001531 else if( first_error )
1532 return( first_error );
1533 else
1534 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535}
1536
1537/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001538 * Parse one or more CRLs and add them to the chained list
1539 */
Paul Bakker23986e52011-04-24 08:57:21 +00001540int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001541{
Paul Bakker23986e52011-04-24 08:57:21 +00001542 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001543 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544 unsigned char *p, *end;
1545 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001546#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001547 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001548 pem_context pem;
1549#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550
1551 crl = chain;
1552
1553 /*
1554 * Check for valid input
1555 */
1556 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001557 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558
1559 while( crl->version != 0 && crl->next != NULL )
1560 crl = crl->next;
1561
1562 /*
1563 * Add new CRL on the end of the chain if needed.
1564 */
1565 if ( crl->version != 0 && crl->next == NULL)
1566 {
1567 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1568
Paul Bakker7d06ad22009-05-02 15:53:56 +00001569 if( crl->next == NULL )
1570 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001571 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001572 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001573 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574
Paul Bakker7d06ad22009-05-02 15:53:56 +00001575 crl = crl->next;
1576 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577 }
1578
Paul Bakker96743fc2011-02-12 14:30:57 +00001579#if defined(POLARSSL_PEM_C)
1580 pem_init( &pem );
1581 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001582 (char *) "-----BEGIN X509 CRL-----",
1583 (char *) "-----END X509 CRL-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00001584 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585
Paul Bakker96743fc2011-02-12 14:30:57 +00001586 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001588 /*
1589 * Was PEM encoded
1590 */
1591 buflen -= use_len;
1592 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001593
1594 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001595 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001596 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001597 p = pem.buf;
1598 pem.buf = NULL;
1599 len = pem.buflen;
1600 pem_free( &pem );
1601 }
Paul Bakker9255e832013-06-06 14:58:28 +02001602 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001603 {
1604 pem_free( &pem );
1605 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001606 }
1607 else
1608 {
1609 /*
1610 * nope, copy the raw DER data
1611 */
1612 p = (unsigned char *) malloc( len = buflen );
1613
1614 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001616
1617 memcpy( p, buf, buflen );
1618
1619 buflen = 0;
1620 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001621#else
1622 p = (unsigned char *) malloc( len = buflen );
1623
1624 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001625 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001626
1627 memcpy( p, buf, buflen );
1628
1629 buflen = 0;
1630#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631
1632 crl->raw.p = p;
1633 crl->raw.len = len;
1634 end = p + len;
1635
1636 /*
1637 * CertificateList ::= SEQUENCE {
1638 * tbsCertList TBSCertList,
1639 * signatureAlgorithm AlgorithmIdentifier,
1640 * signatureValue BIT STRING }
1641 */
1642 if( ( ret = asn1_get_tag( &p, end, &len,
1643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1644 {
1645 x509_crl_free( crl );
1646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1647 }
1648
Paul Bakker23986e52011-04-24 08:57:21 +00001649 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 {
1651 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001652 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1654 }
1655
1656 /*
1657 * TBSCertList ::= SEQUENCE {
1658 */
1659 crl->tbs.p = p;
1660
1661 if( ( ret = asn1_get_tag( &p, end, &len,
1662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1663 {
1664 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001665 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666 }
1667
1668 end = p + len;
1669 crl->tbs.len = end - crl->tbs.p;
1670
1671 /*
1672 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1673 * -- if present, MUST be v2
1674 *
1675 * signature AlgorithmIdentifier
1676 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001677 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1679 {
1680 x509_crl_free( crl );
1681 return( ret );
1682 }
1683
1684 crl->version++;
1685
1686 if( crl->version > 2 )
1687 {
1688 x509_crl_free( crl );
1689 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1690 }
1691
Paul Bakker27d66162010-03-17 06:56:01 +00001692 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693 {
1694 x509_crl_free( crl );
1695 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1696 }
1697
1698 /*
1699 * issuer Name
1700 */
1701 crl->issuer_raw.p = p;
1702
1703 if( ( ret = asn1_get_tag( &p, end, &len,
1704 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1705 {
1706 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001707 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708 }
1709
1710 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1711 {
1712 x509_crl_free( crl );
1713 return( ret );
1714 }
1715
1716 crl->issuer_raw.len = p - crl->issuer_raw.p;
1717
1718 /*
1719 * thisUpdate Time
1720 * nextUpdate Time OPTIONAL
1721 */
Paul Bakker91200182010-02-18 21:26:15 +00001722 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001723 {
1724 x509_crl_free( crl );
1725 return( ret );
1726 }
1727
Paul Bakker91200182010-02-18 21:26:15 +00001728 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001729 {
Paul Bakker9d781402011-05-09 16:17:09 +00001730 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001731 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001732 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001733 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001734 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 x509_crl_free( crl );
1736 return( ret );
1737 }
1738 }
1739
1740 /*
1741 * revokedCertificates SEQUENCE OF SEQUENCE {
1742 * userCertificate CertificateSerialNumber,
1743 * revocationDate Time,
1744 * crlEntryExtensions Extensions OPTIONAL
1745 * -- if present, MUST be v2
1746 * } OPTIONAL
1747 */
1748 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1749 {
1750 x509_crl_free( crl );
1751 return( ret );
1752 }
1753
1754 /*
1755 * crlExtensions EXPLICIT Extensions OPTIONAL
1756 * -- if present, MUST be v2
1757 */
1758 if( crl->version == 2 )
1759 {
1760 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1761
1762 if( ret != 0 )
1763 {
1764 x509_crl_free( crl );
1765 return( ret );
1766 }
1767 }
1768
1769 if( p != end )
1770 {
1771 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001772 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001773 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1774 }
1775
1776 end = crl->raw.p + crl->raw.len;
1777
1778 /*
1779 * signatureAlgorithm AlgorithmIdentifier,
1780 * signatureValue BIT STRING
1781 */
1782 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1783 {
1784 x509_crl_free( crl );
1785 return( ret );
1786 }
1787
Paul Bakker535e97d2012-08-23 10:49:55 +00001788 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1789 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790 {
1791 x509_crl_free( crl );
1792 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1793 }
1794
1795 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1796 {
1797 x509_crl_free( crl );
1798 return( ret );
1799 }
1800
1801 if( p != end )
1802 {
1803 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001804 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001805 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1806 }
1807
1808 if( buflen > 0 )
1809 {
1810 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1811
Paul Bakker7d06ad22009-05-02 15:53:56 +00001812 if( crl->next == NULL )
1813 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001814 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001815 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001816 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817
Paul Bakker7d06ad22009-05-02 15:53:56 +00001818 crl = crl->next;
1819 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820
1821 return( x509parse_crl( crl, buf, buflen ) );
1822 }
1823
1824 return( 0 );
1825}
1826
Paul Bakker335db3f2011-04-25 15:28:35 +00001827#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829 * Load all data from a file into a given buffer.
1830 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001831static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001832{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001833 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001834 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001835
Paul Bakkerd98030e2009-05-02 15:13:40 +00001836 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001837 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001840 if( ( size = ftell( f ) ) == -1 )
1841 {
1842 fclose( f );
1843 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1844 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001845 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001846
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001847 *n = (size_t) size;
1848
1849 if( *n + 1 == 0 ||
1850 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1851 {
1852 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001853 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001854 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001855
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 if( fread( *buf, 1, *n, f ) != *n )
1857 {
1858 fclose( f );
1859 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001860 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001862
Paul Bakkerd98030e2009-05-02 15:13:40 +00001863 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001864
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001866
Paul Bakkerd98030e2009-05-02 15:13:40 +00001867 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001868}
1869
1870/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 * Load one or more certificates and add them to the chained list
1872 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001873int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001874{
1875 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 size_t n;
1877 unsigned char *buf;
1878
Paul Bakker69e095c2011-12-10 21:55:01 +00001879 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1880 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Paul Bakker69e095c2011-12-10 21:55:01 +00001882 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
1884 memset( buf, 0, n + 1 );
1885 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 return( ret );
1888}
1889
Paul Bakker8d914582012-06-04 12:46:42 +00001890int x509parse_crtpath( x509_cert *chain, const char *path )
1891{
1892 int ret = 0;
1893#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001894 int w_ret;
1895 WCHAR szDir[MAX_PATH];
1896 char filename[MAX_PATH];
1897 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001898 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001899
Paul Bakker97872ac2012-11-02 12:53:26 +00001900 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001901 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001902
1903 if( len > MAX_PATH - 3 )
1904 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001905
Paul Bakker3338b792012-10-01 21:13:10 +00001906 memset( szDir, 0, sizeof(szDir) );
1907 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001908 memcpy( filename, path, len );
1909 filename[len++] = '\\';
1910 p = filename + len;
1911 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001912
Paul Bakker40cc9142014-07-07 15:16:47 +02001913 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001914
Paul Bakker97872ac2012-11-02 12:53:26 +00001915 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001916 if (hFind == INVALID_HANDLE_VALUE)
1917 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1918
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001919 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001920 do
1921 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001922 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001923
Paul Bakkere4791f32012-06-04 21:29:15 +00001924 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001925 continue;
1926
Paul Bakker3338b792012-10-01 21:13:10 +00001927 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1928 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001929 p, len - 1,
1930 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001931
Paul Bakker3338b792012-10-01 21:13:10 +00001932 w_ret = x509parse_crtfile( chain, filename );
1933 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001934 ret++;
1935 else
1936 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001937 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001938 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001939
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001940 if (GetLastError() != ERROR_NO_MORE_FILES)
1941 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001942
1943 FindClose( hFind );
1944#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001945#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001946 int t_ret, i;
1947 struct stat sb;
1948 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001949 char entry_name[255];
1950 DIR *dir = opendir( path );
1951
1952 if( dir == NULL)
1953 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1954
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001955 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001956 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001957 if( result == NULL )
1958 break;
1959
1960 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1961
1962 i = stat( entry_name, &sb );
1963
1964 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02001965 {
1966 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001967 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02001968 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001969
1970 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00001971 continue;
1972
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001973 // Ignore parse errors
1974 //
Paul Bakker8d914582012-06-04 12:46:42 +00001975 t_ret = x509parse_crtfile( chain, entry_name );
1976 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001977 ret++;
1978 else
1979 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001980 }
1981 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02001982#else /* POLARSSL_HAVE_READDIR_R */
1983 ((void) chain);
1984 ((void) path);
1985 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
1986#endif /* POLARSSL_HAVE_READDIR_R */
1987#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00001988
1989 return( ret );
1990}
1991
Paul Bakkerd98030e2009-05-02 15:13:40 +00001992/*
1993 * Load one or more CRLs and add them to the chained list
1994 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001995int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001996{
1997 int ret;
1998 size_t n;
1999 unsigned char *buf;
2000
Paul Bakker69e095c2011-12-10 21:55:01 +00002001 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2002 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002003
Paul Bakker27fdf462011-06-09 13:55:13 +00002004 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002005
2006 memset( buf, 0, n + 1 );
2007 free( buf );
2008
2009 return( ret );
2010}
2011
Paul Bakker5121ce52009-01-03 21:22:43 +00002012/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002013 * Load and parse a private RSA key
2014 */
2015int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2016{
2017 int ret;
2018 size_t n;
2019 unsigned char *buf;
2020
Paul Bakker69e095c2011-12-10 21:55:01 +00002021 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2022 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002023
2024 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002025 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002026 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002027 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002028 (unsigned char *) pwd, strlen( pwd ) );
2029
2030 memset( buf, 0, n + 1 );
2031 free( buf );
2032
2033 return( ret );
2034}
2035
2036/*
2037 * Load and parse a public RSA key
2038 */
2039int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2040{
2041 int ret;
2042 size_t n;
2043 unsigned char *buf;
2044
Paul Bakker69e095c2011-12-10 21:55:01 +00002045 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2046 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002047
Paul Bakker27fdf462011-06-09 13:55:13 +00002048 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002049
2050 memset( buf, 0, n + 1 );
2051 free( buf );
2052
2053 return( ret );
2054}
2055#endif /* POLARSSL_FS_IO */
2056
2057/*
Paul Bakker65a19092013-06-06 21:14:58 +02002058 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 */
Paul Bakker65a19092013-06-06 21:14:58 +02002060static int x509parse_key_pkcs1_der( rsa_context *rsa,
2061 const unsigned char *key,
2062 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002063{
Paul Bakker23986e52011-04-24 08:57:21 +00002064 int ret;
2065 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002067
Paul Bakker96743fc2011-02-12 14:30:57 +00002068 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002069 end = p + keylen;
2070
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002072 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002073 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 * RSAPrivateKey ::= SEQUENCE {
2075 * version Version,
2076 * modulus INTEGER, -- n
2077 * publicExponent INTEGER, -- e
2078 * privateExponent INTEGER, -- d
2079 * prime1 INTEGER, -- p
2080 * prime2 INTEGER, -- q
2081 * exponent1 INTEGER, -- d mod (p-1)
2082 * exponent2 INTEGER, -- d mod (q-1)
2083 * coefficient INTEGER, -- (inverse of q) mod p
2084 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2085 * }
2086 */
2087 if( ( ret = asn1_get_tag( &p, end, &len,
2088 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2089 {
Paul Bakker9d781402011-05-09 16:17:09 +00002090 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
2093 end = p + len;
2094
2095 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2096 {
Paul Bakker9d781402011-05-09 16:17:09 +00002097 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 }
2099
2100 if( rsa->ver != 0 )
2101 {
Paul Bakker9d781402011-05-09 16:17:09 +00002102 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
2104
2105 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2106 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2107 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2108 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2109 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2110 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2111 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2112 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2113 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002115 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 }
2117
2118 rsa->len = mpi_size( &rsa->N );
2119
2120 if( p != end )
2121 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002123 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002124 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 }
2126
2127 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2128 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 rsa_free( rsa );
2130 return( ret );
2131 }
2132
Paul Bakker65a19092013-06-06 21:14:58 +02002133 return( 0 );
2134}
2135
2136/*
2137 * Parse an unencrypted PKCS#8 encoded private RSA key
2138 */
2139static int x509parse_key_pkcs8_unencrypted_der(
2140 rsa_context *rsa,
2141 const unsigned char *key,
2142 size_t keylen )
2143{
2144 int ret;
2145 size_t len;
2146 unsigned char *p, *end;
2147 x509_buf pk_alg_oid;
2148
2149 p = (unsigned char *) key;
2150 end = p + keylen;
2151
2152 /*
2153 * This function parses the PrivatKeyInfo object (PKCS#8)
2154 *
2155 * PrivateKeyInfo ::= SEQUENCE {
2156 * version Version,
2157 * algorithm AlgorithmIdentifier,
2158 * PrivateKey BIT STRING
2159 * }
2160 *
2161 * AlgorithmIdentifier ::= SEQUENCE {
2162 * algorithm OBJECT IDENTIFIER,
2163 * parameters ANY DEFINED BY algorithm OPTIONAL
2164 * }
2165 *
2166 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2167 */
2168 if( ( ret = asn1_get_tag( &p, end, &len,
2169 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2170 {
2171 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2172 }
2173
2174 end = p + len;
2175
2176 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2177 {
2178 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2179 }
2180
2181 if( rsa->ver != 0 )
2182 {
2183 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2184 }
2185
2186 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2187 {
2188 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2189 }
2190
2191 /*
2192 * only RSA keys handled at this time
2193 */
2194 if( pk_alg_oid.len != 9 ||
2195 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2196 {
2197 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2198 }
2199
2200 /*
2201 * Get the OCTET STRING and parse the PKCS#1 format inside
2202 */
2203 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2204 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2205
2206 if( ( end - p ) < 1 )
2207 {
2208 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2209 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2210 }
2211
2212 end = p + len;
2213
2214 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2215 return( ret );
2216
2217 return( 0 );
2218}
2219
2220/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002221 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002222 */
2223static int x509parse_key_pkcs8_encrypted_der(
2224 rsa_context *rsa,
2225 const unsigned char *key,
2226 size_t keylen,
2227 const unsigned char *pwd,
2228 size_t pwdlen )
2229{
2230 int ret;
2231 size_t len;
2232 unsigned char *p, *end, *end2;
2233 x509_buf pbe_alg_oid, pbe_params;
2234 unsigned char buf[2048];
2235
2236 memset(buf, 0, 2048);
2237
2238 p = (unsigned char *) key;
2239 end = p + keylen;
2240
Paul Bakker1fd43212013-06-17 15:14:42 +02002241 if( pwdlen == 0 )
2242 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2243
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002244 /*
2245 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2246 *
2247 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2248 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2249 * encryptedData EncryptedData
2250 * }
2251 *
2252 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2253 *
2254 * EncryptedData ::= OCTET STRING
2255 *
2256 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2257 */
2258 if( ( ret = asn1_get_tag( &p, end, &len,
2259 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2260 {
2261 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2262 }
2263
2264 end = p + len;
2265
2266 if( ( ret = asn1_get_tag( &p, end, &len,
2267 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2268 {
2269 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2270 }
2271
2272 end2 = p + len;
2273
2274 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2275 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2276
2277 pbe_alg_oid.p = p;
2278 p += pbe_alg_oid.len;
2279
2280 /*
2281 * Store the algorithm parameters
2282 */
2283 pbe_params.p = p;
2284 pbe_params.len = end2 - p;
2285 p += pbe_params.len;
2286
2287 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2288 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2289
2290 // buf has been sized to 2048 bytes
2291 if( len > 2048 )
2292 return( POLARSSL_ERR_X509_INVALID_INPUT );
2293
2294 /*
2295 * Decrypt EncryptedData with appropriate PDE
2296 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002297#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002298 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2299 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002300 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2301 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2302 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002303 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002304 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2305 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2306
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002307 return( ret );
2308 }
2309 }
2310 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2311 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002312 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2313 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2314 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002315 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002316 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2317 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2318
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002319 return( ret );
2320 }
2321 }
2322 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2323 {
2324 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2325 PKCS12_PBE_DECRYPT,
2326 pwd, pwdlen,
2327 p, len, buf ) ) != 0 )
2328 {
2329 return( ret );
2330 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002331
2332 // Best guess for password mismatch when using RC4. If first tag is
2333 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2334 //
2335 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2336 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002337 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002338 else
2339#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002340#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002341 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002342 {
2343 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2344 p, len, buf ) ) != 0 )
2345 {
2346 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2347 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2348
2349 return( ret );
2350 }
2351 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002352 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002353#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002354 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2355
2356 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2357}
2358
2359/*
Paul Bakker65a19092013-06-06 21:14:58 +02002360 * Parse a private RSA key
2361 */
2362int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2363 const unsigned char *pwd, size_t pwdlen )
2364{
2365 int ret;
2366
Paul Bakker96743fc2011-02-12 14:30:57 +00002367#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002368 size_t len;
2369 pem_context pem;
2370
2371 pem_init( &pem );
2372 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002373 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2374 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002375 key, pwd, pwdlen, &len );
2376 if( ret == 0 )
2377 {
2378 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2379 {
2380 rsa_free( rsa );
2381 }
2382
2383 pem_free( &pem );
2384 return( ret );
2385 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002386 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2387 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2388 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2389 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002390 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002391 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002392
2393 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002394 (char *) "-----BEGIN PRIVATE KEY-----",
2395 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002396 key, NULL, 0, &len );
2397 if( ret == 0 )
2398 {
2399 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2400 pem.buf, pem.buflen ) ) != 0 )
2401 {
2402 rsa_free( rsa );
2403 }
2404
2405 pem_free( &pem );
2406 return( ret );
2407 }
2408 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002409 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002410
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002411 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002412 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2413 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002414 key, NULL, 0, &len );
2415 if( ret == 0 )
2416 {
2417 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2418 pem.buf, pem.buflen,
2419 pwd, pwdlen ) ) != 0 )
2420 {
2421 rsa_free( rsa );
2422 }
2423
2424 pem_free( &pem );
2425 return( ret );
2426 }
2427 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002428 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002429#else
2430 ((void) pwd);
2431 ((void) pwdlen);
2432#endif /* POLARSSL_PEM_C */
2433
2434 // At this point we only know it's not a PEM formatted key. Could be any
2435 // of the known DER encoded private key formats
2436 //
2437 // We try the different DER format parsers to see if one passes without
2438 // error
2439 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002440 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2441 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002442 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002443 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002446 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002447
2448 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2449 {
2450 return( ret );
2451 }
2452
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002453 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2454 return( 0 );
2455
2456 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002457
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002458 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2459 return( 0 );
2460
2461 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002462
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002463 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464}
2465
2466/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002467 * Parse a public RSA key
2468 */
Paul Bakker23986e52011-04-24 08:57:21 +00002469int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002470{
Paul Bakker23986e52011-04-24 08:57:21 +00002471 int ret;
2472 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002473 unsigned char *p, *end;
2474 x509_buf alg_oid;
2475#if defined(POLARSSL_PEM_C)
2476 pem_context pem;
2477
2478 pem_init( &pem );
2479 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002480 (char *) "-----BEGIN PUBLIC KEY-----",
2481 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002482 key, NULL, 0, &len );
2483
2484 if( ret == 0 )
2485 {
2486 /*
2487 * Was PEM encoded
2488 */
2489 keylen = pem.buflen;
2490 }
Paul Bakker9255e832013-06-06 14:58:28 +02002491 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002492 {
2493 pem_free( &pem );
2494 return( ret );
2495 }
2496
2497 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2498#else
2499 p = (unsigned char *) key;
2500#endif
2501 end = p + keylen;
2502
2503 /*
2504 * PublicKeyInfo ::= SEQUENCE {
2505 * algorithm AlgorithmIdentifier,
2506 * PublicKey BIT STRING
2507 * }
2508 *
2509 * AlgorithmIdentifier ::= SEQUENCE {
2510 * algorithm OBJECT IDENTIFIER,
2511 * parameters ANY DEFINED BY algorithm OPTIONAL
2512 * }
2513 *
2514 * RSAPublicKey ::= SEQUENCE {
2515 * modulus INTEGER, -- n
2516 * publicExponent INTEGER -- e
2517 * }
2518 */
2519
2520 if( ( ret = asn1_get_tag( &p, end, &len,
2521 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2522 {
2523#if defined(POLARSSL_PEM_C)
2524 pem_free( &pem );
2525#endif
2526 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002527 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002528 }
2529
2530 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2531 {
2532#if defined(POLARSSL_PEM_C)
2533 pem_free( &pem );
2534#endif
2535 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002536 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002537 }
2538
2539 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2540 {
2541#if defined(POLARSSL_PEM_C)
2542 pem_free( &pem );
2543#endif
2544 rsa_free( rsa );
2545 return( ret );
2546 }
2547
2548 rsa->len = mpi_size( &rsa->N );
2549
2550#if defined(POLARSSL_PEM_C)
2551 pem_free( &pem );
2552#endif
2553
2554 return( 0 );
2555}
2556
Paul Bakkereaa89f82011-04-04 21:36:15 +00002557#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002558/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002559 * Parse DHM parameters
2560 */
Paul Bakker23986e52011-04-24 08:57:21 +00002561int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002562{
Paul Bakker23986e52011-04-24 08:57:21 +00002563 int ret;
2564 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002565 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002566#if defined(POLARSSL_PEM_C)
2567 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002568
Paul Bakker96743fc2011-02-12 14:30:57 +00002569 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002570
Paul Bakker96743fc2011-02-12 14:30:57 +00002571 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002572 (char *) "-----BEGIN DH PARAMETERS-----",
2573 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002574 dhmin, NULL, 0, &dhminlen );
2575
2576 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002577 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002578 /*
2579 * Was PEM encoded
2580 */
2581 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002582 }
Paul Bakker9255e832013-06-06 14:58:28 +02002583 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002584 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002585 pem_free( &pem );
2586 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002587 }
2588
Paul Bakker96743fc2011-02-12 14:30:57 +00002589 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2590#else
2591 p = (unsigned char *) dhmin;
2592#endif
2593 end = p + dhminlen;
2594
Paul Bakker1b57b062011-01-06 15:48:19 +00002595 memset( dhm, 0, sizeof( dhm_context ) );
2596
Paul Bakker1b57b062011-01-06 15:48:19 +00002597 /*
2598 * DHParams ::= SEQUENCE {
2599 * prime INTEGER, -- P
2600 * generator INTEGER, -- g
2601 * }
2602 */
2603 if( ( ret = asn1_get_tag( &p, end, &len,
2604 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2605 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002606#if defined(POLARSSL_PEM_C)
2607 pem_free( &pem );
2608#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002609 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002610 }
2611
2612 end = p + len;
2613
2614 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2615 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2616 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002617#if defined(POLARSSL_PEM_C)
2618 pem_free( &pem );
2619#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002620 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002621 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002622 }
2623
2624 if( p != end )
2625 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002626#if defined(POLARSSL_PEM_C)
2627 pem_free( &pem );
2628#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002629 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002630 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002631 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2632 }
2633
Paul Bakker96743fc2011-02-12 14:30:57 +00002634#if defined(POLARSSL_PEM_C)
2635 pem_free( &pem );
2636#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002637
2638 return( 0 );
2639}
2640
Paul Bakker335db3f2011-04-25 15:28:35 +00002641#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002642/*
2643 * Load and parse a private RSA key
2644 */
2645int x509parse_dhmfile( dhm_context *dhm, const char *path )
2646{
2647 int ret;
2648 size_t n;
2649 unsigned char *buf;
2650
Paul Bakker69e095c2011-12-10 21:55:01 +00002651 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2652 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002653
Paul Bakker27fdf462011-06-09 13:55:13 +00002654 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002655
2656 memset( buf, 0, n + 1 );
2657 free( buf );
2658
2659 return( ret );
2660}
Paul Bakker335db3f2011-04-25 15:28:35 +00002661#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002662#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002663
Paul Bakker5121ce52009-01-03 21:22:43 +00002664#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002665#include <stdarg.h>
2666
2667#if !defined vsnprintf
2668#define vsnprintf _vsnprintf
2669#endif // vsnprintf
2670
2671/*
2672 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2673 * Result value is not size of buffer needed, but -1 if no fit is possible.
2674 *
2675 * This fuction tries to 'fix' this by at least suggesting enlarging the
2676 * size by 20.
2677 */
2678int compat_snprintf(char *str, size_t size, const char *format, ...)
2679{
2680 va_list ap;
2681 int res = -1;
2682
2683 va_start( ap, format );
2684
2685 res = vsnprintf( str, size, format, ap );
2686
2687 va_end( ap );
2688
2689 // No quick fix possible
2690 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002691 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002692
2693 return res;
2694}
2695
2696#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002697#endif
2698
Paul Bakkerd98030e2009-05-02 15:13:40 +00002699#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2700
2701#define SAFE_SNPRINTF() \
2702{ \
2703 if( ret == -1 ) \
2704 return( -1 ); \
2705 \
Paul Bakker23986e52011-04-24 08:57:21 +00002706 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002707 p[n - 1] = '\0'; \
2708 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2709 } \
2710 \
Paul Bakker23986e52011-04-24 08:57:21 +00002711 n -= (unsigned int) ret; \
2712 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002713}
2714
Paul Bakker5121ce52009-01-03 21:22:43 +00002715/*
2716 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002717 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002719int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720{
Paul Bakker23986e52011-04-24 08:57:21 +00002721 int ret;
2722 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002724 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 char s[128], *p;
2726
2727 memset( s, 0, sizeof( s ) );
2728
2729 name = dn;
2730 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002731 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
2733 while( name != NULL )
2734 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002735 if( !name->oid.p )
2736 {
2737 name = name->next;
2738 continue;
2739 }
2740
Paul Bakker74111d32011-01-15 16:57:55 +00002741 if( name != dn )
2742 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002743 ret = snprintf( p, n, ", " );
2744 SAFE_SNPRINTF();
2745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Paul Bakker535e97d2012-08-23 10:49:55 +00002747 if( name->oid.len == 3 &&
2748 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 {
2750 switch( name->oid.p[2] )
2751 {
2752 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002753 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002754
2755 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002756 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
2758 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002759 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002762 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
2764 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002765 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766
2767 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002768 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
2770 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002771 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 name->oid.p[2] );
2773 break;
2774 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002775 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002777 else if( name->oid.len == 9 &&
2778 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 {
2780 switch( name->oid.p[8] )
2781 {
2782 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002783 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
2785 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002786 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 name->oid.p[8] );
2788 break;
2789 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 }
2792 else
Paul Bakker74111d32011-01-15 16:57:55 +00002793 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002795 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
2798 for( i = 0; i < name->val.len; i++ )
2799 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002800 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 break;
2802
2803 c = name->val.p[i];
2804 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2805 s[i] = '?';
2806 else s[i] = c;
2807 }
2808 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 ret = snprintf( p, n, "%s", s );
2810 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 name = name->next;
2812 }
2813
Paul Bakker23986e52011-04-24 08:57:21 +00002814 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815}
2816
2817/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002818 * Store the serial in printable form into buf; no more
2819 * than size characters will be written
2820 */
2821int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2822{
Paul Bakker23986e52011-04-24 08:57:21 +00002823 int ret;
2824 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002825 char *p;
2826
2827 p = buf;
2828 n = size;
2829
2830 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002831 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002832
2833 for( i = 0; i < nr; i++ )
2834 {
Paul Bakker93048802011-12-05 14:38:06 +00002835 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002836 continue;
2837
Paul Bakkerdd476992011-01-16 21:34:59 +00002838 ret = snprintf( p, n, "%02X%s",
2839 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2840 SAFE_SNPRINTF();
2841 }
2842
Paul Bakker03c7c252011-11-25 12:37:37 +00002843 if( nr != serial->len )
2844 {
2845 ret = snprintf( p, n, "...." );
2846 SAFE_SNPRINTF();
2847 }
2848
Paul Bakker23986e52011-04-24 08:57:21 +00002849 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002850}
2851
2852/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002853 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002855int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2856 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857{
Paul Bakker23986e52011-04-24 08:57:21 +00002858 int ret;
2859 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002860 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
2862 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002863 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Paul Bakkerd98030e2009-05-02 15:13:40 +00002865 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002867 SAFE_SNPRINTF();
2868 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002870 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Paul Bakkerdd476992011-01-16 21:34:59 +00002872 ret = x509parse_serial_gets( p, n, &crt->serial);
2873 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Paul Bakkerd98030e2009-05-02 15:13:40 +00002875 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2876 SAFE_SNPRINTF();
2877 ret = x509parse_dn_gets( p, n, &crt->issuer );
2878 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002879
Paul Bakkerd98030e2009-05-02 15:13:40 +00002880 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2881 SAFE_SNPRINTF();
2882 ret = x509parse_dn_gets( p, n, &crt->subject );
2883 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakkerd98030e2009-05-02 15:13:40 +00002885 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2887 crt->valid_from.year, crt->valid_from.mon,
2888 crt->valid_from.day, crt->valid_from.hour,
2889 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002890 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakkerd98030e2009-05-02 15:13:40 +00002892 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2894 crt->valid_to.year, crt->valid_to.mon,
2895 crt->valid_to.day, crt->valid_to.hour,
2896 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002897 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Paul Bakkerd98030e2009-05-02 15:13:40 +00002899 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2900 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakker27d66162010-03-17 06:56:01 +00002902 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2905 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2906 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2907 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2908 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2909 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2910 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2911 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2912 default: ret = snprintf( p, n, "???" ); break;
2913 }
2914 SAFE_SNPRINTF();
2915
2916 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002917 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002918 SAFE_SNPRINTF();
2919
Paul Bakker23986e52011-04-24 08:57:21 +00002920 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002921}
2922
Paul Bakker74111d32011-01-15 16:57:55 +00002923/*
2924 * Return an informational string describing the given OID
2925 */
2926const char *x509_oid_get_description( x509_buf *oid )
2927{
2928 if ( oid == NULL )
2929 return ( NULL );
2930
2931 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2932 return( STRING_SERVER_AUTH );
2933
2934 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2935 return( STRING_CLIENT_AUTH );
2936
2937 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2938 return( STRING_CODE_SIGNING );
2939
2940 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2941 return( STRING_EMAIL_PROTECTION );
2942
2943 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2944 return( STRING_TIME_STAMPING );
2945
2946 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2947 return( STRING_OCSP_SIGNING );
2948
2949 return( NULL );
2950}
2951
2952/* Return the x.y.z.... style numeric string for the given OID */
2953int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2954{
Paul Bakker23986e52011-04-24 08:57:21 +00002955 int ret;
2956 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002957 unsigned int value;
2958 char *p;
2959
2960 p = buf;
2961 n = size;
2962
2963 /* First byte contains first two dots */
2964 if( oid->len > 0 )
2965 {
2966 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2967 SAFE_SNPRINTF();
2968 }
2969
2970 /* TODO: value can overflow in value. */
2971 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002972 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002973 {
2974 value <<= 7;
2975 value += oid->p[i] & 0x7F;
2976
2977 if( !( oid->p[i] & 0x80 ) )
2978 {
2979 /* Last byte */
2980 ret = snprintf( p, n, ".%d", value );
2981 SAFE_SNPRINTF();
2982 value = 0;
2983 }
2984 }
2985
Paul Bakker23986e52011-04-24 08:57:21 +00002986 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002987}
2988
Paul Bakkerd98030e2009-05-02 15:13:40 +00002989/*
2990 * Return an informational string about the CRL.
2991 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002992int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2993 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002994{
Paul Bakker23986e52011-04-24 08:57:21 +00002995 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002996 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002997 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002998 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002999
3000 p = buf;
3001 n = size;
3002
3003 ret = snprintf( p, n, "%sCRL version : %d",
3004 prefix, crl->version );
3005 SAFE_SNPRINTF();
3006
3007 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3008 SAFE_SNPRINTF();
3009 ret = x509parse_dn_gets( p, n, &crl->issuer );
3010 SAFE_SNPRINTF();
3011
3012 ret = snprintf( p, n, "\n%sthis update : " \
3013 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3014 crl->this_update.year, crl->this_update.mon,
3015 crl->this_update.day, crl->this_update.hour,
3016 crl->this_update.min, crl->this_update.sec );
3017 SAFE_SNPRINTF();
3018
3019 ret = snprintf( p, n, "\n%snext update : " \
3020 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3021 crl->next_update.year, crl->next_update.mon,
3022 crl->next_update.day, crl->next_update.hour,
3023 crl->next_update.min, crl->next_update.sec );
3024 SAFE_SNPRINTF();
3025
3026 entry = &crl->entry;
3027
3028 ret = snprintf( p, n, "\n%sRevoked certificates:",
3029 prefix );
3030 SAFE_SNPRINTF();
3031
Paul Bakker9be19372009-07-27 20:21:53 +00003032 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003033 {
3034 ret = snprintf( p, n, "\n%sserial number: ",
3035 prefix );
3036 SAFE_SNPRINTF();
3037
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003038 ret = x509parse_serial_gets( p, n, &entry->serial);
3039 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041 ret = snprintf( p, n, " revocation date: " \
3042 "%04d-%02d-%02d %02d:%02d:%02d",
3043 entry->revocation_date.year, entry->revocation_date.mon,
3044 entry->revocation_date.day, entry->revocation_date.hour,
3045 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003046 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003047
3048 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 }
3050
Paul Bakkerd98030e2009-05-02 15:13:40 +00003051 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3052 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Paul Bakker27d66162010-03-17 06:56:01 +00003054 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003055 {
3056 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3057 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3058 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3059 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3060 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3061 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3062 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3063 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3064 default: ret = snprintf( p, n, "???" ); break;
3065 }
3066 SAFE_SNPRINTF();
3067
Paul Bakker1e27bb22009-07-19 20:25:25 +00003068 ret = snprintf( p, n, "\n" );
3069 SAFE_SNPRINTF();
3070
Paul Bakker23986e52011-04-24 08:57:21 +00003071 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072}
3073
3074/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003075 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003077static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003078{
Paul Bakkercce9d772011-11-18 14:26:47 +00003079#if defined(_WIN32)
3080 SYSTEMTIME st;
3081
Paul Bakkerf48de952014-07-08 14:39:41 +02003082 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003083
Paul Bakker0d844dd2014-07-07 17:44:14 +02003084 now->year = st.wYear;
3085 now->mon = st.wMonth;
3086 now->day = st.wDay;
3087 now->hour = st.wHour;
3088 now->min = st.wMinute;
3089 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003090#else
Paul Bakker358a8412014-07-08 12:14:37 +02003091 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 time_t tt;
3093
3094 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003095 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakker358a8412014-07-08 12:14:37 +02003097 now->year = lt.tm_year + 1900;
3098 now->mon = lt.tm_mon + 1;
3099 now->day = lt.tm_mday;
3100 now->hour = lt.tm_hour;
3101 now->min = lt.tm_min;
3102 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003103#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003104}
Paul Bakkercce9d772011-11-18 14:26:47 +00003105
Paul Bakker0d844dd2014-07-07 17:44:14 +02003106/*
3107 * Return 0 if before <= after, 1 otherwise
3108 */
3109static int x509_check_time( const x509_time *before, const x509_time *after )
3110{
3111 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003112 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker0d844dd2014-07-07 17:44:14 +02003114 if( before->year == after->year &&
3115 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003116 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003117
Paul Bakker0d844dd2014-07-07 17:44:14 +02003118 if( before->year == after->year &&
3119 before->mon == after->mon &&
3120 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003121 return( 1 );
3122
Paul Bakker0d844dd2014-07-07 17:44:14 +02003123 if( before->year == after->year &&
3124 before->mon == after->mon &&
3125 before->day == after->day &&
3126 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003127 return( 1 );
3128
Paul Bakker0d844dd2014-07-07 17:44:14 +02003129 if( before->year == after->year &&
3130 before->mon == after->mon &&
3131 before->day == after->day &&
3132 before->hour == after->hour &&
3133 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003134 return( 1 );
3135
Paul Bakker0d844dd2014-07-07 17:44:14 +02003136 if( before->year == after->year &&
3137 before->mon == after->mon &&
3138 before->day == after->day &&
3139 before->hour == after->hour &&
3140 before->min == after->min &&
3141 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003142 return( 1 );
3143
Paul Bakker40ea7de2009-05-03 10:18:48 +00003144 return( 0 );
3145}
3146
Paul Bakker0d844dd2014-07-07 17:44:14 +02003147int x509parse_time_expired( const x509_time *to )
3148{
3149 x509_time now;
3150
3151 x509_get_current_time( &now );
3152
3153 return( x509_check_time( &now, to ) );
3154}
3155
3156int x509parse_time_future( const x509_time *from )
3157{
3158 x509_time now;
3159
3160 x509_get_current_time( &now );
3161
3162 return( x509_check_time( from, &now ) );
3163}
3164
Paul Bakker40ea7de2009-05-03 10:18:48 +00003165/*
3166 * Return 1 if the certificate is revoked, or 0 otherwise.
3167 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003168int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003169{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003170 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003171
3172 while( cur != NULL && cur->serial.len != 0 )
3173 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003174 if( crt->serial.len == cur->serial.len &&
3175 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003176 {
3177 if( x509parse_time_expired( &cur->revocation_date ) )
3178 return( 1 );
3179 }
3180
3181 cur = cur->next;
3182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
3184 return( 0 );
3185}
3186
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003187/*
3188 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003189 */
Paul Bakker23986e52011-04-24 08:57:21 +00003190static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 unsigned char *out )
3192{
3193 switch( alg )
3194 {
Paul Bakker40e46942009-01-03 21:51:57 +00003195#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003196 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003197#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003198#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003199 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003200#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003201#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003202 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003203#endif
3204#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003205 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003206#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003207#if defined(POLARSSL_SHA2_C)
3208 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3209 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3210#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003211#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003212 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3213 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3214#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003216 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 break;
3218 }
3219}
3220
3221/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003222 * Check that the given certificate is valid accoring to the CRL.
3223 */
3224static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3225 x509_crl *crl_list)
3226{
3227 int flags = 0;
3228 int hash_id;
3229 unsigned char hash[64];
3230
Paul Bakker915275b2012-09-28 07:10:55 +00003231 if( ca == NULL )
3232 return( flags );
3233
Paul Bakker76fd75a2011-01-16 21:12:10 +00003234 /*
3235 * TODO: What happens if no CRL is present?
3236 * Suggestion: Revocation state should be unknown if no CRL is present.
3237 * For backwards compatibility this is not yet implemented.
3238 */
3239
Paul Bakker915275b2012-09-28 07:10:55 +00003240 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003241 {
Paul Bakker915275b2012-09-28 07:10:55 +00003242 if( crl_list->version == 0 ||
3243 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003244 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3245 crl_list->issuer_raw.len ) != 0 )
3246 {
3247 crl_list = crl_list->next;
3248 continue;
3249 }
3250
3251 /*
3252 * Check if CRL is correctly signed by the trusted CA
3253 */
3254 hash_id = crl_list->sig_alg;
3255
3256 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3257
Paul Bakker43f97992013-09-23 11:23:31 +02003258 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003259 0, hash, crl_list->sig.p ) == 0 )
3260 {
3261 /*
3262 * CRL is not trusted
3263 */
3264 flags |= BADCRL_NOT_TRUSTED;
3265 break;
3266 }
3267
3268 /*
3269 * Check for validity of CRL (Do not drop out)
3270 */
3271 if( x509parse_time_expired( &crl_list->next_update ) )
3272 flags |= BADCRL_EXPIRED;
3273
Paul Bakker50a5c532014-07-08 10:59:10 +02003274 if( x509parse_time_future( &crl_list->this_update ) )
3275 flags |= BADCRL_FUTURE;
3276
Paul Bakker76fd75a2011-01-16 21:12:10 +00003277 /*
3278 * Check if certificate is revoked
3279 */
3280 if( x509parse_revoked(crt, crl_list) )
3281 {
3282 flags |= BADCERT_REVOKED;
3283 break;
3284 }
3285
3286 crl_list = crl_list->next;
3287 }
3288 return flags;
3289}
3290
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003291// Equal == 0, inequal == 1
3292static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3293{
3294 size_t i;
3295 unsigned char diff;
3296 const unsigned char *n1 = s1, *n2 = s2;
3297
3298 for( i = 0; i < len; i++ )
3299 {
3300 diff = n1[i] ^ n2[i];
3301
Paul Bakkerc941adb2014-07-07 14:17:24 +02003302 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003303 continue;
3304
Paul Bakkerc941adb2014-07-07 14:17:24 +02003305 if( diff == 32 &&
3306 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3307 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3308 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003309 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003310 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003311
3312 return( 1 );
3313 }
3314
3315 return( 0 );
3316}
3317
Paul Bakker1d073c52014-07-08 20:15:51 +02003318static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003319{
3320 size_t i;
3321 size_t cn_idx = 0;
3322
Paul Bakker57b12982012-02-11 17:38:38 +00003323 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003324 return( 0 );
3325
3326 for( i = 0; i < strlen( cn ); ++i )
3327 {
3328 if( cn[i] == '.' )
3329 {
3330 cn_idx = i;
3331 break;
3332 }
3333 }
3334
3335 if( cn_idx == 0 )
3336 return( 0 );
3337
Paul Bakker535e97d2012-08-23 10:49:55 +00003338 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003339 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003340 {
3341 return( 1 );
3342 }
3343
3344 return( 0 );
3345}
3346
Paul Bakker915275b2012-09-28 07:10:55 +00003347static int x509parse_verify_top(
3348 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003349 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003350 int (*f_vrfy)(void *, x509_cert *, int, int *),
3351 void *p_vrfy )
3352{
3353 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003354 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003355 unsigned char hash[64];
3356
3357 if( x509parse_time_expired( &child->valid_to ) )
3358 *flags |= BADCERT_EXPIRED;
3359
Paul Bakker50a5c532014-07-08 10:59:10 +02003360 if( x509parse_time_future( &child->valid_from ) )
3361 *flags |= BADCERT_FUTURE;
3362
Paul Bakker915275b2012-09-28 07:10:55 +00003363 /*
3364 * Child is the top of the chain. Check against the trust_ca list.
3365 */
3366 *flags |= BADCERT_NOT_TRUSTED;
3367
3368 while( trust_ca != NULL )
3369 {
3370 if( trust_ca->version == 0 ||
3371 child->issuer_raw.len != trust_ca->subject_raw.len ||
3372 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3373 child->issuer_raw.len ) != 0 )
3374 {
3375 trust_ca = trust_ca->next;
3376 continue;
3377 }
3378
Paul Bakker9a736322012-11-14 12:39:52 +00003379 /*
3380 * Reduce path_len to check against if top of the chain is
3381 * the same as the trusted CA
3382 */
3383 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3384 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3385 child->issuer_raw.len ) == 0 )
3386 {
3387 check_path_cnt--;
3388 }
3389
Paul Bakker915275b2012-09-28 07:10:55 +00003390 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003391 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003392 {
3393 trust_ca = trust_ca->next;
3394 continue;
3395 }
3396
3397 hash_id = child->sig_alg;
3398
3399 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3400
Paul Bakker43f97992013-09-23 11:23:31 +02003401 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003402 0, hash, child->sig.p ) != 0 )
3403 {
3404 trust_ca = trust_ca->next;
3405 continue;
3406 }
3407
3408 /*
3409 * Top of chain is signed by a trusted CA
3410 */
3411 *flags &= ~BADCERT_NOT_TRUSTED;
3412 break;
3413 }
3414
Paul Bakker9a736322012-11-14 12:39:52 +00003415 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003416 * If top of chain is not the same as the trusted CA send a verify request
3417 * to the callback for any issues with validity and CRL presence for the
3418 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003419 */
3420 if( trust_ca != NULL &&
3421 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3422 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3423 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003424 {
3425 /* Check trusted CA's CRL for then chain's top crt */
3426 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3427
3428 if( x509parse_time_expired( &trust_ca->valid_to ) )
3429 ca_flags |= BADCERT_EXPIRED;
3430
Paul Bakker50a5c532014-07-08 10:59:10 +02003431 if( x509parse_time_future( &trust_ca->valid_from ) )
3432 ca_flags |= BADCERT_FUTURE;
3433
Paul Bakker915275b2012-09-28 07:10:55 +00003434 if( NULL != f_vrfy )
3435 {
Paul Bakker9a736322012-11-14 12:39:52 +00003436 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003437 return( ret );
3438 }
3439 }
3440
3441 /* Call callback on top cert */
3442 if( NULL != f_vrfy )
3443 {
Paul Bakker9a736322012-11-14 12:39:52 +00003444 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003445 return( ret );
3446 }
3447
Paul Bakker915275b2012-09-28 07:10:55 +00003448 *flags |= ca_flags;
3449
3450 return( 0 );
3451}
3452
3453static int x509parse_verify_child(
3454 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003455 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003456 int (*f_vrfy)(void *, x509_cert *, int, int *),
3457 void *p_vrfy )
3458{
3459 int hash_id, ret;
3460 int parent_flags = 0;
3461 unsigned char hash[64];
3462 x509_cert *grandparent;
3463
3464 if( x509parse_time_expired( &child->valid_to ) )
3465 *flags |= BADCERT_EXPIRED;
3466
Paul Bakker50a5c532014-07-08 10:59:10 +02003467 if( x509parse_time_future( &child->valid_from ) )
3468 *flags |= BADCERT_FUTURE;
3469
Paul Bakker915275b2012-09-28 07:10:55 +00003470 hash_id = child->sig_alg;
3471
3472 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3473
Paul Bakker43f97992013-09-23 11:23:31 +02003474 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3475 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003476 *flags |= BADCERT_NOT_TRUSTED;
3477
3478 /* Check trusted CA's CRL for the given crt */
3479 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3480
3481 grandparent = parent->next;
3482
3483 while( grandparent != NULL )
3484 {
3485 if( grandparent->version == 0 ||
3486 grandparent->ca_istrue == 0 ||
3487 parent->issuer_raw.len != grandparent->subject_raw.len ||
3488 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3489 parent->issuer_raw.len ) != 0 )
3490 {
3491 grandparent = grandparent->next;
3492 continue;
3493 }
3494 break;
3495 }
3496
Paul Bakker915275b2012-09-28 07:10:55 +00003497 if( grandparent != NULL )
3498 {
3499 /*
3500 * Part of the chain
3501 */
Paul Bakker9a736322012-11-14 12:39:52 +00003502 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 +00003503 if( ret != 0 )
3504 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003505 }
Paul Bakker915275b2012-09-28 07:10:55 +00003506 else
3507 {
Paul Bakker9a736322012-11-14 12:39:52 +00003508 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 +00003509 if( ret != 0 )
3510 return( ret );
3511 }
3512
3513 /* child is verified to be a child of the parent, call verify callback */
3514 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003515 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003516 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003517
3518 *flags |= parent_flags;
3519
3520 return( 0 );
3521}
3522
Paul Bakker76fd75a2011-01-16 21:12:10 +00003523/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 * Verify the certificate validity
3525 */
3526int x509parse_verify( x509_cert *crt,
3527 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003528 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003529 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003530 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003531 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003532{
Paul Bakker23986e52011-04-24 08:57:21 +00003533 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003534 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003535 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003536 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003538 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
Paul Bakker40ea7de2009-05-03 10:18:48 +00003540 *flags = 0;
3541
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 if( cn != NULL )
3543 {
3544 name = &crt->subject;
3545 cn_len = strlen( cn );
3546
Paul Bakker4d2c1242012-05-10 14:12:46 +00003547 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003549 cur = &crt->subject_alt_names;
3550
3551 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003552 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003553 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003554 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003555 break;
3556
Paul Bakker535e97d2012-08-23 10:49:55 +00003557 if( cur->buf.len > 2 &&
3558 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003559 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003560 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003561
Paul Bakker4d2c1242012-05-10 14:12:46 +00003562 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003563 }
3564
3565 if( cur == NULL )
3566 *flags |= BADCERT_CN_MISMATCH;
3567 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003568 else
3569 {
3570 while( name != NULL )
3571 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003572 if( name->oid.len == 3 &&
3573 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003574 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003575 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003576 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003577 break;
3578
Paul Bakker535e97d2012-08-23 10:49:55 +00003579 if( name->val.len > 2 &&
3580 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003581 x509_wildcard_verify( cn, &name->val ) )
3582 break;
3583 }
3584
3585 name = name->next;
3586 }
3587
3588 if( name == NULL )
3589 *flags |= BADCERT_CN_MISMATCH;
3590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 }
3592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003594 * Iterate upwards in the given cert chain, to find our crt parent.
3595 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003597 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
Paul Bakker76fd75a2011-01-16 21:12:10 +00003599 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003601 if( parent->ca_istrue == 0 ||
3602 crt->issuer_raw.len != parent->subject_raw.len ||
3603 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 crt->issuer_raw.len ) != 0 )
3605 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003606 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 continue;
3608 }
Paul Bakker915275b2012-09-28 07:10:55 +00003609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
Paul Bakker915275b2012-09-28 07:10:55 +00003612 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 {
Paul Bakker915275b2012-09-28 07:10:55 +00003614 /*
3615 * Part of the chain
3616 */
Paul Bakker9a736322012-11-14 12:39:52 +00003617 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003618 if( ret != 0 )
3619 return( ret );
3620 }
3621 else
Paul Bakker74111d32011-01-15 16:57:55 +00003622 {
Paul Bakker9a736322012-11-14 12:39:52 +00003623 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003624 if( ret != 0 )
3625 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003626 }
Paul Bakker915275b2012-09-28 07:10:55 +00003627
3628 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003629 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003630
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 return( 0 );
3632}
3633
3634/*
3635 * Unallocate all certificate data
3636 */
3637void x509_free( x509_cert *crt )
3638{
3639 x509_cert *cert_cur = crt;
3640 x509_cert *cert_prv;
3641 x509_name *name_cur;
3642 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003643 x509_sequence *seq_cur;
3644 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
3646 if( crt == NULL )
3647 return;
3648
3649 do
3650 {
3651 rsa_free( &cert_cur->rsa );
3652
3653 name_cur = cert_cur->issuer.next;
3654 while( name_cur != NULL )
3655 {
3656 name_prv = name_cur;
3657 name_cur = name_cur->next;
3658 memset( name_prv, 0, sizeof( x509_name ) );
3659 free( name_prv );
3660 }
3661
3662 name_cur = cert_cur->subject.next;
3663 while( name_cur != NULL )
3664 {
3665 name_prv = name_cur;
3666 name_cur = name_cur->next;
3667 memset( name_prv, 0, sizeof( x509_name ) );
3668 free( name_prv );
3669 }
3670
Paul Bakker74111d32011-01-15 16:57:55 +00003671 seq_cur = cert_cur->ext_key_usage.next;
3672 while( seq_cur != NULL )
3673 {
3674 seq_prv = seq_cur;
3675 seq_cur = seq_cur->next;
3676 memset( seq_prv, 0, sizeof( x509_sequence ) );
3677 free( seq_prv );
3678 }
3679
Paul Bakker8afa70d2012-02-11 18:42:45 +00003680 seq_cur = cert_cur->subject_alt_names.next;
3681 while( seq_cur != NULL )
3682 {
3683 seq_prv = seq_cur;
3684 seq_cur = seq_cur->next;
3685 memset( seq_prv, 0, sizeof( x509_sequence ) );
3686 free( seq_prv );
3687 }
3688
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 if( cert_cur->raw.p != NULL )
3690 {
3691 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3692 free( cert_cur->raw.p );
3693 }
3694
3695 cert_cur = cert_cur->next;
3696 }
3697 while( cert_cur != NULL );
3698
3699 cert_cur = crt;
3700 do
3701 {
3702 cert_prv = cert_cur;
3703 cert_cur = cert_cur->next;
3704
3705 memset( cert_prv, 0, sizeof( x509_cert ) );
3706 if( cert_prv != crt )
3707 free( cert_prv );
3708 }
3709 while( cert_cur != NULL );
3710}
3711
Paul Bakkerd98030e2009-05-02 15:13:40 +00003712/*
3713 * Unallocate all CRL data
3714 */
3715void x509_crl_free( x509_crl *crl )
3716{
3717 x509_crl *crl_cur = crl;
3718 x509_crl *crl_prv;
3719 x509_name *name_cur;
3720 x509_name *name_prv;
3721 x509_crl_entry *entry_cur;
3722 x509_crl_entry *entry_prv;
3723
3724 if( crl == NULL )
3725 return;
3726
3727 do
3728 {
3729 name_cur = crl_cur->issuer.next;
3730 while( name_cur != NULL )
3731 {
3732 name_prv = name_cur;
3733 name_cur = name_cur->next;
3734 memset( name_prv, 0, sizeof( x509_name ) );
3735 free( name_prv );
3736 }
3737
3738 entry_cur = crl_cur->entry.next;
3739 while( entry_cur != NULL )
3740 {
3741 entry_prv = entry_cur;
3742 entry_cur = entry_cur->next;
3743 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3744 free( entry_prv );
3745 }
3746
3747 if( crl_cur->raw.p != NULL )
3748 {
3749 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3750 free( crl_cur->raw.p );
3751 }
3752
3753 crl_cur = crl_cur->next;
3754 }
3755 while( crl_cur != NULL );
3756
3757 crl_cur = crl;
3758 do
3759 {
3760 crl_prv = crl_cur;
3761 crl_cur = crl_cur->next;
3762
3763 memset( crl_prv, 0, sizeof( x509_crl ) );
3764 if( crl_prv != crl )
3765 free( crl_prv );
3766 }
3767 while( crl_cur != NULL );
3768}
3769
Paul Bakker40e46942009-01-03 21:51:57 +00003770#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
Paul Bakker40e46942009-01-03 21:51:57 +00003772#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
3774/*
3775 * Checkup routine
3776 */
3777int x509_self_test( int verbose )
3778{
Paul Bakker5690efc2011-05-26 13:16:06 +00003779#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003780 int ret;
3781 int flags;
3782 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 x509_cert cacert;
3784 x509_cert clicert;
3785 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003786#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003787 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003788#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003789
3790 if( verbose != 0 )
3791 printf( " X.509 certificate load: " );
3792
3793 memset( &clicert, 0, sizeof( x509_cert ) );
3794
Paul Bakkereae09db2013-06-06 12:35:54 +02003795 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003796 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 if( ret != 0 )
3798 {
3799 if( verbose != 0 )
3800 printf( "failed\n" );
3801
3802 return( ret );
3803 }
3804
3805 memset( &cacert, 0, sizeof( x509_cert ) );
3806
Paul Bakkereae09db2013-06-06 12:35:54 +02003807 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003808 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 if( ret != 0 )
3810 {
3811 if( verbose != 0 )
3812 printf( "failed\n" );
3813
3814 return( ret );
3815 }
3816
3817 if( verbose != 0 )
3818 printf( "passed\n X.509 private key load: " );
3819
3820 i = strlen( test_ca_key );
3821 j = strlen( test_ca_pwd );
3822
Paul Bakker66b78b22011-03-25 14:22:50 +00003823 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3824
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003826 (const unsigned char *) test_ca_key, i,
3827 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 {
3829 if( verbose != 0 )
3830 printf( "failed\n" );
3831
3832 return( ret );
3833 }
3834
3835 if( verbose != 0 )
3836 printf( "passed\n X.509 signature verify: ");
3837
Paul Bakker23986e52011-04-24 08:57:21 +00003838 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 if( ret != 0 )
3840 {
3841 if( verbose != 0 )
3842 printf( "failed\n" );
3843
3844 return( ret );
3845 }
3846
Paul Bakker5690efc2011-05-26 13:16:06 +00003847#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003849 printf( "passed\n X.509 DHM parameter load: " );
3850
3851 i = strlen( test_dhm_params );
3852 j = strlen( test_ca_pwd );
3853
Paul Bakkereae09db2013-06-06 12:35:54 +02003854 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003855 {
3856 if( verbose != 0 )
3857 printf( "failed\n" );
3858
3859 return( ret );
3860 }
3861
3862 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003864#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003865
3866 x509_free( &cacert );
3867 x509_free( &clicert );
3868 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003869#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003870 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003871#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
3873 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003874#else
3875 ((void) verbose);
3876 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3877#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003878}
3879
3880#endif
3881
3882#endif