blob: 35f27298667c319105067443dd07a41838272284 [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 Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakker2ca8ad12013-02-19 13:17:38 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65#include <string.h>
66#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000067#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000068#include <windows.h>
69#else
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker335db3f2011-04-25 15:28:35 +000073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000075#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000076#include <sys/types.h>
77#include <dirent.h>
78#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000079#endif
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
Paul Bakker5121ce52009-01-03 21:22:43 +000082 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
83 */
84static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000085 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000086 int *ver )
87{
Paul Bakker23986e52011-04-24 08:57:21 +000088 int ret;
89 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91 if( ( ret = asn1_get_tag( p, end, &len,
92 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
93 {
Paul Bakker40e46942009-01-03 21:51:57 +000094 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000095 {
96 *ver = 0;
97 return( 0 );
98 }
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 return( ret );
101 }
102
103 end = *p + len;
104
105 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000106 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000109 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000110 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112 return( 0 );
113}
114
115/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000116 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000117 */
118static int x509_crl_get_version( unsigned char **p,
119 const unsigned char *end,
120 int *ver )
121{
122 int ret;
123
124 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
125 {
126 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000127 {
128 *ver = 0;
129 return( 0 );
130 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000131
132 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
133 }
134
135 return( 0 );
136}
137
138/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * CertificateSerialNumber ::= INTEGER
140 */
141static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000142 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 x509_buf *serial )
144{
145 int ret;
146
147 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000148 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000149 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
152 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000153 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000154 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156 serial->tag = *(*p)++;
157
158 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000159 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161 serial->p = *p;
162 *p += serial->len;
163
164 return( 0 );
165}
166
167/*
168 * AlgorithmIdentifier ::= SEQUENCE {
169 * algorithm OBJECT IDENTIFIER,
170 * parameters ANY DEFINED BY algorithm OPTIONAL }
171 */
172static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000173 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 x509_buf *alg )
175{
Paul Bakker23986e52011-04-24 08:57:21 +0000176 int ret;
177 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 if( ( ret = asn1_get_tag( p, end, &len,
180 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000181 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 end = *p + len;
184 alg->tag = **p;
185
186 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000187 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 alg->p = *p;
190 *p += alg->len;
191
192 if( *p == end )
193 return( 0 );
194
195 /*
196 * assume the algorithm parameters must be NULL
197 */
198 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000199 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000202 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000203 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205 return( 0 );
206}
207
208/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 * AttributeTypeAndValue ::= SEQUENCE {
210 * type AttributeType,
211 * value AttributeValue }
212 *
213 * AttributeType ::= OBJECT IDENTIFIER
214 *
215 * AttributeValue ::= ANY DEFINED BY AttributeType
216 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000217static int x509_get_attr_type_value( unsigned char **p,
218 const unsigned char *end,
219 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220{
Paul Bakker23986e52011-04-24 08:57:21 +0000221 int ret;
222 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 x509_buf *oid;
224 x509_buf *val;
225
226 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000228 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 oid = &cur->oid;
231 oid->tag = **p;
232
233 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000234 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236 oid->p = *p;
237 *p += oid->len;
238
239 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000240 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000241 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
244 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
245 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000246 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000247 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 val = &cur->val;
250 val->tag = *(*p)++;
251
252 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000253 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 val->p = *p;
256 *p += val->len;
257
258 cur->next = NULL;
259
Paul Bakker400ff6f2011-02-20 10:40:16 +0000260 return( 0 );
261}
262
263/*
264 * RelativeDistinguishedName ::=
265 * SET OF AttributeTypeAndValue
266 *
267 * AttributeTypeAndValue ::= SEQUENCE {
268 * type AttributeType,
269 * value AttributeValue }
270 *
271 * AttributeType ::= OBJECT IDENTIFIER
272 *
273 * AttributeValue ::= ANY DEFINED BY AttributeType
274 */
275static int x509_get_name( unsigned char **p,
276 const unsigned char *end,
277 x509_name *cur )
278{
Paul Bakker23986e52011-04-24 08:57:21 +0000279 int ret;
280 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000281 const unsigned char *end2;
282 x509_name *use;
283
284 if( ( ret = asn1_get_tag( p, end, &len,
285 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000286 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000287
288 end2 = end;
289 end = *p + len;
290 use = cur;
291
292 do
293 {
294 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
295 return( ret );
296
297 if( *p != end )
298 {
299 use->next = (x509_name *) malloc(
300 sizeof( x509_name ) );
301
302 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000303 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000304
305 memset( use->next, 0, sizeof( x509_name ) );
306
307 use = use->next;
308 }
309 }
310 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 /*
313 * recurse until end of SEQUENCE is reached
314 */
315 if( *p == end2 )
316 return( 0 );
317
318 cur->next = (x509_name *) malloc(
319 sizeof( x509_name ) );
320
321 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000322 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Paul Bakker430ffbe2012-05-01 08:14:20 +0000324 memset( cur->next, 0, sizeof( x509_name ) );
325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 return( x509_get_name( p, end2, cur->next ) );
327}
328
329/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * Time ::= CHOICE {
331 * utcTime UTCTime,
332 * generalTime GeneralizedTime }
333 */
Paul Bakker91200182010-02-18 21:26:15 +0000334static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000335 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 x509_time *time )
337{
Paul Bakker23986e52011-04-24 08:57:21 +0000338 int ret;
339 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000340 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000341 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000344 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
345 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000346
Paul Bakker91200182010-02-18 21:26:15 +0000347 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348
Paul Bakker91200182010-02-18 21:26:15 +0000349 if ( tag == ASN1_UTC_TIME )
350 {
351 (*p)++;
352 ret = asn1_get_len( p, end, &len );
353
354 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000355 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356
Paul Bakker91200182010-02-18 21:26:15 +0000357 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000358 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
359 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000360
Paul Bakker91200182010-02-18 21:26:15 +0000361 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
362 &time->year, &time->mon, &time->day,
363 &time->hour, &time->min, &time->sec ) < 5 )
364 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000365
Paul Bakker400ff6f2011-02-20 10:40:16 +0000366 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000367 time->year += 1900;
368
369 *p += len;
370
371 return( 0 );
372 }
373 else if ( tag == ASN1_GENERALIZED_TIME )
374 {
375 (*p)++;
376 ret = asn1_get_len( p, end, &len );
377
378 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000379 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000380
381 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000382 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
383 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000384
385 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
386 &time->year, &time->mon, &time->day,
387 &time->hour, &time->min, &time->sec ) < 5 )
388 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
389
390 *p += len;
391
392 return( 0 );
393 }
394 else
Paul Bakker9d781402011-05-09 16:17:09 +0000395 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000396}
397
398
399/*
400 * Validity ::= SEQUENCE {
401 * notBefore Time,
402 * notAfter Time }
403 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000404static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000405 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 x509_time *from,
407 x509_time *to )
408{
Paul Bakker23986e52011-04-24 08:57:21 +0000409 int ret;
410 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 if( ( ret = asn1_get_tag( p, end, &len,
413 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000414 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 end = *p + len;
417
Paul Bakker91200182010-02-18 21:26:15 +0000418 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000419 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Paul Bakker91200182010-02-18 21:26:15 +0000421 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000422 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000425 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000426 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 return( 0 );
429}
430
431/*
432 * SubjectPublicKeyInfo ::= SEQUENCE {
433 * algorithm AlgorithmIdentifier,
434 * subjectPublicKey BIT STRING }
435 */
436static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000437 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 x509_buf *pk_alg_oid,
439 mpi *N, mpi *E )
440{
Paul Bakker23986e52011-04-24 08:57:21 +0000441 int ret, can_handle;
442 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 unsigned char *end2;
444
445 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
446 return( ret );
447
448 /*
449 * only RSA public keys handled at this time
450 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000451 can_handle = 0;
452
453 if( pk_alg_oid->len == 9 &&
454 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
455 can_handle = 1;
456
457 if( pk_alg_oid->len == 9 &&
458 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
459 {
460 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
461 can_handle = 1;
462
463 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
464 can_handle = 1;
465 }
466
467 if( pk_alg_oid->len == 5 &&
468 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
469 can_handle = 1;
470
471 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000472 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000475 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000479 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481 end2 = *p + len;
482
483 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 /*
487 * RSAPublicKey ::= SEQUENCE {
488 * modulus INTEGER, -- n
489 * publicExponent INTEGER -- e
490 * }
491 */
492 if( ( ret = asn1_get_tag( p, end2, &len,
493 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000494 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000497 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000498 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
501 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000502 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000505 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000506 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 return( 0 );
509}
510
511static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000512 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 x509_buf *sig )
514{
Paul Bakker23986e52011-04-24 08:57:21 +0000515 int ret;
516 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
Paul Bakker8afa70d2012-02-11 18:42:45 +0000518 if( ( end - *p ) < 1 )
519 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
520 POLARSSL_ERR_ASN1_OUT_OF_DATA );
521
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 sig->tag = **p;
523
524 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000525 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker74111d32011-01-15 16:57:55 +0000527
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000529 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 sig->len = len;
532 sig->p = *p;
533
534 *p += len;
535
536 return( 0 );
537}
538
539/*
540 * X.509 v2/v3 unique identifier (not parsed)
541 */
542static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000543 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 x509_buf *uid, int n )
545{
546 int ret;
547
548 if( *p == end )
549 return( 0 );
550
551 uid->tag = **p;
552
553 if( ( ret = asn1_get_tag( p, end, &uid->len,
554 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
555 {
Paul Bakker40e46942009-01-03 21:51:57 +0000556 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 return( 0 );
558
559 return( ret );
560 }
561
562 uid->p = *p;
563 *p += uid->len;
564
565 return( 0 );
566}
567
568/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000569 * X.509 Extensions (No parsing of extensions, pointer should
570 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 */
572static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000574 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
Paul Bakker23986e52011-04-24 08:57:21 +0000576 int ret;
577 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579 if( *p == end )
580 return( 0 );
581
582 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000583
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000585 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588 ext->p = *p;
589 end = *p + ext->len;
590
591 /*
592 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
593 *
594 * Extension ::= SEQUENCE {
595 * extnID OBJECT IDENTIFIER,
596 * critical BOOLEAN DEFAULT FALSE,
597 * extnValue OCTET STRING }
598 */
599 if( ( ret = asn1_get_tag( p, end, &len,
600 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000601 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000604 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000605 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakkerd98030e2009-05-02 15:13:40 +0000607 return( 0 );
608}
609
610/*
611 * X.509 CRL v2 extensions (no extensions parsed yet.)
612 */
613static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000614 const unsigned char *end,
615 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000616{
Paul Bakker23986e52011-04-24 08:57:21 +0000617 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000618 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000619
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000620 /* Get explicit tag */
621 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000622 {
623 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
624 return( 0 );
625
626 return( ret );
627 }
628
629 while( *p < end )
630 {
631 if( ( ret = asn1_get_tag( p, end, &len,
632 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000633 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000634
635 *p += len;
636 }
637
638 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000639 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000640 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
641
642 return( 0 );
643}
644
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000645/*
646 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
647 */
648static int x509_get_crl_entry_ext( unsigned char **p,
649 const unsigned char *end,
650 x509_buf *ext )
651{
652 int ret;
653 size_t len = 0;
654
655 /* OPTIONAL */
656 if (end <= *p)
657 return( 0 );
658
659 ext->tag = **p;
660 ext->p = *p;
661
662 /*
663 * Get CRL-entry extension sequence header
664 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
665 */
666 if( ( ret = asn1_get_tag( p, end, &ext->len,
667 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
668 {
669 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
670 {
671 ext->p = NULL;
672 return( 0 );
673 }
674 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
675 }
676
677 end = *p + ext->len;
678
679 if( end != *p + ext->len )
680 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
681 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
682
683 while( *p < end )
684 {
685 if( ( ret = asn1_get_tag( p, end, &len,
686 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
687 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
688
689 *p += len;
690 }
691
692 if( *p != end )
693 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
694 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
695
696 return( 0 );
697}
698
Paul Bakker74111d32011-01-15 16:57:55 +0000699static int x509_get_basic_constraints( unsigned char **p,
700 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000701 int *ca_istrue,
702 int *max_pathlen )
703{
Paul Bakker23986e52011-04-24 08:57:21 +0000704 int ret;
705 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000706
707 /*
708 * BasicConstraints ::= SEQUENCE {
709 * cA BOOLEAN DEFAULT FALSE,
710 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
711 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000712 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000713 *max_pathlen = 0; /* endless */
714
715 if( ( ret = asn1_get_tag( p, end, &len,
716 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000717 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000718
719 if( *p == end )
720 return 0;
721
Paul Bakker3cccddb2011-01-16 21:46:31 +0000722 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000723 {
724 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000725 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000726
727 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000728 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000729
Paul Bakker3cccddb2011-01-16 21:46:31 +0000730 if( *ca_istrue != 0 )
731 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000732 }
733
734 if( *p == end )
735 return 0;
736
737 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000739
740 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000741 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000742 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
743
744 (*max_pathlen)++;
745
Paul Bakker74111d32011-01-15 16:57:55 +0000746 return 0;
747}
748
749static int x509_get_ns_cert_type( unsigned char **p,
750 const unsigned char *end,
751 unsigned char *ns_cert_type)
752{
753 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000754 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000755
756 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000757 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000758
759 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000760 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000761 POLARSSL_ERR_ASN1_INVALID_LENGTH );
762
763 /* Get actual bitstring */
764 *ns_cert_type = *bs.p;
765 return 0;
766}
767
768static int x509_get_key_usage( unsigned char **p,
769 const unsigned char *end,
770 unsigned char *key_usage)
771{
772 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000773 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000774
775 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000776 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000777
Paul Bakker94a67962012-08-23 13:03:52 +0000778 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000780 POLARSSL_ERR_ASN1_INVALID_LENGTH );
781
782 /* Get actual bitstring */
783 *key_usage = *bs.p;
784 return 0;
785}
786
Paul Bakkerd98030e2009-05-02 15:13:40 +0000787/*
Paul Bakker74111d32011-01-15 16:57:55 +0000788 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
789 *
790 * KeyPurposeId ::= OBJECT IDENTIFIER
791 */
792static int x509_get_ext_key_usage( unsigned char **p,
793 const unsigned char *end,
794 x509_sequence *ext_key_usage)
795{
796 int ret;
797
798 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000799 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000800
801 /* Sequence length must be >= 1 */
802 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000803 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000804 POLARSSL_ERR_ASN1_INVALID_LENGTH );
805
806 return 0;
807}
808
809/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000810 * SubjectAltName ::= GeneralNames
811 *
812 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
813 *
814 * GeneralName ::= CHOICE {
815 * otherName [0] OtherName,
816 * rfc822Name [1] IA5String,
817 * dNSName [2] IA5String,
818 * x400Address [3] ORAddress,
819 * directoryName [4] Name,
820 * ediPartyName [5] EDIPartyName,
821 * uniformResourceIdentifier [6] IA5String,
822 * iPAddress [7] OCTET STRING,
823 * registeredID [8] OBJECT IDENTIFIER }
824 *
825 * OtherName ::= SEQUENCE {
826 * type-id OBJECT IDENTIFIER,
827 * value [0] EXPLICIT ANY DEFINED BY type-id }
828 *
829 * EDIPartyName ::= SEQUENCE {
830 * nameAssigner [0] DirectoryString OPTIONAL,
831 * partyName [1] DirectoryString }
832 *
833 * NOTE: PolarSSL only parses and uses dNSName at this point.
834 */
835static int x509_get_subject_alt_name( unsigned char **p,
836 const unsigned char *end,
837 x509_sequence *subject_alt_name )
838{
839 int ret;
840 size_t len, tag_len;
841 asn1_buf *buf;
842 unsigned char tag;
843 asn1_sequence *cur = subject_alt_name;
844
845 /* Get main sequence tag */
846 if( ( ret = asn1_get_tag( p, end, &len,
847 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
849
850 if( *p + len != end )
851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
852 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
853
854 while( *p < end )
855 {
856 if( ( end - *p ) < 1 )
857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
858 POLARSSL_ERR_ASN1_OUT_OF_DATA );
859
860 tag = **p;
861 (*p)++;
862 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
863 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
864
865 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
866 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
867 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
868
869 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
870 {
871 *p += tag_len;
872 continue;
873 }
874
875 buf = &(cur->buf);
876 buf->tag = tag;
877 buf->p = *p;
878 buf->len = tag_len;
879 *p += buf->len;
880
881 /* Allocate and assign next pointer */
882 if (*p < end)
883 {
884 cur->next = (asn1_sequence *) malloc(
885 sizeof( asn1_sequence ) );
886
887 if( cur->next == NULL )
888 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
889 POLARSSL_ERR_ASN1_MALLOC_FAILED );
890
Paul Bakker535e97d2012-08-23 10:49:55 +0000891 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000892 cur = cur->next;
893 }
894 }
895
896 /* Set final sequence entry's next pointer to NULL */
897 cur->next = NULL;
898
899 if( *p != end )
900 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
901 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
902
903 return( 0 );
904}
905
906/*
Paul Bakker74111d32011-01-15 16:57:55 +0000907 * X.509 v3 extensions
908 *
909 * TODO: Perform all of the basic constraints tests required by the RFC
910 * TODO: Set values for undetected extensions to a sane default?
911 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000912 */
913static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000914 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000915 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000916{
Paul Bakker23986e52011-04-24 08:57:21 +0000917 int ret;
918 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000919 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000920
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000921 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000922 {
923 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
924 return( 0 );
925
926 return( ret );
927 }
928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 while( *p < end )
930 {
Paul Bakker74111d32011-01-15 16:57:55 +0000931 /*
932 * Extension ::= SEQUENCE {
933 * extnID OBJECT IDENTIFIER,
934 * critical BOOLEAN DEFAULT FALSE,
935 * extnValue OCTET STRING }
936 */
937 x509_buf extn_oid = {0, 0, NULL};
938 int is_critical = 0; /* DEFAULT FALSE */
939
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 if( ( ret = asn1_get_tag( p, end, &len,
941 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000942 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000944 end_ext_data = *p + len;
945
Paul Bakker74111d32011-01-15 16:57:55 +0000946 /* Get extension ID */
947 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Paul Bakker74111d32011-01-15 16:57:55 +0000949 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 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 Bakker74111d32011-01-15 16:57:55 +0000952 extn_oid.p = *p;
953 *p += extn_oid.len;
954
955 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000956 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000957 POLARSSL_ERR_ASN1_OUT_OF_DATA );
958
959 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000960 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000961 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000962 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Paul Bakker74111d32011-01-15 16:57:55 +0000964 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000965 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000967 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000969 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000970
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000971 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000972 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000973 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000974
Paul Bakker74111d32011-01-15 16:57:55 +0000975 /*
976 * Detect supported extensions
977 */
978 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
979 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 {
Paul Bakker74111d32011-01-15 16:57:55 +0000981 /* Parse basic constraints */
982 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000983 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000984 return ( ret );
985 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000986 }
Paul Bakker74111d32011-01-15 16:57:55 +0000987 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
988 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
989 {
990 /* Parse netscape certificate type */
991 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
992 &crt->ns_cert_type ) ) != 0 )
993 return ( ret );
994 crt->ext_types |= EXT_NS_CERT_TYPE;
995 }
996 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
997 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
998 {
999 /* Parse key usage */
1000 if( ( ret = x509_get_key_usage( p, end_ext_octet,
1001 &crt->key_usage ) ) != 0 )
1002 return ( ret );
1003 crt->ext_types |= EXT_KEY_USAGE;
1004 }
1005 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
1006 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
1007 {
1008 /* Parse extended key usage */
1009 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1010 &crt->ext_key_usage ) ) != 0 )
1011 return ( ret );
1012 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1013 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001014 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1015 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1016 {
1017 /* Parse extended key usage */
1018 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1019 &crt->subject_alt_names ) ) != 0 )
1020 return ( ret );
1021 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1022 }
Paul Bakker74111d32011-01-15 16:57:55 +00001023 else
1024 {
1025 /* No parser found, skip extension */
1026 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001027
Paul Bakker5c721f92011-07-27 16:51:09 +00001028#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001029 if( is_critical )
1030 {
1031 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001032 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001033 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1034 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001035#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037 }
1038
1039 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001040 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001041 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 return( 0 );
1044}
1045
1046/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001047 * X.509 CRL Entries
1048 */
1049static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001050 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001051 x509_crl_entry *entry )
1052{
Paul Bakker23986e52011-04-24 08:57:21 +00001053 int ret;
1054 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001055 x509_crl_entry *cur_entry = entry;
1056
1057 if( *p == end )
1058 return( 0 );
1059
Paul Bakker9be19372009-07-27 20:21:53 +00001060 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001061 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1062 {
1063 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1064 return( 0 );
1065
1066 return( ret );
1067 }
1068
Paul Bakker9be19372009-07-27 20:21:53 +00001069 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001070
1071 while( *p < end )
1072 {
Paul Bakker23986e52011-04-24 08:57:21 +00001073 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001074 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001075
1076 if( ( ret = asn1_get_tag( p, end, &len2,
1077 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1078 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001079 return( ret );
1080 }
1081
Paul Bakker9be19372009-07-27 20:21:53 +00001082 cur_entry->raw.tag = **p;
1083 cur_entry->raw.p = *p;
1084 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001085 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001086
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001087 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001088 return( ret );
1089
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001090 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001091 return( ret );
1092
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001093 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001094 return( ret );
1095
Paul Bakker74111d32011-01-15 16:57:55 +00001096 if ( *p < end )
1097 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001098 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001099
1100 if( cur_entry->next == NULL )
1101 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1102
Paul Bakkerd98030e2009-05-02 15:13:40 +00001103 cur_entry = cur_entry->next;
1104 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1105 }
1106 }
1107
1108 return( 0 );
1109}
1110
Paul Bakker27d66162010-03-17 06:56:01 +00001111static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1112{
1113 if( sig_oid->len == 9 &&
1114 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1115 {
1116 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1117 {
1118 *sig_alg = sig_oid->p[8];
1119 return( 0 );
1120 }
1121
1122 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1123 {
1124 *sig_alg = sig_oid->p[8];
1125 return( 0 );
1126 }
1127
1128 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1129 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001130 if( sig_oid->len == 5 &&
1131 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1132 {
1133 *sig_alg = SIG_RSA_SHA1;
1134 return( 0 );
1135 }
Paul Bakker27d66162010-03-17 06:56:01 +00001136
1137 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1138}
1139
Paul Bakkerd98030e2009-05-02 15:13:40 +00001140/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001141 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001143int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144{
Paul Bakker23986e52011-04-24 08:57:21 +00001145 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001146 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001147 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Paul Bakker320a4b52009-03-28 18:52:39 +00001149 /*
1150 * Check for valid input
1151 */
1152 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001153 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001154
Paul Bakker96743fc2011-02-12 14:30:57 +00001155 p = (unsigned char *) malloc( len = buflen );
1156
1157 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001158 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001159
1160 memcpy( p, buf, buflen );
1161
1162 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
1164 crt->raw.p = p;
1165 crt->raw.len = len;
1166 end = p + len;
1167
1168 /*
1169 * Certificate ::= SEQUENCE {
1170 * tbsCertificate TBSCertificate,
1171 * signatureAlgorithm AlgorithmIdentifier,
1172 * signatureValue BIT STRING }
1173 */
1174 if( ( ret = asn1_get_tag( &p, end, &len,
1175 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1176 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001177 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001178 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179 }
1180
Paul Bakkerb00ca422012-09-25 12:10:00 +00001181 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001183 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001184 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001185 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001187 crt_end = p + len;
1188
Paul Bakker5121ce52009-01-03 21:22:43 +00001189 /*
1190 * TBSCertificate ::= SEQUENCE {
1191 */
1192 crt->tbs.p = p;
1193
1194 if( ( ret = asn1_get_tag( &p, end, &len,
1195 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1196 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001197 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001198 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
1201 end = p + len;
1202 crt->tbs.len = end - crt->tbs.p;
1203
1204 /*
1205 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1206 *
1207 * CertificateSerialNumber ::= INTEGER
1208 *
1209 * signature AlgorithmIdentifier
1210 */
1211 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1212 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1213 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1214 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001215 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 return( ret );
1217 }
1218
1219 crt->version++;
1220
1221 if( crt->version > 3 )
1222 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001223 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001224 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 }
1226
Paul Bakker27d66162010-03-17 06:56:01 +00001227 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001229 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001230 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232
1233 /*
1234 * issuer Name
1235 */
1236 crt->issuer_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
1245 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1246 {
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->issuer_raw.len = p - crt->issuer_raw.p;
1252
1253 /*
1254 * Validity ::= SEQUENCE {
1255 * notBefore Time,
1256 * notAfter Time }
1257 *
1258 */
1259 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1260 &crt->valid_to ) ) != 0 )
1261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 return( ret );
1264 }
1265
1266 /*
1267 * subject Name
1268 */
1269 crt->subject_raw.p = p;
1270
1271 if( ( ret = asn1_get_tag( &p, end, &len,
1272 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001275 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
1277
Paul Bakkercefb3962012-06-27 11:51:09 +00001278 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001280 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 return( ret );
1282 }
1283
1284 crt->subject_raw.len = p - crt->subject_raw.p;
1285
1286 /*
1287 * SubjectPublicKeyInfo ::= SEQUENCE
1288 * algorithm AlgorithmIdentifier,
1289 * subjectPublicKey BIT STRING }
1290 */
1291 if( ( ret = asn1_get_tag( &p, end, &len,
1292 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1293 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001294 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001295 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 }
1297
1298 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1299 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1300 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001301 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 return( ret );
1303 }
1304
1305 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1306 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001307 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 return( ret );
1309 }
1310
1311 crt->rsa.len = mpi_size( &crt->rsa.N );
1312
1313 /*
1314 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1315 * -- If present, version shall be v2 or v3
1316 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1317 * -- If present, version shall be v2 or v3
1318 * extensions [3] EXPLICIT Extensions OPTIONAL
1319 * -- If present, version shall be v3
1320 */
1321 if( crt->version == 2 || crt->version == 3 )
1322 {
1323 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1324 if( ret != 0 )
1325 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001326 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 return( ret );
1328 }
1329 }
1330
1331 if( crt->version == 2 || crt->version == 3 )
1332 {
1333 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1334 if( ret != 0 )
1335 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001336 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 return( ret );
1338 }
1339 }
1340
1341 if( crt->version == 3 )
1342 {
Paul Bakker74111d32011-01-15 16:57:55 +00001343 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 if( ret != 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
1351 if( p != end )
1352 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001353 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001354 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001355 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
1357
Paul Bakkerb00ca422012-09-25 12:10:00 +00001358 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001359
1360 /*
1361 * signatureAlgorithm AlgorithmIdentifier,
1362 * signatureValue BIT STRING
1363 */
1364 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1365 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001366 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 return( ret );
1368 }
1369
Paul Bakker535e97d2012-08-23 10:49:55 +00001370 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1371 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001373 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001374 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 }
1376
1377 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1378 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001379 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 return( ret );
1381 }
1382
1383 if( p != end )
1384 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001385 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001386 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001387 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 }
1389
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001390 return( 0 );
1391}
1392
1393/*
1394 * Parse one or more PEM certificates from a buffer and add them to the chained list
1395 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001396int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001397{
Paul Bakker69e095c2011-12-10 21:55:01 +00001398 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001399 x509_cert *crt, *prev = NULL;
1400 int buf_format = X509_FORMAT_DER;
1401
1402 crt = chain;
1403
1404 /*
1405 * Check for valid input
1406 */
1407 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001408 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001409
1410 while( crt->version != 0 && crt->next != NULL )
1411 {
1412 prev = crt;
1413 crt = crt->next;
1414 }
1415
1416 /*
1417 * Add new certificate on the end of the chain if needed.
1418 */
1419 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001420 {
1421 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1422
Paul Bakker7d06ad22009-05-02 15:53:56 +00001423 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001424 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001425
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001426 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001427 crt = crt->next;
1428 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001431 /*
1432 * Determine buffer content. Buffer contains either one DER certificate or
1433 * one or more PEM certificates.
1434 */
1435#if defined(POLARSSL_PEM_C)
Paul Bakkereae09db2013-06-06 12:35:54 +02001436 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001437 buf_format = X509_FORMAT_PEM;
1438#endif
1439
1440 if( buf_format == X509_FORMAT_DER )
1441 return x509parse_crt_der( crt, buf, buflen );
1442
1443#if defined(POLARSSL_PEM_C)
1444 if( buf_format == X509_FORMAT_PEM )
1445 {
1446 pem_context pem;
1447
1448 while( buflen > 0 )
1449 {
1450 size_t use_len;
1451 pem_init( &pem );
1452
1453 ret = pem_read_buffer( &pem,
1454 "-----BEGIN CERTIFICATE-----",
1455 "-----END CERTIFICATE-----",
1456 buf, NULL, 0, &use_len );
1457
1458 if( ret == 0 )
1459 {
1460 /*
1461 * Was PEM encoded
1462 */
1463 buflen -= use_len;
1464 buf += use_len;
1465 }
Paul Bakker64171862013-06-06 15:01:18 +02001466 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1467 {
1468 return( ret );
1469 }
Paul Bakker9255e832013-06-06 14:58:28 +02001470 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001471 {
1472 pem_free( &pem );
1473
Paul Bakker64171862013-06-06 15:01:18 +02001474 /*
1475 * PEM header and footer were found
1476 */
1477 buflen -= use_len;
1478 buf += use_len;
1479
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001480 if( first_error == 0 )
1481 first_error = ret;
1482
1483 continue;
1484 }
1485 else
1486 break;
1487
1488 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1489
1490 pem_free( &pem );
1491
1492 if( ret != 0 )
1493 {
1494 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001495 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001496 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001497 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001498 {
1499 if( prev )
1500 prev->next = NULL;
1501
1502 if( crt != chain )
1503 free( crt );
1504
1505 return( ret );
1506 }
1507
1508 if( first_error == 0 )
1509 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001510
1511 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001512
1513 memset( crt, 0, sizeof( x509_cert ) );
1514 continue;
1515 }
1516
1517 success = 1;
1518
1519 /*
1520 * Add new certificate to the list
1521 */
1522 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1523
1524 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001525 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001526
1527 prev = crt;
1528 crt = crt->next;
1529 memset( crt, 0, sizeof( x509_cert ) );
1530 }
1531 }
1532#endif
1533
1534 if( crt->version == 0 )
1535 {
1536 if( prev )
1537 prev->next = NULL;
1538
1539 if( crt != chain )
1540 free( crt );
1541 }
1542
1543 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001544 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001545 else if( first_error )
1546 return( first_error );
1547 else
1548 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001549}
1550
1551/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001552 * Parse one or more CRLs and add them to the chained list
1553 */
Paul Bakker23986e52011-04-24 08:57:21 +00001554int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555{
Paul Bakker23986e52011-04-24 08:57:21 +00001556 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001557 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001558 unsigned char *p, *end;
1559 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001560#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001561 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001562 pem_context pem;
1563#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001564
1565 crl = chain;
1566
1567 /*
1568 * Check for valid input
1569 */
1570 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001571 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001572
1573 while( crl->version != 0 && crl->next != NULL )
1574 crl = crl->next;
1575
1576 /*
1577 * Add new CRL on the end of the chain if needed.
1578 */
1579 if ( crl->version != 0 && crl->next == NULL)
1580 {
1581 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1582
Paul Bakker7d06ad22009-05-02 15:53:56 +00001583 if( crl->next == NULL )
1584 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001586 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001587 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588
Paul Bakker7d06ad22009-05-02 15:53:56 +00001589 crl = crl->next;
1590 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001591 }
1592
Paul Bakker96743fc2011-02-12 14:30:57 +00001593#if defined(POLARSSL_PEM_C)
1594 pem_init( &pem );
1595 ret = pem_read_buffer( &pem,
1596 "-----BEGIN X509 CRL-----",
1597 "-----END X509 CRL-----",
1598 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001599
Paul Bakker96743fc2011-02-12 14:30:57 +00001600 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001601 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001602 /*
1603 * Was PEM encoded
1604 */
1605 buflen -= use_len;
1606 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001607
1608 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001609 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001610 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001611 p = pem.buf;
1612 pem.buf = NULL;
1613 len = pem.buflen;
1614 pem_free( &pem );
1615 }
Paul Bakker9255e832013-06-06 14:58:28 +02001616 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001617 {
1618 pem_free( &pem );
1619 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001620 }
1621 else
1622 {
1623 /*
1624 * nope, copy the raw DER data
1625 */
1626 p = (unsigned char *) malloc( len = buflen );
1627
1628 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001629 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001630
1631 memcpy( p, buf, buflen );
1632
1633 buflen = 0;
1634 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001635#else
1636 p = (unsigned char *) malloc( len = buflen );
1637
1638 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001639 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001640
1641 memcpy( p, buf, buflen );
1642
1643 buflen = 0;
1644#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645
1646 crl->raw.p = p;
1647 crl->raw.len = len;
1648 end = p + len;
1649
1650 /*
1651 * CertificateList ::= SEQUENCE {
1652 * tbsCertList TBSCertList,
1653 * signatureAlgorithm AlgorithmIdentifier,
1654 * signatureValue BIT STRING }
1655 */
1656 if( ( ret = asn1_get_tag( &p, end, &len,
1657 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1658 {
1659 x509_crl_free( crl );
1660 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1661 }
1662
Paul Bakker23986e52011-04-24 08:57:21 +00001663 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664 {
1665 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001666 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001667 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1668 }
1669
1670 /*
1671 * TBSCertList ::= SEQUENCE {
1672 */
1673 crl->tbs.p = p;
1674
1675 if( ( ret = asn1_get_tag( &p, end, &len,
1676 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1677 {
1678 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001679 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001680 }
1681
1682 end = p + len;
1683 crl->tbs.len = end - crl->tbs.p;
1684
1685 /*
1686 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1687 * -- if present, MUST be v2
1688 *
1689 * signature AlgorithmIdentifier
1690 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001691 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001692 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1693 {
1694 x509_crl_free( crl );
1695 return( ret );
1696 }
1697
1698 crl->version++;
1699
1700 if( crl->version > 2 )
1701 {
1702 x509_crl_free( crl );
1703 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1704 }
1705
Paul Bakker27d66162010-03-17 06:56:01 +00001706 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001707 {
1708 x509_crl_free( crl );
1709 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1710 }
1711
1712 /*
1713 * issuer Name
1714 */
1715 crl->issuer_raw.p = p;
1716
1717 if( ( ret = asn1_get_tag( &p, end, &len,
1718 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1719 {
1720 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001721 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001722 }
1723
1724 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1725 {
1726 x509_crl_free( crl );
1727 return( ret );
1728 }
1729
1730 crl->issuer_raw.len = p - crl->issuer_raw.p;
1731
1732 /*
1733 * thisUpdate Time
1734 * nextUpdate Time OPTIONAL
1735 */
Paul Bakker91200182010-02-18 21:26:15 +00001736 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001737 {
1738 x509_crl_free( crl );
1739 return( ret );
1740 }
1741
Paul Bakker91200182010-02-18 21:26:15 +00001742 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001743 {
Paul Bakker9d781402011-05-09 16:17:09 +00001744 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001745 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001746 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001747 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001748 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001749 x509_crl_free( crl );
1750 return( ret );
1751 }
1752 }
1753
1754 /*
1755 * revokedCertificates SEQUENCE OF SEQUENCE {
1756 * userCertificate CertificateSerialNumber,
1757 * revocationDate Time,
1758 * crlEntryExtensions Extensions OPTIONAL
1759 * -- if present, MUST be v2
1760 * } OPTIONAL
1761 */
1762 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1763 {
1764 x509_crl_free( crl );
1765 return( ret );
1766 }
1767
1768 /*
1769 * crlExtensions EXPLICIT Extensions OPTIONAL
1770 * -- if present, MUST be v2
1771 */
1772 if( crl->version == 2 )
1773 {
1774 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1775
1776 if( ret != 0 )
1777 {
1778 x509_crl_free( crl );
1779 return( ret );
1780 }
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 end = crl->raw.p + crl->raw.len;
1791
1792 /*
1793 * signatureAlgorithm AlgorithmIdentifier,
1794 * signatureValue BIT STRING
1795 */
1796 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1797 {
1798 x509_crl_free( crl );
1799 return( ret );
1800 }
1801
Paul Bakker535e97d2012-08-23 10:49:55 +00001802 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1803 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001804 {
1805 x509_crl_free( crl );
1806 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1807 }
1808
1809 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1810 {
1811 x509_crl_free( crl );
1812 return( ret );
1813 }
1814
1815 if( p != end )
1816 {
1817 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001818 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001819 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1820 }
1821
1822 if( buflen > 0 )
1823 {
1824 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1825
Paul Bakker7d06ad22009-05-02 15:53:56 +00001826 if( crl->next == NULL )
1827 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001828 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001829 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001830 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001831
Paul Bakker7d06ad22009-05-02 15:53:56 +00001832 crl = crl->next;
1833 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834
1835 return( x509parse_crl( crl, buf, buflen ) );
1836 }
1837
1838 return( 0 );
1839}
1840
Paul Bakker335db3f2011-04-25 15:28:35 +00001841#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843 * Load all data from a file into a given buffer.
1844 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001845int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001846{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001847 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001848
Paul Bakkerd98030e2009-05-02 15:13:40 +00001849 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001850 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001851
Paul Bakkerd98030e2009-05-02 15:13:40 +00001852 fseek( f, 0, SEEK_END );
1853 *n = (size_t) ftell( f );
1854 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001855
Paul Bakkerd98030e2009-05-02 15:13:40 +00001856 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001857 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001858
Paul Bakkerd98030e2009-05-02 15:13:40 +00001859 if( fread( *buf, 1, *n, f ) != *n )
1860 {
1861 fclose( f );
1862 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001863 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001864 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001865
Paul Bakkerd98030e2009-05-02 15:13:40 +00001866 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001867
Paul Bakkerd98030e2009-05-02 15:13:40 +00001868 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001869
Paul Bakkerd98030e2009-05-02 15:13:40 +00001870 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001871}
1872
1873/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 * Load one or more certificates and add them to the chained list
1875 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001876int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001877{
1878 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 size_t n;
1880 unsigned char *buf;
1881
Paul Bakker69e095c2011-12-10 21:55:01 +00001882 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1883 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Paul Bakker69e095c2011-12-10 21:55:01 +00001885 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 memset( buf, 0, n + 1 );
1888 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001889
1890 return( ret );
1891}
1892
Paul Bakker8d914582012-06-04 12:46:42 +00001893int x509parse_crtpath( x509_cert *chain, const char *path )
1894{
1895 int ret = 0;
1896#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001897 int w_ret;
1898 WCHAR szDir[MAX_PATH];
1899 char filename[MAX_PATH];
1900 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001901 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001902
Paul Bakker97872ac2012-11-02 12:53:26 +00001903 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001904 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001905
1906 if( len > MAX_PATH - 3 )
1907 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001908
Paul Bakker3338b792012-10-01 21:13:10 +00001909 memset( szDir, 0, sizeof(szDir) );
1910 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001911 memcpy( filename, path, len );
1912 filename[len++] = '\\';
1913 p = filename + len;
1914 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001915
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001916 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001917
Paul Bakker97872ac2012-11-02 12:53:26 +00001918 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001919 if (hFind == INVALID_HANDLE_VALUE)
1920 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1921
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001922 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001923 do
1924 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001925 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001926
Paul Bakkere4791f32012-06-04 21:29:15 +00001927 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001928 continue;
1929
Paul Bakker3338b792012-10-01 21:13:10 +00001930 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1931 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001932 p, len - 1,
1933 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001934
Paul Bakker3338b792012-10-01 21:13:10 +00001935 w_ret = x509parse_crtfile( chain, filename );
1936 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001937 {
1938 ret = w_ret;
1939 goto cleanup;
1940 }
Paul Bakker3338b792012-10-01 21:13:10 +00001941
1942 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001943 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001944 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001945
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001946 if (GetLastError() != ERROR_NO_MORE_FILES)
1947 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001948
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001949cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001950 FindClose( hFind );
1951#else
1952 int t_ret;
1953 struct dirent *entry;
1954 char entry_name[255];
1955 DIR *dir = opendir( path );
1956
1957 if( dir == NULL)
1958 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1959
1960 while( ( entry = readdir( dir ) ) != NULL )
1961 {
1962 if( entry->d_type != DT_REG )
1963 continue;
1964
1965 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1966 t_ret = x509parse_crtfile( chain, entry_name );
1967 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001968 {
1969 ret = t_ret;
1970 break;
1971 }
Paul Bakker8d914582012-06-04 12:46:42 +00001972
1973 ret += t_ret;
1974 }
1975 closedir( dir );
1976#endif
1977
1978 return( ret );
1979}
1980
Paul Bakkerd98030e2009-05-02 15:13:40 +00001981/*
1982 * Load one or more CRLs and add them to the chained list
1983 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001984int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001985{
1986 int ret;
1987 size_t n;
1988 unsigned char *buf;
1989
Paul Bakker69e095c2011-12-10 21:55:01 +00001990 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1991 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001992
Paul Bakker27fdf462011-06-09 13:55:13 +00001993 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001994
1995 memset( buf, 0, n + 1 );
1996 free( buf );
1997
1998 return( ret );
1999}
2000
Paul Bakker5121ce52009-01-03 21:22:43 +00002001/*
Paul Bakker335db3f2011-04-25 15:28:35 +00002002 * Load and parse a private RSA key
2003 */
2004int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
2005{
2006 int ret;
2007 size_t n;
2008 unsigned char *buf;
2009
Paul Bakker69e095c2011-12-10 21:55:01 +00002010 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2011 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002012
2013 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00002014 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00002015 else
Paul Bakker27fdf462011-06-09 13:55:13 +00002016 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00002017 (unsigned char *) pwd, strlen( pwd ) );
2018
2019 memset( buf, 0, n + 1 );
2020 free( buf );
2021
2022 return( ret );
2023}
2024
2025/*
2026 * Load and parse a public RSA key
2027 */
2028int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2029{
2030 int ret;
2031 size_t n;
2032 unsigned char *buf;
2033
Paul Bakker69e095c2011-12-10 21:55:01 +00002034 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2035 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002036
Paul Bakker27fdf462011-06-09 13:55:13 +00002037 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002038
2039 memset( buf, 0, n + 1 );
2040 free( buf );
2041
2042 return( ret );
2043}
2044#endif /* POLARSSL_FS_IO */
2045
2046/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 * Parse a private RSA key
2048 */
Paul Bakker23986e52011-04-24 08:57:21 +00002049int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2050 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051{
Paul Bakker23986e52011-04-24 08:57:21 +00002052 int ret;
2053 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002055 unsigned char *p_alt;
2056 x509_buf pk_alg_oid;
2057
Paul Bakker96743fc2011-02-12 14:30:57 +00002058#if defined(POLARSSL_PEM_C)
2059 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
Paul Bakker96743fc2011-02-12 14:30:57 +00002061 pem_init( &pem );
2062 ret = pem_read_buffer( &pem,
2063 "-----BEGIN RSA PRIVATE KEY-----",
2064 "-----END RSA PRIVATE KEY-----",
2065 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Paul Bakker9255e832013-06-06 14:58:28 +02002067 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00002068 {
2069 ret = pem_read_buffer( &pem,
2070 "-----BEGIN PRIVATE KEY-----",
2071 "-----END PRIVATE KEY-----",
2072 key, pwd, pwdlen, &len );
2073 }
2074
Paul Bakker96743fc2011-02-12 14:30:57 +00002075 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002077 /*
2078 * Was PEM encoded
2079 */
2080 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 }
Paul Bakker9255e832013-06-06 14:58:28 +02002082 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002083 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002084 pem_free( &pem );
2085 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Paul Bakker96743fc2011-02-12 14:30:57 +00002088 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2089#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002090 ((void) pwd);
2091 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002092 p = (unsigned char *) key;
2093#endif
2094 end = p + keylen;
2095
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002097 * Note: Depending on the type of private key file one can expect either a
2098 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2099 *
2100 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002101 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002102 * algorithm AlgorithmIdentifier,
2103 * PrivateKey BIT STRING
2104 * }
2105 *
2106 * AlgorithmIdentifier ::= SEQUENCE {
2107 * algorithm OBJECT IDENTIFIER,
2108 * parameters ANY DEFINED BY algorithm OPTIONAL
2109 * }
2110 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 * RSAPrivateKey ::= SEQUENCE {
2112 * version Version,
2113 * modulus INTEGER, -- n
2114 * publicExponent INTEGER, -- e
2115 * privateExponent INTEGER, -- d
2116 * prime1 INTEGER, -- p
2117 * prime2 INTEGER, -- q
2118 * exponent1 INTEGER, -- d mod (p-1)
2119 * exponent2 INTEGER, -- d mod (q-1)
2120 * coefficient INTEGER, -- (inverse of q) mod p
2121 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2122 * }
2123 */
2124 if( ( ret = asn1_get_tag( &p, end, &len,
2125 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2126 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002127#if defined(POLARSSL_PEM_C)
2128 pem_free( &pem );
2129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002131 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
2133
2134 end = p + len;
2135
2136 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2137 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002138#if defined(POLARSSL_PEM_C)
2139 pem_free( &pem );
2140#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002142 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 }
2144
2145 if( rsa->ver != 0 )
2146 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002147#if defined(POLARSSL_PEM_C)
2148 pem_free( &pem );
2149#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002151 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153
Paul Bakkered56b222011-07-13 11:26:43 +00002154 p_alt = p;
2155
2156 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2157 {
2158 // Assume that we have the PKCS#1 format if wrong
2159 // tag was encountered
2160 //
2161 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2162 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2163 {
2164#if defined(POLARSSL_PEM_C)
2165 pem_free( &pem );
2166#endif
2167 rsa_free( rsa );
2168 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2169 }
2170 }
2171 else
2172 {
2173 int can_handle;
2174
2175 /*
2176 * only RSA keys handled at this time
2177 */
2178 can_handle = 0;
2179
2180 if( pk_alg_oid.len == 9 &&
2181 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2182 can_handle = 1;
2183
2184 if( pk_alg_oid.len == 9 &&
2185 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2186 {
2187 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2188 can_handle = 1;
2189
2190 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2191 can_handle = 1;
2192 }
2193
2194 if( pk_alg_oid.len == 5 &&
2195 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2196 can_handle = 1;
2197
2198 if( can_handle == 0 )
2199 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2200
2201 /*
2202 * Parse the PKCS#8 format
2203 */
2204
2205 p = p_alt;
2206 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2207 {
2208#if defined(POLARSSL_PEM_C)
2209 pem_free( &pem );
2210#endif
2211 rsa_free( rsa );
2212 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2213 }
2214
2215 if( ( end - p ) < 1 )
2216 {
2217#if defined(POLARSSL_PEM_C)
2218 pem_free( &pem );
2219#endif
2220 rsa_free( rsa );
2221 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2222 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2223 }
2224
2225 end = p + len;
2226
2227 if( ( ret = asn1_get_tag( &p, end, &len,
2228 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2229 {
2230#if defined(POLARSSL_PEM_C)
2231 pem_free( &pem );
2232#endif
2233 rsa_free( rsa );
2234 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2235 }
2236
2237 end = p + len;
2238
2239 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2240 {
2241#if defined(POLARSSL_PEM_C)
2242 pem_free( &pem );
2243#endif
2244 rsa_free( rsa );
2245 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2246 }
2247
2248 if( rsa->ver != 0 )
2249 {
2250#if defined(POLARSSL_PEM_C)
2251 pem_free( &pem );
2252#endif
2253 rsa_free( rsa );
2254 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2255 }
2256 }
2257
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2259 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2260 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2261 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2262 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2263 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2264 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2265 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2266 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002267#if defined(POLARSSL_PEM_C)
2268 pem_free( &pem );
2269#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002271 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 }
2273
2274 rsa->len = mpi_size( &rsa->N );
2275
2276 if( p != end )
2277 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002278#if defined(POLARSSL_PEM_C)
2279 pem_free( &pem );
2280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002282 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002283 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285
2286 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2287 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002288#if defined(POLARSSL_PEM_C)
2289 pem_free( &pem );
2290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 rsa_free( rsa );
2292 return( ret );
2293 }
2294
Paul Bakker96743fc2011-02-12 14:30:57 +00002295#if defined(POLARSSL_PEM_C)
2296 pem_free( &pem );
2297#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 return( 0 );
2300}
2301
2302/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002303 * Parse a public RSA key
2304 */
Paul Bakker23986e52011-04-24 08:57:21 +00002305int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002306{
Paul Bakker23986e52011-04-24 08:57:21 +00002307 int ret;
2308 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002309 unsigned char *p, *end;
2310 x509_buf alg_oid;
2311#if defined(POLARSSL_PEM_C)
2312 pem_context pem;
2313
2314 pem_init( &pem );
2315 ret = pem_read_buffer( &pem,
2316 "-----BEGIN PUBLIC KEY-----",
2317 "-----END PUBLIC KEY-----",
2318 key, NULL, 0, &len );
2319
2320 if( ret == 0 )
2321 {
2322 /*
2323 * Was PEM encoded
2324 */
2325 keylen = pem.buflen;
2326 }
Paul Bakker9255e832013-06-06 14:58:28 +02002327 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002328 {
2329 pem_free( &pem );
2330 return( ret );
2331 }
2332
2333 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2334#else
2335 p = (unsigned char *) key;
2336#endif
2337 end = p + keylen;
2338
2339 /*
2340 * PublicKeyInfo ::= SEQUENCE {
2341 * algorithm AlgorithmIdentifier,
2342 * PublicKey BIT STRING
2343 * }
2344 *
2345 * AlgorithmIdentifier ::= SEQUENCE {
2346 * algorithm OBJECT IDENTIFIER,
2347 * parameters ANY DEFINED BY algorithm OPTIONAL
2348 * }
2349 *
2350 * RSAPublicKey ::= SEQUENCE {
2351 * modulus INTEGER, -- n
2352 * publicExponent INTEGER -- e
2353 * }
2354 */
2355
2356 if( ( ret = asn1_get_tag( &p, end, &len,
2357 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2358 {
2359#if defined(POLARSSL_PEM_C)
2360 pem_free( &pem );
2361#endif
2362 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002363 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002364 }
2365
2366 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2367 {
2368#if defined(POLARSSL_PEM_C)
2369 pem_free( &pem );
2370#endif
2371 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002372 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002373 }
2374
2375 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2376 {
2377#if defined(POLARSSL_PEM_C)
2378 pem_free( &pem );
2379#endif
2380 rsa_free( rsa );
2381 return( ret );
2382 }
2383
2384 rsa->len = mpi_size( &rsa->N );
2385
2386#if defined(POLARSSL_PEM_C)
2387 pem_free( &pem );
2388#endif
2389
2390 return( 0 );
2391}
2392
Paul Bakkereaa89f82011-04-04 21:36:15 +00002393#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002394/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002395 * Parse DHM parameters
2396 */
Paul Bakker23986e52011-04-24 08:57:21 +00002397int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002398{
Paul Bakker23986e52011-04-24 08:57:21 +00002399 int ret;
2400 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002401 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002402#if defined(POLARSSL_PEM_C)
2403 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002404
Paul Bakker96743fc2011-02-12 14:30:57 +00002405 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002406
Paul Bakker96743fc2011-02-12 14:30:57 +00002407 ret = pem_read_buffer( &pem,
2408 "-----BEGIN DH PARAMETERS-----",
2409 "-----END DH PARAMETERS-----",
2410 dhmin, NULL, 0, &dhminlen );
2411
2412 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002413 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002414 /*
2415 * Was PEM encoded
2416 */
2417 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002418 }
Paul Bakker9255e832013-06-06 14:58:28 +02002419 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002420 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002421 pem_free( &pem );
2422 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002423 }
2424
Paul Bakker96743fc2011-02-12 14:30:57 +00002425 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2426#else
2427 p = (unsigned char *) dhmin;
2428#endif
2429 end = p + dhminlen;
2430
Paul Bakker1b57b062011-01-06 15:48:19 +00002431 memset( dhm, 0, sizeof( dhm_context ) );
2432
Paul Bakker1b57b062011-01-06 15:48:19 +00002433 /*
2434 * DHParams ::= SEQUENCE {
2435 * prime INTEGER, -- P
2436 * generator INTEGER, -- g
2437 * }
2438 */
2439 if( ( ret = asn1_get_tag( &p, end, &len,
2440 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2441 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002442#if defined(POLARSSL_PEM_C)
2443 pem_free( &pem );
2444#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002445 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002446 }
2447
2448 end = p + len;
2449
2450 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2451 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2452 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002453#if defined(POLARSSL_PEM_C)
2454 pem_free( &pem );
2455#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002456 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002457 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002458 }
2459
2460 if( p != end )
2461 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002462#if defined(POLARSSL_PEM_C)
2463 pem_free( &pem );
2464#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002465 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002466 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002467 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2468 }
2469
Paul Bakker96743fc2011-02-12 14:30:57 +00002470#if defined(POLARSSL_PEM_C)
2471 pem_free( &pem );
2472#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002473
2474 return( 0 );
2475}
2476
Paul Bakker335db3f2011-04-25 15:28:35 +00002477#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002478/*
2479 * Load and parse a private RSA key
2480 */
2481int x509parse_dhmfile( dhm_context *dhm, const char *path )
2482{
2483 int ret;
2484 size_t n;
2485 unsigned char *buf;
2486
Paul Bakker69e095c2011-12-10 21:55:01 +00002487 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2488 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002489
Paul Bakker27fdf462011-06-09 13:55:13 +00002490 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002491
2492 memset( buf, 0, n + 1 );
2493 free( buf );
2494
2495 return( ret );
2496}
Paul Bakker335db3f2011-04-25 15:28:35 +00002497#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002498#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002499
Paul Bakker5121ce52009-01-03 21:22:43 +00002500#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002501#include <stdarg.h>
2502
2503#if !defined vsnprintf
2504#define vsnprintf _vsnprintf
2505#endif // vsnprintf
2506
2507/*
2508 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2509 * Result value is not size of buffer needed, but -1 if no fit is possible.
2510 *
2511 * This fuction tries to 'fix' this by at least suggesting enlarging the
2512 * size by 20.
2513 */
2514int compat_snprintf(char *str, size_t size, const char *format, ...)
2515{
2516 va_list ap;
2517 int res = -1;
2518
2519 va_start( ap, format );
2520
2521 res = vsnprintf( str, size, format, ap );
2522
2523 va_end( ap );
2524
2525 // No quick fix possible
2526 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002527 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002528
2529 return res;
2530}
2531
2532#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002533#endif
2534
Paul Bakkerd98030e2009-05-02 15:13:40 +00002535#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2536
2537#define SAFE_SNPRINTF() \
2538{ \
2539 if( ret == -1 ) \
2540 return( -1 ); \
2541 \
Paul Bakker23986e52011-04-24 08:57:21 +00002542 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002543 p[n - 1] = '\0'; \
2544 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2545 } \
2546 \
Paul Bakker23986e52011-04-24 08:57:21 +00002547 n -= (unsigned int) ret; \
2548 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002549}
2550
Paul Bakker5121ce52009-01-03 21:22:43 +00002551/*
2552 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002553 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002555int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002556{
Paul Bakker23986e52011-04-24 08:57:21 +00002557 int ret;
2558 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002560 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 char s[128], *p;
2562
2563 memset( s, 0, sizeof( s ) );
2564
2565 name = dn;
2566 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 while( name != NULL )
2570 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002571 if( !name->oid.p )
2572 {
2573 name = name->next;
2574 continue;
2575 }
2576
Paul Bakker74111d32011-01-15 16:57:55 +00002577 if( name != dn )
2578 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002579 ret = snprintf( p, n, ", " );
2580 SAFE_SNPRINTF();
2581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Paul Bakker535e97d2012-08-23 10:49:55 +00002583 if( name->oid.len == 3 &&
2584 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 {
2586 switch( name->oid.p[2] )
2587 {
2588 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
2591 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002592 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
2594 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002595 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002596
2597 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002598 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599
2600 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002601 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
2603 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002604 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
2606 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002607 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 name->oid.p[2] );
2609 break;
2610 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002611 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002613 else if( name->oid.len == 9 &&
2614 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 {
2616 switch( name->oid.p[8] )
2617 {
2618 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002619 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
2621 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002622 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002623 name->oid.p[8] );
2624 break;
2625 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002626 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 }
2628 else
Paul Bakker74111d32011-01-15 16:57:55 +00002629 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002631 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002633
2634 for( i = 0; i < name->val.len; i++ )
2635 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002636 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 break;
2638
2639 c = name->val.p[i];
2640 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2641 s[i] = '?';
2642 else s[i] = c;
2643 }
2644 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002645 ret = snprintf( p, n, "%s", s );
2646 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 name = name->next;
2648 }
2649
Paul Bakker23986e52011-04-24 08:57:21 +00002650 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651}
2652
2653/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002654 * Store the serial in printable form into buf; no more
2655 * than size characters will be written
2656 */
2657int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2658{
Paul Bakker23986e52011-04-24 08:57:21 +00002659 int ret;
2660 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002661 char *p;
2662
2663 p = buf;
2664 n = size;
2665
2666 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002667 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002668
2669 for( i = 0; i < nr; i++ )
2670 {
Paul Bakker93048802011-12-05 14:38:06 +00002671 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002672 continue;
2673
Paul Bakkerdd476992011-01-16 21:34:59 +00002674 ret = snprintf( p, n, "%02X%s",
2675 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2676 SAFE_SNPRINTF();
2677 }
2678
Paul Bakker03c7c252011-11-25 12:37:37 +00002679 if( nr != serial->len )
2680 {
2681 ret = snprintf( p, n, "...." );
2682 SAFE_SNPRINTF();
2683 }
2684
Paul Bakker23986e52011-04-24 08:57:21 +00002685 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002686}
2687
2688/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002689 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002691int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2692 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002693{
Paul Bakker23986e52011-04-24 08:57:21 +00002694 int ret;
2695 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002696 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002699 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Paul Bakkerd98030e2009-05-02 15:13:40 +00002701 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002703 SAFE_SNPRINTF();
2704 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Paul Bakkerdd476992011-01-16 21:34:59 +00002708 ret = x509parse_serial_gets( p, n, &crt->serial);
2709 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Paul Bakkerd98030e2009-05-02 15:13:40 +00002711 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2712 SAFE_SNPRINTF();
2713 ret = x509parse_dn_gets( p, n, &crt->issuer );
2714 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Paul Bakkerd98030e2009-05-02 15:13:40 +00002716 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2717 SAFE_SNPRINTF();
2718 ret = x509parse_dn_gets( p, n, &crt->subject );
2719 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakkerd98030e2009-05-02 15:13:40 +00002721 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2723 crt->valid_from.year, crt->valid_from.mon,
2724 crt->valid_from.day, crt->valid_from.hour,
2725 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002726 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002727
Paul Bakkerd98030e2009-05-02 15:13:40 +00002728 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2730 crt->valid_to.year, crt->valid_to.mon,
2731 crt->valid_to.day, crt->valid_to.hour,
2732 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002733 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2736 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
Paul Bakker27d66162010-03-17 06:56:01 +00002738 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002740 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2741 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2742 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2743 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2744 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2745 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2746 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2747 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2748 default: ret = snprintf( p, n, "???" ); break;
2749 }
2750 SAFE_SNPRINTF();
2751
2752 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002753 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002754 SAFE_SNPRINTF();
2755
Paul Bakker23986e52011-04-24 08:57:21 +00002756 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002757}
2758
Paul Bakker74111d32011-01-15 16:57:55 +00002759/* Compare a given OID string with an OID x509_buf * */
2760#define OID_CMP(oid_str, oid_buf) \
2761 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2762 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2763
2764/*
2765 * Return an informational string describing the given OID
2766 */
2767const char *x509_oid_get_description( x509_buf *oid )
2768{
2769 if ( oid == NULL )
2770 return ( NULL );
2771
2772 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2773 return( STRING_SERVER_AUTH );
2774
2775 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2776 return( STRING_CLIENT_AUTH );
2777
2778 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2779 return( STRING_CODE_SIGNING );
2780
2781 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2782 return( STRING_EMAIL_PROTECTION );
2783
2784 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2785 return( STRING_TIME_STAMPING );
2786
2787 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2788 return( STRING_OCSP_SIGNING );
2789
2790 return( NULL );
2791}
2792
2793/* Return the x.y.z.... style numeric string for the given OID */
2794int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2795{
Paul Bakker23986e52011-04-24 08:57:21 +00002796 int ret;
2797 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002798 unsigned int value;
2799 char *p;
2800
2801 p = buf;
2802 n = size;
2803
2804 /* First byte contains first two dots */
2805 if( oid->len > 0 )
2806 {
2807 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2808 SAFE_SNPRINTF();
2809 }
2810
2811 /* TODO: value can overflow in value. */
2812 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002813 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002814 {
2815 value <<= 7;
2816 value += oid->p[i] & 0x7F;
2817
2818 if( !( oid->p[i] & 0x80 ) )
2819 {
2820 /* Last byte */
2821 ret = snprintf( p, n, ".%d", value );
2822 SAFE_SNPRINTF();
2823 value = 0;
2824 }
2825 }
2826
Paul Bakker23986e52011-04-24 08:57:21 +00002827 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002828}
2829
Paul Bakkerd98030e2009-05-02 15:13:40 +00002830/*
2831 * Return an informational string about the CRL.
2832 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002833int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2834 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002835{
Paul Bakker23986e52011-04-24 08:57:21 +00002836 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002837 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002838 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002839 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002840
2841 p = buf;
2842 n = size;
2843
2844 ret = snprintf( p, n, "%sCRL version : %d",
2845 prefix, crl->version );
2846 SAFE_SNPRINTF();
2847
2848 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2849 SAFE_SNPRINTF();
2850 ret = x509parse_dn_gets( p, n, &crl->issuer );
2851 SAFE_SNPRINTF();
2852
2853 ret = snprintf( p, n, "\n%sthis update : " \
2854 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2855 crl->this_update.year, crl->this_update.mon,
2856 crl->this_update.day, crl->this_update.hour,
2857 crl->this_update.min, crl->this_update.sec );
2858 SAFE_SNPRINTF();
2859
2860 ret = snprintf( p, n, "\n%snext update : " \
2861 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2862 crl->next_update.year, crl->next_update.mon,
2863 crl->next_update.day, crl->next_update.hour,
2864 crl->next_update.min, crl->next_update.sec );
2865 SAFE_SNPRINTF();
2866
2867 entry = &crl->entry;
2868
2869 ret = snprintf( p, n, "\n%sRevoked certificates:",
2870 prefix );
2871 SAFE_SNPRINTF();
2872
Paul Bakker9be19372009-07-27 20:21:53 +00002873 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002874 {
2875 ret = snprintf( p, n, "\n%sserial number: ",
2876 prefix );
2877 SAFE_SNPRINTF();
2878
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002879 ret = x509parse_serial_gets( p, n, &entry->serial);
2880 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002881
Paul Bakkerd98030e2009-05-02 15:13:40 +00002882 ret = snprintf( p, n, " revocation date: " \
2883 "%04d-%02d-%02d %02d:%02d:%02d",
2884 entry->revocation_date.year, entry->revocation_date.mon,
2885 entry->revocation_date.day, entry->revocation_date.hour,
2886 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002887 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002888
2889 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 }
2891
Paul Bakkerd98030e2009-05-02 15:13:40 +00002892 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2893 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002894
Paul Bakker27d66162010-03-17 06:56:01 +00002895 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002896 {
2897 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2898 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2899 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2900 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2901 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2902 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2903 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2904 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2905 default: ret = snprintf( p, n, "???" ); break;
2906 }
2907 SAFE_SNPRINTF();
2908
Paul Bakker1e27bb22009-07-19 20:25:25 +00002909 ret = snprintf( p, n, "\n" );
2910 SAFE_SNPRINTF();
2911
Paul Bakker23986e52011-04-24 08:57:21 +00002912 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913}
2914
2915/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002916 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002918int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919{
Paul Bakkercce9d772011-11-18 14:26:47 +00002920 int year, mon, day;
2921 int hour, min, sec;
2922
2923#if defined(_WIN32)
2924 SYSTEMTIME st;
2925
2926 GetLocalTime(&st);
2927
2928 year = st.wYear;
2929 mon = st.wMonth;
2930 day = st.wDay;
2931 hour = st.wHour;
2932 min = st.wMinute;
2933 sec = st.wSecond;
2934#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 struct tm *lt;
2936 time_t tt;
2937
2938 tt = time( NULL );
2939 lt = localtime( &tt );
2940
Paul Bakkercce9d772011-11-18 14:26:47 +00002941 year = lt->tm_year + 1900;
2942 mon = lt->tm_mon + 1;
2943 day = lt->tm_mday;
2944 hour = lt->tm_hour;
2945 min = lt->tm_min;
2946 sec = lt->tm_sec;
2947#endif
2948
2949 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002950 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Paul Bakkercce9d772011-11-18 14:26:47 +00002952 if( year == to->year &&
2953 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002954 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakkercce9d772011-11-18 14:26:47 +00002956 if( year == to->year &&
2957 mon == to->mon &&
2958 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002959 return( 1 );
2960
Paul Bakkercce9d772011-11-18 14:26:47 +00002961 if( year == to->year &&
2962 mon == to->mon &&
2963 day == to->day &&
2964 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002965 return( 1 );
2966
Paul Bakkercce9d772011-11-18 14:26:47 +00002967 if( year == to->year &&
2968 mon == to->mon &&
2969 day == to->day &&
2970 hour == to->hour &&
2971 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002972 return( 1 );
2973
Paul Bakkercce9d772011-11-18 14:26:47 +00002974 if( year == to->year &&
2975 mon == to->mon &&
2976 day == to->day &&
2977 hour == to->hour &&
2978 min == to->min &&
2979 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002980 return( 1 );
2981
Paul Bakker40ea7de2009-05-03 10:18:48 +00002982 return( 0 );
2983}
2984
2985/*
2986 * Return 1 if the certificate is revoked, or 0 otherwise.
2987 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002988int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002989{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002990 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002991
2992 while( cur != NULL && cur->serial.len != 0 )
2993 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002994 if( crt->serial.len == cur->serial.len &&
2995 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002996 {
2997 if( x509parse_time_expired( &cur->revocation_date ) )
2998 return( 1 );
2999 }
3000
3001 cur = cur->next;
3002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
3004 return( 0 );
3005}
3006
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003007/*
3008 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003009 */
Paul Bakker23986e52011-04-24 08:57:21 +00003010static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 unsigned char *out )
3012{
3013 switch( alg )
3014 {
Paul Bakker40e46942009-01-03 21:51:57 +00003015#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003016 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017#endif
Paul Bakker40e46942009-01-03 21:51:57 +00003018#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003019 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003020#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003021#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003022 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003023#endif
3024#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003025 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003026#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003027#if defined(POLARSSL_SHA2_C)
3028 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3029 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3030#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003031#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003032 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3033 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003036 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 break;
3038 }
3039}
3040
3041/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003042 * Check that the given certificate is valid accoring to the CRL.
3043 */
3044static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3045 x509_crl *crl_list)
3046{
3047 int flags = 0;
3048 int hash_id;
3049 unsigned char hash[64];
3050
Paul Bakker915275b2012-09-28 07:10:55 +00003051 if( ca == NULL )
3052 return( flags );
3053
Paul Bakker76fd75a2011-01-16 21:12:10 +00003054 /*
3055 * TODO: What happens if no CRL is present?
3056 * Suggestion: Revocation state should be unknown if no CRL is present.
3057 * For backwards compatibility this is not yet implemented.
3058 */
3059
Paul Bakker915275b2012-09-28 07:10:55 +00003060 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003061 {
Paul Bakker915275b2012-09-28 07:10:55 +00003062 if( crl_list->version == 0 ||
3063 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003064 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3065 crl_list->issuer_raw.len ) != 0 )
3066 {
3067 crl_list = crl_list->next;
3068 continue;
3069 }
3070
3071 /*
3072 * Check if CRL is correctly signed by the trusted CA
3073 */
3074 hash_id = crl_list->sig_alg;
3075
3076 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3077
3078 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3079 0, hash, crl_list->sig.p ) == 0 )
3080 {
3081 /*
3082 * CRL is not trusted
3083 */
3084 flags |= BADCRL_NOT_TRUSTED;
3085 break;
3086 }
3087
3088 /*
3089 * Check for validity of CRL (Do not drop out)
3090 */
3091 if( x509parse_time_expired( &crl_list->next_update ) )
3092 flags |= BADCRL_EXPIRED;
3093
3094 /*
3095 * Check if certificate is revoked
3096 */
3097 if( x509parse_revoked(crt, crl_list) )
3098 {
3099 flags |= BADCERT_REVOKED;
3100 break;
3101 }
3102
3103 crl_list = crl_list->next;
3104 }
3105 return flags;
3106}
3107
Paul Bakker57b12982012-02-11 17:38:38 +00003108int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003109{
3110 size_t i;
3111 size_t cn_idx = 0;
3112
Paul Bakker57b12982012-02-11 17:38:38 +00003113 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003114 return( 0 );
3115
3116 for( i = 0; i < strlen( cn ); ++i )
3117 {
3118 if( cn[i] == '.' )
3119 {
3120 cn_idx = i;
3121 break;
3122 }
3123 }
3124
3125 if( cn_idx == 0 )
3126 return( 0 );
3127
Paul Bakker535e97d2012-08-23 10:49:55 +00003128 if( strlen( cn ) - cn_idx == name->len - 1 &&
3129 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003130 {
3131 return( 1 );
3132 }
3133
3134 return( 0 );
3135}
3136
Paul Bakker915275b2012-09-28 07:10:55 +00003137static int x509parse_verify_top(
3138 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003139 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003140 int (*f_vrfy)(void *, x509_cert *, int, int *),
3141 void *p_vrfy )
3142{
3143 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003144 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003145 unsigned char hash[64];
3146
3147 if( x509parse_time_expired( &child->valid_to ) )
3148 *flags |= BADCERT_EXPIRED;
3149
3150 /*
3151 * Child is the top of the chain. Check against the trust_ca list.
3152 */
3153 *flags |= BADCERT_NOT_TRUSTED;
3154
3155 while( trust_ca != NULL )
3156 {
3157 if( trust_ca->version == 0 ||
3158 child->issuer_raw.len != trust_ca->subject_raw.len ||
3159 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3160 child->issuer_raw.len ) != 0 )
3161 {
3162 trust_ca = trust_ca->next;
3163 continue;
3164 }
3165
Paul Bakker9a736322012-11-14 12:39:52 +00003166 /*
3167 * Reduce path_len to check against if top of the chain is
3168 * the same as the trusted CA
3169 */
3170 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3171 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3172 child->issuer_raw.len ) == 0 )
3173 {
3174 check_path_cnt--;
3175 }
3176
Paul Bakker915275b2012-09-28 07:10:55 +00003177 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003178 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003179 {
3180 trust_ca = trust_ca->next;
3181 continue;
3182 }
3183
3184 hash_id = child->sig_alg;
3185
3186 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3187
3188 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3189 0, hash, child->sig.p ) != 0 )
3190 {
3191 trust_ca = trust_ca->next;
3192 continue;
3193 }
3194
3195 /*
3196 * Top of chain is signed by a trusted CA
3197 */
3198 *flags &= ~BADCERT_NOT_TRUSTED;
3199 break;
3200 }
3201
Paul Bakker9a736322012-11-14 12:39:52 +00003202 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003203 * If top of chain is not the same as the trusted CA send a verify request
3204 * to the callback for any issues with validity and CRL presence for the
3205 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003206 */
3207 if( trust_ca != NULL &&
3208 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3209 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3210 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003211 {
3212 /* Check trusted CA's CRL for then chain's top crt */
3213 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3214
3215 if( x509parse_time_expired( &trust_ca->valid_to ) )
3216 ca_flags |= BADCERT_EXPIRED;
3217
Paul Bakker915275b2012-09-28 07:10:55 +00003218 if( NULL != f_vrfy )
3219 {
Paul Bakker9a736322012-11-14 12:39:52 +00003220 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003221 return( ret );
3222 }
3223 }
3224
3225 /* Call callback on top cert */
3226 if( NULL != f_vrfy )
3227 {
Paul Bakker9a736322012-11-14 12:39:52 +00003228 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003229 return( ret );
3230 }
3231
Paul Bakker915275b2012-09-28 07:10:55 +00003232 *flags |= ca_flags;
3233
3234 return( 0 );
3235}
3236
3237static int x509parse_verify_child(
3238 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003239 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003240 int (*f_vrfy)(void *, x509_cert *, int, int *),
3241 void *p_vrfy )
3242{
3243 int hash_id, ret;
3244 int parent_flags = 0;
3245 unsigned char hash[64];
3246 x509_cert *grandparent;
3247
3248 if( x509parse_time_expired( &child->valid_to ) )
3249 *flags |= BADCERT_EXPIRED;
3250
3251 hash_id = child->sig_alg;
3252
3253 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3254
3255 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3256 child->sig.p ) != 0 )
3257 *flags |= BADCERT_NOT_TRUSTED;
3258
3259 /* Check trusted CA's CRL for the given crt */
3260 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3261
3262 grandparent = parent->next;
3263
3264 while( grandparent != NULL )
3265 {
3266 if( grandparent->version == 0 ||
3267 grandparent->ca_istrue == 0 ||
3268 parent->issuer_raw.len != grandparent->subject_raw.len ||
3269 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3270 parent->issuer_raw.len ) != 0 )
3271 {
3272 grandparent = grandparent->next;
3273 continue;
3274 }
3275 break;
3276 }
3277
Paul Bakker915275b2012-09-28 07:10:55 +00003278 if( grandparent != NULL )
3279 {
3280 /*
3281 * Part of the chain
3282 */
Paul Bakker9a736322012-11-14 12:39:52 +00003283 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 +00003284 if( ret != 0 )
3285 return( ret );
3286 }
3287 else
3288 {
Paul Bakker9a736322012-11-14 12:39:52 +00003289 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 +00003290 if( ret != 0 )
3291 return( ret );
3292 }
3293
3294 /* child is verified to be a child of the parent, call verify callback */
3295 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003296 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003297 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003298
3299 *flags |= parent_flags;
3300
3301 return( 0 );
3302}
3303
Paul Bakker76fd75a2011-01-16 21:12:10 +00003304/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 * Verify the certificate validity
3306 */
3307int x509parse_verify( x509_cert *crt,
3308 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003309 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003310 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003311 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003312 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313{
Paul Bakker23986e52011-04-24 08:57:21 +00003314 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003315 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003316 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003317 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003319 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Paul Bakker40ea7de2009-05-03 10:18:48 +00003321 *flags = 0;
3322
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 if( cn != NULL )
3324 {
3325 name = &crt->subject;
3326 cn_len = strlen( cn );
3327
Paul Bakker4d2c1242012-05-10 14:12:46 +00003328 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003330 cur = &crt->subject_alt_names;
3331
3332 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003333 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003334 if( cur->buf.len == cn_len &&
3335 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003336 break;
3337
Paul Bakker535e97d2012-08-23 10:49:55 +00003338 if( cur->buf.len > 2 &&
3339 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003340 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003341 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakker4d2c1242012-05-10 14:12:46 +00003343 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003344 }
3345
3346 if( cur == NULL )
3347 *flags |= BADCERT_CN_MISMATCH;
3348 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003349 else
3350 {
3351 while( name != NULL )
3352 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003353 if( name->oid.len == 3 &&
3354 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003355 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003356 if( name->val.len == cn_len &&
3357 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003358 break;
3359
Paul Bakker535e97d2012-08-23 10:49:55 +00003360 if( name->val.len > 2 &&
3361 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003362 x509_wildcard_verify( cn, &name->val ) )
3363 break;
3364 }
3365
3366 name = name->next;
3367 }
3368
3369 if( name == NULL )
3370 *flags |= BADCERT_CN_MISMATCH;
3371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 }
3373
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003375 * Iterate upwards in the given cert chain, to find our crt parent.
3376 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003378 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Paul Bakker76fd75a2011-01-16 21:12:10 +00003380 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003382 if( parent->ca_istrue == 0 ||
3383 crt->issuer_raw.len != parent->subject_raw.len ||
3384 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003385 crt->issuer_raw.len ) != 0 )
3386 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003387 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 continue;
3389 }
Paul Bakker915275b2012-09-28 07:10:55 +00003390 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003391 }
3392
Paul Bakker915275b2012-09-28 07:10:55 +00003393 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 {
Paul Bakker915275b2012-09-28 07:10:55 +00003395 /*
3396 * Part of the chain
3397 */
Paul Bakker9a736322012-11-14 12:39:52 +00003398 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003399 if( ret != 0 )
3400 return( ret );
3401 }
3402 else
Paul Bakker74111d32011-01-15 16:57:55 +00003403 {
Paul Bakker9a736322012-11-14 12:39:52 +00003404 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003405 if( ret != 0 )
3406 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003407 }
Paul Bakker915275b2012-09-28 07:10:55 +00003408
3409 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003410 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003411
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 return( 0 );
3413}
3414
3415/*
3416 * Unallocate all certificate data
3417 */
3418void x509_free( x509_cert *crt )
3419{
3420 x509_cert *cert_cur = crt;
3421 x509_cert *cert_prv;
3422 x509_name *name_cur;
3423 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003424 x509_sequence *seq_cur;
3425 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003426
3427 if( crt == NULL )
3428 return;
3429
3430 do
3431 {
3432 rsa_free( &cert_cur->rsa );
3433
3434 name_cur = cert_cur->issuer.next;
3435 while( name_cur != NULL )
3436 {
3437 name_prv = name_cur;
3438 name_cur = name_cur->next;
3439 memset( name_prv, 0, sizeof( x509_name ) );
3440 free( name_prv );
3441 }
3442
3443 name_cur = cert_cur->subject.next;
3444 while( name_cur != NULL )
3445 {
3446 name_prv = name_cur;
3447 name_cur = name_cur->next;
3448 memset( name_prv, 0, sizeof( x509_name ) );
3449 free( name_prv );
3450 }
3451
Paul Bakker74111d32011-01-15 16:57:55 +00003452 seq_cur = cert_cur->ext_key_usage.next;
3453 while( seq_cur != NULL )
3454 {
3455 seq_prv = seq_cur;
3456 seq_cur = seq_cur->next;
3457 memset( seq_prv, 0, sizeof( x509_sequence ) );
3458 free( seq_prv );
3459 }
3460
Paul Bakker8afa70d2012-02-11 18:42:45 +00003461 seq_cur = cert_cur->subject_alt_names.next;
3462 while( seq_cur != NULL )
3463 {
3464 seq_prv = seq_cur;
3465 seq_cur = seq_cur->next;
3466 memset( seq_prv, 0, sizeof( x509_sequence ) );
3467 free( seq_prv );
3468 }
3469
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 if( cert_cur->raw.p != NULL )
3471 {
3472 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3473 free( cert_cur->raw.p );
3474 }
3475
3476 cert_cur = cert_cur->next;
3477 }
3478 while( cert_cur != NULL );
3479
3480 cert_cur = crt;
3481 do
3482 {
3483 cert_prv = cert_cur;
3484 cert_cur = cert_cur->next;
3485
3486 memset( cert_prv, 0, sizeof( x509_cert ) );
3487 if( cert_prv != crt )
3488 free( cert_prv );
3489 }
3490 while( cert_cur != NULL );
3491}
3492
Paul Bakkerd98030e2009-05-02 15:13:40 +00003493/*
3494 * Unallocate all CRL data
3495 */
3496void x509_crl_free( x509_crl *crl )
3497{
3498 x509_crl *crl_cur = crl;
3499 x509_crl *crl_prv;
3500 x509_name *name_cur;
3501 x509_name *name_prv;
3502 x509_crl_entry *entry_cur;
3503 x509_crl_entry *entry_prv;
3504
3505 if( crl == NULL )
3506 return;
3507
3508 do
3509 {
3510 name_cur = crl_cur->issuer.next;
3511 while( name_cur != NULL )
3512 {
3513 name_prv = name_cur;
3514 name_cur = name_cur->next;
3515 memset( name_prv, 0, sizeof( x509_name ) );
3516 free( name_prv );
3517 }
3518
3519 entry_cur = crl_cur->entry.next;
3520 while( entry_cur != NULL )
3521 {
3522 entry_prv = entry_cur;
3523 entry_cur = entry_cur->next;
3524 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3525 free( entry_prv );
3526 }
3527
3528 if( crl_cur->raw.p != NULL )
3529 {
3530 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3531 free( crl_cur->raw.p );
3532 }
3533
3534 crl_cur = crl_cur->next;
3535 }
3536 while( crl_cur != NULL );
3537
3538 crl_cur = crl;
3539 do
3540 {
3541 crl_prv = crl_cur;
3542 crl_cur = crl_cur->next;
3543
3544 memset( crl_prv, 0, sizeof( x509_crl ) );
3545 if( crl_prv != crl )
3546 free( crl_prv );
3547 }
3548 while( crl_cur != NULL );
3549}
3550
Paul Bakker40e46942009-01-03 21:51:57 +00003551#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Paul Bakker40e46942009-01-03 21:51:57 +00003553#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
3555/*
3556 * Checkup routine
3557 */
3558int x509_self_test( int verbose )
3559{
Paul Bakker5690efc2011-05-26 13:16:06 +00003560#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003561 int ret;
3562 int flags;
3563 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 x509_cert cacert;
3565 x509_cert clicert;
3566 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003567#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003568 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003569#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
3571 if( verbose != 0 )
3572 printf( " X.509 certificate load: " );
3573
3574 memset( &clicert, 0, sizeof( x509_cert ) );
3575
Paul Bakkereae09db2013-06-06 12:35:54 +02003576 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003577 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 if( ret != 0 )
3579 {
3580 if( verbose != 0 )
3581 printf( "failed\n" );
3582
3583 return( ret );
3584 }
3585
3586 memset( &cacert, 0, sizeof( x509_cert ) );
3587
Paul Bakkereae09db2013-06-06 12:35:54 +02003588 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003589 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003590 if( ret != 0 )
3591 {
3592 if( verbose != 0 )
3593 printf( "failed\n" );
3594
3595 return( ret );
3596 }
3597
3598 if( verbose != 0 )
3599 printf( "passed\n X.509 private key load: " );
3600
3601 i = strlen( test_ca_key );
3602 j = strlen( test_ca_pwd );
3603
Paul Bakker66b78b22011-03-25 14:22:50 +00003604 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3605
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 if( ( ret = x509parse_key( &rsa,
Paul Bakkereae09db2013-06-06 12:35:54 +02003607 (const unsigned char *) test_ca_key, i,
3608 (const unsigned char *) test_ca_pwd, j ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 {
3610 if( verbose != 0 )
3611 printf( "failed\n" );
3612
3613 return( ret );
3614 }
3615
3616 if( verbose != 0 )
3617 printf( "passed\n X.509 signature verify: ");
3618
Paul Bakker23986e52011-04-24 08:57:21 +00003619 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003620 if( ret != 0 )
3621 {
Paul Bakker23986e52011-04-24 08:57:21 +00003622 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 if( verbose != 0 )
3624 printf( "failed\n" );
3625
3626 return( ret );
3627 }
3628
Paul Bakker5690efc2011-05-26 13:16:06 +00003629#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003631 printf( "passed\n X.509 DHM parameter load: " );
3632
3633 i = strlen( test_dhm_params );
3634 j = strlen( test_ca_pwd );
3635
Paul Bakkereae09db2013-06-06 12:35:54 +02003636 if( ( ret = x509parse_dhm( &dhm, (const unsigned char *) test_dhm_params, i ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003637 {
3638 if( verbose != 0 )
3639 printf( "failed\n" );
3640
3641 return( ret );
3642 }
3643
3644 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
3648 x509_free( &cacert );
3649 x509_free( &clicert );
3650 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003651#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003652 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003654
3655 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003656#else
3657 ((void) verbose);
3658 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003660}
3661
3662#endif
3663
3664#endif