blob: c5b0161e7188fa7f01196754b3aeb8b3c70f1197 [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
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020056#include "mbedtls/legacy_or_psa.h"
Przemek Stekielfd183662022-08-02 15:29:20 +020057
Hanno Becker1eeca412018-10-15 12:01:35 +010058#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
59#define CHECK_RANGE(min, max, val) \
60 do \
61 { \
62 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
63 { \
64 return( ret ); \
65 } \
66 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000067
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068/*
69 * CertificateSerialNumber ::= INTEGER
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
72 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073{
Janos Follath865b3eb2019-12-16 11:46:15 +000074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075
76 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010077 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
78 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
81 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010082 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
83 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 serial->tag = *(*p)++;
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010088 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
90 serial->p = *p;
91 *p += serial->len;
92
93 return( 0 );
94}
95
96/* Get an algorithm identifier without parameters (eg for signatures)
97 *
98 * AlgorithmIdentifier ::= SEQUENCE {
99 * algorithm OBJECT IDENTIFIER,
100 * parameters ANY DEFINED BY algorithm OPTIONAL }
101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
103 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104{
Janos Follath865b3eb2019-12-16 11:46:15 +0000105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100108 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
110 return( 0 );
111}
112
113/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100114 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
117 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100118{
Janos Follath865b3eb2019-12-16 11:46:15 +0000119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100122 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123
124 return( 0 );
125}
126
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200127/*
128 * Convert md type to string
129 */
130static inline const char* md_type_to_string( mbedtls_md_type_t md_alg )
131{
132 switch( md_alg )
133 {
Przemek Stekielfd183662022-08-02 15:29:20 +0200134#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200135 case MBEDTLS_MD_MD5:
136 return( "MD5" );
137#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200138#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200139 case MBEDTLS_MD_SHA1:
140 return( "SHA1" );
141#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200142#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200143 case MBEDTLS_MD_SHA224:
144 return( "SHA224" );
145#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200146#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200147 case MBEDTLS_MD_SHA256:
148 return( "SHA256" );
149#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200150#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200151 case MBEDTLS_MD_SHA384:
152 return( "SHA384" );
153#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200154#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200155 case MBEDTLS_MD_SHA512:
156 return( "SHA512" );
157#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200158#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200159 case MBEDTLS_MD_RIPEMD160:
160 return( "RIPEMD160" );
161#endif
162 case MBEDTLS_MD_NONE:
163 return( NULL );
164 default:
165 return( NULL );
166 }
167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100170/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100171 * HashAlgorithm ::= AlgorithmIdentifier
172 *
173 * AlgorithmIdentifier ::= SEQUENCE {
174 * algorithm OBJECT IDENTIFIER,
175 * parameters ANY DEFINED BY algorithm OPTIONAL }
176 *
177 * For HashAlgorithm, parameters MUST be NULL or absent.
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179static 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 +0100180{
Janos Follath865b3eb2019-12-16 11:46:15 +0000181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182 unsigned char *p;
183 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185 size_t len;
186
187 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100189 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
190 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400192 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100193 end = p + alg->len;
194
195 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100196 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
197 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100198
199 /* Parse md_oid */
200 md_oid.tag = *p;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100203 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100204
205 md_oid.p = p;
206 p += md_oid.len;
207
208 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100210 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100211
212 /* Make sure params is absent of NULL */
213 if( p == end )
214 return( 0 );
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100217 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100218
219 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100220 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
221 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100222
223 return( 0 );
224}
225
226/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100227 * RSASSA-PSS-params ::= SEQUENCE {
228 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
229 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
230 * saltLength [2] INTEGER DEFAULT 20,
231 * trailerField [3] INTEGER DEFAULT 1 }
232 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200233 *
234 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
235 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
236 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
239 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200240 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100241{
Janos Follath865b3eb2019-12-16 11:46:15 +0000242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100243 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100244 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100245 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100247
248 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 *md_alg = MBEDTLS_MD_SHA1;
250 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100251 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252
253 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100255 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
256 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257
258 p = (unsigned char *) params->p;
259 end = p + params->len;
260
261 if( p == end )
262 return( 0 );
263
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100264 /*
265 * HashAlgorithm
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
268 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100269 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100270 end2 = p + len;
271
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100272 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100274 return( ret );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100277 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100278
279 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100280 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
281 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100284 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100285
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100286 if( p == end )
287 return( 0 );
288
289 /*
290 * MaskGenAlgorithm
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
293 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100294 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100295 end2 = p + len;
296
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100297 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100299 return( ret );
300
301 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100303 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
304 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100305
306 /* Parse HashAlgorithm */
307 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
308 return( 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é-Gonnardf346bab2014-01-23 16:24:44 +0100313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100315 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100316
317 if( p == end )
318 return( 0 );
319
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100320 /*
321 * salt_len
322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
324 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100325 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100326 end2 = p + len;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100329 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100330
331 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100332 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
333 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100336 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
338 if( p == end )
339 return( 0 );
340
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100341 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200342 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
345 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100346 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200347 int trailer_field;
348
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100349 end2 = p + len;
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100352 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100353
354 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100355 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
356 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200357
358 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100360 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100362 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100363
364 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100365 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
366 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100367
368 return( 0 );
369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100371
372/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 * AttributeTypeAndValue ::= SEQUENCE {
374 * type AttributeType,
375 * value AttributeValue }
376 *
377 * AttributeType ::= OBJECT IDENTIFIER
378 *
379 * AttributeValue ::= ANY DEFINED BY AttributeType
380 */
381static int x509_get_attr_type_value( unsigned char **p,
382 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384{
Janos Follath865b3eb2019-12-16 11:46:15 +0000385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_x509_buf *oid;
388 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
391 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100392 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393
Hanno Becker12f62fb2019-02-12 17:22:36 +0000394 end = *p + len;
395
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100397 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
398 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
400 oid = &cur->oid;
401 oid->tag = **p;
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100404 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406 oid->p = *p;
407 *p += oid->len;
408
409 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100410 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
411 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
414 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
415 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
416 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
418 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
420 val = &cur->val;
421 val->tag = *(*p)++;
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100424 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 val->p = *p;
427 *p += val->len;
428
Hanno Becker12f62fb2019-02-12 17:22:36 +0000429 if( *p != end )
430 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
432 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000433 }
434
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 cur->next = NULL;
436
437 return( 0 );
438}
439
440/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000441 * Name ::= CHOICE { -- only one possibility for now --
442 * rdnSequence RDNSequence }
443 *
444 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
445 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446 * RelativeDistinguishedName ::=
447 * SET OF AttributeTypeAndValue
448 *
449 * AttributeTypeAndValue ::= SEQUENCE {
450 * type AttributeType,
451 * value AttributeValue }
452 *
453 * AttributeType ::= OBJECT IDENTIFIER
454 *
455 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200456 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000457 * The data structure is optimized for the common case where each RDN has only
458 * one element, which is represented as a list of AttributeTypeAndValue.
459 * For the general case we still use a flat list, but we mark elements of the
460 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 * this list, eg mbedtls_x509_dn_gets().
David Horstmanned794832022-10-04 18:12:06 +0100462 *
David Horstmann11307a12022-10-17 18:10:23 +0100463 * On success, this function may allocate a linked list starting at cur->next
David Horstmanned794832022-10-04 18:12:06 +0100464 * that must later be free'd by the caller using mbedtls_free(). In error
465 * cases, this function frees all allocated memory internally and the caller
466 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
469 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470{
Janos Follath865b3eb2019-12-16 11:46:15 +0000471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200472 size_t set_len;
473 const unsigned char *end_set;
David Horstmanned794832022-10-04 18:12:06 +0100474 mbedtls_x509_name *head = cur;
475 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100477 /* don't use recursion, we'd risk stack overflow if not optimized */
478 while( 1 )
479 {
480 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000481 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
484 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
David Horstmanned794832022-10-04 18:12:06 +0100485 {
486 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret );
487 goto error;
488 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100490 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000492 while( 1 )
493 {
494 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
David Horstmanned794832022-10-04 18:12:06 +0100495 goto error;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000497 if( *p == end_set )
498 break;
499
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200500 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000501 cur->next_merged = 1;
502
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200503 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000504
505 if( cur->next == NULL )
David Horstmanned794832022-10-04 18:12:06 +0100506 {
507 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
508 goto error;
509 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000510
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000511 cur = cur->next;
512 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100514 /*
515 * continue until end of SEQUENCE is reached
516 */
517 if( *p == end )
518 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200520 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100522 if( cur->next == NULL )
David Horstmanned794832022-10-04 18:12:06 +0100523 {
524 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
525 goto error;
526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100528 cur = cur->next;
529 }
David Horstmanned794832022-10-04 18:12:06 +0100530
531error:
David Horstmanned794832022-10-04 18:12:06 +0100532 /* Skip the first element as we did not allocate it */
533 allocated = head->next;
534
David Horstmanned794832022-10-04 18:12:06 +0100535 while( allocated != NULL )
536 {
537 prev = allocated;
538 allocated = allocated->next;
539
540 mbedtls_platform_zeroize( prev, sizeof( *prev ) );
541 mbedtls_free( prev );
542 }
543
544 mbedtls_platform_zeroize( head, sizeof( *head ) );
545
David Horstmann078250e2022-10-18 18:11:13 +0100546 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547}
548
Janos Follath87c98072017-02-03 12:36:59 +0000549static int x509_parse_int( unsigned char **p, size_t n, int *res )
550{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000551 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000552
553 for( ; n > 0; --n )
554 {
555 if( ( **p < '0') || ( **p > '9' ) )
556 return ( MBEDTLS_ERR_X509_INVALID_DATE );
557
Rich Evans7d5a55a2015-02-13 11:48:02 +0000558 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000559 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000560 }
Janos Follath87c98072017-02-03 12:36:59 +0000561
562 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000563}
564
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000565static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100566{
567 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000568 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100569
Hanno Becker61937d42017-04-26 15:01:23 +0100570 CHECK_RANGE( 0, 9999, t->year );
571 CHECK_RANGE( 0, 23, t->hour );
572 CHECK_RANGE( 0, 59, t->min );
573 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100574
Hanno Becker61937d42017-04-26 15:01:23 +0100575 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100576 {
577 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000578 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100579 break;
580 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000581 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100582 break;
583 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000584 if( ( !( t->year % 4 ) && t->year % 100 ) ||
585 !( t->year % 400 ) )
586 month_len = 29;
587 else
588 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100589 break;
590 default:
591 return( ret );
592 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000593 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100594
595 return( 0 );
596}
597
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598/*
Janos Follath87c98072017-02-03 12:36:59 +0000599 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
600 * field.
601 */
602static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100603 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000604{
Janos Follath865b3eb2019-12-16 11:46:15 +0000605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000606
607 /*
608 * Minimum length is 10 or 12 depending on yearlen
609 */
610 if ( len < yearlen + 8 )
611 return ( MBEDTLS_ERR_X509_INVALID_DATE );
612 len -= yearlen + 8;
613
614 /*
615 * Parse year, month, day, hour, minute
616 */
Hanno Becker61937d42017-04-26 15:01:23 +0100617 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000618 if ( 2 == yearlen )
619 {
Hanno Becker61937d42017-04-26 15:01:23 +0100620 if ( tm->year < 50 )
621 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000622
Hanno Becker61937d42017-04-26 15:01:23 +0100623 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000624 }
625
Hanno Becker61937d42017-04-26 15:01:23 +0100626 CHECK( x509_parse_int( p, 2, &tm->mon ) );
627 CHECK( x509_parse_int( p, 2, &tm->day ) );
628 CHECK( x509_parse_int( p, 2, &tm->hour ) );
629 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000630
631 /*
632 * Parse seconds if present
633 */
634 if ( len >= 2 )
635 {
Hanno Becker61937d42017-04-26 15:01:23 +0100636 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000637 len -= 2;
638 }
639 else
640 return ( MBEDTLS_ERR_X509_INVALID_DATE );
641
642 /*
643 * Parse trailing 'Z' if present
644 */
645 if ( 1 == len && 'Z' == **p )
646 {
647 (*p)++;
648 len--;
649 }
650
651 /*
652 * We should have parsed all characters at this point
653 */
654 if ( 0 != len )
655 return ( MBEDTLS_ERR_X509_INVALID_DATE );
656
Hanno Becker61937d42017-04-26 15:01:23 +0100657 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000658
659 return ( 0 );
660}
661
662/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200663 * Time ::= CHOICE {
664 * utcTime UTCTime,
665 * generalTime GeneralizedTime }
666 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100668 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669{
Janos Follath865b3eb2019-12-16 11:46:15 +0000670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000671 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 unsigned char tag;
673
674 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100675 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
676 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677
678 tag = **p;
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000681 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000683 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100685 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
686 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000687
688 (*p)++;
689 ret = mbedtls_asn1_get_len( p, end, &len );
690
691 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100692 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000693
Hanno Becker61937d42017-04-26 15:01:23 +0100694 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695}
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698{
Janos Follath865b3eb2019-12-16 11:46:15 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100701 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
703 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100704 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
705 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Andres AG4bdbe092016-09-19 16:58:45 +0100707 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100710 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Andres AG4bdbe092016-09-19 16:58:45 +0100712 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713 sig->len = len;
714 sig->p = *p;
715
716 *p += len;
717
718 return( 0 );
719}
720
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100721/*
722 * Get signature algorithm from alg OID and optional parameters
723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
725 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727{
Janos Follath865b3eb2019-12-16 11:46:15 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200730 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
737 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100740
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200741 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200742 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200743 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200746 md_alg,
747 &pss_opts->mgf1_hash_id,
748 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100749 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200753 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100754
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200755 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100756 }
757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100759 {
760 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100762 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100764 }
765
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 return( 0 );
767}
768
769/*
770 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800771 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000774 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775{
Janos Follath865b3eb2019-12-16 11:46:15 +0000776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 size_t len;
778
Hanno Becker3cddba82019-02-11 14:33:36 +0000779 /* Extension structure use EXPLICIT tagging. That is, the actual
780 * `Extensions` structure is wrapped by a tag-length pair using
781 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000782 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
783 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
784 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100785 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786
Hanno Becker6ccfb182019-02-12 11:52:10 +0000787 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
788 ext->p = *p;
789 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790
791 /*
792 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
795 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100796 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
798 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100799 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
802 return( 0 );
803}
804
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805/*
806 * Store the name in printable form into buf; no more
807 * than size characters will be written
808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810{
Janos Follath865b3eb2019-12-16 11:46:15 +0000811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100812 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000813 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
818 memset( s, 0, sizeof( s ) );
819
820 name = dn;
821 p = buf;
822 n = size;
823
824 while( name != NULL )
825 {
826 if( !name->oid.p )
827 {
828 name = name->next;
829 continue;
830 }
831
832 if( name != dn )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200835 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 }
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839
840 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200844 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
Werner Lewisb33dacd2022-05-20 12:48:46 +0100846 for( i = 0, j = 0; i < name->val.len; i++, j++ )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 {
Werner Lewisb33dacd2022-05-20 12:48:46 +0100848 if( j >= sizeof( s ) - 1 )
849 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850
851 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100852 // Special characters requiring escaping, RFC 1779
853 if( c && strchr( ",=+<>#;\"\\", c ) )
854 {
855 if( j + 1 >= sizeof( s ) - 1 )
Werner Lewis9b0e9402022-06-27 12:01:22 +0100856 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Werner Lewisb33dacd2022-05-20 12:48:46 +0100857 s[j++] = '\\';
858 }
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900859 if( c < 32 || c >= 127 )
Werner Lewisb33dacd2022-05-20 12:48:46 +0100860 s[j] = '?';
861 else s[j] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100863 s[j] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200865 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000866
867 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 name = name->next;
869 }
870
871 return( (int) ( size - n ) );
872}
873
874/*
875 * Store the serial in printable form into buf; no more
876 * than size characters will be written
877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879{
Janos Follath865b3eb2019-12-16 11:46:15 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 size_t i, n, nr;
882 char *p;
883
884 p = buf;
885 n = size;
886
887 nr = ( serial->len <= 32 )
888 ? serial->len : 28;
889
890 for( i = 0; i < nr; i++ )
891 {
892 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
893 continue;
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200897 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 }
899
900 if( nr != serial->len )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200903 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 }
905
906 return( (int) ( size - n ) );
907}
908
Hanno Becker612a2f12020-10-09 09:19:39 +0100909#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200911 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
914 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200915 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100916{
Janos Follath865b3eb2019-12-16 11:46:15 +0000917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100918 char *p = buf;
919 size_t n = size;
920 const char *desc = NULL;
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100923 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100925 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200927 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
930 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100935
Przemek Stekiel9e30fc92022-06-27 12:48:35 +0200936 const char *name = md_type_to_string( md_alg );
937 const char *mgf_name = md_type_to_string( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200940 name ? name : "???",
941 mgf_name ? mgf_name : "???",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200942 (unsigned int) pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200943 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100944 }
945#else
946 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200947 ((void) md_alg);
948 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100950
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200951 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100952}
Hanno Becker612a2f12020-10-09 09:19:39 +0100953#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100954
955/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 * Helper for writing "RSA key size", "EC key size", etc
957 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200958int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959{
960 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200961 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200965 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966
967 return( 0 );
968}
969
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200970#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200971/*
972 * Set the time structure to the current time.
973 * Return 0 on success, non-zero on failure.
974 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200975static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100976{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000977 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100978 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200979 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
SimonBd5800b72016-04-26 07:43:27 +0100981 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100982 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200983
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200984 if( lt == NULL )
985 ret = -1;
986 else
987 {
988 now->year = lt->tm_year + 1900;
989 now->mon = lt->tm_mon + 1;
990 now->day = lt->tm_mday;
991 now->hour = lt->tm_hour;
992 now->min = lt->tm_min;
993 now->sec = lt->tm_sec;
994 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200995
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200996 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100997}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100999/*
1000 * Return 0 if before <= after, 1 otherwise
1001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001003{
1004 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 return( 1 );
1006
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001007 if( before->year == after->year &&
1008 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 return( 1 );
1010
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001011 if( before->year == after->year &&
1012 before->mon == after->mon &&
1013 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 return( 1 );
1015
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001016 if( before->year == after->year &&
1017 before->mon == after->mon &&
1018 before->day == after->day &&
1019 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 return( 1 );
1021
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022 if( before->year == after->year &&
1023 before->mon == after->mon &&
1024 before->day == after->day &&
1025 before->hour == after->hour &&
1026 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 return( 1 );
1028
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001029 if( before->year == after->year &&
1030 before->mon == after->mon &&
1031 before->day == after->day &&
1032 before->hour == after->hour &&
1033 before->min == after->min &&
1034 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 return( 1 );
1036
1037 return( 0 );
1038}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001040int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001043
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001044 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001045 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001046
1047 return( x509_check_time( &now, to ) );
1048}
1049
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001050int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001053
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001054 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001055 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001056
1057 return( x509_check_time( from, &now ) );
1058}
1059
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001060#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001061
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001062int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063{
1064 ((void) to);
1065 return( 0 );
1066}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001068int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001069{
1070 ((void) from);
1071 return( 0 );
1072}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001073#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#endif /* MBEDTLS_X509_USE_C */