blob: 4233e53723b483681787f698548103381fa4d829 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020010 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
16 */
17
Gilles Peskinedb09ef62020-06-03 01:43:33 +020018#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_X509_USE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/x509.h"
23#include "mbedtls/asn1.h"
Janos Follath73c616b2019-12-18 15:07:04 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000026
Rich Evans36796df2015-02-12 18:27:14 +000027#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <string.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#endif
33
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Simon Butcherb5b6af22016-07-13 14:46:18 +010036#if defined(MBEDTLS_HAVE_TIME)
37#include "mbedtls/platform_time.h"
38#endif
Nicholas Wilson512b4ee2017-12-05 12:07:33 +000039#if defined(MBEDTLS_HAVE_TIME_DATE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010040#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041#include <time.h>
42#endif
43
Demi Marie Obenour0e207412022-12-04 04:24:22 -050044#define CHECK(code) \
45 do { \
46 if ((ret = (code)) != 0) { \
47 return ret; \
48 } \
49 } while (0)
50
Hanno Becker1eeca412018-10-15 12:01:35 +010051#define CHECK_RANGE(min, max, val) \
Demi Marie Obenour0e207412022-12-04 04:24:22 -050052 do { \
53 if ((val) < (min) || (val) > (max)) { \
54 return ret; \
Hanno Becker1eeca412018-10-15 12:01:35 +010055 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000057
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058/*
59 * CertificateSerialNumber ::= INTEGER
60 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
62 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063{
Janos Follath865b3eb2019-12-16 11:46:15 +000064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 if ((end - *p) < 1) {
67 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
68 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
69 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
72 **p != MBEDTLS_ASN1_INTEGER) {
73 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
74 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
75 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076
77 serial->tag = *(*p)++;
78
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
80 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
81 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082
83 serial->p = *p;
84 *p += serial->len;
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087}
88
89/* Get an algorithm identifier without parameters (eg for signatures)
90 *
91 * AlgorithmIdentifier ::= SEQUENCE {
92 * algorithm OBJECT IDENTIFIER,
93 * parameters ANY DEFINED BY algorithm OPTIONAL }
94 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
96 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097{
Janos Follath865b3eb2019-12-16 11:46:15 +000098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
101 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
102 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105}
106
107/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100108 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
111 mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100112{
Janos Follath865b3eb2019-12-16 11:46:15 +0000113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
116 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
117 }
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100120}
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100123/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100124 * HashAlgorithm ::= AlgorithmIdentifier
125 *
126 * AlgorithmIdentifier ::= SEQUENCE {
127 * algorithm OBJECT IDENTIFIER,
128 * parameters ANY DEFINED BY algorithm OPTIONAL }
129 *
130 * For HashAlgorithm, parameters MUST be NULL or absent.
131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132static 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 +0100133{
Janos Follath865b3eb2019-12-16 11:46:15 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100135 unsigned char *p;
136 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100138 size_t len;
139
140 /* Make sure we got a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
142 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
143 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
144 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100145
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400146 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100147 end = p + alg->len;
148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (p >= end) {
150 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
151 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
152 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100153
154 /* Parse md_oid */
155 md_oid.tag = *p;
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
158 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
159 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100160
161 md_oid.p = p;
162 p += md_oid.len;
163
164 /* Get md_alg from md_oid */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
166 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
167 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100168
169 /* Make sure params is absent of NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 if (p == end) {
171 return 0;
172 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
176 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if (p != end) {
179 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
180 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
181 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100184}
185
186/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100187 * RSASSA-PSS-params ::= SEQUENCE {
188 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
189 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
190 * saltLength [2] INTEGER DEFAULT 20,
191 * trailerField [3] INTEGER DEFAULT 1 }
192 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200193 *
194 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
195 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
Tom Cosgrove49f99bc2022-12-04 16:44:21 +0000196 * option. Enforce this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
199 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
200 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100201{
Janos Follath865b3eb2019-12-16 11:46:15 +0000202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100203 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100204 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100205 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100207
208 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 *md_alg = MBEDTLS_MD_SHA1;
210 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100211 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100212
213 /* Make sure params is a SEQUENCE and setup bounds */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
215 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
216 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
217 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100218
219 p = (unsigned char *) params->p;
220 end = p + params->len;
221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (p == end) {
223 return 0;
224 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100225
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100226 /*
227 * HashAlgorithm
228 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
230 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
231 0)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100232 end2 = p + len;
233
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100234 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
236 return ret;
237 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
240 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
241 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if (p != end2) {
244 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
245 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
246 }
247 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
248 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100249 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (p == end) {
252 return 0;
253 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100254
255 /*
256 * MaskGenAlgorithm
257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
259 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
260 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100261 end2 = p + len;
262
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100263 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
265 return ret;
266 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100267
268 /* Only MFG1 is recognised for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
270 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
271 MBEDTLS_ERR_OID_NOT_FOUND);
272 }
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100273
274 /* Parse HashAlgorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
276 return ret;
277 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 if (p != end2) {
280 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
281 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
282 }
283 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
284 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100285 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if (p == end) {
288 return 0;
289 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100290
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100291 /*
292 * salt_len
293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
295 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
296 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100297 end2 = p + len;
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
300 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
301 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 if (p != end2) {
304 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
305 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
306 }
307 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100309 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (p == end) {
312 return 0;
313 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100314
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100315 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200316 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100317 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
319 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
320 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200321 int trailer_field;
322
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100323 end2 = p + len;
324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
326 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
327 }
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (p != end2) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
331 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
332 }
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 if (trailer_field != 1) {
335 return MBEDTLS_ERR_X509_INVALID_ALG;
336 }
337 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
338 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100339 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 if (p != end) {
342 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
344 }
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100349
350/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 * AttributeTypeAndValue ::= SEQUENCE {
352 * type AttributeType,
353 * value AttributeValue }
354 *
355 * AttributeType ::= OBJECT IDENTIFIER
356 *
357 * AttributeValue ::= ANY DEFINED BY AttributeType
358 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359static int x509_get_attr_type_value(unsigned char **p,
360 const unsigned char *end,
361 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362{
Janos Follath865b3eb2019-12-16 11:46:15 +0000363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_x509_buf *oid;
366 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
369 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
370 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
371 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
Hanno Becker12f62fb2019-02-12 17:22:36 +0000373 end = *p + len;
374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 if ((end - *p) < 1) {
376 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
377 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
378 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380 oid = &cur->oid;
381 oid->tag = **p;
382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
384 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
385 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 oid->p = *p;
388 *p += oid->len;
389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 if ((end - *p) < 1) {
391 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
392 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
393 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
397 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 **p != MBEDTLS_ASN1_BIT_STRING) {
399 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
400 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
401 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 val = &cur->val;
404 val->tag = *(*p)++;
405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
408 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
410 val->p = *p;
411 *p += val->len;
412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 if (*p != end) {
414 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
415 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Hanno Becker12f62fb2019-02-12 17:22:36 +0000416 }
417
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 cur->next = NULL;
419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421}
422
423/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000424 * Name ::= CHOICE { -- only one possibility for now --
425 * rdnSequence RDNSequence }
426 *
427 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
428 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 * RelativeDistinguishedName ::=
430 * SET OF AttributeTypeAndValue
431 *
432 * AttributeTypeAndValue ::= SEQUENCE {
433 * type AttributeType,
434 * value AttributeValue }
435 *
436 * AttributeType ::= OBJECT IDENTIFIER
437 *
438 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200439 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000440 * The data structure is optimized for the common case where each RDN has only
441 * one element, which is represented as a list of AttributeTypeAndValue.
442 * For the general case we still use a flat list, but we mark elements of the
443 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 * this list, eg mbedtls_x509_dn_gets().
David Horstmann8c176b42022-10-04 18:12:06 +0100445 *
David Horstmann5ad5e162022-10-17 18:10:23 +0100446 * On success, this function may allocate a linked list starting at cur->next
David Horstmann8c176b42022-10-04 18:12:06 +0100447 * that must later be free'd by the caller using mbedtls_free(). In error
448 * cases, this function frees all allocated memory internally and the caller
449 * has no freeing responsibilities.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
452 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453{
Janos Follath865b3eb2019-12-16 11:46:15 +0000454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200455 size_t set_len;
456 const unsigned char *end_set;
David Horstmann8c176b42022-10-04 18:12:06 +0100457 mbedtls_x509_name *head = cur;
458 mbedtls_x509_name *prev, *allocated;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100460 /* don't use recursion, we'd risk stack overflow if not optimized */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100462 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000463 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100464 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
466 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
467 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
David Horstmann8c176b42022-10-04 18:12:06 +0100468 goto error;
469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100471 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 while (1) {
474 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
David Horstmann8c176b42022-10-04 18:12:06 +0100475 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 if (*p == end_set) {
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000479 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000481
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200482 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000483 cur->next_merged = 1;
484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100488 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
489 goto error;
490 }
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000491
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000492 cur = cur->next;
493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100495 /*
496 * continue until end of SEQUENCE is reached
497 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 if (*p == end) {
499 return 0;
500 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if (cur->next == NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100505 ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
506 goto error;
507 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100509 cur = cur->next;
510 }
David Horstmann8c176b42022-10-04 18:12:06 +0100511
512error:
David Horstmann8c176b42022-10-04 18:12:06 +0100513 /* Skip the first element as we did not allocate it */
514 allocated = head->next;
515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 while (allocated != NULL) {
David Horstmann8c176b42022-10-04 18:12:06 +0100517 prev = allocated;
518 allocated = allocated->next;
519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 mbedtls_platform_zeroize(prev, sizeof(*prev));
521 mbedtls_free(prev);
David Horstmann8c176b42022-10-04 18:12:06 +0100522 }
523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 mbedtls_platform_zeroize(head, sizeof(*head));
David Horstmann8c176b42022-10-04 18:12:06 +0100525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527}
528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000530{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000531 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 for (; n > 0; --n) {
534 if ((**p < '0') || (**p > '9')) {
535 return MBEDTLS_ERR_X509_INVALID_DATE;
536 }
Janos Follath87c98072017-02-03 12:36:59 +0000537
Rich Evans7d5a55a2015-02-13 11:48:02 +0000538 *res *= 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100539 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000540 }
Janos Follath87c98072017-02-03 12:36:59 +0000541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000543}
544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100546{
547 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000548 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 CHECK_RANGE(0, 9999, t->year);
551 CHECK_RANGE(0, 23, t->hour);
552 CHECK_RANGE(0, 59, t->min);
553 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 switch (t->mon) {
Andres AG4b76aec2016-09-23 13:16:02 +0100556 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000557 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100558 break;
559 case 4: case 6: case 9: case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000560 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100561 break;
562 case 2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if ((!(t->year % 4) && t->year % 100) ||
564 !(t->year % 400)) {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000565 month_len = 29;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 } else {
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000567 month_len = 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 }
Andres AG4b76aec2016-09-23 13:16:02 +0100569 break;
570 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100572 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100576}
577
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578/*
Janos Follath87c98072017-02-03 12:36:59 +0000579 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
580 * field.
581 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
583 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000584{
Janos Follath865b3eb2019-12-16 11:46:15 +0000585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000586
587 /*
588 * Minimum length is 10 or 12 depending on yearlen
589 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 if (len < yearlen + 8) {
591 return MBEDTLS_ERR_X509_INVALID_DATE;
592 }
Janos Follath87c98072017-02-03 12:36:59 +0000593 len -= yearlen + 8;
594
595 /*
596 * Parse year, month, day, hour, minute
597 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 CHECK(x509_parse_int(p, yearlen, &tm->year));
599 if (2 == yearlen) {
600 if (tm->year < 50) {
Hanno Becker61937d42017-04-26 15:01:23 +0100601 tm->year += 100;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 }
Janos Follath87c98072017-02-03 12:36:59 +0000603
Hanno Becker61937d42017-04-26 15:01:23 +0100604 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000605 }
606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 CHECK(x509_parse_int(p, 2, &tm->mon));
608 CHECK(x509_parse_int(p, 2, &tm->day));
609 CHECK(x509_parse_int(p, 2, &tm->hour));
610 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000611
612 /*
613 * Parse seconds if present
614 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 if (len >= 2) {
616 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000617 len -= 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 } else {
619 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000620 }
Janos Follath87c98072017-02-03 12:36:59 +0000621
622 /*
623 * Parse trailing 'Z' if present
624 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000626 (*p)++;
627 len--;
628 }
629
630 /*
631 * We should have parsed all characters at this point
632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (0 != len) {
634 return MBEDTLS_ERR_X509_INVALID_DATE;
635 }
Janos Follath87c98072017-02-03 12:36:59 +0000636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000640}
641
642/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 * Time ::= CHOICE {
644 * utcTime UTCTime,
645 * generalTime GeneralizedTime }
646 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
648 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649{
Janos Follath865b3eb2019-12-16 11:46:15 +0000650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000651 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 unsigned char tag;
653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 if ((end - *p) < 1) {
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
656 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
657 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 tag = **p;
660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 if (tag == MBEDTLS_ASN1_UTC_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000662 year_len = 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
Janos Follath87c98072017-02-03 12:36:59 +0000664 year_len = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 } else {
666 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
667 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
668 }
Janos Follath87c98072017-02-03 12:36:59 +0000669
670 (*p)++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 if (ret != 0) {
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
675 }
Janos Follath87c98072017-02-03 12:36:59 +0000676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678}
679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681{
Janos Follath865b3eb2019-12-16 11:46:15 +0000682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100684 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 if ((end - *p) < 1) {
687 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
688 MBEDTLS_ERR_ASN1_OUT_OF_DATA);
689 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
Andres AG4bdbe092016-09-19 16:58:45 +0100691 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
694 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
695 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Andres AG4bdbe092016-09-19 16:58:45 +0100697 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 sig->len = len;
699 sig->p = *p;
700
701 *p += len;
702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704}
705
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100706/*
707 * Get signature algorithm from alg OID and optional parameters
708 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
710 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
711 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712{
Janos Follath865b3eb2019-12-16 11:46:15 +0000713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 if (*sig_opts != NULL) {
716 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
717 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
720 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
721 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
728 if (pss_opts == NULL) {
729 return MBEDTLS_ERR_X509_ALLOC_FAILED;
730 }
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
733 md_alg,
734 &pss_opts->mgf1_hash_id,
735 &pss_opts->expected_salt_len);
736 if (ret != 0) {
737 mbedtls_free(pss_opts);
738 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200739 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100740
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200741 *sig_opts = (void *) pss_opts;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100744 {
745 /* Make sure parameters are absent or NULL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
747 sig_params->len != 0) {
748 return MBEDTLS_ERR_X509_INVALID_ALG;
749 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100750 }
751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200753}
754
755/*
756 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800757 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
760 mbedtls_x509_buf *ext, int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761{
Janos Follath865b3eb2019-12-16 11:46:15 +0000762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 size_t len;
764
Hanno Becker3cddba82019-02-11 14:33:36 +0000765 /* Extension structure use EXPLICIT tagging. That is, the actual
766 * `Extensions` structure is wrapped by a tag-length pair using
767 * the respective context-specific tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
769 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
770 if (ret != 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
772 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773
Hanno Becker6ccfb182019-02-12 11:52:10 +0000774 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
775 ext->p = *p;
776 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777
778 /*
779 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
783 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
784 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100786 if (end != *p + len) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
789 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792}
793
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794/*
795 * Store the name in printable form into buf; no more
796 * than size characters will be written
797 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799{
Janos Follath865b3eb2019-12-16 11:46:15 +0000800 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100801 size_t i, j, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000802 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
809 name = dn;
810 p = buf;
811 n = size;
812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 while (name != NULL) {
814 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 name = name->next;
816 continue;
817 }
818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 if (name != dn) {
820 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200821 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 }
823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 if (ret == 0) {
827 ret = mbedtls_snprintf(p, n, "%s=", short_name);
828 } else {
829 ret = mbedtls_snprintf(p, n, "\?\?=");
830 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200831 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 for (i = 0, j = 0; i < name->val.len; i++, j++) {
834 if (j >= sizeof(s) - 1) {
835 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
836 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
838 c = name->val.p[i];
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100839 // Special characters requiring escaping, RFC 1779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840 if (c && strchr(",=+<>#;\"\\", c)) {
841 if (j + 1 >= sizeof(s) - 1) {
842 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
843 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100844 s[j++] = '\\';
845 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 if (c < 32 || c >= 127) {
847 s[j] = '?';
848 } else {
849 s[j] = c;
850 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 }
Werner Lewis02c9d3b2022-05-20 12:48:46 +0100852 s[j] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200854 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000855
856 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 name = name->next;
858 }
859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100860 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861}
862
863/*
864 * Store the serial in printable form into buf; no more
865 * than size characters will be written
866 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868{
Janos Follath865b3eb2019-12-16 11:46:15 +0000869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 size_t i, n, nr;
871 char *p;
872
873 p = buf;
874 n = size;
875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100876 nr = (serial->len <= 32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 ? serial->len : 28;
878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 for (i = 0; i < nr; i++) {
880 if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 ret = mbedtls_snprintf(p, n, "%02X%s",
885 serial->p[i], (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200886 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 }
888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889 if (nr != serial->len) {
890 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200891 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 }
893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895}
896
897/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200898 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100899 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
901 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
902 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100903{
Janos Follath865b3eb2019-12-16 11:46:15 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100905 char *p = buf;
906 size_t n = size;
907 const char *desc = NULL;
908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
910 if (ret != 0) {
911 ret = mbedtls_snprintf(p, n, "???");
912 } else {
913 ret = mbedtls_snprintf(p, n, "%s", desc);
914 }
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200915 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 const mbedtls_pk_rsassa_pss_options *pss_opts;
920 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 md_info = mbedtls_md_info_from_type(md_alg);
925 mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
928 md_info ? mbedtls_md_get_name(md_info) : "???",
929 mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???",
930 (unsigned int) pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200931 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100932 }
933#else
934 ((void) pk_alg);
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200935 ((void) md_alg);
936 ((void) sig_opts);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 return (int) (size - n);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100940}
941
942/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 * Helper for writing "RSA key size", "EC key size", etc
944 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946{
947 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200948 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200952 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955}
956
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +0200957#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200958/*
959 * Set the time structure to the current time.
960 * Return 0 on success, non-zero on failure.
961 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100963{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000964 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100965 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200966 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 tt = mbedtls_time(NULL);
969 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 if (lt == NULL) {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200972 ret = -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 } else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200974 now->year = lt->tm_year + 1900;
975 now->mon = lt->tm_mon + 1;
976 now->day = lt->tm_mday;
977 now->hour = lt->tm_hour;
978 now->min = lt->tm_min;
979 now->sec = lt->tm_sec;
980 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100983}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100985/*
986 * Return 0 if before <= after, 1 otherwise
987 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100989{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100990 if (before->year > after->year) {
991 return 1;
992 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 if (before->year == after->year &&
995 before->mon > after->mon) {
996 return 1;
997 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001000 before->mon == after->mon &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 before->day > after->day) {
1002 return 1;
1003 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001006 before->mon == after->mon &&
1007 before->day == after->day &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 before->hour > after->hour) {
1009 return 1;
1010 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 if (before->year == after->year &&
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001013 before->mon == after->mon &&
1014 before->day == after->day &&
1015 before->hour == after->hour &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001016 before->min > after->min) {
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 &&
1024 before->min == after->min &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 before->sec > after->sec) {
1026 return 1;
1027 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 if (x509_get_current_time(&now) != 0) {
1037 return 1;
1038 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001041}
1042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 if (x509_get_current_time(&now) != 0) {
1048 return 1;
1049 }
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001052}
1053
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001054#else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057{
1058 ((void) to);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001062int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001063{
1064 ((void) from);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001065 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001066}
Manuel Pégourié-Gonnard60c793b2015-06-18 20:52:58 +02001067#endif /* MBEDTLS_HAVE_TIME_DATE */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_SELF_TEST)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001070
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001071#include "mbedtls/x509_crt.h"
1072#include "mbedtls/certs.h"
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001073
1074/*
1075 * Checkup routine
1076 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077int mbedtls_x509_self_test(int verbose)
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001078{
Junhwan Park39bdab72018-10-17 21:01:08 +09001079 int ret = 0;
Gilles Peskine750c3532017-05-05 18:56:30 +02001080#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001081 uint32_t flags;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_x509_crt cacert;
1083 mbedtls_x509_crt clicert;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 if (verbose != 0) {
1086 mbedtls_printf(" X.509 certificate load: ");
1087 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089 mbedtls_x509_crt_init(&cacert);
1090 mbedtls_x509_crt_init(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
1093 mbedtls_test_cli_crt_len);
1094 if (ret != 0) {
1095 if (verbose != 0) {
1096 mbedtls_printf("failed\n");
1097 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001098
Junhwan Park39bdab72018-10-17 21:01:08 +09001099 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001100 }
1101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001102 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt,
1103 mbedtls_test_ca_crt_len);
1104 if (ret != 0) {
1105 if (verbose != 0) {
1106 mbedtls_printf("failed\n");
1107 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001108
Junhwan Park39bdab72018-10-17 21:01:08 +09001109 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001110 }
1111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112 if (verbose != 0) {
1113 mbedtls_printf("passed\n X.509 signature verify: ");
1114 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL);
1117 if (ret != 0) {
1118 if (verbose != 0) {
1119 mbedtls_printf("failed\n");
1120 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001121
Junhwan Park39bdab72018-10-17 21:01:08 +09001122 goto cleanup;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001123 }
1124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 if (verbose != 0) {
1126 mbedtls_printf("passed\n\n");
1127 }
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001128
Junhwan Park39bdab72018-10-17 21:01:08 +09001129cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001130 mbedtls_x509_crt_free(&cacert);
1131 mbedtls_x509_crt_free(&clicert);
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001132#else
1133 ((void) verbose);
Simon Butcher3aa64052020-03-27 16:55:35 +00001134#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 return ret;
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001136}
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#endif /* MBEDTLS_SELF_TEST */
Paul Bakkere9e6ae32013-09-16 22:53:25 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#endif /* MBEDTLS_X509_USE_C */