blob: d61ef4a2798bab4debf64788287002f5b92a3cba [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
Demi Marie Obenour0e207412022-12-04 04:24:22 -050056#define CHECK(code) \
57 do { \
58 if ((ret = (code)) != 0) { \
59 return ret; \
60 } \
61 } while (0)
62
Hanno Becker1eeca412018-10-15 12:01:35 +010063#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour0e207412022-12-04 04:24:22 -050064 do { \
65 if ((val) < (min) || (val) > (max)) { \
66 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010067 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000069
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070/*
71 * CertificateSerialNumber ::= INTEGER
72 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
74 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
Janos Follath865b3eb2019-12-16 11:46:15 +000076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 if ((end - *p) < 1) {
79 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
80 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
81 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
84 **p != MBEDTLS_ASN1_INTEGER) {
85 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
86 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
87 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
89 serial->tag = *(*p)++;
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
92 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
93 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094
95 serial->p = *p;
96 *p += serial->len;
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099}
100
101/* Get an algorithm identifier without parameters (eg for signatures)
102 *
103 * AlgorithmIdentifier ::= SEQUENCE {
104 * algorithm OBJECT IDENTIFIER,
105 * parameters ANY DEFINED BY algorithm OPTIONAL }
106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
108 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109{
Janos Follath865b3eb2019-12-16 11:46:15 +0000110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
113 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
114 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117}
118
119/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100120 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
123 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100124{
Janos Follath865b3eb2019-12-16 11:46:15 +0000125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
128 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
129 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100135/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100136 * HashAlgorithm ::= AlgorithmIdentifier
137 *
138 * AlgorithmIdentifier ::= SEQUENCE {
139 * algorithm OBJECT IDENTIFIER,
140 * parameters ANY DEFINED BY algorithm OPTIONAL }
141 *
142 * For HashAlgorithm, parameters MUST be NULL or absent.
143 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144static 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 +0100145{
Janos Follath865b3eb2019-12-16 11:46:15 +0000146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100147 unsigned char *p;
148 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100150 size_t len;
151
152 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
154 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
155 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
156 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100157
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400158 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100159 end = p + alg->len;
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if (p >= end) {
162 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
163 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
164 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100165
166 /* Parse md_oid */
167 md_oid.tag = *p;
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
170 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
171 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100172
173 md_oid.p = p;
174 p += md_oid.len;
175
176 /* Get md_alg from md_oid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
178 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
179 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100180
181 /* Make sure params is absent of NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (p == end) {
183 return 0;
184 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
187 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
188 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if (p != end) {
191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
192 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
193 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100196}
197
198/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100199 * RSASSA-PSS-params ::= SEQUENCE {
200 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
201 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
202 * saltLength [2] INTEGER DEFAULT 20,
203 * trailerField [3] INTEGER DEFAULT 1 }
204 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200205 *
206 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
207 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000208 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
211 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
212 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100213{
Janos Follath865b3eb2019-12-16 11:46:15 +0000214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100216 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219
220 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 *md_alg = MBEDTLS_MD_SHA1;
222 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100223 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100224
225 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
227 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
228 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
229 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100230
231 p = (unsigned char *) params->p;
232 end = p + params->len;
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 if (p == end) {
235 return 0;
236 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100237
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 /*
239 * HashAlgorithm
240 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
242 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
243 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100244 end2 = p + len;
245
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100246 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
248 return ret;
249 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
253 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 if (p != end2) {
256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
257 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
258 }
259 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100261 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (p == end) {
264 return 0;
265 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100266
267 /*
268 * MaskGenAlgorithm
269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
271 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
272 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100273 end2 = p + len;
274
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100275 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
277 return ret;
278 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100279
280 /* Only MFG1 is recognised for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
282 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
283 MBEDTLS_ERR_OID_NOT_FOUND);
284 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100285
286 /* Parse HashAlgorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
288 return ret;
289 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 if (p != end2) {
292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
293 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
294 }
295 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
296 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100297 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if (p == end) {
300 return 0;
301 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100302
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100303 /*
304 * salt_len
305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
307 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
308 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100309 end2 = p + len;
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
312 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
313 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 if (p != end2) {
316 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
317 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
318 }
319 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
320 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100321 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (p == end) {
324 return 0;
325 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100326
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100327 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200328 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100329 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
331 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
332 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200333 int trailer_field;
334
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100335 end2 = p + len;
336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
339 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if (p != end2) {
342 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
344 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 if (trailer_field != 1) {
347 return MBEDTLS_ERR_X509_INVALID_ALG;
348 }
349 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
350 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100351 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if (p != end) {
354 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
355 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
356 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100359}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100361
362/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 * AttributeTypeAndValue ::= SEQUENCE {
364 * type AttributeType,
365 * value AttributeValue }
366 *
367 * AttributeType ::= OBJECT IDENTIFIER
368 *
369 * AttributeValue ::= ANY DEFINED BY AttributeType
370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371static int x509_get_attr_type_value(unsigned char **p,
372 const unsigned char *end,
373 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374{
Janos Follath865b3eb2019-12-16 11:46:15 +0000375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_x509_buf *oid;
378 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
381 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
382 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
383 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384
Hanno Becker12f62fb2019-02-12 17:22:36 +0000385 end = *p + len;
386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if ((end - *p) < 1) {
388 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
389 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
390 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392 oid = &cur->oid;
393 oid->tag = **p;
394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
397 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
399 oid->p = *p;
400 *p += oid->len;
401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 if ((end - *p) < 1) {
403 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
404 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
405 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
409 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 **p != MBEDTLS_ASN1_BIT_STRING) {
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
412 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
413 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415 val = &cur->val;
416 val->tag = *(*p)++;
417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
422 val->p = *p;
423 *p += val->len;
424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 if (*p != end) {
426 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
427 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000428 }
429
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 cur->next = NULL;
431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433}
434
435/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000436 * Name ::= CHOICE { -- only one possibility for now --
437 * rdnSequence RDNSequence }
438 *
439 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
440 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 * RelativeDistinguishedName ::=
442 * SET OF AttributeTypeAndValue
443 *
444 * AttributeTypeAndValue ::= SEQUENCE {
445 * type AttributeType,
446 * value AttributeValue }
447 *
448 * AttributeType ::= OBJECT IDENTIFIER
449 *
450 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200451 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000452 * The data structure is optimized for the common case where each RDN has only
453 * one element, which is represented as a list of AttributeTypeAndValue.
454 * For the general case we still use a flat list, but we mark elements of the
455 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 * this list, eg mbedtls_x509_dn_gets().
David Horstmann8c176b42022-10-04 18:12:06 +0100457 *
David Horstmann5ad5e162022-10-17 18:10:23 +0100458 * On success, this function may allocate a linked list starting at cur->next
David Horstmann8c176b42022-10-04 18:12:06 +0100459 * that must later be free'd by the caller using mbedtls_free(). In error
460 * cases, this function frees all allocated memory internally and the caller
461 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
464 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465{
Janos Follath865b3eb2019-12-16 11:46:15 +0000466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200467 size_t set_len;
468 const unsigned char *end_set;
David Horstmann8c176b42022-10-04 18:12:06 +0100469 mbedtls_x509_name *head = cur;
470 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100474 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000475 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100476 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
478 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
479 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmann8c176b42022-10-04 18:12:06 +0100480 goto error;
481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100483 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 while (1) {
486 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmann8c176b42022-10-04 18:12:06 +0100487 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000493
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200494 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000495 cur->next_merged = 1;
496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100500 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
501 goto error;
502 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000503
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000504 cur = cur->next;
505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100507 /*
508 * continue until end of SEQUENCE is reached
509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 if (*p == end) {
511 return 0;
512 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100517 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
518 goto error;
519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100521 cur = cur->next;
522 }
David Horstmann8c176b42022-10-04 18:12:06 +0100523
524error:
David Horstmann8c176b42022-10-04 18:12:06 +0100525 /* Skip the first element as we did not allocate it */
526 allocated = head->next;
527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 while (allocated != NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100529 prev = allocated;
530 allocated = allocated->next;
531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 mbedtls_platform_zeroize(prev, sizeof(*prev));
533 mbedtls_free(prev);
David Horstmann8c176b42022-10-04 18:12:06 +0100534 }
535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 mbedtls_platform_zeroize(head, sizeof(*head));
David Horstmann8c176b42022-10-04 18:12:06 +0100537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539}
540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000542{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000543 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 for (; n > 0; --n) {
546 if ((**p < '0') || (**p > '9')) {
547 return MBEDTLS_ERR_X509_INVALID_DATE;
548 }
Janos Follath87c98072017-02-03 12:36:59 +0000549
Rich Evans7d5a55a2015-02-13 11:48:02 +0000550 *res *= 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000552 }
Janos Follath87c98072017-02-03 12:36:59 +0000553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000555}
556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100558{
559 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000560 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 CHECK_RANGE(0, 9999, t->year);
563 CHECK_RANGE(0, 23, t->hour);
564 CHECK_RANGE(0, 59, t->min);
565 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 switch (t->mon) {
Andres AG4b76aec2016-09-23 13:16:02 +0100568 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000569 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100570 break;
571 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000572 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100573 break;
574 case 2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 if ((!(t->year % 4) && t->year % 100) ||
576 !(t->year % 400)) {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000577 month_len = 29;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 } else {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000579 month_len = 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 }
Andres AG4b76aec2016-09-23 13:16:02 +0100581 break;
582 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100584 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100588}
589
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590/*
Janos Follath87c98072017-02-03 12:36:59 +0000591 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
592 * field.
593 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
595 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000596{
Janos Follath865b3eb2019-12-16 11:46:15 +0000597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000598
599 /*
600 * Minimum length is 10 or 12 depending on yearlen
601 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 if (len < yearlen + 8) {
603 return MBEDTLS_ERR_X509_INVALID_DATE;
604 }
Janos Follath87c98072017-02-03 12:36:59 +0000605 len -= yearlen + 8;
606
607 /*
608 * Parse year, month, day, hour, minute
609 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 CHECK(x509_parse_int(p, yearlen, &tm->year));
611 if (2 == yearlen) {
612 if (tm->year < 50) {
Hanno Becker61937d42017-04-26 15:01:23 +0100613 tm->year += 100;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 }
Janos Follath87c98072017-02-03 12:36:59 +0000615
Hanno Becker61937d42017-04-26 15:01:23 +0100616 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000617 }
618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 CHECK(x509_parse_int(p, 2, &tm->mon));
620 CHECK(x509_parse_int(p, 2, &tm->day));
621 CHECK(x509_parse_int(p, 2, &tm->hour));
622 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000623
624 /*
625 * Parse seconds if present
626 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100627 if (len >= 2) {
628 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000629 len -= 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 } else {
631 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000632 }
Janos Follath87c98072017-02-03 12:36:59 +0000633
634 /*
635 * Parse trailing 'Z' if present
636 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000638 (*p)++;
639 len--;
640 }
641
642 /*
643 * We should have parsed all characters at this point
644 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 if (0 != len) {
646 return MBEDTLS_ERR_X509_INVALID_DATE;
647 }
Janos Follath87c98072017-02-03 12:36:59 +0000648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000652}
653
654/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 * Time ::= CHOICE {
656 * utcTime UTCTime,
657 * generalTime GeneralizedTime }
658 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
660 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661{
Janos Follath865b3eb2019-12-16 11:46:15 +0000662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000663 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 unsigned char tag;
665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 if ((end - *p) < 1) {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
668 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 tag = **p;
672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000674 year_len = 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000676 year_len = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 } else {
678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
679 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
680 }
Janos Follath87c98072017-02-03 12:36:59 +0000681
682 (*p)++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 if (ret != 0) {
686 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
687 }
Janos Follath87c98072017-02-03 12:36:59 +0000688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690}
691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693{
Janos Follath865b3eb2019-12-16 11:46:15 +0000694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100696 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698 if ((end - *p) < 1) {
699 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
700 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
701 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Andres AG4bdbe092016-09-19 16:58:45 +0100703 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
706 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
707 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708
Andres AG4bdbe092016-09-19 16:58:45 +0100709 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 sig->len = len;
711 sig->p = *p;
712
713 *p += len;
714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716}
717
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100718/*
719 * Get signature algorithm from alg OID and optional parameters
720 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
722 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
723 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724{
Janos Follath865b3eb2019-12-16 11:46:15 +0000725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (*sig_opts != NULL) {
728 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
729 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100731 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
732 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
733 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
740 if (pss_opts == NULL) {
741 return MBEDTLS_ERR_X509_ALLOC_FAILED;
742 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
745 md_alg,
746 &pss_opts->mgf1_hash_id,
747 &pss_opts->expected_salt_len);
748 if (ret != 0) {
749 mbedtls_free(pss_opts);
750 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200751 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100752
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200753 *sig_opts = (void *) pss_opts;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100756 {
757 /* Make sure parameters are absent or NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
759 sig_params->len != 0) {
760 return MBEDTLS_ERR_X509_INVALID_ALG;
761 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100762 }
763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100764 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765}
766
767/*
768 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800769 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
772 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773{
Janos Follath865b3eb2019-12-16 11:46:15 +0000774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 size_t len;
776
Hanno Becker3cddba82019-02-11 14:33:36 +0000777 /* Extension structure use EXPLICIT tagging. That is, the actual
778 * `Extensions` structure is wrapped by a tag-length pair using
779 * the respective context-specific tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100780 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
781 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
782 if (ret != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
784 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
Hanno Becker6ccfb182019-02-12 11:52:10 +0000786 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
787 ext->p = *p;
788 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789
790 /*
791 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
794 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
796 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 if (end != *p + len) {
799 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
801 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804}
805
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806/*
807 * Store the name in printable form into buf; no more
808 * than size characters will be written
809 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811{
Janos Follath865b3eb2019-12-16 11:46:15 +0000812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100813 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000814 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820
821 name = dn;
822 p = buf;
823 n = size;
824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 while (name != NULL) {
826 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 name = name->next;
828 continue;
829 }
830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 if (name != dn) {
832 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200833 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200834 }
835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100838 if (ret == 0) {
839 ret = mbedtls_snprintf(p, n, "%s=", short_name);
840 } else {
841 ret = mbedtls_snprintf(p, n, "\?\?=");
842 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200843 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 for (i = 0, j = 0; i < name->val.len; i++, j++) {
846 if (j >= sizeof(s) - 1) {
847 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
848 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849
850 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100851 // Special characters requiring escaping, RFC 1779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 if (c && strchr(",=+<>#;\"\\", c)) {
853 if (j + 1 >= sizeof(s) - 1) {
854 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
855 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100856 s[j++] = '\\';
857 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 if (c < 32 || c >= 127) {
859 s[j] = '?';
860 } else {
861 s[j] = c;
862 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200863 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100864 s[j] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000867
868 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 name = name->next;
870 }
871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100872 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873}
874
875/*
876 * Store the serial in printable form into buf; no more
877 * than size characters will be written
878 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880{
Janos Follath865b3eb2019-12-16 11:46:15 +0000881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 size_t i, n, nr;
883 char *p;
884
885 p = buf;
886 n = size;
887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 ? serial->len : 28;
890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 for (i = 0; i < nr; i++) {
892 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 ret = mbedtls_snprintf(p, n, "%02X%s",
897 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200898 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 }
900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901 if (nr != serial->len) {
902 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200903 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 }
905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907}
908
909/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200910 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100911 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
913 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
914 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100915{
Janos Follath865b3eb2019-12-16 11:46:15 +0000916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100917 char *p = buf;
918 size_t n = size;
919 const char *desc = NULL;
920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
922 if (ret != 0) {
923 ret = mbedtls_snprintf(p, n, "???");
924 } else {
925 ret = mbedtls_snprintf(p, n, "%s", desc);
926 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200927 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 const mbedtls_pk_rsassa_pss_options *pss_opts;
932 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 md_info = mbedtls_md_info_from_type(md_alg);
937 mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
940 md_info ? mbedtls_md_get_name(md_info) : "???",
941 mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???",
942 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200943 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100944 }
945#else
946 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200947 ((void) md_alg);
948 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100952}
953
954/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 * Helper for writing "RSA key size", "EC key size", etc
956 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958{
959 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200960 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200964 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967}
968
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200969#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200970/*
971 * Set the time structure to the current time.
972 * Return 0 on success, non-zero on failure.
973 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100975{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000976 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100977 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200978 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100980 tt = mbedtls_time(NULL);
981 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100983 if (lt == NULL) {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200984 ret = -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 } else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200986 now->year = lt->tm_year + 1900;
987 now->mon = lt->tm_mon + 1;
988 now->day = lt->tm_mday;
989 now->hour = lt->tm_hour;
990 now->min = lt->tm_min;
991 now->sec = lt->tm_sec;
992 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100995}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100997/*
998 * Return 0 if before <= after, 1 otherwise
999 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001001{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 if (before->year > after->year) {
1003 return 1;
1004 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 if (before->year == after->year &&
1007 before->mon > after->mon) {
1008 return 1;
1009 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001012 before->mon == after->mon &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 before->day > after->day) {
1014 return 1;
1015 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001018 before->mon == after->mon &&
1019 before->day == after->day &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 before->hour > after->hour) {
1021 return 1;
1022 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001025 before->mon == after->mon &&
1026 before->day == after->day &&
1027 before->hour == after->hour &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001028 before->min > after->min) {
1029 return 1;
1030 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001033 before->mon == after->mon &&
1034 before->day == after->day &&
1035 before->hour == after->hour &&
1036 before->min == after->min &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 before->sec > after->sec) {
1038 return 1;
1039 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 if (x509_get_current_time(&now) != 0) {
1049 return 1;
1050 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001053}
1054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 if (x509_get_current_time(&now) != 0) {
1060 return 1;
1061 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001064}
1065
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001066#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069{
1070 ((void) to);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001075{
1076 ((void) from);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001078}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001079#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001082
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001083#include "mbedtls/x509_crt.h"
1084#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001085
1086/*
1087 * Checkup routine
1088 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089int mbedtls_x509_self_test(int verbose)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001090{
Junhwan Park39bdab72018-10-17 21:01:08 +09001091 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001092#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001093 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_x509_crt cacert;
1095 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097 if (verbose != 0) {
1098 mbedtls_printf(" X.509 certificate load: ");
1099 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101 mbedtls_x509_crt_init(&cacert);
1102 mbedtls_x509_crt_init(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001104 ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
1105 mbedtls_test_cli_crt_len);
1106 if (ret != 0) {
1107 if (verbose != 0) {
1108 mbedtls_printf("failed\n");
1109 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001110
Junhwan Park39bdab72018-10-17 21:01:08 +09001111 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001112 }
1113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt,
1115 mbedtls_test_ca_crt_len);
1116 if (ret != 0) {
1117 if (verbose != 0) {
1118 mbedtls_printf("failed\n");
1119 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001120
Junhwan Park39bdab72018-10-17 21:01:08 +09001121 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001122 }
1123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if (verbose != 0) {
1125 mbedtls_printf("passed\n X.509 signature verify: ");
1126 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL);
1129 if (ret != 0) {
1130 if (verbose != 0) {
1131 mbedtls_printf("failed\n");
1132 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001133
Junhwan Park39bdab72018-10-17 21:01:08 +09001134 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001135 }
1136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 if (verbose != 0) {
1138 mbedtls_printf("passed\n\n");
1139 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001140
Junhwan Park39bdab72018-10-17 21:01:08 +09001141cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 mbedtls_x509_crt_free(&cacert);
1143 mbedtls_x509_crt_free(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001144#else
1145 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001146#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 return ret;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001148}
1149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_X509_USE_C */