blob: 23c57eaf9be5472008f6d98000a4659542ae7337 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, 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 Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010046#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010048#endif
49#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010051#endif
52#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010054#endif
55#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010057#endif
58#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000059#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010060#endif
61#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000062#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010063#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000064#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66#include <string.h>
67#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000068#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000069#include <windows.h>
70#else
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Paul Bakker335db3f2011-04-25 15:28:35 +000074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000076#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000077#include <sys/types.h>
78#include <dirent.h>
79#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000080#endif
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082/*
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
84 */
85static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000086 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000087 int *ver )
88{
Paul Bakker23986e52011-04-24 08:57:21 +000089 int ret;
90 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( ( ret = asn1_get_tag( p, end, &len,
93 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
94 {
Paul Bakker40e46942009-01-03 21:51:57 +000095 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000096 {
97 *ver = 0;
98 return( 0 );
99 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101 return( ret );
102 }
103
104 end = *p + len;
105
106 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000107 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000110 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000111 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 return( 0 );
114}
115
116/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000117 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000118 */
119static int x509_crl_get_version( unsigned char **p,
120 const unsigned char *end,
121 int *ver )
122{
123 int ret;
124
125 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
126 {
127 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000128 {
129 *ver = 0;
130 return( 0 );
131 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000132
133 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
134 }
135
136 return( 0 );
137}
138
139/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * CertificateSerialNumber ::= INTEGER
141 */
142static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 x509_buf *serial )
145{
146 int ret;
147
148 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
153 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000154 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000155 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->tag = *(*p)++;
158
159 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000160 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162 serial->p = *p;
163 *p += serial->len;
164
165 return( 0 );
166}
167
168/*
169 * AlgorithmIdentifier ::= SEQUENCE {
170 * algorithm OBJECT IDENTIFIER,
171 * parameters ANY DEFINED BY algorithm OPTIONAL }
172 */
173static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 x509_buf *alg )
176{
Paul Bakker23986e52011-04-24 08:57:21 +0000177 int ret;
178 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 if( ( ret = asn1_get_tag( p, end, &len,
181 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000182 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 end = *p + len;
185 alg->tag = **p;
186
187 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000188 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 alg->p = *p;
191 *p += alg->len;
192
193 if( *p == end )
194 return( 0 );
195
196 /*
197 * assume the algorithm parameters must be NULL
198 */
199 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000200 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000203 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000204 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 return( 0 );
207}
208
209/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * AttributeTypeAndValue ::= SEQUENCE {
211 * type AttributeType,
212 * value AttributeValue }
213 *
214 * AttributeType ::= OBJECT IDENTIFIER
215 *
216 * AttributeValue ::= ANY DEFINED BY AttributeType
217 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000218static int x509_get_attr_type_value( unsigned char **p,
219 const unsigned char *end,
220 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Paul Bakker23986e52011-04-24 08:57:21 +0000222 int ret;
223 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 x509_buf *oid;
225 x509_buf *val;
226
227 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000229 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 oid = &cur->oid;
232 oid->tag = **p;
233
234 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000235 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 oid->p = *p;
238 *p += oid->len;
239
240 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000241 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000242 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
245 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
246 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000247 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000248 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 val = &cur->val;
251 val->tag = *(*p)++;
252
253 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000254 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256 val->p = *p;
257 *p += val->len;
258
259 cur->next = NULL;
260
Paul Bakker400ff6f2011-02-20 10:40:16 +0000261 return( 0 );
262}
263
264/*
265 * RelativeDistinguishedName ::=
266 * SET OF AttributeTypeAndValue
267 *
268 * AttributeTypeAndValue ::= SEQUENCE {
269 * type AttributeType,
270 * value AttributeValue }
271 *
272 * AttributeType ::= OBJECT IDENTIFIER
273 *
274 * AttributeValue ::= ANY DEFINED BY AttributeType
275 */
276static int x509_get_name( unsigned char **p,
277 const unsigned char *end,
278 x509_name *cur )
279{
Paul Bakker23986e52011-04-24 08:57:21 +0000280 int ret;
281 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000282 const unsigned char *end2;
283 x509_name *use;
284
285 if( ( ret = asn1_get_tag( p, end, &len,
286 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000287 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
289 end2 = end;
290 end = *p + len;
291 use = cur;
292
293 do
294 {
295 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
296 return( ret );
297
298 if( *p != end )
299 {
300 use->next = (x509_name *) malloc(
301 sizeof( x509_name ) );
302
303 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000304 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000305
306 memset( use->next, 0, sizeof( x509_name ) );
307
308 use = use->next;
309 }
310 }
311 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 /*
314 * recurse until end of SEQUENCE is reached
315 */
316 if( *p == end2 )
317 return( 0 );
318
319 cur->next = (x509_name *) malloc(
320 sizeof( x509_name ) );
321
322 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000323 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Paul Bakker430ffbe2012-05-01 08:14:20 +0000325 memset( cur->next, 0, sizeof( x509_name ) );
326
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 return( x509_get_name( p, end2, cur->next ) );
328}
329
330/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * Time ::= CHOICE {
332 * utcTime UTCTime,
333 * generalTime GeneralizedTime }
334 */
Paul Bakker91200182010-02-18 21:26:15 +0000335static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000336 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337 x509_time *time )
338{
Paul Bakker23986e52011-04-24 08:57:21 +0000339 int ret;
340 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000342 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343
Paul Bakker91200182010-02-18 21:26:15 +0000344 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000345 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
346 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker91200182010-02-18 21:26:15 +0000348 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker91200182010-02-18 21:26:15 +0000350 if ( tag == ASN1_UTC_TIME )
351 {
352 (*p)++;
353 ret = asn1_get_len( p, end, &len );
354
355 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000356 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357
Paul Bakker91200182010-02-18 21:26:15 +0000358 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000359 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
360 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker91200182010-02-18 21:26:15 +0000362 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
363 &time->year, &time->mon, &time->day,
364 &time->hour, &time->min, &time->sec ) < 5 )
365 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000366
Paul Bakker400ff6f2011-02-20 10:40:16 +0000367 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000368 time->year += 1900;
369
370 *p += len;
371
372 return( 0 );
373 }
374 else if ( tag == ASN1_GENERALIZED_TIME )
375 {
376 (*p)++;
377 ret = asn1_get_len( p, end, &len );
378
379 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000380 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000381
382 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000383 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
384 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000385
386 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
387 &time->year, &time->mon, &time->day,
388 &time->hour, &time->min, &time->sec ) < 5 )
389 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
390
391 *p += len;
392
393 return( 0 );
394 }
395 else
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000397}
398
399
400/*
401 * Validity ::= SEQUENCE {
402 * notBefore Time,
403 * notAfter Time }
404 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000405static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000406 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 x509_time *from,
408 x509_time *to )
409{
Paul Bakker23986e52011-04-24 08:57:21 +0000410 int ret;
411 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 if( ( ret = asn1_get_tag( p, end, &len,
414 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000415 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417 end = *p + len;
418
Paul Bakker91200182010-02-18 21:26:15 +0000419 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000420 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Paul Bakker91200182010-02-18 21:26:15 +0000422 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000423 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000426 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000427 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 return( 0 );
430}
431
432/*
433 * SubjectPublicKeyInfo ::= SEQUENCE {
434 * algorithm AlgorithmIdentifier,
435 * subjectPublicKey BIT STRING }
436 */
437static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000438 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 x509_buf *pk_alg_oid,
440 mpi *N, mpi *E )
441{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200442 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000443 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 unsigned char *end2;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200445 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
448 return( ret );
449
450 /*
451 * only RSA public keys handled at this time
452 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200453 if( oid_get_pk_alg( pk_alg_oid, &pk_alg ) != 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000454 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000460 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000461 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 end2 = *p + len;
464
465 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * RSAPublicKey ::= SEQUENCE {
470 * modulus INTEGER, -- n
471 * publicExponent INTEGER -- e
472 * }
473 */
474 if( ( ret = asn1_get_tag( p, end2, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000480 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
483 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_buf *sig )
496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret;
498 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
Paul Bakker8afa70d2012-02-11 18:42:45 +0000500 if( ( end - *p ) < 1 )
501 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
502 POLARSSL_ERR_ASN1_OUT_OF_DATA );
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 sig->tag = **p;
505
506 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Paul Bakker74111d32011-01-15 16:57:55 +0000509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000511 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 sig->len = len;
514 sig->p = *p;
515
516 *p += len;
517
518 return( 0 );
519}
520
521/*
522 * X.509 v2/v3 unique identifier (not parsed)
523 */
524static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000525 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 x509_buf *uid, int n )
527{
528 int ret;
529
530 if( *p == end )
531 return( 0 );
532
533 uid->tag = **p;
534
535 if( ( ret = asn1_get_tag( p, end, &uid->len,
536 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
537 {
Paul Bakker40e46942009-01-03 21:51:57 +0000538 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 return( 0 );
540
541 return( ret );
542 }
543
544 uid->p = *p;
545 *p += uid->len;
546
547 return( 0 );
548}
549
550/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000551 * X.509 Extensions (No parsing of extensions, pointer should
552 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
554static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000556 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000557{
Paul Bakker23986e52011-04-24 08:57:21 +0000558 int ret;
559 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 if( *p == end )
562 return( 0 );
563
564 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000567 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 ext->p = *p;
571 end = *p + ext->len;
572
573 /*
574 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
575 *
576 * Extension ::= SEQUENCE {
577 * extnID OBJECT IDENTIFIER,
578 * critical BOOLEAN DEFAULT FALSE,
579 * extnValue OCTET STRING }
580 */
581 if( ( ret = asn1_get_tag( p, end, &len,
582 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000583 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000586 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000587 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Paul Bakkerd98030e2009-05-02 15:13:40 +0000589 return( 0 );
590}
591
592/*
593 * X.509 CRL v2 extensions (no extensions parsed yet.)
594 */
595static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000596 const unsigned char *end,
597 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000598{
Paul Bakker23986e52011-04-24 08:57:21 +0000599 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000600 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000601
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000602 /* Get explicit tag */
603 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000604 {
605 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
606 return( 0 );
607
608 return( ret );
609 }
610
611 while( *p < end )
612 {
613 if( ( ret = asn1_get_tag( p, end, &len,
614 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000615 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000616
617 *p += len;
618 }
619
620 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000621 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000622 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
623
624 return( 0 );
625}
626
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000627/*
628 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
629 */
630static int x509_get_crl_entry_ext( unsigned char **p,
631 const unsigned char *end,
632 x509_buf *ext )
633{
634 int ret;
635 size_t len = 0;
636
637 /* OPTIONAL */
638 if (end <= *p)
639 return( 0 );
640
641 ext->tag = **p;
642 ext->p = *p;
643
644 /*
645 * Get CRL-entry extension sequence header
646 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
647 */
648 if( ( ret = asn1_get_tag( p, end, &ext->len,
649 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
650 {
651 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
652 {
653 ext->p = NULL;
654 return( 0 );
655 }
656 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
657 }
658
659 end = *p + ext->len;
660
661 if( end != *p + ext->len )
662 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
663 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
664
665 while( *p < end )
666 {
667 if( ( ret = asn1_get_tag( p, end, &len,
668 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
669 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
670
671 *p += len;
672 }
673
674 if( *p != end )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
676 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
677
678 return( 0 );
679}
680
Paul Bakker74111d32011-01-15 16:57:55 +0000681static int x509_get_basic_constraints( unsigned char **p,
682 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000683 int *ca_istrue,
684 int *max_pathlen )
685{
Paul Bakker23986e52011-04-24 08:57:21 +0000686 int ret;
687 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000688
689 /*
690 * BasicConstraints ::= SEQUENCE {
691 * cA BOOLEAN DEFAULT FALSE,
692 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
693 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000694 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000695 *max_pathlen = 0; /* endless */
696
697 if( ( ret = asn1_get_tag( p, end, &len,
698 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000699 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000700
701 if( *p == end )
702 return 0;
703
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000705 {
706 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000707 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000708
709 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000710 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000711
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 if( *ca_istrue != 0 )
713 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000714 }
715
716 if( *p == end )
717 return 0;
718
719 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
722 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000723 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000724 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
725
726 (*max_pathlen)++;
727
Paul Bakker74111d32011-01-15 16:57:55 +0000728 return 0;
729}
730
731static int x509_get_ns_cert_type( unsigned char **p,
732 const unsigned char *end,
733 unsigned char *ns_cert_type)
734{
735 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000736 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000737
738 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000739 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000740
741 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000742 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000743 POLARSSL_ERR_ASN1_INVALID_LENGTH );
744
745 /* Get actual bitstring */
746 *ns_cert_type = *bs.p;
747 return 0;
748}
749
750static int x509_get_key_usage( unsigned char **p,
751 const unsigned char *end,
752 unsigned char *key_usage)
753{
754 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000755 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000756
757 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000758 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000759
Paul Bakker94a67962012-08-23 13:03:52 +0000760 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000761 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000762 POLARSSL_ERR_ASN1_INVALID_LENGTH );
763
764 /* Get actual bitstring */
765 *key_usage = *bs.p;
766 return 0;
767}
768
Paul Bakkerd98030e2009-05-02 15:13:40 +0000769/*
Paul Bakker74111d32011-01-15 16:57:55 +0000770 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
771 *
772 * KeyPurposeId ::= OBJECT IDENTIFIER
773 */
774static int x509_get_ext_key_usage( unsigned char **p,
775 const unsigned char *end,
776 x509_sequence *ext_key_usage)
777{
778 int ret;
779
780 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000782
783 /* Sequence length must be >= 1 */
784 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000785 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000786 POLARSSL_ERR_ASN1_INVALID_LENGTH );
787
788 return 0;
789}
790
791/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000792 * SubjectAltName ::= GeneralNames
793 *
794 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
795 *
796 * GeneralName ::= CHOICE {
797 * otherName [0] OtherName,
798 * rfc822Name [1] IA5String,
799 * dNSName [2] IA5String,
800 * x400Address [3] ORAddress,
801 * directoryName [4] Name,
802 * ediPartyName [5] EDIPartyName,
803 * uniformResourceIdentifier [6] IA5String,
804 * iPAddress [7] OCTET STRING,
805 * registeredID [8] OBJECT IDENTIFIER }
806 *
807 * OtherName ::= SEQUENCE {
808 * type-id OBJECT IDENTIFIER,
809 * value [0] EXPLICIT ANY DEFINED BY type-id }
810 *
811 * EDIPartyName ::= SEQUENCE {
812 * nameAssigner [0] DirectoryString OPTIONAL,
813 * partyName [1] DirectoryString }
814 *
815 * NOTE: PolarSSL only parses and uses dNSName at this point.
816 */
817static int x509_get_subject_alt_name( unsigned char **p,
818 const unsigned char *end,
819 x509_sequence *subject_alt_name )
820{
821 int ret;
822 size_t len, tag_len;
823 asn1_buf *buf;
824 unsigned char tag;
825 asn1_sequence *cur = subject_alt_name;
826
827 /* Get main sequence tag */
828 if( ( ret = asn1_get_tag( p, end, &len,
829 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
830 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
831
832 if( *p + len != end )
833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
834 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
835
836 while( *p < end )
837 {
838 if( ( end - *p ) < 1 )
839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
840 POLARSSL_ERR_ASN1_OUT_OF_DATA );
841
842 tag = **p;
843 (*p)++;
844 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
846
847 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
849 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
850
851 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
852 {
853 *p += tag_len;
854 continue;
855 }
856
857 buf = &(cur->buf);
858 buf->tag = tag;
859 buf->p = *p;
860 buf->len = tag_len;
861 *p += buf->len;
862
863 /* Allocate and assign next pointer */
864 if (*p < end)
865 {
866 cur->next = (asn1_sequence *) malloc(
867 sizeof( asn1_sequence ) );
868
869 if( cur->next == NULL )
870 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
871 POLARSSL_ERR_ASN1_MALLOC_FAILED );
872
Paul Bakker535e97d2012-08-23 10:49:55 +0000873 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000874 cur = cur->next;
875 }
876 }
877
878 /* Set final sequence entry's next pointer to NULL */
879 cur->next = NULL;
880
881 if( *p != end )
882 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
883 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
884
885 return( 0 );
886}
887
888/*
Paul Bakker74111d32011-01-15 16:57:55 +0000889 * X.509 v3 extensions
890 *
891 * TODO: Perform all of the basic constraints tests required by the RFC
892 * TODO: Set values for undetected extensions to a sane default?
893 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000894 */
895static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000896 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000897 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000898{
Paul Bakker23986e52011-04-24 08:57:21 +0000899 int ret;
900 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000901 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000902
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000903 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000904 {
905 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
906 return( 0 );
907
908 return( ret );
909 }
910
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 while( *p < end )
912 {
Paul Bakker74111d32011-01-15 16:57:55 +0000913 /*
914 * Extension ::= SEQUENCE {
915 * extnID OBJECT IDENTIFIER,
916 * critical BOOLEAN DEFAULT FALSE,
917 * extnValue OCTET STRING }
918 */
919 x509_buf extn_oid = {0, 0, NULL};
920 int is_critical = 0; /* DEFAULT FALSE */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200921 int ext_type = 0;
Paul Bakker74111d32011-01-15 16:57:55 +0000922
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 if( ( ret = asn1_get_tag( p, end, &len,
924 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000925 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000927 end_ext_data = *p + len;
928
Paul Bakker74111d32011-01-15 16:57:55 +0000929 /* Get extension ID */
930 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakker74111d32011-01-15 16:57:55 +0000932 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000933 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Paul Bakker74111d32011-01-15 16:57:55 +0000935 extn_oid.p = *p;
936 *p += extn_oid.len;
937
938 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000939 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000940 POLARSSL_ERR_ASN1_OUT_OF_DATA );
941
942 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000943 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000944 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000945 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Paul Bakker74111d32011-01-15 16:57:55 +0000947 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000949 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000950 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000952 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000953
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000954 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000955 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000956 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Paul Bakker74111d32011-01-15 16:57:55 +0000958 /*
959 * Detect supported extensions
960 */
Paul Bakkerc70b9822013-04-07 22:00:46 +0200961 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
962
963 if( ret != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000964 {
965 /* No parser found, skip extension */
966 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Paul Bakker5c721f92011-07-27 16:51:09 +0000968#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000969 if( is_critical )
970 {
971 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000972 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000973 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
974 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000975#endif
Paul Bakkerc70b9822013-04-07 22:00:46 +0200976 continue;
977 }
978
979 crt->ext_types |= ext_type;
980
981 switch( ext_type )
982 {
983 case EXT_BASIC_CONSTRAINTS:
984 /* Parse basic constraints */
985 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
986 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
987 return ( ret );
988 break;
989
990 case EXT_KEY_USAGE:
991 /* Parse key usage */
992 if( ( ret = x509_get_key_usage( p, end_ext_octet,
993 &crt->key_usage ) ) != 0 )
994 return ( ret );
995 break;
996
997 case EXT_EXTENDED_KEY_USAGE:
998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
1001 return ( ret );
1002 break;
1003
1004 case EXT_SUBJECT_ALT_NAME:
1005 /* Parse subject alt name */
1006 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1007 &crt->subject_alt_names ) ) != 0 )
1008 return ( ret );
1009 break;
1010
1011 case EXT_NS_CERT_TYPE:
1012 /* Parse netscape certificate type */
1013 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1014 &crt->ns_cert_type ) ) != 0 )
1015 return ( ret );
1016 break;
1017
1018 default:
1019 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker74111d32011-01-15 16:57:55 +00001020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001021 }
1022
1023 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001024 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001025 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker5121ce52009-01-03 21:22:43 +00001027 return( 0 );
1028}
1029
1030/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001031 * X.509 CRL Entries
1032 */
1033static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001034 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001035 x509_crl_entry *entry )
1036{
Paul Bakker23986e52011-04-24 08:57:21 +00001037 int ret;
1038 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001039 x509_crl_entry *cur_entry = entry;
1040
1041 if( *p == end )
1042 return( 0 );
1043
Paul Bakker9be19372009-07-27 20:21:53 +00001044 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001045 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1046 {
1047 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1048 return( 0 );
1049
1050 return( ret );
1051 }
1052
Paul Bakker9be19372009-07-27 20:21:53 +00001053 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001054
1055 while( *p < end )
1056 {
Paul Bakker23986e52011-04-24 08:57:21 +00001057 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001058 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001059
1060 if( ( ret = asn1_get_tag( p, end, &len2,
1061 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1062 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001063 return( ret );
1064 }
1065
Paul Bakker9be19372009-07-27 20:21:53 +00001066 cur_entry->raw.tag = **p;
1067 cur_entry->raw.p = *p;
1068 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001069 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001070
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001071 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001072 return( ret );
1073
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001074 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075 return( ret );
1076
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001077 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001078 return( ret );
1079
Paul Bakker74111d32011-01-15 16:57:55 +00001080 if ( *p < end )
1081 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001083
1084 if( cur_entry->next == NULL )
1085 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1086
Paul Bakkerd98030e2009-05-02 15:13:40 +00001087 cur_entry = cur_entry->next;
1088 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1089 }
1090 }
1091
1092 return( 0 );
1093}
1094
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095static int x509_get_sig_alg( const x509_buf *sig_oid, md_type_t *md_alg,
1096 pk_type_t *pk_alg )
Paul Bakker27d66162010-03-17 06:56:01 +00001097{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001098 int ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg );
Paul Bakker27d66162010-03-17 06:56:01 +00001099
Paul Bakkerc70b9822013-04-07 22:00:46 +02001100 if( ret != 0 )
1101 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG + ret );
Paul Bakker27d66162010-03-17 06:56:01 +00001102
Paul Bakkerc70b9822013-04-07 22:00:46 +02001103 return( 0 );
Paul Bakker27d66162010-03-17 06:56:01 +00001104}
1105
Paul Bakkerd98030e2009-05-02 15:13:40 +00001106/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001107 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001109int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110{
Paul Bakker23986e52011-04-24 08:57:21 +00001111 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001112 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001113 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001114
Paul Bakker320a4b52009-03-28 18:52:39 +00001115 /*
1116 * Check for valid input
1117 */
1118 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001119 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001120
Paul Bakker96743fc2011-02-12 14:30:57 +00001121 p = (unsigned char *) malloc( len = buflen );
1122
1123 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001124 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001125
1126 memcpy( p, buf, buflen );
1127
1128 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001129
1130 crt->raw.p = p;
1131 crt->raw.len = len;
1132 end = p + len;
1133
1134 /*
1135 * Certificate ::= SEQUENCE {
1136 * tbsCertificate TBSCertificate,
1137 * signatureAlgorithm AlgorithmIdentifier,
1138 * signatureValue BIT STRING }
1139 */
1140 if( ( ret = asn1_get_tag( &p, end, &len,
1141 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1142 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001143 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001144 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 }
1146
Paul Bakkerb00ca422012-09-25 12:10:00 +00001147 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001149 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001150 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001151 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001152 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001153 crt_end = p + len;
1154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 /*
1156 * TBSCertificate ::= SEQUENCE {
1157 */
1158 crt->tbs.p = p;
1159
1160 if( ( ret = asn1_get_tag( &p, end, &len,
1161 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1162 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001163 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001164 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
1166
1167 end = p + len;
1168 crt->tbs.len = end - crt->tbs.p;
1169
1170 /*
1171 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1172 *
1173 * CertificateSerialNumber ::= INTEGER
1174 *
1175 * signature AlgorithmIdentifier
1176 */
1177 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1178 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1179 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1180 {
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->version++;
1186
1187 if( crt->version > 3 )
1188 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001189 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001190 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
1192
Paul Bakkerc70b9822013-04-07 22:00:46 +02001193 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
1194 &crt->sig_pk ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001196 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001197 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 }
1199
1200 /*
1201 * issuer Name
1202 */
1203 crt->issuer_raw.p = p;
1204
1205 if( ( ret = asn1_get_tag( &p, end, &len,
1206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1207 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001208 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001209 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
1212 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1213 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001214 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 return( ret );
1216 }
1217
1218 crt->issuer_raw.len = p - crt->issuer_raw.p;
1219
1220 /*
1221 * Validity ::= SEQUENCE {
1222 * notBefore Time,
1223 * notAfter Time }
1224 *
1225 */
1226 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1227 &crt->valid_to ) ) != 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 * subject Name
1235 */
1236 crt->subject_raw.p = p;
1237
1238 if( ( ret = asn1_get_tag( &p, end, &len,
1239 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1240 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001241 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001242 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 }
1244
Paul Bakkercefb3962012-06-27 11:51:09 +00001245 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001247 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 return( ret );
1249 }
1250
1251 crt->subject_raw.len = p - crt->subject_raw.p;
1252
1253 /*
1254 * SubjectPublicKeyInfo ::= SEQUENCE
1255 * algorithm AlgorithmIdentifier,
1256 * subjectPublicKey BIT STRING }
1257 */
1258 if( ( ret = asn1_get_tag( &p, end, &len,
1259 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1260 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001261 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001262 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
1264
1265 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1266 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1267 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001268 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 return( ret );
1270 }
1271
1272 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 return( ret );
1276 }
1277
1278 crt->rsa.len = mpi_size( &crt->rsa.N );
1279
1280 /*
1281 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1282 * -- If present, version shall be v2 or v3
1283 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1284 * -- If present, version shall be v2 or v3
1285 * extensions [3] EXPLICIT Extensions OPTIONAL
1286 * -- If present, version shall be v3
1287 */
1288 if( crt->version == 2 || crt->version == 3 )
1289 {
1290 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1291 if( ret != 0 )
1292 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001293 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 return( ret );
1295 }
1296 }
1297
1298 if( crt->version == 2 || crt->version == 3 )
1299 {
1300 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1301 if( ret != 0 )
1302 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001303 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001304 return( ret );
1305 }
1306 }
1307
1308 if( crt->version == 3 )
1309 {
Paul Bakker74111d32011-01-15 16:57:55 +00001310 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 if( ret != 0 )
1312 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001314 return( ret );
1315 }
1316 }
1317
1318 if( p != end )
1319 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001320 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001321 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001322 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
1324
Paul Bakkerb00ca422012-09-25 12:10:00 +00001325 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001326
1327 /*
1328 * signatureAlgorithm AlgorithmIdentifier,
1329 * signatureValue BIT STRING
1330 */
1331 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1332 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001333 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 return( ret );
1335 }
1336
Paul Bakker535e97d2012-08-23 10:49:55 +00001337 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1338 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001340 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001341 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 }
1343
1344 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1345 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001346 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 return( ret );
1348 }
1349
1350 if( p != end )
1351 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001352 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001353 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001354 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 }
1356
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001357 return( 0 );
1358}
1359
1360/*
1361 * Parse one or more PEM certificates from a buffer and add them to the chained list
1362 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001363int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001364{
Paul Bakker69e095c2011-12-10 21:55:01 +00001365 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001366 x509_cert *crt, *prev = NULL;
1367 int buf_format = X509_FORMAT_DER;
1368
1369 crt = chain;
1370
1371 /*
1372 * Check for valid input
1373 */
1374 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001375 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001376
1377 while( crt->version != 0 && crt->next != NULL )
1378 {
1379 prev = crt;
1380 crt = crt->next;
1381 }
1382
1383 /*
1384 * Add new certificate on the end of the chain if needed.
1385 */
1386 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001387 {
1388 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1389
Paul Bakker7d06ad22009-05-02 15:53:56 +00001390 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001391 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001392
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001393 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001394 crt = crt->next;
1395 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001398 /*
1399 * Determine buffer content. Buffer contains either one DER certificate or
1400 * one or more PEM certificates.
1401 */
1402#if defined(POLARSSL_PEM_C)
Paul Bakker3c2122f2013-06-24 19:03:14 +02001403 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001404 buf_format = X509_FORMAT_PEM;
1405#endif
1406
1407 if( buf_format == X509_FORMAT_DER )
1408 return x509parse_crt_der( crt, buf, buflen );
1409
1410#if defined(POLARSSL_PEM_C)
1411 if( buf_format == X509_FORMAT_PEM )
1412 {
1413 pem_context pem;
1414
1415 while( buflen > 0 )
1416 {
1417 size_t use_len;
1418 pem_init( &pem );
1419
1420 ret = pem_read_buffer( &pem,
1421 "-----BEGIN CERTIFICATE-----",
1422 "-----END CERTIFICATE-----",
1423 buf, NULL, 0, &use_len );
1424
1425 if( ret == 0 )
1426 {
1427 /*
1428 * Was PEM encoded
1429 */
1430 buflen -= use_len;
1431 buf += use_len;
1432 }
Paul Bakker5ed3b342013-06-24 19:05:46 +02001433 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1434 {
1435 return( ret );
1436 }
Paul Bakker00b28602013-06-24 13:02:41 +02001437 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001438 {
1439 pem_free( &pem );
1440
Paul Bakker5ed3b342013-06-24 19:05:46 +02001441 /*
1442 * PEM header and footer were found
1443 */
1444 buflen -= use_len;
1445 buf += use_len;
1446
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001447 if( first_error == 0 )
1448 first_error = ret;
1449
1450 continue;
1451 }
1452 else
1453 break;
1454
1455 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1456
1457 pem_free( &pem );
1458
1459 if( ret != 0 )
1460 {
1461 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001462 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001463 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001464 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001465 {
1466 if( prev )
1467 prev->next = NULL;
1468
1469 if( crt != chain )
1470 free( crt );
1471
1472 return( ret );
1473 }
1474
1475 if( first_error == 0 )
1476 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001477
1478 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001479
1480 memset( crt, 0, sizeof( x509_cert ) );
1481 continue;
1482 }
1483
1484 success = 1;
1485
1486 /*
1487 * Add new certificate to the list
1488 */
1489 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1490
1491 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001492 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001493
1494 prev = crt;
1495 crt = crt->next;
1496 memset( crt, 0, sizeof( x509_cert ) );
1497 }
1498 }
1499#endif
1500
1501 if( crt->version == 0 )
1502 {
1503 if( prev )
1504 prev->next = NULL;
1505
1506 if( crt != chain )
1507 free( crt );
1508 }
1509
1510 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001511 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001512 else if( first_error )
1513 return( first_error );
1514 else
1515 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001516}
1517
1518/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001519 * Parse one or more CRLs and add them to the chained list
1520 */
Paul Bakker23986e52011-04-24 08:57:21 +00001521int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001522{
Paul Bakker23986e52011-04-24 08:57:21 +00001523 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001524 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001525 unsigned char *p, *end;
1526 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001527#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001528 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001529 pem_context pem;
1530#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001531
1532 crl = chain;
1533
1534 /*
1535 * Check for valid input
1536 */
1537 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001538 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001539
1540 while( crl->version != 0 && crl->next != NULL )
1541 crl = crl->next;
1542
1543 /*
1544 * Add new CRL on the end of the chain if needed.
1545 */
1546 if ( crl->version != 0 && crl->next == NULL)
1547 {
1548 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1549
Paul Bakker7d06ad22009-05-02 15:53:56 +00001550 if( crl->next == NULL )
1551 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001553 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001554 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555
Paul Bakker7d06ad22009-05-02 15:53:56 +00001556 crl = crl->next;
1557 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558 }
1559
Paul Bakker96743fc2011-02-12 14:30:57 +00001560#if defined(POLARSSL_PEM_C)
1561 pem_init( &pem );
1562 ret = pem_read_buffer( &pem,
1563 "-----BEGIN X509 CRL-----",
1564 "-----END X509 CRL-----",
1565 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566
Paul Bakker96743fc2011-02-12 14:30:57 +00001567 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001568 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001569 /*
1570 * Was PEM encoded
1571 */
1572 buflen -= use_len;
1573 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001574
1575 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001576 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001578 p = pem.buf;
1579 pem.buf = NULL;
1580 len = pem.buflen;
1581 pem_free( &pem );
1582 }
Paul Bakker00b28602013-06-24 13:02:41 +02001583 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001584 {
1585 pem_free( &pem );
1586 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001587 }
1588 else
1589 {
1590 /*
1591 * nope, copy the raw DER data
1592 */
1593 p = (unsigned char *) malloc( len = buflen );
1594
1595 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001596 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001597
1598 memcpy( p, buf, buflen );
1599
1600 buflen = 0;
1601 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001602#else
1603 p = (unsigned char *) malloc( len = buflen );
1604
1605 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001606 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001607
1608 memcpy( p, buf, buflen );
1609
1610 buflen = 0;
1611#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612
1613 crl->raw.p = p;
1614 crl->raw.len = len;
1615 end = p + len;
1616
1617 /*
1618 * CertificateList ::= SEQUENCE {
1619 * tbsCertList TBSCertList,
1620 * signatureAlgorithm AlgorithmIdentifier,
1621 * signatureValue BIT STRING }
1622 */
1623 if( ( ret = asn1_get_tag( &p, end, &len,
1624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1625 {
1626 x509_crl_free( crl );
1627 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1628 }
1629
Paul Bakker23986e52011-04-24 08:57:21 +00001630 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001631 {
1632 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001633 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001634 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1635 }
1636
1637 /*
1638 * TBSCertList ::= SEQUENCE {
1639 */
1640 crl->tbs.p = p;
1641
1642 if( ( ret = asn1_get_tag( &p, end, &len,
1643 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1644 {
1645 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001646 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001647 }
1648
1649 end = p + len;
1650 crl->tbs.len = end - crl->tbs.p;
1651
1652 /*
1653 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1654 * -- if present, MUST be v2
1655 *
1656 * signature AlgorithmIdentifier
1657 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001658 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001659 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( ret );
1663 }
1664
1665 crl->version++;
1666
1667 if( crl->version > 2 )
1668 {
1669 x509_crl_free( crl );
1670 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1671 }
1672
Paul Bakkerc70b9822013-04-07 22:00:46 +02001673 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_md,
1674 &crl->sig_pk ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001675 {
1676 x509_crl_free( crl );
1677 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1678 }
1679
1680 /*
1681 * issuer Name
1682 */
1683 crl->issuer_raw.p = p;
1684
1685 if( ( ret = asn1_get_tag( &p, end, &len,
1686 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1687 {
1688 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001689 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 }
1691
1692 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1693 {
1694 x509_crl_free( crl );
1695 return( ret );
1696 }
1697
1698 crl->issuer_raw.len = p - crl->issuer_raw.p;
1699
1700 /*
1701 * thisUpdate Time
1702 * nextUpdate Time OPTIONAL
1703 */
Paul Bakker91200182010-02-18 21:26:15 +00001704 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705 {
1706 x509_crl_free( crl );
1707 return( ret );
1708 }
1709
Paul Bakker91200182010-02-18 21:26:15 +00001710 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 {
Paul Bakker9d781402011-05-09 16:17:09 +00001712 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001713 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001714 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001715 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001716 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717 x509_crl_free( crl );
1718 return( ret );
1719 }
1720 }
1721
1722 /*
1723 * revokedCertificates SEQUENCE OF SEQUENCE {
1724 * userCertificate CertificateSerialNumber,
1725 * revocationDate Time,
1726 * crlEntryExtensions Extensions OPTIONAL
1727 * -- if present, MUST be v2
1728 * } OPTIONAL
1729 */
1730 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1731 {
1732 x509_crl_free( crl );
1733 return( ret );
1734 }
1735
1736 /*
1737 * crlExtensions EXPLICIT Extensions OPTIONAL
1738 * -- if present, MUST be v2
1739 */
1740 if( crl->version == 2 )
1741 {
1742 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1743
1744 if( ret != 0 )
1745 {
1746 x509_crl_free( crl );
1747 return( ret );
1748 }
1749 }
1750
1751 if( p != end )
1752 {
1753 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001754 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1756 }
1757
1758 end = crl->raw.p + crl->raw.len;
1759
1760 /*
1761 * signatureAlgorithm AlgorithmIdentifier,
1762 * signatureValue BIT STRING
1763 */
1764 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1765 {
1766 x509_crl_free( crl );
1767 return( ret );
1768 }
1769
Paul Bakker535e97d2012-08-23 10:49:55 +00001770 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1771 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001772 {
1773 x509_crl_free( crl );
1774 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1775 }
1776
1777 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1778 {
1779 x509_crl_free( crl );
1780 return( ret );
1781 }
1782
1783 if( p != end )
1784 {
1785 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001786 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001787 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1788 }
1789
1790 if( buflen > 0 )
1791 {
1792 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1793
Paul Bakker7d06ad22009-05-02 15:53:56 +00001794 if( crl->next == NULL )
1795 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001796 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001797 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001798 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001799
Paul Bakker7d06ad22009-05-02 15:53:56 +00001800 crl = crl->next;
1801 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001802
1803 return( x509parse_crl( crl, buf, buflen ) );
1804 }
1805
1806 return( 0 );
1807}
1808
Paul Bakker335db3f2011-04-25 15:28:35 +00001809#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001810/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001811 * Load all data from a file into a given buffer.
1812 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001813int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001814{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001815 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001816
Paul Bakkerd98030e2009-05-02 15:13:40 +00001817 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001818 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001819
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820 fseek( f, 0, SEEK_END );
1821 *n = (size_t) ftell( f );
1822 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001823
Paul Bakkerd98030e2009-05-02 15:13:40 +00001824 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001825 {
1826 fclose( f );
Paul Bakker69e095c2011-12-10 21:55:01 +00001827 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerf6a19bd2013-05-14 13:26:51 +02001828 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829
Paul Bakkerd98030e2009-05-02 15:13:40 +00001830 if( fread( *buf, 1, *n, f ) != *n )
1831 {
1832 fclose( f );
1833 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001834 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001835 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001836
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001838
Paul Bakkerd98030e2009-05-02 15:13:40 +00001839 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001840
Paul Bakkerd98030e2009-05-02 15:13:40 +00001841 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001842}
1843
1844/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 * Load one or more certificates and add them to the chained list
1846 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001847int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001848{
1849 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 size_t n;
1851 unsigned char *buf;
1852
Paul Bakker69e095c2011-12-10 21:55:01 +00001853 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1854 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Paul Bakker69e095c2011-12-10 21:55:01 +00001856 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
1858 memset( buf, 0, n + 1 );
1859 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
1861 return( ret );
1862}
1863
Paul Bakker8d914582012-06-04 12:46:42 +00001864int x509parse_crtpath( x509_cert *chain, const char *path )
1865{
1866 int ret = 0;
1867#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001868 int w_ret;
1869 WCHAR szDir[MAX_PATH];
1870 char filename[MAX_PATH];
1871 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001872 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001873
Paul Bakker97872ac2012-11-02 12:53:26 +00001874 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001875 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001876
1877 if( len > MAX_PATH - 3 )
1878 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001879
Paul Bakker3338b792012-10-01 21:13:10 +00001880 memset( szDir, 0, sizeof(szDir) );
1881 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001882 memcpy( filename, path, len );
1883 filename[len++] = '\\';
1884 p = filename + len;
1885 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001886
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001887 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001888
Paul Bakker97872ac2012-11-02 12:53:26 +00001889 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001890 if (hFind == INVALID_HANDLE_VALUE)
1891 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1892
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001893 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001894 do
1895 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001896 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001897
Paul Bakkere4791f32012-06-04 21:29:15 +00001898 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001899 continue;
1900
Paul Bakker3338b792012-10-01 21:13:10 +00001901 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1902 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001903 p, len - 1,
1904 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001905
Paul Bakker3338b792012-10-01 21:13:10 +00001906 w_ret = x509parse_crtfile( chain, filename );
1907 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001908 {
1909 ret = w_ret;
1910 goto cleanup;
1911 }
Paul Bakker3338b792012-10-01 21:13:10 +00001912
1913 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001914 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001915 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001916
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001917 if (GetLastError() != ERROR_NO_MORE_FILES)
1918 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001919
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001920cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001921 FindClose( hFind );
1922#else
1923 int t_ret;
1924 struct dirent *entry;
1925 char entry_name[255];
1926 DIR *dir = opendir( path );
1927
1928 if( dir == NULL)
1929 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1930
1931 while( ( entry = readdir( dir ) ) != NULL )
1932 {
1933 if( entry->d_type != DT_REG )
1934 continue;
1935
1936 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1937 t_ret = x509parse_crtfile( chain, entry_name );
1938 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001939 {
1940 ret = t_ret;
1941 break;
1942 }
Paul Bakker8d914582012-06-04 12:46:42 +00001943
1944 ret += t_ret;
1945 }
1946 closedir( dir );
1947#endif
1948
1949 return( ret );
1950}
1951
Paul Bakkerd98030e2009-05-02 15:13:40 +00001952/*
1953 * Load one or more CRLs and add them to the chained list
1954 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001955int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001956{
1957 int ret;
1958 size_t n;
1959 unsigned char *buf;
1960
Paul Bakker69e095c2011-12-10 21:55:01 +00001961 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1962 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001963
Paul Bakker27fdf462011-06-09 13:55:13 +00001964 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001965
1966 memset( buf, 0, n + 1 );
1967 free( buf );
1968
1969 return( ret );
1970}
1971
Paul Bakker5121ce52009-01-03 21:22:43 +00001972/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001973 * Load and parse a private RSA key
1974 */
1975int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1976{
1977 int ret;
1978 size_t n;
1979 unsigned char *buf;
1980
Paul Bakker69e095c2011-12-10 21:55:01 +00001981 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1982 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001983
1984 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001985 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001986 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001987 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001988 (unsigned char *) pwd, strlen( pwd ) );
1989
1990 memset( buf, 0, n + 1 );
1991 free( buf );
1992
1993 return( ret );
1994}
1995
1996/*
1997 * Load and parse a public RSA key
1998 */
1999int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2000{
2001 int ret;
2002 size_t n;
2003 unsigned char *buf;
2004
Paul Bakker69e095c2011-12-10 21:55:01 +00002005 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2006 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002007
Paul Bakker27fdf462011-06-09 13:55:13 +00002008 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002009
2010 memset( buf, 0, n + 1 );
2011 free( buf );
2012
2013 return( ret );
2014}
2015#endif /* POLARSSL_FS_IO */
2016
2017/*
Paul Bakkere2f50402013-06-24 19:00:59 +02002018 * Parse a PKCS#1 encoded private RSA key
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 */
Paul Bakkere2f50402013-06-24 19:00:59 +02002020static int x509parse_key_pkcs1_der( rsa_context *rsa,
2021 const unsigned char *key,
2022 size_t keylen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023{
Paul Bakker23986e52011-04-24 08:57:21 +00002024 int ret;
2025 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002027
Paul Bakker96743fc2011-02-12 14:30:57 +00002028 p = (unsigned char *) key;
Paul Bakker96743fc2011-02-12 14:30:57 +00002029 end = p + keylen;
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 /*
Paul Bakkere2f50402013-06-24 19:00:59 +02002032 * This function parses the RSAPrivateKey (PKCS#1)
Paul Bakkered56b222011-07-13 11:26:43 +00002033 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 * RSAPrivateKey ::= SEQUENCE {
2035 * version Version,
2036 * modulus INTEGER, -- n
2037 * publicExponent INTEGER, -- e
2038 * privateExponent INTEGER, -- d
2039 * prime1 INTEGER, -- p
2040 * prime2 INTEGER, -- q
2041 * exponent1 INTEGER, -- d mod (p-1)
2042 * exponent2 INTEGER, -- d mod (q-1)
2043 * coefficient INTEGER, -- (inverse of q) mod p
2044 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2045 * }
2046 */
2047 if( ( ret = asn1_get_tag( &p, end, &len,
2048 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2049 {
Paul Bakker9d781402011-05-09 16:17:09 +00002050 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
2052
2053 end = p + len;
2054
2055 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2056 {
Paul Bakker9d781402011-05-09 16:17:09 +00002057 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059
2060 if( rsa->ver != 0 )
2061 {
Paul Bakker9d781402011-05-09 16:17:09 +00002062 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064
2065 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2066 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2067 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2068 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2069 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2070 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2071 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2072 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2073 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002075 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 }
2077
2078 rsa->len = mpi_size( &rsa->N );
2079
2080 if( p != end )
2081 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002083 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002084 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 }
2086
2087 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2088 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 rsa_free( rsa );
2090 return( ret );
2091 }
2092
Paul Bakkere2f50402013-06-24 19:00:59 +02002093 return( 0 );
2094}
2095
2096/*
2097 * Parse an unencrypted PKCS#8 encoded private RSA key
2098 */
2099static int x509parse_key_pkcs8_unencrypted_der(
2100 rsa_context *rsa,
2101 const unsigned char *key,
2102 size_t keylen )
2103{
2104 int ret;
2105 size_t len;
2106 unsigned char *p, *end;
2107 x509_buf pk_alg_oid;
2108 pk_type_t pk_alg = POLARSSL_PK_NONE;
2109
2110 p = (unsigned char *) key;
2111 end = p + keylen;
2112
2113 /*
2114 * This function parses the PrivatKeyInfo object (PKCS#8)
2115 *
2116 * PrivateKeyInfo ::= SEQUENCE {
2117 * version Version,
2118 * algorithm AlgorithmIdentifier,
2119 * PrivateKey BIT STRING
2120 * }
2121 *
2122 * AlgorithmIdentifier ::= SEQUENCE {
2123 * algorithm OBJECT IDENTIFIER,
2124 * parameters ANY DEFINED BY algorithm OPTIONAL
2125 * }
2126 *
2127 * The PrivateKey BIT STRING is a PKCS#1 RSAPrivateKey
2128 */
2129 if( ( ret = asn1_get_tag( &p, end, &len,
2130 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2131 {
2132 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2133 }
2134
2135 end = p + len;
2136
2137 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2138 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2139
2140 if( rsa->ver != 0 )
2141 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2142
2143 if( ( ret = x509_get_alg( &p, end, &pk_alg_oid ) ) != 0 )
2144 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2145
2146 /*
2147 * only RSA keys handled at this time
2148 */
2149 if( oid_get_pk_alg( &pk_alg_oid, &pk_alg ) != 0 )
2150 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2151
2152 /*
2153 * Get the OCTET STRING and parse the PKCS#1 format inside
2154 */
2155 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2156 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2157
2158 if( ( end - p ) < 1 )
2159 {
2160 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2161 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2162 }
2163
2164 end = p + len;
2165
2166 if( ( ret = x509parse_key_pkcs1_der( rsa, p, end - p ) ) != 0 )
2167 return( ret );
2168
2169 return( 0 );
2170}
2171
2172/*
2173 * Parse a private RSA key
2174 */
2175int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2176 const unsigned char *pwd, size_t pwdlen )
2177{
2178 int ret;
2179
Paul Bakker96743fc2011-02-12 14:30:57 +00002180#if defined(POLARSSL_PEM_C)
Paul Bakkere2f50402013-06-24 19:00:59 +02002181 size_t len;
2182 pem_context pem;
2183
2184 pem_init( &pem );
2185 ret = pem_read_buffer( &pem,
2186 "-----BEGIN RSA PRIVATE KEY-----",
2187 "-----END RSA PRIVATE KEY-----",
2188 key, pwd, pwdlen, &len );
2189 if( ret == 0 )
2190 {
2191 if( ( ret = x509parse_key_pkcs1_der( rsa, pem.buf, pem.buflen ) ) != 0 )
2192 {
2193 rsa_free( rsa );
2194 }
2195
2196 pem_free( &pem );
2197 return( ret );
2198 }
2199 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2200 {
2201 pem_free( &pem );
2202 return( ret );
2203 }
2204
2205 ret = pem_read_buffer( &pem,
2206 "-----BEGIN PRIVATE KEY-----",
2207 "-----END PRIVATE KEY-----",
2208 key, NULL, 0, &len );
2209 if( ret == 0 )
2210 {
2211 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa,
2212 pem.buf, pem.buflen ) ) != 0 )
2213 {
2214 rsa_free( rsa );
2215 }
2216
2217 pem_free( &pem );
2218 return( ret );
2219 }
2220 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
2221 {
2222 pem_free( &pem );
2223 return( ret );
2224 }
2225
Paul Bakker96743fc2011-02-12 14:30:57 +00002226 pem_free( &pem );
Paul Bakkere2f50402013-06-24 19:00:59 +02002227#else
2228 ((void) pwd);
2229 ((void) pwdlen);
2230#endif /* POLARSSL_PEM_C */
2231
2232 // At this point we only know it's not a PEM formatted key. Could be any
2233 // of the known DER encoded private key formats
2234 //
2235 // We try the different DER format parsers to see if one passes without
2236 // error
2237 //
2238 if( ( ret = x509parse_key_pkcs8_unencrypted_der( rsa, key, keylen ) ) != 0 )
2239 {
2240 rsa_free( rsa );
2241
2242 if( ( ret = x509parse_key_pkcs1_der( rsa, key, keylen ) ) != 0 )
2243 {
2244 rsa_free( rsa );
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2246 }
2247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
2249 return( 0 );
2250}
2251
2252/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002253 * Parse a public RSA key
2254 */
Paul Bakker23986e52011-04-24 08:57:21 +00002255int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002256{
Paul Bakker23986e52011-04-24 08:57:21 +00002257 int ret;
2258 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002259 unsigned char *p, *end;
2260 x509_buf alg_oid;
2261#if defined(POLARSSL_PEM_C)
2262 pem_context pem;
2263
2264 pem_init( &pem );
2265 ret = pem_read_buffer( &pem,
2266 "-----BEGIN PUBLIC KEY-----",
2267 "-----END PUBLIC KEY-----",
2268 key, NULL, 0, &len );
2269
2270 if( ret == 0 )
2271 {
2272 /*
2273 * Was PEM encoded
2274 */
2275 keylen = pem.buflen;
2276 }
Paul Bakker00b28602013-06-24 13:02:41 +02002277 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002278 {
2279 pem_free( &pem );
2280 return( ret );
2281 }
2282
2283 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2284#else
2285 p = (unsigned char *) key;
2286#endif
2287 end = p + keylen;
2288
2289 /*
2290 * PublicKeyInfo ::= SEQUENCE {
2291 * algorithm AlgorithmIdentifier,
2292 * PublicKey BIT STRING
2293 * }
2294 *
2295 * AlgorithmIdentifier ::= SEQUENCE {
2296 * algorithm OBJECT IDENTIFIER,
2297 * parameters ANY DEFINED BY algorithm OPTIONAL
2298 * }
2299 *
2300 * RSAPublicKey ::= SEQUENCE {
2301 * modulus INTEGER, -- n
2302 * publicExponent INTEGER -- e
2303 * }
2304 */
2305
2306 if( ( ret = asn1_get_tag( &p, end, &len,
2307 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2308 {
2309#if defined(POLARSSL_PEM_C)
2310 pem_free( &pem );
2311#endif
2312 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002313 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002314 }
2315
2316 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2317 {
2318#if defined(POLARSSL_PEM_C)
2319 pem_free( &pem );
2320#endif
2321 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002322 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002323 }
2324
2325 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2326 {
2327#if defined(POLARSSL_PEM_C)
2328 pem_free( &pem );
2329#endif
2330 rsa_free( rsa );
2331 return( ret );
2332 }
2333
2334 rsa->len = mpi_size( &rsa->N );
2335
2336#if defined(POLARSSL_PEM_C)
2337 pem_free( &pem );
2338#endif
2339
2340 return( 0 );
2341}
2342
Paul Bakkereaa89f82011-04-04 21:36:15 +00002343#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002344/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002345 * Parse DHM parameters
2346 */
Paul Bakker23986e52011-04-24 08:57:21 +00002347int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002348{
Paul Bakker23986e52011-04-24 08:57:21 +00002349 int ret;
2350 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002351 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002352#if defined(POLARSSL_PEM_C)
2353 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002354
Paul Bakker96743fc2011-02-12 14:30:57 +00002355 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002356
Paul Bakker96743fc2011-02-12 14:30:57 +00002357 ret = pem_read_buffer( &pem,
2358 "-----BEGIN DH PARAMETERS-----",
2359 "-----END DH PARAMETERS-----",
2360 dhmin, NULL, 0, &dhminlen );
2361
2362 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002363 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002364 /*
2365 * Was PEM encoded
2366 */
2367 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002368 }
Paul Bakker00b28602013-06-24 13:02:41 +02002369 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002370 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002371 pem_free( &pem );
2372 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002373 }
2374
Paul Bakker96743fc2011-02-12 14:30:57 +00002375 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2376#else
2377 p = (unsigned char *) dhmin;
2378#endif
2379 end = p + dhminlen;
2380
Paul Bakker1b57b062011-01-06 15:48:19 +00002381 memset( dhm, 0, sizeof( dhm_context ) );
2382
Paul Bakker1b57b062011-01-06 15:48:19 +00002383 /*
2384 * DHParams ::= SEQUENCE {
2385 * prime INTEGER, -- P
2386 * generator INTEGER, -- g
2387 * }
2388 */
2389 if( ( ret = asn1_get_tag( &p, end, &len,
2390 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2391 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002392#if defined(POLARSSL_PEM_C)
2393 pem_free( &pem );
2394#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002395 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002396 }
2397
2398 end = p + len;
2399
2400 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2401 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2402 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002403#if defined(POLARSSL_PEM_C)
2404 pem_free( &pem );
2405#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002406 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002407 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002408 }
2409
2410 if( p != end )
2411 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002412#if defined(POLARSSL_PEM_C)
2413 pem_free( &pem );
2414#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002415 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002416 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002417 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2418 }
2419
Paul Bakker96743fc2011-02-12 14:30:57 +00002420#if defined(POLARSSL_PEM_C)
2421 pem_free( &pem );
2422#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002423
2424 return( 0 );
2425}
2426
Paul Bakker335db3f2011-04-25 15:28:35 +00002427#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002428/*
2429 * Load and parse a private RSA key
2430 */
2431int x509parse_dhmfile( dhm_context *dhm, const char *path )
2432{
2433 int ret;
2434 size_t n;
2435 unsigned char *buf;
2436
Paul Bakker69e095c2011-12-10 21:55:01 +00002437 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2438 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002439
Paul Bakker27fdf462011-06-09 13:55:13 +00002440 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002441
2442 memset( buf, 0, n + 1 );
2443 free( buf );
2444
2445 return( ret );
2446}
Paul Bakker335db3f2011-04-25 15:28:35 +00002447#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002448#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002449
Paul Bakker5121ce52009-01-03 21:22:43 +00002450#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002451#include <stdarg.h>
2452
2453#if !defined vsnprintf
2454#define vsnprintf _vsnprintf
2455#endif // vsnprintf
2456
2457/*
2458 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2459 * Result value is not size of buffer needed, but -1 if no fit is possible.
2460 *
2461 * This fuction tries to 'fix' this by at least suggesting enlarging the
2462 * size by 20.
2463 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002464static int compat_snprintf(char *str, size_t size, const char *format, ...)
Paul Bakkerd98030e2009-05-02 15:13:40 +00002465{
2466 va_list ap;
2467 int res = -1;
2468
2469 va_start( ap, format );
2470
2471 res = vsnprintf( str, size, format, ap );
2472
2473 va_end( ap );
2474
2475 // No quick fix possible
2476 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002477 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002478
2479 return res;
2480}
2481
2482#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002483#endif
2484
Paul Bakkerd98030e2009-05-02 15:13:40 +00002485#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2486
2487#define SAFE_SNPRINTF() \
2488{ \
2489 if( ret == -1 ) \
2490 return( -1 ); \
2491 \
Paul Bakker23986e52011-04-24 08:57:21 +00002492 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 p[n - 1] = '\0'; \
2494 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2495 } \
2496 \
Paul Bakker23986e52011-04-24 08:57:21 +00002497 n -= (unsigned int) ret; \
2498 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499}
2500
Paul Bakker5121ce52009-01-03 21:22:43 +00002501/*
2502 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002505int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506{
Paul Bakker23986e52011-04-24 08:57:21 +00002507 int ret;
2508 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002510 const x509_name *name;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002511 const char *short_name = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 char s[128], *p;
2513
2514 memset( s, 0, sizeof( s ) );
2515
2516 name = dn;
2517 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002518 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
2520 while( name != NULL )
2521 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002522 if( !name->oid.p )
2523 {
2524 name = name->next;
2525 continue;
2526 }
2527
Paul Bakker74111d32011-01-15 16:57:55 +00002528 if( name != dn )
2529 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002530 ret = snprintf( p, n, ", " );
2531 SAFE_SNPRINTF();
2532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Paul Bakkerc70b9822013-04-07 22:00:46 +02002534 ret = oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakkerc70b9822013-04-07 22:00:46 +02002536 if( ret == 0 )
2537 ret = snprintf( p, n, "%s=", short_name );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 else
Paul Bakkerd98030e2009-05-02 15:13:40 +00002539 ret = snprintf( p, n, "\?\?=" );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002540 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 for( i = 0; i < name->val.len; i++ )
2543 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002544 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 break;
2546
2547 c = name->val.p[i];
2548 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2549 s[i] = '?';
2550 else s[i] = c;
2551 }
2552 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 ret = snprintf( p, n, "%s", s );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002554 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 name = name->next;
2556 }
2557
Paul Bakker23986e52011-04-24 08:57:21 +00002558 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559}
2560
2561/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002562 * Store the serial in printable form into buf; no more
2563 * than size characters will be written
2564 */
2565int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2566{
Paul Bakker23986e52011-04-24 08:57:21 +00002567 int ret;
2568 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002569 char *p;
2570
2571 p = buf;
2572 n = size;
2573
2574 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002575 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002576
2577 for( i = 0; i < nr; i++ )
2578 {
Paul Bakker93048802011-12-05 14:38:06 +00002579 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002580 continue;
2581
Paul Bakkerdd476992011-01-16 21:34:59 +00002582 ret = snprintf( p, n, "%02X%s",
2583 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2584 SAFE_SNPRINTF();
2585 }
2586
Paul Bakker03c7c252011-11-25 12:37:37 +00002587 if( nr != serial->len )
2588 {
2589 ret = snprintf( p, n, "...." );
2590 SAFE_SNPRINTF();
2591 }
2592
Paul Bakker23986e52011-04-24 08:57:21 +00002593 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002594}
2595
2596/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002599int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2600 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601{
Paul Bakker23986e52011-04-24 08:57:21 +00002602 int ret;
2603 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002604 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002605 const char *desc = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002608 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Paul Bakkerd98030e2009-05-02 15:13:40 +00002610 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002612 SAFE_SNPRINTF();
2613 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002615 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002616
Paul Bakkerdd476992011-01-16 21:34:59 +00002617 ret = x509parse_serial_gets( p, n, &crt->serial);
2618 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Paul Bakkerd98030e2009-05-02 15:13:40 +00002620 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2621 SAFE_SNPRINTF();
2622 ret = x509parse_dn_gets( p, n, &crt->issuer );
2623 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
Paul Bakkerd98030e2009-05-02 15:13:40 +00002625 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2626 SAFE_SNPRINTF();
2627 ret = x509parse_dn_gets( p, n, &crt->subject );
2628 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002629
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2632 crt->valid_from.year, crt->valid_from.mon,
2633 crt->valid_from.day, crt->valid_from.hour,
2634 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002635 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002636
Paul Bakkerd98030e2009-05-02 15:13:40 +00002637 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2639 crt->valid_to.year, crt->valid_to.mon,
2640 crt->valid_to.day, crt->valid_to.hour,
2641 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002642 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Paul Bakkerc70b9822013-04-07 22:00:46 +02002644 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002646
Paul Bakkerc70b9822013-04-07 22:00:46 +02002647 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
2648 if( ret != 0 )
2649 ret = snprintf( p, n, "???" );
2650 else
2651 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002652 SAFE_SNPRINTF();
2653
2654 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002655 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002656 SAFE_SNPRINTF();
2657
Paul Bakker23986e52011-04-24 08:57:21 +00002658 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002659}
2660
Paul Bakker74111d32011-01-15 16:57:55 +00002661/*
2662 * Return an informational string describing the given OID
2663 */
2664const char *x509_oid_get_description( x509_buf *oid )
2665{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002666 const char *desc = NULL;
2667 int ret;
Paul Bakker74111d32011-01-15 16:57:55 +00002668
Paul Bakkerc70b9822013-04-07 22:00:46 +02002669 ret = oid_get_extended_key_usage( oid, &desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002670
Paul Bakkerc70b9822013-04-07 22:00:46 +02002671 if( ret != 0 )
2672 return( NULL );
Paul Bakker74111d32011-01-15 16:57:55 +00002673
Paul Bakkerc70b9822013-04-07 22:00:46 +02002674 return( desc );
Paul Bakker74111d32011-01-15 16:57:55 +00002675}
2676
2677/* Return the x.y.z.... style numeric string for the given OID */
2678int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2679{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002680 return oid_get_numeric_string( buf, size, oid );
Paul Bakker74111d32011-01-15 16:57:55 +00002681}
2682
Paul Bakkerd98030e2009-05-02 15:13:40 +00002683/*
2684 * Return an informational string about the CRL.
2685 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002686int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2687 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688{
Paul Bakker23986e52011-04-24 08:57:21 +00002689 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002690 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002691 char *p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002692 const char *desc;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002693 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002694
2695 p = buf;
2696 n = size;
2697
2698 ret = snprintf( p, n, "%sCRL version : %d",
2699 prefix, crl->version );
2700 SAFE_SNPRINTF();
2701
2702 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2703 SAFE_SNPRINTF();
2704 ret = x509parse_dn_gets( p, n, &crl->issuer );
2705 SAFE_SNPRINTF();
2706
2707 ret = snprintf( p, n, "\n%sthis update : " \
2708 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2709 crl->this_update.year, crl->this_update.mon,
2710 crl->this_update.day, crl->this_update.hour,
2711 crl->this_update.min, crl->this_update.sec );
2712 SAFE_SNPRINTF();
2713
2714 ret = snprintf( p, n, "\n%snext update : " \
2715 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2716 crl->next_update.year, crl->next_update.mon,
2717 crl->next_update.day, crl->next_update.hour,
2718 crl->next_update.min, crl->next_update.sec );
2719 SAFE_SNPRINTF();
2720
2721 entry = &crl->entry;
2722
2723 ret = snprintf( p, n, "\n%sRevoked certificates:",
2724 prefix );
2725 SAFE_SNPRINTF();
2726
Paul Bakker9be19372009-07-27 20:21:53 +00002727 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002728 {
2729 ret = snprintf( p, n, "\n%sserial number: ",
2730 prefix );
2731 SAFE_SNPRINTF();
2732
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002733 ret = x509parse_serial_gets( p, n, &entry->serial);
2734 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735
Paul Bakkerd98030e2009-05-02 15:13:40 +00002736 ret = snprintf( p, n, " revocation date: " \
2737 "%04d-%02d-%02d %02d:%02d:%02d",
2738 entry->revocation_date.year, entry->revocation_date.mon,
2739 entry->revocation_date.day, entry->revocation_date.hour,
2740 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002741 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002742
2743 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
Paul Bakkerc70b9822013-04-07 22:00:46 +02002746 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002747 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Paul Bakkerc70b9822013-04-07 22:00:46 +02002749 ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc );
2750 if( ret != 0 )
2751 ret = snprintf( p, n, "???" );
2752 else
2753 ret = snprintf( p, n, desc );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754 SAFE_SNPRINTF();
2755
Paul Bakker1e27bb22009-07-19 20:25:25 +00002756 ret = snprintf( p, n, "\n" );
2757 SAFE_SNPRINTF();
2758
Paul Bakker23986e52011-04-24 08:57:21 +00002759 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760}
2761
2762/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002763 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002765int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002766{
Paul Bakkercce9d772011-11-18 14:26:47 +00002767 int year, mon, day;
2768 int hour, min, sec;
2769
2770#if defined(_WIN32)
2771 SYSTEMTIME st;
2772
2773 GetLocalTime(&st);
2774
2775 year = st.wYear;
2776 mon = st.wMonth;
2777 day = st.wDay;
2778 hour = st.wHour;
2779 min = st.wMinute;
2780 sec = st.wSecond;
2781#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 struct tm *lt;
2783 time_t tt;
2784
2785 tt = time( NULL );
2786 lt = localtime( &tt );
2787
Paul Bakkercce9d772011-11-18 14:26:47 +00002788 year = lt->tm_year + 1900;
2789 mon = lt->tm_mon + 1;
2790 day = lt->tm_mday;
2791 hour = lt->tm_hour;
2792 min = lt->tm_min;
2793 sec = lt->tm_sec;
2794#endif
2795
2796 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002797 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Paul Bakkercce9d772011-11-18 14:26:47 +00002799 if( year == to->year &&
2800 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002801 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002802
Paul Bakkercce9d772011-11-18 14:26:47 +00002803 if( year == to->year &&
2804 mon == to->mon &&
2805 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002806 return( 1 );
2807
Paul Bakkercce9d772011-11-18 14:26:47 +00002808 if( year == to->year &&
2809 mon == to->mon &&
2810 day == to->day &&
2811 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002812 return( 1 );
2813
Paul Bakkercce9d772011-11-18 14:26:47 +00002814 if( year == to->year &&
2815 mon == to->mon &&
2816 day == to->day &&
2817 hour == to->hour &&
2818 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002819 return( 1 );
2820
Paul Bakkercce9d772011-11-18 14:26:47 +00002821 if( year == to->year &&
2822 mon == to->mon &&
2823 day == to->day &&
2824 hour == to->hour &&
2825 min == to->min &&
2826 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002827 return( 1 );
2828
Paul Bakker40ea7de2009-05-03 10:18:48 +00002829 return( 0 );
2830}
2831
2832/*
2833 * Return 1 if the certificate is revoked, or 0 otherwise.
2834 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002835int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002836{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002837 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002838
2839 while( cur != NULL && cur->serial.len != 0 )
2840 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002841 if( crt->serial.len == cur->serial.len &&
2842 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002843 {
2844 if( x509parse_time_expired( &cur->revocation_date ) )
2845 return( 1 );
2846 }
2847
2848 cur = cur->next;
2849 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002850
2851 return( 0 );
2852}
2853
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002854/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002855 * Check that the given certificate is valid accoring to the CRL.
2856 */
2857static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2858 x509_crl *crl_list)
2859{
2860 int flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002861 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2862 const md_info_t *md_info;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002863
Paul Bakker915275b2012-09-28 07:10:55 +00002864 if( ca == NULL )
2865 return( flags );
2866
Paul Bakker76fd75a2011-01-16 21:12:10 +00002867 /*
2868 * TODO: What happens if no CRL is present?
2869 * Suggestion: Revocation state should be unknown if no CRL is present.
2870 * For backwards compatibility this is not yet implemented.
2871 */
2872
Paul Bakker915275b2012-09-28 07:10:55 +00002873 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00002874 {
Paul Bakker915275b2012-09-28 07:10:55 +00002875 if( crl_list->version == 0 ||
2876 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00002877 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2878 crl_list->issuer_raw.len ) != 0 )
2879 {
2880 crl_list = crl_list->next;
2881 continue;
2882 }
2883
2884 /*
2885 * Check if CRL is correctly signed by the trusted CA
2886 */
Paul Bakkerc70b9822013-04-07 22:00:46 +02002887 md_info = md_info_from_type( crl_list->sig_md );
2888 if( md_info == NULL )
2889 {
2890 /*
2891 * Cannot check 'unknown' hash
2892 */
2893 flags |= BADCRL_NOT_TRUSTED;
2894 break;
2895 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002896
Paul Bakkerc70b9822013-04-07 22:00:46 +02002897 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002898
Paul Bakkerc70b9822013-04-07 22:00:46 +02002899 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, crl_list->sig_md,
Paul Bakker76fd75a2011-01-16 21:12:10 +00002900 0, hash, crl_list->sig.p ) == 0 )
2901 {
2902 /*
2903 * CRL is not trusted
2904 */
2905 flags |= BADCRL_NOT_TRUSTED;
2906 break;
2907 }
2908
2909 /*
2910 * Check for validity of CRL (Do not drop out)
2911 */
2912 if( x509parse_time_expired( &crl_list->next_update ) )
2913 flags |= BADCRL_EXPIRED;
2914
2915 /*
2916 * Check if certificate is revoked
2917 */
2918 if( x509parse_revoked(crt, crl_list) )
2919 {
2920 flags |= BADCERT_REVOKED;
2921 break;
2922 }
2923
2924 crl_list = crl_list->next;
2925 }
2926 return flags;
2927}
2928
Paul Bakker57b12982012-02-11 17:38:38 +00002929int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002930{
2931 size_t i;
2932 size_t cn_idx = 0;
2933
Paul Bakker57b12982012-02-11 17:38:38 +00002934 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002935 return( 0 );
2936
2937 for( i = 0; i < strlen( cn ); ++i )
2938 {
2939 if( cn[i] == '.' )
2940 {
2941 cn_idx = i;
2942 break;
2943 }
2944 }
2945
2946 if( cn_idx == 0 )
2947 return( 0 );
2948
Paul Bakker535e97d2012-08-23 10:49:55 +00002949 if( strlen( cn ) - cn_idx == name->len - 1 &&
2950 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00002951 {
2952 return( 1 );
2953 }
2954
2955 return( 0 );
2956}
2957
Paul Bakker915275b2012-09-28 07:10:55 +00002958static int x509parse_verify_top(
2959 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00002960 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00002961 int (*f_vrfy)(void *, x509_cert *, int, int *),
2962 void *p_vrfy )
2963{
Paul Bakkerc70b9822013-04-07 22:00:46 +02002964 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00002965 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002966 unsigned char hash[POLARSSL_MD_MAX_SIZE];
2967 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00002968
2969 if( x509parse_time_expired( &child->valid_to ) )
2970 *flags |= BADCERT_EXPIRED;
2971
2972 /*
2973 * Child is the top of the chain. Check against the trust_ca list.
2974 */
2975 *flags |= BADCERT_NOT_TRUSTED;
2976
2977 while( trust_ca != NULL )
2978 {
2979 if( trust_ca->version == 0 ||
2980 child->issuer_raw.len != trust_ca->subject_raw.len ||
2981 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
2982 child->issuer_raw.len ) != 0 )
2983 {
2984 trust_ca = trust_ca->next;
2985 continue;
2986 }
2987
Paul Bakker9a736322012-11-14 12:39:52 +00002988 /*
2989 * Reduce path_len to check against if top of the chain is
2990 * the same as the trusted CA
2991 */
2992 if( child->subject_raw.len == trust_ca->subject_raw.len &&
2993 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
2994 child->issuer_raw.len ) == 0 )
2995 {
2996 check_path_cnt--;
2997 }
2998
Paul Bakker915275b2012-09-28 07:10:55 +00002999 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003000 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003001 {
3002 trust_ca = trust_ca->next;
3003 continue;
3004 }
3005
Paul Bakkerc70b9822013-04-07 22:00:46 +02003006 md_info = md_info_from_type( child->sig_md );
3007 if( md_info == NULL )
3008 {
3009 /*
3010 * Cannot check 'unknown' hash
3011 */
3012 continue;
3013 }
Paul Bakker915275b2012-09-28 07:10:55 +00003014
Paul Bakkerc70b9822013-04-07 22:00:46 +02003015 md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker915275b2012-09-28 07:10:55 +00003016
Paul Bakkerc70b9822013-04-07 22:00:46 +02003017 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, child->sig_md,
Paul Bakker915275b2012-09-28 07:10:55 +00003018 0, hash, child->sig.p ) != 0 )
3019 {
3020 trust_ca = trust_ca->next;
3021 continue;
3022 }
3023
3024 /*
3025 * Top of chain is signed by a trusted CA
3026 */
3027 *flags &= ~BADCERT_NOT_TRUSTED;
3028 break;
3029 }
3030
Paul Bakker9a736322012-11-14 12:39:52 +00003031 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003032 * If top of chain is not the same as the trusted CA send a verify request
3033 * to the callback for any issues with validity and CRL presence for the
3034 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003035 */
3036 if( trust_ca != NULL &&
3037 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3038 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3039 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003040 {
3041 /* Check trusted CA's CRL for then chain's top crt */
3042 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3043
3044 if( x509parse_time_expired( &trust_ca->valid_to ) )
3045 ca_flags |= BADCERT_EXPIRED;
3046
Paul Bakker915275b2012-09-28 07:10:55 +00003047 if( NULL != f_vrfy )
3048 {
Paul Bakker9a736322012-11-14 12:39:52 +00003049 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003050 return( ret );
3051 }
3052 }
3053
3054 /* Call callback on top cert */
3055 if( NULL != f_vrfy )
3056 {
Paul Bakker9a736322012-11-14 12:39:52 +00003057 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003058 return( ret );
3059 }
3060
Paul Bakker915275b2012-09-28 07:10:55 +00003061 *flags |= ca_flags;
3062
3063 return( 0 );
3064}
3065
3066static int x509parse_verify_child(
3067 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003068 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003069 int (*f_vrfy)(void *, x509_cert *, int, int *),
3070 void *p_vrfy )
3071{
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072 int ret;
Paul Bakker915275b2012-09-28 07:10:55 +00003073 int parent_flags = 0;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003074 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakker915275b2012-09-28 07:10:55 +00003075 x509_cert *grandparent;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003076 const md_info_t *md_info;
Paul Bakker915275b2012-09-28 07:10:55 +00003077
3078 if( x509parse_time_expired( &child->valid_to ) )
3079 *flags |= BADCERT_EXPIRED;
3080
Paul Bakkerc70b9822013-04-07 22:00:46 +02003081 md_info = md_info_from_type( child->sig_md );
3082 if( md_info == NULL )
3083 {
3084 /*
3085 * Cannot check 'unknown' hash
3086 */
Paul Bakker915275b2012-09-28 07:10:55 +00003087 *flags |= BADCERT_NOT_TRUSTED;
Paul Bakkerc70b9822013-04-07 22:00:46 +02003088 }
3089 else
3090 {
3091 md( md_info, child->tbs.p, child->tbs.len, hash );
3092
3093 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, child->sig_md, 0, hash,
3094 child->sig.p ) != 0 )
3095 *flags |= BADCERT_NOT_TRUSTED;
3096 }
3097
Paul Bakker915275b2012-09-28 07:10:55 +00003098 /* Check trusted CA's CRL for the given crt */
3099 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3100
3101 grandparent = parent->next;
3102
3103 while( grandparent != NULL )
3104 {
3105 if( grandparent->version == 0 ||
3106 grandparent->ca_istrue == 0 ||
3107 parent->issuer_raw.len != grandparent->subject_raw.len ||
3108 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3109 parent->issuer_raw.len ) != 0 )
3110 {
3111 grandparent = grandparent->next;
3112 continue;
3113 }
3114 break;
3115 }
3116
Paul Bakker915275b2012-09-28 07:10:55 +00003117 if( grandparent != NULL )
3118 {
3119 /*
3120 * Part of the chain
3121 */
Paul Bakker9a736322012-11-14 12:39:52 +00003122 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 +00003123 if( ret != 0 )
3124 return( ret );
3125 }
3126 else
3127 {
Paul Bakker9a736322012-11-14 12:39:52 +00003128 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 +00003129 if( ret != 0 )
3130 return( ret );
3131 }
3132
3133 /* child is verified to be a child of the parent, call verify callback */
3134 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003135 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003136 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003137
3138 *flags |= parent_flags;
3139
3140 return( 0 );
3141}
3142
Paul Bakker76fd75a2011-01-16 21:12:10 +00003143/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 * Verify the certificate validity
3145 */
3146int x509parse_verify( x509_cert *crt,
3147 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003148 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003149 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003150 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003151 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003152{
Paul Bakker23986e52011-04-24 08:57:21 +00003153 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003154 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003155 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003156 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003158 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Paul Bakker40ea7de2009-05-03 10:18:48 +00003160 *flags = 0;
3161
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 if( cn != NULL )
3163 {
3164 name = &crt->subject;
3165 cn_len = strlen( cn );
3166
Paul Bakker4d2c1242012-05-10 14:12:46 +00003167 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003169 cur = &crt->subject_alt_names;
3170
3171 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003172 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003173 if( cur->buf.len == cn_len &&
3174 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003175 break;
3176
Paul Bakker535e97d2012-08-23 10:49:55 +00003177 if( cur->buf.len > 2 &&
3178 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003179 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003180 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Paul Bakker4d2c1242012-05-10 14:12:46 +00003182 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003183 }
3184
3185 if( cur == NULL )
3186 *flags |= BADCERT_CN_MISMATCH;
3187 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003188 else
3189 {
3190 while( name != NULL )
3191 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003192 if( OID_CMP( OID_AT_CN, &name->oid ) )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003193 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003194 if( name->val.len == cn_len &&
3195 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003196 break;
3197
Paul Bakker535e97d2012-08-23 10:49:55 +00003198 if( name->val.len > 2 &&
3199 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003200 x509_wildcard_verify( cn, &name->val ) )
3201 break;
3202 }
3203
3204 name = name->next;
3205 }
3206
3207 if( name == NULL )
3208 *flags |= BADCERT_CN_MISMATCH;
3209 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003210 }
3211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003213 * Iterate upwards in the given cert chain, to find our crt parent.
3214 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003216 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003217
Paul Bakker76fd75a2011-01-16 21:12:10 +00003218 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003220 if( parent->ca_istrue == 0 ||
3221 crt->issuer_raw.len != parent->subject_raw.len ||
3222 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 crt->issuer_raw.len ) != 0 )
3224 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003225 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 continue;
3227 }
Paul Bakker915275b2012-09-28 07:10:55 +00003228 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 }
3230
Paul Bakker915275b2012-09-28 07:10:55 +00003231 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 {
Paul Bakker915275b2012-09-28 07:10:55 +00003233 /*
3234 * Part of the chain
3235 */
Paul Bakker9a736322012-11-14 12:39:52 +00003236 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003237 if( ret != 0 )
3238 return( ret );
3239 }
3240 else
Paul Bakker74111d32011-01-15 16:57:55 +00003241 {
Paul Bakker9a736322012-11-14 12:39:52 +00003242 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003243 if( ret != 0 )
3244 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003245 }
Paul Bakker915275b2012-09-28 07:10:55 +00003246
3247 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003248 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 return( 0 );
3251}
3252
3253/*
3254 * Unallocate all certificate data
3255 */
3256void x509_free( x509_cert *crt )
3257{
3258 x509_cert *cert_cur = crt;
3259 x509_cert *cert_prv;
3260 x509_name *name_cur;
3261 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003262 x509_sequence *seq_cur;
3263 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 if( crt == NULL )
3266 return;
3267
3268 do
3269 {
3270 rsa_free( &cert_cur->rsa );
3271
3272 name_cur = cert_cur->issuer.next;
3273 while( name_cur != NULL )
3274 {
3275 name_prv = name_cur;
3276 name_cur = name_cur->next;
3277 memset( name_prv, 0, sizeof( x509_name ) );
3278 free( name_prv );
3279 }
3280
3281 name_cur = cert_cur->subject.next;
3282 while( name_cur != NULL )
3283 {
3284 name_prv = name_cur;
3285 name_cur = name_cur->next;
3286 memset( name_prv, 0, sizeof( x509_name ) );
3287 free( name_prv );
3288 }
3289
Paul Bakker74111d32011-01-15 16:57:55 +00003290 seq_cur = cert_cur->ext_key_usage.next;
3291 while( seq_cur != NULL )
3292 {
3293 seq_prv = seq_cur;
3294 seq_cur = seq_cur->next;
3295 memset( seq_prv, 0, sizeof( x509_sequence ) );
3296 free( seq_prv );
3297 }
3298
Paul Bakker8afa70d2012-02-11 18:42:45 +00003299 seq_cur = cert_cur->subject_alt_names.next;
3300 while( seq_cur != NULL )
3301 {
3302 seq_prv = seq_cur;
3303 seq_cur = seq_cur->next;
3304 memset( seq_prv, 0, sizeof( x509_sequence ) );
3305 free( seq_prv );
3306 }
3307
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 if( cert_cur->raw.p != NULL )
3309 {
3310 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3311 free( cert_cur->raw.p );
3312 }
3313
3314 cert_cur = cert_cur->next;
3315 }
3316 while( cert_cur != NULL );
3317
3318 cert_cur = crt;
3319 do
3320 {
3321 cert_prv = cert_cur;
3322 cert_cur = cert_cur->next;
3323
3324 memset( cert_prv, 0, sizeof( x509_cert ) );
3325 if( cert_prv != crt )
3326 free( cert_prv );
3327 }
3328 while( cert_cur != NULL );
3329}
3330
Paul Bakkerd98030e2009-05-02 15:13:40 +00003331/*
3332 * Unallocate all CRL data
3333 */
3334void x509_crl_free( x509_crl *crl )
3335{
3336 x509_crl *crl_cur = crl;
3337 x509_crl *crl_prv;
3338 x509_name *name_cur;
3339 x509_name *name_prv;
3340 x509_crl_entry *entry_cur;
3341 x509_crl_entry *entry_prv;
3342
3343 if( crl == NULL )
3344 return;
3345
3346 do
3347 {
3348 name_cur = crl_cur->issuer.next;
3349 while( name_cur != NULL )
3350 {
3351 name_prv = name_cur;
3352 name_cur = name_cur->next;
3353 memset( name_prv, 0, sizeof( x509_name ) );
3354 free( name_prv );
3355 }
3356
3357 entry_cur = crl_cur->entry.next;
3358 while( entry_cur != NULL )
3359 {
3360 entry_prv = entry_cur;
3361 entry_cur = entry_cur->next;
3362 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3363 free( entry_prv );
3364 }
3365
3366 if( crl_cur->raw.p != NULL )
3367 {
3368 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3369 free( crl_cur->raw.p );
3370 }
3371
3372 crl_cur = crl_cur->next;
3373 }
3374 while( crl_cur != NULL );
3375
3376 crl_cur = crl;
3377 do
3378 {
3379 crl_prv = crl_cur;
3380 crl_cur = crl_cur->next;
3381
3382 memset( crl_prv, 0, sizeof( x509_crl ) );
3383 if( crl_prv != crl )
3384 free( crl_prv );
3385 }
3386 while( crl_cur != NULL );
3387}
3388
Paul Bakker40e46942009-01-03 21:51:57 +00003389#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
Paul Bakker40e46942009-01-03 21:51:57 +00003391#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
3393/*
3394 * Checkup routine
3395 */
3396int x509_self_test( int verbose )
3397{
Paul Bakker5690efc2011-05-26 13:16:06 +00003398#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003399 int ret;
3400 int flags;
3401 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 x509_cert cacert;
3403 x509_cert clicert;
3404 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003405#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003406 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003407#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
3409 if( verbose != 0 )
3410 printf( " X.509 certificate load: " );
3411
3412 memset( &clicert, 0, sizeof( x509_cert ) );
3413
Paul Bakker3c2122f2013-06-24 19:03:14 +02003414 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003415 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003416 if( ret != 0 )
3417 {
3418 if( verbose != 0 )
3419 printf( "failed\n" );
3420
3421 return( ret );
3422 }
3423
3424 memset( &cacert, 0, sizeof( x509_cert ) );
3425
Paul Bakker3c2122f2013-06-24 19:03:14 +02003426 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003427 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 if( ret != 0 )
3429 {
3430 if( verbose != 0 )
3431 printf( "failed\n" );
3432
3433 return( ret );
3434 }
3435
3436 if( verbose != 0 )
3437 printf( "passed\n X.509 private key load: " );
3438
3439 i = strlen( test_ca_key );
3440 j = strlen( test_ca_pwd );
3441
Paul Bakker66b78b22011-03-25 14:22:50 +00003442 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3443
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 if( ( ret = x509parse_key( &rsa,
Paul Bakker3c2122f2013-06-24 19:03:14 +02003445 (const unsigned char *) test_ca_key, i,
3446 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 {
3448 if( verbose != 0 )
3449 printf( "failed\n" );
3450
3451 return( ret );
3452 }
3453
3454 if( verbose != 0 )
3455 printf( "passed\n X.509 signature verify: ");
3456
Paul Bakker23986e52011-04-24 08:57:21 +00003457 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 if( ret != 0 )
3459 {
Paul Bakker23986e52011-04-24 08:57:21 +00003460 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 if( verbose != 0 )
3462 printf( "failed\n" );
3463
3464 return( ret );
3465 }
3466
Paul Bakker5690efc2011-05-26 13:16:06 +00003467#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003469 printf( "passed\n X.509 DHM parameter load: " );
3470
3471 i = strlen( test_dhm_params );
3472 j = strlen( test_ca_pwd );
3473
Paul Bakker3c2122f2013-06-24 19:03:14 +02003474 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003475 {
3476 if( verbose != 0 )
3477 printf( "failed\n" );
3478
3479 return( ret );
3480 }
3481
3482 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003484#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
3486 x509_free( &cacert );
3487 x509_free( &clicert );
3488 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003489#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003490 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003491#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003492
3493 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003494#else
3495 ((void) verbose);
3496 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003498}
3499
3500#endif
3501
3502#endif