blob: 493cf3a9965c508a895d176ac81cd201ca7385c1 [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"
45#include "polarssl/md2.h"
46#include "polarssl/md4.h"
47#include "polarssl/md5.h"
48#include "polarssl/sha1.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000049#include "polarssl/sha2.h"
50#include "polarssl/sha4.h"
Paul Bakker1b57b062011-01-06 15:48:19 +000051#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
53#include <string.h>
54#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000063#if !defined(_WIN32)
Paul Bakker8d914582012-06-04 12:46:42 +000064#include <sys/types.h>
65#include <dirent.h>
66#endif
Paul Bakker335db3f2011-04-25 15:28:35 +000067#endif
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
Paul Bakker5121ce52009-01-03 21:22:43 +000070 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
71 */
72static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000073 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000074 int *ver )
75{
Paul Bakker23986e52011-04-24 08:57:21 +000076 int ret;
77 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 if( ( ret = asn1_get_tag( p, end, &len,
80 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
81 {
Paul Bakker40e46942009-01-03 21:51:57 +000082 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000083 {
84 *ver = 0;
85 return( 0 );
86 }
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88 return( ret );
89 }
90
91 end = *p + len;
92
93 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000094 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000097 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000098 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 return( 0 );
101}
102
103/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000104 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000105 */
106static int x509_crl_get_version( unsigned char **p,
107 const unsigned char *end,
108 int *ver )
109{
110 int ret;
111
112 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
113 {
114 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000115 {
116 *ver = 0;
117 return( 0 );
118 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000119
120 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
121 }
122
123 return( 0 );
124}
125
126/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * CertificateSerialNumber ::= INTEGER
128 */
129static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 x509_buf *serial )
132{
133 int ret;
134
135 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000136 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000137 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
140 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000141 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000142 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 serial->tag = *(*p)++;
145
146 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000147 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 serial->p = *p;
150 *p += serial->len;
151
152 return( 0 );
153}
154
155/*
156 * AlgorithmIdentifier ::= SEQUENCE {
157 * algorithm OBJECT IDENTIFIER,
158 * parameters ANY DEFINED BY algorithm OPTIONAL }
159 */
160static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 x509_buf *alg )
163{
Paul Bakker23986e52011-04-24 08:57:21 +0000164 int ret;
165 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 if( ( ret = asn1_get_tag( p, end, &len,
168 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000169 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171 end = *p + len;
172 alg->tag = **p;
173
174 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000175 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
177 alg->p = *p;
178 *p += alg->len;
179
180 if( *p == end )
181 return( 0 );
182
183 /*
184 * assume the algorithm parameters must be NULL
185 */
186 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 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 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000190 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000191 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193 return( 0 );
194}
195
196/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 * AttributeTypeAndValue ::= SEQUENCE {
198 * type AttributeType,
199 * value AttributeValue }
200 *
201 * AttributeType ::= OBJECT IDENTIFIER
202 *
203 * AttributeValue ::= ANY DEFINED BY AttributeType
204 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000205static int x509_get_attr_type_value( unsigned char **p,
206 const unsigned char *end,
207 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208{
Paul Bakker23986e52011-04-24 08:57:21 +0000209 int ret;
210 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 x509_buf *oid;
212 x509_buf *val;
213
214 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000216 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 oid = &cur->oid;
219 oid->tag = **p;
220
221 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000222 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 oid->p = *p;
225 *p += oid->len;
226
227 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000228 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000229 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
232 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
233 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000234 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000235 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 val = &cur->val;
238 val->tag = *(*p)++;
239
240 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000241 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 val->p = *p;
244 *p += val->len;
245
246 cur->next = NULL;
247
Paul Bakker400ff6f2011-02-20 10:40:16 +0000248 return( 0 );
249}
250
251/*
252 * RelativeDistinguishedName ::=
253 * SET OF AttributeTypeAndValue
254 *
255 * AttributeTypeAndValue ::= SEQUENCE {
256 * type AttributeType,
257 * value AttributeValue }
258 *
259 * AttributeType ::= OBJECT IDENTIFIER
260 *
261 * AttributeValue ::= ANY DEFINED BY AttributeType
262 */
263static int x509_get_name( unsigned char **p,
264 const unsigned char *end,
265 x509_name *cur )
266{
Paul Bakker23986e52011-04-24 08:57:21 +0000267 int ret;
268 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000269 const unsigned char *end2;
270 x509_name *use;
271
272 if( ( ret = asn1_get_tag( p, end, &len,
273 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000274 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000275
276 end2 = end;
277 end = *p + len;
278 use = cur;
279
280 do
281 {
282 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
283 return( ret );
284
285 if( *p != end )
286 {
287 use->next = (x509_name *) malloc(
288 sizeof( x509_name ) );
289
290 if( use->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000291 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000292
293 memset( use->next, 0, sizeof( x509_name ) );
294
295 use = use->next;
296 }
297 }
298 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 /*
301 * recurse until end of SEQUENCE is reached
302 */
303 if( *p == end2 )
304 return( 0 );
305
306 cur->next = (x509_name *) malloc(
307 sizeof( x509_name ) );
308
309 if( cur->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +0000310 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Paul Bakker430ffbe2012-05-01 08:14:20 +0000312 memset( cur->next, 0, sizeof( x509_name ) );
313
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 return( x509_get_name( p, end2, cur->next ) );
315}
316
317/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 * Time ::= CHOICE {
319 * utcTime UTCTime,
320 * generalTime GeneralizedTime }
321 */
Paul Bakker91200182010-02-18 21:26:15 +0000322static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000323 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324 x509_time *time )
325{
Paul Bakker23986e52011-04-24 08:57:21 +0000326 int ret;
327 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000329 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000332 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
333 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000334
Paul Bakker91200182010-02-18 21:26:15 +0000335 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336
Paul Bakker91200182010-02-18 21:26:15 +0000337 if ( tag == ASN1_UTC_TIME )
338 {
339 (*p)++;
340 ret = asn1_get_len( p, end, &len );
341
342 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000343 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000346 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
347 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348
Paul Bakker91200182010-02-18 21:26:15 +0000349 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
350 &time->year, &time->mon, &time->day,
351 &time->hour, &time->min, &time->sec ) < 5 )
352 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000353
Paul Bakker400ff6f2011-02-20 10:40:16 +0000354 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000355 time->year += 1900;
356
357 *p += len;
358
359 return( 0 );
360 }
361 else if ( tag == ASN1_GENERALIZED_TIME )
362 {
363 (*p)++;
364 ret = asn1_get_len( p, end, &len );
365
366 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000367 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000368
369 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000370 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
371 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000372
373 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
374 &time->year, &time->mon, &time->day,
375 &time->hour, &time->min, &time->sec ) < 5 )
376 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
377
378 *p += len;
379
380 return( 0 );
381 }
382 else
Paul Bakker9d781402011-05-09 16:17:09 +0000383 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000384}
385
386
387/*
388 * Validity ::= SEQUENCE {
389 * notBefore Time,
390 * notAfter Time }
391 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000392static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000393 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 x509_time *from,
395 x509_time *to )
396{
Paul Bakker23986e52011-04-24 08:57:21 +0000397 int ret;
398 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 if( ( ret = asn1_get_tag( p, end, &len,
401 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000402 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404 end = *p + len;
405
Paul Bakker91200182010-02-18 21:26:15 +0000406 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000407 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Paul Bakker91200182010-02-18 21:26:15 +0000409 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000410 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000413 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000414 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 return( 0 );
417}
418
419/*
420 * SubjectPublicKeyInfo ::= SEQUENCE {
421 * algorithm AlgorithmIdentifier,
422 * subjectPublicKey BIT STRING }
423 */
424static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000425 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 x509_buf *pk_alg_oid,
427 mpi *N, mpi *E )
428{
Paul Bakker23986e52011-04-24 08:57:21 +0000429 int ret, can_handle;
430 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 unsigned char *end2;
432
433 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
434 return( ret );
435
436 /*
437 * only RSA public keys handled at this time
438 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000439 can_handle = 0;
440
441 if( pk_alg_oid->len == 9 &&
442 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
443 can_handle = 1;
444
445 if( pk_alg_oid->len == 9 &&
446 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
447 {
448 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
449 can_handle = 1;
450
451 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
452 can_handle = 1;
453 }
454
455 if( pk_alg_oid->len == 5 &&
456 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
457 can_handle = 1;
458
459 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000460 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000463 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000467 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 end2 = *p + len;
470
471 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000472 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474 /*
475 * RSAPublicKey ::= SEQUENCE {
476 * modulus INTEGER, -- n
477 * publicExponent INTEGER -- e
478 * }
479 */
480 if( ( ret = asn1_get_tag( p, end2, &len,
481 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000482 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000485 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000486 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
489 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000490 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000493 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 return( 0 );
497}
498
499static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000500 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 x509_buf *sig )
502{
Paul Bakker23986e52011-04-24 08:57:21 +0000503 int ret;
504 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Paul Bakker8afa70d2012-02-11 18:42:45 +0000506 if( ( end - *p ) < 1 )
507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
508 POLARSSL_ERR_ASN1_OUT_OF_DATA );
509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 sig->tag = **p;
511
512 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000513 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker74111d32011-01-15 16:57:55 +0000515
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 sig->len = len;
520 sig->p = *p;
521
522 *p += len;
523
524 return( 0 );
525}
526
527/*
528 * X.509 v2/v3 unique identifier (not parsed)
529 */
530static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 x509_buf *uid, int n )
533{
534 int ret;
535
536 if( *p == end )
537 return( 0 );
538
539 uid->tag = **p;
540
541 if( ( ret = asn1_get_tag( p, end, &uid->len,
542 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
543 {
Paul Bakker40e46942009-01-03 21:51:57 +0000544 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 return( 0 );
546
547 return( ret );
548 }
549
550 uid->p = *p;
551 *p += uid->len;
552
553 return( 0 );
554}
555
556/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000557 * X.509 Extensions (No parsing of extensions, pointer should
558 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
560static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000562 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000563{
Paul Bakker23986e52011-04-24 08:57:21 +0000564 int ret;
565 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 if( *p == end )
568 return( 0 );
569
570 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000571
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000573 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 ext->p = *p;
577 end = *p + ext->len;
578
579 /*
580 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
581 *
582 * Extension ::= SEQUENCE {
583 * extnID OBJECT IDENTIFIER,
584 * critical BOOLEAN DEFAULT FALSE,
585 * extnValue OCTET STRING }
586 */
587 if( ( ret = asn1_get_tag( p, end, &len,
588 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000589 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000592 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000593 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakkerd98030e2009-05-02 15:13:40 +0000595 return( 0 );
596}
597
598/*
599 * X.509 CRL v2 extensions (no extensions parsed yet.)
600 */
601static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000602 const unsigned char *end,
603 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000604{
Paul Bakker23986e52011-04-24 08:57:21 +0000605 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000606 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000607
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000608 /* Get explicit tag */
609 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000610 {
611 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
612 return( 0 );
613
614 return( ret );
615 }
616
617 while( *p < end )
618 {
619 if( ( ret = asn1_get_tag( p, end, &len,
620 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000621 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000622
623 *p += len;
624 }
625
626 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000627 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000628 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
629
630 return( 0 );
631}
632
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000633/*
634 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
635 */
636static int x509_get_crl_entry_ext( unsigned char **p,
637 const unsigned char *end,
638 x509_buf *ext )
639{
640 int ret;
641 size_t len = 0;
642
643 /* OPTIONAL */
644 if (end <= *p)
645 return( 0 );
646
647 ext->tag = **p;
648 ext->p = *p;
649
650 /*
651 * Get CRL-entry extension sequence header
652 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
653 */
654 if( ( ret = asn1_get_tag( p, end, &ext->len,
655 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
656 {
657 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
658 {
659 ext->p = NULL;
660 return( 0 );
661 }
662 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
663 }
664
665 end = *p + ext->len;
666
667 if( end != *p + ext->len )
668 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
669 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
670
671 while( *p < end )
672 {
673 if( ( ret = asn1_get_tag( p, end, &len,
674 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
675 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
676
677 *p += len;
678 }
679
680 if( *p != end )
681 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
682 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
683
684 return( 0 );
685}
686
Paul Bakker74111d32011-01-15 16:57:55 +0000687static int x509_get_basic_constraints( unsigned char **p,
688 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000689 int *ca_istrue,
690 int *max_pathlen )
691{
Paul Bakker23986e52011-04-24 08:57:21 +0000692 int ret;
693 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000694
695 /*
696 * BasicConstraints ::= SEQUENCE {
697 * cA BOOLEAN DEFAULT FALSE,
698 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
699 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000700 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000701 *max_pathlen = 0; /* endless */
702
703 if( ( ret = asn1_get_tag( p, end, &len,
704 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000705 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000706
707 if( *p == end )
708 return 0;
709
Paul Bakker3cccddb2011-01-16 21:46:31 +0000710 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000711 {
712 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000713 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000714
715 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000716 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000717
Paul Bakker3cccddb2011-01-16 21:46:31 +0000718 if( *ca_istrue != 0 )
719 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000720 }
721
722 if( *p == end )
723 return 0;
724
725 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000726 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000727
728 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000729 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000730 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
731
732 (*max_pathlen)++;
733
Paul Bakker74111d32011-01-15 16:57:55 +0000734 return 0;
735}
736
737static int x509_get_ns_cert_type( unsigned char **p,
738 const unsigned char *end,
739 unsigned char *ns_cert_type)
740{
741 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000742 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000743
744 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000745 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000746
747 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000748 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000749 POLARSSL_ERR_ASN1_INVALID_LENGTH );
750
751 /* Get actual bitstring */
752 *ns_cert_type = *bs.p;
753 return 0;
754}
755
756static int x509_get_key_usage( unsigned char **p,
757 const unsigned char *end,
758 unsigned char *key_usage)
759{
760 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000761 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000762
763 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000764 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000765
Paul Bakker94a67962012-08-23 13:03:52 +0000766 if( bs.len < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000767 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000768 POLARSSL_ERR_ASN1_INVALID_LENGTH );
769
770 /* Get actual bitstring */
771 *key_usage = *bs.p;
772 return 0;
773}
774
Paul Bakkerd98030e2009-05-02 15:13:40 +0000775/*
Paul Bakker74111d32011-01-15 16:57:55 +0000776 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
777 *
778 * KeyPurposeId ::= OBJECT IDENTIFIER
779 */
780static int x509_get_ext_key_usage( unsigned char **p,
781 const unsigned char *end,
782 x509_sequence *ext_key_usage)
783{
784 int ret;
785
786 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000787 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000788
789 /* Sequence length must be >= 1 */
790 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000791 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000792 POLARSSL_ERR_ASN1_INVALID_LENGTH );
793
794 return 0;
795}
796
797/*
Paul Bakkera8cd2392012-02-11 16:09:32 +0000798 * SubjectAltName ::= GeneralNames
799 *
800 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
801 *
802 * GeneralName ::= CHOICE {
803 * otherName [0] OtherName,
804 * rfc822Name [1] IA5String,
805 * dNSName [2] IA5String,
806 * x400Address [3] ORAddress,
807 * directoryName [4] Name,
808 * ediPartyName [5] EDIPartyName,
809 * uniformResourceIdentifier [6] IA5String,
810 * iPAddress [7] OCTET STRING,
811 * registeredID [8] OBJECT IDENTIFIER }
812 *
813 * OtherName ::= SEQUENCE {
814 * type-id OBJECT IDENTIFIER,
815 * value [0] EXPLICIT ANY DEFINED BY type-id }
816 *
817 * EDIPartyName ::= SEQUENCE {
818 * nameAssigner [0] DirectoryString OPTIONAL,
819 * partyName [1] DirectoryString }
820 *
821 * NOTE: PolarSSL only parses and uses dNSName at this point.
822 */
823static int x509_get_subject_alt_name( unsigned char **p,
824 const unsigned char *end,
825 x509_sequence *subject_alt_name )
826{
827 int ret;
828 size_t len, tag_len;
829 asn1_buf *buf;
830 unsigned char tag;
831 asn1_sequence *cur = subject_alt_name;
832
833 /* Get main sequence tag */
834 if( ( ret = asn1_get_tag( p, end, &len,
835 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
836 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
837
838 if( *p + len != end )
839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
840 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
841
842 while( *p < end )
843 {
844 if( ( end - *p ) < 1 )
845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
846 POLARSSL_ERR_ASN1_OUT_OF_DATA );
847
848 tag = **p;
849 (*p)++;
850 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
852
853 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
854 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
855 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
856
857 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
858 {
859 *p += tag_len;
860 continue;
861 }
862
863 buf = &(cur->buf);
864 buf->tag = tag;
865 buf->p = *p;
866 buf->len = tag_len;
867 *p += buf->len;
868
869 /* Allocate and assign next pointer */
870 if (*p < end)
871 {
872 cur->next = (asn1_sequence *) malloc(
873 sizeof( asn1_sequence ) );
874
875 if( cur->next == NULL )
876 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
877 POLARSSL_ERR_ASN1_MALLOC_FAILED );
878
Paul Bakker535e97d2012-08-23 10:49:55 +0000879 memset( cur->next, 0, sizeof( asn1_sequence ) );
Paul Bakkera8cd2392012-02-11 16:09:32 +0000880 cur = cur->next;
881 }
882 }
883
884 /* Set final sequence entry's next pointer to NULL */
885 cur->next = NULL;
886
887 if( *p != end )
888 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
889 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
890
891 return( 0 );
892}
893
894/*
Paul Bakker74111d32011-01-15 16:57:55 +0000895 * X.509 v3 extensions
896 *
897 * TODO: Perform all of the basic constraints tests required by the RFC
898 * TODO: Set values for undetected extensions to a sane default?
899 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000900 */
901static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000902 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000903 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000904{
Paul Bakker23986e52011-04-24 08:57:21 +0000905 int ret;
906 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000907 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000908
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000909 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000910 {
911 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
912 return( 0 );
913
914 return( ret );
915 }
916
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 while( *p < end )
918 {
Paul Bakker74111d32011-01-15 16:57:55 +0000919 /*
920 * Extension ::= SEQUENCE {
921 * extnID OBJECT IDENTIFIER,
922 * critical BOOLEAN DEFAULT FALSE,
923 * extnValue OCTET STRING }
924 */
925 x509_buf extn_oid = {0, 0, NULL};
926 int is_critical = 0; /* DEFAULT FALSE */
927
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 if( ( ret = asn1_get_tag( p, end, &len,
929 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000930 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000932 end_ext_data = *p + len;
933
Paul Bakker74111d32011-01-15 16:57:55 +0000934 /* Get extension ID */
935 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Paul Bakker74111d32011-01-15 16:57:55 +0000937 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000938 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Paul Bakker74111d32011-01-15 16:57:55 +0000940 extn_oid.p = *p;
941 *p += extn_oid.len;
942
943 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000944 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000945 POLARSSL_ERR_ASN1_OUT_OF_DATA );
946
947 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000948 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000949 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
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 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000953 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000955 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000957 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000958
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000959 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000960 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000961 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Paul Bakker74111d32011-01-15 16:57:55 +0000963 /*
964 * Detect supported extensions
965 */
966 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
967 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000968 {
Paul Bakker74111d32011-01-15 16:57:55 +0000969 /* Parse basic constraints */
970 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000971 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000972 return ( ret );
973 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 }
Paul Bakker74111d32011-01-15 16:57:55 +0000975 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
976 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
977 {
978 /* Parse netscape certificate type */
979 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
980 &crt->ns_cert_type ) ) != 0 )
981 return ( ret );
982 crt->ext_types |= EXT_NS_CERT_TYPE;
983 }
984 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
985 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
986 {
987 /* Parse key usage */
988 if( ( ret = x509_get_key_usage( p, end_ext_octet,
989 &crt->key_usage ) ) != 0 )
990 return ( ret );
991 crt->ext_types |= EXT_KEY_USAGE;
992 }
993 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
994 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
995 {
996 /* Parse extended key usage */
997 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
998 &crt->ext_key_usage ) ) != 0 )
999 return ( ret );
1000 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
1001 }
Paul Bakkera8cd2392012-02-11 16:09:32 +00001002 else if( ( OID_SIZE( OID_SUBJECT_ALT_NAME ) == extn_oid.len ) &&
1003 memcmp( extn_oid.p, OID_SUBJECT_ALT_NAME, extn_oid.len ) == 0 )
1004 {
1005 /* Parse extended key usage */
1006 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1007 &crt->subject_alt_names ) ) != 0 )
1008 return ( ret );
1009 crt->ext_types |= EXT_SUBJECT_ALT_NAME;
1010 }
Paul Bakker74111d32011-01-15 16:57:55 +00001011 else
1012 {
1013 /* No parser found, skip extension */
1014 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
Paul Bakker5c721f92011-07-27 16:51:09 +00001016#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +00001017 if( is_critical )
1018 {
1019 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +00001020 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +00001021 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
1022 }
Paul Bakker5c721f92011-07-27 16:51:09 +00001023#endif
Paul Bakker74111d32011-01-15 16:57:55 +00001024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001025 }
1026
1027 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +00001028 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +00001029 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 return( 0 );
1032}
1033
1034/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001035 * X.509 CRL Entries
1036 */
1037static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001038 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001039 x509_crl_entry *entry )
1040{
Paul Bakker23986e52011-04-24 08:57:21 +00001041 int ret;
1042 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001043 x509_crl_entry *cur_entry = entry;
1044
1045 if( *p == end )
1046 return( 0 );
1047
Paul Bakker9be19372009-07-27 20:21:53 +00001048 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +00001049 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1050 {
1051 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1052 return( 0 );
1053
1054 return( ret );
1055 }
1056
Paul Bakker9be19372009-07-27 20:21:53 +00001057 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001058
1059 while( *p < end )
1060 {
Paul Bakker23986e52011-04-24 08:57:21 +00001061 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001062 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001063
1064 if( ( ret = asn1_get_tag( p, end, &len2,
1065 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
1066 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001067 return( ret );
1068 }
1069
Paul Bakker9be19372009-07-27 20:21:53 +00001070 cur_entry->raw.tag = **p;
1071 cur_entry->raw.p = *p;
1072 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001073 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +00001074
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001075 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001076 return( ret );
1077
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001078 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001079 return( ret );
1080
Paul Bakkerb5a11ab2011-10-12 09:58:41 +00001081 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001082 return( ret );
1083
Paul Bakker74111d32011-01-15 16:57:55 +00001084 if ( *p < end )
1085 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001086 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkerb15b8512012-01-13 13:44:06 +00001087
1088 if( cur_entry->next == NULL )
1089 return( POLARSSL_ERR_X509_MALLOC_FAILED );
1090
Paul Bakkerd98030e2009-05-02 15:13:40 +00001091 cur_entry = cur_entry->next;
1092 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
1093 }
1094 }
1095
1096 return( 0 );
1097}
1098
Paul Bakker27d66162010-03-17 06:56:01 +00001099static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
1100{
1101 if( sig_oid->len == 9 &&
1102 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1103 {
1104 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1105 {
1106 *sig_alg = sig_oid->p[8];
1107 return( 0 );
1108 }
1109
1110 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1111 {
1112 *sig_alg = sig_oid->p[8];
1113 return( 0 );
1114 }
1115
1116 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1117 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001118 if( sig_oid->len == 5 &&
1119 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1120 {
1121 *sig_alg = SIG_RSA_SHA1;
1122 return( 0 );
1123 }
Paul Bakker27d66162010-03-17 06:56:01 +00001124
1125 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1126}
1127
Paul Bakkerd98030e2009-05-02 15:13:40 +00001128/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001129 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001131int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132{
Paul Bakker23986e52011-04-24 08:57:21 +00001133 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001134 size_t len;
Paul Bakkerb00ca422012-09-25 12:10:00 +00001135 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Paul Bakker320a4b52009-03-28 18:52:39 +00001137 /*
1138 * Check for valid input
1139 */
1140 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001141 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001142
Paul Bakker96743fc2011-02-12 14:30:57 +00001143 p = (unsigned char *) malloc( len = buflen );
1144
1145 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001146 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001147
1148 memcpy( p, buf, buflen );
1149
1150 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
1152 crt->raw.p = p;
1153 crt->raw.len = len;
1154 end = p + len;
1155
1156 /*
1157 * Certificate ::= SEQUENCE {
1158 * tbsCertificate TBSCertificate,
1159 * signatureAlgorithm AlgorithmIdentifier,
1160 * signatureValue BIT STRING }
1161 */
1162 if( ( ret = asn1_get_tag( &p, end, &len,
1163 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1164 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001165 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001166 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 }
1168
Paul Bakkerb00ca422012-09-25 12:10:00 +00001169 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001171 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001172 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001173 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 }
Paul Bakkerb00ca422012-09-25 12:10:00 +00001175 crt_end = p + len;
1176
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 /*
1178 * TBSCertificate ::= SEQUENCE {
1179 */
1180 crt->tbs.p = p;
1181
1182 if( ( ret = asn1_get_tag( &p, end, &len,
1183 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1184 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001185 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001186 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
1188
1189 end = p + len;
1190 crt->tbs.len = end - crt->tbs.p;
1191
1192 /*
1193 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1194 *
1195 * CertificateSerialNumber ::= INTEGER
1196 *
1197 * signature AlgorithmIdentifier
1198 */
1199 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1200 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1201 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1202 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001203 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 return( ret );
1205 }
1206
1207 crt->version++;
1208
1209 if( crt->version > 3 )
1210 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001211 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001212 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 }
1214
Paul Bakker27d66162010-03-17 06:56:01 +00001215 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001217 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001218 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 }
1220
1221 /*
1222 * issuer Name
1223 */
1224 crt->issuer_raw.p = p;
1225
1226 if( ( ret = asn1_get_tag( &p, end, &len,
1227 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1228 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001229 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001230 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 }
1232
1233 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1234 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001235 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 return( ret );
1237 }
1238
1239 crt->issuer_raw.len = p - crt->issuer_raw.p;
1240
1241 /*
1242 * Validity ::= SEQUENCE {
1243 * notBefore Time,
1244 * notAfter Time }
1245 *
1246 */
1247 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1248 &crt->valid_to ) ) != 0 )
1249 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001250 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 return( ret );
1252 }
1253
1254 /*
1255 * subject Name
1256 */
1257 crt->subject_raw.p = p;
1258
1259 if( ( ret = asn1_get_tag( &p, end, &len,
1260 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1261 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001262 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001263 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 }
1265
Paul Bakkercefb3962012-06-27 11:51:09 +00001266 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001268 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 return( ret );
1270 }
1271
1272 crt->subject_raw.len = p - crt->subject_raw.p;
1273
1274 /*
1275 * SubjectPublicKeyInfo ::= SEQUENCE
1276 * algorithm AlgorithmIdentifier,
1277 * subjectPublicKey BIT STRING }
1278 */
1279 if( ( ret = asn1_get_tag( &p, end, &len,
1280 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1281 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001282 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001283 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 }
1285
1286 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1287 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1288 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001289 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 return( ret );
1291 }
1292
1293 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1294 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001295 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 return( ret );
1297 }
1298
1299 crt->rsa.len = mpi_size( &crt->rsa.N );
1300
1301 /*
1302 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1303 * -- If present, version shall be v2 or v3
1304 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1305 * -- If present, version shall be v2 or v3
1306 * extensions [3] EXPLICIT Extensions OPTIONAL
1307 * -- If present, version shall be v3
1308 */
1309 if( crt->version == 2 || crt->version == 3 )
1310 {
1311 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1312 if( ret != 0 )
1313 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001314 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 return( ret );
1316 }
1317 }
1318
1319 if( crt->version == 2 || crt->version == 3 )
1320 {
1321 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1322 if( ret != 0 )
1323 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001324 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 return( ret );
1326 }
1327 }
1328
1329 if( crt->version == 3 )
1330 {
Paul Bakker74111d32011-01-15 16:57:55 +00001331 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 if( ret != 0 )
1333 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 return( ret );
1336 }
1337 }
1338
1339 if( p != end )
1340 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001341 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001342 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001343 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
1345
Paul Bakkerb00ca422012-09-25 12:10:00 +00001346 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
1348 /*
1349 * signatureAlgorithm AlgorithmIdentifier,
1350 * signatureValue BIT STRING
1351 */
1352 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1353 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001354 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 return( ret );
1356 }
1357
Paul Bakker535e97d2012-08-23 10:49:55 +00001358 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1359 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001361 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001362 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 }
1364
1365 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1366 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001367 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001368 return( ret );
1369 }
1370
1371 if( p != end )
1372 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001373 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001374 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001375 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
1377
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001378 return( 0 );
1379}
1380
1381/*
1382 * Parse one or more PEM certificates from a buffer and add them to the chained list
1383 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001384int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001385{
Paul Bakker69e095c2011-12-10 21:55:01 +00001386 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001387 x509_cert *crt, *prev = NULL;
1388 int buf_format = X509_FORMAT_DER;
1389
1390 crt = chain;
1391
1392 /*
1393 * Check for valid input
1394 */
1395 if( crt == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001396 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001397
1398 while( crt->version != 0 && crt->next != NULL )
1399 {
1400 prev = crt;
1401 crt = crt->next;
1402 }
1403
1404 /*
1405 * Add new certificate on the end of the chain if needed.
1406 */
1407 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001408 {
1409 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1410
Paul Bakker7d06ad22009-05-02 15:53:56 +00001411 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001412 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001413
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001414 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001415 crt = crt->next;
1416 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001419 /*
1420 * Determine buffer content. Buffer contains either one DER certificate or
1421 * one or more PEM certificates.
1422 */
1423#if defined(POLARSSL_PEM_C)
1424 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1425 buf_format = X509_FORMAT_PEM;
1426#endif
1427
1428 if( buf_format == X509_FORMAT_DER )
1429 return x509parse_crt_der( crt, buf, buflen );
1430
1431#if defined(POLARSSL_PEM_C)
1432 if( buf_format == X509_FORMAT_PEM )
1433 {
1434 pem_context pem;
1435
1436 while( buflen > 0 )
1437 {
1438 size_t use_len;
1439 pem_init( &pem );
1440
1441 ret = pem_read_buffer( &pem,
1442 "-----BEGIN CERTIFICATE-----",
1443 "-----END CERTIFICATE-----",
1444 buf, NULL, 0, &use_len );
1445
1446 if( ret == 0 )
1447 {
1448 /*
1449 * Was PEM encoded
1450 */
1451 buflen -= use_len;
1452 buf += use_len;
1453 }
1454 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1455 {
1456 pem_free( &pem );
1457
1458 if( first_error == 0 )
1459 first_error = ret;
1460
1461 continue;
1462 }
1463 else
1464 break;
1465
1466 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1467
1468 pem_free( &pem );
1469
1470 if( ret != 0 )
1471 {
1472 /*
Paul Bakker69e095c2011-12-10 21:55:01 +00001473 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001474 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001475 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001476 {
1477 if( prev )
1478 prev->next = NULL;
1479
1480 if( crt != chain )
1481 free( crt );
1482
1483 return( ret );
1484 }
1485
1486 if( first_error == 0 )
1487 first_error = ret;
Paul Bakker69e095c2011-12-10 21:55:01 +00001488
1489 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001490
1491 memset( crt, 0, sizeof( x509_cert ) );
1492 continue;
1493 }
1494
1495 success = 1;
1496
1497 /*
1498 * Add new certificate to the list
1499 */
1500 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1501
1502 if( crt->next == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001503 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001504
1505 prev = crt;
1506 crt = crt->next;
1507 memset( crt, 0, sizeof( x509_cert ) );
1508 }
1509 }
1510#endif
1511
1512 if( crt->version == 0 )
1513 {
1514 if( prev )
1515 prev->next = NULL;
1516
1517 if( crt != chain )
1518 free( crt );
1519 }
1520
1521 if( success )
Paul Bakker69e095c2011-12-10 21:55:01 +00001522 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001523 else if( first_error )
1524 return( first_error );
1525 else
1526 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001527}
1528
1529/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001530 * Parse one or more CRLs and add them to the chained list
1531 */
Paul Bakker23986e52011-04-24 08:57:21 +00001532int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001533{
Paul Bakker23986e52011-04-24 08:57:21 +00001534 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001535 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001536 unsigned char *p, *end;
1537 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001538#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001539 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001540 pem_context pem;
1541#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001542
1543 crl = chain;
1544
1545 /*
1546 * Check for valid input
1547 */
1548 if( crl == NULL || buf == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001549 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550
1551 while( crl->version != 0 && crl->next != NULL )
1552 crl = crl->next;
1553
1554 /*
1555 * Add new CRL on the end of the chain if needed.
1556 */
1557 if ( crl->version != 0 && crl->next == NULL)
1558 {
1559 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1560
Paul Bakker7d06ad22009-05-02 15:53:56 +00001561 if( crl->next == NULL )
1562 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001563 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001564 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001565 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566
Paul Bakker7d06ad22009-05-02 15:53:56 +00001567 crl = crl->next;
1568 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001569 }
1570
Paul Bakker96743fc2011-02-12 14:30:57 +00001571#if defined(POLARSSL_PEM_C)
1572 pem_init( &pem );
1573 ret = pem_read_buffer( &pem,
1574 "-----BEGIN X509 CRL-----",
1575 "-----END X509 CRL-----",
1576 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001577
Paul Bakker96743fc2011-02-12 14:30:57 +00001578 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001579 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001580 /*
1581 * Was PEM encoded
1582 */
1583 buflen -= use_len;
1584 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585
1586 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001587 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001588 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001589 p = pem.buf;
1590 pem.buf = NULL;
1591 len = pem.buflen;
1592 pem_free( &pem );
1593 }
1594 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1595 {
1596 pem_free( &pem );
1597 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001598 }
1599 else
1600 {
1601 /*
1602 * nope, copy the raw DER data
1603 */
1604 p = (unsigned char *) malloc( len = buflen );
1605
1606 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001607 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001608
1609 memcpy( p, buf, buflen );
1610
1611 buflen = 0;
1612 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001613#else
1614 p = (unsigned char *) malloc( len = buflen );
1615
1616 if( p == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001617 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001618
1619 memcpy( p, buf, buflen );
1620
1621 buflen = 0;
1622#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623
1624 crl->raw.p = p;
1625 crl->raw.len = len;
1626 end = p + len;
1627
1628 /*
1629 * CertificateList ::= SEQUENCE {
1630 * tbsCertList TBSCertList,
1631 * signatureAlgorithm AlgorithmIdentifier,
1632 * signatureValue BIT STRING }
1633 */
1634 if( ( ret = asn1_get_tag( &p, end, &len,
1635 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1636 {
1637 x509_crl_free( crl );
1638 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1639 }
1640
Paul Bakker23986e52011-04-24 08:57:21 +00001641 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001642 {
1643 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001644 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001645 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1646 }
1647
1648 /*
1649 * TBSCertList ::= SEQUENCE {
1650 */
1651 crl->tbs.p = p;
1652
1653 if( ( ret = asn1_get_tag( &p, end, &len,
1654 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1655 {
1656 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001657 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001658 }
1659
1660 end = p + len;
1661 crl->tbs.len = end - crl->tbs.p;
1662
1663 /*
1664 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1665 * -- if present, MUST be v2
1666 *
1667 * signature AlgorithmIdentifier
1668 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001669 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001670 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1671 {
1672 x509_crl_free( crl );
1673 return( ret );
1674 }
1675
1676 crl->version++;
1677
1678 if( crl->version > 2 )
1679 {
1680 x509_crl_free( crl );
1681 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1682 }
1683
Paul Bakker27d66162010-03-17 06:56:01 +00001684 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001685 {
1686 x509_crl_free( crl );
1687 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1688 }
1689
1690 /*
1691 * issuer Name
1692 */
1693 crl->issuer_raw.p = p;
1694
1695 if( ( ret = asn1_get_tag( &p, end, &len,
1696 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1697 {
1698 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001699 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001700 }
1701
1702 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1703 {
1704 x509_crl_free( crl );
1705 return( ret );
1706 }
1707
1708 crl->issuer_raw.len = p - crl->issuer_raw.p;
1709
1710 /*
1711 * thisUpdate Time
1712 * nextUpdate Time OPTIONAL
1713 */
Paul Bakker91200182010-02-18 21:26:15 +00001714 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001715 {
1716 x509_crl_free( crl );
1717 return( ret );
1718 }
1719
Paul Bakker91200182010-02-18 21:26:15 +00001720 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721 {
Paul Bakker9d781402011-05-09 16:17:09 +00001722 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001723 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001724 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001725 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001726 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001727 x509_crl_free( crl );
1728 return( ret );
1729 }
1730 }
1731
1732 /*
1733 * revokedCertificates SEQUENCE OF SEQUENCE {
1734 * userCertificate CertificateSerialNumber,
1735 * revocationDate Time,
1736 * crlEntryExtensions Extensions OPTIONAL
1737 * -- if present, MUST be v2
1738 * } OPTIONAL
1739 */
1740 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1741 {
1742 x509_crl_free( crl );
1743 return( ret );
1744 }
1745
1746 /*
1747 * crlExtensions EXPLICIT Extensions OPTIONAL
1748 * -- if present, MUST be v2
1749 */
1750 if( crl->version == 2 )
1751 {
1752 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1753
1754 if( ret != 0 )
1755 {
1756 x509_crl_free( crl );
1757 return( ret );
1758 }
1759 }
1760
1761 if( p != end )
1762 {
1763 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001764 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001765 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1766 }
1767
1768 end = crl->raw.p + crl->raw.len;
1769
1770 /*
1771 * signatureAlgorithm AlgorithmIdentifier,
1772 * signatureValue BIT STRING
1773 */
1774 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1775 {
1776 x509_crl_free( crl );
1777 return( ret );
1778 }
1779
Paul Bakker535e97d2012-08-23 10:49:55 +00001780 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1781 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001782 {
1783 x509_crl_free( crl );
1784 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1785 }
1786
1787 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1788 {
1789 x509_crl_free( crl );
1790 return( ret );
1791 }
1792
1793 if( p != end )
1794 {
1795 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001796 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001797 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1798 }
1799
1800 if( buflen > 0 )
1801 {
1802 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1803
Paul Bakker7d06ad22009-05-02 15:53:56 +00001804 if( crl->next == NULL )
1805 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001806 x509_crl_free( crl );
Paul Bakker69e095c2011-12-10 21:55:01 +00001807 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001808 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001809
Paul Bakker7d06ad22009-05-02 15:53:56 +00001810 crl = crl->next;
1811 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001812
1813 return( x509parse_crl( crl, buf, buflen ) );
1814 }
1815
1816 return( 0 );
1817}
1818
Paul Bakker335db3f2011-04-25 15:28:35 +00001819#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001820/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001821 * Load all data from a file into a given buffer.
1822 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001823int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001824{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001825 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001826
Paul Bakkerd98030e2009-05-02 15:13:40 +00001827 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001828 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001829
Paul Bakkerd98030e2009-05-02 15:13:40 +00001830 fseek( f, 0, SEEK_END );
1831 *n = (size_t) ftell( f );
1832 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001833
Paul Bakkerd98030e2009-05-02 15:13:40 +00001834 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker69e095c2011-12-10 21:55:01 +00001835 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001836
Paul Bakkerd98030e2009-05-02 15:13:40 +00001837 if( fread( *buf, 1, *n, f ) != *n )
1838 {
1839 fclose( f );
1840 free( *buf );
Paul Bakker69e095c2011-12-10 21:55:01 +00001841 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001842 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001843
Paul Bakkerd98030e2009-05-02 15:13:40 +00001844 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001845
Paul Bakkerd98030e2009-05-02 15:13:40 +00001846 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001847
Paul Bakkerd98030e2009-05-02 15:13:40 +00001848 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001849}
1850
1851/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 * Load one or more certificates and add them to the chained list
1853 */
Paul Bakker69e095c2011-12-10 21:55:01 +00001854int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001855{
1856 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 size_t n;
1858 unsigned char *buf;
1859
Paul Bakker69e095c2011-12-10 21:55:01 +00001860 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1861 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Paul Bakker69e095c2011-12-10 21:55:01 +00001863 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
1865 memset( buf, 0, n + 1 );
1866 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
1868 return( ret );
1869}
1870
Paul Bakker8d914582012-06-04 12:46:42 +00001871int x509parse_crtpath( x509_cert *chain, const char *path )
1872{
1873 int ret = 0;
1874#if defined(_WIN32)
Paul Bakker3338b792012-10-01 21:13:10 +00001875 int w_ret;
1876 WCHAR szDir[MAX_PATH];
1877 char filename[MAX_PATH];
1878 char *p;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001879 int len = strlen( path );
Paul Bakker3338b792012-10-01 21:13:10 +00001880
Paul Bakker97872ac2012-11-02 12:53:26 +00001881 WIN32_FIND_DATAW file_data;
Paul Bakker8d914582012-06-04 12:46:42 +00001882 HANDLE hFind;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001883
1884 if( len > MAX_PATH - 3 )
1885 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker8d914582012-06-04 12:46:42 +00001886
Paul Bakker3338b792012-10-01 21:13:10 +00001887 memset( szDir, 0, sizeof(szDir) );
1888 memset( filename, 0, MAX_PATH );
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001889 memcpy( filename, path, len );
1890 filename[len++] = '\\';
1891 p = filename + len;
1892 filename[len++] = '*';
Paul Bakker3338b792012-10-01 21:13:10 +00001893
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001894 w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
Paul Bakker8d914582012-06-04 12:46:42 +00001895
Paul Bakker97872ac2012-11-02 12:53:26 +00001896 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker8d914582012-06-04 12:46:42 +00001897 if (hFind == INVALID_HANDLE_VALUE)
1898 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1899
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001900 len = MAX_PATH - len;
Paul Bakker8d914582012-06-04 12:46:42 +00001901 do
1902 {
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001903 memset( p, 0, len );
Paul Bakker3338b792012-10-01 21:13:10 +00001904
Paul Bakkere4791f32012-06-04 21:29:15 +00001905 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
Paul Bakker8d914582012-06-04 12:46:42 +00001906 continue;
1907
Paul Bakker3338b792012-10-01 21:13:10 +00001908 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1909 lstrlenW(file_data.cFileName),
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001910 p, len - 1,
1911 NULL, NULL );
Paul Bakker8d914582012-06-04 12:46:42 +00001912
Paul Bakker3338b792012-10-01 21:13:10 +00001913 w_ret = x509parse_crtfile( chain, filename );
1914 if( w_ret < 0 )
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001915 {
1916 ret = w_ret;
1917 goto cleanup;
1918 }
Paul Bakker3338b792012-10-01 21:13:10 +00001919
1920 ret += w_ret;
Paul Bakker8d914582012-06-04 12:46:42 +00001921 }
Paul Bakker97872ac2012-11-02 12:53:26 +00001922 while( FindNextFileW( hFind, &file_data ) != 0 );
Paul Bakker8d914582012-06-04 12:46:42 +00001923
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001924 if (GetLastError() != ERROR_NO_MORE_FILES)
1925 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
Paul Bakker8d914582012-06-04 12:46:42 +00001926
Paul Bakker4a2bd0d2012-11-02 11:06:08 +00001927cleanup:
Paul Bakker8d914582012-06-04 12:46:42 +00001928 FindClose( hFind );
1929#else
1930 int t_ret;
1931 struct dirent *entry;
1932 char entry_name[255];
1933 DIR *dir = opendir( path );
1934
1935 if( dir == NULL)
1936 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1937
1938 while( ( entry = readdir( dir ) ) != NULL )
1939 {
1940 if( entry->d_type != DT_REG )
1941 continue;
1942
1943 snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry->d_name );
1944 t_ret = x509parse_crtfile( chain, entry_name );
1945 if( t_ret < 0 )
Paul Bakker97872ac2012-11-02 12:53:26 +00001946 {
1947 ret = t_ret;
1948 break;
1949 }
Paul Bakker8d914582012-06-04 12:46:42 +00001950
1951 ret += t_ret;
1952 }
1953 closedir( dir );
1954#endif
1955
1956 return( ret );
1957}
1958
Paul Bakkerd98030e2009-05-02 15:13:40 +00001959/*
1960 * Load one or more CRLs and add them to the chained list
1961 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001962int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001963{
1964 int ret;
1965 size_t n;
1966 unsigned char *buf;
1967
Paul Bakker69e095c2011-12-10 21:55:01 +00001968 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1969 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001970
Paul Bakker27fdf462011-06-09 13:55:13 +00001971 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001972
1973 memset( buf, 0, n + 1 );
1974 free( buf );
1975
1976 return( ret );
1977}
1978
Paul Bakker5121ce52009-01-03 21:22:43 +00001979/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001980 * Load and parse a private RSA key
1981 */
1982int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1983{
1984 int ret;
1985 size_t n;
1986 unsigned char *buf;
1987
Paul Bakker69e095c2011-12-10 21:55:01 +00001988 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1989 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001990
1991 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001992 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001993 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001994 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001995 (unsigned char *) pwd, strlen( pwd ) );
1996
1997 memset( buf, 0, n + 1 );
1998 free( buf );
1999
2000 return( ret );
2001}
2002
2003/*
2004 * Load and parse a public RSA key
2005 */
2006int x509parse_public_keyfile( rsa_context *rsa, const char *path )
2007{
2008 int ret;
2009 size_t n;
2010 unsigned char *buf;
2011
Paul Bakker69e095c2011-12-10 21:55:01 +00002012 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
2013 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00002014
Paul Bakker27fdf462011-06-09 13:55:13 +00002015 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00002016
2017 memset( buf, 0, n + 1 );
2018 free( buf );
2019
2020 return( ret );
2021}
2022#endif /* POLARSSL_FS_IO */
2023
2024/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 * Parse a private RSA key
2026 */
Paul Bakker23986e52011-04-24 08:57:21 +00002027int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
2028 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029{
Paul Bakker23986e52011-04-24 08:57:21 +00002030 int ret;
2031 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00002033 unsigned char *p_alt;
2034 x509_buf pk_alg_oid;
2035
Paul Bakker96743fc2011-02-12 14:30:57 +00002036#if defined(POLARSSL_PEM_C)
2037 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Paul Bakker96743fc2011-02-12 14:30:57 +00002039 pem_init( &pem );
2040 ret = pem_read_buffer( &pem,
2041 "-----BEGIN RSA PRIVATE KEY-----",
2042 "-----END RSA PRIVATE KEY-----",
2043 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002044
Paul Bakkered56b222011-07-13 11:26:43 +00002045 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2046 {
2047 ret = pem_read_buffer( &pem,
2048 "-----BEGIN PRIVATE KEY-----",
2049 "-----END PRIVATE KEY-----",
2050 key, pwd, pwdlen, &len );
2051 }
2052
Paul Bakker96743fc2011-02-12 14:30:57 +00002053 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002055 /*
2056 * Was PEM encoded
2057 */
2058 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002059 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002060 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00002061 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002062 pem_free( &pem );
2063 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00002064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002065
Paul Bakker96743fc2011-02-12 14:30:57 +00002066 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2067#else
Paul Bakker5690efc2011-05-26 13:16:06 +00002068 ((void) pwd);
2069 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00002070 p = (unsigned char *) key;
2071#endif
2072 end = p + keylen;
2073
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 /*
Paul Bakkered56b222011-07-13 11:26:43 +00002075 * Note: Depending on the type of private key file one can expect either a
2076 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
2077 *
2078 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00002079 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00002080 * algorithm AlgorithmIdentifier,
2081 * PrivateKey BIT STRING
2082 * }
2083 *
2084 * AlgorithmIdentifier ::= SEQUENCE {
2085 * algorithm OBJECT IDENTIFIER,
2086 * parameters ANY DEFINED BY algorithm OPTIONAL
2087 * }
2088 *
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 * RSAPrivateKey ::= SEQUENCE {
2090 * version Version,
2091 * modulus INTEGER, -- n
2092 * publicExponent INTEGER, -- e
2093 * privateExponent INTEGER, -- d
2094 * prime1 INTEGER, -- p
2095 * prime2 INTEGER, -- q
2096 * exponent1 INTEGER, -- d mod (p-1)
2097 * exponent2 INTEGER, -- d mod (q-1)
2098 * coefficient INTEGER, -- (inverse of q) mod p
2099 * otherPrimeInfos OtherPrimeInfos OPTIONAL
2100 * }
2101 */
2102 if( ( ret = asn1_get_tag( &p, end, &len,
2103 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2104 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002105#if defined(POLARSSL_PEM_C)
2106 pem_free( &pem );
2107#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002109 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
2111
2112 end = p + len;
2113
2114 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2115 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002116#if defined(POLARSSL_PEM_C)
2117 pem_free( &pem );
2118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002120 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 }
2122
2123 if( rsa->ver != 0 )
2124 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002125#if defined(POLARSSL_PEM_C)
2126 pem_free( &pem );
2127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002129 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 }
2131
Paul Bakkered56b222011-07-13 11:26:43 +00002132 p_alt = p;
2133
2134 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
2135 {
2136 // Assume that we have the PKCS#1 format if wrong
2137 // tag was encountered
2138 //
2139 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
2140 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
2141 {
2142#if defined(POLARSSL_PEM_C)
2143 pem_free( &pem );
2144#endif
2145 rsa_free( rsa );
2146 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
2147 }
2148 }
2149 else
2150 {
2151 int can_handle;
2152
2153 /*
2154 * only RSA keys handled at this time
2155 */
2156 can_handle = 0;
2157
2158 if( pk_alg_oid.len == 9 &&
2159 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
2160 can_handle = 1;
2161
2162 if( pk_alg_oid.len == 9 &&
2163 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
2164 {
2165 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
2166 can_handle = 1;
2167
2168 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
2169 can_handle = 1;
2170 }
2171
2172 if( pk_alg_oid.len == 5 &&
2173 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
2174 can_handle = 1;
2175
2176 if( can_handle == 0 )
2177 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
2178
2179 /*
2180 * Parse the PKCS#8 format
2181 */
2182
2183 p = p_alt;
2184 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2185 {
2186#if defined(POLARSSL_PEM_C)
2187 pem_free( &pem );
2188#endif
2189 rsa_free( rsa );
2190 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2191 }
2192
2193 if( ( end - p ) < 1 )
2194 {
2195#if defined(POLARSSL_PEM_C)
2196 pem_free( &pem );
2197#endif
2198 rsa_free( rsa );
2199 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2200 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2201 }
2202
2203 end = p + len;
2204
2205 if( ( ret = asn1_get_tag( &p, end, &len,
2206 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 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 end = p + len;
2216
2217 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2218 {
2219#if defined(POLARSSL_PEM_C)
2220 pem_free( &pem );
2221#endif
2222 rsa_free( rsa );
2223 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2224 }
2225
2226 if( rsa->ver != 0 )
2227 {
2228#if defined(POLARSSL_PEM_C)
2229 pem_free( &pem );
2230#endif
2231 rsa_free( rsa );
2232 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2233 }
2234 }
2235
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2237 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2238 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2239 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2240 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2241 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2242 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2243 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2244 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002245#if defined(POLARSSL_PEM_C)
2246 pem_free( &pem );
2247#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002249 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 }
2251
2252 rsa->len = mpi_size( &rsa->N );
2253
2254 if( p != end )
2255 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002256#if defined(POLARSSL_PEM_C)
2257 pem_free( &pem );
2258#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002260 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002261 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263
2264 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2265 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002266#if defined(POLARSSL_PEM_C)
2267 pem_free( &pem );
2268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 rsa_free( rsa );
2270 return( ret );
2271 }
2272
Paul Bakker96743fc2011-02-12 14:30:57 +00002273#if defined(POLARSSL_PEM_C)
2274 pem_free( &pem );
2275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
2277 return( 0 );
2278}
2279
2280/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002281 * Parse a public RSA key
2282 */
Paul Bakker23986e52011-04-24 08:57:21 +00002283int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002284{
Paul Bakker23986e52011-04-24 08:57:21 +00002285 int ret;
2286 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002287 unsigned char *p, *end;
2288 x509_buf alg_oid;
2289#if defined(POLARSSL_PEM_C)
2290 pem_context pem;
2291
2292 pem_init( &pem );
2293 ret = pem_read_buffer( &pem,
2294 "-----BEGIN PUBLIC KEY-----",
2295 "-----END PUBLIC KEY-----",
2296 key, NULL, 0, &len );
2297
2298 if( ret == 0 )
2299 {
2300 /*
2301 * Was PEM encoded
2302 */
2303 keylen = pem.buflen;
2304 }
2305 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2306 {
2307 pem_free( &pem );
2308 return( ret );
2309 }
2310
2311 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2312#else
2313 p = (unsigned char *) key;
2314#endif
2315 end = p + keylen;
2316
2317 /*
2318 * PublicKeyInfo ::= SEQUENCE {
2319 * algorithm AlgorithmIdentifier,
2320 * PublicKey BIT STRING
2321 * }
2322 *
2323 * AlgorithmIdentifier ::= SEQUENCE {
2324 * algorithm OBJECT IDENTIFIER,
2325 * parameters ANY DEFINED BY algorithm OPTIONAL
2326 * }
2327 *
2328 * RSAPublicKey ::= SEQUENCE {
2329 * modulus INTEGER, -- n
2330 * publicExponent INTEGER -- e
2331 * }
2332 */
2333
2334 if( ( ret = asn1_get_tag( &p, end, &len,
2335 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2336 {
2337#if defined(POLARSSL_PEM_C)
2338 pem_free( &pem );
2339#endif
2340 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002341 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002342 }
2343
2344 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2345 {
2346#if defined(POLARSSL_PEM_C)
2347 pem_free( &pem );
2348#endif
2349 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002350 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002351 }
2352
2353 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2354 {
2355#if defined(POLARSSL_PEM_C)
2356 pem_free( &pem );
2357#endif
2358 rsa_free( rsa );
2359 return( ret );
2360 }
2361
2362 rsa->len = mpi_size( &rsa->N );
2363
2364#if defined(POLARSSL_PEM_C)
2365 pem_free( &pem );
2366#endif
2367
2368 return( 0 );
2369}
2370
Paul Bakkereaa89f82011-04-04 21:36:15 +00002371#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002372/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002373 * Parse DHM parameters
2374 */
Paul Bakker23986e52011-04-24 08:57:21 +00002375int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002376{
Paul Bakker23986e52011-04-24 08:57:21 +00002377 int ret;
2378 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002379 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002380#if defined(POLARSSL_PEM_C)
2381 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002382
Paul Bakker96743fc2011-02-12 14:30:57 +00002383 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002384
Paul Bakker96743fc2011-02-12 14:30:57 +00002385 ret = pem_read_buffer( &pem,
2386 "-----BEGIN DH PARAMETERS-----",
2387 "-----END DH PARAMETERS-----",
2388 dhmin, NULL, 0, &dhminlen );
2389
2390 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002391 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002392 /*
2393 * Was PEM encoded
2394 */
2395 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002396 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002397 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002398 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002399 pem_free( &pem );
2400 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002401 }
2402
Paul Bakker96743fc2011-02-12 14:30:57 +00002403 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2404#else
2405 p = (unsigned char *) dhmin;
2406#endif
2407 end = p + dhminlen;
2408
Paul Bakker1b57b062011-01-06 15:48:19 +00002409 memset( dhm, 0, sizeof( dhm_context ) );
2410
Paul Bakker1b57b062011-01-06 15:48:19 +00002411 /*
2412 * DHParams ::= SEQUENCE {
2413 * prime INTEGER, -- P
2414 * generator INTEGER, -- g
2415 * }
2416 */
2417 if( ( ret = asn1_get_tag( &p, end, &len,
2418 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2419 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002420#if defined(POLARSSL_PEM_C)
2421 pem_free( &pem );
2422#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002423 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002424 }
2425
2426 end = p + len;
2427
2428 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2429 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2430 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002431#if defined(POLARSSL_PEM_C)
2432 pem_free( &pem );
2433#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002434 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002435 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002436 }
2437
2438 if( p != end )
2439 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002440#if defined(POLARSSL_PEM_C)
2441 pem_free( &pem );
2442#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002443 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002444 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002445 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2446 }
2447
Paul Bakker96743fc2011-02-12 14:30:57 +00002448#if defined(POLARSSL_PEM_C)
2449 pem_free( &pem );
2450#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002451
2452 return( 0 );
2453}
2454
Paul Bakker335db3f2011-04-25 15:28:35 +00002455#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002456/*
2457 * Load and parse a private RSA key
2458 */
2459int x509parse_dhmfile( dhm_context *dhm, const char *path )
2460{
2461 int ret;
2462 size_t n;
2463 unsigned char *buf;
2464
Paul Bakker69e095c2011-12-10 21:55:01 +00002465 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2466 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002467
Paul Bakker27fdf462011-06-09 13:55:13 +00002468 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002469
2470 memset( buf, 0, n + 1 );
2471 free( buf );
2472
2473 return( ret );
2474}
Paul Bakker335db3f2011-04-25 15:28:35 +00002475#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002476#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002477
Paul Bakker5121ce52009-01-03 21:22:43 +00002478#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002479#include <stdarg.h>
2480
2481#if !defined vsnprintf
2482#define vsnprintf _vsnprintf
2483#endif // vsnprintf
2484
2485/*
2486 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2487 * Result value is not size of buffer needed, but -1 if no fit is possible.
2488 *
2489 * This fuction tries to 'fix' this by at least suggesting enlarging the
2490 * size by 20.
2491 */
2492int compat_snprintf(char *str, size_t size, const char *format, ...)
2493{
2494 va_list ap;
2495 int res = -1;
2496
2497 va_start( ap, format );
2498
2499 res = vsnprintf( str, size, format, ap );
2500
2501 va_end( ap );
2502
2503 // No quick fix possible
2504 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002505 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506
2507 return res;
2508}
2509
2510#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002511#endif
2512
Paul Bakkerd98030e2009-05-02 15:13:40 +00002513#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2514
2515#define SAFE_SNPRINTF() \
2516{ \
2517 if( ret == -1 ) \
2518 return( -1 ); \
2519 \
Paul Bakker23986e52011-04-24 08:57:21 +00002520 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002521 p[n - 1] = '\0'; \
2522 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2523 } \
2524 \
Paul Bakker23986e52011-04-24 08:57:21 +00002525 n -= (unsigned int) ret; \
2526 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527}
2528
Paul Bakker5121ce52009-01-03 21:22:43 +00002529/*
2530 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002531 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002533int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534{
Paul Bakker23986e52011-04-24 08:57:21 +00002535 int ret;
2536 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002538 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 char s[128], *p;
2540
2541 memset( s, 0, sizeof( s ) );
2542
2543 name = dn;
2544 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002545 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
2547 while( name != NULL )
2548 {
Paul Bakkercefb3962012-06-27 11:51:09 +00002549 if( !name->oid.p )
2550 {
2551 name = name->next;
2552 continue;
2553 }
2554
Paul Bakker74111d32011-01-15 16:57:55 +00002555 if( name != dn )
2556 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002557 ret = snprintf( p, n, ", " );
2558 SAFE_SNPRINTF();
2559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Paul Bakker535e97d2012-08-23 10:49:55 +00002561 if( name->oid.len == 3 &&
2562 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002563 {
2564 switch( name->oid.p[2] )
2565 {
2566 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002567 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002568
2569 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002570 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002571
2572 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
2575 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002576 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
2578 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002579 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
2581 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002582 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
2584 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002585 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 name->oid.p[2] );
2587 break;
2588 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002589 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 }
Paul Bakker535e97d2012-08-23 10:49:55 +00002591 else if( name->oid.len == 9 &&
2592 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 {
2594 switch( name->oid.p[8] )
2595 {
2596 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002597 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002598
2599 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002600 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 name->oid.p[8] );
2602 break;
2603 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002604 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002605 }
2606 else
Paul Bakker74111d32011-01-15 16:57:55 +00002607 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002608 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002609 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
2612 for( i = 0; i < name->val.len; i++ )
2613 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002614 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 break;
2616
2617 c = name->val.p[i];
2618 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2619 s[i] = '?';
2620 else s[i] = c;
2621 }
2622 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002623 ret = snprintf( p, n, "%s", s );
2624 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 name = name->next;
2626 }
2627
Paul Bakker23986e52011-04-24 08:57:21 +00002628 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629}
2630
2631/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002632 * Store the serial in printable form into buf; no more
2633 * than size characters will be written
2634 */
2635int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2636{
Paul Bakker23986e52011-04-24 08:57:21 +00002637 int ret;
2638 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002639 char *p;
2640
2641 p = buf;
2642 n = size;
2643
2644 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002645 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002646
2647 for( i = 0; i < nr; i++ )
2648 {
Paul Bakker93048802011-12-05 14:38:06 +00002649 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002650 continue;
2651
Paul Bakkerdd476992011-01-16 21:34:59 +00002652 ret = snprintf( p, n, "%02X%s",
2653 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2654 SAFE_SNPRINTF();
2655 }
2656
Paul Bakker03c7c252011-11-25 12:37:37 +00002657 if( nr != serial->len )
2658 {
2659 ret = snprintf( p, n, "...." );
2660 SAFE_SNPRINTF();
2661 }
2662
Paul Bakker23986e52011-04-24 08:57:21 +00002663 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002664}
2665
2666/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002667 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002669int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2670 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002671{
Paul Bakker23986e52011-04-24 08:57:21 +00002672 int ret;
2673 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
2676 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002677 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Paul Bakkerd98030e2009-05-02 15:13:40 +00002679 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002681 SAFE_SNPRINTF();
2682 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Paul Bakkerdd476992011-01-16 21:34:59 +00002686 ret = x509parse_serial_gets( p, n, &crt->serial);
2687 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Paul Bakkerd98030e2009-05-02 15:13:40 +00002689 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2690 SAFE_SNPRINTF();
2691 ret = x509parse_dn_gets( p, n, &crt->issuer );
2692 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Paul Bakkerd98030e2009-05-02 15:13:40 +00002694 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2695 SAFE_SNPRINTF();
2696 ret = x509parse_dn_gets( p, n, &crt->subject );
2697 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Paul Bakkerd98030e2009-05-02 15:13:40 +00002699 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2701 crt->valid_from.year, crt->valid_from.mon,
2702 crt->valid_from.day, crt->valid_from.hour,
2703 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002704 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Paul Bakkerd98030e2009-05-02 15:13:40 +00002706 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2708 crt->valid_to.year, crt->valid_to.mon,
2709 crt->valid_to.day, crt->valid_to.hour,
2710 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002711 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Paul Bakkerd98030e2009-05-02 15:13:40 +00002713 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2714 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Paul Bakker27d66162010-03-17 06:56:01 +00002716 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002718 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2719 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2720 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2721 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2722 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2723 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2724 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2725 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2726 default: ret = snprintf( p, n, "???" ); break;
2727 }
2728 SAFE_SNPRINTF();
2729
2730 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakker5c2364c2012-10-01 14:41:15 +00002731 (int) crt->rsa.N.n * (int) sizeof( t_uint ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002732 SAFE_SNPRINTF();
2733
Paul Bakker23986e52011-04-24 08:57:21 +00002734 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002735}
2736
Paul Bakker74111d32011-01-15 16:57:55 +00002737/* Compare a given OID string with an OID x509_buf * */
2738#define OID_CMP(oid_str, oid_buf) \
2739 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2740 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2741
2742/*
2743 * Return an informational string describing the given OID
2744 */
2745const char *x509_oid_get_description( x509_buf *oid )
2746{
2747 if ( oid == NULL )
2748 return ( NULL );
2749
2750 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2751 return( STRING_SERVER_AUTH );
2752
2753 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2754 return( STRING_CLIENT_AUTH );
2755
2756 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2757 return( STRING_CODE_SIGNING );
2758
2759 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2760 return( STRING_EMAIL_PROTECTION );
2761
2762 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2763 return( STRING_TIME_STAMPING );
2764
2765 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2766 return( STRING_OCSP_SIGNING );
2767
2768 return( NULL );
2769}
2770
2771/* Return the x.y.z.... style numeric string for the given OID */
2772int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2773{
Paul Bakker23986e52011-04-24 08:57:21 +00002774 int ret;
2775 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002776 unsigned int value;
2777 char *p;
2778
2779 p = buf;
2780 n = size;
2781
2782 /* First byte contains first two dots */
2783 if( oid->len > 0 )
2784 {
2785 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2786 SAFE_SNPRINTF();
2787 }
2788
2789 /* TODO: value can overflow in value. */
2790 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002791 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002792 {
2793 value <<= 7;
2794 value += oid->p[i] & 0x7F;
2795
2796 if( !( oid->p[i] & 0x80 ) )
2797 {
2798 /* Last byte */
2799 ret = snprintf( p, n, ".%d", value );
2800 SAFE_SNPRINTF();
2801 value = 0;
2802 }
2803 }
2804
Paul Bakker23986e52011-04-24 08:57:21 +00002805 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002806}
2807
Paul Bakkerd98030e2009-05-02 15:13:40 +00002808/*
2809 * Return an informational string about the CRL.
2810 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002811int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2812 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002813{
Paul Bakker23986e52011-04-24 08:57:21 +00002814 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002815 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002816 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002817 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002818
2819 p = buf;
2820 n = size;
2821
2822 ret = snprintf( p, n, "%sCRL version : %d",
2823 prefix, crl->version );
2824 SAFE_SNPRINTF();
2825
2826 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2827 SAFE_SNPRINTF();
2828 ret = x509parse_dn_gets( p, n, &crl->issuer );
2829 SAFE_SNPRINTF();
2830
2831 ret = snprintf( p, n, "\n%sthis update : " \
2832 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2833 crl->this_update.year, crl->this_update.mon,
2834 crl->this_update.day, crl->this_update.hour,
2835 crl->this_update.min, crl->this_update.sec );
2836 SAFE_SNPRINTF();
2837
2838 ret = snprintf( p, n, "\n%snext update : " \
2839 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2840 crl->next_update.year, crl->next_update.mon,
2841 crl->next_update.day, crl->next_update.hour,
2842 crl->next_update.min, crl->next_update.sec );
2843 SAFE_SNPRINTF();
2844
2845 entry = &crl->entry;
2846
2847 ret = snprintf( p, n, "\n%sRevoked certificates:",
2848 prefix );
2849 SAFE_SNPRINTF();
2850
Paul Bakker9be19372009-07-27 20:21:53 +00002851 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002852 {
2853 ret = snprintf( p, n, "\n%sserial number: ",
2854 prefix );
2855 SAFE_SNPRINTF();
2856
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002857 ret = x509parse_serial_gets( p, n, &entry->serial);
2858 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002859
Paul Bakkerd98030e2009-05-02 15:13:40 +00002860 ret = snprintf( p, n, " revocation date: " \
2861 "%04d-%02d-%02d %02d:%02d:%02d",
2862 entry->revocation_date.year, entry->revocation_date.mon,
2863 entry->revocation_date.day, entry->revocation_date.hour,
2864 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002865 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002866
2867 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002868 }
2869
Paul Bakkerd98030e2009-05-02 15:13:40 +00002870 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2871 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Paul Bakker27d66162010-03-17 06:56:01 +00002873 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002874 {
2875 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2876 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2877 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2878 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2879 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2880 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2881 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2882 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2883 default: ret = snprintf( p, n, "???" ); break;
2884 }
2885 SAFE_SNPRINTF();
2886
Paul Bakker1e27bb22009-07-19 20:25:25 +00002887 ret = snprintf( p, n, "\n" );
2888 SAFE_SNPRINTF();
2889
Paul Bakker23986e52011-04-24 08:57:21 +00002890 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891}
2892
2893/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002894 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002896int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897{
Paul Bakkercce9d772011-11-18 14:26:47 +00002898 int year, mon, day;
2899 int hour, min, sec;
2900
2901#if defined(_WIN32)
2902 SYSTEMTIME st;
2903
2904 GetLocalTime(&st);
2905
2906 year = st.wYear;
2907 mon = st.wMonth;
2908 day = st.wDay;
2909 hour = st.wHour;
2910 min = st.wMinute;
2911 sec = st.wSecond;
2912#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 struct tm *lt;
2914 time_t tt;
2915
2916 tt = time( NULL );
2917 lt = localtime( &tt );
2918
Paul Bakkercce9d772011-11-18 14:26:47 +00002919 year = lt->tm_year + 1900;
2920 mon = lt->tm_mon + 1;
2921 day = lt->tm_mday;
2922 hour = lt->tm_hour;
2923 min = lt->tm_min;
2924 sec = lt->tm_sec;
2925#endif
2926
2927 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002928 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakkercce9d772011-11-18 14:26:47 +00002930 if( year == to->year &&
2931 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002932 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakkercce9d772011-11-18 14:26:47 +00002934 if( year == to->year &&
2935 mon == to->mon &&
2936 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002937 return( 1 );
2938
Paul Bakkercce9d772011-11-18 14:26:47 +00002939 if( year == to->year &&
2940 mon == to->mon &&
2941 day == to->day &&
2942 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002943 return( 1 );
2944
Paul Bakkercce9d772011-11-18 14:26:47 +00002945 if( year == to->year &&
2946 mon == to->mon &&
2947 day == to->day &&
2948 hour == to->hour &&
2949 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002950 return( 1 );
2951
Paul Bakkercce9d772011-11-18 14:26:47 +00002952 if( year == to->year &&
2953 mon == to->mon &&
2954 day == to->day &&
2955 hour == to->hour &&
2956 min == to->min &&
2957 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002958 return( 1 );
2959
Paul Bakker40ea7de2009-05-03 10:18:48 +00002960 return( 0 );
2961}
2962
2963/*
2964 * Return 1 if the certificate is revoked, or 0 otherwise.
2965 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002966int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002967{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002968 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002969
2970 while( cur != NULL && cur->serial.len != 0 )
2971 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002972 if( crt->serial.len == cur->serial.len &&
2973 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002974 {
2975 if( x509parse_time_expired( &cur->revocation_date ) )
2976 return( 1 );
2977 }
2978
2979 cur = cur->next;
2980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
2982 return( 0 );
2983}
2984
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002985/*
2986 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002987 */
Paul Bakker23986e52011-04-24 08:57:21 +00002988static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 unsigned char *out )
2990{
2991 switch( alg )
2992 {
Paul Bakker40e46942009-01-03 21:51:57 +00002993#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002994 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002996#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002997 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002999#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003000 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003001#endif
3002#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003003 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003004#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00003005#if defined(POLARSSL_SHA2_C)
3006 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
3007 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
3008#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00003009#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00003010 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
3011 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
3012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003014 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 break;
3016 }
3017}
3018
3019/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00003020 * Check that the given certificate is valid accoring to the CRL.
3021 */
3022static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
3023 x509_crl *crl_list)
3024{
3025 int flags = 0;
3026 int hash_id;
3027 unsigned char hash[64];
3028
Paul Bakker915275b2012-09-28 07:10:55 +00003029 if( ca == NULL )
3030 return( flags );
3031
Paul Bakker76fd75a2011-01-16 21:12:10 +00003032 /*
3033 * TODO: What happens if no CRL is present?
3034 * Suggestion: Revocation state should be unknown if no CRL is present.
3035 * For backwards compatibility this is not yet implemented.
3036 */
3037
Paul Bakker915275b2012-09-28 07:10:55 +00003038 while( crl_list != NULL )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003039 {
Paul Bakker915275b2012-09-28 07:10:55 +00003040 if( crl_list->version == 0 ||
3041 crl_list->issuer_raw.len != ca->subject_raw.len ||
Paul Bakker76fd75a2011-01-16 21:12:10 +00003042 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
3043 crl_list->issuer_raw.len ) != 0 )
3044 {
3045 crl_list = crl_list->next;
3046 continue;
3047 }
3048
3049 /*
3050 * Check if CRL is correctly signed by the trusted CA
3051 */
3052 hash_id = crl_list->sig_alg;
3053
3054 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
3055
3056 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
3057 0, hash, crl_list->sig.p ) == 0 )
3058 {
3059 /*
3060 * CRL is not trusted
3061 */
3062 flags |= BADCRL_NOT_TRUSTED;
3063 break;
3064 }
3065
3066 /*
3067 * Check for validity of CRL (Do not drop out)
3068 */
3069 if( x509parse_time_expired( &crl_list->next_update ) )
3070 flags |= BADCRL_EXPIRED;
3071
3072 /*
3073 * Check if certificate is revoked
3074 */
3075 if( x509parse_revoked(crt, crl_list) )
3076 {
3077 flags |= BADCERT_REVOKED;
3078 break;
3079 }
3080
3081 crl_list = crl_list->next;
3082 }
3083 return flags;
3084}
3085
Paul Bakker57b12982012-02-11 17:38:38 +00003086int x509_wildcard_verify( const char *cn, x509_buf *name )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003087{
3088 size_t i;
3089 size_t cn_idx = 0;
3090
Paul Bakker57b12982012-02-11 17:38:38 +00003091 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003092 return( 0 );
3093
3094 for( i = 0; i < strlen( cn ); ++i )
3095 {
3096 if( cn[i] == '.' )
3097 {
3098 cn_idx = i;
3099 break;
3100 }
3101 }
3102
3103 if( cn_idx == 0 )
3104 return( 0 );
3105
Paul Bakker535e97d2012-08-23 10:49:55 +00003106 if( strlen( cn ) - cn_idx == name->len - 1 &&
3107 memcmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003108 {
3109 return( 1 );
3110 }
3111
3112 return( 0 );
3113}
3114
Paul Bakker915275b2012-09-28 07:10:55 +00003115static int x509parse_verify_top(
3116 x509_cert *child, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003117 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003118 int (*f_vrfy)(void *, x509_cert *, int, int *),
3119 void *p_vrfy )
3120{
3121 int hash_id, ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003122 int ca_flags = 0, check_path_cnt = path_cnt + 1;
Paul Bakker915275b2012-09-28 07:10:55 +00003123 unsigned char hash[64];
3124
3125 if( x509parse_time_expired( &child->valid_to ) )
3126 *flags |= BADCERT_EXPIRED;
3127
3128 /*
3129 * Child is the top of the chain. Check against the trust_ca list.
3130 */
3131 *flags |= BADCERT_NOT_TRUSTED;
3132
3133 while( trust_ca != NULL )
3134 {
3135 if( trust_ca->version == 0 ||
3136 child->issuer_raw.len != trust_ca->subject_raw.len ||
3137 memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
3138 child->issuer_raw.len ) != 0 )
3139 {
3140 trust_ca = trust_ca->next;
3141 continue;
3142 }
3143
Paul Bakker9a736322012-11-14 12:39:52 +00003144 /*
3145 * Reduce path_len to check against if top of the chain is
3146 * the same as the trusted CA
3147 */
3148 if( child->subject_raw.len == trust_ca->subject_raw.len &&
3149 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3150 child->issuer_raw.len ) == 0 )
3151 {
3152 check_path_cnt--;
3153 }
3154
Paul Bakker915275b2012-09-28 07:10:55 +00003155 if( trust_ca->max_pathlen > 0 &&
Paul Bakker9a736322012-11-14 12:39:52 +00003156 trust_ca->max_pathlen < check_path_cnt )
Paul Bakker915275b2012-09-28 07:10:55 +00003157 {
3158 trust_ca = trust_ca->next;
3159 continue;
3160 }
3161
3162 hash_id = child->sig_alg;
3163
3164 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3165
3166 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3167 0, hash, child->sig.p ) != 0 )
3168 {
3169 trust_ca = trust_ca->next;
3170 continue;
3171 }
3172
3173 /*
3174 * Top of chain is signed by a trusted CA
3175 */
3176 *flags &= ~BADCERT_NOT_TRUSTED;
3177 break;
3178 }
3179
Paul Bakker9a736322012-11-14 12:39:52 +00003180 /*
Paul Bakker3497d8c2012-11-24 11:53:17 +01003181 * If top of chain is not the same as the trusted CA send a verify request
3182 * to the callback for any issues with validity and CRL presence for the
3183 * trusted CA certificate.
Paul Bakker9a736322012-11-14 12:39:52 +00003184 */
3185 if( trust_ca != NULL &&
3186 ( child->subject_raw.len != trust_ca->subject_raw.len ||
3187 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
3188 child->issuer_raw.len ) != 0 ) )
Paul Bakker915275b2012-09-28 07:10:55 +00003189 {
3190 /* Check trusted CA's CRL for then chain's top crt */
3191 *flags |= x509parse_verifycrl( child, trust_ca, ca_crl );
3192
3193 if( x509parse_time_expired( &trust_ca->valid_to ) )
3194 ca_flags |= BADCERT_EXPIRED;
3195
Paul Bakker915275b2012-09-28 07:10:55 +00003196 if( NULL != f_vrfy )
3197 {
Paul Bakker9a736322012-11-14 12:39:52 +00003198 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003199 return( ret );
3200 }
3201 }
3202
3203 /* Call callback on top cert */
3204 if( NULL != f_vrfy )
3205 {
Paul Bakker9a736322012-11-14 12:39:52 +00003206 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003207 return( ret );
3208 }
3209
Paul Bakker915275b2012-09-28 07:10:55 +00003210 *flags |= ca_flags;
3211
3212 return( 0 );
3213}
3214
3215static int x509parse_verify_child(
3216 x509_cert *child, x509_cert *parent, x509_cert *trust_ca,
Paul Bakker9a736322012-11-14 12:39:52 +00003217 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003218 int (*f_vrfy)(void *, x509_cert *, int, int *),
3219 void *p_vrfy )
3220{
3221 int hash_id, ret;
3222 int parent_flags = 0;
3223 unsigned char hash[64];
3224 x509_cert *grandparent;
3225
3226 if( x509parse_time_expired( &child->valid_to ) )
3227 *flags |= BADCERT_EXPIRED;
3228
3229 hash_id = child->sig_alg;
3230
3231 x509_hash( child->tbs.p, child->tbs.len, hash_id, hash );
3232
3233 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
3234 child->sig.p ) != 0 )
3235 *flags |= BADCERT_NOT_TRUSTED;
3236
3237 /* Check trusted CA's CRL for the given crt */
3238 *flags |= x509parse_verifycrl(child, parent, ca_crl);
3239
3240 grandparent = parent->next;
3241
3242 while( grandparent != NULL )
3243 {
3244 if( grandparent->version == 0 ||
3245 grandparent->ca_istrue == 0 ||
3246 parent->issuer_raw.len != grandparent->subject_raw.len ||
3247 memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
3248 parent->issuer_raw.len ) != 0 )
3249 {
3250 grandparent = grandparent->next;
3251 continue;
3252 }
3253 break;
3254 }
3255
Paul Bakker915275b2012-09-28 07:10:55 +00003256 if( grandparent != NULL )
3257 {
3258 /*
3259 * Part of the chain
3260 */
Paul Bakker9a736322012-11-14 12:39:52 +00003261 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 +00003262 if( ret != 0 )
3263 return( ret );
3264 }
3265 else
3266 {
Paul Bakker9a736322012-11-14 12:39:52 +00003267 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 +00003268 if( ret != 0 )
3269 return( ret );
3270 }
3271
3272 /* child is verified to be a child of the parent, call verify callback */
3273 if( NULL != f_vrfy )
Paul Bakker9a736322012-11-14 12:39:52 +00003274 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker915275b2012-09-28 07:10:55 +00003275 return( ret );
Paul Bakker915275b2012-09-28 07:10:55 +00003276
3277 *flags |= parent_flags;
3278
3279 return( 0 );
3280}
3281
Paul Bakker76fd75a2011-01-16 21:12:10 +00003282/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 * Verify the certificate validity
3284 */
3285int x509parse_verify( x509_cert *crt,
3286 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00003287 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003288 const char *cn, int *flags,
Paul Bakker915275b2012-09-28 07:10:55 +00003289 int (*f_vrfy)(void *, x509_cert *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003290 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291{
Paul Bakker23986e52011-04-24 08:57:21 +00003292 size_t cn_len;
Paul Bakker915275b2012-09-28 07:10:55 +00003293 int ret;
Paul Bakker9a736322012-11-14 12:39:52 +00003294 int pathlen = 0;
Paul Bakker76fd75a2011-01-16 21:12:10 +00003295 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 x509_name *name;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003297 x509_sequence *cur = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
Paul Bakker40ea7de2009-05-03 10:18:48 +00003299 *flags = 0;
3300
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 if( cn != NULL )
3302 {
3303 name = &crt->subject;
3304 cn_len = strlen( cn );
3305
Paul Bakker4d2c1242012-05-10 14:12:46 +00003306 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
Paul Bakker4d2c1242012-05-10 14:12:46 +00003308 cur = &crt->subject_alt_names;
3309
3310 while( cur != NULL )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003311 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003312 if( cur->buf.len == cn_len &&
3313 memcmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003314 break;
3315
Paul Bakker535e97d2012-08-23 10:49:55 +00003316 if( cur->buf.len > 2 &&
3317 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003318 x509_wildcard_verify( cn, &cur->buf ) )
Paul Bakkera8cd2392012-02-11 16:09:32 +00003319 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
Paul Bakker4d2c1242012-05-10 14:12:46 +00003321 cur = cur->next;
Paul Bakkera8cd2392012-02-11 16:09:32 +00003322 }
3323
3324 if( cur == NULL )
3325 *flags |= BADCERT_CN_MISMATCH;
3326 }
Paul Bakker4d2c1242012-05-10 14:12:46 +00003327 else
3328 {
3329 while( name != NULL )
3330 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003331 if( name->oid.len == 3 &&
3332 memcmp( name->oid.p, OID_CN, 3 ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003333 {
Paul Bakker535e97d2012-08-23 10:49:55 +00003334 if( name->val.len == cn_len &&
3335 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker4d2c1242012-05-10 14:12:46 +00003336 break;
3337
Paul Bakker535e97d2012-08-23 10:49:55 +00003338 if( name->val.len > 2 &&
3339 memcmp( name->val.p, "*.", 2 ) == 0 &&
Paul Bakker4d2c1242012-05-10 14:12:46 +00003340 x509_wildcard_verify( cn, &name->val ) )
3341 break;
3342 }
3343
3344 name = name->next;
3345 }
3346
3347 if( name == NULL )
3348 *flags |= BADCERT_CN_MISMATCH;
3349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 }
3351
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 /*
Paul Bakker915275b2012-09-28 07:10:55 +00003353 * Iterate upwards in the given cert chain, to find our crt parent.
3354 * Ignore any upper cert with CA != TRUE.
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00003356 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
Paul Bakker76fd75a2011-01-16 21:12:10 +00003358 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003359 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003360 if( parent->ca_istrue == 0 ||
3361 crt->issuer_raw.len != parent->subject_raw.len ||
3362 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 crt->issuer_raw.len ) != 0 )
3364 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003365 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 continue;
3367 }
Paul Bakker915275b2012-09-28 07:10:55 +00003368 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 }
3370
Paul Bakker915275b2012-09-28 07:10:55 +00003371 if( parent != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003372 {
Paul Bakker915275b2012-09-28 07:10:55 +00003373 /*
3374 * Part of the chain
3375 */
Paul Bakker9a736322012-11-14 12:39:52 +00003376 ret = x509parse_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003377 if( ret != 0 )
3378 return( ret );
3379 }
3380 else
Paul Bakker74111d32011-01-15 16:57:55 +00003381 {
Paul Bakker9a736322012-11-14 12:39:52 +00003382 ret = x509parse_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker915275b2012-09-28 07:10:55 +00003383 if( ret != 0 )
3384 return( ret );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003385 }
Paul Bakker915275b2012-09-28 07:10:55 +00003386
3387 if( *flags != 0 )
Paul Bakker76fd75a2011-01-16 21:12:10 +00003388 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003389
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 return( 0 );
3391}
3392
3393/*
3394 * Unallocate all certificate data
3395 */
3396void x509_free( x509_cert *crt )
3397{
3398 x509_cert *cert_cur = crt;
3399 x509_cert *cert_prv;
3400 x509_name *name_cur;
3401 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003402 x509_sequence *seq_cur;
3403 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
3405 if( crt == NULL )
3406 return;
3407
3408 do
3409 {
3410 rsa_free( &cert_cur->rsa );
3411
3412 name_cur = cert_cur->issuer.next;
3413 while( name_cur != NULL )
3414 {
3415 name_prv = name_cur;
3416 name_cur = name_cur->next;
3417 memset( name_prv, 0, sizeof( x509_name ) );
3418 free( name_prv );
3419 }
3420
3421 name_cur = cert_cur->subject.next;
3422 while( name_cur != NULL )
3423 {
3424 name_prv = name_cur;
3425 name_cur = name_cur->next;
3426 memset( name_prv, 0, sizeof( x509_name ) );
3427 free( name_prv );
3428 }
3429
Paul Bakker74111d32011-01-15 16:57:55 +00003430 seq_cur = cert_cur->ext_key_usage.next;
3431 while( seq_cur != NULL )
3432 {
3433 seq_prv = seq_cur;
3434 seq_cur = seq_cur->next;
3435 memset( seq_prv, 0, sizeof( x509_sequence ) );
3436 free( seq_prv );
3437 }
3438
Paul Bakker8afa70d2012-02-11 18:42:45 +00003439 seq_cur = cert_cur->subject_alt_names.next;
3440 while( seq_cur != NULL )
3441 {
3442 seq_prv = seq_cur;
3443 seq_cur = seq_cur->next;
3444 memset( seq_prv, 0, sizeof( x509_sequence ) );
3445 free( seq_prv );
3446 }
3447
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 if( cert_cur->raw.p != NULL )
3449 {
3450 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3451 free( cert_cur->raw.p );
3452 }
3453
3454 cert_cur = cert_cur->next;
3455 }
3456 while( cert_cur != NULL );
3457
3458 cert_cur = crt;
3459 do
3460 {
3461 cert_prv = cert_cur;
3462 cert_cur = cert_cur->next;
3463
3464 memset( cert_prv, 0, sizeof( x509_cert ) );
3465 if( cert_prv != crt )
3466 free( cert_prv );
3467 }
3468 while( cert_cur != NULL );
3469}
3470
Paul Bakkerd98030e2009-05-02 15:13:40 +00003471/*
3472 * Unallocate all CRL data
3473 */
3474void x509_crl_free( x509_crl *crl )
3475{
3476 x509_crl *crl_cur = crl;
3477 x509_crl *crl_prv;
3478 x509_name *name_cur;
3479 x509_name *name_prv;
3480 x509_crl_entry *entry_cur;
3481 x509_crl_entry *entry_prv;
3482
3483 if( crl == NULL )
3484 return;
3485
3486 do
3487 {
3488 name_cur = crl_cur->issuer.next;
3489 while( name_cur != NULL )
3490 {
3491 name_prv = name_cur;
3492 name_cur = name_cur->next;
3493 memset( name_prv, 0, sizeof( x509_name ) );
3494 free( name_prv );
3495 }
3496
3497 entry_cur = crl_cur->entry.next;
3498 while( entry_cur != NULL )
3499 {
3500 entry_prv = entry_cur;
3501 entry_cur = entry_cur->next;
3502 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3503 free( entry_prv );
3504 }
3505
3506 if( crl_cur->raw.p != NULL )
3507 {
3508 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3509 free( crl_cur->raw.p );
3510 }
3511
3512 crl_cur = crl_cur->next;
3513 }
3514 while( crl_cur != NULL );
3515
3516 crl_cur = crl;
3517 do
3518 {
3519 crl_prv = crl_cur;
3520 crl_cur = crl_cur->next;
3521
3522 memset( crl_prv, 0, sizeof( x509_crl ) );
3523 if( crl_prv != crl )
3524 free( crl_prv );
3525 }
3526 while( crl_cur != NULL );
3527}
3528
Paul Bakker40e46942009-01-03 21:51:57 +00003529#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
Paul Bakker40e46942009-01-03 21:51:57 +00003531#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
3533/*
3534 * Checkup routine
3535 */
3536int x509_self_test( int verbose )
3537{
Paul Bakker5690efc2011-05-26 13:16:06 +00003538#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003539 int ret;
3540 int flags;
3541 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 x509_cert cacert;
3543 x509_cert clicert;
3544 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003545#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003546 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
3549 if( verbose != 0 )
3550 printf( " X.509 certificate load: " );
3551
3552 memset( &clicert, 0, sizeof( x509_cert ) );
3553
3554 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003555 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 if( ret != 0 )
3557 {
3558 if( verbose != 0 )
3559 printf( "failed\n" );
3560
3561 return( ret );
3562 }
3563
3564 memset( &cacert, 0, sizeof( x509_cert ) );
3565
3566 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +00003567 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 if( ret != 0 )
3569 {
3570 if( verbose != 0 )
3571 printf( "failed\n" );
3572
3573 return( ret );
3574 }
3575
3576 if( verbose != 0 )
3577 printf( "passed\n X.509 private key load: " );
3578
3579 i = strlen( test_ca_key );
3580 j = strlen( test_ca_pwd );
3581
Paul Bakker66b78b22011-03-25 14:22:50 +00003582 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3583
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 if( ( ret = x509parse_key( &rsa,
3585 (unsigned char *) test_ca_key, i,
3586 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3587 {
3588 if( verbose != 0 )
3589 printf( "failed\n" );
3590
3591 return( ret );
3592 }
3593
3594 if( verbose != 0 )
3595 printf( "passed\n X.509 signature verify: ");
3596
Paul Bakker23986e52011-04-24 08:57:21 +00003597 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 if( ret != 0 )
3599 {
Paul Bakker23986e52011-04-24 08:57:21 +00003600 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 if( verbose != 0 )
3602 printf( "failed\n" );
3603
3604 return( ret );
3605 }
3606
Paul Bakker5690efc2011-05-26 13:16:06 +00003607#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003609 printf( "passed\n X.509 DHM parameter load: " );
3610
3611 i = strlen( test_dhm_params );
3612 j = strlen( test_ca_pwd );
3613
3614 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3615 {
3616 if( verbose != 0 )
3617 printf( "failed\n" );
3618
3619 return( ret );
3620 }
3621
3622 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
3626 x509_free( &cacert );
3627 x509_free( &clicert );
3628 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003629#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003630 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003631#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
3633 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003634#else
3635 ((void) verbose);
3636 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003638}
3639
3640#endif
3641
3642#endif