blob: f0cf3a5128cec50802b446ad68caec554f1031bb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, 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
Manuel Pégourié-Gonnard0c2fa1442015-04-23 10:52:49 +0200522 if( len-- < 2 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000523 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 sig->len = len;
526 sig->p = *p;
527
528 *p += len;
529
530 return( 0 );
531}
532
533/*
534 * X.509 v2/v3 unique identifier (not parsed)
535 */
536static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 x509_buf *uid, int n )
539{
540 int ret;
541
542 if( *p == end )
543 return( 0 );
544
545 uid->tag = **p;
546
547 if( ( ret = asn1_get_tag( p, end, &uid->len,
548 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
549 {
Paul Bakker40e46942009-01-03 21:51:57 +0000550 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 return( 0 );
552
553 return( ret );
554 }
555
556 uid->p = *p;
557 *p += uid->len;
558
559 return( 0 );
560}
561
562/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000563 * X.509 Extensions (No parsing of extensions, pointer should
564 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
566static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000568 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000569{
Paul Bakker23986e52011-04-24 08:57:21 +0000570 int ret;
571 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 if( *p == end )
574 return( 0 );
575
576 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000577
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000579 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 ext->p = *p;
583 end = *p + ext->len;
584
585 /*
586 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
587 *
588 * Extension ::= SEQUENCE {
589 * extnID OBJECT IDENTIFIER,
590 * critical BOOLEAN DEFAULT FALSE,
591 * extnValue OCTET STRING }
592 */
593 if( ( ret = asn1_get_tag( p, end, &len,
594 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000595 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000598 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000599 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Paul Bakkerd98030e2009-05-02 15:13:40 +0000601 return( 0 );
602}
603
604/*
605 * X.509 CRL v2 extensions (no extensions parsed yet.)
606 */
607static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000608 const unsigned char *end,
609 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000610{
Paul Bakker23986e52011-04-24 08:57:21 +0000611 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000612 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000613
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000614 /* Get explicit tag */
615 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000616 {
617 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
618 return( 0 );
619
620 return( ret );
621 }
622
623 while( *p < end )
624 {
625 if( ( ret = asn1_get_tag( p, end, &len,
626 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000627 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000628
629 *p += len;
630 }
631
632 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000633 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
635
636 return( 0 );
637}
638
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000639/*
640 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
641 */
642static int x509_get_crl_entry_ext( unsigned char **p,
643 const unsigned char *end,
644 x509_buf *ext )
645{
646 int ret;
647 size_t len = 0;
648
649 /* OPTIONAL */
650 if (end <= *p)
651 return( 0 );
652
653 ext->tag = **p;
654 ext->p = *p;
655
656 /*
657 * Get CRL-entry extension sequence header
658 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
659 */
660 if( ( ret = asn1_get_tag( p, end, &ext->len,
661 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
662 {
663 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
664 {
665 ext->p = NULL;
666 return( 0 );
667 }
668 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
669 }
670
671 end = *p + ext->len;
672
673 if( end != *p + ext->len )
674 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
675 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
676
677 while( *p < end )
678 {
679 if( ( ret = asn1_get_tag( p, end, &len,
680 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
681 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
682
683 *p += len;
684 }
685
686 if( *p != end )
687 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
688 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
689
690 return( 0 );
691}
692
Paul Bakker74111d32011-01-15 16:57:55 +0000693static int x509_get_basic_constraints( unsigned char **p,
694 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000695 int *ca_istrue,
696 int *max_pathlen )
697{
Paul Bakker23986e52011-04-24 08:57:21 +0000698 int ret;
699 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000700
701 /*
702 * BasicConstraints ::= SEQUENCE {
703 * cA BOOLEAN DEFAULT FALSE,
704 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
705 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000706 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000707 *max_pathlen = 0; /* endless */
708
709 if( ( ret = asn1_get_tag( p, end, &len,
710 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000711 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000712
713 if( *p == end )
714 return 0;
715
Paul Bakker3cccddb2011-01-16 21:46:31 +0000716 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000717 {
718 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000719 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000720
721 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000722 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000723
Paul Bakker3cccddb2011-01-16 21:46:31 +0000724 if( *ca_istrue != 0 )
725 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000726 }
727
728 if( *p == end )
729 return 0;
730
731 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000732 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000733
734 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000735 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000736 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
737
738 (*max_pathlen)++;
739
Paul Bakker74111d32011-01-15 16:57:55 +0000740 return 0;
741}
742
743static int x509_get_ns_cert_type( unsigned char **p,
744 const unsigned char *end,
745 unsigned char *ns_cert_type)
746{
747 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000748 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000749
750 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000751 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000752
753 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000754 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000755 POLARSSL_ERR_ASN1_INVALID_LENGTH );
756
757 /* Get actual bitstring */
758 *ns_cert_type = *bs.p;
759 return 0;
760}
761
762static int x509_get_key_usage( unsigned char **p,
763 const unsigned char *end,
764 unsigned char *key_usage)
765{
766 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000767 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000768
769 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000770 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000771
Paul Bakker94a67962012-08-23 13:03:52 +0000772 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000773 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000774 POLARSSL_ERR_ASN1_INVALID_LENGTH );
775
776 /* Get actual bitstring */
777 *key_usage = *bs.p;
778 return 0;
779}
780
Paul Bakkerd98030e2009-05-02 15:13:40 +0000781/*
Paul Bakker74111d32011-01-15 16:57:55 +0000782 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
783 *
784 * KeyPurposeId ::= OBJECT IDENTIFIER
785 */
786static int x509_get_ext_key_usage( unsigned char **p,
787 const unsigned char *end,
788 x509_sequence *ext_key_usage)
789{
790 int ret;
791
792 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000793 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000794
795 /* Sequence length must be >= 1 */
796 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000797 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000798 POLARSSL_ERR_ASN1_INVALID_LENGTH );
799
800 return 0;
801}
802
803/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000804 * SubjectAltName ::= GeneralNames
805 *
806 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
807 *
808 * GeneralName ::= CHOICE {
809 * otherName [0] OtherName,
810 * rfc822Name [1] IA5String,
811 * dNSName [2] IA5String,
812 * x400Address [3] ORAddress,
813 * directoryName [4] Name,
814 * ediPartyName [5] EDIPartyName,
815 * uniformResourceIdentifier [6] IA5String,
816 * iPAddress [7] OCTET STRING,
817 * registeredID [8] OBJECT IDENTIFIER }
818 *
819 * OtherName ::= SEQUENCE {
820 * type-id OBJECT IDENTIFIER,
821 * value [0] EXPLICIT ANY DEFINED BY type-id }
822 *
823 * EDIPartyName ::= SEQUENCE {
824 * nameAssigner [0] DirectoryString OPTIONAL,
825 * partyName [1] DirectoryString }
826 *
827 * NOTE: PolarSSL only parses and uses dNSName at this point.
828 */
829static int x509_get_subject_alt_name( unsigned char **p,
830 const unsigned char *end,
831 x509_sequence *subject_alt_name )
832{
833 int ret;
834 size_t len, tag_len;
835 asn1_buf *buf;
836 unsigned char tag;
837 asn1_sequence *cur = subject_alt_name;
838
839 /* Get main sequence tag */
840 if( ( ret = asn1_get_tag( p, end, &len,
841 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
842 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
843
844 if( *p + len != end )
845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
846 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
847
848 while( *p < end )
849 {
850 if( ( end - *p ) < 1 )
851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
852 POLARSSL_ERR_ASN1_OUT_OF_DATA );
853
854 tag = **p;
855 (*p)++;
856 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
858
859 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
860 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
861 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
862
863 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
864 {
865 *p += tag_len;
866 continue;
867 }
868
869 buf = &(cur->buf);
870 buf->tag = tag;
871 buf->p = *p;
872 buf->len = tag_len;
873 *p += buf->len;
874
875 /* Allocate and assign next pointer */
876 if (*p < end)
877 {
Manuel Pégourié-Gonnardfdec9572014-11-11 23:11:16 +0100878 if( cur->next != NULL )
879 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
880
Paul Bakkera8cd2392012-02-11 16:09:32 +0000881 cur->next = (asn1_sequence *) malloc(
882 sizeof( asn1_sequence ) );
883
884 if( cur->next == NULL )
885 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
886 POLARSSL_ERR_ASN1_MALLOC_FAILED );
887
Paul Bakker535e97d2012-08-23 10:49:55 +0000888 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000889 cur = cur->next;
890 }
891 }
892
893 /* Set final sequence entry's next pointer to NULL */
894 cur->next = NULL;
895
896 if( *p != end )
897 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
898 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
899
900 return( 0 );
901}
902
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100903static int x509_get_crt_ext_type( const x509_buf *oid )
904{
905 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == oid->len ) &&
906 memcmp( oid->p, OID_BASIC_CONSTRAINTS, oid->len ) == 0 )
907 {
908 return( EXT_BASIC_CONSTRAINTS );
909 }
910 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == oid->len ) &&
911 memcmp( oid->p, OID_NS_CERT_TYPE, oid->len ) == 0 )
912 {
913 return( EXT_NS_CERT_TYPE );
914 }
915 else if( ( OID_SIZE( OID_KEY_USAGE ) == oid->len ) &&
916 memcmp( oid->p, OID_KEY_USAGE, oid->len ) == 0 )
917 {
918 return( EXT_KEY_USAGE );
919 }
920 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == oid->len ) &&
921 memcmp( oid->p, OID_EXTENDED_KEY_USAGE, oid->len ) == 0 )
922 {
923 return( EXT_EXTENDED_KEY_USAGE );
924 }
925 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == oid->len ) &&
926 memcmp( oid->p, OID_SUBJECT_ALT_NAME, oid->len ) == 0 )
927 {
928 return( EXT_SUBJECT_ALT_NAME );
929 }
930
931 return( -1 );
932}
933
Paul Bakkera8cd2392012-02-11 16:09:32 +0000934/*
Paul Bakker74111d32011-01-15 16:57:55 +0000935 * X.509 v3 extensions
936 *
937 * TODO: Perform all of the basic constraints tests required by the RFC
938 * TODO: Set values for undetected extensions to a sane default?
939 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000940 */
941static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000942 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000943 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000944{
Paul Bakker23986e52011-04-24 08:57:21 +0000945 int ret;
946 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000947 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000948
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000949 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000950 {
951 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
952 return( 0 );
953
954 return( ret );
955 }
956
Paul Bakker5121ce52009-01-03 21:22:43 +0000957 while( *p < end )
958 {
Paul Bakker74111d32011-01-15 16:57:55 +0000959 /*
960 * Extension ::= SEQUENCE {
961 * extnID OBJECT IDENTIFIER,
962 * critical BOOLEAN DEFAULT FALSE,
963 * extnValue OCTET STRING }
964 */
965 x509_buf extn_oid = {0, 0, NULL};
966 int is_critical = 0; /* DEFAULT FALSE */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100967 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000968
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 if( ( ret = asn1_get_tag( p, end, &len,
970 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000971 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000973 end_ext_data = *p + len;
974
Paul Bakker74111d32011-01-15 16:57:55 +0000975 /* Get extension ID */
976 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000977
Paul Bakker74111d32011-01-15 16:57:55 +0000978 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000979 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000980
Paul Bakker74111d32011-01-15 16:57:55 +0000981 extn_oid.p = *p;
982 *p += extn_oid.len;
983
984 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000985 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000986 POLARSSL_ERR_ASN1_OUT_OF_DATA );
987
988 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000989 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000990 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000991 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
Paul Bakker74111d32011-01-15 16:57:55 +0000993 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000994 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000996 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000998 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000999
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001000 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001001 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001002 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Paul Bakker74111d32011-01-15 16:57:55 +00001004 /*
1005 * Detect supported extensions
1006 */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001007 ext_type = x509_get_crt_ext_type( &extn_oid );
1008
1009 if( ext_type < 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001010 {
1011 /* No parser found, skip extension */
1012 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Paul Bakker5c721f92011-07-27 16:51:09 +00001014#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001015 if( is_critical )
1016 {
1017 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001018 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001019 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1020 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001021#endif
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001022 continue;
1023 }
1024
1025 /* Forbid repeated extensions */
1026 if( ( crt->ext_types & ext_type ) != 0 )
1027 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
1028
1029 crt->ext_types |= ext_type;
1030
1031 switch( ext_type )
1032 {
1033 case EXT_BASIC_CONSTRAINTS:
1034 /* Parse basic constraints */
1035 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1036 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1037 return( ret );
1038 break;
1039
1040 case EXT_KEY_USAGE:
1041 /* Parse key usage */
1042 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1043 &crt->key_usage ) ) != 0 )
1044 return( ret );
1045 break;
1046
1047 case EXT_EXTENDED_KEY_USAGE:
1048 /* Parse extended key usage */
1049 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1050 &crt->ext_key_usage ) ) != 0 )
1051 return( ret );
1052 break;
1053
1054 case EXT_SUBJECT_ALT_NAME:
1055 /* Parse subject alt name */
1056 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1057 &crt->subject_alt_names ) ) != 0 )
1058 return( ret );
1059 break;
1060
1061 case EXT_NS_CERT_TYPE:
1062 /* Parse netscape certificate type */
1063 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1064 &crt->ns_cert_type ) ) != 0 )
1065 return( ret );
1066 break;
1067
1068 default:
1069 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 }
1072
1073 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001074 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001075 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 return( 0 );
1078}
1079
1080/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001081 * X.509 CRL Entries
1082 */
1083static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001084 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001085 x509_crl_entry *entry )
1086{
Paul Bakker23986e52011-04-24 08:57:21 +00001087 int ret;
1088 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001089 x509_crl_entry *cur_entry = entry;
1090
1091 if( *p == end )
1092 return( 0 );
1093
Paul Bakker9be19372009-07-27 20:21:53 +00001094 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001095 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1096 {
1097 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1098 return( 0 );
1099
1100 return( ret );
1101 }
1102
Paul Bakker9be19372009-07-27 20:21:53 +00001103 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001104
1105 while( *p < end )
1106 {
Paul Bakker23986e52011-04-24 08:57:21 +00001107 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001108 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001109
1110 if( ( ret = asn1_get_tag( p, end, &len2,
1111 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1112 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001113 return( ret );
1114 }
1115
Paul Bakker9be19372009-07-27 20:21:53 +00001116 cur_entry->raw.tag = **p;
1117 cur_entry->raw.p = *p;
1118 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001119 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001120
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001121 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001122 return( ret );
1123
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001124 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001125 return( ret );
1126
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001127 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001128 return( ret );
1129
Paul Bakker74111d32011-01-15 16:57:55 +00001130 if ( *p < end )
1131 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001132 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001133
1134 if( cur_entry->next == NULL )
1135 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1136
Paul Bakkerd98030e2009-05-02 15:13:40 +00001137 cur_entry = cur_entry->next;
1138 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1139 }
1140 }
1141
1142 return( 0 );
1143}
1144
Paul Bakker27d66162010-03-17 06:56:01 +00001145static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1146{
1147 if( sig_oid->len == 9 &&
1148 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1149 {
1150 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1151 {
1152 *sig_alg = sig_oid->p[8];
1153 return( 0 );
1154 }
1155
1156 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1157 {
1158 *sig_alg = sig_oid->p[8];
1159 return( 0 );
1160 }
1161
1162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1163 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001164 if( sig_oid->len == 5 &&
1165 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1166 {
1167 *sig_alg = SIG_RSA_SHA1;
1168 return( 0 );
1169 }
Paul Bakker27d66162010-03-17 06:56:01 +00001170
1171 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1172}
1173
Paul Bakkerd98030e2009-05-02 15:13:40 +00001174/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001175 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001177static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1178 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Paul Bakker23986e52011-04-24 08:57:21 +00001180 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001181 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001182 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Paul Bakker320a4b52009-03-28 18:52:39 +00001184 /*
1185 * Check for valid input
1186 */
1187 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001188 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001189
Paul Bakker96743fc2011-02-12 14:30:57 +00001190 p = (unsigned char *) malloc( len = buflen );
1191
1192 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001193 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001194
1195 memcpy( p, buf, buflen );
1196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 crt->raw.p = p;
1198 crt->raw.len = len;
1199 end = p + len;
1200
1201 /*
1202 * Certificate ::= SEQUENCE {
1203 * tbsCertificate TBSCertificate,
1204 * signatureAlgorithm AlgorithmIdentifier,
1205 * signatureValue BIT STRING }
1206 */
1207 if( ( ret = asn1_get_tag( &p, end, &len,
1208 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1209 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001210 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001211 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 }
1213
Paul Bakkerb00ca422012-09-25 12:10:00 +00001214 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001216 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001217 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001218 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001220 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001221
Paul Bakker5121ce52009-01-03 21:22:43 +00001222 /*
1223 * TBSCertificate ::= SEQUENCE {
1224 */
1225 crt->tbs.p = p;
1226
1227 if( ( ret = asn1_get_tag( &p, end, &len,
1228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1229 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001230 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001231 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 }
1233
1234 end = p + len;
1235 crt->tbs.len = end - crt->tbs.p;
1236
1237 /*
1238 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1239 *
1240 * CertificateSerialNumber ::= INTEGER
1241 *
1242 * signature AlgorithmIdentifier
1243 */
1244 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1245 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1246 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1247 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001248 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 return( ret );
1250 }
1251
1252 crt->version++;
1253
1254 if( crt->version > 3 )
1255 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001256 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001257 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259
Paul Bakker27d66162010-03-17 06:56:01 +00001260 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001263 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
1266 /*
1267 * issuer Name
1268 */
1269 crt->issuer_raw.p = p;
1270
1271 if( ( ret = asn1_get_tag( &p, end, &len,
1272 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001275 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
1277
1278 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1279 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001280 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 return( ret );
1282 }
1283
1284 crt->issuer_raw.len = p - crt->issuer_raw.p;
1285
1286 /*
1287 * Validity ::= SEQUENCE {
1288 * notBefore Time,
1289 * notAfter Time }
1290 *
1291 */
1292 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1293 &crt->valid_to ) ) != 0 )
1294 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 return( ret );
1297 }
1298
1299 /*
1300 * subject Name
1301 */
1302 crt->subject_raw.p = p;
1303
1304 if( ( ret = asn1_get_tag( &p, end, &len,
1305 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1306 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001307 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001308 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 }
1310
Paul Bakkercefb3962012-06-27 11:51:09 +00001311 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 return( ret );
1315 }
1316
1317 crt->subject_raw.len = p - crt->subject_raw.p;
1318
1319 /*
1320 * SubjectPublicKeyInfo ::= SEQUENCE
1321 * algorithm AlgorithmIdentifier,
1322 * subjectPublicKey BIT STRING }
1323 */
1324 if( ( ret = asn1_get_tag( &p, end, &len,
1325 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1326 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001327 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001328 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 }
1330
1331 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1332 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337
1338 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1339 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001340 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341 return( ret );
1342 }
1343
1344 crt->rsa.len = mpi_size( &crt->rsa.N );
1345
1346 /*
1347 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1348 * -- If present, version shall be v2 or v3
1349 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1350 * -- If present, version shall be v2 or v3
1351 * extensions [3] EXPLICIT Extensions OPTIONAL
1352 * -- If present, version shall be v3
1353 */
1354 if( crt->version == 2 || crt->version == 3 )
1355 {
1356 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1357 if( ret != 0 )
1358 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001359 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 return( ret );
1361 }
1362 }
1363
1364 if( crt->version == 2 || crt->version == 3 )
1365 {
1366 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1367 if( ret != 0 )
1368 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001369 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 return( ret );
1371 }
1372 }
1373
1374 if( crt->version == 3 )
1375 {
Paul Bakker74111d32011-01-15 16:57:55 +00001376 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 if( ret != 0 )
1378 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001379 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 return( ret );
1381 }
1382 }
1383
1384 if( p != end )
1385 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001386 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001387 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001388 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 }
1390
Paul Bakkerb00ca422012-09-25 12:10:00 +00001391 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
1393 /*
1394 * signatureAlgorithm AlgorithmIdentifier,
1395 * signatureValue BIT STRING
1396 */
1397 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1398 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001399 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400 return( ret );
1401 }
1402
Paul Bakker535e97d2012-08-23 10:49:55 +00001403 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1404 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001405 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001406 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001407 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 }
1409
1410 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1411 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001412 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 return( ret );
1414 }
1415
1416 if( p != end )
1417 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001418 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001419 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001420 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001423 return( 0 );
1424}
1425
1426/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001427 * Parse one X.509 certificate in DER format from a buffer and add them to a
1428 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001429 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001430int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001431{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001432 int ret;
1433 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001434
1435 /*
1436 * Check for valid input
1437 */
1438 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001439 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001440
1441 while( crt->version != 0 && crt->next != NULL )
1442 {
1443 prev = crt;
1444 crt = crt->next;
1445 }
1446
1447 /*
1448 * Add new certificate on the end of the chain if needed.
1449 */
1450 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001451 {
1452 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1453
Paul Bakker7d06ad22009-05-02 15:53:56 +00001454 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001455 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001456
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001457 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001458 crt = crt->next;
1459 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Paul Bakkerd6d41092013-06-13 09:00:25 +02001462 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1463 {
1464 if( prev )
1465 prev->next = NULL;
1466
1467 if( crt != chain )
1468 free( crt );
1469
1470 return( ret );
1471 }
1472
1473 return( 0 );
1474}
1475
1476/*
1477 * Parse one or more PEM certificates from a buffer and add them to the chained list
1478 */
1479int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1480{
1481 int ret, success = 0, first_error = 0, total_failed = 0;
1482 int buf_format = X509_FORMAT_DER;
1483
1484 /*
1485 * Check for valid input
1486 */
1487 if( chain == NULL || buf == NULL )
1488 return( POLARSSL_ERR_X509_INVALID_INPUT );
1489
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001490 /*
1491 * Determine buffer content. Buffer contains either one DER certificate or
1492 * one or more PEM certificates.
1493 */
1494#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001495 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001496 buf_format = X509_FORMAT_PEM;
1497#endif
1498
1499 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001500 return x509parse_crt_der( chain, buf, buflen );
1501
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001502#if defined(POLARSSL_PEM_C)
1503 if( buf_format == X509_FORMAT_PEM )
1504 {
1505 pem_context pem;
1506
1507 while( buflen > 0 )
1508 {
1509 size_t use_len;
1510 pem_init( &pem );
1511
1512 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001513 (char *) "-----BEGIN CERTIFICATE-----",
1514 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001515 buf, NULL, 0, &use_len );
1516
1517 if( ret == 0 )
1518 {
1519 /*
1520 * Was PEM encoded
1521 */
1522 buflen -= use_len;
1523 buf += use_len;
1524 }
Paul Bakker64171862013-06-06 15:01:18 +02001525 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1526 {
1527 return( ret );
1528 }
Paul Bakker9255e832013-06-06 14:58:28 +02001529 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001530 {
1531 pem_free( &pem );
1532
Paul Bakker64171862013-06-06 15:01:18 +02001533 /*
1534 * PEM header and footer were found
1535 */
1536 buflen -= use_len;
1537 buf += use_len;
1538
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001539 if( first_error == 0 )
1540 first_error = ret;
1541
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001542 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001543 continue;
1544 }
1545 else
1546 break;
1547
Paul Bakkerd6d41092013-06-13 09:00:25 +02001548 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001549
1550 pem_free( &pem );
1551
1552 if( ret != 0 )
1553 {
1554 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001555 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001556 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001557 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001558 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001559
1560 if( first_error == 0 )
1561 first_error = ret;
1562
Paul Bakkerd6d41092013-06-13 09:00:25 +02001563 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001564 continue;
1565 }
1566
1567 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001568 }
1569 }
1570#endif
1571
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001572 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001573 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001574 else if( first_error )
1575 return( first_error );
1576 else
1577 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001578}
1579
1580/*
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001581 * Parse one CRLs in DER format and append it to the chained list
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001583static int x509_crl_parse_der( x509_crl *chain,
1584 const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585{
Paul Bakker23986e52011-04-24 08:57:21 +00001586 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001587 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588 unsigned char *p, *end;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001589 x509_buf sig_params1, sig_params2;
1590 x509_crl *crl = chain;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001591
1592 /*
1593 * Check for valid input
1594 */
1595 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001596 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001598 memset( &sig_params1, 0, sizeof( x509_buf ) );
1599 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001600
1601 /*
1602 * Add new CRL on the end of the chain if needed.
1603 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001604 while( crl->version != 0 && crl->next != NULL )
1605 crl = crl->next;
1606
1607 if( crl->version != 0 && crl->next == NULL )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001608 {
1609 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1610
Paul Bakker7d06ad22009-05-02 15:53:56 +00001611 if( crl->next == NULL )
1612 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001614 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001615 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001616
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001617 memset( crl->next, 0, sizeof( x509_crl ) );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001618 crl = crl->next;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001619 }
1620
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001621 /*
1622 * Copy raw DER-encoded CRL
1623 */
1624 if( ( p = malloc( buflen ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001625 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001626
1627 memcpy( p, buf, buflen );
1628
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 crl->raw.p = p;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001630 crl->raw.len = buflen;
1631
1632 end = p + buflen;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001633
1634 /*
1635 * CertificateList ::= SEQUENCE {
1636 * tbsCertList TBSCertList,
1637 * signatureAlgorithm AlgorithmIdentifier,
1638 * signatureValue BIT STRING }
1639 */
1640 if( ( ret = asn1_get_tag( &p, end, &len,
1641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1642 {
1643 x509_crl_free( crl );
1644 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1645 }
1646
Paul Bakker23986e52011-04-24 08:57:21 +00001647 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001648 {
1649 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001650 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001651 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1652 }
1653
1654 /*
1655 * TBSCertList ::= SEQUENCE {
1656 */
1657 crl->tbs.p = p;
1658
1659 if( ( ret = asn1_get_tag( &p, end, &len,
1660 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1661 {
1662 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001663 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664 }
1665
1666 end = p + len;
1667 crl->tbs.len = end - crl->tbs.p;
1668
1669 /*
1670 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1671 * -- if present, MUST be v2
1672 *
1673 * signature AlgorithmIdentifier
1674 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001675 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001676 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1677 {
1678 x509_crl_free( crl );
1679 return( ret );
1680 }
1681
1682 crl->version++;
1683
1684 if( crl->version > 2 )
1685 {
1686 x509_crl_free( crl );
1687 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1688 }
1689
Paul Bakker27d66162010-03-17 06:56:01 +00001690 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 {
1692 x509_crl_free( crl );
1693 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1694 }
1695
1696 /*
1697 * issuer Name
1698 */
1699 crl->issuer_raw.p = p;
1700
1701 if( ( ret = asn1_get_tag( &p, end, &len,
1702 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1703 {
1704 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001705 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001706 }
1707
1708 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1709 {
1710 x509_crl_free( crl );
1711 return( ret );
1712 }
1713
1714 crl->issuer_raw.len = p - crl->issuer_raw.p;
1715
1716 /*
1717 * thisUpdate Time
1718 * nextUpdate Time OPTIONAL
1719 */
Paul Bakker91200182010-02-18 21:26:15 +00001720 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721 {
1722 x509_crl_free( crl );
1723 return( ret );
1724 }
1725
Paul Bakker91200182010-02-18 21:26:15 +00001726 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 {
Paul Bakker9d781402011-05-09 16:17:09 +00001728 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001729 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001730 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001731 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001732 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733 x509_crl_free( crl );
1734 return( ret );
1735 }
1736 }
1737
1738 /*
1739 * revokedCertificates SEQUENCE OF SEQUENCE {
1740 * userCertificate CertificateSerialNumber,
1741 * revocationDate Time,
1742 * crlEntryExtensions Extensions OPTIONAL
1743 * -- if present, MUST be v2
1744 * } OPTIONAL
1745 */
1746 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1747 {
1748 x509_crl_free( crl );
1749 return( ret );
1750 }
1751
1752 /*
1753 * crlExtensions EXPLICIT Extensions OPTIONAL
1754 * -- if present, MUST be v2
1755 */
1756 if( crl->version == 2 )
1757 {
1758 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1759
1760 if( ret != 0 )
1761 {
1762 x509_crl_free( crl );
1763 return( ret );
1764 }
1765 }
1766
1767 if( p != end )
1768 {
1769 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001770 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001771 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1772 }
1773
1774 end = crl->raw.p + crl->raw.len;
1775
1776 /*
1777 * signatureAlgorithm AlgorithmIdentifier,
1778 * signatureValue BIT STRING
1779 */
1780 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1781 {
1782 x509_crl_free( crl );
1783 return( ret );
1784 }
1785
Paul Bakker535e97d2012-08-23 10:49:55 +00001786 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1787 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001788 {
1789 x509_crl_free( crl );
1790 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1791 }
1792
1793 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1794 {
1795 x509_crl_free( crl );
1796 return( ret );
1797 }
1798
1799 if( p != end )
1800 {
1801 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001802 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001803 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1804 }
1805
Paul Bakkerd98030e2009-05-02 15:13:40 +00001806 return( 0 );
1807}
1808
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001809/*
1810 * Parse one or more CRLs and add them to the chained list
1811 */
1812int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
1813{
1814#if defined(POLARSSL_PEM_C)
1815 int ret;
1816 size_t use_len;
1817 pem_context pem;
1818 int is_pem = 0;
1819
1820 if( chain == NULL || buf == NULL )
1821 return( POLARSSL_ERR_X509_INVALID_INPUT );
1822
1823 do
1824 {
1825 pem_init( &pem );
1826 ret = pem_read_buffer( &pem,
1827 (char *) "-----BEGIN X509 CRL-----",
1828 (char *) "-----END X509 CRL-----",
1829 buf, NULL, 0, &use_len );
1830
1831 if( ret == 0 )
1832 {
1833 /*
1834 * Was PEM encoded
1835 */
1836 is_pem = 1;
1837
1838 buflen -= use_len;
1839 buf += use_len;
1840
1841 if( ( ret = x509_crl_parse_der( chain,
1842 pem.buf, pem.buflen ) ) != 0 )
1843 {
1844 return( ret );
1845 }
1846
1847 pem_free( &pem );
1848 }
1849 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1850 {
1851 pem_free( &pem );
1852 return( ret );
1853 }
1854 }
1855 while( is_pem && buflen > 0 );
1856
1857 if( is_pem )
1858 return( 0 );
1859 else
1860#endif /* POLARSSL_PEM_C */
1861 return( x509_crl_parse_der( chain, buf, buflen ) );
1862}
1863
Paul Bakker335db3f2011-04-25 15:28:35 +00001864#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001865/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001866 * Load all data from a file into a given buffer.
1867 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001868static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001869{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001871 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001872
Paul Bakkerd98030e2009-05-02 15:13:40 +00001873 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001874 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001875
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001877 if( ( size = ftell( f ) ) == -1 )
1878 {
1879 fclose( f );
1880 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1881 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001882 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001883
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001884 *n = (size_t) size;
1885
1886 if( *n + 1 == 0 ||
1887 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1888 {
1889 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001890 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001891 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001892
Paul Bakkerd98030e2009-05-02 15:13:40 +00001893 if( fread( *buf, 1, *n, f ) != *n )
1894 {
1895 fclose( f );
1896 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001897 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001899
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001901
Paul Bakkerd98030e2009-05-02 15:13:40 +00001902 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001903
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001905}
1906
1907/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 * Load one or more certificates and add them to the chained list
1909 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001910int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911{
1912 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001913 size_t n;
1914 unsigned char *buf;
1915
Paul Bakker69e095c2011-12-10 21:55:01 +00001916 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1917 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Paul Bakker69e095c2011-12-10 21:55:01 +00001919 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
1921 memset( buf, 0, n + 1 );
1922 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
1924 return( ret );
1925}
1926
Paul Bakker8d914582012-06-04 12:46:42 +00001927int x509parse_crtpath( x509_cert *chain, const char *path )
1928{
1929 int ret = 0;
1930#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001931 int w_ret;
1932 WCHAR szDir[MAX_PATH];
1933 char filename[MAX_PATH];
1934 char *p;
Manuel Pégourié-Gonnard80e6cff2015-10-21 10:16:29 +02001935 size_t len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001936
Paul Bakker97872ac2012-11-02 12:53:26 +00001937 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001938 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001939
1940 if( len > MAX_PATH - 3 )
1941 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001942
Paul Bakker3338b792012-10-01 21:13:10 +00001943 memset( szDir, 0, sizeof(szDir) );
1944 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001945 memcpy( filename, path, len );
1946 filename[len++] = '\\';
1947 p = filename + len;
1948 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001949
Manuel Pégourié-Gonnard80e6cff2015-10-21 10:16:29 +02001950 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int) len, szDir, MAX_PATH - 3 );
Manuel Pégourié-Gonnard2dc15c82015-02-05 11:34:49 +00001951 if( w_ret == 0 )
1952 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001953
Paul Bakker97872ac2012-11-02 12:53:26 +00001954 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001955 if (hFind == INVALID_HANDLE_VALUE)
1956 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1957
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001958 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001959 do
1960 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001961 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001962
Paul Bakkere4791f32012-06-04 21:29:15 +00001963 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001964 continue;
1965
Paul Bakker3338b792012-10-01 21:13:10 +00001966 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1967 lstrlenW(file_data.cFileName),
Manuel Pégourié-Gonnard80e6cff2015-10-21 10:16:29 +02001968 p, (int) len - 1,
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001969 NULL, NULL );
Manuel Pégourié-Gonnard2dc15c82015-02-05 11:34:49 +00001970 if( w_ret == 0 )
1971 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker8d914582012-06-04 12:46:42 +00001972
Paul Bakker3338b792012-10-01 21:13:10 +00001973 w_ret = x509parse_crtfile( chain, filename );
1974 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001975 ret++;
1976 else
1977 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001978 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001979 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001980
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001981 if (GetLastError() != ERROR_NO_MORE_FILES)
1982 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001983
1984 FindClose( hFind );
1985#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001986#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001987 int t_ret, i;
1988 struct stat sb;
1989 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001990 char entry_name[255];
1991 DIR *dir = opendir( path );
1992
1993 if( dir == NULL)
1994 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1995
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001996 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001997 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001998 if( result == NULL )
1999 break;
2000
2001 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2002
2003 i = stat( entry_name, &sb );
2004
2005 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002006 {
2007 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002008 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002009 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002010
2011 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002012 continue;
2013
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002014 // Ignore parse errors
2015 //
Paul Bakker8d914582012-06-04 12:46:42 +00002016 t_ret = x509parse_crtfile( chain, entry_name );
2017 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002018 ret++;
2019 else
2020 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002021 }
2022 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002023#else /* POLARSSL_HAVE_READDIR_R */
2024 ((void) chain);
2025 ((void) path);
2026 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2027#endif /* POLARSSL_HAVE_READDIR_R */
2028#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002029
2030 return( ret );
2031}
2032
Paul Bakkerd98030e2009-05-02 15:13:40 +00002033/*
2034 * Load one or more CRLs and add them to the chained list
2035 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002036int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002037{
2038 int ret;
2039 size_t n;
2040 unsigned char *buf;
2041
Paul Bakker69e095c2011-12-10 21:55:01 +00002042 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2043 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002044
Paul Bakker27fdf462011-06-09 13:55:13 +00002045 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002046
2047 memset( buf, 0, n + 1 );
2048 free( buf );
2049
2050 return( ret );
2051}
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002054 * Load and parse a private RSA key
2055 */
2056int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2057{
2058 int ret;
2059 size_t n;
2060 unsigned char *buf;
2061
Paul Bakker69e095c2011-12-10 21:55:01 +00002062 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2063 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002064
2065 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002066 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002067 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002068 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002069 (unsigned char *) pwd, strlen( pwd ) );
2070
2071 memset( buf, 0, n + 1 );
2072 free( buf );
2073
2074 return( ret );
2075}
2076
2077/*
2078 * Load and parse a public RSA key
2079 */
2080int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2081{
2082 int ret;
2083 size_t n;
2084 unsigned char *buf;
2085
Paul Bakker69e095c2011-12-10 21:55:01 +00002086 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2087 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002088
Paul Bakker27fdf462011-06-09 13:55:13 +00002089 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002090
2091 memset( buf, 0, n + 1 );
2092 free( buf );
2093
2094 return( ret );
2095}
2096#endif /* POLARSSL_FS_IO */
2097
2098/*
Paul Bakker65a19092013-06-06 21:14:58 +02002099 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 */
Paul Bakker65a19092013-06-06 21:14:58 +02002101static int x509parse_key_pkcs1_der( rsa_context *rsa,
2102 const unsigned char *key,
2103 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104{
Paul Bakker23986e52011-04-24 08:57:21 +00002105 int ret;
2106 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002108
Paul Bakker96743fc2011-02-12 14:30:57 +00002109 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002110 end = p + keylen;
2111
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002113 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002114 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 * RSAPrivateKey ::= SEQUENCE {
2116 * version Version,
2117 * modulus INTEGER, -- n
2118 * publicExponent INTEGER, -- e
2119 * privateExponent INTEGER, -- d
2120 * prime1 INTEGER, -- p
2121 * prime2 INTEGER, -- q
2122 * exponent1 INTEGER, -- d mod (p-1)
2123 * exponent2 INTEGER, -- d mod (q-1)
2124 * coefficient INTEGER, -- (inverse of q) mod p
2125 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2126 * }
2127 */
2128 if( ( ret = asn1_get_tag( &p, end, &len,
2129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2130 {
Paul Bakker9d781402011-05-09 16:17:09 +00002131 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
2134 end = p + len;
2135
2136 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2137 {
Paul Bakker9d781402011-05-09 16:17:09 +00002138 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
2140
2141 if( rsa->ver != 0 )
2142 {
Paul Bakker9d781402011-05-09 16:17:09 +00002143 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 }
2145
2146 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2147 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2148 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2149 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2150 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2151 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2152 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2153 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2154 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002156 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
2159 rsa->len = mpi_size( &rsa->N );
2160
2161 if( p != end )
2162 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002163 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002164 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002165 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 }
2167
2168 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2169 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 rsa_free( rsa );
2171 return( ret );
2172 }
2173
Paul Bakker65a19092013-06-06 21:14:58 +02002174 return( 0 );
2175}
2176
2177/*
2178 * Parse an unencrypted PKCS#8 encoded private RSA key
2179 */
2180static int x509parse_key_pkcs8_unencrypted_der(
2181 rsa_context *rsa,
2182 const unsigned char *key,
2183 size_t keylen )
2184{
2185 int ret;
2186 size_t len;
2187 unsigned char *p, *end;
2188 x509_buf pk_alg_oid;
2189
2190 p = (unsigned char *) key;
2191 end = p + keylen;
2192
2193 /*
2194 * This function parses the PrivatKeyInfo object (PKCS#8)
2195 *
2196 * PrivateKeyInfo ::= SEQUENCE {
2197 * version Version,
2198 * algorithm AlgorithmIdentifier,
2199 * PrivateKey BIT STRING
2200 * }
2201 *
2202 * AlgorithmIdentifier ::= SEQUENCE {
2203 * algorithm OBJECT IDENTIFIER,
2204 * parameters ANY DEFINED BY algorithm OPTIONAL
2205 * }
2206 *
2207 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2208 */
2209 if( ( ret = asn1_get_tag( &p, end, &len,
2210 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2211 {
2212 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2213 }
2214
2215 end = p + len;
2216
2217 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2218 {
2219 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2220 }
2221
2222 if( rsa->ver != 0 )
2223 {
2224 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2225 }
2226
2227 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2228 {
2229 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2230 }
2231
2232 /*
2233 * only RSA keys handled at this time
2234 */
2235 if( pk_alg_oid.len != 9 ||
2236 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2237 {
2238 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2239 }
2240
2241 /*
2242 * Get the OCTET STRING and parse the PKCS#1 format inside
2243 */
2244 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2246
2247 if( ( end - p ) < 1 )
2248 {
2249 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2250 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2251 }
2252
2253 end = p + len;
2254
2255 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2256 return( ret );
2257
2258 return( 0 );
2259}
2260
2261/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002262 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002263 */
2264static int x509parse_key_pkcs8_encrypted_der(
2265 rsa_context *rsa,
2266 const unsigned char *key,
2267 size_t keylen,
2268 const unsigned char *pwd,
2269 size_t pwdlen )
2270{
2271 int ret;
2272 size_t len;
2273 unsigned char *p, *end, *end2;
2274 x509_buf pbe_alg_oid, pbe_params;
2275 unsigned char buf[2048];
2276
2277 memset(buf, 0, 2048);
2278
2279 p = (unsigned char *) key;
2280 end = p + keylen;
2281
Paul Bakker1fd43212013-06-17 15:14:42 +02002282 if( pwdlen == 0 )
2283 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2284
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002285 /*
2286 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2287 *
2288 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2289 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2290 * encryptedData EncryptedData
2291 * }
2292 *
2293 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2294 *
2295 * EncryptedData ::= OCTET STRING
2296 *
2297 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2298 */
2299 if( ( ret = asn1_get_tag( &p, end, &len,
2300 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2301 {
2302 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2303 }
2304
2305 end = p + len;
2306
2307 if( ( ret = asn1_get_tag( &p, end, &len,
2308 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2309 {
2310 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2311 }
2312
2313 end2 = p + len;
2314
2315 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2316 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2317
2318 pbe_alg_oid.p = p;
2319 p += pbe_alg_oid.len;
2320
2321 /*
2322 * Store the algorithm parameters
2323 */
2324 pbe_params.p = p;
2325 pbe_params.len = end2 - p;
2326 p += pbe_params.len;
2327
2328 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2329 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2330
2331 // buf has been sized to 2048 bytes
2332 if( len > 2048 )
2333 return( POLARSSL_ERR_X509_INVALID_INPUT );
2334
2335 /*
2336 * Decrypt EncryptedData with appropriate PDE
2337 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002338#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002339 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2340 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002341 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2342 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2343 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002344 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002345 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2346 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2347
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002348 return( ret );
2349 }
2350 }
2351 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2352 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002353 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2354 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2355 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002356 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002357 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2358 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2359
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002360 return( ret );
2361 }
2362 }
2363 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2364 {
2365 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2366 PKCS12_PBE_DECRYPT,
2367 pwd, pwdlen,
2368 p, len, buf ) ) != 0 )
2369 {
2370 return( ret );
2371 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002372
2373 // Best guess for password mismatch when using RC4. If first tag is
2374 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2375 //
2376 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2377 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002378 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002379 else
2380#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002381#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002382 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002383 {
2384 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2385 p, len, buf ) ) != 0 )
2386 {
2387 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2388 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2389
2390 return( ret );
2391 }
2392 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002393 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002394#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002395 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2396
2397 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2398}
2399
2400/*
Paul Bakker65a19092013-06-06 21:14:58 +02002401 * Parse a private RSA key
2402 */
2403int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2404 const unsigned char *pwd, size_t pwdlen )
2405{
2406 int ret;
2407
Paul Bakker96743fc2011-02-12 14:30:57 +00002408#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002409 size_t len;
2410 pem_context pem;
2411
2412 pem_init( &pem );
2413 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002414 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2415 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002416 key, pwd, pwdlen, &len );
2417 if( ret == 0 )
2418 {
2419 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2420 {
2421 rsa_free( rsa );
2422 }
2423
2424 pem_free( &pem );
2425 return( ret );
2426 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002427 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2428 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2429 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2430 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002431 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002432 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002433
2434 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002435 (char *) "-----BEGIN PRIVATE KEY-----",
2436 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002437 key, NULL, 0, &len );
2438 if( ret == 0 )
2439 {
2440 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2441 pem.buf, pem.buflen ) ) != 0 )
2442 {
2443 rsa_free( rsa );
2444 }
2445
2446 pem_free( &pem );
2447 return( ret );
2448 }
2449 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002450 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002451
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002452 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002453 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2454 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002455 key, NULL, 0, &len );
2456 if( ret == 0 )
2457 {
2458 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2459 pem.buf, pem.buflen,
2460 pwd, pwdlen ) ) != 0 )
2461 {
2462 rsa_free( rsa );
2463 }
2464
2465 pem_free( &pem );
2466 return( ret );
2467 }
2468 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002469 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002470#else
2471 ((void) pwd);
2472 ((void) pwdlen);
2473#endif /* POLARSSL_PEM_C */
2474
2475 // At this point we only know it's not a PEM formatted key. Could be any
2476 // of the known DER encoded private key formats
2477 //
2478 // We try the different DER format parsers to see if one passes without
2479 // error
2480 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002481 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2482 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002483 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002484 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002485 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002487 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002488
2489 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2490 {
2491 return( ret );
2492 }
2493
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002494 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2495 return( 0 );
2496
2497 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002498
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002499 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2500 return( 0 );
2501
2502 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002503
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002504 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505}
2506
2507/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002508 * Parse a public RSA key
2509 */
Paul Bakker23986e52011-04-24 08:57:21 +00002510int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002511{
Paul Bakker23986e52011-04-24 08:57:21 +00002512 int ret;
2513 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002514 unsigned char *p, *end;
2515 x509_buf alg_oid;
2516#if defined(POLARSSL_PEM_C)
2517 pem_context pem;
2518
2519 pem_init( &pem );
2520 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002521 (char *) "-----BEGIN PUBLIC KEY-----",
2522 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002523 key, NULL, 0, &len );
2524
2525 if( ret == 0 )
2526 {
2527 /*
2528 * Was PEM encoded
2529 */
2530 keylen = pem.buflen;
2531 }
Paul Bakker9255e832013-06-06 14:58:28 +02002532 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002533 {
2534 pem_free( &pem );
2535 return( ret );
2536 }
2537
2538 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2539#else
2540 p = (unsigned char *) key;
2541#endif
2542 end = p + keylen;
2543
2544 /*
2545 * PublicKeyInfo ::= SEQUENCE {
2546 * algorithm AlgorithmIdentifier,
2547 * PublicKey BIT STRING
2548 * }
2549 *
2550 * AlgorithmIdentifier ::= SEQUENCE {
2551 * algorithm OBJECT IDENTIFIER,
2552 * parameters ANY DEFINED BY algorithm OPTIONAL
2553 * }
2554 *
2555 * RSAPublicKey ::= SEQUENCE {
2556 * modulus INTEGER, -- n
2557 * publicExponent INTEGER -- e
2558 * }
2559 */
2560
2561 if( ( ret = asn1_get_tag( &p, end, &len,
2562 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2563 {
2564#if defined(POLARSSL_PEM_C)
2565 pem_free( &pem );
2566#endif
2567 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002568 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002569 }
2570
2571 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2572 {
2573#if defined(POLARSSL_PEM_C)
2574 pem_free( &pem );
2575#endif
2576 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002577 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002578 }
2579
2580 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2581 {
2582#if defined(POLARSSL_PEM_C)
2583 pem_free( &pem );
2584#endif
2585 rsa_free( rsa );
2586 return( ret );
2587 }
2588
2589 rsa->len = mpi_size( &rsa->N );
2590
2591#if defined(POLARSSL_PEM_C)
2592 pem_free( &pem );
2593#endif
2594
2595 return( 0 );
2596}
2597
Paul Bakkereaa89f82011-04-04 21:36:15 +00002598#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002599/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002600 * Parse DHM parameters
2601 */
Paul Bakker23986e52011-04-24 08:57:21 +00002602int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002603{
Paul Bakker23986e52011-04-24 08:57:21 +00002604 int ret;
2605 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002606 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002607#if defined(POLARSSL_PEM_C)
2608 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002609
Paul Bakker96743fc2011-02-12 14:30:57 +00002610 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002611
Paul Bakker96743fc2011-02-12 14:30:57 +00002612 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002613 (char *) "-----BEGIN DH PARAMETERS-----",
2614 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002615 dhmin, NULL, 0, &dhminlen );
2616
2617 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002618 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002619 /*
2620 * Was PEM encoded
2621 */
2622 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002623 }
Paul Bakker9255e832013-06-06 14:58:28 +02002624 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002625 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002626 pem_free( &pem );
2627 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002628 }
2629
Paul Bakker96743fc2011-02-12 14:30:57 +00002630 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2631#else
2632 p = (unsigned char *) dhmin;
2633#endif
2634 end = p + dhminlen;
2635
Paul Bakker1b57b062011-01-06 15:48:19 +00002636 memset( dhm, 0, sizeof( dhm_context ) );
2637
Paul Bakker1b57b062011-01-06 15:48:19 +00002638 /*
2639 * DHParams ::= SEQUENCE {
2640 * prime INTEGER, -- P
2641 * generator INTEGER, -- g
2642 * }
2643 */
2644 if( ( ret = asn1_get_tag( &p, end, &len,
2645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2646 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002647#if defined(POLARSSL_PEM_C)
2648 pem_free( &pem );
2649#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002650 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002651 }
2652
2653 end = p + len;
2654
2655 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2656 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2657 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002658#if defined(POLARSSL_PEM_C)
2659 pem_free( &pem );
2660#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002661 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002663 }
2664
2665 if( p != end )
2666 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002667#if defined(POLARSSL_PEM_C)
2668 pem_free( &pem );
2669#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002670 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002671 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002672 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2673 }
2674
Paul Bakker96743fc2011-02-12 14:30:57 +00002675#if defined(POLARSSL_PEM_C)
2676 pem_free( &pem );
2677#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002678
2679 return( 0 );
2680}
2681
Paul Bakker335db3f2011-04-25 15:28:35 +00002682#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002683/*
2684 * Load and parse a private RSA key
2685 */
2686int x509parse_dhmfile( dhm_context *dhm, const char *path )
2687{
2688 int ret;
2689 size_t n;
2690 unsigned char *buf;
2691
Paul Bakker69e095c2011-12-10 21:55:01 +00002692 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2693 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002694
Paul Bakker27fdf462011-06-09 13:55:13 +00002695 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002696
2697 memset( buf, 0, n + 1 );
2698 free( buf );
2699
2700 return( ret );
2701}
Paul Bakker335db3f2011-04-25 15:28:35 +00002702#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002703#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706#include <stdarg.h>
2707
2708#if !defined vsnprintf
2709#define vsnprintf _vsnprintf
2710#endif // vsnprintf
2711
2712/*
2713 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2714 * Result value is not size of buffer needed, but -1 if no fit is possible.
2715 *
2716 * This fuction tries to 'fix' this by at least suggesting enlarging the
2717 * size by 20.
2718 */
2719int compat_snprintf(char *str, size_t size, const char *format, ...)
2720{
2721 va_list ap;
2722 int res = -1;
2723
2724 va_start( ap, format );
2725
2726 res = vsnprintf( str, size, format, ap );
2727
2728 va_end( ap );
2729
2730 // No quick fix possible
2731 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002732 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002733
2734 return res;
2735}
2736
2737#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002738#endif
2739
Paul Bakkerd98030e2009-05-02 15:13:40 +00002740#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2741
2742#define SAFE_SNPRINTF() \
2743{ \
2744 if( ret == -1 ) \
2745 return( -1 ); \
2746 \
Paul Bakker23986e52011-04-24 08:57:21 +00002747 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002748 p[n - 1] = '\0'; \
2749 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2750 } \
2751 \
Paul Bakker23986e52011-04-24 08:57:21 +00002752 n -= (unsigned int) ret; \
2753 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754}
2755
Paul Bakker5121ce52009-01-03 21:22:43 +00002756/*
2757 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002758 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002760int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002761{
Paul Bakker23986e52011-04-24 08:57:21 +00002762 int ret;
2763 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002765 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 char s[128], *p;
2767
2768 memset( s, 0, sizeof( s ) );
2769
2770 name = dn;
2771 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002772 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
2774 while( name != NULL )
2775 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002776 if( !name->oid.p )
2777 {
2778 name = name->next;
2779 continue;
2780 }
2781
Paul Bakker74111d32011-01-15 16:57:55 +00002782 if( name != dn )
2783 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002784 ret = snprintf( p, n, ", " );
2785 SAFE_SNPRINTF();
2786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002787
Paul Bakker535e97d2012-08-23 10:49:55 +00002788 if( name->oid.len == 3 &&
2789 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 {
2791 switch( name->oid.p[2] )
2792 {
2793 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002794 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
2796 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002797 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
2799 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002800 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002801
2802 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002803 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
2805 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002806 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002809 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002812 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 name->oid.p[2] );
2814 break;
2815 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002816 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002818 else if( name->oid.len == 9 &&
2819 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 {
2821 switch( name->oid.p[8] )
2822 {
2823 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002824 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
2826 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002827 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 name->oid.p[8] );
2829 break;
2830 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002831 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 }
2833 else
Paul Bakker74111d32011-01-15 16:57:55 +00002834 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002835 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002836 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
2839 for( i = 0; i < name->val.len; i++ )
2840 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002841 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842 break;
2843
2844 c = name->val.p[i];
2845 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2846 s[i] = '?';
2847 else s[i] = c;
2848 }
2849 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002850 ret = snprintf( p, n, "%s", s );
2851 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 name = name->next;
2853 }
2854
Paul Bakker23986e52011-04-24 08:57:21 +00002855 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856}
2857
2858/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002859 * Store the serial in printable form into buf; no more
2860 * than size characters will be written
2861 */
2862int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2863{
Paul Bakker23986e52011-04-24 08:57:21 +00002864 int ret;
2865 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002866 char *p;
2867
2868 p = buf;
2869 n = size;
2870
2871 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002872 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002873
2874 for( i = 0; i < nr; i++ )
2875 {
Paul Bakker93048802011-12-05 14:38:06 +00002876 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002877 continue;
2878
Paul Bakkerdd476992011-01-16 21:34:59 +00002879 ret = snprintf( p, n, "%02X%s",
2880 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2881 SAFE_SNPRINTF();
2882 }
2883
Paul Bakker03c7c252011-11-25 12:37:37 +00002884 if( nr != serial->len )
2885 {
2886 ret = snprintf( p, n, "...." );
2887 SAFE_SNPRINTF();
2888 }
2889
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002891}
2892
2893/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002894 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002896int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2897 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898{
Paul Bakker23986e52011-04-24 08:57:21 +00002899 int ret;
2900 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002901 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
2903 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002904 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakkerd98030e2009-05-02 15:13:40 +00002906 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002908 SAFE_SNPRINTF();
2909 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002911 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakkerdd476992011-01-16 21:34:59 +00002913 ret = x509parse_serial_gets( p, n, &crt->serial);
2914 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakkerd98030e2009-05-02 15:13:40 +00002916 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2917 SAFE_SNPRINTF();
2918 ret = x509parse_dn_gets( p, n, &crt->issuer );
2919 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakkerd98030e2009-05-02 15:13:40 +00002921 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2922 SAFE_SNPRINTF();
2923 ret = x509parse_dn_gets( p, n, &crt->subject );
2924 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakkerd98030e2009-05-02 15:13:40 +00002926 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2928 crt->valid_from.year, crt->valid_from.mon,
2929 crt->valid_from.day, crt->valid_from.hour,
2930 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002931 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Paul Bakkerd98030e2009-05-02 15:13:40 +00002933 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2935 crt->valid_to.year, crt->valid_to.mon,
2936 crt->valid_to.day, crt->valid_to.hour,
2937 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002938 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakkerd98030e2009-05-02 15:13:40 +00002940 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2941 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Paul Bakker27d66162010-03-17 06:56:01 +00002943 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002945 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2946 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2947 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2948 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2949 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2950 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2951 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2952 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2953 default: ret = snprintf( p, n, "???" ); break;
2954 }
2955 SAFE_SNPRINTF();
2956
2957 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002958 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002959 SAFE_SNPRINTF();
2960
Paul Bakker23986e52011-04-24 08:57:21 +00002961 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002962}
2963
Paul Bakker74111d32011-01-15 16:57:55 +00002964/*
2965 * Return an informational string describing the given OID
2966 */
2967const char *x509_oid_get_description( x509_buf *oid )
2968{
2969 if ( oid == NULL )
2970 return ( NULL );
2971
2972 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2973 return( STRING_SERVER_AUTH );
2974
2975 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2976 return( STRING_CLIENT_AUTH );
2977
2978 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2979 return( STRING_CODE_SIGNING );
2980
2981 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2982 return( STRING_EMAIL_PROTECTION );
2983
2984 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2985 return( STRING_TIME_STAMPING );
2986
2987 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2988 return( STRING_OCSP_SIGNING );
2989
2990 return( NULL );
2991}
2992
2993/* Return the x.y.z.... style numeric string for the given OID */
2994int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2995{
Paul Bakker23986e52011-04-24 08:57:21 +00002996 int ret;
2997 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002998 unsigned int value;
2999 char *p;
3000
3001 p = buf;
3002 n = size;
3003
3004 /* First byte contains first two dots */
3005 if( oid->len > 0 )
3006 {
3007 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3008 SAFE_SNPRINTF();
3009 }
3010
3011 /* TODO: value can overflow in value. */
3012 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003013 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003014 {
3015 value <<= 7;
3016 value += oid->p[i] & 0x7F;
3017
3018 if( !( oid->p[i] & 0x80 ) )
3019 {
3020 /* Last byte */
3021 ret = snprintf( p, n, ".%d", value );
3022 SAFE_SNPRINTF();
3023 value = 0;
3024 }
3025 }
3026
Paul Bakker23986e52011-04-24 08:57:21 +00003027 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003028}
3029
Paul Bakkerd98030e2009-05-02 15:13:40 +00003030/*
3031 * Return an informational string about the CRL.
3032 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003033int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3034 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003035{
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003037 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003038 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003039 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003040
3041 p = buf;
3042 n = size;
3043
3044 ret = snprintf( p, n, "%sCRL version : %d",
3045 prefix, crl->version );
3046 SAFE_SNPRINTF();
3047
3048 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3049 SAFE_SNPRINTF();
3050 ret = x509parse_dn_gets( p, n, &crl->issuer );
3051 SAFE_SNPRINTF();
3052
3053 ret = snprintf( p, n, "\n%sthis update : " \
3054 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3055 crl->this_update.year, crl->this_update.mon,
3056 crl->this_update.day, crl->this_update.hour,
3057 crl->this_update.min, crl->this_update.sec );
3058 SAFE_SNPRINTF();
3059
3060 ret = snprintf( p, n, "\n%snext update : " \
3061 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3062 crl->next_update.year, crl->next_update.mon,
3063 crl->next_update.day, crl->next_update.hour,
3064 crl->next_update.min, crl->next_update.sec );
3065 SAFE_SNPRINTF();
3066
3067 entry = &crl->entry;
3068
3069 ret = snprintf( p, n, "\n%sRevoked certificates:",
3070 prefix );
3071 SAFE_SNPRINTF();
3072
Paul Bakker9be19372009-07-27 20:21:53 +00003073 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003074 {
3075 ret = snprintf( p, n, "\n%sserial number: ",
3076 prefix );
3077 SAFE_SNPRINTF();
3078
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003079 ret = x509parse_serial_gets( p, n, &entry->serial);
3080 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003081
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082 ret = snprintf( p, n, " revocation date: " \
3083 "%04d-%02d-%02d %02d:%02d:%02d",
3084 entry->revocation_date.year, entry->revocation_date.mon,
3085 entry->revocation_date.day, entry->revocation_date.hour,
3086 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003087 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003088
3089 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
3091
Paul Bakkerd98030e2009-05-02 15:13:40 +00003092 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3093 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Paul Bakker27d66162010-03-17 06:56:01 +00003095 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003096 {
3097 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3098 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3099 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3100 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3101 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3102 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3103 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3104 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3105 default: ret = snprintf( p, n, "???" ); break;
3106 }
3107 SAFE_SNPRINTF();
3108
Paul Bakker1e27bb22009-07-19 20:25:25 +00003109 ret = snprintf( p, n, "\n" );
3110 SAFE_SNPRINTF();
3111
Paul Bakker23986e52011-04-24 08:57:21 +00003112 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113}
3114
3115/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003116 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003118static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003119{
Paul Bakkercce9d772011-11-18 14:26:47 +00003120#if defined(_WIN32)
3121 SYSTEMTIME st;
3122
Paul Bakkerf48de952014-07-08 14:39:41 +02003123 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003124
Paul Bakker0d844dd2014-07-07 17:44:14 +02003125 now->year = st.wYear;
3126 now->mon = st.wMonth;
3127 now->day = st.wDay;
3128 now->hour = st.wHour;
3129 now->min = st.wMinute;
3130 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003131#else
Paul Bakker358a8412014-07-08 12:14:37 +02003132 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003133 time_t tt;
3134
3135 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003136 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
Paul Bakker358a8412014-07-08 12:14:37 +02003138 now->year = lt.tm_year + 1900;
3139 now->mon = lt.tm_mon + 1;
3140 now->day = lt.tm_mday;
3141 now->hour = lt.tm_hour;
3142 now->min = lt.tm_min;
3143 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003144#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003145}
Paul Bakkercce9d772011-11-18 14:26:47 +00003146
Paul Bakker0d844dd2014-07-07 17:44:14 +02003147/*
3148 * Return 0 if before <= after, 1 otherwise
3149 */
3150static int x509_check_time( const x509_time *before, const x509_time *after )
3151{
3152 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003153 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakker0d844dd2014-07-07 17:44:14 +02003155 if( before->year == after->year &&
3156 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003157 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker0d844dd2014-07-07 17:44:14 +02003159 if( before->year == after->year &&
3160 before->mon == after->mon &&
3161 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003162 return( 1 );
3163
Paul Bakker0d844dd2014-07-07 17:44:14 +02003164 if( before->year == after->year &&
3165 before->mon == after->mon &&
3166 before->day == after->day &&
3167 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003168 return( 1 );
3169
Paul Bakker0d844dd2014-07-07 17:44:14 +02003170 if( before->year == after->year &&
3171 before->mon == after->mon &&
3172 before->day == after->day &&
3173 before->hour == after->hour &&
3174 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003175 return( 1 );
3176
Paul Bakker0d844dd2014-07-07 17:44:14 +02003177 if( before->year == after->year &&
3178 before->mon == after->mon &&
3179 before->day == after->day &&
3180 before->hour == after->hour &&
3181 before->min == after->min &&
3182 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003183 return( 1 );
3184
Paul Bakker40ea7de2009-05-03 10:18:48 +00003185 return( 0 );
3186}
3187
Paul Bakker0d844dd2014-07-07 17:44:14 +02003188int x509parse_time_expired( const x509_time *to )
3189{
3190 x509_time now;
3191
3192 x509_get_current_time( &now );
3193
3194 return( x509_check_time( &now, to ) );
3195}
3196
3197int x509parse_time_future( const x509_time *from )
3198{
3199 x509_time now;
3200
3201 x509_get_current_time( &now );
3202
3203 return( x509_check_time( from, &now ) );
3204}
3205
Paul Bakker40ea7de2009-05-03 10:18:48 +00003206/*
3207 * Return 1 if the certificate is revoked, or 0 otherwise.
3208 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003209int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003210{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003211 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003212
3213 while( cur != NULL && cur->serial.len != 0 )
3214 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003215 if( crt->serial.len == cur->serial.len &&
3216 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003217 {
3218 if( x509parse_time_expired( &cur->revocation_date ) )
3219 return( 1 );
3220 }
3221
3222 cur = cur->next;
3223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 return( 0 );
3226}
3227
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003228/*
3229 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003230 */
Paul Bakker23986e52011-04-24 08:57:21 +00003231static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 unsigned char *out )
3233{
3234 switch( alg )
3235 {
Paul Bakker40e46942009-01-03 21:51:57 +00003236#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003237 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003239#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003240 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003241#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003242#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003243 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003244#endif
3245#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003246 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003247#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003248#if defined(POLARSSL_SHA2_C)
3249 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3250 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3251#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003252#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003253 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3254 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3255#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003257 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 break;
3259 }
3260}
3261
3262/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003263 * Check that the given certificate is valid accoring to the CRL.
3264 */
3265static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3266 x509_crl *crl_list)
3267{
3268 int flags = 0;
3269 int hash_id;
3270 unsigned char hash[64];
3271
Paul Bakker915275b2012-09-28 07:10:55 +00003272 if( ca == NULL )
3273 return( flags );
3274
Paul Bakker76fd75a2011-01-16 21:12:10 +00003275 /*
3276 * TODO: What happens if no CRL is present?
3277 * Suggestion: Revocation state should be unknown if no CRL is present.
3278 * For backwards compatibility this is not yet implemented.
3279 */
3280
Paul Bakker915275b2012-09-28 07:10:55 +00003281 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003282 {
Paul Bakker915275b2012-09-28 07:10:55 +00003283 if( crl_list->version == 0 ||
3284 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003285 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3286 crl_list->issuer_raw.len ) != 0 )
3287 {
3288 crl_list = crl_list->next;
3289 continue;
3290 }
3291
3292 /*
3293 * Check if CRL is correctly signed by the trusted CA
3294 */
3295 hash_id = crl_list->sig_alg;
3296
3297 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3298
Paul Bakker43f97992013-09-23 11:23:31 +02003299 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003300 0, hash, crl_list->sig.p ) == 0 )
3301 {
3302 /*
3303 * CRL is not trusted
3304 */
3305 flags |= BADCRL_NOT_TRUSTED;
3306 break;
3307 }
3308
3309 /*
3310 * Check for validity of CRL (Do not drop out)
3311 */
3312 if( x509parse_time_expired( &crl_list->next_update ) )
3313 flags |= BADCRL_EXPIRED;
3314
Paul Bakker50a5c532014-07-08 10:59:10 +02003315 if( x509parse_time_future( &crl_list->this_update ) )
3316 flags |= BADCRL_FUTURE;
3317
Paul Bakker76fd75a2011-01-16 21:12:10 +00003318 /*
3319 * Check if certificate is revoked
3320 */
3321 if( x509parse_revoked(crt, crl_list) )
3322 {
3323 flags |= BADCERT_REVOKED;
3324 break;
3325 }
3326
3327 crl_list = crl_list->next;
3328 }
3329 return flags;
3330}
3331
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003332// Equal == 0, inequal == 1
3333static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3334{
3335 size_t i;
3336 unsigned char diff;
3337 const unsigned char *n1 = s1, *n2 = s2;
3338
3339 for( i = 0; i < len; i++ )
3340 {
3341 diff = n1[i] ^ n2[i];
3342
Paul Bakkerc941adb2014-07-07 14:17:24 +02003343 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003344 continue;
3345
Paul Bakkerc941adb2014-07-07 14:17:24 +02003346 if( diff == 32 &&
3347 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3348 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3349 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003350 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003351 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003352
3353 return( 1 );
3354 }
3355
3356 return( 0 );
3357}
3358
Paul Bakker1d073c52014-07-08 20:15:51 +02003359static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003360{
3361 size_t i;
3362 size_t cn_idx = 0;
3363
Paul Bakker57b12982012-02-11 17:38:38 +00003364 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003365 return( 0 );
3366
3367 for( i = 0; i < strlen( cn ); ++i )
3368 {
3369 if( cn[i] == '.' )
3370 {
3371 cn_idx = i;
3372 break;
3373 }
3374 }
3375
3376 if( cn_idx == 0 )
3377 return( 0 );
3378
Paul Bakker535e97d2012-08-23 10:49:55 +00003379 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003380 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003381 {
3382 return( 1 );
3383 }
3384
3385 return( 0 );
3386}
3387
Paul Bakker915275b2012-09-28 07:10:55 +00003388static int x509parse_verify_top(
3389 x509_cert *child, x509_cert *trust_ca,
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003390 x509_crl *ca_crl,
3391 int path_cnt, int self_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003392 int (*f_vrfy)(void *, x509_cert *, int, int *),
3393 void *p_vrfy )
3394{
3395 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003396 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003397 unsigned char hash[64];
3398
3399 if( x509parse_time_expired( &child->valid_to ) )
3400 *flags |= BADCERT_EXPIRED;
3401
Paul Bakker50a5c532014-07-08 10:59:10 +02003402 if( x509parse_time_future( &child->valid_from ) )
3403 *flags |= BADCERT_FUTURE;
3404
Paul Bakker915275b2012-09-28 07:10:55 +00003405 /*
3406 * Child is the top of the chain. Check against the trust_ca list.
3407 */
3408 *flags |= BADCERT_NOT_TRUSTED;
3409
3410 while( trust_ca != NULL )
3411 {
3412 if( trust_ca->version == 0 ||
3413 child->issuer_raw.len != trust_ca->subject_raw.len ||
3414 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3415 child->issuer_raw.len ) != 0 )
3416 {
3417 trust_ca = trust_ca->next;
3418 continue;
3419 }
3420
Paul Bakker9a736322012-11-14 12:39:52 +00003421 /*
3422 * Reduce path_len to check against if top of the chain is
3423 * the same as the trusted CA
3424 */
3425 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3426 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3427 child->issuer_raw.len ) == 0 )
3428 {
3429 check_path_cnt--;
3430 }
3431
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003432 /* Self signed certificates do not count towards the limit */
Paul Bakker915275b2012-09-28 07:10:55 +00003433 if( trust_ca->max_pathlen > 0 &&
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003434 trust_ca->max_pathlen < check_path_cnt - self_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003435 {
3436 trust_ca = trust_ca->next;
3437 continue;
3438 }
3439
3440 hash_id = child->sig_alg;
3441
3442 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3443
Paul Bakker43f97992013-09-23 11:23:31 +02003444 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003445 0, hash, child->sig.p ) != 0 )
3446 {
3447 trust_ca = trust_ca->next;
3448 continue;
3449 }
3450
3451 /*
3452 * Top of chain is signed by a trusted CA
3453 */
3454 *flags &= ~BADCERT_NOT_TRUSTED;
3455 break;
3456 }
3457
Paul Bakker9a736322012-11-14 12:39:52 +00003458 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003459 * If top of chain is not the same as the trusted CA send a verify request
3460 * to the callback for any issues with validity and CRL presence for the
3461 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003462 */
3463 if( trust_ca != NULL &&
3464 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3465 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3466 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003467 {
3468 /* Check trusted CA's CRL for then chain's top crt */
3469 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3470
3471 if( x509parse_time_expired( &trust_ca->valid_to ) )
3472 ca_flags |= BADCERT_EXPIRED;
3473
Paul Bakker50a5c532014-07-08 10:59:10 +02003474 if( x509parse_time_future( &trust_ca->valid_from ) )
3475 ca_flags |= BADCERT_FUTURE;
3476
Paul Bakker915275b2012-09-28 07:10:55 +00003477 if( NULL != f_vrfy )
3478 {
Paul Bakker9a736322012-11-14 12:39:52 +00003479 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003480 return( ret );
3481 }
3482 }
3483
3484 /* Call callback on top cert */
3485 if( NULL != f_vrfy )
3486 {
Paul Bakker9a736322012-11-14 12:39:52 +00003487 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003488 return( ret );
3489 }
3490
Paul Bakker915275b2012-09-28 07:10:55 +00003491 *flags |= ca_flags;
3492
3493 return( 0 );
3494}
3495
3496static int x509parse_verify_child(
3497 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003498 x509_crl *ca_crl,
3499 int path_cnt, int self_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003500 int (*f_vrfy)(void *, x509_cert *, int, int *),
3501 void *p_vrfy )
3502{
3503 int hash_id, ret;
3504 int parent_flags = 0;
3505 unsigned char hash[64];
3506 x509_cert *grandparent;
3507
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003508 /* Counting intermediate self signed certificates */
3509 if( path_cnt != 0 &&
3510 child->issuer_raw.len == child->subject_raw.len &&
3511 memcmp( child->issuer_raw.p, child->subject_raw.p,
3512 child->issuer_raw.len ) == 0 )
3513 {
3514 self_cnt++;
3515 }
3516
Manuel Pégourié-Gonnard4cdb3ba2014-11-20 17:12:15 +01003517 /* path_cnt is 0 for the first intermediate CA */
3518 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
3519 {
3520 *flags |= BADCERT_NOT_TRUSTED;
3521 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
3522 }
3523
Paul Bakker915275b2012-09-28 07:10:55 +00003524 if( x509parse_time_expired( &child->valid_to ) )
3525 *flags |= BADCERT_EXPIRED;
3526
Paul Bakker50a5c532014-07-08 10:59:10 +02003527 if( x509parse_time_future( &child->valid_from ) )
3528 *flags |= BADCERT_FUTURE;
3529
Paul Bakker915275b2012-09-28 07:10:55 +00003530 hash_id = child->sig_alg;
3531
3532 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3533
Paul Bakker43f97992013-09-23 11:23:31 +02003534 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3535 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003536 *flags |= BADCERT_NOT_TRUSTED;
3537
3538 /* Check trusted CA's CRL for the given crt */
3539 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3540
3541 grandparent = parent->next;
3542
3543 while( grandparent != NULL )
3544 {
3545 if( grandparent->version == 0 ||
3546 grandparent->ca_istrue == 0 ||
3547 parent->issuer_raw.len != grandparent->subject_raw.len ||
3548 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3549 parent->issuer_raw.len ) != 0 )
3550 {
3551 grandparent = grandparent->next;
3552 continue;
3553 }
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003554
3555 /* +2 because the current step is not yet accounted for
3556 * and because max_pathlen is one higher than it should be.
3557 * Also self signed certificates do not count to the limit. */
3558 if( grandparent->max_pathlen > 0 &&
3559 grandparent->max_pathlen < 2 + path_cnt - self_cnt )
3560 {
3561 grandparent = grandparent->next;
3562 continue;
3563 }
3564
Paul Bakker915275b2012-09-28 07:10:55 +00003565 break;
3566 }
3567
Paul Bakker915275b2012-09-28 07:10:55 +00003568 if( grandparent != NULL )
3569 {
3570 /*
3571 * Part of the chain
3572 */
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003573 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003574 if( ret != 0 )
3575 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003576 }
Paul Bakker915275b2012-09-28 07:10:55 +00003577 else
3578 {
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003579 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003580 if( ret != 0 )
3581 return( ret );
3582 }
3583
3584 /* child is verified to be a child of the parent, call verify callback */
3585 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003586 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003587 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003588
3589 *flags |= parent_flags;
3590
3591 return( 0 );
3592}
3593
Paul Bakker76fd75a2011-01-16 21:12:10 +00003594/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 * Verify the certificate validity
3596 */
3597int x509parse_verify( x509_cert *crt,
3598 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003599 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003600 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003601 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003602 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003603{
Paul Bakker23986e52011-04-24 08:57:21 +00003604 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003605 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003606 int pathlen = 0;
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003607 int selfsigned = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003608 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003610 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Paul Bakker40ea7de2009-05-03 10:18:48 +00003612 *flags = 0;
3613
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 if( cn != NULL )
3615 {
3616 name = &crt->subject;
3617 cn_len = strlen( cn );
3618
Paul Bakker4d2c1242012-05-10 14:12:46 +00003619 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003620 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003621 cur = &crt->subject_alt_names;
3622
3623 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003624 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003625 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003626 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003627 break;
3628
Paul Bakker535e97d2012-08-23 10:49:55 +00003629 if( cur->buf.len > 2 &&
3630 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003631 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003632 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
Paul Bakker4d2c1242012-05-10 14:12:46 +00003634 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003635 }
3636
3637 if( cur == NULL )
3638 *flags |= BADCERT_CN_MISMATCH;
3639 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003640 else
3641 {
3642 while( name != NULL )
3643 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003644 if( name->oid.len == 3 &&
3645 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003646 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003647 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003648 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003649 break;
3650
Paul Bakker535e97d2012-08-23 10:49:55 +00003651 if( name->val.len > 2 &&
3652 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003653 x509_wildcard_verify( cn, &name->val ) )
3654 break;
3655 }
3656
3657 name = name->next;
3658 }
3659
3660 if( name == NULL )
3661 *flags |= BADCERT_CN_MISMATCH;
3662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 }
3664
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003666 * Iterate upwards in the given cert chain, to find our crt parent.
3667 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003669 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
Paul Bakker76fd75a2011-01-16 21:12:10 +00003671 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003673 if( parent->ca_istrue == 0 ||
3674 crt->issuer_raw.len != parent->subject_raw.len ||
3675 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 crt->issuer_raw.len ) != 0 )
3677 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003678 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 continue;
3680 }
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003681
Paul Bakker915275b2012-09-28 07:10:55 +00003682 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 }
3684
Paul Bakker915275b2012-09-28 07:10:55 +00003685 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 {
Paul Bakker915275b2012-09-28 07:10:55 +00003687 /*
3688 * Part of the chain
3689 */
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003690 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003691 if( ret != 0 )
3692 return( ret );
3693 }
3694 else
Paul Bakker74111d32011-01-15 16:57:55 +00003695 {
Manuel Pégourié-Gonnarde51f47c2015-10-29 16:05:11 +01003696 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, selfsigned, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003697 if( ret != 0 )
3698 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003699 }
Paul Bakker915275b2012-09-28 07:10:55 +00003700
3701 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003702 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003703
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 return( 0 );
3705}
3706
3707/*
3708 * Unallocate all certificate data
3709 */
3710void x509_free( x509_cert *crt )
3711{
3712 x509_cert *cert_cur = crt;
3713 x509_cert *cert_prv;
3714 x509_name *name_cur;
3715 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003716 x509_sequence *seq_cur;
3717 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
3719 if( crt == NULL )
3720 return;
3721
3722 do
3723 {
3724 rsa_free( &cert_cur->rsa );
3725
3726 name_cur = cert_cur->issuer.next;
3727 while( name_cur != NULL )
3728 {
3729 name_prv = name_cur;
3730 name_cur = name_cur->next;
3731 memset( name_prv, 0, sizeof( x509_name ) );
3732 free( name_prv );
3733 }
3734
3735 name_cur = cert_cur->subject.next;
3736 while( name_cur != NULL )
3737 {
3738 name_prv = name_cur;
3739 name_cur = name_cur->next;
3740 memset( name_prv, 0, sizeof( x509_name ) );
3741 free( name_prv );
3742 }
3743
Paul Bakker74111d32011-01-15 16:57:55 +00003744 seq_cur = cert_cur->ext_key_usage.next;
3745 while( seq_cur != NULL )
3746 {
3747 seq_prv = seq_cur;
3748 seq_cur = seq_cur->next;
3749 memset( seq_prv, 0, sizeof( x509_sequence ) );
3750 free( seq_prv );
3751 }
3752
Paul Bakker8afa70d2012-02-11 18:42:45 +00003753 seq_cur = cert_cur->subject_alt_names.next;
3754 while( seq_cur != NULL )
3755 {
3756 seq_prv = seq_cur;
3757 seq_cur = seq_cur->next;
3758 memset( seq_prv, 0, sizeof( x509_sequence ) );
3759 free( seq_prv );
3760 }
3761
Paul Bakker5121ce52009-01-03 21:22:43 +00003762 if( cert_cur->raw.p != NULL )
3763 {
3764 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3765 free( cert_cur->raw.p );
3766 }
3767
3768 cert_cur = cert_cur->next;
3769 }
3770 while( cert_cur != NULL );
3771
3772 cert_cur = crt;
3773 do
3774 {
3775 cert_prv = cert_cur;
3776 cert_cur = cert_cur->next;
3777
3778 memset( cert_prv, 0, sizeof( x509_cert ) );
3779 if( cert_prv != crt )
3780 free( cert_prv );
3781 }
3782 while( cert_cur != NULL );
3783}
3784
Paul Bakkerd98030e2009-05-02 15:13:40 +00003785/*
3786 * Unallocate all CRL data
3787 */
3788void x509_crl_free( x509_crl *crl )
3789{
3790 x509_crl *crl_cur = crl;
3791 x509_crl *crl_prv;
3792 x509_name *name_cur;
3793 x509_name *name_prv;
3794 x509_crl_entry *entry_cur;
3795 x509_crl_entry *entry_prv;
3796
3797 if( crl == NULL )
3798 return;
3799
3800 do
3801 {
3802 name_cur = crl_cur->issuer.next;
3803 while( name_cur != NULL )
3804 {
3805 name_prv = name_cur;
3806 name_cur = name_cur->next;
3807 memset( name_prv, 0, sizeof( x509_name ) );
3808 free( name_prv );
3809 }
3810
3811 entry_cur = crl_cur->entry.next;
3812 while( entry_cur != NULL )
3813 {
3814 entry_prv = entry_cur;
3815 entry_cur = entry_cur->next;
3816 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3817 free( entry_prv );
3818 }
3819
3820 if( crl_cur->raw.p != NULL )
3821 {
3822 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3823 free( crl_cur->raw.p );
3824 }
3825
3826 crl_cur = crl_cur->next;
3827 }
3828 while( crl_cur != NULL );
3829
3830 crl_cur = crl;
3831 do
3832 {
3833 crl_prv = crl_cur;
3834 crl_cur = crl_cur->next;
3835
3836 memset( crl_prv, 0, sizeof( x509_crl ) );
3837 if( crl_prv != crl )
3838 free( crl_prv );
3839 }
3840 while( crl_cur != NULL );
3841}
3842
Paul Bakker40e46942009-01-03 21:51:57 +00003843#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003844
Paul Bakker40e46942009-01-03 21:51:57 +00003845#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
3847/*
3848 * Checkup routine
3849 */
3850int x509_self_test( int verbose )
3851{
Paul Bakker5690efc2011-05-26 13:16:06 +00003852#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003853 int ret;
3854 int flags;
3855 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 x509_cert cacert;
3857 x509_cert clicert;
3858 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003859#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003860 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
3863 if( verbose != 0 )
3864 printf( " X.509 certificate load: " );
3865
3866 memset( &clicert, 0, sizeof( x509_cert ) );
3867
Paul Bakkereae09db2013-06-06 12:35:54 +02003868 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003869 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 if( ret != 0 )
3871 {
3872 if( verbose != 0 )
3873 printf( "failed\n" );
3874
3875 return( ret );
3876 }
3877
3878 memset( &cacert, 0, sizeof( x509_cert ) );
3879
Paul Bakkereae09db2013-06-06 12:35:54 +02003880 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003881 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 if( ret != 0 )
3883 {
3884 if( verbose != 0 )
3885 printf( "failed\n" );
3886
3887 return( ret );
3888 }
3889
3890 if( verbose != 0 )
3891 printf( "passed\n X.509 private key load: " );
3892
3893 i = strlen( test_ca_key );
3894 j = strlen( test_ca_pwd );
3895
Paul Bakker66b78b22011-03-25 14:22:50 +00003896 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3897
Paul Bakker5121ce52009-01-03 21:22:43 +00003898 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003899 (const unsigned char *) test_ca_key, i,
3900 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 {
3902 if( verbose != 0 )
3903 printf( "failed\n" );
3904
3905 return( ret );
3906 }
3907
3908 if( verbose != 0 )
3909 printf( "passed\n X.509 signature verify: ");
3910
Paul Bakker23986e52011-04-24 08:57:21 +00003911 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 if( ret != 0 )
3913 {
3914 if( verbose != 0 )
3915 printf( "failed\n" );
3916
3917 return( ret );
3918 }
3919
Paul Bakker5690efc2011-05-26 13:16:06 +00003920#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003922 printf( "passed\n X.509 DHM parameter load: " );
3923
3924 i = strlen( test_dhm_params );
3925 j = strlen( test_ca_pwd );
3926
Paul Bakkereae09db2013-06-06 12:35:54 +02003927 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003928 {
3929 if( verbose != 0 )
3930 printf( "failed\n" );
3931
3932 return( ret );
3933 }
3934
3935 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003936 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003937#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
3939 x509_free( &cacert );
3940 x509_free( &clicert );
3941 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003942#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003943 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
3946 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003947#else
3948 ((void) verbose);
3949 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3950#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003951}
3952
3953#endif
3954
3955#endif