blob: c9aa738f53826e9d00a35567c1639b17aeb3f733 [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 Bakker4f229e52011-12-04 22:11:35 +000055#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000056#include <windows.h>
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker335db3f2011-04-25 15:28:35 +000061#if defined(POLARSSL_FS_IO)
62#include <stdio.h>
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 )
Paul Bakker732e1a82011-12-11 16:35:09 +0000287 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000288
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 )
Paul Bakker732e1a82011-12-11 16:35:09 +0000306 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
Paul Bakker07156682012-05-30 07:33:30 +0000308 memset( cur->next, 0, sizeof( x509_name ) );
309
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 return( x509_get_name( p, end2, cur->next ) );
311}
312
313/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * Time ::= CHOICE {
315 * utcTime UTCTime,
316 * generalTime GeneralizedTime }
317 */
Paul Bakker91200182010-02-18 21:26:15 +0000318static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000319 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320 x509_time *time )
321{
Paul Bakker23986e52011-04-24 08:57:21 +0000322 int ret;
323 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000324 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000325 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000326
Paul Bakker91200182010-02-18 21:26:15 +0000327 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000328 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
329 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330
Paul Bakker91200182010-02-18 21:26:15 +0000331 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332
Paul Bakker91200182010-02-18 21:26:15 +0000333 if ( tag == ASN1_UTC_TIME )
334 {
335 (*p)++;
336 ret = asn1_get_len( p, end, &len );
337
338 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000339 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000340
Paul Bakker91200182010-02-18 21:26:15 +0000341 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000342 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
343 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
346 &time->year, &time->mon, &time->day,
347 &time->hour, &time->min, &time->sec ) < 5 )
348 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349
Paul Bakker400ff6f2011-02-20 10:40:16 +0000350 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000351 time->year += 1900;
352
353 *p += len;
354
355 return( 0 );
356 }
357 else if ( tag == ASN1_GENERALIZED_TIME )
358 {
359 (*p)++;
360 ret = asn1_get_len( p, end, &len );
361
362 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000363 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000364
365 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000366 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
367 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000368
369 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
370 &time->year, &time->mon, &time->day,
371 &time->hour, &time->min, &time->sec ) < 5 )
372 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
373
374 *p += len;
375
376 return( 0 );
377 }
378 else
Paul Bakker9d781402011-05-09 16:17:09 +0000379 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000380}
381
382
383/*
384 * Validity ::= SEQUENCE {
385 * notBefore Time,
386 * notAfter Time }
387 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000388static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000389 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 x509_time *from,
391 x509_time *to )
392{
Paul Bakker23986e52011-04-24 08:57:21 +0000393 int ret;
394 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 if( ( ret = asn1_get_tag( p, end, &len,
397 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000398 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 end = *p + len;
401
Paul Bakker91200182010-02-18 21:26:15 +0000402 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000403 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Paul Bakker91200182010-02-18 21:26:15 +0000405 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000406 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000409 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000410 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 return( 0 );
413}
414
415/*
416 * SubjectPublicKeyInfo ::= SEQUENCE {
417 * algorithm AlgorithmIdentifier,
418 * subjectPublicKey BIT STRING }
419 */
420static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000421 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 x509_buf *pk_alg_oid,
423 mpi *N, mpi *E )
424{
Paul Bakker23986e52011-04-24 08:57:21 +0000425 int ret, can_handle;
426 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 unsigned char *end2;
428
429 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
430 return( ret );
431
432 /*
433 * only RSA public keys handled at this time
434 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000435 can_handle = 0;
436
437 if( pk_alg_oid->len == 9 &&
438 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
439 can_handle = 1;
440
441 if( pk_alg_oid->len == 9 &&
442 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
443 {
444 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
445 can_handle = 1;
446
447 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
448 can_handle = 1;
449 }
450
451 if( pk_alg_oid->len == 5 &&
452 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
453 can_handle = 1;
454
455 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000456 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000459 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000462 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000463 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 end2 = *p + len;
466
467 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000468 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 /*
471 * RSAPublicKey ::= SEQUENCE {
472 * modulus INTEGER, -- n
473 * publicExponent INTEGER -- e
474 * }
475 */
476 if( ( ret = asn1_get_tag( p, end2, &len,
477 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000478 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000481 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000482 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
485 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000486 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000489 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000490 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 return( 0 );
493}
494
495static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 x509_buf *sig )
498{
Paul Bakker23986e52011-04-24 08:57:21 +0000499 int ret;
500 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 sig->tag = **p;
503
504 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000505 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakker74111d32011-01-15 16:57:55 +0000507
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000509 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 sig->len = len;
512 sig->p = *p;
513
514 *p += len;
515
516 return( 0 );
517}
518
519/*
520 * X.509 v2/v3 unique identifier (not parsed)
521 */
522static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000523 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 x509_buf *uid, int n )
525{
526 int ret;
527
528 if( *p == end )
529 return( 0 );
530
531 uid->tag = **p;
532
533 if( ( ret = asn1_get_tag( p, end, &uid->len,
534 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
535 {
Paul Bakker40e46942009-01-03 21:51:57 +0000536 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 return( 0 );
538
539 return( ret );
540 }
541
542 uid->p = *p;
543 *p += uid->len;
544
545 return( 0 );
546}
547
548/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000549 * X.509 Extensions (No parsing of extensions, pointer should
550 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
552static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000554 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000555{
Paul Bakker23986e52011-04-24 08:57:21 +0000556 int ret;
557 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 if( *p == end )
560 return( 0 );
561
562 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000563
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000565 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 ext->p = *p;
569 end = *p + ext->len;
570
571 /*
572 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
573 *
574 * Extension ::= SEQUENCE {
575 * extnID OBJECT IDENTIFIER,
576 * critical BOOLEAN DEFAULT FALSE,
577 * extnValue OCTET STRING }
578 */
579 if( ( ret = asn1_get_tag( p, end, &len,
580 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000581 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000584 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000585 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Paul Bakkerd98030e2009-05-02 15:13:40 +0000587 return( 0 );
588}
589
590/*
591 * X.509 CRL v2 extensions (no extensions parsed yet.)
592 */
593static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000594 const unsigned char *end,
595 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000596{
Paul Bakker23986e52011-04-24 08:57:21 +0000597 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000598 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000600 /* Get explicit tag */
601 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000602 {
603 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
604 return( 0 );
605
606 return( ret );
607 }
608
609 while( *p < end )
610 {
611 if( ( ret = asn1_get_tag( p, end, &len,
612 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000613 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614
615 *p += len;
616 }
617
618 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000619 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000620 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
621
622 return( 0 );
623}
624
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000625/*
626 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
627 */
628static int x509_get_crl_entry_ext( unsigned char **p,
629 const unsigned char *end,
630 x509_buf *ext )
631{
632 int ret;
633 size_t len = 0;
634
635 /* OPTIONAL */
636 if (end <= *p)
637 return( 0 );
638
639 ext->tag = **p;
640 ext->p = *p;
641
642 /*
643 * Get CRL-entry extension sequence header
644 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
645 */
646 if( ( ret = asn1_get_tag( p, end, &ext->len,
647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
648 {
649 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
650 {
651 ext->p = NULL;
652 return( 0 );
653 }
654 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
655 }
656
657 end = *p + ext->len;
658
659 if( end != *p + ext->len )
660 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
661 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
662
663 while( *p < end )
664 {
665 if( ( ret = asn1_get_tag( p, end, &len,
666 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
667 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
668
669 *p += len;
670 }
671
672 if( *p != end )
673 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
674 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
675
676 return( 0 );
677}
678
Paul Bakker74111d32011-01-15 16:57:55 +0000679static int x509_get_basic_constraints( unsigned char **p,
680 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000681 int *ca_istrue,
682 int *max_pathlen )
683{
Paul Bakker23986e52011-04-24 08:57:21 +0000684 int ret;
685 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000686
687 /*
688 * BasicConstraints ::= SEQUENCE {
689 * cA BOOLEAN DEFAULT FALSE,
690 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
691 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000692 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000693 *max_pathlen = 0; /* endless */
694
695 if( ( ret = asn1_get_tag( p, end, &len,
696 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000697 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000698
699 if( *p == end )
700 return 0;
701
Paul Bakker3cccddb2011-01-16 21:46:31 +0000702 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000703 {
704 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000705 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000706
707 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000708 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000709
Paul Bakker3cccddb2011-01-16 21:46:31 +0000710 if( *ca_istrue != 0 )
711 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000712 }
713
714 if( *p == end )
715 return 0;
716
717 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000718 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000719
720 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000721 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000722 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
723
724 (*max_pathlen)++;
725
Paul Bakker74111d32011-01-15 16:57:55 +0000726 return 0;
727}
728
729static int x509_get_ns_cert_type( unsigned char **p,
730 const unsigned char *end,
731 unsigned char *ns_cert_type)
732{
733 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000734 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000735
736 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000737 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000738
739 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000740 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000741 POLARSSL_ERR_ASN1_INVALID_LENGTH );
742
743 /* Get actual bitstring */
744 *ns_cert_type = *bs.p;
745 return 0;
746}
747
748static int x509_get_key_usage( unsigned char **p,
749 const unsigned char *end,
750 unsigned char *key_usage)
751{
752 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000753 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000754
755 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000756 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000757
Paul Bakkercebdf172011-11-11 15:01:31 +0000758 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000759 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000760 POLARSSL_ERR_ASN1_INVALID_LENGTH );
761
762 /* Get actual bitstring */
763 *key_usage = *bs.p;
764 return 0;
765}
766
Paul Bakkerd98030e2009-05-02 15:13:40 +0000767/*
Paul Bakker74111d32011-01-15 16:57:55 +0000768 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
769 *
770 * KeyPurposeId ::= OBJECT IDENTIFIER
771 */
772static int x509_get_ext_key_usage( unsigned char **p,
773 const unsigned char *end,
774 x509_sequence *ext_key_usage)
775{
776 int ret;
777
778 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000779 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000780
781 /* Sequence length must be >= 1 */
782 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000783 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000784 POLARSSL_ERR_ASN1_INVALID_LENGTH );
785
786 return 0;
787}
788
789/*
790 * X.509 v3 extensions
791 *
792 * TODO: Perform all of the basic constraints tests required by the RFC
793 * TODO: Set values for undetected extensions to a sane default?
794 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000795 */
796static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000797 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000798 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000799{
Paul Bakker23986e52011-04-24 08:57:21 +0000800 int ret;
801 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000802 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000803
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000804 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000805 {
806 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
807 return( 0 );
808
809 return( ret );
810 }
811
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 while( *p < end )
813 {
Paul Bakker74111d32011-01-15 16:57:55 +0000814 /*
815 * Extension ::= SEQUENCE {
816 * extnID OBJECT IDENTIFIER,
817 * critical BOOLEAN DEFAULT FALSE,
818 * extnValue OCTET STRING }
819 */
820 x509_buf extn_oid = {0, 0, NULL};
821 int is_critical = 0; /* DEFAULT FALSE */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 if( ( ret = asn1_get_tag( p, end, &len,
824 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000825 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000826
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000827 end_ext_data = *p + len;
828
Paul Bakker74111d32011-01-15 16:57:55 +0000829 /* Get extension ID */
830 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Paul Bakker74111d32011-01-15 16:57:55 +0000832 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000833 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker74111d32011-01-15 16:57:55 +0000835 extn_oid.p = *p;
836 *p += extn_oid.len;
837
838 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000839 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000840 POLARSSL_ERR_ASN1_OUT_OF_DATA );
841
842 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000843 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000844 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000848 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000850 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000852 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000853
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000854 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000855 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000856 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakker74111d32011-01-15 16:57:55 +0000858 /*
859 * Detect supported extensions
860 */
861 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
862 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 {
Paul Bakker74111d32011-01-15 16:57:55 +0000864 /* Parse basic constraints */
865 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000866 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000867 return ( ret );
868 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 }
Paul Bakker74111d32011-01-15 16:57:55 +0000870 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
871 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
872 {
873 /* Parse netscape certificate type */
874 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
875 &crt->ns_cert_type ) ) != 0 )
876 return ( ret );
877 crt->ext_types |= EXT_NS_CERT_TYPE;
878 }
879 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
880 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
881 {
882 /* Parse key usage */
883 if( ( ret = x509_get_key_usage( p, end_ext_octet,
884 &crt->key_usage ) ) != 0 )
885 return ( ret );
886 crt->ext_types |= EXT_KEY_USAGE;
887 }
888 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
889 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
890 {
891 /* Parse extended key usage */
892 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
893 &crt->ext_key_usage ) ) != 0 )
894 return ( ret );
895 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
896 }
897 else
898 {
899 /* No parser found, skip extension */
900 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000901
Paul Bakker5c721f92011-07-27 16:51:09 +0000902#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000903 if( is_critical )
904 {
905 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000906 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000907 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
908 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000909#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000910 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 }
912
913 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000914 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000915 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker5121ce52009-01-03 21:22:43 +0000917 return( 0 );
918}
919
920/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000921 * X.509 CRL Entries
922 */
923static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000924 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000925 x509_crl_entry *entry )
926{
Paul Bakker23986e52011-04-24 08:57:21 +0000927 int ret;
928 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000929 x509_crl_entry *cur_entry = entry;
930
931 if( *p == end )
932 return( 0 );
933
Paul Bakker9be19372009-07-27 20:21:53 +0000934 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000935 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
936 {
937 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
938 return( 0 );
939
940 return( ret );
941 }
942
Paul Bakker9be19372009-07-27 20:21:53 +0000943 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000944
945 while( *p < end )
946 {
Paul Bakker23986e52011-04-24 08:57:21 +0000947 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000948 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000949
950 if( ( ret = asn1_get_tag( p, end, &len2,
951 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
952 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000953 return( ret );
954 }
955
Paul Bakker9be19372009-07-27 20:21:53 +0000956 cur_entry->raw.tag = **p;
957 cur_entry->raw.p = *p;
958 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000959 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000960
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000961 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000962 return( ret );
963
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000964 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 return( ret );
966
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000967 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000968 return( ret );
969
Paul Bakker74111d32011-01-15 16:57:55 +0000970 if ( *p < end )
971 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000972 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkere2e36d32012-01-23 09:56:51 +0000973
974 if( cur_entry->next == NULL )
975 return( POLARSSL_ERR_X509_MALLOC_FAILED );
976
Paul Bakkerd98030e2009-05-02 15:13:40 +0000977 cur_entry = cur_entry->next;
978 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
979 }
980 }
981
982 return( 0 );
983}
984
Paul Bakker27d66162010-03-17 06:56:01 +0000985static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
986{
987 if( sig_oid->len == 9 &&
988 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
989 {
990 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
991 {
992 *sig_alg = sig_oid->p[8];
993 return( 0 );
994 }
995
996 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
997 {
998 *sig_alg = sig_oid->p[8];
999 return( 0 );
1000 }
1001
1002 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1003 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001004 if( sig_oid->len == 5 &&
1005 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1006 {
1007 *sig_alg = SIG_RSA_SHA1;
1008 return( 0 );
1009 }
Paul Bakker27d66162010-03-17 06:56:01 +00001010
1011 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1012}
1013
Paul Bakkerd98030e2009-05-02 15:13:40 +00001014/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001015 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001017int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018{
Paul Bakker23986e52011-04-24 08:57:21 +00001019 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001020 size_t len;
Paul Bakker47f62612013-01-14 16:40:55 +01001021 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakker320a4b52009-03-28 18:52:39 +00001023 /*
1024 * Check for valid input
1025 */
1026 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001027 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001028
Paul Bakker96743fc2011-02-12 14:30:57 +00001029 p = (unsigned char *) malloc( len = buflen );
1030
1031 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001032 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001033
1034 memcpy( p, buf, buflen );
1035
1036 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
1038 crt->raw.p = p;
1039 crt->raw.len = len;
1040 end = p + len;
1041
1042 /*
1043 * Certificate ::= SEQUENCE {
1044 * tbsCertificate TBSCertificate,
1045 * signatureAlgorithm AlgorithmIdentifier,
1046 * signatureValue BIT STRING }
1047 */
1048 if( ( ret = asn1_get_tag( &p, end, &len,
1049 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1050 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001051 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001052 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 }
1054
Paul Bakker47f62612013-01-14 16:40:55 +01001055 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001057 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001058 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001059 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 }
Paul Bakker47f62612013-01-14 16:40:55 +01001061 crt_end = p + len;
1062
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 /*
1064 * TBSCertificate ::= SEQUENCE {
1065 */
1066 crt->tbs.p = p;
1067
1068 if( ( ret = asn1_get_tag( &p, end, &len,
1069 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1070 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001071 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001072 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073 }
1074
1075 end = p + len;
1076 crt->tbs.len = end - crt->tbs.p;
1077
1078 /*
1079 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1080 *
1081 * CertificateSerialNumber ::= INTEGER
1082 *
1083 * signature AlgorithmIdentifier
1084 */
1085 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1086 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1087 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1088 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001089 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 return( ret );
1091 }
1092
1093 crt->version++;
1094
1095 if( crt->version > 3 )
1096 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001097 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001098 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001099 }
1100
Paul Bakker27d66162010-03-17 06:56:01 +00001101 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001103 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001104 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 }
1106
1107 /*
1108 * issuer Name
1109 */
1110 crt->issuer_raw.p = p;
1111
1112 if( ( ret = asn1_get_tag( &p, end, &len,
1113 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001116 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
1119 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1120 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001121 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 return( ret );
1123 }
1124
1125 crt->issuer_raw.len = p - crt->issuer_raw.p;
1126
1127 /*
1128 * Validity ::= SEQUENCE {
1129 * notBefore Time,
1130 * notAfter Time }
1131 *
1132 */
1133 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1134 &crt->valid_to ) ) != 0 )
1135 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001136 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 return( ret );
1138 }
1139
1140 /*
1141 * subject Name
1142 */
1143 crt->subject_raw.p = p;
1144
1145 if( ( ret = asn1_get_tag( &p, end, &len,
1146 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1147 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001148 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001149 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
1151
1152 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1153 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001154 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 return( ret );
1156 }
1157
1158 crt->subject_raw.len = p - crt->subject_raw.p;
1159
1160 /*
1161 * SubjectPublicKeyInfo ::= SEQUENCE
1162 * algorithm AlgorithmIdentifier,
1163 * subjectPublicKey BIT STRING }
1164 */
1165 if( ( ret = asn1_get_tag( &p, end, &len,
1166 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1167 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001168 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001169 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
1171
1172 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1173 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1174 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001175 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 return( ret );
1177 }
1178
1179 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1180 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001181 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 return( ret );
1183 }
1184
1185 crt->rsa.len = mpi_size( &crt->rsa.N );
1186
1187 /*
1188 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1189 * -- If present, version shall be v2 or v3
1190 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1191 * -- If present, version shall be v2 or v3
1192 * extensions [3] EXPLICIT Extensions OPTIONAL
1193 * -- If present, version shall be v3
1194 */
1195 if( crt->version == 2 || crt->version == 3 )
1196 {
1197 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1198 if( ret != 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 if( crt->version == 2 || crt->version == 3 )
1206 {
1207 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1208 if( ret != 0 )
1209 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001210 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 return( ret );
1212 }
1213 }
1214
1215 if( crt->version == 3 )
1216 {
Paul Bakker74111d32011-01-15 16:57:55 +00001217 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 if( ret != 0 )
1219 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001220 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 return( ret );
1222 }
1223 }
1224
1225 if( p != end )
1226 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001227 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001228 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001229 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
1231
Paul Bakker47f62612013-01-14 16:40:55 +01001232 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001233
1234 /*
1235 * signatureAlgorithm AlgorithmIdentifier,
1236 * signatureValue BIT STRING
1237 */
1238 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1239 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001240 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 return( ret );
1242 }
1243
Paul Bakker320a4b52009-03-28 18:52:39 +00001244 if( memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001246 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001247 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001248 }
1249
1250 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
1256 if( p != end )
1257 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001258 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001259 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001260 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001263 return( 0 );
1264}
1265
1266/*
1267 * Parse one or more PEM certificates from a buffer and add them to the chained list
1268 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001269int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001270{
Paul Bakker732e1a82011-12-11 16:35:09 +00001271 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001272 x509_cert *crt, *prev = NULL;
1273 int buf_format = X509_FORMAT_DER;
1274
1275 crt = chain;
1276
1277 /*
1278 * Check for valid input
1279 */
1280 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001281 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001282
1283 while( crt->version != 0 && crt->next != NULL )
1284 {
1285 prev = crt;
1286 crt = crt->next;
1287 }
1288
1289 /*
1290 * Add new certificate on the end of the chain if needed.
1291 */
1292 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001293 {
1294 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1295
Paul Bakker7d06ad22009-05-02 15:53:56 +00001296 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001297 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001298
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001299 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001300 crt = crt->next;
1301 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001304 /*
1305 * Determine buffer content. Buffer contains either one DER certificate or
1306 * one or more PEM certificates.
1307 */
1308#if defined(POLARSSL_PEM_C)
1309 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1310 buf_format = X509_FORMAT_PEM;
1311#endif
1312
1313 if( buf_format == X509_FORMAT_DER )
1314 return x509parse_crt_der( crt, buf, buflen );
1315
1316#if defined(POLARSSL_PEM_C)
1317 if( buf_format == X509_FORMAT_PEM )
1318 {
1319 pem_context pem;
1320
1321 while( buflen > 0 )
1322 {
1323 size_t use_len;
1324 pem_init( &pem );
1325
1326 ret = pem_read_buffer( &pem,
1327 "-----BEGIN CERTIFICATE-----",
1328 "-----END CERTIFICATE-----",
1329 buf, NULL, 0, &use_len );
1330
1331 if( ret == 0 )
1332 {
1333 /*
1334 * Was PEM encoded
1335 */
1336 buflen -= use_len;
1337 buf += use_len;
1338 }
1339 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1340 {
1341 pem_free( &pem );
1342
1343 if( first_error == 0 )
1344 first_error = ret;
1345
1346 continue;
1347 }
1348 else
1349 break;
1350
1351 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1352
1353 pem_free( &pem );
1354
1355 if( ret != 0 )
1356 {
1357 /*
Paul Bakker732e1a82011-12-11 16:35:09 +00001358 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001359 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001360 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001361 {
1362 if( prev )
1363 prev->next = NULL;
1364
1365 if( crt != chain )
1366 free( crt );
1367
1368 return( ret );
1369 }
1370
1371 if( first_error == 0 )
1372 first_error = ret;
Paul Bakker732e1a82011-12-11 16:35:09 +00001373
1374 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001375
1376 memset( crt, 0, sizeof( x509_cert ) );
1377 continue;
1378 }
1379
1380 success = 1;
1381
1382 /*
1383 * Add new certificate to the list
1384 */
1385 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1386
1387 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001388 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001389
1390 prev = crt;
1391 crt = crt->next;
1392 memset( crt, 0, sizeof( x509_cert ) );
1393 }
1394 }
1395#endif
1396
1397 if( crt->version == 0 )
1398 {
1399 if( prev )
1400 prev->next = NULL;
1401
1402 if( crt != chain )
1403 free( crt );
1404 }
1405
1406 if( success )
Paul Bakker732e1a82011-12-11 16:35:09 +00001407 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001408 else if( first_error )
1409 return( first_error );
1410 else
1411 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412}
1413
1414/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001415 * Parse one or more CRLs and add them to the chained list
1416 */
Paul Bakker23986e52011-04-24 08:57:21 +00001417int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001418{
Paul Bakker23986e52011-04-24 08:57:21 +00001419 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001420 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001421 unsigned char *p, *end;
1422 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001423#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001424 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001425 pem_context pem;
1426#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001427
1428 crl = chain;
1429
1430 /*
1431 * Check for valid input
1432 */
1433 if( crl == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001434 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001435
1436 while( crl->version != 0 && crl->next != NULL )
1437 crl = crl->next;
1438
1439 /*
1440 * Add new CRL on the end of the chain if needed.
1441 */
1442 if ( crl->version != 0 && crl->next == NULL)
1443 {
1444 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1445
Paul Bakker7d06ad22009-05-02 15:53:56 +00001446 if( crl->next == NULL )
1447 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001448 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001449 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001450 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001451
Paul Bakker7d06ad22009-05-02 15:53:56 +00001452 crl = crl->next;
1453 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001454 }
1455
Paul Bakker96743fc2011-02-12 14:30:57 +00001456#if defined(POLARSSL_PEM_C)
1457 pem_init( &pem );
1458 ret = pem_read_buffer( &pem,
1459 "-----BEGIN X509 CRL-----",
1460 "-----END X509 CRL-----",
1461 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001462
Paul Bakker96743fc2011-02-12 14:30:57 +00001463 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001464 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001465 /*
1466 * Was PEM encoded
1467 */
1468 buflen -= use_len;
1469 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001470
1471 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001472 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001473 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001474 p = pem.buf;
1475 pem.buf = NULL;
1476 len = pem.buflen;
1477 pem_free( &pem );
1478 }
1479 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1480 {
1481 pem_free( &pem );
1482 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001483 }
1484 else
1485 {
1486 /*
1487 * nope, copy the raw DER data
1488 */
1489 p = (unsigned char *) malloc( len = buflen );
1490
1491 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001492 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001493
1494 memcpy( p, buf, buflen );
1495
1496 buflen = 0;
1497 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001498#else
1499 p = (unsigned char *) malloc( len = buflen );
1500
1501 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001502 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001503
1504 memcpy( p, buf, buflen );
1505
1506 buflen = 0;
1507#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001508
1509 crl->raw.p = p;
1510 crl->raw.len = len;
1511 end = p + len;
1512
1513 /*
1514 * CertificateList ::= SEQUENCE {
1515 * tbsCertList TBSCertList,
1516 * signatureAlgorithm AlgorithmIdentifier,
1517 * signatureValue BIT STRING }
1518 */
1519 if( ( ret = asn1_get_tag( &p, end, &len,
1520 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1521 {
1522 x509_crl_free( crl );
1523 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1524 }
1525
Paul Bakker23986e52011-04-24 08:57:21 +00001526 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001527 {
1528 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001529 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001530 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1531 }
1532
1533 /*
1534 * TBSCertList ::= SEQUENCE {
1535 */
1536 crl->tbs.p = p;
1537
1538 if( ( ret = asn1_get_tag( &p, end, &len,
1539 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1540 {
1541 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001542 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001543 }
1544
1545 end = p + len;
1546 crl->tbs.len = end - crl->tbs.p;
1547
1548 /*
1549 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1550 * -- if present, MUST be v2
1551 *
1552 * signature AlgorithmIdentifier
1553 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001554 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001555 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1556 {
1557 x509_crl_free( crl );
1558 return( ret );
1559 }
1560
1561 crl->version++;
1562
1563 if( crl->version > 2 )
1564 {
1565 x509_crl_free( crl );
1566 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1567 }
1568
Paul Bakker27d66162010-03-17 06:56:01 +00001569 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001570 {
1571 x509_crl_free( crl );
1572 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1573 }
1574
1575 /*
1576 * issuer Name
1577 */
1578 crl->issuer_raw.p = p;
1579
1580 if( ( ret = asn1_get_tag( &p, end, &len,
1581 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1582 {
1583 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001584 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001585 }
1586
1587 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1588 {
1589 x509_crl_free( crl );
1590 return( ret );
1591 }
1592
1593 crl->issuer_raw.len = p - crl->issuer_raw.p;
1594
1595 /*
1596 * thisUpdate Time
1597 * nextUpdate Time OPTIONAL
1598 */
Paul Bakker91200182010-02-18 21:26:15 +00001599 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001600 {
1601 x509_crl_free( crl );
1602 return( ret );
1603 }
1604
Paul Bakker91200182010-02-18 21:26:15 +00001605 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001606 {
Paul Bakker9d781402011-05-09 16:17:09 +00001607 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001608 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001609 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001610 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001611 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001612 x509_crl_free( crl );
1613 return( ret );
1614 }
1615 }
1616
1617 /*
1618 * revokedCertificates SEQUENCE OF SEQUENCE {
1619 * userCertificate CertificateSerialNumber,
1620 * revocationDate Time,
1621 * crlEntryExtensions Extensions OPTIONAL
1622 * -- if present, MUST be v2
1623 * } OPTIONAL
1624 */
1625 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1626 {
1627 x509_crl_free( crl );
1628 return( ret );
1629 }
1630
1631 /*
1632 * crlExtensions EXPLICIT Extensions OPTIONAL
1633 * -- if present, MUST be v2
1634 */
1635 if( crl->version == 2 )
1636 {
1637 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1638
1639 if( ret != 0 )
1640 {
1641 x509_crl_free( crl );
1642 return( ret );
1643 }
1644 }
1645
1646 if( p != end )
1647 {
1648 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001649 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001650 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1651 }
1652
1653 end = crl->raw.p + crl->raw.len;
1654
1655 /*
1656 * signatureAlgorithm AlgorithmIdentifier,
1657 * signatureValue BIT STRING
1658 */
1659 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1660 {
1661 x509_crl_free( crl );
1662 return( ret );
1663 }
1664
1665 if( memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
1666 {
1667 x509_crl_free( crl );
1668 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1669 }
1670
1671 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1672 {
1673 x509_crl_free( crl );
1674 return( ret );
1675 }
1676
1677 if( p != end )
1678 {
1679 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001680 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001681 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1682 }
1683
1684 if( buflen > 0 )
1685 {
1686 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1687
Paul Bakker7d06ad22009-05-02 15:53:56 +00001688 if( crl->next == NULL )
1689 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001691 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001692 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001693
Paul Bakker7d06ad22009-05-02 15:53:56 +00001694 crl = crl->next;
1695 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001696
1697 return( x509parse_crl( crl, buf, buflen ) );
1698 }
1699
1700 return( 0 );
1701}
1702
Paul Bakker335db3f2011-04-25 15:28:35 +00001703#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001704/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001705 * Load all data from a file into a given buffer.
1706 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001707int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001708{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001709 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001710
Paul Bakkerd98030e2009-05-02 15:13:40 +00001711 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001712 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001713
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 fseek( f, 0, SEEK_END );
1715 *n = (size_t) ftell( f );
1716 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001717
Paul Bakkerd98030e2009-05-02 15:13:40 +00001718 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001719 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001720
Paul Bakkerd98030e2009-05-02 15:13:40 +00001721 if( fread( *buf, 1, *n, f ) != *n )
1722 {
1723 fclose( f );
1724 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001725 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001726 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001727
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001729
Paul Bakkerd98030e2009-05-02 15:13:40 +00001730 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001731
Paul Bakkerd98030e2009-05-02 15:13:40 +00001732 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001733}
1734
1735/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 * Load one or more certificates and add them to the chained list
1737 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001738int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001739{
1740 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 size_t n;
1742 unsigned char *buf;
1743
Paul Bakker732e1a82011-12-11 16:35:09 +00001744 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1745 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
Paul Bakker732e1a82011-12-11 16:35:09 +00001747 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749 memset( buf, 0, n + 1 );
1750 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 return( ret );
1753}
1754
Paul Bakkerd98030e2009-05-02 15:13:40 +00001755/*
1756 * Load one or more CRLs and add them to the chained list
1757 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001758int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001759{
1760 int ret;
1761 size_t n;
1762 unsigned char *buf;
1763
Paul Bakker732e1a82011-12-11 16:35:09 +00001764 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1765 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001766
Paul Bakker27fdf462011-06-09 13:55:13 +00001767 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001768
1769 memset( buf, 0, n + 1 );
1770 free( buf );
1771
1772 return( ret );
1773}
1774
Paul Bakker5121ce52009-01-03 21:22:43 +00001775/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001776 * Load and parse a private RSA key
1777 */
1778int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1779{
1780 int ret;
1781 size_t n;
1782 unsigned char *buf;
1783
Paul Bakker732e1a82011-12-11 16:35:09 +00001784 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1785 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001786
1787 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001788 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001789 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001790 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001791 (unsigned char *) pwd, strlen( pwd ) );
1792
1793 memset( buf, 0, n + 1 );
1794 free( buf );
1795
1796 return( ret );
1797}
1798
1799/*
1800 * Load and parse a public RSA key
1801 */
1802int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1803{
1804 int ret;
1805 size_t n;
1806 unsigned char *buf;
1807
Paul Bakker732e1a82011-12-11 16:35:09 +00001808 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1809 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001810
Paul Bakker27fdf462011-06-09 13:55:13 +00001811 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001812
1813 memset( buf, 0, n + 1 );
1814 free( buf );
1815
1816 return( ret );
1817}
1818#endif /* POLARSSL_FS_IO */
1819
1820/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 * Parse a private RSA key
1822 */
Paul Bakker23986e52011-04-24 08:57:21 +00001823int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1824 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001825{
Paul Bakker23986e52011-04-24 08:57:21 +00001826 int ret;
1827 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001829 unsigned char *p_alt;
1830 x509_buf pk_alg_oid;
1831
Paul Bakker96743fc2011-02-12 14:30:57 +00001832#if defined(POLARSSL_PEM_C)
1833 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Paul Bakker96743fc2011-02-12 14:30:57 +00001835 pem_init( &pem );
1836 ret = pem_read_buffer( &pem,
1837 "-----BEGIN RSA PRIVATE KEY-----",
1838 "-----END RSA PRIVATE KEY-----",
1839 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Paul Bakkered56b222011-07-13 11:26:43 +00001841 if( ret == POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
1842 {
1843 ret = pem_read_buffer( &pem,
1844 "-----BEGIN PRIVATE KEY-----",
1845 "-----END PRIVATE KEY-----",
1846 key, pwd, pwdlen, &len );
1847 }
1848
Paul Bakker96743fc2011-02-12 14:30:57 +00001849 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001851 /*
1852 * Was PEM encoded
1853 */
1854 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001856 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001857 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001858 pem_free( &pem );
1859 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Paul Bakker96743fc2011-02-12 14:30:57 +00001862 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1863#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001864 ((void) pwd);
1865 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001866 p = (unsigned char *) key;
1867#endif
1868 end = p + keylen;
1869
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001871 * Note: Depending on the type of private key file one can expect either a
1872 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1873 *
1874 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001875 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001876 * algorithm AlgorithmIdentifier,
1877 * PrivateKey BIT STRING
1878 * }
1879 *
1880 * AlgorithmIdentifier ::= SEQUENCE {
1881 * algorithm OBJECT IDENTIFIER,
1882 * parameters ANY DEFINED BY algorithm OPTIONAL
1883 * }
1884 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 * RSAPrivateKey ::= SEQUENCE {
1886 * version Version,
1887 * modulus INTEGER, -- n
1888 * publicExponent INTEGER, -- e
1889 * privateExponent INTEGER, -- d
1890 * prime1 INTEGER, -- p
1891 * prime2 INTEGER, -- q
1892 * exponent1 INTEGER, -- d mod (p-1)
1893 * exponent2 INTEGER, -- d mod (q-1)
1894 * coefficient INTEGER, -- (inverse of q) mod p
1895 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1896 * }
1897 */
1898 if( ( ret = asn1_get_tag( &p, end, &len,
1899 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1900 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001901#if defined(POLARSSL_PEM_C)
1902 pem_free( &pem );
1903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001905 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 }
1907
1908 end = p + len;
1909
1910 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1911 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001912#if defined(POLARSSL_PEM_C)
1913 pem_free( &pem );
1914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001916 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 }
1918
1919 if( rsa->ver != 0 )
1920 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001921#if defined(POLARSSL_PEM_C)
1922 pem_free( &pem );
1923#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001925 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 }
1927
Paul Bakkered56b222011-07-13 11:26:43 +00001928 p_alt = p;
1929
1930 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1931 {
1932 // Assume that we have the PKCS#1 format if wrong
1933 // tag was encountered
1934 //
1935 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1936 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1937 {
1938#if defined(POLARSSL_PEM_C)
1939 pem_free( &pem );
1940#endif
1941 rsa_free( rsa );
1942 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1943 }
1944 }
1945 else
1946 {
1947 int can_handle;
1948
1949 /*
1950 * only RSA keys handled at this time
1951 */
1952 can_handle = 0;
1953
1954 if( pk_alg_oid.len == 9 &&
1955 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1956 can_handle = 1;
1957
1958 if( pk_alg_oid.len == 9 &&
1959 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1960 {
1961 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1962 can_handle = 1;
1963
1964 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1965 can_handle = 1;
1966 }
1967
1968 if( pk_alg_oid.len == 5 &&
1969 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1970 can_handle = 1;
1971
1972 if( can_handle == 0 )
1973 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1974
1975 /*
1976 * Parse the PKCS#8 format
1977 */
1978
1979 p = p_alt;
1980 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
1981 {
1982#if defined(POLARSSL_PEM_C)
1983 pem_free( &pem );
1984#endif
1985 rsa_free( rsa );
1986 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
1987 }
1988
1989 if( ( end - p ) < 1 )
1990 {
1991#if defined(POLARSSL_PEM_C)
1992 pem_free( &pem );
1993#endif
1994 rsa_free( rsa );
1995 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
1996 POLARSSL_ERR_ASN1_OUT_OF_DATA );
1997 }
1998
1999 end = p + len;
2000
2001 if( ( ret = asn1_get_tag( &p, end, &len,
2002 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2003 {
2004#if defined(POLARSSL_PEM_C)
2005 pem_free( &pem );
2006#endif
2007 rsa_free( rsa );
2008 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2009 }
2010
2011 end = p + len;
2012
2013 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2014 {
2015#if defined(POLARSSL_PEM_C)
2016 pem_free( &pem );
2017#endif
2018 rsa_free( rsa );
2019 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2020 }
2021
2022 if( rsa->ver != 0 )
2023 {
2024#if defined(POLARSSL_PEM_C)
2025 pem_free( &pem );
2026#endif
2027 rsa_free( rsa );
2028 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2029 }
2030 }
2031
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2033 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2034 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2035 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2036 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2037 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2038 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2039 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2040 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002041#if defined(POLARSSL_PEM_C)
2042 pem_free( &pem );
2043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002045 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 }
2047
2048 rsa->len = mpi_size( &rsa->N );
2049
2050 if( p != end )
2051 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002052#if defined(POLARSSL_PEM_C)
2053 pem_free( &pem );
2054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002055 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002056 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002057 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059
2060 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2061 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002062#if defined(POLARSSL_PEM_C)
2063 pem_free( &pem );
2064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 rsa_free( rsa );
2066 return( ret );
2067 }
2068
Paul Bakker96743fc2011-02-12 14:30:57 +00002069#if defined(POLARSSL_PEM_C)
2070 pem_free( &pem );
2071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002072
2073 return( 0 );
2074}
2075
2076/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002077 * Parse a public RSA key
2078 */
Paul Bakker23986e52011-04-24 08:57:21 +00002079int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002080{
Paul Bakker23986e52011-04-24 08:57:21 +00002081 int ret;
2082 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002083 unsigned char *p, *end;
2084 x509_buf alg_oid;
2085#if defined(POLARSSL_PEM_C)
2086 pem_context pem;
2087
2088 pem_init( &pem );
2089 ret = pem_read_buffer( &pem,
2090 "-----BEGIN PUBLIC KEY-----",
2091 "-----END PUBLIC KEY-----",
2092 key, NULL, 0, &len );
2093
2094 if( ret == 0 )
2095 {
2096 /*
2097 * Was PEM encoded
2098 */
2099 keylen = pem.buflen;
2100 }
2101 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
2102 {
2103 pem_free( &pem );
2104 return( ret );
2105 }
2106
2107 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2108#else
2109 p = (unsigned char *) key;
2110#endif
2111 end = p + keylen;
2112
2113 /*
2114 * PublicKeyInfo ::= SEQUENCE {
2115 * algorithm AlgorithmIdentifier,
2116 * PublicKey BIT STRING
2117 * }
2118 *
2119 * AlgorithmIdentifier ::= SEQUENCE {
2120 * algorithm OBJECT IDENTIFIER,
2121 * parameters ANY DEFINED BY algorithm OPTIONAL
2122 * }
2123 *
2124 * RSAPublicKey ::= SEQUENCE {
2125 * modulus INTEGER, -- n
2126 * publicExponent INTEGER -- e
2127 * }
2128 */
2129
2130 if( ( ret = asn1_get_tag( &p, end, &len,
2131 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2132 {
2133#if defined(POLARSSL_PEM_C)
2134 pem_free( &pem );
2135#endif
2136 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002137 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002138 }
2139
2140 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2141 {
2142#if defined(POLARSSL_PEM_C)
2143 pem_free( &pem );
2144#endif
2145 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002146 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002147 }
2148
2149 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2150 {
2151#if defined(POLARSSL_PEM_C)
2152 pem_free( &pem );
2153#endif
2154 rsa_free( rsa );
2155 return( ret );
2156 }
2157
2158 rsa->len = mpi_size( &rsa->N );
2159
2160#if defined(POLARSSL_PEM_C)
2161 pem_free( &pem );
2162#endif
2163
2164 return( 0 );
2165}
2166
Paul Bakkereaa89f82011-04-04 21:36:15 +00002167#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002168/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002169 * Parse DHM parameters
2170 */
Paul Bakker23986e52011-04-24 08:57:21 +00002171int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002172{
Paul Bakker23986e52011-04-24 08:57:21 +00002173 int ret;
2174 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002175 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002176#if defined(POLARSSL_PEM_C)
2177 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002178
Paul Bakker96743fc2011-02-12 14:30:57 +00002179 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002180
Paul Bakker96743fc2011-02-12 14:30:57 +00002181 ret = pem_read_buffer( &pem,
2182 "-----BEGIN DH PARAMETERS-----",
2183 "-----END DH PARAMETERS-----",
2184 dhmin, NULL, 0, &dhminlen );
2185
2186 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002187 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002188 /*
2189 * Was PEM encoded
2190 */
2191 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002192 }
Paul Bakker96743fc2011-02-12 14:30:57 +00002193 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002194 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002195 pem_free( &pem );
2196 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002197 }
2198
Paul Bakker96743fc2011-02-12 14:30:57 +00002199 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2200#else
2201 p = (unsigned char *) dhmin;
2202#endif
2203 end = p + dhminlen;
2204
Paul Bakker1b57b062011-01-06 15:48:19 +00002205 memset( dhm, 0, sizeof( dhm_context ) );
2206
Paul Bakker1b57b062011-01-06 15:48:19 +00002207 /*
2208 * DHParams ::= SEQUENCE {
2209 * prime INTEGER, -- P
2210 * generator INTEGER, -- g
2211 * }
2212 */
2213 if( ( ret = asn1_get_tag( &p, end, &len,
2214 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2215 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002216#if defined(POLARSSL_PEM_C)
2217 pem_free( &pem );
2218#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002219 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002220 }
2221
2222 end = p + len;
2223
2224 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2225 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2226 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002227#if defined(POLARSSL_PEM_C)
2228 pem_free( &pem );
2229#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002230 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002231 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002232 }
2233
2234 if( p != end )
2235 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002236#if defined(POLARSSL_PEM_C)
2237 pem_free( &pem );
2238#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002239 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002240 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002241 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2242 }
2243
Paul Bakker96743fc2011-02-12 14:30:57 +00002244#if defined(POLARSSL_PEM_C)
2245 pem_free( &pem );
2246#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002247
2248 return( 0 );
2249}
2250
Paul Bakker335db3f2011-04-25 15:28:35 +00002251#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002252/*
2253 * Load and parse a private RSA key
2254 */
2255int x509parse_dhmfile( dhm_context *dhm, const char *path )
2256{
2257 int ret;
2258 size_t n;
2259 unsigned char *buf;
2260
Paul Bakker732e1a82011-12-11 16:35:09 +00002261 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2262 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002263
Paul Bakker27fdf462011-06-09 13:55:13 +00002264 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002265
2266 memset( buf, 0, n + 1 );
2267 free( buf );
2268
2269 return( ret );
2270}
Paul Bakker335db3f2011-04-25 15:28:35 +00002271#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002272#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002273
Paul Bakker5121ce52009-01-03 21:22:43 +00002274#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002275#include <stdarg.h>
2276
2277#if !defined vsnprintf
2278#define vsnprintf _vsnprintf
2279#endif // vsnprintf
2280
2281/*
2282 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2283 * Result value is not size of buffer needed, but -1 if no fit is possible.
2284 *
2285 * This fuction tries to 'fix' this by at least suggesting enlarging the
2286 * size by 20.
2287 */
2288int compat_snprintf(char *str, size_t size, const char *format, ...)
2289{
2290 va_list ap;
2291 int res = -1;
2292
2293 va_start( ap, format );
2294
2295 res = vsnprintf( str, size, format, ap );
2296
2297 va_end( ap );
2298
2299 // No quick fix possible
2300 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002301 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002302
2303 return res;
2304}
2305
2306#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002307#endif
2308
Paul Bakkerd98030e2009-05-02 15:13:40 +00002309#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2310
2311#define SAFE_SNPRINTF() \
2312{ \
2313 if( ret == -1 ) \
2314 return( -1 ); \
2315 \
Paul Bakker23986e52011-04-24 08:57:21 +00002316 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002317 p[n - 1] = '\0'; \
2318 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2319 } \
2320 \
Paul Bakker23986e52011-04-24 08:57:21 +00002321 n -= (unsigned int) ret; \
2322 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002323}
2324
Paul Bakker5121ce52009-01-03 21:22:43 +00002325/*
2326 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002327 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002329int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330{
Paul Bakker23986e52011-04-24 08:57:21 +00002331 int ret;
2332 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002334 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 char s[128], *p;
2336
2337 memset( s, 0, sizeof( s ) );
2338
2339 name = dn;
2340 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002341 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
2343 while( name != NULL )
2344 {
Paul Bakker74111d32011-01-15 16:57:55 +00002345 if( name != dn )
2346 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002347 ret = snprintf( p, n, ", " );
2348 SAFE_SNPRINTF();
2349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
2351 if( memcmp( name->oid.p, OID_X520, 2 ) == 0 )
2352 {
2353 switch( name->oid.p[2] )
2354 {
2355 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002356 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
2358 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002359 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002360
2361 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002362 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002365 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002368 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002369
2370 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002372
2373 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002374 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 name->oid.p[2] );
2376 break;
2377 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002378 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380 else if( memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
2381 {
2382 switch( name->oid.p[8] )
2383 {
2384 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002385 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002388 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 name->oid.p[8] );
2390 break;
2391 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002392 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394 else
Paul Bakker74111d32011-01-15 16:57:55 +00002395 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002397 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 for( i = 0; i < name->val.len; i++ )
2401 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002402 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 break;
2404
2405 c = name->val.p[i];
2406 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2407 s[i] = '?';
2408 else s[i] = c;
2409 }
2410 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411 ret = snprintf( p, n, "%s", s );
2412 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 name = name->next;
2414 }
2415
Paul Bakker23986e52011-04-24 08:57:21 +00002416 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417}
2418
2419/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002420 * Store the serial in printable form into buf; no more
2421 * than size characters will be written
2422 */
2423int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2424{
Paul Bakker23986e52011-04-24 08:57:21 +00002425 int ret;
2426 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002427 char *p;
2428
2429 p = buf;
2430 n = size;
2431
2432 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002433 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002434
2435 for( i = 0; i < nr; i++ )
2436 {
Paul Bakker93048802011-12-05 14:38:06 +00002437 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002438 continue;
2439
Paul Bakkerdd476992011-01-16 21:34:59 +00002440 ret = snprintf( p, n, "%02X%s",
2441 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2442 SAFE_SNPRINTF();
2443 }
2444
Paul Bakker03c7c252011-11-25 12:37:37 +00002445 if( nr != serial->len )
2446 {
2447 ret = snprintf( p, n, "...." );
2448 SAFE_SNPRINTF();
2449 }
2450
Paul Bakker23986e52011-04-24 08:57:21 +00002451 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002452}
2453
2454/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002455 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002457int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2458 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459{
Paul Bakker23986e52011-04-24 08:57:21 +00002460 int ret;
2461 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002462 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
2464 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002465 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002466
Paul Bakkerd98030e2009-05-02 15:13:40 +00002467 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002469 SAFE_SNPRINTF();
2470 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002472 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Paul Bakkerdd476992011-01-16 21:34:59 +00002474 ret = x509parse_serial_gets( p, n, &crt->serial);
2475 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Paul Bakkerd98030e2009-05-02 15:13:40 +00002477 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2478 SAFE_SNPRINTF();
2479 ret = x509parse_dn_gets( p, n, &crt->issuer );
2480 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002481
Paul Bakkerd98030e2009-05-02 15:13:40 +00002482 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2483 SAFE_SNPRINTF();
2484 ret = x509parse_dn_gets( p, n, &crt->subject );
2485 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002486
Paul Bakkerd98030e2009-05-02 15:13:40 +00002487 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2489 crt->valid_from.year, crt->valid_from.mon,
2490 crt->valid_from.day, crt->valid_from.hour,
2491 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002492 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
Paul Bakkerd98030e2009-05-02 15:13:40 +00002494 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2496 crt->valid_to.year, crt->valid_to.mon,
2497 crt->valid_to.day, crt->valid_to.hour,
2498 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002499 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002500
Paul Bakkerd98030e2009-05-02 15:13:40 +00002501 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2502 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Paul Bakker27d66162010-03-17 06:56:01 +00002504 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002506 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2507 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2508 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2509 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2510 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2511 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2512 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2513 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2514 default: ret = snprintf( p, n, "???" ); break;
2515 }
2516 SAFE_SNPRINTF();
2517
2518 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002519 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 SAFE_SNPRINTF();
2521
Paul Bakker23986e52011-04-24 08:57:21 +00002522 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002523}
2524
Paul Bakker74111d32011-01-15 16:57:55 +00002525/* Compare a given OID string with an OID x509_buf * */
2526#define OID_CMP(oid_str, oid_buf) \
2527 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2528 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2529
2530/*
2531 * Return an informational string describing the given OID
2532 */
2533const char *x509_oid_get_description( x509_buf *oid )
2534{
2535 if ( oid == NULL )
2536 return ( NULL );
2537
2538 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2539 return( STRING_SERVER_AUTH );
2540
2541 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2542 return( STRING_CLIENT_AUTH );
2543
2544 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2545 return( STRING_CODE_SIGNING );
2546
2547 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2548 return( STRING_EMAIL_PROTECTION );
2549
2550 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2551 return( STRING_TIME_STAMPING );
2552
2553 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2554 return( STRING_OCSP_SIGNING );
2555
2556 return( NULL );
2557}
2558
2559/* Return the x.y.z.... style numeric string for the given OID */
2560int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2561{
Paul Bakker23986e52011-04-24 08:57:21 +00002562 int ret;
2563 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002564 unsigned int value;
2565 char *p;
2566
2567 p = buf;
2568 n = size;
2569
2570 /* First byte contains first two dots */
2571 if( oid->len > 0 )
2572 {
2573 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2574 SAFE_SNPRINTF();
2575 }
2576
2577 /* TODO: value can overflow in value. */
2578 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002579 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002580 {
2581 value <<= 7;
2582 value += oid->p[i] & 0x7F;
2583
2584 if( !( oid->p[i] & 0x80 ) )
2585 {
2586 /* Last byte */
2587 ret = snprintf( p, n, ".%d", value );
2588 SAFE_SNPRINTF();
2589 value = 0;
2590 }
2591 }
2592
Paul Bakker23986e52011-04-24 08:57:21 +00002593 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002594}
2595
Paul Bakkerd98030e2009-05-02 15:13:40 +00002596/*
2597 * Return an informational string about the CRL.
2598 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002599int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2600 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002601{
Paul Bakker23986e52011-04-24 08:57:21 +00002602 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002603 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002604 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002605 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002606
2607 p = buf;
2608 n = size;
2609
2610 ret = snprintf( p, n, "%sCRL version : %d",
2611 prefix, crl->version );
2612 SAFE_SNPRINTF();
2613
2614 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2615 SAFE_SNPRINTF();
2616 ret = x509parse_dn_gets( p, n, &crl->issuer );
2617 SAFE_SNPRINTF();
2618
2619 ret = snprintf( p, n, "\n%sthis update : " \
2620 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2621 crl->this_update.year, crl->this_update.mon,
2622 crl->this_update.day, crl->this_update.hour,
2623 crl->this_update.min, crl->this_update.sec );
2624 SAFE_SNPRINTF();
2625
2626 ret = snprintf( p, n, "\n%snext update : " \
2627 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2628 crl->next_update.year, crl->next_update.mon,
2629 crl->next_update.day, crl->next_update.hour,
2630 crl->next_update.min, crl->next_update.sec );
2631 SAFE_SNPRINTF();
2632
2633 entry = &crl->entry;
2634
2635 ret = snprintf( p, n, "\n%sRevoked certificates:",
2636 prefix );
2637 SAFE_SNPRINTF();
2638
Paul Bakker9be19372009-07-27 20:21:53 +00002639 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002640 {
2641 ret = snprintf( p, n, "\n%sserial number: ",
2642 prefix );
2643 SAFE_SNPRINTF();
2644
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002645 ret = x509parse_serial_gets( p, n, &entry->serial);
2646 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002647
Paul Bakkerd98030e2009-05-02 15:13:40 +00002648 ret = snprintf( p, n, " revocation date: " \
2649 "%04d-%02d-%02d %02d:%02d:%02d",
2650 entry->revocation_date.year, entry->revocation_date.mon,
2651 entry->revocation_date.day, entry->revocation_date.hour,
2652 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002653 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002654
2655 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
2657
Paul Bakkerd98030e2009-05-02 15:13:40 +00002658 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2659 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Paul Bakker27d66162010-03-17 06:56:01 +00002661 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002662 {
2663 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2664 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2665 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2666 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2667 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2668 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2669 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2670 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2671 default: ret = snprintf( p, n, "???" ); break;
2672 }
2673 SAFE_SNPRINTF();
2674
Paul Bakker1e27bb22009-07-19 20:25:25 +00002675 ret = snprintf( p, n, "\n" );
2676 SAFE_SNPRINTF();
2677
Paul Bakker23986e52011-04-24 08:57:21 +00002678 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002679}
2680
2681/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002682 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002683 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002684int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002685{
Paul Bakkercce9d772011-11-18 14:26:47 +00002686 int year, mon, day;
2687 int hour, min, sec;
2688
2689#if defined(_WIN32)
2690 SYSTEMTIME st;
2691
2692 GetLocalTime(&st);
2693
2694 year = st.wYear;
2695 mon = st.wMonth;
2696 day = st.wDay;
2697 hour = st.wHour;
2698 min = st.wMinute;
2699 sec = st.wSecond;
2700#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 struct tm *lt;
2702 time_t tt;
2703
2704 tt = time( NULL );
2705 lt = localtime( &tt );
2706
Paul Bakkercce9d772011-11-18 14:26:47 +00002707 year = lt->tm_year + 1900;
2708 mon = lt->tm_mon + 1;
2709 day = lt->tm_mday;
2710 hour = lt->tm_hour;
2711 min = lt->tm_min;
2712 sec = lt->tm_sec;
2713#endif
2714
2715 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002716 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Paul Bakkercce9d772011-11-18 14:26:47 +00002718 if( year == to->year &&
2719 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002720 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
Paul Bakkercce9d772011-11-18 14:26:47 +00002722 if( year == to->year &&
2723 mon == to->mon &&
2724 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002725 return( 1 );
2726
Paul Bakkercce9d772011-11-18 14:26:47 +00002727 if( year == to->year &&
2728 mon == to->mon &&
2729 day == to->day &&
2730 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002731 return( 1 );
2732
Paul Bakkercce9d772011-11-18 14:26:47 +00002733 if( year == to->year &&
2734 mon == to->mon &&
2735 day == to->day &&
2736 hour == to->hour &&
2737 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002738 return( 1 );
2739
Paul Bakkercce9d772011-11-18 14:26:47 +00002740 if( year == to->year &&
2741 mon == to->mon &&
2742 day == to->day &&
2743 hour == to->hour &&
2744 min == to->min &&
2745 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002746 return( 1 );
2747
Paul Bakker40ea7de2009-05-03 10:18:48 +00002748 return( 0 );
2749}
2750
2751/*
2752 * Return 1 if the certificate is revoked, or 0 otherwise.
2753 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002754int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002755{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002756 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002757
2758 while( cur != NULL && cur->serial.len != 0 )
2759 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002760 if( crt->serial.len == cur->serial.len &&
2761 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002762 {
2763 if( x509parse_time_expired( &cur->revocation_date ) )
2764 return( 1 );
2765 }
2766
2767 cur = cur->next;
2768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
2770 return( 0 );
2771}
2772
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002773/*
2774 * Wrapper for x509 hashes.
2775 *
Paul Bakker0f5f72e2011-01-18 14:58:55 +00002776 * \param out Buffer to receive the hash (Should be at least 64 bytes)
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002777 */
Paul Bakker23986e52011-04-24 08:57:21 +00002778static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 unsigned char *out )
2780{
2781 switch( alg )
2782 {
Paul Bakker40e46942009-01-03 21:51:57 +00002783#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002784 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002785#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002786#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002787 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002788#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002789#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002790 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002791#endif
2792#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002793 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002794#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002795#if defined(POLARSSL_SHA2_C)
2796 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2797 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2798#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002799#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002800 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2801 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2802#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002804 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805 break;
2806 }
2807}
2808
2809/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002810 * Check that the given certificate is valid accoring to the CRL.
2811 */
2812static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2813 x509_crl *crl_list)
2814{
2815 int flags = 0;
2816 int hash_id;
2817 unsigned char hash[64];
2818
2819 /*
2820 * TODO: What happens if no CRL is present?
2821 * Suggestion: Revocation state should be unknown if no CRL is present.
2822 * For backwards compatibility this is not yet implemented.
2823 */
2824
2825 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2826 {
2827 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2828 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2829 crl_list->issuer_raw.len ) != 0 )
2830 {
2831 crl_list = crl_list->next;
2832 continue;
2833 }
2834
2835 /*
2836 * Check if CRL is correctly signed by the trusted CA
2837 */
2838 hash_id = crl_list->sig_alg;
2839
2840 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2841
2842 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2843 0, hash, crl_list->sig.p ) == 0 )
2844 {
2845 /*
2846 * CRL is not trusted
2847 */
2848 flags |= BADCRL_NOT_TRUSTED;
2849 break;
2850 }
2851
2852 /*
2853 * Check for validity of CRL (Do not drop out)
2854 */
2855 if( x509parse_time_expired( &crl_list->next_update ) )
2856 flags |= BADCRL_EXPIRED;
2857
2858 /*
2859 * Check if certificate is revoked
2860 */
2861 if( x509parse_revoked(crt, crl_list) )
2862 {
2863 flags |= BADCERT_REVOKED;
2864 break;
2865 }
2866
2867 crl_list = crl_list->next;
2868 }
2869 return flags;
2870}
2871
2872/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 * Verify the certificate validity
2874 */
2875int x509parse_verify( x509_cert *crt,
2876 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002877 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002878 const char *cn, int *flags,
2879 int (*f_vrfy)(void *, x509_cert *, int, int),
2880 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881{
Paul Bakker23986e52011-04-24 08:57:21 +00002882 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 int hash_id;
2884 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002885 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002887 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Paul Bakker40ea7de2009-05-03 10:18:48 +00002889 *flags = 0;
2890
2891 if( x509parse_time_expired( &crt->valid_to ) )
2892 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002893
2894 if( cn != NULL )
2895 {
2896 name = &crt->subject;
2897 cn_len = strlen( cn );
2898
2899 while( name != NULL )
2900 {
2901 if( memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2902 memcmp( name->val.p, cn, cn_len ) == 0 &&
2903 name->val.len == cn_len )
2904 break;
2905
2906 name = name->next;
2907 }
2908
2909 if( name == NULL )
2910 *flags |= BADCERT_CN_MISMATCH;
2911 }
2912
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 /*
2914 * Iterate upwards in the given cert chain,
2915 * ignoring any upper cert with CA != TRUE.
2916 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002917 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
2919 pathlen = 1;
2920
Paul Bakker76fd75a2011-01-16 21:12:10 +00002921 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002923 if( parent->ca_istrue == 0 ||
2924 crt->issuer_raw.len != parent->subject_raw.len ||
2925 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 crt->issuer_raw.len ) != 0 )
2927 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002928 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 continue;
2930 }
2931
Paul Bakker27d66162010-03-17 06:56:01 +00002932 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
2934 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2935
Paul Bakker76fd75a2011-01-16 21:12:10 +00002936 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2937 crt->sig.p ) != 0 )
2938 *flags |= BADCERT_NOT_TRUSTED;
2939
2940 /* Check trusted CA's CRL for the given crt */
2941 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002942
2943 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002944 if( NULL != f_vrfy )
2945 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002946 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002947 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002948 else
2949 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002950 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002951 else if( *flags != 0 )
2952 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
2954 pathlen++;
2955
Paul Bakker76fd75a2011-01-16 21:12:10 +00002956 crt = parent;
2957 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 }
2959
2960 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002961 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002963 *flags |= BADCERT_NOT_TRUSTED;
2964
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002965 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 {
2967 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2968 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2969 crt->issuer_raw.len ) != 0 )
2970 {
2971 trust_ca = trust_ca->next;
2972 continue;
2973 }
2974
2975 if( trust_ca->max_pathlen > 0 &&
2976 trust_ca->max_pathlen < pathlen )
2977 break;
2978
Paul Bakker27d66162010-03-17 06:56:01 +00002979 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
2981 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2982
2983 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
2984 0, hash, crt->sig.p ) == 0 )
2985 {
2986 /*
2987 * cert. is signed by a trusted CA
2988 */
2989 *flags &= ~BADCERT_NOT_TRUSTED;
2990 break;
2991 }
2992
2993 trust_ca = trust_ca->next;
2994 }
2995
Paul Bakker76fd75a2011-01-16 21:12:10 +00002996 /* Check trusted CA's CRL for the given crt */
2997 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002998
2999 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003000 if( NULL != f_vrfy )
3001 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003002 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003003 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003004 else
3005 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003006 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003007 else if( *flags != 0 )
3008 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 return( 0 );
3011}
3012
3013/*
3014 * Unallocate all certificate data
3015 */
3016void x509_free( x509_cert *crt )
3017{
3018 x509_cert *cert_cur = crt;
3019 x509_cert *cert_prv;
3020 x509_name *name_cur;
3021 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003022 x509_sequence *seq_cur;
3023 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024
3025 if( crt == NULL )
3026 return;
3027
3028 do
3029 {
3030 rsa_free( &cert_cur->rsa );
3031
3032 name_cur = cert_cur->issuer.next;
3033 while( name_cur != NULL )
3034 {
3035 name_prv = name_cur;
3036 name_cur = name_cur->next;
3037 memset( name_prv, 0, sizeof( x509_name ) );
3038 free( name_prv );
3039 }
3040
3041 name_cur = cert_cur->subject.next;
3042 while( name_cur != NULL )
3043 {
3044 name_prv = name_cur;
3045 name_cur = name_cur->next;
3046 memset( name_prv, 0, sizeof( x509_name ) );
3047 free( name_prv );
3048 }
3049
Paul Bakker74111d32011-01-15 16:57:55 +00003050 seq_cur = cert_cur->ext_key_usage.next;
3051 while( seq_cur != NULL )
3052 {
3053 seq_prv = seq_cur;
3054 seq_cur = seq_cur->next;
3055 memset( seq_prv, 0, sizeof( x509_sequence ) );
3056 free( seq_prv );
3057 }
3058
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 if( cert_cur->raw.p != NULL )
3060 {
3061 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3062 free( cert_cur->raw.p );
3063 }
3064
3065 cert_cur = cert_cur->next;
3066 }
3067 while( cert_cur != NULL );
3068
3069 cert_cur = crt;
3070 do
3071 {
3072 cert_prv = cert_cur;
3073 cert_cur = cert_cur->next;
3074
3075 memset( cert_prv, 0, sizeof( x509_cert ) );
3076 if( cert_prv != crt )
3077 free( cert_prv );
3078 }
3079 while( cert_cur != NULL );
3080}
3081
Paul Bakkerd98030e2009-05-02 15:13:40 +00003082/*
3083 * Unallocate all CRL data
3084 */
3085void x509_crl_free( x509_crl *crl )
3086{
3087 x509_crl *crl_cur = crl;
3088 x509_crl *crl_prv;
3089 x509_name *name_cur;
3090 x509_name *name_prv;
3091 x509_crl_entry *entry_cur;
3092 x509_crl_entry *entry_prv;
3093
3094 if( crl == NULL )
3095 return;
3096
3097 do
3098 {
3099 name_cur = crl_cur->issuer.next;
3100 while( name_cur != NULL )
3101 {
3102 name_prv = name_cur;
3103 name_cur = name_cur->next;
3104 memset( name_prv, 0, sizeof( x509_name ) );
3105 free( name_prv );
3106 }
3107
3108 entry_cur = crl_cur->entry.next;
3109 while( entry_cur != NULL )
3110 {
3111 entry_prv = entry_cur;
3112 entry_cur = entry_cur->next;
3113 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3114 free( entry_prv );
3115 }
3116
3117 if( crl_cur->raw.p != NULL )
3118 {
3119 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3120 free( crl_cur->raw.p );
3121 }
3122
3123 crl_cur = crl_cur->next;
3124 }
3125 while( crl_cur != NULL );
3126
3127 crl_cur = crl;
3128 do
3129 {
3130 crl_prv = crl_cur;
3131 crl_cur = crl_cur->next;
3132
3133 memset( crl_prv, 0, sizeof( x509_crl ) );
3134 if( crl_prv != crl )
3135 free( crl_prv );
3136 }
3137 while( crl_cur != NULL );
3138}
3139
Paul Bakker40e46942009-01-03 21:51:57 +00003140#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003141
Paul Bakker40e46942009-01-03 21:51:57 +00003142#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
3144/*
3145 * Checkup routine
3146 */
3147int x509_self_test( int verbose )
3148{
Paul Bakker5690efc2011-05-26 13:16:06 +00003149#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003150 int ret;
3151 int flags;
3152 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 x509_cert cacert;
3154 x509_cert clicert;
3155 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003156#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003157 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
3160 if( verbose != 0 )
3161 printf( " X.509 certificate load: " );
3162
3163 memset( &clicert, 0, sizeof( x509_cert ) );
3164
3165 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003166 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ret != 0 )
3168 {
3169 if( verbose != 0 )
3170 printf( "failed\n" );
3171
3172 return( ret );
3173 }
3174
3175 memset( &cacert, 0, sizeof( x509_cert ) );
3176
3177 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003178 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 if( ret != 0 )
3180 {
3181 if( verbose != 0 )
3182 printf( "failed\n" );
3183
3184 return( ret );
3185 }
3186
3187 if( verbose != 0 )
3188 printf( "passed\n X.509 private key load: " );
3189
3190 i = strlen( test_ca_key );
3191 j = strlen( test_ca_pwd );
3192
Paul Bakker66b78b22011-03-25 14:22:50 +00003193 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3194
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 if( ( ret = x509parse_key( &rsa,
3196 (unsigned char *) test_ca_key, i,
3197 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3198 {
3199 if( verbose != 0 )
3200 printf( "failed\n" );
3201
3202 return( ret );
3203 }
3204
3205 if( verbose != 0 )
3206 printf( "passed\n X.509 signature verify: ");
3207
Paul Bakker23986e52011-04-24 08:57:21 +00003208 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 if( ret != 0 )
3210 {
Paul Bakker23986e52011-04-24 08:57:21 +00003211 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 if( verbose != 0 )
3213 printf( "failed\n" );
3214
3215 return( ret );
3216 }
3217
Paul Bakker5690efc2011-05-26 13:16:06 +00003218#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003220 printf( "passed\n X.509 DHM parameter load: " );
3221
3222 i = strlen( test_dhm_params );
3223 j = strlen( test_ca_pwd );
3224
3225 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3226 {
3227 if( verbose != 0 )
3228 printf( "failed\n" );
3229
3230 return( ret );
3231 }
3232
3233 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003235#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
3237 x509_free( &cacert );
3238 x509_free( &clicert );
3239 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003240#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003241 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003243
3244 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003245#else
3246 ((void) verbose);
3247 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003249}
3250
3251#endif
3252
3253#endif