blob: 488ae8c365ec00b9b145b4f4b0fbebd1ab141d08 [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/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 * Parse one or more CRLs and add them to the chained list
1586 */
Paul Bakker23986e52011-04-24 08:57:21 +00001587int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588{
Paul Bakker23986e52011-04-24 08:57:21 +00001589 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001590 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001591 unsigned char *p, *end;
1592 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001593#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001594 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001595 pem_context pem;
1596#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597
1598 crl = chain;
1599
1600 /*
1601 * Check for valid input
1602 */
1603 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001604 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001605
1606 while( crl->version != 0 && crl->next != NULL )
1607 crl = crl->next;
1608
1609 /*
1610 * Add new CRL on the end of the chain if needed.
1611 */
1612 if ( crl->version != 0 && crl->next == NULL)
1613 {
1614 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1615
Paul Bakker7d06ad22009-05-02 15:53:56 +00001616 if( crl->next == NULL )
1617 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001618 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001619 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001620 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001621
Paul Bakker7d06ad22009-05-02 15:53:56 +00001622 crl = crl->next;
1623 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001624 }
1625
Paul Bakker96743fc2011-02-12 14:30:57 +00001626#if defined(POLARSSL_PEM_C)
1627 pem_init( &pem );
1628 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001629 (char *) "-----BEGIN X509 CRL-----",
1630 (char *) "-----END X509 CRL-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00001631 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632
Paul Bakker96743fc2011-02-12 14:30:57 +00001633 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001635 /*
1636 * Was PEM encoded
1637 */
1638 buflen -= use_len;
1639 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001640
1641 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001642 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001644 p = pem.buf;
1645 pem.buf = NULL;
1646 len = pem.buflen;
1647 pem_free( &pem );
1648 }
Paul Bakker9255e832013-06-06 14:58:28 +02001649 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001650 {
1651 pem_free( &pem );
1652 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653 }
1654 else
1655 {
1656 /*
1657 * nope, copy the raw DER data
1658 */
1659 p = (unsigned char *) malloc( len = buflen );
1660
1661 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001662 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001663
1664 memcpy( p, buf, buflen );
1665
1666 buflen = 0;
1667 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001668#else
1669 p = (unsigned char *) malloc( len = buflen );
1670
1671 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001672 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001673
1674 memcpy( p, buf, buflen );
1675
1676 buflen = 0;
1677#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001678
1679 crl->raw.p = p;
1680 crl->raw.len = len;
1681 end = p + len;
1682
1683 /*
1684 * CertificateList ::= SEQUENCE {
1685 * tbsCertList TBSCertList,
1686 * signatureAlgorithm AlgorithmIdentifier,
1687 * signatureValue BIT STRING }
1688 */
1689 if( ( ret = asn1_get_tag( &p, end, &len,
1690 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1691 {
1692 x509_crl_free( crl );
1693 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1694 }
1695
Paul Bakker23986e52011-04-24 08:57:21 +00001696 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001697 {
1698 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001699 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001700 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1701 }
1702
1703 /*
1704 * TBSCertList ::= SEQUENCE {
1705 */
1706 crl->tbs.p = p;
1707
1708 if( ( ret = asn1_get_tag( &p, end, &len,
1709 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1710 {
1711 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001712 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001713 }
1714
1715 end = p + len;
1716 crl->tbs.len = end - crl->tbs.p;
1717
1718 /*
1719 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1720 * -- if present, MUST be v2
1721 *
1722 * signature AlgorithmIdentifier
1723 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001724 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001725 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1726 {
1727 x509_crl_free( crl );
1728 return( ret );
1729 }
1730
1731 crl->version++;
1732
1733 if( crl->version > 2 )
1734 {
1735 x509_crl_free( crl );
1736 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1737 }
1738
Paul Bakker27d66162010-03-17 06:56:01 +00001739 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001740 {
1741 x509_crl_free( crl );
1742 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1743 }
1744
1745 /*
1746 * issuer Name
1747 */
1748 crl->issuer_raw.p = p;
1749
1750 if( ( ret = asn1_get_tag( &p, end, &len,
1751 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1752 {
1753 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001754 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 }
1756
1757 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1758 {
1759 x509_crl_free( crl );
1760 return( ret );
1761 }
1762
1763 crl->issuer_raw.len = p - crl->issuer_raw.p;
1764
1765 /*
1766 * thisUpdate Time
1767 * nextUpdate Time OPTIONAL
1768 */
Paul Bakker91200182010-02-18 21:26:15 +00001769 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001770 {
1771 x509_crl_free( crl );
1772 return( ret );
1773 }
1774
Paul Bakker91200182010-02-18 21:26:15 +00001775 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001776 {
Paul Bakker9d781402011-05-09 16:17:09 +00001777 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001778 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001779 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001780 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001781 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001782 x509_crl_free( crl );
1783 return( ret );
1784 }
1785 }
1786
1787 /*
1788 * revokedCertificates SEQUENCE OF SEQUENCE {
1789 * userCertificate CertificateSerialNumber,
1790 * revocationDate Time,
1791 * crlEntryExtensions Extensions OPTIONAL
1792 * -- if present, MUST be v2
1793 * } OPTIONAL
1794 */
1795 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1796 {
1797 x509_crl_free( crl );
1798 return( ret );
1799 }
1800
1801 /*
1802 * crlExtensions EXPLICIT Extensions OPTIONAL
1803 * -- if present, MUST be v2
1804 */
1805 if( crl->version == 2 )
1806 {
1807 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1808
1809 if( ret != 0 )
1810 {
1811 x509_crl_free( crl );
1812 return( ret );
1813 }
1814 }
1815
1816 if( p != end )
1817 {
1818 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001819 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1821 }
1822
1823 end = crl->raw.p + crl->raw.len;
1824
1825 /*
1826 * signatureAlgorithm AlgorithmIdentifier,
1827 * signatureValue BIT STRING
1828 */
1829 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1830 {
1831 x509_crl_free( crl );
1832 return( ret );
1833 }
1834
Paul Bakker535e97d2012-08-23 10:49:55 +00001835 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1836 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 {
1838 x509_crl_free( crl );
1839 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1840 }
1841
1842 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1843 {
1844 x509_crl_free( crl );
1845 return( ret );
1846 }
1847
1848 if( p != end )
1849 {
1850 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001851 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001852 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1853 }
1854
1855 if( buflen > 0 )
1856 {
1857 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1858
Paul Bakker7d06ad22009-05-02 15:53:56 +00001859 if( crl->next == NULL )
1860 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001861 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001862 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001863 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001864
Paul Bakker7d06ad22009-05-02 15:53:56 +00001865 crl = crl->next;
1866 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001867
1868 return( x509parse_crl( crl, buf, buflen ) );
1869 }
1870
1871 return( 0 );
1872}
1873
Paul Bakker335db3f2011-04-25 15:28:35 +00001874#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001875/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876 * Load all data from a file into a given buffer.
1877 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001878static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001879{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001880 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001881 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001882
Paul Bakkerd98030e2009-05-02 15:13:40 +00001883 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001884 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001885
Paul Bakkerd98030e2009-05-02 15:13:40 +00001886 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001887 if( ( size = ftell( f ) ) == -1 )
1888 {
1889 fclose( f );
1890 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1891 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001893
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001894 *n = (size_t) size;
1895
1896 if( *n + 1 == 0 ||
1897 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1898 {
1899 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001900 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001901 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001902
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903 if( fread( *buf, 1, *n, f ) != *n )
1904 {
1905 fclose( f );
1906 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001907 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001908 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001909
Paul Bakkerd98030e2009-05-02 15:13:40 +00001910 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001911
Paul Bakkerd98030e2009-05-02 15:13:40 +00001912 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001913
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915}
1916
1917/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 * Load one or more certificates and add them to the chained list
1919 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001920int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001921{
1922 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 size_t n;
1924 unsigned char *buf;
1925
Paul Bakker69e095c2011-12-10 21:55:01 +00001926 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1927 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Paul Bakker69e095c2011-12-10 21:55:01 +00001929 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
1931 memset( buf, 0, n + 1 );
1932 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
1934 return( ret );
1935}
1936
Paul Bakker8d914582012-06-04 12:46:42 +00001937int x509parse_crtpath( x509_cert *chain, const char *path )
1938{
1939 int ret = 0;
1940#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001941 int w_ret;
1942 WCHAR szDir[MAX_PATH];
1943 char filename[MAX_PATH];
1944 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001945 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001946
Paul Bakker97872ac2012-11-02 12:53:26 +00001947 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001948 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001949
1950 if( len > MAX_PATH - 3 )
1951 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001952
Paul Bakker3338b792012-10-01 21:13:10 +00001953 memset( szDir, 0, sizeof(szDir) );
1954 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001955 memcpy( filename, path, len );
1956 filename[len++] = '\\';
1957 p = filename + len;
1958 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001959
Paul Bakker40cc9142014-07-07 15:16:47 +02001960 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001961
Paul Bakker97872ac2012-11-02 12:53:26 +00001962 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001963 if (hFind == INVALID_HANDLE_VALUE)
1964 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1965
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001966 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001967 do
1968 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001969 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001970
Paul Bakkere4791f32012-06-04 21:29:15 +00001971 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001972 continue;
1973
Paul Bakker3338b792012-10-01 21:13:10 +00001974 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1975 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001976 p, len - 1,
1977 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001978
Paul Bakker3338b792012-10-01 21:13:10 +00001979 w_ret = x509parse_crtfile( chain, filename );
1980 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001981 ret++;
1982 else
1983 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001984 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001985 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001986
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001987 if (GetLastError() != ERROR_NO_MORE_FILES)
1988 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001989
1990 FindClose( hFind );
1991#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001992#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001993 int t_ret, i;
1994 struct stat sb;
1995 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001996 char entry_name[255];
1997 DIR *dir = opendir( path );
1998
1999 if( dir == NULL)
2000 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2001
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002002 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002003 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002004 if( result == NULL )
2005 break;
2006
2007 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2008
2009 i = stat( entry_name, &sb );
2010
2011 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002012 {
2013 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002014 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002015 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002016
2017 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002018 continue;
2019
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002020 // Ignore parse errors
2021 //
Paul Bakker8d914582012-06-04 12:46:42 +00002022 t_ret = x509parse_crtfile( chain, entry_name );
2023 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002024 ret++;
2025 else
2026 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002027 }
2028 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002029#else /* POLARSSL_HAVE_READDIR_R */
2030 ((void) chain);
2031 ((void) path);
2032 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2033#endif /* POLARSSL_HAVE_READDIR_R */
2034#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002035
2036 return( ret );
2037}
2038
Paul Bakkerd98030e2009-05-02 15:13:40 +00002039/*
2040 * Load one or more CRLs and add them to the chained list
2041 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002042int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002043{
2044 int ret;
2045 size_t n;
2046 unsigned char *buf;
2047
Paul Bakker69e095c2011-12-10 21:55:01 +00002048 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2049 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002050
Paul Bakker27fdf462011-06-09 13:55:13 +00002051 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002052
2053 memset( buf, 0, n + 1 );
2054 free( buf );
2055
2056 return( ret );
2057}
2058
Paul Bakker5121ce52009-01-03 21:22:43 +00002059/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002060 * Load and parse a private RSA key
2061 */
2062int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2063{
2064 int ret;
2065 size_t n;
2066 unsigned char *buf;
2067
Paul Bakker69e095c2011-12-10 21:55:01 +00002068 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2069 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002070
2071 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002072 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002073 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002074 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002075 (unsigned char *) pwd, strlen( pwd ) );
2076
2077 memset( buf, 0, n + 1 );
2078 free( buf );
2079
2080 return( ret );
2081}
2082
2083/*
2084 * Load and parse a public RSA key
2085 */
2086int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2087{
2088 int ret;
2089 size_t n;
2090 unsigned char *buf;
2091
Paul Bakker69e095c2011-12-10 21:55:01 +00002092 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2093 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002094
Paul Bakker27fdf462011-06-09 13:55:13 +00002095 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002096
2097 memset( buf, 0, n + 1 );
2098 free( buf );
2099
2100 return( ret );
2101}
2102#endif /* POLARSSL_FS_IO */
2103
2104/*
Paul Bakker65a19092013-06-06 21:14:58 +02002105 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 */
Paul Bakker65a19092013-06-06 21:14:58 +02002107static int x509parse_key_pkcs1_der( rsa_context *rsa,
2108 const unsigned char *key,
2109 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110{
Paul Bakker23986e52011-04-24 08:57:21 +00002111 int ret;
2112 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002114
Paul Bakker96743fc2011-02-12 14:30:57 +00002115 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002116 end = p + keylen;
2117
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002119 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002120 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 * RSAPrivateKey ::= SEQUENCE {
2122 * version Version,
2123 * modulus INTEGER, -- n
2124 * publicExponent INTEGER, -- e
2125 * privateExponent INTEGER, -- d
2126 * prime1 INTEGER, -- p
2127 * prime2 INTEGER, -- q
2128 * exponent1 INTEGER, -- d mod (p-1)
2129 * exponent2 INTEGER, -- d mod (q-1)
2130 * coefficient INTEGER, -- (inverse of q) mod p
2131 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2132 * }
2133 */
2134 if( ( ret = asn1_get_tag( &p, end, &len,
2135 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2136 {
Paul Bakker9d781402011-05-09 16:17:09 +00002137 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138 }
2139
2140 end = p + len;
2141
2142 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2143 {
Paul Bakker9d781402011-05-09 16:17:09 +00002144 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146
2147 if( rsa->ver != 0 )
2148 {
Paul Bakker9d781402011-05-09 16:17:09 +00002149 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 }
2151
2152 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2153 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2154 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2155 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2156 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2157 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2158 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2159 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2160 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002162 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
2165 rsa->len = mpi_size( &rsa->N );
2166
2167 if( p != end )
2168 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002170 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002171 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 }
2173
2174 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2175 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 rsa_free( rsa );
2177 return( ret );
2178 }
2179
Paul Bakker65a19092013-06-06 21:14:58 +02002180 return( 0 );
2181}
2182
2183/*
2184 * Parse an unencrypted PKCS#8 encoded private RSA key
2185 */
2186static int x509parse_key_pkcs8_unencrypted_der(
2187 rsa_context *rsa,
2188 const unsigned char *key,
2189 size_t keylen )
2190{
2191 int ret;
2192 size_t len;
2193 unsigned char *p, *end;
2194 x509_buf pk_alg_oid;
2195
2196 p = (unsigned char *) key;
2197 end = p + keylen;
2198
2199 /*
2200 * This function parses the PrivatKeyInfo object (PKCS#8)
2201 *
2202 * PrivateKeyInfo ::= SEQUENCE {
2203 * version Version,
2204 * algorithm AlgorithmIdentifier,
2205 * PrivateKey BIT STRING
2206 * }
2207 *
2208 * AlgorithmIdentifier ::= SEQUENCE {
2209 * algorithm OBJECT IDENTIFIER,
2210 * parameters ANY DEFINED BY algorithm OPTIONAL
2211 * }
2212 *
2213 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2214 */
2215 if( ( ret = asn1_get_tag( &p, end, &len,
2216 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2217 {
2218 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2219 }
2220
2221 end = p + len;
2222
2223 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2224 {
2225 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2226 }
2227
2228 if( rsa->ver != 0 )
2229 {
2230 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2231 }
2232
2233 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2234 {
2235 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2236 }
2237
2238 /*
2239 * only RSA keys handled at this time
2240 */
2241 if( pk_alg_oid.len != 9 ||
2242 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2243 {
2244 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2245 }
2246
2247 /*
2248 * Get the OCTET STRING and parse the PKCS#1 format inside
2249 */
2250 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2251 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2252
2253 if( ( end - p ) < 1 )
2254 {
2255 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2256 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2257 }
2258
2259 end = p + len;
2260
2261 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2262 return( ret );
2263
2264 return( 0 );
2265}
2266
2267/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002268 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002269 */
2270static int x509parse_key_pkcs8_encrypted_der(
2271 rsa_context *rsa,
2272 const unsigned char *key,
2273 size_t keylen,
2274 const unsigned char *pwd,
2275 size_t pwdlen )
2276{
2277 int ret;
2278 size_t len;
2279 unsigned char *p, *end, *end2;
2280 x509_buf pbe_alg_oid, pbe_params;
2281 unsigned char buf[2048];
2282
2283 memset(buf, 0, 2048);
2284
2285 p = (unsigned char *) key;
2286 end = p + keylen;
2287
Paul Bakker1fd43212013-06-17 15:14:42 +02002288 if( pwdlen == 0 )
2289 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2290
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002291 /*
2292 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2293 *
2294 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2295 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2296 * encryptedData EncryptedData
2297 * }
2298 *
2299 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2300 *
2301 * EncryptedData ::= OCTET STRING
2302 *
2303 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2304 */
2305 if( ( ret = asn1_get_tag( &p, end, &len,
2306 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2307 {
2308 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2309 }
2310
2311 end = p + len;
2312
2313 if( ( ret = asn1_get_tag( &p, end, &len,
2314 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2315 {
2316 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2317 }
2318
2319 end2 = p + len;
2320
2321 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2322 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2323
2324 pbe_alg_oid.p = p;
2325 p += pbe_alg_oid.len;
2326
2327 /*
2328 * Store the algorithm parameters
2329 */
2330 pbe_params.p = p;
2331 pbe_params.len = end2 - p;
2332 p += pbe_params.len;
2333
2334 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2335 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2336
2337 // buf has been sized to 2048 bytes
2338 if( len > 2048 )
2339 return( POLARSSL_ERR_X509_INVALID_INPUT );
2340
2341 /*
2342 * Decrypt EncryptedData with appropriate PDE
2343 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002344#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002345 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2346 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002347 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2348 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2349 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002350 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002351 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2352 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2353
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002354 return( ret );
2355 }
2356 }
2357 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2358 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002359 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2360 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2361 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002362 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002363 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2364 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2365
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002366 return( ret );
2367 }
2368 }
2369 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2370 {
2371 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2372 PKCS12_PBE_DECRYPT,
2373 pwd, pwdlen,
2374 p, len, buf ) ) != 0 )
2375 {
2376 return( ret );
2377 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002378
2379 // Best guess for password mismatch when using RC4. If first tag is
2380 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2381 //
2382 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2383 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002384 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002385 else
2386#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002387#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002388 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002389 {
2390 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2391 p, len, buf ) ) != 0 )
2392 {
2393 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2394 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2395
2396 return( ret );
2397 }
2398 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002399 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002400#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002401 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2402
2403 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2404}
2405
2406/*
Paul Bakker65a19092013-06-06 21:14:58 +02002407 * Parse a private RSA key
2408 */
2409int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2410 const unsigned char *pwd, size_t pwdlen )
2411{
2412 int ret;
2413
Paul Bakker96743fc2011-02-12 14:30:57 +00002414#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002415 size_t len;
2416 pem_context pem;
2417
2418 pem_init( &pem );
2419 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002420 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2421 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002422 key, pwd, pwdlen, &len );
2423 if( ret == 0 )
2424 {
2425 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2426 {
2427 rsa_free( rsa );
2428 }
2429
2430 pem_free( &pem );
2431 return( ret );
2432 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002433 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2434 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2435 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2436 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002437 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002438 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002439
2440 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002441 (char *) "-----BEGIN PRIVATE KEY-----",
2442 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002443 key, NULL, 0, &len );
2444 if( ret == 0 )
2445 {
2446 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2447 pem.buf, pem.buflen ) ) != 0 )
2448 {
2449 rsa_free( rsa );
2450 }
2451
2452 pem_free( &pem );
2453 return( ret );
2454 }
2455 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002456 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002457
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002458 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002459 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2460 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002461 key, NULL, 0, &len );
2462 if( ret == 0 )
2463 {
2464 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2465 pem.buf, pem.buflen,
2466 pwd, pwdlen ) ) != 0 )
2467 {
2468 rsa_free( rsa );
2469 }
2470
2471 pem_free( &pem );
2472 return( ret );
2473 }
2474 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002475 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002476#else
2477 ((void) pwd);
2478 ((void) pwdlen);
2479#endif /* POLARSSL_PEM_C */
2480
2481 // At this point we only know it's not a PEM formatted key. Could be any
2482 // of the known DER encoded private key formats
2483 //
2484 // We try the different DER format parsers to see if one passes without
2485 // error
2486 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002487 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2488 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002489 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002490 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002491 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002493 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002494
2495 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2496 {
2497 return( ret );
2498 }
2499
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002500 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2501 return( 0 );
2502
2503 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002504
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002505 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2506 return( 0 );
2507
2508 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002509
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002510 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511}
2512
2513/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002514 * Parse a public RSA key
2515 */
Paul Bakker23986e52011-04-24 08:57:21 +00002516int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002517{
Paul Bakker23986e52011-04-24 08:57:21 +00002518 int ret;
2519 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002520 unsigned char *p, *end;
2521 x509_buf alg_oid;
2522#if defined(POLARSSL_PEM_C)
2523 pem_context pem;
2524
2525 pem_init( &pem );
2526 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002527 (char *) "-----BEGIN PUBLIC KEY-----",
2528 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002529 key, NULL, 0, &len );
2530
2531 if( ret == 0 )
2532 {
2533 /*
2534 * Was PEM encoded
2535 */
2536 keylen = pem.buflen;
2537 }
Paul Bakker9255e832013-06-06 14:58:28 +02002538 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002539 {
2540 pem_free( &pem );
2541 return( ret );
2542 }
2543
2544 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2545#else
2546 p = (unsigned char *) key;
2547#endif
2548 end = p + keylen;
2549
2550 /*
2551 * PublicKeyInfo ::= SEQUENCE {
2552 * algorithm AlgorithmIdentifier,
2553 * PublicKey BIT STRING
2554 * }
2555 *
2556 * AlgorithmIdentifier ::= SEQUENCE {
2557 * algorithm OBJECT IDENTIFIER,
2558 * parameters ANY DEFINED BY algorithm OPTIONAL
2559 * }
2560 *
2561 * RSAPublicKey ::= SEQUENCE {
2562 * modulus INTEGER, -- n
2563 * publicExponent INTEGER -- e
2564 * }
2565 */
2566
2567 if( ( ret = asn1_get_tag( &p, end, &len,
2568 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2569 {
2570#if defined(POLARSSL_PEM_C)
2571 pem_free( &pem );
2572#endif
2573 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002574 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002575 }
2576
2577 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2578 {
2579#if defined(POLARSSL_PEM_C)
2580 pem_free( &pem );
2581#endif
2582 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002583 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002584 }
2585
2586 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2587 {
2588#if defined(POLARSSL_PEM_C)
2589 pem_free( &pem );
2590#endif
2591 rsa_free( rsa );
2592 return( ret );
2593 }
2594
2595 rsa->len = mpi_size( &rsa->N );
2596
2597#if defined(POLARSSL_PEM_C)
2598 pem_free( &pem );
2599#endif
2600
2601 return( 0 );
2602}
2603
Paul Bakkereaa89f82011-04-04 21:36:15 +00002604#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002605/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002606 * Parse DHM parameters
2607 */
Paul Bakker23986e52011-04-24 08:57:21 +00002608int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002609{
Paul Bakker23986e52011-04-24 08:57:21 +00002610 int ret;
2611 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002612 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002613#if defined(POLARSSL_PEM_C)
2614 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002615
Paul Bakker96743fc2011-02-12 14:30:57 +00002616 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002617
Paul Bakker96743fc2011-02-12 14:30:57 +00002618 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002619 (char *) "-----BEGIN DH PARAMETERS-----",
2620 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002621 dhmin, NULL, 0, &dhminlen );
2622
2623 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002624 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002625 /*
2626 * Was PEM encoded
2627 */
2628 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002629 }
Paul Bakker9255e832013-06-06 14:58:28 +02002630 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002631 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002632 pem_free( &pem );
2633 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002634 }
2635
Paul Bakker96743fc2011-02-12 14:30:57 +00002636 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2637#else
2638 p = (unsigned char *) dhmin;
2639#endif
2640 end = p + dhminlen;
2641
Paul Bakker1b57b062011-01-06 15:48:19 +00002642 memset( dhm, 0, sizeof( dhm_context ) );
2643
Paul Bakker1b57b062011-01-06 15:48:19 +00002644 /*
2645 * DHParams ::= SEQUENCE {
2646 * prime INTEGER, -- P
2647 * generator INTEGER, -- g
2648 * }
2649 */
2650 if( ( ret = asn1_get_tag( &p, end, &len,
2651 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2652 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002653#if defined(POLARSSL_PEM_C)
2654 pem_free( &pem );
2655#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002656 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002657 }
2658
2659 end = p + len;
2660
2661 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2662 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2663 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002664#if defined(POLARSSL_PEM_C)
2665 pem_free( &pem );
2666#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002667 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002668 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002669 }
2670
2671 if( p != end )
2672 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002673#if defined(POLARSSL_PEM_C)
2674 pem_free( &pem );
2675#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002676 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002677 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002678 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2679 }
2680
Paul Bakker96743fc2011-02-12 14:30:57 +00002681#if defined(POLARSSL_PEM_C)
2682 pem_free( &pem );
2683#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002684
2685 return( 0 );
2686}
2687
Paul Bakker335db3f2011-04-25 15:28:35 +00002688#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002689/*
2690 * Load and parse a private RSA key
2691 */
2692int x509parse_dhmfile( dhm_context *dhm, const char *path )
2693{
2694 int ret;
2695 size_t n;
2696 unsigned char *buf;
2697
Paul Bakker69e095c2011-12-10 21:55:01 +00002698 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2699 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002700
Paul Bakker27fdf462011-06-09 13:55:13 +00002701 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002702
2703 memset( buf, 0, n + 1 );
2704 free( buf );
2705
2706 return( ret );
2707}
Paul Bakker335db3f2011-04-25 15:28:35 +00002708#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002709#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002710
Paul Bakker5121ce52009-01-03 21:22:43 +00002711#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002712#include <stdarg.h>
2713
2714#if !defined vsnprintf
2715#define vsnprintf _vsnprintf
2716#endif // vsnprintf
2717
2718/*
2719 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2720 * Result value is not size of buffer needed, but -1 if no fit is possible.
2721 *
2722 * This fuction tries to 'fix' this by at least suggesting enlarging the
2723 * size by 20.
2724 */
2725int compat_snprintf(char *str, size_t size, const char *format, ...)
2726{
2727 va_list ap;
2728 int res = -1;
2729
2730 va_start( ap, format );
2731
2732 res = vsnprintf( str, size, format, ap );
2733
2734 va_end( ap );
2735
2736 // No quick fix possible
2737 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002738 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002739
2740 return res;
2741}
2742
2743#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002744#endif
2745
Paul Bakkerd98030e2009-05-02 15:13:40 +00002746#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2747
2748#define SAFE_SNPRINTF() \
2749{ \
2750 if( ret == -1 ) \
2751 return( -1 ); \
2752 \
Paul Bakker23986e52011-04-24 08:57:21 +00002753 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754 p[n - 1] = '\0'; \
2755 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2756 } \
2757 \
Paul Bakker23986e52011-04-24 08:57:21 +00002758 n -= (unsigned int) ret; \
2759 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002760}
2761
Paul Bakker5121ce52009-01-03 21:22:43 +00002762/*
2763 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002764 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002766int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002767{
Paul Bakker23986e52011-04-24 08:57:21 +00002768 int ret;
2769 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002771 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 char s[128], *p;
2773
2774 memset( s, 0, sizeof( s ) );
2775
2776 name = dn;
2777 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002778 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002779
2780 while( name != NULL )
2781 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002782 if( !name->oid.p )
2783 {
2784 name = name->next;
2785 continue;
2786 }
2787
Paul Bakker74111d32011-01-15 16:57:55 +00002788 if( name != dn )
2789 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002790 ret = snprintf( p, n, ", " );
2791 SAFE_SNPRINTF();
2792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakker535e97d2012-08-23 10:49:55 +00002794 if( name->oid.len == 3 &&
2795 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 {
2797 switch( name->oid.p[2] )
2798 {
2799 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
2802 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002803 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002806 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
2814 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002815 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816
2817 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002818 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 name->oid.p[2] );
2820 break;
2821 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002822 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002824 else if( name->oid.len == 9 &&
2825 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 {
2827 switch( name->oid.p[8] )
2828 {
2829 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002830 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
2832 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002833 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 name->oid.p[8] );
2835 break;
2836 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002837 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 }
2839 else
Paul Bakker74111d32011-01-15 16:57:55 +00002840 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002841 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002842 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
2845 for( i = 0; i < name->val.len; i++ )
2846 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002847 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 break;
2849
2850 c = name->val.p[i];
2851 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2852 s[i] = '?';
2853 else s[i] = c;
2854 }
2855 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002856 ret = snprintf( p, n, "%s", s );
2857 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 name = name->next;
2859 }
2860
Paul Bakker23986e52011-04-24 08:57:21 +00002861 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862}
2863
2864/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002865 * Store the serial in printable form into buf; no more
2866 * than size characters will be written
2867 */
2868int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2869{
Paul Bakker23986e52011-04-24 08:57:21 +00002870 int ret;
2871 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002872 char *p;
2873
2874 p = buf;
2875 n = size;
2876
2877 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002878 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002879
2880 for( i = 0; i < nr; i++ )
2881 {
Paul Bakker93048802011-12-05 14:38:06 +00002882 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002883 continue;
2884
Paul Bakkerdd476992011-01-16 21:34:59 +00002885 ret = snprintf( p, n, "%02X%s",
2886 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2887 SAFE_SNPRINTF();
2888 }
2889
Paul Bakker03c7c252011-11-25 12:37:37 +00002890 if( nr != serial->len )
2891 {
2892 ret = snprintf( p, n, "...." );
2893 SAFE_SNPRINTF();
2894 }
2895
Paul Bakker23986e52011-04-24 08:57:21 +00002896 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002897}
2898
2899/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002900 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002902int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2903 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002904{
Paul Bakker23986e52011-04-24 08:57:21 +00002905 int ret;
2906 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002907 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
2909 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002910 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002914 SAFE_SNPRINTF();
2915 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002917 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakkerdd476992011-01-16 21:34:59 +00002919 ret = x509parse_serial_gets( p, n, &crt->serial);
2920 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2923 SAFE_SNPRINTF();
2924 ret = x509parse_dn_gets( p, n, &crt->issuer );
2925 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2928 SAFE_SNPRINTF();
2929 ret = x509parse_dn_gets( p, n, &crt->subject );
2930 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Paul Bakkerd98030e2009-05-02 15:13:40 +00002932 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2934 crt->valid_from.year, crt->valid_from.mon,
2935 crt->valid_from.day, crt->valid_from.hour,
2936 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002937 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Paul Bakkerd98030e2009-05-02 15:13:40 +00002939 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2941 crt->valid_to.year, crt->valid_to.mon,
2942 crt->valid_to.day, crt->valid_to.hour,
2943 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002944 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2947 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker27d66162010-03-17 06:56:01 +00002949 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002951 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2952 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2953 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2954 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2955 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2956 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2957 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2958 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2959 default: ret = snprintf( p, n, "???" ); break;
2960 }
2961 SAFE_SNPRINTF();
2962
2963 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002964 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002965 SAFE_SNPRINTF();
2966
Paul Bakker23986e52011-04-24 08:57:21 +00002967 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002968}
2969
Paul Bakker74111d32011-01-15 16:57:55 +00002970/*
2971 * Return an informational string describing the given OID
2972 */
2973const char *x509_oid_get_description( x509_buf *oid )
2974{
2975 if ( oid == NULL )
2976 return ( NULL );
2977
2978 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2979 return( STRING_SERVER_AUTH );
2980
2981 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2982 return( STRING_CLIENT_AUTH );
2983
2984 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2985 return( STRING_CODE_SIGNING );
2986
2987 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2988 return( STRING_EMAIL_PROTECTION );
2989
2990 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2991 return( STRING_TIME_STAMPING );
2992
2993 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2994 return( STRING_OCSP_SIGNING );
2995
2996 return( NULL );
2997}
2998
2999/* Return the x.y.z.... style numeric string for the given OID */
3000int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
3001{
Paul Bakker23986e52011-04-24 08:57:21 +00003002 int ret;
3003 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00003004 unsigned int value;
3005 char *p;
3006
3007 p = buf;
3008 n = size;
3009
3010 /* First byte contains first two dots */
3011 if( oid->len > 0 )
3012 {
3013 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3014 SAFE_SNPRINTF();
3015 }
3016
3017 /* TODO: value can overflow in value. */
3018 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003019 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003020 {
3021 value <<= 7;
3022 value += oid->p[i] & 0x7F;
3023
3024 if( !( oid->p[i] & 0x80 ) )
3025 {
3026 /* Last byte */
3027 ret = snprintf( p, n, ".%d", value );
3028 SAFE_SNPRINTF();
3029 value = 0;
3030 }
3031 }
3032
Paul Bakker23986e52011-04-24 08:57:21 +00003033 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003034}
3035
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036/*
3037 * Return an informational string about the CRL.
3038 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003039int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3040 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041{
Paul Bakker23986e52011-04-24 08:57:21 +00003042 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003043 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003044 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003045 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003046
3047 p = buf;
3048 n = size;
3049
3050 ret = snprintf( p, n, "%sCRL version : %d",
3051 prefix, crl->version );
3052 SAFE_SNPRINTF();
3053
3054 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3055 SAFE_SNPRINTF();
3056 ret = x509parse_dn_gets( p, n, &crl->issuer );
3057 SAFE_SNPRINTF();
3058
3059 ret = snprintf( p, n, "\n%sthis update : " \
3060 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3061 crl->this_update.year, crl->this_update.mon,
3062 crl->this_update.day, crl->this_update.hour,
3063 crl->this_update.min, crl->this_update.sec );
3064 SAFE_SNPRINTF();
3065
3066 ret = snprintf( p, n, "\n%snext update : " \
3067 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3068 crl->next_update.year, crl->next_update.mon,
3069 crl->next_update.day, crl->next_update.hour,
3070 crl->next_update.min, crl->next_update.sec );
3071 SAFE_SNPRINTF();
3072
3073 entry = &crl->entry;
3074
3075 ret = snprintf( p, n, "\n%sRevoked certificates:",
3076 prefix );
3077 SAFE_SNPRINTF();
3078
Paul Bakker9be19372009-07-27 20:21:53 +00003079 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003080 {
3081 ret = snprintf( p, n, "\n%sserial number: ",
3082 prefix );
3083 SAFE_SNPRINTF();
3084
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003085 ret = x509parse_serial_gets( p, n, &entry->serial);
3086 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003087
Paul Bakkerd98030e2009-05-02 15:13:40 +00003088 ret = snprintf( p, n, " revocation date: " \
3089 "%04d-%02d-%02d %02d:%02d:%02d",
3090 entry->revocation_date.year, entry->revocation_date.mon,
3091 entry->revocation_date.day, entry->revocation_date.hour,
3092 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003093 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003094
3095 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 }
3097
Paul Bakkerd98030e2009-05-02 15:13:40 +00003098 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3099 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Paul Bakker27d66162010-03-17 06:56:01 +00003101 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003102 {
3103 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3104 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3105 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3106 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3107 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3108 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3109 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3110 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3111 default: ret = snprintf( p, n, "???" ); break;
3112 }
3113 SAFE_SNPRINTF();
3114
Paul Bakker1e27bb22009-07-19 20:25:25 +00003115 ret = snprintf( p, n, "\n" );
3116 SAFE_SNPRINTF();
3117
Paul Bakker23986e52011-04-24 08:57:21 +00003118 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003119}
3120
3121/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003122 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003124static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003125{
Paul Bakkercce9d772011-11-18 14:26:47 +00003126#if defined(_WIN32)
3127 SYSTEMTIME st;
3128
Paul Bakkerf48de952014-07-08 14:39:41 +02003129 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003130
Paul Bakker0d844dd2014-07-07 17:44:14 +02003131 now->year = st.wYear;
3132 now->mon = st.wMonth;
3133 now->day = st.wDay;
3134 now->hour = st.wHour;
3135 now->min = st.wMinute;
3136 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003137#else
Paul Bakker358a8412014-07-08 12:14:37 +02003138 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 time_t tt;
3140
3141 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003142 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
Paul Bakker358a8412014-07-08 12:14:37 +02003144 now->year = lt.tm_year + 1900;
3145 now->mon = lt.tm_mon + 1;
3146 now->day = lt.tm_mday;
3147 now->hour = lt.tm_hour;
3148 now->min = lt.tm_min;
3149 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003150#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003151}
Paul Bakkercce9d772011-11-18 14:26:47 +00003152
Paul Bakker0d844dd2014-07-07 17:44:14 +02003153/*
3154 * Return 0 if before <= after, 1 otherwise
3155 */
3156static int x509_check_time( const x509_time *before, const x509_time *after )
3157{
3158 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003159 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker0d844dd2014-07-07 17:44:14 +02003161 if( before->year == after->year &&
3162 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003163 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Paul Bakker0d844dd2014-07-07 17:44:14 +02003165 if( before->year == after->year &&
3166 before->mon == after->mon &&
3167 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +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 )
Paul Bakkerb6194992011-01-16 21:40:22 +00003174 return( 1 );
3175
Paul Bakker0d844dd2014-07-07 17:44:14 +02003176 if( before->year == after->year &&
3177 before->mon == after->mon &&
3178 before->day == after->day &&
3179 before->hour == after->hour &&
3180 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003181 return( 1 );
3182
Paul Bakker0d844dd2014-07-07 17:44:14 +02003183 if( before->year == after->year &&
3184 before->mon == after->mon &&
3185 before->day == after->day &&
3186 before->hour == after->hour &&
3187 before->min == after->min &&
3188 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003189 return( 1 );
3190
Paul Bakker40ea7de2009-05-03 10:18:48 +00003191 return( 0 );
3192}
3193
Paul Bakker0d844dd2014-07-07 17:44:14 +02003194int x509parse_time_expired( const x509_time *to )
3195{
3196 x509_time now;
3197
3198 x509_get_current_time( &now );
3199
3200 return( x509_check_time( &now, to ) );
3201}
3202
3203int x509parse_time_future( const x509_time *from )
3204{
3205 x509_time now;
3206
3207 x509_get_current_time( &now );
3208
3209 return( x509_check_time( from, &now ) );
3210}
3211
Paul Bakker40ea7de2009-05-03 10:18:48 +00003212/*
3213 * Return 1 if the certificate is revoked, or 0 otherwise.
3214 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003215int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003216{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003217 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003218
3219 while( cur != NULL && cur->serial.len != 0 )
3220 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003221 if( crt->serial.len == cur->serial.len &&
3222 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003223 {
3224 if( x509parse_time_expired( &cur->revocation_date ) )
3225 return( 1 );
3226 }
3227
3228 cur = cur->next;
3229 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
3231 return( 0 );
3232}
3233
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003234/*
3235 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003236 */
Paul Bakker23986e52011-04-24 08:57:21 +00003237static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 unsigned char *out )
3239{
3240 switch( alg )
3241 {
Paul Bakker40e46942009-01-03 21:51:57 +00003242#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003243 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003244#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003245#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003246 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003247#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003248#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003249 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003250#endif
3251#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003252 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003253#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003254#if defined(POLARSSL_SHA2_C)
3255 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3256 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3257#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003258#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003259 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3260 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003263 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 break;
3265 }
3266}
3267
3268/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003269 * Check that the given certificate is valid accoring to the CRL.
3270 */
3271static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3272 x509_crl *crl_list)
3273{
3274 int flags = 0;
3275 int hash_id;
3276 unsigned char hash[64];
3277
Paul Bakker915275b2012-09-28 07:10:55 +00003278 if( ca == NULL )
3279 return( flags );
3280
Paul Bakker76fd75a2011-01-16 21:12:10 +00003281 /*
3282 * TODO: What happens if no CRL is present?
3283 * Suggestion: Revocation state should be unknown if no CRL is present.
3284 * For backwards compatibility this is not yet implemented.
3285 */
3286
Paul Bakker915275b2012-09-28 07:10:55 +00003287 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003288 {
Paul Bakker915275b2012-09-28 07:10:55 +00003289 if( crl_list->version == 0 ||
3290 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003291 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3292 crl_list->issuer_raw.len ) != 0 )
3293 {
3294 crl_list = crl_list->next;
3295 continue;
3296 }
3297
3298 /*
3299 * Check if CRL is correctly signed by the trusted CA
3300 */
3301 hash_id = crl_list->sig_alg;
3302
3303 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3304
Paul Bakker43f97992013-09-23 11:23:31 +02003305 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003306 0, hash, crl_list->sig.p ) == 0 )
3307 {
3308 /*
3309 * CRL is not trusted
3310 */
3311 flags |= BADCRL_NOT_TRUSTED;
3312 break;
3313 }
3314
3315 /*
3316 * Check for validity of CRL (Do not drop out)
3317 */
3318 if( x509parse_time_expired( &crl_list->next_update ) )
3319 flags |= BADCRL_EXPIRED;
3320
Paul Bakker50a5c532014-07-08 10:59:10 +02003321 if( x509parse_time_future( &crl_list->this_update ) )
3322 flags |= BADCRL_FUTURE;
3323
Paul Bakker76fd75a2011-01-16 21:12:10 +00003324 /*
3325 * Check if certificate is revoked
3326 */
3327 if( x509parse_revoked(crt, crl_list) )
3328 {
3329 flags |= BADCERT_REVOKED;
3330 break;
3331 }
3332
3333 crl_list = crl_list->next;
3334 }
3335 return flags;
3336}
3337
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003338// Equal == 0, inequal == 1
3339static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3340{
3341 size_t i;
3342 unsigned char diff;
3343 const unsigned char *n1 = s1, *n2 = s2;
3344
3345 for( i = 0; i < len; i++ )
3346 {
3347 diff = n1[i] ^ n2[i];
3348
Paul Bakkerc941adb2014-07-07 14:17:24 +02003349 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003350 continue;
3351
Paul Bakkerc941adb2014-07-07 14:17:24 +02003352 if( diff == 32 &&
3353 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3354 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3355 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003356 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003357 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003358
3359 return( 1 );
3360 }
3361
3362 return( 0 );
3363}
3364
Paul Bakker1d073c52014-07-08 20:15:51 +02003365static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003366{
3367 size_t i;
3368 size_t cn_idx = 0;
3369
Paul Bakker57b12982012-02-11 17:38:38 +00003370 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003371 return( 0 );
3372
3373 for( i = 0; i < strlen( cn ); ++i )
3374 {
3375 if( cn[i] == '.' )
3376 {
3377 cn_idx = i;
3378 break;
3379 }
3380 }
3381
3382 if( cn_idx == 0 )
3383 return( 0 );
3384
Paul Bakker535e97d2012-08-23 10:49:55 +00003385 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003386 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003387 {
3388 return( 1 );
3389 }
3390
3391 return( 0 );
3392}
3393
Paul Bakker915275b2012-09-28 07:10:55 +00003394static int x509parse_verify_top(
3395 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003396 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003397 int (*f_vrfy)(void *, x509_cert *, int, int *),
3398 void *p_vrfy )
3399{
3400 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003401 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003402 unsigned char hash[64];
3403
3404 if( x509parse_time_expired( &child->valid_to ) )
3405 *flags |= BADCERT_EXPIRED;
3406
Paul Bakker50a5c532014-07-08 10:59:10 +02003407 if( x509parse_time_future( &child->valid_from ) )
3408 *flags |= BADCERT_FUTURE;
3409
Paul Bakker915275b2012-09-28 07:10:55 +00003410 /*
3411 * Child is the top of the chain. Check against the trust_ca list.
3412 */
3413 *flags |= BADCERT_NOT_TRUSTED;
3414
3415 while( trust_ca != NULL )
3416 {
3417 if( trust_ca->version == 0 ||
3418 child->issuer_raw.len != trust_ca->subject_raw.len ||
3419 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3420 child->issuer_raw.len ) != 0 )
3421 {
3422 trust_ca = trust_ca->next;
3423 continue;
3424 }
3425
Paul Bakker9a736322012-11-14 12:39:52 +00003426 /*
3427 * Reduce path_len to check against if top of the chain is
3428 * the same as the trusted CA
3429 */
3430 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3431 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3432 child->issuer_raw.len ) == 0 )
3433 {
3434 check_path_cnt--;
3435 }
3436
Paul Bakker915275b2012-09-28 07:10:55 +00003437 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003438 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003439 {
3440 trust_ca = trust_ca->next;
3441 continue;
3442 }
3443
3444 hash_id = child->sig_alg;
3445
3446 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3447
Paul Bakker43f97992013-09-23 11:23:31 +02003448 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003449 0, hash, child->sig.p ) != 0 )
3450 {
3451 trust_ca = trust_ca->next;
3452 continue;
3453 }
3454
3455 /*
3456 * Top of chain is signed by a trusted CA
3457 */
3458 *flags &= ~BADCERT_NOT_TRUSTED;
3459 break;
3460 }
3461
Paul Bakker9a736322012-11-14 12:39:52 +00003462 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003463 * If top of chain is not the same as the trusted CA send a verify request
3464 * to the callback for any issues with validity and CRL presence for the
3465 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003466 */
3467 if( trust_ca != NULL &&
3468 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3469 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3470 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003471 {
3472 /* Check trusted CA's CRL for then chain's top crt */
3473 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3474
3475 if( x509parse_time_expired( &trust_ca->valid_to ) )
3476 ca_flags |= BADCERT_EXPIRED;
3477
Paul Bakker50a5c532014-07-08 10:59:10 +02003478 if( x509parse_time_future( &trust_ca->valid_from ) )
3479 ca_flags |= BADCERT_FUTURE;
3480
Paul Bakker915275b2012-09-28 07:10:55 +00003481 if( NULL != f_vrfy )
3482 {
Paul Bakker9a736322012-11-14 12:39:52 +00003483 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003484 return( ret );
3485 }
3486 }
3487
3488 /* Call callback on top cert */
3489 if( NULL != f_vrfy )
3490 {
Paul Bakker9a736322012-11-14 12:39:52 +00003491 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003492 return( ret );
3493 }
3494
Paul Bakker915275b2012-09-28 07:10:55 +00003495 *flags |= ca_flags;
3496
3497 return( 0 );
3498}
3499
3500static int x509parse_verify_child(
3501 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003502 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003503 int (*f_vrfy)(void *, x509_cert *, int, int *),
3504 void *p_vrfy )
3505{
3506 int hash_id, ret;
3507 int parent_flags = 0;
3508 unsigned char hash[64];
3509 x509_cert *grandparent;
3510
3511 if( x509parse_time_expired( &child->valid_to ) )
3512 *flags |= BADCERT_EXPIRED;
3513
Paul Bakker50a5c532014-07-08 10:59:10 +02003514 if( x509parse_time_future( &child->valid_from ) )
3515 *flags |= BADCERT_FUTURE;
3516
Paul Bakker915275b2012-09-28 07:10:55 +00003517 hash_id = child->sig_alg;
3518
3519 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3520
Paul Bakker43f97992013-09-23 11:23:31 +02003521 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3522 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003523 *flags |= BADCERT_NOT_TRUSTED;
3524
3525 /* Check trusted CA's CRL for the given crt */
3526 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3527
3528 grandparent = parent->next;
3529
3530 while( grandparent != NULL )
3531 {
3532 if( grandparent->version == 0 ||
3533 grandparent->ca_istrue == 0 ||
3534 parent->issuer_raw.len != grandparent->subject_raw.len ||
3535 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3536 parent->issuer_raw.len ) != 0 )
3537 {
3538 grandparent = grandparent->next;
3539 continue;
3540 }
3541 break;
3542 }
3543
Paul Bakker915275b2012-09-28 07:10:55 +00003544 if( grandparent != NULL )
3545 {
3546 /*
3547 * Part of the chain
3548 */
Paul Bakker9a736322012-11-14 12:39:52 +00003549 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 +00003550 if( ret != 0 )
3551 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003552 }
Paul Bakker915275b2012-09-28 07:10:55 +00003553 else
3554 {
Paul Bakker9a736322012-11-14 12:39:52 +00003555 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 +00003556 if( ret != 0 )
3557 return( ret );
3558 }
3559
3560 /* child is verified to be a child of the parent, call verify callback */
3561 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003562 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003563 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003564
3565 *flags |= parent_flags;
3566
3567 return( 0 );
3568}
3569
Paul Bakker76fd75a2011-01-16 21:12:10 +00003570/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 * Verify the certificate validity
3572 */
3573int x509parse_verify( x509_cert *crt,
3574 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003575 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003576 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003577 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003578 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003579{
Paul Bakker23986e52011-04-24 08:57:21 +00003580 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003581 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003582 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003583 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003585 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Paul Bakker40ea7de2009-05-03 10:18:48 +00003587 *flags = 0;
3588
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 if( cn != NULL )
3590 {
3591 name = &crt->subject;
3592 cn_len = strlen( cn );
3593
Paul Bakker4d2c1242012-05-10 14:12:46 +00003594 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003596 cur = &crt->subject_alt_names;
3597
3598 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003599 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003600 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003601 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003602 break;
3603
Paul Bakker535e97d2012-08-23 10:49:55 +00003604 if( cur->buf.len > 2 &&
3605 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003606 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003607 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Paul Bakker4d2c1242012-05-10 14:12:46 +00003609 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003610 }
3611
3612 if( cur == NULL )
3613 *flags |= BADCERT_CN_MISMATCH;
3614 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003615 else
3616 {
3617 while( name != NULL )
3618 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003619 if( name->oid.len == 3 &&
3620 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003621 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003622 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003623 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003624 break;
3625
Paul Bakker535e97d2012-08-23 10:49:55 +00003626 if( name->val.len > 2 &&
3627 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003628 x509_wildcard_verify( cn, &name->val ) )
3629 break;
3630 }
3631
3632 name = name->next;
3633 }
3634
3635 if( name == NULL )
3636 *flags |= BADCERT_CN_MISMATCH;
3637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 }
3639
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003641 * Iterate upwards in the given cert chain, to find our crt parent.
3642 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003644 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Paul Bakker76fd75a2011-01-16 21:12:10 +00003646 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003648 if( parent->ca_istrue == 0 ||
3649 crt->issuer_raw.len != parent->subject_raw.len ||
3650 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 crt->issuer_raw.len ) != 0 )
3652 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003653 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003654 continue;
3655 }
Paul Bakker915275b2012-09-28 07:10:55 +00003656 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 }
3658
Paul Bakker915275b2012-09-28 07:10:55 +00003659 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 {
Paul Bakker915275b2012-09-28 07:10:55 +00003661 /*
3662 * Part of the chain
3663 */
Paul Bakker9a736322012-11-14 12:39:52 +00003664 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003665 if( ret != 0 )
3666 return( ret );
3667 }
3668 else
Paul Bakker74111d32011-01-15 16:57:55 +00003669 {
Paul Bakker9a736322012-11-14 12:39:52 +00003670 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003671 if( ret != 0 )
3672 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003673 }
Paul Bakker915275b2012-09-28 07:10:55 +00003674
3675 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003676 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003677
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 return( 0 );
3679}
3680
3681/*
3682 * Unallocate all certificate data
3683 */
3684void x509_free( x509_cert *crt )
3685{
3686 x509_cert *cert_cur = crt;
3687 x509_cert *cert_prv;
3688 x509_name *name_cur;
3689 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003690 x509_sequence *seq_cur;
3691 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692
3693 if( crt == NULL )
3694 return;
3695
3696 do
3697 {
3698 rsa_free( &cert_cur->rsa );
3699
3700 name_cur = cert_cur->issuer.next;
3701 while( name_cur != NULL )
3702 {
3703 name_prv = name_cur;
3704 name_cur = name_cur->next;
3705 memset( name_prv, 0, sizeof( x509_name ) );
3706 free( name_prv );
3707 }
3708
3709 name_cur = cert_cur->subject.next;
3710 while( name_cur != NULL )
3711 {
3712 name_prv = name_cur;
3713 name_cur = name_cur->next;
3714 memset( name_prv, 0, sizeof( x509_name ) );
3715 free( name_prv );
3716 }
3717
Paul Bakker74111d32011-01-15 16:57:55 +00003718 seq_cur = cert_cur->ext_key_usage.next;
3719 while( seq_cur != NULL )
3720 {
3721 seq_prv = seq_cur;
3722 seq_cur = seq_cur->next;
3723 memset( seq_prv, 0, sizeof( x509_sequence ) );
3724 free( seq_prv );
3725 }
3726
Paul Bakker8afa70d2012-02-11 18:42:45 +00003727 seq_cur = cert_cur->subject_alt_names.next;
3728 while( seq_cur != NULL )
3729 {
3730 seq_prv = seq_cur;
3731 seq_cur = seq_cur->next;
3732 memset( seq_prv, 0, sizeof( x509_sequence ) );
3733 free( seq_prv );
3734 }
3735
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 if( cert_cur->raw.p != NULL )
3737 {
3738 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3739 free( cert_cur->raw.p );
3740 }
3741
3742 cert_cur = cert_cur->next;
3743 }
3744 while( cert_cur != NULL );
3745
3746 cert_cur = crt;
3747 do
3748 {
3749 cert_prv = cert_cur;
3750 cert_cur = cert_cur->next;
3751
3752 memset( cert_prv, 0, sizeof( x509_cert ) );
3753 if( cert_prv != crt )
3754 free( cert_prv );
3755 }
3756 while( cert_cur != NULL );
3757}
3758
Paul Bakkerd98030e2009-05-02 15:13:40 +00003759/*
3760 * Unallocate all CRL data
3761 */
3762void x509_crl_free( x509_crl *crl )
3763{
3764 x509_crl *crl_cur = crl;
3765 x509_crl *crl_prv;
3766 x509_name *name_cur;
3767 x509_name *name_prv;
3768 x509_crl_entry *entry_cur;
3769 x509_crl_entry *entry_prv;
3770
3771 if( crl == NULL )
3772 return;
3773
3774 do
3775 {
3776 name_cur = crl_cur->issuer.next;
3777 while( name_cur != NULL )
3778 {
3779 name_prv = name_cur;
3780 name_cur = name_cur->next;
3781 memset( name_prv, 0, sizeof( x509_name ) );
3782 free( name_prv );
3783 }
3784
3785 entry_cur = crl_cur->entry.next;
3786 while( entry_cur != NULL )
3787 {
3788 entry_prv = entry_cur;
3789 entry_cur = entry_cur->next;
3790 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3791 free( entry_prv );
3792 }
3793
3794 if( crl_cur->raw.p != NULL )
3795 {
3796 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3797 free( crl_cur->raw.p );
3798 }
3799
3800 crl_cur = crl_cur->next;
3801 }
3802 while( crl_cur != NULL );
3803
3804 crl_cur = crl;
3805 do
3806 {
3807 crl_prv = crl_cur;
3808 crl_cur = crl_cur->next;
3809
3810 memset( crl_prv, 0, sizeof( x509_crl ) );
3811 if( crl_prv != crl )
3812 free( crl_prv );
3813 }
3814 while( crl_cur != NULL );
3815}
3816
Paul Bakker40e46942009-01-03 21:51:57 +00003817#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
Paul Bakker40e46942009-01-03 21:51:57 +00003819#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003820
3821/*
3822 * Checkup routine
3823 */
3824int x509_self_test( int verbose )
3825{
Paul Bakker5690efc2011-05-26 13:16:06 +00003826#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003827 int ret;
3828 int flags;
3829 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 x509_cert cacert;
3831 x509_cert clicert;
3832 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003833#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003834 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003836
3837 if( verbose != 0 )
3838 printf( " X.509 certificate load: " );
3839
3840 memset( &clicert, 0, sizeof( x509_cert ) );
3841
Paul Bakkereae09db2013-06-06 12:35:54 +02003842 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003843 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 if( ret != 0 )
3845 {
3846 if( verbose != 0 )
3847 printf( "failed\n" );
3848
3849 return( ret );
3850 }
3851
3852 memset( &cacert, 0, sizeof( x509_cert ) );
3853
Paul Bakkereae09db2013-06-06 12:35:54 +02003854 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003855 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 if( ret != 0 )
3857 {
3858 if( verbose != 0 )
3859 printf( "failed\n" );
3860
3861 return( ret );
3862 }
3863
3864 if( verbose != 0 )
3865 printf( "passed\n X.509 private key load: " );
3866
3867 i = strlen( test_ca_key );
3868 j = strlen( test_ca_pwd );
3869
Paul Bakker66b78b22011-03-25 14:22:50 +00003870 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3871
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003873 (const unsigned char *) test_ca_key, i,
3874 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 {
3876 if( verbose != 0 )
3877 printf( "failed\n" );
3878
3879 return( ret );
3880 }
3881
3882 if( verbose != 0 )
3883 printf( "passed\n X.509 signature verify: ");
3884
Paul Bakker23986e52011-04-24 08:57:21 +00003885 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 if( ret != 0 )
3887 {
3888 if( verbose != 0 )
3889 printf( "failed\n" );
3890
3891 return( ret );
3892 }
3893
Paul Bakker5690efc2011-05-26 13:16:06 +00003894#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003896 printf( "passed\n X.509 DHM parameter load: " );
3897
3898 i = strlen( test_dhm_params );
3899 j = strlen( test_ca_pwd );
3900
Paul Bakkereae09db2013-06-06 12:35:54 +02003901 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003902 {
3903 if( verbose != 0 )
3904 printf( "failed\n" );
3905
3906 return( ret );
3907 }
3908
3909 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003911#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003912
3913 x509_free( &cacert );
3914 x509_free( &clicert );
3915 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003916#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003917 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003918#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003919
3920 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003921#else
3922 ((void) verbose);
3923 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003925}
3926
3927#endif
3928
3929#endif