blob: aa3951799cacef471d8871475a757f897a0e7c67 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#else
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <stdio.h>
50#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010051#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020052#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010053#define mbedtls_printf printf
54#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#endif
56
Simon Butcherb5b6af22016-07-13 14:46:18 +010057#if defined(MBEDTLS_HAVE_TIME)
58#include "mbedtls/platform_time.h"
59#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000060#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010061#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <time.h>
63#endif
64
Przemek Stekielfd183662022-08-02 15:29:20 +020065#include "legacy_or_psa.h"
66
Hanno Becker1eeca412018-10-15 12:01:35 +010067#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
68#define CHECK_RANGE(min, max, val) \
69 do \
70 { \
71 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
72 { \
73 return( ret ); \
74 } \
75 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000076
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077/*
78 * CertificateSerialNumber ::= INTEGER
79 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
81 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010086 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
87 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
90 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010091 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
92 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
94 serial->tag = *(*p)++;
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010097 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
99 serial->p = *p;
100 *p += serial->len;
101
102 return( 0 );
103}
104
105/* Get an algorithm identifier without parameters (eg for signatures)
106 *
107 * AlgorithmIdentifier ::= SEQUENCE {
108 * algorithm OBJECT IDENTIFIER,
109 * parameters ANY DEFINED BY algorithm OPTIONAL }
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
112 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113{
Janos Follath865b3eb2019-12-16 11:46:15 +0000114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100117 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118
119 return( 0 );
120}
121
122/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100123 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
126 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100127{
Janos Follath865b3eb2019-12-16 11:46:15 +0000128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100131 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100132
133 return( 0 );
134}
135
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200136/*
137 * Convert md type to string
138 */
139static inline const char* md_type_to_string( mbedtls_md_type_t md_alg )
140{
141 switch( md_alg )
142 {
Przemek Stekielfd183662022-08-02 15:29:20 +0200143#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200144 case MBEDTLS_MD_MD5:
145 return( "MD5" );
146#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200147#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200148 case MBEDTLS_MD_SHA1:
149 return( "SHA1" );
150#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200151#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200152 case MBEDTLS_MD_SHA224:
153 return( "SHA224" );
154#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200155#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200156 case MBEDTLS_MD_SHA256:
157 return( "SHA256" );
158#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200159#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200160 case MBEDTLS_MD_SHA384:
161 return( "SHA384" );
162#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200163#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200164 case MBEDTLS_MD_SHA512:
165 return( "SHA512" );
166#endif
Przemek Stekielfd183662022-08-02 15:29:20 +0200167#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_MD_OR_PSA)
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200168 case MBEDTLS_MD_RIPEMD160:
169 return( "RIPEMD160" );
170#endif
171 case MBEDTLS_MD_NONE:
172 return( NULL );
173 default:
174 return( NULL );
175 }
176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100179/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100180 * HashAlgorithm ::= AlgorithmIdentifier
181 *
182 * AlgorithmIdentifier ::= SEQUENCE {
183 * algorithm OBJECT IDENTIFIER,
184 * parameters ANY DEFINED BY algorithm OPTIONAL }
185 *
186 * For HashAlgorithm, parameters MUST be NULL or absent.
187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188static 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 +0100189{
Janos Follath865b3eb2019-12-16 11:46:15 +0000190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191 unsigned char *p;
192 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194 size_t len;
195
196 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100198 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
199 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100200
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400201 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100202 end = p + alg->len;
203
204 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
206 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100207
208 /* Parse md_oid */
209 md_oid.tag = *p;
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100212 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100213
214 md_oid.p = p;
215 p += md_oid.len;
216
217 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100219 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100220
221 /* Make sure params is absent of NULL */
222 if( p == end )
223 return( 0 );
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100226 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100227
228 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100229 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
230 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100231
232 return( 0 );
233}
234
235/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100236 * RSASSA-PSS-params ::= SEQUENCE {
237 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
238 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
239 * saltLength [2] INTEGER DEFAULT 20,
240 * trailerField [3] INTEGER DEFAULT 1 }
241 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200242 *
243 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
244 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
245 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
248 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200249 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250{
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100253 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100256
257 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 *md_alg = MBEDTLS_MD_SHA1;
259 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100260 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100261
262 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100264 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
265 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100266
267 p = (unsigned char *) params->p;
268 end = p + params->len;
269
270 if( p == end )
271 return( 0 );
272
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273 /*
274 * HashAlgorithm
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
277 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100278 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100279 end2 = p + len;
280
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100281 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100283 return( ret );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100286 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100287
288 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100289 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100293 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100294
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100295 if( p == end )
296 return( 0 );
297
298 /*
299 * MaskGenAlgorithm
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
302 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100303 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100304 end2 = p + len;
305
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100306 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100308 return( ret );
309
310 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100312 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
313 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100314
315 /* Parse HashAlgorithm */
316 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
317 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100318
319 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100320 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
321 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100324 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100325
326 if( p == end )
327 return( 0 );
328
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100329 /*
330 * salt_len
331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
333 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100334 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100335 end2 = p + len;
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100338 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100339
340 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100341 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100345 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100346
347 if( p == end )
348 return( 0 );
349
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100350 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200351 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
354 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100355 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200356 int trailer_field;
357
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100358 end2 = p + len;
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100361 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100362
363 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100364 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
365 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200366
367 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100371 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100372
373 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100374 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
375 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100376
377 return( 0 );
378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100380
381/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 * AttributeTypeAndValue ::= SEQUENCE {
383 * type AttributeType,
384 * value AttributeValue }
385 *
386 * AttributeType ::= OBJECT IDENTIFIER
387 *
388 * AttributeValue ::= ANY DEFINED BY AttributeType
389 */
390static int x509_get_attr_type_value( unsigned char **p,
391 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393{
Janos Follath865b3eb2019-12-16 11:46:15 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_x509_buf *oid;
397 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
400 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100401 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
Hanno Becker12f62fb2019-02-12 17:22:36 +0000403 end = *p + len;
404
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100406 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
407 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
409 oid = &cur->oid;
410 oid->tag = **p;
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100413 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415 oid->p = *p;
416 *p += oid->len;
417
418 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100419 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
420 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
423 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
424 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
425 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100426 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
427 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 val = &cur->val;
430 val->tag = *(*p)++;
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100433 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 val->p = *p;
436 *p += val->len;
437
Hanno Becker12f62fb2019-02-12 17:22:36 +0000438 if( *p != end )
439 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100440 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
441 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000442 }
443
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 cur->next = NULL;
445
446 return( 0 );
447}
448
449/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000450 * Name ::= CHOICE { -- only one possibility for now --
451 * rdnSequence RDNSequence }
452 *
453 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
454 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 * RelativeDistinguishedName ::=
456 * SET OF AttributeTypeAndValue
457 *
458 * AttributeTypeAndValue ::= SEQUENCE {
459 * type AttributeType,
460 * value AttributeValue }
461 *
462 * AttributeType ::= OBJECT IDENTIFIER
463 *
464 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200465 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000466 * The data structure is optimized for the common case where each RDN has only
467 * one element, which is represented as a list of AttributeTypeAndValue.
468 * For the general case we still use a flat list, but we mark elements of the
469 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
473 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474{
Janos Follath865b3eb2019-12-16 11:46:15 +0000475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200476 size_t set_len;
477 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100479 /* don't use recursion, we'd risk stack overflow if not optimized */
480 while( 1 )
481 {
482 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000483 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
486 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100487 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100489 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491 while( 1 )
492 {
493 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
494 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000496 if( *p == end_set )
497 break;
498
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200499 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000500 cur->next_merged = 1;
501
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200502 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000503
504 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200505 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000506
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000507 cur = cur->next;
508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100510 /*
511 * continue until end of SEQUENCE is reached
512 */
513 if( *p == end )
514 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200516 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100518 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200519 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100521 cur = cur->next;
522 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523}
524
Janos Follath87c98072017-02-03 12:36:59 +0000525static int x509_parse_int( unsigned char **p, size_t n, int *res )
526{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000527 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000528
529 for( ; n > 0; --n )
530 {
531 if( ( **p < '0') || ( **p > '9' ) )
532 return ( MBEDTLS_ERR_X509_INVALID_DATE );
533
Rich Evans7d5a55a2015-02-13 11:48:02 +0000534 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000535 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000536 }
Janos Follath87c98072017-02-03 12:36:59 +0000537
538 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000539}
540
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000541static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100542{
543 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000544 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100545
Hanno Becker61937d42017-04-26 15:01:23 +0100546 CHECK_RANGE( 0, 9999, t->year );
547 CHECK_RANGE( 0, 23, t->hour );
548 CHECK_RANGE( 0, 59, t->min );
549 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100550
Hanno Becker61937d42017-04-26 15:01:23 +0100551 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100552 {
553 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000554 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100555 break;
556 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000557 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100558 break;
559 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000560 if( ( !( t->year % 4 ) && t->year % 100 ) ||
561 !( t->year % 400 ) )
562 month_len = 29;
563 else
564 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100565 break;
566 default:
567 return( ret );
568 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000569 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100570
571 return( 0 );
572}
573
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574/*
Janos Follath87c98072017-02-03 12:36:59 +0000575 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
576 * field.
577 */
578static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100579 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000580{
Janos Follath865b3eb2019-12-16 11:46:15 +0000581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000582
583 /*
584 * Minimum length is 10 or 12 depending on yearlen
585 */
586 if ( len < yearlen + 8 )
587 return ( MBEDTLS_ERR_X509_INVALID_DATE );
588 len -= yearlen + 8;
589
590 /*
591 * Parse year, month, day, hour, minute
592 */
Hanno Becker61937d42017-04-26 15:01:23 +0100593 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000594 if ( 2 == yearlen )
595 {
Hanno Becker61937d42017-04-26 15:01:23 +0100596 if ( tm->year < 50 )
597 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000598
Hanno Becker61937d42017-04-26 15:01:23 +0100599 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000600 }
601
Hanno Becker61937d42017-04-26 15:01:23 +0100602 CHECK( x509_parse_int( p, 2, &tm->mon ) );
603 CHECK( x509_parse_int( p, 2, &tm->day ) );
604 CHECK( x509_parse_int( p, 2, &tm->hour ) );
605 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000606
607 /*
608 * Parse seconds if present
609 */
610 if ( len >= 2 )
611 {
Hanno Becker61937d42017-04-26 15:01:23 +0100612 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000613 len -= 2;
614 }
615 else
616 return ( MBEDTLS_ERR_X509_INVALID_DATE );
617
618 /*
619 * Parse trailing 'Z' if present
620 */
621 if ( 1 == len && 'Z' == **p )
622 {
623 (*p)++;
624 len--;
625 }
626
627 /*
628 * We should have parsed all characters at this point
629 */
630 if ( 0 != len )
631 return ( MBEDTLS_ERR_X509_INVALID_DATE );
632
Hanno Becker61937d42017-04-26 15:01:23 +0100633 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000634
635 return ( 0 );
636}
637
638/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639 * Time ::= CHOICE {
640 * utcTime UTCTime,
641 * generalTime GeneralizedTime }
642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100644 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645{
Janos Follath865b3eb2019-12-16 11:46:15 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000647 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 unsigned char tag;
649
650 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100651 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
652 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
654 tag = **p;
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000657 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000659 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100661 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
662 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000663
664 (*p)++;
665 ret = mbedtls_asn1_get_len( p, end, &len );
666
667 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100668 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000669
Hanno Becker61937d42017-04-26 15:01:23 +0100670 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671}
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674{
Janos Follath865b3eb2019-12-16 11:46:15 +0000675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100677 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678
679 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100680 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
681 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Andres AG4bdbe092016-09-19 16:58:45 +0100683 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100686 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Andres AG4bdbe092016-09-19 16:58:45 +0100688 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 sig->len = len;
690 sig->p = *p;
691
692 *p += len;
693
694 return( 0 );
695}
696
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100697/*
698 * Get signature algorithm from alg OID and optional parameters
699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
701 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200702 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703{
Janos Follath865b3eb2019-12-16 11:46:15 +0000704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200706 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100710 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
713 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100716
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200717 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200718 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200719 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200722 md_alg,
723 &pss_opts->mgf1_hash_id,
724 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100725 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100728 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200729 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100730
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200731 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100732 }
733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100735 {
736 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100738 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100740 }
741
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 return( 0 );
743}
744
745/*
746 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800747 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000750 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751{
Janos Follath865b3eb2019-12-16 11:46:15 +0000752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753 size_t len;
754
Hanno Becker3cddba82019-02-11 14:33:36 +0000755 /* Extension structure use EXPLICIT tagging. That is, the actual
756 * `Extensions` structure is wrapped by a tag-length pair using
757 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000758 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
759 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
760 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100761 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762
Hanno Becker6ccfb182019-02-12 11:52:10 +0000763 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
764 ext->p = *p;
765 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766
767 /*
768 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
771 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100772 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100775 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
776 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777
778 return( 0 );
779}
780
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781/*
782 * Store the name in printable form into buf; no more
783 * than size characters will be written
784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786{
Janos Follath865b3eb2019-12-16 11:46:15 +0000787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewisb33dacd2022-05-20 12:48:46 +0100788 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000789 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
794 memset( s, 0, sizeof( s ) );
795
796 name = dn;
797 p = buf;
798 n = size;
799
800 while( name != NULL )
801 {
802 if( !name->oid.p )
803 {
804 name = name->next;
805 continue;
806 }
807
808 if( name != dn )
809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200811 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 }
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815
816 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200820 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821
Werner Lewisb33dacd2022-05-20 12:48:46 +0100822 for( i = 0, j = 0; i < name->val.len; i++, j++ )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 {
Werner Lewisb33dacd2022-05-20 12:48:46 +0100824 if( j >= sizeof( s ) - 1 )
825 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826
827 c = name->val.p[i];
Werner Lewisb33dacd2022-05-20 12:48:46 +0100828 // Special characters requiring escaping, RFC 1779
829 if( c && strchr( ",=+<>#;\"\\", c ) )
830 {
831 if( j + 1 >= sizeof( s ) - 1 )
Werner Lewis9b0e9402022-06-27 12:01:22 +0100832 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Werner Lewisb33dacd2022-05-20 12:48:46 +0100833 s[j++] = '\\';
834 }
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900835 if( c < 32 || c >= 127 )
Werner Lewisb33dacd2022-05-20 12:48:46 +0100836 s[j] = '?';
837 else s[j] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838 }
Werner Lewisb33dacd2022-05-20 12:48:46 +0100839 s[j] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200841 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000842
843 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 name = name->next;
845 }
846
847 return( (int) ( size - n ) );
848}
849
850/*
851 * Store the serial in printable form into buf; no more
852 * than size characters will be written
853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855{
Janos Follath865b3eb2019-12-16 11:46:15 +0000856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 size_t i, n, nr;
858 char *p;
859
860 p = buf;
861 n = size;
862
863 nr = ( serial->len <= 32 )
864 ? serial->len : 28;
865
866 for( i = 0; i < nr; i++ )
867 {
868 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
869 continue;
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200873 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 }
875
876 if( nr != serial->len )
877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200879 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 }
881
882 return( (int) ( size - n ) );
883}
884
Hanno Becker612a2f12020-10-09 09:19:39 +0100885#if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200887 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
890 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200891 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100892{
Janos Follath865b3eb2019-12-16 11:46:15 +0000893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100894 char *p = buf;
895 size_t n = size;
896 const char *desc = NULL;
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100899 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200903 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
906 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100911
Przemek Stekiel9e30fc92022-06-27 12:48:35 +0200912 const char *name = md_type_to_string( md_alg );
913 const char *mgf_name = md_type_to_string( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200916 name ? name : "???",
917 mgf_name ? mgf_name : "???",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200918 (unsigned int) pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200919 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100920 }
921#else
922 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200923 ((void) md_alg);
924 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100926
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200927 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100928}
Hanno Becker612a2f12020-10-09 09:19:39 +0100929#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100930
931/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 * Helper for writing "RSA key size", "EC key size", etc
933 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200934int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935{
936 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200937 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200941 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943 return( 0 );
944}
945
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200946#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200947/*
948 * Set the time structure to the current time.
949 * Return 0 on success, non-zero on failure.
950 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200951static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100952{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000953 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100954 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200955 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956
SimonBd5800b72016-04-26 07:43:27 +0100957 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100958 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200959
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200960 if( lt == NULL )
961 ret = -1;
962 else
963 {
964 now->year = lt->tm_year + 1900;
965 now->mon = lt->tm_mon + 1;
966 now->day = lt->tm_mday;
967 now->hour = lt->tm_hour;
968 now->min = lt->tm_min;
969 now->sec = lt->tm_sec;
970 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200971
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200972 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100973}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100975/*
976 * Return 0 if before <= after, 1 otherwise
977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100979{
980 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 return( 1 );
982
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100983 if( before->year == after->year &&
984 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 return( 1 );
986
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100987 if( before->year == after->year &&
988 before->mon == after->mon &&
989 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 return( 1 );
991
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100992 if( before->year == after->year &&
993 before->mon == after->mon &&
994 before->day == after->day &&
995 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 return( 1 );
997
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998 if( before->year == after->year &&
999 before->mon == after->mon &&
1000 before->day == after->day &&
1001 before->hour == after->hour &&
1002 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 return( 1 );
1004
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001005 if( before->year == after->year &&
1006 before->mon == after->mon &&
1007 before->day == after->day &&
1008 before->hour == after->hour &&
1009 before->min == after->min &&
1010 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 return( 1 );
1012
1013 return( 0 );
1014}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001015
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001016int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001019
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001020 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001021 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001022
1023 return( x509_check_time( &now, to ) );
1024}
1025
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001026int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001029
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +02001030 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +02001031 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001032
1033 return( x509_check_time( from, &now ) );
1034}
1035
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001036#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001037
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001038int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039{
1040 ((void) to);
1041 return( 0 );
1042}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001043
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001044int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001045{
1046 ((void) from);
1047 return( 0 );
1048}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001049#endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#endif /* MBEDTLS_X509_USE_C */