blob: 38eb2e6607d88c7b119b9ef5666f439ec8192aeb [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056#define CHECK(code) if ((ret = (code)) != 0) { return ret; }
Hanno Becker1eeca412018-10-15 12:01:35 +010057#define CHECK_RANGE(min, max, val) \
58 do \
59 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 if ((val) < (min) || (val) > (max)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010061 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010063 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000065
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066/*
67 * CertificateSerialNumber ::= INTEGER
68 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069int 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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 if ((end - *p) < 1) {
75 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
76 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
77 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
80 **p != MBEDTLS_ASN1_INTEGER) {
81 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
82 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
83 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
85 serial->tag = *(*p)++;
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
88 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
89 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090
91 serial->p = *p;
92 *p += serial->len;
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
97/* Get an algorithm identifier without parameters (eg for signatures)
98 *
99 * AlgorithmIdentifier ::= SEQUENCE {
100 * algorithm OBJECT IDENTIFIER,
101 * parameters ANY DEFINED BY algorithm OPTIONAL }
102 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
104 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105{
Janos Follath865b3eb2019-12-16 11:46:15 +0000106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
109 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
110 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113}
114
115/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100116 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
119 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120{
Janos Follath865b3eb2019-12-16 11:46:15 +0000121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
124 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
125 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100128}
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100131/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100132 * HashAlgorithm ::= AlgorithmIdentifier
133 *
134 * AlgorithmIdentifier ::= SEQUENCE {
135 * algorithm OBJECT IDENTIFIER,
136 * parameters ANY DEFINED BY algorithm OPTIONAL }
137 *
138 * For HashAlgorithm, parameters MUST be NULL or absent.
139 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140static 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 +0100141{
Janos Follath865b3eb2019-12-16 11:46:15 +0000142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100143 unsigned char *p;
144 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100146 size_t len;
147
148 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
150 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
151 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
152 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100153
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400154 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100155 end = p + alg->len;
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 if (p >= end) {
158 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
159 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
160 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100161
162 /* Parse md_oid */
163 md_oid.tag = *p;
164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
166 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
167 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100168
169 md_oid.p = p;
170 p += md_oid.len;
171
172 /* Get md_alg from md_oid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
175 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100176
177 /* Make sure params is absent of NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if (p == end) {
179 return 0;
180 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
184 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if (p != end) {
187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
188 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
189 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100192}
193
194/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100195 * RSASSA-PSS-params ::= SEQUENCE {
196 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
197 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
198 * saltLength [2] INTEGER DEFAULT 20,
199 * trailerField [3] INTEGER DEFAULT 1 }
200 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200201 *
202 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
203 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000204 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100205 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
207 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
208 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100209{
Janos Follath865b3eb2019-12-16 11:46:15 +0000210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100212 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215
216 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 *md_alg = MBEDTLS_MD_SHA1;
218 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100220
221 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
224 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
225 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100226
227 p = (unsigned char *) params->p;
228 end = p + params->len;
229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 if (p == end) {
231 return 0;
232 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100233
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100234 /*
235 * HashAlgorithm
236 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
238 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
239 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100240 end2 = p + len;
241
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100242 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
244 return ret;
245 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
249 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (p != end2) {
252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
253 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
254 }
255 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100257 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 if (p == end) {
260 return 0;
261 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100262
263 /*
264 * MaskGenAlgorithm
265 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
267 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
268 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100269 end2 = p + len;
270
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100271 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
273 return ret;
274 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100275
276 /* Only MFG1 is recognised for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
278 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
279 MBEDTLS_ERR_OID_NOT_FOUND);
280 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100281
282 /* Parse HashAlgorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
284 return ret;
285 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if (p != end2) {
288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
290 }
291 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100293 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if (p == end) {
296 return 0;
297 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100299 /*
300 * salt_len
301 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
303 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
304 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100305 end2 = p + len;
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
309 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (p != end2) {
312 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
313 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
314 }
315 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100317 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 if (p == end) {
320 return 0;
321 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100322
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100323 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200324 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100325 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
327 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
328 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200329 int trailer_field;
330
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100331 end2 = p + len;
332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
334 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
335 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if (p != end2) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
340 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 if (trailer_field != 1) {
343 return MBEDTLS_ERR_X509_INVALID_ALG;
344 }
345 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
346 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100347 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (p != end) {
350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
351 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
352 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100357
358/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 * AttributeTypeAndValue ::= SEQUENCE {
360 * type AttributeType,
361 * value AttributeValue }
362 *
363 * AttributeType ::= OBJECT IDENTIFIER
364 *
365 * AttributeValue ::= ANY DEFINED BY AttributeType
366 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367static int x509_get_attr_type_value(unsigned char **p,
368 const unsigned char *end,
369 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370{
Janos Follath865b3eb2019-12-16 11:46:15 +0000371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_x509_buf *oid;
374 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
377 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
378 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
379 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380
Hanno Becker12f62fb2019-02-12 17:22:36 +0000381 end = *p + len;
382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if ((end - *p) < 1) {
384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
385 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
386 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
388 oid = &cur->oid;
389 oid->tag = **p;
390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
392 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
393 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
395 oid->p = *p;
396 *p += oid->len;
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if ((end - *p) < 1) {
399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
400 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
401 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
405 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 **p != MBEDTLS_ASN1_BIT_STRING) {
407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
408 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
409 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410
411 val = &cur->val;
412 val->tag = *(*p)++;
413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
415 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
416 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
418 val->p = *p;
419 *p += val->len;
420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 if (*p != end) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
423 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000424 }
425
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 cur->next = NULL;
427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429}
430
431/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000432 * Name ::= CHOICE { -- only one possibility for now --
433 * rdnSequence RDNSequence }
434 *
435 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
436 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 * RelativeDistinguishedName ::=
438 * SET OF AttributeTypeAndValue
439 *
440 * AttributeTypeAndValue ::= SEQUENCE {
441 * type AttributeType,
442 * value AttributeValue }
443 *
444 * AttributeType ::= OBJECT IDENTIFIER
445 *
446 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200447 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000448 * The data structure is optimized for the common case where each RDN has only
449 * one element, which is represented as a list of AttributeTypeAndValue.
450 * For the general case we still use a flat list, but we mark elements of the
451 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 * this list, eg mbedtls_x509_dn_gets().
David Horstmann8c176b42022-10-04 18:12:06 +0100453 *
David Horstmann5ad5e162022-10-17 18:10:23 +0100454 * On success, this function may allocate a linked list starting at cur->next
David Horstmann8c176b42022-10-04 18:12:06 +0100455 * that must later be free'd by the caller using mbedtls_free(). In error
456 * cases, this function frees all allocated memory internally and the caller
457 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
460 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461{
Janos Follath865b3eb2019-12-16 11:46:15 +0000462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200463 size_t set_len;
464 const unsigned char *end_set;
David Horstmann8c176b42022-10-04 18:12:06 +0100465 mbedtls_x509_name *head = cur;
466 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100468 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100470 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000471 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
474 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
475 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmann8c176b42022-10-04 18:12:06 +0100476 goto error;
477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100479 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 while (1) {
482 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmann8c176b42022-10-04 18:12:06 +0100483 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000487 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000489
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200490 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491 cur->next_merged = 1;
492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100496 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
497 goto error;
498 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000499
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000500 cur = cur->next;
501 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100503 /*
504 * continue until end of SEQUENCE is reached
505 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 if (*p == end) {
507 return 0;
508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100513 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
514 goto error;
515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100517 cur = cur->next;
518 }
David Horstmann8c176b42022-10-04 18:12:06 +0100519
520error:
David Horstmann8c176b42022-10-04 18:12:06 +0100521 /* Skip the first element as we did not allocate it */
522 allocated = head->next;
523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 while (allocated != NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100525 prev = allocated;
526 allocated = allocated->next;
527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 mbedtls_platform_zeroize(prev, sizeof(*prev));
529 mbedtls_free(prev);
David Horstmann8c176b42022-10-04 18:12:06 +0100530 }
531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 mbedtls_platform_zeroize(head, sizeof(*head));
David Horstmann8c176b42022-10-04 18:12:06 +0100533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535}
536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000538{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000539 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 for (; n > 0; --n) {
542 if ((**p < '0') || (**p > '9')) {
543 return MBEDTLS_ERR_X509_INVALID_DATE;
544 }
Janos Follath87c98072017-02-03 12:36:59 +0000545
Rich Evans7d5a55a2015-02-13 11:48:02 +0000546 *res *= 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000548 }
Janos Follath87c98072017-02-03 12:36:59 +0000549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000551}
552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100554{
555 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000556 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 CHECK_RANGE(0, 9999, t->year);
559 CHECK_RANGE(0, 23, t->hour);
560 CHECK_RANGE(0, 59, t->min);
561 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 switch (t->mon) {
Andres AG4b76aec2016-09-23 13:16:02 +0100564 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000565 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100566 break;
567 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000568 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100569 break;
570 case 2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 if ((!(t->year % 4) && t->year % 100) ||
572 !(t->year % 400)) {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000573 month_len = 29;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 } else {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000575 month_len = 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 }
Andres AG4b76aec2016-09-23 13:16:02 +0100577 break;
578 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100580 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100584}
585
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586/*
Janos Follath87c98072017-02-03 12:36:59 +0000587 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
588 * field.
589 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
591 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000592{
Janos Follath865b3eb2019-12-16 11:46:15 +0000593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000594
595 /*
596 * Minimum length is 10 or 12 depending on yearlen
597 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 if (len < yearlen + 8) {
599 return MBEDTLS_ERR_X509_INVALID_DATE;
600 }
Janos Follath87c98072017-02-03 12:36:59 +0000601 len -= yearlen + 8;
602
603 /*
604 * Parse year, month, day, hour, minute
605 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 CHECK(x509_parse_int(p, yearlen, &tm->year));
607 if (2 == yearlen) {
608 if (tm->year < 50) {
Hanno Becker61937d42017-04-26 15:01:23 +0100609 tm->year += 100;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 }
Janos Follath87c98072017-02-03 12:36:59 +0000611
Hanno Becker61937d42017-04-26 15:01:23 +0100612 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000613 }
614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 CHECK(x509_parse_int(p, 2, &tm->mon));
616 CHECK(x509_parse_int(p, 2, &tm->day));
617 CHECK(x509_parse_int(p, 2, &tm->hour));
618 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000619
620 /*
621 * Parse seconds if present
622 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 if (len >= 2) {
624 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000625 len -= 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626 } else {
627 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000628 }
Janos Follath87c98072017-02-03 12:36:59 +0000629
630 /*
631 * Parse trailing 'Z' if present
632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000634 (*p)++;
635 len--;
636 }
637
638 /*
639 * We should have parsed all characters at this point
640 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 if (0 != len) {
642 return MBEDTLS_ERR_X509_INVALID_DATE;
643 }
Janos Follath87c98072017-02-03 12:36:59 +0000644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000648}
649
650/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 * Time ::= CHOICE {
652 * utcTime UTCTime,
653 * generalTime GeneralizedTime }
654 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
656 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657{
Janos Follath865b3eb2019-12-16 11:46:15 +0000658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000659 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660 unsigned char tag;
661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662 if ((end - *p) < 1) {
663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
664 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
665 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 tag = **p;
668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000670 year_len = 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000672 year_len = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 } else {
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
675 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
676 }
Janos Follath87c98072017-02-03 12:36:59 +0000677
678 (*p)++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if (ret != 0) {
682 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
683 }
Janos Follath87c98072017-02-03 12:36:59 +0000684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686}
687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Janos Follath865b3eb2019-12-16 11:46:15 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100692 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 if ((end - *p) < 1) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
696 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Andres AG4bdbe092016-09-19 16:58:45 +0100699 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
702 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Andres AG4bdbe092016-09-19 16:58:45 +0100705 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706 sig->len = len;
707 sig->p = *p;
708
709 *p += len;
710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712}
713
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100714/*
715 * Get signature algorithm from alg OID and optional parameters
716 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
718 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
719 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720{
Janos Follath865b3eb2019-12-16 11:46:15 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (*sig_opts != NULL) {
724 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
725 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
728 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
736 if (pss_opts == NULL) {
737 return MBEDTLS_ERR_X509_ALLOC_FAILED;
738 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
741 md_alg,
742 &pss_opts->mgf1_hash_id,
743 &pss_opts->expected_salt_len);
744 if (ret != 0) {
745 mbedtls_free(pss_opts);
746 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200747 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100748
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200749 *sig_opts = (void *) pss_opts;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752 {
753 /* Make sure parameters are absent or NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
755 sig_params->len != 0) {
756 return MBEDTLS_ERR_X509_INVALID_ALG;
757 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100758 }
759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761}
762
763/*
764 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800765 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
768 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769{
Janos Follath865b3eb2019-12-16 11:46:15 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 size_t len;
772
Hanno Becker3cddba82019-02-11 14:33:36 +0000773 /* Extension structure use EXPLICIT tagging. That is, the actual
774 * `Extensions` structure is wrapped by a tag-length pair using
775 * the respective context-specific tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
777 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
778 if (ret != 0) {
779 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
780 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
Hanno Becker6ccfb182019-02-12 11:52:10 +0000782 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
783 ext->p = *p;
784 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
786 /*
787 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
790 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
792 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 if (end != *p + len) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
797 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800}
801
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802/*
803 * Store the name in printable form into buf; no more
804 * than size characters will be written
805 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100806int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807{
Janos Follath865b3eb2019-12-16 11:46:15 +0000808 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100809 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000810 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816
817 name = dn;
818 p = buf;
819 n = size;
820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 while (name != NULL) {
822 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 name = name->next;
824 continue;
825 }
826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 if (name != dn) {
828 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200829 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 }
831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (ret == 0) {
835 ret = mbedtls_snprintf(p, n, "%s=", short_name);
836 } else {
837 ret = mbedtls_snprintf(p, n, "\?\?=");
838 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200839 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100841 for (i = 0, j = 0; i < name->val.len; i++, j++) {
842 if (j >= sizeof(s) - 1) {
843 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
844 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845
846 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100847 // Special characters requiring escaping, RFC 1779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 if (c && strchr(",=+<>#;\"\\", c)) {
849 if (j + 1 >= sizeof(s) - 1) {
850 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
851 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100852 s[j++] = '\\';
853 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 if (c < 32 || c >= 127) {
855 s[j] = '?';
856 } else {
857 s[j] = c;
858 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100860 s[j] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200862 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000863
864 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 name = name->next;
866 }
867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869}
870
871/*
872 * Store the serial in printable form into buf; no more
873 * than size characters will be written
874 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876{
Janos Follath865b3eb2019-12-16 11:46:15 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 size_t i, n, nr;
879 char *p;
880
881 p = buf;
882 n = size;
883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 ? serial->len : 28;
886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 for (i = 0; i < nr; i++) {
888 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 ret = mbedtls_snprintf(p, n, "%02X%s",
893 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200894 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 }
896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 if (nr != serial->len) {
898 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200899 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 }
901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903}
904
905/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200906 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100907 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
909 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
910 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100911{
Janos Follath865b3eb2019-12-16 11:46:15 +0000912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100913 char *p = buf;
914 size_t n = size;
915 const char *desc = NULL;
916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
918 if (ret != 0) {
919 ret = mbedtls_snprintf(p, n, "???");
920 } else {
921 ret = mbedtls_snprintf(p, n, "%s", desc);
922 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200923 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100926 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 const mbedtls_pk_rsassa_pss_options *pss_opts;
928 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 md_info = mbedtls_md_info_from_type(md_alg);
933 mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
936 md_info ? mbedtls_md_get_name(md_info) : "???",
937 mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???",
938 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200939 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100940 }
941#else
942 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200943 ((void) md_alg);
944 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100948}
949
950/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 * Helper for writing "RSA key size", "EC key size", etc
952 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954{
955 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200956 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200960 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963}
964
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200965#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200966/*
967 * Set the time structure to the current time.
968 * Return 0 on success, non-zero on failure.
969 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100971{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000972 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100973 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200974 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 tt = mbedtls_time(NULL);
977 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 if (lt == NULL) {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200980 ret = -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 } else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200982 now->year = lt->tm_year + 1900;
983 now->mon = lt->tm_mon + 1;
984 now->day = lt->tm_mday;
985 now->hour = lt->tm_hour;
986 now->min = lt->tm_min;
987 now->sec = lt->tm_sec;
988 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100990 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100991}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100993/*
994 * Return 0 if before <= after, 1 otherwise
995 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100997{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998 if (before->year > after->year) {
999 return 1;
1000 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 if (before->year == after->year &&
1003 before->mon > after->mon) {
1004 return 1;
1005 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001008 before->mon == after->mon &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001009 before->day > after->day) {
1010 return 1;
1011 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001014 before->mon == after->mon &&
1015 before->day == after->day &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001016 before->hour > after->hour) {
1017 return 1;
1018 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001021 before->mon == after->mon &&
1022 before->day == after->day &&
1023 before->hour == after->hour &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 before->min > after->min) {
1025 return 1;
1026 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001028 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001029 before->mon == after->mon &&
1030 before->day == after->day &&
1031 before->hour == after->hour &&
1032 before->min == after->min &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 before->sec > after->sec) {
1034 return 1;
1035 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 if (x509_get_current_time(&now) != 0) {
1045 return 1;
1046 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001049}
1050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 if (x509_get_current_time(&now) != 0) {
1056 return 1;
1057 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001060}
1061
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001062#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065{
1066 ((void) to);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001071{
1072 ((void) from);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001074}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001075#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001078
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001079#include "mbedtls/x509_crt.h"
1080#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001081
1082/*
1083 * Checkup routine
1084 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085int mbedtls_x509_self_test(int verbose)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001086{
Junhwan Park39bdab72018-10-17 21:01:08 +09001087 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001088#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001089 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_x509_crt cacert;
1091 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093 if (verbose != 0) {
1094 mbedtls_printf(" X.509 certificate load: ");
1095 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097 mbedtls_x509_crt_init(&cacert);
1098 mbedtls_x509_crt_init(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
1101 mbedtls_test_cli_crt_len);
1102 if (ret != 0) {
1103 if (verbose != 0) {
1104 mbedtls_printf("failed\n");
1105 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001106
Junhwan Park39bdab72018-10-17 21:01:08 +09001107 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001108 }
1109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt,
1111 mbedtls_test_ca_crt_len);
1112 if (ret != 0) {
1113 if (verbose != 0) {
1114 mbedtls_printf("failed\n");
1115 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001116
Junhwan Park39bdab72018-10-17 21:01:08 +09001117 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001118 }
1119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 if (verbose != 0) {
1121 mbedtls_printf("passed\n X.509 signature verify: ");
1122 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL);
1125 if (ret != 0) {
1126 if (verbose != 0) {
1127 mbedtls_printf("failed\n");
1128 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001129
Junhwan Park39bdab72018-10-17 21:01:08 +09001130 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001131 }
1132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 if (verbose != 0) {
1134 mbedtls_printf("passed\n\n");
1135 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001136
Junhwan Park39bdab72018-10-17 21:01:08 +09001137cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 mbedtls_x509_crt_free(&cacert);
1139 mbedtls_x509_crt_free(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001140#else
1141 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001142#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 return ret;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001144}
1145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_X509_USE_C */