blob: fff6dd69c795e98cd9d9608325dbabba74e5063a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000023 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Paul Bakker5121ce52009-01-03 21:22:43 +000025 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
28 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
29 *
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
31 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 */
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000039#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000040#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010042#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000043#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010044#endif
45#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010047#endif
48#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010050#endif
51#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010053#endif
54#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000055#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010056#endif
57#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010059#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000060#include "polarssl/dhm.h"
Paul Bakker1fd43212013-06-17 15:14:42 +020061#if defined(POLARSSL_PKCS5_C)
62#include "polarssl/pkcs5.h"
63#endif
Paul Bakker14a222c2013-06-18 16:35:48 +020064#if defined(POLARSSL_PKCS12_C)
65#include "polarssl/pkcs12.h"
66#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68#include <string.h>
69#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000070#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000071#include <windows.h>
72#else
Paul Bakker5121ce52009-01-03 21:22:43 +000073#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000074#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker335db3f2011-04-25 15:28:35 +000076#if defined(POLARSSL_FS_IO)
77#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000078#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000079#include <sys/types.h>
Paul Bakkercbfcaa92013-06-13 09:20:25 +020080#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000081#include <dirent.h>
82#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000083#endif
84
Paul Bakkercf6e95d2013-06-12 13:18:15 +020085/* Compare a given OID string with an OID x509_buf * */
86#define OID_CMP(oid_str, oid_buf) \
87 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
88 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
89
Paul Bakker5121ce52009-01-03 21:22:43 +000090/*
Paul Bakker5121ce52009-01-03 21:22:43 +000091 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
92 */
93static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000094 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000095 int *ver )
96{
Paul Bakker23986e52011-04-24 08:57:21 +000097 int ret;
98 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 if( ( ret = asn1_get_tag( p, end, &len,
101 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
102 {
Paul Bakker40e46942009-01-03 21:51:57 +0000103 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000104 {
105 *ver = 0;
106 return( 0 );
107 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 return( ret );
110 }
111
112 end = *p + len;
113
114 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000115 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000118 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000119 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 return( 0 );
122}
123
124/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000125 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000126 */
127static int x509_crl_get_version( unsigned char **p,
128 const unsigned char *end,
129 int *ver )
130{
131 int ret;
132
133 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
134 {
135 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000136 {
137 *ver = 0;
138 return( 0 );
139 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000140
141 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
142 }
143
144 return( 0 );
145}
146
147/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 * CertificateSerialNumber ::= INTEGER
149 */
150static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 x509_buf *serial )
153{
154 int ret;
155
156 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000157 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000158 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
161 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000162 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000163 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165 serial->tag = *(*p)++;
166
167 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000168 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170 serial->p = *p;
171 *p += serial->len;
172
173 return( 0 );
174}
175
176/*
177 * AlgorithmIdentifier ::= SEQUENCE {
178 * algorithm OBJECT IDENTIFIER,
179 * parameters ANY DEFINED BY algorithm OPTIONAL }
180 */
181static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000182 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 x509_buf *alg )
184{
Paul Bakker23986e52011-04-24 08:57:21 +0000185 int ret;
186 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188 if( ( ret = asn1_get_tag( p, end, &len,
189 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000190 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 end = *p + len;
Manuel Pégourié-Gonnardd8a1ea72014-11-17 12:04:51 +0100193
194 if( len < 1 )
195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
196 POLARSSL_ERR_ASN1_OUT_OF_DATA );
197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 alg->tag = **p;
199
200 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000201 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 alg->p = *p;
204 *p += alg->len;
205
206 if( *p == end )
207 return( 0 );
208
209 /*
210 * assume the algorithm parameters must be NULL
211 */
212 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000213 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000216 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000217 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219 return( 0 );
220}
221
222/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 * AttributeTypeAndValue ::= SEQUENCE {
224 * type AttributeType,
225 * value AttributeValue }
226 *
227 * AttributeType ::= OBJECT IDENTIFIER
228 *
229 * AttributeValue ::= ANY DEFINED BY AttributeType
230 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000231static int x509_get_attr_type_value( unsigned char **p,
232 const unsigned char *end,
233 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000234{
Paul Bakker23986e52011-04-24 08:57:21 +0000235 int ret;
236 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 x509_buf *oid;
238 x509_buf *val;
239
240 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 oid = &cur->oid;
Manuel Pégourié-Gonnardd8a1ea72014-11-17 12:04:51 +0100245
246 if( len < 1 )
247 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
248 POLARSSL_ERR_ASN1_OUT_OF_DATA );
249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 oid->tag = **p;
251
252 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000253 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 oid->p = *p;
256 *p += oid->len;
257
258 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000259 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000260 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
263 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
264 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000265 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000266 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268 val = &cur->val;
269 val->tag = *(*p)++;
270
271 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000272 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274 val->p = *p;
275 *p += val->len;
276
277 cur->next = NULL;
278
Paul Bakker400ff6f2011-02-20 10:40:16 +0000279 return( 0 );
280}
281
282/*
283 * RelativeDistinguishedName ::=
284 * SET OF AttributeTypeAndValue
285 *
286 * AttributeTypeAndValue ::= SEQUENCE {
287 * type AttributeType,
288 * value AttributeValue }
289 *
290 * AttributeType ::= OBJECT IDENTIFIER
291 *
292 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200293 *
294 * We restrict RelativeDistinguishedName to be a set of 1 element. This is
295 * the most common case, and our x509_name structure currently can't handle
296 * more than that.
Paul Bakker400ff6f2011-02-20 10:40:16 +0000297 */
298static int x509_get_name( unsigned char **p,
299 const unsigned char *end,
300 x509_name *cur )
301{
Paul Bakker23986e52011-04-24 08:57:21 +0000302 int ret;
Manuel Pégourié-Gonnard6b440382014-10-23 14:53:46 +0200303 size_t set_len;
304 const unsigned char *end_set;
305
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100306 /* don't use recursion, we'd risk stack overflow if not optimized */
307 while( 1 )
308 {
309 /*
310 * parse first SET, restricted to 1 element
311 */
312 if( ( ret = asn1_get_tag( p, end, &set_len,
313 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
314 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000315
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100316 end_set = *p + set_len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000317
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100318 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
319 return( ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000320
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100321 if( *p != end_set )
322 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100324 /*
325 * continue until end of SEQUENCE is reached
326 */
327 if( *p == end )
328 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100330 cur->next = (x509_name *) malloc( sizeof( x509_name ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100332 if( cur->next == NULL )
333 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100335 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker430ffbe2012-05-01 08:14:20 +0000336
Manuel Pégourié-Gonnard360eb912014-11-12 16:52:34 +0100337 cur = cur->next;
338 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000339}
340
341/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 * Time ::= CHOICE {
343 * utcTime UTCTime,
344 * generalTime GeneralizedTime }
345 */
Paul Bakker91200182010-02-18 21:26:15 +0000346static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348 x509_time *time )
349{
Paul Bakker23986e52011-04-24 08:57:21 +0000350 int ret;
351 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000353 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000354
Paul Bakker91200182010-02-18 21:26:15 +0000355 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
357 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000358
Paul Bakker91200182010-02-18 21:26:15 +0000359 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000360
Paul Bakker91200182010-02-18 21:26:15 +0000361 if ( tag == ASN1_UTC_TIME )
362 {
363 (*p)++;
364 ret = asn1_get_len( p, end, &len );
365
366 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000368
Paul Bakker91200182010-02-18 21:26:15 +0000369 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000370 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
371 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000372
Paul Bakker243d6182014-07-08 14:40:58 +0200373 if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000374 &time->year, &time->mon, &time->day,
375 &time->hour, &time->min, &time->sec ) < 5 )
376 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000377
Paul Bakker400ff6f2011-02-20 10:40:16 +0000378 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000379 time->year += 1900;
380
381 *p += len;
382
383 return( 0 );
384 }
385 else if ( tag == ASN1_GENERALIZED_TIME )
386 {
387 (*p)++;
388 ret = asn1_get_len( p, end, &len );
389
390 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000392
393 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000394 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
395 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000396
Paul Bakker243d6182014-07-08 14:40:58 +0200397 if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ",
Paul Bakker91200182010-02-18 21:26:15 +0000398 &time->year, &time->mon, &time->day,
399 &time->hour, &time->min, &time->sec ) < 5 )
400 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
401
402 *p += len;
403
404 return( 0 );
405 }
406 else
Paul Bakker9d781402011-05-09 16:17:09 +0000407 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000408}
409
410
411/*
412 * Validity ::= SEQUENCE {
413 * notBefore Time,
414 * notAfter Time }
415 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000416static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 x509_time *from,
419 x509_time *to )
420{
Paul Bakker23986e52011-04-24 08:57:21 +0000421 int ret;
422 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 if( ( ret = asn1_get_tag( p, end, &len,
425 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 end = *p + len;
429
Paul Bakker91200182010-02-18 21:26:15 +0000430 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000431 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
Paul Bakker91200182010-02-18 21:26:15 +0000433 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000434 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000437 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000438 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 return( 0 );
441}
442
443/*
444 * SubjectPublicKeyInfo ::= SEQUENCE {
445 * algorithm AlgorithmIdentifier,
446 * subjectPublicKey BIT STRING }
447 */
448static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000449 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 x509_buf *pk_alg_oid,
451 mpi *N, mpi *E )
452{
Paul Bakker65a19092013-06-06 21:14:58 +0200453 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000454 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 unsigned char *end2;
456
457 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
458 return( ret );
459
460 /*
461 * only RSA public keys handled at this time
462 */
Paul Bakker65a19092013-06-06 21:14:58 +0200463 if( pk_alg_oid->len != 9 ||
464 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) != 0 )
Paul Bakker400ff6f2011-02-20 10:40:16 +0000465 {
Paul Bakkered56b222011-07-13 11:26:43 +0000466 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker65a19092013-06-06 21:14:58 +0200467 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000470 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000473 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000474 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476 end2 = *p + len;
477
478 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 /*
482 * RSAPublicKey ::= SEQUENCE {
483 * modulus INTEGER, -- n
484 * publicExponent INTEGER -- e
485 * }
486 */
487 if( ( ret = asn1_get_tag( p, end2, &len,
488 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000492 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000493 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
496 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000497 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000500 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000501 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 return( 0 );
504}
505
506static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 x509_buf *sig )
509{
Paul Bakker23986e52011-04-24 08:57:21 +0000510 int ret;
511 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakker8afa70d2012-02-11 18:42:45 +0000513 if( ( end - *p ) < 1 )
514 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
515 POLARSSL_ERR_ASN1_OUT_OF_DATA );
516
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 sig->tag = **p;
518
519 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000520 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Paul Bakker74111d32011-01-15 16:57:55 +0000522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 sig->len = len;
527 sig->p = *p;
528
529 *p += len;
530
531 return( 0 );
532}
533
534/*
535 * X.509 v2/v3 unique identifier (not parsed)
536 */
537static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 x509_buf *uid, int n )
540{
541 int ret;
542
543 if( *p == end )
544 return( 0 );
545
546 uid->tag = **p;
547
548 if( ( ret = asn1_get_tag( p, end, &uid->len,
549 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
550 {
Paul Bakker40e46942009-01-03 21:51:57 +0000551 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 return( 0 );
553
554 return( ret );
555 }
556
557 uid->p = *p;
558 *p += uid->len;
559
560 return( 0 );
561}
562
563/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000564 * X.509 Extensions (No parsing of extensions, pointer should
565 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
567static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000569 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570{
Paul Bakker23986e52011-04-24 08:57:21 +0000571 int ret;
572 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 if( *p == end )
575 return( 0 );
576
577 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000578
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000580 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 ext->p = *p;
584 end = *p + ext->len;
585
586 /*
587 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
588 *
589 * Extension ::= SEQUENCE {
590 * extnID OBJECT IDENTIFIER,
591 * critical BOOLEAN DEFAULT FALSE,
592 * extnValue OCTET STRING }
593 */
594 if( ( ret = asn1_get_tag( p, end, &len,
595 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000599 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000600 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602 return( 0 );
603}
604
605/*
606 * X.509 CRL v2 extensions (no extensions parsed yet.)
607 */
608static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000609 const unsigned char *end,
610 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611{
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000613 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000615 /* Get explicit tag */
616 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000617 {
618 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
619 return( 0 );
620
621 return( ret );
622 }
623
624 while( *p < end )
625 {
626 if( ( ret = asn1_get_tag( p, end, &len,
627 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000629
630 *p += len;
631 }
632
633 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000635 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
636
637 return( 0 );
638}
639
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000640/*
641 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
642 */
643static int x509_get_crl_entry_ext( unsigned char **p,
644 const unsigned char *end,
645 x509_buf *ext )
646{
647 int ret;
648 size_t len = 0;
649
650 /* OPTIONAL */
651 if (end <= *p)
652 return( 0 );
653
654 ext->tag = **p;
655 ext->p = *p;
656
657 /*
658 * Get CRL-entry extension sequence header
659 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
660 */
661 if( ( ret = asn1_get_tag( p, end, &ext->len,
662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
663 {
664 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
665 {
666 ext->p = NULL;
667 return( 0 );
668 }
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
670 }
671
672 end = *p + ext->len;
673
674 if( end != *p + ext->len )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
676 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
677
678 while( *p < end )
679 {
680 if( ( ret = asn1_get_tag( p, end, &len,
681 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
683
684 *p += len;
685 }
686
687 if( *p != end )
688 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
689 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
690
691 return( 0 );
692}
693
Paul Bakker74111d32011-01-15 16:57:55 +0000694static int x509_get_basic_constraints( unsigned char **p,
695 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000696 int *ca_istrue,
697 int *max_pathlen )
698{
Paul Bakker23986e52011-04-24 08:57:21 +0000699 int ret;
700 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000701
702 /*
703 * BasicConstraints ::= SEQUENCE {
704 * cA BOOLEAN DEFAULT FALSE,
705 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
706 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000707 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000708 *max_pathlen = 0; /* endless */
709
710 if( ( ret = asn1_get_tag( p, end, &len,
711 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000712 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000713
714 if( *p == end )
715 return 0;
716
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000718 {
719 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
722 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000724
Paul Bakker3cccddb2011-01-16 21:46:31 +0000725 if( *ca_istrue != 0 )
726 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000727 }
728
729 if( *p == end )
730 return 0;
731
732 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000734
735 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 (*max_pathlen)++;
740
Paul Bakker74111d32011-01-15 16:57:55 +0000741 return 0;
742}
743
744static int x509_get_ns_cert_type( unsigned char **p,
745 const unsigned char *end,
746 unsigned char *ns_cert_type)
747{
748 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000749 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000755 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000756 POLARSSL_ERR_ASN1_INVALID_LENGTH );
757
758 /* Get actual bitstring */
759 *ns_cert_type = *bs.p;
760 return 0;
761}
762
763static int x509_get_key_usage( unsigned char **p,
764 const unsigned char *end,
765 unsigned char *key_usage)
766{
767 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000768 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000769
770 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
Paul Bakker94a67962012-08-23 13:03:52 +0000773 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000775 POLARSSL_ERR_ASN1_INVALID_LENGTH );
776
777 /* Get actual bitstring */
778 *key_usage = *bs.p;
779 return 0;
780}
781
Paul Bakkerd98030e2009-05-02 15:13:40 +0000782/*
Paul Bakker74111d32011-01-15 16:57:55 +0000783 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
784 *
785 * KeyPurposeId ::= OBJECT IDENTIFIER
786 */
787static int x509_get_ext_key_usage( unsigned char **p,
788 const unsigned char *end,
789 x509_sequence *ext_key_usage)
790{
791 int ret;
792
793 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000794 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000795
796 /* Sequence length must be >= 1 */
797 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000799 POLARSSL_ERR_ASN1_INVALID_LENGTH );
800
801 return 0;
802}
803
804/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000805 * SubjectAltName ::= GeneralNames
806 *
807 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
808 *
809 * GeneralName ::= CHOICE {
810 * otherName [0] OtherName,
811 * rfc822Name [1] IA5String,
812 * dNSName [2] IA5String,
813 * x400Address [3] ORAddress,
814 * directoryName [4] Name,
815 * ediPartyName [5] EDIPartyName,
816 * uniformResourceIdentifier [6] IA5String,
817 * iPAddress [7] OCTET STRING,
818 * registeredID [8] OBJECT IDENTIFIER }
819 *
820 * OtherName ::= SEQUENCE {
821 * type-id OBJECT IDENTIFIER,
822 * value [0] EXPLICIT ANY DEFINED BY type-id }
823 *
824 * EDIPartyName ::= SEQUENCE {
825 * nameAssigner [0] DirectoryString OPTIONAL,
826 * partyName [1] DirectoryString }
827 *
828 * NOTE: PolarSSL only parses and uses dNSName at this point.
829 */
830static int x509_get_subject_alt_name( unsigned char **p,
831 const unsigned char *end,
832 x509_sequence *subject_alt_name )
833{
834 int ret;
835 size_t len, tag_len;
836 asn1_buf *buf;
837 unsigned char tag;
838 asn1_sequence *cur = subject_alt_name;
839
840 /* Get main sequence tag */
841 if( ( ret = asn1_get_tag( p, end, &len,
842 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
844
845 if( *p + len != end )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 while( *p < end )
850 {
851 if( ( end - *p ) < 1 )
852 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
853 POLARSSL_ERR_ASN1_OUT_OF_DATA );
854
855 tag = **p;
856 (*p)++;
857 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
858 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
859
860 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
862 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
863
864 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
865 {
866 *p += tag_len;
867 continue;
868 }
869
870 buf = &(cur->buf);
871 buf->tag = tag;
872 buf->p = *p;
873 buf->len = tag_len;
874 *p += buf->len;
875
876 /* Allocate and assign next pointer */
877 if (*p < end)
878 {
Manuel Pégourié-Gonnardfdec9572014-11-11 23:11:16 +0100879 if( cur->next != NULL )
880 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
881
Paul Bakkera8cd2392012-02-11 16:09:32 +0000882 cur->next = (asn1_sequence *) malloc(
883 sizeof( asn1_sequence ) );
884
885 if( cur->next == NULL )
886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
887 POLARSSL_ERR_ASN1_MALLOC_FAILED );
888
Paul Bakker535e97d2012-08-23 10:49:55 +0000889 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000890 cur = cur->next;
891 }
892 }
893
894 /* Set final sequence entry's next pointer to NULL */
895 cur->next = NULL;
896
897 if( *p != end )
898 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
899 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
900
901 return( 0 );
902}
903
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100904static int x509_get_crt_ext_type( const x509_buf *oid )
905{
906 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == oid->len ) &&
907 memcmp( oid->p, OID_BASIC_CONSTRAINTS, oid->len ) == 0 )
908 {
909 return( EXT_BASIC_CONSTRAINTS );
910 }
911 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == oid->len ) &&
912 memcmp( oid->p, OID_NS_CERT_TYPE, oid->len ) == 0 )
913 {
914 return( EXT_NS_CERT_TYPE );
915 }
916 else if( ( OID_SIZE( OID_KEY_USAGE ) == oid->len ) &&
917 memcmp( oid->p, OID_KEY_USAGE, oid->len ) == 0 )
918 {
919 return( EXT_KEY_USAGE );
920 }
921 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == oid->len ) &&
922 memcmp( oid->p, OID_EXTENDED_KEY_USAGE, oid->len ) == 0 )
923 {
924 return( EXT_EXTENDED_KEY_USAGE );
925 }
926 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == oid->len ) &&
927 memcmp( oid->p, OID_SUBJECT_ALT_NAME, oid->len ) == 0 )
928 {
929 return( EXT_SUBJECT_ALT_NAME );
930 }
931
932 return( -1 );
933}
934
Paul Bakkera8cd2392012-02-11 16:09:32 +0000935/*
Paul Bakker74111d32011-01-15 16:57:55 +0000936 * X.509 v3 extensions
937 *
938 * TODO: Perform all of the basic constraints tests required by the RFC
939 * TODO: Set values for undetected extensions to a sane default?
940 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000941 */
942static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000943 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000944 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000945{
Paul Bakker23986e52011-04-24 08:57:21 +0000946 int ret;
947 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000949
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000950 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000951 {
952 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
953 return( 0 );
954
955 return( ret );
956 }
957
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 while( *p < end )
959 {
Paul Bakker74111d32011-01-15 16:57:55 +0000960 /*
961 * Extension ::= SEQUENCE {
962 * extnID OBJECT IDENTIFIER,
963 * critical BOOLEAN DEFAULT FALSE,
964 * extnValue OCTET STRING }
965 */
966 x509_buf extn_oid = {0, 0, NULL};
967 int is_critical = 0; /* DEFAULT FALSE */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100968 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000969
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 if( ( ret = asn1_get_tag( p, end, &len,
971 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000972 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000974 end_ext_data = *p + len;
975
Paul Bakker74111d32011-01-15 16:57:55 +0000976 /* Get extension ID */
977 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker74111d32011-01-15 16:57:55 +0000979 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000980 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 extn_oid.p = *p;
983 *p += extn_oid.len;
984
985 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000986 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000987 POLARSSL_ERR_ASN1_OUT_OF_DATA );
988
989 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000990 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000991 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000992 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Paul Bakker74111d32011-01-15 16:57:55 +0000994 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000995 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000997 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000999 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001000
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001001 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001002 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001003 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker74111d32011-01-15 16:57:55 +00001005 /*
1006 * Detect supported extensions
1007 */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001008 ext_type = x509_get_crt_ext_type( &extn_oid );
1009
1010 if( ext_type < 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001011 {
1012 /* No parser found, skip extension */
1013 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Paul Bakker5c721f92011-07-27 16:51:09 +00001015#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001016 if( is_critical )
1017 {
1018 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001019 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001020 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1021 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001022#endif
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001023 continue;
1024 }
1025
1026 /* Forbid repeated extensions */
1027 if( ( crt->ext_types & ext_type ) != 0 )
1028 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
1029
1030 crt->ext_types |= ext_type;
1031
1032 switch( ext_type )
1033 {
1034 case EXT_BASIC_CONSTRAINTS:
1035 /* Parse basic constraints */
1036 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1037 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1038 return( ret );
1039 break;
1040
1041 case EXT_KEY_USAGE:
1042 /* Parse key usage */
1043 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1044 &crt->key_usage ) ) != 0 )
1045 return( ret );
1046 break;
1047
1048 case EXT_EXTENDED_KEY_USAGE:
1049 /* Parse extended key usage */
1050 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1051 &crt->ext_key_usage ) ) != 0 )
1052 return( ret );
1053 break;
1054
1055 case EXT_SUBJECT_ALT_NAME:
1056 /* Parse subject alt name */
1057 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1058 &crt->subject_alt_names ) ) != 0 )
1059 return( ret );
1060 break;
1061
1062 case EXT_NS_CERT_TYPE:
1063 /* Parse netscape certificate type */
1064 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1065 &crt->ns_cert_type ) ) != 0 )
1066 return( ret );
1067 break;
1068
1069 default:
1070 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
1073
1074 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001075 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001076 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 return( 0 );
1079}
1080
1081/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 * X.509 CRL Entries
1083 */
1084static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001085 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001086 x509_crl_entry *entry )
1087{
Paul Bakker23986e52011-04-24 08:57:21 +00001088 int ret;
1089 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001090 x509_crl_entry *cur_entry = entry;
1091
1092 if( *p == end )
1093 return( 0 );
1094
Paul Bakker9be19372009-07-27 20:21:53 +00001095 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001096 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1097 {
1098 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1099 return( 0 );
1100
1101 return( ret );
1102 }
1103
Paul Bakker9be19372009-07-27 20:21:53 +00001104 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001105
1106 while( *p < end )
1107 {
Paul Bakker23986e52011-04-24 08:57:21 +00001108 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001109 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001110
1111 if( ( ret = asn1_get_tag( p, end, &len2,
1112 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1113 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001114 return( ret );
1115 }
1116
Paul Bakker9be19372009-07-27 20:21:53 +00001117 cur_entry->raw.tag = **p;
1118 cur_entry->raw.p = *p;
1119 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001120 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001121
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001122 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001123 return( ret );
1124
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001125 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 return( ret );
1127
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001128 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001129 return( ret );
1130
Paul Bakker74111d32011-01-15 16:57:55 +00001131 if ( *p < end )
1132 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001133 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001134
1135 if( cur_entry->next == NULL )
1136 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1137
Paul Bakkerd98030e2009-05-02 15:13:40 +00001138 cur_entry = cur_entry->next;
1139 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1140 }
1141 }
1142
1143 return( 0 );
1144}
1145
Paul Bakker27d66162010-03-17 06:56:01 +00001146static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1147{
1148 if( sig_oid->len == 9 &&
1149 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1150 {
1151 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1152 {
1153 *sig_alg = sig_oid->p[8];
1154 return( 0 );
1155 }
1156
1157 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1158 {
1159 *sig_alg = sig_oid->p[8];
1160 return( 0 );
1161 }
1162
1163 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1164 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001165 if( sig_oid->len == 5 &&
1166 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1167 {
1168 *sig_alg = SIG_RSA_SHA1;
1169 return( 0 );
1170 }
Paul Bakker27d66162010-03-17 06:56:01 +00001171
1172 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1173}
1174
Paul Bakkerd98030e2009-05-02 15:13:40 +00001175/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001176 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001178static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1179 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001180{
Paul Bakker23986e52011-04-24 08:57:21 +00001181 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001182 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001183 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
Paul Bakker320a4b52009-03-28 18:52:39 +00001185 /*
1186 * Check for valid input
1187 */
1188 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001189 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001190
Paul Bakker96743fc2011-02-12 14:30:57 +00001191 p = (unsigned char *) malloc( len = buflen );
1192
1193 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001194 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001195
1196 memcpy( p, buf, buflen );
1197
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 crt->raw.p = p;
1199 crt->raw.len = len;
1200 end = p + len;
1201
1202 /*
1203 * Certificate ::= SEQUENCE {
1204 * tbsCertificate TBSCertificate,
1205 * signatureAlgorithm AlgorithmIdentifier,
1206 * signatureValue BIT STRING }
1207 */
1208 if( ( ret = asn1_get_tag( &p, end, &len,
1209 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001212 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
Paul Bakkerb00ca422012-09-25 12:10:00 +00001215 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001218 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001219 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001221 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001222
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 /*
1224 * TBSCertificate ::= SEQUENCE {
1225 */
1226 crt->tbs.p = p;
1227
1228 if( ( ret = asn1_get_tag( &p, end, &len,
1229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001232 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
1235 end = p + len;
1236 crt->tbs.len = end - crt->tbs.p;
1237
1238 /*
1239 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1240 *
1241 * CertificateSerialNumber ::= INTEGER
1242 *
1243 * signature AlgorithmIdentifier
1244 */
1245 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1246 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1247 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 return( ret );
1251 }
1252
1253 crt->version++;
1254
1255 if( crt->version > 3 )
1256 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001257 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001258 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
Paul Bakker27d66162010-03-17 06:56:01 +00001261 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001263 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001264 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
1266
1267 /*
1268 * issuer Name
1269 */
1270 crt->issuer_raw.p = p;
1271
1272 if( ( ret = asn1_get_tag( &p, end, &len,
1273 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001276 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 }
1278
1279 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1280 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001281 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 return( ret );
1283 }
1284
1285 crt->issuer_raw.len = p - crt->issuer_raw.p;
1286
1287 /*
1288 * Validity ::= SEQUENCE {
1289 * notBefore Time,
1290 * notAfter Time }
1291 *
1292 */
1293 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1294 &crt->valid_to ) ) != 0 )
1295 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 return( ret );
1298 }
1299
1300 /*
1301 * subject Name
1302 */
1303 crt->subject_raw.p = p;
1304
1305 if( ( ret = asn1_get_tag( &p, end, &len,
1306 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1307 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001308 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001309 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
1311
Paul Bakkercefb3962012-06-27 11:51:09 +00001312 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317
1318 crt->subject_raw.len = p - crt->subject_raw.p;
1319
1320 /*
1321 * SubjectPublicKeyInfo ::= SEQUENCE
1322 * algorithm AlgorithmIdentifier,
1323 * subjectPublicKey BIT STRING }
1324 */
1325 if( ( ret = asn1_get_tag( &p, end, &len,
1326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1327 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001328 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001329 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 }
1331
1332 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1333 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1334 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001335 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 return( ret );
1337 }
1338
1339 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 return( ret );
1343 }
1344
1345 crt->rsa.len = mpi_size( &crt->rsa.N );
1346
1347 /*
1348 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1349 * -- If present, version shall be v2 or v3
1350 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1351 * -- If present, version shall be v2 or v3
1352 * extensions [3] EXPLICIT Extensions OPTIONAL
1353 * -- If present, version shall be v3
1354 */
1355 if( crt->version == 2 || crt->version == 3 )
1356 {
1357 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1358 if( ret != 0 )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 return( ret );
1362 }
1363 }
1364
1365 if( crt->version == 2 || crt->version == 3 )
1366 {
1367 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1368 if( ret != 0 )
1369 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001370 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 return( ret );
1372 }
1373 }
1374
1375 if( crt->version == 3 )
1376 {
Paul Bakker74111d32011-01-15 16:57:55 +00001377 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 if( ret != 0 )
1379 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001380 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 return( ret );
1382 }
1383 }
1384
1385 if( p != end )
1386 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001387 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001388 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001389 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 }
1391
Paul Bakkerb00ca422012-09-25 12:10:00 +00001392 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 /*
1395 * signatureAlgorithm AlgorithmIdentifier,
1396 * signatureValue BIT STRING
1397 */
1398 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1399 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001400 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 return( ret );
1402 }
1403
Paul Bakker535e97d2012-08-23 10:49:55 +00001404 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1405 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001407 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001408 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
1411 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1412 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001413 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 return( ret );
1415 }
1416
1417 if( p != end )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001420 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001421 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001424 return( 0 );
1425}
1426
1427/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001428 * Parse one X.509 certificate in DER format from a buffer and add them to a
1429 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001430 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001431int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001432{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001433 int ret;
1434 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001435
1436 /*
1437 * Check for valid input
1438 */
1439 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001440 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001441
1442 while( crt->version != 0 && crt->next != NULL )
1443 {
1444 prev = crt;
1445 crt = crt->next;
1446 }
1447
1448 /*
1449 * Add new certificate on the end of the chain if needed.
1450 */
1451 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001452 {
1453 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1454
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001456 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001457
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001458 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001459 crt = crt->next;
1460 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Paul Bakkerd6d41092013-06-13 09:00:25 +02001463 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1464 {
1465 if( prev )
1466 prev->next = NULL;
1467
1468 if( crt != chain )
1469 free( crt );
1470
1471 return( ret );
1472 }
1473
1474 return( 0 );
1475}
1476
1477/*
1478 * Parse one or more PEM certificates from a buffer and add them to the chained list
1479 */
1480int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1481{
1482 int ret, success = 0, first_error = 0, total_failed = 0;
1483 int buf_format = X509_FORMAT_DER;
1484
1485 /*
1486 * Check for valid input
1487 */
1488 if( chain == NULL || buf == NULL )
1489 return( POLARSSL_ERR_X509_INVALID_INPUT );
1490
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001491 /*
1492 * Determine buffer content. Buffer contains either one DER certificate or
1493 * one or more PEM certificates.
1494 */
1495#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001496 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001497 buf_format = X509_FORMAT_PEM;
1498#endif
1499
1500 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001501 return x509parse_crt_der( chain, buf, buflen );
1502
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001503#if defined(POLARSSL_PEM_C)
1504 if( buf_format == X509_FORMAT_PEM )
1505 {
1506 pem_context pem;
1507
1508 while( buflen > 0 )
1509 {
1510 size_t use_len;
1511 pem_init( &pem );
1512
1513 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001514 (char *) "-----BEGIN CERTIFICATE-----",
1515 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001516 buf, NULL, 0, &use_len );
1517
1518 if( ret == 0 )
1519 {
1520 /*
1521 * Was PEM encoded
1522 */
1523 buflen -= use_len;
1524 buf += use_len;
1525 }
Paul Bakker64171862013-06-06 15:01:18 +02001526 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1527 {
1528 return( ret );
1529 }
Paul Bakker9255e832013-06-06 14:58:28 +02001530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001531 {
1532 pem_free( &pem );
1533
Paul Bakker64171862013-06-06 15:01:18 +02001534 /*
1535 * PEM header and footer were found
1536 */
1537 buflen -= use_len;
1538 buf += use_len;
1539
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001540 if( first_error == 0 )
1541 first_error = ret;
1542
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001543 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544 continue;
1545 }
1546 else
1547 break;
1548
Paul Bakkerd6d41092013-06-13 09:00:25 +02001549 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001550
1551 pem_free( &pem );
1552
1553 if( ret != 0 )
1554 {
1555 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001556 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001557 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001558 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001559 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001560
1561 if( first_error == 0 )
1562 first_error = ret;
1563
Paul Bakkerd6d41092013-06-13 09:00:25 +02001564 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001565 continue;
1566 }
1567
1568 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001569 }
1570 }
1571#endif
1572
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001574 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001575 else if( first_error )
1576 return( first_error );
1577 else
1578 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579}
1580
1581/*
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001582 * Parse one CRLs in DER format and append it to the chained list
Paul Bakkerd98030e2009-05-02 15:13:40 +00001583 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001584static int x509_crl_parse_der( x509_crl *chain,
1585 const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001586{
Paul Bakker23986e52011-04-24 08:57:21 +00001587 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001588 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 unsigned char *p, *end;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001590 x509_buf sig_params1, sig_params2;
1591 x509_crl *crl = chain;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001592
1593 /*
1594 * Check for valid input
1595 */
1596 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001597 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001599 memset( &sig_params1, 0, sizeof( x509_buf ) );
1600 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601
1602 /*
1603 * Add new CRL on the end of the chain if needed.
1604 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001605 while( crl->version != 0 && crl->next != NULL )
1606 crl = crl->next;
1607
1608 if( crl->version != 0 && crl->next == NULL )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 {
1610 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1611
Paul Bakker7d06ad22009-05-02 15:53:56 +00001612 if( crl->next == NULL )
1613 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001616 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001617
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001618 memset( crl->next, 0, sizeof( x509_crl ) );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001619 crl = crl->next;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001620 }
1621
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001622 /*
1623 * Copy raw DER-encoded CRL
1624 */
1625 if( ( p = malloc( buflen ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001626 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001627
1628 memcpy( p, buf, buflen );
1629
Paul Bakkerd98030e2009-05-02 15:13:40 +00001630 crl->raw.p = p;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001631 crl->raw.len = buflen;
1632
1633 end = p + buflen;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634
1635 /*
1636 * CertificateList ::= SEQUENCE {
1637 * tbsCertList TBSCertList,
1638 * signatureAlgorithm AlgorithmIdentifier,
1639 * signatureValue BIT STRING }
1640 */
1641 if( ( ret = asn1_get_tag( &p, end, &len,
1642 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1643 {
1644 x509_crl_free( crl );
1645 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1646 }
1647
Paul Bakker23986e52011-04-24 08:57:21 +00001648 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 {
1650 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001651 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1653 }
1654
1655 /*
1656 * TBSCertList ::= SEQUENCE {
1657 */
1658 crl->tbs.p = p;
1659
1660 if( ( ret = asn1_get_tag( &p, end, &len,
1661 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1662 {
1663 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001664 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001665 }
1666
1667 end = p + len;
1668 crl->tbs.len = end - crl->tbs.p;
1669
1670 /*
1671 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1672 * -- if present, MUST be v2
1673 *
1674 * signature AlgorithmIdentifier
1675 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001676 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001677 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1678 {
1679 x509_crl_free( crl );
1680 return( ret );
1681 }
1682
1683 crl->version++;
1684
1685 if( crl->version > 2 )
1686 {
1687 x509_crl_free( crl );
1688 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1689 }
1690
Paul Bakker27d66162010-03-17 06:56:01 +00001691 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692 {
1693 x509_crl_free( crl );
1694 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1695 }
1696
1697 /*
1698 * issuer Name
1699 */
1700 crl->issuer_raw.p = p;
1701
1702 if( ( ret = asn1_get_tag( &p, end, &len,
1703 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001706 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 }
1708
1709 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1710 {
1711 x509_crl_free( crl );
1712 return( ret );
1713 }
1714
1715 crl->issuer_raw.len = p - crl->issuer_raw.p;
1716
1717 /*
1718 * thisUpdate Time
1719 * nextUpdate Time OPTIONAL
1720 */
Paul Bakker91200182010-02-18 21:26:15 +00001721 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 {
1723 x509_crl_free( crl );
1724 return( ret );
1725 }
1726
Paul Bakker91200182010-02-18 21:26:15 +00001727 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 {
Paul Bakker9d781402011-05-09 16:17:09 +00001729 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001730 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001731 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001732 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001733 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 x509_crl_free( crl );
1735 return( ret );
1736 }
1737 }
1738
1739 /*
1740 * revokedCertificates SEQUENCE OF SEQUENCE {
1741 * userCertificate CertificateSerialNumber,
1742 * revocationDate Time,
1743 * crlEntryExtensions Extensions OPTIONAL
1744 * -- if present, MUST be v2
1745 * } OPTIONAL
1746 */
1747 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
1750 return( ret );
1751 }
1752
1753 /*
1754 * crlExtensions EXPLICIT Extensions OPTIONAL
1755 * -- if present, MUST be v2
1756 */
1757 if( crl->version == 2 )
1758 {
1759 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1760
1761 if( ret != 0 )
1762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766 }
1767
1768 if( p != end )
1769 {
1770 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001771 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1773 }
1774
1775 end = crl->raw.p + crl->raw.len;
1776
1777 /*
1778 * signatureAlgorithm AlgorithmIdentifier,
1779 * signatureValue BIT STRING
1780 */
1781 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1782 {
1783 x509_crl_free( crl );
1784 return( ret );
1785 }
1786
Paul Bakker535e97d2012-08-23 10:49:55 +00001787 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1788 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001789 {
1790 x509_crl_free( crl );
1791 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1792 }
1793
1794 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1795 {
1796 x509_crl_free( crl );
1797 return( ret );
1798 }
1799
1800 if( p != end )
1801 {
1802 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001803 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1805 }
1806
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 return( 0 );
1808}
1809
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001810/*
1811 * Parse one or more CRLs and add them to the chained list
1812 */
1813int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
1814{
1815#if defined(POLARSSL_PEM_C)
1816 int ret;
1817 size_t use_len;
1818 pem_context pem;
1819 int is_pem = 0;
1820
1821 if( chain == NULL || buf == NULL )
1822 return( POLARSSL_ERR_X509_INVALID_INPUT );
1823
1824 do
1825 {
1826 pem_init( &pem );
1827 ret = pem_read_buffer( &pem,
1828 (char *) "-----BEGIN X509 CRL-----",
1829 (char *) "-----END X509 CRL-----",
1830 buf, NULL, 0, &use_len );
1831
1832 if( ret == 0 )
1833 {
1834 /*
1835 * Was PEM encoded
1836 */
1837 is_pem = 1;
1838
1839 buflen -= use_len;
1840 buf += use_len;
1841
1842 if( ( ret = x509_crl_parse_der( chain,
1843 pem.buf, pem.buflen ) ) != 0 )
1844 {
1845 return( ret );
1846 }
1847
1848 pem_free( &pem );
1849 }
1850 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1851 {
1852 pem_free( &pem );
1853 return( ret );
1854 }
1855 }
1856 while( is_pem && buflen > 0 );
1857
1858 if( is_pem )
1859 return( 0 );
1860 else
1861#endif /* POLARSSL_PEM_C */
1862 return( x509_crl_parse_der( chain, buf, buflen ) );
1863}
1864
Paul Bakker335db3f2011-04-25 15:28:35 +00001865#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001866/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001867 * Load all data from a file into a given buffer.
1868 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001869static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001870{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001871 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001872 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001875 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001878 if( ( size = ftell( f ) ) == -1 )
1879 {
1880 fclose( f );
1881 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1882 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001883 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001884
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001885 *n = (size_t) size;
1886
1887 if( *n + 1 == 0 ||
1888 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1889 {
1890 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001891 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001892 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001893
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894 if( fread( *buf, 1, *n, f ) != *n )
1895 {
1896 fclose( f );
1897 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001898 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001900
Paul Bakkerd98030e2009-05-02 15:13:40 +00001901 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001902
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001904
Paul Bakkerd98030e2009-05-02 15:13:40 +00001905 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001906}
1907
1908/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 * Load one or more certificates and add them to the chained list
1910 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001911int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001912{
1913 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 size_t n;
1915 unsigned char *buf;
1916
Paul Bakker69e095c2011-12-10 21:55:01 +00001917 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Paul Bakker69e095c2011-12-10 21:55:01 +00001920 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
1922 memset( buf, 0, n + 1 );
1923 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 return( ret );
1926}
1927
Paul Bakker8d914582012-06-04 12:46:42 +00001928int x509parse_crtpath( x509_cert *chain, const char *path )
1929{
1930 int ret = 0;
1931#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001932 int w_ret;
1933 WCHAR szDir[MAX_PATH];
1934 char filename[MAX_PATH];
1935 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001936 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001937
Paul Bakker97872ac2012-11-02 12:53:26 +00001938 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001939 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001940
1941 if( len > MAX_PATH - 3 )
1942 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001943
Paul Bakker3338b792012-10-01 21:13:10 +00001944 memset( szDir, 0, sizeof(szDir) );
1945 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001946 memcpy( filename, path, len );
1947 filename[len++] = '\\';
1948 p = filename + len;
1949 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001950
Paul Bakker40cc9142014-07-07 15:16:47 +02001951 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001952
Paul Bakker97872ac2012-11-02 12:53:26 +00001953 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001954 if (hFind == INVALID_HANDLE_VALUE)
1955 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1956
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001957 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001958 do
1959 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001960 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001961
Paul Bakkere4791f32012-06-04 21:29:15 +00001962 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001963 continue;
1964
Paul Bakker3338b792012-10-01 21:13:10 +00001965 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1966 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001967 p, len - 1,
1968 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001969
Paul Bakker3338b792012-10-01 21:13:10 +00001970 w_ret = x509parse_crtfile( chain, filename );
1971 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001972 ret++;
1973 else
1974 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001975 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001976 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001977
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001978 if (GetLastError() != ERROR_NO_MORE_FILES)
1979 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001980
1981 FindClose( hFind );
1982#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001983#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001984 int t_ret, i;
1985 struct stat sb;
1986 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001987 char entry_name[255];
1988 DIR *dir = opendir( path );
1989
1990 if( dir == NULL)
1991 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1992
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001993 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001994 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001995 if( result == NULL )
1996 break;
1997
1998 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1999
2000 i = stat( entry_name, &sb );
2001
2002 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002003 {
2004 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002005 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002006 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002007
2008 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002009 continue;
2010
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002011 // Ignore parse errors
2012 //
Paul Bakker8d914582012-06-04 12:46:42 +00002013 t_ret = x509parse_crtfile( chain, entry_name );
2014 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002015 ret++;
2016 else
2017 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002018 }
2019 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002020#else /* POLARSSL_HAVE_READDIR_R */
2021 ((void) chain);
2022 ((void) path);
2023 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2024#endif /* POLARSSL_HAVE_READDIR_R */
2025#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002026
2027 return( ret );
2028}
2029
Paul Bakkerd98030e2009-05-02 15:13:40 +00002030/*
2031 * Load one or more CRLs and add them to the chained list
2032 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002033int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002034{
2035 int ret;
2036 size_t n;
2037 unsigned char *buf;
2038
Paul Bakker69e095c2011-12-10 21:55:01 +00002039 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2040 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002041
Paul Bakker27fdf462011-06-09 13:55:13 +00002042 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002043
2044 memset( buf, 0, n + 1 );
2045 free( buf );
2046
2047 return( ret );
2048}
2049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002051 * Load and parse a private RSA key
2052 */
2053int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2054{
2055 int ret;
2056 size_t n;
2057 unsigned char *buf;
2058
Paul Bakker69e095c2011-12-10 21:55:01 +00002059 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2060 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002061
2062 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002063 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002064 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002065 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002066 (unsigned char *) pwd, strlen( pwd ) );
2067
2068 memset( buf, 0, n + 1 );
2069 free( buf );
2070
2071 return( ret );
2072}
2073
2074/*
2075 * Load and parse a public RSA key
2076 */
2077int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2078{
2079 int ret;
2080 size_t n;
2081 unsigned char *buf;
2082
Paul Bakker69e095c2011-12-10 21:55:01 +00002083 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2084 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002085
Paul Bakker27fdf462011-06-09 13:55:13 +00002086 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002087
2088 memset( buf, 0, n + 1 );
2089 free( buf );
2090
2091 return( ret );
2092}
2093#endif /* POLARSSL_FS_IO */
2094
2095/*
Paul Bakker65a19092013-06-06 21:14:58 +02002096 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 */
Paul Bakker65a19092013-06-06 21:14:58 +02002098static int x509parse_key_pkcs1_der( rsa_context *rsa,
2099 const unsigned char *key,
2100 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002101{
Paul Bakker23986e52011-04-24 08:57:21 +00002102 int ret;
2103 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002105
Paul Bakker96743fc2011-02-12 14:30:57 +00002106 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002107 end = p + keylen;
2108
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002110 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002111 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 * RSAPrivateKey ::= SEQUENCE {
2113 * version Version,
2114 * modulus INTEGER, -- n
2115 * publicExponent INTEGER, -- e
2116 * privateExponent INTEGER, -- d
2117 * prime1 INTEGER, -- p
2118 * prime2 INTEGER, -- q
2119 * exponent1 INTEGER, -- d mod (p-1)
2120 * exponent2 INTEGER, -- d mod (q-1)
2121 * coefficient INTEGER, -- (inverse of q) mod p
2122 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2123 * }
2124 */
2125 if( ( ret = asn1_get_tag( &p, end, &len,
2126 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2127 {
Paul Bakker9d781402011-05-09 16:17:09 +00002128 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 }
2130
2131 end = p + len;
2132
2133 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2134 {
Paul Bakker9d781402011-05-09 16:17:09 +00002135 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 }
2137
2138 if( rsa->ver != 0 )
2139 {
Paul Bakker9d781402011-05-09 16:17:09 +00002140 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
2143 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2144 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2145 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2146 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2147 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2148 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2149 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2150 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2151 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002153 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 }
2155
2156 rsa->len = mpi_size( &rsa->N );
2157
2158 if( p != end )
2159 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002160 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002161 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002162 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 }
2164
2165 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2166 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 rsa_free( rsa );
2168 return( ret );
2169 }
2170
Paul Bakker65a19092013-06-06 21:14:58 +02002171 return( 0 );
2172}
2173
2174/*
2175 * Parse an unencrypted PKCS#8 encoded private RSA key
2176 */
2177static int x509parse_key_pkcs8_unencrypted_der(
2178 rsa_context *rsa,
2179 const unsigned char *key,
2180 size_t keylen )
2181{
2182 int ret;
2183 size_t len;
2184 unsigned char *p, *end;
2185 x509_buf pk_alg_oid;
2186
2187 p = (unsigned char *) key;
2188 end = p + keylen;
2189
2190 /*
2191 * This function parses the PrivatKeyInfo object (PKCS#8)
2192 *
2193 * PrivateKeyInfo ::= SEQUENCE {
2194 * version Version,
2195 * algorithm AlgorithmIdentifier,
2196 * PrivateKey BIT STRING
2197 * }
2198 *
2199 * AlgorithmIdentifier ::= SEQUENCE {
2200 * algorithm OBJECT IDENTIFIER,
2201 * parameters ANY DEFINED BY algorithm OPTIONAL
2202 * }
2203 *
2204 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2205 */
2206 if( ( ret = asn1_get_tag( &p, end, &len,
2207 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2208 {
2209 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2210 }
2211
2212 end = p + len;
2213
2214 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2215 {
2216 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2217 }
2218
2219 if( rsa->ver != 0 )
2220 {
2221 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2222 }
2223
2224 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2225 {
2226 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2227 }
2228
2229 /*
2230 * only RSA keys handled at this time
2231 */
2232 if( pk_alg_oid.len != 9 ||
2233 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2234 {
2235 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2236 }
2237
2238 /*
2239 * Get the OCTET STRING and parse the PKCS#1 format inside
2240 */
2241 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2242 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2243
2244 if( ( end - p ) < 1 )
2245 {
2246 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2247 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2248 }
2249
2250 end = p + len;
2251
2252 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2253 return( ret );
2254
2255 return( 0 );
2256}
2257
2258/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002259 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002260 */
2261static int x509parse_key_pkcs8_encrypted_der(
2262 rsa_context *rsa,
2263 const unsigned char *key,
2264 size_t keylen,
2265 const unsigned char *pwd,
2266 size_t pwdlen )
2267{
2268 int ret;
2269 size_t len;
2270 unsigned char *p, *end, *end2;
2271 x509_buf pbe_alg_oid, pbe_params;
2272 unsigned char buf[2048];
2273
2274 memset(buf, 0, 2048);
2275
2276 p = (unsigned char *) key;
2277 end = p + keylen;
2278
Paul Bakker1fd43212013-06-17 15:14:42 +02002279 if( pwdlen == 0 )
2280 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2281
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002282 /*
2283 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2284 *
2285 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2286 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2287 * encryptedData EncryptedData
2288 * }
2289 *
2290 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2291 *
2292 * EncryptedData ::= OCTET STRING
2293 *
2294 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2295 */
2296 if( ( ret = asn1_get_tag( &p, end, &len,
2297 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2298 {
2299 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2300 }
2301
2302 end = p + len;
2303
2304 if( ( ret = asn1_get_tag( &p, end, &len,
2305 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2306 {
2307 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2308 }
2309
2310 end2 = p + len;
2311
2312 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2313 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2314
2315 pbe_alg_oid.p = p;
2316 p += pbe_alg_oid.len;
2317
2318 /*
2319 * Store the algorithm parameters
2320 */
2321 pbe_params.p = p;
2322 pbe_params.len = end2 - p;
2323 p += pbe_params.len;
2324
2325 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2326 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2327
2328 // buf has been sized to 2048 bytes
2329 if( len > 2048 )
2330 return( POLARSSL_ERR_X509_INVALID_INPUT );
2331
2332 /*
2333 * Decrypt EncryptedData with appropriate PDE
2334 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002335#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002336 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2337 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002338 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2339 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2340 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002341 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002342 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2343 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2344
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002345 return( ret );
2346 }
2347 }
2348 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2349 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002350 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2351 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2352 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002353 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002354 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2355 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2356
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002357 return( ret );
2358 }
2359 }
2360 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2361 {
2362 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2363 PKCS12_PBE_DECRYPT,
2364 pwd, pwdlen,
2365 p, len, buf ) ) != 0 )
2366 {
2367 return( ret );
2368 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002369
2370 // Best guess for password mismatch when using RC4. If first tag is
2371 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2372 //
2373 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2374 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002375 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002376 else
2377#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002378#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002379 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002380 {
2381 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2382 p, len, buf ) ) != 0 )
2383 {
2384 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2385 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2386
2387 return( ret );
2388 }
2389 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002390 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002391#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002392 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2393
2394 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2395}
2396
2397/*
Paul Bakker65a19092013-06-06 21:14:58 +02002398 * Parse a private RSA key
2399 */
2400int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2401 const unsigned char *pwd, size_t pwdlen )
2402{
2403 int ret;
2404
Paul Bakker96743fc2011-02-12 14:30:57 +00002405#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002406 size_t len;
2407 pem_context pem;
2408
2409 pem_init( &pem );
2410 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002411 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2412 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002413 key, pwd, pwdlen, &len );
2414 if( ret == 0 )
2415 {
2416 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2417 {
2418 rsa_free( rsa );
2419 }
2420
2421 pem_free( &pem );
2422 return( ret );
2423 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002424 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2425 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2426 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2427 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002428 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002429 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002430
2431 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002432 (char *) "-----BEGIN PRIVATE KEY-----",
2433 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002434 key, NULL, 0, &len );
2435 if( ret == 0 )
2436 {
2437 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2438 pem.buf, pem.buflen ) ) != 0 )
2439 {
2440 rsa_free( rsa );
2441 }
2442
2443 pem_free( &pem );
2444 return( ret );
2445 }
2446 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002447 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002448
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002449 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002450 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2451 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002452 key, NULL, 0, &len );
2453 if( ret == 0 )
2454 {
2455 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2456 pem.buf, pem.buflen,
2457 pwd, pwdlen ) ) != 0 )
2458 {
2459 rsa_free( rsa );
2460 }
2461
2462 pem_free( &pem );
2463 return( ret );
2464 }
2465 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002466 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002467#else
2468 ((void) pwd);
2469 ((void) pwdlen);
2470#endif /* POLARSSL_PEM_C */
2471
2472 // At this point we only know it's not a PEM formatted key. Could be any
2473 // of the known DER encoded private key formats
2474 //
2475 // We try the different DER format parsers to see if one passes without
2476 // error
2477 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002478 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2479 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002480 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002481 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002484 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002485
2486 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2487 {
2488 return( ret );
2489 }
2490
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002491 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2492 return( 0 );
2493
2494 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002495
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002496 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2497 return( 0 );
2498
2499 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002500
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002501 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502}
2503
2504/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002505 * Parse a public RSA key
2506 */
Paul Bakker23986e52011-04-24 08:57:21 +00002507int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002508{
Paul Bakker23986e52011-04-24 08:57:21 +00002509 int ret;
2510 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002511 unsigned char *p, *end;
2512 x509_buf alg_oid;
2513#if defined(POLARSSL_PEM_C)
2514 pem_context pem;
2515
2516 pem_init( &pem );
2517 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002518 (char *) "-----BEGIN PUBLIC KEY-----",
2519 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002520 key, NULL, 0, &len );
2521
2522 if( ret == 0 )
2523 {
2524 /*
2525 * Was PEM encoded
2526 */
2527 keylen = pem.buflen;
2528 }
Paul Bakker9255e832013-06-06 14:58:28 +02002529 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002530 {
2531 pem_free( &pem );
2532 return( ret );
2533 }
2534
2535 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2536#else
2537 p = (unsigned char *) key;
2538#endif
2539 end = p + keylen;
2540
2541 /*
2542 * PublicKeyInfo ::= SEQUENCE {
2543 * algorithm AlgorithmIdentifier,
2544 * PublicKey BIT STRING
2545 * }
2546 *
2547 * AlgorithmIdentifier ::= SEQUENCE {
2548 * algorithm OBJECT IDENTIFIER,
2549 * parameters ANY DEFINED BY algorithm OPTIONAL
2550 * }
2551 *
2552 * RSAPublicKey ::= SEQUENCE {
2553 * modulus INTEGER, -- n
2554 * publicExponent INTEGER -- e
2555 * }
2556 */
2557
2558 if( ( ret = asn1_get_tag( &p, end, &len,
2559 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2560 {
2561#if defined(POLARSSL_PEM_C)
2562 pem_free( &pem );
2563#endif
2564 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002565 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002566 }
2567
2568 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 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_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002575 }
2576
2577 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2578 {
2579#if defined(POLARSSL_PEM_C)
2580 pem_free( &pem );
2581#endif
2582 rsa_free( rsa );
2583 return( ret );
2584 }
2585
2586 rsa->len = mpi_size( &rsa->N );
2587
2588#if defined(POLARSSL_PEM_C)
2589 pem_free( &pem );
2590#endif
2591
2592 return( 0 );
2593}
2594
Paul Bakkereaa89f82011-04-04 21:36:15 +00002595#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002596/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002597 * Parse DHM parameters
2598 */
Paul Bakker23986e52011-04-24 08:57:21 +00002599int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002600{
Paul Bakker23986e52011-04-24 08:57:21 +00002601 int ret;
2602 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002603 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002604#if defined(POLARSSL_PEM_C)
2605 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002606
Paul Bakker96743fc2011-02-12 14:30:57 +00002607 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002608
Paul Bakker96743fc2011-02-12 14:30:57 +00002609 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002610 (char *) "-----BEGIN DH PARAMETERS-----",
2611 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002612 dhmin, NULL, 0, &dhminlen );
2613
2614 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002615 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002616 /*
2617 * Was PEM encoded
2618 */
2619 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002620 }
Paul Bakker9255e832013-06-06 14:58:28 +02002621 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002622 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002623 pem_free( &pem );
2624 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002625 }
2626
Paul Bakker96743fc2011-02-12 14:30:57 +00002627 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2628#else
2629 p = (unsigned char *) dhmin;
2630#endif
2631 end = p + dhminlen;
2632
Paul Bakker1b57b062011-01-06 15:48:19 +00002633 memset( dhm, 0, sizeof( dhm_context ) );
2634
Paul Bakker1b57b062011-01-06 15:48:19 +00002635 /*
2636 * DHParams ::= SEQUENCE {
2637 * prime INTEGER, -- P
2638 * generator INTEGER, -- g
2639 * }
2640 */
2641 if( ( ret = asn1_get_tag( &p, end, &len,
2642 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2643 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002644#if defined(POLARSSL_PEM_C)
2645 pem_free( &pem );
2646#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002647 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002648 }
2649
2650 end = p + len;
2651
2652 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2653 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2654 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002655#if defined(POLARSSL_PEM_C)
2656 pem_free( &pem );
2657#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002658 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002659 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002660 }
2661
2662 if( p != end )
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 +
Paul Bakker1b57b062011-01-06 15:48:19 +00002669 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2670 }
2671
Paul Bakker96743fc2011-02-12 14:30:57 +00002672#if defined(POLARSSL_PEM_C)
2673 pem_free( &pem );
2674#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002675
2676 return( 0 );
2677}
2678
Paul Bakker335db3f2011-04-25 15:28:35 +00002679#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002680/*
2681 * Load and parse a private RSA key
2682 */
2683int x509parse_dhmfile( dhm_context *dhm, const char *path )
2684{
2685 int ret;
2686 size_t n;
2687 unsigned char *buf;
2688
Paul Bakker69e095c2011-12-10 21:55:01 +00002689 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2690 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002691
Paul Bakker27fdf462011-06-09 13:55:13 +00002692 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002693
2694 memset( buf, 0, n + 1 );
2695 free( buf );
2696
2697 return( ret );
2698}
Paul Bakker335db3f2011-04-25 15:28:35 +00002699#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002700#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002701
Paul Bakker5121ce52009-01-03 21:22:43 +00002702#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002703#include <stdarg.h>
2704
2705#if !defined vsnprintf
2706#define vsnprintf _vsnprintf
2707#endif // vsnprintf
2708
2709/*
2710 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2711 * Result value is not size of buffer needed, but -1 if no fit is possible.
2712 *
2713 * This fuction tries to 'fix' this by at least suggesting enlarging the
2714 * size by 20.
2715 */
2716int compat_snprintf(char *str, size_t size, const char *format, ...)
2717{
2718 va_list ap;
2719 int res = -1;
2720
2721 va_start( ap, format );
2722
2723 res = vsnprintf( str, size, format, ap );
2724
2725 va_end( ap );
2726
2727 // No quick fix possible
2728 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002729 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002730
2731 return res;
2732}
2733
2734#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002735#endif
2736
Paul Bakkerd98030e2009-05-02 15:13:40 +00002737#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2738
2739#define SAFE_SNPRINTF() \
2740{ \
2741 if( ret == -1 ) \
2742 return( -1 ); \
2743 \
Paul Bakker23986e52011-04-24 08:57:21 +00002744 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002745 p[n - 1] = '\0'; \
2746 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2747 } \
2748 \
Paul Bakker23986e52011-04-24 08:57:21 +00002749 n -= (unsigned int) ret; \
2750 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002751}
2752
Paul Bakker5121ce52009-01-03 21:22:43 +00002753/*
2754 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002755 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002757int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002758{
Paul Bakker23986e52011-04-24 08:57:21 +00002759 int ret;
2760 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002761 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002762 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 char s[128], *p;
2764
2765 memset( s, 0, sizeof( s ) );
2766
2767 name = dn;
2768 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002769 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
2771 while( name != NULL )
2772 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002773 if( !name->oid.p )
2774 {
2775 name = name->next;
2776 continue;
2777 }
2778
Paul Bakker74111d32011-01-15 16:57:55 +00002779 if( name != dn )
2780 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002781 ret = snprintf( p, n, ", " );
2782 SAFE_SNPRINTF();
2783 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002784
Paul Bakker535e97d2012-08-23 10:49:55 +00002785 if( name->oid.len == 3 &&
2786 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 {
2788 switch( name->oid.p[2] )
2789 {
2790 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002791 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
2793 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
2796 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002797 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
2799 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
2802 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002803 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002806 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002810 name->oid.p[2] );
2811 break;
2812 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002813 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002815 else if( name->oid.len == 9 &&
2816 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 {
2818 switch( name->oid.p[8] )
2819 {
2820 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002821 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
2823 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002824 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 name->oid.p[8] );
2826 break;
2827 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002828 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 }
2830 else
Paul Bakker74111d32011-01-15 16:57:55 +00002831 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002832 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002833 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
2836 for( i = 0; i < name->val.len; i++ )
2837 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002838 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 break;
2840
2841 c = name->val.p[i];
2842 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2843 s[i] = '?';
2844 else s[i] = c;
2845 }
2846 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002847 ret = snprintf( p, n, "%s", s );
2848 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 name = name->next;
2850 }
2851
Paul Bakker23986e52011-04-24 08:57:21 +00002852 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853}
2854
2855/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002856 * Store the serial in printable form into buf; no more
2857 * than size characters will be written
2858 */
2859int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2860{
Paul Bakker23986e52011-04-24 08:57:21 +00002861 int ret;
2862 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002863 char *p;
2864
2865 p = buf;
2866 n = size;
2867
2868 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002869 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002870
2871 for( i = 0; i < nr; i++ )
2872 {
Paul Bakker93048802011-12-05 14:38:06 +00002873 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002874 continue;
2875
Paul Bakkerdd476992011-01-16 21:34:59 +00002876 ret = snprintf( p, n, "%02X%s",
2877 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2878 SAFE_SNPRINTF();
2879 }
2880
Paul Bakker03c7c252011-11-25 12:37:37 +00002881 if( nr != serial->len )
2882 {
2883 ret = snprintf( p, n, "...." );
2884 SAFE_SNPRINTF();
2885 }
2886
Paul Bakker23986e52011-04-24 08:57:21 +00002887 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002888}
2889
2890/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002891 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002893int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2894 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002895{
Paul Bakker23986e52011-04-24 08:57:21 +00002896 int ret;
2897 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002898 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
2900 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002901 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakkerd98030e2009-05-02 15:13:40 +00002903 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002905 SAFE_SNPRINTF();
2906 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002908 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakkerdd476992011-01-16 21:34:59 +00002910 ret = x509parse_serial_gets( p, n, &crt->serial);
2911 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakkerd98030e2009-05-02 15:13:40 +00002913 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2914 SAFE_SNPRINTF();
2915 ret = x509parse_dn_gets( p, n, &crt->issuer );
2916 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Paul Bakkerd98030e2009-05-02 15:13:40 +00002918 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2919 SAFE_SNPRINTF();
2920 ret = x509parse_dn_gets( p, n, &crt->subject );
2921 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakkerd98030e2009-05-02 15:13:40 +00002923 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2925 crt->valid_from.year, crt->valid_from.mon,
2926 crt->valid_from.day, crt->valid_from.hour,
2927 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002928 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakkerd98030e2009-05-02 15:13:40 +00002930 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2932 crt->valid_to.year, crt->valid_to.mon,
2933 crt->valid_to.day, crt->valid_to.hour,
2934 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002935 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
Paul Bakkerd98030e2009-05-02 15:13:40 +00002937 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2938 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakker27d66162010-03-17 06:56:01 +00002940 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002942 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2943 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2944 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2945 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2946 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2947 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2948 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2949 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2950 default: ret = snprintf( p, n, "???" ); break;
2951 }
2952 SAFE_SNPRINTF();
2953
2954 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002955 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002956 SAFE_SNPRINTF();
2957
Paul Bakker23986e52011-04-24 08:57:21 +00002958 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002959}
2960
Paul Bakker74111d32011-01-15 16:57:55 +00002961/*
2962 * Return an informational string describing the given OID
2963 */
2964const char *x509_oid_get_description( x509_buf *oid )
2965{
2966 if ( oid == NULL )
2967 return ( NULL );
2968
2969 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2970 return( STRING_SERVER_AUTH );
2971
2972 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2973 return( STRING_CLIENT_AUTH );
2974
2975 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2976 return( STRING_CODE_SIGNING );
2977
2978 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2979 return( STRING_EMAIL_PROTECTION );
2980
2981 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2982 return( STRING_TIME_STAMPING );
2983
2984 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2985 return( STRING_OCSP_SIGNING );
2986
2987 return( NULL );
2988}
2989
2990/* Return the x.y.z.... style numeric string for the given OID */
2991int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2992{
Paul Bakker23986e52011-04-24 08:57:21 +00002993 int ret;
2994 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002995 unsigned int value;
2996 char *p;
2997
2998 p = buf;
2999 n = size;
3000
3001 /* First byte contains first two dots */
3002 if( oid->len > 0 )
3003 {
3004 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3005 SAFE_SNPRINTF();
3006 }
3007
3008 /* TODO: value can overflow in value. */
3009 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003010 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003011 {
3012 value <<= 7;
3013 value += oid->p[i] & 0x7F;
3014
3015 if( !( oid->p[i] & 0x80 ) )
3016 {
3017 /* Last byte */
3018 ret = snprintf( p, n, ".%d", value );
3019 SAFE_SNPRINTF();
3020 value = 0;
3021 }
3022 }
3023
Paul Bakker23986e52011-04-24 08:57:21 +00003024 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003025}
3026
Paul Bakkerd98030e2009-05-02 15:13:40 +00003027/*
3028 * Return an informational string about the CRL.
3029 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003030int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3031 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003032{
Paul Bakker23986e52011-04-24 08:57:21 +00003033 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003034 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003035 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003036 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003037
3038 p = buf;
3039 n = size;
3040
3041 ret = snprintf( p, n, "%sCRL version : %d",
3042 prefix, crl->version );
3043 SAFE_SNPRINTF();
3044
3045 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3046 SAFE_SNPRINTF();
3047 ret = x509parse_dn_gets( p, n, &crl->issuer );
3048 SAFE_SNPRINTF();
3049
3050 ret = snprintf( p, n, "\n%sthis update : " \
3051 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3052 crl->this_update.year, crl->this_update.mon,
3053 crl->this_update.day, crl->this_update.hour,
3054 crl->this_update.min, crl->this_update.sec );
3055 SAFE_SNPRINTF();
3056
3057 ret = snprintf( p, n, "\n%snext update : " \
3058 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3059 crl->next_update.year, crl->next_update.mon,
3060 crl->next_update.day, crl->next_update.hour,
3061 crl->next_update.min, crl->next_update.sec );
3062 SAFE_SNPRINTF();
3063
3064 entry = &crl->entry;
3065
3066 ret = snprintf( p, n, "\n%sRevoked certificates:",
3067 prefix );
3068 SAFE_SNPRINTF();
3069
Paul Bakker9be19372009-07-27 20:21:53 +00003070 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003071 {
3072 ret = snprintf( p, n, "\n%sserial number: ",
3073 prefix );
3074 SAFE_SNPRINTF();
3075
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003076 ret = x509parse_serial_gets( p, n, &entry->serial);
3077 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003078
Paul Bakkerd98030e2009-05-02 15:13:40 +00003079 ret = snprintf( p, n, " revocation date: " \
3080 "%04d-%02d-%02d %02d:%02d:%02d",
3081 entry->revocation_date.year, entry->revocation_date.mon,
3082 entry->revocation_date.day, entry->revocation_date.hour,
3083 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003084 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003085
3086 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 }
3088
Paul Bakkerd98030e2009-05-02 15:13:40 +00003089 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3090 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Paul Bakker27d66162010-03-17 06:56:01 +00003092 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003093 {
3094 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3095 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3096 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3097 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3098 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3099 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3100 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3101 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3102 default: ret = snprintf( p, n, "???" ); break;
3103 }
3104 SAFE_SNPRINTF();
3105
Paul Bakker1e27bb22009-07-19 20:25:25 +00003106 ret = snprintf( p, n, "\n" );
3107 SAFE_SNPRINTF();
3108
Paul Bakker23986e52011-04-24 08:57:21 +00003109 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003110}
3111
3112/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003113 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003115static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003116{
Paul Bakkercce9d772011-11-18 14:26:47 +00003117#if defined(_WIN32)
3118 SYSTEMTIME st;
3119
Paul Bakkerf48de952014-07-08 14:39:41 +02003120 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003121
Paul Bakker0d844dd2014-07-07 17:44:14 +02003122 now->year = st.wYear;
3123 now->mon = st.wMonth;
3124 now->day = st.wDay;
3125 now->hour = st.wHour;
3126 now->min = st.wMinute;
3127 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003128#else
Paul Bakker358a8412014-07-08 12:14:37 +02003129 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 time_t tt;
3131
3132 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003133 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Paul Bakker358a8412014-07-08 12:14:37 +02003135 now->year = lt.tm_year + 1900;
3136 now->mon = lt.tm_mon + 1;
3137 now->day = lt.tm_mday;
3138 now->hour = lt.tm_hour;
3139 now->min = lt.tm_min;
3140 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003141#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003142}
Paul Bakkercce9d772011-11-18 14:26:47 +00003143
Paul Bakker0d844dd2014-07-07 17:44:14 +02003144/*
3145 * Return 0 if before <= after, 1 otherwise
3146 */
3147static int x509_check_time( const x509_time *before, const x509_time *after )
3148{
3149 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003150 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151
Paul Bakker0d844dd2014-07-07 17:44:14 +02003152 if( before->year == after->year &&
3153 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003154 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakker0d844dd2014-07-07 17:44:14 +02003156 if( before->year == after->year &&
3157 before->mon == after->mon &&
3158 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003159 return( 1 );
3160
Paul Bakker0d844dd2014-07-07 17:44:14 +02003161 if( before->year == after->year &&
3162 before->mon == after->mon &&
3163 before->day == after->day &&
3164 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003165 return( 1 );
3166
Paul Bakker0d844dd2014-07-07 17:44:14 +02003167 if( before->year == after->year &&
3168 before->mon == after->mon &&
3169 before->day == after->day &&
3170 before->hour == after->hour &&
3171 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003172 return( 1 );
3173
Paul Bakker0d844dd2014-07-07 17:44:14 +02003174 if( before->year == after->year &&
3175 before->mon == after->mon &&
3176 before->day == after->day &&
3177 before->hour == after->hour &&
3178 before->min == after->min &&
3179 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003180 return( 1 );
3181
Paul Bakker40ea7de2009-05-03 10:18:48 +00003182 return( 0 );
3183}
3184
Paul Bakker0d844dd2014-07-07 17:44:14 +02003185int x509parse_time_expired( const x509_time *to )
3186{
3187 x509_time now;
3188
3189 x509_get_current_time( &now );
3190
3191 return( x509_check_time( &now, to ) );
3192}
3193
3194int x509parse_time_future( const x509_time *from )
3195{
3196 x509_time now;
3197
3198 x509_get_current_time( &now );
3199
3200 return( x509_check_time( from, &now ) );
3201}
3202
Paul Bakker40ea7de2009-05-03 10:18:48 +00003203/*
3204 * Return 1 if the certificate is revoked, or 0 otherwise.
3205 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003206int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003207{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003208 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003209
3210 while( cur != NULL && cur->serial.len != 0 )
3211 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003212 if( crt->serial.len == cur->serial.len &&
3213 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003214 {
3215 if( x509parse_time_expired( &cur->revocation_date ) )
3216 return( 1 );
3217 }
3218
3219 cur = cur->next;
3220 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
3222 return( 0 );
3223}
3224
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003225/*
3226 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003227 */
Paul Bakker23986e52011-04-24 08:57:21 +00003228static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 unsigned char *out )
3230{
3231 switch( alg )
3232 {
Paul Bakker40e46942009-01-03 21:51:57 +00003233#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003234 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003235#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003236#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003237 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003239#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003240 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003241#endif
3242#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003243 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003244#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003245#if defined(POLARSSL_SHA2_C)
3246 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3247 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3248#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003249#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003250 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3251 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003254 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 break;
3256 }
3257}
3258
3259/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003260 * Check that the given certificate is valid accoring to the CRL.
3261 */
3262static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3263 x509_crl *crl_list)
3264{
3265 int flags = 0;
3266 int hash_id;
3267 unsigned char hash[64];
3268
Paul Bakker915275b2012-09-28 07:10:55 +00003269 if( ca == NULL )
3270 return( flags );
3271
Paul Bakker76fd75a2011-01-16 21:12:10 +00003272 /*
3273 * TODO: What happens if no CRL is present?
3274 * Suggestion: Revocation state should be unknown if no CRL is present.
3275 * For backwards compatibility this is not yet implemented.
3276 */
3277
Paul Bakker915275b2012-09-28 07:10:55 +00003278 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003279 {
Paul Bakker915275b2012-09-28 07:10:55 +00003280 if( crl_list->version == 0 ||
3281 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003282 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3283 crl_list->issuer_raw.len ) != 0 )
3284 {
3285 crl_list = crl_list->next;
3286 continue;
3287 }
3288
3289 /*
3290 * Check if CRL is correctly signed by the trusted CA
3291 */
3292 hash_id = crl_list->sig_alg;
3293
3294 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3295
Paul Bakker43f97992013-09-23 11:23:31 +02003296 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003297 0, hash, crl_list->sig.p ) == 0 )
3298 {
3299 /*
3300 * CRL is not trusted
3301 */
3302 flags |= BADCRL_NOT_TRUSTED;
3303 break;
3304 }
3305
3306 /*
3307 * Check for validity of CRL (Do not drop out)
3308 */
3309 if( x509parse_time_expired( &crl_list->next_update ) )
3310 flags |= BADCRL_EXPIRED;
3311
Paul Bakker50a5c532014-07-08 10:59:10 +02003312 if( x509parse_time_future( &crl_list->this_update ) )
3313 flags |= BADCRL_FUTURE;
3314
Paul Bakker76fd75a2011-01-16 21:12:10 +00003315 /*
3316 * Check if certificate is revoked
3317 */
3318 if( x509parse_revoked(crt, crl_list) )
3319 {
3320 flags |= BADCERT_REVOKED;
3321 break;
3322 }
3323
3324 crl_list = crl_list->next;
3325 }
3326 return flags;
3327}
3328
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003329// Equal == 0, inequal == 1
3330static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3331{
3332 size_t i;
3333 unsigned char diff;
3334 const unsigned char *n1 = s1, *n2 = s2;
3335
3336 for( i = 0; i < len; i++ )
3337 {
3338 diff = n1[i] ^ n2[i];
3339
Paul Bakkerc941adb2014-07-07 14:17:24 +02003340 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003341 continue;
3342
Paul Bakkerc941adb2014-07-07 14:17:24 +02003343 if( diff == 32 &&
3344 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3345 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3346 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003347 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003348 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003349
3350 return( 1 );
3351 }
3352
3353 return( 0 );
3354}
3355
Paul Bakker1d073c52014-07-08 20:15:51 +02003356static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003357{
3358 size_t i;
3359 size_t cn_idx = 0;
3360
Paul Bakker57b12982012-02-11 17:38:38 +00003361 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003362 return( 0 );
3363
3364 for( i = 0; i < strlen( cn ); ++i )
3365 {
3366 if( cn[i] == '.' )
3367 {
3368 cn_idx = i;
3369 break;
3370 }
3371 }
3372
3373 if( cn_idx == 0 )
3374 return( 0 );
3375
Paul Bakker535e97d2012-08-23 10:49:55 +00003376 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003377 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003378 {
3379 return( 1 );
3380 }
3381
3382 return( 0 );
3383}
3384
Paul Bakker915275b2012-09-28 07:10:55 +00003385static int x509parse_verify_top(
3386 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003387 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003388 int (*f_vrfy)(void *, x509_cert *, int, int *),
3389 void *p_vrfy )
3390{
3391 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003392 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003393 unsigned char hash[64];
3394
3395 if( x509parse_time_expired( &child->valid_to ) )
3396 *flags |= BADCERT_EXPIRED;
3397
Paul Bakker50a5c532014-07-08 10:59:10 +02003398 if( x509parse_time_future( &child->valid_from ) )
3399 *flags |= BADCERT_FUTURE;
3400
Paul Bakker915275b2012-09-28 07:10:55 +00003401 /*
3402 * Child is the top of the chain. Check against the trust_ca list.
3403 */
3404 *flags |= BADCERT_NOT_TRUSTED;
3405
3406 while( trust_ca != NULL )
3407 {
3408 if( trust_ca->version == 0 ||
3409 child->issuer_raw.len != trust_ca->subject_raw.len ||
3410 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3411 child->issuer_raw.len ) != 0 )
3412 {
3413 trust_ca = trust_ca->next;
3414 continue;
3415 }
3416
Paul Bakker9a736322012-11-14 12:39:52 +00003417 /*
3418 * Reduce path_len to check against if top of the chain is
3419 * the same as the trusted CA
3420 */
3421 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3422 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3423 child->issuer_raw.len ) == 0 )
3424 {
3425 check_path_cnt--;
3426 }
3427
Paul Bakker915275b2012-09-28 07:10:55 +00003428 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003429 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003430 {
3431 trust_ca = trust_ca->next;
3432 continue;
3433 }
3434
3435 hash_id = child->sig_alg;
3436
3437 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3438
Paul Bakker43f97992013-09-23 11:23:31 +02003439 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003440 0, hash, child->sig.p ) != 0 )
3441 {
3442 trust_ca = trust_ca->next;
3443 continue;
3444 }
3445
3446 /*
3447 * Top of chain is signed by a trusted CA
3448 */
3449 *flags &= ~BADCERT_NOT_TRUSTED;
3450 break;
3451 }
3452
Paul Bakker9a736322012-11-14 12:39:52 +00003453 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003454 * If top of chain is not the same as the trusted CA send a verify request
3455 * to the callback for any issues with validity and CRL presence for the
3456 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003457 */
3458 if( trust_ca != NULL &&
3459 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3460 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3461 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003462 {
3463 /* Check trusted CA's CRL for then chain's top crt */
3464 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3465
3466 if( x509parse_time_expired( &trust_ca->valid_to ) )
3467 ca_flags |= BADCERT_EXPIRED;
3468
Paul Bakker50a5c532014-07-08 10:59:10 +02003469 if( x509parse_time_future( &trust_ca->valid_from ) )
3470 ca_flags |= BADCERT_FUTURE;
3471
Paul Bakker915275b2012-09-28 07:10:55 +00003472 if( NULL != f_vrfy )
3473 {
Paul Bakker9a736322012-11-14 12:39:52 +00003474 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003475 return( ret );
3476 }
3477 }
3478
3479 /* Call callback on top cert */
3480 if( NULL != f_vrfy )
3481 {
Paul Bakker9a736322012-11-14 12:39:52 +00003482 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003483 return( ret );
3484 }
3485
Paul Bakker915275b2012-09-28 07:10:55 +00003486 *flags |= ca_flags;
3487
3488 return( 0 );
3489}
3490
3491static int x509parse_verify_child(
3492 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003493 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003494 int (*f_vrfy)(void *, x509_cert *, int, int *),
3495 void *p_vrfy )
3496{
3497 int hash_id, ret;
3498 int parent_flags = 0;
3499 unsigned char hash[64];
3500 x509_cert *grandparent;
3501
Manuel Pégourié-Gonnard4cdb3ba2014-11-20 17:12:15 +01003502 /* path_cnt is 0 for the first intermediate CA */
3503 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
3504 {
3505 *flags |= BADCERT_NOT_TRUSTED;
3506 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
3507 }
3508
Paul Bakker915275b2012-09-28 07:10:55 +00003509 if( x509parse_time_expired( &child->valid_to ) )
3510 *flags |= BADCERT_EXPIRED;
3511
Paul Bakker50a5c532014-07-08 10:59:10 +02003512 if( x509parse_time_future( &child->valid_from ) )
3513 *flags |= BADCERT_FUTURE;
3514
Paul Bakker915275b2012-09-28 07:10:55 +00003515 hash_id = child->sig_alg;
3516
3517 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3518
Paul Bakker43f97992013-09-23 11:23:31 +02003519 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3520 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003521 *flags |= BADCERT_NOT_TRUSTED;
3522
3523 /* Check trusted CA's CRL for the given crt */
3524 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3525
3526 grandparent = parent->next;
3527
3528 while( grandparent != NULL )
3529 {
3530 if( grandparent->version == 0 ||
3531 grandparent->ca_istrue == 0 ||
3532 parent->issuer_raw.len != grandparent->subject_raw.len ||
3533 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3534 parent->issuer_raw.len ) != 0 )
3535 {
3536 grandparent = grandparent->next;
3537 continue;
3538 }
3539 break;
3540 }
3541
Paul Bakker915275b2012-09-28 07:10:55 +00003542 if( grandparent != NULL )
3543 {
3544 /*
3545 * Part of the chain
3546 */
Paul Bakker9a736322012-11-14 12:39:52 +00003547 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 +00003548 if( ret != 0 )
3549 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003550 }
Paul Bakker915275b2012-09-28 07:10:55 +00003551 else
3552 {
Paul Bakker9a736322012-11-14 12:39:52 +00003553 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 +00003554 if( ret != 0 )
3555 return( ret );
3556 }
3557
3558 /* child is verified to be a child of the parent, call verify callback */
3559 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003560 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003561 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003562
3563 *flags |= parent_flags;
3564
3565 return( 0 );
3566}
3567
Paul Bakker76fd75a2011-01-16 21:12:10 +00003568/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 * Verify the certificate validity
3570 */
3571int x509parse_verify( x509_cert *crt,
3572 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003573 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003574 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003575 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003576 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003577{
Paul Bakker23986e52011-04-24 08:57:21 +00003578 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003579 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003580 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003581 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003583 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
Paul Bakker40ea7de2009-05-03 10:18:48 +00003585 *flags = 0;
3586
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 if( cn != NULL )
3588 {
3589 name = &crt->subject;
3590 cn_len = strlen( cn );
3591
Paul Bakker4d2c1242012-05-10 14:12:46 +00003592 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003594 cur = &crt->subject_alt_names;
3595
3596 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003597 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003598 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003599 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003600 break;
3601
Paul Bakker535e97d2012-08-23 10:49:55 +00003602 if( cur->buf.len > 2 &&
3603 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003604 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003605 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Paul Bakker4d2c1242012-05-10 14:12:46 +00003607 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003608 }
3609
3610 if( cur == NULL )
3611 *flags |= BADCERT_CN_MISMATCH;
3612 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003613 else
3614 {
3615 while( name != NULL )
3616 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003617 if( name->oid.len == 3 &&
3618 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003619 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003620 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003621 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003622 break;
3623
Paul Bakker535e97d2012-08-23 10:49:55 +00003624 if( name->val.len > 2 &&
3625 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003626 x509_wildcard_verify( cn, &name->val ) )
3627 break;
3628 }
3629
3630 name = name->next;
3631 }
3632
3633 if( name == NULL )
3634 *flags |= BADCERT_CN_MISMATCH;
3635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 }
3637
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003639 * Iterate upwards in the given cert chain, to find our crt parent.
3640 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003642 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Paul Bakker76fd75a2011-01-16 21:12:10 +00003644 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003646 if( parent->ca_istrue == 0 ||
3647 crt->issuer_raw.len != parent->subject_raw.len ||
3648 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 crt->issuer_raw.len ) != 0 )
3650 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003651 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 continue;
3653 }
Paul Bakker915275b2012-09-28 07:10:55 +00003654 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 }
3656
Paul Bakker915275b2012-09-28 07:10:55 +00003657 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 {
Paul Bakker915275b2012-09-28 07:10:55 +00003659 /*
3660 * Part of the chain
3661 */
Paul Bakker9a736322012-11-14 12:39:52 +00003662 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003663 if( ret != 0 )
3664 return( ret );
3665 }
3666 else
Paul Bakker74111d32011-01-15 16:57:55 +00003667 {
Paul Bakker9a736322012-11-14 12:39:52 +00003668 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003669 if( ret != 0 )
3670 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003671 }
Paul Bakker915275b2012-09-28 07:10:55 +00003672
3673 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003674 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003675
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 return( 0 );
3677}
3678
3679/*
3680 * Unallocate all certificate data
3681 */
3682void x509_free( x509_cert *crt )
3683{
3684 x509_cert *cert_cur = crt;
3685 x509_cert *cert_prv;
3686 x509_name *name_cur;
3687 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003688 x509_sequence *seq_cur;
3689 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
3691 if( crt == NULL )
3692 return;
3693
3694 do
3695 {
3696 rsa_free( &cert_cur->rsa );
3697
3698 name_cur = cert_cur->issuer.next;
3699 while( name_cur != NULL )
3700 {
3701 name_prv = name_cur;
3702 name_cur = name_cur->next;
3703 memset( name_prv, 0, sizeof( x509_name ) );
3704 free( name_prv );
3705 }
3706
3707 name_cur = cert_cur->subject.next;
3708 while( name_cur != NULL )
3709 {
3710 name_prv = name_cur;
3711 name_cur = name_cur->next;
3712 memset( name_prv, 0, sizeof( x509_name ) );
3713 free( name_prv );
3714 }
3715
Paul Bakker74111d32011-01-15 16:57:55 +00003716 seq_cur = cert_cur->ext_key_usage.next;
3717 while( seq_cur != NULL )
3718 {
3719 seq_prv = seq_cur;
3720 seq_cur = seq_cur->next;
3721 memset( seq_prv, 0, sizeof( x509_sequence ) );
3722 free( seq_prv );
3723 }
3724
Paul Bakker8afa70d2012-02-11 18:42:45 +00003725 seq_cur = cert_cur->subject_alt_names.next;
3726 while( seq_cur != NULL )
3727 {
3728 seq_prv = seq_cur;
3729 seq_cur = seq_cur->next;
3730 memset( seq_prv, 0, sizeof( x509_sequence ) );
3731 free( seq_prv );
3732 }
3733
Paul Bakker5121ce52009-01-03 21:22:43 +00003734 if( cert_cur->raw.p != NULL )
3735 {
3736 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3737 free( cert_cur->raw.p );
3738 }
3739
3740 cert_cur = cert_cur->next;
3741 }
3742 while( cert_cur != NULL );
3743
3744 cert_cur = crt;
3745 do
3746 {
3747 cert_prv = cert_cur;
3748 cert_cur = cert_cur->next;
3749
3750 memset( cert_prv, 0, sizeof( x509_cert ) );
3751 if( cert_prv != crt )
3752 free( cert_prv );
3753 }
3754 while( cert_cur != NULL );
3755}
3756
Paul Bakkerd98030e2009-05-02 15:13:40 +00003757/*
3758 * Unallocate all CRL data
3759 */
3760void x509_crl_free( x509_crl *crl )
3761{
3762 x509_crl *crl_cur = crl;
3763 x509_crl *crl_prv;
3764 x509_name *name_cur;
3765 x509_name *name_prv;
3766 x509_crl_entry *entry_cur;
3767 x509_crl_entry *entry_prv;
3768
3769 if( crl == NULL )
3770 return;
3771
3772 do
3773 {
3774 name_cur = crl_cur->issuer.next;
3775 while( name_cur != NULL )
3776 {
3777 name_prv = name_cur;
3778 name_cur = name_cur->next;
3779 memset( name_prv, 0, sizeof( x509_name ) );
3780 free( name_prv );
3781 }
3782
3783 entry_cur = crl_cur->entry.next;
3784 while( entry_cur != NULL )
3785 {
3786 entry_prv = entry_cur;
3787 entry_cur = entry_cur->next;
3788 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3789 free( entry_prv );
3790 }
3791
3792 if( crl_cur->raw.p != NULL )
3793 {
3794 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3795 free( crl_cur->raw.p );
3796 }
3797
3798 crl_cur = crl_cur->next;
3799 }
3800 while( crl_cur != NULL );
3801
3802 crl_cur = crl;
3803 do
3804 {
3805 crl_prv = crl_cur;
3806 crl_cur = crl_cur->next;
3807
3808 memset( crl_prv, 0, sizeof( x509_crl ) );
3809 if( crl_prv != crl )
3810 free( crl_prv );
3811 }
3812 while( crl_cur != NULL );
3813}
3814
Paul Bakker40e46942009-01-03 21:51:57 +00003815#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
Paul Bakker40e46942009-01-03 21:51:57 +00003817#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
3819/*
3820 * Checkup routine
3821 */
3822int x509_self_test( int verbose )
3823{
Paul Bakker5690efc2011-05-26 13:16:06 +00003824#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003825 int ret;
3826 int flags;
3827 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 x509_cert cacert;
3829 x509_cert clicert;
3830 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003831#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003832 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
3835 if( verbose != 0 )
3836 printf( " X.509 certificate load: " );
3837
3838 memset( &clicert, 0, sizeof( x509_cert ) );
3839
Paul Bakkereae09db2013-06-06 12:35:54 +02003840 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003841 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003842 if( ret != 0 )
3843 {
3844 if( verbose != 0 )
3845 printf( "failed\n" );
3846
3847 return( ret );
3848 }
3849
3850 memset( &cacert, 0, sizeof( x509_cert ) );
3851
Paul Bakkereae09db2013-06-06 12:35:54 +02003852 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003853 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 if( ret != 0 )
3855 {
3856 if( verbose != 0 )
3857 printf( "failed\n" );
3858
3859 return( ret );
3860 }
3861
3862 if( verbose != 0 )
3863 printf( "passed\n X.509 private key load: " );
3864
3865 i = strlen( test_ca_key );
3866 j = strlen( test_ca_pwd );
3867
Paul Bakker66b78b22011-03-25 14:22:50 +00003868 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3869
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003871 (const unsigned char *) test_ca_key, i,
3872 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 {
3874 if( verbose != 0 )
3875 printf( "failed\n" );
3876
3877 return( ret );
3878 }
3879
3880 if( verbose != 0 )
3881 printf( "passed\n X.509 signature verify: ");
3882
Paul Bakker23986e52011-04-24 08:57:21 +00003883 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 if( ret != 0 )
3885 {
3886 if( verbose != 0 )
3887 printf( "failed\n" );
3888
3889 return( ret );
3890 }
3891
Paul Bakker5690efc2011-05-26 13:16:06 +00003892#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003894 printf( "passed\n X.509 DHM parameter load: " );
3895
3896 i = strlen( test_dhm_params );
3897 j = strlen( test_ca_pwd );
3898
Paul Bakkereae09db2013-06-06 12:35:54 +02003899 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003900 {
3901 if( verbose != 0 )
3902 printf( "failed\n" );
3903
3904 return( ret );
3905 }
3906
3907 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003909#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 x509_free( &cacert );
3912 x509_free( &clicert );
3913 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003914#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003915 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003916#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
3918 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003919#else
3920 ((void) verbose);
3921 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003923}
3924
3925#endif
3926
3927#endif