blob: 54c8666d235b75aa3e3449ae5dae637776abd894 [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é-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#if defined(MBEDTLS_HAVE_TIME)
49#include "mbedtls/platform_time.h"
50#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000051#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010052#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include <time.h>
54#endif
55
Hanno Becker1eeca412018-10-15 12:01:35 +010056#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
57#define CHECK_RANGE(min, max, val) \
58 do \
59 { \
60 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
61 { \
62 return( ret ); \
63 } \
64 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066/*
67 * CertificateSerialNumber ::= INTEGER
68 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
70 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071{
Janos Follath865b3eb2019-12-16 11:46:15 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
74 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010075 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
79 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010080 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
81 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
83 serial->tag = *(*p)++;
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010086 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
88 serial->p = *p;
89 *p += serial->len;
90
91 return( 0 );
92}
93
94/* Get an algorithm identifier without parameters (eg for signatures)
95 *
96 * AlgorithmIdentifier ::= SEQUENCE {
97 * algorithm OBJECT IDENTIFIER,
98 * parameters ANY DEFINED BY algorithm OPTIONAL }
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
101 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102{
Janos Follath865b3eb2019-12-16 11:46:15 +0000103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100106 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
108 return( 0 );
109}
110
111/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100112 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
115 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100116{
Janos Follath865b3eb2019-12-16 11:46:15 +0000117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100120 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121
122 return( 0 );
123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100127 * HashAlgorithm ::= AlgorithmIdentifier
128 *
129 * AlgorithmIdentifier ::= SEQUENCE {
130 * algorithm OBJECT IDENTIFIER,
131 * parameters ANY DEFINED BY algorithm OPTIONAL }
132 *
133 * For HashAlgorithm, parameters MUST be NULL or absent.
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135static 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 +0100136{
Janos Follath865b3eb2019-12-16 11:46:15 +0000137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100138 unsigned char *p;
139 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100141 size_t len;
142
143 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
146 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100147
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400148 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100149 end = p + alg->len;
150
151 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100152 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
153 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100154
155 /* Parse md_oid */
156 md_oid.tag = *p;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100159 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100160
161 md_oid.p = p;
162 p += md_oid.len;
163
164 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100166 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100167
168 /* Make sure params is absent of NULL */
169 if( p == end )
170 return( 0 );
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100173 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100174
175 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100176 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
177 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100178
179 return( 0 );
180}
181
182/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100183 * RSASSA-PSS-params ::= SEQUENCE {
184 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
185 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
186 * saltLength [2] INTEGER DEFAULT 20,
187 * trailerField [3] INTEGER DEFAULT 1 }
188 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200189 *
190 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
191 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000192 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
195 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200196 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100197{
Janos Follath865b3eb2019-12-16 11:46:15 +0000198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100199 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100200 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100203
204 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 *md_alg = MBEDTLS_MD_SHA1;
206 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100207 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208
209 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100211 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
212 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213
214 p = (unsigned char *) params->p;
215 end = p + params->len;
216
217 if( p == end )
218 return( 0 );
219
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100220 /*
221 * HashAlgorithm
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
224 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100225 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100226 end2 = p + len;
227
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100228 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230 return( ret );
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100233 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234
235 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100236 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
237 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100240 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100241
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100242 if( p == end )
243 return( 0 );
244
245 /*
246 * MaskGenAlgorithm
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
249 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 end2 = p + len;
252
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100253 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100255 return( ret );
256
257 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100259 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
260 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100261
262 /* Parse HashAlgorithm */
263 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
264 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265
266 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100267 return( MBEDTLS_ERROR_ADD( 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 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100271 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100272
273 if( p == end )
274 return( 0 );
275
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100276 /*
277 * salt_len
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 | 2 ) ) == 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100285 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100286
287 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100288 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100292 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293
294 if( p == end )
295 return( 0 );
296
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200298 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
301 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200303 int trailer_field;
304
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305 end2 = p + len;
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100308 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309
310 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100311 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
312 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200313
314 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100319
320 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
322 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100323
324 return( 0 );
325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100327
328/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 * AttributeTypeAndValue ::= SEQUENCE {
330 * type AttributeType,
331 * value AttributeValue }
332 *
333 * AttributeType ::= OBJECT IDENTIFIER
334 *
335 * AttributeValue ::= ANY DEFINED BY AttributeType
336 */
337static int x509_get_attr_type_value( unsigned char **p,
338 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340{
Janos Follath865b3eb2019-12-16 11:46:15 +0000341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_x509_buf *oid;
344 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100348 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
Hanno Becker12f62fb2019-02-12 17:22:36 +0000350 end = *p + len;
351
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100353 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
354 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
356 oid = &cur->oid;
357 oid->tag = **p;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100360 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362 oid->p = *p;
363 *p += oid->len;
364
365 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100366 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
367 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
370 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
371 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
372 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100373 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
374 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376 val = &cur->val;
377 val->tag = *(*p)++;
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100380 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382 val->p = *p;
383 *p += val->len;
384
Hanno Becker12f62fb2019-02-12 17:22:36 +0000385 if( *p != end )
386 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100387 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000389 }
390
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 cur->next = NULL;
392
393 return( 0 );
394}
395
396/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000397 * Name ::= CHOICE { -- only one possibility for now --
398 * rdnSequence RDNSequence }
399 *
400 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
401 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 * RelativeDistinguishedName ::=
403 * SET OF AttributeTypeAndValue
404 *
405 * AttributeTypeAndValue ::= SEQUENCE {
406 * type AttributeType,
407 * value AttributeValue }
408 *
409 * AttributeType ::= OBJECT IDENTIFIER
410 *
411 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200412 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000413 * The data structure is optimized for the common case where each RDN has only
414 * one element, which is represented as a list of AttributeTypeAndValue.
415 * For the general case we still use a flat list, but we mark elements of the
416 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 * this list, eg mbedtls_x509_dn_gets().
David Horstmann8c176b42022-10-04 18:12:06 +0100418 *
David Horstmann5ad5e162022-10-17 18:10:23 +0100419 * On success, this function may allocate a linked list starting at cur->next
David Horstmann8c176b42022-10-04 18:12:06 +0100420 * that must later be free'd by the caller using mbedtls_free(). In error
421 * cases, this function frees all allocated memory internally and the caller
422 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
425 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426{
Janos Follath865b3eb2019-12-16 11:46:15 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200428 size_t set_len;
429 const unsigned char *end_set;
David Horstmann8c176b42022-10-04 18:12:06 +0100430 mbedtls_x509_name *head = cur;
431 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100433 /* don't use recursion, we'd risk stack overflow if not optimized */
434 while( 1 )
435 {
436 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000437 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
440 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
David Horstmann8c176b42022-10-04 18:12:06 +0100441 {
442 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret );
443 goto error;
444 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100446 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000448 while( 1 )
449 {
450 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
David Horstmann8c176b42022-10-04 18:12:06 +0100451 goto error;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000453 if( *p == end_set )
454 break;
455
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200456 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000457 cur->next_merged = 1;
458
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200459 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000460
461 if( cur->next == NULL )
David Horstmann8c176b42022-10-04 18:12:06 +0100462 {
463 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
464 goto error;
465 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000466
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000467 cur = cur->next;
468 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 /*
471 * continue until end of SEQUENCE is reached
472 */
473 if( *p == end )
474 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200476 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100478 if( cur->next == NULL )
David Horstmann8c176b42022-10-04 18:12:06 +0100479 {
480 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
481 goto error;
482 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100484 cur = cur->next;
485 }
David Horstmann8c176b42022-10-04 18:12:06 +0100486
487error:
David Horstmann8c176b42022-10-04 18:12:06 +0100488 /* Skip the first element as we did not allocate it */
489 allocated = head->next;
490
David Horstmann8c176b42022-10-04 18:12:06 +0100491 while( allocated != NULL )
492 {
493 prev = allocated;
494 allocated = allocated->next;
495
496 mbedtls_platform_zeroize( prev, sizeof( *prev ) );
497 mbedtls_free( prev );
498 }
499
500 mbedtls_platform_zeroize( head, sizeof( *head ) );
501
David Horstmann670a9932022-10-18 18:11:13 +0100502 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503}
504
Janos Follath87c98072017-02-03 12:36:59 +0000505static int x509_parse_int( unsigned char **p, size_t n, int *res )
506{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000507 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000508
509 for( ; n > 0; --n )
510 {
511 if( ( **p < '0') || ( **p > '9' ) )
512 return ( MBEDTLS_ERR_X509_INVALID_DATE );
513
Rich Evans7d5a55a2015-02-13 11:48:02 +0000514 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000515 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000516 }
Janos Follath87c98072017-02-03 12:36:59 +0000517
518 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000519}
520
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000521static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100522{
523 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000524 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100525
Hanno Becker61937d42017-04-26 15:01:23 +0100526 CHECK_RANGE( 0, 9999, t->year );
527 CHECK_RANGE( 0, 23, t->hour );
528 CHECK_RANGE( 0, 59, t->min );
529 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100530
Hanno Becker61937d42017-04-26 15:01:23 +0100531 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100532 {
533 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000534 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100535 break;
536 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000537 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100538 break;
539 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000540 if( ( !( t->year % 4 ) && t->year % 100 ) ||
541 !( t->year % 400 ) )
542 month_len = 29;
543 else
544 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100545 break;
546 default:
547 return( ret );
548 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000549 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100550
551 return( 0 );
552}
553
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554/*
Janos Follath87c98072017-02-03 12:36:59 +0000555 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
556 * field.
557 */
558static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100559 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000560{
Janos Follath865b3eb2019-12-16 11:46:15 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000562
563 /*
564 * Minimum length is 10 or 12 depending on yearlen
565 */
566 if ( len < yearlen + 8 )
567 return ( MBEDTLS_ERR_X509_INVALID_DATE );
568 len -= yearlen + 8;
569
570 /*
571 * Parse year, month, day, hour, minute
572 */
Hanno Becker61937d42017-04-26 15:01:23 +0100573 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000574 if ( 2 == yearlen )
575 {
Hanno Becker61937d42017-04-26 15:01:23 +0100576 if ( tm->year < 50 )
577 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000578
Hanno Becker61937d42017-04-26 15:01:23 +0100579 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000580 }
581
Hanno Becker61937d42017-04-26 15:01:23 +0100582 CHECK( x509_parse_int( p, 2, &tm->mon ) );
583 CHECK( x509_parse_int( p, 2, &tm->day ) );
584 CHECK( x509_parse_int( p, 2, &tm->hour ) );
585 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000586
587 /*
588 * Parse seconds if present
589 */
590 if ( len >= 2 )
591 {
Hanno Becker61937d42017-04-26 15:01:23 +0100592 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000593 len -= 2;
594 }
595 else
596 return ( MBEDTLS_ERR_X509_INVALID_DATE );
597
598 /*
599 * Parse trailing 'Z' if present
600 */
601 if ( 1 == len && 'Z' == **p )
602 {
603 (*p)++;
604 len--;
605 }
606
607 /*
608 * We should have parsed all characters at this point
609 */
610 if ( 0 != len )
611 return ( MBEDTLS_ERR_X509_INVALID_DATE );
612
Hanno Becker61937d42017-04-26 15:01:23 +0100613 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000614
615 return ( 0 );
616}
617
618/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 * Time ::= CHOICE {
620 * utcTime UTCTime,
621 * generalTime GeneralizedTime }
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100624 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625{
Janos Follath865b3eb2019-12-16 11:46:15 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000627 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 unsigned char tag;
629
630 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100631 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
632 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
634 tag = **p;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000637 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000639 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100641 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
642 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000643
644 (*p)++;
645 ret = mbedtls_asn1_get_len( p, end, &len );
646
647 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100648 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000649
Hanno Becker61937d42017-04-26 15:01:23 +0100650 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651}
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654{
Janos Follath865b3eb2019-12-16 11:46:15 +0000655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100657 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100660 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
661 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
Andres AG4bdbe092016-09-19 16:58:45 +0100663 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100666 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667
Andres AG4bdbe092016-09-19 16:58:45 +0100668 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669 sig->len = len;
670 sig->p = *p;
671
672 *p += len;
673
674 return( 0 );
675}
676
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100677/*
678 * Get signature algorithm from alg OID and optional parameters
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
681 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200682 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683{
Janos Follath865b3eb2019-12-16 11:46:15 +0000684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200686 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100690 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
693 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100696
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200697 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200698 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200699 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200702 md_alg,
703 &pss_opts->mgf1_hash_id,
704 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100705 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100708 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200709 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100710
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200711 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100712 }
713 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100715 {
716 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100718 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100720 }
721
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 return( 0 );
723}
724
725/*
726 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800727 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000730 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731{
Janos Follath865b3eb2019-12-16 11:46:15 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 size_t len;
734
Hanno Becker3cddba82019-02-11 14:33:36 +0000735 /* Extension structure use EXPLICIT tagging. That is, the actual
736 * `Extensions` structure is wrapped by a tag-length pair using
737 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000738 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
739 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
740 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100741 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742
Hanno Becker6ccfb182019-02-12 11:52:10 +0000743 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
744 ext->p = *p;
745 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746
747 /*
748 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
751 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100752 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753
754 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100755 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757
758 return( 0 );
759}
760
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761/*
762 * Store the name in printable form into buf; no more
763 * than size characters will be written
764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766{
Janos Follath865b3eb2019-12-16 11:46:15 +0000767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100768 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000769 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 memset( s, 0, sizeof( s ) );
775
776 name = dn;
777 p = buf;
778 n = size;
779
780 while( name != NULL )
781 {
782 if( !name->oid.p )
783 {
784 name = name->next;
785 continue;
786 }
787
788 if( name != dn )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200791 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 }
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795
796 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200800 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100802 for( i = 0, j = 0; i < name->val.len; i++, j++ )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 {
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100804 if( j >= sizeof( s ) - 1 )
805 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
807 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100808 // Special characters requiring escaping, RFC 1779
809 if( c && strchr( ",=+<>#;\"\\", c ) )
810 {
811 if( j + 1 >= sizeof( s ) - 1 )
Werner Lewis1421efa2022-06-27 12:01:22 +0100812 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100813 s[j++] = '\\';
814 }
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900815 if( c < 32 || c >= 127 )
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100816 s[j] = '?';
817 else s[j] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100819 s[j] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200821 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000822
823 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 name = name->next;
825 }
826
827 return( (int) ( size - n ) );
828}
829
830/*
831 * Store the serial in printable form into buf; no more
832 * than size characters will be written
833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835{
Janos Follath865b3eb2019-12-16 11:46:15 +0000836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837 size_t i, n, nr;
838 char *p;
839
840 p = buf;
841 n = size;
842
843 nr = ( serial->len <= 32 )
844 ? serial->len : 28;
845
846 for( i = 0; i < nr; i++ )
847 {
848 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
849 continue;
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200853 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854 }
855
856 if( nr != serial->len )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200859 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860 }
861
862 return( (int) ( size - n ) );
863}
864
865/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200866 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
869 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200870 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100871{
Janos Follath865b3eb2019-12-16 11:46:15 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100873 char *p = buf;
874 size_t n = size;
875 const char *desc = NULL;
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100878 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200882 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
885 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 const mbedtls_pk_rsassa_pss_options *pss_opts;
888 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 md_info = mbedtls_md_info_from_type( md_alg );
893 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
896 md_info ? mbedtls_md_get_name( md_info ) : "???",
897 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200898 (unsigned int) pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200899 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100900 }
901#else
902 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200903 ((void) md_alg);
904 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100906
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200907 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100908}
909
910/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 * Helper for writing "RSA key size", "EC key size", etc
912 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200913int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914{
915 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200916 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200920 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 return( 0 );
923}
924
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200925#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200926/*
927 * Set the time structure to the current time.
928 * Return 0 on success, non-zero on failure.
929 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200930static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100931{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000932 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100933 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200934 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
SimonBd5800b72016-04-26 07:43:27 +0100936 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100937 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200938
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200939 if( lt == NULL )
940 ret = -1;
941 else
942 {
943 now->year = lt->tm_year + 1900;
944 now->mon = lt->tm_mon + 1;
945 now->day = lt->tm_mday;
946 now->hour = lt->tm_hour;
947 now->min = lt->tm_min;
948 now->sec = lt->tm_sec;
949 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200950
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200951 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100952}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100954/*
955 * Return 0 if before <= after, 1 otherwise
956 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100958{
959 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 return( 1 );
961
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100962 if( before->year == after->year &&
963 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 return( 1 );
965
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100966 if( before->year == after->year &&
967 before->mon == after->mon &&
968 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969 return( 1 );
970
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100971 if( before->year == after->year &&
972 before->mon == after->mon &&
973 before->day == after->day &&
974 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 return( 1 );
976
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100977 if( before->year == after->year &&
978 before->mon == after->mon &&
979 before->day == after->day &&
980 before->hour == after->hour &&
981 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 return( 1 );
983
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100984 if( before->year == after->year &&
985 before->mon == after->mon &&
986 before->day == after->day &&
987 before->hour == after->hour &&
988 before->min == after->min &&
989 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 return( 1 );
991
992 return( 0 );
993}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100994
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100995int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200999 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001000 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001001
1002 return( x509_check_time( &now, to ) );
1003}
1004
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001005int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001006{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001008
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001009 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001010 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001011
1012 return( x509_check_time( from, &now ) );
1013}
1014
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001015#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001016
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001017int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018{
1019 ((void) to);
1020 return( 0 );
1021}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001023int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001024{
1025 ((void) from);
1026 return( 0 );
1027}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001028#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001032#include "mbedtls/x509_crt.h"
1033#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001034
1035/*
1036 * Checkup routine
1037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001039{
Junhwan Park39bdab72018-10-17 21:01:08 +09001040 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001041#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001042 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_x509_crt cacert;
1044 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001045
1046 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001048
Junhwan Park39bdab72018-10-17 21:01:08 +09001049 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001053 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001054 if( ret != 0 )
1055 {
1056 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001058
Junhwan Park39bdab72018-10-17 21:01:08 +09001059 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001060 }
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001063 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001064 if( ret != 0 )
1065 {
1066 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001068
Junhwan Park39bdab72018-10-17 21:01:08 +09001069 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001070 }
1071
1072 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001076 if( ret != 0 )
1077 {
1078 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001080
Junhwan Park39bdab72018-10-17 21:01:08 +09001081 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001082 }
1083
1084 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001086
Junhwan Park39bdab72018-10-17 21:01:08 +09001087cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_x509_crt_free( &cacert );
1089 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001090#else
1091 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001092#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park39bdab72018-10-17 21:01:08 +09001093 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001094}
1095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#endif /* MBEDTLS_X509_USE_C */