blob: 5a42911e70561c65b81f2949a4d8113b5cd6eab8 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 common functions for parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
28 */
29
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/x509.h"
35#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Rich Evans36796df2015-02-12 18:27:14 +000039#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Simon Butcherb5b6af22016-07-13 14:46:18 +010048#if defined(MBEDTLS_HAVE_TIME)
49#include "mbedtls/platform_time.h"
50#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000051#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010052#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#include <time.h>
54#endif
55
Hanno Becker1eeca412018-10-15 12:01:35 +010056#define CHECK(code) if( ( ret = ( code ) ) != 0 ){ return( ret ); }
57#define CHECK_RANGE(min, max, val) \
58 do \
59 { \
60 if( ( val ) < ( min ) || ( val ) > ( max ) ) \
61 { \
62 return( ret ); \
63 } \
64 } while( 0 )
Rich Evans7d5a55a2015-02-13 11:48:02 +000065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066/*
67 * CertificateSerialNumber ::= INTEGER
68 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
70 mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071{
Janos Follath865b3eb2019-12-16 11:46:15 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
74 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +010075 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
79 **p != MBEDTLS_ASN1_INTEGER )
Chris Jones9f7a6932021-04-14 12:12:09 +010080 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL,
81 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
83 serial->tag = *(*p)++;
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +010086 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SERIAL, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087
88 serial->p = *p;
89 *p += serial->len;
90
91 return( 0 );
92}
93
94/* Get an algorithm identifier without parameters (eg for signatures)
95 *
96 * AlgorithmIdentifier ::= SEQUENCE {
97 * algorithm OBJECT IDENTIFIER,
98 * parameters ANY DEFINED BY algorithm OPTIONAL }
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
101 mbedtls_x509_buf *alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102{
Janos Follath865b3eb2019-12-16 11:46:15 +0000103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100106 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
108 return( 0 );
109}
110
111/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100112 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
115 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100116{
Janos Follath865b3eb2019-12-16 11:46:15 +0000117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100120 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121
122 return( 0 );
123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100127 * HashAlgorithm ::= AlgorithmIdentifier
128 *
129 * AlgorithmIdentifier ::= SEQUENCE {
130 * algorithm OBJECT IDENTIFIER,
131 * parameters ANY DEFINED BY algorithm OPTIONAL }
132 *
133 * For HashAlgorithm, parameters MUST be NULL or absent.
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100136{
Janos Follath865b3eb2019-12-16 11:46:15 +0000137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100138 unsigned char *p;
139 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100141 size_t len;
142
143 /* Make sure we got a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
146 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100147
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400148 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100149 end = p + alg->len;
150
151 if( p >= end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100152 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
153 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100154
155 /* Parse md_oid */
156 md_oid.tag = *p;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100159 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100160
161 md_oid.p = p;
162 p += md_oid.len;
163
164 /* Get md_alg from md_oid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100166 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100167
168 /* Make sure params is absent of NULL */
169 if( p == end )
170 return( 0 );
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100173 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100174
175 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100176 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
177 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100178
179 return( 0 );
180}
181
182/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100183 * RSASSA-PSS-params ::= SEQUENCE {
184 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
185 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
186 * saltLength [2] INTEGER DEFAULT 20,
187 * trailerField [3] INTEGER DEFAULT 1 }
188 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200189 *
190 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
191 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
192 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
195 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200196 int *salt_len )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100197{
Janos Follath865b3eb2019-12-16 11:46:15 +0000198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100199 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100200 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100203
204 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 *md_alg = MBEDTLS_MD_SHA1;
206 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100207 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100208
209 /* Make sure params is a SEQUENCE and setup bounds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100211 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
212 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213
214 p = (unsigned char *) params->p;
215 end = p + params->len;
216
217 if( p == end )
218 return( 0 );
219
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100220 /*
221 * HashAlgorithm
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
224 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100225 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100226 end2 = p + len;
227
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100228 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100230 return( ret );
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100233 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234
235 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100236 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
237 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100240 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100241
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100242 if( p == end )
243 return( 0 );
244
245 /*
246 * MaskGenAlgorithm
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
249 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100251 end2 = p + len;
252
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100253 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100255 return( ret );
256
257 /* Only MFG1 is recognised for now */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100259 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
260 MBEDTLS_ERR_OID_NOT_FOUND ) );
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100261
262 /* Parse HashAlgorithm */
263 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
264 return( ret );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100265
266 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100267 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
268 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100271 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100272
273 if( p == end )
274 return( 0 );
275
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100276 /*
277 * salt_len
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
280 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100281 {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100282 end2 = p + len;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100285 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100286
287 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100288 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100292 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293
294 if( p == end )
295 return( 0 );
296
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200298 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
301 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302 {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200303 int trailer_field;
304
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305 end2 = p + len;
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100308 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309
310 if( p != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100311 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
312 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200313
314 if( trailer_field != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Chris Jones9f7a6932021-04-14 12:12:09 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG, ret ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100319
320 if( p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100321 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_ALG,
322 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100323
324 return( 0 );
325}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100327
328/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 * AttributeTypeAndValue ::= SEQUENCE {
330 * type AttributeType,
331 * value AttributeValue }
332 *
333 * AttributeType ::= OBJECT IDENTIFIER
334 *
335 * AttributeValue ::= ANY DEFINED BY AttributeType
336 */
337static int x509_get_attr_type_value( unsigned char **p,
338 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340{
Janos Follath865b3eb2019-12-16 11:46:15 +0000341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_x509_buf *oid;
344 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
347 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100348 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
Hanno Becker12f62fb2019-02-12 17:22:36 +0000350 end = *p + len;
351
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100353 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
354 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
356 oid = &cur->oid;
357 oid->tag = **p;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100360 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361
362 oid->p = *p;
363 *p += oid->len;
364
365 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100366 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
367 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
370 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
371 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
372 **p != MBEDTLS_ASN1_BIT_STRING )
Chris Jones9f7a6932021-04-14 12:12:09 +0100373 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
374 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376 val = &cur->val;
377 val->tag = *(*p)++;
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100380 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381
382 val->p = *p;
383 *p += val->len;
384
Hanno Becker12f62fb2019-02-12 17:22:36 +0000385 if( *p != end )
386 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100387 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME,
388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Hanno Becker12f62fb2019-02-12 17:22:36 +0000389 }
390
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 cur->next = NULL;
392
393 return( 0 );
394}
395
396/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000397 * Name ::= CHOICE { -- only one possibility for now --
398 * rdnSequence RDNSequence }
399 *
400 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
401 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 * RelativeDistinguishedName ::=
403 * SET OF AttributeTypeAndValue
404 *
405 * AttributeTypeAndValue ::= SEQUENCE {
406 * type AttributeType,
407 * value AttributeValue }
408 *
409 * AttributeType ::= OBJECT IDENTIFIER
410 *
411 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200412 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000413 * The data structure is optimized for the common case where each RDN has only
414 * one element, which is represented as a list of AttributeTypeAndValue.
415 * For the general case we still use a flat list, but we mark elements of the
416 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
420 mbedtls_x509_name *cur )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
Janos Follath865b3eb2019-12-16 11:46:15 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200423 size_t set_len;
424 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100426 /* don't use recursion, we'd risk stack overflow if not optimized */
427 while( 1 )
428 {
429 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000430 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
433 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100434 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_NAME, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100436 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000438 while( 1 )
439 {
440 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
441 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000443 if( *p == end_set )
444 break;
445
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200446 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000447 cur->next_merged = 1;
448
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200449 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000450
451 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200452 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000453
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000454 cur = cur->next;
455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100457 /*
458 * continue until end of SEQUENCE is reached
459 */
460 if( *p == end )
461 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200463 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100465 if( cur->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200466 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100468 cur = cur->next;
469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470}
471
Janos Follath87c98072017-02-03 12:36:59 +0000472static int x509_parse_int( unsigned char **p, size_t n, int *res )
473{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000474 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000475
476 for( ; n > 0; --n )
477 {
478 if( ( **p < '0') || ( **p > '9' ) )
479 return ( MBEDTLS_ERR_X509_INVALID_DATE );
480
Rich Evans7d5a55a2015-02-13 11:48:02 +0000481 *res *= 10;
Janos Follath87c98072017-02-03 12:36:59 +0000482 *res += ( *(*p)++ - '0' );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000483 }
Janos Follath87c98072017-02-03 12:36:59 +0000484
485 return( 0 );
Rich Evans7d5a55a2015-02-13 11:48:02 +0000486}
487
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000488static int x509_date_is_valid(const mbedtls_x509_time *t )
Andres AG4b76aec2016-09-23 13:16:02 +0100489{
490 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000491 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100492
Hanno Becker61937d42017-04-26 15:01:23 +0100493 CHECK_RANGE( 0, 9999, t->year );
494 CHECK_RANGE( 0, 23, t->hour );
495 CHECK_RANGE( 0, 59, t->min );
496 CHECK_RANGE( 0, 59, t->sec );
Andres AG4b76aec2016-09-23 13:16:02 +0100497
Hanno Becker61937d42017-04-26 15:01:23 +0100498 switch( t->mon )
Andres AG4b76aec2016-09-23 13:16:02 +0100499 {
500 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000501 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100502 break;
503 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000504 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100505 break;
506 case 2:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000507 if( ( !( t->year % 4 ) && t->year % 100 ) ||
508 !( t->year % 400 ) )
509 month_len = 29;
510 else
511 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100512 break;
513 default:
514 return( ret );
515 }
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000516 CHECK_RANGE( 1, month_len, t->day );
Andres AG4b76aec2016-09-23 13:16:02 +0100517
518 return( 0 );
519}
520
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521/*
Janos Follath87c98072017-02-03 12:36:59 +0000522 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
523 * field.
524 */
525static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
Hanno Becker61937d42017-04-26 15:01:23 +0100526 mbedtls_x509_time *tm )
Janos Follath87c98072017-02-03 12:36:59 +0000527{
Janos Follath865b3eb2019-12-16 11:46:15 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000529
530 /*
531 * Minimum length is 10 or 12 depending on yearlen
532 */
533 if ( len < yearlen + 8 )
534 return ( MBEDTLS_ERR_X509_INVALID_DATE );
535 len -= yearlen + 8;
536
537 /*
538 * Parse year, month, day, hour, minute
539 */
Hanno Becker61937d42017-04-26 15:01:23 +0100540 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
Janos Follath87c98072017-02-03 12:36:59 +0000541 if ( 2 == yearlen )
542 {
Hanno Becker61937d42017-04-26 15:01:23 +0100543 if ( tm->year < 50 )
544 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000545
Hanno Becker61937d42017-04-26 15:01:23 +0100546 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000547 }
548
Hanno Becker61937d42017-04-26 15:01:23 +0100549 CHECK( x509_parse_int( p, 2, &tm->mon ) );
550 CHECK( x509_parse_int( p, 2, &tm->day ) );
551 CHECK( x509_parse_int( p, 2, &tm->hour ) );
552 CHECK( x509_parse_int( p, 2, &tm->min ) );
Janos Follath87c98072017-02-03 12:36:59 +0000553
554 /*
555 * Parse seconds if present
556 */
557 if ( len >= 2 )
558 {
Hanno Becker61937d42017-04-26 15:01:23 +0100559 CHECK( x509_parse_int( p, 2, &tm->sec ) );
Janos Follath87c98072017-02-03 12:36:59 +0000560 len -= 2;
561 }
562 else
563 return ( MBEDTLS_ERR_X509_INVALID_DATE );
564
565 /*
566 * Parse trailing 'Z' if present
567 */
568 if ( 1 == len && 'Z' == **p )
569 {
570 (*p)++;
571 len--;
572 }
573
574 /*
575 * We should have parsed all characters at this point
576 */
577 if ( 0 != len )
578 return ( MBEDTLS_ERR_X509_INVALID_DATE );
579
Hanno Becker61937d42017-04-26 15:01:23 +0100580 CHECK( x509_date_is_valid( tm ) );
Janos Follath87c98072017-02-03 12:36:59 +0000581
582 return ( 0 );
583}
584
585/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586 * Time ::= CHOICE {
587 * utcTime UTCTime,
588 * generalTime GeneralizedTime }
589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
Hanno Becker61937d42017-04-26 15:01:23 +0100591 mbedtls_x509_time *tm )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592{
Janos Follath865b3eb2019-12-16 11:46:15 +0000593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000594 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 unsigned char tag;
596
597 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100598 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
599 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
601 tag = **p;
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( tag == MBEDTLS_ASN1_UTC_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000604 year_len = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
Janos Follath87c98072017-02-03 12:36:59 +0000606 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607 else
Chris Jones9f7a6932021-04-14 12:12:09 +0100608 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
609 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Janos Follath87c98072017-02-03 12:36:59 +0000610
611 (*p)++;
612 ret = mbedtls_asn1_get_len( p, end, &len );
613
614 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100615 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Janos Follath87c98072017-02-03 12:36:59 +0000616
Hanno Becker61937d42017-04-26 15:01:23 +0100617 return x509_parse_time( p, len, year_len, tm );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618}
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621{
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100624 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
626 if( ( end - *p ) < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100627 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE,
628 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
Andres AG4bdbe092016-09-19 16:58:45 +0100630 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100633 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
Andres AG4bdbe092016-09-19 16:58:45 +0100635 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 sig->len = len;
637 sig->p = *p;
638
639 *p += len;
640
641 return( 0 );
642}
643
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100644/*
645 * Get signature algorithm from alg OID and optional parameters
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
648 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200649 void **sig_opts )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200653 if( *sig_opts != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100657 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
660 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100663
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200664 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200665 if( pss_opts == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200666 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200669 md_alg,
670 &pss_opts->mgf1_hash_id,
671 &pss_opts->expected_salt_len );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100672 if( ret != 0 )
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_free( pss_opts );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100675 return( ret );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200676 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100677
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200678 *sig_opts = (void *) pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100679 }
680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100682 {
683 /* Make sure parameters are absent or NULL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100685 sig_params->len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_X509_INVALID_ALG );
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100687 }
688
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 return( 0 );
690}
691
692/*
693 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800694 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
Hanno Becker6ccfb182019-02-12 11:52:10 +0000697 mbedtls_x509_buf *ext, int tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698{
Janos Follath865b3eb2019-12-16 11:46:15 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 size_t len;
701
Hanno Becker3cddba82019-02-11 14:33:36 +0000702 /* Extension structure use EXPLICIT tagging. That is, the actual
703 * `Extensions` structure is wrapped by a tag-length pair using
704 * the respective context-specific tag. */
Hanno Becker6ccfb182019-02-12 11:52:10 +0000705 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
706 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
707 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100708 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Hanno Becker6ccfb182019-02-12 11:52:10 +0000710 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
711 ext->p = *p;
712 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
714 /*
715 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
718 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100719 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720
721 if( end != *p + len )
Chris Jones9f7a6932021-04-14 12:12:09 +0100722 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
723 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
725 return( 0 );
726}
727
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728/*
729 * Store the name in printable form into buf; no more
730 * than size characters will be written
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733{
Janos Follath865b3eb2019-12-16 11:46:15 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100735 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000736 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200740
741 memset( s, 0, sizeof( s ) );
742
743 name = dn;
744 p = buf;
745 n = size;
746
747 while( name != NULL )
748 {
749 if( !name->oid.p )
750 {
751 name = name->next;
752 continue;
753 }
754
755 if( name != dn )
756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200758 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 }
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762
763 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 ret = mbedtls_snprintf( p, n, "%s=", short_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 ret = mbedtls_snprintf( p, n, "\?\?=" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200767 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100769 for( i = 0, j = 0; i < name->val.len; i++, j++ )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 {
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100771 if( j >= sizeof( s ) - 1 )
772 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
774 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100775 // Special characters requiring escaping, RFC 1779
776 if( c && strchr( ",=+<>#;\"\\", c ) )
777 {
778 if( j + 1 >= sizeof( s ) - 1 )
Werner Lewis1421efa2022-06-27 12:01:22 +0100779 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100780 s[j++] = '\\';
781 }
Koh M. Nakagawa46b87822020-05-16 10:08:09 +0900782 if( c < 32 || c >= 127 )
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100783 s[j] = '?';
784 else s[j] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100786 s[j] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 ret = mbedtls_snprintf( p, n, "%s", s );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200788 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000789
790 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 name = name->next;
792 }
793
794 return( (int) ( size - n ) );
795}
796
797/*
798 * Store the serial in printable form into buf; no more
799 * than size characters will be written
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802{
Janos Follath865b3eb2019-12-16 11:46:15 +0000803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 size_t i, n, nr;
805 char *p;
806
807 p = buf;
808 n = size;
809
810 nr = ( serial->len <= 32 )
811 ? serial->len : 28;
812
813 for( i = 0; i < nr; i++ )
814 {
815 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
816 continue;
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 ret = mbedtls_snprintf( p, n, "%02X%s",
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200820 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 }
822
823 if( nr != serial->len )
824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 ret = mbedtls_snprintf( p, n, "...." );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200826 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 }
828
829 return( (int) ( size - n ) );
830}
831
832/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200833 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
836 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200837 const void *sig_opts )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100838{
Janos Follath865b3eb2019-12-16 11:46:15 +0000839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100840 char *p = buf;
841 size_t n = size;
842 const char *desc = NULL;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100845 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 ret = mbedtls_snprintf( p, n, "???" );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 ret = mbedtls_snprintf( p, n, "%s", desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200849 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
852 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 const mbedtls_pk_rsassa_pss_options *pss_opts;
855 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 md_info = mbedtls_md_info_from_type( md_alg );
860 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
863 md_info ? mbedtls_md_get_name( md_info ) : "???",
864 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200865 (unsigned int) pss_opts->expected_salt_len );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100867 }
868#else
869 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200870 ((void) md_alg);
871 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100873
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200874 return( (int)( size - n ) );
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100875}
876
877/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 * Helper for writing "RSA key size", "EC key size", etc
879 */
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200880int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881{
882 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200883 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 ret = mbedtls_snprintf( p, n, "%s key size", name );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200887 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
889 return( 0 );
890}
891
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200892#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200893/*
894 * Set the time structure to the current time.
895 * Return 0 on success, non-zero on failure.
896 */
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200897static int x509_get_current_time( mbedtls_x509_time *now )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100898{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000899 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100900 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200901 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902
SimonBd5800b72016-04-26 07:43:27 +0100903 tt = mbedtls_time( NULL );
Hanno Becker6a739782018-09-05 15:06:19 +0100904 lt = mbedtls_platform_gmtime_r( &tt, &tm_buf );
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200905
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200906 if( lt == NULL )
907 ret = -1;
908 else
909 {
910 now->year = lt->tm_year + 1900;
911 now->mon = lt->tm_mon + 1;
912 now->day = lt->tm_mday;
913 now->hour = lt->tm_hour;
914 now->min = lt->tm_min;
915 now->sec = lt->tm_sec;
916 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200917
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200918 return( ret );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100919}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100921/*
922 * Return 0 if before <= after, 1 otherwise
923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100925{
926 if( before->year > after->year )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 return( 1 );
928
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100929 if( before->year == after->year &&
930 before->mon > after->mon )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 return( 1 );
932
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100933 if( before->year == after->year &&
934 before->mon == after->mon &&
935 before->day > after->day )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 return( 1 );
937
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100938 if( before->year == after->year &&
939 before->mon == after->mon &&
940 before->day == after->day &&
941 before->hour > after->hour )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 return( 1 );
943
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100944 if( before->year == after->year &&
945 before->mon == after->mon &&
946 before->day == after->day &&
947 before->hour == after->hour &&
948 before->min > after->min )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 return( 1 );
950
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100951 if( before->year == after->year &&
952 before->mon == after->mon &&
953 before->day == after->day &&
954 before->hour == after->hour &&
955 before->min == after->min &&
956 before->sec > after->sec )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 return( 1 );
958
959 return( 0 );
960}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100961
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100962int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100965
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200966 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200967 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100968
969 return( x509_check_time( &now, to ) );
970}
971
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100972int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100973{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100975
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200976 if( x509_get_current_time( &now ) != 0 )
Manuel Pégourié-Gonnarde7e89842015-06-22 19:15:32 +0200977 return( 1 );
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100978
979 return( x509_check_time( from, &now ) );
980}
981
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200982#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100983
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100984int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985{
986 ((void) to);
987 return( 0 );
988}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100989
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +0100990int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100991{
992 ((void) from);
993 return( 0 );
994}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200995#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +0200998
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000999#include "mbedtls/x509_crt.h"
1000#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001001
1002/*
1003 * Checkup routine
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005int mbedtls_x509_self_test( int verbose )
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001006{
Junhwan Park39bdab72018-10-17 21:01:08 +09001007 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001008#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001009 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_x509_crt cacert;
1011 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001012
1013 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_printf( " X.509 certificate load: " );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001015
Junhwan Park39bdab72018-10-17 21:01:08 +09001016 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_x509_crt_init( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001020 mbedtls_test_cli_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001021 if( ret != 0 )
1022 {
1023 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001025
Junhwan Park39bdab72018-10-17 21:01:08 +09001026 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001027 }
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001030 mbedtls_test_ca_crt_len );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001031 if( ret != 0 )
1032 {
1033 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001035
Junhwan Park39bdab72018-10-17 21:01:08 +09001036 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001037 }
1038
1039 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_printf( "passed\n X.509 signature verify: ");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001043 if( ret != 0 )
1044 {
1045 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_printf( "failed\n" );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001047
Junhwan Park39bdab72018-10-17 21:01:08 +09001048 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001049 }
1050
1051 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_printf( "passed\n\n");
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001053
Junhwan Park39bdab72018-10-17 21:01:08 +09001054cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_x509_crt_free( &cacert );
1056 mbedtls_x509_crt_free( &clicert );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001057#else
1058 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001059#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Junhwan Park39bdab72018-10-17 21:01:08 +09001060 return( ret );
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001061}
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_X509_USE_C */