blob: 631fe55308383e78b821788fd099a5cf04ca9f47 [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/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
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 Bakkercce9d772011-11-18 14:26:47 +000055#if defined(_WIN32_WCE)
56#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>
63#endif
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065/*
Paul Bakker5121ce52009-01-03 21:22:43 +000066 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
67 */
68static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000069 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000070 int *ver )
71{
Paul Bakker23986e52011-04-24 08:57:21 +000072 int ret;
73 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 if( ( ret = asn1_get_tag( p, end, &len,
76 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
77 {
Paul Bakker40e46942009-01-03 21:51:57 +000078 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000079 {
80 *ver = 0;
81 return( 0 );
82 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 return( ret );
85 }
86
87 end = *p + len;
88
89 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000090 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +000093 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +000094 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( 0 );
97}
98
99/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000100 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000101 */
102static int x509_crl_get_version( unsigned char **p,
103 const unsigned char *end,
104 int *ver )
105{
106 int ret;
107
108 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
109 {
110 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000111 {
112 *ver = 0;
113 return( 0 );
114 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000115
116 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
117 }
118
119 return( 0 );
120}
121
122/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 * CertificateSerialNumber ::= INTEGER
124 */
125static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 x509_buf *serial )
128{
129 int ret;
130
131 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000133 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
136 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000137 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000138 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 serial->tag = *(*p)++;
141
142 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000143 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 serial->p = *p;
146 *p += serial->len;
147
148 return( 0 );
149}
150
151/*
152 * AlgorithmIdentifier ::= SEQUENCE {
153 * algorithm OBJECT IDENTIFIER,
154 * parameters ANY DEFINED BY algorithm OPTIONAL }
155 */
156static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000157 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 x509_buf *alg )
159{
Paul Bakker23986e52011-04-24 08:57:21 +0000160 int ret;
161 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ( ret = asn1_get_tag( p, end, &len,
164 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000165 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 end = *p + len;
168 alg->tag = **p;
169
170 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000171 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 alg->p = *p;
174 *p += alg->len;
175
176 if( *p == end )
177 return( 0 );
178
179 /*
180 * assume the algorithm parameters must be NULL
181 */
182 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000186 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000187 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 return( 0 );
190}
191
192/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 * AttributeTypeAndValue ::= SEQUENCE {
194 * type AttributeType,
195 * value AttributeValue }
196 *
197 * AttributeType ::= OBJECT IDENTIFIER
198 *
199 * AttributeValue ::= ANY DEFINED BY AttributeType
200 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000201static int x509_get_attr_type_value( unsigned char **p,
202 const unsigned char *end,
203 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204{
Paul Bakker23986e52011-04-24 08:57:21 +0000205 int ret;
206 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 x509_buf *oid;
208 x509_buf *val;
209
210 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 oid = &cur->oid;
215 oid->tag = **p;
216
217 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 oid->p = *p;
221 *p += oid->len;
222
223 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000225 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
228 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
229 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000231 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 val = &cur->val;
234 val->tag = *(*p)++;
235
236 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000237 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 val->p = *p;
240 *p += val->len;
241
242 cur->next = NULL;
243
Paul Bakker400ff6f2011-02-20 10:40:16 +0000244 return( 0 );
245}
246
247/*
248 * RelativeDistinguishedName ::=
249 * SET OF AttributeTypeAndValue
250 *
251 * AttributeTypeAndValue ::= SEQUENCE {
252 * type AttributeType,
253 * value AttributeValue }
254 *
255 * AttributeType ::= OBJECT IDENTIFIER
256 *
257 * AttributeValue ::= ANY DEFINED BY AttributeType
258 */
259static int x509_get_name( unsigned char **p,
260 const unsigned char *end,
261 x509_name *cur )
262{
Paul Bakker23986e52011-04-24 08:57:21 +0000263 int ret;
264 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000265 const unsigned char *end2;
266 x509_name *use;
267
268 if( ( ret = asn1_get_tag( p, end, &len,
269 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000270 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000271
272 end2 = end;
273 end = *p + len;
274 use = cur;
275
276 do
277 {
278 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
279 return( ret );
280
281 if( *p != end )
282 {
283 use->next = (x509_name *) malloc(
284 sizeof( x509_name ) );
285
286 if( use->next == NULL )
287 return( 1 );
288
289 memset( use->next, 0, sizeof( x509_name ) );
290
291 use = use->next;
292 }
293 }
294 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 /*
297 * recurse until end of SEQUENCE is reached
298 */
299 if( *p == end2 )
300 return( 0 );
301
302 cur->next = (x509_name *) malloc(
303 sizeof( x509_name ) );
304
305 if( cur->next == NULL )
306 return( 1 );
307
308 return( x509_get_name( p, end2, cur->next ) );
309}
310
311/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * Time ::= CHOICE {
313 * utcTime UTCTime,
314 * generalTime GeneralizedTime }
315 */
Paul Bakker91200182010-02-18 21:26:15 +0000316static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000318 x509_time *time )
319{
Paul Bakker23986e52011-04-24 08:57:21 +0000320 int ret;
321 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000322 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000323 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324
Paul Bakker91200182010-02-18 21:26:15 +0000325 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000326 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
327 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328
Paul Bakker91200182010-02-18 21:26:15 +0000329 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 if ( tag == ASN1_UTC_TIME )
332 {
333 (*p)++;
334 ret = asn1_get_len( p, end, &len );
335
336 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000337 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000340 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
341 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
344 &time->year, &time->mon, &time->day,
345 &time->hour, &time->min, &time->sec ) < 5 )
346 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000347
Paul Bakker400ff6f2011-02-20 10:40:16 +0000348 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000349 time->year += 1900;
350
351 *p += len;
352
353 return( 0 );
354 }
355 else if ( tag == ASN1_GENERALIZED_TIME )
356 {
357 (*p)++;
358 ret = asn1_get_len( p, end, &len );
359
360 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000361 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000362
363 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000364 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
365 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000366
367 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
368 &time->year, &time->mon, &time->day,
369 &time->hour, &time->min, &time->sec ) < 5 )
370 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
371
372 *p += len;
373
374 return( 0 );
375 }
376 else
Paul Bakker9d781402011-05-09 16:17:09 +0000377 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000378}
379
380
381/*
382 * Validity ::= SEQUENCE {
383 * notBefore Time,
384 * notAfter Time }
385 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000386static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000387 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 x509_time *from,
389 x509_time *to )
390{
Paul Bakker23986e52011-04-24 08:57:21 +0000391 int ret;
392 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394 if( ( ret = asn1_get_tag( p, end, &len,
395 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000396 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398 end = *p + len;
399
Paul Bakker91200182010-02-18 21:26:15 +0000400 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000401 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Paul Bakker91200182010-02-18 21:26:15 +0000403 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000407 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000408 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410 return( 0 );
411}
412
413/*
414 * SubjectPublicKeyInfo ::= SEQUENCE {
415 * algorithm AlgorithmIdentifier,
416 * subjectPublicKey BIT STRING }
417 */
418static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000419 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 x509_buf *pk_alg_oid,
421 mpi *N, mpi *E )
422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 int ret, can_handle;
424 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 unsigned char *end2;
426
427 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
428 return( ret );
429
430 /*
431 * only RSA public keys handled at this time
432 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000433 can_handle = 0;
434
435 if( pk_alg_oid->len == 9 &&
436 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
437 can_handle = 1;
438
439 if( pk_alg_oid->len == 9 &&
440 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
441 {
442 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
443 can_handle = 1;
444
445 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
446 can_handle = 1;
447 }
448
449 if( pk_alg_oid->len == 5 &&
450 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
451 can_handle = 1;
452
453 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000454 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000457 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000460 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000461 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 end2 = *p + len;
464
465 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000466 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * RSAPublicKey ::= SEQUENCE {
470 * modulus INTEGER, -- n
471 * publicExponent INTEGER -- e
472 * }
473 */
474 if( ( ret = asn1_get_tag( p, end2, &len,
475 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000476 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000479 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000480 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
483 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000484 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000487 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000488 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 return( 0 );
491}
492
493static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 x509_buf *sig )
496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 int ret;
498 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 sig->tag = **p;
501
502 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000503 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker74111d32011-01-15 16:57:55 +0000505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000507 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 sig->len = len;
510 sig->p = *p;
511
512 *p += len;
513
514 return( 0 );
515}
516
517/*
518 * X.509 v2/v3 unique identifier (not parsed)
519 */
520static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 x509_buf *uid, int n )
523{
524 int ret;
525
526 if( *p == end )
527 return( 0 );
528
529 uid->tag = **p;
530
531 if( ( ret = asn1_get_tag( p, end, &uid->len,
532 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
533 {
Paul Bakker40e46942009-01-03 21:51:57 +0000534 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 return( 0 );
536
537 return( ret );
538 }
539
540 uid->p = *p;
541 *p += uid->len;
542
543 return( 0 );
544}
545
546/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000547 * X.509 Extensions (No parsing of extensions, pointer should
548 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 */
550static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000551 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000552 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000553{
Paul Bakker23986e52011-04-24 08:57:21 +0000554 int ret;
555 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( *p == end )
558 return( 0 );
559
560 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000563 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 ext->p = *p;
567 end = *p + ext->len;
568
569 /*
570 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
571 *
572 * Extension ::= SEQUENCE {
573 * extnID OBJECT IDENTIFIER,
574 * critical BOOLEAN DEFAULT FALSE,
575 * extnValue OCTET STRING }
576 */
577 if( ( ret = asn1_get_tag( p, end, &len,
578 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000579 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000582 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000583 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Paul Bakkerd98030e2009-05-02 15:13:40 +0000585 return( 0 );
586}
587
588/*
589 * X.509 CRL v2 extensions (no extensions parsed yet.)
590 */
591static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000592 const unsigned char *end,
593 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000594{
Paul Bakker23986e52011-04-24 08:57:21 +0000595 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000596 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000597
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000598 /* Get explicit tag */
599 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000600 {
601 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
602 return( 0 );
603
604 return( ret );
605 }
606
607 while( *p < end )
608 {
609 if( ( ret = asn1_get_tag( p, end, &len,
610 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000611 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000612
613 *p += len;
614 }
615
616 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000617 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000618 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
619
620 return( 0 );
621}
622
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000623/*
624 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
625 */
626static int x509_get_crl_entry_ext( unsigned char **p,
627 const unsigned char *end,
628 x509_buf *ext )
629{
630 int ret;
631 size_t len = 0;
632
633 /* OPTIONAL */
634 if (end <= *p)
635 return( 0 );
636
637 ext->tag = **p;
638 ext->p = *p;
639
640 /*
641 * Get CRL-entry extension sequence header
642 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
643 */
644 if( ( ret = asn1_get_tag( p, end, &ext->len,
645 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
646 {
647 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
648 {
649 ext->p = NULL;
650 return( 0 );
651 }
652 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
653 }
654
655 end = *p + ext->len;
656
657 if( end != *p + ext->len )
658 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
659 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
660
661 while( *p < end )
662 {
663 if( ( ret = asn1_get_tag( p, end, &len,
664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
665 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
666
667 *p += len;
668 }
669
670 if( *p != end )
671 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
672 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
673
674 return( 0 );
675}
676
Paul Bakker74111d32011-01-15 16:57:55 +0000677static int x509_get_basic_constraints( unsigned char **p,
678 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000679 int *ca_istrue,
680 int *max_pathlen )
681{
Paul Bakker23986e52011-04-24 08:57:21 +0000682 int ret;
683 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000684
685 /*
686 * BasicConstraints ::= SEQUENCE {
687 * cA BOOLEAN DEFAULT FALSE,
688 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
689 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000690 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000691 *max_pathlen = 0; /* endless */
692
693 if( ( ret = asn1_get_tag( p, end, &len,
694 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000695 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000696
697 if( *p == end )
698 return 0;
699
Paul Bakker3cccddb2011-01-16 21:46:31 +0000700 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000701 {
702 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000703 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000704
705 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000706 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000707
Paul Bakker3cccddb2011-01-16 21:46:31 +0000708 if( *ca_istrue != 0 )
709 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000710 }
711
712 if( *p == end )
713 return 0;
714
715 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 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
718 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000719 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000720 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
721
722 (*max_pathlen)++;
723
Paul Bakker74111d32011-01-15 16:57:55 +0000724 return 0;
725}
726
727static int x509_get_ns_cert_type( unsigned char **p,
728 const unsigned char *end,
729 unsigned char *ns_cert_type)
730{
731 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000732 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000733
734 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000735 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000736
737 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000738 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000739 POLARSSL_ERR_ASN1_INVALID_LENGTH );
740
741 /* Get actual bitstring */
742 *ns_cert_type = *bs.p;
743 return 0;
744}
745
746static int x509_get_key_usage( unsigned char **p,
747 const unsigned char *end,
748 unsigned char *key_usage)
749{
750 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000751 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000752
753 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000754 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000755
Paul Bakkercebdf172011-11-11 15:01:31 +0000756 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000757 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000758 POLARSSL_ERR_ASN1_INVALID_LENGTH );
759
760 /* Get actual bitstring */
761 *key_usage = *bs.p;
762 return 0;
763}
764
Paul Bakkerd98030e2009-05-02 15:13:40 +0000765/*
Paul Bakker74111d32011-01-15 16:57:55 +0000766 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
767 *
768 * KeyPurposeId ::= OBJECT IDENTIFIER
769 */
770static int x509_get_ext_key_usage( unsigned char **p,
771 const unsigned char *end,
772 x509_sequence *ext_key_usage)
773{
774 int ret;
775
776 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000777 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000778
779 /* Sequence length must be >= 1 */
780 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000781 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000782 POLARSSL_ERR_ASN1_INVALID_LENGTH );
783
784 return 0;
785}
786
787/*
788 * X.509 v3 extensions
789 *
790 * TODO: Perform all of the basic constraints tests required by the RFC
791 * TODO: Set values for undetected extensions to a sane default?
792 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000793 */
794static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000795 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000796 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000797{
Paul Bakker23986e52011-04-24 08:57:21 +0000798 int ret;
799 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000800 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000801
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000802 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000803 {
804 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
805 return( 0 );
806
807 return( ret );
808 }
809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 while( *p < end )
811 {
Paul Bakker74111d32011-01-15 16:57:55 +0000812 /*
813 * Extension ::= SEQUENCE {
814 * extnID OBJECT IDENTIFIER,
815 * critical BOOLEAN DEFAULT FALSE,
816 * extnValue OCTET STRING }
817 */
818 x509_buf extn_oid = {0, 0, NULL};
819 int is_critical = 0; /* DEFAULT FALSE */
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 if( ( ret = asn1_get_tag( p, end, &len,
822 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000823 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000825 end_ext_data = *p + len;
826
Paul Bakker74111d32011-01-15 16:57:55 +0000827 /* Get extension ID */
828 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Paul Bakker74111d32011-01-15 16:57:55 +0000830 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000831 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker74111d32011-01-15 16:57:55 +0000833 extn_oid.p = *p;
834 *p += extn_oid.len;
835
836 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000838 POLARSSL_ERR_ASN1_OUT_OF_DATA );
839
840 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000841 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000842 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000843 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
Paul Bakker74111d32011-01-15 16:57:55 +0000845 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000846 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000848 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000850 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000851
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000852 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000853 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000854 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker74111d32011-01-15 16:57:55 +0000856 /*
857 * Detect supported extensions
858 */
859 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
860 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 {
Paul Bakker74111d32011-01-15 16:57:55 +0000862 /* Parse basic constraints */
863 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000864 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000865 return ( ret );
866 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 }
Paul Bakker74111d32011-01-15 16:57:55 +0000868 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
869 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
870 {
871 /* Parse netscape certificate type */
872 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
873 &crt->ns_cert_type ) ) != 0 )
874 return ( ret );
875 crt->ext_types |= EXT_NS_CERT_TYPE;
876 }
877 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
878 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
879 {
880 /* Parse key usage */
881 if( ( ret = x509_get_key_usage( p, end_ext_octet,
882 &crt->key_usage ) ) != 0 )
883 return ( ret );
884 crt->ext_types |= EXT_KEY_USAGE;
885 }
886 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
887 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
888 {
889 /* Parse extended key usage */
890 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
891 &crt->ext_key_usage ) ) != 0 )
892 return ( ret );
893 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
894 }
895 else
896 {
897 /* No parser found, skip extension */
898 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Paul Bakker5c721f92011-07-27 16:51:09 +0000900#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000901 if( is_critical )
902 {
903 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000904 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000905 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
906 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000907#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000908 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 }
910
911 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000912 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000913 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 return( 0 );
916}
917
918/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000919 * X.509 CRL Entries
920 */
921static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000922 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000923 x509_crl_entry *entry )
924{
Paul Bakker23986e52011-04-24 08:57:21 +0000925 int ret;
926 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000927 x509_crl_entry *cur_entry = entry;
928
929 if( *p == end )
930 return( 0 );
931
Paul Bakker9be19372009-07-27 20:21:53 +0000932 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
934 {
935 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
936 return( 0 );
937
938 return( ret );
939 }
940
Paul Bakker9be19372009-07-27 20:21:53 +0000941 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000942
943 while( *p < end )
944 {
Paul Bakker23986e52011-04-24 08:57:21 +0000945 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000946 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947
948 if( ( ret = asn1_get_tag( p, end, &len2,
949 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
950 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000951 return( ret );
952 }
953
Paul Bakker9be19372009-07-27 20:21:53 +0000954 cur_entry->raw.tag = **p;
955 cur_entry->raw.p = *p;
956 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000957 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000958
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000959 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000960 return( ret );
961
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000962 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000963 return( ret );
964
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000965 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000966 return( ret );
967
Paul Bakker74111d32011-01-15 16:57:55 +0000968 if ( *p < end )
969 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000970 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
971 cur_entry = cur_entry->next;
972 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
973 }
974 }
975
976 return( 0 );
977}
978
Paul Bakker27d66162010-03-17 06:56:01 +0000979static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
980{
981 if( sig_oid->len == 9 &&
982 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
983 {
984 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
985 {
986 *sig_alg = sig_oid->p[8];
987 return( 0 );
988 }
989
990 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
991 {
992 *sig_alg = sig_oid->p[8];
993 return( 0 );
994 }
995
996 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
997 }
Paul Bakker400ff6f2011-02-20 10:40:16 +0000998 if( sig_oid->len == 5 &&
999 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1000 {
1001 *sig_alg = SIG_RSA_SHA1;
1002 return( 0 );
1003 }
Paul Bakker27d66162010-03-17 06:56:01 +00001004
1005 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1006}
1007
Paul Bakkerd98030e2009-05-02 15:13:40 +00001008/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 * Parse one or more certificates and add them to the chained list
1010 */
Paul Bakker23986e52011-04-24 08:57:21 +00001011int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001012{
Paul Bakker23986e52011-04-24 08:57:21 +00001013 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001014 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 unsigned char *p, *end;
1016 x509_cert *crt;
Paul Bakker96743fc2011-02-12 14:30:57 +00001017#if defined(POLARSSL_PEM_C)
1018 pem_context pem;
Paul Bakker5690efc2011-05-26 13:16:06 +00001019 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001020#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
1022 crt = chain;
1023
Paul Bakker320a4b52009-03-28 18:52:39 +00001024 /*
1025 * Check for valid input
1026 */
1027 if( crt == NULL || buf == NULL )
1028 return( 1 );
1029
Paul Bakkere9581d62009-03-28 20:29:25 +00001030 while( crt->version != 0 && crt->next != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 crt = crt->next;
1032
1033 /*
Paul Bakker320a4b52009-03-28 18:52:39 +00001034 * Add new certificate on the end of the chain if needed.
1035 */
Paul Bakkere9581d62009-03-28 20:29:25 +00001036 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001037 {
1038 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1039
Paul Bakker7d06ad22009-05-02 15:53:56 +00001040 if( crt->next == NULL )
1041 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001042 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001043 return( 1 );
1044 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001045
Paul Bakker7d06ad22009-05-02 15:53:56 +00001046 crt = crt->next;
1047 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001048 }
1049
Paul Bakker96743fc2011-02-12 14:30:57 +00001050#if defined(POLARSSL_PEM_C)
1051 pem_init( &pem );
1052 ret = pem_read_buffer( &pem,
1053 "-----BEGIN CERTIFICATE-----",
1054 "-----END CERTIFICATE-----",
1055 buf, NULL, 0, &use_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Paul Bakker96743fc2011-02-12 14:30:57 +00001057 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001059 /*
1060 * Was PEM encoded
1061 */
1062 buflen -= use_len;
1063 buf += use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
1065 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001066 * Steal PEM buffer
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001068 p = pem.buf;
1069 pem.buf = NULL;
1070 len = pem.buflen;
1071 pem_free( &pem );
1072 }
1073 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1074 {
1075 pem_free( &pem );
1076 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001077 }
1078 else
1079 {
1080 /*
1081 * nope, copy the raw DER data
1082 */
1083 p = (unsigned char *) malloc( len = buflen );
1084
1085 if( p == NULL )
1086 return( 1 );
1087
1088 memcpy( p, buf, buflen );
1089
1090 buflen = 0;
1091 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001092#else
1093 p = (unsigned char *) malloc( len = buflen );
1094
1095 if( p == NULL )
1096 return( 1 );
1097
1098 memcpy( p, buf, buflen );
1099
1100 buflen = 0;
1101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
1103 crt->raw.p = p;
1104 crt->raw.len = len;
1105 end = p + len;
1106
1107 /*
1108 * Certificate ::= SEQUENCE {
1109 * tbsCertificate TBSCertificate,
1110 * signatureAlgorithm AlgorithmIdentifier,
1111 * signatureValue BIT STRING }
1112 */
1113 if( ( ret = asn1_get_tag( &p, end, &len,
1114 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1115 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001116 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001117 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 }
1119
Paul Bakker23986e52011-04-24 08:57:21 +00001120 if( len != (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001122 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001123 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001124 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001125 }
1126
1127 /*
1128 * TBSCertificate ::= SEQUENCE {
1129 */
1130 crt->tbs.p = p;
1131
1132 if( ( ret = asn1_get_tag( &p, end, &len,
1133 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1134 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001135 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001136 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 }
1138
1139 end = p + len;
1140 crt->tbs.len = end - crt->tbs.p;
1141
1142 /*
1143 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1144 *
1145 * CertificateSerialNumber ::= INTEGER
1146 *
1147 * signature AlgorithmIdentifier
1148 */
1149 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1150 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1151 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1152 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001153 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 return( ret );
1155 }
1156
1157 crt->version++;
1158
1159 if( crt->version > 3 )
1160 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001161 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001162 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
1164
Paul Bakker27d66162010-03-17 06:56:01 +00001165 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001167 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001168 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
1170
1171 /*
1172 * issuer Name
1173 */
1174 crt->issuer_raw.p = p;
1175
1176 if( ( ret = asn1_get_tag( &p, end, &len,
1177 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1178 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001179 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001180 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 }
1182
1183 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1184 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001185 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 return( ret );
1187 }
1188
1189 crt->issuer_raw.len = p - crt->issuer_raw.p;
1190
1191 /*
1192 * Validity ::= SEQUENCE {
1193 * notBefore Time,
1194 * notAfter Time }
1195 *
1196 */
1197 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1198 &crt->valid_to ) ) != 0 )
1199 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001200 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 return( ret );
1202 }
1203
1204 /*
1205 * subject Name
1206 */
1207 crt->subject_raw.p = p;
1208
1209 if( ( ret = asn1_get_tag( &p, end, &len,
1210 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1211 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001212 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001213 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 }
1215
1216 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1217 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001218 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 return( ret );
1220 }
1221
1222 crt->subject_raw.len = p - crt->subject_raw.p;
1223
1224 /*
1225 * SubjectPublicKeyInfo ::= SEQUENCE
1226 * algorithm AlgorithmIdentifier,
1227 * subjectPublicKey BIT STRING }
1228 */
1229 if( ( ret = asn1_get_tag( &p, end, &len,
1230 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1231 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001232 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001233 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001234 }
1235
1236 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1237 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1238 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001239 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 return( ret );
1241 }
1242
1243 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1244 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001245 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 return( ret );
1247 }
1248
1249 crt->rsa.len = mpi_size( &crt->rsa.N );
1250
1251 /*
1252 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1253 * -- If present, version shall be v2 or v3
1254 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1255 * -- If present, version shall be v2 or v3
1256 * extensions [3] EXPLICIT Extensions OPTIONAL
1257 * -- If present, version shall be v3
1258 */
1259 if( crt->version == 2 || crt->version == 3 )
1260 {
1261 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1262 if( ret != 0 )
1263 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001264 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001265 return( ret );
1266 }
1267 }
1268
1269 if( crt->version == 2 || crt->version == 3 )
1270 {
1271 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1272 if( ret != 0 )
1273 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001274 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001275 return( ret );
1276 }
1277 }
1278
1279 if( crt->version == 3 )
1280 {
Paul Bakker74111d32011-01-15 16:57:55 +00001281 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 if( ret != 0 )
1283 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001284 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 return( ret );
1286 }
1287 }
1288
1289 if( p != end )
1290 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001291 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001292 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001293 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 }
1295
1296 end = crt->raw.p + crt->raw.len;
1297
1298 /*
1299 * signatureAlgorithm AlgorithmIdentifier,
1300 * signatureValue BIT STRING
1301 */
1302 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1303 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001304 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 return( ret );
1306 }
1307
Paul Bakker320a4b52009-03-28 18:52:39 +00001308 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001310 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001311 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 }
1313
1314 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1315 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001316 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 return( ret );
1318 }
1319
1320 if( p != end )
1321 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001322 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001323 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001324 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 if( buflen > 0 )
Paul Bakker320a4b52009-03-28 18:52:39 +00001328 {
1329 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1330
Paul Bakker7d06ad22009-05-02 15:53:56 +00001331 if( crt->next == NULL )
1332 {
Paul Bakker320a4b52009-03-28 18:52:39 +00001333 x509_free( crt );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001334 return( 1 );
1335 }
Paul Bakker320a4b52009-03-28 18:52:39 +00001336
Paul Bakker7d06ad22009-05-02 15:53:56 +00001337 crt = crt->next;
1338 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001339
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 return( x509parse_crt( crt, buf, buflen ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001341 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
1343 return( 0 );
1344}
1345
1346/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001347 * Parse one or more CRLs and add them to the chained list
1348 */
Paul Bakker23986e52011-04-24 08:57:21 +00001349int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001350{
Paul Bakker23986e52011-04-24 08:57:21 +00001351 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001352 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001353 unsigned char *p, *end;
1354 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001355#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001356 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001357 pem_context pem;
1358#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001359
1360 crl = chain;
1361
1362 /*
1363 * Check for valid input
1364 */
1365 if( crl == NULL || buf == NULL )
1366 return( 1 );
1367
1368 while( crl->version != 0 && crl->next != NULL )
1369 crl = crl->next;
1370
1371 /*
1372 * Add new CRL on the end of the chain if needed.
1373 */
1374 if ( crl->version != 0 && crl->next == NULL)
1375 {
1376 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1377
Paul Bakker7d06ad22009-05-02 15:53:56 +00001378 if( crl->next == NULL )
1379 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001380 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001381 return( 1 );
1382 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001383
Paul Bakker7d06ad22009-05-02 15:53:56 +00001384 crl = crl->next;
1385 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001386 }
1387
Paul Bakker96743fc2011-02-12 14:30:57 +00001388#if defined(POLARSSL_PEM_C)
1389 pem_init( &pem );
1390 ret = pem_read_buffer( &pem,
1391 "-----BEGIN X509 CRL-----",
1392 "-----END X509 CRL-----",
1393 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001394
Paul Bakker96743fc2011-02-12 14:30:57 +00001395 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001396 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001397 /*
1398 * Was PEM encoded
1399 */
1400 buflen -= use_len;
1401 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001402
1403 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001404 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001405 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001406 p = pem.buf;
1407 pem.buf = NULL;
1408 len = pem.buflen;
1409 pem_free( &pem );
1410 }
1411 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1412 {
1413 pem_free( &pem );
1414 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001415 }
1416 else
1417 {
1418 /*
1419 * nope, copy the raw DER data
1420 */
1421 p = (unsigned char *) malloc( len = buflen );
1422
1423 if( p == NULL )
1424 return( 1 );
1425
1426 memcpy( p, buf, buflen );
1427
1428 buflen = 0;
1429 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001430#else
1431 p = (unsigned char *) malloc( len = buflen );
1432
1433 if( p == NULL )
1434 return( 1 );
1435
1436 memcpy( p, buf, buflen );
1437
1438 buflen = 0;
1439#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001440
1441 crl->raw.p = p;
1442 crl->raw.len = len;
1443 end = p + len;
1444
1445 /*
1446 * CertificateList ::= SEQUENCE {
1447 * tbsCertList TBSCertList,
1448 * signatureAlgorithm AlgorithmIdentifier,
1449 * signatureValue BIT STRING }
1450 */
1451 if( ( ret = asn1_get_tag( &p, end, &len,
1452 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1453 {
1454 x509_crl_free( crl );
1455 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1456 }
1457
Paul Bakker23986e52011-04-24 08:57:21 +00001458 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001459 {
1460 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001461 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001462 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1463 }
1464
1465 /*
1466 * TBSCertList ::= SEQUENCE {
1467 */
1468 crl->tbs.p = p;
1469
1470 if( ( ret = asn1_get_tag( &p, end, &len,
1471 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1472 {
1473 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001474 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001475 }
1476
1477 end = p + len;
1478 crl->tbs.len = end - crl->tbs.p;
1479
1480 /*
1481 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1482 * -- if present, MUST be v2
1483 *
1484 * signature AlgorithmIdentifier
1485 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001486 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001487 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1488 {
1489 x509_crl_free( crl );
1490 return( ret );
1491 }
1492
1493 crl->version++;
1494
1495 if( crl->version > 2 )
1496 {
1497 x509_crl_free( crl );
1498 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1499 }
1500
Paul Bakker27d66162010-03-17 06:56:01 +00001501 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001502 {
1503 x509_crl_free( crl );
1504 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1505 }
1506
1507 /*
1508 * issuer Name
1509 */
1510 crl->issuer_raw.p = p;
1511
1512 if( ( ret = asn1_get_tag( &p, end, &len,
1513 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1514 {
1515 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001516 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001517 }
1518
1519 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1520 {
1521 x509_crl_free( crl );
1522 return( ret );
1523 }
1524
1525 crl->issuer_raw.len = p - crl->issuer_raw.p;
1526
1527 /*
1528 * thisUpdate Time
1529 * nextUpdate Time OPTIONAL
1530 */
Paul Bakker91200182010-02-18 21:26:15 +00001531 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001532 {
1533 x509_crl_free( crl );
1534 return( ret );
1535 }
1536
Paul Bakker91200182010-02-18 21:26:15 +00001537 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001538 {
Paul Bakker9d781402011-05-09 16:17:09 +00001539 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001540 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001541 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001542 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001543 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001544 x509_crl_free( crl );
1545 return( ret );
1546 }
1547 }
1548
1549 /*
1550 * revokedCertificates SEQUENCE OF SEQUENCE {
1551 * userCertificate CertificateSerialNumber,
1552 * revocationDate Time,
1553 * crlEntryExtensions Extensions OPTIONAL
1554 * -- if present, MUST be v2
1555 * } OPTIONAL
1556 */
1557 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1558 {
1559 x509_crl_free( crl );
1560 return( ret );
1561 }
1562
1563 /*
1564 * crlExtensions EXPLICIT Extensions OPTIONAL
1565 * -- if present, MUST be v2
1566 */
1567 if( crl->version == 2 )
1568 {
1569 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1570
1571 if( ret != 0 )
1572 {
1573 x509_crl_free( crl );
1574 return( ret );
1575 }
1576 }
1577
1578 if( p != end )
1579 {
1580 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001581 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001582 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1583 }
1584
1585 end = crl->raw.p + crl->raw.len;
1586
1587 /*
1588 * signatureAlgorithm AlgorithmIdentifier,
1589 * signatureValue BIT STRING
1590 */
1591 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1592 {
1593 x509_crl_free( crl );
1594 return( ret );
1595 }
1596
1597 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1598 {
1599 x509_crl_free( crl );
1600 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1601 }
1602
1603 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1604 {
1605 x509_crl_free( crl );
1606 return( ret );
1607 }
1608
1609 if( p != end )
1610 {
1611 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001612 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001613 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1614 }
1615
1616 if( buflen > 0 )
1617 {
1618 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1619
Paul Bakker7d06ad22009-05-02 15:53:56 +00001620 if( crl->next == NULL )
1621 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001622 x509_crl_free( crl );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001623 return( 1 );
1624 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001625
Paul Bakker7d06ad22009-05-02 15:53:56 +00001626 crl = crl->next;
1627 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001628
1629 return( x509parse_crl( crl, buf, buflen ) );
1630 }
1631
1632 return( 0 );
1633}
1634
Paul Bakker335db3f2011-04-25 15:28:35 +00001635#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001636/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001637 * Load all data from a file into a given buffer.
1638 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001639int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001640{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001641 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001642
Paul Bakkerd98030e2009-05-02 15:13:40 +00001643 if( ( f = fopen( path, "rb" ) ) == NULL )
1644 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001645
Paul Bakkerd98030e2009-05-02 15:13:40 +00001646 fseek( f, 0, SEEK_END );
1647 *n = (size_t) ftell( f );
1648 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001649
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
1651 return( 1 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001652
Paul Bakkerd98030e2009-05-02 15:13:40 +00001653 if( fread( *buf, 1, *n, f ) != *n )
1654 {
1655 fclose( f );
1656 free( *buf );
1657 return( 1 );
1658 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001659
Paul Bakkerd98030e2009-05-02 15:13:40 +00001660 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001661
Paul Bakkerd98030e2009-05-02 15:13:40 +00001662 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001663
Paul Bakkerd98030e2009-05-02 15:13:40 +00001664 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001665}
1666
1667/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 * Load one or more certificates and add them to the chained list
1669 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001670int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
1672 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 size_t n;
1674 unsigned char *buf;
1675
Paul Bakker2b245eb2009-04-19 18:44:26 +00001676 if ( load_file( path, &buf, &n ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 return( 1 );
1678
Paul Bakker27fdf462011-06-09 13:55:13 +00001679 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
1681 memset( buf, 0, n + 1 );
1682 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684 return( ret );
1685}
1686
Paul Bakkerd98030e2009-05-02 15:13:40 +00001687/*
1688 * Load one or more CRLs and add them to the chained list
1689 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001690int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001691{
1692 int ret;
1693 size_t n;
1694 unsigned char *buf;
1695
1696 if ( load_file( path, &buf, &n ) )
1697 return( 1 );
1698
Paul Bakker27fdf462011-06-09 13:55:13 +00001699 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001700
1701 memset( buf, 0, n + 1 );
1702 free( buf );
1703
1704 return( ret );
1705}
1706
Paul Bakker5121ce52009-01-03 21:22:43 +00001707/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001708 * Load and parse a private RSA key
1709 */
1710int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1711{
1712 int ret;
1713 size_t n;
1714 unsigned char *buf;
1715
1716 if ( load_file( path, &buf, &n ) )
1717 return( 1 );
1718
1719 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001720 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001721 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001722 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001723 (unsigned char *) pwd, strlen( pwd ) );
1724
1725 memset( buf, 0, n + 1 );
1726 free( buf );
1727
1728 return( ret );
1729}
1730
1731/*
1732 * Load and parse a public RSA key
1733 */
1734int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1735{
1736 int ret;
1737 size_t n;
1738 unsigned char *buf;
1739
1740 if ( load_file( path, &buf, &n ) )
1741 return( 1 );
1742
Paul Bakker27fdf462011-06-09 13:55:13 +00001743 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001744
1745 memset( buf, 0, n + 1 );
1746 free( buf );
1747
1748 return( ret );
1749}
1750#endif /* POLARSSL_FS_IO */
1751
1752/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 * Parse a private RSA key
1754 */
Paul Bakker23986e52011-04-24 08:57:21 +00001755int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1756 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001757{
Paul Bakker23986e52011-04-24 08:57:21 +00001758 int ret;
1759 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001761 unsigned char *p_alt;
1762 x509_buf pk_alg_oid;
1763
Paul Bakker96743fc2011-02-12 14:30:57 +00001764#if defined(POLARSSL_PEM_C)
1765 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001766
Paul Bakker96743fc2011-02-12 14:30:57 +00001767 pem_init( &pem );
1768 ret = pem_read_buffer( &pem,
1769 "-----BEGIN RSA PRIVATE KEY-----",
1770 "-----END RSA PRIVATE KEY-----",
1771 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
Paul Bakkered56b222011-07-13 11:26:43 +00001773 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1774 {
1775 ret = pem_read_buffer( &pem,
1776 "-----BEGIN PRIVATE KEY-----",
1777 "-----END PRIVATE KEY-----",
1778 key, pwd, pwdlen, &len );
1779 }
1780
Paul Bakker96743fc2011-02-12 14:30:57 +00001781 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001783 /*
1784 * Was PEM encoded
1785 */
1786 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001788 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001789 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001790 pem_free( &pem );
1791 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001792 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001793
Paul Bakker96743fc2011-02-12 14:30:57 +00001794 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1795#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001796 ((void) pwd);
1797 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001798 p = (unsigned char *) key;
1799#endif
1800 end = p + keylen;
1801
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001803 * Note: Depending on the type of private key file one can expect either a
1804 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1805 *
1806 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001807 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001808 * algorithm AlgorithmIdentifier,
1809 * PrivateKey BIT STRING
1810 * }
1811 *
1812 * AlgorithmIdentifier ::= SEQUENCE {
1813 * algorithm OBJECT IDENTIFIER,
1814 * parameters ANY DEFINED BY algorithm OPTIONAL
1815 * }
1816 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 * RSAPrivateKey ::= SEQUENCE {
1818 * version Version,
1819 * modulus INTEGER, -- n
1820 * publicExponent INTEGER, -- e
1821 * privateExponent INTEGER, -- d
1822 * prime1 INTEGER, -- p
1823 * prime2 INTEGER, -- q
1824 * exponent1 INTEGER, -- d mod (p-1)
1825 * exponent2 INTEGER, -- d mod (q-1)
1826 * coefficient INTEGER, -- (inverse of q) mod p
1827 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1828 * }
1829 */
1830 if( ( ret = asn1_get_tag( &p, end, &len,
1831 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1832 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001833#if defined(POLARSSL_PEM_C)
1834 pem_free( &pem );
1835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001837 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 }
1839
1840 end = p + len;
1841
1842 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1843 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001844#if defined(POLARSSL_PEM_C)
1845 pem_free( &pem );
1846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001848 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
1850
1851 if( rsa->ver != 0 )
1852 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001853#if defined(POLARSSL_PEM_C)
1854 pem_free( &pem );
1855#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001857 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
1859
Paul Bakkered56b222011-07-13 11:26:43 +00001860 p_alt = p;
1861
1862 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1863 {
1864 // Assume that we have the PKCS#1 format if wrong
1865 // tag was encountered
1866 //
1867 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1868 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1869 {
1870#if defined(POLARSSL_PEM_C)
1871 pem_free( &pem );
1872#endif
1873 rsa_free( rsa );
1874 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1875 }
1876 }
1877 else
1878 {
1879 int can_handle;
1880
1881 /*
1882 * only RSA keys handled at this time
1883 */
1884 can_handle = 0;
1885
1886 if( pk_alg_oid.len == 9 &&
1887 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1888 can_handle = 1;
1889
1890 if( pk_alg_oid.len == 9 &&
1891 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1892 {
1893 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1894 can_handle = 1;
1895
1896 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1897 can_handle = 1;
1898 }
1899
1900 if( pk_alg_oid.len == 5 &&
1901 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1902 can_handle = 1;
1903
1904 if( can_handle == 0 )
1905 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1906
1907 /*
1908 * Parse the PKCS#8 format
1909 */
1910
1911 p = p_alt;
1912 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1913 {
1914#if defined(POLARSSL_PEM_C)
1915 pem_free( &pem );
1916#endif
1917 rsa_free( rsa );
1918 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1919 }
1920
1921 if( ( end - p ) < 1 )
1922 {
1923#if defined(POLARSSL_PEM_C)
1924 pem_free( &pem );
1925#endif
1926 rsa_free( rsa );
1927 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1928 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1929 }
1930
1931 end = p + len;
1932
1933 if( ( ret = asn1_get_tag( &p, end, &len,
1934 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1935 {
1936#if defined(POLARSSL_PEM_C)
1937 pem_free( &pem );
1938#endif
1939 rsa_free( rsa );
1940 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1941 }
1942
1943 end = p + len;
1944
1945 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1946 {
1947#if defined(POLARSSL_PEM_C)
1948 pem_free( &pem );
1949#endif
1950 rsa_free( rsa );
1951 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1952 }
1953
1954 if( rsa->ver != 0 )
1955 {
1956#if defined(POLARSSL_PEM_C)
1957 pem_free( &pem );
1958#endif
1959 rsa_free( rsa );
1960 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
1961 }
1962 }
1963
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
1965 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
1966 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
1967 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
1968 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
1969 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
1970 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
1971 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
1972 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001973#if defined(POLARSSL_PEM_C)
1974 pem_free( &pem );
1975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001977 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978 }
1979
1980 rsa->len = mpi_size( &rsa->N );
1981
1982 if( p != end )
1983 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001984#if defined(POLARSSL_PEM_C)
1985 pem_free( &pem );
1986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001988 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001989 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 }
1991
1992 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
1993 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001994#if defined(POLARSSL_PEM_C)
1995 pem_free( &pem );
1996#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 rsa_free( rsa );
1998 return( ret );
1999 }
2000
Paul Bakker96743fc2011-02-12 14:30:57 +00002001#if defined(POLARSSL_PEM_C)
2002 pem_free( &pem );
2003#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002004
2005 return( 0 );
2006}
2007
2008/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002009 * Parse a public RSA key
2010 */
Paul Bakker23986e52011-04-24 08:57:21 +00002011int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002012{
Paul Bakker23986e52011-04-24 08:57:21 +00002013 int ret;
2014 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002015 unsigned char *p, *end;
2016 x509_buf alg_oid;
2017#if defined(POLARSSL_PEM_C)
2018 pem_context pem;
2019
2020 pem_init( &pem );
2021 ret = pem_read_buffer( &pem,
2022 "-----BEGIN PUBLIC KEY-----",
2023 "-----END PUBLIC KEY-----",
2024 key, NULL, 0, &len );
2025
2026 if( ret == 0 )
2027 {
2028 /*
2029 * Was PEM encoded
2030 */
2031 keylen = pem.buflen;
2032 }
2033 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2034 {
2035 pem_free( &pem );
2036 return( ret );
2037 }
2038
2039 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2040#else
2041 p = (unsigned char *) key;
2042#endif
2043 end = p + keylen;
2044
2045 /*
2046 * PublicKeyInfo ::= SEQUENCE {
2047 * algorithm AlgorithmIdentifier,
2048 * PublicKey BIT STRING
2049 * }
2050 *
2051 * AlgorithmIdentifier ::= SEQUENCE {
2052 * algorithm OBJECT IDENTIFIER,
2053 * parameters ANY DEFINED BY algorithm OPTIONAL
2054 * }
2055 *
2056 * RSAPublicKey ::= SEQUENCE {
2057 * modulus INTEGER, -- n
2058 * publicExponent INTEGER -- e
2059 * }
2060 */
2061
2062 if( ( ret = asn1_get_tag( &p, end, &len,
2063 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2064 {
2065#if defined(POLARSSL_PEM_C)
2066 pem_free( &pem );
2067#endif
2068 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002069 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002070 }
2071
2072 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2073 {
2074#if defined(POLARSSL_PEM_C)
2075 pem_free( &pem );
2076#endif
2077 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002078 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002079 }
2080
2081 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2082 {
2083#if defined(POLARSSL_PEM_C)
2084 pem_free( &pem );
2085#endif
2086 rsa_free( rsa );
2087 return( ret );
2088 }
2089
2090 rsa->len = mpi_size( &rsa->N );
2091
2092#if defined(POLARSSL_PEM_C)
2093 pem_free( &pem );
2094#endif
2095
2096 return( 0 );
2097}
2098
Paul Bakkereaa89f82011-04-04 21:36:15 +00002099#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002100/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002101 * Parse DHM parameters
2102 */
Paul Bakker23986e52011-04-24 08:57:21 +00002103int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002104{
Paul Bakker23986e52011-04-24 08:57:21 +00002105 int ret;
2106 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002107 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002108#if defined(POLARSSL_PEM_C)
2109 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002110
Paul Bakker96743fc2011-02-12 14:30:57 +00002111 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002112
Paul Bakker96743fc2011-02-12 14:30:57 +00002113 ret = pem_read_buffer( &pem,
2114 "-----BEGIN DH PARAMETERS-----",
2115 "-----END DH PARAMETERS-----",
2116 dhmin, NULL, 0, &dhminlen );
2117
2118 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002119 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002120 /*
2121 * Was PEM encoded
2122 */
2123 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002124 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002125 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002126 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002127 pem_free( &pem );
2128 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002129 }
2130
Paul Bakker96743fc2011-02-12 14:30:57 +00002131 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2132#else
2133 p = (unsigned char *) dhmin;
2134#endif
2135 end = p + dhminlen;
2136
Paul Bakker1b57b062011-01-06 15:48:19 +00002137 memset( dhm, 0, sizeof( dhm_context ) );
2138
Paul Bakker1b57b062011-01-06 15:48:19 +00002139 /*
2140 * DHParams ::= SEQUENCE {
2141 * prime INTEGER, -- P
2142 * generator INTEGER, -- g
2143 * }
2144 */
2145 if( ( ret = asn1_get_tag( &p, end, &len,
2146 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2147 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002148#if defined(POLARSSL_PEM_C)
2149 pem_free( &pem );
2150#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002151 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002152 }
2153
2154 end = p + len;
2155
2156 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2157 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2158 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002159#if defined(POLARSSL_PEM_C)
2160 pem_free( &pem );
2161#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002162 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002163 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002164 }
2165
2166 if( p != end )
2167 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002168#if defined(POLARSSL_PEM_C)
2169 pem_free( &pem );
2170#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002171 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002172 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002173 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2174 }
2175
Paul Bakker96743fc2011-02-12 14:30:57 +00002176#if defined(POLARSSL_PEM_C)
2177 pem_free( &pem );
2178#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002179
2180 return( 0 );
2181}
2182
Paul Bakker335db3f2011-04-25 15:28:35 +00002183#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002184/*
2185 * Load and parse a private RSA key
2186 */
2187int x509parse_dhmfile( dhm_context *dhm, const char *path )
2188{
2189 int ret;
2190 size_t n;
2191 unsigned char *buf;
2192
2193 if ( load_file( path, &buf, &n ) )
2194 return( 1 );
2195
Paul Bakker27fdf462011-06-09 13:55:13 +00002196 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002197
2198 memset( buf, 0, n + 1 );
2199 free( buf );
2200
2201 return( ret );
2202}
Paul Bakker335db3f2011-04-25 15:28:35 +00002203#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002204#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002205
Paul Bakker5121ce52009-01-03 21:22:43 +00002206#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002207#include <stdarg.h>
2208
2209#if !defined vsnprintf
2210#define vsnprintf _vsnprintf
2211#endif // vsnprintf
2212
2213/*
2214 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2215 * Result value is not size of buffer needed, but -1 if no fit is possible.
2216 *
2217 * This fuction tries to 'fix' this by at least suggesting enlarging the
2218 * size by 20.
2219 */
2220int compat_snprintf(char *str, size_t size, const char *format, ...)
2221{
2222 va_list ap;
2223 int res = -1;
2224
2225 va_start( ap, format );
2226
2227 res = vsnprintf( str, size, format, ap );
2228
2229 va_end( ap );
2230
2231 // No quick fix possible
2232 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002233 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002234
2235 return res;
2236}
2237
2238#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002239#endif
2240
Paul Bakkerd98030e2009-05-02 15:13:40 +00002241#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2242
2243#define SAFE_SNPRINTF() \
2244{ \
2245 if( ret == -1 ) \
2246 return( -1 ); \
2247 \
Paul Bakker23986e52011-04-24 08:57:21 +00002248 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002249 p[n - 1] = '\0'; \
2250 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2251 } \
2252 \
Paul Bakker23986e52011-04-24 08:57:21 +00002253 n -= (unsigned int) ret; \
2254 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002255}
2256
Paul Bakker5121ce52009-01-03 21:22:43 +00002257/*
2258 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002259 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002261int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262{
Paul Bakker23986e52011-04-24 08:57:21 +00002263 int ret;
2264 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002266 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 char s[128], *p;
2268
2269 memset( s, 0, sizeof( s ) );
2270
2271 name = dn;
2272 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002273 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
2275 while( name != NULL )
2276 {
Paul Bakker74111d32011-01-15 16:57:55 +00002277 if( name != dn )
2278 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002279 ret = snprintf( p, n, ", " );
2280 SAFE_SNPRINTF();
2281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2284 {
2285 switch( name->oid.p[2] )
2286 {
2287 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002288 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002289
2290 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002291 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
2293 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002294 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
2296 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002297 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002298
2299 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002300 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
2302 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002303 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
2305 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002306 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 name->oid.p[2] );
2308 break;
2309 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002310 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002311 }
2312 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2313 {
2314 switch( name->oid.p[8] )
2315 {
2316 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002317 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
2319 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002320 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 name->oid.p[8] );
2322 break;
2323 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002324 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 }
2326 else
Paul Bakker74111d32011-01-15 16:57:55 +00002327 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002328 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002329 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002330 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
2332 for( i = 0; i < name->val.len; i++ )
2333 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002334 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 break;
2336
2337 c = name->val.p[i];
2338 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2339 s[i] = '?';
2340 else s[i] = c;
2341 }
2342 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002343 ret = snprintf( p, n, "%s", s );
2344 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 name = name->next;
2346 }
2347
Paul Bakker23986e52011-04-24 08:57:21 +00002348 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002349}
2350
2351/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002352 * Store the serial in printable form into buf; no more
2353 * than size characters will be written
2354 */
2355int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2356{
Paul Bakker23986e52011-04-24 08:57:21 +00002357 int ret;
2358 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002359 char *p;
2360
2361 p = buf;
2362 n = size;
2363
2364 nr = ( serial->len <= 32 )
2365 ? serial->len : 32;
2366
2367 for( i = 0; i < nr; i++ )
2368 {
2369 ret = snprintf( p, n, "%02X%s",
2370 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2371 SAFE_SNPRINTF();
2372 }
2373
Paul Bakker23986e52011-04-24 08:57:21 +00002374 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002375}
2376
2377/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002380int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2381 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002382{
Paul Bakker23986e52011-04-24 08:57:21 +00002383 int ret;
2384 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002388 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002389
Paul Bakkerd98030e2009-05-02 15:13:40 +00002390 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002392 SAFE_SNPRINTF();
2393 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002395 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002396
Paul Bakkerdd476992011-01-16 21:34:59 +00002397 ret = x509parse_serial_gets( p, n, &crt->serial);
2398 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Paul Bakkerd98030e2009-05-02 15:13:40 +00002400 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2401 SAFE_SNPRINTF();
2402 ret = x509parse_dn_gets( p, n, &crt->issuer );
2403 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Paul Bakkerd98030e2009-05-02 15:13:40 +00002405 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2406 SAFE_SNPRINTF();
2407 ret = x509parse_dn_gets( p, n, &crt->subject );
2408 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Paul Bakkerd98030e2009-05-02 15:13:40 +00002410 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2412 crt->valid_from.year, crt->valid_from.mon,
2413 crt->valid_from.day, crt->valid_from.hour,
2414 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002415 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Paul Bakkerd98030e2009-05-02 15:13:40 +00002417 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2419 crt->valid_to.year, crt->valid_to.mon,
2420 crt->valid_to.day, crt->valid_to.hour,
2421 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002422 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002423
Paul Bakkerd98030e2009-05-02 15:13:40 +00002424 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2425 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Paul Bakker27d66162010-03-17 06:56:01 +00002427 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002429 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2430 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2431 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2432 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2433 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2434 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2435 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2436 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2437 default: ret = snprintf( p, n, "???" ); break;
2438 }
2439 SAFE_SNPRINTF();
2440
2441 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002442 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002443 SAFE_SNPRINTF();
2444
Paul Bakker23986e52011-04-24 08:57:21 +00002445 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002446}
2447
Paul Bakker74111d32011-01-15 16:57:55 +00002448/* Compare a given OID string with an OID x509_buf * */
2449#define OID_CMP(oid_str, oid_buf) \
2450 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2451 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2452
2453/*
2454 * Return an informational string describing the given OID
2455 */
2456const char *x509_oid_get_description( x509_buf *oid )
2457{
2458 if ( oid == NULL )
2459 return ( NULL );
2460
2461 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2462 return( STRING_SERVER_AUTH );
2463
2464 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2465 return( STRING_CLIENT_AUTH );
2466
2467 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2468 return( STRING_CODE_SIGNING );
2469
2470 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2471 return( STRING_EMAIL_PROTECTION );
2472
2473 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2474 return( STRING_TIME_STAMPING );
2475
2476 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2477 return( STRING_OCSP_SIGNING );
2478
2479 return( NULL );
2480}
2481
2482/* Return the x.y.z.... style numeric string for the given OID */
2483int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2484{
Paul Bakker23986e52011-04-24 08:57:21 +00002485 int ret;
2486 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002487 unsigned int value;
2488 char *p;
2489
2490 p = buf;
2491 n = size;
2492
2493 /* First byte contains first two dots */
2494 if( oid->len > 0 )
2495 {
2496 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2497 SAFE_SNPRINTF();
2498 }
2499
2500 /* TODO: value can overflow in value. */
2501 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002502 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002503 {
2504 value <<= 7;
2505 value += oid->p[i] & 0x7F;
2506
2507 if( !( oid->p[i] & 0x80 ) )
2508 {
2509 /* Last byte */
2510 ret = snprintf( p, n, ".%d", value );
2511 SAFE_SNPRINTF();
2512 value = 0;
2513 }
2514 }
2515
Paul Bakker23986e52011-04-24 08:57:21 +00002516 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002517}
2518
Paul Bakkerd98030e2009-05-02 15:13:40 +00002519/*
2520 * Return an informational string about the CRL.
2521 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002522int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2523 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002524{
Paul Bakker23986e52011-04-24 08:57:21 +00002525 int ret;
2526 size_t i, n, nr;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002528 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002529
2530 p = buf;
2531 n = size;
2532
2533 ret = snprintf( p, n, "%sCRL version : %d",
2534 prefix, crl->version );
2535 SAFE_SNPRINTF();
2536
2537 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2538 SAFE_SNPRINTF();
2539 ret = x509parse_dn_gets( p, n, &crl->issuer );
2540 SAFE_SNPRINTF();
2541
2542 ret = snprintf( p, n, "\n%sthis update : " \
2543 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2544 crl->this_update.year, crl->this_update.mon,
2545 crl->this_update.day, crl->this_update.hour,
2546 crl->this_update.min, crl->this_update.sec );
2547 SAFE_SNPRINTF();
2548
2549 ret = snprintf( p, n, "\n%snext update : " \
2550 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2551 crl->next_update.year, crl->next_update.mon,
2552 crl->next_update.day, crl->next_update.hour,
2553 crl->next_update.min, crl->next_update.sec );
2554 SAFE_SNPRINTF();
2555
2556 entry = &crl->entry;
2557
2558 ret = snprintf( p, n, "\n%sRevoked certificates:",
2559 prefix );
2560 SAFE_SNPRINTF();
2561
Paul Bakker9be19372009-07-27 20:21:53 +00002562 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002563 {
2564 ret = snprintf( p, n, "\n%sserial number: ",
2565 prefix );
2566 SAFE_SNPRINTF();
2567
2568 nr = ( entry->serial.len <= 32 )
2569 ? entry->serial.len : 32;
2570
Paul Bakker74111d32011-01-15 16:57:55 +00002571 for( i = 0; i < nr; i++ )
2572 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002573 ret = snprintf( p, n, "%02X%s",
2574 entry->serial.p[i], ( i < nr - 1 ) ? ":" : "" );
2575 SAFE_SNPRINTF();
2576 }
2577
2578 ret = snprintf( p, n, " revocation date: " \
2579 "%04d-%02d-%02d %02d:%02d:%02d",
2580 entry->revocation_date.year, entry->revocation_date.mon,
2581 entry->revocation_date.day, entry->revocation_date.hour,
2582 entry->revocation_date.min, entry->revocation_date.sec );
2583 SAFE_SNPRINTF();
2584
2585 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
Paul Bakkerd98030e2009-05-02 15:13:40 +00002588 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2589 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
Paul Bakker27d66162010-03-17 06:56:01 +00002591 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002592 {
2593 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2594 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2595 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2596 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2597 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2598 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2599 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2600 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2601 default: ret = snprintf( p, n, "???" ); break;
2602 }
2603 SAFE_SNPRINTF();
2604
Paul Bakker1e27bb22009-07-19 20:25:25 +00002605 ret = snprintf( p, n, "\n" );
2606 SAFE_SNPRINTF();
2607
Paul Bakker23986e52011-04-24 08:57:21 +00002608 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609}
2610
2611/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002612 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002614int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002615{
Paul Bakkercce9d772011-11-18 14:26:47 +00002616 int year, mon, day;
2617 int hour, min, sec;
2618
2619#if defined(_WIN32)
2620 SYSTEMTIME st;
2621
2622 GetLocalTime(&st);
2623
2624 year = st.wYear;
2625 mon = st.wMonth;
2626 day = st.wDay;
2627 hour = st.wHour;
2628 min = st.wMinute;
2629 sec = st.wSecond;
2630#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 struct tm *lt;
2632 time_t tt;
2633
2634 tt = time( NULL );
2635 lt = localtime( &tt );
2636
Paul Bakkercce9d772011-11-18 14:26:47 +00002637 year = lt->tm_year + 1900;
2638 mon = lt->tm_mon + 1;
2639 day = lt->tm_mday;
2640 hour = lt->tm_hour;
2641 min = lt->tm_min;
2642 sec = lt->tm_sec;
2643#endif
2644
2645 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002646 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Paul Bakkercce9d772011-11-18 14:26:47 +00002648 if( year == to->year &&
2649 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002650 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Paul Bakkercce9d772011-11-18 14:26:47 +00002652 if( year == to->year &&
2653 mon == to->mon &&
2654 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002655 return( 1 );
2656
Paul Bakkercce9d772011-11-18 14:26:47 +00002657 if( year == to->year &&
2658 mon == to->mon &&
2659 day == to->day &&
2660 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002661 return( 1 );
2662
Paul Bakkercce9d772011-11-18 14:26:47 +00002663 if( year == to->year &&
2664 mon == to->mon &&
2665 day == to->day &&
2666 hour == to->hour &&
2667 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002668 return( 1 );
2669
Paul Bakkercce9d772011-11-18 14:26:47 +00002670 if( year == to->year &&
2671 mon == to->mon &&
2672 day == to->day &&
2673 hour == to->hour &&
2674 min == to->min &&
2675 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002676 return( 1 );
2677
Paul Bakker40ea7de2009-05-03 10:18:48 +00002678 return( 0 );
2679}
2680
2681/*
2682 * Return 1 if the certificate is revoked, or 0 otherwise.
2683 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002684int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002685{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002686 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002687
2688 while( cur != NULL && cur->serial.len != 0 )
2689 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002690 if( crt->serial.len == cur->serial.len &&
2691 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002692 {
2693 if( x509parse_time_expired( &cur->revocation_date ) )
2694 return( 1 );
2695 }
2696
2697 cur = cur->next;
2698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700 return( 0 );
2701}
2702
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002703/*
2704 * Wrapper for x509 hashes.
2705 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002706 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002707 */
Paul Bakker23986e52011-04-24 08:57:21 +00002708static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 unsigned char *out )
2710{
2711 switch( alg )
2712 {
Paul Bakker40e46942009-01-03 21:51:57 +00002713#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002714 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002716#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002717 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002718#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002719#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002720 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002721#endif
2722#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002723 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002724#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002725#if defined(POLARSSL_SHA2_C)
2726 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2727 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2728#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002729#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002730 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2731 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002734 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 break;
2736 }
2737}
2738
2739/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002740 * Check that the given certificate is valid accoring to the CRL.
2741 */
2742static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2743 x509_crl *crl_list)
2744{
2745 int flags = 0;
2746 int hash_id;
2747 unsigned char hash[64];
2748
2749 /*
2750 * TODO: What happens if no CRL is present?
2751 * Suggestion: Revocation state should be unknown if no CRL is present.
2752 * For backwards compatibility this is not yet implemented.
2753 */
2754
2755 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2756 {
2757 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2758 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2759 crl_list->issuer_raw.len ) != 0 )
2760 {
2761 crl_list = crl_list->next;
2762 continue;
2763 }
2764
2765 /*
2766 * Check if CRL is correctly signed by the trusted CA
2767 */
2768 hash_id = crl_list->sig_alg;
2769
2770 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2771
2772 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2773 0, hash, crl_list->sig.p ) == 0 )
2774 {
2775 /*
2776 * CRL is not trusted
2777 */
2778 flags |= BADCRL_NOT_TRUSTED;
2779 break;
2780 }
2781
2782 /*
2783 * Check for validity of CRL (Do not drop out)
2784 */
2785 if( x509parse_time_expired( &crl_list->next_update ) )
2786 flags |= BADCRL_EXPIRED;
2787
2788 /*
2789 * Check if certificate is revoked
2790 */
2791 if( x509parse_revoked(crt, crl_list) )
2792 {
2793 flags |= BADCERT_REVOKED;
2794 break;
2795 }
2796
2797 crl_list = crl_list->next;
2798 }
2799 return flags;
2800}
2801
2802/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 * Verify the certificate validity
2804 */
2805int x509parse_verify( x509_cert *crt,
2806 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002807 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002808 const char *cn, int *flags,
2809 int (*f_vrfy)(void *, x509_cert *, int, int),
2810 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811{
Paul Bakker23986e52011-04-24 08:57:21 +00002812 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 int hash_id;
2814 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002815 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002817 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Paul Bakker40ea7de2009-05-03 10:18:48 +00002819 *flags = 0;
2820
2821 if( x509parse_time_expired( &crt->valid_to ) )
2822 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823
2824 if( cn != NULL )
2825 {
2826 name = &crt->subject;
2827 cn_len = strlen( cn );
2828
2829 while( name != NULL )
2830 {
2831 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2832 memcmp( name->val.p, cn, cn_len ) == 0 &&
2833 name->val.len == cn_len )
2834 break;
2835
2836 name = name->next;
2837 }
2838
2839 if( name == NULL )
2840 *flags |= BADCERT_CN_MISMATCH;
2841 }
2842
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 /*
2844 * Iterate upwards in the given cert chain,
2845 * ignoring any upper cert with CA != TRUE.
2846 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002847 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
2849 pathlen = 1;
2850
Paul Bakker76fd75a2011-01-16 21:12:10 +00002851 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002853 if( parent->ca_istrue == 0 ||
2854 crt->issuer_raw.len != parent->subject_raw.len ||
2855 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 crt->issuer_raw.len ) != 0 )
2857 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002858 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002859 continue;
2860 }
2861
Paul Bakker27d66162010-03-17 06:56:01 +00002862 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
2864 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2865
Paul Bakker76fd75a2011-01-16 21:12:10 +00002866 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2867 crt->sig.p ) != 0 )
2868 *flags |= BADCERT_NOT_TRUSTED;
2869
2870 /* Check trusted CA's CRL for the given crt */
2871 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002872
2873 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002874 if( NULL != f_vrfy )
2875 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002876 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002877 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002878 else
2879 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002880 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002881 else if( *flags != 0 )
2882 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
2884 pathlen++;
2885
Paul Bakker76fd75a2011-01-16 21:12:10 +00002886 crt = parent;
2887 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 }
2889
2890 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002891 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002893 *flags |= BADCERT_NOT_TRUSTED;
2894
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002895 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002896 {
2897 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2898 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2899 crt->issuer_raw.len ) != 0 )
2900 {
2901 trust_ca = trust_ca->next;
2902 continue;
2903 }
2904
2905 if( trust_ca->max_pathlen > 0 &&
2906 trust_ca->max_pathlen < pathlen )
2907 break;
2908
Paul Bakker27d66162010-03-17 06:56:01 +00002909 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
2911 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2912
2913 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2914 0, hash, crt->sig.p ) == 0 )
2915 {
2916 /*
2917 * cert. is signed by a trusted CA
2918 */
2919 *flags &= ~BADCERT_NOT_TRUSTED;
2920 break;
2921 }
2922
2923 trust_ca = trust_ca->next;
2924 }
2925
Paul Bakker76fd75a2011-01-16 21:12:10 +00002926 /* Check trusted CA's CRL for the given crt */
2927 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002928
2929 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00002930 if( NULL != f_vrfy )
2931 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002932 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002933 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002934 else
2935 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002936 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002937 else if( *flags != 0 )
2938 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002939
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 return( 0 );
2941}
2942
2943/*
2944 * Unallocate all certificate data
2945 */
2946void x509_free( x509_cert *crt )
2947{
2948 x509_cert *cert_cur = crt;
2949 x509_cert *cert_prv;
2950 x509_name *name_cur;
2951 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00002952 x509_sequence *seq_cur;
2953 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
2955 if( crt == NULL )
2956 return;
2957
2958 do
2959 {
2960 rsa_free( &cert_cur->rsa );
2961
2962 name_cur = cert_cur->issuer.next;
2963 while( name_cur != NULL )
2964 {
2965 name_prv = name_cur;
2966 name_cur = name_cur->next;
2967 memset( name_prv, 0, sizeof( x509_name ) );
2968 free( name_prv );
2969 }
2970
2971 name_cur = cert_cur->subject.next;
2972 while( name_cur != NULL )
2973 {
2974 name_prv = name_cur;
2975 name_cur = name_cur->next;
2976 memset( name_prv, 0, sizeof( x509_name ) );
2977 free( name_prv );
2978 }
2979
Paul Bakker74111d32011-01-15 16:57:55 +00002980 seq_cur = cert_cur->ext_key_usage.next;
2981 while( seq_cur != NULL )
2982 {
2983 seq_prv = seq_cur;
2984 seq_cur = seq_cur->next;
2985 memset( seq_prv, 0, sizeof( x509_sequence ) );
2986 free( seq_prv );
2987 }
2988
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 if( cert_cur->raw.p != NULL )
2990 {
2991 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2992 free( cert_cur->raw.p );
2993 }
2994
2995 cert_cur = cert_cur->next;
2996 }
2997 while( cert_cur != NULL );
2998
2999 cert_cur = crt;
3000 do
3001 {
3002 cert_prv = cert_cur;
3003 cert_cur = cert_cur->next;
3004
3005 memset( cert_prv, 0, sizeof( x509_cert ) );
3006 if( cert_prv != crt )
3007 free( cert_prv );
3008 }
3009 while( cert_cur != NULL );
3010}
3011
Paul Bakkerd98030e2009-05-02 15:13:40 +00003012/*
3013 * Unallocate all CRL data
3014 */
3015void x509_crl_free( x509_crl *crl )
3016{
3017 x509_crl *crl_cur = crl;
3018 x509_crl *crl_prv;
3019 x509_name *name_cur;
3020 x509_name *name_prv;
3021 x509_crl_entry *entry_cur;
3022 x509_crl_entry *entry_prv;
3023
3024 if( crl == NULL )
3025 return;
3026
3027 do
3028 {
3029 name_cur = crl_cur->issuer.next;
3030 while( name_cur != NULL )
3031 {
3032 name_prv = name_cur;
3033 name_cur = name_cur->next;
3034 memset( name_prv, 0, sizeof( x509_name ) );
3035 free( name_prv );
3036 }
3037
3038 entry_cur = crl_cur->entry.next;
3039 while( entry_cur != NULL )
3040 {
3041 entry_prv = entry_cur;
3042 entry_cur = entry_cur->next;
3043 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3044 free( entry_prv );
3045 }
3046
3047 if( crl_cur->raw.p != NULL )
3048 {
3049 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3050 free( crl_cur->raw.p );
3051 }
3052
3053 crl_cur = crl_cur->next;
3054 }
3055 while( crl_cur != NULL );
3056
3057 crl_cur = crl;
3058 do
3059 {
3060 crl_prv = crl_cur;
3061 crl_cur = crl_cur->next;
3062
3063 memset( crl_prv, 0, sizeof( x509_crl ) );
3064 if( crl_prv != crl )
3065 free( crl_prv );
3066 }
3067 while( crl_cur != NULL );
3068}
3069
Paul Bakker40e46942009-01-03 21:51:57 +00003070#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
Paul Bakker40e46942009-01-03 21:51:57 +00003072#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
3074/*
3075 * Checkup routine
3076 */
3077int x509_self_test( int verbose )
3078{
Paul Bakker5690efc2011-05-26 13:16:06 +00003079#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003080 int ret;
3081 int flags;
3082 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 x509_cert cacert;
3084 x509_cert clicert;
3085 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003086#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003087 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
3090 if( verbose != 0 )
3091 printf( " X.509 certificate load: " );
3092
3093 memset( &clicert, 0, sizeof( x509_cert ) );
3094
3095 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
3096 strlen( test_cli_crt ) );
3097 if( ret != 0 )
3098 {
3099 if( verbose != 0 )
3100 printf( "failed\n" );
3101
3102 return( ret );
3103 }
3104
3105 memset( &cacert, 0, sizeof( x509_cert ) );
3106
3107 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
3108 strlen( test_ca_crt ) );
3109 if( ret != 0 )
3110 {
3111 if( verbose != 0 )
3112 printf( "failed\n" );
3113
3114 return( ret );
3115 }
3116
3117 if( verbose != 0 )
3118 printf( "passed\n X.509 private key load: " );
3119
3120 i = strlen( test_ca_key );
3121 j = strlen( test_ca_pwd );
3122
Paul Bakker66b78b22011-03-25 14:22:50 +00003123 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3124
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 if( ( ret = x509parse_key( &rsa,
3126 (unsigned char *) test_ca_key, i,
3127 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3128 {
3129 if( verbose != 0 )
3130 printf( "failed\n" );
3131
3132 return( ret );
3133 }
3134
3135 if( verbose != 0 )
3136 printf( "passed\n X.509 signature verify: ");
3137
Paul Bakker23986e52011-04-24 08:57:21 +00003138 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 if( ret != 0 )
3140 {
Paul Bakker23986e52011-04-24 08:57:21 +00003141 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003142 if( verbose != 0 )
3143 printf( "failed\n" );
3144
3145 return( ret );
3146 }
3147
Paul Bakker5690efc2011-05-26 13:16:06 +00003148#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003149 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003150 printf( "passed\n X.509 DHM parameter load: " );
3151
3152 i = strlen( test_dhm_params );
3153 j = strlen( test_ca_pwd );
3154
3155 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3156 {
3157 if( verbose != 0 )
3158 printf( "failed\n" );
3159
3160 return( ret );
3161 }
3162
3163 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
3167 x509_free( &cacert );
3168 x509_free( &clicert );
3169 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003170#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003171 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003172#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
3174 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003175#else
3176 ((void) verbose);
3177 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3178#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003179}
3180
3181#endif
3182
3183#endif