blob: 9d62e9e4b81a83b21e8a3e1f213619b8e9fb097a [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
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100897static int x509_get_crt_ext_type( const x509_buf *oid )
898{
899 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == oid->len ) &&
900 memcmp( oid->p, OID_BASIC_CONSTRAINTS, oid->len ) == 0 )
901 {
902 return( EXT_BASIC_CONSTRAINTS );
903 }
904 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == oid->len ) &&
905 memcmp( oid->p, OID_NS_CERT_TYPE, oid->len ) == 0 )
906 {
907 return( EXT_NS_CERT_TYPE );
908 }
909 else if( ( OID_SIZE( OID_KEY_USAGE ) == oid->len ) &&
910 memcmp( oid->p, OID_KEY_USAGE, oid->len ) == 0 )
911 {
912 return( EXT_KEY_USAGE );
913 }
914 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == oid->len ) &&
915 memcmp( oid->p, OID_EXTENDED_KEY_USAGE, oid->len ) == 0 )
916 {
917 return( EXT_EXTENDED_KEY_USAGE );
918 }
919 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == oid->len ) &&
920 memcmp( oid->p, OID_SUBJECT_ALT_NAME, oid->len ) == 0 )
921 {
922 return( EXT_SUBJECT_ALT_NAME );
923 }
924
925 return( -1 );
926}
927
Paul Bakkera8cd2392012-02-11 16:09:32 +0000928/*
Paul Bakker74111d32011-01-15 16:57:55 +0000929 * X.509 v3 extensions
930 *
931 * TODO: Perform all of the basic constraints tests required by the RFC
932 * TODO: Set values for undetected extensions to a sane default?
933 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000934 */
935static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000936 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000937 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000938{
Paul Bakker23986e52011-04-24 08:57:21 +0000939 int ret;
940 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000941 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000942
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000943 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000944 {
945 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
946 return( 0 );
947
948 return( ret );
949 }
950
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 while( *p < end )
952 {
Paul Bakker74111d32011-01-15 16:57:55 +0000953 /*
954 * Extension ::= SEQUENCE {
955 * extnID OBJECT IDENTIFIER,
956 * critical BOOLEAN DEFAULT FALSE,
957 * extnValue OCTET STRING }
958 */
959 x509_buf extn_oid = {0, 0, NULL};
960 int is_critical = 0; /* DEFAULT FALSE */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100961 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000962
Paul Bakker5121ce52009-01-03 21:22:43 +0000963 if( ( ret = asn1_get_tag( p, end, &len,
964 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000965 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000967 end_ext_data = *p + len;
968
Paul Bakker74111d32011-01-15 16:57:55 +0000969 /* Get extension ID */
970 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Paul Bakker74111d32011-01-15 16:57:55 +0000972 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000973 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
Paul Bakker74111d32011-01-15 16:57:55 +0000975 extn_oid.p = *p;
976 *p += extn_oid.len;
977
978 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000979 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000980 POLARSSL_ERR_ASN1_OUT_OF_DATA );
981
982 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000983 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000984 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000985 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Paul Bakker74111d32011-01-15 16:57:55 +0000987 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000988 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000990 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000992 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000993
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000994 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000996 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker74111d32011-01-15 16:57:55 +0000998 /*
999 * Detect supported extensions
1000 */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001001 ext_type = x509_get_crt_ext_type( &extn_oid );
1002
1003 if( ext_type < 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001004 {
1005 /* No parser found, skip extension */
1006 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker5c721f92011-07-27 16:51:09 +00001008#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001009 if( is_critical )
1010 {
1011 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001012 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001013 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1014 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001015#endif
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001016 continue;
1017 }
1018
1019 /* Forbid repeated extensions */
1020 if( ( crt->ext_types & ext_type ) != 0 )
1021 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
1022
1023 crt->ext_types |= ext_type;
1024
1025 switch( ext_type )
1026 {
1027 case EXT_BASIC_CONSTRAINTS:
1028 /* Parse basic constraints */
1029 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1030 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1031 return( ret );
1032 break;
1033
1034 case EXT_KEY_USAGE:
1035 /* Parse key usage */
1036 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1037 &crt->key_usage ) ) != 0 )
1038 return( ret );
1039 break;
1040
1041 case EXT_EXTENDED_KEY_USAGE:
1042 /* Parse extended key usage */
1043 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1044 &crt->ext_key_usage ) ) != 0 )
1045 return( ret );
1046 break;
1047
1048 case EXT_SUBJECT_ALT_NAME:
1049 /* Parse subject alt name */
1050 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1051 &crt->subject_alt_names ) ) != 0 )
1052 return( ret );
1053 break;
1054
1055 case EXT_NS_CERT_TYPE:
1056 /* Parse netscape certificate type */
1057 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1058 &crt->ns_cert_type ) ) != 0 )
1059 return( ret );
1060 break;
1061
1062 default:
1063 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
1067 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001068 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001069 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 return( 0 );
1072}
1073
1074/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 * X.509 CRL Entries
1076 */
1077static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001078 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001079 x509_crl_entry *entry )
1080{
Paul Bakker23986e52011-04-24 08:57:21 +00001081 int ret;
1082 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001083 x509_crl_entry *cur_entry = entry;
1084
1085 if( *p == end )
1086 return( 0 );
1087
Paul Bakker9be19372009-07-27 20:21:53 +00001088 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1090 {
1091 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1092 return( 0 );
1093
1094 return( ret );
1095 }
1096
Paul Bakker9be19372009-07-27 20:21:53 +00001097 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001098
1099 while( *p < end )
1100 {
Paul Bakker23986e52011-04-24 08:57:21 +00001101 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001102 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001103
1104 if( ( ret = asn1_get_tag( p, end, &len2,
1105 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1106 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001107 return( ret );
1108 }
1109
Paul Bakker9be19372009-07-27 20:21:53 +00001110 cur_entry->raw.tag = **p;
1111 cur_entry->raw.p = *p;
1112 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001113 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001114
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001115 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001116 return( ret );
1117
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001118 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001119 return( ret );
1120
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001121 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001122 return( ret );
1123
Paul Bakker74111d32011-01-15 16:57:55 +00001124 if ( *p < end )
1125 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001127
1128 if( cur_entry->next == NULL )
1129 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1130
Paul Bakkerd98030e2009-05-02 15:13:40 +00001131 cur_entry = cur_entry->next;
1132 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1133 }
1134 }
1135
1136 return( 0 );
1137}
1138
Paul Bakker27d66162010-03-17 06:56:01 +00001139static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1140{
1141 if( sig_oid->len == 9 &&
1142 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1143 {
1144 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1145 {
1146 *sig_alg = sig_oid->p[8];
1147 return( 0 );
1148 }
1149
1150 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1151 {
1152 *sig_alg = sig_oid->p[8];
1153 return( 0 );
1154 }
1155
1156 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1157 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001158 if( sig_oid->len == 5 &&
1159 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1160 {
1161 *sig_alg = SIG_RSA_SHA1;
1162 return( 0 );
1163 }
Paul Bakker27d66162010-03-17 06:56:01 +00001164
1165 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1166}
1167
Paul Bakkerd98030e2009-05-02 15:13:40 +00001168/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001169 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001171static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1172 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173{
Paul Bakker23986e52011-04-24 08:57:21 +00001174 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001175 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001176 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Paul Bakker320a4b52009-03-28 18:52:39 +00001178 /*
1179 * Check for valid input
1180 */
1181 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001182 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001183
Paul Bakker96743fc2011-02-12 14:30:57 +00001184 p = (unsigned char *) malloc( len = buflen );
1185
1186 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001187 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001188
1189 memcpy( p, buf, buflen );
1190
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 crt->raw.p = p;
1192 crt->raw.len = len;
1193 end = p + len;
1194
1195 /*
1196 * Certificate ::= SEQUENCE {
1197 * tbsCertificate TBSCertificate,
1198 * signatureAlgorithm AlgorithmIdentifier,
1199 * signatureValue BIT STRING }
1200 */
1201 if( ( ret = asn1_get_tag( &p, end, &len,
1202 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1203 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001204 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001205 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 }
1207
Paul Bakkerb00ca422012-09-25 12:10:00 +00001208 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001210 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001211 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001212 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001214 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001215
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 /*
1217 * TBSCertificate ::= SEQUENCE {
1218 */
1219 crt->tbs.p = p;
1220
1221 if( ( ret = asn1_get_tag( &p, end, &len,
1222 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1223 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001224 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001225 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 }
1227
1228 end = p + len;
1229 crt->tbs.len = end - crt->tbs.p;
1230
1231 /*
1232 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1233 *
1234 * CertificateSerialNumber ::= INTEGER
1235 *
1236 * signature AlgorithmIdentifier
1237 */
1238 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1239 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1240 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1241 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001242 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 return( ret );
1244 }
1245
1246 crt->version++;
1247
1248 if( crt->version > 3 )
1249 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001251 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253
Paul Bakker27d66162010-03-17 06:56:01 +00001254 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001256 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001257 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259
1260 /*
1261 * issuer Name
1262 */
1263 crt->issuer_raw.p = p;
1264
1265 if( ( ret = asn1_get_tag( &p, end, &len,
1266 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1267 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001268 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001269 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 }
1271
1272 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 return( ret );
1276 }
1277
1278 crt->issuer_raw.len = p - crt->issuer_raw.p;
1279
1280 /*
1281 * Validity ::= SEQUENCE {
1282 * notBefore Time,
1283 * notAfter Time }
1284 *
1285 */
1286 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1287 &crt->valid_to ) ) != 0 )
1288 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001289 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 return( ret );
1291 }
1292
1293 /*
1294 * subject Name
1295 */
1296 crt->subject_raw.p = p;
1297
1298 if( ( ret = asn1_get_tag( &p, end, &len,
1299 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1300 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001301 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001302 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
1304
Paul Bakkercefb3962012-06-27 11:51:09 +00001305 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001307 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 return( ret );
1309 }
1310
1311 crt->subject_raw.len = p - crt->subject_raw.p;
1312
1313 /*
1314 * SubjectPublicKeyInfo ::= SEQUENCE
1315 * algorithm AlgorithmIdentifier,
1316 * subjectPublicKey BIT STRING }
1317 */
1318 if( ( ret = asn1_get_tag( &p, end, &len,
1319 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1320 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001321 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001322 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
1324
1325 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1326 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1327 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001328 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 return( ret );
1330 }
1331
1332 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337
1338 crt->rsa.len = mpi_size( &crt->rsa.N );
1339
1340 /*
1341 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1342 * -- If present, version shall be v2 or v3
1343 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1344 * -- If present, version shall be v2 or v3
1345 * extensions [3] EXPLICIT Extensions OPTIONAL
1346 * -- If present, version shall be v3
1347 */
1348 if( crt->version == 2 || crt->version == 3 )
1349 {
1350 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1351 if( ret != 0 )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 return( ret );
1355 }
1356 }
1357
1358 if( crt->version == 2 || crt->version == 3 )
1359 {
1360 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1361 if( ret != 0 )
1362 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001363 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 return( ret );
1365 }
1366 }
1367
1368 if( crt->version == 3 )
1369 {
Paul Bakker74111d32011-01-15 16:57:55 +00001370 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 if( ret != 0 )
1372 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001373 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 return( ret );
1375 }
1376 }
1377
1378 if( p != end )
1379 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001380 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001381 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001382 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 }
1384
Paul Bakkerb00ca422012-09-25 12:10:00 +00001385 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386
1387 /*
1388 * signatureAlgorithm AlgorithmIdentifier,
1389 * signatureValue BIT STRING
1390 */
1391 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1392 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001393 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 return( ret );
1395 }
1396
Paul Bakker535e97d2012-08-23 10:49:55 +00001397 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1398 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001400 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001401 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 }
1403
1404 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1405 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001406 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 return( ret );
1408 }
1409
1410 if( p != end )
1411 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001412 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001413 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001414 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 }
1416
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001417 return( 0 );
1418}
1419
1420/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001421 * Parse one X.509 certificate in DER format from a buffer and add them to a
1422 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001423 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001424int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001425{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001426 int ret;
1427 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001428
1429 /*
1430 * Check for valid input
1431 */
1432 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001433 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001434
1435 while( crt->version != 0 && crt->next != NULL )
1436 {
1437 prev = crt;
1438 crt = crt->next;
1439 }
1440
1441 /*
1442 * Add new certificate on the end of the chain if needed.
1443 */
1444 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001445 {
1446 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1447
Paul Bakker7d06ad22009-05-02 15:53:56 +00001448 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001449 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001450
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001451 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001452 crt = crt->next;
1453 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Paul Bakkerd6d41092013-06-13 09:00:25 +02001456 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1457 {
1458 if( prev )
1459 prev->next = NULL;
1460
1461 if( crt != chain )
1462 free( crt );
1463
1464 return( ret );
1465 }
1466
1467 return( 0 );
1468}
1469
1470/*
1471 * Parse one or more PEM certificates from a buffer and add them to the chained list
1472 */
1473int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1474{
1475 int ret, success = 0, first_error = 0, total_failed = 0;
1476 int buf_format = X509_FORMAT_DER;
1477
1478 /*
1479 * Check for valid input
1480 */
1481 if( chain == NULL || buf == NULL )
1482 return( POLARSSL_ERR_X509_INVALID_INPUT );
1483
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001484 /*
1485 * Determine buffer content. Buffer contains either one DER certificate or
1486 * one or more PEM certificates.
1487 */
1488#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001489 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001490 buf_format = X509_FORMAT_PEM;
1491#endif
1492
1493 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001494 return x509parse_crt_der( chain, buf, buflen );
1495
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001496#if defined(POLARSSL_PEM_C)
1497 if( buf_format == X509_FORMAT_PEM )
1498 {
1499 pem_context pem;
1500
1501 while( buflen > 0 )
1502 {
1503 size_t use_len;
1504 pem_init( &pem );
1505
1506 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001507 (char *) "-----BEGIN CERTIFICATE-----",
1508 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001509 buf, NULL, 0, &use_len );
1510
1511 if( ret == 0 )
1512 {
1513 /*
1514 * Was PEM encoded
1515 */
1516 buflen -= use_len;
1517 buf += use_len;
1518 }
Paul Bakker64171862013-06-06 15:01:18 +02001519 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1520 {
1521 return( ret );
1522 }
Paul Bakker9255e832013-06-06 14:58:28 +02001523 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001524 {
1525 pem_free( &pem );
1526
Paul Bakker64171862013-06-06 15:01:18 +02001527 /*
1528 * PEM header and footer were found
1529 */
1530 buflen -= use_len;
1531 buf += use_len;
1532
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001533 if( first_error == 0 )
1534 first_error = ret;
1535
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001536 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001537 continue;
1538 }
1539 else
1540 break;
1541
Paul Bakkerd6d41092013-06-13 09:00:25 +02001542 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001543
1544 pem_free( &pem );
1545
1546 if( ret != 0 )
1547 {
1548 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001549 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001550 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001551 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001552 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001553
1554 if( first_error == 0 )
1555 first_error = ret;
1556
Paul Bakkerd6d41092013-06-13 09:00:25 +02001557 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001558 continue;
1559 }
1560
1561 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001562 }
1563 }
1564#endif
1565
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001566 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001567 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001568 else if( first_error )
1569 return( first_error );
1570 else
1571 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572}
1573
1574/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001575 * Parse one or more CRLs and add them to the chained list
1576 */
Paul Bakker23986e52011-04-24 08:57:21 +00001577int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578{
Paul Bakker23986e52011-04-24 08:57:21 +00001579 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001580 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001581 unsigned char *p, *end;
1582 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001583#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001584 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001585 pem_context pem;
1586#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587
1588 crl = chain;
1589
1590 /*
1591 * Check for valid input
1592 */
1593 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001594 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001595
1596 while( crl->version != 0 && crl->next != NULL )
1597 crl = crl->next;
1598
1599 /*
1600 * Add new CRL on the end of the chain if needed.
1601 */
1602 if ( crl->version != 0 && crl->next == NULL)
1603 {
1604 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1605
Paul Bakker7d06ad22009-05-02 15:53:56 +00001606 if( crl->next == NULL )
1607 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001608 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001609 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001610 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001611
Paul Bakker7d06ad22009-05-02 15:53:56 +00001612 crl = crl->next;
1613 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 }
1615
Paul Bakker96743fc2011-02-12 14:30:57 +00001616#if defined(POLARSSL_PEM_C)
1617 pem_init( &pem );
1618 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001619 (char *) "-----BEGIN X509 CRL-----",
1620 (char *) "-----END X509 CRL-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00001621 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001622
Paul Bakker96743fc2011-02-12 14:30:57 +00001623 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001625 /*
1626 * Was PEM encoded
1627 */
1628 buflen -= use_len;
1629 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001630
1631 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001632 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001633 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001634 p = pem.buf;
1635 pem.buf = NULL;
1636 len = pem.buflen;
1637 pem_free( &pem );
1638 }
Paul Bakker9255e832013-06-06 14:58:28 +02001639 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001640 {
1641 pem_free( &pem );
1642 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 }
1644 else
1645 {
1646 /*
1647 * nope, copy the raw DER data
1648 */
1649 p = (unsigned char *) malloc( len = buflen );
1650
1651 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001652 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653
1654 memcpy( p, buf, buflen );
1655
1656 buflen = 0;
1657 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001658#else
1659 p = (unsigned char *) malloc( len = buflen );
1660
1661 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001662 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001663
1664 memcpy( p, buf, buflen );
1665
1666 buflen = 0;
1667#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668
1669 crl->raw.p = p;
1670 crl->raw.len = len;
1671 end = p + len;
1672
1673 /*
1674 * CertificateList ::= SEQUENCE {
1675 * tbsCertList TBSCertList,
1676 * signatureAlgorithm AlgorithmIdentifier,
1677 * signatureValue BIT STRING }
1678 */
1679 if( ( ret = asn1_get_tag( &p, end, &len,
1680 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1681 {
1682 x509_crl_free( crl );
1683 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1684 }
1685
Paul Bakker23986e52011-04-24 08:57:21 +00001686 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687 {
1688 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001689 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1691 }
1692
1693 /*
1694 * TBSCertList ::= SEQUENCE {
1695 */
1696 crl->tbs.p = p;
1697
1698 if( ( ret = asn1_get_tag( &p, end, &len,
1699 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1700 {
1701 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001702 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001703 }
1704
1705 end = p + len;
1706 crl->tbs.len = end - crl->tbs.p;
1707
1708 /*
1709 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1710 * -- if present, MUST be v2
1711 *
1712 * signature AlgorithmIdentifier
1713 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001714 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001715 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1716 {
1717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720
1721 crl->version++;
1722
1723 if( crl->version > 2 )
1724 {
1725 x509_crl_free( crl );
1726 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1727 }
1728
Paul Bakker27d66162010-03-17 06:56:01 +00001729 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001730 {
1731 x509_crl_free( crl );
1732 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1733 }
1734
1735 /*
1736 * issuer Name
1737 */
1738 crl->issuer_raw.p = p;
1739
1740 if( ( ret = asn1_get_tag( &p, end, &len,
1741 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1742 {
1743 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001744 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001745 }
1746
1747 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
1750 return( ret );
1751 }
1752
1753 crl->issuer_raw.len = p - crl->issuer_raw.p;
1754
1755 /*
1756 * thisUpdate Time
1757 * nextUpdate Time OPTIONAL
1758 */
Paul Bakker91200182010-02-18 21:26:15 +00001759 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001760 {
1761 x509_crl_free( crl );
1762 return( ret );
1763 }
1764
Paul Bakker91200182010-02-18 21:26:15 +00001765 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766 {
Paul Bakker9d781402011-05-09 16:17:09 +00001767 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001768 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001769 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001770 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001771 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 x509_crl_free( crl );
1773 return( ret );
1774 }
1775 }
1776
1777 /*
1778 * revokedCertificates SEQUENCE OF SEQUENCE {
1779 * userCertificate CertificateSerialNumber,
1780 * revocationDate Time,
1781 * crlEntryExtensions Extensions OPTIONAL
1782 * -- if present, MUST be v2
1783 * } OPTIONAL
1784 */
1785 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1786 {
1787 x509_crl_free( crl );
1788 return( ret );
1789 }
1790
1791 /*
1792 * crlExtensions EXPLICIT Extensions OPTIONAL
1793 * -- if present, MUST be v2
1794 */
1795 if( crl->version == 2 )
1796 {
1797 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1798
1799 if( ret != 0 )
1800 {
1801 x509_crl_free( crl );
1802 return( ret );
1803 }
1804 }
1805
1806 if( p != end )
1807 {
1808 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001809 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1811 }
1812
1813 end = crl->raw.p + crl->raw.len;
1814
1815 /*
1816 * signatureAlgorithm AlgorithmIdentifier,
1817 * signatureValue BIT STRING
1818 */
1819 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1820 {
1821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824
Paul Bakker535e97d2012-08-23 10:49:55 +00001825 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1826 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 {
1828 x509_crl_free( crl );
1829 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1830 }
1831
1832 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1833 {
1834 x509_crl_free( crl );
1835 return( ret );
1836 }
1837
1838 if( p != end )
1839 {
1840 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001841 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1843 }
1844
1845 if( buflen > 0 )
1846 {
1847 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1848
Paul Bakker7d06ad22009-05-02 15:53:56 +00001849 if( crl->next == NULL )
1850 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001851 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001852 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001853 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001854
Paul Bakker7d06ad22009-05-02 15:53:56 +00001855 crl = crl->next;
1856 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001857
1858 return( x509parse_crl( crl, buf, buflen ) );
1859 }
1860
1861 return( 0 );
1862}
1863
Paul Bakker335db3f2011-04-25 15:28:35 +00001864#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001866 * Load all data from a file into a given buffer.
1867 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001868static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001869{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001871 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001872
Paul Bakkerd98030e2009-05-02 15:13:40 +00001873 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001874 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001875
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001877 if( ( size = ftell( f ) ) == -1 )
1878 {
1879 fclose( f );
1880 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1881 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001882 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001883
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001884 *n = (size_t) size;
1885
1886 if( *n + 1 == 0 ||
1887 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1888 {
1889 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001890 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001891 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001892
Paul Bakkerd98030e2009-05-02 15:13:40 +00001893 if( fread( *buf, 1, *n, f ) != *n )
1894 {
1895 fclose( f );
1896 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001897 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001899
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001901
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001903
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001905}
1906
1907/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 * Load one or more certificates and add them to the chained list
1909 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001910int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911{
1912 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 size_t n;
1914 unsigned char *buf;
1915
Paul Bakker69e095c2011-12-10 21:55:01 +00001916 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1917 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Paul Bakker69e095c2011-12-10 21:55:01 +00001919 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
1921 memset( buf, 0, n + 1 );
1922 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
1924 return( ret );
1925}
1926
Paul Bakker8d914582012-06-04 12:46:42 +00001927int x509parse_crtpath( x509_cert *chain, const char *path )
1928{
1929 int ret = 0;
1930#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001931 int w_ret;
1932 WCHAR szDir[MAX_PATH];
1933 char filename[MAX_PATH];
1934 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001935 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001936
Paul Bakker97872ac2012-11-02 12:53:26 +00001937 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001938 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001939
1940 if( len > MAX_PATH - 3 )
1941 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001942
Paul Bakker3338b792012-10-01 21:13:10 +00001943 memset( szDir, 0, sizeof(szDir) );
1944 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001945 memcpy( filename, path, len );
1946 filename[len++] = '\\';
1947 p = filename + len;
1948 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001949
Paul Bakker40cc9142014-07-07 15:16:47 +02001950 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001951
Paul Bakker97872ac2012-11-02 12:53:26 +00001952 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001953 if (hFind == INVALID_HANDLE_VALUE)
1954 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1955
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001956 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001957 do
1958 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001959 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001960
Paul Bakkere4791f32012-06-04 21:29:15 +00001961 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001962 continue;
1963
Paul Bakker3338b792012-10-01 21:13:10 +00001964 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1965 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001966 p, len - 1,
1967 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001968
Paul Bakker3338b792012-10-01 21:13:10 +00001969 w_ret = x509parse_crtfile( chain, filename );
1970 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001971 ret++;
1972 else
1973 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001974 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001975 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001976
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001977 if (GetLastError() != ERROR_NO_MORE_FILES)
1978 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001979
1980 FindClose( hFind );
1981#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001982#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001983 int t_ret, i;
1984 struct stat sb;
1985 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001986 char entry_name[255];
1987 DIR *dir = opendir( path );
1988
1989 if( dir == NULL)
1990 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1991
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001992 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001993 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001994 if( result == NULL )
1995 break;
1996
1997 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1998
1999 i = stat( entry_name, &sb );
2000
2001 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002002 {
2003 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002004 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002005 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002006
2007 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002008 continue;
2009
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002010 // Ignore parse errors
2011 //
Paul Bakker8d914582012-06-04 12:46:42 +00002012 t_ret = x509parse_crtfile( chain, entry_name );
2013 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002014 ret++;
2015 else
2016 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002017 }
2018 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002019#else /* POLARSSL_HAVE_READDIR_R */
2020 ((void) chain);
2021 ((void) path);
2022 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2023#endif /* POLARSSL_HAVE_READDIR_R */
2024#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002025
2026 return( ret );
2027}
2028
Paul Bakkerd98030e2009-05-02 15:13:40 +00002029/*
2030 * Load one or more CRLs and add them to the chained list
2031 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002032int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002033{
2034 int ret;
2035 size_t n;
2036 unsigned char *buf;
2037
Paul Bakker69e095c2011-12-10 21:55:01 +00002038 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2039 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002040
Paul Bakker27fdf462011-06-09 13:55:13 +00002041 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002042
2043 memset( buf, 0, n + 1 );
2044 free( buf );
2045
2046 return( ret );
2047}
2048
Paul Bakker5121ce52009-01-03 21:22:43 +00002049/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002050 * Load and parse a private RSA key
2051 */
2052int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2053{
2054 int ret;
2055 size_t n;
2056 unsigned char *buf;
2057
Paul Bakker69e095c2011-12-10 21:55:01 +00002058 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2059 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002060
2061 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002062 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002063 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002064 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002065 (unsigned char *) pwd, strlen( pwd ) );
2066
2067 memset( buf, 0, n + 1 );
2068 free( buf );
2069
2070 return( ret );
2071}
2072
2073/*
2074 * Load and parse a public RSA key
2075 */
2076int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2077{
2078 int ret;
2079 size_t n;
2080 unsigned char *buf;
2081
Paul Bakker69e095c2011-12-10 21:55:01 +00002082 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2083 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002084
Paul Bakker27fdf462011-06-09 13:55:13 +00002085 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002086
2087 memset( buf, 0, n + 1 );
2088 free( buf );
2089
2090 return( ret );
2091}
2092#endif /* POLARSSL_FS_IO */
2093
2094/*
Paul Bakker65a19092013-06-06 21:14:58 +02002095 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 */
Paul Bakker65a19092013-06-06 21:14:58 +02002097static int x509parse_key_pkcs1_der( rsa_context *rsa,
2098 const unsigned char *key,
2099 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100{
Paul Bakker23986e52011-04-24 08:57:21 +00002101 int ret;
2102 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002104
Paul Bakker96743fc2011-02-12 14:30:57 +00002105 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002106 end = p + keylen;
2107
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002109 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002110 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 * RSAPrivateKey ::= SEQUENCE {
2112 * version Version,
2113 * modulus INTEGER, -- n
2114 * publicExponent INTEGER, -- e
2115 * privateExponent INTEGER, -- d
2116 * prime1 INTEGER, -- p
2117 * prime2 INTEGER, -- q
2118 * exponent1 INTEGER, -- d mod (p-1)
2119 * exponent2 INTEGER, -- d mod (q-1)
2120 * coefficient INTEGER, -- (inverse of q) mod p
2121 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2122 * }
2123 */
2124 if( ( ret = asn1_get_tag( &p, end, &len,
2125 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2126 {
Paul Bakker9d781402011-05-09 16:17:09 +00002127 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
2130 end = p + len;
2131
2132 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2133 {
Paul Bakker9d781402011-05-09 16:17:09 +00002134 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002135 }
2136
2137 if( rsa->ver != 0 )
2138 {
Paul Bakker9d781402011-05-09 16:17:09 +00002139 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
2142 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2143 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2144 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2145 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2146 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2147 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2148 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2149 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2150 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002151 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002152 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154
2155 rsa->len = mpi_size( &rsa->N );
2156
2157 if( p != end )
2158 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002161 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002162 }
2163
2164 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2165 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 rsa_free( rsa );
2167 return( ret );
2168 }
2169
Paul Bakker65a19092013-06-06 21:14:58 +02002170 return( 0 );
2171}
2172
2173/*
2174 * Parse an unencrypted PKCS#8 encoded private RSA key
2175 */
2176static int x509parse_key_pkcs8_unencrypted_der(
2177 rsa_context *rsa,
2178 const unsigned char *key,
2179 size_t keylen )
2180{
2181 int ret;
2182 size_t len;
2183 unsigned char *p, *end;
2184 x509_buf pk_alg_oid;
2185
2186 p = (unsigned char *) key;
2187 end = p + keylen;
2188
2189 /*
2190 * This function parses the PrivatKeyInfo object (PKCS#8)
2191 *
2192 * PrivateKeyInfo ::= SEQUENCE {
2193 * version Version,
2194 * algorithm AlgorithmIdentifier,
2195 * PrivateKey BIT STRING
2196 * }
2197 *
2198 * AlgorithmIdentifier ::= SEQUENCE {
2199 * algorithm OBJECT IDENTIFIER,
2200 * parameters ANY DEFINED BY algorithm OPTIONAL
2201 * }
2202 *
2203 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2204 */
2205 if( ( ret = asn1_get_tag( &p, end, &len,
2206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2207 {
2208 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2209 }
2210
2211 end = p + len;
2212
2213 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2214 {
2215 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2216 }
2217
2218 if( rsa->ver != 0 )
2219 {
2220 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2221 }
2222
2223 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2224 {
2225 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2226 }
2227
2228 /*
2229 * only RSA keys handled at this time
2230 */
2231 if( pk_alg_oid.len != 9 ||
2232 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2233 {
2234 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2235 }
2236
2237 /*
2238 * Get the OCTET STRING and parse the PKCS#1 format inside
2239 */
2240 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2241 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2242
2243 if( ( end - p ) < 1 )
2244 {
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2246 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2247 }
2248
2249 end = p + len;
2250
2251 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2252 return( ret );
2253
2254 return( 0 );
2255}
2256
2257/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002258 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002259 */
2260static int x509parse_key_pkcs8_encrypted_der(
2261 rsa_context *rsa,
2262 const unsigned char *key,
2263 size_t keylen,
2264 const unsigned char *pwd,
2265 size_t pwdlen )
2266{
2267 int ret;
2268 size_t len;
2269 unsigned char *p, *end, *end2;
2270 x509_buf pbe_alg_oid, pbe_params;
2271 unsigned char buf[2048];
2272
2273 memset(buf, 0, 2048);
2274
2275 p = (unsigned char *) key;
2276 end = p + keylen;
2277
Paul Bakker1fd43212013-06-17 15:14:42 +02002278 if( pwdlen == 0 )
2279 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2280
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002281 /*
2282 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2283 *
2284 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2285 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2286 * encryptedData EncryptedData
2287 * }
2288 *
2289 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2290 *
2291 * EncryptedData ::= OCTET STRING
2292 *
2293 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2294 */
2295 if( ( ret = asn1_get_tag( &p, end, &len,
2296 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2297 {
2298 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2299 }
2300
2301 end = p + len;
2302
2303 if( ( ret = asn1_get_tag( &p, end, &len,
2304 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2305 {
2306 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2307 }
2308
2309 end2 = p + len;
2310
2311 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2312 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2313
2314 pbe_alg_oid.p = p;
2315 p += pbe_alg_oid.len;
2316
2317 /*
2318 * Store the algorithm parameters
2319 */
2320 pbe_params.p = p;
2321 pbe_params.len = end2 - p;
2322 p += pbe_params.len;
2323
2324 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2325 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2326
2327 // buf has been sized to 2048 bytes
2328 if( len > 2048 )
2329 return( POLARSSL_ERR_X509_INVALID_INPUT );
2330
2331 /*
2332 * Decrypt EncryptedData with appropriate PDE
2333 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002334#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002335 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2336 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002337 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2338 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2339 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002340 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002341 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2342 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2343
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002344 return( ret );
2345 }
2346 }
2347 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2348 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002349 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2350 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2351 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002352 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002353 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2354 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2355
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002356 return( ret );
2357 }
2358 }
2359 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2360 {
2361 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2362 PKCS12_PBE_DECRYPT,
2363 pwd, pwdlen,
2364 p, len, buf ) ) != 0 )
2365 {
2366 return( ret );
2367 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002368
2369 // Best guess for password mismatch when using RC4. If first tag is
2370 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2371 //
2372 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2373 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002374 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002375 else
2376#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002377#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002378 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002379 {
2380 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2381 p, len, buf ) ) != 0 )
2382 {
2383 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2384 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2385
2386 return( ret );
2387 }
2388 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002389 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002390#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002391 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2392
2393 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2394}
2395
2396/*
Paul Bakker65a19092013-06-06 21:14:58 +02002397 * Parse a private RSA key
2398 */
2399int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2400 const unsigned char *pwd, size_t pwdlen )
2401{
2402 int ret;
2403
Paul Bakker96743fc2011-02-12 14:30:57 +00002404#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002405 size_t len;
2406 pem_context pem;
2407
2408 pem_init( &pem );
2409 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002410 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2411 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002412 key, pwd, pwdlen, &len );
2413 if( ret == 0 )
2414 {
2415 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2416 {
2417 rsa_free( rsa );
2418 }
2419
2420 pem_free( &pem );
2421 return( ret );
2422 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002423 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2424 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2425 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2426 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002427 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002428 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002429
2430 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002431 (char *) "-----BEGIN PRIVATE KEY-----",
2432 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002433 key, NULL, 0, &len );
2434 if( ret == 0 )
2435 {
2436 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2437 pem.buf, pem.buflen ) ) != 0 )
2438 {
2439 rsa_free( rsa );
2440 }
2441
2442 pem_free( &pem );
2443 return( ret );
2444 }
2445 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002446 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002447
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002448 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002449 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2450 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002451 key, NULL, 0, &len );
2452 if( ret == 0 )
2453 {
2454 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2455 pem.buf, pem.buflen,
2456 pwd, pwdlen ) ) != 0 )
2457 {
2458 rsa_free( rsa );
2459 }
2460
2461 pem_free( &pem );
2462 return( ret );
2463 }
2464 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002465 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002466#else
2467 ((void) pwd);
2468 ((void) pwdlen);
2469#endif /* POLARSSL_PEM_C */
2470
2471 // At this point we only know it's not a PEM formatted key. Could be any
2472 // of the known DER encoded private key formats
2473 //
2474 // We try the different DER format parsers to see if one passes without
2475 // error
2476 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002477 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2478 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002479 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002480 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002483 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002484
2485 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2486 {
2487 return( ret );
2488 }
2489
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002490 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2491 return( 0 );
2492
2493 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002494
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002495 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2496 return( 0 );
2497
2498 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002499
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002500 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002501}
2502
2503/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002504 * Parse a public RSA key
2505 */
Paul Bakker23986e52011-04-24 08:57:21 +00002506int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002507{
Paul Bakker23986e52011-04-24 08:57:21 +00002508 int ret;
2509 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002510 unsigned char *p, *end;
2511 x509_buf alg_oid;
2512#if defined(POLARSSL_PEM_C)
2513 pem_context pem;
2514
2515 pem_init( &pem );
2516 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002517 (char *) "-----BEGIN PUBLIC KEY-----",
2518 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002519 key, NULL, 0, &len );
2520
2521 if( ret == 0 )
2522 {
2523 /*
2524 * Was PEM encoded
2525 */
2526 keylen = pem.buflen;
2527 }
Paul Bakker9255e832013-06-06 14:58:28 +02002528 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002529 {
2530 pem_free( &pem );
2531 return( ret );
2532 }
2533
2534 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2535#else
2536 p = (unsigned char *) key;
2537#endif
2538 end = p + keylen;
2539
2540 /*
2541 * PublicKeyInfo ::= SEQUENCE {
2542 * algorithm AlgorithmIdentifier,
2543 * PublicKey BIT STRING
2544 * }
2545 *
2546 * AlgorithmIdentifier ::= SEQUENCE {
2547 * algorithm OBJECT IDENTIFIER,
2548 * parameters ANY DEFINED BY algorithm OPTIONAL
2549 * }
2550 *
2551 * RSAPublicKey ::= SEQUENCE {
2552 * modulus INTEGER, -- n
2553 * publicExponent INTEGER -- e
2554 * }
2555 */
2556
2557 if( ( ret = asn1_get_tag( &p, end, &len,
2558 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2559 {
2560#if defined(POLARSSL_PEM_C)
2561 pem_free( &pem );
2562#endif
2563 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002564 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002565 }
2566
2567 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2568 {
2569#if defined(POLARSSL_PEM_C)
2570 pem_free( &pem );
2571#endif
2572 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002573 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002574 }
2575
2576 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2577 {
2578#if defined(POLARSSL_PEM_C)
2579 pem_free( &pem );
2580#endif
2581 rsa_free( rsa );
2582 return( ret );
2583 }
2584
2585 rsa->len = mpi_size( &rsa->N );
2586
2587#if defined(POLARSSL_PEM_C)
2588 pem_free( &pem );
2589#endif
2590
2591 return( 0 );
2592}
2593
Paul Bakkereaa89f82011-04-04 21:36:15 +00002594#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002595/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002596 * Parse DHM parameters
2597 */
Paul Bakker23986e52011-04-24 08:57:21 +00002598int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002599{
Paul Bakker23986e52011-04-24 08:57:21 +00002600 int ret;
2601 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002602 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002603#if defined(POLARSSL_PEM_C)
2604 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002605
Paul Bakker96743fc2011-02-12 14:30:57 +00002606 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002607
Paul Bakker96743fc2011-02-12 14:30:57 +00002608 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002609 (char *) "-----BEGIN DH PARAMETERS-----",
2610 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002611 dhmin, NULL, 0, &dhminlen );
2612
2613 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002614 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002615 /*
2616 * Was PEM encoded
2617 */
2618 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002619 }
Paul Bakker9255e832013-06-06 14:58:28 +02002620 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002621 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002622 pem_free( &pem );
2623 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002624 }
2625
Paul Bakker96743fc2011-02-12 14:30:57 +00002626 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2627#else
2628 p = (unsigned char *) dhmin;
2629#endif
2630 end = p + dhminlen;
2631
Paul Bakker1b57b062011-01-06 15:48:19 +00002632 memset( dhm, 0, sizeof( dhm_context ) );
2633
Paul Bakker1b57b062011-01-06 15:48:19 +00002634 /*
2635 * DHParams ::= SEQUENCE {
2636 * prime INTEGER, -- P
2637 * generator INTEGER, -- g
2638 * }
2639 */
2640 if( ( ret = asn1_get_tag( &p, end, &len,
2641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2642 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002643#if defined(POLARSSL_PEM_C)
2644 pem_free( &pem );
2645#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002646 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002647 }
2648
2649 end = p + len;
2650
2651 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2652 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2653 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002654#if defined(POLARSSL_PEM_C)
2655 pem_free( &pem );
2656#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002657 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002658 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002659 }
2660
2661 if( p != end )
2662 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002663#if defined(POLARSSL_PEM_C)
2664 pem_free( &pem );
2665#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002666 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002667 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002668 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2669 }
2670
Paul Bakker96743fc2011-02-12 14:30:57 +00002671#if defined(POLARSSL_PEM_C)
2672 pem_free( &pem );
2673#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002674
2675 return( 0 );
2676}
2677
Paul Bakker335db3f2011-04-25 15:28:35 +00002678#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002679/*
2680 * Load and parse a private RSA key
2681 */
2682int x509parse_dhmfile( dhm_context *dhm, const char *path )
2683{
2684 int ret;
2685 size_t n;
2686 unsigned char *buf;
2687
Paul Bakker69e095c2011-12-10 21:55:01 +00002688 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2689 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002690
Paul Bakker27fdf462011-06-09 13:55:13 +00002691 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002692
2693 memset( buf, 0, n + 1 );
2694 free( buf );
2695
2696 return( ret );
2697}
Paul Bakker335db3f2011-04-25 15:28:35 +00002698#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002699#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002700
Paul Bakker5121ce52009-01-03 21:22:43 +00002701#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002702#include <stdarg.h>
2703
2704#if !defined vsnprintf
2705#define vsnprintf _vsnprintf
2706#endif // vsnprintf
2707
2708/*
2709 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2710 * Result value is not size of buffer needed, but -1 if no fit is possible.
2711 *
2712 * This fuction tries to 'fix' this by at least suggesting enlarging the
2713 * size by 20.
2714 */
2715int compat_snprintf(char *str, size_t size, const char *format, ...)
2716{
2717 va_list ap;
2718 int res = -1;
2719
2720 va_start( ap, format );
2721
2722 res = vsnprintf( str, size, format, ap );
2723
2724 va_end( ap );
2725
2726 // No quick fix possible
2727 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002728 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002729
2730 return res;
2731}
2732
2733#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002734#endif
2735
Paul Bakkerd98030e2009-05-02 15:13:40 +00002736#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2737
2738#define SAFE_SNPRINTF() \
2739{ \
2740 if( ret == -1 ) \
2741 return( -1 ); \
2742 \
Paul Bakker23986e52011-04-24 08:57:21 +00002743 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002744 p[n - 1] = '\0'; \
2745 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2746 } \
2747 \
Paul Bakker23986e52011-04-24 08:57:21 +00002748 n -= (unsigned int) ret; \
2749 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002750}
2751
Paul Bakker5121ce52009-01-03 21:22:43 +00002752/*
2753 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002756int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757{
Paul Bakker23986e52011-04-24 08:57:21 +00002758 int ret;
2759 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002761 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 char s[128], *p;
2763
2764 memset( s, 0, sizeof( s ) );
2765
2766 name = dn;
2767 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002768 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
2770 while( name != NULL )
2771 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002772 if( !name->oid.p )
2773 {
2774 name = name->next;
2775 continue;
2776 }
2777
Paul Bakker74111d32011-01-15 16:57:55 +00002778 if( name != dn )
2779 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002780 ret = snprintf( p, n, ", " );
2781 SAFE_SNPRINTF();
2782 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Paul Bakker535e97d2012-08-23 10:49:55 +00002784 if( name->oid.len == 3 &&
2785 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
2787 switch( name->oid.p[2] )
2788 {
2789 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
2792 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002793 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
2795 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002796 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
2798 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002799 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
2801 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002802 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
2804 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002805 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002808 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 name->oid.p[2] );
2810 break;
2811 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002814 else if( name->oid.len == 9 &&
2815 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 {
2817 switch( name->oid.p[8] )
2818 {
2819 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002820 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
2822 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002823 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 name->oid.p[8] );
2825 break;
2826 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
2829 else
Paul Bakker74111d32011-01-15 16:57:55 +00002830 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002831 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002832 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
2835 for( i = 0; i < name->val.len; i++ )
2836 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002837 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 break;
2839
2840 c = name->val.p[i];
2841 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2842 s[i] = '?';
2843 else s[i] = c;
2844 }
2845 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002846 ret = snprintf( p, n, "%s", s );
2847 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 name = name->next;
2849 }
2850
Paul Bakker23986e52011-04-24 08:57:21 +00002851 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852}
2853
2854/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002855 * Store the serial in printable form into buf; no more
2856 * than size characters will be written
2857 */
2858int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2859{
Paul Bakker23986e52011-04-24 08:57:21 +00002860 int ret;
2861 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002862 char *p;
2863
2864 p = buf;
2865 n = size;
2866
2867 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002868 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002869
2870 for( i = 0; i < nr; i++ )
2871 {
Paul Bakker93048802011-12-05 14:38:06 +00002872 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002873 continue;
2874
Paul Bakkerdd476992011-01-16 21:34:59 +00002875 ret = snprintf( p, n, "%02X%s",
2876 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2877 SAFE_SNPRINTF();
2878 }
2879
Paul Bakker03c7c252011-11-25 12:37:37 +00002880 if( nr != serial->len )
2881 {
2882 ret = snprintf( p, n, "...." );
2883 SAFE_SNPRINTF();
2884 }
2885
Paul Bakker23986e52011-04-24 08:57:21 +00002886 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002887}
2888
2889/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002890 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002892int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2893 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002894{
Paul Bakker23986e52011-04-24 08:57:21 +00002895 int ret;
2896 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002897 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
2899 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002900 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakkerd98030e2009-05-02 15:13:40 +00002902 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904 SAFE_SNPRINTF();
2905 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002907 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Paul Bakkerdd476992011-01-16 21:34:59 +00002909 ret = x509parse_serial_gets( p, n, &crt->serial);
2910 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2913 SAFE_SNPRINTF();
2914 ret = x509parse_dn_gets( p, n, &crt->issuer );
2915 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakkerd98030e2009-05-02 15:13:40 +00002917 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2918 SAFE_SNPRINTF();
2919 ret = x509parse_dn_gets( p, n, &crt->subject );
2920 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2924 crt->valid_from.year, crt->valid_from.mon,
2925 crt->valid_from.day, crt->valid_from.hour,
2926 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Paul Bakkerd98030e2009-05-02 15:13:40 +00002929 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2931 crt->valid_to.year, crt->valid_to.mon,
2932 crt->valid_to.day, crt->valid_to.hour,
2933 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002934 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakkerd98030e2009-05-02 15:13:40 +00002936 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2937 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Paul Bakker27d66162010-03-17 06:56:01 +00002939 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002941 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2942 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2943 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2944 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2945 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2946 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2947 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2948 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2949 default: ret = snprintf( p, n, "???" ); break;
2950 }
2951 SAFE_SNPRINTF();
2952
2953 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002954 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002955 SAFE_SNPRINTF();
2956
Paul Bakker23986e52011-04-24 08:57:21 +00002957 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002958}
2959
Paul Bakker74111d32011-01-15 16:57:55 +00002960/*
2961 * Return an informational string describing the given OID
2962 */
2963const char *x509_oid_get_description( x509_buf *oid )
2964{
2965 if ( oid == NULL )
2966 return ( NULL );
2967
2968 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2969 return( STRING_SERVER_AUTH );
2970
2971 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2972 return( STRING_CLIENT_AUTH );
2973
2974 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2975 return( STRING_CODE_SIGNING );
2976
2977 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2978 return( STRING_EMAIL_PROTECTION );
2979
2980 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2981 return( STRING_TIME_STAMPING );
2982
2983 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2984 return( STRING_OCSP_SIGNING );
2985
2986 return( NULL );
2987}
2988
2989/* Return the x.y.z.... style numeric string for the given OID */
2990int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2991{
Paul Bakker23986e52011-04-24 08:57:21 +00002992 int ret;
2993 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002994 unsigned int value;
2995 char *p;
2996
2997 p = buf;
2998 n = size;
2999
3000 /* First byte contains first two dots */
3001 if( oid->len > 0 )
3002 {
3003 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3004 SAFE_SNPRINTF();
3005 }
3006
3007 /* TODO: value can overflow in value. */
3008 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003009 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003010 {
3011 value <<= 7;
3012 value += oid->p[i] & 0x7F;
3013
3014 if( !( oid->p[i] & 0x80 ) )
3015 {
3016 /* Last byte */
3017 ret = snprintf( p, n, ".%d", value );
3018 SAFE_SNPRINTF();
3019 value = 0;
3020 }
3021 }
3022
Paul Bakker23986e52011-04-24 08:57:21 +00003023 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003024}
3025
Paul Bakkerd98030e2009-05-02 15:13:40 +00003026/*
3027 * Return an informational string about the CRL.
3028 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003029int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3030 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031{
Paul Bakker23986e52011-04-24 08:57:21 +00003032 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003033 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003034 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003035 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036
3037 p = buf;
3038 n = size;
3039
3040 ret = snprintf( p, n, "%sCRL version : %d",
3041 prefix, crl->version );
3042 SAFE_SNPRINTF();
3043
3044 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3045 SAFE_SNPRINTF();
3046 ret = x509parse_dn_gets( p, n, &crl->issuer );
3047 SAFE_SNPRINTF();
3048
3049 ret = snprintf( p, n, "\n%sthis update : " \
3050 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3051 crl->this_update.year, crl->this_update.mon,
3052 crl->this_update.day, crl->this_update.hour,
3053 crl->this_update.min, crl->this_update.sec );
3054 SAFE_SNPRINTF();
3055
3056 ret = snprintf( p, n, "\n%snext update : " \
3057 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3058 crl->next_update.year, crl->next_update.mon,
3059 crl->next_update.day, crl->next_update.hour,
3060 crl->next_update.min, crl->next_update.sec );
3061 SAFE_SNPRINTF();
3062
3063 entry = &crl->entry;
3064
3065 ret = snprintf( p, n, "\n%sRevoked certificates:",
3066 prefix );
3067 SAFE_SNPRINTF();
3068
Paul Bakker9be19372009-07-27 20:21:53 +00003069 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003070 {
3071 ret = snprintf( p, n, "\n%sserial number: ",
3072 prefix );
3073 SAFE_SNPRINTF();
3074
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003075 ret = x509parse_serial_gets( p, n, &entry->serial);
3076 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003077
Paul Bakkerd98030e2009-05-02 15:13:40 +00003078 ret = snprintf( p, n, " revocation date: " \
3079 "%04d-%02d-%02d %02d:%02d:%02d",
3080 entry->revocation_date.year, entry->revocation_date.mon,
3081 entry->revocation_date.day, entry->revocation_date.hour,
3082 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003083 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003084
3085 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 }
3087
Paul Bakkerd98030e2009-05-02 15:13:40 +00003088 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3089 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003090
Paul Bakker27d66162010-03-17 06:56:01 +00003091 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003092 {
3093 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3094 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3095 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3096 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3097 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3098 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3099 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3100 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3101 default: ret = snprintf( p, n, "???" ); break;
3102 }
3103 SAFE_SNPRINTF();
3104
Paul Bakker1e27bb22009-07-19 20:25:25 +00003105 ret = snprintf( p, n, "\n" );
3106 SAFE_SNPRINTF();
3107
Paul Bakker23986e52011-04-24 08:57:21 +00003108 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109}
3110
3111/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003112 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003114static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003115{
Paul Bakkercce9d772011-11-18 14:26:47 +00003116#if defined(_WIN32)
3117 SYSTEMTIME st;
3118
Paul Bakkerf48de952014-07-08 14:39:41 +02003119 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003120
Paul Bakker0d844dd2014-07-07 17:44:14 +02003121 now->year = st.wYear;
3122 now->mon = st.wMonth;
3123 now->day = st.wDay;
3124 now->hour = st.wHour;
3125 now->min = st.wMinute;
3126 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003127#else
Paul Bakker358a8412014-07-08 12:14:37 +02003128 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 time_t tt;
3130
3131 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003132 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003133
Paul Bakker358a8412014-07-08 12:14:37 +02003134 now->year = lt.tm_year + 1900;
3135 now->mon = lt.tm_mon + 1;
3136 now->day = lt.tm_mday;
3137 now->hour = lt.tm_hour;
3138 now->min = lt.tm_min;
3139 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003140#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003141}
Paul Bakkercce9d772011-11-18 14:26:47 +00003142
Paul Bakker0d844dd2014-07-07 17:44:14 +02003143/*
3144 * Return 0 if before <= after, 1 otherwise
3145 */
3146static int x509_check_time( const x509_time *before, const x509_time *after )
3147{
3148 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003149 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
Paul Bakker0d844dd2014-07-07 17:44:14 +02003151 if( before->year == after->year &&
3152 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003153 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakker0d844dd2014-07-07 17:44:14 +02003155 if( before->year == after->year &&
3156 before->mon == after->mon &&
3157 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003158 return( 1 );
3159
Paul Bakker0d844dd2014-07-07 17:44:14 +02003160 if( before->year == after->year &&
3161 before->mon == after->mon &&
3162 before->day == after->day &&
3163 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003164 return( 1 );
3165
Paul Bakker0d844dd2014-07-07 17:44:14 +02003166 if( before->year == after->year &&
3167 before->mon == after->mon &&
3168 before->day == after->day &&
3169 before->hour == after->hour &&
3170 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003171 return( 1 );
3172
Paul Bakker0d844dd2014-07-07 17:44:14 +02003173 if( before->year == after->year &&
3174 before->mon == after->mon &&
3175 before->day == after->day &&
3176 before->hour == after->hour &&
3177 before->min == after->min &&
3178 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003179 return( 1 );
3180
Paul Bakker40ea7de2009-05-03 10:18:48 +00003181 return( 0 );
3182}
3183
Paul Bakker0d844dd2014-07-07 17:44:14 +02003184int x509parse_time_expired( const x509_time *to )
3185{
3186 x509_time now;
3187
3188 x509_get_current_time( &now );
3189
3190 return( x509_check_time( &now, to ) );
3191}
3192
3193int x509parse_time_future( const x509_time *from )
3194{
3195 x509_time now;
3196
3197 x509_get_current_time( &now );
3198
3199 return( x509_check_time( from, &now ) );
3200}
3201
Paul Bakker40ea7de2009-05-03 10:18:48 +00003202/*
3203 * Return 1 if the certificate is revoked, or 0 otherwise.
3204 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003205int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003206{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003207 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003208
3209 while( cur != NULL && cur->serial.len != 0 )
3210 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003211 if( crt->serial.len == cur->serial.len &&
3212 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003213 {
3214 if( x509parse_time_expired( &cur->revocation_date ) )
3215 return( 1 );
3216 }
3217
3218 cur = cur->next;
3219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003220
3221 return( 0 );
3222}
3223
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003224/*
3225 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003226 */
Paul Bakker23986e52011-04-24 08:57:21 +00003227static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 unsigned char *out )
3229{
3230 switch( alg )
3231 {
Paul Bakker40e46942009-01-03 21:51:57 +00003232#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003233 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003234#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003235#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003236 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003237#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003238#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003239 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003240#endif
3241#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003242 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003243#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003244#if defined(POLARSSL_SHA2_C)
3245 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3246 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3247#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003248#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003249 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3250 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003253 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 break;
3255 }
3256}
3257
3258/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003259 * Check that the given certificate is valid accoring to the CRL.
3260 */
3261static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3262 x509_crl *crl_list)
3263{
3264 int flags = 0;
3265 int hash_id;
3266 unsigned char hash[64];
3267
Paul Bakker915275b2012-09-28 07:10:55 +00003268 if( ca == NULL )
3269 return( flags );
3270
Paul Bakker76fd75a2011-01-16 21:12:10 +00003271 /*
3272 * TODO: What happens if no CRL is present?
3273 * Suggestion: Revocation state should be unknown if no CRL is present.
3274 * For backwards compatibility this is not yet implemented.
3275 */
3276
Paul Bakker915275b2012-09-28 07:10:55 +00003277 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003278 {
Paul Bakker915275b2012-09-28 07:10:55 +00003279 if( crl_list->version == 0 ||
3280 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003281 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3282 crl_list->issuer_raw.len ) != 0 )
3283 {
3284 crl_list = crl_list->next;
3285 continue;
3286 }
3287
3288 /*
3289 * Check if CRL is correctly signed by the trusted CA
3290 */
3291 hash_id = crl_list->sig_alg;
3292
3293 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3294
Paul Bakker43f97992013-09-23 11:23:31 +02003295 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003296 0, hash, crl_list->sig.p ) == 0 )
3297 {
3298 /*
3299 * CRL is not trusted
3300 */
3301 flags |= BADCRL_NOT_TRUSTED;
3302 break;
3303 }
3304
3305 /*
3306 * Check for validity of CRL (Do not drop out)
3307 */
3308 if( x509parse_time_expired( &crl_list->next_update ) )
3309 flags |= BADCRL_EXPIRED;
3310
Paul Bakker50a5c532014-07-08 10:59:10 +02003311 if( x509parse_time_future( &crl_list->this_update ) )
3312 flags |= BADCRL_FUTURE;
3313
Paul Bakker76fd75a2011-01-16 21:12:10 +00003314 /*
3315 * Check if certificate is revoked
3316 */
3317 if( x509parse_revoked(crt, crl_list) )
3318 {
3319 flags |= BADCERT_REVOKED;
3320 break;
3321 }
3322
3323 crl_list = crl_list->next;
3324 }
3325 return flags;
3326}
3327
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003328// Equal == 0, inequal == 1
3329static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3330{
3331 size_t i;
3332 unsigned char diff;
3333 const unsigned char *n1 = s1, *n2 = s2;
3334
3335 for( i = 0; i < len; i++ )
3336 {
3337 diff = n1[i] ^ n2[i];
3338
Paul Bakkerc941adb2014-07-07 14:17:24 +02003339 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003340 continue;
3341
Paul Bakkerc941adb2014-07-07 14:17:24 +02003342 if( diff == 32 &&
3343 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3344 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3345 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003346 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003347 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003348
3349 return( 1 );
3350 }
3351
3352 return( 0 );
3353}
3354
Paul Bakker1d073c52014-07-08 20:15:51 +02003355static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003356{
3357 size_t i;
3358 size_t cn_idx = 0;
3359
Paul Bakker57b12982012-02-11 17:38:38 +00003360 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003361 return( 0 );
3362
3363 for( i = 0; i < strlen( cn ); ++i )
3364 {
3365 if( cn[i] == '.' )
3366 {
3367 cn_idx = i;
3368 break;
3369 }
3370 }
3371
3372 if( cn_idx == 0 )
3373 return( 0 );
3374
Paul Bakker535e97d2012-08-23 10:49:55 +00003375 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003376 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003377 {
3378 return( 1 );
3379 }
3380
3381 return( 0 );
3382}
3383
Paul Bakker915275b2012-09-28 07:10:55 +00003384static int x509parse_verify_top(
3385 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003386 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003387 int (*f_vrfy)(void *, x509_cert *, int, int *),
3388 void *p_vrfy )
3389{
3390 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003391 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003392 unsigned char hash[64];
3393
3394 if( x509parse_time_expired( &child->valid_to ) )
3395 *flags |= BADCERT_EXPIRED;
3396
Paul Bakker50a5c532014-07-08 10:59:10 +02003397 if( x509parse_time_future( &child->valid_from ) )
3398 *flags |= BADCERT_FUTURE;
3399
Paul Bakker915275b2012-09-28 07:10:55 +00003400 /*
3401 * Child is the top of the chain. Check against the trust_ca list.
3402 */
3403 *flags |= BADCERT_NOT_TRUSTED;
3404
3405 while( trust_ca != NULL )
3406 {
3407 if( trust_ca->version == 0 ||
3408 child->issuer_raw.len != trust_ca->subject_raw.len ||
3409 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3410 child->issuer_raw.len ) != 0 )
3411 {
3412 trust_ca = trust_ca->next;
3413 continue;
3414 }
3415
Paul Bakker9a736322012-11-14 12:39:52 +00003416 /*
3417 * Reduce path_len to check against if top of the chain is
3418 * the same as the trusted CA
3419 */
3420 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3421 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3422 child->issuer_raw.len ) == 0 )
3423 {
3424 check_path_cnt--;
3425 }
3426
Paul Bakker915275b2012-09-28 07:10:55 +00003427 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003428 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003429 {
3430 trust_ca = trust_ca->next;
3431 continue;
3432 }
3433
3434 hash_id = child->sig_alg;
3435
3436 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3437
Paul Bakker43f97992013-09-23 11:23:31 +02003438 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003439 0, hash, child->sig.p ) != 0 )
3440 {
3441 trust_ca = trust_ca->next;
3442 continue;
3443 }
3444
3445 /*
3446 * Top of chain is signed by a trusted CA
3447 */
3448 *flags &= ~BADCERT_NOT_TRUSTED;
3449 break;
3450 }
3451
Paul Bakker9a736322012-11-14 12:39:52 +00003452 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003453 * If top of chain is not the same as the trusted CA send a verify request
3454 * to the callback for any issues with validity and CRL presence for the
3455 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003456 */
3457 if( trust_ca != NULL &&
3458 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3459 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3460 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003461 {
3462 /* Check trusted CA's CRL for then chain's top crt */
3463 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3464
3465 if( x509parse_time_expired( &trust_ca->valid_to ) )
3466 ca_flags |= BADCERT_EXPIRED;
3467
Paul Bakker50a5c532014-07-08 10:59:10 +02003468 if( x509parse_time_future( &trust_ca->valid_from ) )
3469 ca_flags |= BADCERT_FUTURE;
3470
Paul Bakker915275b2012-09-28 07:10:55 +00003471 if( NULL != f_vrfy )
3472 {
Paul Bakker9a736322012-11-14 12:39:52 +00003473 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003474 return( ret );
3475 }
3476 }
3477
3478 /* Call callback on top cert */
3479 if( NULL != f_vrfy )
3480 {
Paul Bakker9a736322012-11-14 12:39:52 +00003481 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003482 return( ret );
3483 }
3484
Paul Bakker915275b2012-09-28 07:10:55 +00003485 *flags |= ca_flags;
3486
3487 return( 0 );
3488}
3489
3490static int x509parse_verify_child(
3491 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003492 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003493 int (*f_vrfy)(void *, x509_cert *, int, int *),
3494 void *p_vrfy )
3495{
3496 int hash_id, ret;
3497 int parent_flags = 0;
3498 unsigned char hash[64];
3499 x509_cert *grandparent;
3500
3501 if( x509parse_time_expired( &child->valid_to ) )
3502 *flags |= BADCERT_EXPIRED;
3503
Paul Bakker50a5c532014-07-08 10:59:10 +02003504 if( x509parse_time_future( &child->valid_from ) )
3505 *flags |= BADCERT_FUTURE;
3506
Paul Bakker915275b2012-09-28 07:10:55 +00003507 hash_id = child->sig_alg;
3508
3509 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3510
Paul Bakker43f97992013-09-23 11:23:31 +02003511 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3512 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003513 *flags |= BADCERT_NOT_TRUSTED;
3514
3515 /* Check trusted CA's CRL for the given crt */
3516 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3517
3518 grandparent = parent->next;
3519
3520 while( grandparent != NULL )
3521 {
3522 if( grandparent->version == 0 ||
3523 grandparent->ca_istrue == 0 ||
3524 parent->issuer_raw.len != grandparent->subject_raw.len ||
3525 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3526 parent->issuer_raw.len ) != 0 )
3527 {
3528 grandparent = grandparent->next;
3529 continue;
3530 }
3531 break;
3532 }
3533
Paul Bakker915275b2012-09-28 07:10:55 +00003534 if( grandparent != NULL )
3535 {
3536 /*
3537 * Part of the chain
3538 */
Paul Bakker9a736322012-11-14 12:39:52 +00003539 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 +00003540 if( ret != 0 )
3541 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003542 }
Paul Bakker915275b2012-09-28 07:10:55 +00003543 else
3544 {
Paul Bakker9a736322012-11-14 12:39:52 +00003545 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 +00003546 if( ret != 0 )
3547 return( ret );
3548 }
3549
3550 /* child is verified to be a child of the parent, call verify callback */
3551 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003552 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003553 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003554
3555 *flags |= parent_flags;
3556
3557 return( 0 );
3558}
3559
Paul Bakker76fd75a2011-01-16 21:12:10 +00003560/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 * Verify the certificate validity
3562 */
3563int x509parse_verify( x509_cert *crt,
3564 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003565 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003566 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003567 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003568 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003569{
Paul Bakker23986e52011-04-24 08:57:21 +00003570 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003571 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003572 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003573 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003575 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003576
Paul Bakker40ea7de2009-05-03 10:18:48 +00003577 *flags = 0;
3578
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 if( cn != NULL )
3580 {
3581 name = &crt->subject;
3582 cn_len = strlen( cn );
3583
Paul Bakker4d2c1242012-05-10 14:12:46 +00003584 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003586 cur = &crt->subject_alt_names;
3587
3588 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003589 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003590 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003591 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003592 break;
3593
Paul Bakker535e97d2012-08-23 10:49:55 +00003594 if( cur->buf.len > 2 &&
3595 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003596 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003597 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
Paul Bakker4d2c1242012-05-10 14:12:46 +00003599 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003600 }
3601
3602 if( cur == NULL )
3603 *flags |= BADCERT_CN_MISMATCH;
3604 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003605 else
3606 {
3607 while( name != NULL )
3608 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003609 if( name->oid.len == 3 &&
3610 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003611 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003612 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003613 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003614 break;
3615
Paul Bakker535e97d2012-08-23 10:49:55 +00003616 if( name->val.len > 2 &&
3617 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003618 x509_wildcard_verify( cn, &name->val ) )
3619 break;
3620 }
3621
3622 name = name->next;
3623 }
3624
3625 if( name == NULL )
3626 *flags |= BADCERT_CN_MISMATCH;
3627 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 }
3629
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003631 * Iterate upwards in the given cert chain, to find our crt parent.
3632 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003634 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003635
Paul Bakker76fd75a2011-01-16 21:12:10 +00003636 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003638 if( parent->ca_istrue == 0 ||
3639 crt->issuer_raw.len != parent->subject_raw.len ||
3640 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 crt->issuer_raw.len ) != 0 )
3642 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003643 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 continue;
3645 }
Paul Bakker915275b2012-09-28 07:10:55 +00003646 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 }
3648
Paul Bakker915275b2012-09-28 07:10:55 +00003649 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 {
Paul Bakker915275b2012-09-28 07:10:55 +00003651 /*
3652 * Part of the chain
3653 */
Paul Bakker9a736322012-11-14 12:39:52 +00003654 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003655 if( ret != 0 )
3656 return( ret );
3657 }
3658 else
Paul Bakker74111d32011-01-15 16:57:55 +00003659 {
Paul Bakker9a736322012-11-14 12:39:52 +00003660 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003661 if( ret != 0 )
3662 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003663 }
Paul Bakker915275b2012-09-28 07:10:55 +00003664
3665 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003666 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003667
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 return( 0 );
3669}
3670
3671/*
3672 * Unallocate all certificate data
3673 */
3674void x509_free( x509_cert *crt )
3675{
3676 x509_cert *cert_cur = crt;
3677 x509_cert *cert_prv;
3678 x509_name *name_cur;
3679 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003680 x509_sequence *seq_cur;
3681 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
3683 if( crt == NULL )
3684 return;
3685
3686 do
3687 {
3688 rsa_free( &cert_cur->rsa );
3689
3690 name_cur = cert_cur->issuer.next;
3691 while( name_cur != NULL )
3692 {
3693 name_prv = name_cur;
3694 name_cur = name_cur->next;
3695 memset( name_prv, 0, sizeof( x509_name ) );
3696 free( name_prv );
3697 }
3698
3699 name_cur = cert_cur->subject.next;
3700 while( name_cur != NULL )
3701 {
3702 name_prv = name_cur;
3703 name_cur = name_cur->next;
3704 memset( name_prv, 0, sizeof( x509_name ) );
3705 free( name_prv );
3706 }
3707
Paul Bakker74111d32011-01-15 16:57:55 +00003708 seq_cur = cert_cur->ext_key_usage.next;
3709 while( seq_cur != NULL )
3710 {
3711 seq_prv = seq_cur;
3712 seq_cur = seq_cur->next;
3713 memset( seq_prv, 0, sizeof( x509_sequence ) );
3714 free( seq_prv );
3715 }
3716
Paul Bakker8afa70d2012-02-11 18:42:45 +00003717 seq_cur = cert_cur->subject_alt_names.next;
3718 while( seq_cur != NULL )
3719 {
3720 seq_prv = seq_cur;
3721 seq_cur = seq_cur->next;
3722 memset( seq_prv, 0, sizeof( x509_sequence ) );
3723 free( seq_prv );
3724 }
3725
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 if( cert_cur->raw.p != NULL )
3727 {
3728 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3729 free( cert_cur->raw.p );
3730 }
3731
3732 cert_cur = cert_cur->next;
3733 }
3734 while( cert_cur != NULL );
3735
3736 cert_cur = crt;
3737 do
3738 {
3739 cert_prv = cert_cur;
3740 cert_cur = cert_cur->next;
3741
3742 memset( cert_prv, 0, sizeof( x509_cert ) );
3743 if( cert_prv != crt )
3744 free( cert_prv );
3745 }
3746 while( cert_cur != NULL );
3747}
3748
Paul Bakkerd98030e2009-05-02 15:13:40 +00003749/*
3750 * Unallocate all CRL data
3751 */
3752void x509_crl_free( x509_crl *crl )
3753{
3754 x509_crl *crl_cur = crl;
3755 x509_crl *crl_prv;
3756 x509_name *name_cur;
3757 x509_name *name_prv;
3758 x509_crl_entry *entry_cur;
3759 x509_crl_entry *entry_prv;
3760
3761 if( crl == NULL )
3762 return;
3763
3764 do
3765 {
3766 name_cur = crl_cur->issuer.next;
3767 while( name_cur != NULL )
3768 {
3769 name_prv = name_cur;
3770 name_cur = name_cur->next;
3771 memset( name_prv, 0, sizeof( x509_name ) );
3772 free( name_prv );
3773 }
3774
3775 entry_cur = crl_cur->entry.next;
3776 while( entry_cur != NULL )
3777 {
3778 entry_prv = entry_cur;
3779 entry_cur = entry_cur->next;
3780 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3781 free( entry_prv );
3782 }
3783
3784 if( crl_cur->raw.p != NULL )
3785 {
3786 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3787 free( crl_cur->raw.p );
3788 }
3789
3790 crl_cur = crl_cur->next;
3791 }
3792 while( crl_cur != NULL );
3793
3794 crl_cur = crl;
3795 do
3796 {
3797 crl_prv = crl_cur;
3798 crl_cur = crl_cur->next;
3799
3800 memset( crl_prv, 0, sizeof( x509_crl ) );
3801 if( crl_prv != crl )
3802 free( crl_prv );
3803 }
3804 while( crl_cur != NULL );
3805}
3806
Paul Bakker40e46942009-01-03 21:51:57 +00003807#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003808
Paul Bakker40e46942009-01-03 21:51:57 +00003809#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003810
3811/*
3812 * Checkup routine
3813 */
3814int x509_self_test( int verbose )
3815{
Paul Bakker5690efc2011-05-26 13:16:06 +00003816#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003817 int ret;
3818 int flags;
3819 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 x509_cert cacert;
3821 x509_cert clicert;
3822 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003823#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003824 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003826
3827 if( verbose != 0 )
3828 printf( " X.509 certificate load: " );
3829
3830 memset( &clicert, 0, sizeof( x509_cert ) );
3831
Paul Bakkereae09db2013-06-06 12:35:54 +02003832 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003833 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 if( ret != 0 )
3835 {
3836 if( verbose != 0 )
3837 printf( "failed\n" );
3838
3839 return( ret );
3840 }
3841
3842 memset( &cacert, 0, sizeof( x509_cert ) );
3843
Paul Bakkereae09db2013-06-06 12:35:54 +02003844 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003845 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 if( ret != 0 )
3847 {
3848 if( verbose != 0 )
3849 printf( "failed\n" );
3850
3851 return( ret );
3852 }
3853
3854 if( verbose != 0 )
3855 printf( "passed\n X.509 private key load: " );
3856
3857 i = strlen( test_ca_key );
3858 j = strlen( test_ca_pwd );
3859
Paul Bakker66b78b22011-03-25 14:22:50 +00003860 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3861
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003863 (const unsigned char *) test_ca_key, i,
3864 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 {
3866 if( verbose != 0 )
3867 printf( "failed\n" );
3868
3869 return( ret );
3870 }
3871
3872 if( verbose != 0 )
3873 printf( "passed\n X.509 signature verify: ");
3874
Paul Bakker23986e52011-04-24 08:57:21 +00003875 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 if( ret != 0 )
3877 {
3878 if( verbose != 0 )
3879 printf( "failed\n" );
3880
3881 return( ret );
3882 }
3883
Paul Bakker5690efc2011-05-26 13:16:06 +00003884#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003886 printf( "passed\n X.509 DHM parameter load: " );
3887
3888 i = strlen( test_dhm_params );
3889 j = strlen( test_ca_pwd );
3890
Paul Bakkereae09db2013-06-06 12:35:54 +02003891 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003892 {
3893 if( verbose != 0 )
3894 printf( "failed\n" );
3895
3896 return( ret );
3897 }
3898
3899 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003902
3903 x509_free( &cacert );
3904 x509_free( &clicert );
3905 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003906#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003907 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003908#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
3910 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003911#else
3912 ((void) verbose);
3913 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003915}
3916
3917#endif
3918
3919#endif