blob: 54110ba52d9752ed4bf46bfacb81d31660beb566 [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;
Manuel Pégourié-Gonnardd8a1ea72014-11-17 12:04:51 +0100196
197 if( len < 1 )
198 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
199 POLARSSL_ERR_ASN1_OUT_OF_DATA );
200
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 alg->tag = **p;
202
203 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000204 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 alg->p = *p;
207 *p += alg->len;
208
209 if( *p == end )
210 return( 0 );
211
212 /*
213 * assume the algorithm parameters must be NULL
214 */
215 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000216 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000219 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000220 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 return( 0 );
223}
224
225/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 * AttributeTypeAndValue ::= SEQUENCE {
227 * type AttributeType,
228 * value AttributeValue }
229 *
230 * AttributeType ::= OBJECT IDENTIFIER
231 *
232 * AttributeValue ::= ANY DEFINED BY AttributeType
233 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000234static int x509_get_attr_type_value( unsigned char **p,
235 const unsigned char *end,
236 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237{
Paul Bakker23986e52011-04-24 08:57:21 +0000238 int ret;
239 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 x509_buf *oid;
241 x509_buf *val;
242
243 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000245 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 oid = &cur->oid;
Manuel Pégourié-Gonnardd8a1ea72014-11-17 12:04:51 +0100248
249 if( len < 1 )
250 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
251 POLARSSL_ERR_ASN1_OUT_OF_DATA );
252
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 oid->tag = **p;
254
255 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000256 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258 oid->p = *p;
259 *p += oid->len;
260
261 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000262 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000263 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
266 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
267 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000268 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000269 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
271 val = &cur->val;
272 val->tag = *(*p)++;
273
274 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000275 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277 val->p = *p;
278 *p += val->len;
279
280 cur->next = NULL;
281
Paul Bakker400ff6f2011-02-20 10:40:16 +0000282 return( 0 );
283}
284
285/*
286 * RelativeDistinguishedName ::=
287 * SET OF AttributeTypeAndValue
288 *
289 * AttributeTypeAndValue ::= SEQUENCE {
290 * type AttributeType,
291 * value AttributeValue }
292 *
293 * AttributeType ::= OBJECT IDENTIFIER
294 *
295 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200296 *
297 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
298 * the most common case, and our x509_name structure currently can't handle
299 * more than that.
Paul Bakker400ff6f2011-02-20 10:40:16 +0000300 */
301static int x509_get_name( unsigned char **p,
302 const unsigned char *end,
303 x509_name *cur )
304{
Paul Bakker23986e52011-04-24 08:57:21 +0000305 int ret;
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200306 size_t set_len;
307 const unsigned char *end_set;
308
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100309 /* don't use recursion, we'd risk stack overflow if not optimized */
310 while( 1 )
311 {
312 /*
313 * parse first SET, restricted to 1 element
314 */
315 if( ( ret = asn1_get_tag( p, end, &set_len,
316 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
317 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000318
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100319 end_set = *p + set_len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000320
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100321 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
322 return( ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000323
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100324 if( *p != end_set )
325 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100327 /*
328 * continue until end of SEQUENCE is reached
329 */
330 if( *p == end )
331 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100333 cur->next = (x509_name *) malloc( sizeof( x509_name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100335 if( cur->next == NULL )
336 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100338 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker430ffbe2012-05-01 08:14:20 +0000339
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100340 cur = cur->next;
341 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000342}
343
344/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 * Time ::= CHOICE {
346 * utcTime UTCTime,
347 * generalTime GeneralizedTime }
348 */
Paul Bakker91200182010-02-18 21:26:15 +0000349static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000350 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000351 x509_time *time )
352{
Paul Bakker23986e52011-04-24 08:57:21 +0000353 int ret;
354 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000356 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357
Paul Bakker91200182010-02-18 21:26:15 +0000358 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000359 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
360 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker91200182010-02-18 21:26:15 +0000362 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000363
Paul Bakker91200182010-02-18 21:26:15 +0000364 if ( tag == ASN1_UTC_TIME )
365 {
366 (*p)++;
367 ret = asn1_get_len( p, end, &len );
368
369 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000370 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000371
Paul Bakker91200182010-02-18 21:26:15 +0000372 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000373 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
374 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000375
Paul Bakker243d6182014-07-08 14:40:58 +0200376 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000377 &time->year, &time->mon, &time->day,
378 &time->hour, &time->min, &time->sec ) < 5 )
379 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000380
Paul Bakker400ff6f2011-02-20 10:40:16 +0000381 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000382 time->year += 1900;
383
384 *p += len;
385
386 return( 0 );
387 }
388 else if ( tag == ASN1_GENERALIZED_TIME )
389 {
390 (*p)++;
391 ret = asn1_get_len( p, end, &len );
392
393 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000394 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000395
396 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000397 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
398 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000399
Paul Bakker243d6182014-07-08 14:40:58 +0200400 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000401 &time->year, &time->mon, &time->day,
402 &time->hour, &time->min, &time->sec ) < 5 )
403 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
404
405 *p += len;
406
407 return( 0 );
408 }
409 else
Paul Bakker9d781402011-05-09 16:17:09 +0000410 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000411}
412
413
414/*
415 * Validity ::= SEQUENCE {
416 * notBefore Time,
417 * notAfter Time }
418 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000419static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000420 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 x509_time *from,
422 x509_time *to )
423{
Paul Bakker23986e52011-04-24 08:57:21 +0000424 int ret;
425 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 if( ( ret = asn1_get_tag( p, end, &len,
428 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000429 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 end = *p + len;
432
Paul Bakker91200182010-02-18 21:26:15 +0000433 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000434 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Paul Bakker91200182010-02-18 21:26:15 +0000436 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000440 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000441 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443 return( 0 );
444}
445
446/*
447 * SubjectPublicKeyInfo ::= SEQUENCE {
448 * algorithm AlgorithmIdentifier,
449 * subjectPublicKey BIT STRING }
450 */
451static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000452 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 x509_buf *pk_alg_oid,
454 mpi *N, mpi *E )
455{
Paul Bakker65a19092013-06-06 21:14:58 +0200456 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000457 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 unsigned char *end2;
459
460 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
461 return( ret );
462
463 /*
464 * only RSA public keys handled at this time
465 */
Paul Bakker65a19092013-06-06 21:14:58 +0200466 if( pk_alg_oid->len != 9 ||
467 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) != 0 )
Paul Bakker400ff6f2011-02-20 10:40:16 +0000468 {
Paul Bakkered56b222011-07-13 11:26:43 +0000469 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker65a19092013-06-06 21:14:58 +0200470 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000473 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000477 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479 end2 = *p + len;
480
481 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 /*
485 * RSAPublicKey ::= SEQUENCE {
486 * modulus INTEGER, -- n
487 * publicExponent INTEGER -- e
488 * }
489 */
490 if( ( ret = asn1_get_tag( p, end2, &len,
491 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000495 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000496 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
499 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000500 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000504 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 return( 0 );
507}
508
509static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000510 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 x509_buf *sig )
512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 int ret;
514 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker8afa70d2012-02-11 18:42:45 +0000516 if( ( end - *p ) < 1 )
517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
518 POLARSSL_ERR_ASN1_OUT_OF_DATA );
519
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 sig->tag = **p;
521
522 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000523 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Paul Bakker74111d32011-01-15 16:57:55 +0000525
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000527 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 sig->len = len;
530 sig->p = *p;
531
532 *p += len;
533
534 return( 0 );
535}
536
537/*
538 * X.509 v2/v3 unique identifier (not parsed)
539 */
540static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 x509_buf *uid, int n )
543{
544 int ret;
545
546 if( *p == end )
547 return( 0 );
548
549 uid->tag = **p;
550
551 if( ( ret = asn1_get_tag( p, end, &uid->len,
552 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
553 {
Paul Bakker40e46942009-01-03 21:51:57 +0000554 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 return( 0 );
556
557 return( ret );
558 }
559
560 uid->p = *p;
561 *p += uid->len;
562
563 return( 0 );
564}
565
566/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000567 * X.509 Extensions (No parsing of extensions, pointer should
568 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
570static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000571 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000572 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000573{
Paul Bakker23986e52011-04-24 08:57:21 +0000574 int ret;
575 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 if( *p == end )
578 return( 0 );
579
580 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000583 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 ext->p = *p;
587 end = *p + ext->len;
588
589 /*
590 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
591 *
592 * Extension ::= SEQUENCE {
593 * extnID OBJECT IDENTIFIER,
594 * critical BOOLEAN DEFAULT FALSE,
595 * extnValue OCTET STRING }
596 */
597 if( ( ret = asn1_get_tag( p, end, &len,
598 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000599 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000602 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000603 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Paul Bakkerd98030e2009-05-02 15:13:40 +0000605 return( 0 );
606}
607
608/*
609 * X.509 CRL v2 extensions (no extensions parsed yet.)
610 */
611static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000612 const unsigned char *end,
613 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614{
Paul Bakker23986e52011-04-24 08:57:21 +0000615 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000616 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000617
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000618 /* Get explicit tag */
619 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000620 {
621 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
622 return( 0 );
623
624 return( ret );
625 }
626
627 while( *p < end )
628 {
629 if( ( ret = asn1_get_tag( p, end, &len,
630 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000632
633 *p += len;
634 }
635
636 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000637 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000638 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
639
640 return( 0 );
641}
642
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000643/*
644 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
645 */
646static int x509_get_crl_entry_ext( unsigned char **p,
647 const unsigned char *end,
648 x509_buf *ext )
649{
650 int ret;
651 size_t len = 0;
652
653 /* OPTIONAL */
654 if (end <= *p)
655 return( 0 );
656
657 ext->tag = **p;
658 ext->p = *p;
659
660 /*
661 * Get CRL-entry extension sequence header
662 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
663 */
664 if( ( ret = asn1_get_tag( p, end, &ext->len,
665 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
666 {
667 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
668 {
669 ext->p = NULL;
670 return( 0 );
671 }
672 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
673 }
674
675 end = *p + ext->len;
676
677 if( end != *p + ext->len )
678 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
679 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
680
681 while( *p < end )
682 {
683 if( ( ret = asn1_get_tag( p, end, &len,
684 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
685 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
686
687 *p += len;
688 }
689
690 if( *p != end )
691 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
692 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
693
694 return( 0 );
695}
696
Paul Bakker74111d32011-01-15 16:57:55 +0000697static int x509_get_basic_constraints( unsigned char **p,
698 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000699 int *ca_istrue,
700 int *max_pathlen )
701{
Paul Bakker23986e52011-04-24 08:57:21 +0000702 int ret;
703 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000704
705 /*
706 * BasicConstraints ::= SEQUENCE {
707 * cA BOOLEAN DEFAULT FALSE,
708 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
709 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000710 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000711 *max_pathlen = 0; /* endless */
712
713 if( ( ret = asn1_get_tag( p, end, &len,
714 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000715 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000716
717 if( *p == end )
718 return 0;
719
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000721 {
722 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000723 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000724
725 if( ret != 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
Paul Bakker3cccddb2011-01-16 21:46:31 +0000728 if( *ca_istrue != 0 )
729 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000730 }
731
732 if( *p == end )
733 return 0;
734
735 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000737
738 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000739 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000740 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
741
742 (*max_pathlen)++;
743
Paul Bakker74111d32011-01-15 16:57:55 +0000744 return 0;
745}
746
747static int x509_get_ns_cert_type( unsigned char **p,
748 const unsigned char *end,
749 unsigned char *ns_cert_type)
750{
751 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000752 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000755 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000756
757 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000758 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000759 POLARSSL_ERR_ASN1_INVALID_LENGTH );
760
761 /* Get actual bitstring */
762 *ns_cert_type = *bs.p;
763 return 0;
764}
765
766static int x509_get_key_usage( unsigned char **p,
767 const unsigned char *end,
768 unsigned char *key_usage)
769{
770 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000771 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000772
773 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000775
Paul Bakker94a67962012-08-23 13:03:52 +0000776 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000778 POLARSSL_ERR_ASN1_INVALID_LENGTH );
779
780 /* Get actual bitstring */
781 *key_usage = *bs.p;
782 return 0;
783}
784
Paul Bakkerd98030e2009-05-02 15:13:40 +0000785/*
Paul Bakker74111d32011-01-15 16:57:55 +0000786 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
787 *
788 * KeyPurposeId ::= OBJECT IDENTIFIER
789 */
790static int x509_get_ext_key_usage( unsigned char **p,
791 const unsigned char *end,
792 x509_sequence *ext_key_usage)
793{
794 int ret;
795
796 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000797 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000798
799 /* Sequence length must be >= 1 */
800 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000801 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000802 POLARSSL_ERR_ASN1_INVALID_LENGTH );
803
804 return 0;
805}
806
807/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000808 * SubjectAltName ::= GeneralNames
809 *
810 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
811 *
812 * GeneralName ::= CHOICE {
813 * otherName [0] OtherName,
814 * rfc822Name [1] IA5String,
815 * dNSName [2] IA5String,
816 * x400Address [3] ORAddress,
817 * directoryName [4] Name,
818 * ediPartyName [5] EDIPartyName,
819 * uniformResourceIdentifier [6] IA5String,
820 * iPAddress [7] OCTET STRING,
821 * registeredID [8] OBJECT IDENTIFIER }
822 *
823 * OtherName ::= SEQUENCE {
824 * type-id OBJECT IDENTIFIER,
825 * value [0] EXPLICIT ANY DEFINED BY type-id }
826 *
827 * EDIPartyName ::= SEQUENCE {
828 * nameAssigner [0] DirectoryString OPTIONAL,
829 * partyName [1] DirectoryString }
830 *
831 * NOTE: PolarSSL only parses and uses dNSName at this point.
832 */
833static int x509_get_subject_alt_name( unsigned char **p,
834 const unsigned char *end,
835 x509_sequence *subject_alt_name )
836{
837 int ret;
838 size_t len, tag_len;
839 asn1_buf *buf;
840 unsigned char tag;
841 asn1_sequence *cur = subject_alt_name;
842
843 /* Get main sequence tag */
844 if( ( ret = asn1_get_tag( p, end, &len,
845 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
847
848 if( *p + len != end )
849 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
850 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
851
852 while( *p < end )
853 {
854 if( ( end - *p ) < 1 )
855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
856 POLARSSL_ERR_ASN1_OUT_OF_DATA );
857
858 tag = **p;
859 (*p)++;
860 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
862
863 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
864 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
865 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
866
867 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
868 {
869 *p += tag_len;
870 continue;
871 }
872
873 buf = &(cur->buf);
874 buf->tag = tag;
875 buf->p = *p;
876 buf->len = tag_len;
877 *p += buf->len;
878
879 /* Allocate and assign next pointer */
880 if (*p < end)
881 {
Manuel Pégourié-Gonnardfdec9572014-11-11 23:11:16 +0100882 if( cur->next != NULL )
883 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
884
Paul Bakkera8cd2392012-02-11 16:09:32 +0000885 cur->next = (asn1_sequence *) malloc(
886 sizeof( asn1_sequence ) );
887
888 if( cur->next == NULL )
889 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
890 POLARSSL_ERR_ASN1_MALLOC_FAILED );
891
Paul Bakker535e97d2012-08-23 10:49:55 +0000892 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000893 cur = cur->next;
894 }
895 }
896
897 /* Set final sequence entry's next pointer to NULL */
898 cur->next = NULL;
899
900 if( *p != end )
901 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
902 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
903
904 return( 0 );
905}
906
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100907static int x509_get_crt_ext_type( const x509_buf *oid )
908{
909 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == oid->len ) &&
910 memcmp( oid->p, OID_BASIC_CONSTRAINTS, oid->len ) == 0 )
911 {
912 return( EXT_BASIC_CONSTRAINTS );
913 }
914 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == oid->len ) &&
915 memcmp( oid->p, OID_NS_CERT_TYPE, oid->len ) == 0 )
916 {
917 return( EXT_NS_CERT_TYPE );
918 }
919 else if( ( OID_SIZE( OID_KEY_USAGE ) == oid->len ) &&
920 memcmp( oid->p, OID_KEY_USAGE, oid->len ) == 0 )
921 {
922 return( EXT_KEY_USAGE );
923 }
924 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == oid->len ) &&
925 memcmp( oid->p, OID_EXTENDED_KEY_USAGE, oid->len ) == 0 )
926 {
927 return( EXT_EXTENDED_KEY_USAGE );
928 }
929 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == oid->len ) &&
930 memcmp( oid->p, OID_SUBJECT_ALT_NAME, oid->len ) == 0 )
931 {
932 return( EXT_SUBJECT_ALT_NAME );
933 }
934
935 return( -1 );
936}
937
Paul Bakkera8cd2392012-02-11 16:09:32 +0000938/*
Paul Bakker74111d32011-01-15 16:57:55 +0000939 * X.509 v3 extensions
940 *
941 * TODO: Perform all of the basic constraints tests required by the RFC
942 * TODO: Set values for undetected extensions to a sane default?
943 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000944 */
945static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000946 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000947 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000948{
Paul Bakker23986e52011-04-24 08:57:21 +0000949 int ret;
950 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000951 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000952
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000953 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000954 {
955 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
956 return( 0 );
957
958 return( ret );
959 }
960
Paul Bakker5121ce52009-01-03 21:22:43 +0000961 while( *p < end )
962 {
Paul Bakker74111d32011-01-15 16:57:55 +0000963 /*
964 * Extension ::= SEQUENCE {
965 * extnID OBJECT IDENTIFIER,
966 * critical BOOLEAN DEFAULT FALSE,
967 * extnValue OCTET STRING }
968 */
969 x509_buf extn_oid = {0, 0, NULL};
970 int is_critical = 0; /* DEFAULT FALSE */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100971 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000972
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 if( ( ret = asn1_get_tag( p, end, &len,
974 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000975 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000977 end_ext_data = *p + len;
978
Paul Bakker74111d32011-01-15 16:57:55 +0000979 /* Get extension ID */
980 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000983 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker74111d32011-01-15 16:57:55 +0000985 extn_oid.p = *p;
986 *p += extn_oid.len;
987
988 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000989 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000990 POLARSSL_ERR_ASN1_OUT_OF_DATA );
991
992 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000993 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000994 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000995 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000996
Paul Bakker74111d32011-01-15 16:57:55 +0000997 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000998 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +00001000 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001002 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001003
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001004 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001005 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001006 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001007
Paul Bakker74111d32011-01-15 16:57:55 +00001008 /*
1009 * Detect supported extensions
1010 */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001011 ext_type = x509_get_crt_ext_type( &extn_oid );
1012
1013 if( ext_type < 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001014 {
1015 /* No parser found, skip extension */
1016 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Paul Bakker5c721f92011-07-27 16:51:09 +00001018#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001019 if( is_critical )
1020 {
1021 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001022 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001023 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1024 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001025#endif
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001026 continue;
1027 }
1028
1029 /* Forbid repeated extensions */
1030 if( ( crt->ext_types & ext_type ) != 0 )
1031 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
1032
1033 crt->ext_types |= ext_type;
1034
1035 switch( ext_type )
1036 {
1037 case EXT_BASIC_CONSTRAINTS:
1038 /* Parse basic constraints */
1039 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1040 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1041 return( ret );
1042 break;
1043
1044 case EXT_KEY_USAGE:
1045 /* Parse key usage */
1046 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1047 &crt->key_usage ) ) != 0 )
1048 return( ret );
1049 break;
1050
1051 case EXT_EXTENDED_KEY_USAGE:
1052 /* Parse extended key usage */
1053 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1054 &crt->ext_key_usage ) ) != 0 )
1055 return( ret );
1056 break;
1057
1058 case EXT_SUBJECT_ALT_NAME:
1059 /* Parse subject alt name */
1060 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1061 &crt->subject_alt_names ) ) != 0 )
1062 return( ret );
1063 break;
1064
1065 case EXT_NS_CERT_TYPE:
1066 /* Parse netscape certificate type */
1067 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1068 &crt->ns_cert_type ) ) != 0 )
1069 return( ret );
1070 break;
1071
1072 default:
1073 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 }
1076
1077 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001079 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 return( 0 );
1082}
1083
1084/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001085 * X.509 CRL Entries
1086 */
1087static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001088 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 x509_crl_entry *entry )
1090{
Paul Bakker23986e52011-04-24 08:57:21 +00001091 int ret;
1092 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001093 x509_crl_entry *cur_entry = entry;
1094
1095 if( *p == end )
1096 return( 0 );
1097
Paul Bakker9be19372009-07-27 20:21:53 +00001098 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001099 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1100 {
1101 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1102 return( 0 );
1103
1104 return( ret );
1105 }
1106
Paul Bakker9be19372009-07-27 20:21:53 +00001107 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001108
1109 while( *p < end )
1110 {
Paul Bakker23986e52011-04-24 08:57:21 +00001111 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001112 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001113
1114 if( ( ret = asn1_get_tag( p, end, &len2,
1115 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1116 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001117 return( ret );
1118 }
1119
Paul Bakker9be19372009-07-27 20:21:53 +00001120 cur_entry->raw.tag = **p;
1121 cur_entry->raw.p = *p;
1122 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001123 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001124
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001125 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 return( ret );
1127
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001128 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001129 return( ret );
1130
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001131 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001132 return( ret );
1133
Paul Bakker74111d32011-01-15 16:57:55 +00001134 if ( *p < end )
1135 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001136 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001137
1138 if( cur_entry->next == NULL )
1139 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1140
Paul Bakkerd98030e2009-05-02 15:13:40 +00001141 cur_entry = cur_entry->next;
1142 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1143 }
1144 }
1145
1146 return( 0 );
1147}
1148
Paul Bakker27d66162010-03-17 06:56:01 +00001149static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1150{
1151 if( sig_oid->len == 9 &&
1152 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1153 {
1154 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1155 {
1156 *sig_alg = sig_oid->p[8];
1157 return( 0 );
1158 }
1159
1160 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1161 {
1162 *sig_alg = sig_oid->p[8];
1163 return( 0 );
1164 }
1165
1166 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1167 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001168 if( sig_oid->len == 5 &&
1169 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1170 {
1171 *sig_alg = SIG_RSA_SHA1;
1172 return( 0 );
1173 }
Paul Bakker27d66162010-03-17 06:56:01 +00001174
1175 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1176}
1177
Paul Bakkerd98030e2009-05-02 15:13:40 +00001178/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001179 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001181static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1182 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001183{
Paul Bakker23986e52011-04-24 08:57:21 +00001184 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001185 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001186 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001187
Paul Bakker320a4b52009-03-28 18:52:39 +00001188 /*
1189 * Check for valid input
1190 */
1191 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001192 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001193
Paul Bakker96743fc2011-02-12 14:30:57 +00001194 p = (unsigned char *) malloc( len = buflen );
1195
1196 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001197 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001198
1199 memcpy( p, buf, buflen );
1200
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 crt->raw.p = p;
1202 crt->raw.len = len;
1203 end = p + len;
1204
1205 /*
1206 * Certificate ::= SEQUENCE {
1207 * tbsCertificate TBSCertificate,
1208 * signatureAlgorithm AlgorithmIdentifier,
1209 * signatureValue BIT STRING }
1210 */
1211 if( ( ret = asn1_get_tag( &p, end, &len,
1212 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001215 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 }
1217
Paul Bakkerb00ca422012-09-25 12:10:00 +00001218 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001220 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001221 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001222 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001224 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001225
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 /*
1227 * TBSCertificate ::= SEQUENCE {
1228 */
1229 crt->tbs.p = p;
1230
1231 if( ( ret = asn1_get_tag( &p, end, &len,
1232 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1233 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001234 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001235 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 }
1237
1238 end = p + len;
1239 crt->tbs.len = end - crt->tbs.p;
1240
1241 /*
1242 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1243 *
1244 * CertificateSerialNumber ::= INTEGER
1245 *
1246 * signature AlgorithmIdentifier
1247 */
1248 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1249 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1250 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
1256 crt->version++;
1257
1258 if( crt->version > 3 )
1259 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001260 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001261 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 }
1263
Paul Bakker27d66162010-03-17 06:56:01 +00001264 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001266 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001267 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
1269
1270 /*
1271 * issuer Name
1272 */
1273 crt->issuer_raw.p = p;
1274
1275 if( ( ret = asn1_get_tag( &p, end, &len,
1276 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1277 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001278 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001279 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 }
1281
1282 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 return( ret );
1286 }
1287
1288 crt->issuer_raw.len = p - crt->issuer_raw.p;
1289
1290 /*
1291 * Validity ::= SEQUENCE {
1292 * notBefore Time,
1293 * notAfter Time }
1294 *
1295 */
1296 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1297 &crt->valid_to ) ) != 0 )
1298 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001299 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 return( ret );
1301 }
1302
1303 /*
1304 * subject Name
1305 */
1306 crt->subject_raw.p = p;
1307
1308 if( ( ret = asn1_get_tag( &p, end, &len,
1309 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1310 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001311 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001312 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
1314
Paul Bakkercefb3962012-06-27 11:51:09 +00001315 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001317 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 return( ret );
1319 }
1320
1321 crt->subject_raw.len = p - crt->subject_raw.p;
1322
1323 /*
1324 * SubjectPublicKeyInfo ::= SEQUENCE
1325 * algorithm AlgorithmIdentifier,
1326 * subjectPublicKey BIT STRING }
1327 */
1328 if( ( ret = asn1_get_tag( &p, end, &len,
1329 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1330 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001331 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001332 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 }
1334
1335 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1336 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1337 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001338 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 return( ret );
1340 }
1341
1342 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1343 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001344 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 return( ret );
1346 }
1347
1348 crt->rsa.len = mpi_size( &crt->rsa.N );
1349
1350 /*
1351 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1352 * -- If present, version shall be v2 or v3
1353 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1354 * -- If present, version shall be v2 or v3
1355 * extensions [3] EXPLICIT Extensions OPTIONAL
1356 * -- If present, version shall be v3
1357 */
1358 if( crt->version == 2 || crt->version == 3 )
1359 {
1360 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
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 == 2 || crt->version == 3 )
1369 {
1370 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1371 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( crt->version == 3 )
1379 {
Paul Bakker74111d32011-01-15 16:57:55 +00001380 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 if( ret != 0 )
1382 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001383 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001384 return( ret );
1385 }
1386 }
1387
1388 if( p != end )
1389 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001390 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001391 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001392 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
1394
Paul Bakkerb00ca422012-09-25 12:10:00 +00001395 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
1397 /*
1398 * signatureAlgorithm AlgorithmIdentifier,
1399 * signatureValue BIT STRING
1400 */
1401 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1402 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001403 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 return( ret );
1405 }
1406
Paul Bakker535e97d2012-08-23 10:49:55 +00001407 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1408 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001410 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001411 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
1413
1414 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1415 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001416 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 return( ret );
1418 }
1419
1420 if( p != end )
1421 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001422 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001423 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001424 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 }
1426
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427 return( 0 );
1428}
1429
1430/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001431 * Parse one X.509 certificate in DER format from a buffer and add them to a
1432 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001433 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001434int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001435{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001436 int ret;
1437 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001438
1439 /*
1440 * Check for valid input
1441 */
1442 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001443 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001444
1445 while( crt->version != 0 && crt->next != NULL )
1446 {
1447 prev = crt;
1448 crt = crt->next;
1449 }
1450
1451 /*
1452 * Add new certificate on the end of the chain if needed.
1453 */
1454 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001455 {
1456 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1457
Paul Bakker7d06ad22009-05-02 15:53:56 +00001458 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001459 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001460
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001461 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001462 crt = crt->next;
1463 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001465
Paul Bakkerd6d41092013-06-13 09:00:25 +02001466 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1467 {
1468 if( prev )
1469 prev->next = NULL;
1470
1471 if( crt != chain )
1472 free( crt );
1473
1474 return( ret );
1475 }
1476
1477 return( 0 );
1478}
1479
1480/*
1481 * Parse one or more PEM certificates from a buffer and add them to the chained list
1482 */
1483int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1484{
1485 int ret, success = 0, first_error = 0, total_failed = 0;
1486 int buf_format = X509_FORMAT_DER;
1487
1488 /*
1489 * Check for valid input
1490 */
1491 if( chain == NULL || buf == NULL )
1492 return( POLARSSL_ERR_X509_INVALID_INPUT );
1493
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001494 /*
1495 * Determine buffer content. Buffer contains either one DER certificate or
1496 * one or more PEM certificates.
1497 */
1498#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001499 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001500 buf_format = X509_FORMAT_PEM;
1501#endif
1502
1503 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001504 return x509parse_crt_der( chain, buf, buflen );
1505
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001506#if defined(POLARSSL_PEM_C)
1507 if( buf_format == X509_FORMAT_PEM )
1508 {
1509 pem_context pem;
1510
1511 while( buflen > 0 )
1512 {
1513 size_t use_len;
1514 pem_init( &pem );
1515
1516 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001517 (char *) "-----BEGIN CERTIFICATE-----",
1518 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001519 buf, NULL, 0, &use_len );
1520
1521 if( ret == 0 )
1522 {
1523 /*
1524 * Was PEM encoded
1525 */
1526 buflen -= use_len;
1527 buf += use_len;
1528 }
Paul Bakker64171862013-06-06 15:01:18 +02001529 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1530 {
1531 return( ret );
1532 }
Paul Bakker9255e832013-06-06 14:58:28 +02001533 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001534 {
1535 pem_free( &pem );
1536
Paul Bakker64171862013-06-06 15:01:18 +02001537 /*
1538 * PEM header and footer were found
1539 */
1540 buflen -= use_len;
1541 buf += use_len;
1542
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001543 if( first_error == 0 )
1544 first_error = ret;
1545
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001546 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001547 continue;
1548 }
1549 else
1550 break;
1551
Paul Bakkerd6d41092013-06-13 09:00:25 +02001552 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001553
1554 pem_free( &pem );
1555
1556 if( ret != 0 )
1557 {
1558 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001559 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001560 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001561 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001562 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001563
1564 if( first_error == 0 )
1565 first_error = ret;
1566
Paul Bakkerd6d41092013-06-13 09:00:25 +02001567 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001568 continue;
1569 }
1570
1571 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001572 }
1573 }
1574#endif
1575
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001576 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001577 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001578 else if( first_error )
1579 return( first_error );
1580 else
1581 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582}
1583
1584/*
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001585 * Parse one CRLs in DER format and append it to the chained list
Paul Bakkerd98030e2009-05-02 15:13:40 +00001586 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001587static int x509_crl_parse_der( x509_crl *chain,
1588 const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589{
Paul Bakker23986e52011-04-24 08:57:21 +00001590 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001591 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001592 unsigned char *p, *end;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001593 x509_buf sig_params1, sig_params2;
1594 x509_crl *crl = chain;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001595
1596 /*
1597 * Check for valid input
1598 */
1599 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001600 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001602 memset( &sig_params1, 0, sizeof( x509_buf ) );
1603 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001604
1605 /*
1606 * Add new CRL on the end of the chain if needed.
1607 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001608 while( crl->version != 0 && crl->next != NULL )
1609 crl = crl->next;
1610
1611 if( crl->version != 0 && crl->next == NULL )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 {
1613 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1614
Paul Bakker7d06ad22009-05-02 15:53:56 +00001615 if( crl->next == NULL )
1616 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001617 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001618 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001619 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001620
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001621 memset( crl->next, 0, sizeof( x509_crl ) );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001622 crl = crl->next;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623 }
1624
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001625 /*
1626 * Copy raw DER-encoded CRL
1627 */
1628 if( ( p = malloc( buflen ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001629 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001630
1631 memcpy( p, buf, buflen );
1632
Paul Bakkerd98030e2009-05-02 15:13:40 +00001633 crl->raw.p = p;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001634 crl->raw.len = buflen;
1635
1636 end = p + buflen;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001637
1638 /*
1639 * CertificateList ::= SEQUENCE {
1640 * tbsCertList TBSCertList,
1641 * signatureAlgorithm AlgorithmIdentifier,
1642 * signatureValue BIT STRING }
1643 */
1644 if( ( ret = asn1_get_tag( &p, end, &len,
1645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1646 {
1647 x509_crl_free( crl );
1648 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1649 }
1650
Paul Bakker23986e52011-04-24 08:57:21 +00001651 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652 {
1653 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001654 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001655 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1656 }
1657
1658 /*
1659 * TBSCertList ::= SEQUENCE {
1660 */
1661 crl->tbs.p = p;
1662
1663 if( ( ret = asn1_get_tag( &p, end, &len,
1664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1665 {
1666 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001667 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668 }
1669
1670 end = p + len;
1671 crl->tbs.len = end - crl->tbs.p;
1672
1673 /*
1674 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1675 * -- if present, MUST be v2
1676 *
1677 * signature AlgorithmIdentifier
1678 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001679 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1681 {
1682 x509_crl_free( crl );
1683 return( ret );
1684 }
1685
1686 crl->version++;
1687
1688 if( crl->version > 2 )
1689 {
1690 x509_crl_free( crl );
1691 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1692 }
1693
Paul Bakker27d66162010-03-17 06:56:01 +00001694 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001695 {
1696 x509_crl_free( crl );
1697 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1698 }
1699
1700 /*
1701 * issuer Name
1702 */
1703 crl->issuer_raw.p = p;
1704
1705 if( ( ret = asn1_get_tag( &p, end, &len,
1706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1707 {
1708 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001709 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001710 }
1711
1712 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1713 {
1714 x509_crl_free( crl );
1715 return( ret );
1716 }
1717
1718 crl->issuer_raw.len = p - crl->issuer_raw.p;
1719
1720 /*
1721 * thisUpdate Time
1722 * nextUpdate Time OPTIONAL
1723 */
Paul Bakker91200182010-02-18 21:26:15 +00001724 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 {
1726 x509_crl_free( crl );
1727 return( ret );
1728 }
1729
Paul Bakker91200182010-02-18 21:26:15 +00001730 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001731 {
Paul Bakker9d781402011-05-09 16:17:09 +00001732 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001733 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001734 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001735 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001736 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001737 x509_crl_free( crl );
1738 return( ret );
1739 }
1740 }
1741
1742 /*
1743 * revokedCertificates SEQUENCE OF SEQUENCE {
1744 * userCertificate CertificateSerialNumber,
1745 * revocationDate Time,
1746 * crlEntryExtensions Extensions OPTIONAL
1747 * -- if present, MUST be v2
1748 * } OPTIONAL
1749 */
1750 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1751 {
1752 x509_crl_free( crl );
1753 return( ret );
1754 }
1755
1756 /*
1757 * crlExtensions EXPLICIT Extensions OPTIONAL
1758 * -- if present, MUST be v2
1759 */
1760 if( crl->version == 2 )
1761 {
1762 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1763
1764 if( ret != 0 )
1765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769 }
1770
1771 if( p != end )
1772 {
1773 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001774 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001775 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1776 }
1777
1778 end = crl->raw.p + crl->raw.len;
1779
1780 /*
1781 * signatureAlgorithm AlgorithmIdentifier,
1782 * signatureValue BIT STRING
1783 */
1784 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1785 {
1786 x509_crl_free( crl );
1787 return( ret );
1788 }
1789
Paul Bakker535e97d2012-08-23 10:49:55 +00001790 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1791 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792 {
1793 x509_crl_free( crl );
1794 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1795 }
1796
1797 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1798 {
1799 x509_crl_free( crl );
1800 return( ret );
1801 }
1802
1803 if( p != end )
1804 {
1805 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001806 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1808 }
1809
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 return( 0 );
1811}
1812
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001813/*
1814 * Parse one or more CRLs and add them to the chained list
1815 */
1816int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
1817{
1818#if defined(POLARSSL_PEM_C)
1819 int ret;
1820 size_t use_len;
1821 pem_context pem;
1822 int is_pem = 0;
1823
1824 if( chain == NULL || buf == NULL )
1825 return( POLARSSL_ERR_X509_INVALID_INPUT );
1826
1827 do
1828 {
1829 pem_init( &pem );
1830 ret = pem_read_buffer( &pem,
1831 (char *) "-----BEGIN X509 CRL-----",
1832 (char *) "-----END X509 CRL-----",
1833 buf, NULL, 0, &use_len );
1834
1835 if( ret == 0 )
1836 {
1837 /*
1838 * Was PEM encoded
1839 */
1840 is_pem = 1;
1841
1842 buflen -= use_len;
1843 buf += use_len;
1844
1845 if( ( ret = x509_crl_parse_der( chain,
1846 pem.buf, pem.buflen ) ) != 0 )
1847 {
1848 return( ret );
1849 }
1850
1851 pem_free( &pem );
1852 }
1853 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1854 {
1855 pem_free( &pem );
1856 return( ret );
1857 }
1858 }
1859 while( is_pem && buflen > 0 );
1860
1861 if( is_pem )
1862 return( 0 );
1863 else
1864#endif /* POLARSSL_PEM_C */
1865 return( x509_crl_parse_der( chain, buf, buflen ) );
1866}
1867
Paul Bakker335db3f2011-04-25 15:28:35 +00001868#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001870 * Load all data from a file into a given buffer.
1871 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001872static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001875 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001878 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001879
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001881 if( ( size = ftell( f ) ) == -1 )
1882 {
1883 fclose( f );
1884 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1885 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001886 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001887
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001888 *n = (size_t) size;
1889
1890 if( *n + 1 == 0 ||
1891 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1892 {
1893 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001894 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001895 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001896
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897 if( fread( *buf, 1, *n, f ) != *n )
1898 {
1899 fclose( f );
1900 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001901 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001903
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001905
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001907
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001909}
1910
1911/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 * Load one or more certificates and add them to the chained list
1913 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001914int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001915{
1916 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 size_t n;
1918 unsigned char *buf;
1919
Paul Bakker69e095c2011-12-10 21:55:01 +00001920 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1921 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Paul Bakker69e095c2011-12-10 21:55:01 +00001923 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 memset( buf, 0, n + 1 );
1926 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
1928 return( ret );
1929}
1930
Paul Bakker8d914582012-06-04 12:46:42 +00001931int x509parse_crtpath( x509_cert *chain, const char *path )
1932{
1933 int ret = 0;
1934#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001935 int w_ret;
1936 WCHAR szDir[MAX_PATH];
1937 char filename[MAX_PATH];
1938 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001939 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001940
Paul Bakker97872ac2012-11-02 12:53:26 +00001941 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001942 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001943
1944 if( len > MAX_PATH - 3 )
1945 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001946
Paul Bakker3338b792012-10-01 21:13:10 +00001947 memset( szDir, 0, sizeof(szDir) );
1948 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001949 memcpy( filename, path, len );
1950 filename[len++] = '\\';
1951 p = filename + len;
1952 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001953
Paul Bakker40cc9142014-07-07 15:16:47 +02001954 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001955
Paul Bakker97872ac2012-11-02 12:53:26 +00001956 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001957 if (hFind == INVALID_HANDLE_VALUE)
1958 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1959
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001960 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001961 do
1962 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001963 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001964
Paul Bakkere4791f32012-06-04 21:29:15 +00001965 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001966 continue;
1967
Paul Bakker3338b792012-10-01 21:13:10 +00001968 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1969 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001970 p, len - 1,
1971 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001972
Paul Bakker3338b792012-10-01 21:13:10 +00001973 w_ret = x509parse_crtfile( chain, filename );
1974 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001975 ret++;
1976 else
1977 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001978 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001979 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001980
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001981 if (GetLastError() != ERROR_NO_MORE_FILES)
1982 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001983
1984 FindClose( hFind );
1985#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001986#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001987 int t_ret, i;
1988 struct stat sb;
1989 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001990 char entry_name[255];
1991 DIR *dir = opendir( path );
1992
1993 if( dir == NULL)
1994 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1995
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001996 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001997 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001998 if( result == NULL )
1999 break;
2000
2001 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2002
2003 i = stat( entry_name, &sb );
2004
2005 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002006 {
2007 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002008 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002009 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002010
2011 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002012 continue;
2013
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002014 // Ignore parse errors
2015 //
Paul Bakker8d914582012-06-04 12:46:42 +00002016 t_ret = x509parse_crtfile( chain, entry_name );
2017 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002018 ret++;
2019 else
2020 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002021 }
2022 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002023#else /* POLARSSL_HAVE_READDIR_R */
2024 ((void) chain);
2025 ((void) path);
2026 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2027#endif /* POLARSSL_HAVE_READDIR_R */
2028#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002029
2030 return( ret );
2031}
2032
Paul Bakkerd98030e2009-05-02 15:13:40 +00002033/*
2034 * Load one or more CRLs and add them to the chained list
2035 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002036int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002037{
2038 int ret;
2039 size_t n;
2040 unsigned char *buf;
2041
Paul Bakker69e095c2011-12-10 21:55:01 +00002042 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2043 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002044
Paul Bakker27fdf462011-06-09 13:55:13 +00002045 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002046
2047 memset( buf, 0, n + 1 );
2048 free( buf );
2049
2050 return( ret );
2051}
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002054 * Load and parse a private RSA key
2055 */
2056int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2057{
2058 int ret;
2059 size_t n;
2060 unsigned char *buf;
2061
Paul Bakker69e095c2011-12-10 21:55:01 +00002062 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2063 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002064
2065 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002066 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002067 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002068 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002069 (unsigned char *) pwd, strlen( pwd ) );
2070
2071 memset( buf, 0, n + 1 );
2072 free( buf );
2073
2074 return( ret );
2075}
2076
2077/*
2078 * Load and parse a public RSA key
2079 */
2080int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2081{
2082 int ret;
2083 size_t n;
2084 unsigned char *buf;
2085
Paul Bakker69e095c2011-12-10 21:55:01 +00002086 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2087 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002088
Paul Bakker27fdf462011-06-09 13:55:13 +00002089 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002090
2091 memset( buf, 0, n + 1 );
2092 free( buf );
2093
2094 return( ret );
2095}
2096#endif /* POLARSSL_FS_IO */
2097
2098/*
Paul Bakker65a19092013-06-06 21:14:58 +02002099 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 */
Paul Bakker65a19092013-06-06 21:14:58 +02002101static int x509parse_key_pkcs1_der( rsa_context *rsa,
2102 const unsigned char *key,
2103 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104{
Paul Bakker23986e52011-04-24 08:57:21 +00002105 int ret;
2106 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002108
Paul Bakker96743fc2011-02-12 14:30:57 +00002109 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002110 end = p + keylen;
2111
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002113 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002114 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 * RSAPrivateKey ::= SEQUENCE {
2116 * version Version,
2117 * modulus INTEGER, -- n
2118 * publicExponent INTEGER, -- e
2119 * privateExponent INTEGER, -- d
2120 * prime1 INTEGER, -- p
2121 * prime2 INTEGER, -- q
2122 * exponent1 INTEGER, -- d mod (p-1)
2123 * exponent2 INTEGER, -- d mod (q-1)
2124 * coefficient INTEGER, -- (inverse of q) mod p
2125 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2126 * }
2127 */
2128 if( ( ret = asn1_get_tag( &p, end, &len,
2129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2130 {
Paul Bakker9d781402011-05-09 16:17:09 +00002131 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
2134 end = p + len;
2135
2136 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2137 {
Paul Bakker9d781402011-05-09 16:17:09 +00002138 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
2140
2141 if( rsa->ver != 0 )
2142 {
Paul Bakker9d781402011-05-09 16:17:09 +00002143 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145
2146 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2147 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2148 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2149 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2150 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2151 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2152 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2153 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2154 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002156 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
2159 rsa->len = mpi_size( &rsa->N );
2160
2161 if( p != end )
2162 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002164 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002165 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
2168 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2169 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 rsa_free( rsa );
2171 return( ret );
2172 }
2173
Paul Bakker65a19092013-06-06 21:14:58 +02002174 return( 0 );
2175}
2176
2177/*
2178 * Parse an unencrypted PKCS#8 encoded private RSA key
2179 */
2180static int x509parse_key_pkcs8_unencrypted_der(
2181 rsa_context *rsa,
2182 const unsigned char *key,
2183 size_t keylen )
2184{
2185 int ret;
2186 size_t len;
2187 unsigned char *p, *end;
2188 x509_buf pk_alg_oid;
2189
2190 p = (unsigned char *) key;
2191 end = p + keylen;
2192
2193 /*
2194 * This function parses the PrivatKeyInfo object (PKCS#8)
2195 *
2196 * PrivateKeyInfo ::= SEQUENCE {
2197 * version Version,
2198 * algorithm AlgorithmIdentifier,
2199 * PrivateKey BIT STRING
2200 * }
2201 *
2202 * AlgorithmIdentifier ::= SEQUENCE {
2203 * algorithm OBJECT IDENTIFIER,
2204 * parameters ANY DEFINED BY algorithm OPTIONAL
2205 * }
2206 *
2207 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2208 */
2209 if( ( ret = asn1_get_tag( &p, end, &len,
2210 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2211 {
2212 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2213 }
2214
2215 end = p + len;
2216
2217 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2218 {
2219 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2220 }
2221
2222 if( rsa->ver != 0 )
2223 {
2224 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2225 }
2226
2227 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2228 {
2229 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2230 }
2231
2232 /*
2233 * only RSA keys handled at this time
2234 */
2235 if( pk_alg_oid.len != 9 ||
2236 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2237 {
2238 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2239 }
2240
2241 /*
2242 * Get the OCTET STRING and parse the PKCS#1 format inside
2243 */
2244 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2246
2247 if( ( end - p ) < 1 )
2248 {
2249 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2250 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2251 }
2252
2253 end = p + len;
2254
2255 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2256 return( ret );
2257
2258 return( 0 );
2259}
2260
2261/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002262 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002263 */
2264static int x509parse_key_pkcs8_encrypted_der(
2265 rsa_context *rsa,
2266 const unsigned char *key,
2267 size_t keylen,
2268 const unsigned char *pwd,
2269 size_t pwdlen )
2270{
2271 int ret;
2272 size_t len;
2273 unsigned char *p, *end, *end2;
2274 x509_buf pbe_alg_oid, pbe_params;
2275 unsigned char buf[2048];
2276
2277 memset(buf, 0, 2048);
2278
2279 p = (unsigned char *) key;
2280 end = p + keylen;
2281
Paul Bakker1fd43212013-06-17 15:14:42 +02002282 if( pwdlen == 0 )
2283 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2284
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002285 /*
2286 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2287 *
2288 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2289 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2290 * encryptedData EncryptedData
2291 * }
2292 *
2293 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2294 *
2295 * EncryptedData ::= OCTET STRING
2296 *
2297 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2298 */
2299 if( ( ret = asn1_get_tag( &p, end, &len,
2300 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2301 {
2302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2303 }
2304
2305 end = p + len;
2306
2307 if( ( ret = asn1_get_tag( &p, end, &len,
2308 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2309 {
2310 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2311 }
2312
2313 end2 = p + len;
2314
2315 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2316 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2317
2318 pbe_alg_oid.p = p;
2319 p += pbe_alg_oid.len;
2320
2321 /*
2322 * Store the algorithm parameters
2323 */
2324 pbe_params.p = p;
2325 pbe_params.len = end2 - p;
2326 p += pbe_params.len;
2327
2328 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2329 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2330
2331 // buf has been sized to 2048 bytes
2332 if( len > 2048 )
2333 return( POLARSSL_ERR_X509_INVALID_INPUT );
2334
2335 /*
2336 * Decrypt EncryptedData with appropriate PDE
2337 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002338#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002339 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2340 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002341 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2342 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2343 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002344 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002345 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2346 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2347
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002348 return( ret );
2349 }
2350 }
2351 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2352 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002353 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2354 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2355 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002356 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002357 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2358 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2359
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002360 return( ret );
2361 }
2362 }
2363 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2364 {
2365 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2366 PKCS12_PBE_DECRYPT,
2367 pwd, pwdlen,
2368 p, len, buf ) ) != 0 )
2369 {
2370 return( ret );
2371 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002372
2373 // Best guess for password mismatch when using RC4. If first tag is
2374 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2375 //
2376 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2377 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002378 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002379 else
2380#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002381#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002382 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002383 {
2384 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2385 p, len, buf ) ) != 0 )
2386 {
2387 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2388 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2389
2390 return( ret );
2391 }
2392 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002393 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002394#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002395 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2396
2397 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2398}
2399
2400/*
Paul Bakker65a19092013-06-06 21:14:58 +02002401 * Parse a private RSA key
2402 */
2403int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2404 const unsigned char *pwd, size_t pwdlen )
2405{
2406 int ret;
2407
Paul Bakker96743fc2011-02-12 14:30:57 +00002408#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002409 size_t len;
2410 pem_context pem;
2411
2412 pem_init( &pem );
2413 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002414 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2415 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002416 key, pwd, pwdlen, &len );
2417 if( ret == 0 )
2418 {
2419 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2420 {
2421 rsa_free( rsa );
2422 }
2423
2424 pem_free( &pem );
2425 return( ret );
2426 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002427 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2428 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2429 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2430 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002431 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002432 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002433
2434 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002435 (char *) "-----BEGIN PRIVATE KEY-----",
2436 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002437 key, NULL, 0, &len );
2438 if( ret == 0 )
2439 {
2440 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2441 pem.buf, pem.buflen ) ) != 0 )
2442 {
2443 rsa_free( rsa );
2444 }
2445
2446 pem_free( &pem );
2447 return( ret );
2448 }
2449 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002450 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002451
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002452 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002453 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2454 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002455 key, NULL, 0, &len );
2456 if( ret == 0 )
2457 {
2458 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2459 pem.buf, pem.buflen,
2460 pwd, pwdlen ) ) != 0 )
2461 {
2462 rsa_free( rsa );
2463 }
2464
2465 pem_free( &pem );
2466 return( ret );
2467 }
2468 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002469 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002470#else
2471 ((void) pwd);
2472 ((void) pwdlen);
2473#endif /* POLARSSL_PEM_C */
2474
2475 // At this point we only know it's not a PEM formatted key. Could be any
2476 // of the known DER encoded private key formats
2477 //
2478 // We try the different DER format parsers to see if one passes without
2479 // error
2480 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002481 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2482 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002483 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002484 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002485 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002487 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002488
2489 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2490 {
2491 return( ret );
2492 }
2493
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002494 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2495 return( 0 );
2496
2497 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002498
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002499 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2500 return( 0 );
2501
2502 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002503
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002504 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505}
2506
2507/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002508 * Parse a public RSA key
2509 */
Paul Bakker23986e52011-04-24 08:57:21 +00002510int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002511{
Paul Bakker23986e52011-04-24 08:57:21 +00002512 int ret;
2513 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002514 unsigned char *p, *end;
2515 x509_buf alg_oid;
2516#if defined(POLARSSL_PEM_C)
2517 pem_context pem;
2518
2519 pem_init( &pem );
2520 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002521 (char *) "-----BEGIN PUBLIC KEY-----",
2522 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002523 key, NULL, 0, &len );
2524
2525 if( ret == 0 )
2526 {
2527 /*
2528 * Was PEM encoded
2529 */
2530 keylen = pem.buflen;
2531 }
Paul Bakker9255e832013-06-06 14:58:28 +02002532 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002533 {
2534 pem_free( &pem );
2535 return( ret );
2536 }
2537
2538 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2539#else
2540 p = (unsigned char *) key;
2541#endif
2542 end = p + keylen;
2543
2544 /*
2545 * PublicKeyInfo ::= SEQUENCE {
2546 * algorithm AlgorithmIdentifier,
2547 * PublicKey BIT STRING
2548 * }
2549 *
2550 * AlgorithmIdentifier ::= SEQUENCE {
2551 * algorithm OBJECT IDENTIFIER,
2552 * parameters ANY DEFINED BY algorithm OPTIONAL
2553 * }
2554 *
2555 * RSAPublicKey ::= SEQUENCE {
2556 * modulus INTEGER, -- n
2557 * publicExponent INTEGER -- e
2558 * }
2559 */
2560
2561 if( ( ret = asn1_get_tag( &p, end, &len,
2562 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2563 {
2564#if defined(POLARSSL_PEM_C)
2565 pem_free( &pem );
2566#endif
2567 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002568 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002569 }
2570
2571 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2572 {
2573#if defined(POLARSSL_PEM_C)
2574 pem_free( &pem );
2575#endif
2576 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002577 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002578 }
2579
2580 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2581 {
2582#if defined(POLARSSL_PEM_C)
2583 pem_free( &pem );
2584#endif
2585 rsa_free( rsa );
2586 return( ret );
2587 }
2588
2589 rsa->len = mpi_size( &rsa->N );
2590
2591#if defined(POLARSSL_PEM_C)
2592 pem_free( &pem );
2593#endif
2594
2595 return( 0 );
2596}
2597
Paul Bakkereaa89f82011-04-04 21:36:15 +00002598#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002599/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002600 * Parse DHM parameters
2601 */
Paul Bakker23986e52011-04-24 08:57:21 +00002602int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002603{
Paul Bakker23986e52011-04-24 08:57:21 +00002604 int ret;
2605 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002606 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002607#if defined(POLARSSL_PEM_C)
2608 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002609
Paul Bakker96743fc2011-02-12 14:30:57 +00002610 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002611
Paul Bakker96743fc2011-02-12 14:30:57 +00002612 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002613 (char *) "-----BEGIN DH PARAMETERS-----",
2614 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002615 dhmin, NULL, 0, &dhminlen );
2616
2617 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002618 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002619 /*
2620 * Was PEM encoded
2621 */
2622 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002623 }
Paul Bakker9255e832013-06-06 14:58:28 +02002624 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002625 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002626 pem_free( &pem );
2627 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002628 }
2629
Paul Bakker96743fc2011-02-12 14:30:57 +00002630 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2631#else
2632 p = (unsigned char *) dhmin;
2633#endif
2634 end = p + dhminlen;
2635
Paul Bakker1b57b062011-01-06 15:48:19 +00002636 memset( dhm, 0, sizeof( dhm_context ) );
2637
Paul Bakker1b57b062011-01-06 15:48:19 +00002638 /*
2639 * DHParams ::= SEQUENCE {
2640 * prime INTEGER, -- P
2641 * generator INTEGER, -- g
2642 * }
2643 */
2644 if( ( ret = asn1_get_tag( &p, end, &len,
2645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2646 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002647#if defined(POLARSSL_PEM_C)
2648 pem_free( &pem );
2649#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002650 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002651 }
2652
2653 end = p + len;
2654
2655 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2656 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2657 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002658#if defined(POLARSSL_PEM_C)
2659 pem_free( &pem );
2660#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002661 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002663 }
2664
2665 if( p != end )
2666 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002667#if defined(POLARSSL_PEM_C)
2668 pem_free( &pem );
2669#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002670 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002671 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002672 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2673 }
2674
Paul Bakker96743fc2011-02-12 14:30:57 +00002675#if defined(POLARSSL_PEM_C)
2676 pem_free( &pem );
2677#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002678
2679 return( 0 );
2680}
2681
Paul Bakker335db3f2011-04-25 15:28:35 +00002682#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002683/*
2684 * Load and parse a private RSA key
2685 */
2686int x509parse_dhmfile( dhm_context *dhm, const char *path )
2687{
2688 int ret;
2689 size_t n;
2690 unsigned char *buf;
2691
Paul Bakker69e095c2011-12-10 21:55:01 +00002692 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2693 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002694
Paul Bakker27fdf462011-06-09 13:55:13 +00002695 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002696
2697 memset( buf, 0, n + 1 );
2698 free( buf );
2699
2700 return( ret );
2701}
Paul Bakker335db3f2011-04-25 15:28:35 +00002702#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002703#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706#include <stdarg.h>
2707
2708#if !defined vsnprintf
2709#define vsnprintf _vsnprintf
2710#endif // vsnprintf
2711
2712/*
2713 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2714 * Result value is not size of buffer needed, but -1 if no fit is possible.
2715 *
2716 * This fuction tries to 'fix' this by at least suggesting enlarging the
2717 * size by 20.
2718 */
2719int compat_snprintf(char *str, size_t size, const char *format, ...)
2720{
2721 va_list ap;
2722 int res = -1;
2723
2724 va_start( ap, format );
2725
2726 res = vsnprintf( str, size, format, ap );
2727
2728 va_end( ap );
2729
2730 // No quick fix possible
2731 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002732 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002733
2734 return res;
2735}
2736
2737#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002738#endif
2739
Paul Bakkerd98030e2009-05-02 15:13:40 +00002740#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2741
2742#define SAFE_SNPRINTF() \
2743{ \
2744 if( ret == -1 ) \
2745 return( -1 ); \
2746 \
Paul Bakker23986e52011-04-24 08:57:21 +00002747 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748 p[n - 1] = '\0'; \
2749 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2750 } \
2751 \
Paul Bakker23986e52011-04-24 08:57:21 +00002752 n -= (unsigned int) ret; \
2753 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754}
2755
Paul Bakker5121ce52009-01-03 21:22:43 +00002756/*
2757 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002758 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002760int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002761{
Paul Bakker23986e52011-04-24 08:57:21 +00002762 int ret;
2763 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002765 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 char s[128], *p;
2767
2768 memset( s, 0, sizeof( s ) );
2769
2770 name = dn;
2771 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
2774 while( name != NULL )
2775 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002776 if( !name->oid.p )
2777 {
2778 name = name->next;
2779 continue;
2780 }
2781
Paul Bakker74111d32011-01-15 16:57:55 +00002782 if( name != dn )
2783 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002784 ret = snprintf( p, n, ", " );
2785 SAFE_SNPRINTF();
2786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Paul Bakker535e97d2012-08-23 10:49:55 +00002788 if( name->oid.len == 3 &&
2789 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 {
2791 switch( name->oid.p[2] )
2792 {
2793 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
2796 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002797 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
2799 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
2802 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002803 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002806 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 name->oid.p[2] );
2814 break;
2815 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002816 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002818 else if( name->oid.len == 9 &&
2819 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 {
2821 switch( name->oid.p[8] )
2822 {
2823 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002824 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
2826 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 name->oid.p[8] );
2829 break;
2830 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002831 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 }
2833 else
Paul Bakker74111d32011-01-15 16:57:55 +00002834 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002835 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002836 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839 for( i = 0; i < name->val.len; i++ )
2840 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002841 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 break;
2843
2844 c = name->val.p[i];
2845 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2846 s[i] = '?';
2847 else s[i] = c;
2848 }
2849 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850 ret = snprintf( p, n, "%s", s );
2851 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 name = name->next;
2853 }
2854
Paul Bakker23986e52011-04-24 08:57:21 +00002855 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856}
2857
2858/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002859 * Store the serial in printable form into buf; no more
2860 * than size characters will be written
2861 */
2862int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2863{
Paul Bakker23986e52011-04-24 08:57:21 +00002864 int ret;
2865 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002866 char *p;
2867
2868 p = buf;
2869 n = size;
2870
2871 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002872 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002873
2874 for( i = 0; i < nr; i++ )
2875 {
Paul Bakker93048802011-12-05 14:38:06 +00002876 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002877 continue;
2878
Paul Bakkerdd476992011-01-16 21:34:59 +00002879 ret = snprintf( p, n, "%02X%s",
2880 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2881 SAFE_SNPRINTF();
2882 }
2883
Paul Bakker03c7c252011-11-25 12:37:37 +00002884 if( nr != serial->len )
2885 {
2886 ret = snprintf( p, n, "...." );
2887 SAFE_SNPRINTF();
2888 }
2889
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002891}
2892
2893/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002894 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002896int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2897 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898{
Paul Bakker23986e52011-04-24 08:57:21 +00002899 int ret;
2900 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002901 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
2903 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakkerd98030e2009-05-02 15:13:40 +00002906 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002908 SAFE_SNPRINTF();
2909 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002911 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakkerdd476992011-01-16 21:34:59 +00002913 ret = x509parse_serial_gets( p, n, &crt->serial);
2914 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakkerd98030e2009-05-02 15:13:40 +00002916 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2917 SAFE_SNPRINTF();
2918 ret = x509parse_dn_gets( p, n, &crt->issuer );
2919 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakkerd98030e2009-05-02 15:13:40 +00002921 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2922 SAFE_SNPRINTF();
2923 ret = x509parse_dn_gets( p, n, &crt->subject );
2924 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakkerd98030e2009-05-02 15:13:40 +00002926 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2928 crt->valid_from.year, crt->valid_from.mon,
2929 crt->valid_from.day, crt->valid_from.hour,
2930 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002931 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Paul Bakkerd98030e2009-05-02 15:13:40 +00002933 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2935 crt->valid_to.year, crt->valid_to.mon,
2936 crt->valid_to.day, crt->valid_to.hour,
2937 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002938 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakkerd98030e2009-05-02 15:13:40 +00002940 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2941 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Paul Bakker27d66162010-03-17 06:56:01 +00002943 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002945 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2946 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2947 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2948 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2949 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2950 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2951 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2952 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2953 default: ret = snprintf( p, n, "???" ); break;
2954 }
2955 SAFE_SNPRINTF();
2956
2957 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002958 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002959 SAFE_SNPRINTF();
2960
Paul Bakker23986e52011-04-24 08:57:21 +00002961 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002962}
2963
Paul Bakker74111d32011-01-15 16:57:55 +00002964/*
2965 * Return an informational string describing the given OID
2966 */
2967const char *x509_oid_get_description( x509_buf *oid )
2968{
2969 if ( oid == NULL )
2970 return ( NULL );
2971
2972 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2973 return( STRING_SERVER_AUTH );
2974
2975 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2976 return( STRING_CLIENT_AUTH );
2977
2978 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2979 return( STRING_CODE_SIGNING );
2980
2981 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2982 return( STRING_EMAIL_PROTECTION );
2983
2984 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2985 return( STRING_TIME_STAMPING );
2986
2987 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2988 return( STRING_OCSP_SIGNING );
2989
2990 return( NULL );
2991}
2992
2993/* Return the x.y.z.... style numeric string for the given OID */
2994int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2995{
Paul Bakker23986e52011-04-24 08:57:21 +00002996 int ret;
2997 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002998 unsigned int value;
2999 char *p;
3000
3001 p = buf;
3002 n = size;
3003
3004 /* First byte contains first two dots */
3005 if( oid->len > 0 )
3006 {
3007 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3008 SAFE_SNPRINTF();
3009 }
3010
3011 /* TODO: value can overflow in value. */
3012 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003013 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003014 {
3015 value <<= 7;
3016 value += oid->p[i] & 0x7F;
3017
3018 if( !( oid->p[i] & 0x80 ) )
3019 {
3020 /* Last byte */
3021 ret = snprintf( p, n, ".%d", value );
3022 SAFE_SNPRINTF();
3023 value = 0;
3024 }
3025 }
3026
Paul Bakker23986e52011-04-24 08:57:21 +00003027 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003028}
3029
Paul Bakkerd98030e2009-05-02 15:13:40 +00003030/*
3031 * Return an informational string about the CRL.
3032 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003033int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3034 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003035{
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003037 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003038 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003039 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040
3041 p = buf;
3042 n = size;
3043
3044 ret = snprintf( p, n, "%sCRL version : %d",
3045 prefix, crl->version );
3046 SAFE_SNPRINTF();
3047
3048 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3049 SAFE_SNPRINTF();
3050 ret = x509parse_dn_gets( p, n, &crl->issuer );
3051 SAFE_SNPRINTF();
3052
3053 ret = snprintf( p, n, "\n%sthis update : " \
3054 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3055 crl->this_update.year, crl->this_update.mon,
3056 crl->this_update.day, crl->this_update.hour,
3057 crl->this_update.min, crl->this_update.sec );
3058 SAFE_SNPRINTF();
3059
3060 ret = snprintf( p, n, "\n%snext update : " \
3061 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3062 crl->next_update.year, crl->next_update.mon,
3063 crl->next_update.day, crl->next_update.hour,
3064 crl->next_update.min, crl->next_update.sec );
3065 SAFE_SNPRINTF();
3066
3067 entry = &crl->entry;
3068
3069 ret = snprintf( p, n, "\n%sRevoked certificates:",
3070 prefix );
3071 SAFE_SNPRINTF();
3072
Paul Bakker9be19372009-07-27 20:21:53 +00003073 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003074 {
3075 ret = snprintf( p, n, "\n%sserial number: ",
3076 prefix );
3077 SAFE_SNPRINTF();
3078
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003079 ret = x509parse_serial_gets( p, n, &entry->serial);
3080 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003081
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082 ret = snprintf( p, n, " revocation date: " \
3083 "%04d-%02d-%02d %02d:%02d:%02d",
3084 entry->revocation_date.year, entry->revocation_date.mon,
3085 entry->revocation_date.day, entry->revocation_date.hour,
3086 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003087 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003088
3089 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
3091
Paul Bakkerd98030e2009-05-02 15:13:40 +00003092 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3093 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Paul Bakker27d66162010-03-17 06:56:01 +00003095 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003096 {
3097 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3098 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3099 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3100 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3101 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3102 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3103 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3104 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3105 default: ret = snprintf( p, n, "???" ); break;
3106 }
3107 SAFE_SNPRINTF();
3108
Paul Bakker1e27bb22009-07-19 20:25:25 +00003109 ret = snprintf( p, n, "\n" );
3110 SAFE_SNPRINTF();
3111
Paul Bakker23986e52011-04-24 08:57:21 +00003112 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113}
3114
3115/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003116 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003118static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003119{
Paul Bakkercce9d772011-11-18 14:26:47 +00003120#if defined(_WIN32)
3121 SYSTEMTIME st;
3122
Paul Bakkerf48de952014-07-08 14:39:41 +02003123 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003124
Paul Bakker0d844dd2014-07-07 17:44:14 +02003125 now->year = st.wYear;
3126 now->mon = st.wMonth;
3127 now->day = st.wDay;
3128 now->hour = st.wHour;
3129 now->min = st.wMinute;
3130 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003131#else
Paul Bakker358a8412014-07-08 12:14:37 +02003132 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 time_t tt;
3134
3135 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003136 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
Paul Bakker358a8412014-07-08 12:14:37 +02003138 now->year = lt.tm_year + 1900;
3139 now->mon = lt.tm_mon + 1;
3140 now->day = lt.tm_mday;
3141 now->hour = lt.tm_hour;
3142 now->min = lt.tm_min;
3143 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003144#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003145}
Paul Bakkercce9d772011-11-18 14:26:47 +00003146
Paul Bakker0d844dd2014-07-07 17:44:14 +02003147/*
3148 * Return 0 if before <= after, 1 otherwise
3149 */
3150static int x509_check_time( const x509_time *before, const x509_time *after )
3151{
3152 if( before->year > after->year )
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 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003157 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker0d844dd2014-07-07 17:44:14 +02003159 if( before->year == after->year &&
3160 before->mon == after->mon &&
3161 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003162 return( 1 );
3163
Paul Bakker0d844dd2014-07-07 17:44:14 +02003164 if( before->year == after->year &&
3165 before->mon == after->mon &&
3166 before->day == after->day &&
3167 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003168 return( 1 );
3169
Paul Bakker0d844dd2014-07-07 17:44:14 +02003170 if( before->year == after->year &&
3171 before->mon == after->mon &&
3172 before->day == after->day &&
3173 before->hour == after->hour &&
3174 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003175 return( 1 );
3176
Paul Bakker0d844dd2014-07-07 17:44:14 +02003177 if( before->year == after->year &&
3178 before->mon == after->mon &&
3179 before->day == after->day &&
3180 before->hour == after->hour &&
3181 before->min == after->min &&
3182 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003183 return( 1 );
3184
Paul Bakker40ea7de2009-05-03 10:18:48 +00003185 return( 0 );
3186}
3187
Paul Bakker0d844dd2014-07-07 17:44:14 +02003188int x509parse_time_expired( const x509_time *to )
3189{
3190 x509_time now;
3191
3192 x509_get_current_time( &now );
3193
3194 return( x509_check_time( &now, to ) );
3195}
3196
3197int x509parse_time_future( const x509_time *from )
3198{
3199 x509_time now;
3200
3201 x509_get_current_time( &now );
3202
3203 return( x509_check_time( from, &now ) );
3204}
3205
Paul Bakker40ea7de2009-05-03 10:18:48 +00003206/*
3207 * Return 1 if the certificate is revoked, or 0 otherwise.
3208 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003209int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003210{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003211 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003212
3213 while( cur != NULL && cur->serial.len != 0 )
3214 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003215 if( crt->serial.len == cur->serial.len &&
3216 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003217 {
3218 if( x509parse_time_expired( &cur->revocation_date ) )
3219 return( 1 );
3220 }
3221
3222 cur = cur->next;
3223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 return( 0 );
3226}
3227
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003228/*
3229 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003230 */
Paul Bakker23986e52011-04-24 08:57:21 +00003231static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 unsigned char *out )
3233{
3234 switch( alg )
3235 {
Paul Bakker40e46942009-01-03 21:51:57 +00003236#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003237 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003239#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003240 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003241#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003242#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003243 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003244#endif
3245#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003246 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003247#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003248#if defined(POLARSSL_SHA2_C)
3249 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3250 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3251#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003252#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003253 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3254 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3255#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003257 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 break;
3259 }
3260}
3261
3262/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003263 * Check that the given certificate is valid accoring to the CRL.
3264 */
3265static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3266 x509_crl *crl_list)
3267{
3268 int flags = 0;
3269 int hash_id;
3270 unsigned char hash[64];
3271
Paul Bakker915275b2012-09-28 07:10:55 +00003272 if( ca == NULL )
3273 return( flags );
3274
Paul Bakker76fd75a2011-01-16 21:12:10 +00003275 /*
3276 * TODO: What happens if no CRL is present?
3277 * Suggestion: Revocation state should be unknown if no CRL is present.
3278 * For backwards compatibility this is not yet implemented.
3279 */
3280
Paul Bakker915275b2012-09-28 07:10:55 +00003281 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003282 {
Paul Bakker915275b2012-09-28 07:10:55 +00003283 if( crl_list->version == 0 ||
3284 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003285 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3286 crl_list->issuer_raw.len ) != 0 )
3287 {
3288 crl_list = crl_list->next;
3289 continue;
3290 }
3291
3292 /*
3293 * Check if CRL is correctly signed by the trusted CA
3294 */
3295 hash_id = crl_list->sig_alg;
3296
3297 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3298
Paul Bakker43f97992013-09-23 11:23:31 +02003299 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003300 0, hash, crl_list->sig.p ) == 0 )
3301 {
3302 /*
3303 * CRL is not trusted
3304 */
3305 flags |= BADCRL_NOT_TRUSTED;
3306 break;
3307 }
3308
3309 /*
3310 * Check for validity of CRL (Do not drop out)
3311 */
3312 if( x509parse_time_expired( &crl_list->next_update ) )
3313 flags |= BADCRL_EXPIRED;
3314
Paul Bakker50a5c532014-07-08 10:59:10 +02003315 if( x509parse_time_future( &crl_list->this_update ) )
3316 flags |= BADCRL_FUTURE;
3317
Paul Bakker76fd75a2011-01-16 21:12:10 +00003318 /*
3319 * Check if certificate is revoked
3320 */
3321 if( x509parse_revoked(crt, crl_list) )
3322 {
3323 flags |= BADCERT_REVOKED;
3324 break;
3325 }
3326
3327 crl_list = crl_list->next;
3328 }
3329 return flags;
3330}
3331
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003332// Equal == 0, inequal == 1
3333static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3334{
3335 size_t i;
3336 unsigned char diff;
3337 const unsigned char *n1 = s1, *n2 = s2;
3338
3339 for( i = 0; i < len; i++ )
3340 {
3341 diff = n1[i] ^ n2[i];
3342
Paul Bakkerc941adb2014-07-07 14:17:24 +02003343 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003344 continue;
3345
Paul Bakkerc941adb2014-07-07 14:17:24 +02003346 if( diff == 32 &&
3347 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3348 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3349 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003350 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003351 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003352
3353 return( 1 );
3354 }
3355
3356 return( 0 );
3357}
3358
Paul Bakker1d073c52014-07-08 20:15:51 +02003359static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003360{
3361 size_t i;
3362 size_t cn_idx = 0;
3363
Paul Bakker57b12982012-02-11 17:38:38 +00003364 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003365 return( 0 );
3366
3367 for( i = 0; i < strlen( cn ); ++i )
3368 {
3369 if( cn[i] == '.' )
3370 {
3371 cn_idx = i;
3372 break;
3373 }
3374 }
3375
3376 if( cn_idx == 0 )
3377 return( 0 );
3378
Paul Bakker535e97d2012-08-23 10:49:55 +00003379 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003380 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003381 {
3382 return( 1 );
3383 }
3384
3385 return( 0 );
3386}
3387
Paul Bakker915275b2012-09-28 07:10:55 +00003388static int x509parse_verify_top(
3389 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003390 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003391 int (*f_vrfy)(void *, x509_cert *, int, int *),
3392 void *p_vrfy )
3393{
3394 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003395 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003396 unsigned char hash[64];
3397
3398 if( x509parse_time_expired( &child->valid_to ) )
3399 *flags |= BADCERT_EXPIRED;
3400
Paul Bakker50a5c532014-07-08 10:59:10 +02003401 if( x509parse_time_future( &child->valid_from ) )
3402 *flags |= BADCERT_FUTURE;
3403
Paul Bakker915275b2012-09-28 07:10:55 +00003404 /*
3405 * Child is the top of the chain. Check against the trust_ca list.
3406 */
3407 *flags |= BADCERT_NOT_TRUSTED;
3408
3409 while( trust_ca != NULL )
3410 {
3411 if( trust_ca->version == 0 ||
3412 child->issuer_raw.len != trust_ca->subject_raw.len ||
3413 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3414 child->issuer_raw.len ) != 0 )
3415 {
3416 trust_ca = trust_ca->next;
3417 continue;
3418 }
3419
Paul Bakker9a736322012-11-14 12:39:52 +00003420 /*
3421 * Reduce path_len to check against if top of the chain is
3422 * the same as the trusted CA
3423 */
3424 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3425 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3426 child->issuer_raw.len ) == 0 )
3427 {
3428 check_path_cnt--;
3429 }
3430
Paul Bakker915275b2012-09-28 07:10:55 +00003431 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003432 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003433 {
3434 trust_ca = trust_ca->next;
3435 continue;
3436 }
3437
3438 hash_id = child->sig_alg;
3439
3440 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3441
Paul Bakker43f97992013-09-23 11:23:31 +02003442 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003443 0, hash, child->sig.p ) != 0 )
3444 {
3445 trust_ca = trust_ca->next;
3446 continue;
3447 }
3448
3449 /*
3450 * Top of chain is signed by a trusted CA
3451 */
3452 *flags &= ~BADCERT_NOT_TRUSTED;
3453 break;
3454 }
3455
Paul Bakker9a736322012-11-14 12:39:52 +00003456 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003457 * If top of chain is not the same as the trusted CA send a verify request
3458 * to the callback for any issues with validity and CRL presence for the
3459 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003460 */
3461 if( trust_ca != NULL &&
3462 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3463 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3464 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003465 {
3466 /* Check trusted CA's CRL for then chain's top crt */
3467 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3468
3469 if( x509parse_time_expired( &trust_ca->valid_to ) )
3470 ca_flags |= BADCERT_EXPIRED;
3471
Paul Bakker50a5c532014-07-08 10:59:10 +02003472 if( x509parse_time_future( &trust_ca->valid_from ) )
3473 ca_flags |= BADCERT_FUTURE;
3474
Paul Bakker915275b2012-09-28 07:10:55 +00003475 if( NULL != f_vrfy )
3476 {
Paul Bakker9a736322012-11-14 12:39:52 +00003477 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003478 return( ret );
3479 }
3480 }
3481
3482 /* Call callback on top cert */
3483 if( NULL != f_vrfy )
3484 {
Paul Bakker9a736322012-11-14 12:39:52 +00003485 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003486 return( ret );
3487 }
3488
Paul Bakker915275b2012-09-28 07:10:55 +00003489 *flags |= ca_flags;
3490
3491 return( 0 );
3492}
3493
3494static int x509parse_verify_child(
3495 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003496 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003497 int (*f_vrfy)(void *, x509_cert *, int, int *),
3498 void *p_vrfy )
3499{
3500 int hash_id, ret;
3501 int parent_flags = 0;
3502 unsigned char hash[64];
3503 x509_cert *grandparent;
3504
Manuel Pégourié-Gonnard4cdb3ba2014-11-20 17:12:15 +01003505 /* path_cnt is 0 for the first intermediate CA */
3506 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
3507 {
3508 *flags |= BADCERT_NOT_TRUSTED;
3509 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
3510 }
3511
Paul Bakker915275b2012-09-28 07:10:55 +00003512 if( x509parse_time_expired( &child->valid_to ) )
3513 *flags |= BADCERT_EXPIRED;
3514
Paul Bakker50a5c532014-07-08 10:59:10 +02003515 if( x509parse_time_future( &child->valid_from ) )
3516 *flags |= BADCERT_FUTURE;
3517
Paul Bakker915275b2012-09-28 07:10:55 +00003518 hash_id = child->sig_alg;
3519
3520 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3521
Paul Bakker43f97992013-09-23 11:23:31 +02003522 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3523 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003524 *flags |= BADCERT_NOT_TRUSTED;
3525
3526 /* Check trusted CA's CRL for the given crt */
3527 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3528
3529 grandparent = parent->next;
3530
3531 while( grandparent != NULL )
3532 {
3533 if( grandparent->version == 0 ||
3534 grandparent->ca_istrue == 0 ||
3535 parent->issuer_raw.len != grandparent->subject_raw.len ||
3536 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3537 parent->issuer_raw.len ) != 0 )
3538 {
3539 grandparent = grandparent->next;
3540 continue;
3541 }
3542 break;
3543 }
3544
Paul Bakker915275b2012-09-28 07:10:55 +00003545 if( grandparent != NULL )
3546 {
3547 /*
3548 * Part of the chain
3549 */
Paul Bakker9a736322012-11-14 12:39:52 +00003550 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 +00003551 if( ret != 0 )
3552 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003553 }
Paul Bakker915275b2012-09-28 07:10:55 +00003554 else
3555 {
Paul Bakker9a736322012-11-14 12:39:52 +00003556 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 +00003557 if( ret != 0 )
3558 return( ret );
3559 }
3560
3561 /* child is verified to be a child of the parent, call verify callback */
3562 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003563 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003564 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003565
3566 *flags |= parent_flags;
3567
3568 return( 0 );
3569}
3570
Paul Bakker76fd75a2011-01-16 21:12:10 +00003571/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 * Verify the certificate validity
3573 */
3574int x509parse_verify( x509_cert *crt,
3575 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003576 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003577 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003578 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003579 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003580{
Paul Bakker23986e52011-04-24 08:57:21 +00003581 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003582 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003583 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003584 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003586 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Paul Bakker40ea7de2009-05-03 10:18:48 +00003588 *flags = 0;
3589
Paul Bakker5121ce52009-01-03 21:22:43 +00003590 if( cn != NULL )
3591 {
3592 name = &crt->subject;
3593 cn_len = strlen( cn );
3594
Paul Bakker4d2c1242012-05-10 14:12:46 +00003595 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003597 cur = &crt->subject_alt_names;
3598
3599 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003600 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003601 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003602 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003603 break;
3604
Paul Bakker535e97d2012-08-23 10:49:55 +00003605 if( cur->buf.len > 2 &&
3606 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003607 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003608 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Paul Bakker4d2c1242012-05-10 14:12:46 +00003610 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003611 }
3612
3613 if( cur == NULL )
3614 *flags |= BADCERT_CN_MISMATCH;
3615 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003616 else
3617 {
3618 while( name != NULL )
3619 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003620 if( name->oid.len == 3 &&
3621 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003622 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003623 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003624 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003625 break;
3626
Paul Bakker535e97d2012-08-23 10:49:55 +00003627 if( name->val.len > 2 &&
3628 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003629 x509_wildcard_verify( cn, &name->val ) )
3630 break;
3631 }
3632
3633 name = name->next;
3634 }
3635
3636 if( name == NULL )
3637 *flags |= BADCERT_CN_MISMATCH;
3638 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 }
3640
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003642 * Iterate upwards in the given cert chain, to find our crt parent.
3643 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003645 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
Paul Bakker76fd75a2011-01-16 21:12:10 +00003647 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003649 if( parent->ca_istrue == 0 ||
3650 crt->issuer_raw.len != parent->subject_raw.len ||
3651 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 crt->issuer_raw.len ) != 0 )
3653 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003654 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 continue;
3656 }
Paul Bakker915275b2012-09-28 07:10:55 +00003657 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 }
3659
Paul Bakker915275b2012-09-28 07:10:55 +00003660 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 {
Paul Bakker915275b2012-09-28 07:10:55 +00003662 /*
3663 * Part of the chain
3664 */
Paul Bakker9a736322012-11-14 12:39:52 +00003665 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003666 if( ret != 0 )
3667 return( ret );
3668 }
3669 else
Paul Bakker74111d32011-01-15 16:57:55 +00003670 {
Paul Bakker9a736322012-11-14 12:39:52 +00003671 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003672 if( ret != 0 )
3673 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003674 }
Paul Bakker915275b2012-09-28 07:10:55 +00003675
3676 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003677 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003678
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 return( 0 );
3680}
3681
3682/*
3683 * Unallocate all certificate data
3684 */
3685void x509_free( x509_cert *crt )
3686{
3687 x509_cert *cert_cur = crt;
3688 x509_cert *cert_prv;
3689 x509_name *name_cur;
3690 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003691 x509_sequence *seq_cur;
3692 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
3694 if( crt == NULL )
3695 return;
3696
3697 do
3698 {
3699 rsa_free( &cert_cur->rsa );
3700
3701 name_cur = cert_cur->issuer.next;
3702 while( name_cur != NULL )
3703 {
3704 name_prv = name_cur;
3705 name_cur = name_cur->next;
3706 memset( name_prv, 0, sizeof( x509_name ) );
3707 free( name_prv );
3708 }
3709
3710 name_cur = cert_cur->subject.next;
3711 while( name_cur != NULL )
3712 {
3713 name_prv = name_cur;
3714 name_cur = name_cur->next;
3715 memset( name_prv, 0, sizeof( x509_name ) );
3716 free( name_prv );
3717 }
3718
Paul Bakker74111d32011-01-15 16:57:55 +00003719 seq_cur = cert_cur->ext_key_usage.next;
3720 while( seq_cur != NULL )
3721 {
3722 seq_prv = seq_cur;
3723 seq_cur = seq_cur->next;
3724 memset( seq_prv, 0, sizeof( x509_sequence ) );
3725 free( seq_prv );
3726 }
3727
Paul Bakker8afa70d2012-02-11 18:42:45 +00003728 seq_cur = cert_cur->subject_alt_names.next;
3729 while( seq_cur != NULL )
3730 {
3731 seq_prv = seq_cur;
3732 seq_cur = seq_cur->next;
3733 memset( seq_prv, 0, sizeof( x509_sequence ) );
3734 free( seq_prv );
3735 }
3736
Paul Bakker5121ce52009-01-03 21:22:43 +00003737 if( cert_cur->raw.p != NULL )
3738 {
3739 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3740 free( cert_cur->raw.p );
3741 }
3742
3743 cert_cur = cert_cur->next;
3744 }
3745 while( cert_cur != NULL );
3746
3747 cert_cur = crt;
3748 do
3749 {
3750 cert_prv = cert_cur;
3751 cert_cur = cert_cur->next;
3752
3753 memset( cert_prv, 0, sizeof( x509_cert ) );
3754 if( cert_prv != crt )
3755 free( cert_prv );
3756 }
3757 while( cert_cur != NULL );
3758}
3759
Paul Bakkerd98030e2009-05-02 15:13:40 +00003760/*
3761 * Unallocate all CRL data
3762 */
3763void x509_crl_free( x509_crl *crl )
3764{
3765 x509_crl *crl_cur = crl;
3766 x509_crl *crl_prv;
3767 x509_name *name_cur;
3768 x509_name *name_prv;
3769 x509_crl_entry *entry_cur;
3770 x509_crl_entry *entry_prv;
3771
3772 if( crl == NULL )
3773 return;
3774
3775 do
3776 {
3777 name_cur = crl_cur->issuer.next;
3778 while( name_cur != NULL )
3779 {
3780 name_prv = name_cur;
3781 name_cur = name_cur->next;
3782 memset( name_prv, 0, sizeof( x509_name ) );
3783 free( name_prv );
3784 }
3785
3786 entry_cur = crl_cur->entry.next;
3787 while( entry_cur != NULL )
3788 {
3789 entry_prv = entry_cur;
3790 entry_cur = entry_cur->next;
3791 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3792 free( entry_prv );
3793 }
3794
3795 if( crl_cur->raw.p != NULL )
3796 {
3797 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3798 free( crl_cur->raw.p );
3799 }
3800
3801 crl_cur = crl_cur->next;
3802 }
3803 while( crl_cur != NULL );
3804
3805 crl_cur = crl;
3806 do
3807 {
3808 crl_prv = crl_cur;
3809 crl_cur = crl_cur->next;
3810
3811 memset( crl_prv, 0, sizeof( x509_crl ) );
3812 if( crl_prv != crl )
3813 free( crl_prv );
3814 }
3815 while( crl_cur != NULL );
3816}
3817
Paul Bakker40e46942009-01-03 21:51:57 +00003818#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003819
Paul Bakker40e46942009-01-03 21:51:57 +00003820#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003821
3822/*
3823 * Checkup routine
3824 */
3825int x509_self_test( int verbose )
3826{
Paul Bakker5690efc2011-05-26 13:16:06 +00003827#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003828 int ret;
3829 int flags;
3830 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 x509_cert cacert;
3832 x509_cert clicert;
3833 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003834#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003835 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003836#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
3838 if( verbose != 0 )
3839 printf( " X.509 certificate load: " );
3840
3841 memset( &clicert, 0, sizeof( x509_cert ) );
3842
Paul Bakkereae09db2013-06-06 12:35:54 +02003843 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003844 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 if( ret != 0 )
3846 {
3847 if( verbose != 0 )
3848 printf( "failed\n" );
3849
3850 return( ret );
3851 }
3852
3853 memset( &cacert, 0, sizeof( x509_cert ) );
3854
Paul Bakkereae09db2013-06-06 12:35:54 +02003855 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003856 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 if( ret != 0 )
3858 {
3859 if( verbose != 0 )
3860 printf( "failed\n" );
3861
3862 return( ret );
3863 }
3864
3865 if( verbose != 0 )
3866 printf( "passed\n X.509 private key load: " );
3867
3868 i = strlen( test_ca_key );
3869 j = strlen( test_ca_pwd );
3870
Paul Bakker66b78b22011-03-25 14:22:50 +00003871 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3872
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003874 (const unsigned char *) test_ca_key, i,
3875 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 {
3877 if( verbose != 0 )
3878 printf( "failed\n" );
3879
3880 return( ret );
3881 }
3882
3883 if( verbose != 0 )
3884 printf( "passed\n X.509 signature verify: ");
3885
Paul Bakker23986e52011-04-24 08:57:21 +00003886 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 if( ret != 0 )
3888 {
3889 if( verbose != 0 )
3890 printf( "failed\n" );
3891
3892 return( ret );
3893 }
3894
Paul Bakker5690efc2011-05-26 13:16:06 +00003895#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003897 printf( "passed\n X.509 DHM parameter load: " );
3898
3899 i = strlen( test_dhm_params );
3900 j = strlen( test_ca_pwd );
3901
Paul Bakkereae09db2013-06-06 12:35:54 +02003902 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003903 {
3904 if( verbose != 0 )
3905 printf( "failed\n" );
3906
3907 return( ret );
3908 }
3909
3910 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003912#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003913
3914 x509_free( &cacert );
3915 x509_free( &clicert );
3916 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003917#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003918 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003919#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003920
3921 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003922#else
3923 ((void) verbose);
3924 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3925#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003926}
3927
3928#endif
3929
3930#endif