blob: 5d8338418c51dc16811dcc92327af91442fbd9e3 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#else
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <stdio.h>
50#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010051#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020052#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010053#define mbedtls_printf printf
54#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#endif
56
Simon Butcherb5b6af22016-07-13 14:46:18 +010057#if defined(MBEDTLS_HAVE_TIME)
58#include "mbedtls/platform_time.h"
59#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000060#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010061#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <time.h>
63#endif
64
Hanno Becker1eeca412018-10-15 12:01:35 +010065#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
66#define CHECK_RANGE(min, max, val) \
67 do \
68 { \
69 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
70 { \
71 return( ret ); \
72 } \
73 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000074
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075/*
76 * CertificateSerialNumber ::= INTEGER
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
79 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080{
Janos Follath865b3eb2019-12-16 11:46:15 +000081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
83 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010084 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
85 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
88 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010089 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
90 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
92 serial->tag = *(*p)++;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010095 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97 serial->p = *p;
98 *p += serial->len;
99
100 return( 0 );
101}
102
103/* Get an algorithm identifier without parameters (eg for signatures)
104 *
105 * AlgorithmIdentifier ::= SEQUENCE {
106 * algorithm OBJECT IDENTIFIER,
107 * parameters ANY DEFINED BY algorithm OPTIONAL }
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
110 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111{
Janos Follath865b3eb2019-12-16 11:46:15 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100115 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
117 return( 0 );
118}
119
120/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100121 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
124 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100125{
Janos Follath865b3eb2019-12-16 11:46:15 +0000126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100129 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130
131 return( 0 );
132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100135/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100136 * HashAlgorithm ::= AlgorithmIdentifier
137 *
138 * AlgorithmIdentifier ::= SEQUENCE {
139 * algorithm OBJECT IDENTIFIER,
140 * parameters ANY DEFINED BY algorithm OPTIONAL }
141 *
142 * For HashAlgorithm, parameters MUST be NULL or absent.
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144static 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 +0100145{
Janos Follath865b3eb2019-12-16 11:46:15 +0000146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100147 unsigned char *p;
148 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100150 size_t len;
151
152 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100154 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
155 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100156
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400157 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100158 end = p + alg->len;
159
160 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100161 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
162 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100163
164 /* Parse md_oid */
165 md_oid.tag = *p;
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100168 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100169
170 md_oid.p = p;
171 p += md_oid.len;
172
173 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100175 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100176
177 /* Make sure params is absent of NULL */
178 if( p == end )
179 return( 0 );
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100182 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100183
184 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100185 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
186 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187
188 return( 0 );
189}
190
191/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100192 * RSASSA-PSS-params ::= SEQUENCE {
193 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
194 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
195 * saltLength [2] INTEGER DEFAULT 20,
196 * trailerField [3] INTEGER DEFAULT 1 }
197 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200198 *
199 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
200 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
201 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
204 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200205 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100206{
Janos Follath865b3eb2019-12-16 11:46:15 +0000207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100209 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100210 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100212
213 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 *md_alg = MBEDTLS_MD_SHA1;
215 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100216 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217
218 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100220 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
221 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100222
223 p = (unsigned char *) params->p;
224 end = p + params->len;
225
226 if( p == end )
227 return( 0 );
228
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100229 /*
230 * HashAlgorithm
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
233 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100235 end2 = p + len;
236
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100237 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100239 return( ret );
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100242 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100243
244 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100245 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
246 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100249 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 if( p == end )
252 return( 0 );
253
254 /*
255 * MaskGenAlgorithm
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
258 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100260 end2 = p + len;
261
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100262 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100264 return( ret );
265
266 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100268 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
269 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100270
271 /* Parse HashAlgorithm */
272 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
273 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100274
275 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100276 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
277 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100280 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100281
282 if( p == end )
283 return( 0 );
284
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100285 /*
286 * salt_len
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
289 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291 end2 = p + len;
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100294 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100295
296 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100297 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
298 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100299 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100301 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302
303 if( p == end )
304 return( 0 );
305
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100306 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200307 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
310 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200312 int trailer_field;
313
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100314 end2 = p + len;
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100317 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100318
319 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100320 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
321 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200322
323 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100327 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100328
329 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100330 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
331 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332
333 return( 0 );
334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100336
337/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 * AttributeTypeAndValue ::= SEQUENCE {
339 * type AttributeType,
340 * value AttributeValue }
341 *
342 * AttributeType ::= OBJECT IDENTIFIER
343 *
344 * AttributeValue ::= ANY DEFINED BY AttributeType
345 */
346static int x509_get_attr_type_value( unsigned char **p,
347 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349{
Janos Follath865b3eb2019-12-16 11:46:15 +0000350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_x509_buf *oid;
353 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
356 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100357 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
Hanno Becker12f62fb2019-02-12 17:22:36 +0000359 end = *p + len;
360
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100362 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
363 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
365 oid = &cur->oid;
366 oid->tag = **p;
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100369 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
371 oid->p = *p;
372 *p += oid->len;
373
374 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100375 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
376 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
379 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
380 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
381 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100382 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
383 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
385 val = &cur->val;
386 val->tag = *(*p)++;
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100389 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390
391 val->p = *p;
392 *p += val->len;
393
Hanno Becker12f62fb2019-02-12 17:22:36 +0000394 if( *p != end )
395 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100396 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
397 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000398 }
399
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 cur->next = NULL;
401
402 return( 0 );
403}
404
405/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000406 * Name ::= CHOICE { -- only one possibility for now --
407 * rdnSequence RDNSequence }
408 *
409 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
410 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 * RelativeDistinguishedName ::=
412 * SET OF AttributeTypeAndValue
413 *
414 * AttributeTypeAndValue ::= SEQUENCE {
415 * type AttributeType,
416 * value AttributeValue }
417 *
418 * AttributeType ::= OBJECT IDENTIFIER
419 *
420 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200421 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000422 * The data structure is optimized for the common case where each RDN has only
423 * one element, which is represented as a list of AttributeTypeAndValue.
424 * For the general case we still use a flat list, but we mark elements of the
425 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 * this list, eg mbedtls_x509_dn_gets().
David Horstmann8c176b42022-10-04 18:12:06 +0100427 *
428 * On success, this function allocates a linked list starting at cur->next
429 * that must later be free'd by the caller using mbedtls_free(). In error
430 * cases, this function frees all allocated memory internally and the caller
431 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
434 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435{
Janos Follath865b3eb2019-12-16 11:46:15 +0000436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200437 size_t set_len;
438 const unsigned char *end_set;
David Horstmann8c176b42022-10-04 18:12:06 +0100439 mbedtls_x509_name *head = cur;
440 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100442 /* don't use recursion, we'd risk stack overflow if not optimized */
443 while( 1 )
444 {
445 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000446 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
449 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
David Horstmann8c176b42022-10-04 18:12:06 +0100450 {
451 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret );
452 goto error;
453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100455 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000457 while( 1 )
458 {
459 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
David Horstmann8c176b42022-10-04 18:12:06 +0100460 goto error;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000462 if( *p == end_set )
463 break;
464
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200465 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000466 cur->next_merged = 1;
467
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200468 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000469
470 if( cur->next == NULL )
David Horstmann8c176b42022-10-04 18:12:06 +0100471 {
472 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
473 goto error;
474 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000475
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000476 cur = cur->next;
477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100479 /*
480 * continue until end of SEQUENCE is reached
481 */
482 if( *p == end )
483 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200485 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100487 if( cur->next == NULL )
David Horstmann8c176b42022-10-04 18:12:06 +0100488 {
489 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
490 goto error;
491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100493 cur = cur->next;
494 }
David Horstmann8c176b42022-10-04 18:12:06 +0100495
496error:
497 prev = NULL;
498
499 /* Skip the first element as we did not allocate it */
500 allocated = head->next;
501
502 /* Make sure we cannot be followed along this list */
503 head->next = NULL;
504
505 while( allocated != NULL )
506 {
507 prev = allocated;
508 allocated = allocated->next;
509
510 mbedtls_platform_zeroize( prev, sizeof( *prev ) );
511 mbedtls_free( prev );
512 }
513
514 mbedtls_platform_zeroize( head, sizeof( *head ) );
515
516 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517}
518
Janos Follath87c98072017-02-03 12:36:59 +0000519static int x509_parse_int( unsigned char **p, size_t n, int *res )
520{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000521 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000522
523 for( ; n > 0; --n )
524 {
525 if( ( **p < '0') || ( **p > '9' ) )
526 return ( MBEDTLS_ERR_X509_INVALID_DATE );
527
Rich Evans7d5a55a2015-02-13 11:48:02 +0000528 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000529 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000530 }
Janos Follath87c98072017-02-03 12:36:59 +0000531
532 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000533}
534
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000535static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100536{
537 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000538 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100539
Hanno Becker61937d42017-04-26 15:01:23 +0100540 CHECK_RANGE( 0, 9999, t->year );
541 CHECK_RANGE( 0, 23, t->hour );
542 CHECK_RANGE( 0, 59, t->min );
543 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100544
Hanno Becker61937d42017-04-26 15:01:23 +0100545 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100546 {
547 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000548 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100549 break;
550 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000551 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100552 break;
553 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000554 if( ( !( t->year % 4 ) && t->year % 100 ) ||
555 !( t->year % 400 ) )
556 month_len = 29;
557 else
558 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100559 break;
560 default:
561 return( ret );
562 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000563 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100564
565 return( 0 );
566}
567
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568/*
Janos Follath87c98072017-02-03 12:36:59 +0000569 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
570 * field.
571 */
572static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100573 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000574{
Janos Follath865b3eb2019-12-16 11:46:15 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000576
577 /*
578 * Minimum length is 10 or 12 depending on yearlen
579 */
580 if ( len < yearlen + 8 )
581 return ( MBEDTLS_ERR_X509_INVALID_DATE );
582 len -= yearlen + 8;
583
584 /*
585 * Parse year, month, day, hour, minute
586 */
Hanno Becker61937d42017-04-26 15:01:23 +0100587 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000588 if ( 2 == yearlen )
589 {
Hanno Becker61937d42017-04-26 15:01:23 +0100590 if ( tm->year < 50 )
591 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000592
Hanno Becker61937d42017-04-26 15:01:23 +0100593 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000594 }
595
Hanno Becker61937d42017-04-26 15:01:23 +0100596 CHECK( x509_parse_int( p, 2, &tm->mon ) );
597 CHECK( x509_parse_int( p, 2, &tm->day ) );
598 CHECK( x509_parse_int( p, 2, &tm->hour ) );
599 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000600
601 /*
602 * Parse seconds if present
603 */
604 if ( len >= 2 )
605 {
Hanno Becker61937d42017-04-26 15:01:23 +0100606 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000607 len -= 2;
608 }
609 else
610 return ( MBEDTLS_ERR_X509_INVALID_DATE );
611
612 /*
613 * Parse trailing 'Z' if present
614 */
615 if ( 1 == len && 'Z' == **p )
616 {
617 (*p)++;
618 len--;
619 }
620
621 /*
622 * We should have parsed all characters at this point
623 */
624 if ( 0 != len )
625 return ( MBEDTLS_ERR_X509_INVALID_DATE );
626
Hanno Becker61937d42017-04-26 15:01:23 +0100627 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000628
629 return ( 0 );
630}
631
632/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 * Time ::= CHOICE {
634 * utcTime UTCTime,
635 * generalTime GeneralizedTime }
636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100638 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639{
Janos Follath865b3eb2019-12-16 11:46:15 +0000640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000641 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642 unsigned char tag;
643
644 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100645 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
646 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647
648 tag = **p;
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000651 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000653 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100655 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
656 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000657
658 (*p)++;
659 ret = mbedtls_asn1_get_len( p, end, &len );
660
661 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100662 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000663
Hanno Becker61937d42017-04-26 15:01:23 +0100664 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665}
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668{
Janos Follath865b3eb2019-12-16 11:46:15 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100671 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
673 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100674 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
675 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
Andres AG4bdbe092016-09-19 16:58:45 +0100677 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100680 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
Andres AG4bdbe092016-09-19 16:58:45 +0100682 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 sig->len = len;
684 sig->p = *p;
685
686 *p += len;
687
688 return( 0 );
689}
690
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100691/*
692 * Get signature algorithm from alg OID and optional parameters
693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
695 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200696 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697{
Janos Follath865b3eb2019-12-16 11:46:15 +0000698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200700 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100704 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
707 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100710
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200711 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200712 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200713 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200716 md_alg,
717 &pss_opts->mgf1_hash_id,
718 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100719 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100722 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200723 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100724
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200725 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100726 }
727 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100729 {
730 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100732 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100734 }
735
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 return( 0 );
737}
738
739/*
740 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800741 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000744 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745{
Janos Follath865b3eb2019-12-16 11:46:15 +0000746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 size_t len;
748
Hanno Becker3cddba82019-02-11 14:33:36 +0000749 /* Extension structure use EXPLICIT tagging. That is, the actual
750 * `Extensions` structure is wrapped by a tag-length pair using
751 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000752 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
753 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
754 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756
Hanno Becker6ccfb182019-02-12 11:52:10 +0000757 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
758 ext->p = *p;
759 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760
761 /*
762 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767
768 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100769 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
770 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771
772 return( 0 );
773}
774
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775/*
776 * Store the name in printable form into buf; no more
777 * than size characters will be written
778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780{
Janos Follath865b3eb2019-12-16 11:46:15 +0000781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100782 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000783 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200787
788 memset( s, 0, sizeof( s ) );
789
790 name = dn;
791 p = buf;
792 n = size;
793
794 while( name != NULL )
795 {
796 if( !name->oid.p )
797 {
798 name = name->next;
799 continue;
800 }
801
802 if( name != dn )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200805 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 }
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809
810 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200814 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100816 for( i = 0, j = 0; i < name->val.len; i++, j++ )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 {
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100818 if( j >= sizeof( s ) - 1 )
819 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820
821 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100822 // Special characters requiring escaping, RFC 1779
823 if( c && strchr( ",=+<>#;\"\\", c ) )
824 {
825 if( j + 1 >= sizeof( s ) - 1 )
Werner Lewis1421efa2022-06-27 12:01:22 +0100826 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100827 s[j++] = '\\';
828 }
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900829 if( c < 32 || c >= 127 )
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100830 s[j] = '?';
831 else s[j] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100833 s[j] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200835 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000836
837 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838 name = name->next;
839 }
840
841 return( (int) ( size - n ) );
842}
843
844/*
845 * Store the serial in printable form into buf; no more
846 * than size characters will be written
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849{
Janos Follath865b3eb2019-12-16 11:46:15 +0000850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 size_t i, n, nr;
852 char *p;
853
854 p = buf;
855 n = size;
856
857 nr = ( serial->len <= 32 )
858 ? serial->len : 28;
859
860 for( i = 0; i < nr; i++ )
861 {
862 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
863 continue;
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200867 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 }
869
870 if( nr != serial->len )
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200873 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 }
875
876 return( (int) ( size - n ) );
877}
878
879/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200880 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
883 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200884 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100885{
Janos Follath865b3eb2019-12-16 11:46:15 +0000886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100887 char *p = buf;
888 size_t n = size;
889 const char *desc = NULL;
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100892 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200896 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
899 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 const mbedtls_pk_rsassa_pss_options *pss_opts;
902 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 md_info = mbedtls_md_info_from_type( md_alg );
907 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
910 md_info ? mbedtls_md_get_name( md_info ) : "???",
911 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200912 (unsigned int) pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200913 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100914 }
915#else
916 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200917 ((void) md_alg);
918 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100920
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200921 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100922}
923
924/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 * Helper for writing "RSA key size", "EC key size", etc
926 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200927int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928{
929 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200930 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200934 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 return( 0 );
937}
938
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200939#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200940/*
941 * Set the time structure to the current time.
942 * Return 0 on success, non-zero on failure.
943 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200944static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100945{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000946 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100947 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200948 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
SimonBd5800b72016-04-26 07:43:27 +0100950 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100951 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200952
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200953 if( lt == NULL )
954 ret = -1;
955 else
956 {
957 now->year = lt->tm_year + 1900;
958 now->mon = lt->tm_mon + 1;
959 now->day = lt->tm_mday;
960 now->hour = lt->tm_hour;
961 now->min = lt->tm_min;
962 now->sec = lt->tm_sec;
963 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200964
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200965 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100966}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100968/*
969 * Return 0 if before <= after, 1 otherwise
970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100972{
973 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 return( 1 );
975
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100976 if( before->year == after->year &&
977 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 return( 1 );
979
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100980 if( before->year == after->year &&
981 before->mon == after->mon &&
982 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 return( 1 );
984
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100985 if( before->year == after->year &&
986 before->mon == after->mon &&
987 before->day == after->day &&
988 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 return( 1 );
990
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100991 if( before->year == after->year &&
992 before->mon == after->mon &&
993 before->day == after->day &&
994 before->hour == after->hour &&
995 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 return( 1 );
997
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998 if( before->year == after->year &&
999 before->mon == after->mon &&
1000 before->day == after->day &&
1001 before->hour == after->hour &&
1002 before->min == after->min &&
1003 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 return( 1 );
1005
1006 return( 0 );
1007}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001008
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001009int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001012
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001013 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001014 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015
1016 return( x509_check_time( &now, to ) );
1017}
1018
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001019int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001023 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001024 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025
1026 return( x509_check_time( from, &now ) );
1027}
1028
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001029#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001030
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001031int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032{
1033 ((void) to);
1034 return( 0 );
1035}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001036
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001037int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001038{
1039 ((void) from);
1040 return( 0 );
1041}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001042#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001046#include "mbedtls/x509_crt.h"
1047#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001048
1049/*
1050 * Checkup routine
1051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001053{
Junhwan Park39bdab72018-10-17 21:01:08 +09001054 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001055#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001056 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_x509_crt cacert;
1058 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001059
1060 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001062
Junhwan Park39bdab72018-10-17 21:01:08 +09001063 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001067 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001068 if( ret != 0 )
1069 {
1070 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001072
Junhwan Park39bdab72018-10-17 21:01:08 +09001073 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001074 }
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001077 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001078 if( ret != 0 )
1079 {
1080 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001082
Junhwan Park39bdab72018-10-17 21:01:08 +09001083 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001084 }
1085
1086 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001090 if( ret != 0 )
1091 {
1092 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001094
Junhwan Park39bdab72018-10-17 21:01:08 +09001095 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001096 }
1097
1098 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001100
Junhwan Park39bdab72018-10-17 21:01:08 +09001101cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_x509_crt_free( &cacert );
1103 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001104#else
1105 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001106#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park39bdab72018-10-17 21:01:08 +09001107 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001108}
1109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_X509_USE_C */