blob: 55938f95448418c305f9d336b8c5e33f52d67d8d [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{
1334 int ret, success = 0, first_error = 0, total_failed = 0;
1335 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 {
1358 pem_context pem;
1359
1360 while( buflen > 0 )
1361 {
1362 size_t use_len;
1363 pem_init( &pem );
1364
1365 ret = pem_read_buffer( &pem,
1366 "-----BEGIN CERTIFICATE-----",
1367 "-----END CERTIFICATE-----",
1368 buf, NULL, 0, &use_len );
1369
1370 if( ret == 0 )
1371 {
1372 /*
1373 * Was PEM encoded
1374 */
1375 buflen -= use_len;
1376 buf += use_len;
1377 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001378 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1379 {
1380 return( ret );
1381 }
Paul Bakker00b28602013-06-24 13:02:41 +02001382 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001383 {
1384 pem_free( &pem );
1385
Paul Bakker5ed3b342013-06-24 19:05:46 +02001386 /*
1387 * PEM header and footer were found
1388 */
1389 buflen -= use_len;
1390 buf += use_len;
1391
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001392 if( first_error == 0 )
1393 first_error = ret;
1394
1395 continue;
1396 }
1397 else
1398 break;
1399
Paul Bakker42c65812013-06-24 19:21:59 +02001400 ret = x509parse_crt_der( chain, pem.buf, pem.buflen );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001401
1402 pem_free( &pem );
1403
1404 if( ret != 0 )
1405 {
1406 /*
Paul Bakker42c65812013-06-24 19:21:59 +02001407 * Quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001408 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001409 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001410 return( ret );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001411
1412 if( first_error == 0 )
1413 first_error = ret;
1414
Paul Bakker42c65812013-06-24 19:21:59 +02001415 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001416 continue;
1417 }
1418
1419 success = 1;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001420 }
1421 }
1422#endif
1423
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001424 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001425 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001426 else if( first_error )
1427 return( first_error );
1428 else
1429 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430}
1431
1432/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001433 * Parse a CSR
1434 */
1435int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen )
1436{
1437 int ret;
1438 size_t len;
1439 unsigned char *p, *end;
1440#if defined(POLARSSL_PEM_C)
1441 size_t use_len;
1442 pem_context pem;
1443#endif
1444
1445 /*
1446 * Check for valid input
1447 */
1448 if( csr == NULL || buf == NULL )
1449 return( POLARSSL_ERR_X509_INVALID_INPUT );
1450
1451 memset( csr, 0, sizeof( x509_csr ) );
1452
1453#if defined(POLARSSL_PEM_C)
1454 pem_init( &pem );
1455 ret = pem_read_buffer( &pem,
1456 "-----BEGIN CERTIFICATE REQUEST-----",
1457 "-----END CERTIFICATE REQUEST-----",
1458 buf, NULL, 0, &use_len );
1459
1460 if( ret == 0 )
1461 {
1462 /*
1463 * Was PEM encoded
1464 */
1465 buflen -= use_len;
1466 buf += use_len;
1467
1468 /*
1469 * Steal PEM buffer
1470 */
1471 p = pem.buf;
1472 pem.buf = NULL;
1473 len = pem.buflen;
1474 pem_free( &pem );
1475 }
1476 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1477 {
1478 pem_free( &pem );
1479 return( ret );
1480 }
1481 else
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001482#endif
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001483 {
1484 /*
1485 * nope, copy the raw DER data
1486 */
1487 p = (unsigned char *) polarssl_malloc( len = buflen );
1488
1489 if( p == NULL )
1490 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1491
1492 memcpy( p, buf, buflen );
1493
1494 buflen = 0;
1495 }
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001496
1497 csr->raw.p = p;
1498 csr->raw.len = len;
1499 end = p + len;
1500
1501 /*
1502 * CertificationRequest ::= SEQUENCE {
1503 * certificationRequestInfo CertificationRequestInfo,
1504 * signatureAlgorithm AlgorithmIdentifier,
1505 * signature BIT STRING
1506 * }
1507 */
1508 if( ( ret = asn1_get_tag( &p, end, &len,
1509 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1510 {
1511 x509_csr_free( csr );
1512 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1513 }
1514
1515 if( len != (size_t) ( end - p ) )
1516 {
1517 x509_csr_free( csr );
1518 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1519 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1520 }
1521
1522 /*
1523 * CertificationRequestInfo ::= SEQUENCE {
1524 */
1525 csr->cri.p = p;
1526
1527 if( ( ret = asn1_get_tag( &p, end, &len,
1528 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1529 {
1530 x509_csr_free( csr );
1531 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1532 }
1533
1534 end = p + len;
1535 csr->cri.len = end - csr->cri.p;
1536
1537 /*
1538 * Version ::= INTEGER { v1(0) }
1539 */
1540 if( ( ret = x509_csr_get_version( &p, end, &csr->version ) ) != 0 )
1541 {
1542 x509_csr_free( csr );
1543 return( ret );
1544 }
1545
1546 csr->version++;
1547
1548 if( csr->version != 1 )
1549 {
1550 x509_csr_free( csr );
1551 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1552 }
1553
1554 /*
1555 * subject Name
1556 */
1557 csr->subject_raw.p = p;
1558
1559 if( ( ret = asn1_get_tag( &p, end, &len,
1560 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1561 {
1562 x509_csr_free( csr );
1563 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1564 }
1565
1566 if( ( ret = x509_get_name( &p, p + len, &csr->subject ) ) != 0 )
1567 {
1568 x509_csr_free( csr );
1569 return( ret );
1570 }
1571
1572 csr->subject_raw.len = p - csr->subject_raw.p;
1573
1574 /*
1575 * subjectPKInfo SubjectPublicKeyInfo
1576 */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577 if( ( ret = pk_parse_get_pubkey( &p, end, &csr->pk ) ) != 0 )
Paul Bakkerf9f377e2013-09-09 15:35:10 +02001578 {
1579 x509_csr_free( csr );
1580 return( ret );
1581 }
1582
1583 /*
1584 * attributes [0] Attributes
1585 */
1586 if( ( ret = asn1_get_tag( &p, end, &len,
1587 ASN1_CONSTRUCTED | ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1588 {
1589 x509_csr_free( csr );
1590 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
1591 }
1592 // TODO Parse Attributes / extension requests
1593
1594 p += len;
1595
1596 end = csr->raw.p + csr->raw.len;
1597
1598 /*
1599 * signatureAlgorithm AlgorithmIdentifier,
1600 * signature BIT STRING
1601 */
1602 if( ( ret = x509_get_alg_null( &p, end, &csr->sig_oid ) ) != 0 )
1603 {
1604 x509_csr_free( csr );
1605 return( ret );
1606 }
1607
1608 if( ( ret = x509_get_sig_alg( &csr->sig_oid, &csr->sig_md,
1609 &csr->sig_pk ) ) != 0 )
1610 {
1611 x509_csr_free( csr );
1612 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1613 }
1614
1615 if( ( ret = x509_get_sig( &p, end, &csr->sig ) ) != 0 )
1616 {
1617 x509_csr_free( csr );
1618 return( ret );
1619 }
1620
1621 if( p != end )
1622 {
1623 x509_csr_free( csr );
1624 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
1625 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1626 }
1627
1628 return( 0 );
1629}
1630
1631/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001632 * Parse one or more CRLs and add them to the chained list
1633 */
Paul Bakker23986e52011-04-24 08:57:21 +00001634int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635{
Paul Bakker23986e52011-04-24 08:57:21 +00001636 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001637 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001638 unsigned char *p, *end;
1639 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001640#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001641 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001642 pem_context pem;
1643#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001644
1645 crl = chain;
1646
1647 /*
1648 * Check for valid input
1649 */
1650 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001651 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001652
1653 while( crl->version != 0 && crl->next != NULL )
1654 crl = crl->next;
1655
1656 /*
1657 * Add new CRL on the end of the chain if needed.
1658 */
1659 if ( crl->version != 0 && crl->next == NULL)
1660 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001661 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662
Paul Bakker7d06ad22009-05-02 15:53:56 +00001663 if( crl->next == NULL )
1664 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001665 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001666 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001667 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001668
Paul Bakker7d06ad22009-05-02 15:53:56 +00001669 crl = crl->next;
1670 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001671 }
1672
Paul Bakker96743fc2011-02-12 14:30:57 +00001673#if defined(POLARSSL_PEM_C)
1674 pem_init( &pem );
1675 ret = pem_read_buffer( &pem,
1676 "-----BEGIN X509 CRL-----",
1677 "-----END X509 CRL-----",
1678 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001679
Paul Bakker96743fc2011-02-12 14:30:57 +00001680 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001682 /*
1683 * Was PEM encoded
1684 */
1685 buflen -= use_len;
1686 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687
1688 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001689 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001691 p = pem.buf;
1692 pem.buf = NULL;
1693 len = pem.buflen;
1694 pem_free( &pem );
1695 }
Paul Bakker00b28602013-06-24 13:02:41 +02001696 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001697 {
1698 pem_free( &pem );
1699 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001700 }
1701 else
Manuel Pégourié-Gonnard85dfe082013-09-10 15:59:02 +02001702#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001703 {
1704 /*
1705 * nope, copy the raw DER data
1706 */
Paul Bakker6e339b52013-07-03 13:37:05 +02001707 p = (unsigned char *) polarssl_malloc( len = buflen );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001708
1709 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001710 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711
1712 memcpy( p, buf, buflen );
1713
1714 buflen = 0;
1715 }
1716
1717 crl->raw.p = p;
1718 crl->raw.len = len;
1719 end = p + len;
1720
1721 /*
1722 * CertificateList ::= SEQUENCE {
1723 * tbsCertList TBSCertList,
1724 * signatureAlgorithm AlgorithmIdentifier,
1725 * signatureValue BIT STRING }
1726 */
1727 if( ( ret = asn1_get_tag( &p, end, &len,
1728 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1729 {
1730 x509_crl_free( crl );
1731 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1732 }
1733
Paul Bakker23986e52011-04-24 08:57:21 +00001734 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 {
1736 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001737 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1739 }
1740
1741 /*
1742 * TBSCertList ::= SEQUENCE {
1743 */
1744 crl->tbs.p = p;
1745
1746 if( ( ret = asn1_get_tag( &p, end, &len,
1747 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1748 {
1749 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001750 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001751 }
1752
1753 end = p + len;
1754 crl->tbs.len = end - crl->tbs.p;
1755
1756 /*
1757 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1758 * -- if present, MUST be v2
1759 *
1760 * signature AlgorithmIdentifier
1761 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001762 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001763 ( ret = x509_get_alg_null( &p, end, &crl->sig_oid1 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001764 {
1765 x509_crl_free( crl );
1766 return( ret );
1767 }
1768
1769 crl->version++;
1770
1771 if( crl->version > 2 )
1772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1775 }
1776
Paul Bakkerc70b9822013-04-07 22:00:46 +02001777 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1778 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779 {
1780 x509_crl_free( crl );
1781 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1782 }
1783
1784 /*
1785 * issuer Name
1786 */
1787 crl->issuer_raw.p = p;
1788
1789 if( ( ret = asn1_get_tag( &p, end, &len,
1790 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1791 {
1792 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001793 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001794 }
1795
1796 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1797 {
1798 x509_crl_free( crl );
1799 return( ret );
1800 }
1801
1802 crl->issuer_raw.len = p - crl->issuer_raw.p;
1803
1804 /*
1805 * thisUpdate Time
1806 * nextUpdate Time OPTIONAL
1807 */
Paul Bakker91200182010-02-18 21:26:15 +00001808 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001809 {
1810 x509_crl_free( crl );
1811 return( ret );
1812 }
1813
Paul Bakker91200182010-02-18 21:26:15 +00001814 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 {
Paul Bakker9d781402011-05-09 16:17:09 +00001816 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001817 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001818 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001819 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001820 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001821 x509_crl_free( crl );
1822 return( ret );
1823 }
1824 }
1825
1826 /*
1827 * revokedCertificates SEQUENCE OF SEQUENCE {
1828 * userCertificate CertificateSerialNumber,
1829 * revocationDate Time,
1830 * crlEntryExtensions Extensions OPTIONAL
1831 * -- if present, MUST be v2
1832 * } OPTIONAL
1833 */
1834 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1835 {
1836 x509_crl_free( crl );
1837 return( ret );
1838 }
1839
1840 /*
1841 * crlExtensions EXPLICIT Extensions OPTIONAL
1842 * -- if present, MUST be v2
1843 */
1844 if( crl->version == 2 )
1845 {
1846 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1847
1848 if( ret != 0 )
1849 {
1850 x509_crl_free( crl );
1851 return( ret );
1852 }
1853 }
1854
1855 if( p != end )
1856 {
1857 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001858 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1860 }
1861
1862 end = crl->raw.p + crl->raw.len;
1863
1864 /*
1865 * signatureAlgorithm AlgorithmIdentifier,
1866 * signatureValue BIT STRING
1867 */
Manuel Pégourié-Gonnarda1555132013-07-10 13:18:41 +02001868 if( ( ret = x509_get_alg_null( &p, end, &crl->sig_oid2 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001869 {
1870 x509_crl_free( crl );
1871 return( ret );
1872 }
1873
Paul Bakker535e97d2012-08-23 10:49:55 +00001874 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1875 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001876 {
1877 x509_crl_free( crl );
1878 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1879 }
1880
1881 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1882 {
1883 x509_crl_free( crl );
1884 return( ret );
1885 }
1886
1887 if( p != end )
1888 {
1889 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001890 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001891 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1892 }
1893
1894 if( buflen > 0 )
1895 {
Paul Bakker6e339b52013-07-03 13:37:05 +02001896 crl->next = (x509_crl *) polarssl_malloc( sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001897
Paul Bakker7d06ad22009-05-02 15:53:56 +00001898 if( crl->next == NULL )
1899 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001900 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001901 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001902 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001903
Paul Bakker7d06ad22009-05-02 15:53:56 +00001904 crl = crl->next;
1905 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001906
1907 return( x509parse_crl( crl, buf, buflen ) );
1908 }
1909
1910 return( 0 );
1911}
1912
Paul Bakker335db3f2011-04-25 15:28:35 +00001913#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001914/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001915 * Load all data from a file into a given buffer.
1916 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001917static int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001918{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001919 FILE *f;
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001920 long size;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001921
Paul Bakkerd98030e2009-05-02 15:13:40 +00001922 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001923 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001924
Paul Bakkerd98030e2009-05-02 15:13:40 +00001925 fseek( f, 0, SEEK_END );
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001926 if( ( size = ftell( f ) ) == -1 )
1927 {
1928 fclose( f );
1929 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1930 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001931 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001932
Paul Bakker42c3ccf2013-08-19 14:29:31 +02001933 *n = (size_t) size;
1934
Paul Bakker694d3ae2013-08-19 14:23:38 +02001935 if( *n + 1 == 0 ||
1936 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001937 {
1938 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001939 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001940 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001941
Paul Bakkerd98030e2009-05-02 15:13:40 +00001942 if( fread( *buf, 1, *n, f ) != *n )
1943 {
1944 fclose( f );
Paul Bakker6e339b52013-07-03 13:37:05 +02001945 polarssl_free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001946 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001947 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001948
Paul Bakkerd98030e2009-05-02 15:13:40 +00001949 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001950
Paul Bakkerd98030e2009-05-02 15:13:40 +00001951 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001952
Paul Bakkerd98030e2009-05-02 15:13:40 +00001953 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001954}
1955
1956/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 * Load one or more certificates and add them to the chained list
1958 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001959int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960{
1961 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 size_t n;
1963 unsigned char *buf;
1964
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02001965 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00001966 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
Paul Bakker69e095c2011-12-10 21:55:01 +00001968 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
1970 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02001971 polarssl_free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
1973 return( ret );
1974}
1975
Paul Bakker8d914582012-06-04 12:46:42 +00001976int x509parse_crtpath( x509_cert *chain, const char *path )
1977{
1978 int ret = 0;
1979#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001980 int w_ret;
1981 WCHAR szDir[MAX_PATH];
1982 char filename[MAX_PATH];
1983 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001984 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001985
Paul Bakker97872ac2012-11-02 12:53:26 +00001986 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001987 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001988
1989 if( len > MAX_PATH - 3 )
1990 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001991
Paul Bakker3338b792012-10-01 21:13:10 +00001992 memset( szDir, 0, sizeof(szDir) );
1993 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001994 memcpy( filename, path, len );
1995 filename[len++] = '\\';
1996 p = filename + len;
1997 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001998
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001999 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00002000
Paul Bakker97872ac2012-11-02 12:53:26 +00002001 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00002002 if (hFind == INVALID_HANDLE_VALUE)
2003 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2004
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002005 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00002006 do
2007 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002008 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00002009
Paul Bakkere4791f32012-06-04 21:29:15 +00002010 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00002011 continue;
2012
Paul Bakker3338b792012-10-01 21:13:10 +00002013 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
2014 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002015 p, len - 1,
2016 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00002017
Paul Bakker3338b792012-10-01 21:13:10 +00002018 w_ret = x509parse_crtfile( chain, filename );
2019 if( w_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002020 ret++;
2021 else
2022 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002023 }
Paul Bakker97872ac2012-11-02 12:53:26 +00002024 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00002025
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002026 if (GetLastError() != ERROR_NO_MORE_FILES)
2027 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00002028
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00002029cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00002030 FindClose( hFind );
2031#else
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002032 int t_ret, i;
2033 struct stat sb;
2034 struct dirent entry, *result = NULL;
Paul Bakker8d914582012-06-04 12:46:42 +00002035 char entry_name[255];
2036 DIR *dir = opendir( path );
2037
2038 if( dir == NULL)
2039 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
2040
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002041 while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
Paul Bakker8d914582012-06-04 12:46:42 +00002042 {
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002043 if( result == NULL )
2044 break;
2045
2046 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
2047
2048 i = stat( entry_name, &sb );
2049
2050 if( i == -1 )
Paul Bakker003dbad2013-09-09 17:26:14 +02002051 {
2052 closedir( dir );
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002053 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker003dbad2013-09-09 17:26:14 +02002054 }
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002055
2056 if( !S_ISREG( sb.st_mode ) )
Paul Bakker8d914582012-06-04 12:46:42 +00002057 continue;
2058
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002059 // Ignore parse errors
2060 //
Paul Bakker8d914582012-06-04 12:46:42 +00002061 t_ret = x509parse_crtfile( chain, entry_name );
2062 if( t_ret < 0 )
Paul Bakker2c8cdd22013-06-24 19:22:42 +02002063 ret++;
2064 else
2065 ret += t_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00002066 }
2067 closedir( dir );
2068#endif
2069
2070 return( ret );
2071}
2072
Paul Bakkerd98030e2009-05-02 15:13:40 +00002073/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002074 * Load a CSR into the structure
2075 */
2076int x509parse_csrfile( x509_csr *csr, const char *path )
2077{
2078 int ret;
2079 size_t n;
2080 unsigned char *buf;
2081
2082 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2083 return( ret );
2084
2085 ret = x509parse_csr( csr, buf, n );
2086
2087 memset( buf, 0, n + 1 );
2088 polarssl_free( buf );
2089
2090 return( ret );
2091}
2092
2093/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002094 * Load one or more CRLs and add them to the chained list
2095 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002096int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002097{
2098 int ret;
2099 size_t n;
2100 unsigned char *buf;
2101
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002102 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker69e095c2011-12-10 21:55:01 +00002103 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002104
Paul Bakker27fdf462011-06-09 13:55:13 +00002105 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002106
2107 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002108 polarssl_free( buf );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002109
2110 return( ret );
2111}
2112
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002113#if defined(POLARSSL_RSA_C)
2114/*
2115 * Load and parse a private RSA key
2116 */
2117int x509parse_keyfile_rsa( rsa_context *rsa, const char *path, const char *pwd )
2118{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002119 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002120 pk_context pk;
2121
2122 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002123
Paul Bakker1a7550a2013-09-15 13:01:22 +02002124 ret = pk_parse_keyfile( &pk, path, pwd );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002125
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002126 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2127 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2128
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002129 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002130 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002131 else
2132 rsa_free( rsa );
2133
2134 pk_free( &pk );
2135
2136 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002137}
2138
2139/*
2140 * Load and parse a public RSA key
2141 */
2142int x509parse_public_keyfile_rsa( rsa_context *rsa, const char *path )
2143{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002144 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002145 pk_context pk;
2146
2147 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002148
Paul Bakker1a7550a2013-09-15 13:01:22 +02002149 ret = pk_parse_public_keyfile( &pk, path );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002150
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002151 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2152 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2153
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002154 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002155 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002156 else
2157 rsa_free( rsa );
2158
2159 pk_free( &pk );
2160
2161 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002162}
2163#endif /* POLARSSL_RSA_C */
Paul Bakker335db3f2011-04-25 15:28:35 +00002164#endif /* POLARSSL_FS_IO */
2165
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002166#if defined(POLARSSL_RSA_C)
Paul Bakker335db3f2011-04-25 15:28:35 +00002167/*
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002168 * Parse a private RSA key
2169 */
2170int x509parse_key_rsa( rsa_context *rsa,
2171 const unsigned char *key, size_t keylen,
2172 const unsigned char *pwd, size_t pwdlen )
2173{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002174 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002175 pk_context pk;
2176
2177 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002178
Paul Bakker1a7550a2013-09-15 13:01:22 +02002179 ret = pk_parse_key( &pk, key, keylen, pwd, pwdlen );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002180
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002181 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2182 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2183
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002184 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002185 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002186 else
2187 rsa_free( rsa );
2188
2189 pk_free( &pk );
2190
2191 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002192}
2193
2194/*
2195 * Parse a public RSA key
2196 */
2197int x509parse_public_key_rsa( rsa_context *rsa,
2198 const unsigned char *key, size_t keylen )
2199{
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002200 int ret;
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002201 pk_context pk;
2202
2203 pk_init( &pk );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002204
Paul Bakker1a7550a2013-09-15 13:01:22 +02002205 ret = pk_parse_public_key( &pk, key, keylen );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002206
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02002207 if( ret == 0 && ! pk_can_do( &pk, POLARSSL_PK_RSA ) )
2208 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
2209
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002210 if( ret == 0 )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002211 rsa_copy( rsa, pk_rsa( pk ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02002212 else
2213 rsa_free( rsa );
2214
2215 pk_free( &pk );
2216
2217 return( ret );
Manuel Pégourié-Gonnardab2d9832013-07-11 16:17:23 +02002218}
2219#endif /* POLARSSL_RSA_C */
2220
Paul Bakkereaa89f82011-04-04 21:36:15 +00002221#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002222/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002223 * Parse DHM parameters
2224 */
Paul Bakker23986e52011-04-24 08:57:21 +00002225int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002226{
Paul Bakker23986e52011-04-24 08:57:21 +00002227 int ret;
2228 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002229 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002230#if defined(POLARSSL_PEM_C)
2231 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002232
Paul Bakker96743fc2011-02-12 14:30:57 +00002233 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002234
Paul Bakker96743fc2011-02-12 14:30:57 +00002235 ret = pem_read_buffer( &pem,
2236 "-----BEGIN DH PARAMETERS-----",
2237 "-----END DH PARAMETERS-----",
2238 dhmin, NULL, 0, &dhminlen );
2239
2240 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002241 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002242 /*
2243 * Was PEM encoded
2244 */
2245 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002246 }
Paul Bakker00b28602013-06-24 13:02:41 +02002247 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002248 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002249 pem_free( &pem );
2250 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002251 }
2252
Paul Bakker96743fc2011-02-12 14:30:57 +00002253 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2254#else
2255 p = (unsigned char *) dhmin;
2256#endif
2257 end = p + dhminlen;
2258
Paul Bakker1b57b062011-01-06 15:48:19 +00002259 memset( dhm, 0, sizeof( dhm_context ) );
2260
Paul Bakker1b57b062011-01-06 15:48:19 +00002261 /*
2262 * DHParams ::= SEQUENCE {
2263 * prime INTEGER, -- P
2264 * generator INTEGER, -- g
2265 * }
2266 */
2267 if( ( ret = asn1_get_tag( &p, end, &len,
2268 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2269 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002270#if defined(POLARSSL_PEM_C)
2271 pem_free( &pem );
2272#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02002273 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002274 }
2275
2276 end = p + len;
2277
2278 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2279 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2280 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002281#if defined(POLARSSL_PEM_C)
2282 pem_free( &pem );
2283#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002284 dhm_free( dhm );
Paul Bakker1a7550a2013-09-15 13:01:22 +02002285 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002286 }
2287
2288 if( p != end )
2289 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002290#if defined(POLARSSL_PEM_C)
2291 pem_free( &pem );
2292#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002293 dhm_free( dhm );
Paul Bakker1a7550a2013-09-15 13:01:22 +02002294 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002295 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2296 }
2297
Paul Bakker96743fc2011-02-12 14:30:57 +00002298#if defined(POLARSSL_PEM_C)
2299 pem_free( &pem );
2300#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002301
2302 return( 0 );
2303}
2304
Paul Bakker335db3f2011-04-25 15:28:35 +00002305#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002306/*
Manuel Pégourié-Gonnard4250a1f2013-06-27 13:00:00 +02002307 * Load and parse DHM parameters
Paul Bakker1b57b062011-01-06 15:48:19 +00002308 */
2309int x509parse_dhmfile( dhm_context *dhm, const char *path )
2310{
2311 int ret;
2312 size_t n;
2313 unsigned char *buf;
2314
Paul Bakker69e095c2011-12-10 21:55:01 +00002315 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2316 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002317
Paul Bakker27fdf462011-06-09 13:55:13 +00002318 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002319
2320 memset( buf, 0, n + 1 );
Paul Bakker6e339b52013-07-03 13:37:05 +02002321 polarssl_free( buf );
Paul Bakker1b57b062011-01-06 15:48:19 +00002322
2323 return( ret );
2324}
Paul Bakker335db3f2011-04-25 15:28:35 +00002325#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002326#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002327
Paul Bakker5121ce52009-01-03 21:22:43 +00002328#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002329#include <stdarg.h>
2330
2331#if !defined vsnprintf
2332#define vsnprintf _vsnprintf
2333#endif // vsnprintf
2334
2335/*
2336 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2337 * Result value is not size of buffer needed, but -1 if no fit is possible.
2338 *
2339 * This fuction tries to 'fix' this by at least suggesting enlarging the
2340 * size by 20.
2341 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002342static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002343{
2344 va_list ap;
2345 int res = -1;
2346
2347 va_start( ap, format );
2348
2349 res = vsnprintf( str, size, format, ap );
2350
2351 va_end( ap );
2352
2353 // No quick fix possible
2354 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002355 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002356
2357 return res;
2358}
2359
2360#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002361#endif
2362
Paul Bakkerd98030e2009-05-02 15:13:40 +00002363#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2364
2365#define SAFE_SNPRINTF() \
2366{ \
2367 if( ret == -1 ) \
2368 return( -1 ); \
2369 \
Paul Bakker23986e52011-04-24 08:57:21 +00002370 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 p[n - 1] = '\0'; \
2372 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2373 } \
2374 \
Paul Bakker23986e52011-04-24 08:57:21 +00002375 n -= (unsigned int) ret; \
2376 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002377}
2378
Paul Bakker5121ce52009-01-03 21:22:43 +00002379/*
2380 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002381 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002383int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002384{
Paul Bakker23986e52011-04-24 08:57:21 +00002385 int ret;
2386 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002388 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002389 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 char s[128], *p;
2391
2392 memset( s, 0, sizeof( s ) );
2393
2394 name = dn;
2395 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 while( name != NULL )
2399 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002400 if( !name->oid.p )
2401 {
2402 name = name->next;
2403 continue;
2404 }
2405
Paul Bakker74111d32011-01-15 16:57:55 +00002406 if( name != dn )
2407 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002408 ret = snprintf( p, n, ", " );
2409 SAFE_SNPRINTF();
2410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002411
Paul Bakkerc70b9822013-04-07 22:00:46 +02002412 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Paul Bakkerc70b9822013-04-07 22:00:46 +02002414 if( ret == 0 )
2415 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002417 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002418 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 for( i = 0; i < name->val.len; i++ )
2421 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002422 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 break;
2424
2425 c = name->val.p[i];
2426 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2427 s[i] = '?';
2428 else s[i] = c;
2429 }
2430 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002431 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002432 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002433 name = name->next;
2434 }
2435
Paul Bakker23986e52011-04-24 08:57:21 +00002436 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437}
2438
2439/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002440 * Store the serial in printable form into buf; no more
2441 * than size characters will be written
2442 */
2443int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2444{
Paul Bakker23986e52011-04-24 08:57:21 +00002445 int ret;
2446 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002447 char *p;
2448
2449 p = buf;
2450 n = size;
2451
2452 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002453 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002454
2455 for( i = 0; i < nr; i++ )
2456 {
Paul Bakker93048802011-12-05 14:38:06 +00002457 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002458 continue;
2459
Paul Bakkerdd476992011-01-16 21:34:59 +00002460 ret = snprintf( p, n, "%02X%s",
2461 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2462 SAFE_SNPRINTF();
2463 }
2464
Paul Bakker03c7c252011-11-25 12:37:37 +00002465 if( nr != serial->len )
2466 {
2467 ret = snprintf( p, n, "...." );
2468 SAFE_SNPRINTF();
2469 }
2470
Paul Bakker23986e52011-04-24 08:57:21 +00002471 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002472}
2473
2474/*
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002475 * Helper for writing "RSA key size", "EC key size", etc
2476 */
2477static int x509_key_size_helper( char *buf, size_t size, const char *name )
2478{
2479 char *p = buf;
2480 size_t n = size;
2481 int ret;
2482
2483 if( strlen( name ) + sizeof( " key size" ) > size )
2484 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;
2485
2486 ret = snprintf( p, n, "%s key size", name );
2487 SAFE_SNPRINTF();
2488
2489 return( 0 );
2490}
2491
2492/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002495#define BEFORE_COLON 14
2496#define BC "14"
Paul Bakkerff60ee62010-03-16 21:09:09 +00002497int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2498 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002499{
Paul Bakker23986e52011-04-24 08:57:21 +00002500 int ret;
2501 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002502 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002503 const char *desc = NULL;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002504 char key_size_str[BEFORE_COLON];
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
2506 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002507 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508
Paul Bakkerd98030e2009-05-02 15:13:40 +00002509 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002511 SAFE_SNPRINTF();
2512 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002514 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerdd476992011-01-16 21:34:59 +00002516 ret = x509parse_serial_gets( p, n, &crt->serial);
2517 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002518
Paul Bakkerd98030e2009-05-02 15:13:40 +00002519 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2520 SAFE_SNPRINTF();
2521 ret = x509parse_dn_gets( p, n, &crt->issuer );
2522 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Paul Bakkerd98030e2009-05-02 15:13:40 +00002524 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2525 SAFE_SNPRINTF();
2526 ret = x509parse_dn_gets( p, n, &crt->subject );
2527 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Paul Bakkerd98030e2009-05-02 15:13:40 +00002529 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2531 crt->valid_from.year, crt->valid_from.mon,
2532 crt->valid_from.day, crt->valid_from.hour,
2533 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002534 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakkerd98030e2009-05-02 15:13:40 +00002536 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2538 crt->valid_to.year, crt->valid_to.mon,
2539 crt->valid_to.day, crt->valid_to.hour,
2540 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002541 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
Paul Bakkerc70b9822013-04-07 22:00:46 +02002543 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002544 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Paul Bakkerc70b9822013-04-07 22:00:46 +02002546 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2547 if( ret != 0 )
2548 ret = snprintf( p, n, "???" );
2549 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002550 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002551 SAFE_SNPRINTF();
2552
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002553 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02002554 pk_get_name( &crt->pk ) ) ) != 0 )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +02002555 {
2556 return( ret );
2557 }
2558
2559 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002560 (int) pk_get_size( &crt->pk ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002561 SAFE_SNPRINTF();
2562
Paul Bakker23986e52011-04-24 08:57:21 +00002563 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002564}
2565
Paul Bakker74111d32011-01-15 16:57:55 +00002566/*
2567 * Return an informational string describing the given OID
2568 */
2569const char *x509_oid_get_description( x509_buf *oid )
2570{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002571 const char *desc = NULL;
2572 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002573
Paul Bakkerc70b9822013-04-07 22:00:46 +02002574 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002575
Paul Bakkerc70b9822013-04-07 22:00:46 +02002576 if( ret != 0 )
2577 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002578
Paul Bakkerc70b9822013-04-07 22:00:46 +02002579 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002580}
2581
2582/* Return the x.y.z.... style numeric string for the given OID */
2583int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2584{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002585 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002586}
2587
Paul Bakkerd98030e2009-05-02 15:13:40 +00002588/*
2589 * Return an informational string about the CRL.
2590 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002591int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2592 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002593{
Paul Bakker23986e52011-04-24 08:57:21 +00002594 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002595 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002596 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002597 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002598 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002599
2600 p = buf;
2601 n = size;
2602
2603 ret = snprintf( p, n, "%sCRL version : %d",
2604 prefix, crl->version );
2605 SAFE_SNPRINTF();
2606
2607 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2608 SAFE_SNPRINTF();
2609 ret = x509parse_dn_gets( p, n, &crl->issuer );
2610 SAFE_SNPRINTF();
2611
2612 ret = snprintf( p, n, "\n%sthis update : " \
2613 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2614 crl->this_update.year, crl->this_update.mon,
2615 crl->this_update.day, crl->this_update.hour,
2616 crl->this_update.min, crl->this_update.sec );
2617 SAFE_SNPRINTF();
2618
2619 ret = snprintf( p, n, "\n%snext update : " \
2620 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2621 crl->next_update.year, crl->next_update.mon,
2622 crl->next_update.day, crl->next_update.hour,
2623 crl->next_update.min, crl->next_update.sec );
2624 SAFE_SNPRINTF();
2625
2626 entry = &crl->entry;
2627
2628 ret = snprintf( p, n, "\n%sRevoked certificates:",
2629 prefix );
2630 SAFE_SNPRINTF();
2631
Paul Bakker9be19372009-07-27 20:21:53 +00002632 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002633 {
2634 ret = snprintf( p, n, "\n%sserial number: ",
2635 prefix );
2636 SAFE_SNPRINTF();
2637
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002638 ret = x509parse_serial_gets( p, n, &entry->serial);
2639 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640
Paul Bakkerd98030e2009-05-02 15:13:40 +00002641 ret = snprintf( p, n, " revocation date: " \
2642 "%04d-%02d-%02d %02d:%02d:%02d",
2643 entry->revocation_date.year, entry->revocation_date.mon,
2644 entry->revocation_date.day, entry->revocation_date.hour,
2645 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002646 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647
2648 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
2650
Paul Bakkerc70b9822013-04-07 22:00:46 +02002651 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002652 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakkerc70b9822013-04-07 22:00:46 +02002654 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2655 if( ret != 0 )
2656 ret = snprintf( p, n, "???" );
2657 else
Manuel Pégourié-Gonnard70f17682013-08-23 12:06:11 +02002658 ret = snprintf( p, n, "%s", desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659 SAFE_SNPRINTF();
2660
Paul Bakker1e27bb22009-07-19 20:25:25 +00002661 ret = snprintf( p, n, "\n" );
2662 SAFE_SNPRINTF();
2663
Paul Bakker23986e52011-04-24 08:57:21 +00002664 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665}
2666
2667/*
Paul Bakkerf9f377e2013-09-09 15:35:10 +02002668 * Return an informational string about the CSR.
2669 */
2670int x509parse_csr_info( char *buf, size_t size, const char *prefix,
2671 const x509_csr *csr )
2672{
2673 int ret;
2674 size_t n;
2675 char *p;
2676 const char *desc;
2677 char key_size_str[BEFORE_COLON];
2678
2679 p = buf;
2680 n = size;
2681
2682 ret = snprintf( p, n, "%sCSR version : %d",
2683 prefix, csr->version );
2684 SAFE_SNPRINTF();
2685
2686 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2687 SAFE_SNPRINTF();
2688 ret = x509parse_dn_gets( p, n, &csr->subject );
2689 SAFE_SNPRINTF();
2690
2691 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
2692 SAFE_SNPRINTF();
2693
2694 ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc );
2695 if( ret != 0 )
2696 ret = snprintf( p, n, "???" );
2697 else
2698 ret = snprintf( p, n, "%s", desc );
2699 SAFE_SNPRINTF();
2700
2701 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
2702 pk_get_name( &csr->pk ) ) ) != 0 )
2703 {
2704 return( ret );
2705 }
2706
2707 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
2708 (int) pk_get_size( &csr->pk ) );
2709 SAFE_SNPRINTF();
2710
2711 return( (int) ( size - n ) );
2712}
2713
2714/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002715 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 */
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002717#if defined(POLARSSL_HAVE_TIME)
Paul Bakkerff60ee62010-03-16 21:09:09 +00002718int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002719{
Paul Bakkercce9d772011-11-18 14:26:47 +00002720 int year, mon, day;
2721 int hour, min, sec;
2722
2723#if defined(_WIN32)
2724 SYSTEMTIME st;
2725
2726 GetLocalTime(&st);
2727
2728 year = st.wYear;
2729 mon = st.wMonth;
2730 day = st.wDay;
2731 hour = st.wHour;
2732 min = st.wMinute;
2733 sec = st.wSecond;
2734#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 struct tm *lt;
2736 time_t tt;
2737
2738 tt = time( NULL );
2739 lt = localtime( &tt );
2740
Paul Bakkercce9d772011-11-18 14:26:47 +00002741 year = lt->tm_year + 1900;
2742 mon = lt->tm_mon + 1;
2743 day = lt->tm_mday;
2744 hour = lt->tm_hour;
2745 min = lt->tm_min;
2746 sec = lt->tm_sec;
2747#endif
2748
2749 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002750 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
Paul Bakkercce9d772011-11-18 14:26:47 +00002752 if( year == to->year &&
2753 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002754 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002755
Paul Bakkercce9d772011-11-18 14:26:47 +00002756 if( year == to->year &&
2757 mon == to->mon &&
2758 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002759 return( 1 );
2760
Paul Bakkercce9d772011-11-18 14:26:47 +00002761 if( year == to->year &&
2762 mon == to->mon &&
2763 day == to->day &&
2764 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002765 return( 1 );
2766
Paul Bakkercce9d772011-11-18 14:26:47 +00002767 if( year == to->year &&
2768 mon == to->mon &&
2769 day == to->day &&
2770 hour == to->hour &&
2771 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002772 return( 1 );
2773
Paul Bakkercce9d772011-11-18 14:26:47 +00002774 if( year == to->year &&
2775 mon == to->mon &&
2776 day == to->day &&
2777 hour == to->hour &&
2778 min == to->min &&
2779 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002780 return( 1 );
2781
Paul Bakker40ea7de2009-05-03 10:18:48 +00002782 return( 0 );
2783}
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002784#else /* POLARSSL_HAVE_TIME */
2785int x509parse_time_expired( const x509_time *to )
2786{
2787 ((void) to);
2788 return( 0 );
2789}
2790#endif /* POLARSSL_HAVE_TIME */
Paul Bakker40ea7de2009-05-03 10:18:48 +00002791
2792/*
2793 * Return 1 if the certificate is revoked, or 0 otherwise.
2794 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002795int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002796{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002797 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002798
2799 while( cur != NULL && cur->serial.len != 0 )
2800 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002801 if( crt->serial.len == cur->serial.len &&
2802 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002803 {
2804 if( x509parse_time_expired( &cur->revocation_date ) )
2805 return( 1 );
2806 }
2807
2808 cur = cur->next;
2809 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 return( 0 );
2812}
2813
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002814/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002815 * Check that the given certificate is valid accoring to the CRL.
2816 */
2817static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2818 x509_crl *crl_list)
2819{
2820 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002821 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2822 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002823
Paul Bakker915275b2012-09-28 07:10:55 +00002824 if( ca == NULL )
2825 return( flags );
2826
Paul Bakker76fd75a2011-01-16 21:12:10 +00002827 /*
2828 * TODO: What happens if no CRL is present?
2829 * Suggestion: Revocation state should be unknown if no CRL is present.
2830 * For backwards compatibility this is not yet implemented.
2831 */
2832
Paul Bakker915275b2012-09-28 07:10:55 +00002833 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002834 {
Paul Bakker915275b2012-09-28 07:10:55 +00002835 if( crl_list->version == 0 ||
2836 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002837 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2838 crl_list->issuer_raw.len ) != 0 )
2839 {
2840 crl_list = crl_list->next;
2841 continue;
2842 }
2843
2844 /*
2845 * Check if CRL is correctly signed by the trusted CA
2846 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002847 md_info = md_info_from_type( crl_list->sig_md );
2848 if( md_info == NULL )
2849 {
2850 /*
2851 * Cannot check 'unknown' hash
2852 */
2853 flags |= BADCRL_NOT_TRUSTED;
2854 break;
2855 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002856
Paul Bakkerc70b9822013-04-07 22:00:46 +02002857 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002858
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002859 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002860 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002861 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002862 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002863 flags |= BADCRL_NOT_TRUSTED;
2864 break;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002865 }
2866
2867 /*
2868 * Check for validity of CRL (Do not drop out)
2869 */
2870 if( x509parse_time_expired( &crl_list->next_update ) )
2871 flags |= BADCRL_EXPIRED;
2872
2873 /*
2874 * Check if certificate is revoked
2875 */
2876 if( x509parse_revoked(crt, crl_list) )
2877 {
2878 flags |= BADCERT_REVOKED;
2879 break;
2880 }
2881
2882 crl_list = crl_list->next;
2883 }
2884 return flags;
2885}
2886
Paul Bakkera5943852013-09-09 17:21:45 +02002887// Equal == 0, inequal == 1
2888static int x509_name_cmp( const void *s1, const void *s2, size_t len )
2889{
2890 size_t i;
2891 unsigned char diff;
2892 const unsigned char *n1 = s1, *n2 = s2;
2893
2894 for( i = 0; i < len; i++ )
2895 {
2896 diff = n1[i] ^ n2[i];
2897
2898 if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
2899 continue;
2900
2901 if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
2902 continue;
2903
2904 return( 1 );
2905 }
2906
2907 return( 0 );
2908}
2909
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002910static int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002911{
2912 size_t i;
2913 size_t cn_idx = 0;
2914
Paul Bakker57b12982012-02-11 17:38:38 +00002915 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002916 return( 0 );
2917
2918 for( i = 0; i < strlen( cn ); ++i )
2919 {
2920 if( cn[i] == '.' )
2921 {
2922 cn_idx = i;
2923 break;
2924 }
2925 }
2926
2927 if( cn_idx == 0 )
2928 return( 0 );
2929
Paul Bakker535e97d2012-08-23 10:49:55 +00002930 if( strlen( cn ) - cn_idx == name->len - 1 &&
Paul Bakkera5943852013-09-09 17:21:45 +02002931 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002932 {
2933 return( 1 );
2934 }
2935
2936 return( 0 );
2937}
2938
Paul Bakker915275b2012-09-28 07:10:55 +00002939static int x509parse_verify_top(
2940 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002941 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002942 int (*f_vrfy)(void *, x509_cert *, int, int *),
2943 void *p_vrfy )
2944{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002945 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002946 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002947 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2948 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002949
2950 if( x509parse_time_expired( &child->valid_to ) )
2951 *flags |= BADCERT_EXPIRED;
2952
2953 /*
2954 * Child is the top of the chain. Check against the trust_ca list.
2955 */
2956 *flags |= BADCERT_NOT_TRUSTED;
2957
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02002958 md_info = md_info_from_type( child->sig_md );
2959 if( md_info == NULL )
2960 {
2961 /*
2962 * Cannot check 'unknown', no need to try any CA
2963 */
2964 trust_ca = NULL;
2965 }
2966 else
2967 md( md_info, child->tbs.p, child->tbs.len, hash );
2968
Paul Bakker915275b2012-09-28 07:10:55 +00002969 while( trust_ca != NULL )
2970 {
2971 if( trust_ca->version == 0 ||
2972 child->issuer_raw.len != trust_ca->subject_raw.len ||
2973 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2974 child->issuer_raw.len ) != 0 )
2975 {
2976 trust_ca = trust_ca->next;
2977 continue;
2978 }
2979
Paul Bakker9a736322012-11-14 12:39:52 +00002980 /*
2981 * Reduce path_len to check against if top of the chain is
2982 * the same as the trusted CA
2983 */
2984 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2985 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02002986 child->issuer_raw.len ) == 0 )
Paul Bakker9a736322012-11-14 12:39:52 +00002987 {
2988 check_path_cnt--;
2989 }
2990
Paul Bakker915275b2012-09-28 07:10:55 +00002991 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00002992 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00002993 {
2994 trust_ca = trust_ca->next;
2995 continue;
2996 }
2997
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002998 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02002999 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003000 child->sig.p, child->sig.len ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003001 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003002 trust_ca = trust_ca->next;
3003 continue;
Paul Bakker915275b2012-09-28 07:10:55 +00003004 }
3005
3006 /*
3007 * Top of chain is signed by a trusted CA
3008 */
3009 *flags &= ~BADCERT_NOT_TRUSTED;
3010 break;
3011 }
3012
Paul Bakker9a736322012-11-14 12:39:52 +00003013 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003014 * If top of chain is not the same as the trusted CA send a verify request
3015 * to the callback for any issues with validity and CRL presence for the
3016 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003017 */
3018 if( trust_ca != NULL &&
3019 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3020 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3021 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003022 {
Manuel Pégourié-Gonnardcffe4a62013-08-23 16:47:30 +02003023 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakker915275b2012-09-28 07:10:55 +00003024 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3025
3026 if( x509parse_time_expired( &trust_ca->valid_to ) )
3027 ca_flags |= BADCERT_EXPIRED;
3028
Paul Bakker915275b2012-09-28 07:10:55 +00003029 if( NULL != f_vrfy )
3030 {
Paul Bakker9a736322012-11-14 12:39:52 +00003031 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003032 return( ret );
3033 }
3034 }
3035
3036 /* Call callback on top cert */
3037 if( NULL != f_vrfy )
3038 {
Paul Bakker9a736322012-11-14 12:39:52 +00003039 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003040 return( ret );
3041 }
3042
Paul Bakker915275b2012-09-28 07:10:55 +00003043 *flags |= ca_flags;
3044
3045 return( 0 );
3046}
3047
3048static int x509parse_verify_child(
3049 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003050 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003051 int (*f_vrfy)(void *, x509_cert *, int, int *),
3052 void *p_vrfy )
3053{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003054 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003055 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003056 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003057 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003058 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003059
3060 if( x509parse_time_expired( &child->valid_to ) )
3061 *flags |= BADCERT_EXPIRED;
3062
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063 md_info = md_info_from_type( child->sig_md );
3064 if( md_info == NULL )
3065 {
3066 /*
3067 * Cannot check 'unknown' hash
3068 */
Paul Bakker915275b2012-09-28 07:10:55 +00003069 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003070 }
3071 else
3072 {
3073 md( md_info, child->tbs.p, child->tbs.len, hash );
3074
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003075 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02003076 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003077 child->sig.p, child->sig.len ) != 0 )
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003078 {
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +02003079 *flags |= BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003080 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003081 }
3082
Paul Bakker915275b2012-09-28 07:10:55 +00003083 /* Check trusted CA's CRL for the given crt */
3084 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3085
3086 grandparent = parent->next;
3087
3088 while( grandparent != NULL )
3089 {
3090 if( grandparent->version == 0 ||
3091 grandparent->ca_istrue == 0 ||
3092 parent->issuer_raw.len != grandparent->subject_raw.len ||
3093 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3094 parent->issuer_raw.len ) != 0 )
3095 {
3096 grandparent = grandparent->next;
3097 continue;
3098 }
3099 break;
3100 }
3101
Paul Bakker915275b2012-09-28 07:10:55 +00003102 if( grandparent != NULL )
3103 {
3104 /*
3105 * Part of the chain
3106 */
Paul Bakker9a736322012-11-14 12:39:52 +00003107 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 +00003108 if( ret != 0 )
3109 return( ret );
Paul Bakkera5943852013-09-09 17:21:45 +02003110 }
Paul Bakker915275b2012-09-28 07:10:55 +00003111 else
3112 {
Paul Bakker9a736322012-11-14 12:39:52 +00003113 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 +00003114 if( ret != 0 )
3115 return( ret );
3116 }
3117
3118 /* child is verified to be a child of the parent, call verify callback */
3119 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003120 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003121 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003122
3123 *flags |= parent_flags;
3124
3125 return( 0 );
3126}
3127
Paul Bakker76fd75a2011-01-16 21:12:10 +00003128/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 * Verify the certificate validity
3130 */
3131int x509parse_verify( x509_cert *crt,
3132 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003133 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003134 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003135 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003136 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003137{
Paul Bakker23986e52011-04-24 08:57:21 +00003138 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003139 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003140 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003141 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003142 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003143 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
Paul Bakker40ea7de2009-05-03 10:18:48 +00003145 *flags = 0;
3146
Paul Bakker5121ce52009-01-03 21:22:43 +00003147 if( cn != NULL )
3148 {
3149 name = &crt->subject;
3150 cn_len = strlen( cn );
3151
Paul Bakker4d2c1242012-05-10 14:12:46 +00003152 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003154 cur = &crt->subject_alt_names;
3155
3156 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003157 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003158 if( cur->buf.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003159 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003160 break;
3161
Paul Bakker535e97d2012-08-23 10:49:55 +00003162 if( cur->buf.len > 2 &&
3163 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003164 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003165 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Paul Bakker4d2c1242012-05-10 14:12:46 +00003167 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003168 }
3169
3170 if( cur == NULL )
3171 *flags |= BADCERT_CN_MISMATCH;
3172 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003173 else
3174 {
3175 while( name != NULL )
3176 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003177 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003178 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003179 if( name->val.len == cn_len &&
Paul Bakkera5943852013-09-09 17:21:45 +02003180 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003181 break;
3182
Paul Bakker535e97d2012-08-23 10:49:55 +00003183 if( name->val.len > 2 &&
3184 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003185 x509_wildcard_verify( cn, &name->val ) )
3186 break;
3187 }
3188
3189 name = name->next;
3190 }
3191
3192 if( name == NULL )
3193 *flags |= BADCERT_CN_MISMATCH;
3194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 }
3196
Paul Bakker5121ce52009-01-03 21:22:43 +00003197 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003198 * Iterate upwards in the given cert chain, to find our crt parent.
3199 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003201 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003202
Paul Bakker76fd75a2011-01-16 21:12:10 +00003203 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003205 if( parent->ca_istrue == 0 ||
3206 crt->issuer_raw.len != parent->subject_raw.len ||
3207 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 crt->issuer_raw.len ) != 0 )
3209 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003210 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 continue;
3212 }
Paul Bakker915275b2012-09-28 07:10:55 +00003213 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 }
3215
Paul Bakker915275b2012-09-28 07:10:55 +00003216 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 {
Paul Bakker915275b2012-09-28 07:10:55 +00003218 /*
3219 * Part of the chain
3220 */
Paul Bakker9a736322012-11-14 12:39:52 +00003221 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003222 if( ret != 0 )
3223 return( ret );
3224 }
3225 else
Paul Bakker74111d32011-01-15 16:57:55 +00003226 {
Paul Bakker9a736322012-11-14 12:39:52 +00003227 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003228 if( ret != 0 )
3229 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003230 }
Paul Bakker915275b2012-09-28 07:10:55 +00003231
3232 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003233 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003234
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 return( 0 );
3236}
3237
3238/*
3239 * Unallocate all certificate data
3240 */
3241void x509_free( x509_cert *crt )
3242{
3243 x509_cert *cert_cur = crt;
3244 x509_cert *cert_prv;
3245 x509_name *name_cur;
3246 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003247 x509_sequence *seq_cur;
3248 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
3250 if( crt == NULL )
3251 return;
3252
3253 do
3254 {
Manuel Pégourié-Gonnard674b2242013-07-10 14:32:58 +02003255 pk_free( &cert_cur->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
3257 name_cur = cert_cur->issuer.next;
3258 while( name_cur != NULL )
3259 {
3260 name_prv = name_cur;
3261 name_cur = name_cur->next;
3262 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003263 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 }
3265
3266 name_cur = cert_cur->subject.next;
3267 while( name_cur != NULL )
3268 {
3269 name_prv = name_cur;
3270 name_cur = name_cur->next;
3271 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003272 polarssl_free( name_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 }
3274
Paul Bakker74111d32011-01-15 16:57:55 +00003275 seq_cur = cert_cur->ext_key_usage.next;
3276 while( seq_cur != NULL )
3277 {
3278 seq_prv = seq_cur;
3279 seq_cur = seq_cur->next;
3280 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003281 polarssl_free( seq_prv );
Paul Bakker74111d32011-01-15 16:57:55 +00003282 }
3283
Paul Bakker8afa70d2012-02-11 18:42:45 +00003284 seq_cur = cert_cur->subject_alt_names.next;
3285 while( seq_cur != NULL )
3286 {
3287 seq_prv = seq_cur;
3288 seq_cur = seq_cur->next;
3289 memset( seq_prv, 0, sizeof( x509_sequence ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003290 polarssl_free( seq_prv );
Paul Bakker8afa70d2012-02-11 18:42:45 +00003291 }
3292
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 if( cert_cur->raw.p != NULL )
3294 {
3295 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003296 polarssl_free( cert_cur->raw.p );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
3299 cert_cur = cert_cur->next;
3300 }
3301 while( cert_cur != NULL );
3302
3303 cert_cur = crt;
3304 do
3305 {
3306 cert_prv = cert_cur;
3307 cert_cur = cert_cur->next;
3308
3309 memset( cert_prv, 0, sizeof( x509_cert ) );
3310 if( cert_prv != crt )
Paul Bakker6e339b52013-07-03 13:37:05 +02003311 polarssl_free( cert_prv );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 }
3313 while( cert_cur != NULL );
3314}
3315
Paul Bakkerd98030e2009-05-02 15:13:40 +00003316/*
3317 * Unallocate all CRL data
3318 */
3319void x509_crl_free( x509_crl *crl )
3320{
3321 x509_crl *crl_cur = crl;
3322 x509_crl *crl_prv;
3323 x509_name *name_cur;
3324 x509_name *name_prv;
3325 x509_crl_entry *entry_cur;
3326 x509_crl_entry *entry_prv;
3327
3328 if( crl == NULL )
3329 return;
3330
3331 do
3332 {
3333 name_cur = crl_cur->issuer.next;
3334 while( name_cur != NULL )
3335 {
3336 name_prv = name_cur;
3337 name_cur = name_cur->next;
3338 memset( name_prv, 0, sizeof( x509_name ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003339 polarssl_free( name_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003340 }
3341
3342 entry_cur = crl_cur->entry.next;
3343 while( entry_cur != NULL )
3344 {
3345 entry_prv = entry_cur;
3346 entry_cur = entry_cur->next;
3347 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
Paul Bakker6e339b52013-07-03 13:37:05 +02003348 polarssl_free( entry_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003349 }
3350
3351 if( crl_cur->raw.p != NULL )
3352 {
3353 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
Paul Bakker6e339b52013-07-03 13:37:05 +02003354 polarssl_free( crl_cur->raw.p );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003355 }
3356
3357 crl_cur = crl_cur->next;
3358 }
3359 while( crl_cur != NULL );
3360
3361 crl_cur = crl;
3362 do
3363 {
3364 crl_prv = crl_cur;
3365 crl_cur = crl_cur->next;
3366
3367 memset( crl_prv, 0, sizeof( x509_crl ) );
3368 if( crl_prv != crl )
Paul Bakker6e339b52013-07-03 13:37:05 +02003369 polarssl_free( crl_prv );
Paul Bakkerd98030e2009-05-02 15:13:40 +00003370 }
3371 while( crl_cur != NULL );
3372}
3373
Paul Bakkerf9f377e2013-09-09 15:35:10 +02003374/*
3375 * Unallocate all CSR data
3376 */
3377void x509_csr_free( x509_csr *csr )
3378{
3379 x509_name *name_cur;
3380 x509_name *name_prv;
3381
3382 if( csr == NULL )
3383 return;
3384
3385 pk_free( &csr->pk );
3386
3387 name_cur = csr->subject.next;
3388 while( name_cur != NULL )
3389 {
3390 name_prv = name_cur;
3391 name_cur = name_cur->next;
3392 memset( name_prv, 0, sizeof( x509_name ) );
3393 polarssl_free( name_prv );
3394 }
3395
3396 if( csr->raw.p != NULL )
3397 {
3398 memset( csr->raw.p, 0, csr->raw.len );
3399 polarssl_free( csr->raw.p );
3400 }
3401
3402 memset( csr, 0, sizeof( x509_csr ) );
3403}
3404
Paul Bakker40e46942009-01-03 21:51:57 +00003405#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003406
Paul Bakker40e46942009-01-03 21:51:57 +00003407#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
3409/*
3410 * Checkup routine
3411 */
3412int x509_self_test( int verbose )
3413{
Paul Bakker5690efc2011-05-26 13:16:06 +00003414#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003415 int ret;
3416 int flags;
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 x509_cert cacert;
3418 x509_cert clicert;
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003419 pk_context pkey;
Paul Bakker5690efc2011-05-26 13:16:06 +00003420#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003421 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003422#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003423
3424 if( verbose != 0 )
3425 printf( " X.509 certificate load: " );
3426
3427 memset( &clicert, 0, sizeof( x509_cert ) );
3428
Paul Bakker3c2122f2013-06-24 19:03:14 +02003429 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003430 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( ret != 0 )
3432 {
3433 if( verbose != 0 )
3434 printf( "failed\n" );
3435
3436 return( ret );
3437 }
3438
3439 memset( &cacert, 0, sizeof( x509_cert ) );
3440
Paul Bakker3c2122f2013-06-24 19:03:14 +02003441 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003442 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 if( ret != 0 )
3444 {
3445 if( verbose != 0 )
3446 printf( "failed\n" );
3447
3448 return( ret );
3449 }
3450
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003451#if defined(POLARSSL_MD5_C) && defined(POLARSSL_CIPHER_MODE_CBC) && \
3452 defined(POLARSSL_DES_C) && defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 if( verbose != 0 )
3454 printf( "passed\n X.509 private key load: " );
3455
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003456 pk_init( &pkey );
Paul Bakker66b78b22011-03-25 14:22:50 +00003457
Paul Bakker1a7550a2013-09-15 13:01:22 +02003458 if( ( ret = pk_parse_key( &pkey,
3459 (const unsigned char *) test_ca_key,
3460 strlen( test_ca_key ),
3461 (const unsigned char *) test_ca_pwd,
3462 strlen( test_ca_pwd ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 {
3464 if( verbose != 0 )
3465 printf( "failed\n" );
3466
3467 return( ret );
3468 }
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003470
3471 if( verbose != 0 )
3472 printf( "passed\n X.509 signature verify: ");
3473
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003474 ret = x509parse_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 if( ret != 0 )
3476 {
3477 if( verbose != 0 )
3478 printf( "failed\n" );
3479
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003480 printf("ret = %d, &flags = %04x\n", ret, flags);
3481
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 return( ret );
3483 }
3484
Paul Bakker5690efc2011-05-26 13:16:06 +00003485#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003487 printf( "passed\n X.509 DHM parameter load: " );
3488
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +02003489 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
3490 strlen( test_dhm_params ) ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003491 {
3492 if( verbose != 0 )
3493 printf( "failed\n" );
3494
3495 return( ret );
3496 }
3497
3498 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003500#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003501
3502 x509_free( &cacert );
3503 x509_free( &clicert );
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02003504 pk_free( &pkey );
Paul Bakker5690efc2011-05-26 13:16:06 +00003505#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003506 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003507#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
3509 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003510#else
3511 ((void) verbose);
3512 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003514}
3515
3516#endif
3517
3518#endif