blob: 453ed59905cedb105b951cb4ca9a2a55dc7ac59c [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020048#else
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <stdio.h>
50#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010051#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020052#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010053#define mbedtls_printf printf
54#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055#endif
56
Simon Butcherb5b6af22016-07-13 14:46:18 +010057#if defined(MBEDTLS_HAVE_TIME)
58#include "mbedtls/platform_time.h"
59#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000060#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010061#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <time.h>
63#endif
64
Hanno Becker1eeca412018-10-15 12:01:35 +010065#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
66#define CHECK_RANGE(min, max, val) \
67 do \
68 { \
69 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
70 { \
71 return( ret ); \
72 } \
73 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000074
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075/*
76 * CertificateSerialNumber ::= INTEGER
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
79 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080{
Janos Follath865b3eb2019-12-16 11:46:15 +000081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
83 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010084 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
85 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
88 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010089 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
90 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091
92 serial->tag = *(*p)++;
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010095 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
97 serial->p = *p;
98 *p += serial->len;
99
100 return( 0 );
101}
102
103/* Get an algorithm identifier without parameters (eg for signatures)
104 *
105 * AlgorithmIdentifier ::= SEQUENCE {
106 * algorithm OBJECT IDENTIFIER,
107 * parameters ANY DEFINED BY algorithm OPTIONAL }
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
110 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111{
Janos Follath865b3eb2019-12-16 11:46:15 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100115 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
117 return( 0 );
118}
119
120/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100121 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
124 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100125{
Janos Follath865b3eb2019-12-16 11:46:15 +0000126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100129 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130
131 return( 0 );
132}
133
Przemek Stekiel6230d0d2022-06-27 11:19:04 +0200134/*
135 * Convert md type to string
136 */
137static inline const char* md_type_to_string( mbedtls_md_type_t md_alg )
138{
139 switch( md_alg )
140 {
141#if defined(MBEDTLS_MD5_C)
142 case MBEDTLS_MD_MD5:
143 return( "MD5" );
144#endif
145#if defined(MBEDTLS_SHA1_C)
146 case MBEDTLS_MD_SHA1:
147 return( "SHA1" );
148#endif
149#if defined(MBEDTLS_SHA224_C)
150 case MBEDTLS_MD_SHA224:
151 return( "SHA224" );
152#endif
153#if defined(MBEDTLS_SHA256_C)
154 case MBEDTLS_MD_SHA256:
155 return( "SHA256" );
156#endif
157#if defined(MBEDTLS_SHA384_C)
158 case MBEDTLS_MD_SHA384:
159 return( "SHA384" );
160#endif
161#if defined(MBEDTLS_SHA512_C)
162 case MBEDTLS_MD_SHA512:
163 return( "SHA512" );
164#endif
165#if defined(MBEDTLS_RIPEMD160_C)
166 case MBEDTLS_MD_RIPEMD160:
167 return( "RIPEMD160" );
168#endif
169 case MBEDTLS_MD_NONE:
170 return( NULL );
171 default:
172 return( NULL );
173 }
174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100177/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100178 * HashAlgorithm ::= AlgorithmIdentifier
179 *
180 * AlgorithmIdentifier ::= SEQUENCE {
181 * algorithm OBJECT IDENTIFIER,
182 * parameters ANY DEFINED BY algorithm OPTIONAL }
183 *
184 * For HashAlgorithm, parameters MUST be NULL or absent.
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186static 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 +0100187{
Janos Follath865b3eb2019-12-16 11:46:15 +0000188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100189 unsigned char *p;
190 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100192 size_t len;
193
194 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100196 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
197 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100198
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400199 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100200 end = p + alg->len;
201
202 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100203 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
204 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100205
206 /* Parse md_oid */
207 md_oid.tag = *p;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 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 md_oid.p = p;
213 p += md_oid.len;
214
215 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 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 /* Make sure params is absent of NULL */
220 if( p == end )
221 return( 0 );
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100224 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100225
226 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100227 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
228 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100229
230 return( 0 );
231}
232
233/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100234 * RSASSA-PSS-params ::= SEQUENCE {
235 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
236 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
237 * saltLength [2] INTEGER DEFAULT 20,
238 * trailerField [3] INTEGER DEFAULT 1 }
239 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200240 *
241 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
242 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
243 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
246 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200247 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100248{
Janos Follath865b3eb2019-12-16 11:46:15 +0000249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100252 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100254
255 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 *md_alg = MBEDTLS_MD_SHA1;
257 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100259
260 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100262 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
263 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100264
265 p = (unsigned char *) params->p;
266 end = p + params->len;
267
268 if( p == end )
269 return( 0 );
270
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100271 /*
272 * HashAlgorithm
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
275 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100276 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100277 end2 = p + len;
278
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100279 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100281 return( ret );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100284 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100285
286 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100287 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100289 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100291 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100292
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100293 if( p == end )
294 return( 0 );
295
296 /*
297 * MaskGenAlgorithm
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
300 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100301 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100302 end2 = p + len;
303
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100304 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100306 return( ret );
307
308 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100310 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
311 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100312
313 /* Parse HashAlgorithm */
314 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
315 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100316
317 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100322 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100323
324 if( p == end )
325 return( 0 );
326
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327 /*
328 * salt_len
329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
331 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100332 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100333 end2 = p + len;
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100336 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100337
338 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100339 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
340 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100343 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100344
345 if( p == end )
346 return( 0 );
347
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100348 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200349 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
352 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100353 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200354 int trailer_field;
355
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100356 end2 = p + len;
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100359 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100360
361 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100362 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
363 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200364
365 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100369 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100370
371 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100372 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
373 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100374
375 return( 0 );
376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100378
379/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 * AttributeTypeAndValue ::= SEQUENCE {
381 * type AttributeType,
382 * value AttributeValue }
383 *
384 * AttributeType ::= OBJECT IDENTIFIER
385 *
386 * AttributeValue ::= ANY DEFINED BY AttributeType
387 */
388static int x509_get_attr_type_value( unsigned char **p,
389 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391{
Janos Follath865b3eb2019-12-16 11:46:15 +0000392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_x509_buf *oid;
395 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
398 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100399 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
Hanno Becker12f62fb2019-02-12 17:22:36 +0000401 end = *p + len;
402
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100404 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
405 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
407 oid = &cur->oid;
408 oid->tag = **p;
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100411 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412
413 oid->p = *p;
414 *p += oid->len;
415
416 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100417 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
418 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
421 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
422 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
423 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100424 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
425 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
427 val = &cur->val;
428 val->tag = *(*p)++;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100431 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 val->p = *p;
434 *p += val->len;
435
Hanno Becker12f62fb2019-02-12 17:22:36 +0000436 if( *p != end )
437 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100438 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
439 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000440 }
441
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 cur->next = NULL;
443
444 return( 0 );
445}
446
447/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000448 * Name ::= CHOICE { -- only one possibility for now --
449 * rdnSequence RDNSequence }
450 *
451 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
452 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453 * RelativeDistinguishedName ::=
454 * SET OF AttributeTypeAndValue
455 *
456 * AttributeTypeAndValue ::= SEQUENCE {
457 * type AttributeType,
458 * value AttributeValue }
459 *
460 * AttributeType ::= OBJECT IDENTIFIER
461 *
462 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200463 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000464 * The data structure is optimized for the common case where each RDN has only
465 * one element, which is represented as a list of AttributeTypeAndValue.
466 * For the general case we still use a flat list, but we mark elements of the
467 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
471 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472{
Janos Follath865b3eb2019-12-16 11:46:15 +0000473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200474 size_t set_len;
475 const unsigned char *end_set;
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 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100485 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100487 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000489 while( 1 )
490 {
491 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
492 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000494 if( *p == end_set )
495 break;
496
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200497 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000498 cur->next_merged = 1;
499
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200500 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000501
502 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200503 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000504
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000505 cur = cur->next;
506 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100508 /*
509 * continue until end of SEQUENCE is reached
510 */
511 if( *p == end )
512 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200514 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100516 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200517 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100519 cur = cur->next;
520 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521}
522
Janos Follath87c98072017-02-03 12:36:59 +0000523static int x509_parse_int( unsigned char **p, size_t n, int *res )
524{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000525 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000526
527 for( ; n > 0; --n )
528 {
529 if( ( **p < '0') || ( **p > '9' ) )
530 return ( MBEDTLS_ERR_X509_INVALID_DATE );
531
Rich Evans7d5a55a2015-02-13 11:48:02 +0000532 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000533 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000534 }
Janos Follath87c98072017-02-03 12:36:59 +0000535
536 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000537}
538
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000539static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100540{
541 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000542 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100543
Hanno Becker61937d42017-04-26 15:01:23 +0100544 CHECK_RANGE( 0, 9999, t->year );
545 CHECK_RANGE( 0, 23, t->hour );
546 CHECK_RANGE( 0, 59, t->min );
547 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100548
Hanno Becker61937d42017-04-26 15:01:23 +0100549 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100550 {
551 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000552 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100553 break;
554 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000555 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100556 break;
557 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000558 if( ( !( t->year % 4 ) && t->year % 100 ) ||
559 !( t->year % 400 ) )
560 month_len = 29;
561 else
562 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100563 break;
564 default:
565 return( ret );
566 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000567 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100568
569 return( 0 );
570}
571
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572/*
Janos Follath87c98072017-02-03 12:36:59 +0000573 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
574 * field.
575 */
576static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100577 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000578{
Janos Follath865b3eb2019-12-16 11:46:15 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000580
581 /*
582 * Minimum length is 10 or 12 depending on yearlen
583 */
584 if ( len < yearlen + 8 )
585 return ( MBEDTLS_ERR_X509_INVALID_DATE );
586 len -= yearlen + 8;
587
588 /*
589 * Parse year, month, day, hour, minute
590 */
Hanno Becker61937d42017-04-26 15:01:23 +0100591 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000592 if ( 2 == yearlen )
593 {
Hanno Becker61937d42017-04-26 15:01:23 +0100594 if ( tm->year < 50 )
595 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000596
Hanno Becker61937d42017-04-26 15:01:23 +0100597 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000598 }
599
Hanno Becker61937d42017-04-26 15:01:23 +0100600 CHECK( x509_parse_int( p, 2, &tm->mon ) );
601 CHECK( x509_parse_int( p, 2, &tm->day ) );
602 CHECK( x509_parse_int( p, 2, &tm->hour ) );
603 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000604
605 /*
606 * Parse seconds if present
607 */
608 if ( len >= 2 )
609 {
Hanno Becker61937d42017-04-26 15:01:23 +0100610 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000611 len -= 2;
612 }
613 else
614 return ( MBEDTLS_ERR_X509_INVALID_DATE );
615
616 /*
617 * Parse trailing 'Z' if present
618 */
619 if ( 1 == len && 'Z' == **p )
620 {
621 (*p)++;
622 len--;
623 }
624
625 /*
626 * We should have parsed all characters at this point
627 */
628 if ( 0 != len )
629 return ( MBEDTLS_ERR_X509_INVALID_DATE );
630
Hanno Becker61937d42017-04-26 15:01:23 +0100631 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000632
633 return ( 0 );
634}
635
636/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 * Time ::= CHOICE {
638 * utcTime UTCTime,
639 * generalTime GeneralizedTime }
640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100642 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643{
Janos Follath865b3eb2019-12-16 11:46:15 +0000644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000645 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 unsigned char tag;
647
648 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100649 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
650 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
652 tag = **p;
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000655 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000657 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100659 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
660 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000661
662 (*p)++;
663 ret = mbedtls_asn1_get_len( p, end, &len );
664
665 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100666 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000667
Hanno Becker61937d42017-04-26 15:01:23 +0100668 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669}
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672{
Janos Follath865b3eb2019-12-16 11:46:15 +0000673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100675 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
677 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100678 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
679 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680
Andres AG4bdbe092016-09-19 16:58:45 +0100681 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100684 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Andres AG4bdbe092016-09-19 16:58:45 +0100686 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 sig->len = len;
688 sig->p = *p;
689
690 *p += len;
691
692 return( 0 );
693}
694
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100695/*
696 * Get signature algorithm from alg OID and optional parameters
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
699 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200700 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701{
Janos Follath865b3eb2019-12-16 11:46:15 +0000702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200704 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100708 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
711 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200715 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200716 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200717 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200720 md_alg,
721 &pss_opts->mgf1_hash_id,
722 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100723 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100726 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200727 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100728
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200729 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100730 }
731 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100733 {
734 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100736 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100738 }
739
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740 return( 0 );
741}
742
743/*
744 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800745 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000748 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749{
Janos Follath865b3eb2019-12-16 11:46:15 +0000750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 size_t len;
752
Hanno Becker3cddba82019-02-11 14:33:36 +0000753 /* Extension structure use EXPLICIT tagging. That is, the actual
754 * `Extensions` structure is wrapped by a tag-length pair using
755 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000756 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
757 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
758 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100759 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760
Hanno Becker6ccfb182019-02-12 11:52:10 +0000761 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
762 ext->p = *p;
763 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764
765 /*
766 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
769 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100770 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771
772 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100773 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
774 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
776 return( 0 );
777}
778
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779/*
780 * Store the name in printable form into buf; no more
781 * than size characters will be written
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784{
Janos Follath865b3eb2019-12-16 11:46:15 +0000785 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000787 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
792 memset( s, 0, sizeof( s ) );
793
794 name = dn;
795 p = buf;
796 n = size;
797
798 while( name != NULL )
799 {
800 if( !name->oid.p )
801 {
802 name = name->next;
803 continue;
804 }
805
806 if( name != dn )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200809 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 }
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813
814 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819
820 for( i = 0; i < name->val.len; i++ )
821 {
822 if( i >= sizeof( s ) - 1 )
823 break;
824
825 c = name->val.p[i];
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900826 if( c < 32 || c >= 127 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 s[i] = '?';
828 else s[i] = c;
829 }
830 s[i] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200832 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000833
834 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 name = name->next;
836 }
837
838 return( (int) ( size - n ) );
839}
840
841/*
Werner Lewisb3acb052022-06-17 15:59:58 +0100842 * Return the next relative DN in an X509 name.
843 */
844mbedtls_x509_name * mbedtls_x509_dn_get_next( mbedtls_x509_name * dn )
845{
846 for( ; dn->next != NULL && dn->next_merged; dn = dn->next );
847 return( dn->next );
848}
849
850/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 * 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 Stekiel6230d0d2022-06-27 11:19:04 +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 */