blob: 05ae0defadf606bdce269692506cbd906eb65670 [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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020034# include "mbedtls/x509.h"
35# include "mbedtls/asn1.h"
36# include "mbedtls/error.h"
37# include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039# include <stdio.h>
40# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# if defined(MBEDTLS_PEM_PARSE_C)
43# include "mbedtls/pem.h"
44# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046# if defined(MBEDTLS_PLATFORM_C)
47# include "mbedtls/platform.h"
48# else
49# include <stdio.h>
50# include <stdlib.h>
51# define mbedtls_free free
52# define mbedtls_calloc calloc
53# define mbedtls_printf printf
54# define mbedtls_snprintf snprintf
55# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057# if defined(MBEDTLS_HAVE_TIME)
58# include "mbedtls/platform_time.h"
59# endif
60# if defined(MBEDTLS_HAVE_TIME_DATE)
61# include "mbedtls/platform_util.h"
62# include <time.h>
63# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065# define CHECK(code) \
66 if ((ret = (code)) != 0) { \
67 return ret; \
68 }
69# define CHECK_RANGE(min, max, val) \
70 do { \
71 if ((val) < (min) || (val) > (max)) { \
72 return ret; \
73 } \
74 } while (0)
Rich Evans7d5a55a2015-02-13 11:48:02 +000075
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076/*
77 * CertificateSerialNumber ::= INTEGER
78 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079int mbedtls_x509_get_serial(unsigned char **p,
80 const unsigned char *end,
81 mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082{
Janos Follath865b3eb2019-12-16 11:46:15 +000083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020085 if ((end - *p) < 1)
86 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
87 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
90 **p != MBEDTLS_ASN1_INTEGER)
91 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
92 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093
94 serial->tag = *(*p)++;
95
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0)
97 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098
99 serial->p = *p;
100 *p += serial->len;
101
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103}
104
105/* Get an algorithm identifier without parameters (eg for signatures)
106 *
107 * AlgorithmIdentifier ::= SEQUENCE {
108 * algorithm OBJECT IDENTIFIER,
109 * parameters ANY DEFINED BY algorithm OPTIONAL }
110 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111int mbedtls_x509_get_alg_null(unsigned char **p,
112 const unsigned char *end,
113 mbedtls_x509_buf *alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114{
Janos Follath865b3eb2019-12-16 11:46:15 +0000115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0)
118 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121}
122
123/*
Antonin Décimo36e89b52019-01-23 15:24:37 +0100124 * Parse an algorithm identifier with (optional) parameters
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100125 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126int mbedtls_x509_get_alg(unsigned char **p,
127 const unsigned char *end,
128 mbedtls_x509_buf *alg,
129 mbedtls_x509_buf *params)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100130{
Janos Follath865b3eb2019-12-16 11:46:15 +0000131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100132
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133 if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0)
134 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100135
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136 return 0;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100137}
138
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139# if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100140/*
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100141 * HashAlgorithm ::= AlgorithmIdentifier
142 *
143 * AlgorithmIdentifier ::= SEQUENCE {
144 * algorithm OBJECT IDENTIFIER,
145 * parameters ANY DEFINED BY algorithm OPTIONAL }
146 *
147 * For HashAlgorithm, parameters MUST be NULL or absent.
148 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149static int x509_get_hash_alg(const mbedtls_x509_buf *alg,
150 mbedtls_md_type_t *md_alg)
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100151{
Janos Follath865b3eb2019-12-16 11:46:15 +0000152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100153 unsigned char *p;
154 const unsigned char *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_x509_buf md_oid;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100156 size_t len;
157
158 /* Make sure we got a SEQUENCE and setup bounds */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE))
160 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
161 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100162
Andrzej Kurekfeaebc52020-07-16 04:37:41 -0400163 p = alg->p;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100164 end = p + alg->len;
165
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166 if (p >= end)
167 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
168 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100169
170 /* Parse md_oid */
171 md_oid.tag = *p;
172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173 if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) !=
174 0)
175 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100176
177 md_oid.p = p;
178 p += md_oid.len;
179
180 /* Get md_alg from md_oid */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0)
182 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100183
184 /* Make sure params is absent of NULL */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185 if (p == end)
186 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100187
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 ||
189 len != 0)
190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100191
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200192 if (p != end)
193 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
194 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196 return 0;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100197}
198
199/*
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100200 * RSASSA-PSS-params ::= SEQUENCE {
201 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
202 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
203 * saltLength [2] INTEGER DEFAULT 20,
204 * trailerField [3] INTEGER DEFAULT 1 }
205 * -- Note that the tags in this Sequence are explicit.
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200206 *
207 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
208 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
209 * option. Enfore this at parsing time.
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100210 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
212 mbedtls_md_type_t *md_alg,
213 mbedtls_md_type_t *mgf_md,
214 int *salt_len)
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100215{
Janos Follath865b3eb2019-12-16 11:46:15 +0000216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100217 unsigned char *p;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100218 const unsigned char *end, *end2;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100219 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_x509_buf alg_id, alg_params;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100221
222 /* First set everything to defaults */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 *md_alg = MBEDTLS_MD_SHA1;
224 *mgf_md = MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100225 *salt_len = 20;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100226
227 /* Make sure params is a SEQUENCE and setup bounds */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200228 if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE))
229 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
230 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100231
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200232 p = (unsigned char *)params->p;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100233 end = p + params->len;
234
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235 if (p == end)
236 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100237
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100238 /*
239 * HashAlgorithm
240 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
242 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
243 MBEDTLS_ASN1_CONSTRUCTED | 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) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247 if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0)
248 return ret;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100249
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250 if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0)
251 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 if (p != end2)
254 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
255 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
256 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
257 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100258
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259 if (p == end)
260 return 0;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100261
262 /*
263 * MaskGenAlgorithm
264 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
266 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
267 MBEDTLS_ASN1_CONSTRUCTED | 1)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100268 end2 = p + len;
269
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100270 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200271 if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0)
272 return ret;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100273
274 /* Only MFG1 is recognised for now */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200275 if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0)
276 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
277 MBEDTLS_ERR_OID_NOT_FOUND));
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100278
279 /* Parse HashAlgorithm */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200280 if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0)
281 return ret;
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100282
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200283 if (p != end2)
284 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
285 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
286 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
287 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100288
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289 if (p == end)
290 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100291
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100292 /*
293 * salt_len
294 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200295 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
296 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
297 MBEDTLS_ASN1_CONSTRUCTED | 2)) == 0) {
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100298 end2 = p + len;
299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0)
301 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303 if (p != end2)
304 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
305 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
306 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100308
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200309 if (p == end)
310 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100311
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100312 /*
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200313 * trailer_field (if present, must be 1)
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100314 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
316 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
317 MBEDTLS_ASN1_CONSTRUCTED | 3)) == 0) {
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200318 int trailer_field;
319
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100320 end2 = p + len;
321
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322 if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0)
323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnard9c9cf5b2014-01-24 14:15:20 +0100324
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325 if (p != end2)
326 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Manuel Pégourié-Gonnard78117d52014-05-31 17:08:16 +0200328
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329 if (trailer_field != 1)
330 return MBEDTLS_ERR_X509_INVALID_ALG;
331 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
332 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 if (p != end)
335 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
336 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100337
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200338 return 0;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100339}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200340# endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100341
342/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 * AttributeTypeAndValue ::= SEQUENCE {
344 * type AttributeType,
345 * value AttributeValue }
346 *
347 * AttributeType ::= OBJECT IDENTIFIER
348 *
349 * AttributeValue ::= ANY DEFINED BY AttributeType
350 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351static int x509_get_attr_type_value(unsigned char **p,
352 const unsigned char *end,
353 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354{
Janos Follath865b3eb2019-12-16 11:46:15 +0000355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_x509_buf *oid;
358 mbedtls_x509_buf *val;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 if ((ret = mbedtls_asn1_get_tag(
361 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
362 0)
363 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364
Hanno Becker12f62fb2019-02-12 17:22:36 +0000365 end = *p + len;
366
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367 if ((end - *p) < 1)
368 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
369 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
371 oid = &cur->oid;
372 oid->tag = **p;
373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0)
375 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
377 oid->p = *p;
378 *p += oid->len;
379
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380 if ((end - *p) < 1)
381 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
382 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200384 if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
385 **p != MBEDTLS_ASN1_T61_STRING &&
386 **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
387 **p != MBEDTLS_ASN1_IA5_STRING &&
388 **p != MBEDTLS_ASN1_UNIVERSAL_STRING && **p != MBEDTLS_ASN1_BIT_STRING)
389 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
390 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392 val = &cur->val;
393 val->tag = *(*p)++;
394
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200395 if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0)
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398 val->p = *p;
399 *p += val->len;
400
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 if (*p != end) {
402 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
403 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Hanno Becker12f62fb2019-02-12 17:22:36 +0000404 }
405
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 cur->next = NULL;
407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409}
410
411/*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000412 * Name ::= CHOICE { -- only one possibility for now --
413 * rdnSequence RDNSequence }
414 *
415 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
416 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 * RelativeDistinguishedName ::=
418 * SET OF AttributeTypeAndValue
419 *
420 * AttributeTypeAndValue ::= SEQUENCE {
421 * type AttributeType,
422 * value AttributeValue }
423 *
424 * AttributeType ::= OBJECT IDENTIFIER
425 *
426 * AttributeValue ::= ANY DEFINED BY AttributeType
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200427 *
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000428 * The data structure is optimized for the common case where each RDN has only
429 * one element, which is represented as a list of AttributeTypeAndValue.
430 * For the general case we still use a flat list, but we mark elements of the
431 * same set so that they are "merged" together in the functions that consume
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 * this list, eg mbedtls_x509_dn_gets().
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200434int mbedtls_x509_get_name(unsigned char **p,
435 const unsigned char *end,
436 mbedtls_x509_name *cur)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437{
Janos Follath865b3eb2019-12-16 11:46:15 +0000438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard5d861852014-10-17 12:41:41 +0200439 size_t set_len;
440 const unsigned char *end_set;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100442 /* don't use recursion, we'd risk stack overflow if not optimized */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443 while (1) {
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100444 /*
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000445 * parse SET
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100446 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447 if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
448 MBEDTLS_ASN1_CONSTRUCTED |
449 MBEDTLS_ASN1_SET)) != 0)
450 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452 end_set = *p + set_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200454 while (1) {
455 if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0)
456 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200458 if (*p == end_set)
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000459 break;
460
Manuel Pégourié-Gonnardeecb43c2015-05-12 12:56:41 +0200461 /* Mark this item as being no the only one in a set */
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000462 cur->next_merged = 1;
463
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200464 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000465
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200466 if (cur->next == NULL)
467 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000468
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000469 cur = cur->next;
470 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100472 /*
473 * continue until end of SEQUENCE is reached
474 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475 if (*p == end)
476 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200478 cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200480 if (cur->next == NULL)
481 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Manuel Pégourié-Gonnardd6814432014-11-12 01:25:31 +0100483 cur = cur->next;
484 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485}
486
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200487static int x509_parse_int(unsigned char **p, size_t n, int *res)
Janos Follath87c98072017-02-03 12:36:59 +0000488{
Rich Evans7d5a55a2015-02-13 11:48:02 +0000489 *res = 0;
Janos Follath87c98072017-02-03 12:36:59 +0000490
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200491 for (; n > 0; --n) {
492 if ((**p < '0') || (**p > '9'))
493 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000494
Rich Evans7d5a55a2015-02-13 11:48:02 +0000495 *res *= 10;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200496 *res += (*(*p)++ - '0');
Rich Evans7d5a55a2015-02-13 11:48:02 +0000497 }
Janos Follath87c98072017-02-03 12:36:59 +0000498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499 return 0;
Rich Evans7d5a55a2015-02-13 11:48:02 +0000500}
501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502static int x509_date_is_valid(const mbedtls_x509_time *t)
Andres AG4b76aec2016-09-23 13:16:02 +0100503{
504 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000505 int month_len;
Andres AG4b76aec2016-09-23 13:16:02 +0100506
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200507 CHECK_RANGE(0, 9999, t->year);
508 CHECK_RANGE(0, 23, t->hour);
509 CHECK_RANGE(0, 59, t->min);
510 CHECK_RANGE(0, 59, t->sec);
Andres AG4b76aec2016-09-23 13:16:02 +0100511
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200512 switch (t->mon) {
513 case 1:
514 case 3:
515 case 5:
516 case 7:
517 case 8:
518 case 10:
519 case 12:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000520 month_len = 31;
Andres AG4b76aec2016-09-23 13:16:02 +0100521 break;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522 case 4:
523 case 6:
524 case 9:
525 case 11:
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000526 month_len = 30;
Andres AG4b76aec2016-09-23 13:16:02 +0100527 break;
528 case 2:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200529 if ((!(t->year % 4) && t->year % 100) || !(t->year % 400))
Andres Amaya Garcia735b37e2016-11-21 15:38:02 +0000530 month_len = 29;
531 else
532 month_len = 28;
Andres AG4b76aec2016-09-23 13:16:02 +0100533 break;
534 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200535 return ret;
Andres AG4b76aec2016-09-23 13:16:02 +0100536 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200537 CHECK_RANGE(1, month_len, t->day);
Andres AG4b76aec2016-09-23 13:16:02 +0100538
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200539 return 0;
Andres AG4b76aec2016-09-23 13:16:02 +0100540}
541
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542/*
Janos Follath87c98072017-02-03 12:36:59 +0000543 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
544 * field.
545 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200546static int x509_parse_time(unsigned char **p,
547 size_t len,
548 size_t yearlen,
549 mbedtls_x509_time *tm)
Janos Follath87c98072017-02-03 12:36:59 +0000550{
Janos Follath865b3eb2019-12-16 11:46:15 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000552
553 /*
554 * Minimum length is 10 or 12 depending on yearlen
555 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200556 if (len < yearlen + 8)
557 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000558 len -= yearlen + 8;
559
560 /*
561 * Parse year, month, day, hour, minute
562 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200563 CHECK(x509_parse_int(p, yearlen, &tm->year));
564 if (2 == yearlen) {
565 if (tm->year < 50)
Hanno Becker61937d42017-04-26 15:01:23 +0100566 tm->year += 100;
Janos Follath87c98072017-02-03 12:36:59 +0000567
Hanno Becker61937d42017-04-26 15:01:23 +0100568 tm->year += 1900;
Janos Follath87c98072017-02-03 12:36:59 +0000569 }
570
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200571 CHECK(x509_parse_int(p, 2, &tm->mon));
572 CHECK(x509_parse_int(p, 2, &tm->day));
573 CHECK(x509_parse_int(p, 2, &tm->hour));
574 CHECK(x509_parse_int(p, 2, &tm->min));
Janos Follath87c98072017-02-03 12:36:59 +0000575
576 /*
577 * Parse seconds if present
578 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200579 if (len >= 2) {
580 CHECK(x509_parse_int(p, 2, &tm->sec));
Janos Follath87c98072017-02-03 12:36:59 +0000581 len -= 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200582 } else
583 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000584
585 /*
586 * Parse trailing 'Z' if present
587 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588 if (1 == len && 'Z' == **p) {
Janos Follath87c98072017-02-03 12:36:59 +0000589 (*p)++;
590 len--;
591 }
592
593 /*
594 * We should have parsed all characters at this point
595 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200596 if (0 != len)
597 return MBEDTLS_ERR_X509_INVALID_DATE;
Janos Follath87c98072017-02-03 12:36:59 +0000598
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200599 CHECK(x509_date_is_valid(tm));
Janos Follath87c98072017-02-03 12:36:59 +0000600
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200601 return 0;
Janos Follath87c98072017-02-03 12:36:59 +0000602}
603
604/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 * Time ::= CHOICE {
606 * utcTime UTCTime,
607 * generalTime GeneralizedTime }
608 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609int mbedtls_x509_get_time(unsigned char **p,
610 const unsigned char *end,
611 mbedtls_x509_time *tm)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612{
Janos Follath865b3eb2019-12-16 11:46:15 +0000613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Janos Follath87c98072017-02-03 12:36:59 +0000614 size_t len, year_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 unsigned char tag;
616
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200617 if ((end - *p) < 1)
618 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
619 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
621 tag = **p;
622
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200623 if (tag == MBEDTLS_ASN1_UTC_TIME)
Janos Follath87c98072017-02-03 12:36:59 +0000624 year_len = 2;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625 else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME)
Janos Follath87c98072017-02-03 12:36:59 +0000626 year_len = 4;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200628 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
629 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Janos Follath87c98072017-02-03 12:36:59 +0000630
631 (*p)++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200632 ret = mbedtls_asn1_get_len(p, end, &len);
Janos Follath87c98072017-02-03 12:36:59 +0000633
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200634 if (ret != 0)
635 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
Janos Follath87c98072017-02-03 12:36:59 +0000636
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200637 return x509_parse_time(p, len, year_len, tm);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638}
639
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200640int mbedtls_x509_get_sig(unsigned char **p,
641 const unsigned char *end,
642 mbedtls_x509_buf *sig)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643{
Janos Follath865b3eb2019-12-16 11:46:15 +0000644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 size_t len;
Andres AG4bdbe092016-09-19 16:58:45 +0100646 int tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200648 if ((end - *p) < 1)
649 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
650 MBEDTLS_ERR_ASN1_OUT_OF_DATA));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
Andres AG4bdbe092016-09-19 16:58:45 +0100652 tag_type = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200654 if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0)
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
Andres AG4bdbe092016-09-19 16:58:45 +0100657 sig->tag = tag_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 sig->len = len;
659 sig->p = *p;
660
661 *p += len;
662
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200663 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664}
665
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100666/*
667 * Get signature algorithm from alg OID and optional parameters
668 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200669int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid,
670 const mbedtls_x509_buf *sig_params,
671 mbedtls_md_type_t *md_alg,
672 mbedtls_pk_type_t *pk_alg,
673 void **sig_opts)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674{
Janos Follath865b3eb2019-12-16 11:46:15 +0000675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200677 if (*sig_opts != NULL)
678 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200679
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200680 if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0)
681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200683# if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
684 if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100686
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200687 pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
688 if (pss_opts == NULL)
689 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200690
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691 ret = mbedtls_x509_get_rsassa_pss_params(sig_params, md_alg,
692 &pss_opts->mgf1_hash_id,
693 &pss_opts->expected_salt_len);
694 if (ret != 0) {
695 mbedtls_free(pss_opts);
696 return ret;
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200697 }
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100698
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200699 *sig_opts = (void *)pss_opts;
700 } else
701# endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100702 {
703 /* Make sure parameters are absent or NULL */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200704 if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
705 sig_params->len != 0)
706 return MBEDTLS_ERR_X509_INVALID_ALG;
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100707 }
708
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200709 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710}
711
712/*
713 * X.509 Extensions (No parsing of extensions, pointer should
Brian J Murray1903fb32016-11-06 04:45:15 -0800714 * be either manually updated or extensions should be parsed!)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200716int mbedtls_x509_get_ext(unsigned char **p,
717 const unsigned char *end,
718 mbedtls_x509_buf *ext,
719 int tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720{
Janos Follath865b3eb2019-12-16 11:46:15 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 size_t len;
723
Hanno Becker3cddba82019-02-11 14:33:36 +0000724 /* Extension structure use EXPLICIT tagging. That is, the actual
725 * `Extensions` structure is wrapped by a tag-length pair using
726 * the respective context-specific tag. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200727 ret = mbedtls_asn1_get_tag(p, end, &ext->len,
728 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
729 MBEDTLS_ASN1_CONSTRUCTED | tag);
730 if (ret != 0)
731 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732
Hanno Becker6ccfb182019-02-12 11:52:10 +0000733 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200734 ext->p = *p;
735 end = *p + ext->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
737 /*
738 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200740 if ((ret = mbedtls_asn1_get_tag(
741 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
742 0)
743 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200745 if (end != *p + len)
746 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
747 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200749 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750}
751
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752/*
753 * Store the name in printable form into buf; no more
754 * than size characters will be written
755 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200756int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757{
Janos Follath865b3eb2019-12-16 11:46:15 +0000758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 size_t i, n;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000760 unsigned char c, merge = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 const mbedtls_x509_name *name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 const char *short_name = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200765 memset(s, 0, sizeof(s));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766
767 name = dn;
768 p = buf;
769 n = size;
770
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200771 while (name != NULL) {
772 if (!name->oid.p) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 name = name->next;
774 continue;
775 }
776
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200777 if (name != dn) {
778 ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200779 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780 }
781
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200782 ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200784 if (ret == 0)
785 ret = mbedtls_snprintf(p, n, "%s=", short_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200787 ret = mbedtls_snprintf(p, n, "\?\?=");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200788 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200790 for (i = 0; i < name->val.len; i++) {
791 if (i >= sizeof(s) - 1)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 break;
793
794 c = name->val.p[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200795 if (c < 32 || c >= 127)
796 s[i] = '?';
797 else
798 s[i] = c;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799 }
800 s[i] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200801 ret = mbedtls_snprintf(p, n, "%s", s);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200802 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +0000803
804 merge = name->next_merged;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200805 name = name->next;
806 }
807
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200808 return ((int)(size - n));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809}
810
811/*
812 * Store the serial in printable form into buf; no more
813 * than size characters will be written
814 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200815int mbedtls_x509_serial_gets(char *buf,
816 size_t size,
817 const mbedtls_x509_buf *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818{
Janos Follath865b3eb2019-12-16 11:46:15 +0000819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 size_t i, n, nr;
821 char *p;
822
823 p = buf;
824 n = size;
825
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200826 nr = (serial->len <= 32) ? serial->len : 28;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200828 for (i = 0; i < nr; i++) {
829 if (i == 0 && nr > 1 && serial->p[i] == 0x0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 continue;
831
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200832 ret = mbedtls_snprintf(p, n, "%02X%s", serial->p[i],
833 (i < nr - 1) ? ":" : "");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200834 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 }
836
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200837 if (nr != serial->len) {
838 ret = mbedtls_snprintf(p, n, "....");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200839 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 }
841
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200842 return ((int)(size - n));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843}
844
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200845# if !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846/*
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +0200847 * Helper for writing signature algorithms
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100848 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200849int mbedtls_x509_sig_alg_gets(char *buf,
850 size_t size,
851 const mbedtls_x509_buf *sig_oid,
852 mbedtls_pk_type_t pk_alg,
853 mbedtls_md_type_t md_alg,
854 const void *sig_opts)
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100855{
Janos Follath865b3eb2019-12-16 11:46:15 +0000856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100857 char *p = buf;
858 size_t n = size;
859 const char *desc = NULL;
860
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200861 ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
862 if (ret != 0)
863 ret = mbedtls_snprintf(p, n, "???");
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100864 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200865 ret = mbedtls_snprintf(p, n, "%s", desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100867
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200868# if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
869 if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 const mbedtls_pk_rsassa_pss_options *pss_opts;
871 const mbedtls_md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100872
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200873 pss_opts = (const mbedtls_pk_rsassa_pss_options *)sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100874
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200875 md_info = mbedtls_md_info_from_type(md_alg);
876 mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100877
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200878 ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
879 md_info ? mbedtls_md_get_name(md_info) : "???",
880 mgf_md_info ? mbedtls_md_get_name(mgf_md_info) :
881 "???",
882 (unsigned int)pss_opts->expected_salt_len);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200883 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100884 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200885# else
886 ((void)pk_alg);
887 ((void)md_alg);
888 ((void)sig_opts);
889# endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100890
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200891 return ((int)(size - n));
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100892}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200893# endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +0100894
895/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 * Helper for writing "RSA key size", "EC key size", etc
897 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200898int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899{
900 char *p = buf;
Manuel Pégourié-Gonnardfb317c52015-06-18 16:25:56 +0200901 size_t n = buf_size;
Janos Follath865b3eb2019-12-16 11:46:15 +0000902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200904 ret = mbedtls_snprintf(p, n, "%s key size", name);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +0200905 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200907 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908}
909
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200910# if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200911/*
912 * Set the time structure to the current time.
913 * Return 0 on success, non-zero on failure.
914 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200915static int x509_get_current_time(mbedtls_x509_time *now)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100916{
Nicholas Wilson512b4ee2017-12-05 12:07:33 +0000917 struct tm *lt, tm_buf;
SimonBd5800b72016-04-26 07:43:27 +0100918 mbedtls_time_t tt;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200919 int ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200921 tt = mbedtls_time(NULL);
922 lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200923
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200924 if (lt == NULL)
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200925 ret = -1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200926 else {
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200927 now->year = lt->tm_year + 1900;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200928 now->mon = lt->tm_mon + 1;
929 now->day = lt->tm_mday;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200930 now->hour = lt->tm_hour;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200931 now->min = lt->tm_min;
932 now->sec = lt->tm_sec;
Manuel Pégourié-Gonnard57e10d72015-06-22 18:59:21 +0200933 }
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200934
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200935 return ret;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100936}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100938/*
939 * Return 0 if before <= after, 1 otherwise
940 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200941static int x509_check_time(const mbedtls_x509_time *before,
942 const mbedtls_x509_time *after)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100943{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200944 if (before->year > after->year)
945 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200947 if (before->year == after->year && before->mon > after->mon)
948 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200950 if (before->year == after->year && before->mon == after->mon &&
951 before->day > after->day)
952 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200954 if (before->year == after->year && before->mon == after->mon &&
955 before->day == after->day && before->hour > after->hour)
956 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200958 if (before->year == after->year && before->mon == after->mon &&
959 before->day == after->day && before->hour == after->hour &&
960 before->min > after->min)
961 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200963 if (before->year == after->year && before->mon == after->mon &&
964 before->day == after->day && before->hour == after->hour &&
965 before->min == after->min && before->sec > after->sec)
966 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200968 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100970
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200971int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100974
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200975 if (x509_get_current_time(&now) != 0)
976 return 1;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100977
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200978 return x509_check_time(&now, to);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100979}
980
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200981int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_x509_time now;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100984
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200985 if (x509_get_current_time(&now) != 0)
986 return 1;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100987
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200988 return x509_check_time(from, &now);
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100989}
990
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200991# else /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100992
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200993int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200995 ((void)to);
996 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997}
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +0100998
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200999int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001000{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001001 ((void)from);
1002 return 0;
Manuel Pégourié-Gonnard6304f782014-03-10 12:26:11 +01001003}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001004# endif /* MBEDTLS_HAVE_TIME_DATE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_X509_USE_C */