blob: 3040621ea891e791f0d908ed434ea94693a4348c [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
Paul Bakker74111d32011-01-15 16:57:55 +0000522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 sig->len = len;
527 sig->p = *p;
528
529 *p += len;
530
531 return( 0 );
532}
533
534/*
535 * X.509 v2/v3 unique identifier (not parsed)
536 */
537static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 x509_buf *uid, int n )
540{
541 int ret;
542
543 if( *p == end )
544 return( 0 );
545
546 uid->tag = **p;
547
548 if( ( ret = asn1_get_tag( p, end, &uid->len,
549 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
550 {
Paul Bakker40e46942009-01-03 21:51:57 +0000551 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 return( 0 );
553
554 return( ret );
555 }
556
557 uid->p = *p;
558 *p += uid->len;
559
560 return( 0 );
561}
562
563/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000564 * X.509 Extensions (No parsing of extensions, pointer should
565 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
567static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000569 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570{
Paul Bakker23986e52011-04-24 08:57:21 +0000571 int ret;
572 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 if( *p == end )
575 return( 0 );
576
577 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000578
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000580 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 ext->p = *p;
584 end = *p + ext->len;
585
586 /*
587 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
588 *
589 * Extension ::= SEQUENCE {
590 * extnID OBJECT IDENTIFIER,
591 * critical BOOLEAN DEFAULT FALSE,
592 * extnValue OCTET STRING }
593 */
594 if( ( ret = asn1_get_tag( p, end, &len,
595 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000599 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000600 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602 return( 0 );
603}
604
605/*
606 * X.509 CRL v2 extensions (no extensions parsed yet.)
607 */
608static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000609 const unsigned char *end,
610 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611{
Paul Bakker23986e52011-04-24 08:57:21 +0000612 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000613 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000615 /* Get explicit tag */
616 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000617 {
618 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
619 return( 0 );
620
621 return( ret );
622 }
623
624 while( *p < end )
625 {
626 if( ( ret = asn1_get_tag( p, end, &len,
627 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000628 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000629
630 *p += len;
631 }
632
633 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000634 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000635 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
636
637 return( 0 );
638}
639
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000640/*
641 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
642 */
643static int x509_get_crl_entry_ext( unsigned char **p,
644 const unsigned char *end,
645 x509_buf *ext )
646{
647 int ret;
648 size_t len = 0;
649
650 /* OPTIONAL */
651 if (end <= *p)
652 return( 0 );
653
654 ext->tag = **p;
655 ext->p = *p;
656
657 /*
658 * Get CRL-entry extension sequence header
659 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
660 */
661 if( ( ret = asn1_get_tag( p, end, &ext->len,
662 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
663 {
664 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
665 {
666 ext->p = NULL;
667 return( 0 );
668 }
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
670 }
671
672 end = *p + ext->len;
673
674 if( end != *p + ext->len )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
676 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
677
678 while( *p < end )
679 {
680 if( ( ret = asn1_get_tag( p, end, &len,
681 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
682 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
683
684 *p += len;
685 }
686
687 if( *p != end )
688 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
689 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
690
691 return( 0 );
692}
693
Paul Bakker74111d32011-01-15 16:57:55 +0000694static int x509_get_basic_constraints( unsigned char **p,
695 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000696 int *ca_istrue,
697 int *max_pathlen )
698{
Paul Bakker23986e52011-04-24 08:57:21 +0000699 int ret;
700 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000701
702 /*
703 * BasicConstraints ::= SEQUENCE {
704 * cA BOOLEAN DEFAULT FALSE,
705 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
706 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000707 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000708 *max_pathlen = 0; /* endless */
709
710 if( ( ret = asn1_get_tag( p, end, &len,
711 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000712 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000713
714 if( *p == end )
715 return 0;
716
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000718 {
719 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000720 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
722 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000724
Paul Bakker3cccddb2011-01-16 21:46:31 +0000725 if( *ca_istrue != 0 )
726 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000727 }
728
729 if( *p == end )
730 return 0;
731
732 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000734
735 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000736 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000737 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
738
739 (*max_pathlen)++;
740
Paul Bakker74111d32011-01-15 16:57:55 +0000741 return 0;
742}
743
744static int x509_get_ns_cert_type( unsigned char **p,
745 const unsigned char *end,
746 unsigned char *ns_cert_type)
747{
748 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000749 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000753
754 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000755 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000756 POLARSSL_ERR_ASN1_INVALID_LENGTH );
757
758 /* Get actual bitstring */
759 *ns_cert_type = *bs.p;
760 return 0;
761}
762
763static int x509_get_key_usage( unsigned char **p,
764 const unsigned char *end,
765 unsigned char *key_usage)
766{
767 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000768 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000769
770 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000772
Paul Bakker94a67962012-08-23 13:03:52 +0000773 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000774 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000775 POLARSSL_ERR_ASN1_INVALID_LENGTH );
776
777 /* Get actual bitstring */
778 *key_usage = *bs.p;
779 return 0;
780}
781
Paul Bakkerd98030e2009-05-02 15:13:40 +0000782/*
Paul Bakker74111d32011-01-15 16:57:55 +0000783 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
784 *
785 * KeyPurposeId ::= OBJECT IDENTIFIER
786 */
787static int x509_get_ext_key_usage( unsigned char **p,
788 const unsigned char *end,
789 x509_sequence *ext_key_usage)
790{
791 int ret;
792
793 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000794 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000795
796 /* Sequence length must be >= 1 */
797 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000798 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000799 POLARSSL_ERR_ASN1_INVALID_LENGTH );
800
801 return 0;
802}
803
804/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000805 * SubjectAltName ::= GeneralNames
806 *
807 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
808 *
809 * GeneralName ::= CHOICE {
810 * otherName [0] OtherName,
811 * rfc822Name [1] IA5String,
812 * dNSName [2] IA5String,
813 * x400Address [3] ORAddress,
814 * directoryName [4] Name,
815 * ediPartyName [5] EDIPartyName,
816 * uniformResourceIdentifier [6] IA5String,
817 * iPAddress [7] OCTET STRING,
818 * registeredID [8] OBJECT IDENTIFIER }
819 *
820 * OtherName ::= SEQUENCE {
821 * type-id OBJECT IDENTIFIER,
822 * value [0] EXPLICIT ANY DEFINED BY type-id }
823 *
824 * EDIPartyName ::= SEQUENCE {
825 * nameAssigner [0] DirectoryString OPTIONAL,
826 * partyName [1] DirectoryString }
827 *
828 * NOTE: PolarSSL only parses and uses dNSName at this point.
829 */
830static int x509_get_subject_alt_name( unsigned char **p,
831 const unsigned char *end,
832 x509_sequence *subject_alt_name )
833{
834 int ret;
835 size_t len, tag_len;
836 asn1_buf *buf;
837 unsigned char tag;
838 asn1_sequence *cur = subject_alt_name;
839
840 /* Get main sequence tag */
841 if( ( ret = asn1_get_tag( p, end, &len,
842 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
844
845 if( *p + len != end )
846 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
847 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
848
849 while( *p < end )
850 {
851 if( ( end - *p ) < 1 )
852 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
853 POLARSSL_ERR_ASN1_OUT_OF_DATA );
854
855 tag = **p;
856 (*p)++;
857 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
858 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
859
860 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
861 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
862 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
863
864 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
865 {
866 *p += tag_len;
867 continue;
868 }
869
870 buf = &(cur->buf);
871 buf->tag = tag;
872 buf->p = *p;
873 buf->len = tag_len;
874 *p += buf->len;
875
876 /* Allocate and assign next pointer */
877 if (*p < end)
878 {
Manuel Pégourié-Gonnardfdec9572014-11-11 23:11:16 +0100879 if( cur->next != NULL )
880 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
881
Paul Bakkera8cd2392012-02-11 16:09:32 +0000882 cur->next = (asn1_sequence *) malloc(
883 sizeof( asn1_sequence ) );
884
885 if( cur->next == NULL )
886 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
887 POLARSSL_ERR_ASN1_MALLOC_FAILED );
888
Paul Bakker535e97d2012-08-23 10:49:55 +0000889 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000890 cur = cur->next;
891 }
892 }
893
894 /* Set final sequence entry's next pointer to NULL */
895 cur->next = NULL;
896
897 if( *p != end )
898 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
899 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
900
901 return( 0 );
902}
903
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100904static int x509_get_crt_ext_type( const x509_buf *oid )
905{
906 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == oid->len ) &&
907 memcmp( oid->p, OID_BASIC_CONSTRAINTS, oid->len ) == 0 )
908 {
909 return( EXT_BASIC_CONSTRAINTS );
910 }
911 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == oid->len ) &&
912 memcmp( oid->p, OID_NS_CERT_TYPE, oid->len ) == 0 )
913 {
914 return( EXT_NS_CERT_TYPE );
915 }
916 else if( ( OID_SIZE( OID_KEY_USAGE ) == oid->len ) &&
917 memcmp( oid->p, OID_KEY_USAGE, oid->len ) == 0 )
918 {
919 return( EXT_KEY_USAGE );
920 }
921 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == oid->len ) &&
922 memcmp( oid->p, OID_EXTENDED_KEY_USAGE, oid->len ) == 0 )
923 {
924 return( EXT_EXTENDED_KEY_USAGE );
925 }
926 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == oid->len ) &&
927 memcmp( oid->p, OID_SUBJECT_ALT_NAME, oid->len ) == 0 )
928 {
929 return( EXT_SUBJECT_ALT_NAME );
930 }
931
932 return( -1 );
933}
934
Paul Bakkera8cd2392012-02-11 16:09:32 +0000935/*
Paul Bakker74111d32011-01-15 16:57:55 +0000936 * X.509 v3 extensions
937 *
938 * TODO: Perform all of the basic constraints tests required by the RFC
939 * TODO: Set values for undetected extensions to a sane default?
940 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000941 */
942static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000943 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000944 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000945{
Paul Bakker23986e52011-04-24 08:57:21 +0000946 int ret;
947 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000949
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000950 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000951 {
952 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
953 return( 0 );
954
955 return( ret );
956 }
957
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 while( *p < end )
959 {
Paul Bakker74111d32011-01-15 16:57:55 +0000960 /*
961 * Extension ::= SEQUENCE {
962 * extnID OBJECT IDENTIFIER,
963 * critical BOOLEAN DEFAULT FALSE,
964 * extnValue OCTET STRING }
965 */
966 x509_buf extn_oid = {0, 0, NULL};
967 int is_critical = 0; /* DEFAULT FALSE */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +0100968 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000969
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 if( ( ret = asn1_get_tag( p, end, &len,
971 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000972 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000974 end_ext_data = *p + len;
975
Paul Bakker74111d32011-01-15 16:57:55 +0000976 /* Get extension ID */
977 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Paul Bakker74111d32011-01-15 16:57:55 +0000979 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000980 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 extn_oid.p = *p;
983 *p += extn_oid.len;
984
985 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000986 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000987 POLARSSL_ERR_ASN1_OUT_OF_DATA );
988
989 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000990 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000991 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000992 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Paul Bakker74111d32011-01-15 16:57:55 +0000994 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000995 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000997 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000999 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001000
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001001 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +00001002 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +00001003 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker74111d32011-01-15 16:57:55 +00001005 /*
1006 * Detect supported extensions
1007 */
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001008 ext_type = x509_get_crt_ext_type( &extn_oid );
1009
1010 if( ext_type < 0 )
Paul Bakker74111d32011-01-15 16:57:55 +00001011 {
1012 /* No parser found, skip extension */
1013 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
Paul Bakker5c721f92011-07-27 16:51:09 +00001015#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001016 if( is_critical )
1017 {
1018 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001019 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001020 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1021 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001022#endif
Manuel Pégourié-Gonnard017bf572014-11-17 11:00:23 +01001023 continue;
1024 }
1025
1026 /* Forbid repeated extensions */
1027 if( ( crt->ext_types & ext_type ) != 0 )
1028 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS );
1029
1030 crt->ext_types |= ext_type;
1031
1032 switch( ext_type )
1033 {
1034 case EXT_BASIC_CONSTRAINTS:
1035 /* Parse basic constraints */
1036 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
1037 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
1038 return( ret );
1039 break;
1040
1041 case EXT_KEY_USAGE:
1042 /* Parse key usage */
1043 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1044 &crt->key_usage ) ) != 0 )
1045 return( ret );
1046 break;
1047
1048 case EXT_EXTENDED_KEY_USAGE:
1049 /* Parse extended key usage */
1050 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1051 &crt->ext_key_usage ) ) != 0 )
1052 return( ret );
1053 break;
1054
1055 case EXT_SUBJECT_ALT_NAME:
1056 /* Parse subject alt name */
1057 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1058 &crt->subject_alt_names ) ) != 0 )
1059 return( ret );
1060 break;
1061
1062 case EXT_NS_CERT_TYPE:
1063 /* Parse netscape certificate type */
1064 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1065 &crt->ns_cert_type ) ) != 0 )
1066 return( ret );
1067 break;
1068
1069 default:
1070 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001071 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
1073
1074 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001075 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001076 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 return( 0 );
1079}
1080
1081/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 * X.509 CRL Entries
1083 */
1084static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001085 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001086 x509_crl_entry *entry )
1087{
Paul Bakker23986e52011-04-24 08:57:21 +00001088 int ret;
1089 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001090 x509_crl_entry *cur_entry = entry;
1091
1092 if( *p == end )
1093 return( 0 );
1094
Paul Bakker9be19372009-07-27 20:21:53 +00001095 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001096 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1097 {
1098 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1099 return( 0 );
1100
1101 return( ret );
1102 }
1103
Paul Bakker9be19372009-07-27 20:21:53 +00001104 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001105
1106 while( *p < end )
1107 {
Paul Bakker23986e52011-04-24 08:57:21 +00001108 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001109 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001110
1111 if( ( ret = asn1_get_tag( p, end, &len2,
1112 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1113 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001114 return( ret );
1115 }
1116
Paul Bakker9be19372009-07-27 20:21:53 +00001117 cur_entry->raw.tag = **p;
1118 cur_entry->raw.p = *p;
1119 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001120 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001121
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001122 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001123 return( ret );
1124
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001125 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001126 return( ret );
1127
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001128 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001129 return( ret );
1130
Paul Bakker74111d32011-01-15 16:57:55 +00001131 if ( *p < end )
1132 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001133 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001134
1135 if( cur_entry->next == NULL )
1136 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1137
Paul Bakkerd98030e2009-05-02 15:13:40 +00001138 cur_entry = cur_entry->next;
1139 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1140 }
1141 }
1142
1143 return( 0 );
1144}
1145
Paul Bakker27d66162010-03-17 06:56:01 +00001146static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1147{
1148 if( sig_oid->len == 9 &&
1149 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1150 {
1151 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1152 {
1153 *sig_alg = sig_oid->p[8];
1154 return( 0 );
1155 }
1156
1157 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1158 {
1159 *sig_alg = sig_oid->p[8];
1160 return( 0 );
1161 }
1162
1163 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1164 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001165 if( sig_oid->len == 5 &&
1166 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1167 {
1168 *sig_alg = SIG_RSA_SHA1;
1169 return( 0 );
1170 }
Paul Bakker27d66162010-03-17 06:56:01 +00001171
1172 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1173}
1174
Paul Bakkerd98030e2009-05-02 15:13:40 +00001175/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001176 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001178static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1179 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001180{
Paul Bakker23986e52011-04-24 08:57:21 +00001181 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001182 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001183 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001184
Paul Bakker320a4b52009-03-28 18:52:39 +00001185 /*
1186 * Check for valid input
1187 */
1188 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001189 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001190
Paul Bakker96743fc2011-02-12 14:30:57 +00001191 p = (unsigned char *) malloc( len = buflen );
1192
1193 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001194 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001195
1196 memcpy( p, buf, buflen );
1197
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 crt->raw.p = p;
1199 crt->raw.len = len;
1200 end = p + len;
1201
1202 /*
1203 * Certificate ::= SEQUENCE {
1204 * tbsCertificate TBSCertificate,
1205 * signatureAlgorithm AlgorithmIdentifier,
1206 * signatureValue BIT STRING }
1207 */
1208 if( ( ret = asn1_get_tag( &p, end, &len,
1209 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001212 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
Paul Bakkerb00ca422012-09-25 12:10:00 +00001215 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001218 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001219 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001221 crt_end = p + len;
Paul Bakkerd6d41092013-06-13 09:00:25 +02001222
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 /*
1224 * TBSCertificate ::= SEQUENCE {
1225 */
1226 crt->tbs.p = p;
1227
1228 if( ( ret = asn1_get_tag( &p, end, &len,
1229 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1230 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001231 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001232 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 }
1234
1235 end = p + len;
1236 crt->tbs.len = end - crt->tbs.p;
1237
1238 /*
1239 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1240 *
1241 * CertificateSerialNumber ::= INTEGER
1242 *
1243 * signature AlgorithmIdentifier
1244 */
1245 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1246 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1247 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1248 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001249 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 return( ret );
1251 }
1252
1253 crt->version++;
1254
1255 if( crt->version > 3 )
1256 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001257 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001258 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001259 }
1260
Paul Bakker27d66162010-03-17 06:56:01 +00001261 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001263 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001264 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 }
1266
1267 /*
1268 * issuer Name
1269 */
1270 crt->issuer_raw.p = p;
1271
1272 if( ( ret = asn1_get_tag( &p, end, &len,
1273 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1274 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001275 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001276 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 }
1278
1279 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1280 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001281 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 return( ret );
1283 }
1284
1285 crt->issuer_raw.len = p - crt->issuer_raw.p;
1286
1287 /*
1288 * Validity ::= SEQUENCE {
1289 * notBefore Time,
1290 * notAfter Time }
1291 *
1292 */
1293 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1294 &crt->valid_to ) ) != 0 )
1295 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 return( ret );
1298 }
1299
1300 /*
1301 * subject Name
1302 */
1303 crt->subject_raw.p = p;
1304
1305 if( ( ret = asn1_get_tag( &p, end, &len,
1306 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1307 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001308 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001309 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
1311
Paul Bakkercefb3962012-06-27 11:51:09 +00001312 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317
1318 crt->subject_raw.len = p - crt->subject_raw.p;
1319
1320 /*
1321 * SubjectPublicKeyInfo ::= SEQUENCE
1322 * algorithm AlgorithmIdentifier,
1323 * subjectPublicKey BIT STRING }
1324 */
1325 if( ( ret = asn1_get_tag( &p, end, &len,
1326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1327 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001328 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001329 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 }
1331
1332 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1333 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1334 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001335 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 return( ret );
1337 }
1338
1339 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 return( ret );
1343 }
1344
1345 crt->rsa.len = mpi_size( &crt->rsa.N );
1346
1347 /*
1348 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1349 * -- If present, version shall be v2 or v3
1350 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1351 * -- If present, version shall be v2 or v3
1352 * extensions [3] EXPLICIT Extensions OPTIONAL
1353 * -- If present, version shall be v3
1354 */
1355 if( crt->version == 2 || crt->version == 3 )
1356 {
1357 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1358 if( ret != 0 )
1359 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001360 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 return( ret );
1362 }
1363 }
1364
1365 if( crt->version == 2 || crt->version == 3 )
1366 {
1367 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1368 if( ret != 0 )
1369 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001370 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 return( ret );
1372 }
1373 }
1374
1375 if( crt->version == 3 )
1376 {
Paul Bakker74111d32011-01-15 16:57:55 +00001377 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 if( ret != 0 )
1379 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001380 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 return( ret );
1382 }
1383 }
1384
1385 if( p != end )
1386 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001387 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001388 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001389 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 }
1391
Paul Bakkerb00ca422012-09-25 12:10:00 +00001392 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 /*
1395 * signatureAlgorithm AlgorithmIdentifier,
1396 * signatureValue BIT STRING
1397 */
1398 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1399 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001400 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 return( ret );
1402 }
1403
Paul Bakker535e97d2012-08-23 10:49:55 +00001404 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1405 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001407 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001408 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
1410
1411 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1412 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001413 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 return( ret );
1415 }
1416
1417 if( p != end )
1418 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001419 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001420 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001421 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001424 return( 0 );
1425}
1426
1427/*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001428 * Parse one X.509 certificate in DER format from a buffer and add them to a
1429 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001430 */
Paul Bakkerd6d41092013-06-13 09:00:25 +02001431int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001432{
Paul Bakkerd6d41092013-06-13 09:00:25 +02001433 int ret;
1434 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001435
1436 /*
1437 * Check for valid input
1438 */
1439 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001440 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001441
1442 while( crt->version != 0 && crt->next != NULL )
1443 {
1444 prev = crt;
1445 crt = crt->next;
1446 }
1447
1448 /*
1449 * Add new certificate on the end of the chain if needed.
1450 */
1451 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001452 {
1453 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1454
Paul Bakker7d06ad22009-05-02 15:53:56 +00001455 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001456 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001457
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001458 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001459 crt = crt->next;
1460 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001461 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Paul Bakkerd6d41092013-06-13 09:00:25 +02001463 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1464 {
1465 if( prev )
1466 prev->next = NULL;
1467
1468 if( crt != chain )
1469 free( crt );
1470
1471 return( ret );
1472 }
1473
1474 return( 0 );
1475}
1476
1477/*
1478 * Parse one or more PEM certificates from a buffer and add them to the chained list
1479 */
1480int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1481{
1482 int ret, success = 0, first_error = 0, total_failed = 0;
1483 int buf_format = X509_FORMAT_DER;
1484
1485 /*
1486 * Check for valid input
1487 */
1488 if( chain == NULL || buf == NULL )
1489 return( POLARSSL_ERR_X509_INVALID_INPUT );
1490
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001491 /*
1492 * Determine buffer content. Buffer contains either one DER certificate or
1493 * one or more PEM certificates.
1494 */
1495#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001496 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001497 buf_format = X509_FORMAT_PEM;
1498#endif
1499
1500 if( buf_format == X509_FORMAT_DER )
Paul Bakkerd6d41092013-06-13 09:00:25 +02001501 return x509parse_crt_der( chain, buf, buflen );
1502
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001503#if defined(POLARSSL_PEM_C)
1504 if( buf_format == X509_FORMAT_PEM )
1505 {
1506 pem_context pem;
1507
1508 while( buflen > 0 )
1509 {
1510 size_t use_len;
1511 pem_init( &pem );
1512
1513 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02001514 (char *) "-----BEGIN CERTIFICATE-----",
1515 (char *) "-----END CERTIFICATE-----",
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001516 buf, NULL, 0, &use_len );
1517
1518 if( ret == 0 )
1519 {
1520 /*
1521 * Was PEM encoded
1522 */
1523 buflen -= use_len;
1524 buf += use_len;
1525 }
Paul Bakker64171862013-06-06 15:01:18 +02001526 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1527 {
1528 return( ret );
1529 }
Paul Bakker9255e832013-06-06 14:58:28 +02001530 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001531 {
1532 pem_free( &pem );
1533
Paul Bakker64171862013-06-06 15:01:18 +02001534 /*
1535 * PEM header and footer were found
1536 */
1537 buflen -= use_len;
1538 buf += use_len;
1539
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001540 if( first_error == 0 )
1541 first_error = ret;
1542
Manuel Pégourié-Gonnard7d75ea42014-10-23 15:13:39 +02001543 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001544 continue;
1545 }
1546 else
1547 break;
1548
Paul Bakkerd6d41092013-06-13 09:00:25 +02001549 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001550
1551 pem_free( &pem );
1552
1553 if( ret != 0 )
1554 {
1555 /*
Paul Bakkerd6d41092013-06-13 09:00:25 +02001556 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001557 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001558 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001559 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001560
1561 if( first_error == 0 )
1562 first_error = ret;
1563
Paul Bakkerd6d41092013-06-13 09:00:25 +02001564 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001565 continue;
1566 }
1567
1568 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001569 }
1570 }
1571#endif
1572
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001573 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001574 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001575 else if( first_error )
1576 return( first_error );
1577 else
1578 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001579}
1580
1581/*
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001582 * Parse one CRLs in DER format and append it to the chained list
Paul Bakkerd98030e2009-05-02 15:13:40 +00001583 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001584static int x509_crl_parse_der( x509_crl *chain,
1585 const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001586{
Paul Bakker23986e52011-04-24 08:57:21 +00001587 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001588 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001589 unsigned char *p, *end;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001590 x509_buf sig_params1, sig_params2;
1591 x509_crl *crl = chain;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001592
1593 /*
1594 * Check for valid input
1595 */
1596 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001597 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001599 memset( &sig_params1, 0, sizeof( x509_buf ) );
1600 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601
1602 /*
1603 * Add new CRL on the end of the chain if needed.
1604 */
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001605 while( crl->version != 0 && crl->next != NULL )
1606 crl = crl->next;
1607
1608 if( crl->version != 0 && crl->next == NULL )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001609 {
1610 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1611
Paul Bakker7d06ad22009-05-02 15:53:56 +00001612 if( crl->next == NULL )
1613 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001614 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001615 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001616 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001617
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001618 memset( crl->next, 0, sizeof( x509_crl ) );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001619 crl = crl->next;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001620 }
1621
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001622 /*
1623 * Copy raw DER-encoded CRL
1624 */
1625 if( ( p = malloc( buflen ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001626 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001627
1628 memcpy( p, buf, buflen );
1629
Paul Bakkerd98030e2009-05-02 15:13:40 +00001630 crl->raw.p = p;
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001631 crl->raw.len = buflen;
1632
1633 end = p + buflen;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634
1635 /*
1636 * CertificateList ::= SEQUENCE {
1637 * tbsCertList TBSCertList,
1638 * signatureAlgorithm AlgorithmIdentifier,
1639 * signatureValue BIT STRING }
1640 */
1641 if( ( ret = asn1_get_tag( &p, end, &len,
1642 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1643 {
1644 x509_crl_free( crl );
1645 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1646 }
1647
Paul Bakker23986e52011-04-24 08:57:21 +00001648 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001649 {
1650 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001651 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1653 }
1654
1655 /*
1656 * TBSCertList ::= SEQUENCE {
1657 */
1658 crl->tbs.p = p;
1659
1660 if( ( ret = asn1_get_tag( &p, end, &len,
1661 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1662 {
1663 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001664 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001665 }
1666
1667 end = p + len;
1668 crl->tbs.len = end - crl->tbs.p;
1669
1670 /*
1671 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1672 * -- if present, MUST be v2
1673 *
1674 * signature AlgorithmIdentifier
1675 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001676 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001677 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1678 {
1679 x509_crl_free( crl );
1680 return( ret );
1681 }
1682
1683 crl->version++;
1684
1685 if( crl->version > 2 )
1686 {
1687 x509_crl_free( crl );
1688 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1689 }
1690
Paul Bakker27d66162010-03-17 06:56:01 +00001691 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692 {
1693 x509_crl_free( crl );
1694 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1695 }
1696
1697 /*
1698 * issuer Name
1699 */
1700 crl->issuer_raw.p = p;
1701
1702 if( ( ret = asn1_get_tag( &p, end, &len,
1703 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1704 {
1705 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001706 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 }
1708
1709 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1710 {
1711 x509_crl_free( crl );
1712 return( ret );
1713 }
1714
1715 crl->issuer_raw.len = p - crl->issuer_raw.p;
1716
1717 /*
1718 * thisUpdate Time
1719 * nextUpdate Time OPTIONAL
1720 */
Paul Bakker91200182010-02-18 21:26:15 +00001721 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 {
1723 x509_crl_free( crl );
1724 return( ret );
1725 }
1726
Paul Bakker91200182010-02-18 21:26:15 +00001727 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 {
Paul Bakker9d781402011-05-09 16:17:09 +00001729 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001730 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001731 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001732 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001733 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001734 x509_crl_free( crl );
1735 return( ret );
1736 }
1737 }
1738
1739 /*
1740 * revokedCertificates SEQUENCE OF SEQUENCE {
1741 * userCertificate CertificateSerialNumber,
1742 * revocationDate Time,
1743 * crlEntryExtensions Extensions OPTIONAL
1744 * -- if present, MUST be v2
1745 * } OPTIONAL
1746 */
1747 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
1750 return( ret );
1751 }
1752
1753 /*
1754 * crlExtensions EXPLICIT Extensions OPTIONAL
1755 * -- if present, MUST be v2
1756 */
1757 if( crl->version == 2 )
1758 {
1759 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1760
1761 if( ret != 0 )
1762 {
1763 x509_crl_free( crl );
1764 return( ret );
1765 }
1766 }
1767
1768 if( p != end )
1769 {
1770 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001771 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1773 }
1774
1775 end = crl->raw.p + crl->raw.len;
1776
1777 /*
1778 * signatureAlgorithm AlgorithmIdentifier,
1779 * signatureValue BIT STRING
1780 */
1781 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1782 {
1783 x509_crl_free( crl );
1784 return( ret );
1785 }
1786
Paul Bakker535e97d2012-08-23 10:49:55 +00001787 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1788 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001789 {
1790 x509_crl_free( crl );
1791 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1792 }
1793
1794 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1795 {
1796 x509_crl_free( crl );
1797 return( ret );
1798 }
1799
1800 if( p != end )
1801 {
1802 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001803 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1805 }
1806
Paul Bakkerd98030e2009-05-02 15:13:40 +00001807 return( 0 );
1808}
1809
Manuel Pégourié-Gonnard6a095d22014-11-20 17:03:09 +01001810/*
1811 * Parse one or more CRLs and add them to the chained list
1812 */
1813int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
1814{
1815#if defined(POLARSSL_PEM_C)
1816 int ret;
1817 size_t use_len;
1818 pem_context pem;
1819 int is_pem = 0;
1820
1821 if( chain == NULL || buf == NULL )
1822 return( POLARSSL_ERR_X509_INVALID_INPUT );
1823
1824 do
1825 {
1826 pem_init( &pem );
1827 ret = pem_read_buffer( &pem,
1828 (char *) "-----BEGIN X509 CRL-----",
1829 (char *) "-----END X509 CRL-----",
1830 buf, NULL, 0, &use_len );
1831
1832 if( ret == 0 )
1833 {
1834 /*
1835 * Was PEM encoded
1836 */
1837 is_pem = 1;
1838
1839 buflen -= use_len;
1840 buf += use_len;
1841
1842 if( ( ret = x509_crl_parse_der( chain,
1843 pem.buf, pem.buflen ) ) != 0 )
1844 {
1845 return( ret );
1846 }
1847
1848 pem_free( &pem );
1849 }
1850 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1851 {
1852 pem_free( &pem );
1853 return( ret );
1854 }
1855 }
1856 while( is_pem && buflen > 0 );
1857
1858 if( is_pem )
1859 return( 0 );
1860 else
1861#endif /* POLARSSL_PEM_C */
1862 return( x509_crl_parse_der( chain, buf, buflen ) );
1863}
1864
Paul Bakker335db3f2011-04-25 15:28:35 +00001865#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001866/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001867 * Load all data from a file into a given buffer.
1868 */
Paul Bakker1d073c52014-07-08 20:15:51 +02001869static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001870{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001871 FILE *f;
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001872 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001873
Paul Bakkerd98030e2009-05-02 15:13:40 +00001874 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001875 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001876
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 fseek( f, 0, SEEK_END );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001878 if( ( size = ftell( f ) ) == -1 )
1879 {
1880 fclose( f );
1881 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1882 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001883 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001884
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001885 *n = (size_t) size;
1886
1887 if( *n + 1 == 0 ||
1888 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1889 {
1890 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001891 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerfe7c24c2013-09-11 11:37:33 +02001892 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001893
Paul Bakkerd98030e2009-05-02 15:13:40 +00001894 if( fread( *buf, 1, *n, f ) != *n )
1895 {
1896 fclose( f );
1897 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001898 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001899 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001900
Paul Bakkerd98030e2009-05-02 15:13:40 +00001901 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001902
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001904
Paul Bakkerd98030e2009-05-02 15:13:40 +00001905 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001906}
1907
1908/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 * Load one or more certificates and add them to the chained list
1910 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001911int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001912{
1913 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 size_t n;
1915 unsigned char *buf;
1916
Paul Bakker69e095c2011-12-10 21:55:01 +00001917 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Paul Bakker69e095c2011-12-10 21:55:01 +00001920 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001921
1922 memset( buf, 0, n + 1 );
1923 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 return( ret );
1926}
1927
Paul Bakker8d914582012-06-04 12:46:42 +00001928int x509parse_crtpath( x509_cert *chain, const char *path )
1929{
1930 int ret = 0;
1931#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001932 int w_ret;
1933 WCHAR szDir[MAX_PATH];
1934 char filename[MAX_PATH];
1935 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001936 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001937
Paul Bakker97872ac2012-11-02 12:53:26 +00001938 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001939 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001940
1941 if( len > MAX_PATH - 3 )
1942 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001943
Paul Bakker3338b792012-10-01 21:13:10 +00001944 memset( szDir, 0, sizeof(szDir) );
1945 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001946 memcpy( filename, path, len );
1947 filename[len++] = '\\';
1948 p = filename + len;
1949 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001950
Paul Bakker40cc9142014-07-07 15:16:47 +02001951 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir, MAX_PATH - 3 );
Manuel Pégourié-Gonnard2dc15c82015-02-05 11:34:49 +00001952 if( w_ret == 0 )
1953 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001954
Paul Bakker97872ac2012-11-02 12:53:26 +00001955 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001956 if (hFind == INVALID_HANDLE_VALUE)
1957 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1958
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001959 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001960 do
1961 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001962 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001963
Paul Bakkere4791f32012-06-04 21:29:15 +00001964 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001965 continue;
1966
Paul Bakker3338b792012-10-01 21:13:10 +00001967 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1968 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001969 p, len - 1,
1970 NULL, NULL );
Manuel Pégourié-Gonnard2dc15c82015-02-05 11:34:49 +00001971 if( w_ret == 0 )
1972 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker8d914582012-06-04 12:46:42 +00001973
Paul Bakker3338b792012-10-01 21:13:10 +00001974 w_ret = x509parse_crtfile( chain, filename );
1975 if( w_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001976 ret++;
1977 else
1978 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001979 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001980 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001981
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001982 if (GetLastError() != ERROR_NO_MORE_FILES)
1983 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001984
1985 FindClose( hFind );
1986#else
Paul Bakker9ccb2112014-07-07 13:43:31 +02001987#if defined(POLARSSL_HAVE_READDIR_R)
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001988 int t_ret, i;
1989 struct stat sb;
1990 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00001991 char entry_name[255];
1992 DIR *dir = opendir( path );
1993
1994 if( dir == NULL)
1995 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1996
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001997 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00001998 {
Paul Bakkercbfcaa92013-06-13 09:20:25 +02001999 if( result == NULL )
2000 break;
2001
2002 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2003
2004 i = stat( entry_name, &sb );
2005
2006 if( i == -1 )
Paul Bakker88a22642013-09-11 12:14:16 +02002007 {
2008 closedir( dir );
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002009 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker88a22642013-09-11 12:14:16 +02002010 }
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002011
2012 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002013 continue;
2014
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002015 // Ignore parse errors
2016 //
Paul Bakker8d914582012-06-04 12:46:42 +00002017 t_ret = x509parse_crtfile( chain, entry_name );
2018 if( t_ret < 0 )
Paul Bakkercbfcaa92013-06-13 09:20:25 +02002019 ret++;
2020 else
2021 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002022 }
2023 closedir( dir );
Paul Bakker9ccb2112014-07-07 13:43:31 +02002024#else /* POLARSSL_HAVE_READDIR_R */
2025 ((void) chain);
2026 ((void) path);
2027 ret = POLARSSL_ERR_X509_FEATURE_UNAVAILABLE;
2028#endif /* POLARSSL_HAVE_READDIR_R */
2029#endif /* _WIN32 */
Paul Bakker8d914582012-06-04 12:46:42 +00002030
2031 return( ret );
2032}
2033
Paul Bakkerd98030e2009-05-02 15:13:40 +00002034/*
2035 * Load one or more CRLs and add them to the chained list
2036 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002037int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002038{
2039 int ret;
2040 size_t n;
2041 unsigned char *buf;
2042
Paul Bakker69e095c2011-12-10 21:55:01 +00002043 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2044 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002045
Paul Bakker27fdf462011-06-09 13:55:13 +00002046 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002047
2048 memset( buf, 0, n + 1 );
2049 free( buf );
2050
2051 return( ret );
2052}
2053
Paul Bakker5121ce52009-01-03 21:22:43 +00002054/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002055 * Load and parse a private RSA key
2056 */
2057int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2058{
2059 int ret;
2060 size_t n;
2061 unsigned char *buf;
2062
Paul Bakker69e095c2011-12-10 21:55:01 +00002063 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2064 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002065
2066 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002067 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002068 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002069 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002070 (unsigned char *) pwd, strlen( pwd ) );
2071
2072 memset( buf, 0, n + 1 );
2073 free( buf );
2074
2075 return( ret );
2076}
2077
2078/*
2079 * Load and parse a public RSA key
2080 */
2081int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2082{
2083 int ret;
2084 size_t n;
2085 unsigned char *buf;
2086
Paul Bakker69e095c2011-12-10 21:55:01 +00002087 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2088 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002089
Paul Bakker27fdf462011-06-09 13:55:13 +00002090 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002091
2092 memset( buf, 0, n + 1 );
2093 free( buf );
2094
2095 return( ret );
2096}
2097#endif /* POLARSSL_FS_IO */
2098
2099/*
Paul Bakker65a19092013-06-06 21:14:58 +02002100 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 */
Paul Bakker65a19092013-06-06 21:14:58 +02002102static int x509parse_key_pkcs1_der( rsa_context *rsa,
2103 const unsigned char *key,
2104 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105{
Paul Bakker23986e52011-04-24 08:57:21 +00002106 int ret;
2107 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002109
Paul Bakker96743fc2011-02-12 14:30:57 +00002110 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002111 end = p + keylen;
2112
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 /*
Paul Bakker65a19092013-06-06 21:14:58 +02002114 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002115 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 * RSAPrivateKey ::= SEQUENCE {
2117 * version Version,
2118 * modulus INTEGER, -- n
2119 * publicExponent INTEGER, -- e
2120 * privateExponent INTEGER, -- d
2121 * prime1 INTEGER, -- p
2122 * prime2 INTEGER, -- q
2123 * exponent1 INTEGER, -- d mod (p-1)
2124 * exponent2 INTEGER, -- d mod (q-1)
2125 * coefficient INTEGER, -- (inverse of q) mod p
2126 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2127 * }
2128 */
2129 if( ( ret = asn1_get_tag( &p, end, &len,
2130 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2131 {
Paul Bakker9d781402011-05-09 16:17:09 +00002132 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 }
2134
2135 end = p + len;
2136
2137 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2138 {
Paul Bakker9d781402011-05-09 16:17:09 +00002139 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
2142 if( rsa->ver != 0 )
2143 {
Paul Bakker9d781402011-05-09 16:17:09 +00002144 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 }
2146
2147 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2148 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2149 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2150 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2151 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2152 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2153 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2154 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2155 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002157 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
2159
2160 rsa->len = mpi_size( &rsa->N );
2161
2162 if( p != end )
2163 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002165 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002166 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002167 }
2168
2169 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2170 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002171 rsa_free( rsa );
2172 return( ret );
2173 }
2174
Paul Bakker65a19092013-06-06 21:14:58 +02002175 return( 0 );
2176}
2177
2178/*
2179 * Parse an unencrypted PKCS#8 encoded private RSA key
2180 */
2181static int x509parse_key_pkcs8_unencrypted_der(
2182 rsa_context *rsa,
2183 const unsigned char *key,
2184 size_t keylen )
2185{
2186 int ret;
2187 size_t len;
2188 unsigned char *p, *end;
2189 x509_buf pk_alg_oid;
2190
2191 p = (unsigned char *) key;
2192 end = p + keylen;
2193
2194 /*
2195 * This function parses the PrivatKeyInfo object (PKCS#8)
2196 *
2197 * PrivateKeyInfo ::= SEQUENCE {
2198 * version Version,
2199 * algorithm AlgorithmIdentifier,
2200 * PrivateKey BIT STRING
2201 * }
2202 *
2203 * AlgorithmIdentifier ::= SEQUENCE {
2204 * algorithm OBJECT IDENTIFIER,
2205 * parameters ANY DEFINED BY algorithm OPTIONAL
2206 * }
2207 *
2208 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2209 */
2210 if( ( ret = asn1_get_tag( &p, end, &len,
2211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2212 {
2213 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2214 }
2215
2216 end = p + len;
2217
2218 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2219 {
2220 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2221 }
2222
2223 if( rsa->ver != 0 )
2224 {
2225 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2226 }
2227
2228 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2229 {
2230 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2231 }
2232
2233 /*
2234 * only RSA keys handled at this time
2235 */
2236 if( pk_alg_oid.len != 9 ||
2237 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) != 0 )
2238 {
2239 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2240 }
2241
2242 /*
2243 * Get the OCTET STRING and parse the PKCS#1 format inside
2244 */
2245 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2246 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2247
2248 if( ( end - p ) < 1 )
2249 {
2250 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2251 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2252 }
2253
2254 end = p + len;
2255
2256 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2257 return( ret );
2258
2259 return( 0 );
2260}
2261
2262/*
Paul Bakkerda7fdbd2013-06-19 11:15:43 +02002263 * Parse an encrypted PKCS#8 encoded private RSA key
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002264 */
2265static int x509parse_key_pkcs8_encrypted_der(
2266 rsa_context *rsa,
2267 const unsigned char *key,
2268 size_t keylen,
2269 const unsigned char *pwd,
2270 size_t pwdlen )
2271{
2272 int ret;
2273 size_t len;
2274 unsigned char *p, *end, *end2;
2275 x509_buf pbe_alg_oid, pbe_params;
2276 unsigned char buf[2048];
2277
2278 memset(buf, 0, 2048);
2279
2280 p = (unsigned char *) key;
2281 end = p + keylen;
2282
Paul Bakker1fd43212013-06-17 15:14:42 +02002283 if( pwdlen == 0 )
2284 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
2285
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002286 /*
2287 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
2288 *
2289 * EncryptedPrivateKeyInfo ::= SEQUENCE {
2290 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
2291 * encryptedData EncryptedData
2292 * }
2293 *
2294 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
2295 *
2296 * EncryptedData ::= OCTET STRING
2297 *
2298 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
2299 */
2300 if( ( ret = asn1_get_tag( &p, end, &len,
2301 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2302 {
2303 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2304 }
2305
2306 end = p + len;
2307
2308 if( ( ret = asn1_get_tag( &p, end, &len,
2309 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2310 {
2311 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2312 }
2313
2314 end2 = p + len;
2315
2316 if( ( ret = asn1_get_tag( &p, end, &pbe_alg_oid.len, ASN1_OID ) ) != 0 )
2317 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2318
2319 pbe_alg_oid.p = p;
2320 p += pbe_alg_oid.len;
2321
2322 /*
2323 * Store the algorithm parameters
2324 */
2325 pbe_params.p = p;
2326 pbe_params.len = end2 - p;
2327 p += pbe_params.len;
2328
2329 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2330 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2331
2332 // buf has been sized to 2048 bytes
2333 if( len > 2048 )
2334 return( POLARSSL_ERR_X509_INVALID_INPUT );
2335
2336 /*
2337 * Decrypt EncryptedData with appropriate PDE
2338 */
Paul Bakker14a222c2013-06-18 16:35:48 +02002339#if defined(POLARSSL_PKCS12_C)
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002340 if( OID_CMP( OID_PKCS12_PBE_SHA1_DES3_EDE_CBC, &pbe_alg_oid ) )
2341 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002342 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2343 POLARSSL_CIPHER_DES_EDE3_CBC, POLARSSL_MD_SHA1,
2344 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002345 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002346 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2347 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2348
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002349 return( ret );
2350 }
2351 }
2352 else if( OID_CMP( OID_PKCS12_PBE_SHA1_DES2_EDE_CBC, &pbe_alg_oid ) )
2353 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002354 if( ( ret = pkcs12_pbe( &pbe_params, PKCS12_PBE_DECRYPT,
2355 POLARSSL_CIPHER_DES_EDE_CBC, POLARSSL_MD_SHA1,
2356 pwd, pwdlen, p, len, buf ) ) != 0 )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002357 {
Paul Bakker14a222c2013-06-18 16:35:48 +02002358 if( ret == POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH )
2359 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2360
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002361 return( ret );
2362 }
2363 }
2364 else if( OID_CMP( OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) )
2365 {
2366 if( ( ret = pkcs12_pbe_sha1_rc4_128( &pbe_params,
2367 PKCS12_PBE_DECRYPT,
2368 pwd, pwdlen,
2369 p, len, buf ) ) != 0 )
2370 {
2371 return( ret );
2372 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002373
2374 // Best guess for password mismatch when using RC4. If first tag is
2375 // not ASN1_CONSTRUCTED | ASN1_SEQUENCE
2376 //
2377 if( *buf != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
2378 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002379 }
Paul Bakker14a222c2013-06-18 16:35:48 +02002380 else
2381#endif /* POLARSSL_PKCS12_C */
Paul Bakker1fd43212013-06-17 15:14:42 +02002382#if defined(POLARSSL_PKCS5_C)
Paul Bakker14a222c2013-06-18 16:35:48 +02002383 if( OID_CMP( OID_PKCS5_PBES2, &pbe_alg_oid ) )
Paul Bakker1fd43212013-06-17 15:14:42 +02002384 {
2385 if( ( ret = pkcs5_pbes2( &pbe_params, PKCS5_DECRYPT, pwd, pwdlen,
2386 p, len, buf ) ) != 0 )
2387 {
2388 if( ret == POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH )
2389 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2390
2391 return( ret );
2392 }
2393 }
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002394 else
Paul Bakker14a222c2013-06-18 16:35:48 +02002395#endif /* POLARSSL_PKCS5_C */
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002396 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
2397
2398 return x509parse_key_pkcs8_unencrypted_der( rsa, buf, len );
2399}
2400
2401/*
Paul Bakker65a19092013-06-06 21:14:58 +02002402 * Parse a private RSA key
2403 */
2404int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2405 const unsigned char *pwd, size_t pwdlen )
2406{
2407 int ret;
2408
Paul Bakker96743fc2011-02-12 14:30:57 +00002409#if defined(POLARSSL_PEM_C)
Paul Bakker65a19092013-06-06 21:14:58 +02002410 size_t len;
2411 pem_context pem;
2412
2413 pem_init( &pem );
2414 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002415 (char *) "-----BEGIN RSA PRIVATE KEY-----",
2416 (char *) "-----END RSA PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002417 key, pwd, pwdlen, &len );
2418 if( ret == 0 )
2419 {
2420 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2421 {
2422 rsa_free( rsa );
2423 }
2424
2425 pem_free( &pem );
2426 return( ret );
2427 }
Paul Bakkerb495d3a2013-06-17 15:58:04 +02002428 else if( ret == POLARSSL_ERR_PEM_PASSWORD_MISMATCH )
2429 return( POLARSSL_ERR_X509_PASSWORD_MISMATCH );
2430 else if( ret == POLARSSL_ERR_PEM_PASSWORD_REQUIRED )
2431 return( POLARSSL_ERR_X509_PASSWORD_REQUIRED );
Paul Bakker65a19092013-06-06 21:14:58 +02002432 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002433 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002434
2435 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002436 (char *) "-----BEGIN PRIVATE KEY-----",
2437 (char *) "-----END PRIVATE KEY-----",
Paul Bakker65a19092013-06-06 21:14:58 +02002438 key, NULL, 0, &len );
2439 if( ret == 0 )
2440 {
2441 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2442 pem.buf, pem.buflen ) ) != 0 )
2443 {
2444 rsa_free( rsa );
2445 }
2446
2447 pem_free( &pem );
2448 return( ret );
2449 }
2450 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker65a19092013-06-06 21:14:58 +02002451 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002452
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002453 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002454 (char *) "-----BEGIN ENCRYPTED PRIVATE KEY-----",
2455 (char *) "-----END ENCRYPTED PRIVATE KEY-----",
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002456 key, NULL, 0, &len );
2457 if( ret == 0 )
2458 {
2459 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa,
2460 pem.buf, pem.buflen,
2461 pwd, pwdlen ) ) != 0 )
2462 {
2463 rsa_free( rsa );
2464 }
2465
2466 pem_free( &pem );
2467 return( ret );
2468 }
2469 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002470 return( ret );
Paul Bakker65a19092013-06-06 21:14:58 +02002471#else
2472 ((void) pwd);
2473 ((void) pwdlen);
2474#endif /* POLARSSL_PEM_C */
2475
2476 // At this point we only know it's not a PEM formatted key. Could be any
2477 // of the known DER encoded private key formats
2478 //
2479 // We try the different DER format parsers to see if one passes without
2480 // error
2481 //
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002482 if( ( ret = x509parse_key_pkcs8_encrypted_der( rsa, key, keylen,
2483 pwd, pwdlen ) ) == 0 )
Paul Bakker65a19092013-06-06 21:14:58 +02002484 {
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002485 return( 0 );
Paul Bakker65a19092013-06-06 21:14:58 +02002486 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002488 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002489
2490 if( ret == POLARSSL_ERR_X509_PASSWORD_MISMATCH )
2491 {
2492 return( ret );
2493 }
2494
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002495 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) == 0 )
2496 return( 0 );
2497
2498 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002499
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002500 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) == 0 )
2501 return( 0 );
2502
2503 rsa_free( rsa );
Paul Bakker1fd43212013-06-17 15:14:42 +02002504
Paul Bakkercf6e95d2013-06-12 13:18:15 +02002505 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506}
2507
2508/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002509 * Parse a public RSA key
2510 */
Paul Bakker23986e52011-04-24 08:57:21 +00002511int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002512{
Paul Bakker23986e52011-04-24 08:57:21 +00002513 int ret;
2514 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002515 unsigned char *p, *end;
2516 x509_buf alg_oid;
2517#if defined(POLARSSL_PEM_C)
2518 pem_context pem;
2519
2520 pem_init( &pem );
2521 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002522 (char *) "-----BEGIN PUBLIC KEY-----",
2523 (char *) "-----END PUBLIC KEY-----",
Paul Bakker53019ae2011-03-25 13:58:48 +00002524 key, NULL, 0, &len );
2525
2526 if( ret == 0 )
2527 {
2528 /*
2529 * Was PEM encoded
2530 */
2531 keylen = pem.buflen;
2532 }
Paul Bakker9255e832013-06-06 14:58:28 +02002533 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002534 {
2535 pem_free( &pem );
2536 return( ret );
2537 }
2538
2539 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2540#else
2541 p = (unsigned char *) key;
2542#endif
2543 end = p + keylen;
2544
2545 /*
2546 * PublicKeyInfo ::= SEQUENCE {
2547 * algorithm AlgorithmIdentifier,
2548 * PublicKey BIT STRING
2549 * }
2550 *
2551 * AlgorithmIdentifier ::= SEQUENCE {
2552 * algorithm OBJECT IDENTIFIER,
2553 * parameters ANY DEFINED BY algorithm OPTIONAL
2554 * }
2555 *
2556 * RSAPublicKey ::= SEQUENCE {
2557 * modulus INTEGER, -- n
2558 * publicExponent INTEGER -- e
2559 * }
2560 */
2561
2562 if( ( ret = asn1_get_tag( &p, end, &len,
2563 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2564 {
2565#if defined(POLARSSL_PEM_C)
2566 pem_free( &pem );
2567#endif
2568 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002569 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002570 }
2571
2572 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2573 {
2574#if defined(POLARSSL_PEM_C)
2575 pem_free( &pem );
2576#endif
2577 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002578 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002579 }
2580
2581 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2582 {
2583#if defined(POLARSSL_PEM_C)
2584 pem_free( &pem );
2585#endif
2586 rsa_free( rsa );
2587 return( ret );
2588 }
2589
2590 rsa->len = mpi_size( &rsa->N );
2591
2592#if defined(POLARSSL_PEM_C)
2593 pem_free( &pem );
2594#endif
2595
2596 return( 0 );
2597}
2598
Paul Bakkereaa89f82011-04-04 21:36:15 +00002599#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002600/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002601 * Parse DHM parameters
2602 */
Paul Bakker23986e52011-04-24 08:57:21 +00002603int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002604{
Paul Bakker23986e52011-04-24 08:57:21 +00002605 int ret;
2606 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002607 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002608#if defined(POLARSSL_PEM_C)
2609 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002610
Paul Bakker96743fc2011-02-12 14:30:57 +00002611 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002612
Paul Bakker96743fc2011-02-12 14:30:57 +00002613 ret = pem_read_buffer( &pem,
Paul Bakker1d073c52014-07-08 20:15:51 +02002614 (char *) "-----BEGIN DH PARAMETERS-----",
2615 (char *) "-----END DH PARAMETERS-----",
Paul Bakker96743fc2011-02-12 14:30:57 +00002616 dhmin, NULL, 0, &dhminlen );
2617
2618 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002619 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002620 /*
2621 * Was PEM encoded
2622 */
2623 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002624 }
Paul Bakker9255e832013-06-06 14:58:28 +02002625 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002626 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002627 pem_free( &pem );
2628 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002629 }
2630
Paul Bakker96743fc2011-02-12 14:30:57 +00002631 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2632#else
2633 p = (unsigned char *) dhmin;
2634#endif
2635 end = p + dhminlen;
2636
Paul Bakker1b57b062011-01-06 15:48:19 +00002637 memset( dhm, 0, sizeof( dhm_context ) );
2638
Paul Bakker1b57b062011-01-06 15:48:19 +00002639 /*
2640 * DHParams ::= SEQUENCE {
2641 * prime INTEGER, -- P
2642 * generator INTEGER, -- g
2643 * }
2644 */
2645 if( ( ret = asn1_get_tag( &p, end, &len,
2646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2647 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002648#if defined(POLARSSL_PEM_C)
2649 pem_free( &pem );
2650#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002651 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002652 }
2653
2654 end = p + len;
2655
2656 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2657 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2658 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002659#if defined(POLARSSL_PEM_C)
2660 pem_free( &pem );
2661#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002662 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002663 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002664 }
2665
2666 if( p != end )
2667 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002668#if defined(POLARSSL_PEM_C)
2669 pem_free( &pem );
2670#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002671 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002672 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002673 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2674 }
2675
Paul Bakker96743fc2011-02-12 14:30:57 +00002676#if defined(POLARSSL_PEM_C)
2677 pem_free( &pem );
2678#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002679
2680 return( 0 );
2681}
2682
Paul Bakker335db3f2011-04-25 15:28:35 +00002683#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002684/*
2685 * Load and parse a private RSA key
2686 */
2687int x509parse_dhmfile( dhm_context *dhm, const char *path )
2688{
2689 int ret;
2690 size_t n;
2691 unsigned char *buf;
2692
Paul Bakker69e095c2011-12-10 21:55:01 +00002693 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2694 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002695
Paul Bakker27fdf462011-06-09 13:55:13 +00002696 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002697
2698 memset( buf, 0, n + 1 );
2699 free( buf );
2700
2701 return( ret );
2702}
Paul Bakker335db3f2011-04-25 15:28:35 +00002703#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002704#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002705
Paul Bakker5121ce52009-01-03 21:22:43 +00002706#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002707#include <stdarg.h>
2708
2709#if !defined vsnprintf
2710#define vsnprintf _vsnprintf
2711#endif // vsnprintf
2712
2713/*
2714 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2715 * Result value is not size of buffer needed, but -1 if no fit is possible.
2716 *
2717 * This fuction tries to 'fix' this by at least suggesting enlarging the
2718 * size by 20.
2719 */
2720int compat_snprintf(char *str, size_t size, const char *format, ...)
2721{
2722 va_list ap;
2723 int res = -1;
2724
2725 va_start( ap, format );
2726
2727 res = vsnprintf( str, size, format, ap );
2728
2729 va_end( ap );
2730
2731 // No quick fix possible
2732 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002733 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002734
2735 return res;
2736}
2737
2738#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002739#endif
2740
Paul Bakkerd98030e2009-05-02 15:13:40 +00002741#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2742
2743#define SAFE_SNPRINTF() \
2744{ \
2745 if( ret == -1 ) \
2746 return( -1 ); \
2747 \
Paul Bakker23986e52011-04-24 08:57:21 +00002748 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002749 p[n - 1] = '\0'; \
2750 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2751 } \
2752 \
Paul Bakker23986e52011-04-24 08:57:21 +00002753 n -= (unsigned int) ret; \
2754 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002755}
2756
Paul Bakker5121ce52009-01-03 21:22:43 +00002757/*
2758 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002759 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002761int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002762{
Paul Bakker23986e52011-04-24 08:57:21 +00002763 int ret;
2764 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002766 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 char s[128], *p;
2768
2769 memset( s, 0, sizeof( s ) );
2770
2771 name = dn;
2772 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002773 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002774
2775 while( name != NULL )
2776 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002777 if( !name->oid.p )
2778 {
2779 name = name->next;
2780 continue;
2781 }
2782
Paul Bakker74111d32011-01-15 16:57:55 +00002783 if( name != dn )
2784 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002785 ret = snprintf( p, n, ", " );
2786 SAFE_SNPRINTF();
2787 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Paul Bakker535e97d2012-08-23 10:49:55 +00002789 if( name->oid.len == 3 &&
2790 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 {
2792 switch( name->oid.p[2] )
2793 {
2794 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002795 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002796
2797 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002798 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002799
2800 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002801 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
2803 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002804 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
2806 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002807 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
2809 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002810 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002813 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 name->oid.p[2] );
2815 break;
2816 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002817 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002818 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002819 else if( name->oid.len == 9 &&
2820 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 {
2822 switch( name->oid.p[8] )
2823 {
2824 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002825 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002826
2827 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002828 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 name->oid.p[8] );
2830 break;
2831 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002832 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002833 }
2834 else
Paul Bakker74111d32011-01-15 16:57:55 +00002835 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002836 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002837 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
2840 for( i = 0; i < name->val.len; i++ )
2841 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002842 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 break;
2844
2845 c = name->val.p[i];
2846 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2847 s[i] = '?';
2848 else s[i] = c;
2849 }
2850 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002851 ret = snprintf( p, n, "%s", s );
2852 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 name = name->next;
2854 }
2855
Paul Bakker23986e52011-04-24 08:57:21 +00002856 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857}
2858
2859/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002860 * Store the serial in printable form into buf; no more
2861 * than size characters will be written
2862 */
2863int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2864{
Paul Bakker23986e52011-04-24 08:57:21 +00002865 int ret;
2866 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002867 char *p;
2868
2869 p = buf;
2870 n = size;
2871
2872 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002873 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002874
2875 for( i = 0; i < nr; i++ )
2876 {
Paul Bakker93048802011-12-05 14:38:06 +00002877 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002878 continue;
2879
Paul Bakkerdd476992011-01-16 21:34:59 +00002880 ret = snprintf( p, n, "%02X%s",
2881 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2882 SAFE_SNPRINTF();
2883 }
2884
Paul Bakker03c7c252011-11-25 12:37:37 +00002885 if( nr != serial->len )
2886 {
2887 ret = snprintf( p, n, "...." );
2888 SAFE_SNPRINTF();
2889 }
2890
Paul Bakker23986e52011-04-24 08:57:21 +00002891 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002892}
2893
2894/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002895 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002897int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2898 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002899{
Paul Bakker23986e52011-04-24 08:57:21 +00002900 int ret;
2901 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002902 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903
2904 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002905 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakkerd98030e2009-05-02 15:13:40 +00002907 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002909 SAFE_SNPRINTF();
2910 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002912 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakkerdd476992011-01-16 21:34:59 +00002914 ret = x509parse_serial_gets( p, n, &crt->serial);
2915 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakkerd98030e2009-05-02 15:13:40 +00002917 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2918 SAFE_SNPRINTF();
2919 ret = x509parse_dn_gets( p, n, &crt->issuer );
2920 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakkerd98030e2009-05-02 15:13:40 +00002922 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2923 SAFE_SNPRINTF();
2924 ret = x509parse_dn_gets( p, n, &crt->subject );
2925 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakkerd98030e2009-05-02 15:13:40 +00002927 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2929 crt->valid_from.year, crt->valid_from.mon,
2930 crt->valid_from.day, crt->valid_from.hour,
2931 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002932 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakkerd98030e2009-05-02 15:13:40 +00002934 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2936 crt->valid_to.year, crt->valid_to.mon,
2937 crt->valid_to.day, crt->valid_to.hour,
2938 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002939 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakkerd98030e2009-05-02 15:13:40 +00002941 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2942 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker27d66162010-03-17 06:56:01 +00002944 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002946 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2947 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2948 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2949 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2950 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2951 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2952 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2953 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2954 default: ret = snprintf( p, n, "???" ); break;
2955 }
2956 SAFE_SNPRINTF();
2957
2958 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002959 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002960 SAFE_SNPRINTF();
2961
Paul Bakker23986e52011-04-24 08:57:21 +00002962 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002963}
2964
Paul Bakker74111d32011-01-15 16:57:55 +00002965/*
2966 * Return an informational string describing the given OID
2967 */
2968const char *x509_oid_get_description( x509_buf *oid )
2969{
2970 if ( oid == NULL )
2971 return ( NULL );
2972
2973 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2974 return( STRING_SERVER_AUTH );
2975
2976 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2977 return( STRING_CLIENT_AUTH );
2978
2979 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2980 return( STRING_CODE_SIGNING );
2981
2982 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2983 return( STRING_EMAIL_PROTECTION );
2984
2985 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2986 return( STRING_TIME_STAMPING );
2987
2988 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2989 return( STRING_OCSP_SIGNING );
2990
2991 return( NULL );
2992}
2993
2994/* Return the x.y.z.... style numeric string for the given OID */
2995int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2996{
Paul Bakker23986e52011-04-24 08:57:21 +00002997 int ret;
2998 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002999 unsigned int value;
3000 char *p;
3001
3002 p = buf;
3003 n = size;
3004
3005 /* First byte contains first two dots */
3006 if( oid->len > 0 )
3007 {
3008 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
3009 SAFE_SNPRINTF();
3010 }
3011
3012 /* TODO: value can overflow in value. */
3013 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00003014 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00003015 {
3016 value <<= 7;
3017 value += oid->p[i] & 0x7F;
3018
3019 if( !( oid->p[i] & 0x80 ) )
3020 {
3021 /* Last byte */
3022 ret = snprintf( p, n, ".%d", value );
3023 SAFE_SNPRINTF();
3024 value = 0;
3025 }
3026 }
3027
Paul Bakker23986e52011-04-24 08:57:21 +00003028 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00003029}
3030
Paul Bakkerd98030e2009-05-02 15:13:40 +00003031/*
3032 * Return an informational string about the CRL.
3033 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003034int x509parse_crl_info( char *buf, size_t size, const char *prefix,
3035 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003036{
Paul Bakker23986e52011-04-24 08:57:21 +00003037 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003038 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003039 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00003040 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00003041
3042 p = buf;
3043 n = size;
3044
3045 ret = snprintf( p, n, "%sCRL version : %d",
3046 prefix, crl->version );
3047 SAFE_SNPRINTF();
3048
3049 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
3050 SAFE_SNPRINTF();
3051 ret = x509parse_dn_gets( p, n, &crl->issuer );
3052 SAFE_SNPRINTF();
3053
3054 ret = snprintf( p, n, "\n%sthis update : " \
3055 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3056 crl->this_update.year, crl->this_update.mon,
3057 crl->this_update.day, crl->this_update.hour,
3058 crl->this_update.min, crl->this_update.sec );
3059 SAFE_SNPRINTF();
3060
3061 ret = snprintf( p, n, "\n%snext update : " \
3062 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
3063 crl->next_update.year, crl->next_update.mon,
3064 crl->next_update.day, crl->next_update.hour,
3065 crl->next_update.min, crl->next_update.sec );
3066 SAFE_SNPRINTF();
3067
3068 entry = &crl->entry;
3069
3070 ret = snprintf( p, n, "\n%sRevoked certificates:",
3071 prefix );
3072 SAFE_SNPRINTF();
3073
Paul Bakker9be19372009-07-27 20:21:53 +00003074 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003075 {
3076 ret = snprintf( p, n, "\n%sserial number: ",
3077 prefix );
3078 SAFE_SNPRINTF();
3079
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003080 ret = x509parse_serial_gets( p, n, &entry->serial);
3081 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082
Paul Bakkerd98030e2009-05-02 15:13:40 +00003083 ret = snprintf( p, n, " revocation date: " \
3084 "%04d-%02d-%02d %02d:%02d:%02d",
3085 entry->revocation_date.year, entry->revocation_date.mon,
3086 entry->revocation_date.day, entry->revocation_date.hour,
3087 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00003088 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00003089
3090 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 }
3092
Paul Bakkerd98030e2009-05-02 15:13:40 +00003093 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
3094 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Paul Bakker27d66162010-03-17 06:56:01 +00003096 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00003097 {
3098 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
3099 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
3100 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
3101 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
3102 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
3103 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
3104 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
3105 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
3106 default: ret = snprintf( p, n, "???" ); break;
3107 }
3108 SAFE_SNPRINTF();
3109
Paul Bakker1e27bb22009-07-19 20:25:25 +00003110 ret = snprintf( p, n, "\n" );
3111 SAFE_SNPRINTF();
3112
Paul Bakker23986e52011-04-24 08:57:21 +00003113 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114}
3115
3116/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00003117 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 */
Paul Bakker0d844dd2014-07-07 17:44:14 +02003119static void x509_get_current_time( x509_time *now )
Paul Bakker5121ce52009-01-03 21:22:43 +00003120{
Paul Bakkercce9d772011-11-18 14:26:47 +00003121#if defined(_WIN32)
3122 SYSTEMTIME st;
3123
Paul Bakkerf48de952014-07-08 14:39:41 +02003124 GetSystemTime(&st);
Paul Bakkercce9d772011-11-18 14:26:47 +00003125
Paul Bakker0d844dd2014-07-07 17:44:14 +02003126 now->year = st.wYear;
3127 now->mon = st.wMonth;
3128 now->day = st.wDay;
3129 now->hour = st.wHour;
3130 now->min = st.wMinute;
3131 now->sec = st.wSecond;
Paul Bakkercce9d772011-11-18 14:26:47 +00003132#else
Paul Bakker358a8412014-07-08 12:14:37 +02003133 struct tm lt;
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 time_t tt;
3135
3136 tt = time( NULL );
Paul Bakkerf48de952014-07-08 14:39:41 +02003137 gmtime_r( &tt, &lt );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Paul Bakker358a8412014-07-08 12:14:37 +02003139 now->year = lt.tm_year + 1900;
3140 now->mon = lt.tm_mon + 1;
3141 now->day = lt.tm_mday;
3142 now->hour = lt.tm_hour;
3143 now->min = lt.tm_min;
3144 now->sec = lt.tm_sec;
Paul Bakkercce9d772011-11-18 14:26:47 +00003145#endif
Paul Bakker0d844dd2014-07-07 17:44:14 +02003146}
Paul Bakkercce9d772011-11-18 14:26:47 +00003147
Paul Bakker0d844dd2014-07-07 17:44:14 +02003148/*
3149 * Return 0 if before <= after, 1 otherwise
3150 */
3151static int x509_check_time( const x509_time *before, const x509_time *after )
3152{
3153 if( before->year > after->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003154 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Paul Bakker0d844dd2014-07-07 17:44:14 +02003156 if( before->year == after->year &&
3157 before->mon > after->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003158 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker0d844dd2014-07-07 17:44:14 +02003160 if( before->year == after->year &&
3161 before->mon == after->mon &&
3162 before->day > after->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003163 return( 1 );
3164
Paul Bakker0d844dd2014-07-07 17:44:14 +02003165 if( before->year == after->year &&
3166 before->mon == after->mon &&
3167 before->day == after->day &&
3168 before->hour > after->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00003169 return( 1 );
3170
Paul Bakker0d844dd2014-07-07 17:44:14 +02003171 if( before->year == after->year &&
3172 before->mon == after->mon &&
3173 before->day == after->day &&
3174 before->hour == after->hour &&
3175 before->min > after->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00003176 return( 1 );
3177
Paul Bakker0d844dd2014-07-07 17:44:14 +02003178 if( before->year == after->year &&
3179 before->mon == after->mon &&
3180 before->day == after->day &&
3181 before->hour == after->hour &&
3182 before->min == after->min &&
3183 before->sec > after->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00003184 return( 1 );
3185
Paul Bakker40ea7de2009-05-03 10:18:48 +00003186 return( 0 );
3187}
3188
Paul Bakker0d844dd2014-07-07 17:44:14 +02003189int x509parse_time_expired( const x509_time *to )
3190{
3191 x509_time now;
3192
3193 x509_get_current_time( &now );
3194
3195 return( x509_check_time( &now, to ) );
3196}
3197
3198int x509parse_time_future( const x509_time *from )
3199{
3200 x509_time now;
3201
3202 x509_get_current_time( &now );
3203
3204 return( x509_check_time( from, &now ) );
3205}
3206
Paul Bakker40ea7de2009-05-03 10:18:48 +00003207/*
3208 * Return 1 if the certificate is revoked, or 0 otherwise.
3209 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00003210int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003211{
Paul Bakkerff60ee62010-03-16 21:09:09 +00003212 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003213
3214 while( cur != NULL && cur->serial.len != 0 )
3215 {
Paul Bakkera056efc2011-01-16 21:38:35 +00003216 if( crt->serial.len == cur->serial.len &&
3217 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00003218 {
3219 if( x509parse_time_expired( &cur->revocation_date ) )
3220 return( 1 );
3221 }
3222
3223 cur = cur->next;
3224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
3226 return( 0 );
3227}
3228
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003229/*
3230 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003231 */
Paul Bakker23986e52011-04-24 08:57:21 +00003232static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 unsigned char *out )
3234{
3235 switch( alg )
3236 {
Paul Bakker40e46942009-01-03 21:51:57 +00003237#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003238 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003239#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003240#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003241 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003242#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003243#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003244 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003245#endif
3246#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003247 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003248#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003249#if defined(POLARSSL_SHA2_C)
3250 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3251 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3252#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003253#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003254 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3255 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003258 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 break;
3260 }
3261}
3262
3263/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003264 * Check that the given certificate is valid accoring to the CRL.
3265 */
3266static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3267 x509_crl *crl_list)
3268{
3269 int flags = 0;
3270 int hash_id;
3271 unsigned char hash[64];
3272
Paul Bakker915275b2012-09-28 07:10:55 +00003273 if( ca == NULL )
3274 return( flags );
3275
Paul Bakker76fd75a2011-01-16 21:12:10 +00003276 /*
3277 * TODO: What happens if no CRL is present?
3278 * Suggestion: Revocation state should be unknown if no CRL is present.
3279 * For backwards compatibility this is not yet implemented.
3280 */
3281
Paul Bakker915275b2012-09-28 07:10:55 +00003282 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003283 {
Paul Bakker915275b2012-09-28 07:10:55 +00003284 if( crl_list->version == 0 ||
3285 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003286 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3287 crl_list->issuer_raw.len ) != 0 )
3288 {
3289 crl_list = crl_list->next;
3290 continue;
3291 }
3292
3293 /*
3294 * Check if CRL is correctly signed by the trusted CA
3295 */
3296 hash_id = crl_list->sig_alg;
3297
3298 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3299
Paul Bakker43f97992013-09-23 11:23:31 +02003300 if( !rsa_pkcs1_verify( &ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker76fd75a2011-01-16 21:12:10 +00003301 0, hash, crl_list->sig.p ) == 0 )
3302 {
3303 /*
3304 * CRL is not trusted
3305 */
3306 flags |= BADCRL_NOT_TRUSTED;
3307 break;
3308 }
3309
3310 /*
3311 * Check for validity of CRL (Do not drop out)
3312 */
3313 if( x509parse_time_expired( &crl_list->next_update ) )
3314 flags |= BADCRL_EXPIRED;
3315
Paul Bakker50a5c532014-07-08 10:59:10 +02003316 if( x509parse_time_future( &crl_list->this_update ) )
3317 flags |= BADCRL_FUTURE;
3318
Paul Bakker76fd75a2011-01-16 21:12:10 +00003319 /*
3320 * Check if certificate is revoked
3321 */
3322 if( x509parse_revoked(crt, crl_list) )
3323 {
3324 flags |= BADCERT_REVOKED;
3325 break;
3326 }
3327
3328 crl_list = crl_list->next;
3329 }
3330 return flags;
3331}
3332
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003333// Equal == 0, inequal == 1
3334static int x509_name_cmp( const void *s1, const void *s2, size_t len )
3335{
3336 size_t i;
3337 unsigned char diff;
3338 const unsigned char *n1 = s1, *n2 = s2;
3339
3340 for( i = 0; i < len; i++ )
3341 {
3342 diff = n1[i] ^ n2[i];
3343
Paul Bakkerc941adb2014-07-07 14:17:24 +02003344 if( diff == 0 )
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003345 continue;
3346
Paul Bakkerc941adb2014-07-07 14:17:24 +02003347 if( diff == 32 &&
3348 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
3349 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
3350 {
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003351 continue;
Paul Bakkerc941adb2014-07-07 14:17:24 +02003352 }
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003353
3354 return( 1 );
3355 }
3356
3357 return( 0 );
3358}
3359
Paul Bakker1d073c52014-07-08 20:15:51 +02003360static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003361{
3362 size_t i;
3363 size_t cn_idx = 0;
3364
Paul Bakker57b12982012-02-11 17:38:38 +00003365 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003366 return( 0 );
3367
3368 for( i = 0; i < strlen( cn ); ++i )
3369 {
3370 if( cn[i] == '.' )
3371 {
3372 cn_idx = i;
3373 break;
3374 }
3375 }
3376
3377 if( cn_idx == 0 )
3378 return( 0 );
3379
Paul Bakker535e97d2012-08-23 10:49:55 +00003380 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003381 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003382 {
3383 return( 1 );
3384 }
3385
3386 return( 0 );
3387}
3388
Paul Bakker915275b2012-09-28 07:10:55 +00003389static int x509parse_verify_top(
3390 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003391 x509_crl *ca_crl, int path_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
Paul Bakker915275b2012-09-28 07:10:55 +00003432 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003433 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003434 {
3435 trust_ca = trust_ca->next;
3436 continue;
3437 }
3438
3439 hash_id = child->sig_alg;
3440
3441 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3442
Paul Bakker43f97992013-09-23 11:23:31 +02003443 if( rsa_pkcs1_verify( &trust_ca->rsa, NULL, NULL, RSA_PUBLIC, hash_id,
Paul Bakker915275b2012-09-28 07:10:55 +00003444 0, hash, child->sig.p ) != 0 )
3445 {
3446 trust_ca = trust_ca->next;
3447 continue;
3448 }
3449
3450 /*
3451 * Top of chain is signed by a trusted CA
3452 */
3453 *flags &= ~BADCERT_NOT_TRUSTED;
3454 break;
3455 }
3456
Paul Bakker9a736322012-11-14 12:39:52 +00003457 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003458 * If top of chain is not the same as the trusted CA send a verify request
3459 * to the callback for any issues with validity and CRL presence for the
3460 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003461 */
3462 if( trust_ca != NULL &&
3463 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3464 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3465 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003466 {
3467 /* Check trusted CA's CRL for then chain's top crt */
3468 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3469
3470 if( x509parse_time_expired( &trust_ca->valid_to ) )
3471 ca_flags |= BADCERT_EXPIRED;
3472
Paul Bakker50a5c532014-07-08 10:59:10 +02003473 if( x509parse_time_future( &trust_ca->valid_from ) )
3474 ca_flags |= BADCERT_FUTURE;
3475
Paul Bakker915275b2012-09-28 07:10:55 +00003476 if( NULL != f_vrfy )
3477 {
Paul Bakker9a736322012-11-14 12:39:52 +00003478 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003479 return( ret );
3480 }
3481 }
3482
3483 /* Call callback on top cert */
3484 if( NULL != f_vrfy )
3485 {
Paul Bakker9a736322012-11-14 12:39:52 +00003486 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003487 return( ret );
3488 }
3489
Paul Bakker915275b2012-09-28 07:10:55 +00003490 *flags |= ca_flags;
3491
3492 return( 0 );
3493}
3494
3495static int x509parse_verify_child(
3496 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003497 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003498 int (*f_vrfy)(void *, x509_cert *, int, int *),
3499 void *p_vrfy )
3500{
3501 int hash_id, ret;
3502 int parent_flags = 0;
3503 unsigned char hash[64];
3504 x509_cert *grandparent;
3505
Manuel Pégourié-Gonnard4cdb3ba2014-11-20 17:12:15 +01003506 /* path_cnt is 0 for the first intermediate CA */
3507 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
3508 {
3509 *flags |= BADCERT_NOT_TRUSTED;
3510 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
3511 }
3512
Paul Bakker915275b2012-09-28 07:10:55 +00003513 if( x509parse_time_expired( &child->valid_to ) )
3514 *flags |= BADCERT_EXPIRED;
3515
Paul Bakker50a5c532014-07-08 10:59:10 +02003516 if( x509parse_time_future( &child->valid_from ) )
3517 *flags |= BADCERT_FUTURE;
3518
Paul Bakker915275b2012-09-28 07:10:55 +00003519 hash_id = child->sig_alg;
3520
3521 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3522
Paul Bakker43f97992013-09-23 11:23:31 +02003523 if( rsa_pkcs1_verify( &parent->rsa, NULL, NULL, RSA_PUBLIC, hash_id, 0,
3524 hash, child->sig.p ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003525 *flags |= BADCERT_NOT_TRUSTED;
3526
3527 /* Check trusted CA's CRL for the given crt */
3528 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3529
3530 grandparent = parent->next;
3531
3532 while( grandparent != NULL )
3533 {
3534 if( grandparent->version == 0 ||
3535 grandparent->ca_istrue == 0 ||
3536 parent->issuer_raw.len != grandparent->subject_raw.len ||
3537 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3538 parent->issuer_raw.len ) != 0 )
3539 {
3540 grandparent = grandparent->next;
3541 continue;
3542 }
3543 break;
3544 }
3545
Paul Bakker915275b2012-09-28 07:10:55 +00003546 if( grandparent != NULL )
3547 {
3548 /*
3549 * Part of the chain
3550 */
Paul Bakker9a736322012-11-14 12:39:52 +00003551 ret = x509parse_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003552 if( ret != 0 )
3553 return( ret );
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003554 }
Paul Bakker915275b2012-09-28 07:10:55 +00003555 else
3556 {
Paul Bakker9a736322012-11-14 12:39:52 +00003557 ret = x509parse_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003558 if( ret != 0 )
3559 return( ret );
3560 }
3561
3562 /* child is verified to be a child of the parent, call verify callback */
3563 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003564 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003565 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003566
3567 *flags |= parent_flags;
3568
3569 return( 0 );
3570}
3571
Paul Bakker76fd75a2011-01-16 21:12:10 +00003572/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 * Verify the certificate validity
3574 */
3575int x509parse_verify( x509_cert *crt,
3576 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003577 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003578 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003579 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003580 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003581{
Paul Bakker23986e52011-04-24 08:57:21 +00003582 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003583 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003584 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003585 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003587 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003588
Paul Bakker40ea7de2009-05-03 10:18:48 +00003589 *flags = 0;
3590
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 if( cn != NULL )
3592 {
3593 name = &crt->subject;
3594 cn_len = strlen( cn );
3595
Paul Bakker4d2c1242012-05-10 14:12:46 +00003596 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003598 cur = &crt->subject_alt_names;
3599
3600 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003601 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003602 if( cur->buf.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003603 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003604 break;
3605
Paul Bakker535e97d2012-08-23 10:49:55 +00003606 if( cur->buf.len > 2 &&
3607 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003608 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003609 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Paul Bakker4d2c1242012-05-10 14:12:46 +00003611 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003612 }
3613
3614 if( cur == NULL )
3615 *flags |= BADCERT_CN_MISMATCH;
3616 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003617 else
3618 {
3619 while( name != NULL )
3620 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003621 if( name->oid.len == 3 &&
3622 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003623 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003624 if( name->val.len == cn_len &&
Paul Bakkerf65fbee2013-09-11 11:52:17 +02003625 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003626 break;
3627
Paul Bakker535e97d2012-08-23 10:49:55 +00003628 if( name->val.len > 2 &&
3629 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003630 x509_wildcard_verify( cn, &name->val ) )
3631 break;
3632 }
3633
3634 name = name->next;
3635 }
3636
3637 if( name == NULL )
3638 *flags |= BADCERT_CN_MISMATCH;
3639 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 }
3641
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003643 * Iterate upwards in the given cert chain, to find our crt parent.
3644 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003646 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Paul Bakker76fd75a2011-01-16 21:12:10 +00003648 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003650 if( parent->ca_istrue == 0 ||
3651 crt->issuer_raw.len != parent->subject_raw.len ||
3652 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 crt->issuer_raw.len ) != 0 )
3654 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003655 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003656 continue;
3657 }
Paul Bakker915275b2012-09-28 07:10:55 +00003658 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 }
3660
Paul Bakker915275b2012-09-28 07:10:55 +00003661 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 {
Paul Bakker915275b2012-09-28 07:10:55 +00003663 /*
3664 * Part of the chain
3665 */
Paul Bakker9a736322012-11-14 12:39:52 +00003666 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003667 if( ret != 0 )
3668 return( ret );
3669 }
3670 else
Paul Bakker74111d32011-01-15 16:57:55 +00003671 {
Paul Bakker9a736322012-11-14 12:39:52 +00003672 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003673 if( ret != 0 )
3674 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003675 }
Paul Bakker915275b2012-09-28 07:10:55 +00003676
3677 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003678 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003679
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 return( 0 );
3681}
3682
3683/*
3684 * Unallocate all certificate data
3685 */
3686void x509_free( x509_cert *crt )
3687{
3688 x509_cert *cert_cur = crt;
3689 x509_cert *cert_prv;
3690 x509_name *name_cur;
3691 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003692 x509_sequence *seq_cur;
3693 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003694
3695 if( crt == NULL )
3696 return;
3697
3698 do
3699 {
3700 rsa_free( &cert_cur->rsa );
3701
3702 name_cur = cert_cur->issuer.next;
3703 while( name_cur != NULL )
3704 {
3705 name_prv = name_cur;
3706 name_cur = name_cur->next;
3707 memset( name_prv, 0, sizeof( x509_name ) );
3708 free( name_prv );
3709 }
3710
3711 name_cur = cert_cur->subject.next;
3712 while( name_cur != NULL )
3713 {
3714 name_prv = name_cur;
3715 name_cur = name_cur->next;
3716 memset( name_prv, 0, sizeof( x509_name ) );
3717 free( name_prv );
3718 }
3719
Paul Bakker74111d32011-01-15 16:57:55 +00003720 seq_cur = cert_cur->ext_key_usage.next;
3721 while( seq_cur != NULL )
3722 {
3723 seq_prv = seq_cur;
3724 seq_cur = seq_cur->next;
3725 memset( seq_prv, 0, sizeof( x509_sequence ) );
3726 free( seq_prv );
3727 }
3728
Paul Bakker8afa70d2012-02-11 18:42:45 +00003729 seq_cur = cert_cur->subject_alt_names.next;
3730 while( seq_cur != NULL )
3731 {
3732 seq_prv = seq_cur;
3733 seq_cur = seq_cur->next;
3734 memset( seq_prv, 0, sizeof( x509_sequence ) );
3735 free( seq_prv );
3736 }
3737
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 if( cert_cur->raw.p != NULL )
3739 {
3740 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3741 free( cert_cur->raw.p );
3742 }
3743
3744 cert_cur = cert_cur->next;
3745 }
3746 while( cert_cur != NULL );
3747
3748 cert_cur = crt;
3749 do
3750 {
3751 cert_prv = cert_cur;
3752 cert_cur = cert_cur->next;
3753
3754 memset( cert_prv, 0, sizeof( x509_cert ) );
3755 if( cert_prv != crt )
3756 free( cert_prv );
3757 }
3758 while( cert_cur != NULL );
3759}
3760
Paul Bakkerd98030e2009-05-02 15:13:40 +00003761/*
3762 * Unallocate all CRL data
3763 */
3764void x509_crl_free( x509_crl *crl )
3765{
3766 x509_crl *crl_cur = crl;
3767 x509_crl *crl_prv;
3768 x509_name *name_cur;
3769 x509_name *name_prv;
3770 x509_crl_entry *entry_cur;
3771 x509_crl_entry *entry_prv;
3772
3773 if( crl == NULL )
3774 return;
3775
3776 do
3777 {
3778 name_cur = crl_cur->issuer.next;
3779 while( name_cur != NULL )
3780 {
3781 name_prv = name_cur;
3782 name_cur = name_cur->next;
3783 memset( name_prv, 0, sizeof( x509_name ) );
3784 free( name_prv );
3785 }
3786
3787 entry_cur = crl_cur->entry.next;
3788 while( entry_cur != NULL )
3789 {
3790 entry_prv = entry_cur;
3791 entry_cur = entry_cur->next;
3792 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3793 free( entry_prv );
3794 }
3795
3796 if( crl_cur->raw.p != NULL )
3797 {
3798 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3799 free( crl_cur->raw.p );
3800 }
3801
3802 crl_cur = crl_cur->next;
3803 }
3804 while( crl_cur != NULL );
3805
3806 crl_cur = crl;
3807 do
3808 {
3809 crl_prv = crl_cur;
3810 crl_cur = crl_cur->next;
3811
3812 memset( crl_prv, 0, sizeof( x509_crl ) );
3813 if( crl_prv != crl )
3814 free( crl_prv );
3815 }
3816 while( crl_cur != NULL );
3817}
3818
Paul Bakker40e46942009-01-03 21:51:57 +00003819#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003820
Paul Bakker40e46942009-01-03 21:51:57 +00003821#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
3823/*
3824 * Checkup routine
3825 */
3826int x509_self_test( int verbose )
3827{
Paul Bakker5690efc2011-05-26 13:16:06 +00003828#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003829 int ret;
3830 int flags;
3831 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 x509_cert cacert;
3833 x509_cert clicert;
3834 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003835#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003836 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
3839 if( verbose != 0 )
3840 printf( " X.509 certificate load: " );
3841
3842 memset( &clicert, 0, sizeof( x509_cert ) );
3843
Paul Bakkereae09db2013-06-06 12:35:54 +02003844 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003845 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 if( ret != 0 )
3847 {
3848 if( verbose != 0 )
3849 printf( "failed\n" );
3850
3851 return( ret );
3852 }
3853
3854 memset( &cacert, 0, sizeof( x509_cert ) );
3855
Paul Bakkereae09db2013-06-06 12:35:54 +02003856 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003857 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 if( ret != 0 )
3859 {
3860 if( verbose != 0 )
3861 printf( "failed\n" );
3862
3863 return( ret );
3864 }
3865
3866 if( verbose != 0 )
3867 printf( "passed\n X.509 private key load: " );
3868
3869 i = strlen( test_ca_key );
3870 j = strlen( test_ca_pwd );
3871
Paul Bakker66b78b22011-03-25 14:22:50 +00003872 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3873
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003875 (const unsigned char *) test_ca_key, i,
3876 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 {
3878 if( verbose != 0 )
3879 printf( "failed\n" );
3880
3881 return( ret );
3882 }
3883
3884 if( verbose != 0 )
3885 printf( "passed\n X.509 signature verify: ");
3886
Paul Bakker23986e52011-04-24 08:57:21 +00003887 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888 if( ret != 0 )
3889 {
3890 if( verbose != 0 )
3891 printf( "failed\n" );
3892
3893 return( ret );
3894 }
3895
Paul Bakker5690efc2011-05-26 13:16:06 +00003896#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003898 printf( "passed\n X.509 DHM parameter load: " );
3899
3900 i = strlen( test_dhm_params );
3901 j = strlen( test_ca_pwd );
3902
Paul Bakkereae09db2013-06-06 12:35:54 +02003903 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003904 {
3905 if( verbose != 0 )
3906 printf( "failed\n" );
3907
3908 return( ret );
3909 }
3910
3911 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
3915 x509_free( &cacert );
3916 x509_free( &clicert );
3917 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003918#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003919 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003921
3922 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003923#else
3924 ((void) verbose);
3925 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3926#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003927}
3928
3929#endif
3930
3931#endif