blob: 46d88fa419d9d0f5e82a95e0a67636c7269352f1 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_USE_C)
40
41#include "polarssl/x509.h"
42#include "polarssl/asn1.h"
43#include "polarssl/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000044
45#include <string.h>
46
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#if defined(POLARSSL_PEM_PARSE_C)
48#include "polarssl/pem.h"
49#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
52#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdio.h>
55#include <stdlib.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056#define polarssl_free free
Rich Evansfac657f2015-01-30 11:00:01 +000057#define polarssl_malloc malloc
58#define polarssl_printf printf
59#define polarssl_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
64#else
65#include <time.h>
66#endif
67
68#if defined(POLARSSL_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#if !defined(_WIN32)
70#include <sys/types.h>
71#include <sys/stat.h>
72#include <dirent.h>
73#endif
74#endif
75
Rich Evans7d5a55a2015-02-13 11:48:02 +000076#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
77
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078/*
79 * CertificateSerialNumber ::= INTEGER
80 */
81int x509_get_serial( unsigned char **p, const unsigned char *end,
82 x509_buf *serial )
83{
84 int ret;
85
86 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +020087 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088 POLARSSL_ERR_ASN1_OUT_OF_DATA );
89
90 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
91 **p != ASN1_INTEGER )
Paul Bakker51876562013-09-17 14:36:05 +020092 return( POLARSSL_ERR_X509_INVALID_SERIAL +
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
94
95 serial->tag = *(*p)++;
96
97 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +020098 return( POLARSSL_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
100 serial->p = *p;
101 *p += serial->len;
102
103 return( 0 );
104}
105
106/* Get an algorithm identifier without parameters (eg for signatures)
107 *
108 * AlgorithmIdentifier ::= SEQUENCE {
109 * algorithm OBJECT IDENTIFIER,
110 * parameters ANY DEFINED BY algorithm OPTIONAL }
111 */
112int x509_get_alg_null( unsigned char **p, const unsigned char *end,
113 x509_buf *alg )
114{
115 int ret;
116
117 if( ( ret = asn1_get_alg_null( p, end, alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200118 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
120 return( 0 );
121}
122
123/*
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100124 * Parse an algorithm identifier with (optional) paramaters
125 */
126int x509_get_alg( unsigned char **p, const unsigned char *end,
127 x509_buf *alg, x509_buf *params )
128{
129 int ret;
130
131 if( ( ret = asn1_get_alg( p, end, alg, params ) ) != 0 )
132 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
133
134 return( 0 );
135}
136
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200137#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100138/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100139 * HashAlgorithm ::= AlgorithmIdentifier
140 *
141 * AlgorithmIdentifier ::= SEQUENCE {
142 * algorithm OBJECT IDENTIFIER,
143 * parameters ANY DEFINED BY algorithm OPTIONAL }
144 *
145 * For HashAlgorithm, parameters MUST be NULL or absent.
146 */
147static int x509_get_hash_alg( const x509_buf *alg, md_type_t *md_alg )
148{
149 int ret;
150 unsigned char *p;
151 const unsigned char *end;
152 x509_buf md_oid;
153 size_t len;
154
155 /* Make sure we got a SEQUENCE and setup bounds */
156 if( alg->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
157 return( POLARSSL_ERR_X509_INVALID_ALG +
158 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
159
160 p = (unsigned char *) alg->p;
161 end = p + alg->len;
162
163 if( p >= end )
164 return( POLARSSL_ERR_X509_INVALID_ALG +
165 POLARSSL_ERR_ASN1_OUT_OF_DATA );
166
167 /* Parse md_oid */
168 md_oid.tag = *p;
169
170 if( ( ret = asn1_get_tag( &p, end, &md_oid.len, ASN1_OID ) ) != 0 )
171 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
172
173 md_oid.p = p;
174 p += md_oid.len;
175
176 /* Get md_alg from md_oid */
177 if( ( ret = oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
178 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
179
180 /* Make sure params is absent of NULL */
181 if( p == end )
182 return( 0 );
183
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100184 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_NULL ) ) != 0 || len != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
186
187 if( p != end )
188 return( POLARSSL_ERR_X509_INVALID_ALG +
189 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
190
191 return( 0 );
192}
193
194/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100195 * RSASSA-PSS-params ::= SEQUENCE {
196 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
197 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
198 * saltLength [2] INTEGER DEFAULT 20,
199 * trailerField [3] INTEGER DEFAULT 1 }
200 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200201 *
202 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
203 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
204 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100205 */
206int x509_get_rsassa_pss_params( const x509_buf *params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100207 md_type_t *md_alg, md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200208 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100209{
210 int ret;
211 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100212 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213 size_t len;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100214 x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215
216 /* First set everything to defaults */
217 *md_alg = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100218 *mgf_md = POLARSSL_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100220
221 /* Make sure params is a SEQUENCE and setup bounds */
222 if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
223 return( POLARSSL_ERR_X509_INVALID_ALG +
224 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
225
226 p = (unsigned char *) params->p;
227 end = p + params->len;
228
229 if( p == end )
230 return( 0 );
231
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100232 /*
233 * HashAlgorithm
234 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100235 if( ( ret = asn1_get_tag( &p, end, &len,
236 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) == 0 )
237 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 end2 = p + len;
239
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100240 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100241 if( ( ret = x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100242 return( ret );
243
244 if( ( ret = oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
245 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100246
247 if( p != end2 )
248 return( POLARSSL_ERR_X509_INVALID_ALG +
249 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 }
251 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
252 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
253
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100254 if( p == end )
255 return( 0 );
256
257 /*
258 * MaskGenAlgorithm
259 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 if( ( ret = asn1_get_tag( &p, end, &len,
261 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ) == 0 )
262 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100263 end2 = p + len;
264
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100265 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100266 if( ( ret = x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100267 return( ret );
268
269 /* Only MFG1 is recognised for now */
270 if( ! OID_CMP( OID_MGF1, &alg_id ) )
271 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE +
272 POLARSSL_ERR_OID_NOT_FOUND );
273
274 /* Parse HashAlgorithm */
275 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
276 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100277
278 if( p != end2 )
279 return( POLARSSL_ERR_X509_INVALID_ALG +
280 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100281 }
282 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
283 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
284
285 if( p == end )
286 return( 0 );
287
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100288 /*
289 * salt_len
290 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100291 if( ( ret = asn1_get_tag( &p, end, &len,
292 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 2 ) ) == 0 )
293 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100294 end2 = p + len;
295
296 if( ( ret = asn1_get_int( &p, end2, salt_len ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100298
299 if( p != end2 )
300 return( POLARSSL_ERR_X509_INVALID_ALG +
301 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302 }
303 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
304 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
305
306 if( p == end )
307 return( 0 );
308
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200310 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100311 */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100312 if( ( ret = asn1_get_tag( &p, end, &len,
313 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 3 ) ) == 0 )
314 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200315 int trailer_field;
316
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317 end2 = p + len;
318
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200319 if( ( ret = asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100320 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100321
322 if( p != end2 )
323 return( POLARSSL_ERR_X509_INVALID_ALG +
324 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200325
326 if( trailer_field != 1 )
327 return( POLARSSL_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100328 }
329 else if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
330 return( POLARSSL_ERR_X509_INVALID_ALG + ret );
331
332 if( p != end )
333 return( POLARSSL_ERR_X509_INVALID_ALG +
334 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
335
336 return( 0 );
337}
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200338#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100339
340/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 * AttributeTypeAndValue ::= SEQUENCE {
342 * type AttributeType,
343 * value AttributeValue }
344 *
345 * AttributeType ::= OBJECT IDENTIFIER
346 *
347 * AttributeValue ::= ANY DEFINED BY AttributeType
348 */
349static int x509_get_attr_type_value( unsigned char **p,
350 const unsigned char *end,
351 x509_name *cur )
352{
353 int ret;
354 size_t len;
355 x509_buf *oid;
356 x509_buf *val;
357
358 if( ( ret = asn1_get_tag( p, end, &len,
359 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200360 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200363 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 POLARSSL_ERR_ASN1_OUT_OF_DATA );
365
366 oid = &cur->oid;
367 oid->tag = **p;
368
369 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200370 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371
372 oid->p = *p;
373 *p += oid->len;
374
375 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200376 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 POLARSSL_ERR_ASN1_OUT_OF_DATA );
378
379 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
380 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
381 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker51876562013-09-17 14:36:05 +0200382 return( POLARSSL_ERR_X509_INVALID_NAME +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
384
385 val = &cur->val;
386 val->tag = *(*p)++;
387
388 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200389 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390
391 val->p = *p;
392 *p += val->len;
393
394 cur->next = NULL;
395
396 return( 0 );
397}
398
399/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000400 * Name ::= CHOICE { -- only one possibility for now --
401 * rdnSequence RDNSequence }
402 *
403 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
404 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 * RelativeDistinguishedName ::=
406 * SET OF AttributeTypeAndValue
407 *
408 * AttributeTypeAndValue ::= SEQUENCE {
409 * type AttributeType,
410 * value AttributeValue }
411 *
412 * AttributeType ::= OBJECT IDENTIFIER
413 *
414 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200415 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000416 * The data structure is optimized for the common case where each RDN has only
417 * one element, which is represented as a list of AttributeTypeAndValue.
418 * For the general case we still use a flat list, but we mark elements of the
419 * same set so that they are "merged" together in the functions that consume
420 * this list, eg x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 */
422int x509_get_name( unsigned char **p, const unsigned char *end,
423 x509_name *cur )
424{
425 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200426 size_t set_len;
427 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100429 /* don't use recursion, we'd risk stack overflow if not optimized */
430 while( 1 )
431 {
432 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000433 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100434 */
435 if( ( ret = asn1_get_tag( p, end, &set_len,
436 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
437 return( POLARSSL_ERR_X509_INVALID_NAME + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100439 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000441 while( 1 )
442 {
443 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
444 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000446 if( *p == end_set )
447 break;
448
449 /* Mark this item as being only one in a set */
450 cur->next_merged = 1;
451
452 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
453
454 if( cur->next == NULL )
455 return( POLARSSL_ERR_X509_MALLOC_FAILED );
456
457 memset( cur->next, 0, sizeof( x509_name ) );
458
459 cur = cur->next;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100462 /*
463 * continue until end of SEQUENCE is reached
464 */
465 if( *p == end )
466 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100468 cur->next = (x509_name *) polarssl_malloc( sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 if( cur->next == NULL )
471 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100473 memset( cur->next, 0, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100475 cur = cur->next;
476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477}
478
Rich Evans7d5a55a2015-02-13 11:48:02 +0000479static int x509_parse_int(unsigned char **p, unsigned n, int *res){
480 *res = 0;
481 for( ; n > 0; --n ){
482 if( ( **p < '0') || ( **p > '9' ) ) return POLARSSL_ERR_X509_INVALID_DATE;
483 *res *= 10;
484 *res += (*(*p)++ - '0');
485 }
486 return 0;
487}
488
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489/*
490 * Time ::= CHOICE {
491 * utcTime UTCTime,
492 * generalTime GeneralizedTime }
493 */
494int x509_get_time( unsigned char **p, const unsigned char *end,
495 x509_time *time )
496{
497 int ret;
498 size_t len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 unsigned char tag;
500
501 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200502 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 POLARSSL_ERR_ASN1_OUT_OF_DATA );
504
505 tag = **p;
506
Paul Bakker66d5d072014-06-17 16:39:18 +0200507 if( tag == ASN1_UTC_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508 {
509 (*p)++;
510 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200511
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200513 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
Rich Evans7d5a55a2015-02-13 11:48:02 +0000515 CHECK( x509_parse_int( p, 2, &time->year ) );
516 CHECK( x509_parse_int( p, 2, &time->mon ) );
517 CHECK( x509_parse_int( p, 2, &time->day ) );
518 CHECK( x509_parse_int( p, 2, &time->hour ) );
519 CHECK( x509_parse_int( p, 2, &time->min ) );
520 if( len > 10 )
521 CHECK( x509_parse_int( p, 2, &time->sec ) );
522 if( len > 12 && *(*p)++ != 'Z' )
Paul Bakker51876562013-09-17 14:36:05 +0200523 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 time->year += 100 * ( time->year < 50 );
526 time->year += 1900;
527
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 return( 0 );
529 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200530 else if( tag == ASN1_GENERALIZED_TIME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 {
532 (*p)++;
533 ret = asn1_get_len( p, end, &len );
Paul Bakker51876562013-09-17 14:36:05 +0200534
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200536 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Rich Evans7d5a55a2015-02-13 11:48:02 +0000538 CHECK( x509_parse_int( p, 4, &time->year ) );
539 CHECK( x509_parse_int( p, 2, &time->mon ) );
540 CHECK( x509_parse_int( p, 2, &time->day ) );
541 CHECK( x509_parse_int( p, 2, &time->hour ) );
542 CHECK( x509_parse_int( p, 2, &time->min ) );
543 if( len > 12 )
544 CHECK( x509_parse_int( p, 2, &time->sec ) );
545 if( len > 14 && *(*p)++ != 'Z' )
Paul Bakker51876562013-09-17 14:36:05 +0200546 return( POLARSSL_ERR_X509_INVALID_DATE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 return( 0 );
549 }
550 else
Paul Bakker51876562013-09-17 14:36:05 +0200551 return( POLARSSL_ERR_X509_INVALID_DATE +
552 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553}
554
555int x509_get_sig( unsigned char **p, const unsigned char *end, x509_buf *sig )
556{
557 int ret;
558 size_t len;
559
560 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200561 return( POLARSSL_ERR_X509_INVALID_SIGNATURE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 POLARSSL_ERR_ASN1_OUT_OF_DATA );
563
564 sig->tag = **p;
565
566 if( ( ret = asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200567 return( POLARSSL_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 sig->len = len;
570 sig->p = *p;
571
572 *p += len;
573
574 return( 0 );
575}
576
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100577/*
578 * Get signature algorithm from alg OID and optional parameters
579 */
580int x509_get_sig_alg( const x509_buf *sig_oid, const x509_buf *sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200581 md_type_t *md_alg, pk_type_t *pk_alg,
582 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100584 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200586 if( *sig_opts != NULL )
587 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
588
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100589 if( ( ret = oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200590 return( POLARSSL_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200592#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100593 if( *pk_alg == POLARSSL_PK_RSASSA_PSS )
594 {
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200595 pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100596
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200597 pss_opts = polarssl_malloc( sizeof( pk_rsassa_pss_options ) );
598 if( pss_opts == NULL )
599 return( POLARSSL_ERR_X509_MALLOC_FAILED );
600
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100601 ret = x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200602 md_alg,
603 &pss_opts->mgf1_hash_id,
604 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100605 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200606 {
607 polarssl_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100608 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200609 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100610
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200611 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100612 }
613 else
Paul Bakkerdb20c102014-06-17 14:34:44 +0200614#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100615 {
616 /* Make sure parameters are absent or NULL */
617 if( ( sig_params->tag != ASN1_NULL && sig_params->tag != 0 ) ||
618 sig_params->len != 0 )
619 return( POLARSSL_ERR_X509_INVALID_ALG );
620 }
621
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 return( 0 );
623}
624
625/*
626 * X.509 Extensions (No parsing of extensions, pointer should
627 * be either manually updated or extensions should be parsed!
628 */
629int x509_get_ext( unsigned char **p, const unsigned char *end,
630 x509_buf *ext, int tag )
631{
632 int ret;
633 size_t len;
634
635 if( *p == end )
636 return( 0 );
637
638 ext->tag = **p;
639
640 if( ( ret = asn1_get_tag( p, end, &ext->len,
641 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
642 return( ret );
643
644 ext->p = *p;
645 end = *p + ext->len;
646
647 /*
648 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
649 *
650 * Extension ::= SEQUENCE {
651 * extnID OBJECT IDENTIFIER,
652 * critical BOOLEAN DEFAULT FALSE,
653 * extnValue OCTET STRING }
654 */
655 if( ( ret = asn1_get_tag( p, end, &len,
656 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200657 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 if( end != *p + len )
Paul Bakker51876562013-09-17 14:36:05 +0200660 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
662
663 return( 0 );
664}
665
Paul Bakker6edcd412013-10-29 15:22:54 +0100666#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
667 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668#include <stdarg.h>
669
670#if !defined vsnprintf
671#define vsnprintf _vsnprintf
672#endif // vsnprintf
673
674/*
675 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
676 * Result value is not size of buffer needed, but -1 if no fit is possible.
677 *
678 * This fuction tries to 'fix' this by at least suggesting enlarging the
679 * size by 20.
680 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200681static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682{
683 va_list ap;
684 int res = -1;
685
686 va_start( ap, format );
687
688 res = vsnprintf( str, size, format, ap );
689
690 va_end( ap );
691
692 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +0200693 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 return( (int) size + 20 );
695
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697}
698
699#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +0200700#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701
702#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
703
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200704#define SAFE_SNPRINTF() \
705{ \
706 if( ret == -1 ) \
707 return( -1 ); \
708 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200709 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200710 p[n - 1] = '\0'; \
711 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
712 } \
713 \
714 n -= (unsigned int) ret; \
715 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716}
717
718/*
719 * Store the name in printable form into buf; no more
720 * than size characters will be written
721 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200722int x509_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723{
724 int ret;
725 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000726 unsigned char c, merge = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 const x509_name *name;
728 const char *short_name = NULL;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200729 char s[X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
731 memset( s, 0, sizeof( s ) );
732
733 name = dn;
734 p = buf;
735 n = size;
736
737 while( name != NULL )
738 {
739 if( !name->oid.p )
740 {
741 name = name->next;
742 continue;
743 }
744
745 if( name != dn )
746 {
Rich Evansfac657f2015-01-30 11:00:01 +0000747 ret = polarssl_snprintf( p, n, merge ? " + " : ", " );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 SAFE_SNPRINTF();
749 }
750
751 ret = oid_get_attr_short_name( &name->oid, &short_name );
752
753 if( ret == 0 )
Rich Evansfac657f2015-01-30 11:00:01 +0000754 ret = polarssl_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 else
Rich Evansfac657f2015-01-30 11:00:01 +0000756 ret = polarssl_snprintf( p, n, "\?\?=" );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 SAFE_SNPRINTF();
758
759 for( i = 0; i < name->val.len; i++ )
760 {
761 if( i >= sizeof( s ) - 1 )
762 break;
763
764 c = name->val.p[i];
765 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
766 s[i] = '?';
767 else s[i] = c;
768 }
769 s[i] = '\0';
Rich Evansfac657f2015-01-30 11:00:01 +0000770 ret = polarssl_snprintf( p, n, "%s", s );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000772
773 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 name = name->next;
775 }
776
777 return( (int) ( size - n ) );
778}
779
780/*
781 * Store the serial in printable form into buf; no more
782 * than size characters will be written
783 */
Paul Bakker86d0c192013-09-18 11:11:02 +0200784int x509_serial_gets( char *buf, size_t size, const x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785{
786 int ret;
787 size_t i, n, nr;
788 char *p;
789
790 p = buf;
791 n = size;
792
793 nr = ( serial->len <= 32 )
794 ? serial->len : 28;
795
796 for( i = 0; i < nr; i++ )
797 {
798 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
799 continue;
800
Rich Evansfac657f2015-01-30 11:00:01 +0000801 ret = polarssl_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
803 SAFE_SNPRINTF();
804 }
805
806 if( nr != serial->len )
807 {
Rich Evansfac657f2015-01-30 11:00:01 +0000808 ret = polarssl_snprintf( p, n, "...." );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 SAFE_SNPRINTF();
810 }
811
812 return( (int) ( size - n ) );
813}
814
815/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200816 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100817 */
818int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200819 pk_type_t pk_alg, md_type_t md_alg,
820 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100821{
822 int ret;
823 char *p = buf;
824 size_t n = size;
825 const char *desc = NULL;
826
827 ret = oid_get_sig_alg_desc( sig_oid, &desc );
828 if( ret != 0 )
Rich Evansfac657f2015-01-30 11:00:01 +0000829 ret = polarssl_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100830 else
Rich Evansfac657f2015-01-30 11:00:01 +0000831 ret = polarssl_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100832 SAFE_SNPRINTF();
833
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200834#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100835 if( pk_alg == POLARSSL_PK_RSASSA_PSS )
836 {
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200837 const pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100838 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100839
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200840 pss_opts = (const pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100841
842 md_info = md_info_from_type( md_alg );
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200843 mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100844
Rich Evansfac657f2015-01-30 11:00:01 +0000845 ret = polarssl_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100846 md_info ? md_info->name : "???",
847 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200848 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100849 SAFE_SNPRINTF();
850 }
851#else
852 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200853 ((void) md_alg);
854 ((void) sig_opts);
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +0200855#endif /* POLARSSL_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100856
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200857 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100858}
859
860/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 * Helper for writing "RSA key size", "EC key size", etc
862 */
863int x509_key_size_helper( char *buf, size_t size, const char *name )
864{
865 char *p = buf;
866 size_t n = size;
867 int ret;
868
869 if( strlen( name ) + sizeof( " key size" ) > size )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200870 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871
Rich Evansfac657f2015-01-30 11:00:01 +0000872 ret = polarssl_snprintf( p, n, "%s key size", name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 SAFE_SNPRINTF();
874
875 return( 0 );
876}
877
878/*
879 * Return an informational string describing the given OID
880 */
881const char *x509_oid_get_description( x509_buf *oid )
882{
883 const char *desc = NULL;
884 int ret;
885
886 ret = oid_get_extended_key_usage( oid, &desc );
887
888 if( ret != 0 )
889 return( NULL );
890
891 return( desc );
892}
893
894/* Return the x.y.z.... style numeric string for the given OID */
895int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
896{
897 return oid_get_numeric_string( buf, size, oid );
898}
899
900/*
901 * Return 0 if the x509_time is still valid, or 1 otherwise.
902 */
903#if defined(POLARSSL_HAVE_TIME)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100905static void x509_get_current_time( x509_time *now )
906{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100907#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908 SYSTEMTIME st;
909
Paul Bakker66d5d072014-06-17 16:39:18 +0200910 GetSystemTime( &st );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100912 now->year = st.wYear;
913 now->mon = st.wMonth;
914 now->day = st.wDay;
915 now->hour = st.wHour;
916 now->min = st.wMinute;
917 now->sec = st.wSecond;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918#else
Paul Bakker5fff23b2014-03-26 15:34:54 +0100919 struct tm lt;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920 time_t tt;
921
922 tt = time( NULL );
Manuel Pégourié-Gonnard0776a432014-04-11 12:25:45 +0200923 gmtime_r( &tt, &lt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
Paul Bakker5fff23b2014-03-26 15:34:54 +0100925 now->year = lt.tm_year + 1900;
926 now->mon = lt.tm_mon + 1;
927 now->day = lt.tm_mday;
928 now->hour = lt.tm_hour;
929 now->min = lt.tm_min;
930 now->sec = lt.tm_sec;
Paul Bakker9af723c2014-05-01 13:03:14 +0200931#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100932}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100934/*
935 * Return 0 if before <= after, 1 otherwise
936 */
937static int x509_check_time( const x509_time *before, const x509_time *after )
938{
939 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 return( 1 );
941
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100942 if( before->year == after->year &&
943 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 return( 1 );
945
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100946 if( before->year == after->year &&
947 before->mon == after->mon &&
948 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 return( 1 );
950
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100951 if( before->year == after->year &&
952 before->mon == after->mon &&
953 before->day == after->day &&
954 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 return( 1 );
956
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100957 if( before->year == after->year &&
958 before->mon == after->mon &&
959 before->day == after->day &&
960 before->hour == after->hour &&
961 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 return( 1 );
963
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100964 if( before->year == after->year &&
965 before->mon == after->mon &&
966 before->day == after->day &&
967 before->hour == after->hour &&
968 before->min == after->min &&
969 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 return( 1 );
971
972 return( 0 );
973}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100974
975int x509_time_expired( const x509_time *to )
976{
977 x509_time now;
978
979 x509_get_current_time( &now );
980
981 return( x509_check_time( &now, to ) );
982}
983
984int x509_time_future( const x509_time *from )
985{
986 x509_time now;
987
988 x509_get_current_time( &now );
989
990 return( x509_check_time( from, &now ) );
991}
992
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993#else /* POLARSSL_HAVE_TIME */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100994
Paul Bakker86d0c192013-09-18 11:11:02 +0200995int x509_time_expired( const x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996{
997 ((void) to);
998 return( 0 );
999}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001000
1001int x509_time_future( const x509_time *from )
1002{
1003 ((void) from);
1004 return( 0 );
1005}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006#endif /* POLARSSL_HAVE_TIME */
1007
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001008#if defined(POLARSSL_SELF_TEST)
1009
1010#include "polarssl/x509_crt.h"
1011#include "polarssl/certs.h"
1012
1013/*
1014 * Checkup routine
1015 */
1016int x509_self_test( int verbose )
1017{
Manuel Pégourié-Gonnard3d413702014-04-29 15:29:41 +02001018#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_SHA1_C)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001019 int ret;
1020 int flags;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001021 x509_crt cacert;
1022 x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001023
1024 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001025 polarssl_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001026
Paul Bakkerb6b09562013-09-18 14:17:41 +02001027 x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001028
Paul Bakkerddf26b42013-09-18 13:46:23 +02001029 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
1030 strlen( test_cli_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001031 if( ret != 0 )
1032 {
1033 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001034 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035
1036 return( ret );
1037 }
1038
Paul Bakkerb6b09562013-09-18 14:17:41 +02001039 x509_crt_init( &cacert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001040
Paul Bakkerddf26b42013-09-18 13:46:23 +02001041 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
1042 strlen( test_ca_crt ) );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001043 if( ret != 0 )
1044 {
1045 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001046 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047
1048 return( ret );
1049 }
1050
1051 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001052 polarssl_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001053
Paul Bakkerddf26b42013-09-18 13:46:23 +02001054 ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001055 if( ret != 0 )
1056 {
1057 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001058 polarssl_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059
Paul Bakker66d5d072014-06-17 16:39:18 +02001060 polarssl_printf( "ret = %d, &flags = %04x\n", ret, flags );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001061
1062 return( ret );
1063 }
1064
1065 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001066 polarssl_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001067
1068 x509_crt_free( &cacert );
1069 x509_crt_free( &clicert );
1070
1071 return( 0 );
1072#else
1073 ((void) verbose);
1074 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker9af723c2014-05-01 13:03:14 +02001075#endif /* POLARSSL_CERTS_C && POLARSSL_SHA1_C */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001076}
1077
Paul Bakker9af723c2014-05-01 13:03:14 +02001078#endif /* POLARSSL_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001079
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080#endif /* POLARSSL_X509_USE_C */