blob: 5d32167f3e5c7139cdf56cb8b53d7f850e10c0cf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
Paul Bakkerad8d3542012-02-16 15:28:14 +000026 * The ITU-T X.509 standard defines a certificate format for PKI.
Paul Bakker5121ce52009-01-03 21:22:43 +000027 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 * http://www.ietf.org/rfc/rfc3279.txt
Paul Bakkerad8d3542012-02-16 15:28:14 +000029 * http://www.ietf.org/rfc/rfc3280.txt
Paul Bakker5121ce52009-01-03 21:22:43 +000030 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020043#include "polarssl/oid.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000044#include "polarssl/pem.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000045#include "polarssl/dhm.h"
Paul Bakker28144de2013-06-24 19:28:55 +020046#if defined(POLARSSL_PKCS5_C)
47#include "polarssl/pkcs5.h"
48#endif
Paul Bakker38b50d72013-06-24 19:33:27 +020049#if defined(POLARSSL_PKCS12_C)
50#include "polarssl/pkcs12.h"
51#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker6e339b52013-07-03 13:37:05 +020053#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060#include <string.h>
61#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000062#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000063#include <windows.h>
64#else
Paul Bakker5121ce52009-01-03 21:22:43 +000065#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker335db3f2011-04-25 15:28:35 +000068#if defined(POLARSSL_FS_IO)
69#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000070#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000071#include <sys/types.h>
Paul Bakker2c8cdd22013-06-24 19:22:42 +020072#include <sys/stat.h>
Paul Bakker8d914582012-06-04 12:46:42 +000073#include <dirent.h>
74#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000075#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +0200135 * Version ::= INTEGER { v1(0) }
136 */
137static int x509_csr_get_version( unsigned char **p,
138 const unsigned char *end,
139 int *ver )
140{
141 int ret;
142
143 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
144 {
145 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
146 {
147 *ver = 0;
148 return( 0 );
149 }
150
151 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
152 }
153
154 return( 0 );
155}
156
157/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * CertificateSerialNumber ::= INTEGER
159 */
160static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 x509_buf *serial )
163{
164 int ret;
165
166 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000167 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000168 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
171 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000172 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000173 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175 serial->tag = *(*p)++;
176
177 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000178 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 serial->p = *p;
181 *p += serial->len;
182
183 return( 0 );
184}
185
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200186/* Get an algorithm identifier without parameters (eg for signatures)
187 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 * AlgorithmIdentifier ::= SEQUENCE {
189 * algorithm OBJECT IDENTIFIER,
190 * parameters ANY DEFINED BY algorithm OPTIONAL }
191 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200192static int x509_get_alg_null( unsigned char **p, const unsigned char *end,
193 x509_buf *alg )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194{
Paul Bakker23986e52011-04-24 08:57:21 +0000195 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +0200197 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000198 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 return( 0 );
201}
202
203/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 * AttributeTypeAndValue ::= SEQUENCE {
205 * type AttributeType,
206 * value AttributeValue }
207 *
208 * AttributeType ::= OBJECT IDENTIFIER
209 *
210 * AttributeValue ::= ANY DEFINED BY AttributeType
211 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000212static int x509_get_attr_type_value( unsigned char **p,
213 const unsigned char *end,
214 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215{
Paul Bakker23986e52011-04-24 08:57:21 +0000216 int ret;
217 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 x509_buf *oid;
219 x509_buf *val;
220
221 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000223 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Manuel Pégourié-Gonnard686bfae2013-08-15 13:40:10 +0200225 if( ( end - *p ) < 1 )
226 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
227 POLARSSL_ERR_ASN1_OUT_OF_DATA );
228
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 oid = &cur->oid;
230 oid->tag = **p;
231
232 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000233 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 oid->p = *p;
236 *p += oid->len;
237
238 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000239 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000240 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
242 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
243 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
244 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000245 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000246 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 val = &cur->val;
249 val->tag = *(*p)++;
250
251 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000252 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254 val->p = *p;
255 *p += val->len;
256
257 cur->next = NULL;
258
Paul Bakker400ff6f2011-02-20 10:40:16 +0000259 return( 0 );
260}
261
262/*
263 * RelativeDistinguishedName ::=
264 * SET OF AttributeTypeAndValue
265 *
266 * AttributeTypeAndValue ::= SEQUENCE {
267 * type AttributeType,
268 * value AttributeValue }
269 *
270 * AttributeType ::= OBJECT IDENTIFIER
271 *
272 * AttributeValue ::= ANY DEFINED BY AttributeType
273 */
274static int x509_get_name( unsigned char **p,
275 const unsigned char *end,
276 x509_name *cur )
277{
Paul Bakker23986e52011-04-24 08:57:21 +0000278 int ret;
279 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000280 const unsigned char *end2;
281 x509_name *use;
282
283 if( ( ret = asn1_get_tag( p, end, &len,
284 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000285 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000286
287 end2 = end;
288 end = *p + len;
289 use = cur;
290
291 do
292 {
293 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
294 return( ret );
295
296 if( *p != end )
297 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200298 use->next = (x509_name *) polarssl_malloc(
Paul Bakker400ff6f2011-02-20 10:40:16 +0000299 sizeof( x509_name ) );
300
301 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000302 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000303
304 memset( use->next, 0, sizeof( x509_name ) );
305
306 use = use->next;
307 }
308 }
309 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 /*
312 * recurse until end of SEQUENCE is reached
313 */
314 if( *p == end2 )
315 return( 0 );
316
Paul Bakker6e339b52013-07-03 13:37:05 +0200317 cur->next = (x509_name *) polarssl_malloc(
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 sizeof( x509_name ) );
319
320 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000321 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Paul Bakker430ffbe2012-05-01 08:14:20 +0000323 memset( cur->next, 0, sizeof( x509_name ) );
324
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 return( x509_get_name( p, end2, cur->next ) );
326}
327
328/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 * Time ::= CHOICE {
330 * utcTime UTCTime,
331 * generalTime GeneralizedTime }
332 */
Paul Bakker91200182010-02-18 21:26:15 +0000333static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000334 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000335 x509_time *time )
336{
Paul Bakker23986e52011-04-24 08:57:21 +0000337 int ret;
338 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000339 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000340 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341
Paul Bakker91200182010-02-18 21:26:15 +0000342 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000343 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
344 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000345
Paul Bakker91200182010-02-18 21:26:15 +0000346 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker91200182010-02-18 21:26:15 +0000348 if ( tag == ASN1_UTC_TIME )
349 {
350 (*p)++;
351 ret = asn1_get_len( p, end, &len );
352
353 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000354 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000355
Paul Bakker91200182010-02-18 21:26:15 +0000356 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000357 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
358 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000359
Paul Bakker91200182010-02-18 21:26:15 +0000360 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
361 &time->year, &time->mon, &time->day,
362 &time->hour, &time->min, &time->sec ) < 5 )
363 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000364
Paul Bakker400ff6f2011-02-20 10:40:16 +0000365 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000366 time->year += 1900;
367
368 *p += len;
369
370 return( 0 );
371 }
372 else if ( tag == ASN1_GENERALIZED_TIME )
373 {
374 (*p)++;
375 ret = asn1_get_len( p, end, &len );
376
377 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000378 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000379
380 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000381 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
382 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000383
384 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
385 &time->year, &time->mon, &time->day,
386 &time->hour, &time->min, &time->sec ) < 5 )
387 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
388
389 *p += len;
390
391 return( 0 );
392 }
393 else
Paul Bakker9d781402011-05-09 16:17:09 +0000394 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000395}
396
397
398/*
399 * Validity ::= SEQUENCE {
400 * notBefore Time,
401 * notAfter Time }
402 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000403static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 x509_time *from,
406 x509_time *to )
407{
Paul Bakker23986e52011-04-24 08:57:21 +0000408 int ret;
409 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411 if( ( ret = asn1_get_tag( p, end, &len,
412 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000413 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 end = *p + len;
416
Paul Bakker91200182010-02-18 21:26:15 +0000417 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Paul Bakker91200182010-02-18 21:26:15 +0000420 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000421 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000424 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000425 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427 return( 0 );
428}
429
Paul Bakker5121ce52009-01-03 21:22:43 +0000430static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000431 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 x509_buf *sig )
433{
Paul Bakker23986e52011-04-24 08:57:21 +0000434 int ret;
435 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Paul Bakker8afa70d2012-02-11 18:42:45 +0000437 if( ( end - *p ) < 1 )
438 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
439 POLARSSL_ERR_ASN1_OUT_OF_DATA );
440
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 sig->tag = **p;
442
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +0200443 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000444 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 sig->len = len;
447 sig->p = *p;
448
449 *p += len;
450
451 return( 0 );
452}
453
454/*
455 * X.509 v2/v3 unique identifier (not parsed)
456 */
457static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000458 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 x509_buf *uid, int n )
460{
461 int ret;
462
463 if( *p == end )
464 return( 0 );
465
466 uid->tag = **p;
467
468 if( ( ret = asn1_get_tag( p, end, &uid->len,
469 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
470 {
Paul Bakker40e46942009-01-03 21:51:57 +0000471 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 return( 0 );
473
474 return( ret );
475 }
476
477 uid->p = *p;
478 *p += uid->len;
479
480 return( 0 );
481}
482
483/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000484 * X.509 Extensions (No parsing of extensions, pointer should
485 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 */
487static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000488 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000489 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000490{
Paul Bakker23986e52011-04-24 08:57:21 +0000491 int ret;
492 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( *p == end )
495 return( 0 );
496
497 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000498
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000500 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503 ext->p = *p;
504 end = *p + ext->len;
505
506 /*
507 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
508 *
509 * Extension ::= SEQUENCE {
510 * extnID OBJECT IDENTIFIER,
511 * critical BOOLEAN DEFAULT FALSE,
512 * extnValue OCTET STRING }
513 */
514 if( ( ret = asn1_get_tag( p, end, &len,
515 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000516 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
518 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000519 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000520 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Paul Bakkerd98030e2009-05-02 15:13:40 +0000522 return( 0 );
523}
524
525/*
526 * X.509 CRL v2 extensions (no extensions parsed yet.)
527 */
528static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000529 const unsigned char *end,
530 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000531{
Paul Bakker23986e52011-04-24 08:57:21 +0000532 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000533 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000534
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000535 /* Get explicit tag */
536 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000537 {
538 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
539 return( 0 );
540
541 return( ret );
542 }
543
544 while( *p < end )
545 {
546 if( ( ret = asn1_get_tag( p, end, &len,
547 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000548 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549
550 *p += len;
551 }
552
553 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000554 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000555 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
556
557 return( 0 );
558}
559
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000560/*
561 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
562 */
563static int x509_get_crl_entry_ext( unsigned char **p,
564 const unsigned char *end,
565 x509_buf *ext )
566{
567 int ret;
568 size_t len = 0;
569
570 /* OPTIONAL */
571 if (end <= *p)
572 return( 0 );
573
574 ext->tag = **p;
575 ext->p = *p;
576
577 /*
578 * Get CRL-entry extension sequence header
579 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
580 */
581 if( ( ret = asn1_get_tag( p, end, &ext->len,
582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
583 {
584 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
585 {
586 ext->p = NULL;
587 return( 0 );
588 }
589 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
590 }
591
592 end = *p + ext->len;
593
594 if( end != *p + ext->len )
595 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
596 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
597
598 while( *p < end )
599 {
600 if( ( ret = asn1_get_tag( p, end, &len,
601 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
602 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
603
604 *p += len;
605 }
606
607 if( *p != end )
608 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
609 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
610
611 return( 0 );
612}
613
Paul Bakker74111d32011-01-15 16:57:55 +0000614static int x509_get_basic_constraints( unsigned char **p,
615 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000616 int *ca_istrue,
617 int *max_pathlen )
618{
Paul Bakker23986e52011-04-24 08:57:21 +0000619 int ret;
620 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000621
622 /*
623 * BasicConstraints ::= SEQUENCE {
624 * cA BOOLEAN DEFAULT FALSE,
625 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
626 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000627 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000628 *max_pathlen = 0; /* endless */
629
630 if( ( ret = asn1_get_tag( p, end, &len,
631 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000632 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000633
634 if( *p == end )
635 return 0;
636
Paul Bakker3cccddb2011-01-16 21:46:31 +0000637 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000638 {
639 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000640 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000641
642 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000643 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000644
Paul Bakker3cccddb2011-01-16 21:46:31 +0000645 if( *ca_istrue != 0 )
646 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000647 }
648
649 if( *p == end )
650 return 0;
651
652 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000653 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000654
655 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000656 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000657 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
658
659 (*max_pathlen)++;
660
Paul Bakker74111d32011-01-15 16:57:55 +0000661 return 0;
662}
663
664static int x509_get_ns_cert_type( unsigned char **p,
665 const unsigned char *end,
666 unsigned char *ns_cert_type)
667{
668 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000669 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000670
671 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000672 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000673
674 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000676 POLARSSL_ERR_ASN1_INVALID_LENGTH );
677
678 /* Get actual bitstring */
679 *ns_cert_type = *bs.p;
680 return 0;
681}
682
683static int x509_get_key_usage( unsigned char **p,
684 const unsigned char *end,
685 unsigned char *key_usage)
686{
687 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000688 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000689
690 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000691 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000692
Paul Bakker94a67962012-08-23 13:03:52 +0000693 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000694 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000695 POLARSSL_ERR_ASN1_INVALID_LENGTH );
696
697 /* Get actual bitstring */
698 *key_usage = *bs.p;
699 return 0;
700}
701
Paul Bakkerd98030e2009-05-02 15:13:40 +0000702/*
Paul Bakker74111d32011-01-15 16:57:55 +0000703 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
704 *
705 * KeyPurposeId ::= OBJECT IDENTIFIER
706 */
707static int x509_get_ext_key_usage( unsigned char **p,
708 const unsigned char *end,
709 x509_sequence *ext_key_usage)
710{
711 int ret;
712
713 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000714 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000715
716 /* Sequence length must be >= 1 */
717 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000719 POLARSSL_ERR_ASN1_INVALID_LENGTH );
720
721 return 0;
722}
723
724/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000725 * SubjectAltName ::= GeneralNames
726 *
727 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
728 *
729 * GeneralName ::= CHOICE {
730 * otherName [0] OtherName,
731 * rfc822Name [1] IA5String,
732 * dNSName [2] IA5String,
733 * x400Address [3] ORAddress,
734 * directoryName [4] Name,
735 * ediPartyName [5] EDIPartyName,
736 * uniformResourceIdentifier [6] IA5String,
737 * iPAddress [7] OCTET STRING,
738 * registeredID [8] OBJECT IDENTIFIER }
739 *
740 * OtherName ::= SEQUENCE {
741 * type-id OBJECT IDENTIFIER,
742 * value [0] EXPLICIT ANY DEFINED BY type-id }
743 *
744 * EDIPartyName ::= SEQUENCE {
745 * nameAssigner [0] DirectoryString OPTIONAL,
746 * partyName [1] DirectoryString }
747 *
748 * NOTE: PolarSSL only parses and uses dNSName at this point.
749 */
750static int x509_get_subject_alt_name( unsigned char **p,
751 const unsigned char *end,
752 x509_sequence *subject_alt_name )
753{
754 int ret;
755 size_t len, tag_len;
756 asn1_buf *buf;
757 unsigned char tag;
758 asn1_sequence *cur = subject_alt_name;
759
760 /* Get main sequence tag */
761 if( ( ret = asn1_get_tag( p, end, &len,
762 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
763 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
764
765 if( *p + len != end )
766 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
767 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
768
769 while( *p < end )
770 {
771 if( ( end - *p ) < 1 )
772 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
773 POLARSSL_ERR_ASN1_OUT_OF_DATA );
774
775 tag = **p;
776 (*p)++;
777 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
778 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
779
780 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
782 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
783
784 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
785 {
786 *p += tag_len;
787 continue;
788 }
789
790 buf = &(cur->buf);
791 buf->tag = tag;
792 buf->p = *p;
793 buf->len = tag_len;
794 *p += buf->len;
795
796 /* Allocate and assign next pointer */
797 if (*p < end)
798 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200799 cur->next = (asn1_sequence *) polarssl_malloc(
Paul Bakkera8cd2392012-02-11 16:09:32 +0000800 sizeof( asn1_sequence ) );
801
802 if( cur->next == NULL )
803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
804 POLARSSL_ERR_ASN1_MALLOC_FAILED );
805
Paul Bakker535e97d2012-08-23 10:49:55 +0000806 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000807 cur = cur->next;
808 }
809 }
810
811 /* Set final sequence entry's next pointer to NULL */
812 cur->next = NULL;
813
814 if( *p != end )
815 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
816 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
817
818 return( 0 );
819}
820
821/*
Paul Bakker74111d32011-01-15 16:57:55 +0000822 * X.509 v3 extensions
823 *
824 * TODO: Perform all of the basic constraints tests required by the RFC
825 * TODO: Set values for undetected extensions to a sane default?
826 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000827 */
828static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000829 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000830 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000831{
Paul Bakker23986e52011-04-24 08:57:21 +0000832 int ret;
833 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000834 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000835
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000836 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000837 {
838 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
839 return( 0 );
840
841 return( ret );
842 }
843
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 while( *p < end )
845 {
Paul Bakker74111d32011-01-15 16:57:55 +0000846 /*
847 * Extension ::= SEQUENCE {
848 * extnID OBJECT IDENTIFIER,
849 * critical BOOLEAN DEFAULT FALSE,
850 * extnValue OCTET STRING }
851 */
852 x509_buf extn_oid = {0, 0, NULL};
853 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200854 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000855
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 if( ( ret = asn1_get_tag( p, end, &len,
857 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000858 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000860 end_ext_data = *p + len;
861
Paul Bakker74111d32011-01-15 16:57:55 +0000862 /* Get extension ID */
863 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Paul Bakker74111d32011-01-15 16:57:55 +0000865 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000866 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000867
Paul Bakker74111d32011-01-15 16:57:55 +0000868 extn_oid.p = *p;
869 *p += extn_oid.len;
870
871 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000872 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000873 POLARSSL_ERR_ASN1_OUT_OF_DATA );
874
875 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000876 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000877 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000878 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Paul Bakker74111d32011-01-15 16:57:55 +0000880 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000881 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000883 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000884
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000885 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000886
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000887 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000888 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000889 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Paul Bakker74111d32011-01-15 16:57:55 +0000891 /*
892 * Detect supported extensions
893 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200894 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
895
896 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000897 {
898 /* No parser found, skip extension */
899 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Paul Bakker5c721f92011-07-27 16:51:09 +0000901#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000902 if( is_critical )
903 {
904 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000905 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000906 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
907 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000908#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200909 continue;
910 }
911
912 crt->ext_types |= ext_type;
913
914 switch( ext_type )
915 {
916 case EXT_BASIC_CONSTRAINTS:
917 /* Parse basic constraints */
918 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
919 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
920 return ( ret );
921 break;
922
923 case EXT_KEY_USAGE:
924 /* Parse key usage */
925 if( ( ret = x509_get_key_usage( p, end_ext_octet,
926 &crt->key_usage ) ) != 0 )
927 return ( ret );
928 break;
929
930 case EXT_EXTENDED_KEY_USAGE:
931 /* Parse extended key usage */
932 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
933 &crt->ext_key_usage ) ) != 0 )
934 return ( ret );
935 break;
936
937 case EXT_SUBJECT_ALT_NAME:
938 /* Parse subject alt name */
939 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
940 &crt->subject_alt_names ) ) != 0 )
941 return ( ret );
942 break;
943
944 case EXT_NS_CERT_TYPE:
945 /* Parse netscape certificate type */
946 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
947 &crt->ns_cert_type ) ) != 0 )
948 return ( ret );
949 break;
950
951 default:
952 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +0000953 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 }
955
956 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000957 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000958 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 return( 0 );
961}
962
963/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000964 * X.509 CRL Entries
965 */
966static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000967 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000968 x509_crl_entry *entry )
969{
Paul Bakker23986e52011-04-24 08:57:21 +0000970 int ret;
971 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000972 x509_crl_entry *cur_entry = entry;
973
974 if( *p == end )
975 return( 0 );
976
Paul Bakker9be19372009-07-27 20:21:53 +0000977 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000978 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
979 {
980 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
981 return( 0 );
982
983 return( ret );
984 }
985
Paul Bakker9be19372009-07-27 20:21:53 +0000986 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000987
988 while( *p < end )
989 {
Paul Bakker23986e52011-04-24 08:57:21 +0000990 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000991 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000992
993 if( ( ret = asn1_get_tag( p, end, &len2,
994 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
995 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000996 return( ret );
997 }
998
Paul Bakker9be19372009-07-27 20:21:53 +0000999 cur_entry->raw.tag = **p;
1000 cur_entry->raw.p = *p;
1001 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001002 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001003
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001004 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001005 return( ret );
1006
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001007 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001008 return( ret );
1009
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001010 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001011 return( ret );
1012
Paul Bakker74111d32011-01-15 16:57:55 +00001013 if ( *p < end )
1014 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001015 cur_entry->next = polarssl_malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001016
1017 if( cur_entry->next == NULL )
1018 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1019
Paul Bakkerd98030e2009-05-02 15:13:40 +00001020 cur_entry = cur_entry->next;
1021 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1022 }
1023 }
1024
1025 return( 0 );
1026}
1027
Paul Bakkerc70b9822013-04-07 22:00:46 +02001028static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1029 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001030{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001031 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001032
Paul Bakkerc70b9822013-04-07 22:00:46 +02001033 if( ret != 0 )
1034 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001035
Paul Bakkerc70b9822013-04-07 22:00:46 +02001036 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001037}
1038
Paul Bakkerd98030e2009-05-02 15:13:40 +00001039/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001040 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001041 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001042static int x509parse_crt_der_core( x509_cert *crt, const unsigned char *buf,
1043 size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001044{
Paul Bakker23986e52011-04-24 08:57:21 +00001045 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001046 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001047 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001048
Paul Bakker320a4b52009-03-28 18:52:39 +00001049 /*
1050 * Check for valid input
1051 */
1052 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001053 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001054
Paul Bakker6e339b52013-07-03 13:37:05 +02001055 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakker96743fc2011-02-12 14:30:57 +00001056
1057 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001058 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001059
1060 memcpy( p, buf, buflen );
1061
1062 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001063
1064 crt->raw.p = p;
1065 crt->raw.len = len;
1066 end = p + len;
1067
1068 /*
1069 * Certificate ::= SEQUENCE {
1070 * tbsCertificate TBSCertificate,
1071 * signatureAlgorithm AlgorithmIdentifier,
1072 * signatureValue BIT STRING }
1073 */
1074 if( ( ret = asn1_get_tag( &p, end, &len,
1075 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1076 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001077 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001078 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
1080
Paul Bakkerb00ca422012-09-25 12:10:00 +00001081 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001083 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001085 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001087 crt_end = p + len;
Paul Bakker42c65812013-06-24 19:21:59 +02001088
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 /*
1090 * TBSCertificate ::= SEQUENCE {
1091 */
1092 crt->tbs.p = p;
1093
1094 if( ( ret = asn1_get_tag( &p, end, &len,
1095 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1096 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001097 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001098 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 }
1100
1101 end = p + len;
1102 crt->tbs.len = end - crt->tbs.p;
1103
1104 /*
1105 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1106 *
1107 * CertificateSerialNumber ::= INTEGER
1108 *
1109 * signature AlgorithmIdentifier
1110 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001111 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1112 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1113 ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 return( ret );
1117 }
1118
1119 crt->version++;
1120
1121 if( crt->version > 3 )
1122 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001123 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001124 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 }
1126
Paul Bakkerc70b9822013-04-07 22:00:46 +02001127 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1128 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001130 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001131 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 }
1133
1134 /*
1135 * issuer Name
1136 */
1137 crt->issuer_raw.p = p;
1138
1139 if( ( ret = asn1_get_tag( &p, end, &len,
1140 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1141 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001142 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001143 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 }
1145
1146 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1147 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001148 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 return( ret );
1150 }
1151
1152 crt->issuer_raw.len = p - crt->issuer_raw.p;
1153
1154 /*
1155 * Validity ::= SEQUENCE {
1156 * notBefore Time,
1157 * notAfter Time }
1158 *
1159 */
1160 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1161 &crt->valid_to ) ) != 0 )
1162 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001163 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 return( ret );
1165 }
1166
1167 /*
1168 * subject Name
1169 */
1170 crt->subject_raw.p = p;
1171
1172 if( ( ret = asn1_get_tag( &p, end, &len,
1173 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1174 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001175 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001176 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 }
1178
Paul Bakkercefb3962012-06-27 11:51:09 +00001179 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001180 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001181 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 return( ret );
1183 }
1184
1185 crt->subject_raw.len = p - crt->subject_raw.p;
1186
1187 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001188 * SubjectPublicKeyInfo
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 if( ( ret = pk_parse_get_pubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001192 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 return( ret );
1194 }
1195
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 /*
1197 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1198 * -- If present, version shall be v2 or v3
1199 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1200 * -- If present, version shall be v2 or v3
1201 * extensions [3] EXPLICIT Extensions OPTIONAL
1202 * -- If present, version shall be v3
1203 */
1204 if( crt->version == 2 || crt->version == 3 )
1205 {
1206 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1207 if( ret != 0 )
1208 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001209 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 return( ret );
1211 }
1212 }
1213
1214 if( crt->version == 2 || crt->version == 3 )
1215 {
1216 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1217 if( ret != 0 )
1218 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001219 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 return( ret );
1221 }
1222 }
1223
1224 if( crt->version == 3 )
1225 {
Paul Bakker74111d32011-01-15 16:57:55 +00001226 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001227 if( ret != 0 )
1228 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001229 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 return( ret );
1231 }
1232 }
1233
1234 if( p != end )
1235 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001236 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001237 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001238 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 }
1240
Paul Bakkerb00ca422012-09-25 12:10:00 +00001241 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242
1243 /*
Manuel Pégourié-Gonnard20c12f62013-07-09 12:13:24 +02001244 * }
1245 * -- end of TBSCertificate
1246 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001247 * signatureAlgorithm AlgorithmIdentifier,
1248 * signatureValue BIT STRING
1249 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001250 if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
Paul Bakker535e97d2012-08-23 10:49:55 +00001256 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1257 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001259 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001260 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
1263 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 if( p != end )
1270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001272 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001273 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001276 return( 0 );
1277}
1278
1279/*
Paul Bakker42c65812013-06-24 19:21:59 +02001280 * Parse one X.509 certificate in DER format from a buffer and add them to a
1281 * chained list
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001282 */
Paul Bakker42c65812013-06-24 19:21:59 +02001283int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001284{
Paul Bakker42c65812013-06-24 19:21:59 +02001285 int ret;
1286 x509_cert *crt = chain, *prev = NULL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001287
1288 /*
1289 * Check for valid input
1290 */
1291 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001292 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001293
1294 while( crt->version != 0 && crt->next != NULL )
1295 {
1296 prev = crt;
1297 crt = crt->next;
1298 }
1299
1300 /*
1301 * Add new certificate on the end of the chain if needed.
1302 */
1303 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001304 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001305 crt->next = (x509_cert *) polarssl_malloc( sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001306
Paul Bakker7d06ad22009-05-02 15:53:56 +00001307 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001308 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001309
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001310 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001311 crt = crt->next;
1312 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Paul Bakker42c65812013-06-24 19:21:59 +02001315 if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
1316 {
1317 if( prev )
1318 prev->next = NULL;
1319
1320 if( crt != chain )
Paul Bakker6e339b52013-07-03 13:37:05 +02001321 polarssl_free( crt );
Paul Bakker42c65812013-06-24 19:21:59 +02001322
1323 return( ret );
1324 }
1325
1326 return( 0 );
1327}
1328
1329/*
1330 * Parse one or more PEM certificates from a buffer and add them to the chained list
1331 */
1332int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
1333{
Paul Bakkerdce7fdc2013-09-15 17:15:26 +02001334 int success = 0, first_error = 0, total_failed = 0;
Paul Bakker42c65812013-06-24 19:21:59 +02001335 int buf_format = X509_FORMAT_DER;
1336
1337 /*
1338 * Check for valid input
1339 */
1340 if( chain == NULL || buf == NULL )
1341 return( POLARSSL_ERR_X509_INVALID_INPUT );
1342
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001343 /*
1344 * Determine buffer content. Buffer contains either one DER certificate or
1345 * one or more PEM certificates.
1346 */
1347#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001348 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001349 buf_format = X509_FORMAT_PEM;
1350#endif
1351
1352 if( buf_format == X509_FORMAT_DER )
Paul Bakker42c65812013-06-24 19:21:59 +02001353 return x509parse_crt_der( chain, buf, buflen );
1354
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001355#if defined(POLARSSL_PEM_C)
1356 if( buf_format == X509_FORMAT_PEM )
1357 {
Paul Bakkerdce7fdc2013-09-15 17:15:26 +02001358 int ret;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001359 pem_context pem;
1360
1361 while( buflen > 0 )
1362 {
1363 size_t use_len;
1364 pem_init( &pem );
1365
1366 ret = pem_read_buffer( &pem,
1367 "-----BEGIN CERTIFICATE-----",
1368 "-----END CERTIFICATE-----",
1369 buf, NULL, 0, &use_len );
1370
1371 if( ret == 0 )
1372 {
1373 /*
1374 * Was PEM encoded
1375 */
1376 buflen -= use_len;
1377 buf += use_len;
1378 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001379 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1380 {
1381 return( ret );
1382 }
Paul Bakker00b28602013-06-24 13:02:41 +02001383 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001384 {
1385 pem_free( &pem );
1386
Paul Bakker5ed3b342013-06-24 19:05:46 +02001387 /*
1388 * PEM header and footer were found
1389 */
1390 buflen -= use_len;
1391 buf += use_len;
1392
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001393 if( first_error == 0 )
1394 first_error = ret;
1395
1396 continue;
1397 }
1398 else
1399 break;
1400
Paul Bakker42c65812013-06-24 19:21:59 +02001401 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001402
1403 pem_free( &pem );
1404
1405 if( ret != 0 )
1406 {
1407 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001408 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001409 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001410 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001411 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001412
1413 if( first_error == 0 )
1414 first_error = ret;
1415
Paul Bakker42c65812013-06-24 19:21:59 +02001416 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001417 continue;
1418 }
1419
1420 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001421 }
1422 }
1423#endif
1424
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001425 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001426 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001427 else if( first_error )
1428 return( first_error );
1429 else
1430 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431}
1432
1433/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001434 * Parse a CSR
1435 */
1436int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen )
1437{
1438 int ret;
1439 size_t len;
1440 unsigned char *p, *end;
1441#if defined(POLARSSL_PEM_C)
1442 size_t use_len;
1443 pem_context pem;
1444#endif
1445
1446 /*
1447 * Check for valid input
1448 */
1449 if( csr == NULL || buf == NULL )
1450 return( POLARSSL_ERR_X509_INVALID_INPUT );
1451
1452 memset( csr, 0, sizeof( x509_csr ) );
1453
1454#if defined(POLARSSL_PEM_C)
1455 pem_init( &pem );
1456 ret = pem_read_buffer( &pem,
1457 "-----BEGIN CERTIFICATE REQUEST-----",
1458 "-----END CERTIFICATE REQUEST-----",
1459 buf, NULL, 0, &use_len );
1460
1461 if( ret == 0 )
1462 {
1463 /*
1464 * Was PEM encoded
1465 */
1466 buflen -= use_len;
1467 buf += use_len;
1468
1469 /*
1470 * Steal PEM buffer
1471 */
1472 p = pem.buf;
1473 pem.buf = NULL;
1474 len = pem.buflen;
1475 pem_free( &pem );
1476 }
1477 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1478 {
1479 pem_free( &pem );
1480 return( ret );
1481 }
1482 else
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001483#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001484 {
1485 /*
1486 * nope, copy the raw DER data
1487 */
1488 p = (unsigned char *) polarssl_malloc( len = buflen );
1489
1490 if( p == NULL )
1491 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1492
1493 memcpy( p, buf, buflen );
1494
1495 buflen = 0;
1496 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001497
1498 csr->raw.p = p;
1499 csr->raw.len = len;
1500 end = p + len;
1501
1502 /*
1503 * CertificationRequest ::= SEQUENCE {
1504 * certificationRequestInfo CertificationRequestInfo,
1505 * signatureAlgorithm AlgorithmIdentifier,
1506 * signature BIT STRING
1507 * }
1508 */
1509 if( ( ret = asn1_get_tag( &p, end, &len,
1510 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1511 {
1512 x509_csr_free( csr );
1513 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1514 }
1515
1516 if( len != (size_t) ( end - p ) )
1517 {
1518 x509_csr_free( csr );
1519 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1520 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1521 }
1522
1523 /*
1524 * CertificationRequestInfo ::= SEQUENCE {
1525 */
1526 csr->cri.p = p;
1527
1528 if( ( ret = asn1_get_tag( &p, end, &len,
1529 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1530 {
1531 x509_csr_free( csr );
1532 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1533 }
1534
1535 end = p + len;
1536 csr->cri.len = end - csr->cri.p;
1537
1538 /*
1539 * Version ::= INTEGER { v1(0) }
1540 */
1541 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
1542 {
1543 x509_csr_free( csr );
1544 return( ret );
1545 }
1546
1547 csr->version++;
1548
1549 if( csr->version != 1 )
1550 {
1551 x509_csr_free( csr );
1552 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1553 }
1554
1555 /*
1556 * subject Name
1557 */
1558 csr->subject_raw.p = p;
1559
1560 if( ( ret = asn1_get_tag( &p, end, &len,
1561 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1562 {
1563 x509_csr_free( csr );
1564 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1565 }
1566
1567 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
1568 {
1569 x509_csr_free( csr );
1570 return( ret );
1571 }
1572
1573 csr->subject_raw.len = p - csr->subject_raw.p;
1574
1575 /*
1576 * subjectPKInfo SubjectPublicKeyInfo
1577 */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578 if( ( ret = pk_parse_get_pubkey( &p, end, &csr->pk ) ) != 0 )
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001579 {
1580 x509_csr_free( csr );
1581 return( ret );
1582 }
1583
1584 /*
1585 * attributes [0] Attributes
1586 */
1587 if( ( ret = asn1_get_tag( &p, end, &len,
1588 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1589 {
1590 x509_csr_free( csr );
1591 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1592 }
1593 // TODO Parse Attributes / extension requests
1594
1595 p += len;
1596
1597 end = csr->raw.p + csr->raw.len;
1598
1599 /*
1600 * signatureAlgorithm AlgorithmIdentifier,
1601 * signature BIT STRING
1602 */
1603 if( ( ret = x509_get_alg_null( &p, end, &csr->sig_oid ) ) != 0 )
1604 {
1605 x509_csr_free( csr );
1606 return( ret );
1607 }
1608
1609 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &csr->sig_md,
1610 &csr->sig_pk ) ) != 0 )
1611 {
1612 x509_csr_free( csr );
1613 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1614 }
1615
1616 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
1617 {
1618 x509_csr_free( csr );
1619 return( ret );
1620 }
1621
1622 if( p != end )
1623 {
1624 x509_csr_free( csr );
1625 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1626 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1627 }
1628
1629 return( 0 );
1630}
1631
1632/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001633 * Parse one or more CRLs and add them to the chained list
1634 */
Paul Bakker23986e52011-04-24 08:57:21 +00001635int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001636{
Paul Bakker23986e52011-04-24 08:57:21 +00001637 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001638 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001639 unsigned char *p, *end;
1640 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001641#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001642 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001643 pem_context pem;
1644#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645
1646 crl = chain;
1647
1648 /*
1649 * Check for valid input
1650 */
1651 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001652 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653
1654 while( crl->version != 0 && crl->next != NULL )
1655 crl = crl->next;
1656
1657 /*
1658 * Add new CRL on the end of the chain if needed.
1659 */
1660 if ( crl->version != 0 && crl->next == NULL)
1661 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001662 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001663
Paul Bakker7d06ad22009-05-02 15:53:56 +00001664 if( crl->next == NULL )
1665 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001666 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001667 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001668 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001669
Paul Bakker7d06ad22009-05-02 15:53:56 +00001670 crl = crl->next;
1671 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001672 }
1673
Paul Bakker96743fc2011-02-12 14:30:57 +00001674#if defined(POLARSSL_PEM_C)
1675 pem_init( &pem );
1676 ret = pem_read_buffer( &pem,
1677 "-----BEGIN X509 CRL-----",
1678 "-----END X509 CRL-----",
1679 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680
Paul Bakker96743fc2011-02-12 14:30:57 +00001681 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001682 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001683 /*
1684 * Was PEM encoded
1685 */
1686 buflen -= use_len;
1687 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001688
1689 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001690 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001692 p = pem.buf;
1693 pem.buf = NULL;
1694 len = pem.buflen;
1695 pem_free( &pem );
1696 }
Paul Bakker00b28602013-06-24 13:02:41 +02001697 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001698 {
1699 pem_free( &pem );
1700 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001701 }
1702 else
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001703#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704 {
1705 /*
1706 * nope, copy the raw DER data
1707 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001708 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709
1710 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001711 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001712
1713 memcpy( p, buf, buflen );
1714
1715 buflen = 0;
1716 }
1717
1718 crl->raw.p = p;
1719 crl->raw.len = len;
1720 end = p + len;
1721
1722 /*
1723 * CertificateList ::= SEQUENCE {
1724 * tbsCertList TBSCertList,
1725 * signatureAlgorithm AlgorithmIdentifier,
1726 * signatureValue BIT STRING }
1727 */
1728 if( ( ret = asn1_get_tag( &p, end, &len,
1729 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1730 {
1731 x509_crl_free( crl );
1732 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1733 }
1734
Paul Bakker23986e52011-04-24 08:57:21 +00001735 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001736 {
1737 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001738 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001739 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1740 }
1741
1742 /*
1743 * TBSCertList ::= SEQUENCE {
1744 */
1745 crl->tbs.p = p;
1746
1747 if( ( ret = asn1_get_tag( &p, end, &len,
1748 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1749 {
1750 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001751 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 }
1753
1754 end = p + len;
1755 crl->tbs.len = end - crl->tbs.p;
1756
1757 /*
1758 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1759 * -- if present, MUST be v2
1760 *
1761 * signature AlgorithmIdentifier
1762 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001763 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001764 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769
1770 crl->version++;
1771
1772 if( crl->version > 2 )
1773 {
1774 x509_crl_free( crl );
1775 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1776 }
1777
Paul Bakkerc70b9822013-04-07 22:00:46 +02001778 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1779 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001780 {
1781 x509_crl_free( crl );
1782 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1783 }
1784
1785 /*
1786 * issuer Name
1787 */
1788 crl->issuer_raw.p = p;
1789
1790 if( ( ret = asn1_get_tag( &p, end, &len,
1791 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1792 {
1793 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001794 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001795 }
1796
1797 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1798 {
1799 x509_crl_free( crl );
1800 return( ret );
1801 }
1802
1803 crl->issuer_raw.len = p - crl->issuer_raw.p;
1804
1805 /*
1806 * thisUpdate Time
1807 * nextUpdate Time OPTIONAL
1808 */
Paul Bakker91200182010-02-18 21:26:15 +00001809 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810 {
1811 x509_crl_free( crl );
1812 return( ret );
1813 }
1814
Paul Bakker91200182010-02-18 21:26:15 +00001815 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001816 {
Paul Bakker9d781402011-05-09 16:17:09 +00001817 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001818 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001819 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001820 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001821 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001822 x509_crl_free( crl );
1823 return( ret );
1824 }
1825 }
1826
1827 /*
1828 * revokedCertificates SEQUENCE OF SEQUENCE {
1829 * userCertificate CertificateSerialNumber,
1830 * revocationDate Time,
1831 * crlEntryExtensions Extensions OPTIONAL
1832 * -- if present, MUST be v2
1833 * } OPTIONAL
1834 */
1835 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1836 {
1837 x509_crl_free( crl );
1838 return( ret );
1839 }
1840
1841 /*
1842 * crlExtensions EXPLICIT Extensions OPTIONAL
1843 * -- if present, MUST be v2
1844 */
1845 if( crl->version == 2 )
1846 {
1847 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1848
1849 if( ret != 0 )
1850 {
1851 x509_crl_free( crl );
1852 return( ret );
1853 }
1854 }
1855
1856 if( p != end )
1857 {
1858 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001859 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001860 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1861 }
1862
1863 end = crl->raw.p + crl->raw.len;
1864
1865 /*
1866 * signatureAlgorithm AlgorithmIdentifier,
1867 * signatureValue BIT STRING
1868 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001869 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 {
1871 x509_crl_free( crl );
1872 return( ret );
1873 }
1874
Paul Bakker535e97d2012-08-23 10:49:55 +00001875 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1876 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001877 {
1878 x509_crl_free( crl );
1879 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1880 }
1881
1882 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1883 {
1884 x509_crl_free( crl );
1885 return( ret );
1886 }
1887
1888 if( p != end )
1889 {
1890 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001891 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001892 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1893 }
1894
1895 if( buflen > 0 )
1896 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001897 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001898
Paul Bakker7d06ad22009-05-02 15:53:56 +00001899 if( crl->next == NULL )
1900 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001901 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001902 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001903 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001904
Paul Bakker7d06ad22009-05-02 15:53:56 +00001905 crl = crl->next;
1906 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001907
1908 return( x509parse_crl( crl, buf, buflen ) );
1909 }
1910
1911 return( 0 );
1912}
1913
Paul Bakker335db3f2011-04-25 15:28:35 +00001914#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001915/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001916 * Load all data from a file into a given buffer.
1917 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001918static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001919{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001920 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001921 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001922
Paul Bakkerd98030e2009-05-02 15:13:40 +00001923 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001924 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001925
Paul Bakkerd98030e2009-05-02 15:13:40 +00001926 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001927 if( ( size = ftell( f ) ) == -1 )
1928 {
1929 fclose( f );
1930 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1931 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001932 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001933
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001934 *n = (size_t) size;
1935
Paul Bakker694d3ae2013-08-19 14:23:38 +02001936 if( *n + 1 == 0 ||
1937 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001938 {
1939 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001940 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001941 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001942
Paul Bakkerd98030e2009-05-02 15:13:40 +00001943 if( fread( *buf, 1, *n, f ) != *n )
1944 {
1945 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001946 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001947 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001948 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001949
Paul Bakkerd98030e2009-05-02 15:13:40 +00001950 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001951
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001953
Paul Bakkerd98030e2009-05-02 15:13:40 +00001954 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001955}
1956
1957/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 * Load one or more certificates and add them to the chained list
1959 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001960int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001961{
1962 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 size_t n;
1964 unsigned char *buf;
1965
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001966 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001967 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
Paul Bakker69e095c2011-12-10 21:55:01 +00001969 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
1971 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001972 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
1974 return( ret );
1975}
1976
Paul Bakker8d914582012-06-04 12:46:42 +00001977int x509parse_crtpath( x509_cert *chain, const char *path )
1978{
1979 int ret = 0;
1980#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001981 int w_ret;
1982 WCHAR szDir[MAX_PATH];
1983 char filename[MAX_PATH];
1984 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001985 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001986
Paul Bakker97872ac2012-11-02 12:53:26 +00001987 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001988 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001989
1990 if( len > MAX_PATH - 3 )
1991 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001992
Paul Bakker3338b792012-10-01 21:13:10 +00001993 memset( szDir, 0, sizeof(szDir) );
1994 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001995 memcpy( filename, path, len );
1996 filename[len++] = '\\';
1997 p = filename + len;
1998 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001999
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002000 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002001
Paul Bakker97872ac2012-11-02 12:53:26 +00002002 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002003 if (hFind == INVALID_HANDLE_VALUE)
2004 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2005
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002006 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002007 do
2008 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002009 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002010
Paul Bakkere4791f32012-06-04 21:29:15 +00002011 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002012 continue;
2013
Paul Bakker3338b792012-10-01 21:13:10 +00002014 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2015 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002016 p, len - 1,
2017 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002018
Paul Bakker3338b792012-10-01 21:13:10 +00002019 w_ret = x509parse_crtfile( chain, filename );
2020 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002021 ret++;
2022 else
2023 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002024 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002025 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002026
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002027 if (GetLastError() != ERROR_NO_MORE_FILES)
2028 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002029
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002030cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002031 FindClose( hFind );
2032#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002033 int t_ret, i;
2034 struct stat sb;
2035 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002036 char entry_name[255];
2037 DIR *dir = opendir( path );
2038
2039 if( dir == NULL)
2040 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2041
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002042 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002043 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002044 if( result == NULL )
2045 break;
2046
2047 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2048
2049 i = stat( entry_name, &sb );
2050
2051 if( i == -1 )
Paul Bakker003dbad2013-09-09 17:26:14 +02002052 {
2053 closedir( dir );
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002054 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker003dbad2013-09-09 17:26:14 +02002055 }
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002056
2057 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002058 continue;
2059
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002060 // Ignore parse errors
2061 //
Paul Bakker8d914582012-06-04 12:46:42 +00002062 t_ret = x509parse_crtfile( chain, entry_name );
2063 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002064 ret++;
2065 else
2066 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002067 }
2068 closedir( dir );
2069#endif
2070
2071 return( ret );
2072}
2073
Paul Bakkerd98030e2009-05-02 15:13:40 +00002074/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002075 * Load a CSR into the structure
2076 */
2077int x509parse_csrfile( x509_csr *csr, const char *path )
2078{
2079 int ret;
2080 size_t n;
2081 unsigned char *buf;
2082
2083 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2084 return( ret );
2085
2086 ret = x509parse_csr( csr, buf, n );
2087
2088 memset( buf, 0, n + 1 );
2089 polarssl_free( buf );
2090
2091 return( ret );
2092}
2093
2094/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002095 * Load one or more CRLs and add them to the chained list
2096 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002097int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002098{
2099 int ret;
2100 size_t n;
2101 unsigned char *buf;
2102
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002103 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002104 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002105
Paul Bakker27fdf462011-06-09 13:55:13 +00002106 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002107
2108 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002109 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002110
2111 return( ret );
2112}
2113
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002114#if defined(POLARSSL_RSA_C)
2115/*
2116 * Load and parse a private RSA key
2117 */
2118int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2119{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002120 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002121 pk_context pk;
2122
2123 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002124
Paul Bakker1a7550a2013-09-15 13:01:22 +02002125 ret = pk_parse_keyfile( &pk, path, pwd );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002126
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002127 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2128 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2129
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002130 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002131 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002132 else
2133 rsa_free( rsa );
2134
2135 pk_free( &pk );
2136
2137 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002138}
2139
2140/*
2141 * Load and parse a public RSA key
2142 */
2143int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2144{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002145 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002146 pk_context pk;
2147
2148 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002149
Paul Bakker1a7550a2013-09-15 13:01:22 +02002150 ret = pk_parse_public_keyfile( &pk, path );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002151
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002152 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2153 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2154
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002155 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002156 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002157 else
2158 rsa_free( rsa );
2159
2160 pk_free( &pk );
2161
2162 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002163}
2164#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002165#endif /* POLARSSL_FS_IO */
2166
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002167#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002168/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002169 * Parse a private RSA key
2170 */
2171int x509parse_key_rsa( rsa_context *rsa,
2172 const unsigned char *key, size_t keylen,
2173 const unsigned char *pwd, size_t pwdlen )
2174{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002175 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002176 pk_context pk;
2177
2178 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002179
Paul Bakker1a7550a2013-09-15 13:01:22 +02002180 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002181
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002182 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2183 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2184
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002185 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002186 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002187 else
2188 rsa_free( rsa );
2189
2190 pk_free( &pk );
2191
2192 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002193}
2194
2195/*
2196 * Parse a public RSA key
2197 */
2198int x509parse_public_key_rsa( rsa_context *rsa,
2199 const unsigned char *key, size_t keylen )
2200{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002201 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002202 pk_context pk;
2203
2204 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002205
Paul Bakker1a7550a2013-09-15 13:01:22 +02002206 ret = pk_parse_public_key( &pk, key, keylen );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002207
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002208 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2209 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2210
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002211 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002212 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002213 else
2214 rsa_free( rsa );
2215
2216 pk_free( &pk );
2217
2218 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002219}
2220#endif /* POLARSSL_RSA_C */
2221
Paul Bakker5121ce52009-01-03 21:22:43 +00002222#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002223#include <stdarg.h>
2224
2225#if !defined vsnprintf
2226#define vsnprintf _vsnprintf
2227#endif // vsnprintf
2228
2229/*
2230 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2231 * Result value is not size of buffer needed, but -1 if no fit is possible.
2232 *
2233 * This fuction tries to 'fix' this by at least suggesting enlarging the
2234 * size by 20.
2235 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002236static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002237{
2238 va_list ap;
2239 int res = -1;
2240
2241 va_start( ap, format );
2242
2243 res = vsnprintf( str, size, format, ap );
2244
2245 va_end( ap );
2246
2247 // No quick fix possible
2248 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002249 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002250
2251 return res;
2252}
2253
2254#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002255#endif
2256
Paul Bakkerd98030e2009-05-02 15:13:40 +00002257#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2258
2259#define SAFE_SNPRINTF() \
2260{ \
2261 if( ret == -1 ) \
2262 return( -1 ); \
2263 \
Paul Bakker23986e52011-04-24 08:57:21 +00002264 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002265 p[n - 1] = '\0'; \
2266 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2267 } \
2268 \
Paul Bakker23986e52011-04-24 08:57:21 +00002269 n -= (unsigned int) ret; \
2270 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002271}
2272
Paul Bakker5121ce52009-01-03 21:22:43 +00002273/*
2274 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002275 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002277int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002278{
Paul Bakker23986e52011-04-24 08:57:21 +00002279 int ret;
2280 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002282 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002283 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 char s[128], *p;
2285
2286 memset( s, 0, sizeof( s ) );
2287
2288 name = dn;
2289 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002290 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
2292 while( name != NULL )
2293 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002294 if( !name->oid.p )
2295 {
2296 name = name->next;
2297 continue;
2298 }
2299
Paul Bakker74111d32011-01-15 16:57:55 +00002300 if( name != dn )
2301 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002302 ret = snprintf( p, n, ", " );
2303 SAFE_SNPRINTF();
2304 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
Paul Bakkerc70b9822013-04-07 22:00:46 +02002306 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002307
Paul Bakkerc70b9822013-04-07 22:00:46 +02002308 if( ret == 0 )
2309 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002311 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002312 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 for( i = 0; i < name->val.len; i++ )
2315 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002316 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 break;
2318
2319 c = name->val.p[i];
2320 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2321 s[i] = '?';
2322 else s[i] = c;
2323 }
2324 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002325 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002326 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 name = name->next;
2328 }
2329
Paul Bakker23986e52011-04-24 08:57:21 +00002330 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331}
2332
2333/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002334 * Store the serial in printable form into buf; no more
2335 * than size characters will be written
2336 */
2337int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2338{
Paul Bakker23986e52011-04-24 08:57:21 +00002339 int ret;
2340 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002341 char *p;
2342
2343 p = buf;
2344 n = size;
2345
2346 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002347 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002348
2349 for( i = 0; i < nr; i++ )
2350 {
Paul Bakker93048802011-12-05 14:38:06 +00002351 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002352 continue;
2353
Paul Bakkerdd476992011-01-16 21:34:59 +00002354 ret = snprintf( p, n, "%02X%s",
2355 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2356 SAFE_SNPRINTF();
2357 }
2358
Paul Bakker03c7c252011-11-25 12:37:37 +00002359 if( nr != serial->len )
2360 {
2361 ret = snprintf( p, n, "...." );
2362 SAFE_SNPRINTF();
2363 }
2364
Paul Bakker23986e52011-04-24 08:57:21 +00002365 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002366}
2367
2368/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002369 * Helper for writing "RSA key size", "EC key size", etc
2370 */
2371static int x509_key_size_helper( char *buf, size_t size, const char *name )
2372{
2373 char *p = buf;
2374 size_t n = size;
2375 int ret;
2376
2377 if( strlen( name ) + sizeof( " key size" ) > size )
2378 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
2379
2380 ret = snprintf( p, n, "%s key size", name );
2381 SAFE_SNPRINTF();
2382
2383 return( 0 );
2384}
2385
2386/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002387 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002389#define BEFORE_COLON 14
2390#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00002391int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2392 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002393{
Paul Bakker23986e52011-04-24 08:57:21 +00002394 int ret;
2395 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002397 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002398 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002401 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Paul Bakkerd98030e2009-05-02 15:13:40 +00002403 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002405 SAFE_SNPRINTF();
2406 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Paul Bakkerdd476992011-01-16 21:34:59 +00002410 ret = x509parse_serial_gets( p, n, &crt->serial);
2411 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Paul Bakkerd98030e2009-05-02 15:13:40 +00002413 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2414 SAFE_SNPRINTF();
2415 ret = x509parse_dn_gets( p, n, &crt->issuer );
2416 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002417
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2419 SAFE_SNPRINTF();
2420 ret = x509parse_dn_gets( p, n, &crt->subject );
2421 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Paul Bakkerd98030e2009-05-02 15:13:40 +00002423 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2425 crt->valid_from.year, crt->valid_from.mon,
2426 crt->valid_from.day, crt->valid_from.hour,
2427 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002428 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Paul Bakkerd98030e2009-05-02 15:13:40 +00002430 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2432 crt->valid_to.year, crt->valid_to.mon,
2433 crt->valid_to.day, crt->valid_to.hour,
2434 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002435 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Paul Bakkerc70b9822013-04-07 22:00:46 +02002437 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002438 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Paul Bakkerc70b9822013-04-07 22:00:46 +02002440 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2441 if( ret != 0 )
2442 ret = snprintf( p, n, "???" );
2443 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002444 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002445 SAFE_SNPRINTF();
2446
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002447 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002448 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002449 {
2450 return( ret );
2451 }
2452
2453 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002454 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002455 SAFE_SNPRINTF();
2456
Paul Bakker23986e52011-04-24 08:57:21 +00002457 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002458}
2459
Paul Bakker74111d32011-01-15 16:57:55 +00002460/*
2461 * Return an informational string describing the given OID
2462 */
2463const char *x509_oid_get_description( x509_buf *oid )
2464{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002465 const char *desc = NULL;
2466 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002467
Paul Bakkerc70b9822013-04-07 22:00:46 +02002468 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002469
Paul Bakkerc70b9822013-04-07 22:00:46 +02002470 if( ret != 0 )
2471 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002472
Paul Bakkerc70b9822013-04-07 22:00:46 +02002473 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002474}
2475
2476/* Return the x.y.z.... style numeric string for the given OID */
2477int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2478{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002479 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002480}
2481
Paul Bakkerd98030e2009-05-02 15:13:40 +00002482/*
2483 * Return an informational string about the CRL.
2484 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002485int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2486 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487{
Paul Bakker23986e52011-04-24 08:57:21 +00002488 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002489 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002490 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002491 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002492 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493
2494 p = buf;
2495 n = size;
2496
2497 ret = snprintf( p, n, "%sCRL version : %d",
2498 prefix, crl->version );
2499 SAFE_SNPRINTF();
2500
2501 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2502 SAFE_SNPRINTF();
2503 ret = x509parse_dn_gets( p, n, &crl->issuer );
2504 SAFE_SNPRINTF();
2505
2506 ret = snprintf( p, n, "\n%sthis update : " \
2507 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2508 crl->this_update.year, crl->this_update.mon,
2509 crl->this_update.day, crl->this_update.hour,
2510 crl->this_update.min, crl->this_update.sec );
2511 SAFE_SNPRINTF();
2512
2513 ret = snprintf( p, n, "\n%snext update : " \
2514 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2515 crl->next_update.year, crl->next_update.mon,
2516 crl->next_update.day, crl->next_update.hour,
2517 crl->next_update.min, crl->next_update.sec );
2518 SAFE_SNPRINTF();
2519
2520 entry = &crl->entry;
2521
2522 ret = snprintf( p, n, "\n%sRevoked certificates:",
2523 prefix );
2524 SAFE_SNPRINTF();
2525
Paul Bakker9be19372009-07-27 20:21:53 +00002526 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 {
2528 ret = snprintf( p, n, "\n%sserial number: ",
2529 prefix );
2530 SAFE_SNPRINTF();
2531
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002532 ret = x509parse_serial_gets( p, n, &entry->serial);
2533 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002534
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 ret = snprintf( p, n, " revocation date: " \
2536 "%04d-%02d-%02d %02d:%02d:%02d",
2537 entry->revocation_date.year, entry->revocation_date.mon,
2538 entry->revocation_date.day, entry->revocation_date.hour,
2539 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002540 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541
2542 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 }
2544
Paul Bakkerc70b9822013-04-07 22:00:46 +02002545 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002546 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Paul Bakkerc70b9822013-04-07 22:00:46 +02002548 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2549 if( ret != 0 )
2550 ret = snprintf( p, n, "???" );
2551 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002552 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 SAFE_SNPRINTF();
2554
Paul Bakker1e27bb22009-07-19 20:25:25 +00002555 ret = snprintf( p, n, "\n" );
2556 SAFE_SNPRINTF();
2557
Paul Bakker23986e52011-04-24 08:57:21 +00002558 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559}
2560
2561/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002562 * Return an informational string about the CSR.
2563 */
2564int x509parse_csr_info( char *buf, size_t size, const char *prefix,
2565 const x509_csr *csr )
2566{
2567 int ret;
2568 size_t n;
2569 char *p;
2570 const char *desc;
2571 char key_size_str[BEFORE_COLON];
2572
2573 p = buf;
2574 n = size;
2575
2576 ret = snprintf( p, n, "%sCSR version : %d",
2577 prefix, csr->version );
2578 SAFE_SNPRINTF();
2579
2580 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2581 SAFE_SNPRINTF();
2582 ret = x509parse_dn_gets( p, n, &csr->subject );
2583 SAFE_SNPRINTF();
2584
2585 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
2586 SAFE_SNPRINTF();
2587
2588 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
2589 if( ret != 0 )
2590 ret = snprintf( p, n, "???" );
2591 else
2592 ret = snprintf( p, n, "%s", desc );
2593 SAFE_SNPRINTF();
2594
2595 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
2596 pk_get_name( &csr->pk ) ) ) != 0 )
2597 {
2598 return( ret );
2599 }
2600
2601 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
2602 (int) pk_get_size( &csr->pk ) );
2603 SAFE_SNPRINTF();
2604
2605 return( (int) ( size - n ) );
2606}
2607
2608/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002609 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002611#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00002612int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613{
Paul Bakkercce9d772011-11-18 14:26:47 +00002614 int year, mon, day;
2615 int hour, min, sec;
2616
2617#if defined(_WIN32)
2618 SYSTEMTIME st;
2619
2620 GetLocalTime(&st);
2621
2622 year = st.wYear;
2623 mon = st.wMonth;
2624 day = st.wDay;
2625 hour = st.wHour;
2626 min = st.wMinute;
2627 sec = st.wSecond;
2628#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 struct tm *lt;
2630 time_t tt;
2631
2632 tt = time( NULL );
2633 lt = localtime( &tt );
2634
Paul Bakkercce9d772011-11-18 14:26:47 +00002635 year = lt->tm_year + 1900;
2636 mon = lt->tm_mon + 1;
2637 day = lt->tm_mday;
2638 hour = lt->tm_hour;
2639 min = lt->tm_min;
2640 sec = lt->tm_sec;
2641#endif
2642
2643 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002644 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645
Paul Bakkercce9d772011-11-18 14:26:47 +00002646 if( year == to->year &&
2647 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002648 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Paul Bakkercce9d772011-11-18 14:26:47 +00002650 if( year == to->year &&
2651 mon == to->mon &&
2652 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002653 return( 1 );
2654
Paul Bakkercce9d772011-11-18 14:26:47 +00002655 if( year == to->year &&
2656 mon == to->mon &&
2657 day == to->day &&
2658 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002659 return( 1 );
2660
Paul Bakkercce9d772011-11-18 14:26:47 +00002661 if( year == to->year &&
2662 mon == to->mon &&
2663 day == to->day &&
2664 hour == to->hour &&
2665 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002666 return( 1 );
2667
Paul Bakkercce9d772011-11-18 14:26:47 +00002668 if( year == to->year &&
2669 mon == to->mon &&
2670 day == to->day &&
2671 hour == to->hour &&
2672 min == to->min &&
2673 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002674 return( 1 );
2675
Paul Bakker40ea7de2009-05-03 10:18:48 +00002676 return( 0 );
2677}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002678#else /* POLARSSL_HAVE_TIME */
2679int x509parse_time_expired( const x509_time *to )
2680{
2681 ((void) to);
2682 return( 0 );
2683}
2684#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00002685
2686/*
2687 * Return 1 if the certificate is revoked, or 0 otherwise.
2688 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002689int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002690{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002691 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002692
2693 while( cur != NULL && cur->serial.len != 0 )
2694 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002695 if( crt->serial.len == cur->serial.len &&
2696 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002697 {
2698 if( x509parse_time_expired( &cur->revocation_date ) )
2699 return( 1 );
2700 }
2701
2702 cur = cur->next;
2703 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002704
2705 return( 0 );
2706}
2707
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002708/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002709 * Check that the given certificate is valid accoring to the CRL.
2710 */
2711static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2712 x509_crl *crl_list)
2713{
2714 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002715 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2716 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002717
Paul Bakker915275b2012-09-28 07:10:55 +00002718 if( ca == NULL )
2719 return( flags );
2720
Paul Bakker76fd75a2011-01-16 21:12:10 +00002721 /*
2722 * TODO: What happens if no CRL is present?
2723 * Suggestion: Revocation state should be unknown if no CRL is present.
2724 * For backwards compatibility this is not yet implemented.
2725 */
2726
Paul Bakker915275b2012-09-28 07:10:55 +00002727 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002728 {
Paul Bakker915275b2012-09-28 07:10:55 +00002729 if( crl_list->version == 0 ||
2730 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002731 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2732 crl_list->issuer_raw.len ) != 0 )
2733 {
2734 crl_list = crl_list->next;
2735 continue;
2736 }
2737
2738 /*
2739 * Check if CRL is correctly signed by the trusted CA
2740 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002741 md_info = md_info_from_type( crl_list->sig_md );
2742 if( md_info == NULL )
2743 {
2744 /*
2745 * Cannot check 'unknown' hash
2746 */
2747 flags |= BADCRL_NOT_TRUSTED;
2748 break;
2749 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002750
Paul Bakkerc70b9822013-04-07 22:00:46 +02002751 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002752
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002753 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002754 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002755 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002756 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002757 flags |= BADCRL_NOT_TRUSTED;
2758 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002759 }
2760
2761 /*
2762 * Check for validity of CRL (Do not drop out)
2763 */
2764 if( x509parse_time_expired( &crl_list->next_update ) )
2765 flags |= BADCRL_EXPIRED;
2766
2767 /*
2768 * Check if certificate is revoked
2769 */
2770 if( x509parse_revoked(crt, crl_list) )
2771 {
2772 flags |= BADCERT_REVOKED;
2773 break;
2774 }
2775
2776 crl_list = crl_list->next;
2777 }
2778 return flags;
2779}
2780
Paul Bakkera5943852013-09-09 17:21:45 +02002781// Equal == 0, inequal == 1
2782static int x509_name_cmp( const void *s1, const void *s2, size_t len )
2783{
2784 size_t i;
2785 unsigned char diff;
2786 const unsigned char *n1 = s1, *n2 = s2;
2787
2788 for( i = 0; i < len; i++ )
2789 {
2790 diff = n1[i] ^ n2[i];
2791
2792 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
2793 continue;
2794
2795 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
2796 continue;
2797
2798 return( 1 );
2799 }
2800
2801 return( 0 );
2802}
2803
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002804static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002805{
2806 size_t i;
2807 size_t cn_idx = 0;
2808
Paul Bakker57b12982012-02-11 17:38:38 +00002809 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002810 return( 0 );
2811
2812 for( i = 0; i < strlen( cn ); ++i )
2813 {
2814 if( cn[i] == '.' )
2815 {
2816 cn_idx = i;
2817 break;
2818 }
2819 }
2820
2821 if( cn_idx == 0 )
2822 return( 0 );
2823
Paul Bakker535e97d2012-08-23 10:49:55 +00002824 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkera5943852013-09-09 17:21:45 +02002825 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002826 {
2827 return( 1 );
2828 }
2829
2830 return( 0 );
2831}
2832
Paul Bakker915275b2012-09-28 07:10:55 +00002833static int x509parse_verify_top(
2834 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002835 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002836 int (*f_vrfy)(void *, x509_cert *, int, int *),
2837 void *p_vrfy )
2838{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002839 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002840 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002841 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2842 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002843
2844 if( x509parse_time_expired( &child->valid_to ) )
2845 *flags |= BADCERT_EXPIRED;
2846
2847 /*
2848 * Child is the top of the chain. Check against the trust_ca list.
2849 */
2850 *flags |= BADCERT_NOT_TRUSTED;
2851
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02002852 md_info = md_info_from_type( child->sig_md );
2853 if( md_info == NULL )
2854 {
2855 /*
2856 * Cannot check 'unknown', no need to try any CA
2857 */
2858 trust_ca = NULL;
2859 }
2860 else
2861 md( md_info, child->tbs.p, child->tbs.len, hash );
2862
Paul Bakker915275b2012-09-28 07:10:55 +00002863 while( trust_ca != NULL )
2864 {
2865 if( trust_ca->version == 0 ||
2866 child->issuer_raw.len != trust_ca->subject_raw.len ||
2867 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2868 child->issuer_raw.len ) != 0 )
2869 {
2870 trust_ca = trust_ca->next;
2871 continue;
2872 }
2873
Paul Bakker9a736322012-11-14 12:39:52 +00002874 /*
2875 * Reduce path_len to check against if top of the chain is
2876 * the same as the trusted CA
2877 */
2878 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2879 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002880 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00002881 {
2882 check_path_cnt--;
2883 }
2884
Paul Bakker915275b2012-09-28 07:10:55 +00002885 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00002886 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00002887 {
2888 trust_ca = trust_ca->next;
2889 continue;
2890 }
2891
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002892 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002893 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002894 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00002895 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002896 trust_ca = trust_ca->next;
2897 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00002898 }
2899
2900 /*
2901 * Top of chain is signed by a trusted CA
2902 */
2903 *flags &= ~BADCERT_NOT_TRUSTED;
2904 break;
2905 }
2906
Paul Bakker9a736322012-11-14 12:39:52 +00002907 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01002908 * If top of chain is not the same as the trusted CA send a verify request
2909 * to the callback for any issues with validity and CRL presence for the
2910 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00002911 */
2912 if( trust_ca != NULL &&
2913 ( child->subject_raw.len != trust_ca->subject_raw.len ||
2914 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2915 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00002916 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02002917 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00002918 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
2919
2920 if( x509parse_time_expired( &trust_ca->valid_to ) )
2921 ca_flags |= BADCERT_EXPIRED;
2922
Paul Bakker915275b2012-09-28 07:10:55 +00002923 if( NULL != f_vrfy )
2924 {
Paul Bakker9a736322012-11-14 12:39:52 +00002925 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00002926 return( ret );
2927 }
2928 }
2929
2930 /* Call callback on top cert */
2931 if( NULL != f_vrfy )
2932 {
Paul Bakker9a736322012-11-14 12:39:52 +00002933 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00002934 return( ret );
2935 }
2936
Paul Bakker915275b2012-09-28 07:10:55 +00002937 *flags |= ca_flags;
2938
2939 return( 0 );
2940}
2941
2942static int x509parse_verify_child(
2943 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002944 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002945 int (*f_vrfy)(void *, x509_cert *, int, int *),
2946 void *p_vrfy )
2947{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002948 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00002949 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002950 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00002951 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002952 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002953
2954 if( x509parse_time_expired( &child->valid_to ) )
2955 *flags |= BADCERT_EXPIRED;
2956
Paul Bakkerc70b9822013-04-07 22:00:46 +02002957 md_info = md_info_from_type( child->sig_md );
2958 if( md_info == NULL )
2959 {
2960 /*
2961 * Cannot check 'unknown' hash
2962 */
Paul Bakker915275b2012-09-28 07:10:55 +00002963 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002964 }
2965 else
2966 {
2967 md( md_info, child->tbs.p, child->tbs.len, hash );
2968
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002969 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002970 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002971 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002972 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002973 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002974 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002975 }
2976
Paul Bakker915275b2012-09-28 07:10:55 +00002977 /* Check trusted CA's CRL for the given crt */
2978 *flags |= x509parse_verifycrl(child, parent, ca_crl);
2979
2980 grandparent = parent->next;
2981
2982 while( grandparent != NULL )
2983 {
2984 if( grandparent->version == 0 ||
2985 grandparent->ca_istrue == 0 ||
2986 parent->issuer_raw.len != grandparent->subject_raw.len ||
2987 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
2988 parent->issuer_raw.len ) != 0 )
2989 {
2990 grandparent = grandparent->next;
2991 continue;
2992 }
2993 break;
2994 }
2995
Paul Bakker915275b2012-09-28 07:10:55 +00002996 if( grandparent != NULL )
2997 {
2998 /*
2999 * Part of the chain
3000 */
Paul Bakker9a736322012-11-14 12:39:52 +00003001 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 +00003002 if( ret != 0 )
3003 return( ret );
Paul Bakkera5943852013-09-09 17:21:45 +02003004 }
Paul Bakker915275b2012-09-28 07:10:55 +00003005 else
3006 {
Paul Bakker9a736322012-11-14 12:39:52 +00003007 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 +00003008 if( ret != 0 )
3009 return( ret );
3010 }
3011
3012 /* child is verified to be a child of the parent, call verify callback */
3013 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003014 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003015 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003016
3017 *flags |= parent_flags;
3018
3019 return( 0 );
3020}
3021
Paul Bakker76fd75a2011-01-16 21:12:10 +00003022/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003023 * Verify the certificate validity
3024 */
3025int x509parse_verify( x509_cert *crt,
3026 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003027 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003028 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003029 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003030 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003031{
Paul Bakker23986e52011-04-24 08:57:21 +00003032 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003033 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003034 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003035 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003037 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Paul Bakker40ea7de2009-05-03 10:18:48 +00003039 *flags = 0;
3040
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 if( cn != NULL )
3042 {
3043 name = &crt->subject;
3044 cn_len = strlen( cn );
3045
Paul Bakker4d2c1242012-05-10 14:12:46 +00003046 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003048 cur = &crt->subject_alt_names;
3049
3050 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003051 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003052 if( cur->buf.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003053 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003054 break;
3055
Paul Bakker535e97d2012-08-23 10:49:55 +00003056 if( cur->buf.len > 2 &&
3057 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003058 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003059 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker4d2c1242012-05-10 14:12:46 +00003061 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003062 }
3063
3064 if( cur == NULL )
3065 *flags |= BADCERT_CN_MISMATCH;
3066 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003067 else
3068 {
3069 while( name != NULL )
3070 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003071 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003072 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003073 if( name->val.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003074 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003075 break;
3076
Paul Bakker535e97d2012-08-23 10:49:55 +00003077 if( name->val.len > 2 &&
3078 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003079 x509_wildcard_verify( cn, &name->val ) )
3080 break;
3081 }
3082
3083 name = name->next;
3084 }
3085
3086 if( name == NULL )
3087 *flags |= BADCERT_CN_MISMATCH;
3088 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 }
3090
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003092 * Iterate upwards in the given cert chain, to find our crt parent.
3093 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003095 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Paul Bakker76fd75a2011-01-16 21:12:10 +00003097 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003099 if( parent->ca_istrue == 0 ||
3100 crt->issuer_raw.len != parent->subject_raw.len ||
3101 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 crt->issuer_raw.len ) != 0 )
3103 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003104 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 continue;
3106 }
Paul Bakker915275b2012-09-28 07:10:55 +00003107 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 }
3109
Paul Bakker915275b2012-09-28 07:10:55 +00003110 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 {
Paul Bakker915275b2012-09-28 07:10:55 +00003112 /*
3113 * Part of the chain
3114 */
Paul Bakker9a736322012-11-14 12:39:52 +00003115 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003116 if( ret != 0 )
3117 return( ret );
3118 }
3119 else
Paul Bakker74111d32011-01-15 16:57:55 +00003120 {
Paul Bakker9a736322012-11-14 12:39:52 +00003121 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003122 if( ret != 0 )
3123 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003124 }
Paul Bakker915275b2012-09-28 07:10:55 +00003125
3126 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003127 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003128
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 return( 0 );
3130}
3131
3132/*
3133 * Unallocate all certificate data
3134 */
3135void x509_free( x509_cert *crt )
3136{
3137 x509_cert *cert_cur = crt;
3138 x509_cert *cert_prv;
3139 x509_name *name_cur;
3140 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003141 x509_sequence *seq_cur;
3142 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
3144 if( crt == NULL )
3145 return;
3146
3147 do
3148 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003149 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
3151 name_cur = cert_cur->issuer.next;
3152 while( name_cur != NULL )
3153 {
3154 name_prv = name_cur;
3155 name_cur = name_cur->next;
3156 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003157 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158 }
3159
3160 name_cur = cert_cur->subject.next;
3161 while( name_cur != NULL )
3162 {
3163 name_prv = name_cur;
3164 name_cur = name_cur->next;
3165 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003166 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 }
3168
Paul Bakker74111d32011-01-15 16:57:55 +00003169 seq_cur = cert_cur->ext_key_usage.next;
3170 while( seq_cur != NULL )
3171 {
3172 seq_prv = seq_cur;
3173 seq_cur = seq_cur->next;
3174 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003175 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003176 }
3177
Paul Bakker8afa70d2012-02-11 18:42:45 +00003178 seq_cur = cert_cur->subject_alt_names.next;
3179 while( seq_cur != NULL )
3180 {
3181 seq_prv = seq_cur;
3182 seq_cur = seq_cur->next;
3183 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003184 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003185 }
3186
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 if( cert_cur->raw.p != NULL )
3188 {
3189 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003190 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 }
3192
3193 cert_cur = cert_cur->next;
3194 }
3195 while( cert_cur != NULL );
3196
3197 cert_cur = crt;
3198 do
3199 {
3200 cert_prv = cert_cur;
3201 cert_cur = cert_cur->next;
3202
3203 memset( cert_prv, 0, sizeof( x509_cert ) );
3204 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003205 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 }
3207 while( cert_cur != NULL );
3208}
3209
Paul Bakkerd98030e2009-05-02 15:13:40 +00003210/*
3211 * Unallocate all CRL data
3212 */
3213void x509_crl_free( x509_crl *crl )
3214{
3215 x509_crl *crl_cur = crl;
3216 x509_crl *crl_prv;
3217 x509_name *name_cur;
3218 x509_name *name_prv;
3219 x509_crl_entry *entry_cur;
3220 x509_crl_entry *entry_prv;
3221
3222 if( crl == NULL )
3223 return;
3224
3225 do
3226 {
3227 name_cur = crl_cur->issuer.next;
3228 while( name_cur != NULL )
3229 {
3230 name_prv = name_cur;
3231 name_cur = name_cur->next;
3232 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003233 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003234 }
3235
3236 entry_cur = crl_cur->entry.next;
3237 while( entry_cur != NULL )
3238 {
3239 entry_prv = entry_cur;
3240 entry_cur = entry_cur->next;
3241 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003242 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003243 }
3244
3245 if( crl_cur->raw.p != NULL )
3246 {
3247 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003248 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003249 }
3250
3251 crl_cur = crl_cur->next;
3252 }
3253 while( crl_cur != NULL );
3254
3255 crl_cur = crl;
3256 do
3257 {
3258 crl_prv = crl_cur;
3259 crl_cur = crl_cur->next;
3260
3261 memset( crl_prv, 0, sizeof( x509_crl ) );
3262 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003263 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003264 }
3265 while( crl_cur != NULL );
3266}
3267
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003268/*
3269 * Unallocate all CSR data
3270 */
3271void x509_csr_free( x509_csr *csr )
3272{
3273 x509_name *name_cur;
3274 x509_name *name_prv;
3275
3276 if( csr == NULL )
3277 return;
3278
3279 pk_free( &csr->pk );
3280
3281 name_cur = csr->subject.next;
3282 while( name_cur != NULL )
3283 {
3284 name_prv = name_cur;
3285 name_cur = name_cur->next;
3286 memset( name_prv, 0, sizeof( x509_name ) );
3287 polarssl_free( name_prv );
3288 }
3289
3290 if( csr->raw.p != NULL )
3291 {
3292 memset( csr->raw.p, 0, csr->raw.len );
3293 polarssl_free( csr->raw.p );
3294 }
3295
3296 memset( csr, 0, sizeof( x509_csr ) );
3297}
3298
Paul Bakker40e46942009-01-03 21:51:57 +00003299#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Paul Bakker40e46942009-01-03 21:51:57 +00003301#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
3303/*
3304 * Checkup routine
3305 */
3306int x509_self_test( int verbose )
3307{
Paul Bakker5690efc2011-05-26 13:16:06 +00003308#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003309 int ret;
3310 int flags;
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 x509_cert cacert;
3312 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003313 pk_context pkey;
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
3315 if( verbose != 0 )
3316 printf( " X.509 certificate load: " );
3317
3318 memset( &clicert, 0, sizeof( x509_cert ) );
3319
Paul Bakker3c2122f2013-06-24 19:03:14 +02003320 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003321 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 if( ret != 0 )
3323 {
3324 if( verbose != 0 )
3325 printf( "failed\n" );
3326
3327 return( ret );
3328 }
3329
3330 memset( &cacert, 0, sizeof( x509_cert ) );
3331
Paul Bakker3c2122f2013-06-24 19:03:14 +02003332 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003333 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 if( ret != 0 )
3335 {
3336 if( verbose != 0 )
3337 printf( "failed\n" );
3338
3339 return( ret );
3340 }
3341
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003342#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
3343 defined(POLARSSL_DES_C) && defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 if( verbose != 0 )
3345 printf( "passed\n X.509 private key load: " );
3346
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003347 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00003348
Paul Bakker1a7550a2013-09-15 13:01:22 +02003349 if( ( ret = pk_parse_key( &pkey,
3350 (const unsigned char *) test_ca_key,
3351 strlen( test_ca_key ),
3352 (const unsigned char *) test_ca_pwd,
3353 strlen( test_ca_pwd ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 {
3355 if( verbose != 0 )
3356 printf( "failed\n" );
3357
3358 return( ret );
3359 }
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003361
3362 if( verbose != 0 )
3363 printf( "passed\n X.509 signature verify: ");
3364
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003365 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 if( ret != 0 )
3367 {
3368 if( verbose != 0 )
3369 printf( "failed\n" );
3370
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003371 printf("ret = %d, &flags = %04x\n", ret, flags);
3372
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 return( ret );
3374 }
3375
3376 if( verbose != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +02003377 printf( "passed\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
3379 x509_free( &cacert );
3380 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003381 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
3383 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003384#else
3385 ((void) verbose);
3386 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003388}
3389
3390#endif
3391
3392#endif