blob: de4ed7f7df81427dbc15ba73a47b8d028851df00 [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 Bakkereaa89f82011-04-04 21:36:15 +00002222#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002223/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002224 * Parse DHM parameters
2225 */
Paul Bakker23986e52011-04-24 08:57:21 +00002226int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002227{
Paul Bakker23986e52011-04-24 08:57:21 +00002228 int ret;
2229 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002230 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002231#if defined(POLARSSL_PEM_C)
2232 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002233
Paul Bakker96743fc2011-02-12 14:30:57 +00002234 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002235
Paul Bakker96743fc2011-02-12 14:30:57 +00002236 ret = pem_read_buffer( &pem,
2237 "-----BEGIN DH PARAMETERS-----",
2238 "-----END DH PARAMETERS-----",
2239 dhmin, NULL, 0, &dhminlen );
2240
2241 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002242 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002243 /*
2244 * Was PEM encoded
2245 */
2246 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002247 }
Paul Bakker00b28602013-06-24 13:02:41 +02002248 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002249 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002250 pem_free( &pem );
2251 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002252 }
2253
Paul Bakker96743fc2011-02-12 14:30:57 +00002254 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2255#else
2256 p = (unsigned char *) dhmin;
2257#endif
2258 end = p + dhminlen;
2259
Paul Bakker1b57b062011-01-06 15:48:19 +00002260 memset( dhm, 0, sizeof( dhm_context ) );
2261
Paul Bakker1b57b062011-01-06 15:48:19 +00002262 /*
2263 * DHParams ::= SEQUENCE {
2264 * prime INTEGER, -- P
2265 * generator INTEGER, -- g
2266 * }
2267 */
2268 if( ( ret = asn1_get_tag( &p, end, &len,
2269 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2270 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002271#if defined(POLARSSL_PEM_C)
2272 pem_free( &pem );
2273#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02002274 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002275 }
2276
2277 end = p + len;
2278
2279 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2280 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2281 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002282#if defined(POLARSSL_PEM_C)
2283 pem_free( &pem );
2284#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002285 dhm_free( dhm );
Paul Bakker1a7550a2013-09-15 13:01:22 +02002286 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002287 }
2288
2289 if( p != end )
2290 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002291#if defined(POLARSSL_PEM_C)
2292 pem_free( &pem );
2293#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002294 dhm_free( dhm );
Paul Bakker1a7550a2013-09-15 13:01:22 +02002295 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002296 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2297 }
2298
Paul Bakker96743fc2011-02-12 14:30:57 +00002299#if defined(POLARSSL_PEM_C)
2300 pem_free( &pem );
2301#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002302
2303 return( 0 );
2304}
2305
Paul Bakker335db3f2011-04-25 15:28:35 +00002306#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002307/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002308 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002309 */
2310int x509parse_dhmfile( dhm_context *dhm, const char *path )
2311{
2312 int ret;
2313 size_t n;
2314 unsigned char *buf;
2315
Paul Bakker69e095c2011-12-10 21:55:01 +00002316 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2317 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002318
Paul Bakker27fdf462011-06-09 13:55:13 +00002319 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002320
2321 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002322 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002323
2324 return( ret );
2325}
Paul Bakker335db3f2011-04-25 15:28:35 +00002326#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002327#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002328
Paul Bakker5121ce52009-01-03 21:22:43 +00002329#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002330#include <stdarg.h>
2331
2332#if !defined vsnprintf
2333#define vsnprintf _vsnprintf
2334#endif // vsnprintf
2335
2336/*
2337 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2338 * Result value is not size of buffer needed, but -1 if no fit is possible.
2339 *
2340 * This fuction tries to 'fix' this by at least suggesting enlarging the
2341 * size by 20.
2342 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002343static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002344{
2345 va_list ap;
2346 int res = -1;
2347
2348 va_start( ap, format );
2349
2350 res = vsnprintf( str, size, format, ap );
2351
2352 va_end( ap );
2353
2354 // No quick fix possible
2355 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002356 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002357
2358 return res;
2359}
2360
2361#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002362#endif
2363
Paul Bakkerd98030e2009-05-02 15:13:40 +00002364#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2365
2366#define SAFE_SNPRINTF() \
2367{ \
2368 if( ret == -1 ) \
2369 return( -1 ); \
2370 \
Paul Bakker23986e52011-04-24 08:57:21 +00002371 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002372 p[n - 1] = '\0'; \
2373 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2374 } \
2375 \
Paul Bakker23986e52011-04-24 08:57:21 +00002376 n -= (unsigned int) ret; \
2377 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378}
2379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380/*
2381 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002382 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002384int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385{
Paul Bakker23986e52011-04-24 08:57:21 +00002386 int ret;
2387 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002389 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002390 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 char s[128], *p;
2392
2393 memset( s, 0, sizeof( s ) );
2394
2395 name = dn;
2396 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002397 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
2399 while( name != NULL )
2400 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002401 if( !name->oid.p )
2402 {
2403 name = name->next;
2404 continue;
2405 }
2406
Paul Bakker74111d32011-01-15 16:57:55 +00002407 if( name != dn )
2408 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002409 ret = snprintf( p, n, ", " );
2410 SAFE_SNPRINTF();
2411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
Paul Bakkerc70b9822013-04-07 22:00:46 +02002413 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Paul Bakkerc70b9822013-04-07 22:00:46 +02002415 if( ret == 0 )
2416 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002419 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
2421 for( i = 0; i < name->val.len; i++ )
2422 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002423 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 break;
2425
2426 c = name->val.p[i];
2427 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2428 s[i] = '?';
2429 else s[i] = c;
2430 }
2431 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002432 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002433 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 name = name->next;
2435 }
2436
Paul Bakker23986e52011-04-24 08:57:21 +00002437 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438}
2439
2440/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002441 * Store the serial in printable form into buf; no more
2442 * than size characters will be written
2443 */
2444int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2445{
Paul Bakker23986e52011-04-24 08:57:21 +00002446 int ret;
2447 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002448 char *p;
2449
2450 p = buf;
2451 n = size;
2452
2453 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002454 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002455
2456 for( i = 0; i < nr; i++ )
2457 {
Paul Bakker93048802011-12-05 14:38:06 +00002458 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002459 continue;
2460
Paul Bakkerdd476992011-01-16 21:34:59 +00002461 ret = snprintf( p, n, "%02X%s",
2462 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2463 SAFE_SNPRINTF();
2464 }
2465
Paul Bakker03c7c252011-11-25 12:37:37 +00002466 if( nr != serial->len )
2467 {
2468 ret = snprintf( p, n, "...." );
2469 SAFE_SNPRINTF();
2470 }
2471
Paul Bakker23986e52011-04-24 08:57:21 +00002472 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002473}
2474
2475/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002476 * Helper for writing "RSA key size", "EC key size", etc
2477 */
2478static int x509_key_size_helper( char *buf, size_t size, const char *name )
2479{
2480 char *p = buf;
2481 size_t n = size;
2482 int ret;
2483
2484 if( strlen( name ) + sizeof( " key size" ) > size )
2485 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
2486
2487 ret = snprintf( p, n, "%s key size", name );
2488 SAFE_SNPRINTF();
2489
2490 return( 0 );
2491}
2492
2493/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002496#define BEFORE_COLON 14
2497#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00002498int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2499 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002500{
Paul Bakker23986e52011-04-24 08:57:21 +00002501 int ret;
2502 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002504 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002505 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00002506
2507 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002508 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
Paul Bakkerd98030e2009-05-02 15:13:40 +00002510 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002512 SAFE_SNPRINTF();
2513 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002515 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Paul Bakkerdd476992011-01-16 21:34:59 +00002517 ret = x509parse_serial_gets( p, n, &crt->serial);
2518 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2521 SAFE_SNPRINTF();
2522 ret = x509parse_dn_gets( p, n, &crt->issuer );
2523 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2526 SAFE_SNPRINTF();
2527 ret = x509parse_dn_gets( p, n, &crt->subject );
2528 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Paul Bakkerd98030e2009-05-02 15:13:40 +00002530 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2532 crt->valid_from.year, crt->valid_from.mon,
2533 crt->valid_from.day, crt->valid_from.hour,
2534 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Paul Bakkerd98030e2009-05-02 15:13:40 +00002537 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2539 crt->valid_to.year, crt->valid_to.mon,
2540 crt->valid_to.day, crt->valid_to.hour,
2541 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002542 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Paul Bakkerc70b9822013-04-07 22:00:46 +02002544 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002545 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Paul Bakkerc70b9822013-04-07 22:00:46 +02002547 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2548 if( ret != 0 )
2549 ret = snprintf( p, n, "???" );
2550 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002551 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002552 SAFE_SNPRINTF();
2553
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002554 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002555 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002556 {
2557 return( ret );
2558 }
2559
2560 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002561 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002562 SAFE_SNPRINTF();
2563
Paul Bakker23986e52011-04-24 08:57:21 +00002564 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002565}
2566
Paul Bakker74111d32011-01-15 16:57:55 +00002567/*
2568 * Return an informational string describing the given OID
2569 */
2570const char *x509_oid_get_description( x509_buf *oid )
2571{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002572 const char *desc = NULL;
2573 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002574
Paul Bakkerc70b9822013-04-07 22:00:46 +02002575 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002576
Paul Bakkerc70b9822013-04-07 22:00:46 +02002577 if( ret != 0 )
2578 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002579
Paul Bakkerc70b9822013-04-07 22:00:46 +02002580 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002581}
2582
2583/* Return the x.y.z.... style numeric string for the given OID */
2584int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2585{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002586 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002587}
2588
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589/*
2590 * Return an informational string about the CRL.
2591 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002592int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2593 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002594{
Paul Bakker23986e52011-04-24 08:57:21 +00002595 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002596 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002598 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002599 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600
2601 p = buf;
2602 n = size;
2603
2604 ret = snprintf( p, n, "%sCRL version : %d",
2605 prefix, crl->version );
2606 SAFE_SNPRINTF();
2607
2608 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2609 SAFE_SNPRINTF();
2610 ret = x509parse_dn_gets( p, n, &crl->issuer );
2611 SAFE_SNPRINTF();
2612
2613 ret = snprintf( p, n, "\n%sthis update : " \
2614 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2615 crl->this_update.year, crl->this_update.mon,
2616 crl->this_update.day, crl->this_update.hour,
2617 crl->this_update.min, crl->this_update.sec );
2618 SAFE_SNPRINTF();
2619
2620 ret = snprintf( p, n, "\n%snext update : " \
2621 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2622 crl->next_update.year, crl->next_update.mon,
2623 crl->next_update.day, crl->next_update.hour,
2624 crl->next_update.min, crl->next_update.sec );
2625 SAFE_SNPRINTF();
2626
2627 entry = &crl->entry;
2628
2629 ret = snprintf( p, n, "\n%sRevoked certificates:",
2630 prefix );
2631 SAFE_SNPRINTF();
2632
Paul Bakker9be19372009-07-27 20:21:53 +00002633 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002634 {
2635 ret = snprintf( p, n, "\n%sserial number: ",
2636 prefix );
2637 SAFE_SNPRINTF();
2638
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002639 ret = x509parse_serial_gets( p, n, &entry->serial);
2640 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641
Paul Bakkerd98030e2009-05-02 15:13:40 +00002642 ret = snprintf( p, n, " revocation date: " \
2643 "%04d-%02d-%02d %02d:%02d:%02d",
2644 entry->revocation_date.year, entry->revocation_date.mon,
2645 entry->revocation_date.day, entry->revocation_date.hour,
2646 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002647 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002648
2649 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
2651
Paul Bakkerc70b9822013-04-07 22:00:46 +02002652 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002653 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Paul Bakkerc70b9822013-04-07 22:00:46 +02002655 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2656 if( ret != 0 )
2657 ret = snprintf( p, n, "???" );
2658 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002659 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002660 SAFE_SNPRINTF();
2661
Paul Bakker1e27bb22009-07-19 20:25:25 +00002662 ret = snprintf( p, n, "\n" );
2663 SAFE_SNPRINTF();
2664
Paul Bakker23986e52011-04-24 08:57:21 +00002665 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666}
2667
2668/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002669 * Return an informational string about the CSR.
2670 */
2671int x509parse_csr_info( char *buf, size_t size, const char *prefix,
2672 const x509_csr *csr )
2673{
2674 int ret;
2675 size_t n;
2676 char *p;
2677 const char *desc;
2678 char key_size_str[BEFORE_COLON];
2679
2680 p = buf;
2681 n = size;
2682
2683 ret = snprintf( p, n, "%sCSR version : %d",
2684 prefix, csr->version );
2685 SAFE_SNPRINTF();
2686
2687 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2688 SAFE_SNPRINTF();
2689 ret = x509parse_dn_gets( p, n, &csr->subject );
2690 SAFE_SNPRINTF();
2691
2692 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
2693 SAFE_SNPRINTF();
2694
2695 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
2696 if( ret != 0 )
2697 ret = snprintf( p, n, "???" );
2698 else
2699 ret = snprintf( p, n, "%s", desc );
2700 SAFE_SNPRINTF();
2701
2702 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
2703 pk_get_name( &csr->pk ) ) ) != 0 )
2704 {
2705 return( ret );
2706 }
2707
2708 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
2709 (int) pk_get_size( &csr->pk ) );
2710 SAFE_SNPRINTF();
2711
2712 return( (int) ( size - n ) );
2713}
2714
2715/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002716 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002718#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00002719int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720{
Paul Bakkercce9d772011-11-18 14:26:47 +00002721 int year, mon, day;
2722 int hour, min, sec;
2723
2724#if defined(_WIN32)
2725 SYSTEMTIME st;
2726
2727 GetLocalTime(&st);
2728
2729 year = st.wYear;
2730 mon = st.wMonth;
2731 day = st.wDay;
2732 hour = st.wHour;
2733 min = st.wMinute;
2734 sec = st.wSecond;
2735#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 struct tm *lt;
2737 time_t tt;
2738
2739 tt = time( NULL );
2740 lt = localtime( &tt );
2741
Paul Bakkercce9d772011-11-18 14:26:47 +00002742 year = lt->tm_year + 1900;
2743 mon = lt->tm_mon + 1;
2744 day = lt->tm_mday;
2745 hour = lt->tm_hour;
2746 min = lt->tm_min;
2747 sec = lt->tm_sec;
2748#endif
2749
2750 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002751 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Paul Bakkercce9d772011-11-18 14:26:47 +00002753 if( year == to->year &&
2754 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002755 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Paul Bakkercce9d772011-11-18 14:26:47 +00002757 if( year == to->year &&
2758 mon == to->mon &&
2759 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002760 return( 1 );
2761
Paul Bakkercce9d772011-11-18 14:26:47 +00002762 if( year == to->year &&
2763 mon == to->mon &&
2764 day == to->day &&
2765 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002766 return( 1 );
2767
Paul Bakkercce9d772011-11-18 14:26:47 +00002768 if( year == to->year &&
2769 mon == to->mon &&
2770 day == to->day &&
2771 hour == to->hour &&
2772 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002773 return( 1 );
2774
Paul Bakkercce9d772011-11-18 14:26:47 +00002775 if( year == to->year &&
2776 mon == to->mon &&
2777 day == to->day &&
2778 hour == to->hour &&
2779 min == to->min &&
2780 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002781 return( 1 );
2782
Paul Bakker40ea7de2009-05-03 10:18:48 +00002783 return( 0 );
2784}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002785#else /* POLARSSL_HAVE_TIME */
2786int x509parse_time_expired( const x509_time *to )
2787{
2788 ((void) to);
2789 return( 0 );
2790}
2791#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00002792
2793/*
2794 * Return 1 if the certificate is revoked, or 0 otherwise.
2795 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002796int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002797{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002798 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002799
2800 while( cur != NULL && cur->serial.len != 0 )
2801 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002802 if( crt->serial.len == cur->serial.len &&
2803 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002804 {
2805 if( x509parse_time_expired( &cur->revocation_date ) )
2806 return( 1 );
2807 }
2808
2809 cur = cur->next;
2810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
2812 return( 0 );
2813}
2814
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002815/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002816 * Check that the given certificate is valid accoring to the CRL.
2817 */
2818static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2819 x509_crl *crl_list)
2820{
2821 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002822 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2823 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002824
Paul Bakker915275b2012-09-28 07:10:55 +00002825 if( ca == NULL )
2826 return( flags );
2827
Paul Bakker76fd75a2011-01-16 21:12:10 +00002828 /*
2829 * TODO: What happens if no CRL is present?
2830 * Suggestion: Revocation state should be unknown if no CRL is present.
2831 * For backwards compatibility this is not yet implemented.
2832 */
2833
Paul Bakker915275b2012-09-28 07:10:55 +00002834 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002835 {
Paul Bakker915275b2012-09-28 07:10:55 +00002836 if( crl_list->version == 0 ||
2837 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002838 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2839 crl_list->issuer_raw.len ) != 0 )
2840 {
2841 crl_list = crl_list->next;
2842 continue;
2843 }
2844
2845 /*
2846 * Check if CRL is correctly signed by the trusted CA
2847 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002848 md_info = md_info_from_type( crl_list->sig_md );
2849 if( md_info == NULL )
2850 {
2851 /*
2852 * Cannot check 'unknown' hash
2853 */
2854 flags |= BADCRL_NOT_TRUSTED;
2855 break;
2856 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002857
Paul Bakkerc70b9822013-04-07 22:00:46 +02002858 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002859
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002860 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002861 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002862 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002863 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002864 flags |= BADCRL_NOT_TRUSTED;
2865 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002866 }
2867
2868 /*
2869 * Check for validity of CRL (Do not drop out)
2870 */
2871 if( x509parse_time_expired( &crl_list->next_update ) )
2872 flags |= BADCRL_EXPIRED;
2873
2874 /*
2875 * Check if certificate is revoked
2876 */
2877 if( x509parse_revoked(crt, crl_list) )
2878 {
2879 flags |= BADCERT_REVOKED;
2880 break;
2881 }
2882
2883 crl_list = crl_list->next;
2884 }
2885 return flags;
2886}
2887
Paul Bakkera5943852013-09-09 17:21:45 +02002888// Equal == 0, inequal == 1
2889static int x509_name_cmp( const void *s1, const void *s2, size_t len )
2890{
2891 size_t i;
2892 unsigned char diff;
2893 const unsigned char *n1 = s1, *n2 = s2;
2894
2895 for( i = 0; i < len; i++ )
2896 {
2897 diff = n1[i] ^ n2[i];
2898
2899 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
2900 continue;
2901
2902 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
2903 continue;
2904
2905 return( 1 );
2906 }
2907
2908 return( 0 );
2909}
2910
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002911static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002912{
2913 size_t i;
2914 size_t cn_idx = 0;
2915
Paul Bakker57b12982012-02-11 17:38:38 +00002916 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002917 return( 0 );
2918
2919 for( i = 0; i < strlen( cn ); ++i )
2920 {
2921 if( cn[i] == '.' )
2922 {
2923 cn_idx = i;
2924 break;
2925 }
2926 }
2927
2928 if( cn_idx == 0 )
2929 return( 0 );
2930
Paul Bakker535e97d2012-08-23 10:49:55 +00002931 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkera5943852013-09-09 17:21:45 +02002932 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002933 {
2934 return( 1 );
2935 }
2936
2937 return( 0 );
2938}
2939
Paul Bakker915275b2012-09-28 07:10:55 +00002940static int x509parse_verify_top(
2941 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002942 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002943 int (*f_vrfy)(void *, x509_cert *, int, int *),
2944 void *p_vrfy )
2945{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002946 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002947 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002948 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2949 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002950
2951 if( x509parse_time_expired( &child->valid_to ) )
2952 *flags |= BADCERT_EXPIRED;
2953
2954 /*
2955 * Child is the top of the chain. Check against the trust_ca list.
2956 */
2957 *flags |= BADCERT_NOT_TRUSTED;
2958
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02002959 md_info = md_info_from_type( child->sig_md );
2960 if( md_info == NULL )
2961 {
2962 /*
2963 * Cannot check 'unknown', no need to try any CA
2964 */
2965 trust_ca = NULL;
2966 }
2967 else
2968 md( md_info, child->tbs.p, child->tbs.len, hash );
2969
Paul Bakker915275b2012-09-28 07:10:55 +00002970 while( trust_ca != NULL )
2971 {
2972 if( trust_ca->version == 0 ||
2973 child->issuer_raw.len != trust_ca->subject_raw.len ||
2974 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2975 child->issuer_raw.len ) != 0 )
2976 {
2977 trust_ca = trust_ca->next;
2978 continue;
2979 }
2980
Paul Bakker9a736322012-11-14 12:39:52 +00002981 /*
2982 * Reduce path_len to check against if top of the chain is
2983 * the same as the trusted CA
2984 */
2985 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2986 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002987 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00002988 {
2989 check_path_cnt--;
2990 }
2991
Paul Bakker915275b2012-09-28 07:10:55 +00002992 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00002993 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00002994 {
2995 trust_ca = trust_ca->next;
2996 continue;
2997 }
2998
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002999 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003000 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003001 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003002 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003003 trust_ca = trust_ca->next;
3004 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003005 }
3006
3007 /*
3008 * Top of chain is signed by a trusted CA
3009 */
3010 *flags &= ~BADCERT_NOT_TRUSTED;
3011 break;
3012 }
3013
Paul Bakker9a736322012-11-14 12:39:52 +00003014 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003015 * If top of chain is not the same as the trusted CA send a verify request
3016 * to the callback for any issues with validity and CRL presence for the
3017 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003018 */
3019 if( trust_ca != NULL &&
3020 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3021 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3022 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003023 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003024 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003025 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3026
3027 if( x509parse_time_expired( &trust_ca->valid_to ) )
3028 ca_flags |= BADCERT_EXPIRED;
3029
Paul Bakker915275b2012-09-28 07:10:55 +00003030 if( NULL != f_vrfy )
3031 {
Paul Bakker9a736322012-11-14 12:39:52 +00003032 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003033 return( ret );
3034 }
3035 }
3036
3037 /* Call callback on top cert */
3038 if( NULL != f_vrfy )
3039 {
Paul Bakker9a736322012-11-14 12:39:52 +00003040 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003041 return( ret );
3042 }
3043
Paul Bakker915275b2012-09-28 07:10:55 +00003044 *flags |= ca_flags;
3045
3046 return( 0 );
3047}
3048
3049static int x509parse_verify_child(
3050 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003051 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003052 int (*f_vrfy)(void *, x509_cert *, int, int *),
3053 void *p_vrfy )
3054{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003055 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003056 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003057 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003058 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003059 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003060
3061 if( x509parse_time_expired( &child->valid_to ) )
3062 *flags |= BADCERT_EXPIRED;
3063
Paul Bakkerc70b9822013-04-07 22:00:46 +02003064 md_info = md_info_from_type( child->sig_md );
3065 if( md_info == NULL )
3066 {
3067 /*
3068 * Cannot check 'unknown' hash
3069 */
Paul Bakker915275b2012-09-28 07:10:55 +00003070 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003071 }
3072 else
3073 {
3074 md( md_info, child->tbs.p, child->tbs.len, hash );
3075
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003076 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003077 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003078 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003079 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003080 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003081 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003082 }
3083
Paul Bakker915275b2012-09-28 07:10:55 +00003084 /* Check trusted CA's CRL for the given crt */
3085 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3086
3087 grandparent = parent->next;
3088
3089 while( grandparent != NULL )
3090 {
3091 if( grandparent->version == 0 ||
3092 grandparent->ca_istrue == 0 ||
3093 parent->issuer_raw.len != grandparent->subject_raw.len ||
3094 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3095 parent->issuer_raw.len ) != 0 )
3096 {
3097 grandparent = grandparent->next;
3098 continue;
3099 }
3100 break;
3101 }
3102
Paul Bakker915275b2012-09-28 07:10:55 +00003103 if( grandparent != NULL )
3104 {
3105 /*
3106 * Part of the chain
3107 */
Paul Bakker9a736322012-11-14 12:39:52 +00003108 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 +00003109 if( ret != 0 )
3110 return( ret );
Paul Bakkera5943852013-09-09 17:21:45 +02003111 }
Paul Bakker915275b2012-09-28 07:10:55 +00003112 else
3113 {
Paul Bakker9a736322012-11-14 12:39:52 +00003114 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 +00003115 if( ret != 0 )
3116 return( ret );
3117 }
3118
3119 /* child is verified to be a child of the parent, call verify callback */
3120 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003121 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003122 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003123
3124 *flags |= parent_flags;
3125
3126 return( 0 );
3127}
3128
Paul Bakker76fd75a2011-01-16 21:12:10 +00003129/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003130 * Verify the certificate validity
3131 */
3132int x509parse_verify( x509_cert *crt,
3133 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003134 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003135 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003136 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003137 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003138{
Paul Bakker23986e52011-04-24 08:57:21 +00003139 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003140 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003141 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003142 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003143 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003144 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Paul Bakker40ea7de2009-05-03 10:18:48 +00003146 *flags = 0;
3147
Paul Bakker5121ce52009-01-03 21:22:43 +00003148 if( cn != NULL )
3149 {
3150 name = &crt->subject;
3151 cn_len = strlen( cn );
3152
Paul Bakker4d2c1242012-05-10 14:12:46 +00003153 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003155 cur = &crt->subject_alt_names;
3156
3157 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003158 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003159 if( cur->buf.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003160 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003161 break;
3162
Paul Bakker535e97d2012-08-23 10:49:55 +00003163 if( cur->buf.len > 2 &&
3164 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003165 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003166 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003167
Paul Bakker4d2c1242012-05-10 14:12:46 +00003168 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003169 }
3170
3171 if( cur == NULL )
3172 *flags |= BADCERT_CN_MISMATCH;
3173 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003174 else
3175 {
3176 while( name != NULL )
3177 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003178 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003179 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003180 if( name->val.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003181 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003182 break;
3183
Paul Bakker535e97d2012-08-23 10:49:55 +00003184 if( name->val.len > 2 &&
3185 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003186 x509_wildcard_verify( cn, &name->val ) )
3187 break;
3188 }
3189
3190 name = name->next;
3191 }
3192
3193 if( name == NULL )
3194 *flags |= BADCERT_CN_MISMATCH;
3195 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003196 }
3197
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003199 * Iterate upwards in the given cert chain, to find our crt parent.
3200 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003201 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003202 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003203
Paul Bakker76fd75a2011-01-16 21:12:10 +00003204 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003206 if( parent->ca_istrue == 0 ||
3207 crt->issuer_raw.len != parent->subject_raw.len ||
3208 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 crt->issuer_raw.len ) != 0 )
3210 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003211 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 continue;
3213 }
Paul Bakker915275b2012-09-28 07:10:55 +00003214 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 }
3216
Paul Bakker915275b2012-09-28 07:10:55 +00003217 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 {
Paul Bakker915275b2012-09-28 07:10:55 +00003219 /*
3220 * Part of the chain
3221 */
Paul Bakker9a736322012-11-14 12:39:52 +00003222 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003223 if( ret != 0 )
3224 return( ret );
3225 }
3226 else
Paul Bakker74111d32011-01-15 16:57:55 +00003227 {
Paul Bakker9a736322012-11-14 12:39:52 +00003228 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003229 if( ret != 0 )
3230 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003231 }
Paul Bakker915275b2012-09-28 07:10:55 +00003232
3233 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003234 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003235
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 return( 0 );
3237}
3238
3239/*
3240 * Unallocate all certificate data
3241 */
3242void x509_free( x509_cert *crt )
3243{
3244 x509_cert *cert_cur = crt;
3245 x509_cert *cert_prv;
3246 x509_name *name_cur;
3247 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003248 x509_sequence *seq_cur;
3249 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
3251 if( crt == NULL )
3252 return;
3253
3254 do
3255 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003256 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258 name_cur = cert_cur->issuer.next;
3259 while( name_cur != NULL )
3260 {
3261 name_prv = name_cur;
3262 name_cur = name_cur->next;
3263 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003264 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 }
3266
3267 name_cur = cert_cur->subject.next;
3268 while( name_cur != NULL )
3269 {
3270 name_prv = name_cur;
3271 name_cur = name_cur->next;
3272 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003273 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 }
3275
Paul Bakker74111d32011-01-15 16:57:55 +00003276 seq_cur = cert_cur->ext_key_usage.next;
3277 while( seq_cur != NULL )
3278 {
3279 seq_prv = seq_cur;
3280 seq_cur = seq_cur->next;
3281 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003282 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003283 }
3284
Paul Bakker8afa70d2012-02-11 18:42:45 +00003285 seq_cur = cert_cur->subject_alt_names.next;
3286 while( seq_cur != NULL )
3287 {
3288 seq_prv = seq_cur;
3289 seq_cur = seq_cur->next;
3290 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003291 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003292 }
3293
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 if( cert_cur->raw.p != NULL )
3295 {
3296 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003297 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 }
3299
3300 cert_cur = cert_cur->next;
3301 }
3302 while( cert_cur != NULL );
3303
3304 cert_cur = crt;
3305 do
3306 {
3307 cert_prv = cert_cur;
3308 cert_cur = cert_cur->next;
3309
3310 memset( cert_prv, 0, sizeof( x509_cert ) );
3311 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003312 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 }
3314 while( cert_cur != NULL );
3315}
3316
Paul Bakkerd98030e2009-05-02 15:13:40 +00003317/*
3318 * Unallocate all CRL data
3319 */
3320void x509_crl_free( x509_crl *crl )
3321{
3322 x509_crl *crl_cur = crl;
3323 x509_crl *crl_prv;
3324 x509_name *name_cur;
3325 x509_name *name_prv;
3326 x509_crl_entry *entry_cur;
3327 x509_crl_entry *entry_prv;
3328
3329 if( crl == NULL )
3330 return;
3331
3332 do
3333 {
3334 name_cur = crl_cur->issuer.next;
3335 while( name_cur != NULL )
3336 {
3337 name_prv = name_cur;
3338 name_cur = name_cur->next;
3339 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003340 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003341 }
3342
3343 entry_cur = crl_cur->entry.next;
3344 while( entry_cur != NULL )
3345 {
3346 entry_prv = entry_cur;
3347 entry_cur = entry_cur->next;
3348 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003349 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003350 }
3351
3352 if( crl_cur->raw.p != NULL )
3353 {
3354 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003355 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003356 }
3357
3358 crl_cur = crl_cur->next;
3359 }
3360 while( crl_cur != NULL );
3361
3362 crl_cur = crl;
3363 do
3364 {
3365 crl_prv = crl_cur;
3366 crl_cur = crl_cur->next;
3367
3368 memset( crl_prv, 0, sizeof( x509_crl ) );
3369 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003370 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003371 }
3372 while( crl_cur != NULL );
3373}
3374
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003375/*
3376 * Unallocate all CSR data
3377 */
3378void x509_csr_free( x509_csr *csr )
3379{
3380 x509_name *name_cur;
3381 x509_name *name_prv;
3382
3383 if( csr == NULL )
3384 return;
3385
3386 pk_free( &csr->pk );
3387
3388 name_cur = csr->subject.next;
3389 while( name_cur != NULL )
3390 {
3391 name_prv = name_cur;
3392 name_cur = name_cur->next;
3393 memset( name_prv, 0, sizeof( x509_name ) );
3394 polarssl_free( name_prv );
3395 }
3396
3397 if( csr->raw.p != NULL )
3398 {
3399 memset( csr->raw.p, 0, csr->raw.len );
3400 polarssl_free( csr->raw.p );
3401 }
3402
3403 memset( csr, 0, sizeof( x509_csr ) );
3404}
3405
Paul Bakker40e46942009-01-03 21:51:57 +00003406#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Paul Bakker40e46942009-01-03 21:51:57 +00003408#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
3410/*
3411 * Checkup routine
3412 */
3413int x509_self_test( int verbose )
3414{
Paul Bakker5690efc2011-05-26 13:16:06 +00003415#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003416 int ret;
3417 int flags;
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 x509_cert cacert;
3419 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003420 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00003421#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003422 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003424
3425 if( verbose != 0 )
3426 printf( " X.509 certificate load: " );
3427
3428 memset( &clicert, 0, sizeof( x509_cert ) );
3429
Paul Bakker3c2122f2013-06-24 19:03:14 +02003430 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003431 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 if( ret != 0 )
3433 {
3434 if( verbose != 0 )
3435 printf( "failed\n" );
3436
3437 return( ret );
3438 }
3439
3440 memset( &cacert, 0, sizeof( x509_cert ) );
3441
Paul Bakker3c2122f2013-06-24 19:03:14 +02003442 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003443 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 if( ret != 0 )
3445 {
3446 if( verbose != 0 )
3447 printf( "failed\n" );
3448
3449 return( ret );
3450 }
3451
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003452#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
3453 defined(POLARSSL_DES_C) && defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 if( verbose != 0 )
3455 printf( "passed\n X.509 private key load: " );
3456
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003457 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00003458
Paul Bakker1a7550a2013-09-15 13:01:22 +02003459 if( ( ret = pk_parse_key( &pkey,
3460 (const unsigned char *) test_ca_key,
3461 strlen( test_ca_key ),
3462 (const unsigned char *) test_ca_pwd,
3463 strlen( test_ca_pwd ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 {
3465 if( verbose != 0 )
3466 printf( "failed\n" );
3467
3468 return( ret );
3469 }
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003470#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
3472 if( verbose != 0 )
3473 printf( "passed\n X.509 signature verify: ");
3474
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003475 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 if( ret != 0 )
3477 {
3478 if( verbose != 0 )
3479 printf( "failed\n" );
3480
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003481 printf("ret = %d, &flags = %04x\n", ret, flags);
3482
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 return( ret );
3484 }
3485
Paul Bakker5690efc2011-05-26 13:16:06 +00003486#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003488 printf( "passed\n X.509 DHM parameter load: " );
3489
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003490 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
3491 strlen( test_dhm_params ) ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003492 {
3493 if( verbose != 0 )
3494 printf( "failed\n" );
3495
3496 return( ret );
3497 }
3498
3499 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 x509_free( &cacert );
3504 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003505 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00003506#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003507 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
3510 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003511#else
3512 ((void) verbose);
3513 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3514#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003515}
3516
3517#endif
3518
3519#endif