blob: f86e9e3c88ed0739341d121d45cf1ef924c568e4 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
30 */
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509.h"
Hanno Beckerf6bc8882019-05-02 13:05:58 +010041#include "mbedtls/x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/asn1.h"
43#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000044
Teppo Järvelinf69e6412019-09-03 16:50:17 +030045/* We include x509xxx.c files here so that x509.c is one compilation unit including
46 * all the x509 files. This is done because some of the internal functions are shared.
47 * For code size savings internal functions should be static so that compiler can do better job
48 * when optimizing. We don't wan't x509.c file to get too big so including .c files.
49 */
Teppo Järvelinffaba552019-09-03 12:33:16 +030050#include "x509_crl.c"
51#include "x509_crt.c"
52#include "x509_csr.c"
53#include "x509_create.c"
54#include "x509write_crt.c"
55#include "x509write_csr.c"
56
Rich Evans36796df2015-02-12 18:27:14 +000057#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <string.h>
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#else
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
68#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010069#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020070#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010071#define mbedtls_printf printf
72#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#endif
74
Simon Butcherb5b6af22016-07-13 14:46:18 +010075#if defined(MBEDTLS_HAVE_TIME)
76#include "mbedtls/platform_time.h"
77#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000078#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010079#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <time.h>
81#endif
82
Hanno Beckerd6028a12018-10-15 12:01:35 +010083#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
84#define CHECK_RANGE(min, max, val) \
85 do \
86 { \
87 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
88 { \
89 return( ret ); \
90 } \
91 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000092
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093/*
94 * CertificateSerialNumber ::= INTEGER
95 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +030096static int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098{
99 int ret;
100
101 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
103 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
106 **p != MBEDTLS_ASN1_INTEGER )
107 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
108 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
110 serial->tag = *(*p)++;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
113 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114
115 serial->p = *p;
116 *p += serial->len;
117
118 return( 0 );
119}
120
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300121#if defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) || \
122 ( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) )
123/*
124 * Parse an algorithm identifier with (optional) parameters
125 */
126static int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
127 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
128{
129 int ret;
130
131 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
132 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
133
134 return( 0 );
135}
136#endif /* defined(MBEDTLS_X509_CRL_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C) ||
137 ( !defined(MBEDTLS_X509_ON_DEMAND_PARSING) && defined(MBEDTLS_X509_CRT_PARSE_C) ) */
138
139#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200140/* Get an algorithm identifier without parameters (eg for signatures)
141 *
142 * AlgorithmIdentifier ::= SEQUENCE {
143 * algorithm OBJECT IDENTIFIER,
144 * parameters ANY DEFINED BY algorithm OPTIONAL }
145 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300146static int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148{
149 int ret;
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
152 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153
154 return( 0 );
155}
156
157/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100158 * HashAlgorithm ::= AlgorithmIdentifier
159 *
160 * AlgorithmIdentifier ::= SEQUENCE {
161 * algorithm OBJECT IDENTIFIER,
162 * parameters ANY DEFINED BY algorithm OPTIONAL }
163 *
164 * For HashAlgorithm, parameters MUST be NULL or absent.
165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100167{
168 int ret;
169 unsigned char *p;
170 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100172 size_t len;
173
174 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
176 return( MBEDTLS_ERR_X509_INVALID_ALG +
177 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100178
Andrzej Kurekddc2db42020-07-16 04:37:41 -0400179 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100180 end = p + alg->len;
181
182 if( p >= end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_X509_INVALID_ALG +
184 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185
186 /* Parse md_oid */
187 md_oid.tag = *p;
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
190 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
192 md_oid.p = p;
193 p += md_oid.len;
194
195 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
197 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100198
199 /* Make sure params is absent of NULL */
200 if( p == end )
201 return( 0 );
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
204 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100205
206 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_X509_INVALID_ALG +
208 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100209
210 return( 0 );
211}
212
213/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100214 * RSASSA-PSS-params ::= SEQUENCE {
215 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
216 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
217 * saltLength [2] INTEGER DEFAULT 20,
218 * trailerField [3] INTEGER DEFAULT 1 }
219 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200220 *
221 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
222 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
223 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100224 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300225static int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200227 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100228{
229 int ret;
230 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100231 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100232 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234
235 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 *md_alg = MBEDTLS_MD_SHA1;
237 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100239
240 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
242 return( MBEDTLS_ERR_X509_INVALID_ALG +
243 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100244
245 p = (unsigned char *) params->p;
246 end = p + params->len;
247
248 if( p == end )
249 return( 0 );
250
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 /*
252 * HashAlgorithm
253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
255 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100257 end2 = p + len;
258
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100259 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100261 return( ret );
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
264 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265
266 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_X509_INVALID_ALG +
268 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
271 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100272
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273 if( p == end )
274 return( 0 );
275
276 /*
277 * MaskGenAlgorithm
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
280 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100281 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100282 end2 = p + len;
283
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100284 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100286 return( ret );
287
288 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
290 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
291 MBEDTLS_ERR_OID_NOT_FOUND );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100292
293 /* Parse HashAlgorithm */
294 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
295 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100296
297 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_X509_INVALID_ALG +
299 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
302 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100303
304 if( p == end )
305 return( 0 );
306
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100307 /*
308 * salt_len
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
311 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100312 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100313 end2 = p + len;
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
316 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317
318 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_X509_INVALID_ALG +
320 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
323 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100324
325 if( p == end )
326 return( 0 );
327
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100328 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200329 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
332 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100333 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200334 int trailer_field;
335
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100336 end2 = p + len;
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
339 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100340
341 if( p != end2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_X509_INVALID_ALG +
343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200344
345 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
349 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100350
351 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_X509_INVALID_ALG +
353 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100354
355 return( 0 );
356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100358
359/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 * AttributeTypeAndValue ::= SEQUENCE {
361 * type AttributeType,
362 * value AttributeValue }
363 *
364 * AttributeType ::= OBJECT IDENTIFIER
365 *
366 * AttributeValue ::= ANY DEFINED BY AttributeType
Hanno Beckere452add2019-05-02 13:19:34 +0100367 *
368 * NOTE: This function returns an ASN.1 low-level error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 */
370static int x509_get_attr_type_value( unsigned char **p,
371 const unsigned char *end,
Hanno Becker3470d592019-02-20 09:45:17 +0000372 mbedtls_x509_buf *oid,
373 mbedtls_x509_buf *val )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374{
375 int ret;
376 size_t len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000378 ret = mbedtls_asn1_get_tag( p, end, &len,
379 MBEDTLS_ASN1_CONSTRUCTED |
380 MBEDTLS_ASN1_SEQUENCE );
381 if( ret != 0 )
382 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000384 end = *p + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385
Hanno Becker3470d592019-02-20 09:45:17 +0000386 ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID );
387 if( ret != 0 )
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000388 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
Hanno Beckerace04a62019-02-20 09:35:34 +0000390 oid->tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 oid->p = *p;
392 *p += oid->len;
393
Hanno Becker3470d592019-02-20 09:45:17 +0000394 if( *p == end )
395 {
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000396 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
397 goto exit;
Hanno Becker3470d592019-02-20 09:45:17 +0000398 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
Hanno Beckerb40dc582019-02-20 09:38:45 +0000400 if( !MBEDTLS_ASN1_IS_STRING_TAG( **p ) )
401 {
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000402 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
403 goto exit;
Hanno Beckerb40dc582019-02-20 09:38:45 +0000404 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 val->tag = *(*p)++;
407
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000408 ret = mbedtls_asn1_get_len( p, end, &val->len );
409 if( ret != 0 )
410 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 val->p = *p;
413 *p += val->len;
414
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000415 if( *p != end )
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000416 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
417
418exit:
419 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420}
421
422/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000423 * Name ::= CHOICE { -- only one possibility for now --
424 * rdnSequence RDNSequence }
425 *
426 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
427 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 * RelativeDistinguishedName ::=
429 * SET OF AttributeTypeAndValue
430 *
431 * AttributeTypeAndValue ::= SEQUENCE {
432 * type AttributeType,
433 * value AttributeValue }
434 *
435 * AttributeType ::= OBJECT IDENTIFIER
436 *
437 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200438 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000439 * The data structure is optimized for the common case where each RDN has only
440 * one element, which is represented as a list of AttributeTypeAndValue.
441 * For the general case we still use a flat list, but we mark elements of the
442 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 * this list, eg mbedtls_x509_dn_gets().
Hanno Beckere452add2019-05-02 13:19:34 +0100444 *
445 * NOTE: This function returns an ASN.1 low-level error code.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 */
Hanno Becker3470d592019-02-20 09:45:17 +0000447static int x509_set_sequence_iterate( unsigned char **p,
448 unsigned char const **end_set,
449 unsigned char const *end,
450 mbedtls_x509_buf *oid,
451 mbedtls_x509_buf *val )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452{
453 int ret;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200454 size_t set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
Hanno Becker3470d592019-02-20 09:45:17 +0000456 if( *p == *end_set )
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100457 {
Hanno Becker3470d592019-02-20 09:45:17 +0000458 /* Parse next TLV of ASN.1 SET structure. */
459 ret = mbedtls_asn1_get_tag( p, end, &set_len,
460 MBEDTLS_ASN1_CONSTRUCTED |
461 MBEDTLS_ASN1_SET );
462 if( ret != 0 )
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000463 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Hanno Becker3470d592019-02-20 09:45:17 +0000465 *end_set = *p + set_len;
466 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Hanno Beckere452add2019-05-02 13:19:34 +0100468 /* x509_get_attr_type_value() returns ASN.1 low-level error codes. */
Hanno Becker30cb1ac2019-02-20 11:30:29 +0000469 ret = x509_get_attr_type_value( p, *end_set, oid, val );
470
471exit:
472 return( ret );
Hanno Becker3470d592019-02-20 09:45:17 +0000473}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
Hanno Becker88de3422019-02-20 12:41:55 +0000475/*
476 * Like memcmp, but case-insensitive and always returns -1 if different
477 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300478static int mbedtls_x509_memcasecmp( const void *s1, const void *s2,
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000479 size_t len1, size_t len2 )
Hanno Becker88de3422019-02-20 12:41:55 +0000480{
481 size_t i;
482 unsigned char diff;
483 const unsigned char *n1 = s1, *n2 = s2;
484
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000485 if( len1 != len2 )
486 return( -1 );
487
488 for( i = 0; i < len1; i++ )
Hanno Becker88de3422019-02-20 12:41:55 +0000489 {
490 diff = n1[i] ^ n2[i];
491
492 if( diff == 0 )
493 continue;
494
495 if( diff == 32 &&
496 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
497 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
498 {
499 continue;
500 }
501
502 return( -1 );
503 }
504
505 return( 0 );
506}
507
508/*
509 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
510 * variations (but not all).
511 *
512 * Return 0 if equal, -1 otherwise.
513 */
514static int x509_string_cmp( const mbedtls_x509_buf *a,
515 const mbedtls_x509_buf *b )
516{
517 if( a->tag == b->tag &&
518 a->len == b->len &&
Piotr Nowickie3c4ee52020-06-23 12:59:56 +0200519 mbedtls_platform_memequal( a->p, b->p, b->len ) == 0 )
Hanno Becker88de3422019-02-20 12:41:55 +0000520 {
521 return( 0 );
522 }
523
524 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
525 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000526 mbedtls_x509_memcasecmp( a->p, b->p,
527 a->len, b->len ) == 0 )
Hanno Becker88de3422019-02-20 12:41:55 +0000528 {
529 return( 0 );
530 }
531
532 return( -1 );
533}
534
535/*
Hanno Becker7dee12a2019-02-21 13:58:38 +0000536 * Compare two X.509 Names (aka rdnSequence) given as raw ASN.1 data.
Hanno Becker88de3422019-02-20 12:41:55 +0000537 *
538 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
Hanno Becker7dee12a2019-02-21 13:58:38 +0000539 * We sometimes return unequal when the full algorithm would return equal,
Hanno Becker88de3422019-02-20 12:41:55 +0000540 * but never the other way. (In particular, we don't do Unicode normalisation
541 * or space folding.)
542 *
Hanno Becker67284cc2019-02-21 14:31:51 +0000543 * Further, this function allows to pass a callback to be triggered for every
544 * pair of well-formed and equal entries in the two input name lists.
545 *
Hanno Becker7dee12a2019-02-21 13:58:38 +0000546 * Returns:
Hanno Becker67284cc2019-02-21 14:31:51 +0000547 * - 0 if both sequences are well-formed, present the same X.509 name,
548 * and the callback (if provided) hasn't returned a non-zero value
549 * on any of the name components.
550 * - 1 if a difference was detected in the name components.
551 * - A non-zero error code if the abort callback returns a non-zero value.
552 * In this case, the returned error code is the error code from the callback.
Hanno Becker7dee12a2019-02-21 13:58:38 +0000553 * - A negative error code if a parsing error occurred in either
554 * of the two buffers.
555 *
556 * This function can be used to verify that a buffer contains a well-formed
557 * ASN.1 encoded X.509 name by calling it with equal parameters.
Hanno Becker88de3422019-02-20 12:41:55 +0000558 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300559static int mbedtls_x509_name_cmp_raw( mbedtls_x509_buf_raw const *a,
Hanno Becker67284cc2019-02-21 14:31:51 +0000560 mbedtls_x509_buf_raw const *b,
561 int (*abort_check)( void *ctx,
Hanno Beckerbe0cf9b2019-05-02 13:17:29 +0100562 mbedtls_x509_buf *oid,
563 mbedtls_x509_buf *val,
564 int next_merged ),
Hanno Becker67284cc2019-02-21 14:31:51 +0000565 void *abort_check_ctx )
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000566{
567 int ret;
Hanno Becker1e11f212019-03-04 14:43:43 +0000568 size_t idx;
569 unsigned char *p[2], *end[2], *set[2];
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000570
Hanno Becker1e11f212019-03-04 14:43:43 +0000571 p[0] = a->p;
572 p[1] = b->p;
573 end[0] = p[0] + a->len;
574 end[1] = p[1] + b->len;
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000575
Hanno Becker1e11f212019-03-04 14:43:43 +0000576 for( idx = 0; idx < 2; idx++ )
577 {
578 size_t len;
579 ret = mbedtls_asn1_get_tag( &p[idx], end[idx], &len,
580 MBEDTLS_ASN1_CONSTRUCTED |
581 MBEDTLS_ASN1_SEQUENCE );
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000582
Hanno Becker1e11f212019-03-04 14:43:43 +0000583 if( end[idx] != p[idx] + len )
584 {
585 return( MBEDTLS_ERR_X509_INVALID_NAME +
586 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
587 }
588
589 set[idx] = p[idx];
590 }
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000591
592 while( 1 )
593 {
Hanno Becker6b378122019-02-23 10:20:14 +0000594 int next_merged;
Hanno Becker1e11f212019-03-04 14:43:43 +0000595 mbedtls_x509_buf oid[2], val[2];
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000596
Hanno Becker1e11f212019-03-04 14:43:43 +0000597 ret = x509_set_sequence_iterate( &p[0], (const unsigned char **) &set[0],
598 end[0], &oid[0], &val[0] );
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000599 if( ret != 0 )
600 goto exit;
601
Hanno Becker1e11f212019-03-04 14:43:43 +0000602 ret = x509_set_sequence_iterate( &p[1], (const unsigned char **) &set[1],
603 end[1], &oid[1], &val[1] );
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000604 if( ret != 0 )
605 goto exit;
606
Hanno Becker1e11f212019-03-04 14:43:43 +0000607 if( oid[0].len != oid[1].len ||
Piotr Nowickie3c4ee52020-06-23 12:59:56 +0200608 mbedtls_platform_memequal( oid[0].p, oid[1].p, oid[1].len ) != 0 )
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000609 {
610 return( 1 );
611 }
612
Hanno Becker1e11f212019-03-04 14:43:43 +0000613 if( x509_string_cmp( &val[0], &val[1] ) != 0 )
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000614 return( 1 );
615
Hanno Becker1e11f212019-03-04 14:43:43 +0000616 next_merged = ( set[0] != p[0] );
617 if( next_merged != ( set[1] != p[1] ) )
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000618 return( 1 );
619
Hanno Becker67284cc2019-02-21 14:31:51 +0000620 if( abort_check != NULL )
621 {
Hanno Becker1e11f212019-03-04 14:43:43 +0000622 ret = abort_check( abort_check_ctx, &oid[0], &val[0],
Hanno Becker6b378122019-02-23 10:20:14 +0000623 next_merged );
Hanno Becker67284cc2019-02-21 14:31:51 +0000624 if( ret != 0 )
625 return( ret );
626 }
627
Hanno Becker1e11f212019-03-04 14:43:43 +0000628 if( p[0] == end[0] && p[1] == end[1] )
Hanno Beckera3a2ca12019-02-20 12:42:07 +0000629 break;
630 }
631
632exit:
633 if( ret < 0 )
634 ret += MBEDTLS_ERR_X509_INVALID_NAME;
635
636 return( ret );
637}
638
Hanno Beckerc6573a22019-02-23 09:13:17 +0000639static int x509_get_name_cb( void *ctx,
640 mbedtls_x509_buf *oid,
641 mbedtls_x509_buf *val,
642 int next_merged )
643{
644 mbedtls_x509_name **cur_ptr = (mbedtls_x509_name**) ctx;
645 mbedtls_x509_name *cur = *cur_ptr;
646
647 if( cur->oid.p != NULL )
648 {
649 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
650 if( cur->next == NULL )
651 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
652
653 cur = cur->next;
654 }
655
656 cur->oid = *oid;
657 cur->val = *val;
658 cur->next_merged = next_merged;
659
660 *cur_ptr = cur;
661 return( 0 );
662}
Hanno Becker6b378122019-02-23 10:20:14 +0000663
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300664static int mbedtls_x509_get_name( unsigned char *p,
Hanno Becker1e11f212019-03-04 14:43:43 +0000665 size_t len,
Hanno Becker6b378122019-02-23 10:20:14 +0000666 mbedtls_x509_name *cur )
667{
Hanno Becker1e11f212019-03-04 14:43:43 +0000668 mbedtls_x509_buf_raw name_buf = { p, len };
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200669 mbedtls_platform_memset( cur, 0, sizeof( mbedtls_x509_name ) );
Hanno Becker1e11f212019-03-04 14:43:43 +0000670 return( mbedtls_x509_name_cmp_raw( &name_buf, &name_buf,
671 x509_get_name_cb,
672 &cur ) );
Hanno Becker6b378122019-02-23 10:20:14 +0000673}
674
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300675#if ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) || \
676 defined(MBEDTLS_X509_CRL_PARSE_C)
Janos Follath87c98072017-02-03 12:36:59 +0000677static int x509_parse_int( unsigned char **p, size_t n, int *res )
678{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000679 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000680
681 for( ; n > 0; --n )
682 {
683 if( ( **p < '0') || ( **p > '9' ) )
684 return ( MBEDTLS_ERR_X509_INVALID_DATE );
685
Rich Evans7d5a55a2015-02-13 11:48:02 +0000686 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000687 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000688 }
Janos Follath87c98072017-02-03 12:36:59 +0000689
690 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000691}
692
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000693static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100694{
695 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000696 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100697
Hanno Becker61937d42017-04-26 15:01:23 +0100698 CHECK_RANGE( 0, 9999, t->year );
699 CHECK_RANGE( 0, 23, t->hour );
700 CHECK_RANGE( 0, 59, t->min );
701 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100702
Hanno Becker61937d42017-04-26 15:01:23 +0100703 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100704 {
705 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000706 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100707 break;
708 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000709 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100710 break;
711 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000712 if( ( !( t->year % 4 ) && t->year % 100 ) ||
713 !( t->year % 400 ) )
714 month_len = 29;
715 else
716 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100717 break;
718 default:
719 return( ret );
720 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000721 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100722
723 return( 0 );
724}
725
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726/*
Janos Follath87c98072017-02-03 12:36:59 +0000727 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
728 * field.
729 */
730static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100731 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000732{
733 int ret;
734
735 /*
736 * Minimum length is 10 or 12 depending on yearlen
737 */
738 if ( len < yearlen + 8 )
739 return ( MBEDTLS_ERR_X509_INVALID_DATE );
740 len -= yearlen + 8;
741
742 /*
743 * Parse year, month, day, hour, minute
744 */
Hanno Becker61937d42017-04-26 15:01:23 +0100745 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000746 if ( 2 == yearlen )
747 {
Hanno Becker61937d42017-04-26 15:01:23 +0100748 if ( tm->year < 50 )
749 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000750
Hanno Becker61937d42017-04-26 15:01:23 +0100751 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000752 }
753
Hanno Becker61937d42017-04-26 15:01:23 +0100754 CHECK( x509_parse_int( p, 2, &tm->mon ) );
755 CHECK( x509_parse_int( p, 2, &tm->day ) );
756 CHECK( x509_parse_int( p, 2, &tm->hour ) );
757 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000758
759 /*
760 * Parse seconds if present
761 */
762 if ( len >= 2 )
763 {
Hanno Becker61937d42017-04-26 15:01:23 +0100764 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000765 len -= 2;
766 }
767 else
768 return ( MBEDTLS_ERR_X509_INVALID_DATE );
769
770 /*
771 * Parse trailing 'Z' if present
772 */
773 if ( 1 == len && 'Z' == **p )
774 {
775 (*p)++;
776 len--;
777 }
778
779 /*
780 * We should have parsed all characters at this point
781 */
782 if ( 0 != len )
783 return ( MBEDTLS_ERR_X509_INVALID_DATE );
784
Hanno Becker61937d42017-04-26 15:01:23 +0100785 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000786
787 return ( 0 );
788}
789
790/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 * Time ::= CHOICE {
792 * utcTime UTCTime,
793 * generalTime GeneralizedTime }
794 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300795static int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100796 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797{
798 int ret;
Janos Follath87c98072017-02-03 12:36:59 +0000799 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 unsigned char tag;
801
802 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_X509_INVALID_DATE +
804 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805
806 tag = **p;
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000809 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000811 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_X509_INVALID_DATE +
814 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Janos Follath87c98072017-02-03 12:36:59 +0000815
816 (*p)++;
817 ret = mbedtls_asn1_get_len( p, end, &len );
818
819 if( ret != 0 )
820 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
821
Hanno Becker61937d42017-04-26 15:01:23 +0100822 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823}
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300824#endif /* ( !defined(MBEDTLS_X509_CRT_REMOVE_TIME) && defined(MBEDTLS_X509_CRT_PARSE_C) ) ||
825 defined(MBEDTLS_X509_CRL_PARSE_C) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300827static int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200828{
829 int ret;
830 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100831 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832
833 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
835 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
Andres AG4bdbe092016-09-19 16:58:45 +0100837 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
840 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841
Andres AG4bdbe092016-09-19 16:58:45 +0100842 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 sig->len = len;
844 sig->p = *p;
845
846 *p += len;
847
848 return( 0 );
849}
850
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300851static int mbedtls_x509_get_sig_alg_raw( unsigned char **p, unsigned char const *end,
Hanno Beckerb59d3f12019-02-22 17:49:34 +0000852 mbedtls_md_type_t *md_alg,
853 mbedtls_pk_type_t *pk_alg,
854 void **sig_opts )
855{
856 int ret;
857 mbedtls_asn1_buf alg, params;
858 ret = mbedtls_asn1_get_alg( p, end, &alg, &params );
859 if( ret != 0 )
860 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
861
862 return( mbedtls_x509_get_sig_alg( &alg, &params, md_alg,
863 pk_alg, sig_opts ) );
864}
865
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100866/*
867 * Get signature algorithm from alg OID and optional parameters
868 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300869static int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200871 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872{
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100873 int ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
876 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
879 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100882
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200883 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200884 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200885 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200888 md_alg,
889 &pss_opts->mgf1_hash_id,
890 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100891 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100894 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200895 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100896
Hanno Becker1898b682019-02-22 16:03:29 +0000897 if( sig_opts != NULL )
898 *sig_opts = (void *) pss_opts;
899 else
900 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100901 }
902 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100904 {
905 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100907 sig_params->len != 0 )
Hanno Becker1898b682019-02-22 16:03:29 +0000908 return( MBEDTLS_ERR_X509_INVALID_ALG );
909
910 if( sig_opts != NULL )
911 *sig_opts = NULL;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100912 }
913
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 return( 0 );
915}
916
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300917#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918/*
919 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800920 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300922static int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker2f472142019-02-12 11:52:10 +0000923 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924{
925 int ret;
926 size_t len;
927
Hanno Beckerc74ce442019-02-11 14:33:36 +0000928 /* Extension structure use EXPLICIT tagging. That is, the actual
929 * `Extensions` structure is wrapped by a tag-length pair using
930 * the respective context-specific tag. */
Hanno Becker2f472142019-02-12 11:52:10 +0000931 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
932 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
933 if( ret != 0 )
934 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
Hanno Becker2f472142019-02-12 11:52:10 +0000936 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
937 ext->p = *p;
938 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 /*
941 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
944 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
945 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947 if( end != *p + len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
949 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951 return( 0 );
952}
Teppo Järvelinf69e6412019-09-03 16:50:17 +0300953#endif /* defined(MBEDTLS_X509_CRL_PARSE_C) */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954/*
955 * Store the name in printable form into buf; no more
956 * than size characters will be written
957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959{
960 int ret;
961 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000962 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966
967 memset( s, 0, sizeof( s ) );
968
969 name = dn;
970 p = buf;
971 n = size;
972
973 while( name != NULL )
974 {
975 if( !name->oid.p )
976 {
977 name = name->next;
978 continue;
979 }
980
981 if( name != dn )
982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200984 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 }
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200993 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995 for( i = 0; i < name->val.len; i++ )
996 {
997 if( i >= sizeof( s ) - 1 )
998 break;
999
1000 c = name->val.p[i];
1001 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
1002 s[i] = '?';
1003 else s[i] = c;
1004 }
1005 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001007 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001008
1009 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 name = name->next;
1011 }
1012
1013 return( (int) ( size - n ) );
1014}
1015
1016/*
1017 * Store the serial in printable form into buf; no more
1018 * than size characters will be written
1019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021{
1022 int ret;
1023 size_t i, n, nr;
1024 char *p;
1025
1026 p = buf;
1027 n = size;
1028
1029 nr = ( serial->len <= 32 )
1030 ? serial->len : 28;
1031
1032 for( i = 0; i < nr; i++ )
1033 {
1034 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
1035 continue;
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001039 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 }
1041
1042 if( nr != serial->len )
1043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001045 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 }
1047
1048 return( (int) ( size - n ) );
1049}
1050
Hanno Becker02a21932019-06-10 15:08:43 +01001051#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001053 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001054 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +03001055static int mbedtls_x509_sig_alg_gets( char *buf, size_t size, mbedtls_pk_type_t pk_alg,
Hanno Becker83cd8672019-02-21 17:13:46 +00001056 mbedtls_md_type_t md_alg, const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001057{
1058 int ret;
1059 char *p = buf;
1060 size_t n = size;
1061 const char *desc = NULL;
Hanno Becker83cd8672019-02-21 17:13:46 +00001062 mbedtls_x509_buf sig_oid;
1063 mbedtls_md_type_t tmp_md_alg = md_alg;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001064
Hanno Becker83cd8672019-02-21 17:13:46 +00001065#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1066 /* The hash for RSASSA is determined by the algorithm parameters;
1067 * in the OID list, the hash is set to MBEDTLS_MD_NONE. */
1068 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
1069 tmp_md_alg = MBEDTLS_MD_NONE;
1070#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1071
1072 sig_oid.tag = MBEDTLS_ASN1_OID;
1073 ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, tmp_md_alg,
1074 (const char**) &sig_oid.p,
1075 &sig_oid.len );
1076 if( ret == 0 &&
1077 mbedtls_oid_get_sig_alg_desc( &sig_oid, &desc ) == 0 )
1078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 ret = mbedtls_snprintf( p, n, "%s", desc );
Hanno Becker83cd8672019-02-21 17:13:46 +00001080 }
1081 else
1082 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001083 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
1086 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 const mbedtls_pk_rsassa_pss_options *pss_opts;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001089 mbedtls_md_handle_t md_info, mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 md_info = mbedtls_md_info_from_type( md_alg );
1094 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
1097 md_info ? mbedtls_md_get_name( md_info ) : "???",
1098 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001099 pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001100 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001101 }
1102#else
1103 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001104 ((void) md_alg);
1105 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001107
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001108 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001109}
1110
1111/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 * Helper for writing "RSA key size", "EC key size", etc
1113 */
Teppo Järvelinf69e6412019-09-03 16:50:17 +03001114static int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115{
1116 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +02001117 size_t n = buf_size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 int ret;
1119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001121 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122
1123 return( 0 );
1124}
Teppo Järvelinf69e6412019-09-03 16:50:17 +03001125#endif /* !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001126
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001127#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001128/*
1129 * Set the time structure to the current time.
1130 * Return 0 on success, non-zero on failure.
1131 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001132static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001133{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +00001134 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +01001135 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001136 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137
SimonBd5800b72016-04-26 07:43:27 +01001138 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +01001139 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001140
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001141 if( lt == NULL )
1142 ret = -1;
1143 else
1144 {
1145 now->year = lt->tm_year + 1900;
1146 now->mon = lt->tm_mon + 1;
1147 now->day = lt->tm_mday;
1148 now->hour = lt->tm_hour;
1149 now->min = lt->tm_min;
1150 now->sec = lt->tm_sec;
1151 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001152
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +02001153 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001154}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001156/*
1157 * Return 0 if before <= after, 1 otherwise
1158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001160{
1161 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 return( 1 );
1163
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001164 if( before->year == after->year &&
1165 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 return( 1 );
1167
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001168 if( before->year == after->year &&
1169 before->mon == after->mon &&
1170 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 return( 1 );
1172
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001173 if( before->year == after->year &&
1174 before->mon == after->mon &&
1175 before->day == after->day &&
1176 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 return( 1 );
1178
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001179 if( before->year == after->year &&
1180 before->mon == after->mon &&
1181 before->day == after->day &&
1182 before->hour == after->hour &&
1183 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 return( 1 );
1185
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001186 if( before->year == after->year &&
1187 before->mon == after->mon &&
1188 before->day == after->day &&
1189 before->hour == after->hour &&
1190 before->min == after->min &&
1191 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 return( 1 );
1193
1194 return( 0 );
1195}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001196
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001197int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001200
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001201 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001202 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001203
1204 return( x509_check_time( &now, to ) );
1205}
1206
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001207int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001210
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001211 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001212 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001213
1214 return( x509_check_time( from, &now ) );
1215}
Hanno Beckerc00ccea2019-06-07 17:03:40 +01001216#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217
Hanno Becker2bcc7642019-02-26 19:01:00 +00001218void mbedtls_x509_name_free( mbedtls_x509_name *name )
1219{
1220 while( name != NULL )
1221 {
1222 mbedtls_x509_name *next = name->next;
1223 mbedtls_platform_zeroize( name, sizeof( *name ) );
1224 mbedtls_free( name );
1225 name = next;
1226 }
1227}
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001230
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001231#include "mbedtls/x509_crt.h"
1232#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001233
1234/*
1235 * Checkup routine
1236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001238{
Junhwan Park60ee28b2018-10-17 21:01:08 +09001239 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001240#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001241 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_x509_crt cacert;
1243 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001244
1245 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001247
Junhwan Park60ee28b2018-10-17 21:01:08 +09001248 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001252 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001253 if( ret != 0 )
1254 {
1255 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001257
Junhwan Park60ee28b2018-10-17 21:01:08 +09001258 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001259 }
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001262 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001263 if( ret != 0 )
1264 {
1265 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001267
Junhwan Park60ee28b2018-10-17 21:01:08 +09001268 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001269 }
1270
1271 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001273
Hanno Becker392a8d02019-09-03 09:09:58 +01001274 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL,
1275#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
1276 NULL,
1277#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
1278 &flags
Hanno Becker9ec3fe02019-07-01 17:36:12 +01001279#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Hanno Becker392a8d02019-09-03 09:09:58 +01001280 , NULL, NULL
Hanno Becker9ec3fe02019-07-01 17:36:12 +01001281#endif
Hanno Becker392a8d02019-09-03 09:09:58 +01001282 );
Hanno Becker9ec3fe02019-07-01 17:36:12 +01001283
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001284 if( ret != 0 )
1285 {
1286 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001288
Junhwan Park60ee28b2018-10-17 21:01:08 +09001289 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001290 }
1291
1292 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001294
Junhwan Park60ee28b2018-10-17 21:01:08 +09001295cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_x509_crt_free( &cacert );
1297 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001298#else
1299 ((void) verbose);
Andrzej Kurek825ebd42020-05-18 11:47:25 -04001300#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park60ee28b2018-10-17 21:01:08 +09001301 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_X509_USE_C */